SDL_hidapijoystick.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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. #include "../../SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "SDL_assert.h"
  21. #include "SDL_atomic.h"
  22. #include "SDL_endian.h"
  23. #include "SDL_hints.h"
  24. #include "SDL_thread.h"
  25. #include "SDL_timer.h"
  26. #include "SDL_joystick.h"
  27. #include "../SDL_sysjoystick.h"
  28. #include "SDL_hidapijoystick_c.h"
  29. #include "SDL_hidapi_rumble.h"
  30. #include "../../SDL_hints_c.h"
  31. #if defined(__WIN32__)
  32. #include "../../core/windows/SDL_windows.h"
  33. #include "../windows/SDL_rawinputjoystick_c.h"
  34. #endif
  35. #if defined(__MACOSX__)
  36. #include <CoreFoundation/CoreFoundation.h>
  37. #include <mach/mach.h>
  38. #include <IOKit/IOKitLib.h>
  39. #include <IOKit/hid/IOHIDDevice.h>
  40. #include <IOKit/usb/USBSpec.h>
  41. #endif
  42. #if defined(__LINUX__)
  43. #include "../../core/linux/SDL_udev.h"
  44. #ifdef SDL_USE_LIBUDEV
  45. #include <poll.h>
  46. #endif
  47. #endif
  48. struct joystick_hwdata
  49. {
  50. SDL_HIDAPI_Device *device;
  51. };
  52. static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
  53. #ifdef SDL_JOYSTICK_HIDAPI_PS4
  54. &SDL_HIDAPI_DriverPS4,
  55. #endif
  56. #ifdef SDL_JOYSTICK_HIDAPI_PS5
  57. &SDL_HIDAPI_DriverPS5,
  58. #endif
  59. #ifdef SDL_JOYSTICK_HIDAPI_STEAM
  60. &SDL_HIDAPI_DriverSteam,
  61. #endif
  62. #ifdef SDL_JOYSTICK_HIDAPI_SWITCH
  63. &SDL_HIDAPI_DriverSwitch,
  64. #endif
  65. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  66. &SDL_HIDAPI_DriverXbox360,
  67. &SDL_HIDAPI_DriverXbox360W,
  68. #endif
  69. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  70. &SDL_HIDAPI_DriverXboxOne,
  71. #endif
  72. #ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
  73. &SDL_HIDAPI_DriverGameCube,
  74. #endif
  75. };
  76. static int SDL_HIDAPI_numdrivers = 0;
  77. static SDL_SpinLock SDL_HIDAPI_spinlock;
  78. static SDL_HIDAPI_Device *SDL_HIDAPI_devices;
  79. static int SDL_HIDAPI_numjoysticks = 0;
  80. static SDL_bool initialized = SDL_FALSE;
  81. static SDL_bool shutting_down = SDL_FALSE;
  82. #if defined(SDL_USE_LIBUDEV)
  83. static const SDL_UDEV_Symbols * usyms = NULL;
  84. #endif
  85. static struct
  86. {
  87. SDL_bool m_bHaveDevicesChanged;
  88. SDL_bool m_bCanGetNotifications;
  89. Uint32 m_unLastDetect;
  90. #if defined(__WIN32__)
  91. SDL_threadID m_nThreadID;
  92. WNDCLASSEXA m_wndClass;
  93. HWND m_hwndMsg;
  94. HDEVNOTIFY m_hNotify;
  95. double m_flLastWin32MessageCheck;
  96. #endif
  97. #if defined(__MACOSX__)
  98. IONotificationPortRef m_notificationPort;
  99. mach_port_t m_notificationMach;
  100. #endif
  101. #if defined(SDL_USE_LIBUDEV)
  102. struct udev *m_pUdev;
  103. struct udev_monitor *m_pUdevMonitor;
  104. int m_nUdevFd;
  105. #endif
  106. } SDL_HIDAPI_discovery;
  107. #ifdef __WIN32__
  108. struct _DEV_BROADCAST_HDR
  109. {
  110. DWORD dbch_size;
  111. DWORD dbch_devicetype;
  112. DWORD dbch_reserved;
  113. };
  114. typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
  115. {
  116. DWORD dbcc_size;
  117. DWORD dbcc_devicetype;
  118. DWORD dbcc_reserved;
  119. GUID dbcc_classguid;
  120. char dbcc_name[ 1 ];
  121. } DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;
  122. typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
  123. #define DBT_DEVICEARRIVAL 0x8000 /* system detected a new device */
  124. #define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
  125. #define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
  126. #define DBT_DEVNODES_CHANGED 0x0007
  127. #define DBT_CONFIGCHANGED 0x0018
  128. #define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
  129. #define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
  130. #include <initguid.h>
  131. DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
  132. static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  133. {
  134. switch (message) {
  135. case WM_DEVICECHANGE:
  136. switch (wParam) {
  137. case DBT_DEVICEARRIVAL:
  138. case DBT_DEVICEREMOVECOMPLETE:
  139. if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  140. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  141. }
  142. break;
  143. }
  144. return TRUE;
  145. }
  146. return DefWindowProc(hwnd, message, wParam, lParam);
  147. }
  148. #endif /* __WIN32__ */
  149. #if defined(__MACOSX__)
  150. static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
  151. {
  152. /* Must drain the iterator, or we won't receive new notifications */
  153. io_object_t entry;
  154. while ((entry = IOIteratorNext(portIterator)) != 0) {
  155. IOObjectRelease(entry);
  156. *(SDL_bool*)context = SDL_TRUE;
  157. }
  158. }
  159. #endif /* __MACOSX__ */
  160. static void
  161. HIDAPI_InitializeDiscovery()
  162. {
  163. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  164. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
  165. SDL_HIDAPI_discovery.m_unLastDetect = 0;
  166. #if defined(__WIN32__)
  167. SDL_HIDAPI_discovery.m_nThreadID = SDL_ThreadID();
  168. SDL_zero(SDL_HIDAPI_discovery.m_wndClass);
  169. SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
  170. SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
  171. SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; /* This function is called by windows */
  172. SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);
  173. RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
  174. SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  175. {
  176. DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;
  177. SDL_zero(devBroadcast);
  178. devBroadcast.dbcc_size = sizeof( devBroadcast );
  179. devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  180. devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
  181. /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
  182. * but that seems to be necessary to get a notice after each individual usb input device actually
  183. * installs, rather than just as the composite device is seen.
  184. */
  185. SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification( SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
  186. SDL_HIDAPI_discovery.m_bCanGetNotifications = ( SDL_HIDAPI_discovery.m_hNotify != 0 );
  187. }
  188. #endif /* __WIN32__ */
  189. #if defined(__MACOSX__)
  190. SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
  191. if (SDL_HIDAPI_discovery.m_notificationPort) {
  192. {
  193. io_iterator_t portIterator = 0;
  194. io_object_t entry;
  195. IOReturn result = IOServiceAddMatchingNotification(
  196. SDL_HIDAPI_discovery.m_notificationPort,
  197. kIOFirstMatchNotification,
  198. IOServiceMatching(kIOHIDDeviceKey),
  199. CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator);
  200. if (result == 0) {
  201. /* Must drain the existing iterator, or we won't receive new notifications */
  202. while ((entry = IOIteratorNext(portIterator)) != 0) {
  203. IOObjectRelease(entry);
  204. }
  205. } else {
  206. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  207. SDL_HIDAPI_discovery.m_notificationPort = nil;
  208. }
  209. }
  210. {
  211. io_iterator_t portIterator = 0;
  212. io_object_t entry;
  213. IOReturn result = IOServiceAddMatchingNotification(
  214. SDL_HIDAPI_discovery.m_notificationPort,
  215. kIOTerminatedNotification,
  216. IOServiceMatching(kIOHIDDeviceKey),
  217. CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator);
  218. if (result == 0) {
  219. /* Must drain the existing iterator, or we won't receive new notifications */
  220. while ((entry = IOIteratorNext(portIterator)) != 0) {
  221. IOObjectRelease(entry);
  222. }
  223. } else {
  224. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  225. SDL_HIDAPI_discovery.m_notificationPort = nil;
  226. }
  227. }
  228. }
  229. SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
  230. if (SDL_HIDAPI_discovery.m_notificationPort) {
  231. SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
  232. }
  233. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);
  234. #endif // __MACOSX__
  235. #if defined(SDL_USE_LIBUDEV)
  236. SDL_HIDAPI_discovery.m_pUdev = NULL;
  237. SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
  238. SDL_HIDAPI_discovery.m_nUdevFd = -1;
  239. usyms = SDL_UDEV_GetUdevSyms();
  240. if (usyms) {
  241. SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
  242. }
  243. if (SDL_HIDAPI_discovery.m_pUdev) {
  244. SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
  245. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  246. usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
  247. SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
  248. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
  249. }
  250. }
  251. #endif /* SDL_USE_LIBUDEV */
  252. }
  253. static void
  254. HIDAPI_UpdateDiscovery()
  255. {
  256. if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) {
  257. const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
  258. Uint32 now = SDL_GetTicks();
  259. if (!SDL_HIDAPI_discovery.m_unLastDetect || SDL_TICKS_PASSED(now, SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) {
  260. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  261. SDL_HIDAPI_discovery.m_unLastDetect = now;
  262. }
  263. return;
  264. }
  265. #if defined(__WIN32__)
  266. #if 0 /* just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan. */
  267. /* We'll only get messages on the same thread that created the window */
  268. if (SDL_ThreadID() == SDL_HIDAPI_discovery.m_nThreadID) {
  269. MSG msg;
  270. while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) {
  271. if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) {
  272. TranslateMessage(&msg);
  273. DispatchMessage(&msg);
  274. }
  275. }
  276. }
  277. #endif
  278. #endif /* __WIN32__ */
  279. #if defined(__MACOSX__)
  280. if (SDL_HIDAPI_discovery.m_notificationPort) {
  281. struct { mach_msg_header_t hdr; char payload[ 4096 ]; } msg;
  282. 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) {
  283. IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);
  284. }
  285. }
  286. #endif
  287. #if defined(SDL_USE_LIBUDEV)
  288. if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
  289. /* Drain all notification events.
  290. * We don't expect a lot of device notifications so just
  291. * do a new discovery on any kind or number of notifications.
  292. * This could be made more restrictive if necessary.
  293. */
  294. for (;;) {
  295. struct pollfd PollUdev;
  296. struct udev_device *pUdevDevice;
  297. PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
  298. PollUdev.events = POLLIN;
  299. if (poll(&PollUdev, 1, 0) != 1) {
  300. break;
  301. }
  302. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  303. pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
  304. if (pUdevDevice) {
  305. usyms->udev_device_unref(pUdevDevice);
  306. }
  307. }
  308. }
  309. #endif
  310. }
  311. static void
  312. HIDAPI_ShutdownDiscovery()
  313. {
  314. #if defined(__WIN32__)
  315. if (SDL_HIDAPI_discovery.m_hNotify)
  316. UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
  317. if (SDL_HIDAPI_discovery.m_hwndMsg) {
  318. DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
  319. }
  320. UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
  321. #endif
  322. #if defined(__MACOSX__)
  323. if (SDL_HIDAPI_discovery.m_notificationPort) {
  324. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  325. }
  326. #endif
  327. #if defined(SDL_USE_LIBUDEV)
  328. if (usyms) {
  329. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  330. usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
  331. }
  332. if (SDL_HIDAPI_discovery.m_pUdev) {
  333. usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
  334. }
  335. SDL_UDEV_ReleaseUdevSyms();
  336. usyms = NULL;
  337. }
  338. #endif
  339. }
  340. void
  341. HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size)
  342. {
  343. int i;
  344. char *buffer;
  345. size_t length = SDL_strlen(prefix) + 11*(USB_PACKET_LENGTH/8) + (5*USB_PACKET_LENGTH*2) + 1 + 1;
  346. int start = 0, amount = size;
  347. buffer = (char *)SDL_malloc(length);
  348. SDL_snprintf(buffer, length, prefix, size);
  349. for (i = start; i < start+amount; ++i) {
  350. if ((i % 8) == 0) {
  351. SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), "\n%.2d: ", i);
  352. }
  353. SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), " 0x%.2x", data[i]);
  354. }
  355. SDL_strlcat(buffer, "\n", length);
  356. SDL_Log("%s", buffer);
  357. SDL_free(buffer);
  358. }
  359. static void HIDAPI_JoystickDetect(void);
  360. static void HIDAPI_JoystickClose(SDL_Joystick * joystick);
  361. static SDL_bool
  362. HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  363. {
  364. int i;
  365. SDL_GameControllerType type = SDL_GetJoystickGameControllerType(name, vendor_id, product_id, -1, 0, 0, 0);
  366. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  367. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  368. if (driver->enabled && driver->IsSupportedDevice(name, type, vendor_id, product_id, version, -1, 0, 0, 0)) {
  369. return SDL_TRUE;
  370. }
  371. }
  372. return SDL_FALSE;
  373. }
  374. static SDL_HIDAPI_DeviceDriver *
  375. HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
  376. {
  377. const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
  378. const Uint16 USAGE_JOYSTICK = 0x0004;
  379. const Uint16 USAGE_GAMEPAD = 0x0005;
  380. const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
  381. int i;
  382. SDL_GameControllerType type;
  383. if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
  384. return NULL;
  385. }
  386. if (device->vendor_id != USB_VENDOR_VALVE) {
  387. if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
  388. return NULL;
  389. }
  390. if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
  391. return NULL;
  392. }
  393. }
  394. type = SDL_GetJoystickGameControllerType(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
  395. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  396. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  397. if (driver->enabled && driver->IsSupportedDevice(device->name, type, device->vendor_id, device->product_id, device->version, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol)) {
  398. return driver;
  399. }
  400. }
  401. return NULL;
  402. }
  403. static SDL_HIDAPI_Device *
  404. HIDAPI_GetDeviceByIndex(int device_index, SDL_JoystickID *pJoystickID)
  405. {
  406. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  407. while (device) {
  408. if (device->driver) {
  409. if (device_index < device->num_joysticks) {
  410. if (pJoystickID) {
  411. *pJoystickID = device->joysticks[device_index];
  412. }
  413. return device;
  414. }
  415. device_index -= device->num_joysticks;
  416. }
  417. device = device->next;
  418. }
  419. return NULL;
  420. }
  421. static SDL_HIDAPI_Device *
  422. HIDAPI_GetJoystickByInfo(const char *path, Uint16 vendor_id, Uint16 product_id)
  423. {
  424. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  425. while (device) {
  426. if (device->vendor_id == vendor_id && device->product_id == product_id &&
  427. SDL_strcmp(device->path, path) == 0) {
  428. break;
  429. }
  430. device = device->next;
  431. }
  432. return device;
  433. }
  434. static void
  435. HIDAPI_SetupDeviceDriver(SDL_HIDAPI_Device *device)
  436. {
  437. if (device->driver) {
  438. /* Already setup */
  439. return;
  440. }
  441. device->driver = HIDAPI_GetDeviceDriver(device);
  442. if (device->driver) {
  443. const char *name = device->driver->GetDeviceName(device->vendor_id, device->product_id);
  444. if (name) {
  445. SDL_free(device->name);
  446. device->name = SDL_strdup(name);
  447. }
  448. }
  449. /* Initialize the device, which may cause a connected event */
  450. if (device->driver && !device->driver->InitDevice(device)) {
  451. device->driver = NULL;
  452. }
  453. }
  454. static void
  455. HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device)
  456. {
  457. if (!device->driver) {
  458. /* Already cleaned up */
  459. return;
  460. }
  461. /* Disconnect any joysticks */
  462. while (device->num_joysticks) {
  463. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  464. }
  465. device->driver->FreeDevice(device);
  466. device->driver = NULL;
  467. }
  468. static void SDLCALL
  469. SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  470. {
  471. int i;
  472. SDL_HIDAPI_Device *device;
  473. SDL_bool enabled = SDL_GetStringBoolean(hint, SDL_TRUE);
  474. if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI) == 0) {
  475. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  476. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  477. driver->enabled = SDL_GetHintBoolean(driver->hint, enabled);
  478. }
  479. } else {
  480. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  481. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  482. if (SDL_strcmp(name, driver->hint) == 0) {
  483. driver->enabled = enabled;
  484. }
  485. }
  486. }
  487. SDL_HIDAPI_numdrivers = 0;
  488. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  489. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  490. if (driver->enabled) {
  491. ++SDL_HIDAPI_numdrivers;
  492. }
  493. }
  494. /* Update device list if driver availability changes */
  495. SDL_LockJoysticks();
  496. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  497. if (device->driver && !device->driver->enabled) {
  498. HIDAPI_CleanupDeviceDriver(device);
  499. }
  500. HIDAPI_SetupDeviceDriver(device);
  501. }
  502. SDL_UnlockJoysticks();
  503. }
  504. static int
  505. HIDAPI_JoystickInit(void)
  506. {
  507. int i;
  508. if (initialized) {
  509. return 0;
  510. }
  511. #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__TVOS__)
  512. /* The hidapi framwork is weak-linked on Apple platforms */
  513. int HID_API_EXPORT HID_API_CALL hid_init(void) __attribute__((weak_import));
  514. if (hid_init == NULL) {
  515. SDL_SetError("Couldn't initialize hidapi, framework not available");
  516. return -1;
  517. }
  518. #endif /* __MACOSX__ || __IPHONEOS__ || __TVOS__ */
  519. if (hid_init() < 0) {
  520. SDL_SetError("Couldn't initialize hidapi");
  521. return -1;
  522. }
  523. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  524. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  525. SDL_AddHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  526. }
  527. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  528. SDL_HIDAPIDriverHintChanged, NULL);
  529. HIDAPI_InitializeDiscovery();
  530. HIDAPI_JoystickDetect();
  531. HIDAPI_UpdateDevices();
  532. initialized = SDL_TRUE;
  533. return 0;
  534. }
  535. SDL_bool
  536. HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID)
  537. {
  538. SDL_JoystickID joystickID;
  539. SDL_JoystickID *joysticks = (SDL_JoystickID *)SDL_realloc(device->joysticks, (device->num_joysticks + 1)*sizeof(*device->joysticks));
  540. if (!joysticks) {
  541. return SDL_FALSE;
  542. }
  543. joystickID = SDL_GetNextJoystickInstanceID();
  544. device->joysticks = joysticks;
  545. device->joysticks[device->num_joysticks++] = joystickID;
  546. ++SDL_HIDAPI_numjoysticks;
  547. SDL_PrivateJoystickAdded(joystickID);
  548. if (pJoystickID) {
  549. *pJoystickID = joystickID;
  550. }
  551. return SDL_TRUE;
  552. }
  553. void
  554. HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  555. {
  556. int i, size;
  557. for (i = 0; i < device->num_joysticks; ++i) {
  558. if (device->joysticks[i] == joystickID) {
  559. SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystickID);
  560. if (joystick) {
  561. HIDAPI_JoystickClose(joystick);
  562. }
  563. size = (device->num_joysticks - i - 1) * sizeof(SDL_JoystickID);
  564. SDL_memmove(&device->joysticks[i], &device->joysticks[i+1], size);
  565. --device->num_joysticks;
  566. --SDL_HIDAPI_numjoysticks;
  567. if (device->num_joysticks == 0) {
  568. SDL_free(device->joysticks);
  569. device->joysticks = NULL;
  570. }
  571. if (!shutting_down) {
  572. SDL_PrivateJoystickRemoved(joystickID);
  573. }
  574. return;
  575. }
  576. }
  577. }
  578. static int
  579. HIDAPI_JoystickGetCount(void)
  580. {
  581. return SDL_HIDAPI_numjoysticks;
  582. }
  583. static char *
  584. HIDAPI_ConvertString(const wchar_t *wide_string)
  585. {
  586. char *string = NULL;
  587. if (wide_string) {
  588. string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
  589. if (!string) {
  590. if (sizeof(wchar_t) == sizeof(Uint16)) {
  591. string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
  592. } else if (sizeof(wchar_t) == sizeof(Uint32)) {
  593. string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
  594. }
  595. }
  596. }
  597. return string;
  598. }
  599. static void
  600. HIDAPI_AddDevice(struct hid_device_info *info)
  601. {
  602. SDL_HIDAPI_Device *device;
  603. SDL_HIDAPI_Device *curr, *last = NULL;
  604. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  605. continue;
  606. }
  607. device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
  608. if (!device) {
  609. return;
  610. }
  611. device->path = SDL_strdup(info->path);
  612. if (!device->path) {
  613. SDL_free(device);
  614. return;
  615. }
  616. device->seen = SDL_TRUE;
  617. device->vendor_id = info->vendor_id;
  618. device->product_id = info->product_id;
  619. device->version = info->release_number;
  620. device->interface_number = info->interface_number;
  621. device->interface_class = info->interface_class;
  622. device->interface_subclass = info->interface_subclass;
  623. device->interface_protocol = info->interface_protocol;
  624. device->usage_page = info->usage_page;
  625. device->usage = info->usage;
  626. {
  627. /* FIXME: Is there any way to tell whether this is a Bluetooth device? */
  628. const Uint16 vendor = device->vendor_id;
  629. const Uint16 product = device->product_id;
  630. const Uint16 version = device->version;
  631. Uint16 *guid16 = (Uint16 *)device->guid.data;
  632. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
  633. *guid16++ = 0;
  634. *guid16++ = SDL_SwapLE16(vendor);
  635. *guid16++ = 0;
  636. *guid16++ = SDL_SwapLE16(product);
  637. *guid16++ = 0;
  638. *guid16++ = SDL_SwapLE16(version);
  639. *guid16++ = 0;
  640. /* Note that this is a HIDAPI device for special handling elsewhere */
  641. device->guid.data[14] = 'h';
  642. device->guid.data[15] = 0;
  643. }
  644. device->dev_lock = SDL_CreateMutex();
  645. /* Need the device name before getting the driver to know whether to ignore this device */
  646. {
  647. char *manufacturer_string = HIDAPI_ConvertString(info->manufacturer_string);
  648. char *product_string = HIDAPI_ConvertString(info->product_string);
  649. char *serial_number = HIDAPI_ConvertString(info->serial_number);
  650. device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string);
  651. if (SDL_strncmp(device->name, "0x", 2) == 0) {
  652. /* Couldn't find a controller name, try to give it one based on device type */
  653. const char *name = NULL;
  654. switch (SDL_GetJoystickGameControllerType(NULL, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol)) {
  655. case SDL_CONTROLLER_TYPE_XBOX360:
  656. name = "Xbox 360 Controller";
  657. break;
  658. case SDL_CONTROLLER_TYPE_XBOXONE:
  659. name = "Xbox One Controller";
  660. break;
  661. case SDL_CONTROLLER_TYPE_PS3:
  662. name = "PS3 Controller";
  663. break;
  664. case SDL_CONTROLLER_TYPE_PS4:
  665. name = "PS4 Controller";
  666. break;
  667. case SDL_CONTROLLER_TYPE_PS5:
  668. name = "PS5 Controller";
  669. break;
  670. case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO:
  671. name = "Nintendo Switch Pro Controller";
  672. break;
  673. default:
  674. break;
  675. }
  676. if (name) {
  677. SDL_free(device->name);
  678. device->name = SDL_strdup(name);
  679. }
  680. }
  681. if (manufacturer_string) {
  682. SDL_free(manufacturer_string);
  683. }
  684. if (product_string) {
  685. SDL_free(product_string);
  686. }
  687. if (serial_number && *serial_number) {
  688. device->serial = serial_number;
  689. } else {
  690. SDL_free(serial_number);
  691. }
  692. if (!device->name) {
  693. SDL_free(device->serial);
  694. SDL_free(device->path);
  695. SDL_free(device);
  696. return;
  697. }
  698. }
  699. /* Add it to the list */
  700. if (last) {
  701. last->next = device;
  702. } else {
  703. SDL_HIDAPI_devices = device;
  704. }
  705. HIDAPI_SetupDeviceDriver(device);
  706. #ifdef DEBUG_HIDAPI
  707. SDL_Log("Added HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, serial %s, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->hint : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
  708. #endif
  709. }
  710. static void
  711. HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
  712. {
  713. SDL_HIDAPI_Device *curr, *last;
  714. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  715. if (curr == device) {
  716. if (last) {
  717. last->next = curr->next;
  718. } else {
  719. SDL_HIDAPI_devices = curr->next;
  720. }
  721. HIDAPI_CleanupDeviceDriver(device);
  722. SDL_DestroyMutex(device->dev_lock);
  723. SDL_free(device->serial);
  724. SDL_free(device->name);
  725. SDL_free(device->path);
  726. SDL_free(device);
  727. return;
  728. }
  729. }
  730. }
  731. static void
  732. HIDAPI_UpdateDeviceList(void)
  733. {
  734. SDL_HIDAPI_Device *device;
  735. struct hid_device_info *devs, *info;
  736. SDL_LockJoysticks();
  737. /* Prepare the existing device list */
  738. device = SDL_HIDAPI_devices;
  739. while (device) {
  740. device->seen = SDL_FALSE;
  741. device = device->next;
  742. }
  743. /* Enumerate the devices */
  744. if (SDL_HIDAPI_numdrivers > 0) {
  745. devs = hid_enumerate(0, 0);
  746. if (devs) {
  747. for (info = devs; info; info = info->next) {
  748. device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
  749. if (device) {
  750. device->seen = SDL_TRUE;
  751. } else {
  752. HIDAPI_AddDevice(info);
  753. }
  754. }
  755. hid_free_enumeration(devs);
  756. }
  757. }
  758. /* Remove any devices that weren't seen */
  759. device = SDL_HIDAPI_devices;
  760. while (device) {
  761. SDL_HIDAPI_Device *next = device->next;
  762. if (!device->seen) {
  763. HIDAPI_DelDevice(device);
  764. }
  765. device = next;
  766. }
  767. SDL_UnlockJoysticks();
  768. }
  769. static SDL_bool
  770. HIDAPI_IsEquivalentToDevice(Uint16 vendor_id, Uint16 product_id, SDL_HIDAPI_Device *device)
  771. {
  772. if (vendor_id == device->vendor_id && product_id == device->product_id) {
  773. return SDL_TRUE;
  774. }
  775. if (vendor_id == USB_VENDOR_MICROSOFT) {
  776. /* If we're looking for the wireless XBox 360 controller, also look for the dongle */
  777. if (product_id == 0x02a1 && device->product_id == 0x0719) {
  778. return SDL_TRUE;
  779. }
  780. /* If we're looking for the raw input Xbox One controller, match it against any other Xbox One controller */
  781. if (product_id == USB_PRODUCT_XBOX_ONE_RAW_INPUT_CONTROLLER &&
  782. SDL_GetJoystickGameControllerType(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol) == SDL_CONTROLLER_TYPE_XBOXONE) {
  783. return SDL_TRUE;
  784. }
  785. /* If we're looking for an XInput controller, match it against any other Xbox controller */
  786. if (product_id == USB_PRODUCT_XBOX_ONE_XINPUT_CONTROLLER) {
  787. SDL_GameControllerType type = SDL_GetJoystickGameControllerType(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
  788. if (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE) {
  789. return SDL_TRUE;
  790. }
  791. }
  792. }
  793. return SDL_FALSE;
  794. }
  795. SDL_bool
  796. HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  797. {
  798. SDL_HIDAPI_Device *device;
  799. SDL_bool supported = SDL_FALSE;
  800. SDL_bool result = SDL_FALSE;
  801. /* Make sure we're initialized, as this could be called from other drivers during startup */
  802. if (HIDAPI_JoystickInit() < 0) {
  803. return SDL_FALSE;
  804. }
  805. /* Only update the device list for devices we know might be supported.
  806. If we did this for every device, it would hit the USB driver too hard and potentially
  807. lock up the system. This won't catch devices that we support but can only detect using
  808. USB interface details, like Xbox controllers, but hopefully the device list update is
  809. responsive enough to catch those.
  810. */
  811. supported = HIDAPI_IsDeviceSupported(vendor_id, product_id, version, name);
  812. #if defined(SDL_JOYSTICK_HIDAPI_XBOX360) || defined(SDL_JOYSTICK_HIDAPI_XBOXONE)
  813. if (!supported &&
  814. (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box") || SDL_strstr(name, "XBOX"))) {
  815. supported = SDL_TRUE;
  816. }
  817. #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 || SDL_JOYSTICK_HIDAPI_XBOXONE */
  818. if (supported) {
  819. if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
  820. HIDAPI_UpdateDeviceList();
  821. SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
  822. }
  823. }
  824. /* Note that this isn't a perfect check - there may be multiple devices with 0 VID/PID,
  825. or a different name than we have it listed here, etc, but if we support the device
  826. and we have something similar in our device list, mark it as present.
  827. */
  828. SDL_LockJoysticks();
  829. device = SDL_HIDAPI_devices;
  830. while (device) {
  831. if (device->driver &&
  832. HIDAPI_IsEquivalentToDevice(vendor_id, product_id, device)) {
  833. result = SDL_TRUE;
  834. }
  835. device = device->next;
  836. }
  837. SDL_UnlockJoysticks();
  838. #ifdef DEBUG_HIDAPI
  839. SDL_Log("HIDAPI_IsDevicePresent() returning %s for 0x%.4x / 0x%.4x\n", result ? "true" : "false", vendor_id, product_id);
  840. #endif
  841. return result;
  842. }
  843. static void
  844. HIDAPI_JoystickDetect(void)
  845. {
  846. if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
  847. HIDAPI_UpdateDiscovery();
  848. if (SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
  849. /* FIXME: We probably need to schedule an update in a few seconds as well */
  850. HIDAPI_UpdateDeviceList();
  851. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_FALSE;
  852. }
  853. SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
  854. }
  855. }
  856. void
  857. HIDAPI_UpdateDevices(void)
  858. {
  859. SDL_HIDAPI_Device *device;
  860. /* Update the devices, which may change connected joysticks and send events */
  861. /* Prepare the existing device list */
  862. if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
  863. device = SDL_HIDAPI_devices;
  864. while (device) {
  865. if (device->driver) {
  866. if (SDL_TryLockMutex(device->dev_lock) == 0) {
  867. device->driver->UpdateDevice(device);
  868. SDL_UnlockMutex(device->dev_lock);
  869. }
  870. }
  871. device = device->next;
  872. }
  873. SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
  874. }
  875. }
  876. static const char *
  877. HIDAPI_JoystickGetDeviceName(int device_index)
  878. {
  879. SDL_HIDAPI_Device *device;
  880. const char *name = NULL;
  881. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  882. if (device) {
  883. /* FIXME: The device could be freed after this name is returned... */
  884. name = device->name;
  885. }
  886. return name;
  887. }
  888. static int
  889. HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
  890. {
  891. SDL_HIDAPI_Device *device;
  892. SDL_JoystickID instance_id;
  893. int player_index = -1;
  894. device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
  895. if (device) {
  896. player_index = device->driver->GetDevicePlayerIndex(device, instance_id);
  897. }
  898. return player_index;
  899. }
  900. static void
  901. HIDAPI_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  902. {
  903. SDL_HIDAPI_Device *device;
  904. SDL_JoystickID instance_id;
  905. device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
  906. if (device) {
  907. device->driver->SetDevicePlayerIndex(device, instance_id, player_index);
  908. }
  909. }
  910. static SDL_JoystickGUID
  911. HIDAPI_JoystickGetDeviceGUID(int device_index)
  912. {
  913. SDL_HIDAPI_Device *device;
  914. SDL_JoystickGUID guid;
  915. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  916. if (device) {
  917. SDL_memcpy(&guid, &device->guid, sizeof(guid));
  918. } else {
  919. SDL_zero(guid);
  920. }
  921. return guid;
  922. }
  923. static SDL_JoystickID
  924. HIDAPI_JoystickGetDeviceInstanceID(int device_index)
  925. {
  926. SDL_JoystickID joystickID = -1;
  927. HIDAPI_GetDeviceByIndex(device_index, &joystickID);
  928. return joystickID;
  929. }
  930. static int
  931. HIDAPI_JoystickOpen(SDL_Joystick * joystick, int device_index)
  932. {
  933. SDL_JoystickID joystickID;
  934. SDL_HIDAPI_Device *device = HIDAPI_GetDeviceByIndex(device_index, &joystickID);
  935. struct joystick_hwdata *hwdata;
  936. hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
  937. if (!hwdata) {
  938. return SDL_OutOfMemory();
  939. }
  940. hwdata->device = device;
  941. if (!device->driver->OpenJoystick(device, joystick)) {
  942. SDL_free(hwdata);
  943. return -1;
  944. }
  945. if (!joystick->serial && device->serial) {
  946. joystick->serial = SDL_strdup(device->serial);
  947. }
  948. joystick->hwdata = hwdata;
  949. return 0;
  950. }
  951. static int
  952. HIDAPI_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  953. {
  954. int result;
  955. if (joystick->hwdata) {
  956. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  957. result = device->driver->RumbleJoystick(device, joystick, low_frequency_rumble, high_frequency_rumble);
  958. } else {
  959. SDL_SetError("Rumble failed, device disconnected");
  960. result = -1;
  961. }
  962. return result;
  963. }
  964. static int
  965. HIDAPI_JoystickRumbleTriggers(SDL_Joystick * joystick, Uint16 left_rumble, Uint16 right_rumble)
  966. {
  967. int result;
  968. if (joystick->hwdata) {
  969. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  970. result = device->driver->RumbleJoystickTriggers(device, joystick, left_rumble, right_rumble);
  971. } else {
  972. SDL_SetError("Rumble failed, device disconnected");
  973. result = -1;
  974. }
  975. return result;
  976. }
  977. static SDL_bool
  978. HIDAPI_JoystickHasLED(SDL_Joystick * joystick)
  979. {
  980. SDL_bool result = SDL_FALSE;
  981. if (joystick->hwdata) {
  982. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  983. result = device->driver->HasJoystickLED(device, joystick);
  984. }
  985. return result;
  986. }
  987. static int
  988. HIDAPI_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
  989. {
  990. int result;
  991. if (joystick->hwdata) {
  992. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  993. result = device->driver->SetJoystickLED(device, joystick, red, green, blue);
  994. } else {
  995. SDL_SetError("SetLED failed, device disconnected");
  996. result = -1;
  997. }
  998. return result;
  999. }
  1000. static int
  1001. HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick * joystick, SDL_bool enabled)
  1002. {
  1003. int result;
  1004. if (joystick->hwdata) {
  1005. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1006. result = device->driver->SetJoystickSensorsEnabled(device, joystick, enabled);
  1007. } else {
  1008. SDL_SetError("SetSensorsEnabled failed, device disconnected");
  1009. result = -1;
  1010. }
  1011. return result;
  1012. }
  1013. static void
  1014. HIDAPI_JoystickUpdate(SDL_Joystick * joystick)
  1015. {
  1016. /* This is handled in SDL_HIDAPI_UpdateDevices() */
  1017. }
  1018. static void
  1019. HIDAPI_JoystickClose(SDL_Joystick * joystick)
  1020. {
  1021. if (joystick->hwdata) {
  1022. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1023. /* Wait for pending rumble to complete */
  1024. while (SDL_AtomicGet(&device->rumble_pending) > 0) {
  1025. SDL_Delay(10);
  1026. }
  1027. device->driver->CloseJoystick(device, joystick);
  1028. SDL_free(joystick->hwdata);
  1029. joystick->hwdata = NULL;
  1030. }
  1031. }
  1032. static void
  1033. HIDAPI_JoystickQuit(void)
  1034. {
  1035. int i;
  1036. shutting_down = SDL_TRUE;
  1037. HIDAPI_ShutdownDiscovery();
  1038. while (SDL_HIDAPI_devices) {
  1039. HIDAPI_DelDevice(SDL_HIDAPI_devices);
  1040. }
  1041. SDL_HIDAPI_QuitRumble();
  1042. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  1043. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  1044. SDL_DelHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  1045. }
  1046. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  1047. SDL_HIDAPIDriverHintChanged, NULL);
  1048. hid_exit();
  1049. /* Make sure the drivers cleaned up properly */
  1050. SDL_assert(SDL_HIDAPI_numjoysticks == 0);
  1051. shutting_down = SDL_FALSE;
  1052. initialized = SDL_FALSE;
  1053. }
  1054. static SDL_bool
  1055. HIDAPI_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1056. {
  1057. return SDL_FALSE;
  1058. }
  1059. SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
  1060. {
  1061. HIDAPI_JoystickInit,
  1062. HIDAPI_JoystickGetCount,
  1063. HIDAPI_JoystickDetect,
  1064. HIDAPI_JoystickGetDeviceName,
  1065. HIDAPI_JoystickGetDevicePlayerIndex,
  1066. HIDAPI_JoystickSetDevicePlayerIndex,
  1067. HIDAPI_JoystickGetDeviceGUID,
  1068. HIDAPI_JoystickGetDeviceInstanceID,
  1069. HIDAPI_JoystickOpen,
  1070. HIDAPI_JoystickRumble,
  1071. HIDAPI_JoystickRumbleTriggers,
  1072. HIDAPI_JoystickHasLED,
  1073. HIDAPI_JoystickSetLED,
  1074. HIDAPI_JoystickSetSensorsEnabled,
  1075. HIDAPI_JoystickUpdate,
  1076. HIDAPI_JoystickClose,
  1077. HIDAPI_JoystickQuit,
  1078. HIDAPI_JoystickGetGamepadMapping
  1079. };
  1080. #endif /* SDL_JOYSTICK_HIDAPI */
  1081. /* vi: set ts=4 sw=4 expandtab: */