summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/hidapi/SDL_hidapi.c
blob: b14b75eb4b54026a5e866fdfafdfb066a143a837 (plain)
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
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

/* Original hybrid wrapper for Linux by Valve Software. Their original notes:
 *
 * The libusb version doesn't support Bluetooth, but not all Linux
 * distributions allow access to /dev/hidraw*
 *
 * This merges the two, at a small performance cost, until distributions
 * have granted access to /dev/hidraw*
 */

#include "SDL_internal.h"

#include "SDL_hidapi_c.h"
#include "../joystick/usb_ids.h"
#include "../SDL_hints_c.h"

// Initial type declarations
#define HID_API_NO_EXPORT_DEFINE // do not export hidapi procedures
#include "hidapi/hidapi.h"

#ifndef SDL_HIDAPI_DISABLED

#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
#include "../core/windows/SDL_windows.h"
#endif

#ifdef SDL_PLATFORM_MACOS
#include <CoreFoundation/CoreFoundation.h>
#include <mach/mach.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/hid/IOHIDDevice.h>
#include <IOKit/usb/USBSpec.h>
#include <AvailabilityMacros.h>
// Things named "Master" were renamed to "Main" in macOS 12.0's SDK.
#if MAC_OS_X_VERSION_MIN_REQUIRED < 120000
#define kIOMainPortDefault kIOMasterPortDefault
#endif
#endif

#include "../core/linux/SDL_udev.h"
#ifdef SDL_USE_LIBUDEV
#include <poll.h>
#endif

#ifdef HAVE_INOTIFY
#include <string.h> // strerror
#include <errno.h>  // errno
#include <fcntl.h>
#include <limits.h> // For the definition of NAME_MAX
#include <sys/inotify.h>
#endif

#if defined(SDL_USE_LIBUDEV) || defined(HAVE_INOTIFY)
#include <unistd.h>
#endif

#ifdef SDL_USE_LIBUDEV
typedef enum
{
    ENUMERATION_UNSET,
    ENUMERATION_LIBUDEV,
    ENUMERATION_FALLBACK
} LinuxEnumerationMethod;

static LinuxEnumerationMethod linux_enumeration_method = ENUMERATION_UNSET;
#endif

#ifdef HAVE_INOTIFY
static int inotify_fd = -1;
#endif

#ifdef SDL_USE_LIBUDEV
static const SDL_UDEV_Symbols *usyms = NULL;
#endif

static struct
{
    bool m_bInitialized;
    Uint32 m_unDeviceChangeCounter;
    bool m_bCanGetNotifications;
    Uint64 m_unLastDetect;

#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
    SDL_ThreadID m_nThreadID;
    WNDCLASSEXA m_wndClass;
    HWND m_hwndMsg;
    HDEVNOTIFY m_hNotify;
    double m_flLastWin32MessageCheck;
#endif

#ifdef SDL_PLATFORM_MACOS
    IONotificationPortRef m_notificationPort;
    mach_port_t m_notificationMach;
#endif

#ifdef SDL_USE_LIBUDEV
    struct udev *m_pUdev;
    struct udev_monitor *m_pUdevMonitor;
    int m_nUdevFd;
#endif
} SDL_HIDAPI_discovery;

#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
struct _DEV_BROADCAST_HDR
{
    DWORD dbch_size;
    DWORD dbch_devicetype;
    DWORD dbch_reserved;
};

typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
{
    DWORD dbcc_size;
    DWORD dbcc_devicetype;
    DWORD dbcc_reserved;
    GUID dbcc_classguid;
    char dbcc_name[1];
} DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;

typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
#define DBT_DEVICEARRIVAL          0x8000     // system detected a new device
#define DBT_DEVICEREMOVECOMPLETE   0x8004     // device was removed from the system
#define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 // device interface class
#define DBT_DEVNODES_CHANGED       0x0007
#define DBT_CONFIGCHANGED          0x0018
#define DBT_DEVICETYPESPECIFIC     0x8005 // type specific event
#define DBT_DEVINSTSTARTED         0x8008 // device installed and started

#include <initguid.h>
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);

static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
    case WM_DEVICECHANGE:
        switch (wParam) {
        case DBT_DEVICEARRIVAL:
        case DBT_DEVICEREMOVECOMPLETE:
            if (((DEV_BROADCAST_HDR *)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
                ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
            }
            break;
        }
        return TRUE;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}
#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)

#ifdef SDL_PLATFORM_MACOS
static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
{
    // Must drain the iterator, or we won't receive new notifications
    io_object_t entry;
    while ((entry = IOIteratorNext(portIterator)) != 0) {
        IOObjectRelease(entry);
        ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
    }
}
#endif // SDL_PLATFORM_MACOS

#ifdef HAVE_INOTIFY
#ifdef HAVE_INOTIFY_INIT1
static int SDL_inotify_init1(void)
{
    return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
}
#else
static int SDL_inotify_init1(void)
{
    int fd = inotify_init();
    if (fd < 0) {
        return -1;
    }
    fcntl(fd, F_SETFL, O_NONBLOCK);
    fcntl(fd, F_SETFD, FD_CLOEXEC);
    return fd;
}
#endif

static int StrHasPrefix(const char *string, const char *prefix)
{
    return SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0;
}

static int StrIsInteger(const char *string)
{
    const char *p;

    if (*string == '\0') {
        return 0;
    }

    for (p = string; *p != '\0'; p++) {
        if (*p < '0' || *p > '9') {
            return 0;
        }
    }

    return 1;
}
#endif // HAVE_INOTIFY

static void HIDAPI_InitializeDiscovery(void)
{
    SDL_HIDAPI_discovery.m_bInitialized = true;
    SDL_HIDAPI_discovery.m_unDeviceChangeCounter = 1;
    SDL_HIDAPI_discovery.m_bCanGetNotifications = false;
    SDL_HIDAPI_discovery.m_unLastDetect = 0;

#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
    SDL_HIDAPI_discovery.m_nThreadID = SDL_GetCurrentThreadID();

    SDL_zero(SDL_HIDAPI_discovery.m_wndClass);
    SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
    SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
    SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; // This function is called by windows
    SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);

    RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
    SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);

    {
        DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;

        SDL_zero(devBroadcast);
        devBroadcast.dbcc_size = sizeof(devBroadcast);
        devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
        devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;

        /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
         * but that seems to be necessary to get a notice after each individual usb input device actually
         * installs, rather than just as the composite device is seen.
         */
        SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification(SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
        SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_hNotify != 0);
    }
#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)

