SDL_hidapi.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* Original hybrid wrapper for Linux by Valve Software. Their original notes:
  19. *
  20. * The libusb version doesn't support Bluetooth, but not all Linux
  21. * distributions allow access to /dev/hidraw*
  22. *
  23. * This merges the two, at a small performance cost, until distributions
  24. * have granted access to /dev/hidraw*
  25. */
  26. #include "SDL_internal.h"
  27. #include "SDL_hidapi_c.h"
  28. #include "../joystick/usb_ids.h"
  29. #include "../SDL_hints_c.h"
  30. /* Initial type declarations */
  31. #define HID_API_NO_EXPORT_DEFINE /* do not export hidapi procedures */
  32. #include "hidapi/hidapi.h"
  33. #ifndef SDL_HIDAPI_DISABLED
  34. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  35. #include "../core/windows/SDL_windows.h"
  36. #endif
  37. #ifdef SDL_PLATFORM_MACOS
  38. #include <CoreFoundation/CoreFoundation.h>
  39. #include <mach/mach.h>
  40. #include <IOKit/IOKitLib.h>
  41. #include <IOKit/hid/IOHIDDevice.h>
  42. #include <IOKit/usb/USBSpec.h>
  43. #include <AvailabilityMacros.h>
  44. /* Things named "Master" were renamed to "Main" in macOS 12.0's SDK. */
  45. #if MAC_OS_X_VERSION_MIN_REQUIRED < 120000
  46. #define kIOMainPortDefault kIOMasterPortDefault
  47. #endif
  48. #endif
  49. #include "../core/linux/SDL_udev.h"
  50. #ifdef SDL_USE_LIBUDEV
  51. #include <poll.h>
  52. #include "../core/linux/SDL_sandbox.h"
  53. #endif
  54. #ifdef HAVE_INOTIFY
  55. #include <string.h> /* strerror */
  56. #include <errno.h> /* errno */
  57. #include <fcntl.h>
  58. #include <limits.h> /* For the definition of NAME_MAX */
  59. #include <sys/inotify.h>
  60. #endif
  61. #if defined(SDL_USE_LIBUDEV) || defined(HAVE_INOTIFY)
  62. #include <unistd.h>
  63. #endif
  64. #ifdef SDL_USE_LIBUDEV
  65. typedef enum
  66. {
  67. ENUMERATION_UNSET,
  68. ENUMERATION_LIBUDEV,
  69. ENUMERATION_FALLBACK
  70. } LinuxEnumerationMethod;
  71. static LinuxEnumerationMethod linux_enumeration_method = ENUMERATION_UNSET;
  72. #endif
  73. #ifdef HAVE_INOTIFY
  74. static int inotify_fd = -1;
  75. #endif
  76. #ifdef SDL_USE_LIBUDEV
  77. static const SDL_UDEV_Symbols *usyms = NULL;
  78. #endif
  79. static struct
  80. {
  81. SDL_bool m_bInitialized;
  82. Uint32 m_unDeviceChangeCounter;
  83. SDL_bool m_bCanGetNotifications;
  84. Uint64 m_unLastDetect;
  85. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  86. SDL_ThreadID m_nThreadID;
  87. WNDCLASSEXA m_wndClass;
  88. HWND m_hwndMsg;
  89. HDEVNOTIFY m_hNotify;
  90. double m_flLastWin32MessageCheck;
  91. #endif
  92. #ifdef SDL_PLATFORM_MACOS
  93. IONotificationPortRef m_notificationPort;
  94. mach_port_t m_notificationMach;
  95. #endif
  96. #ifdef SDL_USE_LIBUDEV
  97. struct udev *m_pUdev;
  98. struct udev_monitor *m_pUdevMonitor;
  99. int m_nUdevFd;
  100. #endif
  101. } SDL_HIDAPI_discovery;
  102. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  103. struct _DEV_BROADCAST_HDR
  104. {
  105. DWORD dbch_size;
  106. DWORD dbch_devicetype;
  107. DWORD dbch_reserved;
  108. };
  109. typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
  110. {
  111. DWORD dbcc_size;
  112. DWORD dbcc_devicetype;
  113. DWORD dbcc_reserved;
  114. GUID dbcc_classguid;
  115. char dbcc_name[1];
  116. } DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;
  117. typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
  118. #define DBT_DEVICEARRIVAL 0x8000 /* system detected a new device */
  119. #define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
  120. #define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
  121. #define DBT_DEVNODES_CHANGED 0x0007
  122. #define DBT_CONFIGCHANGED 0x0018
  123. #define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
  124. #define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
  125. #include <initguid.h>
  126. DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
  127. static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  128. {
  129. switch (message) {
  130. case WM_DEVICECHANGE:
  131. switch (wParam) {
  132. case DBT_DEVICEARRIVAL:
  133. case DBT_DEVICEREMOVECOMPLETE:
  134. if (((DEV_BROADCAST_HDR *)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  135. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  136. }
  137. break;
  138. }
  139. return TRUE;
  140. }
  141. return DefWindowProc(hwnd, message, wParam, lParam);
  142. }
  143. #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) */
  144. #ifdef SDL_PLATFORM_MACOS
  145. static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
  146. {
  147. /* Must drain the iterator, or we won't receive new notifications */
  148. io_object_t entry;
  149. while ((entry = IOIteratorNext(portIterator)) != 0) {
  150. IOObjectRelease(entry);
  151. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  152. }
  153. }
  154. #endif /* SDL_PLATFORM_MACOS */
  155. #ifdef HAVE_INOTIFY
  156. #ifdef HAVE_INOTIFY_INIT1
  157. static int SDL_inotify_init1(void)
  158. {
  159. return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  160. }
  161. #else
  162. static int SDL_inotify_init1(void)
  163. {
  164. int fd = inotify_init();
  165. if (fd < 0) {
  166. return -1;
  167. }
  168. fcntl(fd, F_SETFL, O_NONBLOCK);
  169. fcntl(fd, F_SETFD, FD_CLOEXEC);
  170. return fd;
  171. }
  172. #endif
  173. static int StrHasPrefix(const char *string, const char *prefix)
  174. {
  175. return SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0;
  176. }
  177. static int StrIsInteger(const char *string)
  178. {
  179. const char *p;
  180. if (*string == '\0') {
  181. return 0;
  182. }
  183. for (p = string; *p != '\0'; p++) {
  184. if (*p < '0' || *p > '9') {
  185. return 0;
  186. }
  187. }
  188. return 1;
  189. }
  190. #endif /* HAVE_INOTIFY */
  191. static void HIDAPI_InitializeDiscovery(void)
  192. {
  193. SDL_HIDAPI_discovery.m_bInitialized = SDL_TRUE;
  194. SDL_HIDAPI_discovery.m_unDeviceChangeCounter = 1;
  195. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
  196. SDL_HIDAPI_discovery.m_unLastDetect = 0;
  197. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  198. SDL_HIDAPI_discovery.m_nThreadID = SDL_GetCurrentThreadID();
  199. SDL_zero(SDL_HIDAPI_discovery.m_wndClass);
  200. SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
  201. SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
  202. SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; /* This function is called by windows */
  203. SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);
  204. RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
  205. SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  206. {
  207. DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;
  208. SDL_zero(devBroadcast);
  209. devBroadcast.dbcc_size = sizeof(devBroadcast);
  210. devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  211. devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
  212. /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
  213. * but that seems to be necessary to get a notice after each individual usb input device actually
  214. * installs, rather than just as the composite device is seen.
  215. */
  216. SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification(SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
  217. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_hNotify != 0);
  218. }
  219. #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) */
  220. #ifdef SDL_PLATFORM_MACOS
  221. SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMainPortDefault);
  222. if (SDL_HIDAPI_discovery.m_notificationPort) {
  223. {
  224. io_iterator_t portIterator = 0;
  225. io_object_t entry;
  226. IOReturn result = IOServiceAddMatchingNotification(
  227. SDL_HIDAPI_discovery.m_notificationPort,
  228. kIOFirstMatchNotification,
  229. IOServiceMatching(kIOHIDDeviceKey),
  230. CallbackIOServiceFunc, NULL, &portIterator);
  231. if (result == 0) {
  232. /* Must drain the existing iterator, or we won't receive new notifications */
  233. while ((entry = IOIteratorNext(portIterator)) != 0) {
  234. IOObjectRelease(entry);
  235. }
  236. } else {
  237. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  238. SDL_HIDAPI_discovery.m_notificationPort = nil;
  239. }
  240. }
  241. {
  242. io_iterator_t portIterator = 0;
  243. io_object_t entry;
  244. IOReturn result = IOServiceAddMatchingNotification(
  245. SDL_HIDAPI_discovery.m_notificationPort,
  246. kIOTerminatedNotification,
  247. IOServiceMatching(kIOHIDDeviceKey),
  248. CallbackIOServiceFunc, NULL, &portIterator);
  249. if (result == 0) {
  250. /* Must drain the existing iterator, or we won't receive new notifications */
  251. while ((entry = IOIteratorNext(portIterator)) != 0) {
  252. IOObjectRelease(entry);
  253. }
  254. } else {
  255. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  256. SDL_HIDAPI_discovery.m_notificationPort = nil;
  257. }
  258. }
  259. }
  260. SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
  261. if (SDL_HIDAPI_discovery.m_notificationPort) {
  262. SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
  263. }
  264. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);
  265. #endif /* SDL_PLATFORM_MACOS */
  266. #ifdef SDL_USE_LIBUDEV
  267. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  268. SDL_HIDAPI_discovery.m_pUdev = NULL;
  269. SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
  270. SDL_HIDAPI_discovery.m_nUdevFd = -1;
  271. usyms = SDL_UDEV_GetUdevSyms();
  272. if (usyms != NULL) {
  273. SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
  274. if (SDL_HIDAPI_discovery.m_pUdev != NULL) {
  275. SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
  276. if (SDL_HIDAPI_discovery.m_pUdevMonitor != NULL) {
  277. usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
  278. SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
  279. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
  280. }
  281. }
  282. }
  283. } else
  284. #endif /* SDL_USE_LIBUDEV */
  285. {
  286. #ifdef HAVE_INOTIFY
  287. inotify_fd = SDL_inotify_init1();
  288. if (inotify_fd < 0) {
  289. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  290. "Unable to initialize inotify, falling back to polling: %s",
  291. strerror(errno));
  292. return;
  293. }
  294. /* We need to watch for attribute changes in addition to
  295. * creation, because when a device is first created, it has
  296. * permissions that we can't read. When udev chmods it to
  297. * something that we maybe *can* read, we'll get an
  298. * IN_ATTRIB event to tell us. */
  299. if (inotify_add_watch(inotify_fd, "/dev",
  300. IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
  301. close(inotify_fd);
  302. inotify_fd = -1;
  303. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  304. "Unable to add inotify watch, falling back to polling: %s",
  305. strerror(errno));
  306. return;
  307. }
  308. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
  309. #endif /* HAVE_INOTIFY */
  310. }
  311. }
  312. static void HIDAPI_UpdateDiscovery(void)
  313. {
  314. if (!SDL_HIDAPI_discovery.m_bInitialized) {
  315. HIDAPI_InitializeDiscovery();
  316. }
  317. if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) {
  318. const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
  319. Uint64 now = SDL_GetTicks();
  320. if (!SDL_HIDAPI_discovery.m_unLastDetect || now >= (SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) {
  321. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  322. SDL_HIDAPI_discovery.m_unLastDetect = now;
  323. }
  324. return;
  325. }
  326. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  327. #if 0 /* just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan. */
  328. /* We'll only get messages on the same thread that created the window */
  329. if (SDL_GetCurrentThreadID() == SDL_HIDAPI_discovery.m_nThreadID) {
  330. MSG msg;
  331. while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) {
  332. if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) {
  333. TranslateMessage(&msg);
  334. DispatchMessage(&msg);
  335. }
  336. }
  337. }
  338. #endif
  339. #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK) */
  340. #ifdef SDL_PLATFORM_MACOS
  341. if (SDL_HIDAPI_discovery.m_notificationPort) {
  342. struct
  343. {
  344. mach_msg_header_t hdr;
  345. char payload[4096];
  346. } msg;
  347. 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) {
  348. IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);
  349. }
  350. }
  351. #endif
  352. #ifdef SDL_USE_LIBUDEV
  353. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  354. if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
  355. /* Drain all notification events.
  356. * We don't expect a lot of device notifications so just
  357. * do a new discovery on any kind or number of notifications.
  358. * This could be made more restrictive if necessary.
  359. */
  360. for (;;) {
  361. struct pollfd PollUdev;
  362. struct udev_device *pUdevDevice;
  363. PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
  364. PollUdev.events = POLLIN;
  365. if (poll(&PollUdev, 1, 0) != 1) {
  366. break;
  367. }
  368. pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
  369. if (pUdevDevice) {
  370. const char *action = NULL;
  371. action = usyms->udev_device_get_action(pUdevDevice);
  372. if (action == NULL || SDL_strcmp(action, "add") == 0 || SDL_strcmp(action, "remove") == 0) {
  373. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  374. }
  375. usyms->udev_device_unref(pUdevDevice);
  376. }
  377. }
  378. }
  379. } else
  380. #endif /* SDL_USE_LIBUDEV */
  381. {
  382. #ifdef HAVE_INOTIFY
  383. if (inotify_fd >= 0) {
  384. union
  385. {
  386. struct inotify_event event;
  387. char storage[4096];
  388. char enough_for_inotify[sizeof(struct inotify_event) + NAME_MAX + 1];
  389. } buf;
  390. ssize_t bytes;
  391. size_t remain = 0;
  392. size_t len;
  393. bytes = read(inotify_fd, &buf, sizeof(buf));
  394. if (bytes > 0) {
  395. remain = (size_t)bytes;
  396. }
  397. while (remain > 0) {
  398. if (buf.event.len > 0) {
  399. if (StrHasPrefix(buf.event.name, "hidraw") &&
  400. StrIsInteger(buf.event.name + SDL_strlen("hidraw"))) {
  401. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  402. /* We found an hidraw change. We still continue to
  403. * drain the inotify fd to avoid leaving old
  404. * notifications in the queue. */
  405. }
  406. }
  407. len = sizeof(struct inotify_event) + buf.event.len;
  408. remain -= len;
  409. if (remain != 0) {
  410. SDL_memmove(&buf.storage[0], &buf.storage[len], remain);
  411. }
  412. }
  413. }
  414. #endif /* HAVE_INOTIFY */
  415. }
  416. }
  417. static void HIDAPI_ShutdownDiscovery(void)
  418. {
  419. if (!SDL_HIDAPI_discovery.m_bInitialized) {
  420. return;
  421. }
  422. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  423. if (SDL_HIDAPI_discovery.m_hNotify) {
  424. UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
  425. }
  426. if (SDL_HIDAPI_discovery.m_hwndMsg) {
  427. DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
  428. }
  429. UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
  430. #endif
  431. #ifdef SDL_PLATFORM_MACOS
  432. if (SDL_HIDAPI_discovery.m_notificationPort) {
  433. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  434. }
  435. #endif
  436. #ifdef SDL_USE_LIBUDEV
  437. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  438. if (usyms) {
  439. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  440. usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
  441. }
  442. if (SDL_HIDAPI_discovery.m_pUdev) {
  443. usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
  444. }
  445. SDL_UDEV_ReleaseUdevSyms();
  446. usyms = NULL;
  447. }
  448. } else
  449. #endif /* SDL_USE_LIBUDEV */
  450. {
  451. #ifdef HAVE_INOTIFY
  452. if (inotify_fd >= 0) {
  453. close(inotify_fd);
  454. inotify_fd = -1;
  455. }
  456. #endif
  457. }
  458. SDL_HIDAPI_discovery.m_bInitialized = SDL_FALSE;
  459. }
  460. /* Platform HIDAPI Implementation */
  461. #define HIDAPI_USING_SDL_RUNTIME
  462. #define HIDAPI_IGNORE_DEVICE(BUS, VID, PID, USAGE_PAGE, USAGE) \
  463. SDL_HIDAPI_ShouldIgnoreDevice(BUS, VID, PID, USAGE_PAGE, USAGE)
  464. struct PLATFORM_hid_device_;
  465. typedef struct PLATFORM_hid_device_ PLATFORM_hid_device;
  466. #define api_version PLATFORM_api_version
  467. #define create_device_info_for_device PLATFORM_create_device_info_for_device
  468. #define free_hid_device PLATFORM_free_hid_device
  469. #define hid_close PLATFORM_hid_close
  470. #define hid_device PLATFORM_hid_device
  471. #define hid_device_ PLATFORM_hid_device_
  472. #define hid_enumerate PLATFORM_hid_enumerate
  473. #define hid_error PLATFORM_hid_error
  474. #define hid_exit PLATFORM_hid_exit
  475. #define hid_free_enumeration PLATFORM_hid_free_enumeration
  476. #define hid_get_device_info PLATFORM_hid_get_device_info
  477. #define hid_get_feature_report PLATFORM_hid_get_feature_report
  478. #define hid_get_indexed_string PLATFORM_hid_get_indexed_string
  479. #define hid_get_input_report PLATFORM_hid_get_input_report
  480. #define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string
  481. #define hid_get_product_string PLATFORM_hid_get_product_string
  482. #define hid_get_report_descriptor PLATFORM_hid_get_report_descriptor
  483. #define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
  484. #define hid_init PLATFORM_hid_init
  485. #define hid_open_path PLATFORM_hid_open_path
  486. #define hid_open PLATFORM_hid_open
  487. #define hid_read PLATFORM_hid_read
  488. #define hid_read_timeout PLATFORM_hid_read_timeout
  489. #define hid_send_feature_report PLATFORM_hid_send_feature_report
  490. #define hid_set_nonblocking PLATFORM_hid_set_nonblocking
  491. #define hid_version PLATFORM_hid_version
  492. #define hid_version_str PLATFORM_hid_version_str
  493. #define hid_write PLATFORM_hid_write
  494. #define input_report PLATFORM_input_report
  495. #define make_path PLATFORM_make_path
  496. #define new_hid_device PLATFORM_new_hid_device
  497. #define read_thread PLATFORM_read_thread
  498. #define return_data PLATFORM_return_data
  499. #ifdef SDL_PLATFORM_LINUX
  500. #include "SDL_hidapi_linux.h"
  501. #elif defined(SDL_PLATFORM_NETBSD)
  502. #include "SDL_hidapi_netbsd.h"
  503. #elif defined(SDL_PLATFORM_MACOS)
  504. #include "SDL_hidapi_mac.h"
  505. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  506. #include "SDL_hidapi_windows.h"
  507. #elif defined(SDL_PLATFORM_ANDROID)
  508. #include "SDL_hidapi_android.h"
  509. #elif defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)
  510. #include "SDL_hidapi_ios.h"
  511. #endif
  512. #undef api_version
  513. #undef create_device_info_for_device
  514. #undef free_hid_device
  515. #undef hid_close
  516. #undef hid_device
  517. #undef hid_device_
  518. #undef hid_enumerate
  519. #undef hid_error
  520. #undef hid_exit
  521. #undef hid_free_enumeration
  522. #undef hid_get_device_info
  523. #undef hid_get_feature_report
  524. #undef hid_get_indexed_string
  525. #undef hid_get_input_report
  526. #undef hid_get_manufacturer_string
  527. #undef hid_get_product_string
  528. #undef hid_get_report_descriptor
  529. #undef hid_get_serial_number_string
  530. #undef hid_init
  531. #undef hid_open
  532. #undef hid_open_path
  533. #undef hid_read
  534. #undef hid_read_timeout
  535. #undef hid_send_feature_report
  536. #undef hid_set_nonblocking
  537. #undef hid_version
  538. #undef hid_version_str
  539. #undef hid_write
  540. #undef input_report
  541. #undef make_path
  542. #undef new_hid_device
  543. #undef read_thread
  544. #undef return_data
  545. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  546. #define HAVE_DRIVER_BACKEND 1
  547. #endif
  548. #ifdef HAVE_DRIVER_BACKEND
  549. /* DRIVER HIDAPI Implementation */
  550. struct DRIVER_hid_device_;
  551. typedef struct DRIVER_hid_device_ DRIVER_hid_device;
  552. #define hid_close DRIVER_hid_close
  553. #define hid_device DRIVER_hid_device
  554. #define hid_device_ DRIVER_hid_device_
  555. #define hid_enumerate DRIVER_hid_enumerate
  556. #define hid_error DRIVER_hid_error
  557. #define hid_exit DRIVER_hid_exit
  558. #define hid_free_enumeration DRIVER_hid_free_enumeration
  559. #define hid_get_device_info DRIVER_hid_get_device_info
  560. #define hid_get_feature_report DRIVER_hid_get_feature_report
  561. #define hid_get_indexed_string DRIVER_hid_get_indexed_string
  562. #define hid_get_input_report DRIVER_hid_get_input_report
  563. #define hid_get_manufacturer_string DRIVER_hid_get_manufacturer_string
  564. #define hid_get_product_string DRIVER_hid_get_product_string
  565. #define hid_get_report_descriptor DRIVER_hid_get_report_descriptor
  566. #define hid_get_serial_number_string DRIVER_hid_get_serial_number_string
  567. #define hid_init DRIVER_hid_init
  568. #define hid_open DRIVER_hid_open
  569. #define hid_open_path DRIVER_hid_open_path
  570. #define hid_read DRIVER_hid_read
  571. #define hid_read_timeout DRIVER_hid_read_timeout
  572. #define hid_send_feature_report DRIVER_hid_send_feature_report
  573. #define hid_set_nonblocking DRIVER_hid_set_nonblocking
  574. #define hid_write DRIVER_hid_write
  575. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  576. #include "SDL_hidapi_steamxbox.h"
  577. #else
  578. #error Need a driver hid.c for this platform!
  579. #endif
  580. #undef hid_close
  581. #undef hid_device
  582. #undef hid_device_
  583. #undef hid_enumerate
  584. #undef hid_error
  585. #undef hid_exit
  586. #undef hid_free_enumeration
  587. #undef hid_get_device_info
  588. #undef hid_get_feature_report
  589. #undef hid_get_indexed_string
  590. #undef hid_get_input_report
  591. #undef hid_get_manufacturer_string
  592. #undef hid_get_product_string
  593. #undef hid_get_report_descriptor
  594. #undef hid_get_serial_number_string
  595. #undef hid_init
  596. #undef hid_open
  597. #undef hid_open_path
  598. #undef hid_read
  599. #undef hid_read_timeout
  600. #undef hid_send_feature_report
  601. #undef hid_set_nonblocking
  602. #undef hid_write
  603. #endif /* HAVE_DRIVER_BACKEND */
  604. #ifdef HAVE_LIBUSB
  605. /* libusb HIDAPI Implementation */
  606. /* Include this now, for our dynamically-loaded libusb context */
  607. #include <libusb.h>
  608. static struct
  609. {
  610. void *libhandle;
  611. /* *INDENT-OFF* */ /* clang-format off */
  612. int (LIBUSB_CALL *init)(libusb_context **ctx);
  613. void (LIBUSB_CALL *exit)(libusb_context *ctx);
  614. ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list);
  615. void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices);
  616. int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
  617. int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);
  618. int (LIBUSB_CALL *get_config_descriptor)(
  619. libusb_device *dev,
  620. uint8_t config_index,
  621. struct libusb_config_descriptor **config
  622. );
  623. void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config);
  624. uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev);
  625. int (LIBUSB_CALL *get_port_numbers)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len);
  626. uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev);
  627. int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle);
  628. void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle);
  629. libusb_device *(LIBUSB_CALL *get_device)(libusb_device_handle *dev_handle);
  630. int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number);
  631. int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number);
  632. int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
  633. int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  634. int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  635. int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
  636. struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets);
  637. int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer);
  638. int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer);
  639. void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer);
  640. int (LIBUSB_CALL *control_transfer)(
  641. libusb_device_handle *dev_handle,
  642. uint8_t request_type,
  643. uint8_t bRequest,
  644. uint16_t wValue,
  645. uint16_t wIndex,
  646. unsigned char *data,
  647. uint16_t wLength,
  648. unsigned int timeout
  649. );
  650. int (LIBUSB_CALL *interrupt_transfer)(
  651. libusb_device_handle *dev_handle,
  652. unsigned char endpoint,
  653. unsigned char *data,
  654. int length,
  655. int *actual_length,
  656. unsigned int timeout
  657. );
  658. int (LIBUSB_CALL *handle_events)(libusb_context *ctx);
  659. int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed);
  660. const char * (LIBUSB_CALL *error_name)(int errcode);
  661. /* *INDENT-ON* */ /* clang-format on */
  662. } libusb_ctx;
  663. #define libusb_init libusb_ctx.init
  664. #define libusb_exit libusb_ctx.exit
  665. #define libusb_get_device_list libusb_ctx.get_device_list
  666. #define libusb_free_device_list libusb_ctx.free_device_list
  667. #define libusb_get_device_descriptor libusb_ctx.get_device_descriptor
  668. #define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
  669. #define libusb_get_config_descriptor libusb_ctx.get_config_descriptor
  670. #define libusb_free_config_descriptor libusb_ctx.free_config_descriptor
  671. #define libusb_get_bus_number libusb_ctx.get_bus_number
  672. #define libusb_get_port_numbers libusb_ctx.get_port_numbers
  673. #define libusb_get_device_address libusb_ctx.get_device_address
  674. #define libusb_open libusb_ctx.open
  675. #define libusb_close libusb_ctx.close
  676. #define libusb_get_device libusb_ctx.get_device
  677. #define libusb_claim_interface libusb_ctx.claim_interface
  678. #define libusb_release_interface libusb_ctx.release_interface
  679. #define libusb_kernel_driver_active libusb_ctx.kernel_driver_active
  680. #define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver
  681. #define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver
  682. #define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting
  683. #define libusb_alloc_transfer libusb_ctx.alloc_transfer
  684. #define libusb_submit_transfer libusb_ctx.submit_transfer
  685. #define libusb_cancel_transfer libusb_ctx.cancel_transfer
  686. #define libusb_free_transfer libusb_ctx.free_transfer
  687. #define libusb_control_transfer libusb_ctx.control_transfer
  688. #define libusb_interrupt_transfer libusb_ctx.interrupt_transfer
  689. #define libusb_handle_events libusb_ctx.handle_events
  690. #define libusb_handle_events_completed libusb_ctx.handle_events_completed
  691. #define libusb_error_name libusb_ctx.error_name
  692. struct LIBUSB_hid_device_;
  693. typedef struct LIBUSB_hid_device_ LIBUSB_hid_device;
  694. #define free_hid_device LIBUSB_free_hid_device
  695. #define hid_close LIBUSB_hid_close
  696. #define hid_device LIBUSB_hid_device
  697. #define hid_device_ LIBUSB_hid_device_
  698. #define hid_enumerate LIBUSB_hid_enumerate
  699. #define hid_error LIBUSB_hid_error
  700. #define hid_exit LIBUSB_hid_exit
  701. #define hid_free_enumeration LIBUSB_hid_free_enumeration
  702. #define hid_get_device_info LIBUSB_hid_get_device_info
  703. #define hid_get_feature_report LIBUSB_hid_get_feature_report
  704. #define hid_get_indexed_string LIBUSB_hid_get_indexed_string
  705. #define hid_get_input_report LIBUSB_hid_get_input_report
  706. #define hid_get_manufacturer_string LIBUSB_hid_get_manufacturer_string
  707. #define hid_get_product_string LIBUSB_hid_get_product_string
  708. #define hid_get_report_descriptor LIBUSB_hid_get_report_descriptor
  709. #define hid_get_serial_number_string LIBUSB_hid_get_serial_number_string
  710. #define hid_init LIBUSB_hid_init
  711. #define hid_open LIBUSB_hid_open
  712. #define hid_open_path LIBUSB_hid_open_path
  713. #define hid_read LIBUSB_hid_read
  714. #define hid_read_timeout LIBUSB_hid_read_timeout
  715. #define hid_send_feature_report LIBUSB_hid_send_feature_report
  716. #define hid_set_nonblocking LIBUSB_hid_set_nonblocking
  717. #define hid_write LIBUSB_hid_write
  718. #define input_report LIBUSB_input_report
  719. #define make_path LIBUSB_make_path
  720. #define new_hid_device LIBUSB_new_hid_device
  721. #define read_thread LIBUSB_read_thread
  722. #define return_data LIBUSB_return_data
  723. #include "SDL_hidapi_libusb.h"
  724. #undef libusb_init
  725. #undef libusb_exit
  726. #undef libusb_get_device_list
  727. #undef libusb_free_device_list
  728. #undef libusb_get_device_descriptor
  729. #undef libusb_get_active_config_descriptor
  730. #undef libusb_get_config_descriptor
  731. #undef libusb_free_config_descriptor
  732. #undef libusb_get_bus_number
  733. #undef libusb_get_port_numbers
  734. #undef libusb_get_device_address
  735. #undef libusb_open
  736. #undef libusb_close
  737. #undef libusb_get_device
  738. #undef libusb_claim_interface
  739. #undef libusb_release_interface
  740. #undef libusb_kernel_driver_active
  741. #undef libusb_detach_kernel_driver
  742. #undef libusb_attach_kernel_driver
  743. #undef libusb_set_interface_alt_setting
  744. #undef libusb_alloc_transfer
  745. #undef libusb_submit_transfer
  746. #undef libusb_cancel_transfer
  747. #undef libusb_free_transfer
  748. #undef libusb_control_transfer
  749. #undef libusb_interrupt_transfer
  750. #undef libusb_handle_events
  751. #undef libusb_handle_events_completed
  752. #undef libusb_error_name
  753. #undef free_hid_device
  754. #undef hid_close
  755. #undef hid_device
  756. #undef hid_device_
  757. #undef hid_enumerate
  758. #undef hid_error
  759. #undef hid_exit
  760. #undef hid_free_enumeration
  761. #undef hid_get_device_info
  762. #undef hid_get_feature_report
  763. #undef hid_get_indexed_string
  764. #undef hid_get_input_report
  765. #undef hid_get_manufacturer_string
  766. #undef hid_get_product_string
  767. #undef hid_get_report_descriptor
  768. #undef hid_get_serial_number_string
  769. #undef hid_init
  770. #undef hid_open
  771. #undef hid_open_path
  772. #undef hid_read
  773. #undef hid_read_timeout
  774. #undef hid_send_feature_report
  775. #undef hid_set_nonblocking
  776. #undef hid_write
  777. #undef input_report
  778. #undef make_path
  779. #undef new_hid_device
  780. #undef read_thread
  781. #undef return_data
  782. /* If the platform has any backend other than libusb, try to avoid using
  783. * libusb as the main backend for devices, since it detaches drivers and
  784. * therefore makes devices inaccessible to the rest of the OS.
  785. *
  786. * We do this by whitelisting devices we know to be accessible _exclusively_
  787. * via libusb; these are typically devices that look like HIDs but have a
  788. * quirk that requires direct access to the hardware.
  789. */
  790. static const struct {
  791. Uint16 vendor;
  792. Uint16 product;
  793. } SDL_libusb_whitelist[] = {
  794. { 0x057e, 0x0337 } /* Nintendo WUP-028, Wii U/Switch GameCube Adapter */
  795. };
  796. static SDL_bool IsInWhitelist(Uint16 vendor, Uint16 product)
  797. {
  798. int i;
  799. for (i = 0; i < SDL_arraysize(SDL_libusb_whitelist); i += 1) {
  800. if (vendor == SDL_libusb_whitelist[i].vendor &&
  801. product == SDL_libusb_whitelist[i].product) {
  802. return SDL_TRUE;
  803. }
  804. }
  805. return SDL_FALSE;
  806. }
  807. #endif /* HAVE_LIBUSB */
  808. #endif /* !SDL_HIDAPI_DISABLED */
  809. #if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND)
  810. /* We have another way to get HID devices, so use the whitelist to get devices where libusb is preferred */
  811. #define SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT SDL_TRUE
  812. #else
  813. /* libusb is the only way to get HID devices, so don't use the whitelist, get them all */
  814. #define SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT SDL_FALSE
  815. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND */
  816. static SDL_bool use_libusb_whitelist = SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT;
  817. /* Shared HIDAPI Implementation */
  818. struct hidapi_backend
  819. {
  820. int (*hid_write)(void *device, const unsigned char *data, size_t length);
  821. int (*hid_read_timeout)(void *device, unsigned char *data, size_t length, int milliseconds);
  822. int (*hid_read)(void *device, unsigned char *data, size_t length);
  823. int (*hid_set_nonblocking)(void *device, int nonblock);
  824. int (*hid_send_feature_report)(void *device, const unsigned char *data, size_t length);
  825. int (*hid_get_feature_report)(void *device, unsigned char *data, size_t length);
  826. int (*hid_get_input_report)(void *device, unsigned char *data, size_t length);
  827. void (*hid_close)(void *device);
  828. int (*hid_get_manufacturer_string)(void *device, wchar_t *string, size_t maxlen);
  829. int (*hid_get_product_string)(void *device, wchar_t *string, size_t maxlen);
  830. int (*hid_get_serial_number_string)(void *device, wchar_t *string, size_t maxlen);
  831. int (*hid_get_indexed_string)(void *device, int string_index, wchar_t *string, size_t maxlen);
  832. struct hid_device_info *(*hid_get_device_info)(void *device);
  833. int (*hid_get_report_descriptor)(void *device, unsigned char *buf, size_t buf_size);
  834. const wchar_t *(*hid_error)(void *device);
  835. };
  836. #ifdef HAVE_PLATFORM_BACKEND
  837. static const struct hidapi_backend PLATFORM_Backend = {
  838. (void *)PLATFORM_hid_write,
  839. (void *)PLATFORM_hid_read_timeout,
  840. (void *)PLATFORM_hid_read,
  841. (void *)PLATFORM_hid_set_nonblocking,
  842. (void *)PLATFORM_hid_send_feature_report,
  843. (void *)PLATFORM_hid_get_feature_report,
  844. (void *)PLATFORM_hid_get_input_report,
  845. (void *)PLATFORM_hid_close,
  846. (void *)PLATFORM_hid_get_manufacturer_string,
  847. (void *)PLATFORM_hid_get_product_string,
  848. (void *)PLATFORM_hid_get_serial_number_string,
  849. (void *)PLATFORM_hid_get_indexed_string,
  850. (void *)PLATFORM_hid_get_device_info,
  851. (void *)PLATFORM_hid_get_report_descriptor,
  852. (void *)PLATFORM_hid_error
  853. };
  854. #endif /* HAVE_PLATFORM_BACKEND */
  855. #ifdef HAVE_DRIVER_BACKEND
  856. static const struct hidapi_backend DRIVER_Backend = {
  857. (void *)DRIVER_hid_write,
  858. (void *)DRIVER_hid_read_timeout,
  859. (void *)DRIVER_hid_read,
  860. (void *)DRIVER_hid_set_nonblocking,
  861. (void *)DRIVER_hid_send_feature_report,
  862. (void *)DRIVER_hid_get_feature_report,
  863. (void *)DRIVER_hid_get_input_report,
  864. (void *)DRIVER_hid_close,
  865. (void *)DRIVER_hid_get_manufacturer_string,
  866. (void *)DRIVER_hid_get_product_string,
  867. (void *)DRIVER_hid_get_serial_number_string,
  868. (void *)DRIVER_hid_get_indexed_string,
  869. (void *)DRIVER_hid_get_device_info,
  870. (void *)DRIVER_hid_get_report_descriptor,
  871. (void *)DRIVER_hid_error
  872. };
  873. #endif /* HAVE_DRIVER_BACKEND */
  874. #ifdef HAVE_LIBUSB
  875. static const struct hidapi_backend LIBUSB_Backend = {
  876. (void *)LIBUSB_hid_write,
  877. (void *)LIBUSB_hid_read_timeout,
  878. (void *)LIBUSB_hid_read,
  879. (void *)LIBUSB_hid_set_nonblocking,
  880. (void *)LIBUSB_hid_send_feature_report,
  881. (void *)LIBUSB_hid_get_feature_report,
  882. (void *)LIBUSB_hid_get_input_report,
  883. (void *)LIBUSB_hid_close,
  884. (void *)LIBUSB_hid_get_manufacturer_string,
  885. (void *)LIBUSB_hid_get_product_string,
  886. (void *)LIBUSB_hid_get_serial_number_string,
  887. (void *)LIBUSB_hid_get_indexed_string,
  888. (void *)LIBUSB_hid_get_device_info,
  889. (void *)LIBUSB_hid_get_report_descriptor,
  890. (void *)LIBUSB_hid_error
  891. };
  892. #endif /* HAVE_LIBUSB */
  893. struct SDL_hid_device
  894. {
  895. const void *magic;
  896. void *device;
  897. const struct hidapi_backend *backend;
  898. SDL_hid_device_info info;
  899. };
  900. static char device_magic;
  901. #if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)
  902. static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
  903. {
  904. SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));
  905. wrapper->magic = &device_magic;
  906. wrapper->device = device;
  907. wrapper->backend = backend;
  908. SDL_zero(wrapper->info);
  909. return wrapper;
  910. }
  911. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  912. static void DeleteHIDDeviceWrapper(SDL_hid_device *device)
  913. {
  914. device->magic = NULL;
  915. SDL_free(device->info.path);
  916. SDL_free(device->info.serial_number);
  917. SDL_free(device->info.manufacturer_string);
  918. SDL_free(device->info.product_string);
  919. SDL_free(device);
  920. }
  921. #define CHECK_DEVICE_MAGIC(device, retval) \
  922. if (!device || device->magic != &device_magic) { \
  923. SDL_SetError("Invalid device"); \
  924. return retval; \
  925. }
  926. #define COPY_IF_EXISTS(var) \
  927. if (pSrc->var != NULL) { \
  928. pDst->var = SDL_strdup(pSrc->var); \
  929. } else { \
  930. pDst->var = NULL; \
  931. }
  932. #define WCOPY_IF_EXISTS(var) \
  933. if (pSrc->var != NULL) { \
  934. pDst->var = SDL_wcsdup(pSrc->var); \
  935. } else { \
  936. pDst->var = NULL; \
  937. }
  938. static void CopyHIDDeviceInfo(struct hid_device_info *pSrc, struct SDL_hid_device_info *pDst)
  939. {
  940. COPY_IF_EXISTS(path)
  941. pDst->vendor_id = pSrc->vendor_id;
  942. pDst->product_id = pSrc->product_id;
  943. WCOPY_IF_EXISTS(serial_number)
  944. pDst->release_number = pSrc->release_number;
  945. WCOPY_IF_EXISTS(manufacturer_string)
  946. WCOPY_IF_EXISTS(product_string)
  947. pDst->usage_page = pSrc->usage_page;
  948. pDst->usage = pSrc->usage;
  949. pDst->interface_number = pSrc->interface_number;
  950. pDst->interface_class = pSrc->interface_class;
  951. pDst->interface_subclass = pSrc->interface_subclass;
  952. pDst->interface_protocol = pSrc->interface_protocol;
  953. pDst->bus_type = (SDL_hid_bus_type)pSrc->bus_type;
  954. pDst->next = NULL;
  955. }
  956. #undef COPY_IF_EXISTS
  957. #undef WCOPY_IF_EXISTS
  958. static int SDL_hidapi_refcount = 0;
  959. static SDL_bool SDL_hidapi_only_controllers;
  960. static char *SDL_hidapi_ignored_devices = NULL;
  961. static void SDLCALL OnlyControllersChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  962. {
  963. SDL_hidapi_only_controllers = SDL_GetStringBoolean(hint, SDL_TRUE);
  964. }
  965. static void SDLCALL IgnoredDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  966. {
  967. if (SDL_hidapi_ignored_devices) {
  968. SDL_free(SDL_hidapi_ignored_devices);
  969. }
  970. if (hint && *hint) {
  971. SDL_hidapi_ignored_devices = SDL_strdup(hint);
  972. } else {
  973. SDL_hidapi_ignored_devices = NULL;
  974. }
  975. }
  976. SDL_bool SDL_HIDAPI_ShouldIgnoreDevice(int bus, Uint16 vendor_id, Uint16 product_id, Uint16 usage_page, Uint16 usage)
  977. {
  978. /* See if there are any devices we should skip in enumeration */
  979. if (SDL_hidapi_only_controllers && usage_page) {
  980. if (vendor_id == USB_VENDOR_VALVE) {
  981. /* Ignore the mouse/keyboard interface on Steam Controllers */
  982. if (
  983. #ifdef SDL_PLATFORM_WIN32
  984. /* Check the usage page and usage on both USB and Bluetooth */
  985. #else
  986. /* Only check the usage page and usage on USB */
  987. bus == HID_API_BUS_USB &&
  988. #endif
  989. usage_page == USB_USAGEPAGE_GENERIC_DESKTOP &&
  990. (usage == USB_USAGE_GENERIC_KEYBOARD || usage == USB_USAGE_GENERIC_MOUSE)) {
  991. return SDL_TRUE;
  992. }
  993. } else if (usage_page == USB_USAGEPAGE_GENERIC_DESKTOP &&
  994. (usage == USB_USAGE_GENERIC_JOYSTICK || usage == USB_USAGE_GENERIC_GAMEPAD || usage == USB_USAGE_GENERIC_MULTIAXISCONTROLLER)) {
  995. /* This is a controller */
  996. } else {
  997. return SDL_TRUE;
  998. }
  999. }
  1000. if (SDL_hidapi_ignored_devices) {
  1001. char vendor_match[16], product_match[16];
  1002. SDL_snprintf(vendor_match, sizeof(vendor_match), "0x%.4x/0x0000", vendor_id);
  1003. SDL_snprintf(product_match, sizeof(product_match), "0x%.4x/0x%.4x", vendor_id, product_id);
  1004. if (SDL_strcasestr(SDL_hidapi_ignored_devices, vendor_match) ||
  1005. SDL_strcasestr(SDL_hidapi_ignored_devices, product_match)) {
  1006. return SDL_TRUE;
  1007. }
  1008. }
  1009. return SDL_FALSE;
  1010. }
  1011. int SDL_hid_init(void)
  1012. {
  1013. int attempts = 0, success = 0;
  1014. if (SDL_hidapi_refcount > 0) {
  1015. ++SDL_hidapi_refcount;
  1016. return 0;
  1017. }
  1018. SDL_AddHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL);
  1019. SDL_AddHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);
  1020. #ifdef SDL_USE_LIBUDEV
  1021. if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
  1022. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  1023. "udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
  1024. linux_enumeration_method = ENUMERATION_FALLBACK;
  1025. } else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
  1026. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  1027. "Container detected, disabling HIDAPI udev integration");
  1028. linux_enumeration_method = ENUMERATION_FALLBACK;
  1029. } else {
  1030. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  1031. "Using udev for HIDAPI joystick device discovery");
  1032. linux_enumeration_method = ENUMERATION_LIBUDEV;
  1033. }
  1034. #endif
  1035. use_libusb_whitelist = SDL_GetHintBoolean("SDL_HIDAPI_LIBUSB_WHITELIST",
  1036. SDL_HIDAPI_LIBUSB_WHITELIST_DEFAULT);
  1037. #ifdef HAVE_LIBUSB
  1038. if (SDL_getenv("SDL_HIDAPI_DISABLE_LIBUSB") != NULL) {
  1039. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  1040. "libusb disabled by SDL_HIDAPI_DISABLE_LIBUSB");
  1041. libusb_ctx.libhandle = NULL;
  1042. } else {
  1043. ++attempts;
  1044. #ifdef SDL_LIBUSB_DYNAMIC
  1045. libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
  1046. #else
  1047. libusb_ctx.libhandle = (void *)1;
  1048. #endif
  1049. if (libusb_ctx.libhandle != NULL) {
  1050. SDL_bool loaded = SDL_TRUE;
  1051. #ifdef SDL_LIBUSB_DYNAMIC
  1052. #define LOAD_LIBUSB_SYMBOL(type, func) \
  1053. if (!(libusb_ctx.func = (type)SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) { \
  1054. loaded = SDL_FALSE; \
  1055. }
  1056. #else
  1057. #define LOAD_LIBUSB_SYMBOL(type, func) \
  1058. libusb_ctx.func = libusb_##func;
  1059. #endif
  1060. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init)
  1061. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit)
  1062. LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list)
  1063. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list)
  1064. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor)
  1065. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor)
  1066. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor)
  1067. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor)
  1068. LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number)
  1069. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len), get_port_numbers)
  1070. LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address)
  1071. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open)
  1072. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close)
  1073. LOAD_LIBUSB_SYMBOL(libusb_device * (LIBUSB_CALL *)(libusb_device_handle *dev_handle), get_device)
  1074. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface)
  1075. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface)
  1076. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active)
  1077. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver)
  1078. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver)
  1079. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting)
  1080. LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer)
  1081. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer)
  1082. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer)
  1083. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer)
  1084. 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)
  1085. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer)
  1086. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events)
  1087. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed)
  1088. LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name)
  1089. #undef LOAD_LIBUSB_SYMBOL
  1090. if (!loaded) {
  1091. #ifdef SDL_LIBUSB_DYNAMIC
  1092. SDL_UnloadObject(libusb_ctx.libhandle);
  1093. #endif
  1094. libusb_ctx.libhandle = NULL;
  1095. /* SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function"); */
  1096. } else if (LIBUSB_hid_init() < 0) {
  1097. #ifdef SDL_LIBUSB_DYNAMIC
  1098. SDL_UnloadObject(libusb_ctx.libhandle);
  1099. #endif
  1100. libusb_ctx.libhandle = NULL;
  1101. } else {
  1102. ++success;
  1103. }
  1104. }
  1105. }
  1106. #endif /* HAVE_LIBUSB */
  1107. #ifdef HAVE_PLATFORM_BACKEND
  1108. ++attempts;
  1109. #ifdef SDL_PLATFORM_LINUX
  1110. udev_ctx = SDL_UDEV_GetUdevSyms();
  1111. #endif /* __LINUX __ */
  1112. if (udev_ctx && PLATFORM_hid_init() == 0) {
  1113. ++success;
  1114. }
  1115. #endif /* HAVE_PLATFORM_BACKEND */
  1116. if (attempts > 0 && success == 0) {
  1117. return -1;
  1118. }
  1119. #ifdef SDL_PLATFORM_MACOS
  1120. hid_darwin_set_open_exclusive(0);
  1121. #endif
  1122. ++SDL_hidapi_refcount;
  1123. return 0;
  1124. }
  1125. int SDL_hid_exit(void)
  1126. {
  1127. int result = 0;
  1128. if (SDL_hidapi_refcount == 0) {
  1129. return 0;
  1130. }
  1131. --SDL_hidapi_refcount;
  1132. if (SDL_hidapi_refcount > 0) {
  1133. return 0;
  1134. }
  1135. SDL_hidapi_refcount = 0;
  1136. #ifndef SDL_HIDAPI_DISABLED
  1137. HIDAPI_ShutdownDiscovery();
  1138. #endif
  1139. #ifdef HAVE_PLATFORM_BACKEND
  1140. if (udev_ctx) {
  1141. result |= PLATFORM_hid_exit();
  1142. }
  1143. #ifdef SDL_PLATFORM_LINUX
  1144. SDL_UDEV_ReleaseUdevSyms();
  1145. #endif /* __LINUX __ */
  1146. #endif /* HAVE_PLATFORM_BACKEND */
  1147. #ifdef HAVE_LIBUSB
  1148. if (libusb_ctx.libhandle) {
  1149. result |= LIBUSB_hid_exit();
  1150. #ifdef SDL_LIBUSB_DYNAMIC
  1151. SDL_UnloadObject(libusb_ctx.libhandle);
  1152. #endif
  1153. libusb_ctx.libhandle = NULL;
  1154. }
  1155. #endif /* HAVE_LIBUSB */
  1156. SDL_DelHintCallback(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, OnlyControllersChanged, NULL);
  1157. SDL_DelHintCallback(SDL_HINT_HIDAPI_IGNORE_DEVICES, IgnoredDevicesChanged, NULL);
  1158. if (SDL_hidapi_ignored_devices) {
  1159. SDL_free(SDL_hidapi_ignored_devices);
  1160. SDL_hidapi_ignored_devices = NULL;
  1161. }
  1162. return result;
  1163. }
  1164. Uint32 SDL_hid_device_change_count(void)
  1165. {
  1166. Uint32 counter = 0;
  1167. #ifndef SDL_HIDAPI_DISABLED
  1168. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1169. return 0;
  1170. }
  1171. HIDAPI_UpdateDiscovery();
  1172. if (SDL_HIDAPI_discovery.m_unDeviceChangeCounter == 0) {
  1173. /* Counter wrapped! */
  1174. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1175. }
  1176. counter = SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1177. #endif /* !SDL_HIDAPI_DISABLED */
  1178. return counter;
  1179. }
  1180. static void AddDeviceToEnumeration(const char *driver_name, struct hid_device_info *dev, struct SDL_hid_device_info **devs, struct SDL_hid_device_info **last)
  1181. {
  1182. struct SDL_hid_device_info *new_dev;
  1183. #ifdef DEBUG_HIDAPI
  1184. SDL_Log("Adding %s device to enumeration: %ls %ls 0x%.4hx/0x%.4hx/%d",
  1185. driver_name, dev->manufacturer_string, dev->product_string, dev->vendor_id, dev->product_id, dev->interface_number);
  1186. #else
  1187. (void)driver_name;
  1188. #endif
  1189. new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
  1190. if (new_dev == NULL) {
  1191. /* Don't bother returning an error, get as many devices as possible */
  1192. return;
  1193. }
  1194. CopyHIDDeviceInfo(dev, new_dev);
  1195. if ((*last) != NULL) {
  1196. (*last)->next = new_dev;
  1197. } else {
  1198. *devs = new_dev;
  1199. }
  1200. *last = new_dev;
  1201. }
  1202. #if defined(HAVE_LIBUSB) || defined(HAVE_PLATFORM_BACKEND)
  1203. 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 *))
  1204. {
  1205. struct hid_device_info *last = NULL, *curr, *next;
  1206. for (curr = *devs; curr; curr = next) {
  1207. next = curr->next;
  1208. if (dev->vendor_id == curr->vendor_id &&
  1209. dev->product_id == curr->product_id &&
  1210. (dev->interface_number < 0 || curr->interface_number < 0 || dev->interface_number == curr->interface_number)) {
  1211. #ifdef DEBUG_HIDAPI
  1212. SDL_Log("Skipping %s device: %ls %ls 0x%.4hx/0x%.4hx/%d",
  1213. driver_name, curr->manufacturer_string, curr->product_string, curr->vendor_id, curr->product_id, curr->interface_number);
  1214. #else
  1215. (void)driver_name;
  1216. #endif
  1217. if (last) {
  1218. last->next = next;
  1219. } else {
  1220. *devs = next;
  1221. }
  1222. curr->next = NULL;
  1223. free_device_info(curr);
  1224. continue;
  1225. }
  1226. last = curr;
  1227. }
  1228. }
  1229. #endif /* HAVE_LIBUSB || HAVE_PLATFORM_BACKEND */
  1230. #ifdef HAVE_LIBUSB
  1231. static void RemoveNonWhitelistedDevicesFromEnumeration(struct hid_device_info **devs, void (*free_device_info)(struct hid_device_info *))
  1232. {
  1233. struct hid_device_info *last = NULL, *curr, *next;
  1234. for (curr = *devs; curr; curr = next) {
  1235. next = curr->next;
  1236. if (!IsInWhitelist(curr->vendor_id, curr->product_id)) {
  1237. #ifdef DEBUG_HIDAPI
  1238. SDL_Log("Device was not in libusb whitelist, skipping: %ls %ls 0x%.4hx/0x%.4hx/%d",
  1239. curr->manufacturer_string, curr->product_string, curr->vendor_id, curr->product_id, curr->interface_number);
  1240. #endif
  1241. if (last) {
  1242. last->next = next;
  1243. } else {
  1244. *devs = next;
  1245. }
  1246. curr->next = NULL;
  1247. free_device_info(curr);
  1248. continue;
  1249. }
  1250. last = curr;
  1251. }
  1252. }
  1253. #endif /* HAVE_LIBUSB */
  1254. struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)
  1255. {
  1256. struct hid_device_info *driver_devs = NULL;
  1257. struct hid_device_info *usb_devs = NULL;
  1258. struct hid_device_info *raw_devs = NULL;
  1259. struct hid_device_info *dev;
  1260. struct SDL_hid_device_info *devs = NULL, *last = NULL;
  1261. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1262. return NULL;
  1263. }
  1264. /* Collect the available devices */
  1265. #ifdef HAVE_DRIVER_BACKEND
  1266. driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);
  1267. #endif
  1268. #ifdef HAVE_LIBUSB
  1269. if (libusb_ctx.libhandle) {
  1270. usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
  1271. if (use_libusb_whitelist) {
  1272. RemoveNonWhitelistedDevicesFromEnumeration(&usb_devs, LIBUSB_hid_free_enumeration);
  1273. }
  1274. }
  1275. #endif /* HAVE_LIBUSB */
  1276. #ifdef HAVE_PLATFORM_BACKEND
  1277. if (udev_ctx) {
  1278. raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
  1279. }
  1280. #endif
  1281. /* Highest priority are custom driver devices */
  1282. for (dev = driver_devs; dev; dev = dev->next) {
  1283. AddDeviceToEnumeration("driver", dev, &devs, &last);
  1284. #ifdef HAVE_LIBUSB
  1285. RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration);
  1286. #endif
  1287. #ifdef HAVE_PLATFORM_BACKEND
  1288. RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration);
  1289. #endif
  1290. }
  1291. /* If whitelist is in effect, libusb has priority, otherwise raw devices do */
  1292. if (use_libusb_whitelist) {
  1293. for (dev = usb_devs; dev; dev = dev->next) {
  1294. AddDeviceToEnumeration("libusb", dev, &devs, &last);
  1295. #ifdef HAVE_PLATFORM_BACKEND
  1296. RemoveDeviceFromEnumeration("raw", dev, &raw_devs, PLATFORM_hid_free_enumeration);
  1297. #endif
  1298. }
  1299. for (dev = raw_devs; dev; dev = dev->next) {
  1300. AddDeviceToEnumeration("platform", dev, &devs, &last);
  1301. }
  1302. } else {
  1303. for (dev = raw_devs; dev; dev = dev->next) {
  1304. AddDeviceToEnumeration("raw", dev, &devs, &last);
  1305. #ifdef HAVE_LIBUSB
  1306. RemoveDeviceFromEnumeration("libusb", dev, &usb_devs, LIBUSB_hid_free_enumeration);
  1307. #endif
  1308. }
  1309. for (dev = usb_devs; dev; dev = dev->next) {
  1310. AddDeviceToEnumeration("libusb", dev, &devs, &last);
  1311. }
  1312. }
  1313. #ifdef HAVE_DRIVER_BACKEND
  1314. DRIVER_hid_free_enumeration(driver_devs);
  1315. #endif
  1316. #ifdef HAVE_LIBUSB
  1317. LIBUSB_hid_free_enumeration(usb_devs);
  1318. #endif
  1319. #ifdef HAVE_PLATFORM_BACKEND
  1320. PLATFORM_hid_free_enumeration(raw_devs);
  1321. #endif
  1322. return devs;
  1323. }
  1324. void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
  1325. {
  1326. while (devs) {
  1327. struct SDL_hid_device_info *next = devs->next;
  1328. SDL_free(devs->path);
  1329. SDL_free(devs->serial_number);
  1330. SDL_free(devs->manufacturer_string);
  1331. SDL_free(devs->product_string);
  1332. SDL_free(devs);
  1333. devs = next;
  1334. }
  1335. }
  1336. SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
  1337. {
  1338. #if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)
  1339. void *pDevice = NULL;
  1340. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1341. return NULL;
  1342. }
  1343. #ifdef HAVE_PLATFORM_BACKEND
  1344. if (udev_ctx) {
  1345. pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number);
  1346. if (pDevice != NULL) {
  1347. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1348. }
  1349. }
  1350. #endif /* HAVE_PLATFORM_BACKEND */
  1351. #ifdef HAVE_DRIVER_BACKEND
  1352. pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number);
  1353. if (pDevice != NULL) {
  1354. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1355. }
  1356. #endif /* HAVE_DRIVER_BACKEND */
  1357. #ifdef HAVE_LIBUSB
  1358. if (libusb_ctx.libhandle != NULL) {
  1359. pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);
  1360. if (pDevice != NULL) {
  1361. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1362. }
  1363. }
  1364. #endif /* HAVE_LIBUSB */
  1365. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1366. return NULL;
  1367. }
  1368. SDL_hid_device *SDL_hid_open_path(const char *path)
  1369. {
  1370. #if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)
  1371. void *pDevice = NULL;
  1372. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1373. return NULL;
  1374. }
  1375. #ifdef HAVE_PLATFORM_BACKEND
  1376. if (udev_ctx) {
  1377. pDevice = PLATFORM_hid_open_path(path);
  1378. if (pDevice != NULL) {
  1379. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1380. }
  1381. }
  1382. #endif /* HAVE_PLATFORM_BACKEND */
  1383. #ifdef HAVE_DRIVER_BACKEND
  1384. pDevice = DRIVER_hid_open_path(path);
  1385. if (pDevice != NULL) {
  1386. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1387. }
  1388. #endif /* HAVE_DRIVER_BACKEND */
  1389. #ifdef HAVE_LIBUSB
  1390. if (libusb_ctx.libhandle != NULL) {
  1391. pDevice = LIBUSB_hid_open_path(path);
  1392. if (pDevice != NULL) {
  1393. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1394. }
  1395. }
  1396. #endif /* HAVE_LIBUSB */
  1397. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1398. return NULL;
  1399. }
  1400. int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
  1401. {
  1402. CHECK_DEVICE_MAGIC(device, -1);
  1403. return device->backend->hid_write(device->device, data, length);
  1404. }
  1405. int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds)
  1406. {
  1407. CHECK_DEVICE_MAGIC(device, -1);
  1408. return device->backend->hid_read_timeout(device->device, data, length, milliseconds);
  1409. }
  1410. int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length)
  1411. {
  1412. CHECK_DEVICE_MAGIC(device, -1);
  1413. return device->backend->hid_read(device->device, data, length);
  1414. }
  1415. int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock)
  1416. {
  1417. CHECK_DEVICE_MAGIC(device, -1);
  1418. return device->backend->hid_set_nonblocking(device->device, nonblock);
  1419. }
  1420. int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length)
  1421. {
  1422. CHECK_DEVICE_MAGIC(device, -1);
  1423. return device->backend->hid_send_feature_report(device->device, data, length);
  1424. }
  1425. int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length)
  1426. {
  1427. CHECK_DEVICE_MAGIC(device, -1);
  1428. return device->backend->hid_get_feature_report(device->device, data, length);
  1429. }
  1430. int SDL_hid_get_input_report(SDL_hid_device *device, unsigned char *data, size_t length)
  1431. {
  1432. CHECK_DEVICE_MAGIC(device, -1);
  1433. return device->backend->hid_get_input_report(device->device, data, length);
  1434. }
  1435. int SDL_hid_close(SDL_hid_device *device)
  1436. {
  1437. CHECK_DEVICE_MAGIC(device, -1);
  1438. device->backend->hid_close(device->device);
  1439. DeleteHIDDeviceWrapper(device);
  1440. return 0;
  1441. }
  1442. int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1443. {
  1444. CHECK_DEVICE_MAGIC(device, -1);
  1445. return device->backend->hid_get_manufacturer_string(device->device, string, maxlen);
  1446. }
  1447. int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1448. {
  1449. CHECK_DEVICE_MAGIC(device, -1);
  1450. return device->backend->hid_get_product_string(device->device, string, maxlen);
  1451. }
  1452. int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1453. {
  1454. CHECK_DEVICE_MAGIC(device, -1);
  1455. return device->backend->hid_get_serial_number_string(device->device, string, maxlen);
  1456. }
  1457. int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen)
  1458. {
  1459. CHECK_DEVICE_MAGIC(device, -1);
  1460. return device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen);
  1461. }
  1462. SDL_hid_device_info *SDL_hid_get_device_info(SDL_hid_device *device)
  1463. {
  1464. struct hid_device_info *info;
  1465. CHECK_DEVICE_MAGIC(device, NULL);
  1466. info = device->backend->hid_get_device_info(device->device);
  1467. if (info) {
  1468. CopyHIDDeviceInfo(info, &device->info);
  1469. return &device->info;
  1470. } else {
  1471. return NULL;
  1472. }
  1473. }
  1474. int SDL_hid_get_report_descriptor(SDL_hid_device *device, unsigned char *buf, size_t buf_size)
  1475. {
  1476. CHECK_DEVICE_MAGIC(device, -1);
  1477. return device->backend->hid_get_report_descriptor(device->device, buf, buf_size);
  1478. }
  1479. void SDL_hid_ble_scan(SDL_bool active)
  1480. {
  1481. #if !defined(SDL_HIDAPI_DISABLED) && (defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS))
  1482. extern void hid_ble_scan(int bStart);
  1483. hid_ble_scan(active);
  1484. #endif
  1485. }
  1486. #ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
  1487. /* This is needed to enable input for Nyko and EVORETRO GameCube adaptors */
  1488. void SDL_EnableGameCubeAdaptors(void)
  1489. {
  1490. #ifdef HAVE_LIBUSB
  1491. libusb_context *context = NULL;
  1492. libusb_device **devs = NULL;
  1493. libusb_device_handle *handle = NULL;
  1494. struct libusb_device_descriptor desc;
  1495. ssize_t i, num_devs;
  1496. int kernel_detached = 0;
  1497. if (libusb_ctx.libhandle == NULL) {
  1498. return;
  1499. }
  1500. if (libusb_ctx.init(&context) == 0) {
  1501. num_devs = libusb_ctx.get_device_list(context, &devs);
  1502. for (i = 0; i < num_devs; ++i) {
  1503. if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) {
  1504. continue;
  1505. }
  1506. if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
  1507. continue;
  1508. }
  1509. if (libusb_ctx.open(devs[i], &handle) != 0) {
  1510. continue;
  1511. }
  1512. if (libusb_ctx.kernel_driver_active(handle, 0)) {
  1513. if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) {
  1514. kernel_detached = 1;
  1515. }
  1516. }
  1517. if (libusb_ctx.claim_interface(handle, 0) == 0) {
  1518. libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
  1519. libusb_ctx.release_interface(handle, 0);
  1520. }
  1521. if (kernel_detached) {
  1522. libusb_ctx.attach_kernel_driver(handle, 0);
  1523. }
  1524. libusb_ctx.close(handle);
  1525. }
  1526. libusb_ctx.free_device_list(devs, 1);
  1527. libusb_ctx.exit(context);
  1528. }
  1529. #endif /* HAVE_LIBUSB */
  1530. }
  1531. #endif /* HAVE_ENABLE_GAMECUBE_ADAPTORS */