iohidmanager 0.10.4

Safe Rust bindings for Apple's IOKit HID — enumerate, inspect, and subscribe to HID devices on macOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
//! High-level `IOKit` HID wrappers.

use core::ffi::{c_char, c_void};
use core::ptr;
use std::ffi::CString;
use std::fmt;

use crate::error::HidError;
use crate::ffi_impl as ffi;
use doom_fish_utils::panic_safe::catch_user_panic;

/// Device helpers wrapping `IOHIDDevice*` APIs.
pub mod device;
/// Element helpers wrapping `IOHIDElement*` APIs.
pub mod element;
/// Event-system keys and option constants mirrored from `IOHIDEventSystemKeys.h`.
pub mod event_system;
/// Property keys and option constants mirrored from `IOHIDKeys.h`.
pub mod keys;
/// Manager helpers wrapping `IOHIDManager*` callbacks and option flags.
pub mod manager;
/// Queue helpers wrapping `IOHIDQueue*` APIs.
pub mod queue;
/// Service plug-in UUIDs and COM-style interfaces mirrored from `IOHIDDevicePlugIn.h`.
pub mod service_plugin;
/// Transaction helpers wrapping `IOHIDTransaction*` APIs.
pub mod transaction;
/// Usage-page and usage constants mirrored from `IOHIDUsageTables.h`.
pub mod usage;
/// Value helpers wrapping `IOHIDValue*` APIs.
pub mod value;

pub use device::DeviceRemovalSubscription;
pub use manager::{
    HidManagerOptions, ManagerDeviceSubscription, ManagerReportSubscription,
    ManagerValueSubscription,
};
pub use queue::{HidQueue, HidQueueOptions, QueueValueAvailableSubscription};
pub use transaction::{HidTransaction, HidTransactionDirection, HidTransactionOptions};

/// Aliases `IOHIDDeviceGetValueOptions`.
pub type IOHIDDeviceGetValueOptions = ffi::IOHIDDeviceGetValueOptions;
/// Mirrors `kIOHIDDeviceGetValueWithUpdate`.
pub const DEVICE_GET_VALUE_WITH_UPDATE: IOHIDDeviceGetValueOptions =
    ffi::kIOHIDDeviceGetValueWithUpdate;
/// Mirrors `kIOHIDDeviceGetValueWithoutUpdate`.
pub const DEVICE_GET_VALUE_WITHOUT_UPDATE: IOHIDDeviceGetValueOptions =
    ffi::kIOHIDDeviceGetValueWithoutUpdate;

/// Aliases `IOHIDElementCommitDirection`.
pub type IOHIDElementCommitDirection = ffi::IOHIDElementCommitDirection;
/// Mirrors `kIOHIDElementCommitDirectionIn`.
pub const ELEMENT_COMMIT_DIRECTION_IN: IOHIDElementCommitDirection =
    ffi::kIOHIDElementCommitDirectionIn;
/// Mirrors `kIOHIDElementCommitDirectionOut`.
pub const ELEMENT_COMMIT_DIRECTION_OUT: IOHIDElementCommitDirection =
    ffi::kIOHIDElementCommitDirectionOut;

/// Aliases `IOHIDElementCookie`.
pub type IOHIDElementCookie = ffi::IOHIDElementCookie;
/// Aliases `IOHIDElementFlags`.
pub type IOHIDElementFlags = ffi::IOHIDElementFlags;
/// Mirrors `kIOHIDElementFlagsConstantMask`.
pub const ELEMENT_FLAGS_CONSTANT_MASK: IOHIDElementFlags = ffi::kIOHIDElementFlagsConstantMask;
/// Mirrors `kIOHIDElementFlagsVariableMask`.
pub const ELEMENT_FLAGS_VARIABLE_MASK: IOHIDElementFlags = ffi::kIOHIDElementFlagsVariableMask;
/// Mirrors `kIOHIDElementFlagsRelativeMask`.
pub const ELEMENT_FLAGS_RELATIVE_MASK: IOHIDElementFlags = ffi::kIOHIDElementFlagsRelativeMask;
/// Mirrors `kIOHIDElementFlagsWrapMask`.
pub const ELEMENT_FLAGS_WRAP_MASK: IOHIDElementFlags = ffi::kIOHIDElementFlagsWrapMask;
/// Mirrors `kIOHIDElementFlagsNonLinearMask`.
pub const ELEMENT_FLAGS_NON_LINEAR_MASK: IOHIDElementFlags = ffi::kIOHIDElementFlagsNonLinearMask;
/// Mirrors `kIOHIDElementFlagsNoPreferredMask`.
pub const ELEMENT_FLAGS_NO_PREFERRED_MASK: IOHIDElementFlags =
    ffi::kIOHIDElementFlagsNoPreferredMask;
/// Mirrors `kIOHIDElementFlagsNullStateMask`.
pub const ELEMENT_FLAGS_NULL_STATE_MASK: IOHIDElementFlags = ffi::kIOHIDElementFlagsNullStateMask;
/// Mirrors `kIOHIDElementFlagsVolativeMask`.
pub const ELEMENT_FLAGS_VOLATIVE_MASK: IOHIDElementFlags = ffi::kIOHIDElementFlagsVolativeMask;
/// Mirrors `kIOHIDElementFlagsBufferedByteMask`.
pub const ELEMENT_FLAGS_BUFFERED_BYTE_MASK: IOHIDElementFlags =
    ffi::kIOHIDElementFlagsBufferedByteMask;

/// Aliases `IOHIDValueOptions`.
pub type IOHIDValueOptions = ffi::IOHIDValueOptions;
/// Mirrors `kIOHIDValueOptionsFlagRelativeSimple`.
pub const VALUE_OPTIONS_FLAG_RELATIVE_SIMPLE: IOHIDValueOptions =
    ffi::kIOHIDValueOptionsFlagRelativeSimple;
/// Mirrors `kIOHIDValueOptionsFlagPrevious`.
pub const VALUE_OPTIONS_FLAG_PREVIOUS: IOHIDValueOptions = ffi::kIOHIDValueOptionsFlagPrevious;
/// Mirrors `kIOHIDValueOptionsUpdateElementValues`.
pub const VALUE_OPTIONS_UPDATE_ELEMENT_VALUES: IOHIDValueOptions =
    ffi::kIOHIDValueOptionsUpdateElementValues;

/// Aliases `IOHIDCompletionAction`.
pub type IOHIDCompletionAction = ffi::IOHIDCompletionAction;
/// Aliases `IOHIDCompletion`.
pub type IOHIDCompletion = ffi::IOHIDCompletion;
/// Aliases `HIDReportCommandType`.
pub type HIDReportCommandType = ffi::HIDReportCommandType;
/// Mirrors `kIOHIDReportCommandSetReport`.
pub const REPORT_COMMAND_SET_REPORT: HIDReportCommandType = ffi::kIOHIDReportCommandSetReport;
/// Mirrors `kIOHIDReportCommandGetReport`.
pub const REPORT_COMMAND_GET_REPORT: HIDReportCommandType = ffi::kIOHIDReportCommandGetReport;
/// Mirrors `kIOHIDReportOptionNotInterrupt`.
pub const REPORT_OPTION_NOT_INTERRUPT: ffi::IOOptionBits = ffi::kIOHIDReportOptionNotInterrupt;
/// Mirrors `kIOHIDReportOptionVariableSize`.
pub const REPORT_OPTION_VARIABLE_SIZE: ffi::IOOptionBits = ffi::kIOHIDReportOptionVariableSize;
/// Mirrors `kIOHIDDeviceDefaultAsyncRequestTimeout`.
pub const DEVICE_DEFAULT_ASYNC_REQUEST_TIMEOUT: u64 = ffi::kIOHIDDeviceDefaultAsyncRequestTimeout;
/// Mirrors `kIOHIDDeviceMinAsyncRequestTimeout`.
pub const DEVICE_MIN_ASYNC_REQUEST_TIMEOUT: u64 = ffi::kIOHIDDeviceMinAsyncRequestTimeout;
/// Mirrors `kIOHIDDeviceMaxAsyncRequestTimeout`.
pub const DEVICE_MAX_ASYNC_REQUEST_TIMEOUT: u64 = ffi::kIOHIDDeviceMaxAsyncRequestTimeout;

/// Aliases `IOHIDManagerOptions`.
pub type IOHIDManagerOptions = ffi::IOHIDManagerOptions;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
/// Describes a `(usage_page, usage)` pair used by `IOHIDManagerSetDeviceMatching`.
pub enum HidUsage {
    /// Matches `kHIDUsage_GD_Keyboard` on `kHIDPage_GenericDesktop`.
    Keyboard,
    /// Matches `kHIDUsage_GD_Mouse` on `kHIDPage_GenericDesktop`.
    Mouse,
    /// Matches `kHIDUsage_GD_Joystick` on `kHIDPage_GenericDesktop`.
    Joystick,
    /// Matches `kHIDUsage_GD_GamePad` on `kHIDPage_GenericDesktop`.
    GamePad,
    /// Matches an arbitrary `(usage_page, usage)` pair.
    Custom(u32, u32),
}