#ifdef SDL_PLATFORM_MACOS
    SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMainPortDefault);
    if (SDL_HIDAPI_discovery.m_notificationPort) {
        {
            io_iterator_t portIterator = 0;
            io_object_t entry;
            IOReturn result = IOServiceAddMatchingNotification(
                SDL_HIDAPI_discovery.m_notificationPort,
                kIOFirstMatchNotification,
                IOServiceMatching(kIOHIDDeviceKey),
                CallbackIOServiceFunc, NULL, &portIterator);

            if (result == 0) {
                // Must drain the existing iterator, or we won't receive new notifications
                while ((entry = IOIteratorNext(portIterator)) != 0) {
                    IOObjectRelease(entry);
                }
            } else {
                IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
                SDL_HIDAPI_discovery.m_notificationPort = nil;
            }
        }
        {
            io_iterator_t portIterator = 0;
            io_object_t entry;
            IOReturn result = IOServiceAddMatchingNotification(
                SDL_HIDAPI_discovery.m_notificationPort,
                kIOTerminatedNotification,
                IOServiceMatching(kIOHIDDeviceKey),
                CallbackIOServiceFunc, NULL, &portIterator);

            if (result == 0) {
                // Must drain the existing iterator, or we won't receive new notifications
                while ((entry = IOIteratorNext(portIterator)) != 0) {
                    IOObjectRelease(entry);
                }
            } else {
                IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
                SDL_HIDAPI_discovery.m_notificationPort = nil;
            }
        }
    }

    SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
    if (SDL_HIDAPI_discovery.m_notificationPort) {
        SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
    }

    SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);

#endif // SDL_PLATFORM_MACOS

#ifdef SDL_USE_LIBUDEV
    if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
        SDL_HIDAPI_discovery.m_pUdev = NULL;
        SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
        SDL_HIDAPI_discovery.m_nUdevFd = -1;

        usyms = SDL_UDEV_GetUdevSyms();
        if (usyms != NULL) {
            SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
            if (SDL_HIDAPI_discovery.m_pUdev != NULL) {
                SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
                if (SDL_HIDAPI_discovery.m_pUdevMonitor != NULL) {
                    usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
                    SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
                    SDL_HIDAPI_discovery.m_bCanGetNotifications = true;
                }
            }
        }
    } else
#endif // SDL_USE_LIBUDEV
    {
#ifdef HAVE_INOTIFY
        inotify_fd = SDL_inotify_init1();

        if (inotify_fd < 0) {
            SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
                        "Unable to initialize inotify, falling back to polling: %s",
                        strerror(errno));
            return;
        }

        /* We need to watch for attribute changes in addition to
         * creation, because when a device is first created, it has
         * permissions that we can't read. When udev chmods it to
         * something that we maybe *can* read, we'll get an
         * IN_ATTRIB event to tell us. */
        if (inotify_add_watch(inotify_fd, "/dev",
                              IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
            close(inotify_fd);
            inotify_fd = -1;
            SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
                        "Unable to add inotify watch, falling back to polling: %s",
                        strerror(errno));
            return;
        }

        SDL_HIDAPI_discovery.m_bCanGetNotifications = true;
#endif // HAVE_INOTIFY
    }
}

static void HIDAPI_UpdateDiscovery(void)
{
    if (!SDL_HIDAPI_discovery.m_bInitialized) {
        HIDAPI_InitializeDiscovery();
    }

    if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) {
        const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; // Update every 3 seconds
        Uint64 now = SDL_GetTicks();
        if (!SDL_HIDAPI_discovery.m_unLastDetect || now >= (SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) {
            ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
            SDL_HIDAPI_discovery.m_unLastDetect = now;
        }
        return;
    }

#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
#if 0 // just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan.
    // We'll only get messages on the same thread that created the window
    if (SDL_GetCurrentThreadID() == SDL_HIDAPI_discovery.m_nThreadID) {
        MSG msg;
        while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) {
            if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }
#endif
#endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)

#ifdef SDL_PLATFORM_MACOS
    if (SDL_HIDAPI_discovery.m_notificationPort) {
        struct
        {
            mach_msg_header_t hdr;
            char payload[4096];
        } msg;
        while (mach_msg(&msg.hdr, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, sizeof(msg), SDL_HIDAPI_discovery.m_notificationMach, 0, MACH_PORT_NULL) == KERN_SUCCESS) {
            IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);
        }
    }
#endif

#ifdef SDL_USE_LIBUDEV
    if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
        if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
            /* Drain all notification events.
             * We don't expect a lot of device notifications so just
             * do a new discovery on any kind or number of notifications.
             * This could be made more restrictive if necessary.
             */
            for (;;) {
                struct pollfd PollUdev;
                struct udev_device *pUdevDevice;

                PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
                PollUdev.events = POLLIN;
                if (poll(&PollUdev, 1, 0) != 1) {
                    break;
                }

                pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
                if (pUdevDevice) {
                    const char *action = NULL;
                    action = usyms->udev_device_get_action(pUdevDevice);
                    if (action == NULL || SDL_strcmp(action, "add") == 0 || SDL_strcmp(action, "remove") == 0) {
                        ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
                    }
                    usyms->udev_device_unref(pUdevDevice);
                }
            }
        }
    } else
#endif // SDL_USE_LIBUDEV
    {
#ifdef HAVE_INOTIFY
        if (inotify_fd >= 0) {
            union
            {
                struct inotify_event event;
                char storage[4096];
                char enough_for_inotify[sizeof(struct inotify_event) + NAME_MAX + 1];
            } buf;
            ssize_t bytes;
            size_t remain = 0;
            size_t len;

            bytes = read(inotify_fd, &buf, sizeof(buf));

            if (bytes > 0) {
                remain = (size_t)bytes;
            }

            while (remain > 0) {
                if (buf.event.len > 0) {
                    if (StrHasPrefix(buf.event.name, "hidraw") &&
                        StrIsInteger(buf.event.name + SDL_strlen("hidraw"))) {
                        ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
                        /* We found an hidraw change. We still continue to
                         * drain the inotify fd to avoid leaving old
                         * notifications in the queue. */
                    }
                }

                len = sizeof(struct inotify_event) + buf.event.len;
                remain -= len;

                if (remain != 0) {
                    SDL_memmove(&buf.storage[0], &buf.storage[len], remain);
                }
            }
        }
#endif // HAVE_INOTIFY
    }
}

static void HIDAPI_ShutdownDiscovery(void)
{
    if (!SDL_HIDAPI_discovery.m_bInitialized) {
        return;
    }

#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
    if (SDL_HIDAPI_discovery.m_hNotify) {
        UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
    }

    if (SDL_HIDAPI_discovery.m_hwndMsg) {
        DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
    }

    UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
#endif

#ifdef SDL_PLATFORM_MACOS
    if (SDL_HIDAPI_discovery.m_notificationPort) {
        IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
    }
#endif

#ifdef SDL_USE_LIBUDEV
    if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
        if (usyms) {
            if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
                usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
            }
            if (SDL_HIDAPI_discovery.m_pUdev) {
                usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
            }
            SDL_UDEV_ReleaseUdevSyms();
            usyms = NULL;
        }
    } else
