SDL_hidapijoystick.c 36 KB

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