SDL_hidapijoystick.c 34 KB

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