#endif // SDL_USE_LIBUDEV
    {
#ifdef HAVE_INOTIFY
        if (inotify_fd >= 0) {
            close(inotify_fd);
            inotify_fd = -1;
        }
#endif
    }

    SDL_HIDAPI_discovery.m_bInitialized = false;
}

// Platform HIDAPI Implementation

#define HIDAPI_USING_SDL_RUNTIME
#define HIDAPI_IGNORE_DEVICE(BUS, VID, PID, USAGE_PAGE, USAGE) \
        SDL_HIDAPI_ShouldIgnoreDevice(BUS, VID, PID, USAGE_PAGE, USAGE)

struct PLATFORM_hid_device_;
typedef struct PLATFORM_hid_device_ PLATFORM_hid_device;

#define api_version                  PLATFORM_api_version
#define create_device_info_for_device PLATFORM_create_device_info_for_device
#define free_hid_device              PLATFORM_free_hid_device
#define hid_close                    PLATFORM_hid_close
#define hid_device                   PLATFORM_hid_device
#define hid_device_                  PLATFORM_hid_device_
#define hid_enumerate                PLATFORM_hid_enumerate
#define hid_error                    PLATFORM_hid_error
#define hid_exit                     PLATFORM_hid_exit
#define hid_free_enumeration         PLATFORM_hid_free_enumeration
#define hid_get_device_info          PLATFORM_hid_get_device_info
#define hid_get_feature_report       PLATFORM_hid_get_feature_report
#define hid_get_indexed_string       PLATFORM_hid_get_indexed_string
#define hid_get_input_report         PLATFORM_hid_get_input_report
#define hid_get_manufacturer_string  PLATFORM_hid_get_manufacturer_string
#define hid_get_product_string       PLATFORM_hid_get_product_string
#define hid_get_report_descriptor    PLATFORM_hid_get_report_descriptor
#define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
#define hid_init                     PLATFORM_hid_init
#define hid_open_path                PLATFORM_hid_open_path
#define hid_open                     PLATFORM_hid_open
#define hid_read                     PLATFORM_hid_read
#define hid_read_timeout             PLATFORM_hid_read_timeout
#define hid_send_feature_report      PLATFORM_hid_send_feature_report
#define hid_set_nonblocking          PLATFORM_hid_set_nonblocking
#define hid_version                  PLATFORM_hid_version
#define hid_version_str              PLATFORM_hid_version_str
#define hid_write                    PLATFORM_hid_write
#define input_report                 PLATFORM_input_report
#define make_path                    PLATFORM_make_path
#define new_hid_device               PLATFORM_new_hid_device
#define read_thread                  PLATFORM_read_thread
#define return_data                  PLATFORM_return_data

#ifdef SDL_PLATFORM_LINUX
#include "SDL_hidapi_linux.h"
#elif defined(SDL_PLATFORM_NETBSD)
#include "SDL_hidapi_netbsd.h"
#elif defined(SDL_PLATFORM_MACOS)
#include "SDL_hidapi_mac.h"
#elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
#include "SDL_hidapi_windows.h"
#elif defined(SDL_PLATFORM_ANDROID)
#include "SDL_hidapi_android.h"
#elif defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)
#include "SDL_hidapi_ios.h"
#endif

#undef api_version
#undef create_device_info_for_device
#undef free_hid_device
#undef hid_close
#undef hid_device
#undef hid_device_
#undef hid_enumerate
#undef hid_error
#undef hid_exit
#undef hid_free_enumeration
#undef hid_get_device_info
#undef hid_get_feature_report
#undef hid_get_indexed_string
#undef hid_get_input_report
#undef hid_get_manufacturer_string
#undef hid_get_product_string
#undef hid_get_report_descriptor
#undef hid_get_serial_number_string
#undef hid_init
#undef hid_open
#undef hid_open_path
#undef hid_read
#undef hid_read_timeout
#undef hid_send_feature_report
#undef hid_set_nonblocking
#undef hid_version
#undef hid_version_str
#undef hid_write
#undef input_report
#undef make_path
#undef new_hid_device
#undef read_thread
#undef return_data

#ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
#define HAVE_DRIVER_BACKEND 1
#endif

#ifdef HAVE_DRIVER_BACKEND

// DRIVER HIDAPI Implementation

struct DRIVER_hid_device_;
typedef struct DRIVER_hid_device_ DRIVER_hid_device;

#define hid_close                    DRIVER_hid_close
#define hid_device                   DRIVER_hid_device
#define hid_device_                  DRIVER_hid_device_
#define hid_enumerate                DRIVER_hid_enumerate
#define hid_error                    DRIVER_hid_error
#define hid_exit                     DRIVER_hid_exit
#define hid_free_enumeration         DRIVER_hid_free_enumeration
#define hid_get_device_info          DRIVER_hid_get_device_info
#define hid_get_feature_report       DRIVER_hid_get_feature_report
#define hid_get_indexed_string       DRIVER_hid_get_indexed_string
#define hid_get_input_report         DRIVER_hid_get_input_report
#define hid_get_manufacturer_string  DRIVER_hid_get_manufacturer_string
#define hid_get_product_string       DRIVER_hid_get_product_string
#define hid_get_report_descriptor    DRIVER_hid_get_report_descriptor
#define hid_get_serial_number_string DRIVER_hid_get_serial_number_string
#define hid_init                     DRIVER_hid_init
#define hid_open                     DRIVER_hid_open
#define hid_open_path                DRIVER_hid_open_path
#define hid_read                     DRIVER_hid_read
#define hid_read_timeout             DRIVER_hid_read_timeout
#define hid_send_feature_report      DRIVER_hid_send_feature_report
#define hid_set_nonblocking          DRIVER_hid_set_nonblocking
#define hid_write                    DRIVER_hid_write

#ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
#include "SDL_hidapi_steamxbox.h"
#else
#error Need a driver hid.c for this platform!
#endif

#undef hid_close
#undef hid_device
#undef hid_device_
#undef hid_enumerate
#undef hid_error
#undef hid_exit
#undef hid_free_enumeration
#undef hid_get_device_info
#undef hid_get_feature_report
#undef hid_get_indexed_string
#undef hid_get_input_report
#undef hid_get_manufacturer_string
#undef hid_get_product_string
#undef hid_get_report_descriptor
#undef hid_get_serial_number_string
#undef hid_init
#undef hid_open
#undef hid_open_path
#undef hid_read
#undef hid_read_timeout
#undef hid_send_feature_report
#undef hid_set_nonblocking
#undef hid_write

#endif // HAVE_DRIVER_BACKEND

