SDL_hidapi.c 61 KB

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