SDL_hidapi.c 53 KB

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