#ifdef HAVE_LIBUSB
// libusb HIDAPI Implementation

// Include this now, for our dynamically-loaded libusb context
#include <libusb.h>

static struct
{
    SDL_SharedObject *libhandle;

    /* *INDENT-OFF* */ // clang-format off
    int (LIBUSB_CALL *init)(libusb_context **ctx);
    void (LIBUSB_CALL *exit)(libusb_context *ctx);
    ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list);
    void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices);
    int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
    int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev,    struct libusb_config_descriptor **config);
    int (LIBUSB_CALL *get_config_descriptor)(
        libusb_device *dev,
        uint8_t config_index,
        struct libusb_config_descriptor **config
    );
    void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config);
    uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev);
    int (LIBUSB_CALL *get_port_numbers)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len);
    uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev);
    int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle);
    void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle);
    libusb_device *(LIBUSB_CALL *get_device)(libusb_device_handle *dev_handle);
    int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number);
    int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number);
    int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
    int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
    int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
    int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
    struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets);
    int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer);
    int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer);
    void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer);
    int (LIBUSB_CALL *control_transfer)(
        libusb_device_handle *dev_handle,
        uint8_t request_type,
        uint8_t bRequest,
        uint16_t wValue,
        uint16_t wIndex,
        unsigned char *data,
        uint16_t wLength,
        unsigned int timeout
    );
    int (LIBUSB_CALL *interrupt_transfer)(
        libusb_device_handle *dev_handle,
        unsigned char endpoint,
        unsigned char *data,
        int length,
        int *actual_length,
        unsigned int timeout
    );
    int (LIBUSB_CALL *handle_events)(libusb_context *ctx);
    int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed);
    const char * (LIBUSB_CALL *error_name)(int errcode);
/* *INDENT-ON* */ // clang-format on

} libusb_ctx;

#define libusb_init                         libusb_ctx.init
#define libusb_exit                         libusb_ctx.exit
#define libusb_get_device_list              libusb_ctx.get_device_list
#define libusb_free_device_list             libusb_ctx.free_device_list
#define libusb_get_device_descriptor        libusb_ctx.get_device_descriptor
#define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
#define libusb_get_config_descriptor        libusb_ctx.get_config_descriptor
#define libusb_free_config_descriptor       libusb_ctx.free_config_descriptor
#define libusb_get_bus_number               libusb_ctx.get_bus_number
#define libusb_get_port_numbers             libusb_ctx.get_port_numbers
#define libusb_get_device_address           libusb_ctx.get_device_address
#define libusb_open                         libusb_ctx.open
#define libusb_close                        libusb_ctx.close
#define libusb_get_device                   libusb_ctx.get_device
#define libusb_claim_interface              libusb_ctx.claim_interface
#define libusb_release_interface            libusb_ctx.release_interface
#define libusb_kernel_driver_active         libusb_ctx.kernel_driver_active
#define libusb_detach_kernel_driver         libusb_ctx.detach_kernel_driver
#define libusb_attach_kernel_driver         libusb_ctx.attach_kernel_driver
#define libusb_set_interface_alt_setting    libusb_ctx.set_interface_alt_setting
#define libusb_alloc_transfer               libusb_ctx.alloc_transfer
#define libusb_submit_transfer              libusb_ctx.submit_transfer
#define libusb_cancel_transfer              libusb_ctx.cancel_transfer
#define libusb_free_transfer                libusb_ctx.free_transfer
#define libusb_control_transfer             libusb_ctx.control_transfer
#define libusb_interrupt_transfer           libusb_ctx.interrupt_transfer
#define libusb_handle_events                libusb_ctx.handle_events
#define libusb_handle_events_completed      libusb_ctx.handle_events_completed
#define libusb_error_name                   libusb_ctx.error_name

struct LIBUSB_hid_device_;
typedef struct LIBUSB_hid_device_ LIBUSB_hid_device;

#define free_hid_device              LIBUSB_free_hid_device
#define hid_close                    LIBUSB_hid_close
#define hid_device                   LIBUSB_hid_device
#define hid_device_                  LIBUSB_hid_device_
#define hid_enumerate                LIBUSB_hid_enumerate
#define hid_error                    LIBUSB_hid_error
#define hid_exit                     LIBUSB_hid_exit
#define hid_free_enumeration         LIBUSB_hid_free_enumeration
#define hid_get_device_info          LIBUSB_hid_get_device_info
#define hid_get_feature_report       LIBUSB_hid_get_feature_report
#define hid_get_indexed_string       LIBUSB_hid_get_indexed_string
#define hid_get_input_report         LIBUSB_hid_get_input_report
#define hid_get_manufacturer_string  LIBUSB_hid_get_manufacturer_string
#define hid_get_product_string       LIBUSB_hid_get_product_string
#define hid_get_report_descriptor    LIBUSB_hid_get_report_descriptor
#define hid_get_serial_number_string LIBUSB_hid_get_serial_number_string
#define hid_init                     LIBUSB_hid_init
#define hid_open                     LIBUSB_hid_open
#define hid_open_path                LIBUSB_hid_open_path
#define hid_read                     LIBUSB_hid_read
#define hid_read_timeout             LIBUSB_hid_read_timeout
#define hid_send_feature_report      LIBUSB_hid_send_feature_report
#define hid_set_nonblocking          LIBUSB_hid_set_nonblocking
#define hid_write                    LIBUSB_hid_write
#define input_report                 LIBUSB_input_report
#define make_path                    LIBUSB_make_path
#define new_hid_device               LIBUSB_new_hid_device
#define read_thread                  LIBUSB_read_thread
#define return_data                  LIBUSB_return_data

#include "SDL_hidapi_libusb.h"

#undef libusb_init
#undef libusb_exit
#undef libusb_get_device_list
#undef libusb_free_device_list
#undef libusb_get_device_descriptor
#undef libusb_get_active_config_descriptor
#undef libusb_get_config_descriptor
#undef libusb_free_config_descriptor
#undef libusb_get_bus_number
#undef libusb_get_port_numbers
#undef libusb_get_device_address
#undef libusb_open
#undef libusb_close
#undef libusb_get_device
#undef libusb_claim_interface
#undef libusb_release_interface
#undef libusb_kernel_driver_active
#undef libusb_detach_kernel_driver
#undef libusb_attach_kernel_driver
#undef libusb_set_interface_alt_setting
#undef libusb_alloc_transfer
#undef libusb_submit_transfer
#undef libusb_cancel_transfer
#undef libusb_free_transfer
#undef libusb_control_transfer
#undef libusb_interrupt_transfer
#undef libusb_handle_events
#undef libusb_handle_events_completed
#undef libusb_error_name

