SDL_hidapi.c 59 KB

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