SDL_hidapi.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #if !SDL_HIDAPI_DISABLED
  29. #if defined(__WIN32__) || defined(__WINGDK__)
  30. #include "../core/windows/SDL_windows.h"
  31. #endif
  32. #if defined(__MACOS__)
  33. #include <CoreFoundation/CoreFoundation.h>
  34. #include <mach/mach.h>
  35. #include <IOKit/IOKitLib.h>
  36. #include <IOKit/hid/IOHIDDevice.h>
  37. #include <IOKit/usb/USBSpec.h>
  38. #include <AvailabilityMacros.h>
  39. /* Things named "Master" were renamed to "Main" in macOS 12.0's SDK. */
  40. #if MAC_OS_X_VERSION_MIN_REQUIRED < 120000
  41. #define kIOMainPortDefault kIOMasterPortDefault
  42. #endif
  43. #endif
  44. #include "../core/linux/SDL_udev.h"
  45. #ifdef SDL_USE_LIBUDEV
  46. #include <poll.h>
  47. #include "../core/linux/SDL_sandbox.h"
  48. #endif
  49. #ifdef HAVE_INOTIFY
  50. #include <string.h> /* strerror */
  51. #include <errno.h> /* errno */
  52. #include <fcntl.h>
  53. #include <limits.h> /* For the definition of NAME_MAX */
  54. #include <sys/inotify.h>
  55. #endif
  56. #if defined(SDL_USE_LIBUDEV) || defined(HAVE_INOTIFY)
  57. #include <unistd.h>
  58. #endif
  59. #if defined(SDL_USE_LIBUDEV)
  60. typedef enum
  61. {
  62. ENUMERATION_UNSET,
  63. ENUMERATION_LIBUDEV,
  64. ENUMERATION_FALLBACK
  65. } LinuxEnumerationMethod;
  66. static LinuxEnumerationMethod linux_enumeration_method = ENUMERATION_UNSET;
  67. #endif
  68. #if defined(HAVE_INOTIFY)
  69. static int inotify_fd = -1;
  70. #endif
  71. #if defined(SDL_USE_LIBUDEV)
  72. static const SDL_UDEV_Symbols *usyms = NULL;
  73. #endif
  74. static struct
  75. {
  76. SDL_bool m_bInitialized;
  77. Uint32 m_unDeviceChangeCounter;
  78. SDL_bool m_bCanGetNotifications;
  79. Uint64 m_unLastDetect;
  80. #if defined(__WIN32__) || defined(__WINGDK__)
  81. SDL_threadID m_nThreadID;
  82. WNDCLASSEXA m_wndClass;
  83. HWND m_hwndMsg;
  84. HDEVNOTIFY m_hNotify;
  85. double m_flLastWin32MessageCheck;
  86. #endif
  87. #if defined(__MACOS__)
  88. IONotificationPortRef m_notificationPort;
  89. mach_port_t m_notificationMach;
  90. #endif
  91. #if defined(SDL_USE_LIBUDEV)
  92. struct udev *m_pUdev;
  93. struct udev_monitor *m_pUdevMonitor;
  94. int m_nUdevFd;
  95. #endif
  96. } SDL_HIDAPI_discovery;
  97. #if defined(__WIN32__) || defined(__WINGDK__)
  98. struct _DEV_BROADCAST_HDR
  99. {
  100. DWORD dbch_size;
  101. DWORD dbch_devicetype;
  102. DWORD dbch_reserved;
  103. };
  104. typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
  105. {
  106. DWORD dbcc_size;
  107. DWORD dbcc_devicetype;
  108. DWORD dbcc_reserved;
  109. GUID dbcc_classguid;
  110. char dbcc_name[1];
  111. } DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;
  112. typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
  113. #define DBT_DEVICEARRIVAL 0x8000 /* system detected a new device */
  114. #define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
  115. #define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
  116. #define DBT_DEVNODES_CHANGED 0x0007
  117. #define DBT_CONFIGCHANGED 0x0018
  118. #define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
  119. #define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
  120. #include <initguid.h>
  121. DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
  122. static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  123. {
  124. switch (message) {
  125. case WM_DEVICECHANGE:
  126. switch (wParam) {
  127. case DBT_DEVICEARRIVAL:
  128. case DBT_DEVICEREMOVECOMPLETE:
  129. if (((DEV_BROADCAST_HDR *)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  130. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  131. }
  132. break;
  133. }
  134. return TRUE;
  135. }
  136. return DefWindowProc(hwnd, message, wParam, lParam);
  137. }
  138. #endif /* defined(__WIN32__) || defined(__WINGDK__) */
  139. #if defined(__MACOS__)
  140. static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
  141. {
  142. /* Must drain the iterator, or we won't receive new notifications */
  143. io_object_t entry;
  144. while ((entry = IOIteratorNext(portIterator)) != 0) {
  145. IOObjectRelease(entry);
  146. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  147. }
  148. }
  149. #endif /* __MACOS__ */
  150. #ifdef HAVE_INOTIFY
  151. #ifdef HAVE_INOTIFY_INIT1
  152. static int SDL_inotify_init1(void)
  153. {
  154. return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  155. }
  156. #else
  157. static int SDL_inotify_init1(void)
  158. {
  159. int fd = inotify_init();
  160. if (fd < 0) {
  161. return -1;
  162. }
  163. fcntl(fd, F_SETFL, O_NONBLOCK);
  164. fcntl(fd, F_SETFD, FD_CLOEXEC);
  165. return fd;
  166. }
  167. #endif
  168. static int
  169. StrHasPrefix(const char *string, const char *prefix)
  170. {
  171. return SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0;
  172. }
  173. static int
  174. StrIsInteger(const char *string)
  175. {
  176. const char *p;
  177. if (*string == '\0') {
  178. return 0;
  179. }
  180. for (p = string; *p != '\0'; p++) {
  181. if (*p < '0' || *p > '9') {
  182. return 0;
  183. }
  184. }
  185. return 1;
  186. }
  187. #endif /* HAVE_INOTIFY */
  188. static void
  189. HIDAPI_InitializeDiscovery()
  190. {
  191. SDL_HIDAPI_discovery.m_bInitialized = SDL_TRUE;
  192. SDL_HIDAPI_discovery.m_unDeviceChangeCounter = 1;
  193. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
  194. SDL_HIDAPI_discovery.m_unLastDetect = 0;
  195. #if defined(__WIN32__) || defined(__WINGDK__)
  196. SDL_HIDAPI_discovery.m_nThreadID = SDL_ThreadID();
  197. SDL_zero(SDL_HIDAPI_discovery.m_wndClass);
  198. SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
  199. SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
  200. SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; /* This function is called by windows */
  201. SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);
  202. RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
  203. SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  204. {
  205. DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;
  206. SDL_zero(devBroadcast);
  207. devBroadcast.dbcc_size = sizeof(devBroadcast);
  208. devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  209. devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
  210. /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
  211. * but that seems to be necessary to get a notice after each individual usb input device actually
  212. * installs, rather than just as the composite device is seen.
  213. */
  214. SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification(SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
  215. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_hNotify != 0);
  216. }
  217. #endif /* defined(__WIN32__) || defined(__WINGDK__) */
  218. #if defined(__MACOS__)
  219. SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMainPortDefault);
  220. if (SDL_HIDAPI_discovery.m_notificationPort) {
  221. {
  222. io_iterator_t portIterator = 0;
  223. io_object_t entry;
  224. IOReturn result = IOServiceAddMatchingNotification(
  225. SDL_HIDAPI_discovery.m_notificationPort,
  226. kIOFirstMatchNotification,
  227. IOServiceMatching(kIOHIDDeviceKey),
  228. CallbackIOServiceFunc, NULL, &portIterator);
  229. if (result == 0) {
  230. /* Must drain the existing iterator, or we won't receive new notifications */
  231. while ((entry = IOIteratorNext(portIterator)) != 0) {
  232. IOObjectRelease(entry);
  233. }
  234. } else {
  235. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  236. SDL_HIDAPI_discovery.m_notificationPort = nil;
  237. }
  238. }
  239. {
  240. io_iterator_t portIterator = 0;
  241. io_object_t entry;
  242. IOReturn result = IOServiceAddMatchingNotification(
  243. SDL_HIDAPI_discovery.m_notificationPort,
  244. kIOTerminatedNotification,
  245. IOServiceMatching(kIOHIDDeviceKey),
  246. CallbackIOServiceFunc, NULL, &portIterator);
  247. if (result == 0) {
  248. /* Must drain the existing iterator, or we won't receive new notifications */
  249. while ((entry = IOIteratorNext(portIterator)) != 0) {
  250. IOObjectRelease(entry);
  251. }
  252. } else {
  253. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  254. SDL_HIDAPI_discovery.m_notificationPort = nil;
  255. }
  256. }
  257. }
  258. SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
  259. if (SDL_HIDAPI_discovery.m_notificationPort) {
  260. SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
  261. }
  262. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);
  263. #endif /* __MACOS__ */
  264. #if defined(SDL_USE_LIBUDEV)
  265. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  266. SDL_HIDAPI_discovery.m_pUdev = NULL;
  267. SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
  268. SDL_HIDAPI_discovery.m_nUdevFd = -1;
  269. usyms = SDL_UDEV_GetUdevSyms();
  270. if (usyms != NULL) {
  271. SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
  272. if (SDL_HIDAPI_discovery.m_pUdev != NULL) {
  273. SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
  274. if (SDL_HIDAPI_discovery.m_pUdevMonitor != NULL) {
  275. usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
  276. SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
  277. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
  278. }
  279. }
  280. }
  281. } else
  282. #endif /* SDL_USE_LIBUDEV */
  283. {
  284. #if defined(HAVE_INOTIFY)
  285. inotify_fd = SDL_inotify_init1();
  286. if (inotify_fd < 0) {
  287. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  288. "Unable to initialize inotify, falling back to polling: %s",
  289. strerror(errno));
  290. return;
  291. }
  292. /* We need to watch for attribute changes in addition to
  293. * creation, because when a device is first created, it has
  294. * permissions that we can't read. When udev chmods it to
  295. * something that we maybe *can* read, we'll get an
  296. * IN_ATTRIB event to tell us. */
  297. if (inotify_add_watch(inotify_fd, "/dev",
  298. IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
  299. close(inotify_fd);
  300. inotify_fd = -1;
  301. SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
  302. "Unable to add inotify watch, falling back to polling: %s",
  303. strerror(errno));
  304. return;
  305. }
  306. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
  307. #endif /* HAVE_INOTIFY */
  308. }
  309. }
  310. static void
  311. HIDAPI_UpdateDiscovery()
  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(__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(__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. #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
  417. HIDAPI_ShutdownDiscovery()
  418. {
  419. if (!SDL_HIDAPI_discovery.m_bInitialized) {
  420. return;
  421. }
  422. #if defined(__WIN32__) || defined(__WINGDK__)
  423. if (SDL_HIDAPI_discovery.m_hNotify) {
  424. UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
  425. }
  426. if (SDL_HIDAPI_discovery.m_hwndMsg) {
  427. DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
  428. }
  429. UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
  430. #endif
  431. #if defined(__MACOS__)
  432. if (SDL_HIDAPI_discovery.m_notificationPort) {
  433. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  434. }
  435. #endif
  436. #if defined(SDL_USE_LIBUDEV)
  437. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  438. if (usyms) {
  439. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  440. usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
  441. }
  442. if (SDL_HIDAPI_discovery.m_pUdev) {
  443. usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
  444. }
  445. SDL_UDEV_ReleaseUdevSyms();
  446. usyms = NULL;
  447. }
  448. } else
  449. #endif /* SDL_USE_LIBUDEV */
  450. {
  451. #if defined(HAVE_INOTIFY)
  452. if (inotify_fd >= 0) {
  453. close(inotify_fd);
  454. inotify_fd = -1;
  455. }
  456. #endif
  457. }
  458. SDL_HIDAPI_discovery.m_bInitialized = SDL_FALSE;
  459. }
  460. /* Platform HIDAPI Implementation */
  461. #define hid_device PLATFORM_hid_device
  462. #define hid_device_ PLATFORM_hid_device_
  463. #define hid_init PLATFORM_hid_init
  464. #define hid_exit PLATFORM_hid_exit
  465. #define hid_enumerate PLATFORM_hid_enumerate
  466. #define hid_free_enumeration PLATFORM_hid_free_enumeration
  467. #define hid_open PLATFORM_hid_open
  468. #define hid_open_path PLATFORM_hid_open_path
  469. #define hid_write PLATFORM_hid_write
  470. #define hid_read_timeout PLATFORM_hid_read_timeout
  471. #define hid_read PLATFORM_hid_read
  472. #define hid_set_nonblocking PLATFORM_hid_set_nonblocking
  473. #define hid_send_feature_report PLATFORM_hid_send_feature_report
  474. #define hid_get_feature_report PLATFORM_hid_get_feature_report
  475. #define hid_close PLATFORM_hid_close
  476. #define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string
  477. #define hid_get_product_string PLATFORM_hid_get_product_string
  478. #define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
  479. #define hid_get_indexed_string PLATFORM_hid_get_indexed_string
  480. #define hid_error PLATFORM_hid_error
  481. #define new_hid_device PLATFORM_new_hid_device
  482. #define free_hid_device PLATFORM_free_hid_device
  483. #define input_report PLATFORM_input_report
  484. #define return_data PLATFORM_return_data
  485. #define make_path PLATFORM_make_path
  486. #define read_thread PLATFORM_read_thread
  487. #undef HIDAPI_H__
  488. #if __LINUX__
  489. #if SDL_USE_LIBUDEV
  490. static const SDL_UDEV_Symbols *udev_ctx = NULL;
  491. #define udev_device_get_sysattr_value udev_ctx->udev_device_get_sysattr_value
  492. #define udev_new udev_ctx->udev_new
  493. #define udev_unref udev_ctx->udev_unref
  494. #define udev_device_new_from_devnum udev_ctx->udev_device_new_from_devnum
  495. #define udev_device_get_parent_with_subsystem_devtype udev_ctx->udev_device_get_parent_with_subsystem_devtype
  496. #define udev_device_unref udev_ctx->udev_device_unref
  497. #define udev_enumerate_new udev_ctx->udev_enumerate_new
  498. #define udev_enumerate_add_match_subsystem udev_ctx->udev_enumerate_add_match_subsystem
  499. #define udev_enumerate_scan_devices udev_ctx->udev_enumerate_scan_devices
  500. #define udev_enumerate_get_list_entry udev_ctx->udev_enumerate_get_list_entry
  501. #define udev_list_entry_get_name udev_ctx->udev_list_entry_get_name
  502. #define udev_device_new_from_syspath udev_ctx->udev_device_new_from_syspath
  503. #define udev_device_get_devnode udev_ctx->udev_device_get_devnode
  504. #define udev_list_entry_get_next udev_ctx->udev_list_entry_get_next
  505. #define udev_enumerate_unref udev_ctx->udev_enumerate_unref
  506. #include "linux/hid.c"
  507. #define HAVE_PLATFORM_BACKEND 1
  508. #endif /* SDL_USE_LIBUDEV */
  509. #elif __MACOS__
  510. #include "mac/hid.c"
  511. #define HAVE_PLATFORM_BACKEND 1
  512. #define udev_ctx 1
  513. #elif __WINDOWS__ || __WINGDK__
  514. #include "windows/hid.c"
  515. #define HAVE_PLATFORM_BACKEND 1
  516. #define udev_ctx 1
  517. #elif __ANDROID__
  518. /* The implementation for Android is in a separate .cpp file */
  519. #include "hidapi/hidapi.h"
  520. #define HAVE_PLATFORM_BACKEND 1
  521. #define udev_ctx 1
  522. #elif __IOS__ || __TVOS__
  523. /* The implementation for iOS and tvOS is in a separate .m file */
  524. #include "hidapi/hidapi.h"
  525. #define HAVE_PLATFORM_BACKEND 1
  526. #define udev_ctx 1
  527. #endif
  528. #undef hid_device
  529. #undef hid_device_
  530. #undef hid_init
  531. #undef hid_exit
  532. #undef hid_enumerate
  533. #undef hid_free_enumeration
  534. #undef hid_open
  535. #undef hid_open_path
  536. #undef hid_write
  537. #undef hid_read_timeout
  538. #undef hid_read
  539. #undef hid_set_nonblocking
  540. #undef hid_send_feature_report
  541. #undef hid_get_feature_report
  542. #undef hid_close
  543. #undef hid_get_manufacturer_string
  544. #undef hid_get_product_string
  545. #undef hid_get_serial_number_string
  546. #undef hid_get_indexed_string
  547. #undef hid_error
  548. #undef new_hid_device
  549. #undef free_hid_device
  550. #undef input_report
  551. #undef return_data
  552. #undef make_path
  553. #undef read_thread
  554. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  555. #define HAVE_DRIVER_BACKEND 1
  556. #endif
  557. #ifdef HAVE_DRIVER_BACKEND
  558. /* DRIVER HIDAPI Implementation */
  559. #define hid_device DRIVER_hid_device
  560. #define hid_device_ DRIVER_hid_device_
  561. #define hid_init DRIVER_hid_init
  562. #define hid_exit DRIVER_hid_exit
  563. #define hid_enumerate DRIVER_hid_enumerate
  564. #define hid_free_enumeration DRIVER_hid_free_enumeration
  565. #define hid_open DRIVER_hid_open
  566. #define hid_open_path DRIVER_hid_open_path
  567. #define hid_write DRIVER_hid_write
  568. #define hid_read_timeout DRIVER_hid_read_timeout
  569. #define hid_read DRIVER_hid_read
  570. #define hid_set_nonblocking DRIVER_hid_set_nonblocking
  571. #define hid_send_feature_report DRIVER_hid_send_feature_report
  572. #define hid_get_feature_report DRIVER_hid_get_feature_report
  573. #define hid_close DRIVER_hid_close
  574. #define hid_get_manufacturer_string DRIVER_hid_get_manufacturer_string
  575. #define hid_get_product_string DRIVER_hid_get_product_string
  576. #define hid_get_serial_number_string DRIVER_hid_get_serial_number_string
  577. #define hid_get_indexed_string DRIVER_hid_get_indexed_string
  578. #define hid_error DRIVER_hid_error
  579. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  580. #undef HIDAPI_H__
  581. #include "steamxbox/hid.c"
  582. #else
  583. #error Need a driver hid.c for this platform!
  584. #endif
  585. #undef hid_device
  586. #undef hid_device_
  587. #undef hid_init
  588. #undef hid_exit
  589. #undef hid_enumerate
  590. #undef hid_free_enumeration
  591. #undef hid_open
  592. #undef hid_open_path
  593. #undef hid_write
  594. #undef hid_read_timeout
  595. #undef hid_read
  596. #undef hid_set_nonblocking
  597. #undef hid_send_feature_report
  598. #undef hid_get_feature_report
  599. #undef hid_close
  600. #undef hid_get_manufacturer_string
  601. #undef hid_get_product_string
  602. #undef hid_get_serial_number_string
  603. #undef hid_get_indexed_string
  604. #undef hid_error
  605. #endif /* HAVE_DRIVER_BACKEND */
  606. #ifdef HAVE_LIBUSB
  607. /* libusb HIDAPI Implementation */
  608. /* Include this now, for our dynamically-loaded libusb context */
  609. #include <libusb.h>
  610. static struct
  611. {
  612. void *libhandle;
  613. /* *INDENT-OFF* */ /* clang-format off */
  614. int (LIBUSB_CALL *init)(libusb_context **ctx);
  615. void (LIBUSB_CALL *exit)(libusb_context *ctx);
  616. ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list);
  617. void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices);
  618. int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
  619. int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);
  620. int (LIBUSB_CALL *get_config_descriptor)(
  621. libusb_device *dev,
  622. uint8_t config_index,
  623. struct libusb_config_descriptor **config
  624. );
  625. void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config);
  626. uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev);
  627. uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev);
  628. int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle);
  629. void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle);
  630. int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number);
  631. int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number);
  632. int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
  633. int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  634. int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  635. int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
  636. struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets);
  637. int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer);
  638. int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer);
  639. void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer);
  640. int (LIBUSB_CALL *control_transfer)(
  641. libusb_device_handle *dev_handle,
  642. uint8_t request_type,
  643. uint8_t bRequest,
  644. uint16_t wValue,
  645. uint16_t wIndex,
  646. unsigned char *data,
  647. uint16_t wLength,
  648. unsigned int timeout
  649. );
  650. int (LIBUSB_CALL *interrupt_transfer)(
  651. libusb_device_handle *dev_handle,
  652. unsigned char endpoint,
  653. unsigned char *data,
  654. int length,
  655. int *actual_length,
  656. unsigned int timeout
  657. );
  658. int (LIBUSB_CALL *handle_events)(libusb_context *ctx);
  659. int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed);
  660. const char * (LIBUSB_CALL *error_name)(int errcode);
  661. /* *INDENT-ON* */ /* clang-format on */
  662. } libusb_ctx;
  663. #define libusb_init libusb_ctx.init
  664. #define libusb_exit libusb_ctx.exit
  665. #define libusb_get_device_list libusb_ctx.get_device_list
  666. #define libusb_free_device_list libusb_ctx.free_device_list
  667. #define libusb_get_device_descriptor libusb_ctx.get_device_descriptor
  668. #define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
  669. #define libusb_get_config_descriptor libusb_ctx.get_config_descriptor
  670. #define libusb_free_config_descriptor libusb_ctx.free_config_descriptor
  671. #define libusb_get_bus_number libusb_ctx.get_bus_number
  672. #define libusb_get_device_address libusb_ctx.get_device_address
  673. #define libusb_open libusb_ctx.open
  674. #define libusb_close libusb_ctx.close
  675. #define libusb_claim_interface libusb_ctx.claim_interface
  676. #define libusb_release_interface libusb_ctx.release_interface
  677. #define libusb_kernel_driver_active libusb_ctx.kernel_driver_active
  678. #define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver
  679. #define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver
  680. #define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting
  681. #define libusb_alloc_transfer libusb_ctx.alloc_transfer
  682. #define libusb_submit_transfer libusb_ctx.submit_transfer
  683. #define libusb_cancel_transfer libusb_ctx.cancel_transfer
  684. #define libusb_free_transfer libusb_ctx.free_transfer
  685. #define libusb_control_transfer libusb_ctx.control_transfer
  686. #define libusb_interrupt_transfer libusb_ctx.interrupt_transfer
  687. #define libusb_handle_events libusb_ctx.handle_events
  688. #define libusb_handle_events_completed libusb_ctx.handle_events_completed
  689. #define libusb_error_name libusb_ctx.error_name
  690. #define hid_device LIBUSB_hid_device
  691. #define hid_device_ LIBUSB_hid_device_
  692. #define hid_init LIBUSB_hid_init
  693. #define hid_exit LIBUSB_hid_exit
  694. #define hid_enumerate LIBUSB_hid_enumerate
  695. #define hid_free_enumeration LIBUSB_hid_free_enumeration
  696. #define hid_open LIBUSB_hid_open
  697. #define hid_open_path LIBUSB_hid_open_path
  698. #define hid_write LIBUSB_hid_write
  699. #define hid_read_timeout LIBUSB_hid_read_timeout
  700. #define hid_read LIBUSB_hid_read
  701. #define hid_set_nonblocking LIBUSB_hid_set_nonblocking
  702. #define hid_send_feature_report LIBUSB_hid_send_feature_report
  703. #define hid_get_feature_report LIBUSB_hid_get_feature_report
  704. #define hid_close LIBUSB_hid_close
  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_serial_number_string LIBUSB_hid_get_serial_number_string
  708. #define hid_get_indexed_string LIBUSB_hid_get_indexed_string
  709. #define hid_error LIBUSB_hid_error
  710. #define new_hid_device LIBUSB_new_hid_device
  711. #define free_hid_device LIBUSB_free_hid_device
  712. #define input_report LIBUSB_input_report
  713. #define return_data LIBUSB_return_data
  714. #define make_path LIBUSB_make_path
  715. #define read_thread LIBUSB_read_thread
  716. #ifndef __FreeBSD__
  717. /* this is awkwardly inlined, so we need to re-implement it here
  718. * so we can override the libusb_control_transfer call */
  719. static int
  720. 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. #endif /* HAVE_LIBUSB */
  785. #endif /* !SDL_HIDAPI_DISABLED */
  786. /* Shared HIDAPI Implementation */
  787. struct hidapi_backend
  788. {
  789. int (*hid_write)(void *device, const unsigned char *data, size_t length);
  790. int (*hid_read_timeout)(void *device, unsigned char *data, size_t length, int milliseconds);
  791. int (*hid_read)(void *device, unsigned char *data, size_t length);
  792. int (*hid_set_nonblocking)(void *device, int nonblock);
  793. int (*hid_send_feature_report)(void *device, const unsigned char *data, size_t length);
  794. int (*hid_get_feature_report)(void *device, unsigned char *data, size_t length);
  795. void (*hid_close)(void *device);
  796. int (*hid_get_manufacturer_string)(void *device, wchar_t *string, size_t maxlen);
  797. int (*hid_get_product_string)(void *device, wchar_t *string, size_t maxlen);
  798. int (*hid_get_serial_number_string)(void *device, wchar_t *string, size_t maxlen);
  799. int (*hid_get_indexed_string)(void *device, int string_index, wchar_t *string, size_t maxlen);
  800. const wchar_t *(*hid_error)(void *device);
  801. };
  802. #if HAVE_PLATFORM_BACKEND
  803. static const struct hidapi_backend PLATFORM_Backend = {
  804. (void *)PLATFORM_hid_write,
  805. (void *)PLATFORM_hid_read_timeout,
  806. (void *)PLATFORM_hid_read,
  807. (void *)PLATFORM_hid_set_nonblocking,
  808. (void *)PLATFORM_hid_send_feature_report,
  809. (void *)PLATFORM_hid_get_feature_report,
  810. (void *)PLATFORM_hid_close,
  811. (void *)PLATFORM_hid_get_manufacturer_string,
  812. (void *)PLATFORM_hid_get_product_string,
  813. (void *)PLATFORM_hid_get_serial_number_string,
  814. (void *)PLATFORM_hid_get_indexed_string,
  815. (void *)PLATFORM_hid_error
  816. };
  817. #endif /* HAVE_PLATFORM_BACKEND */
  818. #if HAVE_DRIVER_BACKEND
  819. static const struct hidapi_backend DRIVER_Backend = {
  820. (void *)DRIVER_hid_write,
  821. (void *)DRIVER_hid_read_timeout,
  822. (void *)DRIVER_hid_read,
  823. (void *)DRIVER_hid_set_nonblocking,
  824. (void *)DRIVER_hid_send_feature_report,
  825. (void *)DRIVER_hid_get_feature_report,
  826. (void *)DRIVER_hid_close,
  827. (void *)DRIVER_hid_get_manufacturer_string,
  828. (void *)DRIVER_hid_get_product_string,
  829. (void *)DRIVER_hid_get_serial_number_string,
  830. (void *)DRIVER_hid_get_indexed_string,
  831. (void *)DRIVER_hid_error
  832. };
  833. #endif /* HAVE_DRIVER_BACKEND */
  834. #ifdef HAVE_LIBUSB
  835. static const struct hidapi_backend LIBUSB_Backend = {
  836. (void *)LIBUSB_hid_write,
  837. (void *)LIBUSB_hid_read_timeout,
  838. (void *)LIBUSB_hid_read,
  839. (void *)LIBUSB_hid_set_nonblocking,
  840. (void *)LIBUSB_hid_send_feature_report,
  841. (void *)LIBUSB_hid_get_feature_report,
  842. (void *)LIBUSB_hid_close,
  843. (void *)LIBUSB_hid_get_manufacturer_string,
  844. (void *)LIBUSB_hid_get_product_string,
  845. (void *)LIBUSB_hid_get_serial_number_string,
  846. (void *)LIBUSB_hid_get_indexed_string,
  847. (void *)LIBUSB_hid_error
  848. };
  849. #endif /* HAVE_LIBUSB */
  850. struct SDL_hid_device_
  851. {
  852. const void *magic;
  853. void *device;
  854. const struct hidapi_backend *backend;
  855. };
  856. static char device_magic;
  857. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  858. static SDL_hid_device *
  859. CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
  860. {
  861. SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));
  862. wrapper->magic = &device_magic;
  863. wrapper->device = device;
  864. wrapper->backend = backend;
  865. return wrapper;
  866. }
  867. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  868. static void
  869. DeleteHIDDeviceWrapper(SDL_hid_device *device)
  870. {
  871. device->magic = NULL;
  872. SDL_free(device);
  873. }
  874. #define CHECK_DEVICE_MAGIC(device, retval) \
  875. if (!device || device->magic != &device_magic) { \
  876. SDL_SetError("Invalid device"); \
  877. return retval; \
  878. }
  879. #if !SDL_HIDAPI_DISABLED
  880. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  881. #define COPY_IF_EXISTS(var) \
  882. if (pSrc->var != NULL) { \
  883. pDst->var = SDL_strdup(pSrc->var); \
  884. } else { \
  885. pDst->var = NULL; \
  886. }
  887. #define WCOPY_IF_EXISTS(var) \
  888. if (pSrc->var != NULL) { \
  889. pDst->var = SDL_wcsdup(pSrc->var); \
  890. } else { \
  891. pDst->var = NULL; \
  892. }
  893. static void
  894. CopyHIDDeviceInfo(struct SDL_hid_device_info *pSrc, struct SDL_hid_device_info *pDst)
  895. {
  896. COPY_IF_EXISTS(path)
  897. pDst->vendor_id = pSrc->vendor_id;
  898. pDst->product_id = pSrc->product_id;
  899. WCOPY_IF_EXISTS(serial_number)
  900. pDst->release_number = pSrc->release_number;
  901. WCOPY_IF_EXISTS(manufacturer_string)
  902. WCOPY_IF_EXISTS(product_string)
  903. pDst->usage_page = pSrc->usage_page;
  904. pDst->usage = pSrc->usage;
  905. pDst->interface_number = pSrc->interface_number;
  906. pDst->interface_class = pSrc->interface_class;
  907. pDst->interface_subclass = pSrc->interface_subclass;
  908. pDst->interface_protocol = pSrc->interface_protocol;
  909. pDst->next = NULL;
  910. }
  911. #undef COPY_IF_EXISTS
  912. #undef WCOPY_IF_EXISTS
  913. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  914. #endif /* !SDL_HIDAPI_DISABLED */
  915. static int SDL_hidapi_refcount = 0;
  916. static void SDL_SetHIDAPIError(const wchar_t *error)
  917. {
  918. if (error) {
  919. char *error_utf8 = SDL_iconv_wchar_utf8(error);
  920. if (error_utf8) {
  921. SDL_SetError("%s", error_utf8);
  922. SDL_free(error_utf8);
  923. }
  924. }
  925. }
  926. int SDL_hid_init(void)
  927. {
  928. int attempts = 0, success = 0;
  929. if (SDL_hidapi_refcount > 0) {
  930. ++SDL_hidapi_refcount;
  931. return 0;
  932. }
  933. #if defined(SDL_USE_LIBUDEV)
  934. if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
  935. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  936. "udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
  937. linux_enumeration_method = ENUMERATION_FALLBACK;
  938. } else if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
  939. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  940. "Container detected, disabling HIDAPI udev integration");
  941. linux_enumeration_method = ENUMERATION_FALLBACK;
  942. } else {
  943. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  944. "Using udev for HIDAPI joystick device discovery");
  945. linux_enumeration_method = ENUMERATION_LIBUDEV;
  946. }
  947. #endif
  948. #ifdef HAVE_LIBUSB
  949. if (SDL_getenv("SDL_HIDAPI_DISABLE_LIBUSB") != NULL) {
  950. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  951. "libusb disabled by SDL_HIDAPI_DISABLE_LIBUSB");
  952. libusb_ctx.libhandle = NULL;
  953. } else {
  954. ++attempts;
  955. #ifdef SDL_LIBUSB_DYNAMIC
  956. libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
  957. #else
  958. libusb_ctx.libhandle = (void *)1;
  959. #endif
  960. if (libusb_ctx.libhandle != NULL) {
  961. SDL_bool loaded = SDL_TRUE;
  962. #ifdef SDL_LIBUSB_DYNAMIC
  963. #define LOAD_LIBUSB_SYMBOL(type, func) \
  964. if (!(libusb_ctx.func = (type)SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) { \
  965. loaded = SDL_FALSE; \
  966. }
  967. #else
  968. #define LOAD_LIBUSB_SYMBOL(func) \
  969. libusb_ctx.func = libusb_##func;
  970. #endif
  971. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context **), init)
  972. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_context *), exit)
  973. LOAD_LIBUSB_SYMBOL(ssize_t (LIBUSB_CALL *)(libusb_context *, libusb_device ***), get_device_list)
  974. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device **, int), free_device_list)
  975. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_device_descriptor *), get_device_descriptor)
  976. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, struct libusb_config_descriptor **), get_active_config_descriptor)
  977. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, uint8_t, struct libusb_config_descriptor **), get_config_descriptor)
  978. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_config_descriptor *), free_config_descriptor)
  979. LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_bus_number)
  980. LOAD_LIBUSB_SYMBOL(uint8_t (LIBUSB_CALL *)(libusb_device *), get_device_address)
  981. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device *, libusb_device_handle **), open)
  982. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(libusb_device_handle *), close)
  983. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), claim_interface)
  984. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), release_interface)
  985. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), kernel_driver_active)
  986. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), detach_kernel_driver)
  987. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int), attach_kernel_driver)
  988. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, int, int), set_interface_alt_setting)
  989. LOAD_LIBUSB_SYMBOL(struct libusb_transfer * (LIBUSB_CALL *)(int), alloc_transfer)
  990. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), submit_transfer)
  991. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(struct libusb_transfer *), cancel_transfer)
  992. LOAD_LIBUSB_SYMBOL(void (LIBUSB_CALL *)(struct libusb_transfer *), free_transfer)
  993. 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)
  994. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_device_handle *, unsigned char, unsigned char *, int, int *, unsigned int), interrupt_transfer)
  995. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *), handle_events)
  996. LOAD_LIBUSB_SYMBOL(int (LIBUSB_CALL *)(libusb_context *, int *), handle_events_completed)
  997. LOAD_LIBUSB_SYMBOL(const char * (LIBUSB_CALL *)(int), error_name)
  998. #undef LOAD_LIBUSB_SYMBOL
  999. if (!loaded) {
  1000. #ifdef SDL_LIBUSB_DYNAMIC
  1001. SDL_UnloadObject(libusb_ctx.libhandle);
  1002. #endif
  1003. libusb_ctx.libhandle = NULL;
  1004. /* SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function"); */
  1005. } else if (LIBUSB_hid_init() < 0) {
  1006. #ifdef SDL_LIBUSB_DYNAMIC
  1007. SDL_UnloadObject(libusb_ctx.libhandle);
  1008. #endif
  1009. libusb_ctx.libhandle = NULL;
  1010. } else {
  1011. ++success;
  1012. }
  1013. }
  1014. }
  1015. #endif /* HAVE_LIBUSB */
  1016. #if HAVE_PLATFORM_BACKEND
  1017. ++attempts;
  1018. #if __LINUX__
  1019. udev_ctx = SDL_UDEV_GetUdevSyms();
  1020. #endif /* __LINUX __ */
  1021. if (udev_ctx && PLATFORM_hid_init() == 0) {
  1022. ++success;
  1023. }
  1024. #endif /* HAVE_PLATFORM_BACKEND */
  1025. if (attempts > 0 && success == 0) {
  1026. return -1;
  1027. }
  1028. ++SDL_hidapi_refcount;
  1029. return 0;
  1030. }
  1031. int SDL_hid_exit(void)
  1032. {
  1033. int result = 0;
  1034. if (SDL_hidapi_refcount == 0) {
  1035. return 0;
  1036. }
  1037. --SDL_hidapi_refcount;
  1038. if (SDL_hidapi_refcount > 0) {
  1039. return 0;
  1040. }
  1041. SDL_hidapi_refcount = 0;
  1042. #if !SDL_HIDAPI_DISABLED
  1043. HIDAPI_ShutdownDiscovery();
  1044. #endif
  1045. #if HAVE_PLATFORM_BACKEND
  1046. if (udev_ctx) {
  1047. result |= PLATFORM_hid_exit();
  1048. }
  1049. #if __LINUX__
  1050. SDL_UDEV_ReleaseUdevSyms();
  1051. #endif /* __LINUX __ */
  1052. #endif /* HAVE_PLATFORM_BACKEND */
  1053. #ifdef HAVE_LIBUSB
  1054. if (libusb_ctx.libhandle) {
  1055. result |= LIBUSB_hid_exit();
  1056. #ifdef SDL_LIBUSB_DYNAMIC
  1057. SDL_UnloadObject(libusb_ctx.libhandle);
  1058. #endif
  1059. libusb_ctx.libhandle = NULL;
  1060. }
  1061. #endif /* HAVE_LIBUSB */
  1062. return result;
  1063. }
  1064. Uint32 SDL_hid_device_change_count(void)
  1065. {
  1066. Uint32 counter = 0;
  1067. #if !SDL_HIDAPI_DISABLED
  1068. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1069. return 0;
  1070. }
  1071. HIDAPI_UpdateDiscovery();
  1072. if (SDL_HIDAPI_discovery.m_unDeviceChangeCounter == 0) {
  1073. /* Counter wrapped! */
  1074. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1075. }
  1076. counter = SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1077. #endif /* !SDL_HIDAPI_DISABLED */
  1078. return counter;
  1079. }
  1080. struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)
  1081. {
  1082. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1083. #ifdef HAVE_LIBUSB
  1084. struct SDL_hid_device_info *usb_devs = NULL;
  1085. struct SDL_hid_device_info *usb_dev;
  1086. #endif
  1087. #if HAVE_DRIVER_BACKEND
  1088. struct SDL_hid_device_info *driver_devs = NULL;
  1089. struct SDL_hid_device_info *driver_dev;
  1090. #endif
  1091. #if HAVE_PLATFORM_BACKEND
  1092. struct SDL_hid_device_info *raw_devs = NULL;
  1093. struct SDL_hid_device_info *raw_dev;
  1094. #endif
  1095. struct SDL_hid_device_info *devs = NULL, *last = NULL, *new_dev;
  1096. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1097. return NULL;
  1098. }
  1099. #ifdef HAVE_LIBUSB
  1100. if (libusb_ctx.libhandle) {
  1101. usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
  1102. #ifdef DEBUG_HIDAPI
  1103. SDL_Log("libusb devices found:");
  1104. #endif
  1105. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  1106. new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
  1107. if (new_dev == NULL) {
  1108. LIBUSB_hid_free_enumeration(usb_devs);
  1109. SDL_hid_free_enumeration(devs);
  1110. SDL_OutOfMemory();
  1111. return NULL;
  1112. }
  1113. CopyHIDDeviceInfo(usb_dev, new_dev);
  1114. #ifdef DEBUG_HIDAPI
  1115. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  1116. usb_dev->manufacturer_string, usb_dev->product_string,
  1117. usb_dev->vendor_id, usb_dev->product_id);
  1118. #endif
  1119. if (last != NULL) {
  1120. last->next = new_dev;
  1121. } else {
  1122. devs = new_dev;
  1123. }
  1124. last = new_dev;
  1125. }
  1126. }
  1127. #endif /* HAVE_LIBUSB */
  1128. #ifdef HAVE_DRIVER_BACKEND
  1129. driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);
  1130. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  1131. new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
  1132. CopyHIDDeviceInfo(driver_dev, new_dev);
  1133. if (last != NULL) {
  1134. last->next = new_dev;
  1135. } else {
  1136. devs = new_dev;
  1137. }
  1138. last = new_dev;
  1139. }
  1140. #endif /* HAVE_DRIVER_BACKEND */
  1141. #if HAVE_PLATFORM_BACKEND
  1142. if (udev_ctx) {
  1143. raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
  1144. #ifdef DEBUG_HIDAPI
  1145. SDL_Log("hidraw devices found:");
  1146. #endif
  1147. for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
  1148. SDL_bool bFound = SDL_FALSE;
  1149. #ifdef DEBUG_HIDAPI
  1150. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  1151. raw_dev->manufacturer_string, raw_dev->product_string,
  1152. raw_dev->vendor_id, raw_dev->product_id);
  1153. #endif
  1154. #ifdef HAVE_LIBUSB
  1155. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  1156. if (raw_dev->vendor_id == usb_dev->vendor_id &&
  1157. raw_dev->product_id == usb_dev->product_id &&
  1158. (raw_dev->interface_number < 0 || raw_dev->interface_number == usb_dev->interface_number)) {
  1159. bFound = SDL_TRUE;
  1160. break;
  1161. }
  1162. }
  1163. #endif
  1164. #ifdef HAVE_DRIVER_BACKEND
  1165. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  1166. if (raw_dev->vendor_id == driver_dev->vendor_id &&
  1167. raw_dev->product_id == driver_dev->product_id &&
  1168. (raw_dev->interface_number < 0 || raw_dev->interface_number == driver_dev->interface_number)) {
  1169. bFound = SDL_TRUE;
  1170. break;
  1171. }
  1172. }
  1173. #endif
  1174. if (!bFound) {
  1175. new_dev = (struct SDL_hid_device_info *)SDL_malloc(sizeof(struct SDL_hid_device_info));
  1176. if (new_dev == NULL) {
  1177. #ifdef HAVE_LIBUSB
  1178. if (libusb_ctx.libhandle) {
  1179. LIBUSB_hid_free_enumeration(usb_devs);
  1180. }
  1181. #endif
  1182. PLATFORM_hid_free_enumeration(raw_devs);
  1183. SDL_hid_free_enumeration(devs);
  1184. SDL_OutOfMemory();
  1185. return NULL;
  1186. }
  1187. CopyHIDDeviceInfo(raw_dev, new_dev);
  1188. new_dev->next = NULL;
  1189. if (last != NULL) {
  1190. last->next = new_dev;
  1191. } else {
  1192. devs = new_dev;
  1193. }
  1194. last = new_dev;
  1195. }
  1196. }
  1197. PLATFORM_hid_free_enumeration(raw_devs);
  1198. }
  1199. #endif /* HAVE_PLATFORM_BACKEND */
  1200. #ifdef HAVE_LIBUSB
  1201. if (libusb_ctx.libhandle) {
  1202. LIBUSB_hid_free_enumeration(usb_devs);
  1203. }
  1204. #endif
  1205. return devs;
  1206. #else
  1207. return NULL;
  1208. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1209. }
  1210. void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
  1211. {
  1212. while (devs) {
  1213. struct SDL_hid_device_info *next = devs->next;
  1214. SDL_free(devs->path);
  1215. SDL_free(devs->serial_number);
  1216. SDL_free(devs->manufacturer_string);
  1217. SDL_free(devs->product_string);
  1218. SDL_free(devs);
  1219. devs = next;
  1220. }
  1221. }
  1222. SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
  1223. {
  1224. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1225. void *pDevice = NULL;
  1226. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1227. return NULL;
  1228. }
  1229. #if HAVE_PLATFORM_BACKEND
  1230. if (udev_ctx) {
  1231. pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number);
  1232. if (pDevice != NULL) {
  1233. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1234. }
  1235. }
  1236. #endif /* HAVE_PLATFORM_BACKEND */
  1237. #if HAVE_DRIVER_BACKEND
  1238. pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number);
  1239. if (pDevice != NULL) {
  1240. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1241. }
  1242. #endif /* HAVE_DRIVER_BACKEND */
  1243. #ifdef HAVE_LIBUSB
  1244. if (libusb_ctx.libhandle != NULL) {
  1245. pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);
  1246. if (pDevice != NULL) {
  1247. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1248. }
  1249. }
  1250. #endif /* HAVE_LIBUSB */
  1251. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1252. return NULL;
  1253. }
  1254. SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */)
  1255. {
  1256. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1257. void *pDevice = NULL;
  1258. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1259. return NULL;
  1260. }
  1261. #if HAVE_PLATFORM_BACKEND
  1262. if (udev_ctx) {
  1263. pDevice = PLATFORM_hid_open_path(path, bExclusive);
  1264. if (pDevice != NULL) {
  1265. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1266. }
  1267. }
  1268. #endif /* HAVE_PLATFORM_BACKEND */
  1269. #if HAVE_DRIVER_BACKEND
  1270. pDevice = DRIVER_hid_open_path(path, bExclusive);
  1271. if (pDevice != NULL) {
  1272. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1273. }
  1274. #endif /* HAVE_DRIVER_BACKEND */
  1275. #ifdef HAVE_LIBUSB
  1276. if (libusb_ctx.libhandle != NULL) {
  1277. pDevice = LIBUSB_hid_open_path(path, bExclusive);
  1278. if (pDevice != NULL) {
  1279. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1280. }
  1281. }
  1282. #endif /* HAVE_LIBUSB */
  1283. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1284. return NULL;
  1285. }
  1286. int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
  1287. {
  1288. int result;
  1289. CHECK_DEVICE_MAGIC(device, -1);
  1290. result = device->backend->hid_write(device->device, data, length);
  1291. if (result < 0) {
  1292. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1293. }
  1294. return result;
  1295. }
  1296. int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds)
  1297. {
  1298. int result;
  1299. CHECK_DEVICE_MAGIC(device, -1);
  1300. result = device->backend->hid_read_timeout(device->device, data, length, milliseconds);
  1301. if (result < 0) {
  1302. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1303. }
  1304. return result;
  1305. }
  1306. int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length)
  1307. {
  1308. int result;
  1309. CHECK_DEVICE_MAGIC(device, -1);
  1310. result = device->backend->hid_read(device->device, data, length);
  1311. if (result < 0) {
  1312. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1313. }
  1314. return result;
  1315. }
  1316. int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock)
  1317. {
  1318. int result;
  1319. CHECK_DEVICE_MAGIC(device, -1);
  1320. result = device->backend->hid_set_nonblocking(device->device, nonblock);
  1321. if (result < 0) {
  1322. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1323. }
  1324. return result;
  1325. }
  1326. int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length)
  1327. {
  1328. int result;
  1329. CHECK_DEVICE_MAGIC(device, -1);
  1330. result = device->backend->hid_send_feature_report(device->device, data, length);
  1331. if (result < 0) {
  1332. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1333. }
  1334. return result;
  1335. }
  1336. int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length)
  1337. {
  1338. int result;
  1339. CHECK_DEVICE_MAGIC(device, -1);
  1340. result = device->backend->hid_get_feature_report(device->device, data, length);
  1341. if (result < 0) {
  1342. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1343. }
  1344. return result;
  1345. }
  1346. void SDL_hid_close(SDL_hid_device *device)
  1347. {
  1348. CHECK_DEVICE_MAGIC(device, );
  1349. device->backend->hid_close(device->device);
  1350. DeleteHIDDeviceWrapper(device);
  1351. }
  1352. int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1353. {
  1354. int result;
  1355. CHECK_DEVICE_MAGIC(device, -1);
  1356. result = device->backend->hid_get_manufacturer_string(device->device, string, maxlen);
  1357. if (result < 0) {
  1358. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1359. }
  1360. return result;
  1361. }
  1362. int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1363. {
  1364. int result;
  1365. CHECK_DEVICE_MAGIC(device, -1);
  1366. result = device->backend->hid_get_product_string(device->device, string, maxlen);
  1367. if (result < 0) {
  1368. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1369. }
  1370. return result;
  1371. }
  1372. int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1373. {
  1374. int result;
  1375. CHECK_DEVICE_MAGIC(device, -1);
  1376. result = device->backend->hid_get_serial_number_string(device->device, string, maxlen);
  1377. if (result < 0) {
  1378. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1379. }
  1380. return result;
  1381. }
  1382. int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen)
  1383. {
  1384. int result;
  1385. CHECK_DEVICE_MAGIC(device, -1);
  1386. result = device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen);
  1387. if (result < 0) {
  1388. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1389. }
  1390. return result;
  1391. }
  1392. void SDL_hid_ble_scan(SDL_bool active)
  1393. {
  1394. #if !SDL_HIDAPI_DISABLED && (__IOS__ || __TVOS__)
  1395. hid_ble_scan(active);
  1396. #endif
  1397. }
  1398. #ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
  1399. /* This is needed to enable input for Nyko and EVORETRO GameCube adaptors */
  1400. void SDL_EnableGameCubeAdaptors(void)
  1401. {
  1402. #ifdef HAVE_LIBUSB
  1403. libusb_context *context = NULL;
  1404. libusb_device **devs = NULL;
  1405. libusb_device_handle *handle = NULL;
  1406. struct libusb_device_descriptor desc;
  1407. ssize_t i, num_devs;
  1408. int kernel_detached = 0;
  1409. if (libusb_ctx.libhandle == NULL) {
  1410. return;
  1411. }
  1412. if (libusb_ctx.init(&context) == 0) {
  1413. num_devs = libusb_ctx.get_device_list(context, &devs);
  1414. for (i = 0; i < num_devs; ++i) {
  1415. if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) {
  1416. continue;
  1417. }
  1418. if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
  1419. continue;
  1420. }
  1421. if (libusb_ctx.open(devs[i], &handle) != 0) {
  1422. continue;
  1423. }
  1424. if (libusb_ctx.kernel_driver_active(handle, 0)) {
  1425. if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) {
  1426. kernel_detached = 1;
  1427. }
  1428. }
  1429. if (libusb_ctx.claim_interface(handle, 0) == 0) {
  1430. libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
  1431. libusb_ctx.release_interface(handle, 0);
  1432. }
  1433. if (kernel_detached) {
  1434. libusb_ctx.attach_kernel_driver(handle, 0);
  1435. }
  1436. libusb_ctx.close(handle);
  1437. }
  1438. libusb_ctx.free_device_list(devs, 1);
  1439. libusb_ctx.exit(context);
  1440. }
  1441. #endif /* HAVE_LIBUSB */
  1442. }
  1443. #endif /* HAVE_ENABLE_GAMECUBE_ADAPTORS */