#undef free_hid_device
#undef hid_close
#undef hid_device
#undef hid_device_
#undef hid_enumerate
#undef hid_error
#undef hid_exit
#undef hid_free_enumeration
#undef hid_get_device_info
#undef hid_get_feature_report
#undef hid_get_indexed_string
#undef hid_get_input_report
#undef hid_get_manufacturer_string
#undef hid_get_product_string
#undef hid_get_report_descriptor
#undef hid_get_serial_number_string
#undef hid_init
#undef hid_open
#undef hid_open_path
#undef hid_read
#undef hid_read_timeout
#undef hid_send_feature_report
#undef hid_set_nonblocking
#undef hid_write
#undef input_report
#undef make_path
#undef new_hid_device
#undef read_thread
#undef return_data

/* If the platform has any backend other than libusb, try to avoid using
 * libusb as the main backend for devices, since it detaches drivers and
 * therefore makes devices inaccessible to the rest of the OS.
 *
 * We do this by whitelisting devices we know to be accessible _exclusively_
 * via libusb; these are typically devices that look like HIDs but have a
 * quirk that requires direct access to the hardware.
 */
static const struct {
    Uint16 vendor;
    Uint16 product;
} SDL_libusb_whitelist[] = {
    { 0x057e, 0x0337 } // Nintendo WUP-028, Wii U/Switch GameCube Adapter
};

static bool IsInWhitelist(Uint16 vendor, Uint16 product)
{
    int i;
    for (i = 0; i < SDL_arraysize(SDL_libusb_whitelist); i += 1) {
        if (vendor == SDL_libusb_whitelist[i].vendor &&
            product == SDL_libusb_whitelist[i].product) {
            return true;
        }
    }
    return false;
}

#endif // HAVE_LIBUSB

#endif // !SDL_HIDAPI_DISABLED

#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND)
// We have another way to get HID devices, so use the whitelist to get devices where libusb is preferred
#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT true
#else
// libusb is the only way to get HID devices, so don't use the whitelist, get them all
#define SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT false
#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND

static bool use_libusb_whitelist = SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT;

// Shared HIDAPI Implementation

struct hidapi_backend
{
    int (*hid_write)(void *device, const unsigned char *data, size_t length);
    int (*hid_read_timeout)(void *device, unsigned char *data, size_t length, int milliseconds);
    int (*hid_read)(void *device, unsigned char *data, size_t length);
    int (*hid_set_nonblocking)(void *device, int nonblock);
    int (*hid_send_feature_report)(void *device, const unsigned char *data, size_t length);
    int (*hid_get_feature_report)(void *device, unsigned char *data, size_t length);
    int (*hid_get_input_report)(void *device, unsigned char *data, size_t length);
    void (*hid_close)(void *device);
    int (*hid_get_manufacturer_string)(void *device, wchar_t *string, size_t maxlen);
    int (*hid_get_product_string)(void *device, wchar_t *string, size_t maxlen);
    int (*hid_get_serial_number_string)(void *device, wchar_t *string, size_t maxlen);
    int (*hid_get_indexed_string)(void *device, int string_index, wchar_t *string, size_t maxlen);
    struct hid_device_info *(*hid_get_device_info)(void *device);
    int (*hid_get_report_descriptor)(void *device, unsigned char *buf, size_t buf_size);
    const wchar_t *(*hid_error)(void *device);
};

#ifdef HAVE_PLATFORM_BACKEND
static const struct hidapi_backend PLATFORM_Backend = {
    (void *)PLATFORM_hid_write,
    (void *)PLATFORM_hid_read_timeout,
    (void *)PLATFORM_hid_read,
    (void *)PLATFORM_hid_set_nonblocking,
    (void *)PLATFORM_hid_send_feature_report,
    (void *)PLATFORM_hid_get_feature_report,
    (void *)PLATFORM_hid_get_input_report,
    (void *)PLATFORM_hid_close,
    (void *)PLATFORM_hid_get_manufacturer_string,
    (void *)PLATFORM_hid_get_product_string,
    (void *)PLATFORM_hid_get_serial_number_string,
    (void *)PLATFORM_hid_get_indexed_string,
    (void *)PLATFORM_hid_get_device_info,
    (void *)PLATFORM_hid_get_report_descriptor,
    (void *)PLATFORM_hid_error
};
#endif // HAVE_PLATFORM_BACKEND

#ifdef HAVE_DRIVER_BACKEND
static const struct hidapi_backend DRIVER_Backend = {
    (void *)DRIVER_hid_write,
    (void *)DRIVER_hid_read_timeout,
    (void *)DRIVER_hid_read,
    (void *)DRIVER_hid_set_nonblocking,
    (void *)DRIVER_hid_send_feature_report,
    (void *)DRIVER_hid_get_feature_report,
    (void *)DRIVER_hid_get_input_report,
    (void *)DRIVER_hid_close,
    (void *)DRIVER_hid_get_manufacturer_string,
    (void *)DRIVER_hid_get_product_string,
    (void *)DRIVER_hid_get_serial_number_string,
    (void *)DRIVER_hid_get_indexed_string,
    (void *)DRIVER_hid_get_device_info,
    (void *)DRIVER_hid_get_report_descriptor,
    (void *)DRIVER_hid_error
};
#endif // HAVE_DRIVER_BACKEND

#ifdef HAVE_LIBUSB
static const struct hidapi_backend LIBUSB_Backend = {
    (void *)LIBUSB_hid_write,
    (void *)LIBUSB_hid_read_timeout,
    (void *)LIBUSB_hid_read,
    (void *)LIBUSB_hid_set_nonblocking,
    (void *)LIBUSB_hid_send_feature_report,
    (void *)LIBUSB_hid_get_feature_report,
    (void *)LIBUSB_hid_get_input_report,
    (void *)LIBUSB_hid_close,
    (void *)LIBUSB_hid_get_manufacturer_string,
    (void *)LIBUSB_hid_get_product_string,
    (void *)LIBUSB_hid_get_serial_number_string,
    (void *)LIBUSB_hid_get_indexed_string,
    (void *)LIBUSB_hid_get_device_info,
    (void *)LIBUSB_hid_get_report_descriptor,
    (void *)LIBUSB_hid_error
};
#endif // HAVE_LIBUSB

struct SDL_hid_device
{
    void *device;
    const struct hidapi_backend *backend;
    SDL_hid_device_info info;
};

#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)

static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
{
    SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));
    SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, true);
    wrapper->device = device;
    wrapper->backend = backend;
    SDL_zero(wrapper->info);
    return wrapper;
}

#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB

static void DeleteHIDDeviceWrapper(SDL_hid_device *wrapper)
{
    SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, false);
    SDL_free(wrapper->info.path);
    SDL_free(wrapper->info.serial_number);
    SDL_free(wrapper->info.manufacturer_string);
    SDL_free(wrapper->info.product_string);
    SDL_free(wrapper);
}