impl HidUsage {
    #[must_use]
    /// Returns the `(usage_page, usage)` pair consumed by `IOHIDManagerSetDeviceMatching`.
    pub const fn as_pair(self) -> (u32, u32) {
        match self {
            Self::Keyboard => (ffi::kHIDPage_GenericDesktop, ffi::kHIDUsage_GD_Keyboard),
            Self::Mouse => (ffi::kHIDPage_GenericDesktop, ffi::kHIDUsage_GD_Mouse),
            Self::Joystick => (ffi::kHIDPage_GenericDesktop, ffi::kHIDUsage_GD_Joystick),
            Self::GamePad => (ffi::kHIDPage_GenericDesktop, ffi::kHIDUsage_GD_GamePad),
            Self::Custom(page, usage) => (page, usage),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum MatchValue {
    U32(u32),
    String(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
/// Builds matching dictionaries for `IOHIDManagerSetDeviceMatching`.
pub struct DeviceMatch {
    entries: Vec<(String, MatchValue)>,
}

impl DeviceMatch {
    #[must_use]
    /// Creates an empty device-matching dictionary for `IOHIDManagerSetDeviceMatching`.
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    /// Creates a device-matching dictionary from a `HidUsage` pair.
    pub fn usage(usage: HidUsage) -> Self {
        Self::new().with_usage(usage)
    }

    #[must_use]
    /// Adds `kIOHIDDeviceUsagePageKey` and `kIOHIDDeviceUsageKey`.
    pub fn with_usage(mut self, usage: HidUsage) -> Self {
        let (page, usage) = usage.as_pair();
        self.insert(ffi::kIOHIDDeviceUsagePageKey, MatchValue::U32(page));
        self.insert(ffi::kIOHIDDeviceUsageKey, MatchValue::U32(usage));
        self
    }

    #[must_use]
    /// Adds `kIOHIDVendorIDKey`.
    pub fn with_vendor_id(mut self, vendor_id: u32) -> Self {
        self.insert(ffi::kIOHIDVendorIDKey, MatchValue::U32(vendor_id));
        self
    }

    #[must_use]
    /// Adds `kIOHIDProductIDKey`.
    pub fn with_product_id(mut self, product_id: u32) -> Self {
        self.insert(ffi::kIOHIDProductIDKey, MatchValue::U32(product_id));
        self
    }

    #[must_use]
    /// Adds `kIOHIDLocationIDKey`.
    pub fn with_location_id(mut self, location_id: u32) -> Self {
        self.insert(ffi::kIOHIDLocationIDKey, MatchValue::U32(location_id));
        self
    }

    #[must_use]
    /// Adds `kIOHIDTransportKey`.
    pub fn with_transport(mut self, transport: impl Into<String>) -> Self {
        self.insert(
            ffi::kIOHIDTransportKey,
            MatchValue::String(transport.into()),
        );
        self
    }

    #[must_use]
    /// Adds an arbitrary integer entry to the matching dictionary.
    pub fn with_u32(mut self, key: &str, value: u32) -> Self {
        self.insert(key, MatchValue::U32(value));
        self
    }

    #[must_use]
    /// Adds an arbitrary string entry to the matching dictionary.
    pub fn with_string(mut self, key: &str, value: impl Into<String>) -> Self {
        self.insert(key, MatchValue::String(value.into()));
        self
    }

    #[must_use]
    /// Returns `true` when no entries will be passed to `IOHIDManagerSetDeviceMatching`.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    fn insert(&mut self, key: &str, value: MatchValue) {
        if let Some((_, existing)) = self
            .entries
            .iter_mut()
            .find(|(existing_key, _)| existing_key == key)
        {
            *existing = value;
            return;
        }
        self.entries.push((key.to_owned(), value));
    }

    fn to_cf_dictionary(&self) -> Result<ffi::CFDictionaryRef, HidError> {
        build_cf_dictionary(&self.entries)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
/// Builds matching dictionaries for `IOHIDDeviceSetInputValueMatching` and `IOHIDManagerSetInputValueMatching`.
pub struct ElementMatch {
    entries: Vec<(String, MatchValue)>,
}

impl ElementMatch {
    #[must_use]
    /// Creates an empty element-matching dictionary for `IOHIDDeviceSetInputValueMatching`.
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    /// Creates an element-matching dictionary from a `HidUsage` pair.
    pub fn usage(usage: HidUsage) -> Self {
        Self::new().with_usage(usage)
    }

    #[must_use]
    /// Adds `kIOHIDElementUsagePageKey` and `kIOHIDElementUsageKey`.
    pub fn with_usage(mut self, usage: HidUsage) -> Self {
        let (page, usage) = usage.as_pair();
        self.insert(ffi::kIOHIDElementUsagePageKey, MatchValue::U32(page));
        self.insert(ffi::kIOHIDElementUsageKey, MatchValue::U32(usage));
        self
    }

    #[must_use]
    /// Adds `kIOHIDElementUsagePageKey`.
    pub fn with_usage_page(mut self, usage_page: u32) -> Self {
        self.insert(ffi::kIOHIDElementUsagePageKey, MatchValue::U32(usage_page));
        self
    }

    #[must_use]
    /// Adds `kIOHIDElementUsageKey`.
    pub fn with_usage_id(mut self, usage: u32) -> Self {
        self.insert(ffi::kIOHIDElementUsageKey, MatchValue::U32(usage));
        self
    }

    #[must_use]
    /// Adds `kIOHIDElementUsageMinKey`.
    pub fn with_usage_min(mut self, usage_min: u32) -> Self {
        self.insert(ffi::kIOHIDElementUsageMinKey, MatchValue::U32(usage_min));
        self
    }

    #[must_use]
    /// Adds `kIOHIDElementUsageMaxKey`.
    pub fn with_usage_max(mut self, usage_max: u32) -> Self {
        self.insert(ffi::kIOHIDElementUsageMaxKey, MatchValue::U32(usage_max));
        self
    }

    #[must_use]
    /// Adds `kIOHIDElementCookieMinKey`.
    pub fn with_cookie_min(mut self, cookie_min: u32) -> Self {
        self.insert(ffi::kIOHIDElementCookieMinKey, MatchValue::U32(cookie_min));
        self
    }

    #[must_use]
    /// Adds `kIOHIDElementCookieMaxKey`.
    pub fn with_cookie_max(mut self, cookie_max: u32) -> Self {
        self.insert(ffi::kIOHIDElementCookieMaxKey, MatchValue::U32(cookie_max));
        self
    }

    #[must_use]
    /// Adds `kIOHIDElementReportIDKey`.
    pub fn with_report_id(mut self, report_id: u32) -> Self {
        self.insert(ffi::kIOHIDElementReportIDKey, MatchValue::U32(report_id));
        self
    }

    #[must_use]
    /// Adds an arbitrary integer entry to the element-matching dictionary.
    pub fn with_u32(mut self, key: &str, value: u32) -> Self {
        self.insert(key, MatchValue::U32(value));
        self
    }

    #[must_use]
    /// Adds an arbitrary string entry to the element-matching dictionary.
    pub fn with_string(mut self, key: &str, value: impl Into<String>) -> Self {
        self.insert(key, MatchValue::String(value.into()));
        self
    }

    #[must_use]
    /// Returns `true` when no entries will be passed to `IOHIDDeviceSetInputValueMatching`.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    fn insert(&mut self, key: &str, value: MatchValue) {
        if let Some((_, existing)) = self
            .entries
            .iter_mut()
            .find(|(existing_key, _)| existing_key == key)
        {
            *existing = value;
            return;
        }
        self.entries.push((key.to_owned(), value));
    }

    fn to_cf_dictionary(&self) -> Result<ffi::CFDictionaryRef, HidError> {
        build_cf_dictionary(&self.entries)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
/// Wraps `IOHIDReportType`.
pub enum HidReportType {
    /// Mirrors `kIOHIDReportTypeInput`.
    Input,
    /// Mirrors `kIOHIDReportTypeOutput`.
    Output,
    /// Mirrors `kIOHIDReportTypeFeature`.
    Feature,
    /// Preserves an unrecognized raw `IOHIDReportType` value.
    Unknown(u32),
}

impl HidReportType {
    #[must_use]
    /// Returns the raw `IOHIDReportType` constant.
    pub const fn as_raw(self) -> u32 {
        match self {
            Self::Input => ffi::kIOHIDReportTypeInput,
            Self::Output => ffi::kIOHIDReportTypeOutput,
            Self::Feature => ffi::kIOHIDReportTypeFeature,
            Self::Unknown(raw) => raw,
        }
    }

    #[must_use]
    /// Wraps a raw `IOHIDReportType` constant.
    pub const fn from_raw(raw: u32) -> Self {
        match raw {
            ffi::kIOHIDReportTypeInput => Self::Input,
            ffi::kIOHIDReportTypeOutput => Self::Output,
            ffi::kIOHIDReportTypeFeature => Self::Feature,
            other => Self::Unknown(other),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Wraps the scale constants consumed by `IOHIDValueGetScaledValue`.
pub enum HidValueScale {
    /// Mirrors `kIOHIDValueScaleTypeCalibrated`.
    Calibrated,
    /// Mirrors `kIOHIDValueScaleTypePhysical`.
    Physical,
    /// Mirrors `kIOHIDValueScaleTypeExponent`.
    Exponent,
}

impl HidValueScale {
    #[must_use]
    /// Returns the raw scale constant consumed by `IOHIDValueGetScaledValue`.
    pub const fn as_raw(self) -> u32 {
        match self {
            Self::Calibrated => ffi::kIOHIDValueScaleTypeCalibrated,
            Self::Physical => ffi::kIOHIDValueScaleTypePhysical,
            Self::Exponent => ffi::kIOHIDValueScaleTypeExponent,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
/// Wraps the raw element-type values returned by `IOHIDElementGetType`.
pub enum HidElementType {
    /// Represents an input-misc element from `IOHIDElementGetType`.
    InputMisc,
    /// Represents an input-button element from `IOHIDElementGetType`.
    InputButton,
    /// Represents an input-axis element from `IOHIDElementGetType`.
    InputAxis,
    /// Represents an input-scan-code element from `IOHIDElementGetType`.
    InputScanCodes,
    /// Represents an input-null element from `IOHIDElementGetType`.
    InputNull,
    /// Represents an output element from `IOHIDElementGetType`.
    Output,
    /// Represents a feature element from `IOHIDElementGetType`.
    Feature,
    /// Represents a collection element from `IOHIDElementGetType`.
    Collection,
    /// Preserves an unrecognized raw element-type value.
    Unknown(i32),
}

impl HidElementType {
    #[must_use]
    /// Wraps the raw value returned by `IOHIDElementGetType`.
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            1 => Self::InputMisc,
            2 => Self::InputButton,
            3 => Self::InputAxis,
            4 => Self::InputScanCodes,
            5 => Self::InputNull,
            129 => Self::Output,
            257 => Self::Feature,
            513 => Self::Collection,
            other => Self::Unknown(other),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
/// Wraps the raw collection-type values returned by `IOHIDElementGetCollectionType`.
pub enum HidCollectionType {
    /// Represents a physical collection from `IOHIDElementGetCollectionType`.
    Physical,
    /// Represents an application collection from `IOHIDElementGetCollectionType`.
    Application,
    /// Represents a logical collection from `IOHIDElementGetCollectionType`.
    Logical,
    /// Represents a report collection from `IOHIDElementGetCollectionType`.
    Report,
    /// Represents a named-array collection from `IOHIDElementGetCollectionType`.
    NamedArray,
    /// Represents a usage-switch collection from `IOHIDElementGetCollectionType`.
    UsageSwitch,
    /// Represents a usage-modifier collection from `IOHIDElementGetCollectionType`.
    UsageModifier,
    /// Preserves an unrecognized raw collection-type value.
    Unknown(i32),
}

impl HidCollectionType {
    #[must_use]
    /// Wraps the raw value returned by `IOHIDElementGetCollectionType`.
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Physical,
            1 => Self::Application,
            2 => Self::Logical,
            3 => Self::Report,
            4 => Self::NamedArray,
            5 => Self::UsageSwitch,
            6 => Self::UsageModifier,
            other => Self::Unknown(other),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Carries one callback payload from `IOHIDDeviceRegisterInputReportCallback` or `IOHIDManagerRegisterInputReportCallback`.
pub struct HidInputReport {
    /// Report kind from `IOHIDReportType`.
    pub report_type: HidReportType,
    /// Report ID supplied by the callback.
    pub report_id: u32,
    /// Report bytes supplied by the callback.
    pub bytes: Vec<u8>,
    /// Timestamp from `IOHID*RegisterInputReportWithTimeStampCallback`, or `0` when unavailable.
    pub timestamp: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Collects common `IOHIDDeviceGetProperty` values.
pub struct HidDeviceInfo {
    /// Value read from `kIOHIDVendorIDKey`.
    pub vendor_id: Option<u32>,
    /// Value read from `kIOHIDProductIDKey`.
    pub product_id: Option<u32>,
    /// Value read from `kIOHIDProductKey`.
    pub product: Option<String>,
    /// Value read from `kIOHIDManufacturerKey`.
    pub manufacturer: Option<String>,
    /// Value read from `kIOHIDSerialNumberKey`.
    pub serial_number: Option<String>,
    /// Value read from `kIOHIDTransportKey`.
    pub transport: Option<String>,
    /// Value read from `kIOHIDPrimaryUsagePageKey` or `kIOHIDDeviceUsagePageKey`.
    pub usage_page: Option<u32>,
    /// Value read from `kIOHIDPrimaryUsageKey` or `kIOHIDDeviceUsageKey`.
    pub usage: Option<u32>,
    /// Value read from `kIOHIDLocationIDKey`.
    pub location_id: Option<u32>,
}

/// Owns an `IOHIDManagerRef` returned by `IOHIDManagerCreate`.
pub struct HidManager {
    raw: ffi::IOHIDManagerRef,
}

unsafe impl Send for HidManager {}
unsafe impl Sync for HidManager {}

impl Drop for HidManager {
    fn drop(&mut self) {
        if !self.raw.is_null() {
            unsafe {
                let _ = ffi::IOHIDManagerClose(self.raw, ffi::kIOHIDOptionsTypeNone);
                ffi::CFRelease(self.raw);
            }
            self.raw = ptr::null();
        }
    }
}

#[allow(clippy::missing_errors_doc)]
impl HidManager {
    #[must_use]
    /// Wraps `IOHIDManagerGetTypeID`.
    pub fn type_id() -> ffi::CFTypeID {
        unsafe { ffi::IOHIDManagerGetTypeID() }
    }

    /// Wraps `IOHIDManagerCreate` with `kIOHIDManagerOptionNone`.
    pub fn new() -> Result<Self, HidError> {
        Self::with_options(HidManagerOptions::NONE)
    }

    /// Wraps `IOHIDManagerSetDeviceMatching`.
    pub fn set_device_matching(&self, usage: Option<HidUsage>) -> Result<(), HidError> {
        usage.map_or_else(
            || {
                unsafe { ffi::IOHIDManagerSetDeviceMatching(self.raw, ptr::null()) };
                Ok(())
            },
            |usage| self.set_device_matching_dict(Some(&DeviceMatch::usage(usage))),
        )
    }

    /// Wraps `IOHIDManagerSetDeviceMatching` with a custom dictionary.
    pub fn set_device_matching_dict(&self, matching: Option<&DeviceMatch>) -> Result<(), HidError> {
        match matching {
            None => unsafe {
                ffi::IOHIDManagerSetDeviceMatching(self.raw, ptr::null());
                Ok(())
            },
            Some(matching) => {
                let dict = matching.to_cf_dictionary()?;
                unsafe {
                    ffi::IOHIDManagerSetDeviceMatching(self.raw, dict);
                    ffi::CFRelease(dict.cast());
                }
                Ok(())
            }
        }
    }

    /// Wraps `IOHIDManagerSetDeviceMatchingMultiple`.
    pub fn set_device_matching_multiple(&self, matches: &[DeviceMatch]) -> Result<(), HidError> {
        if matches.is_empty() {
            unsafe { ffi::IOHIDManagerSetDeviceMatchingMultiple(self.raw, ptr::null()) };
            return Ok(());
        }
        let array = build_cf_dictionary_array(matches, DeviceMatch::to_cf_dictionary)?;
        unsafe {
            ffi::IOHIDManagerSetDeviceMatchingMultiple(self.raw, array);
            ffi::CFRelease(array.cast());
        }
        Ok(())
    }

    /// Wraps `IOHIDManagerSaveToPropertyDomain`.
    pub fn save_to_property_domain(
        &self,
        application_id: &str,
        user_name: &str,
        host_name: &str,
        options: IOHIDManagerOptions,
    ) -> Result<(), HidError> {
        let app = make_cfstring(application_id)?;
        let user = match make_cfstring(user_name) {
            Ok(user) => user,
            Err(err) => {
                unsafe { ffi::CFRelease(app.cast()) };
                return Err(err);
            }
        };
        let host = match make_cfstring(host_name) {
            Ok(host) => host,
            Err(err) => {
                unsafe {
                    ffi::CFRelease(app.cast());
                    ffi::CFRelease(user.cast());
                }
                return Err(err);
            }
        };
        unsafe {
            ffi::IOHIDManagerSaveToPropertyDomain(self.raw, app, user, host, options);
            ffi::CFRelease(app.cast());
            ffi::CFRelease(user.cast());
            ffi::CFRelease(host.cast());
        }
        Ok(())
    }

    #[must_use]
    /// Reads `IOHIDManagerGetProperty` as a `u32`.
    pub fn property_u32(&self, key: &str) -> Option<u32> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_u32(ffi::IOHIDManagerGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    #[must_use]
    /// Reads `IOHIDManagerGetProperty` as a `String`.
    pub fn property_string(&self, key: &str) -> Option<String> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_string(ffi::IOHIDManagerGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    #[must_use]
    /// Reads `IOHIDManagerGetProperty` as `CFData` bytes.
    pub fn property_data(&self, key: &str) -> Option<Vec<u8>> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_data(ffi::IOHIDManagerGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    /// Wraps `IOHIDManagerSetProperty` for numeric values.
    pub fn set_property_u32(&self, key: &str, value: u32) -> Result<(), HidError> {
        let value_cf = make_cfnumber_u32(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDManagerSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDManagerSetProperty"))
        }
    }

    /// Wraps `IOHIDManagerSetProperty` for string values.
    pub fn set_property_string(&self, key: &str, value: &str) -> Result<(), HidError> {
        let value_cf = make_cfstring(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDManagerSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDManagerSetProperty"))
        }
    }

    /// Wraps `IOHIDManagerSetProperty` for `CFData` bytes.
    pub fn set_property_data(&self, key: &str, value: &[u8]) -> Result<(), HidError> {
        let value_cf = make_cfdata(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDManagerSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDManagerSetProperty"))
        }
    }

    #[must_use]
    /// Wraps `IOHIDManagerCopyDevices` and snapshots each device's properties.
    pub fn devices(&self) -> Vec<HidDeviceInfo> {
        let set = unsafe { ffi::IOHIDManagerCopyDevices(self.raw) };
        if set.is_null() {
            return Vec::new();
        }
        let count = unsafe { ffi::CFSetGetCount(set) };
        let count = usize::try_from(count).unwrap_or(0);
        let mut buffer = vec![ptr::null(); count];
        unsafe { ffi::CFSetGetValues(set, buffer.as_mut_ptr()) };
        let devices = buffer
            .into_iter()
            .filter(|device| !device.is_null())
            .map(|device| read_device_info(device.cast()))
            .collect();
        unsafe { ffi::CFRelease(set.cast()) };
        devices
    }

    #[must_use]
    /// Wraps `IOHIDManagerCopyDevices` and retains live `IOHIDDeviceRef` handles.
    pub fn live_devices(&self) -> Vec<HidDevice> {
        let set = unsafe { ffi::IOHIDManagerCopyDevices(self.raw) };
        if set.is_null() {
            return Vec::new();
        }
        let count = unsafe { ffi::CFSetGetCount(set) };
        let count = usize::try_from(count).unwrap_or(0);
        let mut buffer = vec![ptr::null(); count];
        unsafe { ffi::CFSetGetValues(set, buffer.as_mut_ptr()) };
        let devices = buffer
            .into_iter()
            .filter(|device| !device.is_null())
            .map(|device| {
                unsafe { ffi::CFRetain(device) };
                HidDevice { raw: device.cast() }
            })
            .collect();
        unsafe { ffi::CFRelease(set.cast()) };
        devices
    }

    #[must_use]
    /// Mirrors `IOHIDManagerRef`.
    pub const fn as_ptr(&self) -> ffi::IOHIDManagerRef {
        self.raw
    }
}

/// Owns an `IOHIDDeviceRef` returned by `IOHIDManagerCopyDevices` or `IOHIDDeviceCreate`.
pub struct HidDevice {
    raw: ffi::IOHIDDeviceRef,
}

unsafe impl Send for HidDevice {}
unsafe impl Sync for HidDevice {}

impl Clone for HidDevice {
    fn clone(&self) -> Self {
        unsafe { ffi::CFRetain(self.raw) };
        Self { raw: self.raw }
    }
}

impl Drop for HidDevice {
    fn drop(&mut self) {
        if !self.raw.is_null() {
            unsafe { ffi::CFRelease(self.raw) };
            self.raw = ptr::null();
        }
    }
}

#[allow(clippy::missing_errors_doc, clippy::type_complexity)]
impl HidDevice {
    #[must_use]
    /// Wraps `IOHIDDeviceGetTypeID`.
    pub fn type_id() -> ffi::CFTypeID {
        unsafe { ffi::IOHIDDeviceGetTypeID() }
    }

    #[must_use]
    /// Snapshots common `IOHIDDeviceGetProperty` values.
    pub fn info(&self) -> HidDeviceInfo {
        read_device_info(self.raw)
    }

    #[must_use]
    /// Wraps `IOHIDDeviceGetService`.
    pub fn service(&self) -> ffi::io_service_t {
        unsafe { ffi::IOHIDDeviceGetService(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDDeviceConformsTo`.
    pub fn conforms_to(&self, usage: HidUsage) -> bool {
        let (page, usage) = usage.as_pair();
        unsafe { ffi::IOHIDDeviceConformsTo(self.raw, page, usage) }
    }

    #[must_use]
    /// Reads `IOHIDDeviceGetProperty` as a `u32`.
    pub fn property_u32(&self, key: &str) -> Option<u32> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_u32(ffi::IOHIDDeviceGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    #[must_use]
    /// Reads `IOHIDDeviceGetProperty` as a `String`.
    pub fn property_string(&self, key: &str) -> Option<String> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_string(ffi::IOHIDDeviceGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    #[must_use]
    /// Reads `IOHIDDeviceGetProperty` as `CFData` bytes.
    pub fn property_data(&self, key: &str) -> Option<Vec<u8>> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_data(ffi::IOHIDDeviceGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    #[must_use]
    /// Reads the `kIOHIDReportDescriptorKey` property.
    pub fn report_descriptor(&self) -> Option<Vec<u8>> {
        self.property_data(ffi::kIOHIDReportDescriptorKey)
    }

    /// Wraps `IOHIDDeviceSetProperty` for numeric values.
    pub fn set_property_u32(&self, key: &str, value: u32) -> Result<(), HidError> {
        let value_cf = make_cfnumber_u32(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDDeviceSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDDeviceSetProperty"))
        }
    }

    /// Wraps `IOHIDDeviceSetProperty` for string values.
    pub fn set_property_string(&self, key: &str, value: &str) -> Result<(), HidError> {
        let value_cf = make_cfstring(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDDeviceSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDDeviceSetProperty"))
        }
    }

    /// Wraps `IOHIDDeviceSetProperty` for `CFData` bytes.
    pub fn set_property_data(&self, key: &str, value: &[u8]) -> Result<(), HidError> {
        let value_cf = make_cfdata(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDDeviceSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDDeviceSetProperty"))
        }
    }

    /// Wraps `IOHIDDeviceSetInputValueMatching`.
    pub fn set_input_value_matching(
        &self,
        matching: Option<&ElementMatch>,
    ) -> Result<(), HidError> {
        match matching {
            None => unsafe {
                ffi::IOHIDDeviceSetInputValueMatching(self.raw, ptr::null());
                Ok(())
            },
            Some(matching) => {
                let dict = matching.to_cf_dictionary()?;
                unsafe {
                    ffi::IOHIDDeviceSetInputValueMatching(self.raw, dict);
                    ffi::CFRelease(dict.cast());
                }
                Ok(())
            }
        }
    }

    /// Wraps `IOHIDDeviceSetInputValueMatchingMultiple`.
    pub fn set_input_value_matching_multiple(
        &self,
        matches: &[ElementMatch],
    ) -> Result<(), HidError> {
        if matches.is_empty() {
            unsafe { ffi::IOHIDDeviceSetInputValueMatchingMultiple(self.raw, ptr::null()) };
            return Ok(());
        }
        let array = build_cf_dictionary_array(matches, ElementMatch::to_cf_dictionary)?;
        unsafe {
            ffi::IOHIDDeviceSetInputValueMatchingMultiple(self.raw, array);
            ffi::CFRelease(array.cast());
        }
        Ok(())
    }

    /// Wraps `IOHIDDeviceRegisterInputReportCallback`.
    pub fn on_input_report<F>(
        &self,
        max_report_length: usize,
        callback: F,
    ) -> Result<ReportSubscription, HidError>
    where
        F: Fn(&[u8]) + Send + Sync + 'static,
    {
        if max_report_length == 0 {
            return Err(HidError::InvalidArgument(
                "max_report_length must be greater than zero".to_owned(),
            ));
        }
        let run_loop = open_and_schedule_device(self.raw)?;
        let report_length = ffi::CFIndex::try_from(max_report_length).map_err(|_| {
            HidError::InvalidArgument("max_report_length does not fit CFIndex".to_owned())
        })?;
        let buffer = vec![0_u8; max_report_length].into_boxed_slice();
        let buffer_ptr = Box::into_raw(buffer);
        let callback: Box<dyn Fn(&[u8]) + Send + Sync + 'static> = Box::new(callback);
        let callback_ptr = Box::into_raw(Box::new(callback));
        let context = Box::new(ReportContext {
            buffer_len: max_report_length,
            callback: callback_ptr,
        });
        let context_ptr = Box::into_raw(context);
        unsafe {
            ffi::IOHIDDeviceRegisterInputReportCallback(
                self.raw,
                (*buffer_ptr).as_mut_ptr(),
                report_length,
                Some(report_trampoline),
                context_ptr.cast(),
            );
            ffi::CFRetain(self.raw);
        }
        Ok(ReportSubscription {
            device: self.raw,
            run_loop,
            buffer_ptr,
            buffer_len: max_report_length,
            context: context_ptr,
        })
    }

    /// Wraps `IOHIDDeviceRegisterInputReportWithTimeStampCallback`.
    pub fn on_input_report_with_timestamp<F>(
        &self,
        max_report_length: usize,
        callback: F,
    ) -> Result<TimestampedReportSubscription, HidError>
    where
        F: Fn(HidInputReport) + Send + Sync + 'static,
    {
        if max_report_length == 0 {
            return Err(HidError::InvalidArgument(
                "max_report_length must be greater than zero".to_owned(),
            ));
        }
        let run_loop = open_and_schedule_device(self.raw)?;
        let report_length = ffi::CFIndex::try_from(max_report_length).map_err(|_| {
            HidError::InvalidArgument("max_report_length does not fit CFIndex".to_owned())
        })?;
        let buffer = vec![0_u8; max_report_length].into_boxed_slice();
        let buffer_ptr = Box::into_raw(buffer);
        let callback: Box<dyn Fn(HidInputReport) + Send + Sync + 'static> = Box::new(callback);
        let callback_ptr = Box::into_raw(Box::new(callback));
        let context = Box::new(TimestampedReportContext {
            buffer_len: max_report_length,
            callback: callback_ptr,
        });
        let context_ptr = Box::into_raw(context);
        unsafe {
            ffi::IOHIDDeviceRegisterInputReportWithTimeStampCallback(
                self.raw,
                (*buffer_ptr).as_mut_ptr(),
                report_length,
                Some(timestamped_report_trampoline),
                context_ptr.cast(),
            );
            ffi::CFRetain(self.raw);
        }
        Ok(TimestampedReportSubscription {
            device: self.raw,
            run_loop,
            buffer_ptr,
            buffer_len: max_report_length,
            context: context_ptr,
        })
    }

    /// Wraps `IOHIDDeviceRegisterInputValueCallback`.
    pub fn on_input_value<F>(&self, callback: F) -> Result<ValueSubscription, HidError>
    where
        F: Fn(&HidValue) + Send + Sync + 'static,
    {
        let run_loop = open_and_schedule_device(self.raw)?;
        let callback: Box<dyn Fn(&HidValue) + Send + Sync + 'static> = Box::new(callback);
        let callback_ptr = Box::into_raw(Box::new(callback));
        let context = Box::new(ValueContext {
            callback: callback_ptr,
        });
        let context_ptr = Box::into_raw(context);
        unsafe {
            ffi::IOHIDDeviceRegisterInputValueCallback(
                self.raw,
                Some(value_trampoline),
                context_ptr.cast(),
            );
            ffi::CFRetain(self.raw);
        }
        Ok(ValueSubscription {
            device: self.raw,
            run_loop,
            context: context_ptr,
        })
    }

    #[must_use]
    /// Wraps `IOHIDDeviceCopyMatchingElements` with no filter.
    pub fn elements(&self) -> Vec<HidElement> {
        self.matching_elements(None)
    }

    #[must_use]
    /// Wraps `IOHIDDeviceCopyMatchingElements`.
    pub fn matching_elements(&self, matching: Option<&ElementMatch>) -> Vec<HidElement> {
        let dict = matching.and_then(|matching| matching.to_cf_dictionary().ok());
        let elements = unsafe {
            ffi::IOHIDDeviceCopyMatchingElements(
                self.raw,
                dict.unwrap_or(ptr::null()),
                ffi::kIOHIDOptionsTypeNone,
            )
        };
        if let Some(dict) = dict {
            unsafe { ffi::CFRelease(dict.cast()) };
        }
        elements_from_array(elements, true)
    }

    /// Wraps `IOHIDDeviceGetValue`.
    pub fn get_value(&self, element: &HidElement) -> Result<HidValue, HidError> {
        let mut value = ptr::null();
        let status = unsafe { ffi::IOHIDDeviceGetValue(self.raw, element.raw, &mut value) };
        if status != ffi::kIOReturnSuccess {
            return Err(HidError::IoReturn("IOHIDDeviceGetValue", status));
        }
        clone_value_ref(value).ok_or(HidError::OperationFailed("IOHIDDeviceGetValue"))
    }

    /// Wraps `IOHIDDeviceGetValueWithOptions`.
    pub fn get_value_with_options(
        &self,
        element: &HidElement,
        options: IOHIDDeviceGetValueOptions,
    ) -> Result<HidValue, HidError> {
        let mut value = ptr::null();
        let status = unsafe {
            ffi::IOHIDDeviceGetValueWithOptions(self.raw, element.raw, &mut value, options)
        };
        if status != ffi::kIOReturnSuccess {
            return Err(HidError::IoReturn("IOHIDDeviceGetValueWithOptions", status));
        }
        clone_value_ref(value).ok_or(HidError::OperationFailed("IOHIDDeviceGetValueWithOptions"))
    }

    /// Wraps `IOHIDDeviceSetValue`.
    pub fn set_value(&self, element: &HidElement, value: &HidValue) -> Result<(), HidError> {
        let status = unsafe { ffi::IOHIDDeviceSetValue(self.raw, element.raw, value.raw) };
        if status == ffi::kIOReturnSuccess {
            Ok(())
        } else {
            Err(HidError::IoReturn("IOHIDDeviceSetValue", status))
        }
    }

    /// Wraps `IOHIDDeviceGetReport`.
    pub fn get_report(
        &self,
        report_type: HidReportType,
        report_id: u32,
        max_report_length: usize,
    ) -> Result<Vec<u8>, HidError> {
        if max_report_length == 0 {
            return Err(HidError::InvalidArgument(
                "max_report_length must be greater than zero".to_owned(),
            ));
        }
        let report_id = ffi::CFIndex::from(report_id);
        let mut buffer = vec![0_u8; max_report_length];
        let mut report_length = ffi::CFIndex::try_from(max_report_length).map_err(|_| {
            HidError::InvalidArgument("max_report_length does not fit CFIndex".to_owned())
        })?;
        let status = unsafe {
            ffi::IOHIDDeviceGetReport(
                self.raw,
                report_type.as_raw(),
                report_id,
                buffer.as_mut_ptr(),
                &mut report_length,
            )
        };
        if status != ffi::kIOReturnSuccess {
            return Err(HidError::IoReturn("IOHIDDeviceGetReport", status));
        }
        buffer.truncate(usize::try_from(report_length).unwrap_or(0));
        Ok(buffer)
    }

    /// Wraps `IOHIDDeviceSetReport`.
    pub fn set_report(
        &self,
        report_type: HidReportType,
        report_id: u32,
        report: &[u8],
    ) -> Result<(), HidError> {
        let report_id = ffi::CFIndex::from(report_id);
        let report_length = ffi::CFIndex::try_from(report.len()).map_err(|_| {
            HidError::InvalidArgument("report length does not fit CFIndex".to_owned())
        })?;
        let status = unsafe {
            ffi::IOHIDDeviceSetReport(
                self.raw,
                report_type.as_raw(),
                report_id,
                report.as_ptr(),
                report_length,
            )
        };
        if status == ffi::kIOReturnSuccess {
            Ok(())
        } else {
            Err(HidError::IoReturn("IOHIDDeviceSetReport", status))
        }
    }

    #[must_use]
    /// Mirrors `IOHIDDeviceRef`.
    pub const fn as_ptr(&self) -> ffi::IOHIDDeviceRef {
        self.raw
    }

    /// Create a `HidDevice` from a raw ref that we do **not** already own;
    /// `CFRetain` is called so the resulting wrapper releases on drop.
    #[cfg(feature = "async")]
    pub(crate) fn from_raw_retained(raw: ffi::IOHIDDeviceRef) -> Self {
        debug_assert!(!raw.is_null());
        unsafe { ffi::CFRetain(raw) };
        Self { raw }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Wraps an `IOHIDElementRef`.
pub struct HidElement {
    raw: ffi::IOHIDElementRef,
}

unsafe impl Send for HidElement {}
unsafe impl Sync for HidElement {}

#[allow(clippy::missing_errors_doc)]
impl HidElement {
    #[must_use]
    /// Wraps `IOHIDElementGetTypeID`.
    pub fn type_id() -> ffi::CFTypeID {
        unsafe { ffi::IOHIDElementGetTypeID() }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetType`.
    pub fn element_type(&self) -> i32 {
        unsafe { ffi::IOHIDElementGetType(self.raw) }
    }

    #[must_use]
    /// Classifies the raw value from `IOHIDElementGetType`.
    pub fn element_kind(&self) -> HidElementType {
        HidElementType::from_raw(self.element_type())
    }

    #[must_use]
    /// Wraps `IOHIDElementGetCollectionType` for collection elements.
    pub fn collection_type(&self) -> Option<HidCollectionType> {
        if self.element_kind() != HidElementType::Collection {
            return None;
        }
        Some(HidCollectionType::from_raw(unsafe {
            ffi::IOHIDElementGetCollectionType(self.raw)
        }))
    }

    #[must_use]
    /// Wraps `IOHIDElementGetUsage`.
    pub fn usage(&self) -> u32 {
        unsafe { ffi::IOHIDElementGetUsage(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetUsagePage`.
    pub fn usage_page(&self) -> u32 {
        unsafe { ffi::IOHIDElementGetUsagePage(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetCookie`.
    pub fn cookie(&self) -> IOHIDElementCookie {
        unsafe { ffi::IOHIDElementGetCookie(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementIsVirtual`.
    pub fn is_virtual(&self) -> bool {
        unsafe { ffi::IOHIDElementIsVirtual(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementIsRelative`.
    pub fn is_relative(&self) -> bool {
        unsafe { ffi::IOHIDElementIsRelative(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementIsWrapping`.
    pub fn is_wrapping(&self) -> bool {
        unsafe { ffi::IOHIDElementIsWrapping(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementIsArray`.
    pub fn is_array(&self) -> bool {
        unsafe { ffi::IOHIDElementIsArray(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementIsNonLinear`.
    pub fn is_non_linear(&self) -> bool {
        unsafe { ffi::IOHIDElementIsNonLinear(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementHasPreferredState`.
    pub fn has_preferred_state(&self) -> bool {
        unsafe { ffi::IOHIDElementHasPreferredState(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementHasNullState`.
    pub fn has_null_state(&self) -> bool {
        unsafe { ffi::IOHIDElementHasNullState(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetName`.
    pub fn name(&self) -> Option<String> {
        read_cf_string(unsafe { ffi::IOHIDElementGetName(self.raw).cast() })
    }

    #[must_use]
    /// Wraps `IOHIDElementGetReportID`.
    pub fn report_id(&self) -> u32 {
        unsafe { ffi::IOHIDElementGetReportID(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetReportSize`.
    pub fn report_size_bits(&self) -> u32 {
        unsafe { ffi::IOHIDElementGetReportSize(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetReportCount`.
    pub fn report_count(&self) -> u32 {
        unsafe { ffi::IOHIDElementGetReportCount(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetUnit`.
    pub fn unit(&self) -> u32 {
        unsafe { ffi::IOHIDElementGetUnit(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetUnitExponent`.
    pub fn unit_exponent(&self) -> u32 {
        unsafe { ffi::IOHIDElementGetUnitExponent(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetLogicalMin`.
    pub fn logical_min(&self) -> i64 {
        unsafe { ffi::IOHIDElementGetLogicalMin(self.raw) as i64 }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetLogicalMax`.
    pub fn logical_max(&self) -> i64 {
        unsafe { ffi::IOHIDElementGetLogicalMax(self.raw) as i64 }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetPhysicalMin`.
    pub fn physical_min(&self) -> i64 {
        unsafe { ffi::IOHIDElementGetPhysicalMin(self.raw) as i64 }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetPhysicalMax`.
    pub fn physical_max(&self) -> i64 {
        unsafe { ffi::IOHIDElementGetPhysicalMax(self.raw) as i64 }
    }

    #[must_use]
    /// Wraps `IOHIDElementGetParent`.
    pub fn parent(&self) -> Option<Self> {
        let parent = unsafe { ffi::IOHIDElementGetParent(self.raw) };
        (!parent.is_null()).then_some(Self { raw: parent })
    }

    #[must_use]
    /// Wraps `IOHIDElementGetChildren`.
    pub fn children(&self) -> Vec<Self> {
        elements_from_array(unsafe { ffi::IOHIDElementGetChildren(self.raw) }, false)
    }

    #[must_use]
    /// Wraps `IOHIDElementCopyAttached`.
    pub fn attached(&self) -> Vec<Self> {
        elements_from_array(unsafe { ffi::IOHIDElementCopyAttached(self.raw) }, true)
    }

    #[must_use]
    /// Wraps `IOHIDElementGetDevice`.
    pub fn device(&self) -> Option<HidDevice> {
        let device = unsafe { ffi::IOHIDElementGetDevice(self.raw) };
        if device.is_null() {
            None
        } else {
            unsafe { ffi::CFRetain(device) };
            Some(HidDevice { raw: device })
        }
    }

    #[must_use]
    /// Reads `IOHIDElementGetProperty` as a `u32`.
    pub fn property_u32(&self, key: &str) -> Option<u32> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_u32(ffi::IOHIDElementGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    #[must_use]
    /// Reads `IOHIDElementGetProperty` as a `String`.
    pub fn property_string(&self, key: &str) -> Option<String> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_string(ffi::IOHIDElementGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    #[must_use]
    /// Reads `IOHIDElementGetProperty` as `CFData` bytes.
    pub fn property_data(&self, key: &str) -> Option<Vec<u8>> {
        with_cfstring(key, |key_cf| unsafe {
            read_cf_data(ffi::IOHIDElementGetProperty(self.raw, key_cf))
        })
        .ok()
        .flatten()
    }

    /// Wraps `IOHIDElementSetProperty` for numeric values.
    pub fn set_property_u32(&self, key: &str, value: u32) -> Result<(), HidError> {
        let value_cf = make_cfnumber_u32(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDElementSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDElementSetProperty"))
        }
    }

    /// Wraps `IOHIDElementSetProperty` for string values.
    pub fn set_property_string(&self, key: &str, value: &str) -> Result<(), HidError> {
        let value_cf = make_cfstring(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDElementSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDElementSetProperty"))
        }
    }

    /// Wraps `IOHIDElementSetProperty` for `CFData` bytes.
    pub fn set_property_data(&self, key: &str, value: &[u8]) -> Result<(), HidError> {
        let value_cf = make_cfdata(value)?;
        let ok = with_cfstring(key, |key_cf| unsafe {
            ffi::IOHIDElementSetProperty(self.raw, key_cf, value_cf.cast())
        })?;
        unsafe { ffi::CFRelease(value_cf.cast()) };
        if ok {
            Ok(())
        } else {
            Err(HidError::OperationFailed("IOHIDElementSetProperty"))
        }
    }

    #[must_use]
    /// Mirrors `IOHIDElementRef`.
    pub const fn as_ptr(&self) -> ffi::IOHIDElementRef {
        self.raw
    }
}

/// Owns an `IOHIDValueRef`.
pub struct HidValue {
    raw: ffi::IOHIDValueRef,
}

unsafe impl Send for HidValue {}
unsafe impl Sync for HidValue {}

impl Clone for HidValue {
    fn clone(&self) -> Self {
        unsafe { ffi::CFRetain(self.raw) };
        Self { raw: self.raw }
    }
}

impl fmt::Debug for HidValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HidValue")
            .field("timestamp", &self.timestamp())
            .field("len", &self.len())
            .field("integer_value", &self.integer_value())
            .field("usage_page", &self.element().usage_page())
            .field("usage", &self.element().usage())
            .finish()
    }
}

impl Drop for HidValue {
    fn drop(&mut self) {
        if !self.raw.is_null() {
            unsafe { ffi::CFRelease(self.raw) };
            self.raw = ptr::null();
        }
    }
}

#[allow(clippy::missing_errors_doc)]
impl HidValue {
    #[must_use]
    /// Wraps `IOHIDValueGetTypeID`.
    pub fn type_id() -> ffi::CFTypeID {
        unsafe { ffi::IOHIDValueGetTypeID() }
    }

    /// Wraps `IOHIDValueCreateWithIntegerValue`.
    pub fn from_integer(element: HidElement, timestamp: u64, value: i64) -> Result<Self, HidError> {
        let value = ffi::CFIndex::try_from(value).map_err(|_| {
            HidError::InvalidArgument("integer value does not fit CFIndex".to_owned())
        })?;
        let raw = unsafe {
            ffi::IOHIDValueCreateWithIntegerValue(
                ffi::kCFAllocatorDefault,
                element.raw,
                timestamp,
                value,
            )
        };
        if raw.is_null() {
            Err(HidError::OperationFailed(
                "IOHIDValueCreateWithIntegerValue",
            ))
        } else {
            Ok(Self { raw })
        }
    }

    /// Wraps `IOHIDValueCreateWithBytes`.
    pub fn from_bytes(element: HidElement, timestamp: u64, bytes: &[u8]) -> Result<Self, HidError> {
        let length = ffi::CFIndex::try_from(bytes.len()).map_err(|_| {
            HidError::InvalidArgument("byte slice length does not fit CFIndex".to_owned())
        })?;
        let raw = unsafe {
            ffi::IOHIDValueCreateWithBytes(
                ffi::kCFAllocatorDefault,
                element.raw,
                timestamp,
                bytes.as_ptr(),
                length,
            )
        };
        if raw.is_null() {
            Err(HidError::OperationFailed("IOHIDValueCreateWithBytes"))
        } else {
            Ok(Self { raw })
        }
    }

    #[must_use]
    /// Wraps `IOHIDValueGetElement`.
    pub fn element(&self) -> HidElement {
        HidElement {
            raw: unsafe { ffi::IOHIDValueGetElement(self.raw) },
        }
    }

    #[must_use]
    /// Wraps `IOHIDValueGetTimeStamp`.
    pub fn timestamp(&self) -> u64 {
        unsafe { ffi::IOHIDValueGetTimeStamp(self.raw) }
    }

    #[must_use]
    /// Wraps `IOHIDValueGetLength`.
    pub fn len(&self) -> usize {
        usize::try_from(unsafe { ffi::IOHIDValueGetLength(self.raw) }).unwrap_or(0)
    }

    #[must_use]
    /// Returns `true` when `IOHIDValueGetLength` is zero.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[must_use]
    /// Wraps `IOHIDValueGetBytePtr`.
    pub fn bytes(&self) -> Vec<u8> {
        let length = self.len();
        if length == 0 {
            return Vec::new();
        }
        let bytes = unsafe { ffi::IOHIDValueGetBytePtr(self.raw) };
        if bytes.is_null() {
            return Vec::new();
        }
        unsafe { core::slice::from_raw_parts(bytes, length) }.to_vec()
    }

    #[must_use]
    /// Wraps `IOHIDValueGetIntegerValue`.
    pub fn integer_value(&self) -> i64 {
        unsafe { ffi::IOHIDValueGetIntegerValue(self.raw) as i64 }
    }

    #[must_use]
    /// Wraps `IOHIDValueGetScaledValue`.
    pub fn scaled_value(&self, scale: HidValueScale) -> f64 {
        unsafe { ffi::IOHIDValueGetScaledValue(self.raw, scale.as_raw()) }
    }

    #[must_use]
    /// Mirrors `IOHIDValueRef`.
    pub const fn as_ptr(&self) -> ffi::IOHIDValueRef {
        self.raw
    }
}

#[allow(clippy::type_complexity)]
struct ReportContext {
    buffer_len: usize,
    callback: *mut Box<dyn Fn(&[u8]) + Send + Sync + 'static>,
}

#[allow(clippy::type_complexity)]
struct TimestampedReportContext {
    buffer_len: usize,
    callback: *mut Box<dyn Fn(HidInputReport) + Send + Sync + 'static>,
}

#[allow(clippy::type_complexity)]
struct ValueContext {
    callback: *mut Box<dyn Fn(&HidValue) + Send + Sync + 'static>,
}

unsafe extern "C" fn report_trampoline(
    context: *mut c_void,
    result: ffi::IOReturn,
    _sender: *mut c_void,
    _report_type: ffi::IOHIDReportType,
    _report_id: u32,
    report: *mut u8,
    report_length: ffi::CFIndex,
) {
    if context.is_null() || report.is_null() || result != ffi::kIOReturnSuccess {
        return;
    }
    let context = unsafe { &*context.cast::<ReportContext>() };
    let length = usize::try_from(report_length)
        .unwrap_or(0)
        .min(context.buffer_len);
    let bytes = unsafe { core::slice::from_raw_parts(report.cast_const(), length) };
    let callback = unsafe { &*context.callback };
    catch_user_panic("report_trampoline", || {
        callback(bytes);
    });
}

unsafe extern "C" fn timestamped_report_trampoline(
    context: *mut c_void,
    result: ffi::IOReturn,
    _sender: *mut c_void,
    report_type: ffi::IOHIDReportType,
    report_id: u32,
    report: *mut u8,
    report_length: ffi::CFIndex,
    timestamp: u64,
) {
    if context.is_null() || report.is_null() || result != ffi::kIOReturnSuccess {
        return;
    }
    let context = unsafe { &*context.cast::<TimestampedReportContext>() };
    let length = usize::try_from(report_length)
        .unwrap_or(0)
        .min(context.buffer_len);
    let bytes = unsafe { core::slice::from_raw_parts(report.cast_const(), length) }.to_vec();
    let callback = unsafe { &*context.callback };
    catch_user_panic("timestamped_report_trampoline", || {
        callback(HidInputReport {
            report_type: HidReportType::from_raw(report_type),
            report_id,
            bytes,
            timestamp,
        });
    });
}

unsafe extern "C" fn value_trampoline(
    context: *mut c_void,
    result: ffi::IOReturn,
    _sender: *mut c_void,
    value: ffi::IOHIDValueRef,
) {
    if context.is_null() || value.is_null() || result != ffi::kIOReturnSuccess {
        return;
    }
    let Some(value) = clone_value_ref(value) else {
        return;
    };
    let context = unsafe { &*context.cast::<ValueContext>() };
    let callback = unsafe { &*context.callback };
    catch_user_panic("value_trampoline", || {
        callback(&value);
    });
}

/// Owns a registration from `IOHIDDeviceRegisterInputReportCallback`.
pub struct ReportSubscription {
    device: ffi::IOHIDDeviceRef,
    run_loop: ffi::CFRunLoopRef,
    buffer_ptr: *mut [u8],
    buffer_len: usize,
    context: *mut ReportContext,
}

unsafe impl Send for ReportSubscription {}

impl Drop for ReportSubscription {
    fn drop(&mut self) {
        if self.device.is_null() || self.buffer_ptr.is_null() || self.context.is_null() {
            return;
        }
        let report_length = ffi::CFIndex::try_from(self.buffer_len).unwrap_or(0);
        unsafe {
            ffi::IOHIDDeviceRegisterInputReportCallback(
                self.device,
                (*self.buffer_ptr).as_mut_ptr(),
                report_length,
                None,
                ptr::null_mut(),
            );
            close_and_unschedule_device(self.device, self.run_loop);
            ffi::CFRelease(self.device);
            let context = Box::from_raw(self.context);
            let _ = Box::from_raw(context.callback);
            let _ = Box::from_raw(self.buffer_ptr);
        }
        self.device = ptr::null();
        self.context = ptr::null_mut();
    }
}

/// Owns a registration from `IOHIDDeviceRegisterInputReportWithTimeStampCallback`.
pub struct TimestampedReportSubscription {
    device: ffi::IOHIDDeviceRef,
    run_loop: ffi::CFRunLoopRef,
    buffer_ptr: *mut [u8],
    buffer_len: usize,
    context: *mut TimestampedReportContext,
}

unsafe impl Send for TimestampedReportSubscription {}

impl Drop for TimestampedReportSubscription {
    fn drop(&mut self) {
        if self.device.is_null() || self.buffer_ptr.is_null() || self.context.is_null() {
            return;
        }
        let report_length = ffi::CFIndex::try_from(self.buffer_len).unwrap_or(0);
        unsafe {
            ffi::IOHIDDeviceRegisterInputReportWithTimeStampCallback(
                self.device,
                (*self.buffer_ptr).as_mut_ptr(),
                report_length,
                None,
                ptr::null_mut(),
            );
            close_and_unschedule_device(self.device, self.run_loop);
            ffi::CFRelease(self.device);
            let context = Box::from_raw(self.context);
            let _ = Box::from_raw(context.callback);
            let _ = Box::from_raw(self.buffer_ptr);
        }
        self.device = ptr::null();
        self.context = ptr::null_mut();
    }
}

/// Owns a registration from `IOHIDDeviceRegisterInputValueCallback`.
pub struct ValueSubscription {
    device: ffi::IOHIDDeviceRef,
    run_loop: ffi::CFRunLoopRef,
    context: *mut ValueContext,
}

unsafe impl Send for ValueSubscription {}

impl Drop for ValueSubscription {
    fn drop(&mut self) {
        if self.device.is_null() || self.context.is_null() {
            return;
        }
        unsafe {
            ffi::IOHIDDeviceRegisterInputValueCallback(self.device, None, ptr::null_mut());
            close_and_unschedule_device(self.device, self.run_loop);
            ffi::CFRelease(self.device);
            let context = Box::from_raw(self.context);
            let _ = Box::from_raw(context.callback);
        }
        self.device = ptr::null();
        self.context = ptr::null_mut();
    }
}

fn open_and_schedule_device(device: ffi::IOHIDDeviceRef) -> Result<ffi::CFRunLoopRef, HidError> {
    let status = unsafe { ffi::IOHIDDeviceOpen(device, ffi::kIOHIDOptionsTypeNone) };
    if status != ffi::kIOReturnSuccess {
        return Err(HidError::DeviceOpenFailed(status));
    }
    let run_loop = unsafe { ffi::CFRunLoopGetCurrent() };
    unsafe {
        ffi::IOHIDDeviceScheduleWithRunLoop(device, run_loop, ffi::kCFRunLoopDefaultMode);
    }
    Ok(run_loop)
}

unsafe fn close_and_unschedule_device(device: ffi::IOHIDDeviceRef, run_loop: ffi::CFRunLoopRef) {
    ffi::IOHIDDeviceUnscheduleFromRunLoop(device, run_loop, ffi::kCFRunLoopDefaultMode);
    let _ = ffi::IOHIDDeviceClose(device, ffi::kIOHIDOptionsTypeNone);
}

pub(crate) fn clone_value_ref(raw: ffi::IOHIDValueRef) -> Option<HidValue> {
    if raw.is_null() {
        return None;
    }
    let element = unsafe { ffi::IOHIDValueGetElement(raw) };
    if element.is_null() {
        return None;
    }
    let timestamp = unsafe { ffi::IOHIDValueGetTimeStamp(raw) };
    let length = unsafe { ffi::IOHIDValueGetLength(raw) };
    if length > 0 {
        let bytes = unsafe { ffi::IOHIDValueGetBytePtr(raw) };
        if !bytes.is_null() {
            let copied = unsafe {
                ffi::IOHIDValueCreateWithBytes(
                    ffi::kCFAllocatorDefault,
                    element,
                    timestamp,
                    bytes,
                    length,
                )
            };
            if !copied.is_null() {
                return Some(HidValue { raw: copied });
            }
        }
    }
    let integer = unsafe { ffi::IOHIDValueGetIntegerValue(raw) };
    let copied = unsafe {
        ffi::IOHIDValueCreateWithIntegerValue(ffi::kCFAllocatorDefault, element, timestamp, integer)
    };
    (!copied.is_null()).then_some(HidValue { raw: copied })
}

fn build_cf_dictionary(entries: &[(String, MatchValue)]) -> Result<ffi::CFDictionaryRef, HidError> {
    let capacity = ffi::CFIndex::try_from(entries.len()).map_err(|_| {
        HidError::InvalidArgument("dictionary size does not fit CFIndex".to_owned())
    })?;
    let dict = unsafe {
        ffi::CFDictionaryCreateMutable(
            ffi::kCFAllocatorDefault,
            capacity,
            &raw const ffi::kCFTypeDictionaryKeyCallBacks,
            &raw const ffi::kCFTypeDictionaryValueCallBacks,
        )
    };
    if dict.is_null() {
        return Err(HidError::OperationFailed("CFDictionaryCreateMutable"));
    }

    for (key, value) in entries {
        let key_cf = match make_cfstring(key) {
            Ok(key_cf) => key_cf,
            Err(err) => {
                unsafe { ffi::CFRelease(dict.cast()) };
                return Err(err);
            }
        };
        let value_cf: ffi::CFTypeRef = match value {
            MatchValue::U32(value) => match make_cfnumber_u32(*value) {
                Ok(value_cf) => value_cf.cast(),
                Err(err) => {
                    unsafe {
                        ffi::CFRelease(key_cf.cast());
                        ffi::CFRelease(dict.cast());
                    }
                    return Err(err);
                }
            },
            MatchValue::String(value) => match make_cfstring(value) {
                Ok(value_cf) => value_cf.cast(),
                Err(err) => {
                    unsafe {
                        ffi::CFRelease(key_cf.cast());
                        ffi::CFRelease(dict.cast());
                    }
                    return Err(err);
                }
            },
        };
        unsafe {
            ffi::CFDictionarySetValue(dict, key_cf.cast(), value_cf.cast());
            ffi::CFRelease(key_cf.cast());
            ffi::CFRelease(value_cf);
        }
    }

    Ok(dict.cast())
}

fn build_cf_dictionary_array<T, F>(items: &[T], build: F) -> Result<ffi::CFArrayRef, HidError>
where
    F: Fn(&T) -> Result<ffi::CFDictionaryRef, HidError>,
{
    let capacity = ffi::CFIndex::try_from(items.len())
        .map_err(|_| HidError::InvalidArgument("array size does not fit CFIndex".to_owned()))?;
    let array = unsafe {
        ffi::CFArrayCreateMutable(
            ffi::kCFAllocatorDefault,
            capacity,
            &raw const ffi::kCFTypeArrayCallBacks,
        )
    };
    if array.is_null() {
        return Err(HidError::OperationFailed("CFArrayCreateMutable"));
    }
    for item in items {
        let dict = match build(item) {
            Ok(dict) => dict,
            Err(err) => {
                unsafe { ffi::CFRelease(array.cast()) };
                return Err(err);
            }
        };
        unsafe {
            ffi::CFArrayAppendValue(array, dict.cast());
            ffi::CFRelease(dict.cast());
        }
    }
    Ok(array.cast())
}

fn elements_from_array(array: ffi::CFArrayRef, release_after: bool) -> Vec<HidElement> {
    if array.is_null() {
        return Vec::new();
    }
    let count = unsafe { ffi::CFArrayGetCount(array) };
    let count = usize::try_from(count).unwrap_or(0);
    let mut elements = Vec::with_capacity(count);
    for index in 0..count {
        let raw = unsafe {
            ffi::CFArrayGetValueAtIndex(array, ffi::CFIndex::try_from(index).unwrap_or(0))
        };
        if !raw.is_null() {
            elements.push(HidElement { raw: raw.cast() });
        }
    }
    if release_after {
        unsafe { ffi::CFRelease(array.cast()) };
    }
    elements
}

fn read_device_info(device: ffi::IOHIDDeviceRef) -> HidDeviceInfo {
    HidDeviceInfo {
        vendor_id: read_device_property_u32(device, ffi::kIOHIDVendorIDKey),
        product_id: read_device_property_u32(device, ffi::kIOHIDProductIDKey),
        product: read_device_property_string(device, ffi::kIOHIDProductKey),
        manufacturer: read_device_property_string(device, ffi::kIOHIDManufacturerKey),
        serial_number: read_device_property_string(device, ffi::kIOHIDSerialNumberKey),
        transport: read_device_property_string(device, ffi::kIOHIDTransportKey),
        usage_page: read_device_property_u32(device, ffi::kIOHIDPrimaryUsagePageKey),
        usage: read_device_property_u32(device, ffi::kIOHIDPrimaryUsageKey),
        location_id: read_device_property_u32(device, ffi::kIOHIDLocationIDKey),
    }
}

fn read_device_property_u32(device: ffi::IOHIDDeviceRef, key: &str) -> Option<u32> {
    with_cfstring(key, |key_cf| unsafe {
        read_cf_u32(ffi::IOHIDDeviceGetProperty(device, key_cf))
    })
    .ok()
    .flatten()
}

fn read_device_property_string(device: ffi::IOHIDDeviceRef, key: &str) -> Option<String> {
    with_cfstring(key, |key_cf| unsafe {
        read_cf_string(ffi::IOHIDDeviceGetProperty(device, key_cf))
    })
    .ok()
    .flatten()
}

fn read_cf_u32(value: ffi::CFTypeRef) -> Option<u32> {
    if value.is_null() || unsafe { ffi::CFGetTypeID(value) } != unsafe { ffi::CFNumberGetTypeID() }
    {
        return None;
    }
    let mut number = 0_i64;
    let ok = unsafe {
        ffi::CFNumberGetValue(
            value.cast(),
            ffi::kCFNumberSInt64Type,
            ptr::from_mut(&mut number).cast(),
        )
    };
    ok.then(|| u32::try_from(number).ok()).flatten()
}

fn read_cf_string(value: ffi::CFTypeRef) -> Option<String> {
    if value.is_null() || unsafe { ffi::CFGetTypeID(value) } != unsafe { ffi::CFStringGetTypeID() }
    {
        return None;
    }
    cfstring_to_string(value.cast())
}

fn read_cf_data(value: ffi::CFTypeRef) -> Option<Vec<u8>> {
    if value.is_null() || unsafe { ffi::CFGetTypeID(value) } != unsafe { ffi::CFDataGetTypeID() } {
        return None;
    }
    let length = usize::try_from(unsafe { ffi::CFDataGetLength(value.cast()) }).unwrap_or(0);
    if length == 0 {
        return Some(Vec::new());
    }
    let bytes = unsafe { ffi::CFDataGetBytePtr(value.cast()) };
    if bytes.is_null() {
        return None;
    }
    Some(unsafe { core::slice::from_raw_parts(bytes, length) }.to_vec())
}

fn cfstring_to_string(value: ffi::CFStringRef) -> Option<String> {
    if value.is_null() {
        return None;
    }
    let length = unsafe { ffi::CFStringGetLength(value) };
    let capacity = usize::try_from((length * 4) + 1).unwrap_or(0);
    let mut buffer = vec![0_u8; capacity];
    let ok = unsafe {
        ffi::CFStringGetCString(
            value,
            buffer.as_mut_ptr().cast::<c_char>(),
            ffi::CFIndex::try_from(buffer.len()).unwrap_or(0),
            ffi::kCFStringEncodingUTF8,
        )
    };
    if !ok {
        return None;
    }
    if let Some(end) = buffer.iter().position(|byte| *byte == 0) {
        buffer.truncate(end);
    }
    String::from_utf8(buffer).ok()
}

fn with_cfstring<T, F>(value: &str, f: F) -> Result<T, HidError>
where
    F: FnOnce(ffi::CFStringRef) -> T,
{
    let value_cf = make_cfstring(value)?;
    let result = f(value_cf);
    unsafe { ffi::CFRelease(value_cf.cast()) };
    Ok(result)
}

fn make_cfstring(value: &str) -> Result<ffi::CFStringRef, HidError> {
    let value = CString::new(value).map_err(|err| HidError::InvalidArgument(err.to_string()))?;
    let string = unsafe {
        ffi::CFStringCreateWithCString(
            ffi::kCFAllocatorDefault,
            value.as_ptr(),
            ffi::kCFStringEncodingUTF8,
        )
    };
    if string.is_null() {
        Err(HidError::OperationFailed("CFStringCreateWithCString"))
    } else {
        Ok(string)
    }
}

fn make_cfnumber_u32(value: u32) -> Result<ffi::CFNumberRef, HidError> {
    let value = i64::from(value);
    let number = unsafe {
        ffi::CFNumberCreate(
            ffi::kCFAllocatorDefault,
            ffi::kCFNumberSInt64Type,
            ptr::from_ref(&value).cast(),
        )
    };
    if number.is_null() {
        Err(HidError::OperationFailed("CFNumberCreate"))
    } else {
        Ok(number)
    }
}

fn make_cfdata(value: &[u8]) -> Result<ffi::CFDataRef, HidError> {
    let length = ffi::CFIndex::try_from(value.len()).map_err(|_| {
        HidError::InvalidArgument("byte slice length does not fit CFIndex".to_owned())
    })?;
    let data = unsafe { ffi::CFDataCreate(ffi::kCFAllocatorDefault, value.as_ptr(), length) };
    if data.is_null() {
        Err(HidError::OperationFailed("CFDataCreate"))
    } else {
        Ok(data)
    }
}