SDL_hidapijoystick.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 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_endian.h"
  21. #include "SDL_hints.h"
  22. #include "SDL_log.h"
  23. #include "SDL_mutex.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_hints_c.h"
  30. #if defined(__WIN32__)
  31. #include "../../core/windows/SDL_windows.h"
  32. #endif
  33. #if defined(__MACOSX__)
  34. #include <CoreFoundation/CoreFoundation.h>
  35. #include <mach/mach.h>
  36. #include <IOKit/IOKitLib.h>
  37. #include <IOKit/usb/USBSpec.h>
  38. #endif
  39. #if defined(__LINUX__)
  40. #include "../../core/linux/SDL_udev.h"
  41. #ifdef SDL_USE_LIBUDEV
  42. #include <poll.h>
  43. #endif
  44. #endif
  45. struct joystick_hwdata
  46. {
  47. SDL_HIDAPI_DeviceDriver *driver;
  48. void *context;
  49. SDL_mutex *mutex;
  50. hid_device *dev;
  51. };
  52. typedef struct _SDL_HIDAPI_Device
  53. {
  54. SDL_JoystickID instance_id;
  55. char *name;
  56. char *path;
  57. Uint16 vendor_id;
  58. Uint16 product_id;
  59. Uint16 version;
  60. SDL_JoystickGUID guid;
  61. int interface_number; /* Available on Windows and Linux */
  62. Uint16 usage_page; /* Available on Windows and Mac OS X */
  63. Uint16 usage; /* Available on Windows and Mac OS X */
  64. SDL_HIDAPI_DeviceDriver *driver;
  65. /* Used during scanning for device changes */
  66. SDL_bool seen;
  67. struct _SDL_HIDAPI_Device *next;
  68. } SDL_HIDAPI_Device;
  69. static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
  70. #ifdef SDL_JOYSTICK_HIDAPI_PS4
  71. &SDL_HIDAPI_DriverPS4,
  72. #endif
  73. #ifdef SDL_JOYSTICK_HIDAPI_STEAM
  74. &SDL_HIDAPI_DriverSteam,
  75. #endif
  76. #ifdef SDL_JOYSTICK_HIDAPI_SWITCH
  77. &SDL_HIDAPI_DriverSwitch,
  78. #endif
  79. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  80. &SDL_HIDAPI_DriverXbox360,
  81. #endif
  82. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  83. &SDL_HIDAPI_DriverXboxOne,
  84. #endif
  85. };
  86. static int SDL_HIDAPI_numdrivers = 0;
  87. static SDL_HIDAPI_Device *SDL_HIDAPI_devices;
  88. static int SDL_HIDAPI_numjoysticks = 0;
  89. static SDL_bool initialized = SDL_FALSE;
  90. #if defined(SDL_USE_LIBUDEV)
  91. static const SDL_UDEV_Symbols * usyms = NULL;
  92. #endif
  93. static struct
  94. {
  95. SDL_bool m_bHaveDevicesChanged;
  96. SDL_bool m_bCanGetNotifications;
  97. Uint32 m_unLastDetect;
  98. #if defined(__WIN32__)
  99. SDL_threadID m_nThreadID;
  100. WNDCLASSEXA m_wndClass;
  101. HWND m_hwndMsg;
  102. HDEVNOTIFY m_hNotify;
  103. double m_flLastWin32MessageCheck;
  104. #endif
  105. #if defined(__MACOSX__)
  106. IONotificationPortRef m_notificationPort;
  107. mach_port_t m_notificationMach;
  108. #endif
  109. #if defined(SDL_USE_LIBUDEV)
  110. struct udev *m_pUdev;
  111. struct udev_monitor *m_pUdevMonitor;
  112. int m_nUdevFd;
  113. #endif
  114. } SDL_HIDAPI_discovery;
  115. #ifdef __WIN32__
  116. struct _DEV_BROADCAST_HDR
  117. {
  118. DWORD dbch_size;
  119. DWORD dbch_devicetype;
  120. DWORD dbch_reserved;
  121. };
  122. typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
  123. {
  124. DWORD dbcc_size;
  125. DWORD dbcc_devicetype;
  126. DWORD dbcc_reserved;
  127. GUID dbcc_classguid;
  128. char dbcc_name[ 1 ];
  129. } DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;
  130. typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
  131. #define DBT_DEVICEARRIVAL 0x8000 /* system detected a new device */
  132. #define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
  133. #define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
  134. #define DBT_DEVNODES_CHANGED 0x0007
  135. #define DBT_CONFIGCHANGED 0x0018
  136. #define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
  137. #define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
  138. #include <initguid.h>
  139. DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
  140. static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  141. {
  142. switch (message) {
  143. case WM_DEVICECHANGE:
  144. switch (wParam) {
  145. case DBT_DEVICEARRIVAL:
  146. case DBT_DEVICEREMOVECOMPLETE:
  147. if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  148. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  149. }
  150. break;
  151. }
  152. return TRUE;
  153. }
  154. return DefWindowProc(hwnd, message, wParam, lParam);
  155. }
  156. #endif /* __WIN32__ */
  157. #if defined(__MACOSX__)
  158. static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
  159. {
  160. /* Must drain the iterator, or we won't receive new notifications */
  161. io_object_t entry;
  162. while ((entry = IOIteratorNext(portIterator)) != 0) {
  163. IOObjectRelease(entry);
  164. *(SDL_bool*)context = SDL_TRUE;
  165. }
  166. }
  167. #endif /* __MACOSX__ */
  168. static void
  169. HIDAPI_InitializeDiscovery()
  170. {
  171. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  172. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
  173. SDL_HIDAPI_discovery.m_unLastDetect = 0;
  174. #if defined(__WIN32__)
  175. SDL_HIDAPI_discovery.m_nThreadID = SDL_ThreadID();
  176. SDL_memset(&SDL_HIDAPI_discovery.m_wndClass, 0x0, sizeof(SDL_HIDAPI_discovery.m_wndClass));
  177. SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
  178. SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
  179. SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; /* This function is called by windows */
  180. SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);
  181. RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
  182. SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  183. {
  184. DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;
  185. SDL_memset( &devBroadcast, 0x0, sizeof( devBroadcast ) );
  186. devBroadcast.dbcc_size = sizeof( devBroadcast );
  187. devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  188. devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
  189. /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
  190. * but that seems to be necessary to get a notice after each individual usb input device actually
  191. * installs, rather than just as the composite device is seen.
  192. */
  193. SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification( SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
  194. SDL_HIDAPI_discovery.m_bCanGetNotifications = ( SDL_HIDAPI_discovery.m_hNotify != 0 );
  195. }
  196. #endif /* __WIN32__ */
  197. #if defined(__MACOSX__)
  198. SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
  199. if (SDL_HIDAPI_discovery.m_notificationPort) {
  200. {
  201. CFMutableDictionaryRef matchingDict = IOServiceMatching("IOUSBDevice");
  202. /* Note: IOServiceAddMatchingNotification consumes the reference to matchingDict */
  203. io_iterator_t portIterator = 0;
  204. io_object_t entry;
  205. if (IOServiceAddMatchingNotification(SDL_HIDAPI_discovery.m_notificationPort, kIOMatchedNotification, matchingDict, CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator) == 0) {
  206. /* Must drain the existing iterator, or we won't receive new notifications */
  207. while ((entry = IOIteratorNext(portIterator)) != 0) {
  208. IOObjectRelease(entry);
  209. }
  210. } else {
  211. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  212. SDL_HIDAPI_discovery.m_notificationPort = nil;
  213. }
  214. }
  215. {
  216. CFMutableDictionaryRef matchingDict = IOServiceMatching("IOBluetoothDevice");
  217. /* Note: IOServiceAddMatchingNotification consumes the reference to matchingDict */
  218. io_iterator_t portIterator = 0;
  219. io_object_t entry;
  220. if (IOServiceAddMatchingNotification(SDL_HIDAPI_discovery.m_notificationPort, kIOMatchedNotification, matchingDict, CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator) == 0) {
  221. /* Must drain the existing iterator, or we won't receive new notifications */
  222. while ((entry = IOIteratorNext(portIterator)) != 0) {
  223. IOObjectRelease(entry);
  224. }
  225. } else {
  226. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  227. SDL_HIDAPI_discovery.m_notificationPort = nil;
  228. }
  229. }
  230. }
  231. SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
  232. if (SDL_HIDAPI_discovery.m_notificationPort) {
  233. SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
  234. }
  235. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);
  236. #endif // __MACOSX__
  237. #if defined(SDL_USE_LIBUDEV)
  238. SDL_HIDAPI_discovery.m_pUdev = NULL;
  239. SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
  240. SDL_HIDAPI_discovery.m_nUdevFd = -1;
  241. usyms = SDL_UDEV_GetUdevSyms();
  242. if (usyms) {
  243. SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
  244. }
  245. if (SDL_HIDAPI_discovery.m_pUdev) {
  246. SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
  247. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  248. usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
  249. SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
  250. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
  251. }
  252. }
  253. #endif /* SDL_USE_LIBUDEV */
  254. }
  255. static void
  256. HIDAPI_UpdateDiscovery()
  257. {
  258. if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) {
  259. const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
  260. Uint32 now = SDL_GetTicks();
  261. if (!SDL_HIDAPI_discovery.m_unLastDetect || SDL_TICKS_PASSED(now, SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) {
  262. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  263. SDL_HIDAPI_discovery.m_unLastDetect = now;
  264. }
  265. return;
  266. }
  267. #if defined(__WIN32__)
  268. #if 0 /* just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan. */
  269. /* We'll only get messages on the same thread that created the window */
  270. if (SDL_ThreadID() == SDL_HIDAPI_discovery.m_nThreadID) {
  271. MSG msg;
  272. while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) {
  273. if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) {
  274. TranslateMessage(&msg);
  275. DispatchMessage(&msg);
  276. }
  277. }
  278. }
  279. #endif
  280. #endif /* __WIN32__ */
  281. #if defined(__MACOSX__)
  282. if (SDL_HIDAPI_discovery.m_notificationPort) {
  283. struct { mach_msg_header_t hdr; char payload[ 4096 ]; } msg;
  284. 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) {
  285. IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);
  286. }
  287. }
  288. #endif
  289. #if defined(SDL_USE_LIBUDEV)
  290. if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
  291. /* Drain all notification events.
  292. * We don't expect a lot of device notifications so just
  293. * do a new discovery on any kind or number of notifications.
  294. * This could be made more restrictive if necessary.
  295. */
  296. for (;;) {
  297. struct pollfd PollUdev;
  298. struct udev_device *pUdevDevice;
  299. PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
  300. PollUdev.events = POLLIN;
  301. if (poll(&PollUdev, 1, 0) != 1) {
  302. break;
  303. }
  304. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  305. pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
  306. if (pUdevDevice) {
  307. usyms->udev_device_unref(pUdevDevice);
  308. }
  309. }
  310. }
  311. #endif
  312. }
  313. static void
  314. HIDAPI_ShutdownDiscovery()
  315. {
  316. #if defined(__WIN32__)
  317. if (SDL_HIDAPI_discovery.m_hNotify)
  318. UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
  319. if (SDL_HIDAPI_discovery.m_hwndMsg) {
  320. DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
  321. }
  322. UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
  323. #endif
  324. #if defined(__MACOSX__)
  325. if (SDL_HIDAPI_discovery.m_notificationPort) {
  326. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  327. }
  328. #endif
  329. #if defined(SDL_USE_LIBUDEV)
  330. if (usyms) {
  331. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  332. usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
  333. }
  334. if (SDL_HIDAPI_discovery.m_pUdev) {
  335. usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
  336. }
  337. SDL_UDEV_ReleaseUdevSyms();
  338. usyms = NULL;
  339. }
  340. #endif
  341. }
  342. static SDL_bool
  343. HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  344. {
  345. int i;
  346. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  347. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  348. if (driver->enabled && driver->IsSupportedDevice(vendor_id, product_id, version, -1, name)) {
  349. return SDL_TRUE;
  350. }
  351. }
  352. return SDL_FALSE;
  353. }
  354. static SDL_HIDAPI_DeviceDriver *
  355. HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
  356. {
  357. const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
  358. const Uint16 USAGE_JOYSTICK = 0x0004;
  359. const Uint16 USAGE_GAMEPAD = 0x0005;
  360. const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
  361. int i;
  362. if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
  363. return NULL;
  364. }
  365. if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
  366. return NULL;
  367. }
  368. if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
  369. return NULL;
  370. }
  371. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  372. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  373. if (driver->enabled && driver->IsSupportedDevice(device->vendor_id, device->product_id, device->version, device->interface_number, device->name)) {
  374. return driver;
  375. }
  376. }
  377. return NULL;
  378. }
  379. static SDL_HIDAPI_Device *
  380. HIDAPI_GetJoystickByIndex(int device_index)
  381. {
  382. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  383. while (device) {
  384. if (device->driver) {
  385. if (device_index == 0) {
  386. break;
  387. }
  388. --device_index;
  389. }
  390. device = device->next;
  391. }
  392. return device;
  393. }
  394. static SDL_HIDAPI_Device *
  395. HIDAPI_GetJoystickByInfo(const char *path, Uint16 vendor_id, Uint16 product_id)
  396. {
  397. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  398. while (device) {
  399. if (device->vendor_id == vendor_id && device->product_id == product_id &&
  400. SDL_strcmp(device->path, path) == 0) {
  401. break;
  402. }
  403. device = device->next;
  404. }
  405. return device;
  406. }
  407. static void SDLCALL
  408. SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  409. {
  410. int i;
  411. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  412. SDL_bool enabled = SDL_GetStringBoolean(hint, SDL_TRUE);
  413. if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI) == 0) {
  414. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  415. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  416. driver->enabled = SDL_GetHintBoolean(driver->hint, enabled);
  417. }
  418. } else {
  419. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  420. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  421. if (SDL_strcmp(name, driver->hint) == 0) {
  422. driver->enabled = enabled;
  423. break;
  424. }
  425. }
  426. }
  427. SDL_HIDAPI_numdrivers = 0;
  428. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  429. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  430. if (driver->enabled) {
  431. ++SDL_HIDAPI_numdrivers;
  432. }
  433. }
  434. /* Update device list if driver availability changes */
  435. while (device) {
  436. if (device->driver) {
  437. if (!device->driver->enabled) {
  438. device->driver = NULL;
  439. --SDL_HIDAPI_numjoysticks;
  440. SDL_PrivateJoystickRemoved(device->instance_id);
  441. }
  442. } else {
  443. device->driver = HIDAPI_GetDeviceDriver(device);
  444. if (device->driver) {
  445. device->instance_id = SDL_GetNextJoystickInstanceID();
  446. ++SDL_HIDAPI_numjoysticks;
  447. SDL_PrivateJoystickAdded(device->instance_id);
  448. }
  449. }
  450. device = device->next;
  451. }
  452. }
  453. static void HIDAPI_JoystickDetect(void);
  454. static int
  455. HIDAPI_JoystickInit(void)
  456. {
  457. int i;
  458. if (initialized) {
  459. return 0;
  460. }
  461. if (hid_init() < 0) {
  462. SDL_SetError("Couldn't initialize hidapi");
  463. return -1;
  464. }
  465. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  466. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  467. SDL_AddHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  468. }
  469. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  470. SDL_HIDAPIDriverHintChanged, NULL);
  471. HIDAPI_InitializeDiscovery();
  472. HIDAPI_JoystickDetect();
  473. initialized = SDL_TRUE;
  474. return 0;
  475. }
  476. static int
  477. HIDAPI_JoystickGetCount(void)
  478. {
  479. return SDL_HIDAPI_numjoysticks;
  480. }
  481. static void
  482. HIDAPI_AddDevice(struct hid_device_info *info)
  483. {
  484. SDL_HIDAPI_Device *device;
  485. SDL_HIDAPI_Device *curr, *last = NULL;
  486. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  487. continue;
  488. }
  489. device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
  490. if (!device) {
  491. return;
  492. }
  493. device->instance_id = -1;
  494. device->seen = SDL_TRUE;
  495. device->vendor_id = info->vendor_id;
  496. device->product_id = info->product_id;
  497. device->version = info->release_number;
  498. device->interface_number = info->interface_number;
  499. device->usage_page = info->usage_page;
  500. device->usage = info->usage;
  501. {
  502. /* FIXME: Is there any way to tell whether this is a Bluetooth device? */
  503. const Uint16 vendor = device->vendor_id;
  504. const Uint16 product = device->product_id;
  505. const Uint16 version = device->version;
  506. Uint16 *guid16 = (Uint16 *)device->guid.data;
  507. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
  508. *guid16++ = 0;
  509. *guid16++ = SDL_SwapLE16(vendor);
  510. *guid16++ = 0;
  511. *guid16++ = SDL_SwapLE16(product);
  512. *guid16++ = 0;
  513. *guid16++ = SDL_SwapLE16(version);
  514. *guid16++ = 0;
  515. /* Note that this is a HIDAPI device for special handling elsewhere */
  516. device->guid.data[14] = 'h';
  517. device->guid.data[15] = 0;
  518. }
  519. /* Need the device name before getting the driver to know whether to ignore this device */
  520. if (!device->name) {
  521. const char *name = SDL_GetCustomJoystickName(device->vendor_id, device->product_id);
  522. if (name) {
  523. device->name = SDL_strdup(name);
  524. }
  525. }
  526. if (!device->name && info->manufacturer_string && info->product_string) {
  527. const char *manufacturer_remapped;
  528. char *manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
  529. char *product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
  530. if (!manufacturer_string && !product_string) {
  531. if (sizeof(wchar_t) == sizeof(Uint16)) {
  532. manufacturer_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
  533. product_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
  534. } else if (sizeof(wchar_t) == sizeof(Uint32)) {
  535. manufacturer_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
  536. product_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
  537. }
  538. }
  539. manufacturer_remapped = SDL_GetCustomJoystickManufacturer(manufacturer_string);
  540. if (manufacturer_remapped != manufacturer_string) {
  541. SDL_free(manufacturer_string);
  542. manufacturer_string = SDL_strdup(manufacturer_remapped);
  543. }
  544. if (manufacturer_string && product_string) {
  545. size_t name_size = (SDL_strlen(manufacturer_string) + 1 + SDL_strlen(product_string) + 1);
  546. device->name = (char *)SDL_malloc(name_size);
  547. if (device->name) {
  548. if (SDL_strncasecmp(manufacturer_string, product_string, SDL_strlen(manufacturer_string)) == 0) {
  549. SDL_strlcpy(device->name, product_string, name_size);
  550. } else {
  551. SDL_snprintf(device->name, name_size, "%s %s", manufacturer_string, product_string);
  552. }
  553. }
  554. }
  555. if (manufacturer_string) {
  556. SDL_free(manufacturer_string);
  557. }
  558. if (product_string) {
  559. SDL_free(product_string);
  560. }
  561. }
  562. if (!device->name) {
  563. size_t name_size = (6 + 1 + 6 + 1);
  564. device->name = (char *)SDL_malloc(name_size);
  565. if (!device->name) {
  566. SDL_free(device);
  567. return;
  568. }
  569. SDL_snprintf(device->name, name_size, "0x%.4x/0x%.4x", info->vendor_id, info->product_id);
  570. }
  571. device->driver = HIDAPI_GetDeviceDriver(device);
  572. if (device->driver) {
  573. const char *name = device->driver->GetDeviceName(device->vendor_id, device->product_id);
  574. if (name) {
  575. SDL_free(device->name);
  576. device->name = SDL_strdup(name);
  577. }
  578. }
  579. device->path = SDL_strdup(info->path);
  580. if (!device->path) {
  581. SDL_free(device->name);
  582. SDL_free(device);
  583. return;
  584. }
  585. #ifdef DEBUG_HIDAPI
  586. SDL_Log("Adding HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, interface %d, usage page 0x%.4x, usage 0x%.4x, driver = %s\n", device->name, device->vendor_id, device->product_id, device->version, device->interface_number, device->usage_page, device->usage, device->driver ? device->driver->hint : "NONE");
  587. #endif
  588. /* Add it to the list */
  589. if (last) {
  590. last->next = device;
  591. } else {
  592. SDL_HIDAPI_devices = device;
  593. }
  594. if (device->driver) {
  595. /* It's a joystick! */
  596. device->instance_id = SDL_GetNextJoystickInstanceID();
  597. ++SDL_HIDAPI_numjoysticks;
  598. SDL_PrivateJoystickAdded(device->instance_id);
  599. }
  600. }
  601. static void
  602. HIDAPI_DelDevice(SDL_HIDAPI_Device *device, SDL_bool send_event)
  603. {
  604. SDL_HIDAPI_Device *curr, *last;
  605. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  606. if (curr == device) {
  607. if (last) {
  608. last->next = curr->next;
  609. } else {
  610. SDL_HIDAPI_devices = curr->next;
  611. }
  612. if (device->driver && send_event) {
  613. /* Need to decrement the joystick count before we post the event */
  614. --SDL_HIDAPI_numjoysticks;
  615. SDL_PrivateJoystickRemoved(device->instance_id);
  616. }
  617. SDL_free(device->name);
  618. SDL_free(device->path);
  619. SDL_free(device);
  620. return;
  621. }
  622. }
  623. }
  624. static void
  625. HIDAPI_UpdateDeviceList(void)
  626. {
  627. SDL_HIDAPI_Device *device;
  628. struct hid_device_info *devs, *info;
  629. /* Prepare the existing device list */
  630. device = SDL_HIDAPI_devices;
  631. while (device) {
  632. device->seen = SDL_FALSE;
  633. device = device->next;
  634. }
  635. /* Enumerate the devices */
  636. if (SDL_HIDAPI_numdrivers > 0) {
  637. devs = hid_enumerate(0, 0);
  638. if (devs) {
  639. for (info = devs; info; info = info->next) {
  640. device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
  641. if (device) {
  642. device->seen = SDL_TRUE;
  643. } else {
  644. HIDAPI_AddDevice(info);
  645. }
  646. }
  647. hid_free_enumeration(devs);
  648. }
  649. }
  650. /* Remove any devices that weren't seen */
  651. device = SDL_HIDAPI_devices;
  652. while (device) {
  653. SDL_HIDAPI_Device *next = device->next;
  654. if (!device->seen) {
  655. HIDAPI_DelDevice(device, SDL_TRUE);
  656. }
  657. device = next;
  658. }
  659. }
  660. SDL_bool
  661. HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  662. {
  663. SDL_HIDAPI_Device *device;
  664. /* Make sure we're initialized, as this could be called from other drivers during startup */
  665. if (HIDAPI_JoystickInit() < 0) {
  666. return SDL_FALSE;
  667. }
  668. /* Don't update the device list for devices we know aren't supported */
  669. if (!HIDAPI_IsDeviceSupported(vendor_id, product_id, version, name)) {
  670. return SDL_FALSE;
  671. }
  672. /* Make sure the device list is completely up to date when we check for device presence */
  673. HIDAPI_UpdateDeviceList();
  674. /* Note that this isn't a perfect check - there may be multiple devices with 0 VID/PID,
  675. or a different name than we have it listed here, etc, but if we support the device
  676. and we have something similar in our device list, mark it as present.
  677. */
  678. device = SDL_HIDAPI_devices;
  679. while (device) {
  680. if (device->vendor_id == vendor_id && device->product_id == product_id && device->driver) {
  681. return SDL_TRUE;
  682. }
  683. device = device->next;
  684. }
  685. return SDL_FALSE;
  686. }
  687. static void
  688. HIDAPI_JoystickDetect(void)
  689. {
  690. HIDAPI_UpdateDiscovery();
  691. if (SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
  692. /* FIXME: We probably need to schedule an update in a few seconds as well */
  693. HIDAPI_UpdateDeviceList();
  694. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_FALSE;
  695. }
  696. }
  697. static const char *
  698. HIDAPI_JoystickGetDeviceName(int device_index)
  699. {
  700. return HIDAPI_GetJoystickByIndex(device_index)->name;
  701. }
  702. static int
  703. HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
  704. {
  705. return -1;
  706. }
  707. static SDL_JoystickGUID
  708. HIDAPI_JoystickGetDeviceGUID(int device_index)
  709. {
  710. return HIDAPI_GetJoystickByIndex(device_index)->guid;
  711. }
  712. static SDL_JoystickID
  713. HIDAPI_JoystickGetDeviceInstanceID(int device_index)
  714. {
  715. return HIDAPI_GetJoystickByIndex(device_index)->instance_id;
  716. }
  717. static int
  718. HIDAPI_JoystickOpen(SDL_Joystick * joystick, int device_index)
  719. {
  720. SDL_HIDAPI_Device *device = HIDAPI_GetJoystickByIndex(device_index);
  721. struct joystick_hwdata *hwdata;
  722. hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
  723. if (!hwdata) {
  724. return SDL_OutOfMemory();
  725. }
  726. hwdata->driver = device->driver;
  727. hwdata->dev = hid_open_path(device->path, 0);
  728. if (!hwdata->dev) {
  729. SDL_free(hwdata);
  730. return SDL_SetError("Couldn't open HID device %s", device->path);
  731. }
  732. hwdata->mutex = SDL_CreateMutex();
  733. if (!device->driver->Init(joystick, hwdata->dev, device->vendor_id, device->product_id, &hwdata->context)) {
  734. hid_close(hwdata->dev);
  735. SDL_free(hwdata);
  736. return -1;
  737. }
  738. joystick->hwdata = hwdata;
  739. return 0;
  740. }
  741. static int
  742. HIDAPI_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  743. {
  744. struct joystick_hwdata *hwdata = joystick->hwdata;
  745. SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
  746. int result;
  747. SDL_LockMutex(hwdata->mutex);
  748. result = driver->Rumble(joystick, hwdata->dev, hwdata->context, low_frequency_rumble, high_frequency_rumble, duration_ms);
  749. SDL_UnlockMutex(hwdata->mutex);
  750. return result;
  751. }
  752. static void
  753. HIDAPI_JoystickUpdate(SDL_Joystick * joystick)
  754. {
  755. struct joystick_hwdata *hwdata = joystick->hwdata;
  756. SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
  757. SDL_bool succeeded;
  758. SDL_LockMutex(hwdata->mutex);
  759. succeeded = driver->Update(joystick, hwdata->dev, hwdata->context);
  760. SDL_UnlockMutex(hwdata->mutex);
  761. if (!succeeded) {
  762. SDL_HIDAPI_Device *device;
  763. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  764. if (device->instance_id == joystick->instance_id) {
  765. HIDAPI_DelDevice(device, SDL_TRUE);
  766. break;
  767. }
  768. }
  769. }
  770. }
  771. static void
  772. HIDAPI_JoystickClose(SDL_Joystick * joystick)
  773. {
  774. struct joystick_hwdata *hwdata = joystick->hwdata;
  775. SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
  776. driver->Quit(joystick, hwdata->dev, hwdata->context);
  777. hid_close(hwdata->dev);
  778. SDL_DestroyMutex(hwdata->mutex);
  779. SDL_free(hwdata);
  780. joystick->hwdata = NULL;
  781. }
  782. static void
  783. HIDAPI_JoystickQuit(void)
  784. {
  785. int i;
  786. HIDAPI_ShutdownDiscovery();
  787. while (SDL_HIDAPI_devices) {
  788. HIDAPI_DelDevice(SDL_HIDAPI_devices, SDL_FALSE);
  789. }
  790. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  791. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  792. SDL_DelHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  793. }
  794. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  795. SDL_HIDAPIDriverHintChanged, NULL);
  796. SDL_HIDAPI_numjoysticks = 0;
  797. hid_exit();
  798. initialized = SDL_FALSE;
  799. }
  800. SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
  801. {
  802. HIDAPI_JoystickInit,
  803. HIDAPI_JoystickGetCount,
  804. HIDAPI_JoystickDetect,
  805. HIDAPI_JoystickGetDeviceName,
  806. HIDAPI_JoystickGetDevicePlayerIndex,
  807. HIDAPI_JoystickGetDeviceGUID,
  808. HIDAPI_JoystickGetDeviceInstanceID,
  809. HIDAPI_JoystickOpen,
  810. HIDAPI_JoystickRumble,
  811. HIDAPI_JoystickUpdate,
  812. HIDAPI_JoystickClose,
  813. HIDAPI_JoystickQuit,
  814. };
  815. #endif /* SDL_JOYSTICK_HIDAPI */
  816. /* vi: set ts=4 sw=4 expandtab: */