#define CHECK_DEVICE_MAGIC(device, result)                          \
    if (!SDL_ObjectValid(device, SDL_OBJECT_TYPE_HIDAPI_DEVICE)) {  \
        SDL_SetError("Invalid device");                             \
        return result;                                              \
    }

#define COPY_IF_EXISTS(var)                \
    if (pSrc->var != NULL) {               \
        pDst->var = SDL_strdup(pSrc->var); \
    } else {                               \
        pDst->var = NULL;                  \
    }
#define WCOPY_IF_EXISTS(var)               \
    if (pSrc->var != NULL) {               \
        pDst->var = SDL_wcsdup(pSrc->var); \
    } else {                               \
        pDst->var = NULL;                  \
    }

static void CopyHIDDeviceInfo(struct hid_device_info *pSrc, struct SDL_hid_device_info *pDst)
{
    COPY_IF_EXISTS(path)
    pDst->vendor_id = pSrc->vendor_id;
    pDst->product_id = pSrc->product_id;
    WCOPY_IF_EXISTS(serial_number)
    pDst->release_number = pSrc->release_number;
    WCOPY_IF_EXISTS(manufacturer_string)
    WCOPY_IF_EXISTS(product_string)
    pDst->usage_page = pSrc->usage_page;
    pDst->usage = pSrc->usage;
    pDst->interface_number = pSrc->interface_number;
    pDst->interface_class = pSrc->interface_class;
    pDst->interface_subclass = pSrc->interface_subclass;
    pDst->interface_protocol = pSrc->interface_protocol;
    pDst->bus_type = (SDL_hid_bus_type)pSrc->bus_type;
    pDst->next = NULL;
}

#undef COPY_IF_EXISTS
#undef WCOPY_IF_EXISTS

static int SDL_hidapi_refcount = 0;
static bool SDL_hidapi_only_controllers;
static char *SDL_hidapi_ignored_devices = NULL;

static void SDLCALL OnlyControllersChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
    SDL_hidapi_only_controllers = SDL_GetStringBoolean(hint, true);
}

static void SDLCALL IgnoredDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
    if (SDL_hidapi_ignored_devices) {
        SDL_free(SDL_hidapi_ignored_devices);
    }
    if (hint && *hint) {
        SDL_hidapi_ignored_devices = SDL_strdup(hint);
    } else {
        SDL_hidapi_ignored_devices = NULL;
    }
}

bool SDL_HIDAPI_ShouldIgnoreDevice(int bus, Uint16 vendor_id, Uint16 product_id, Uint16 usage_page, Uint16 usage)
{
    // See if there are any devices we should skip in enumeration
    if (SDL_hidapi_only_controllers && usage_page) {
        if (vendor_id == USB_VENDOR_VALVE) {
            // Ignore the mouse/keyboard interface on Steam Controllers
            if (
#ifdef SDL_PLATFORM_WIN32
                // Check the usage page and usage on both USB and Bluetooth
#else
                // Only check the usage page and usage on USB
                bus == HID_API_BUS_USB &&
#endif
                usage_page == USB_USAGEPAGE_GENERIC_DESKTOP &&
                (usage == USB_USAGE_GENERIC_KEYBOARD || usage == USB_USAGE_GENERIC_MOUSE)) {
                return true;
            }
        } else if (usage_page == USB_USAGEPAGE_GENERIC_DESKTOP &&
                   (usage == USB_USAGE_GENERIC_JOYSTICK || usage == USB_USAGE_GENERIC_GAMEPAD || usage == USB_USAGE_GENERIC_MULTIAXISCONTROLLER)) {
            // This is a controller
        } else {
            return true;
        }
    }
    if (SDL_hidapi_ignored_devices) {
        char vendor_match[16], product_match[16];
        SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", vendor_id);
        SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", vendor_id, product_id);
        if (SDL_strcasestr(SDL_hidapi_ignored_devices, vendor_match) ||
            SDL_strcasestr(SDL_hidapi_ignored_devices, product_match)) {
            return true;
        }
    }
    return false;
}

int SDL_hid_init(void)
{
    int attempts = 0, success = 0;

    if (SDL_hidapi_refcount > 0) {
        ++SDL_hidapi_refcount;
        return 0;
    }

    SDL_AddHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL);
    SDL_AddHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);

#ifdef SDL_USE_LIBUDEV
    if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_UDEV, true)) {
        SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
                     "udev disabled by SDL_HINT_HIDAPI_UDEV");
        linux_enumeration_method = ENUMERATION_FALLBACK;
    } else if (SDL_GetSandbox() != SDL_SANDBOX_NONE) {
        SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
                     "Container detected, disabling HIDAPI udev integration");
        linux_enumeration_method = ENUMERATION_FALLBACK;
    } else {
        SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
                     "Using udev for HIDAPI joystick device discovery");
        linux_enumeration_method = ENUMERATION_LIBUDEV;
    }
#endif

    use_libusb_whitelist = SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB_WHITELIST,
                                              SDL_HINT_HIDAPI_LIBUSB_WHITELIST_DEFAULT);
#ifdef HAVE_LIBUSB
    if (!SDL_GetHintBoolean(SDL_HINT_HIDAPI_LIBUSB, true)) {
        SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
                     "libusb disabled with SDL_HINT_HIDAPI_LIBUSB");
        libusb_ctx.libhandle = NULL;
    } else {
        ++attempts;
#ifdef SDL_LIBUSB_DYNAMIC
        libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
#else
        libusb_ctx.libhandle = (void *)1;
#endif
        if (libusb_ctx.libhandle != NULL) {
            bool loaded = true;
#ifdef SDL_LIBUSB_DYNAMIC
#define LOAD_LIBUSB_SYMBOL(type, func)                                                        \
    if (!(libusb_ctx.func = (type)SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) { \
        loaded = false;                                                                   \
    }
#else
#define LOAD_LIBUSB_SYMBOL(type, func) \
    libusb_ctx.func = libusb_##func;
#endif
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init)
            LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit)
            LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list)
            LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor)
            LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor)
            LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len), get_port_numbers)
            LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open)
            LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close)
            LOAD_LIBUSB_SYMBOL(libusb_device * (LIBUSB_CALL *)(libusb_device_handle *dev_handle), get_device)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting)
            LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer)
            LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, uint8_t, uint8_t, uint16_t, uint16_t, unsigned char *, uint16_t, unsigned int), control_transfer)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events)
            LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed)
            LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name)
#undef LOAD_LIBUSB_SYMBOL

            if (!loaded) {
#ifdef SDL_LIBUSB_DYNAMIC
                SDL_UnloadObject(libusb_ctx.libhandle);
#endif
                libusb_ctx.libhandle = NULL;
                // SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function");
            } else if (LIBUSB_hid_init() < 0) {
#ifdef SDL_LIBUSB_DYNAMIC
                SDL_UnloadObject(libusb_ctx.libhandle);
#endif
                libusb_ctx.libhandle = NULL;
            } else {
                ++success;
            }
        }
    }
