SDL_hidapi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_loadso.h"
  28. #include "SDL_hidapi.h"
  29. #include "SDL_thread.h"
  30. #include "SDL_timer.h"
  31. #include "SDL_hidapi_c.h"
  32. #if !SDL_HIDAPI_DISABLED
  33. #if defined(__WIN32__) || defined(__WINGDK__)
  34. #include "../core/windows/SDL_windows.h"
  35. #endif
  36. #if defined(__MACOS__)
  37. #include <CoreFoundation/CoreFoundation.h>
  38. #include <mach/mach.h>
  39. #include <IOKit/IOKitLib.h>
  40. #include <IOKit/hid/IOHIDDevice.h>
  41. #include <IOKit/usb/USBSpec.h>
  42. #include <AvailabilityMacros.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 <poll.h>
  51. #include <unistd.h>
  52. #include "../core/linux/SDL_sandbox.h"
  53. #endif
  54. #ifdef HAVE_INOTIFY
  55. #include <unistd.h> /* just in case we didn't use that SDL_USE_LIBUDEV block... */
  56. #include <string.h> /* strerror */
  57. #include <errno.h> /* errno */
  58. #include <fcntl.h>
  59. #include <limits.h> /* For the definition of NAME_MAX */
  60. #include <sys/inotify.h>
  61. #endif
  62. #if defined(SDL_USE_LIBUDEV)
  63. typedef enum
  64. {
  65. ENUMERATION_UNSET,
  66. ENUMERATION_LIBUDEV,
  67. ENUMERATION_FALLBACK
  68. } LinuxEnumerationMethod;
  69. static LinuxEnumerationMethod linux_enumeration_method = ENUMERATION_UNSET;
  70. #endif
  71. #if defined(HAVE_INOTIFY)
  72. static int inotify_fd = -1;
  73. #endif
  74. #if defined(SDL_USE_LIBUDEV)
  75. static const SDL_UDEV_Symbols * usyms = NULL;
  76. #endif
  77. static struct
  78. {
  79. SDL_bool m_bInitialized;
  80. Uint32 m_unDeviceChangeCounter;
  81. SDL_bool m_bCanGetNotifications;
  82. Uint32 m_unLastDetect;
  83. #if defined(__WIN32__) || defined(__WINGDK__)
  84. SDL_threadID m_nThreadID;
  85. WNDCLASSEXA m_wndClass;
  86. HWND m_hwndMsg;
  87. HDEVNOTIFY m_hNotify;
  88. double m_flLastWin32MessageCheck;
  89. #endif
  90. #if defined(__MACOS__)
  91. IONotificationPortRef m_notificationPort;
  92. mach_port_t m_notificationMach;
  93. #endif
  94. #if defined(SDL_USE_LIBUDEV)
  95. struct udev *m_pUdev;
  96. struct udev_monitor *m_pUdevMonitor;
  97. int m_nUdevFd;
  98. #endif
  99. } SDL_HIDAPI_discovery;
  100. #if defined(__WIN32__) || defined(__WINGDK__)
  101. struct _DEV_BROADCAST_HDR
  102. {
  103. DWORD dbch_size;
  104. DWORD dbch_devicetype;
  105. DWORD dbch_reserved;
  106. };
  107. typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
  108. {
  109. DWORD dbcc_size;
  110. DWORD dbcc_devicetype;
  111. DWORD dbcc_reserved;
  112. GUID dbcc_classguid;
  113. char dbcc_name[ 1 ];
  114. } DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;
  115. typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
  116. #define DBT_DEVICEARRIVAL 0x8000 /* system detected a new device */
  117. #define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
  118. #define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
  119. #define DBT_DEVNODES_CHANGED 0x0007
  120. #define DBT_CONFIGCHANGED 0x0018
  121. #define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
  122. #define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
  123. #include <initguid.h>
  124. DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
  125. static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  126. {
  127. switch (message) {
  128. case WM_DEVICECHANGE:
  129. switch (wParam) {
  130. case DBT_DEVICEARRIVAL:
  131. case DBT_DEVICEREMOVECOMPLETE:
  132. if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  133. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  134. }
  135. break;
  136. }
  137. return TRUE;
  138. }
  139. return DefWindowProc(hwnd, message, wParam, lParam);
  140. }
  141. #endif /* defined(__WIN32__) || defined(__WINGDK__) */
  142. #if defined(__MACOS__)
  143. static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
  144. {
  145. /* Must drain the iterator, or we won't receive new notifications */
  146. io_object_t entry;
  147. while ((entry = IOIteratorNext(portIterator)) != 0) {
  148. IOObjectRelease(entry);
  149. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  150. }
  151. }
  152. #endif /* __MACOS__ */
  153. #ifdef HAVE_INOTIFY
  154. #ifdef HAVE_INOTIFY_INIT1
  155. static int SDL_inotify_init1(void) {
  156. return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  157. }
  158. #else
  159. static int SDL_inotify_init1(void) {
  160. int fd = inotify_init();
  161. if (fd < 0) return -1;
  162. fcntl(fd, F_SETFL, O_NONBLOCK);
  163. fcntl(fd, F_SETFD, FD_CLOEXEC);
  164. return fd;
  165. }
  166. #endif
  167. static int
  168. StrHasPrefix(const char *string, const char *prefix)
  169. {
  170. return (SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0);
  171. }
  172. static int
  173. StrIsInteger(const char *string)
  174. {
  175. const char *p;
  176. if (*string == '\0') {
  177. return 0;
  178. }
  179. for (p = string; *p != '\0'; p++) {
  180. if (*p < '0' || *p > '9') {
  181. return 0;
  182. }
  183. }
  184. return 1;
  185. }
  186. #endif /* HAVE_INOTIFY */
  187. static void
  188. HIDAPI_InitializeDiscovery()
  189. {
  190. SDL_HIDAPI_discovery.m_bInitialized = SDL_TRUE;
  191. SDL_HIDAPI_discovery.m_unDeviceChangeCounter = 1;
  192. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
  193. SDL_HIDAPI_discovery.m_unLastDetect = 0;
  194. #if defined(__WIN32__) || defined(__WINGDK__)
  195. SDL_HIDAPI_discovery.m_nThreadID = SDL_ThreadID();
  196. SDL_zero(SDL_HIDAPI_discovery.m_wndClass);
  197. SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
  198. SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
  199. SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; /* This function is called by windows */
  200. SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);
  201. RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
  202. SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  203. {
  204. DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;
  205. SDL_zero(devBroadcast);
  206. devBroadcast.dbcc_size = sizeof( devBroadcast );
  207. devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  208. devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
  209. /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
  210. * but that seems to be necessary to get a notice after each individual usb input device actually
  211. * installs, rather than just as the composite device is seen.
  212. */
  213. SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification( SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
  214. SDL_HIDAPI_discovery.m_bCanGetNotifications = ( SDL_HIDAPI_discovery.m_hNotify != 0 );
  215. }
  216. #endif /* defined(__WIN32__) || defined(__WINGDK__) */
  217. #if defined(__MACOS__)
  218. SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMainPortDefault);
  219. if (SDL_HIDAPI_discovery.m_notificationPort) {
  220. {
  221. io_iterator_t portIterator = 0;
  222. io_object_t entry;
  223. IOReturn result = IOServiceAddMatchingNotification(
  224. SDL_HIDAPI_discovery.m_notificationPort,
  225. kIOFirstMatchNotification,
  226. IOServiceMatching(kIOHIDDeviceKey),
  227. CallbackIOServiceFunc, NULL, &portIterator);
  228. if (result == 0) {
  229. /* Must drain the existing iterator, or we won't receive new notifications */
  230. while ((entry = IOIteratorNext(portIterator)) != 0) {
  231. IOObjectRelease(entry);
  232. }
  233. } else {
  234. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  235. SDL_HIDAPI_discovery.m_notificationPort = nil;
  236. }
  237. }
  238. {
  239. io_iterator_t portIterator = 0;
  240. io_object_t entry;
  241. IOReturn result = IOServiceAddMatchingNotification(
  242. SDL_HIDAPI_discovery.m_notificationPort,
  243. kIOTerminatedNotification,
  244. IOServiceMatching(kIOHIDDeviceKey),
  245. CallbackIOServiceFunc, NULL, &portIterator);
  246. if (result == 0) {
  247. /* Must drain the existing iterator, or we won't receive new notifications */
  248. while ((entry = IOIteratorNext(portIterator)) != 0) {
  249. IOObjectRelease(entry);
  250. }
  251. } else {
  252. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  253. SDL_HIDAPI_discovery.m_notificationPort = nil;
  254. }
  255. }
  256. }
  257. SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
  258. if (SDL_HIDAPI_discovery.m_notificationPort) {
  259. SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
  260. }
  261. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);
  262. #endif /* __MACOS__ */
  263. #if defined(SDL_USE_LIBUDEV)
  264. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  265. SDL_HIDAPI_discovery.m_pUdev = NULL;
  266. SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
  267. SDL_HIDAPI_discovery.m_nUdevFd = -1;
  268. usyms = SDL_UDEV_GetUdevSyms();
  269. if (usyms) {
  270. SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
  271. }
  272. if (SDL_HIDAPI_discovery.m_pUdev) {
  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) {
  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. 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(__MACOS__)
  340. if (SDL_HIDAPI_discovery.m_notificationPort) {
  341. struct { mach_msg_header_t hdr; char payload[ 4096 ]; } msg;
  342. 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) {
  343. IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);
  344. }
  345. }
  346. #endif
  347. #if defined(SDL_USE_LIBUDEV)
  348. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  349. if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
  350. /* Drain all notification events.
  351. * We don't expect a lot of device notifications so just
  352. * do a new discovery on any kind or number of notifications.
  353. * This could be made more restrictive if necessary.
  354. */
  355. for (;;) {
  356. struct pollfd PollUdev;
  357. struct udev_device *pUdevDevice;
  358. PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
  359. PollUdev.events = POLLIN;
  360. if (poll(&PollUdev, 1, 0) != 1) {
  361. break;
  362. }
  363. pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
  364. if (pUdevDevice) {
  365. const char *action = NULL;
  366. action = usyms->udev_device_get_action(pUdevDevice);
  367. if (!action || SDL_strcmp(action, "add") == 0 || SDL_strcmp(action, "remove") == 0) {
  368. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  369. }
  370. usyms->udev_device_unref(pUdevDevice);
  371. }
  372. }
  373. }
  374. }
  375. else
  376. #endif /* SDL_USE_LIBUDEV */
  377. {
  378. #if defined(HAVE_INOTIFY)
  379. if (inotify_fd >= 0) {
  380. union
  381. {
  382. struct inotify_event event;
  383. char storage[4096];
  384. char enough_for_inotify[sizeof (struct inotify_event) + NAME_MAX + 1];
  385. } buf;
  386. ssize_t bytes;
  387. size_t remain = 0;
  388. size_t len;
  389. bytes = read(inotify_fd, &buf, sizeof (buf));
  390. if (bytes > 0) {
  391. remain = (size_t) bytes;
  392. }
  393. while (remain > 0) {
  394. if (buf.event.len > 0) {
  395. if (StrHasPrefix(buf.event.name, "hidraw") &&
  396. StrIsInteger(buf.event.name + SDL_strlen ("hidraw"))) {
  397. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  398. /* We found an hidraw change. We still continue to
  399. * drain the inotify fd to avoid leaving old
  400. * notifications in the queue. */
  401. }
  402. }
  403. len = sizeof (struct inotify_event) + buf.event.len;
  404. remain -= len;
  405. if (remain != 0) {
  406. SDL_memmove(&buf.storage[0], &buf.storage[len], remain);
  407. }
  408. }
  409. }
  410. #endif /* HAVE_INOTIFY */
  411. }
  412. }
  413. static void
  414. HIDAPI_ShutdownDiscovery()
  415. {
  416. if (!SDL_HIDAPI_discovery.m_bInitialized) {
  417. return;
  418. }
  419. #if defined(__WIN32__) || defined(__WINGDK__)
  420. if (SDL_HIDAPI_discovery.m_hNotify)
  421. UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
  422. if (SDL_HIDAPI_discovery.m_hwndMsg) {
  423. DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
  424. }
  425. UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
  426. #endif
  427. #if defined(__MACOS__)
  428. if (SDL_HIDAPI_discovery.m_notificationPort) {
  429. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  430. }
  431. #endif
  432. #if defined(SDL_USE_LIBUDEV)
  433. if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
  434. if (usyms) {
  435. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  436. usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
  437. }
  438. if (SDL_HIDAPI_discovery.m_pUdev) {
  439. usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
  440. }
  441. SDL_UDEV_ReleaseUdevSyms();
  442. usyms = NULL;
  443. }
  444. }
  445. else
  446. #endif /* SDL_USE_LIBUDEV */
  447. {
  448. #if defined(HAVE_INOTIFY)
  449. if (inotify_fd >= 0) {
  450. close(inotify_fd);
  451. inotify_fd = -1;
  452. }
  453. #endif
  454. }
  455. SDL_HIDAPI_discovery.m_bInitialized = SDL_FALSE;
  456. }
  457. /* Platform HIDAPI Implementation */
  458. #define hid_device PLATFORM_hid_device
  459. #define hid_device_ PLATFORM_hid_device_
  460. #define hid_init PLATFORM_hid_init
  461. #define hid_exit PLATFORM_hid_exit
  462. #define hid_enumerate PLATFORM_hid_enumerate
  463. #define hid_free_enumeration PLATFORM_hid_free_enumeration
  464. #define hid_open PLATFORM_hid_open
  465. #define hid_open_path PLATFORM_hid_open_path
  466. #define hid_write PLATFORM_hid_write
  467. #define hid_read_timeout PLATFORM_hid_read_timeout
  468. #define hid_read PLATFORM_hid_read
  469. #define hid_set_nonblocking PLATFORM_hid_set_nonblocking
  470. #define hid_send_feature_report PLATFORM_hid_send_feature_report
  471. #define hid_get_feature_report PLATFORM_hid_get_feature_report
  472. #define hid_close PLATFORM_hid_close
  473. #define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string
  474. #define hid_get_product_string PLATFORM_hid_get_product_string
  475. #define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
  476. #define hid_get_indexed_string PLATFORM_hid_get_indexed_string
  477. #define hid_error PLATFORM_hid_error
  478. #define new_hid_device PLATFORM_new_hid_device
  479. #define free_hid_device PLATFORM_free_hid_device
  480. #define input_report PLATFORM_input_report
  481. #define return_data PLATFORM_return_data
  482. #define make_path PLATFORM_make_path
  483. #define read_thread PLATFORM_read_thread
  484. #undef HIDAPI_H__
  485. #if __LINUX__
  486. #if SDL_USE_LIBUDEV
  487. static const SDL_UDEV_Symbols *udev_ctx = NULL;
  488. #define udev_device_get_sysattr_value udev_ctx->udev_device_get_sysattr_value
  489. #define udev_new udev_ctx->udev_new
  490. #define udev_unref udev_ctx->udev_unref
  491. #define udev_device_new_from_devnum udev_ctx->udev_device_new_from_devnum
  492. #define udev_device_get_parent_with_subsystem_devtype udev_ctx->udev_device_get_parent_with_subsystem_devtype
  493. #define udev_device_unref udev_ctx->udev_device_unref
  494. #define udev_enumerate_new udev_ctx->udev_enumerate_new
  495. #define udev_enumerate_add_match_subsystem udev_ctx->udev_enumerate_add_match_subsystem
  496. #define udev_enumerate_scan_devices udev_ctx->udev_enumerate_scan_devices
  497. #define udev_enumerate_get_list_entry udev_ctx->udev_enumerate_get_list_entry
  498. #define udev_list_entry_get_name udev_ctx->udev_list_entry_get_name
  499. #define udev_device_new_from_syspath udev_ctx->udev_device_new_from_syspath
  500. #define udev_device_get_devnode udev_ctx->udev_device_get_devnode
  501. #define udev_list_entry_get_next udev_ctx->udev_list_entry_get_next
  502. #define udev_enumerate_unref udev_ctx->udev_enumerate_unref
  503. #include "linux/hid.c"
  504. #define HAVE_PLATFORM_BACKEND 1
  505. #endif /* SDL_USE_LIBUDEV */
  506. #elif __MACOS__
  507. #include "mac/hid.c"
  508. #define HAVE_PLATFORM_BACKEND 1
  509. #define udev_ctx 1
  510. #elif __WINDOWS__ || __WINGDK__
  511. #include "windows/hid.c"
  512. #define HAVE_PLATFORM_BACKEND 1
  513. #define udev_ctx 1
  514. #elif __ANDROID__
  515. /* The implementation for Android is in a separate .cpp file */
  516. #include "hidapi/hidapi.h"
  517. #define HAVE_PLATFORM_BACKEND 1
  518. #define udev_ctx 1
  519. #elif __IOS__ || __TVOS__
  520. /* The implementation for iOS and tvOS is in a separate .m file */
  521. #include "hidapi/hidapi.h"
  522. #define HAVE_PLATFORM_BACKEND 1
  523. #define udev_ctx 1
  524. #endif
  525. #undef hid_device
  526. #undef hid_device_
  527. #undef hid_init
  528. #undef hid_exit
  529. #undef hid_enumerate
  530. #undef hid_free_enumeration
  531. #undef hid_open
  532. #undef hid_open_path
  533. #undef hid_write
  534. #undef hid_read_timeout
  535. #undef hid_read
  536. #undef hid_set_nonblocking
  537. #undef hid_send_feature_report
  538. #undef hid_get_feature_report
  539. #undef hid_close
  540. #undef hid_get_manufacturer_string
  541. #undef hid_get_product_string
  542. #undef hid_get_serial_number_string
  543. #undef hid_get_indexed_string
  544. #undef hid_error
  545. #undef new_hid_device
  546. #undef free_hid_device
  547. #undef input_report
  548. #undef return_data
  549. #undef make_path
  550. #undef read_thread
  551. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  552. #define HAVE_DRIVER_BACKEND 1
  553. #endif
  554. #ifdef HAVE_DRIVER_BACKEND
  555. /* DRIVER HIDAPI Implementation */
  556. #define hid_device DRIVER_hid_device
  557. #define hid_device_ DRIVER_hid_device_
  558. #define hid_init DRIVER_hid_init
  559. #define hid_exit DRIVER_hid_exit
  560. #define hid_enumerate DRIVER_hid_enumerate
  561. #define hid_free_enumeration DRIVER_hid_free_enumeration
  562. #define hid_open DRIVER_hid_open
  563. #define hid_open_path DRIVER_hid_open_path
  564. #define hid_write DRIVER_hid_write
  565. #define hid_read_timeout DRIVER_hid_read_timeout
  566. #define hid_read DRIVER_hid_read
  567. #define hid_set_nonblocking DRIVER_hid_set_nonblocking
  568. #define hid_send_feature_report DRIVER_hid_send_feature_report
  569. #define hid_get_feature_report DRIVER_hid_get_feature_report
  570. #define hid_close DRIVER_hid_close
  571. #define hid_get_manufacturer_string DRIVER_hid_get_manufacturer_string
  572. #define hid_get_product_string DRIVER_hid_get_product_string
  573. #define hid_get_serial_number_string DRIVER_hid_get_serial_number_string
  574. #define hid_get_indexed_string DRIVER_hid_get_indexed_string
  575. #define hid_error DRIVER_hid_error
  576. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  577. #undef HIDAPI_H__
  578. #include "steamxbox/hid.c"
  579. #else
  580. #error Need a driver hid.c for this platform!
  581. #endif
  582. #undef hid_device
  583. #undef hid_device_
  584. #undef hid_init
  585. #undef hid_exit
  586. #undef hid_enumerate
  587. #undef hid_free_enumeration
  588. #undef hid_open
  589. #undef hid_open_path
  590. #undef hid_write
  591. #undef hid_read_timeout
  592. #undef hid_read
  593. #undef hid_set_nonblocking
  594. #undef hid_send_feature_report
  595. #undef hid_get_feature_report
  596. #undef hid_close
  597. #undef hid_get_manufacturer_string
  598. #undef hid_get_product_string
  599. #undef hid_get_serial_number_string
  600. #undef hid_get_indexed_string
  601. #undef hid_error
  602. #endif /* HAVE_DRIVER_BACKEND */
  603. #ifdef HAVE_LIBUSB
  604. /* libusb HIDAPI Implementation */
  605. /* Include this now, for our dynamically-loaded libusb context */
  606. #include <libusb.h>
  607. static struct
  608. {
  609. void* libhandle;
  610. int (LIBUSB_CALL *init)(libusb_context **ctx);
  611. void (LIBUSB_CALL *exit)(libusb_context *ctx);
  612. ssize_t (LIBUSB_CALL *get_device_list)(libusb_context *ctx, libusb_device ***list);
  613. void (LIBUSB_CALL *free_device_list)(libusb_device **list, int unref_devices);
  614. int (LIBUSB_CALL *get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
  615. int (LIBUSB_CALL *get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);
  616. int (LIBUSB_CALL *get_config_descriptor)(
  617. libusb_device *dev,
  618. uint8_t config_index,
  619. struct libusb_config_descriptor **config
  620. );
  621. void (LIBUSB_CALL *free_config_descriptor)(struct libusb_config_descriptor *config);
  622. uint8_t (LIBUSB_CALL *get_bus_number)(libusb_device *dev);
  623. uint8_t (LIBUSB_CALL *get_device_address)(libusb_device *dev);
  624. int (LIBUSB_CALL *open)(libusb_device *dev, libusb_device_handle **dev_handle);
  625. void (LIBUSB_CALL *close)(libusb_device_handle *dev_handle);
  626. int (LIBUSB_CALL *claim_interface)(libusb_device_handle *dev_handle, int interface_number);
  627. int (LIBUSB_CALL *release_interface)(libusb_device_handle *dev_handle, int interface_number);
  628. int (LIBUSB_CALL *kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
  629. int (LIBUSB_CALL *detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  630. int (LIBUSB_CALL *attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  631. int (LIBUSB_CALL *set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
  632. struct libusb_transfer * (LIBUSB_CALL *alloc_transfer)(int iso_packets);
  633. int (LIBUSB_CALL *submit_transfer)(struct libusb_transfer *transfer);
  634. int (LIBUSB_CALL *cancel_transfer)(struct libusb_transfer *transfer);
  635. void (LIBUSB_CALL *free_transfer)(struct libusb_transfer *transfer);
  636. int (LIBUSB_CALL *control_transfer)(
  637. libusb_device_handle *dev_handle,
  638. uint8_t request_type,
  639. uint8_t bRequest,
  640. uint16_t wValue,
  641. uint16_t wIndex,
  642. unsigned char *data,
  643. uint16_t wLength,
  644. unsigned int timeout
  645. );
  646. int (LIBUSB_CALL *interrupt_transfer)(
  647. libusb_device_handle *dev_handle,
  648. unsigned char endpoint,
  649. unsigned char *data,
  650. int length,
  651. int *actual_length,
  652. unsigned int timeout
  653. );
  654. int (LIBUSB_CALL *handle_events)(libusb_context *ctx);
  655. int (LIBUSB_CALL *handle_events_completed)(libusb_context *ctx, int *completed);
  656. const char * (LIBUSB_CALL *error_name)(int errcode);
  657. } libusb_ctx;
  658. #define libusb_init libusb_ctx.init
  659. #define libusb_exit libusb_ctx.exit
  660. #define libusb_get_device_list libusb_ctx.get_device_list
  661. #define libusb_free_device_list libusb_ctx.free_device_list
  662. #define libusb_get_device_descriptor libusb_ctx.get_device_descriptor
  663. #define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
  664. #define libusb_get_config_descriptor libusb_ctx.get_config_descriptor
  665. #define libusb_free_config_descriptor libusb_ctx.free_config_descriptor
  666. #define libusb_get_bus_number libusb_ctx.get_bus_number
  667. #define libusb_get_device_address libusb_ctx.get_device_address
  668. #define libusb_open libusb_ctx.open
  669. #define libusb_close libusb_ctx.close
  670. #define libusb_claim_interface libusb_ctx.claim_interface
  671. #define libusb_release_interface libusb_ctx.release_interface
  672. #define libusb_kernel_driver_active libusb_ctx.kernel_driver_active
  673. #define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver
  674. #define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver
  675. #define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting
  676. #define libusb_alloc_transfer libusb_ctx.alloc_transfer
  677. #define libusb_submit_transfer libusb_ctx.submit_transfer
  678. #define libusb_cancel_transfer libusb_ctx.cancel_transfer
  679. #define libusb_free_transfer libusb_ctx.free_transfer
  680. #define libusb_control_transfer libusb_ctx.control_transfer
  681. #define libusb_interrupt_transfer libusb_ctx.interrupt_transfer
  682. #define libusb_handle_events libusb_ctx.handle_events
  683. #define libusb_handle_events_completed libusb_ctx.handle_events_completed
  684. #define libusb_error_name libusb_ctx.error_name
  685. #define hid_device LIBUSB_hid_device
  686. #define hid_device_ LIBUSB_hid_device_
  687. #define hid_init LIBUSB_hid_init
  688. #define hid_exit LIBUSB_hid_exit
  689. #define hid_enumerate LIBUSB_hid_enumerate
  690. #define hid_free_enumeration LIBUSB_hid_free_enumeration
  691. #define hid_open LIBUSB_hid_open
  692. #define hid_open_path LIBUSB_hid_open_path
  693. #define hid_write LIBUSB_hid_write
  694. #define hid_read_timeout LIBUSB_hid_read_timeout
  695. #define hid_read LIBUSB_hid_read
  696. #define hid_set_nonblocking LIBUSB_hid_set_nonblocking
  697. #define hid_send_feature_report LIBUSB_hid_send_feature_report
  698. #define hid_get_feature_report LIBUSB_hid_get_feature_report
  699. #define hid_close LIBUSB_hid_close
  700. #define hid_get_manufacturer_string LIBUSB_hid_get_manufacturer_string
  701. #define hid_get_product_string LIBUSB_hid_get_product_string
  702. #define hid_get_serial_number_string LIBUSB_hid_get_serial_number_string
  703. #define hid_get_indexed_string LIBUSB_hid_get_indexed_string
  704. #define hid_error LIBUSB_hid_error
  705. #define new_hid_device LIBUSB_new_hid_device
  706. #define free_hid_device LIBUSB_free_hid_device
  707. #define input_report LIBUSB_input_report
  708. #define return_data LIBUSB_return_data
  709. #define make_path LIBUSB_make_path
  710. #define read_thread LIBUSB_read_thread
  711. #ifndef __FreeBSD__
  712. /* this is awkwardly inlined, so we need to re-implement it here
  713. * so we can override the libusb_control_transfer call */
  714. static int
  715. SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
  716. uint8_t descriptor_index, uint16_t lang_id,
  717. unsigned char *data, int length)
  718. {
  719. return libusb_control_transfer(dev,
  720. LIBUSB_ENDPOINT_IN | 0x0, /* Endpoint 0 IN */
  721. LIBUSB_REQUEST_GET_DESCRIPTOR,
  722. (LIBUSB_DT_STRING << 8) | descriptor_index,
  723. lang_id,
  724. data,
  725. (uint16_t) length,
  726. 1000);
  727. }
  728. #define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
  729. #endif /* __FreeBSD__ */
  730. #undef HIDAPI_H__
  731. #include "libusb/hid.c"
  732. #undef libusb_init
  733. #undef libusb_exit
  734. #undef libusb_get_device_list
  735. #undef libusb_free_device_list
  736. #undef libusb_get_device_descriptor
  737. #undef libusb_get_active_config_descriptor
  738. #undef libusb_get_config_descriptor
  739. #undef libusb_free_config_descriptor
  740. #undef libusb_get_bus_number
  741. #undef libusb_get_device_address
  742. #undef libusb_open
  743. #undef libusb_close
  744. #undef libusb_claim_interface
  745. #undef libusb_release_interface
  746. #undef libusb_kernel_driver_active
  747. #undef libusb_detach_kernel_driver
  748. #undef libusb_attach_kernel_driver
  749. #undef libusb_set_interface_alt_setting
  750. #undef libusb_alloc_transfer
  751. #undef libusb_submit_transfer
  752. #undef libusb_cancel_transfer
  753. #undef libusb_free_transfer
  754. #undef libusb_control_transfer
  755. #undef libusb_interrupt_transfer
  756. #undef libusb_handle_events
  757. #undef libusb_handle_events_completed
  758. #undef libusb_error_name
  759. #undef hid_device
  760. #undef hid_device_
  761. #undef hid_init
  762. #undef hid_exit
  763. #undef hid_enumerate
  764. #undef hid_free_enumeration
  765. #undef hid_open
  766. #undef hid_open_path
  767. #undef hid_write
  768. #undef hid_read_timeout
  769. #undef hid_read
  770. #undef hid_set_nonblocking
  771. #undef hid_send_feature_report
  772. #undef hid_get_feature_report
  773. #undef hid_close
  774. #undef hid_get_manufacturer_string
  775. #undef hid_get_product_string
  776. #undef hid_get_serial_number_string
  777. #undef hid_get_indexed_string
  778. #undef hid_error
  779. #undef new_hid_device
  780. #undef free_hid_device
  781. #undef input_report
  782. #undef return_data
  783. #undef make_path
  784. #undef read_thread
  785. #endif /* HAVE_LIBUSB */
  786. #endif /* !SDL_HIDAPI_DISABLED */
  787. /* Shared HIDAPI Implementation */
  788. struct hidapi_backend {
  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(func) \
  964. if (!(libusb_ctx.func = SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) {loaded = SDL_FALSE;}
  965. #else
  966. #define LOAD_LIBUSB_SYMBOL(func) \
  967. libusb_ctx.func = libusb_##func;
  968. #endif
  969. LOAD_LIBUSB_SYMBOL(init)
  970. LOAD_LIBUSB_SYMBOL(exit)
  971. LOAD_LIBUSB_SYMBOL(get_device_list)
  972. LOAD_LIBUSB_SYMBOL(free_device_list)
  973. LOAD_LIBUSB_SYMBOL(get_device_descriptor)
  974. LOAD_LIBUSB_SYMBOL(get_active_config_descriptor)
  975. LOAD_LIBUSB_SYMBOL(get_config_descriptor)
  976. LOAD_LIBUSB_SYMBOL(free_config_descriptor)
  977. LOAD_LIBUSB_SYMBOL(get_bus_number)
  978. LOAD_LIBUSB_SYMBOL(get_device_address)
  979. LOAD_LIBUSB_SYMBOL(open)
  980. LOAD_LIBUSB_SYMBOL(close)
  981. LOAD_LIBUSB_SYMBOL(claim_interface)
  982. LOAD_LIBUSB_SYMBOL(release_interface)
  983. LOAD_LIBUSB_SYMBOL(kernel_driver_active)
  984. LOAD_LIBUSB_SYMBOL(detach_kernel_driver)
  985. LOAD_LIBUSB_SYMBOL(attach_kernel_driver)
  986. LOAD_LIBUSB_SYMBOL(set_interface_alt_setting)
  987. LOAD_LIBUSB_SYMBOL(alloc_transfer)
  988. LOAD_LIBUSB_SYMBOL(submit_transfer)
  989. LOAD_LIBUSB_SYMBOL(cancel_transfer)
  990. LOAD_LIBUSB_SYMBOL(free_transfer)
  991. LOAD_LIBUSB_SYMBOL(control_transfer)
  992. LOAD_LIBUSB_SYMBOL(interrupt_transfer)
  993. LOAD_LIBUSB_SYMBOL(handle_events)
  994. LOAD_LIBUSB_SYMBOL(handle_events_completed)
  995. LOAD_LIBUSB_SYMBOL(error_name)
  996. #undef LOAD_LIBUSB_SYMBOL
  997. if (!loaded) {
  998. #ifdef SDL_LIBUSB_DYNAMIC
  999. SDL_UnloadObject(libusb_ctx.libhandle);
  1000. #endif
  1001. libusb_ctx.libhandle = NULL;
  1002. /* SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function"); */
  1003. } else if (LIBUSB_hid_init() < 0) {
  1004. #ifdef SDL_LIBUSB_DYNAMIC
  1005. SDL_UnloadObject(libusb_ctx.libhandle);
  1006. #endif
  1007. libusb_ctx.libhandle = NULL;
  1008. } else {
  1009. ++success;
  1010. }
  1011. }
  1012. }
  1013. #endif /* HAVE_LIBUSB */
  1014. #if HAVE_PLATFORM_BACKEND
  1015. ++attempts;
  1016. #if __LINUX__
  1017. udev_ctx = SDL_UDEV_GetUdevSyms();
  1018. #endif /* __LINUX __ */
  1019. if (udev_ctx && PLATFORM_hid_init() == 0) {
  1020. ++success;
  1021. }
  1022. #endif /* HAVE_PLATFORM_BACKEND */
  1023. if (attempts > 0 && success == 0) {
  1024. return -1;
  1025. }
  1026. ++SDL_hidapi_refcount;
  1027. return 0;
  1028. }
  1029. int SDL_hid_exit(void)
  1030. {
  1031. int result = 0;
  1032. if (SDL_hidapi_refcount == 0) {
  1033. return 0;
  1034. }
  1035. --SDL_hidapi_refcount;
  1036. if (SDL_hidapi_refcount > 0) {
  1037. return 0;
  1038. }
  1039. SDL_hidapi_refcount = 0;
  1040. #if !SDL_HIDAPI_DISABLED
  1041. HIDAPI_ShutdownDiscovery();
  1042. #endif
  1043. #if HAVE_PLATFORM_BACKEND
  1044. if (udev_ctx) {
  1045. result |= PLATFORM_hid_exit();
  1046. }
  1047. #if __LINUX__
  1048. SDL_UDEV_ReleaseUdevSyms();
  1049. #endif /* __LINUX __ */
  1050. #endif /* HAVE_PLATFORM_BACKEND */
  1051. #ifdef HAVE_LIBUSB
  1052. if (libusb_ctx.libhandle) {
  1053. result |= LIBUSB_hid_exit();
  1054. #ifdef SDL_LIBUSB_DYNAMIC
  1055. SDL_UnloadObject(libusb_ctx.libhandle);
  1056. #endif
  1057. libusb_ctx.libhandle = NULL;
  1058. }
  1059. #endif /* HAVE_LIBUSB */
  1060. return result;
  1061. }
  1062. Uint32 SDL_hid_device_change_count(void)
  1063. {
  1064. Uint32 counter = 0;
  1065. #if !SDL_HIDAPI_DISABLED
  1066. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1067. return 0;
  1068. }
  1069. HIDAPI_UpdateDiscovery();
  1070. if (SDL_HIDAPI_discovery.m_unDeviceChangeCounter == 0) {
  1071. /* Counter wrapped! */
  1072. ++SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1073. }
  1074. counter = SDL_HIDAPI_discovery.m_unDeviceChangeCounter;
  1075. #endif /* !SDL_HIDAPI_DISABLED */
  1076. return counter;
  1077. }
  1078. struct SDL_hid_device_info *SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id)
  1079. {
  1080. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1081. #ifdef HAVE_LIBUSB
  1082. struct SDL_hid_device_info *usb_devs = NULL;
  1083. struct SDL_hid_device_info *usb_dev;
  1084. #endif
  1085. #if HAVE_DRIVER_BACKEND
  1086. struct SDL_hid_device_info* driver_devs = NULL;
  1087. struct SDL_hid_device_info* driver_dev;
  1088. #endif
  1089. #if HAVE_PLATFORM_BACKEND
  1090. struct SDL_hid_device_info *raw_devs = NULL;
  1091. struct SDL_hid_device_info *raw_dev;
  1092. #endif
  1093. struct SDL_hid_device_info *devs = NULL, *last = NULL, *new_dev;
  1094. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1095. return NULL;
  1096. }
  1097. #ifdef HAVE_LIBUSB
  1098. if (libusb_ctx.libhandle) {
  1099. usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
  1100. #ifdef DEBUG_HIDAPI
  1101. SDL_Log("libusb devices found:");
  1102. #endif
  1103. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  1104. new_dev = (struct SDL_hid_device_info*) SDL_malloc(sizeof(struct SDL_hid_device_info));
  1105. if (!new_dev) {
  1106. LIBUSB_hid_free_enumeration(usb_devs);
  1107. SDL_hid_free_enumeration(devs);
  1108. SDL_OutOfMemory();
  1109. return NULL;
  1110. }
  1111. CopyHIDDeviceInfo(usb_dev, new_dev);
  1112. #ifdef DEBUG_HIDAPI
  1113. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  1114. usb_dev->manufacturer_string, usb_dev->product_string,
  1115. usb_dev->vendor_id, usb_dev->product_id);
  1116. #endif
  1117. if (last != NULL) {
  1118. last->next = new_dev;
  1119. } else {
  1120. devs = new_dev;
  1121. }
  1122. last = new_dev;
  1123. }
  1124. }
  1125. #endif /* HAVE_LIBUSB */
  1126. #ifdef HAVE_DRIVER_BACKEND
  1127. driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);
  1128. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  1129. new_dev = (struct SDL_hid_device_info*) SDL_malloc(sizeof(struct SDL_hid_device_info));
  1130. CopyHIDDeviceInfo(driver_dev, new_dev);
  1131. if (last != NULL) {
  1132. last->next = new_dev;
  1133. } else {
  1134. devs = new_dev;
  1135. }
  1136. last = new_dev;
  1137. }
  1138. #endif /* HAVE_DRIVER_BACKEND */
  1139. #if HAVE_PLATFORM_BACKEND
  1140. if (udev_ctx) {
  1141. raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
  1142. #ifdef DEBUG_HIDAPI
  1143. SDL_Log("hidraw devices found:");
  1144. #endif
  1145. for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
  1146. SDL_bool bFound = SDL_FALSE;
  1147. #ifdef DEBUG_HIDAPI
  1148. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  1149. raw_dev->manufacturer_string, raw_dev->product_string,
  1150. raw_dev->vendor_id, raw_dev->product_id);
  1151. #endif
  1152. #ifdef HAVE_LIBUSB
  1153. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  1154. if (raw_dev->vendor_id == usb_dev->vendor_id &&
  1155. raw_dev->product_id == usb_dev->product_id &&
  1156. (raw_dev->interface_number < 0 || raw_dev->interface_number == usb_dev->interface_number)) {
  1157. bFound = SDL_TRUE;
  1158. break;
  1159. }
  1160. }
  1161. #endif
  1162. #ifdef HAVE_DRIVER_BACKEND
  1163. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  1164. if (raw_dev->vendor_id == driver_dev->vendor_id &&
  1165. raw_dev->product_id == driver_dev->product_id &&
  1166. (raw_dev->interface_number < 0 || raw_dev->interface_number == driver_dev->interface_number)) {
  1167. bFound = SDL_TRUE;
  1168. break;
  1169. }
  1170. }
  1171. #endif
  1172. if (!bFound) {
  1173. new_dev = (struct SDL_hid_device_info*) SDL_malloc(sizeof(struct SDL_hid_device_info));
  1174. if (!new_dev) {
  1175. #ifdef HAVE_LIBUSB
  1176. if (libusb_ctx.libhandle) {
  1177. LIBUSB_hid_free_enumeration(usb_devs);
  1178. }
  1179. #endif
  1180. PLATFORM_hid_free_enumeration(raw_devs);
  1181. SDL_hid_free_enumeration(devs);
  1182. SDL_OutOfMemory();
  1183. return NULL;
  1184. }
  1185. CopyHIDDeviceInfo(raw_dev, new_dev);
  1186. new_dev->next = NULL;
  1187. if (last != NULL) {
  1188. last->next = new_dev;
  1189. } else {
  1190. devs = new_dev;
  1191. }
  1192. last = new_dev;
  1193. }
  1194. }
  1195. PLATFORM_hid_free_enumeration(raw_devs);
  1196. }
  1197. #endif /* HAVE_PLATFORM_BACKEND */
  1198. #ifdef HAVE_LIBUSB
  1199. if (libusb_ctx.libhandle) {
  1200. LIBUSB_hid_free_enumeration(usb_devs);
  1201. }
  1202. #endif
  1203. return devs;
  1204. #else
  1205. return NULL;
  1206. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1207. }
  1208. void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
  1209. {
  1210. while (devs) {
  1211. struct SDL_hid_device_info *next = devs->next;
  1212. SDL_free(devs->path);
  1213. SDL_free(devs->serial_number);
  1214. SDL_free(devs->manufacturer_string);
  1215. SDL_free(devs->product_string);
  1216. SDL_free(devs);
  1217. devs = next;
  1218. }
  1219. }
  1220. SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
  1221. {
  1222. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1223. void *pDevice = NULL;
  1224. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1225. return NULL;
  1226. }
  1227. #if HAVE_PLATFORM_BACKEND
  1228. if (udev_ctx &&
  1229. (pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  1230. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1231. }
  1232. #endif /* HAVE_PLATFORM_BACKEND */
  1233. #if HAVE_DRIVER_BACKEND
  1234. if ((pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  1235. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1236. }
  1237. #endif /* HAVE_DRIVER_BACKEND */
  1238. #ifdef HAVE_LIBUSB
  1239. if (libusb_ctx.libhandle &&
  1240. (pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  1241. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1242. }
  1243. #endif /* HAVE_LIBUSB */
  1244. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1245. return NULL;
  1246. }
  1247. SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */)
  1248. {
  1249. #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(HAVE_LIBUSB)
  1250. void *pDevice = NULL;
  1251. if (SDL_hidapi_refcount == 0 && SDL_hid_init() != 0) {
  1252. return NULL;
  1253. }
  1254. #if HAVE_PLATFORM_BACKEND
  1255. if (udev_ctx &&
  1256. (pDevice = PLATFORM_hid_open_path(path, bExclusive)) != NULL) {
  1257. return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  1258. }
  1259. #endif /* HAVE_PLATFORM_BACKEND */
  1260. #if HAVE_DRIVER_BACKEND
  1261. if ((pDevice = DRIVER_hid_open_path(path, bExclusive)) != NULL) {
  1262. return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  1263. }
  1264. #endif /* HAVE_DRIVER_BACKEND */
  1265. #ifdef HAVE_LIBUSB
  1266. if (libusb_ctx.libhandle &&
  1267. (pDevice = LIBUSB_hid_open_path(path, bExclusive)) != NULL) {
  1268. return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  1269. }
  1270. #endif /* HAVE_LIBUSB */
  1271. #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || HAVE_LIBUSB */
  1272. return NULL;
  1273. }
  1274. int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
  1275. {
  1276. int result;
  1277. CHECK_DEVICE_MAGIC(device, -1);
  1278. result = device->backend->hid_write(device->device, data, length);
  1279. if (result < 0) {
  1280. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1281. }
  1282. return result;
  1283. }
  1284. int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds)
  1285. {
  1286. int result;
  1287. CHECK_DEVICE_MAGIC(device, -1);
  1288. result = device->backend->hid_read_timeout(device->device, data, length, milliseconds);
  1289. if (result < 0) {
  1290. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1291. }
  1292. return result;
  1293. }
  1294. int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length)
  1295. {
  1296. int result;
  1297. CHECK_DEVICE_MAGIC(device, -1);
  1298. result = device->backend->hid_read(device->device, data, length);
  1299. if (result < 0) {
  1300. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1301. }
  1302. return result;
  1303. }
  1304. int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock)
  1305. {
  1306. int result;
  1307. CHECK_DEVICE_MAGIC(device, -1);
  1308. result = device->backend->hid_set_nonblocking(device->device, nonblock);
  1309. if (result < 0) {
  1310. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1311. }
  1312. return result;
  1313. }
  1314. int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length)
  1315. {
  1316. int result;
  1317. CHECK_DEVICE_MAGIC(device, -1);
  1318. result = device->backend->hid_send_feature_report(device->device, data, length);
  1319. if (result < 0) {
  1320. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1321. }
  1322. return result;
  1323. }
  1324. int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length)
  1325. {
  1326. int result;
  1327. CHECK_DEVICE_MAGIC(device, -1);
  1328. result = device->backend->hid_get_feature_report(device->device, data, length);
  1329. if (result < 0) {
  1330. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1331. }
  1332. return result;
  1333. }
  1334. void SDL_hid_close(SDL_hid_device *device)
  1335. {
  1336. CHECK_DEVICE_MAGIC(device,);
  1337. device->backend->hid_close(device->device);
  1338. DeleteHIDDeviceWrapper(device);
  1339. }
  1340. int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1341. {
  1342. int result;
  1343. CHECK_DEVICE_MAGIC(device, -1);
  1344. result = device->backend->hid_get_manufacturer_string(device->device, string, maxlen);
  1345. if (result < 0) {
  1346. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1347. }
  1348. return result;
  1349. }
  1350. int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1351. {
  1352. int result;
  1353. CHECK_DEVICE_MAGIC(device, -1);
  1354. result = device->backend->hid_get_product_string(device->device, string, maxlen);
  1355. if (result < 0) {
  1356. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1357. }
  1358. return result;
  1359. }
  1360. int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
  1361. {
  1362. int result;
  1363. CHECK_DEVICE_MAGIC(device, -1);
  1364. result = device->backend->hid_get_serial_number_string(device->device, string, maxlen);
  1365. if (result < 0) {
  1366. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1367. }
  1368. return result;
  1369. }
  1370. int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen)
  1371. {
  1372. int result;
  1373. CHECK_DEVICE_MAGIC(device, -1);
  1374. result = device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen);
  1375. if (result < 0) {
  1376. SDL_SetHIDAPIError(device->backend->hid_error(device->device));
  1377. }
  1378. return result;
  1379. }
  1380. void SDL_hid_ble_scan(SDL_bool active)
  1381. {
  1382. #if !SDL_HIDAPI_DISABLED && (__IOS__ || __TVOS__)
  1383. hid_ble_scan(active);
  1384. #endif
  1385. }
  1386. #ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
  1387. /* This is needed to enable input for Nyko and EVORETRO GameCube adaptors */
  1388. void SDL_EnableGameCubeAdaptors(void)
  1389. {
  1390. #ifdef HAVE_LIBUSB
  1391. libusb_context *context = NULL;
  1392. libusb_device **devs = NULL;
  1393. libusb_device_handle *handle = NULL;
  1394. struct libusb_device_descriptor desc;
  1395. ssize_t i, num_devs;
  1396. int kernel_detached = 0;
  1397. if (libusb_ctx.libhandle == NULL) {
  1398. return;
  1399. }
  1400. if (libusb_ctx.init(&context) == 0) {
  1401. num_devs = libusb_ctx.get_device_list(context, &devs);
  1402. for (i = 0; i < num_devs; ++i) {
  1403. if (libusb_ctx.get_device_descriptor(devs[i], &desc) != 0) {
  1404. continue;
  1405. }
  1406. if (desc.idVendor != 0x057e || desc.idProduct != 0x0337) {
  1407. continue;
  1408. }
  1409. if (libusb_ctx.open(devs[i], &handle) != 0) {
  1410. continue;
  1411. }
  1412. if (libusb_ctx.kernel_driver_active(handle, 0)) {
  1413. if (libusb_ctx.detach_kernel_driver(handle, 0) == 0) {
  1414. kernel_detached = 1;
  1415. }
  1416. }
  1417. if (libusb_ctx.claim_interface(handle, 0) == 0) {
  1418. libusb_ctx.control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
  1419. libusb_ctx.release_interface(handle, 0);
  1420. }
  1421. if (kernel_detached) {
  1422. libusb_ctx.attach_kernel_driver(handle, 0);
  1423. }
  1424. libusb_ctx.close(handle);
  1425. }
  1426. libusb_ctx.free_device_list(devs, 1);
  1427. libusb_ctx.exit(context);
  1428. }
  1429. #endif /* HAVE_LIBUSB */
  1430. }
  1431. #endif /* HAVE_ENABLE_GAMECUBE_ADAPTORS */
  1432. /* vi: set sts=4 ts=4 sw=4 expandtab: */