SDL_hidapi.c 59 KB

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