#endif // HAVE_LIBUSB

#ifdef HAVE_PLATFORM_BACKEND
    ++attempts;
#ifdef SDL_PLATFORM_LINUX
    udev_ctx = SDL_UDEV_GetUdevSyms();
#endif // __LINUX __
    if (udev_ctx && PLATFORM_hid_init() == 0) {
        ++success;
    }
#endif // HAVE_PLATFORM_BACKEND

    if (attempts > 0 && success == 0) {
        return -1;
    }

#if defined(SDL_PLATFORM_MACOS) && !defined(SDL_HIDAPI_DISABLED)
    hid_darwin_set_open_exclusive(0);
#endif

    ++SDL_hidapi_refcount;
    return 0;
}

int SDL_hid_exit(void)
{
    int result = 0;

    if (SDL_hidapi_refcount == 0) {
        return 0;
    }
    --SDL_hidapi_refcount;
    if (SDL_hidapi_refcount > 0) {
        return 0;
    }
    SDL_hidapi_refcount = 0;

#ifndef SDL_HIDAPI_DISABLED
    HIDAPI_ShutdownDiscovery();
#endif

#ifdef HAVE_PLATFORM_BACKEND
    if (udev_ctx) {
        result |= PLATFORM_hid_exit();
    }
#ifdef SDL_PLATFORM_LINUX
    SDL_UDEV_ReleaseUdevSyms();
#endif // __LINUX __
#endif // HAVE_PLATFORM_BACKEND

#ifdef HAVE_LIBUSB
    if (libusb_ctx.libhandle) {
        result |= LIBUSB_hid_exit();
#ifdef SDL_LIBUSB_DYNAMIC
        SDL_UnloadObject(libusb_ctx.libhandle);
#endif
        libusb_ctx.libhandle = NULL;
    }
#endif // HAVE_LIBUSB

    SDL_RemoveHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL);
    SDL_RemoveHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);

    if (SDL_hidapi_ignored_devices) {
        SDL_free(SDL_hidapi_ignored_devices);
        SDL_hidapi_ignored_devices = NULL;
    }

    return result;
}

Uint32 SDL_hid_device_change_count(void)
{
    Uint32 counter = 0;

#ifndef SDL_HIDAPI_DISABLED
    if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {
        return 0;
    }

    HIDAPI_UpdateDiscovery();

    if (SDL_HIDAPI_discovery.m_unDeviceChangeCounter == 0) {
        // Counter wrapped!
        ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
    }
    counter = SDL_HIDAPI_discovery.m_unDeviceChangeCounter;

#endif // !SDL_HIDAPI_DISABLED

    return counter;
}

static void AddDeviceToEnumeration(const char *driver_name, struct hid_device_info *dev, struct SDL_hid_device_info **devs, struct SDL_hid_device_info **last)
{
    struct SDL_hid_device_info *new_dev;

#ifdef DEBUG_HIDAPI
    SDL_Log("Adding %s device to enumeration: %ls %ls 0x%.4hx/0x%.4hx/%d",
            driver_name, dev->manufacturer_string, dev->product_string, dev->vendor_id, dev->product_id, dev->interface_number);
#else
    (void)driver_name;
#endif

    new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
    if (new_dev == NULL) {
        // Don't bother returning an error, get as many devices as possible
        return;
    }
    CopyHIDDeviceInfo(dev, new_dev);

    if ((*last) != NULL) {
        (*last)->next = new_dev;
    } else {
        *devs = new_dev;
    }
    *last = new_dev;
}

#if defined(HAVE_LIBUSB) || defined(HAVE_PLATFORM_BACKEND)
static void RemoveDeviceFromEnumeration(const char *driver_name, struct hid_device_info *dev, struct hid_device_info **devs, void (*free_device_info)(struct hid_device_info *))
{
    struct hid_device_info *last = NULL, *curr, *next;

    for (curr = *devs; curr; curr = next) {
        next = curr->next;

        if (dev->vendor_id == curr->vendor_id &&
            dev->product_id == curr->product_id &&
            (dev->interface_number < 0 || curr->interface_number < 0 || dev->interface_number == curr->interface_number)) {
#ifdef DEBUG_HIDAPI
            SDL_Log("Skipping %s device: %ls %ls 0x%.4hx/0x%.4hx/%d",
                    driver_name, curr->manufacturer_string, curr->product_string, curr->vendor_id, curr->product_id, curr->interface_number);
#else
            (void)driver_name;
#endif
            if (last) {
                last->next = next;
            } else {
                *devs = next;
            }

            curr->next = NULL;
            free_device_info(curr);
            continue;
        }
        last = curr;
    }
}
#endif // HAVE_LIBUSB || HAVE_PLATFORM_BACKEND

#ifdef HAVE_LIBUSB
static void RemoveNonWhitelistedDevicesFromEnumeration(struct hid_device_info **devs, void (*free_device_info)(struct hid_device_info *))
{
    struct hid_device_info *last = NULL, *curr, *next;

    for (curr = *devs; curr; curr = next) {
        next = curr->next;

        if (!IsInWhitelist(curr->vendor_id, curr->product_id)) {
#ifdef DEBUG_HIDAPI
            SDL_Log("Device was not in libusb whitelist, skipping: %ls %ls 0x%.4hx/0x%.4hx/%d",
                    curr->manufacturer_string, curr->product_string, curr->vendor_id, curr->product_id, curr->interface_number);
#endif
            if (last) {
                last->next = next;
            } else {
                *devs = next;
            }

            curr->next = NULL;
            free_device_info(curr);
            continue;
        }
        last = curr;
    }
}
#endif // HAVE_LIBUSB

struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{
    struct hid_device_info *driver_devs = NULL;
    struct hid_device_info *usb_devs = NULL;
    struct hid_device_info *raw_devs = NULL;
    struct hid_device_info *dev;
    struct SDL_hid_device_info *devs = NULL, *last = NULL;

    if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {
        return NULL;
    }

    // Collect the available devices
#ifdef HAVE_DRIVER_BACKEND
    driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);
#endif

#ifdef HAVE_LIBUSB
    if (libusb_ctx.libhandle) {
        usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);

        if (use_libusb_whitelist) {
            RemoveNonWhitelistedDevicesFromEnumeration(&usb_devs,  LIBUSB_hid_free_enumeration);
        }
    }
#endif // HAVE_LIBUSB

#ifdef HAVE_PLATFORM_BACKEND
    if (udev_ctx) {
        raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
    }
