SDL_hidapi.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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.h"
  28. #include "SDL_hidapi_c.h"
  29. #include "SDL_loadso.h"
  30. #include "SDL_thread.h"
  31. #include "SDL_timer.h"
  32. #ifndef SDL_HIDAPI_DISABLED
  33. #if defined(__WIN32__) || defined(__WINGDK__)
  34. #include "../core/windows/SDL_windows.h"
  35. #endif
  36. #if defined(__MACOSX__)
  37. #include <AvailabilityMacros.h>
  38. #include <CoreFoundation/CoreFoundation.h>
  39. #include <IOKit/IOKitLib.h>
  40. #include <IOKit/hid/IOHIDDevice.h>
  41. #include <IOKit/usb/USBSpec.h>
  42. #include <mach/mach.h>
  43. /* Things named "Master" were renamed to "Main" in macOS 12.0's SDK. */
  44. #if MAC_OS_X_VERSION_MIN_REQUIRED < 120000
  45. #define kIOMainPortDefault kIOMasterPortDefault
  46. #endif
  47. #endif
  48. #include "../core/linux/SDL_udev.h"
  49. #ifdef SDL_USE_LIBUDEV
  50. #include "../core/linux/SDL_sandbox.h"
  51. #include <poll.h>
  52. #endif
  53. #ifdef HAVE_INOTIFY
  54. #include <errno.h> /* errno, strerror */
  55. #include <fcntl.h>
  56. #include <limits.h> /* For the definition of NAME_MAX */
  57. #include <sys/inotify.h>
  58. #include <unistd.h> /* just in case we didn't use that SDL_USE_LIBUDEV block... */
  59. #endif
  60. #if defined(SDL_USE_LIBUDEV) || defined(HAVE_INOTIFY)
  61. #include <unistd.h>
  62. #endif
  63. #if defined(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. #if defined(HAVE_INOTIFY)
  73. static int inotify_fd = -1;
  74. #endif
  75. #if defined(SDL_USE_LIBUDEV)
  76. static const SDL_UDEV_Symbols *usyms = NULL;
  77. #endif
  78. static struct
  79. {
  80. SDL_bool m_bInitialized;
  81. Uint32 m_unDeviceChangeCounter;
  82. SDL_bool m_bCanGetNotifications;
  83. Uint32 m_unLastDetect;
  84. #if defined(__WIN32__) || defined(__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. #if defined(__MACOSX__)
  92. IONotificationPortRef m_notificationPort;
  93. mach_port_t m_notificationMach;
  94. #endif
  95. #if defined(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(__WIN32__) || defined(__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(__WIN32__) || defined(__WINGDK__) */
  143. #if defined(__MACOSX__)
  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 /* __MACOSX__ */
  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 = SDL_TRUE;
  193. SDL_HIDAPI_discovery.m_unDeviceChangeCounter = 1;
  194. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
  195. SDL_HIDAPI_discovery.m_unLastDetect = 0;
  196. #if defined(__WIN32__) || defined(__WINGDK__)
  197. SDL_HIDAPI_discovery.m_nThreadID = SDL_ThreadID();
  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(__WIN32__) || defined(__WINGDK__) */
  219. #if defined(__MACOSX__)
  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 /* __MACOSX__ */
  265. #if defined(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 = SDL_TRUE;
  279. }
  280. }
  281. }
  282. } else
  283. #endif /* SDL_USE_LIBUDEV */
  284. {
  285. #if defined(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 = SDL_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. Uint32 now = SDL_GetTicks();
  319. if (!SDL_HIDAPI_discovery.m_unLastDetect || SDL_TICKS_PASSED(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(__WIN32__) || defined(__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_ThreadID() == 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(__WIN32__) || defined(__WINGDK__) */
  339. #if defined(__MACOSX__)
  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. #if defined(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. #if defined(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(__WIN32__) || defined(__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. #if defined(__MACOSX__)
  431. if (SDL_HIDAPI_discovery.m_notificationPort) {
  432. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  433. }
  434. #endif
  435. #if defined(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. #if defined(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 = SDL_FALSE;
  458. }
  459. /* Platform HIDAPI Implementation */
  460. #define hid_device PLATFORM_hid_device
  461. #define hid_device_ PLATFORM_hid_device_
  462. #define hid_init PLATFORM_hid_init
  463. #define hid_exit PLATFORM_hid_exit
  464. #define hid_enumerate PLATFORM_hid_enumerate
  465. #define hid_free_enumeration PLATFORM_hid_free_enumeration
  466. #define hid_open PLATFORM_hid_open
  467. #define hid_open_path PLATFORM_hid_open_path
  468. #define hid_write PLATFORM_hid_write
  469. #define hid_read_timeout PLATFORM_hid_read_timeout
  470. #define hid_read PLATFORM_hid_read
  471. #define hid_set_nonblocking PLATFORM_hid_set_nonblocking
  472. #define hid_send_feature_report PLATFORM_hid_send_feature_report
  473. #define hid_get_feature_report PLATFORM_hid_get_feature_report
  474. #define hid_close PLATFORM_hid_close
  475. #define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string
  476. #define hid_get_product_string PLATFORM_hid_get_product_string
  477. #define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
  478. #define hid_get_indexed_string PLATFORM_hid_get_indexed_string
  479. #define hid_error PLATFORM_hid_error
  480. #define new_hid_device PLATFORM_new_hid_device
  481. #define free_hid_device PLATFORM_free_hid_device
  482. #define input_report PLATFORM_input_report
  483. #define return_data PLATFORM_return_data
  484. #define make_path PLATFORM_make_path
  485. #define read_thread PLATFORM_read_thread
  486. #undef HIDAPI_H__
  487. #ifdef __LINUX__
  488. #ifdef SDL_USE_LIBUDEV
  489. static const SDL_UDEV_Symbols *udev_ctx = NULL;
  490. #define udev_device_get_sysattr_value udev_ctx->udev_device_get_sysattr_value
  491. #define udev_new udev_ctx->udev_new
  492. #define udev_unref udev_ctx->udev_unref
  493. #define udev_device_new_from_devnum udev_ctx->udev_device_new_from_devnum
  494. #define udev_device_get_parent_with_subsystem_devtype udev_ctx->udev_device_get_parent_with_subsystem_devtype
  495. #define udev_device_unref udev_ctx->udev_device_unref
  496. #define udev_enumerate_new udev_ctx->udev_enumerate_new
  497. #define udev_enumerate_add_match_subsystem udev_ctx->udev_enumerate_add_match_subsystem
  498. #define udev_enumerate_scan_devices udev_ctx->udev_enumerate_scan_devices
  499. #define udev_enumerate_get_list_entry udev_ctx->udev_enumerate_get_list_entry
  500. #define udev_list_entry_get_name udev_ctx->udev_list_entry_get_name
  501. #define udev_device_new_from_syspath udev_ctx->udev_device_new_from_syspath
  502. #define udev_device_get_devnode udev_ctx->udev_device_get_devnode
  503. #define udev_list_entry_get_next udev_ctx->udev_list_entry_get_next
  504. #define udev_enumerate_unref udev_ctx->udev_enumerate_unref
  505. #include "linux/hid.c"
  506. #define HAVE_PLATFORM_BACKEND
  507. #endif /* SDL_USE_LIBUDEV */
  508. #elif defined(__MACOSX__)
  509. #include "mac/hid.c"
  510. #define HAVE_PLATFORM_BACKEND
  511. #define udev_ctx 1
  512. #elif defined(__WINDOWS__) || defined(__WINGDK__)
  513. #include "windows/hid.c"
  514. #define HAVE_PLATFORM_BACKEND
  515. #define udev_ctx 1
  516. #elif defined(__ANDROID__)
  517. /* The implementation for Android is in a separate .cpp file */
  518. #include "hidapi/hidapi.h"
  519. #define HAVE_PLATFORM_BACKEND 1
  520. #define udev_ctx 1
  521. #elif defined(__IPHONEOS__) || defined(__TVOS__)
  522. /* The implementation for iOS and tvOS is in a separate .m file */
  523. #include "hidapi/hidapi.h"
  524. #define HAVE_PLATFORM_BACKEND 1
  525. #define udev_ctx 1
  526. #endif
  527. #undef hid_device
  528. #undef hid_device_
  529. #undef hid_init
  530. #undef hid_exit
  531. #undef hid_enumerate
  532. #undef hid_free_enumeration
  533. #undef hid_open
  534. #undef hid_open_path
  535. #undef hid_write
  536. #undef hid_read_timeout
  537. #undef hid_read
  538. #undef hid_set_nonblocking
  539. #undef hid_send_feature_report
  540. #undef hid_get_feature_report
  541. #undef hid_close
  542. #undef hid_get_manufacturer_string
  543. #undef hid_get_product_string
  544. #undef hid_get_serial_number_string
  545. #undef hid_get_indexed_string
  546. #undef hid_error
  547. #undef new_hid_device
  548. #undef free_hid_device
  549. #undef input_report
  550. #undef return_data
  551. #undef make_path
  552. #undef read_thread
  553. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  554. #define HAVE_DRIVER_BACKEND 1
  555. #else
  556. #define HAVE_DRIVER_BACKEND 0
  557. #endif
  558. #if HAVE_DRIVER_BACKEND
  559. /* DRIVER HIDAPI Implementation */
  560. #define hid_device DRIVER_hid_device
  561. #define hid_device_ DRIVER_hid_device_
  562. #define hid_init DRIVER_hid_init
  563. #define hid_exit DRIVER_hid_exit
  564. #define hid_enumerate DRIVER_hid_enumerate
  565. #define hid_free_enumeration DRIVER_hid_free_enumeration
  566. #define hid_open DRIVER_hid_open
  567. #define hid_open_path DRIVER_hid_open_path
  568. #define hid_write DRIVER_hid_write
  569. #define hid_read_timeout DRIVER_hid_read_timeout
  570. #define hid_read DRIVER_hid_read
  571. #define hid_set_nonblocking DRIVER_hid_set_nonblocking
  572. #define hid_send_feature_report DRIVER_hid_send_feature_report
  573. #define hid_get_feature_report DRIVER_hid_get_feature_report
  574. #define hid_close DRIVER_hid_close
  575. #define hid_get_manufacturer_string DRIVER_hid_get_manufacturer_string
  576. #define hid_get_product_string DRIVER_hid_get_product_string
  577. #define hid_get_serial_number_string DRIVER_hid_get_serial_number_string
  578. #define hid_get_indexed_string DRIVER_hid_get_indexed_string
  579. #define hid_error DRIVER_hid_error
  580. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  581. #undef HIDAPI_H__
  582. #include "steamxbox/hid.c"
  583. #else
  584. #error Need a driver hid.c for this platform!
  585. #endif
  586. #undef hid_device
  587. #undef hid_device_
  588. #undef hid_init
  589. #undef hid_exit
  590. #undef hid_enumerate
  591. #undef hid_free_enumeration
  592. #undef hid_open
  593. #undef hid_open_path
  594. #undef hid_write
  595. #undef hid_read_timeout
  596. #undef hid_read
  597. #undef hid_set_nonblocking
  598. #undef hid_send_feature_report
  599. #undef hid_get_feature_report
  600. #undef hid_close
  601. #undef hid_get_manufacturer_string
  602. #undef hid_get_product_string
  603. #undef hid_get_serial_number_string
  604. #undef hid_get_indexed_string
  605. #undef hid_error
  606. #endif /* HAVE_DRIVER_BACKEND */
  607. #ifdef HAVE_LIBUSB
  608. /* libusb HIDAPI Implementation */
  609. /* Include this now, for our dynamically-loaded libusb context */
  610. #include <libusb.h>
  611. static struct
  612. {
  613. void *libhandle;
  614. /* *INDENT-OFF* */ /* clang-format off */
  615. int (LIBUSB_CALL *init)(libusb_context **ctx);
  616. void (LIBUSB_CALL *exit)(libusb_context *ctx);
  617. ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list);
  618. void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices);
  619. int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
  620. int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);
  621. int (LIBUSB_CALL *get_config_descriptor)(
  622. libusb_device *dev,
  623. uint8_t config_index,
  624. struct libusb_config_descriptor **config
  625. );
  626. void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config);
  627. uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev);
  628. uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev);
  629. int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle);
  630. void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle);
  631. int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number);
  632. int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number);
  633. int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
  634. int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  635. int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  636. int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
  637. struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets);
  638. int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer);
  639. int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer);
  640. void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer);
  641. int (LIBUSB_CALL *control_transfer)(
  642. libusb_device_handle *dev_handle,
  643. uint8_t request_type,
  644. uint8_t bRequest,
  645. uint16_t wValue,
  646. uint16_t wIndex,
  647. unsigned char *data,
  648. uint16_t wLength,
  649. unsigned int timeout
  650. );
  651. int (LIBUSB_CALL *interrupt_transfer)(
  652. libusb_device_handle *dev_handle,
  653. unsigned char endpoint,
  654. unsigned char *data,
  655. int length,
  656. int *actual_length,
  657. unsigned int timeout
  658. );
  659. int (LIBUSB_CALL *handle_events)(libusb_context *ctx);
  660. int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed);
  661. const char * (LIBUSB_CALL *error_name)(int errcode);
  662. /* *INDENT-ON* */ /* clang-format on */
  663. } libusb_ctx;
  664. #define libusb_init libusb_ctx.init
  665. #define libusb_exit libusb_ctx.exit
  666. #define libusb_get_device_list libusb_ctx.get_device_list
  667. #define libusb_free_device_list libusb_ctx.free_device_list
  668. #define libusb_get_device_descriptor libusb_ctx.get_device_descriptor
  669. #define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
  670. #define libusb_get_config_descriptor libusb_ctx.get_config_descriptor
  671. #define libusb_free_config_descriptor libusb_ctx.free_config_descriptor
  672. #define libusb_get_bus_number libusb_ctx.get_bus_number
  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_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. #define hid_device LIBUSB_hid_device
  692. #define hid_device_ LIBUSB_hid_device_
  693. #define hid_init LIBUSB_hid_init
  694. #define hid_exit LIBUSB_hid_exit
  695. #define hid_enumerate LIBUSB_hid_enumerate
  696. #define hid_free_enumeration LIBUSB_hid_free_enumeration
  697. #define hid_open LIBUSB_hid_open
  698. #define hid_open_path LIBUSB_hid_open_path
  699. #define hid_write LIBUSB_hid_write
  700. #define hid_read_timeout LIBUSB_hid_read_timeout
  701. #define hid_read LIBUSB_hid_read
  702. #define hid_set_nonblocking LIBUSB_hid_set_nonblocking
  703. #define hid_send_feature_report LIBUSB_hid_send_feature_report
  704. #define hid_get_feature_report LIBUSB_hid_get_feature_report
  705. #define hid_close LIBUSB_hid_close
  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_serial_number_string LIBUSB_hid_get_serial_number_string
  709. #define hid_get_indexed_string LIBUSB_hid_get_indexed_string
  710. #define hid_error LIBUSB_hid_error
  711. #define new_hid_device LIBUSB_new_hid_device
  712. #define free_hid_device LIBUSB_free_hid_device
  713. #define input_report LIBUSB_input_report
  714. #define return_data LIBUSB_return_data
  715. #define make_path LIBUSB_make_path
  716. #define read_thread LIBUSB_read_thread
  717. #ifndef __FreeBSD__
  718. /* this is awkwardly inlined, so we need to re-implement it here
  719. * so we can override the libusb_control_transfer call */
  720. static int SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
  721. uint8_t descriptor_index, uint16_t lang_id,
  722. unsigned char *data, int length)
  723. {
  724. return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN | 0x0, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | descriptor_index, lang_id,
  725. data, (uint16_t)length, 1000); /* Endpoint 0 IN */
  726. }
  727. #define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
  728. #endif /* __FreeBSD__ */
  729. #undef HIDAPI_H__
  730. #include "libusb/hid.c"
  731. #undef libusb_init
  732. #undef libusb_exit
  733. #undef libusb_get_device_list
  734. #undef libusb_free_device_list
  735. #undef libusb_get_device_descriptor
  736. #undef libusb_get_active_config_descriptor
  737. #undef libusb_get_config_descriptor
  738. #undef libusb_free_config_descriptor
  739. #undef libusb_get_bus_number
  740. #undef libusb_get_device_address
  741. #undef libusb_open
  742. #undef libusb_close
  743. #undef libusb_claim_interface
  744. #undef libusb_release_interface
  745. #undef libusb_kernel_driver_active
  746. #undef libusb_detach_kernel_driver
  747. #undef libusb_attach_kernel_driver
  748. #undef libusb_set_interface_alt_setting
  749. #undef libusb_alloc_transfer
  750. #undef libusb_submit_transfer
  751. #undef libusb_cancel_transfer
  752. #undef libusb_free_transfer
  753. #undef libusb_control_transfer
  754. #undef libusb_interrupt_transfer
  755. #undef libusb_handle_events
  756. #undef libusb_handle_events_completed
  757. #undef libusb_error_name
  758. #undef hid_device
  759. #undef hid_device_
  760. #undef hid_init
  761. #undef hid_exit
  762. #undef hid_enumerate
  763. #undef hid_free_enumeration
  764. #undef hid_open
  765. #undef hid_open_path
  766. #undef hid_write
  767. #undef hid_read_timeout
  768. #undef hid_read
  769. #undef hid_set_nonblocking
  770. #undef hid_send_feature_report
  771. #undef hid_get_feature_report
  772. #undef hid_close
  773. #undef hid_get_manufacturer_string
  774. #undef hid_get_product_string
  775. #undef hid_get_serial_number_string
  776. #undef hid_get_indexed_string
  777. #undef hid_error
  778. #undef new_hid_device
  779. #undef free_hid_device
  780. #undef input_report
  781. #undef return_data
  782. #undef make_path
  783. #undef read_thread
  784. /* If the platform has any backend other than libusb, try to avoid using
  785. * libusb as the main backend for devices, since it detaches drivers and
  786. * therefore makes devices inaccessible to the rest of the OS.
  787. *
  788. * We do this by whitelisting devices we know to be accessible _exclusively_
  789. * via libusb; these are typically devices that look like HIDs but have a
  790. * quirk that requires direct access to the hardware.
  791. */
  792. static const struct {
  793. Uint16 vendor;
  794. Uint16 product;
  795. } SDL_libusb_whitelist[] = {
  796. { 0x057e, 0x0337 } /* Nintendo WUP-028, Wii U/Switch GameCube Adapter */
  797. };
  798. static SDL_bool
  799. IsInWhitelist(Uint16 vendor, Uint16 product)
  800. {
  801. int i;
  802. for (i = 0; i < SDL_arraysize(SDL_libusb_whitelist); i += 1) {
  803. if (vendor == SDL_libusb_whitelist[i].vendor &&
  804. product == SDL_libusb_whitelist[i].product) {
  805. return SDL_TRUE;
  806. }
  807. }
  808. return SDL_FALSE;
  809. }
  810. #if defined(HAVE_PLATFORM_BACKEND) || HAVE_DRIVER_BACKEND
  811. #define use_libusb_whitelist_default SDL_TRUE
  812. #else
  813. #define use_libusb_whitelist_default SDL_FALSE
  814. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND */
  815. static SDL_bool use_libusb_whitelist = use_libusb_whitelist_default;
  816. #endif /* HAVE_LIBUSB */
  817. #endif /* !SDL_HIDAPI_DISABLED */
  818. /* Shared HIDAPI Implementation */
  819. struct hidapi_backend
  820. {
  821. int (*hid_write)(void *device, const unsigned char *data, size_t length);
  822. int (*hid_read_timeout)(void *device, unsigned char *data, size_t length, int milliseconds);
  823. int (*hid_read)(void *device, unsigned char *data, size_t length);
  824. int (*hid_set_nonblocking)(void *device, int nonblock);
  825. int (*hid_send_feature_report)(void *device, const unsigned char *data, size_t length);
  826. int (*hid_get_feature_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. const wchar_t *(*hid_error)(void *device);
  833. };
  834. #ifdef HAVE_PLATFORM_BACKEND
  835. static const struct hidapi_backend PLATFORM_Backend = {
  836. (void *)PLATFORM_hid_write,
  837. (void *)PLATFORM_hid_read_timeout,
  838. (void *)PLATFORM_hid_read,
  839. (void *)PLATFORM_hid_set_nonblocking,
  840. (void *)PLATFORM_hid_send_feature_report,
  841. (void *)PLATFORM_hid_get_feature_report,
  842. (void *)PLATFORM_hid_close,
  843. (void *)PLATFORM_hid_get_manufacturer_string,
  844. (void *)PLATFORM_hid_get_product_string,
  845. (void *)PLATFORM_hid_get_serial_number_string,
  846. (void *)PLATFORM_hid_get_indexed_string,
  847. (void *)PLATFORM_hid_error
  848. };
  849. #endif /* HAVE_PLATFORM_BACKEND */
  850. #if HAVE_DRIVER_BACKEND
  851. static const struct hidapi_backend DRIVER_Backend = {
  852. (void *)DRIVER_hid_write,
  853. (void *)DRIVER_hid_read_timeout,
  854. (void *)DRIVER_hid_read,
  855. (void *)DRIVER_hid_set_nonblocking,
  856. (void *)DRIVER_hid_send_feature_report,
  857. (void *)DRIVER_hid_get_feature_report,
  858. (void *)DRIVER_hid_close,
  859. (void *)DRIVER_hid_get_manufacturer_string,
  860. (void *)DRIVER_hid_get_product_string,
  861. (void *)DRIVER_hid_get_serial_number_string,
  862. (void *)DRIVER_hid_get_indexed_string,
  863. (void *)DRIVER_hid_error
  864. };
  865. #endif /* HAVE_DRIVER_BACKEND */
  866. #ifdef HAVE_LIBUSB
  867. static const struct hidapi_backend LIBUSB_Backend = {
  868. (void *)LIBUSB_hid_write,
  869. (void *)LIBUSB_hid_read_timeout,
  870. (void *)LIBUSB_hid_read,
  871. (void *)LIBUSB_hid_set_nonblocking,
  872. (void *)LIBUSB_hid_send_feature_report,
  873. (void *)LIBUSB_hid_get_feature_report,
  874. (void *)LIBUSB_hid_close,
  875. (void *)LIBUSB_hid_get_manufacturer_string,
  876. (void *)LIBUSB_hid_get_product_string,
  877. (void *)LIBUSB_hid_get_serial_number_string,
  878. (void *)LIBUSB_hid_get_indexed_string,
  879. (void *)LIBUSB_hid_error
  880. };
  881. #endif /* HAVE_LIBUSB */
  882. struct SDL_hid_device_
  883. {
  884. const void *magic;
  885. void *device;
  886. const struct hidapi_backend *backend;
  887. };
  888. static char device_magic;
  889. #if defined(HAVE_PLATFORM_BACKEND) || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  890. static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
  891. {
  892. SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));
  893. wrapper->magic = &device_magic;
  894. wrapper->device = device;
  895. wrapper->backend = backend;
  896. return wrapper;
  897. }
  898. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  899. static void DeleteHIDDeviceWrapper(SDL_hid_device *device)
  900. {
  901. device->magic = NULL;
  902. SDL_free(device);
  903. }
  904. #define CHECK_DEVICE_MAGIC(device, retval) \
  905. if (!device || device->magic != &device_magic) { \
  906. SDL_SetError("Invalid device"); \
  907. return retval; \
  908. }
  909. #ifndef SDL_HIDAPI_DISABLED
  910. #if defined(HAVE_PLATFORM_BACKEND) || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  911. #define COPY_IF_EXISTS(var) \
  912. if (pSrc->var != NULL) { \
  913. pDst->var = SDL_strdup(pSrc->var); \
  914. } else { \
  915. pDst->var = NULL; \
  916. }
  917. #define WCOPY_IF_EXISTS(var) \
  918. if (pSrc->var != NULL) { \
  919. pDst->var = SDL_wcsdup(pSrc->var); \
  920. } else { \
  921. pDst->var = NULL; \
  922. }
  923. static void CopyHIDDeviceInfo(struct SDL_hid_device_info *pSrc, struct SDL_hid_device_info *pDst)
  924. {
  925. COPY_IF_EXISTS(path)
  926. pDst->vendor_id = pSrc->vendor_id;
  927. pDst->product_id = pSrc->product_id;
  928. WCOPY_IF_EXISTS(serial_number)
  929. pDst->release_number = pSrc->release_number;
  930. WCOPY_IF_EXISTS(manufacturer_string)
  931. WCOPY_IF_EXISTS(product_string)
  932. pDst->usage_page = pSrc->usage_page;
  933. pDst->usage = pSrc->usage;
  934. pDst->interface_number = pSrc->interface_number;
  935. pDst->interface_class = pSrc->interface_class;
  936. pDst->interface_subclass = pSrc->interface_subclass;
  937. pDst->interface_protocol = pSrc->interface_protocol;
  938. pDst->next = NULL;
  939. }
  940. #undef COPY_IF_EXISTS
  941. #undef WCOPY_IF_EXISTS
  942. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  943. #endif /* !SDL_HIDAPI_DISABLED */
  944. static int SDL_hidapi_refcount = 0;
  945. static void SDL_SetHIDAPIError(const wchar_t *error)
  946. {
  947. if (error) {
  948. char *error_utf8 = SDL_iconv_wchar_utf8(error);
  949. if (error_utf8) {
  950. SDL_SetError("%s", error_utf8);
  951. SDL_free(error_utf8);
  952. }
  953. }
  954. }
  955. int SDL_hid_init(void)
  956. {
  957. int attempts = 0, success = 0;
  958. if (SDL_hidapi_refcount > 0) {
  959. ++SDL_hidapi_refcount;
  960. return 0;
  961. }
  962. #if defined(SDL_USE_LIBUDEV)
  963. if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
  964. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  965. "udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
  966. linux_enumeration_method = ENUMERATION_FALLBACK;
  967. } else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
  968. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  969. "Container detected, disabling HIDAPI udev integration");
  970. linux_enumeration_method = ENUMERATION_FALLBACK;
  971. } else {
  972. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  973. "Using udev for HIDAPI joystick device discovery");
  974. linux_enumeration_method = ENUMERATION_LIBUDEV;
  975. }
  976. #endif
  977. #ifdef HAVE_LIBUSB
  978. use_libusb_whitelist = SDL_GetHintBoolean("SDL_HIDAPI_LIBUSB_WHITELIST",
  979. use_libusb_whitelist_default);
  980. if (SDL_getenv("SDL_HIDAPI_DISABLE_LIBUSB") != NULL) {
  981. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  982. "libusb disabled by SDL_HIDAPI_DISABLE_LIBUSB");
  983. libusb_ctx.libhandle = NULL;
  984. } else {
  985. ++attempts;
  986. #ifdef SDL_LIBUSB_DYNAMIC
  987. libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
  988. #else
  989. libusb_ctx.libhandle = (void *)1;
  990. #endif
  991. if (libusb_ctx.libhandle != NULL) {
  992. SDL_bool loaded = SDL_TRUE;
  993. #ifdef SDL_LIBUSB_DYNAMIC
  994. #define LOAD_LIBUSB_SYMBOL(func) \
  995. if (!(libusb_ctx.func = SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) { \
  996. loaded = SDL_FALSE; \
  997. }
  998. #else
  999. #define LOAD_LIBUSB_SYMBOL(func) \
  1000. libusb_ctx.func = libusb_##func;
  1001. #endif
  1002. LOAD_LIBUSB_SYMBOL(init)
  1003. LOAD_LIBUSB_SYMBOL(exit)
  1004. LOAD_LIBUSB_SYMBOL(get_device_list)
  1005. LOAD_LIBUSB_SYMBOL(free_device_list)
  1006. LOAD_LIBUSB_SYMBOL(get_device_descriptor)
  1007. LOAD_LIBUSB_SYMBOL(get_active_config_descriptor)
  1008. LOAD_LIBUSB_SYMBOL(get_config_descriptor)
  1009. LOAD_LIBUSB_SYMBOL(free_config_descriptor)
  1010. LOAD_LIBUSB_SYMBOL(get_bus_number)
  1011. LOAD_LIBUSB_SYMBOL(get_device_address)
  1012. LOAD_LIBUSB_SYMBOL(open)
  1013. LOAD_LIBUSB_SYMBOL(close)
  1014. LOAD_LIBUSB_SYMBOL(claim_interface)
  1015. LOAD_LIBUSB_SYMBOL(release_interface)
  1016. LOAD_LIBUSB_SYMBOL(kernel_driver_active)
  1017. LOAD_LIBUSB_SYMBOL(detach_kernel_driver)
  1018. LOAD_LIBUSB_SYMBOL(attach_kernel_driver)
  1019. LOAD_LIBUSB_SYMBOL(set_interface_alt_setting)
  1020. LOAD_LIBUSB_SYMBOL(alloc_transfer)
  1021. LOAD_LIBUSB_SYMBOL(submit_transfer)
  1022. LOAD_LIBUSB_SYMBOL(cancel_transfer)
  1023. LOAD_LIBUSB_SYMBOL(free_transfer)
  1024. LOAD_LIBUSB_SYMBOL(control_transfer)
  1025. LOAD_LIBUSB_SYMBOL(interrupt_transfer)
  1026. LOAD_LIBUSB_SYMBOL(handle_events)
  1027. LOAD_LIBUSB_SYMBOL(handle_events_completed)
  1028. LOAD_LIBUSB_SYMBOL(error_name)
  1029. #undef LOAD_LIBUSB_SYMBOL
  1030. if (!loaded) {
  1031. #ifdef SDL_LIBUSB_DYNAMIC
  1032. SDL_UnloadObject(libusb_ctx.libhandle);
  1033. #endif
  1034. libusb_ctx.libhandle = NULL;
  1035. /* SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function"); */
  1036. } else if (LIBUSB_hid_init() < 0) {
  1037. #ifdef SDL_LIBUSB_DYNAMIC
  1038. SDL_UnloadObject(libusb_ctx.libhandle);
  1039. #endif
  1040. libusb_ctx.libhandle = NULL;
  1041. } else {
  1042. ++success;
  1043. }
  1044. }
  1045. }
  1046. #endif /* HAVE_LIBUSB */
  1047. #ifdef HAVE_PLATFORM_BACKEND
  1048. ++attempts;
  1049. #ifdef __LINUX__
  1050. udev_ctx = SDL_UDEV_GetUdevSyms();
  1051. #endif /* __LINUX __ */
  1052. if (udev_ctx && PLATFORM_hid_init() == 0) {
  1053. ++success;
  1054. }
  1055. #endif /* HAVE_PLATFORM_BACKEND */
  1056. if (attempts > 0 && success == 0) {
  1057. return -1;
  1058. }
  1059. ++SDL_hidapi_refcount;
  1060. return 0;
  1061. }
  1062. int SDL_hid_exit(void)
  1063. {
  1064. int result = 0;
  1065. if (SDL_hidapi_refcount == 0) {
  1066. return 0;
  1067. }
  1068. --SDL_hidapi_refcount;
  1069. if (SDL_hidapi_refcount > 0) {
  1070. return 0;
  1071. }
  1072. SDL_hidapi_refcount = 0;
  1073. #ifndef SDL_HIDAPI_DISABLED
  1074. HIDAPI_ShutdownDiscovery();
  1075. #endif
  1076. #ifdef HAVE_PLATFORM_BACKEND
  1077. if (udev_ctx) {
  1078. result |= PLATFORM_hid_exit();
  1079. }
  1080. #ifdef __LINUX__
  1081. SDL_UDEV_ReleaseUdevSyms();
  1082. #endif /* __LINUX __ */
  1083. #endif /* HAVE_PLATFORM_BACKEND */
  1084. #ifdef HAVE_LIBUSB
  1085. if (libusb_ctx.libhandle) {
  1086. result |= LIBUSB_hid_exit();
  1087. #ifdef SDL_LIBUSB_DYNAMIC
  1088. SDL_UnloadObject(libusb_ctx.libhandle);
  1089. #endif
  1090. libusb_ctx.libhandle = NULL;
  1091. }
  1092. #endif /* HAVE_LIBUSB */
  1093. return result;
  1094. }
  1095. Uint32 SDL_hid_device_change_count(void)
  1096. {
  1097. Uint32 counter = 0;
  1098. #ifndef SDL_HIDAPI_DISABLED
  1099. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1100. return 0;
  1101. }
  1102. HIDAPI_UpdateDiscovery();
  1103. if (SDL_HIDAPI_discovery.m_unDeviceChangeCounter == 0) {
  1104. /* Counter wrapped! */
  1105. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1106. }
  1107. counter = SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1108. #endif /* !SDL_HIDAPI_DISABLED */
  1109. return counter;
  1110. }
  1111. struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)
  1112. {
  1113. #if defined(HAVE_PLATFORM_BACKEND) || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1114. #ifdef HAVE_LIBUSB
  1115. struct SDL_hid_device_info *usb_devs = NULL;
  1116. struct SDL_hid_device_info *usb_dev;
  1117. #endif
  1118. #if HAVE_DRIVER_BACKEND
  1119. struct SDL_hid_device_info *driver_devs = NULL;
  1120. struct SDL_hid_device_info *driver_dev;
  1121. #endif
  1122. #ifdef HAVE_PLATFORM_BACKEND
  1123. struct SDL_hid_device_info *raw_devs = NULL;
  1124. struct SDL_hid_device_info *raw_dev;
  1125. #endif
  1126. struct SDL_hid_device_info *devs = NULL, *last = NULL, *new_dev;
  1127. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1128. return NULL;
  1129. }
  1130. #ifdef HAVE_LIBUSB
  1131. if (libusb_ctx.libhandle) {
  1132. usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
  1133. #ifdef DEBUG_HIDAPI
  1134. SDL_Log("libusb devices found:");
  1135. #endif
  1136. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  1137. if (use_libusb_whitelist) {
  1138. if (!IsInWhitelist(usb_dev->vendor_id, usb_dev->product_id)) {
  1139. #ifdef DEBUG_HIDAPI
  1140. SDL_Log("Device was not in libusb whitelist: %ls %ls 0x%.4hx 0x%.4hx",
  1141. usb_dev->manufacturer_string, usb_dev->product_string,
  1142. usb_dev->vendor_id, usb_dev->product_id);
  1143. #endif /* DEBUG_HIDAPI */
  1144. continue;
  1145. }
  1146. }
  1147. new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
  1148. if (new_dev == NULL) {
  1149. LIBUSB_hid_free_enumeration(usb_devs);
  1150. SDL_hid_free_enumeration(devs);
  1151. SDL_OutOfMemory();
  1152. return NULL;
  1153. }
  1154. CopyHIDDeviceInfo(usb_dev, new_dev);
  1155. #ifdef DEBUG_HIDAPI
  1156. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  1157. usb_dev->manufacturer_string, usb_dev->product_string,
  1158. usb_dev->vendor_id, usb_dev->product_id);
  1159. #endif
  1160. if (last != NULL) {
  1161. last->next = new_dev;
  1162. } else {
  1163. devs = new_dev;
  1164. }
  1165. last = new_dev;
  1166. }
  1167. }
  1168. #endif /* HAVE_LIBUSB */
  1169. #if HAVE_DRIVER_BACKEND
  1170. driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);
  1171. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  1172. new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
  1173. CopyHIDDeviceInfo(driver_dev, new_dev);
  1174. if (last != NULL) {
  1175. last->next = new_dev;
  1176. } else {
  1177. devs = new_dev;
  1178. }
  1179. last = new_dev;
  1180. }
  1181. #endif /* HAVE_DRIVER_BACKEND */
  1182. #ifdef HAVE_PLATFORM_BACKEND
  1183. if (udev_ctx) {
  1184. raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
  1185. #ifdef DEBUG_HIDAPI
  1186. SDL_Log("hidraw devices found:");
  1187. #endif
  1188. for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
  1189. SDL_bool bFound = SDL_FALSE;
  1190. #ifdef DEBUG_HIDAPI
  1191. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  1192. raw_dev->manufacturer_string, raw_dev->product_string,
  1193. raw_dev->vendor_id, raw_dev->product_id);
  1194. #endif
  1195. #ifdef HAVE_LIBUSB
  1196. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  1197. if (raw_dev->vendor_id == usb_dev->vendor_id &&
  1198. raw_dev->product_id == usb_dev->product_id &&
  1199. (raw_dev->interface_number < 0 || raw_dev->interface_number == usb_dev->interface_number)) {
  1200. bFound = SDL_TRUE;
  1201. break;
  1202. }
  1203. }
  1204. #endif
  1205. #if HAVE_DRIVER_BACKEND
  1206. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  1207. if (raw_dev->vendor_id == driver_dev->vendor_id &&
  1208. raw_dev->product_id == driver_dev->product_id &&
  1209. (raw_dev->interface_number < 0 || raw_dev->interface_number == driver_dev->interface_number)) {
  1210. bFound = SDL_TRUE;
  1211. break;
  1212. }
  1213. }
  1214. #endif
  1215. if (!bFound) {
  1216. new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
  1217. if (new_dev == NULL) {
  1218. #ifdef HAVE_LIBUSB
  1219. if (libusb_ctx.libhandle) {
  1220. LIBUSB_hid_free_enumeration(usb_devs);
  1221. }
  1222. #endif
  1223. PLATFORM_hid_free_enumeration(raw_devs);
  1224. SDL_hid_free_enumeration(devs);
  1225. SDL_OutOfMemory();
  1226. return NULL;
  1227. }
  1228. CopyHIDDeviceInfo(raw_dev, new_dev);
  1229. new_dev->next = NULL;
  1230. if (last != NULL) {
  1231. last->next = new_dev;
  1232. } else {
  1233. devs = new_dev;
  1234. }
  1235. last = new_dev;
  1236. }
  1237. }
  1238. PLATFORM_hid_free_enumeration(raw_devs);
  1239. }
  1240. #endif /* HAVE_PLATFORM_BACKEND */
  1241. #ifdef HAVE_LIBUSB
  1242. if (libusb_ctx.libhandle) {
  1243. LIBUSB_hid_free_enumeration(usb_devs);
  1244. }
  1245. #endif
  1246. return devs;
  1247. #else
  1248. return NULL;
  1249. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1250. }
  1251. void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
  1252. {
  1253. while (devs) {
  1254. struct SDL_hid_device_info *next = devs->next;
  1255. SDL_free(devs->path);
  1256. SDL_free(devs->serial_number);
  1257. SDL_free(devs->manufacturer_string);
  1258. SDL_free(devs->product_string);
  1259. SDL_free(devs);
  1260. devs = next;
  1261. }
  1262. }
  1263. SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
  1264. {
  1265. #if defined(HAVE_PLATFORM_BACKEND) || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1266. void *pDevice = NULL;
  1267. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1268. return NULL;
  1269. }
  1270. #ifdef HAVE_PLATFORM_BACKEND
  1271. if (udev_ctx) {
  1272. pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number);
  1273. if (pDevice != NULL) {
  1274. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1275. }
  1276. }
  1277. #endif /* HAVE_PLATFORM_BACKEND */
  1278. #if HAVE_DRIVER_BACKEND
  1279. pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number);
  1280. if (pDevice != NULL) {
  1281. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1282. }
  1283. #endif /* HAVE_DRIVER_BACKEND */
  1284. #ifdef HAVE_LIBUSB
  1285. if (libusb_ctx.libhandle != NULL) {
  1286. pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);
  1287. if (pDevice != NULL) {
  1288. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1289. }
  1290. }
  1291. #endif /* HAVE_LIBUSB */
  1292. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1293. return NULL;
  1294. }
  1295. SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */)
  1296. {
  1297. #if defined(HAVE_PLATFORM_BACKEND) || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1298. void *pDevice = NULL;
  1299. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1300. return NULL;
  1301. }
  1302. #ifdef HAVE_PLATFORM_BACKEND
  1303. if (udev_ctx) {
  1304. pDevice = PLATFORM_hid_open_path(path, bExclusive);
  1305. if (pDevice != NULL) {
  1306. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1307. }
  1308. }
  1309. #endif /* HAVE_PLATFORM_BACKEND */
  1310. #if HAVE_DRIVER_BACKEND
  1311. pDevice = DRIVER_hid_open_path(path, bExclusive);
  1312. if (pDevice != NULL) {
  1313. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1314. }
  1315. #endif /* HAVE_DRIVER_BACKEND */
  1316. #ifdef HAVE_LIBUSB
  1317. if (libusb_ctx.libhandle != NULL) {
  1318. pDevice = LIBUSB_hid_open_path(path, bExclusive);
  1319. if (pDevice != NULL) {
  1320. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1321. }
  1322. }
  1323. #endif /* HAVE_LIBUSB */
  1324. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1325. return NULL;
  1326. }
  1327. int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
  1328. {
  1329. int result;
  1330. CHECK_DEVICE_MAGIC(device, -1);
  1331. result = device->backend->hid_write(device->device, data, length);
  1332. if (result < 0) {
  1333. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1334. }
  1335. return result;
  1336. }
  1337. int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds)
  1338. {
  1339. int result;
  1340. CHECK_DEVICE_MAGIC(device, -1);
  1341. result = device->backend->hid_read_timeout(device->device, data, length, milliseconds);
  1342. if (result < 0) {
  1343. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1344. }
  1345. return result;
  1346. }
  1347. int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length)
  1348. {
  1349. int result;
  1350. CHECK_DEVICE_MAGIC(device, -1);
  1351. result = device->backend->hid_read(device->device, data, length);
  1352. if (result < 0) {
  1353. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1354. }
  1355. return result;
  1356. }
  1357. int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock)
  1358. {
  1359. int result;
  1360. CHECK_DEVICE_MAGIC(device, -1);
  1361. result = device->backend->hid_set_nonblocking(device->device, nonblock);
  1362. if (result < 0) {
  1363. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1364. }
  1365. return result;
  1366. }
  1367. int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length)
  1368. {
  1369. int result;
  1370. CHECK_DEVICE_MAGIC(device, -1);
  1371. result = device->backend->hid_send_feature_report(device->device, data, length);
  1372. if (result < 0) {
  1373. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1374. }
  1375. return result;
  1376. }
  1377. int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length)
  1378. {
  1379. int result;
  1380. CHECK_DEVICE_MAGIC(device, -1);
  1381. result = device->backend->hid_get_feature_report(device->device, data, length);
  1382. if (result < 0) {
  1383. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1384. }
  1385. return result;
  1386. }
  1387. void SDL_hid_close(SDL_hid_device *device)
  1388. {
  1389. CHECK_DEVICE_MAGIC(device, );
  1390. device->backend->hid_close(device->device);
  1391. DeleteHIDDeviceWrapper(device);
  1392. }
  1393. int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1394. {
  1395. int result;
  1396. CHECK_DEVICE_MAGIC(device, -1);
  1397. result = device->backend->hid_get_manufacturer_string(device->device, string, maxlen);
  1398. if (result < 0) {
  1399. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1400. }
  1401. return result;
  1402. }
  1403. int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1404. {
  1405. int result;
  1406. CHECK_DEVICE_MAGIC(device, -1);
  1407. result = device->backend->hid_get_product_string(device->device, string, maxlen);
  1408. if (result < 0) {
  1409. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1410. }
  1411. return result;
  1412. }
  1413. int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1414. {
  1415. int result;
  1416. CHECK_DEVICE_MAGIC(device, -1);
  1417. result = device->backend->hid_get_serial_number_string(device->device, string, maxlen);
  1418. if (result < 0) {
  1419. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1420. }
  1421. return result;
  1422. }
  1423. int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen)
  1424. {
  1425. int result;
  1426. CHECK_DEVICE_MAGIC(device, -1);
  1427. result = device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen);
  1428. if (result < 0) {
  1429. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1430. }
  1431. return result;
  1432. }
  1433. void SDL_hid_ble_scan(SDL_bool active)
  1434. {
  1435. #if !defined(SDL_HIDAPI_DISABLED) && (defined(__IPHONEOS__) || defined(__TVOS__))
  1436. hid_ble_scan(active);
  1437. #endif
  1438. }
  1439. #ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
  1440. /* This is needed to enable input for Nyko and EVORETRO GameCube adaptors */
  1441. void SDL_EnableGameCubeAdaptors(void)
  1442. {
  1443. #ifdef HAVE_LIBUSB
  1444. libusb_context *context = NULL;
  1445. libusb_device **devs = NULL;
  1446. libusb_device_handle *handle = NULL;
  1447. struct libusb_device_descriptor desc;
  1448. ssize_t i, num_devs;
  1449. int kernel_detached = 0;
  1450. if (libusb_ctx.libhandle == NULL) {
  1451. return;
  1452. }
  1453. if (libusb_ctx.init(&context) == 0) {
  1454. num_devs = libusb_ctx.get_device_list(context, &devs);
  1455. for (i = 0; i < num_devs; ++i) {
  1456. if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) {
  1457. continue;
  1458. }
  1459. if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
  1460. continue;
  1461. }
  1462. if (libusb_ctx.open(devs[i], &handle) != 0) {
  1463. continue;
  1464. }
  1465. if (libusb_ctx.kernel_driver_active(handle, 0)) {
  1466. if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) {
  1467. kernel_detached = 1;
  1468. }
  1469. }
  1470. if (libusb_ctx.claim_interface(handle, 0) == 0) {
  1471. libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
  1472. libusb_ctx.release_interface(handle, 0);
  1473. }
  1474. if (kernel_detached) {
  1475. libusb_ctx.attach_kernel_driver(handle, 0);
  1476. }
  1477. libusb_ctx.close(handle);
  1478. }
  1479. libusb_ctx.free_device_list(devs, 1);
  1480. libusb_ctx.exit(context);
  1481. }
  1482. #endif /* HAVE_LIBUSB */
  1483. }
  1484. #endif /* HAVE_ENABLE_GAMECUBE_ADAPTORS */
  1485. /* vi: set sts=4 ts=4 sw=4 expandtab: */