#endif

    // Highest priority are custom driver devices
    for (dev = driver_devs; dev; dev = dev->next) {
        AddDeviceToEnumeration("driver", dev, &devs, &last);
#ifdef HAVE_LIBUSB
        RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration);
#endif
#ifdef HAVE_PLATFORM_BACKEND
        RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration);
#endif
    }

    // If whitelist is in effect, libusb has priority, otherwise raw devices do
    if (use_libusb_whitelist) {
        for (dev = usb_devs; dev; dev = dev->next) {
            AddDeviceToEnumeration("libusb", dev, &devs, &last);
#ifdef HAVE_PLATFORM_BACKEND
            RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration);
#endif
        }
        for (dev = raw_devs; dev; dev = dev->next) {
            AddDeviceToEnumeration("platform", dev, &devs, &last);
        }
    } else {
        for (dev = raw_devs; dev; dev = dev->next) {
            AddDeviceToEnumeration("raw", dev, &devs, &last);
#ifdef HAVE_LIBUSB
            RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration);
#endif
        }
        for (dev = usb_devs; dev; dev = dev->next) {
            AddDeviceToEnumeration("libusb", dev, &devs, &last);
        }
    }

#ifdef HAVE_DRIVER_BACKEND
    DRIVER_hid_free_enumeration(driver_devs);
#endif
#ifdef HAVE_LIBUSB
    LIBUSB_hid_free_enumeration(usb_devs);
#endif
#ifdef HAVE_PLATFORM_BACKEND
    PLATFORM_hid_free_enumeration(raw_devs);
#endif

    return devs;
}

void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
{
    while (devs) {
        struct SDL_hid_device_info *next = devs->next;
        SDL_free(devs->path);
        SDL_free(devs->serial_number);
        SDL_free(devs->manufacturer_string);
        SDL_free(devs->product_string);
        SDL_free(devs);
        devs = next;
    }
}

SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
{
#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)
    void *pDevice = NULL;

    if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {
        return NULL;
    }

#ifdef HAVE_PLATFORM_BACKEND
    if (udev_ctx) {
        pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number);
        if (pDevice != NULL) {
            return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
        }
    }
#endif // HAVE_PLATFORM_BACKEND

#ifdef HAVE_DRIVER_BACKEND
    pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number);
    if (pDevice != NULL) {
        return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
    }
#endif // HAVE_DRIVER_BACKEND

#ifdef HAVE_LIBUSB
    if (libusb_ctx.libhandle != NULL) {
        pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);
        if (pDevice != NULL) {
            return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
        }
    }
#endif // HAVE_LIBUSB

#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB

    return NULL;
}

SDL_hid_device *SDL_hid_open_path(const char *path)
{
#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)
    void *pDevice = NULL;

    if (SDL_hidapi_refcount == 0 && SDL_hid_init() < 0) {
        return NULL;
    }

#ifdef HAVE_PLATFORM_BACKEND
    if (udev_ctx) {
        pDevice = PLATFORM_hid_open_path(path);
        if (pDevice != NULL) {
            return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
        }
    }
#endif // HAVE_PLATFORM_BACKEND

#ifdef HAVE_DRIVER_BACKEND
    pDevice = DRIVER_hid_open_path(path);
    if (pDevice != NULL) {
        return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
    }
#endif // HAVE_DRIVER_BACKEND

#ifdef HAVE_LIBUSB
    if (libusb_ctx.libhandle != NULL) {
        pDevice = LIBUSB_hid_open_path(path);
        if (pDevice != NULL) {
            return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
        }
    }
#endif // HAVE_LIBUSB

#endif // HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB

    return NULL;
}

int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_write(device->device, data, length);
}

int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_read_timeout(device->device, data, length, milliseconds);
}

int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_read(device->device, data, length);
}

int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_set_nonblocking(device->device, nonblock);
}

int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_send_feature_report(device->device, data, length);
}

int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_get_feature_report(device->device, data, length);
}

int SDL_hid_get_input_report(SDL_hid_device *device, unsigned char *data, size_t length)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_get_input_report(device->device, data, length);
}

int SDL_hid_close(SDL_hid_device *device)
{
    CHECK_DEVICE_MAGIC(device, -1);

    device->backend->hid_close(device->device);
    DeleteHIDDeviceWrapper(device);
    return 0;
}

int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_get_manufacturer_string(device->device, string, maxlen);
}

int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_get_product_string(device->device, string, maxlen);
}

int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_get_serial_number_string(device->device, string, maxlen);
}

int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen);
}

SDL_hid_device_info *SDL_hid_get_device_info(SDL_hid_device *device)
{
    struct hid_device_info *info;

    CHECK_DEVICE_MAGIC(device, NULL);

    info = device->backend->hid_get_device_info(device->device);
    if (info) {
        CopyHIDDeviceInfo(info, &device->info);
        return &device->info;
    } else {
        return NULL;
    }
}

int SDL_hid_get_report_descriptor(SDL_hid_device *device, unsigned char *buf, size_t buf_size)
{
    CHECK_DEVICE_MAGIC(device, -1);

    return device->backend->hid_get_report_descriptor(device->device, buf, buf_size);
}

void SDL_hid_ble_scan(bool active)
{
#if !defined(SDL_HIDAPI_DISABLED) && (defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS))
    extern void hid_ble_scan(int bStart);
    hid_ble_scan(active);
#endif
}

#ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
// This is needed to enable input for Nyko and EVORETRO GameCube adaptors
void SDL_EnableGameCubeAdaptors(void)
{
#ifdef HAVE_LIBUSB
    libusb_context *context = NULL;
    libusb_device **devs = NULL;
    libusb_device_handle *handle = NULL;
    struct libusb_device_descriptor desc;
    ssize_t i, num_devs;
    int kernel_detached = 0;

    if (libusb_ctx.libhandle == NULL) {
        return;
    }

    if (libusb_ctx.init(&context) == 0) {
        num_devs = libusb_ctx.get_device_list(context, &devs);
        for (i = 0; i < num_devs; ++i) {
            if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) {
                continue;
            }

            if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
                continue;
            }

            if (libusb_ctx.open(devs[i], &handle) != 0) {
                continue;
            }

            if (libusb_ctx.kernel_driver_active(handle, 0)) {
                if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) {
                    kernel_detached = 1;
                }
            }

            if (libusb_ctx.claim_interface(handle, 0) == 0) {
                libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
                libusb_ctx.release_interface(handle, 0);
            }

            if (kernel_detached) {
                libusb_ctx.attach_kernel_driver(handle, 0);
            }

            libusb_ctx.close(handle);
        }

        libusb_ctx.free_device_list(devs, 1);

        libusb_ctx.exit(context);
    }
#endif // HAVE_LIBUSB
}
#endif // HAVE_ENABLE_GAMECUBE_ADAPTORS