SDL_hidapijoystick.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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_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. #if defined(__WIN32__)
  30. #include "../../core/windows/SDL_windows.h"
  31. #endif
  32. #if defined(__MACOSX__)
  33. #include <CoreFoundation/CoreFoundation.h>
  34. #include <mach/mach.h>
  35. #include <IOKit/IOKitLib.h>
  36. #include <IOKit/usb/USBSpec.h>
  37. #endif
  38. #if defined(__LINUX__)
  39. #include "../../core/linux/SDL_udev.h"
  40. #ifdef SDL_USE_LIBUDEV
  41. #include <poll.h>
  42. #endif
  43. #endif
  44. struct joystick_hwdata
  45. {
  46. SDL_HIDAPI_DeviceDriver *driver;
  47. void *context;
  48. SDL_mutex *mutex;
  49. hid_device *dev;
  50. };
  51. typedef struct _SDL_HIDAPI_Device
  52. {
  53. SDL_JoystickID instance_id;
  54. char *name;
  55. char *path;
  56. Uint16 vendor_id;
  57. Uint16 product_id;
  58. Uint16 version;
  59. SDL_JoystickGUID guid;
  60. int interface_number; /* Available on Windows and Linux */
  61. Uint16 usage_page; /* Available on Windows and Mac OS X */
  62. Uint16 usage; /* Available on Windows and Mac OS X */
  63. SDL_HIDAPI_DeviceDriver *driver;
  64. /* Used during scanning for device changes */
  65. SDL_bool seen;
  66. struct _SDL_HIDAPI_Device *next;
  67. } SDL_HIDAPI_Device;
  68. static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
  69. #ifdef SDL_JOYSTICK_HIDAPI_PS4
  70. &SDL_HIDAPI_DriverPS4,
  71. #endif
  72. #ifdef SDL_JOYSTICK_HIDAPI_STEAM
  73. &SDL_HIDAPI_DriverSteam,
  74. #endif
  75. #ifdef SDL_JOYSTICK_HIDAPI_SWITCH
  76. &SDL_HIDAPI_DriverSwitch,
  77. #endif
  78. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  79. &SDL_HIDAPI_DriverXbox360,
  80. #endif
  81. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  82. &SDL_HIDAPI_DriverXboxOne,
  83. #endif
  84. };
  85. static SDL_HIDAPI_Device *SDL_HIDAPI_devices;
  86. static int SDL_HIDAPI_numjoysticks = 0;
  87. #if defined(SDL_USE_LIBUDEV)
  88. static const SDL_UDEV_Symbols * usyms = NULL;
  89. #endif
  90. static struct
  91. {
  92. SDL_bool m_bHaveDevicesChanged;
  93. SDL_bool m_bCanGetNotifications;
  94. Uint32 m_unLastDetect;
  95. #if defined(__WIN32__)
  96. SDL_threadID m_nThreadID;
  97. WNDCLASSEXA m_wndClass;
  98. HWND m_hwndMsg;
  99. HDEVNOTIFY m_hNotify;
  100. double m_flLastWin32MessageCheck;
  101. #endif
  102. #if defined(__MACOSX__)
  103. IONotificationPortRef m_notificationPort;
  104. mach_port_t m_notificationMach;
  105. #endif
  106. #if defined(SDL_USE_LIBUDEV)
  107. struct udev *m_pUdev;
  108. struct udev_monitor *m_pUdevMonitor;
  109. int m_nUdevFd;
  110. #endif
  111. } SDL_HIDAPI_discovery;
  112. #ifdef __WIN32__
  113. struct _DEV_BROADCAST_HDR
  114. {
  115. DWORD dbch_size;
  116. DWORD dbch_devicetype;
  117. DWORD dbch_reserved;
  118. };
  119. typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
  120. {
  121. DWORD dbcc_size;
  122. DWORD dbcc_devicetype;
  123. DWORD dbcc_reserved;
  124. GUID dbcc_classguid;
  125. char dbcc_name[ 1 ];
  126. } DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;
  127. typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
  128. #define DBT_DEVICEARRIVAL 0x8000 /* system detected a new device */
  129. #define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
  130. #define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
  131. #define DBT_DEVNODES_CHANGED 0x0007
  132. #define DBT_CONFIGCHANGED 0x0018
  133. #define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
  134. #define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
  135. #include <initguid.h>
  136. DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
  137. static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  138. {
  139. switch (message) {
  140. case WM_DEVICECHANGE:
  141. switch (wParam) {
  142. case DBT_DEVICEARRIVAL:
  143. case DBT_DEVICEREMOVECOMPLETE:
  144. if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  145. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  146. }
  147. break;
  148. }
  149. return TRUE;
  150. }
  151. return DefWindowProc(hwnd, message, wParam, lParam);
  152. }
  153. #endif /* __WIN32__ */
  154. #if defined(__MACOSX__)
  155. static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
  156. {
  157. /* Must drain the iterator, or we won't receive new notifications */
  158. io_object_t entry;
  159. while ((entry = IOIteratorNext(portIterator)) != 0) {
  160. IOObjectRelease(entry);
  161. *(SDL_bool*)context = SDL_TRUE;
  162. }
  163. }
  164. #endif /* __MACOSX__ */
  165. static void
  166. HIDAPI_InitializeDiscovery()
  167. {
  168. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  169. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
  170. SDL_HIDAPI_discovery.m_unLastDetect = 0;
  171. #if defined(__WIN32__)
  172. SDL_HIDAPI_discovery.m_nThreadID = SDL_ThreadID();
  173. SDL_memset(&SDL_HIDAPI_discovery.m_wndClass, 0x0, sizeof(SDL_HIDAPI_discovery.m_wndClass));
  174. SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
  175. SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
  176. SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; /* This function is called by windows */
  177. SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);
  178. RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
  179. SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
  180. {
  181. DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;
  182. SDL_memset( &devBroadcast, 0x0, sizeof( devBroadcast ) );
  183. devBroadcast.dbcc_size = sizeof( devBroadcast );
  184. devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  185. devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
  186. /* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
  187. * but that seems to be necessary to get a notice after each individual usb input device actually
  188. * installs, rather than just as the composite device is seen.
  189. */
  190. SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification( SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
  191. SDL_HIDAPI_discovery.m_bCanGetNotifications = ( SDL_HIDAPI_discovery.m_hNotify != 0 );
  192. }
  193. #endif /* __WIN32__ */
  194. #if defined(__MACOSX__)
  195. SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
  196. if (SDL_HIDAPI_discovery.m_notificationPort) {
  197. {
  198. CFMutableDictionaryRef matchingDict = IOServiceMatching("IOUSBDevice");
  199. /* Note: IOServiceAddMatchingNotification consumes the reference to matchingDict */
  200. io_iterator_t portIterator = 0;
  201. io_object_t entry;
  202. if (IOServiceAddMatchingNotification(SDL_HIDAPI_discovery.m_notificationPort, kIOMatchedNotification, matchingDict, CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator) == 0) {
  203. /* Must drain the existing iterator, or we won't receive new notifications */
  204. while ((entry = IOIteratorNext(portIterator)) != 0) {
  205. IOObjectRelease(entry);
  206. }
  207. } else {
  208. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  209. SDL_HIDAPI_discovery.m_notificationPort = nil;
  210. }
  211. }
  212. {
  213. CFMutableDictionaryRef matchingDict = IOServiceMatching("IOBluetoothDevice");
  214. /* Note: IOServiceAddMatchingNotification consumes the reference to matchingDict */
  215. io_iterator_t portIterator = 0;
  216. io_object_t entry;
  217. if (IOServiceAddMatchingNotification(SDL_HIDAPI_discovery.m_notificationPort, kIOMatchedNotification, matchingDict, CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator) == 0) {
  218. /* Must drain the existing iterator, or we won't receive new notifications */
  219. while ((entry = IOIteratorNext(portIterator)) != 0) {
  220. IOObjectRelease(entry);
  221. }
  222. } else {
  223. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  224. SDL_HIDAPI_discovery.m_notificationPort = nil;
  225. }
  226. }
  227. }
  228. SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
  229. if (SDL_HIDAPI_discovery.m_notificationPort) {
  230. SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
  231. }
  232. SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);
  233. #endif // __MACOSX__
  234. #if defined(SDL_USE_LIBUDEV)
  235. SDL_HIDAPI_discovery.m_pUdev = NULL;
  236. SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
  237. SDL_HIDAPI_discovery.m_nUdevFd = -1;
  238. usyms = SDL_UDEV_GetUdevSyms();
  239. if (usyms) {
  240. SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
  241. }
  242. if (SDL_HIDAPI_discovery.m_pUdev) {
  243. SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
  244. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  245. usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
  246. SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
  247. SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
  248. }
  249. }
  250. #endif /* SDL_USE_LIBUDEV */
  251. }
  252. static void
  253. HIDAPI_UpdateDiscovery()
  254. {
  255. if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) {
  256. const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
  257. Uint32 now = SDL_GetTicks();
  258. if (!SDL_HIDAPI_discovery.m_unLastDetect || SDL_TICKS_PASSED(now, SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) {
  259. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  260. SDL_HIDAPI_discovery.m_unLastDetect = now;
  261. }
  262. return;
  263. }
  264. #if defined(__WIN32__)
  265. #if 0 /* just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan. */
  266. /* We'll only get messages on the same thread that created the window */
  267. if (SDL_ThreadID() == SDL_HIDAPI_discovery.m_nThreadID) {
  268. MSG msg;
  269. while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) {
  270. if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) {
  271. TranslateMessage(&msg);
  272. DispatchMessage(&msg);
  273. }
  274. }
  275. }
  276. #endif
  277. #endif /* __WIN32__ */
  278. #if defined(__MACOSX__)
  279. if (SDL_HIDAPI_discovery.m_notificationPort) {
  280. struct { mach_msg_header_t hdr; char payload[ 4096 ]; } msg;
  281. 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) {
  282. IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);
  283. }
  284. }
  285. #endif
  286. #if defined(SDL_USE_LIBUDEV)
  287. if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
  288. /* Drain all notification events.
  289. * We don't expect a lot of device notifications so just
  290. * do a new discovery on any kind or number of notifications.
  291. * This could be made more restrictive if necessary.
  292. */
  293. for (;;) {
  294. struct pollfd PollUdev;
  295. struct udev_device *pUdevDevice;
  296. PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
  297. PollUdev.events = POLLIN;
  298. if (poll(&PollUdev, 1, 0) != 1) {
  299. break;
  300. }
  301. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
  302. pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
  303. if (pUdevDevice) {
  304. usyms->udev_device_unref(pUdevDevice);
  305. }
  306. }
  307. }
  308. #endif
  309. }
  310. static void
  311. HIDAPI_ShutdownDiscovery()
  312. {
  313. #if defined(__WIN32__)
  314. if (SDL_HIDAPI_discovery.m_hNotify)
  315. UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
  316. if (SDL_HIDAPI_discovery.m_hwndMsg) {
  317. DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
  318. }
  319. UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
  320. #endif
  321. #if defined(__MACOSX__)
  322. if (SDL_HIDAPI_discovery.m_notificationPort) {
  323. IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
  324. }
  325. #endif
  326. #if defined(SDL_USE_LIBUDEV)
  327. if (usyms) {
  328. if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
  329. usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
  330. }
  331. if (SDL_HIDAPI_discovery.m_pUdev) {
  332. usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
  333. }
  334. SDL_UDEV_ReleaseUdevSyms();
  335. usyms = NULL;
  336. }
  337. #endif
  338. }
  339. const char *
  340. HIDAPI_XboxControllerName(Uint16 vendor_id, Uint16 product_id)
  341. {
  342. static struct
  343. {
  344. Uint32 vidpid;
  345. const char *name;
  346. } names[] = {
  347. { MAKE_VIDPID(0x0079, 0x18d4), "GPD Win 2 X-Box Controller" },
  348. { MAKE_VIDPID(0x044f, 0xb326), "Thrustmaster Gamepad GP XID" },
  349. { MAKE_VIDPID(0x045e, 0x028e), "Microsoft X-Box 360 pad" },
  350. { MAKE_VIDPID(0x045e, 0x028f), "Microsoft X-Box 360 pad v2" },
  351. { MAKE_VIDPID(0x045e, 0x0291), "Xbox 360 Wireless Receiver (XBOX)" },
  352. { MAKE_VIDPID(0x045e, 0x02d1), "Microsoft X-Box One pad" },
  353. { MAKE_VIDPID(0x045e, 0x02dd), "Microsoft X-Box One pad (Firmware 2015)" },
  354. { MAKE_VIDPID(0x045e, 0x02e3), "Microsoft X-Box One Elite pad" },
  355. { MAKE_VIDPID(0x045e, 0x02ea), "Microsoft X-Box One S pad" },
  356. { MAKE_VIDPID(0x045e, 0x02ff), "Microsoft X-Box One pad" },
  357. { MAKE_VIDPID(0x045e, 0x0719), "Xbox 360 Wireless Receiver" },
  358. { MAKE_VIDPID(0x046d, 0xc21d), "Logitech Gamepad F310" },
  359. { MAKE_VIDPID(0x046d, 0xc21e), "Logitech Gamepad F510" },
  360. { MAKE_VIDPID(0x046d, 0xc21f), "Logitech Gamepad F710" },
  361. { MAKE_VIDPID(0x046d, 0xc242), "Logitech Chillstream Controller" },
  362. { MAKE_VIDPID(0x046d, 0xcaa3), "Logitech DriveFx Racing Wheel" },
  363. { MAKE_VIDPID(0x056e, 0x2004), "Elecom JC-U3613M" },
  364. { MAKE_VIDPID(0x06a3, 0xf51a), "Saitek P3600" },
  365. { MAKE_VIDPID(0x0738, 0x4716), "Mad Catz Wired Xbox 360 Controller" },
  366. { MAKE_VIDPID(0x0738, 0x4718), "Mad Catz Street Fighter IV FightStick SE" },
  367. { MAKE_VIDPID(0x0738, 0x4726), "Mad Catz Xbox 360 Controller" },
  368. { MAKE_VIDPID(0x0738, 0x4728), "Mad Catz Street Fighter IV FightPad" },
  369. { MAKE_VIDPID(0x0738, 0x4736), "Mad Catz MicroCon Gamepad" },
  370. { MAKE_VIDPID(0x0738, 0x4738), "Mad Catz Wired Xbox 360 Controller (SFIV)" },
  371. { MAKE_VIDPID(0x0738, 0x4740), "Mad Catz Beat Pad" },
  372. { MAKE_VIDPID(0x0738, 0x4758), "Mad Catz Arcade Game Stick" },
  373. { MAKE_VIDPID(0x0738, 0x4a01), "Mad Catz FightStick TE 2" },
  374. { MAKE_VIDPID(0x0738, 0x9871), "Mad Catz Portable Drum" },
  375. { MAKE_VIDPID(0x0738, 0xb726), "Mad Catz Xbox controller - MW2" },
  376. { MAKE_VIDPID(0x0738, 0xb738), "Mad Catz MVC2TE Stick 2" },
  377. { MAKE_VIDPID(0x0738, 0xbeef), "Mad Catz JOYTECH NEO SE Advanced GamePad" },
  378. { MAKE_VIDPID(0x0738, 0xcb02), "Saitek Cyborg Rumble Pad - PC/Xbox 360" },
  379. { MAKE_VIDPID(0x0738, 0xcb03), "Saitek P3200 Rumble Pad - PC/Xbox 360" },
  380. { MAKE_VIDPID(0x0738, 0xcb29), "Saitek Aviator Stick AV8R02" },
  381. { MAKE_VIDPID(0x0738, 0xf738), "Super SFIV FightStick TE S" },
  382. { MAKE_VIDPID(0x07ff, 0xffff), "Mad Catz GamePad" },
  383. { MAKE_VIDPID(0x0e6f, 0x0105), "HSM3 Xbox360 dancepad" },
  384. { MAKE_VIDPID(0x0e6f, 0x0113), "Afterglow AX.1 Gamepad for Xbox 360" },
  385. { MAKE_VIDPID(0x0e6f, 0x011f), "Rock Candy Gamepad Wired Controller" },
  386. { MAKE_VIDPID(0x0e6f, 0x0131), "PDP EA Sports Controller" },
  387. { MAKE_VIDPID(0x0e6f, 0x0133), "Xbox 360 Wired Controller" },
  388. { MAKE_VIDPID(0x0e6f, 0x0139), "Afterglow Prismatic Wired Controller" },
  389. { MAKE_VIDPID(0x0e6f, 0x013a), "PDP Xbox One Controller" },
  390. { MAKE_VIDPID(0x0e6f, 0x0146), "Rock Candy Wired Controller for Xbox One" },
  391. { MAKE_VIDPID(0x0e6f, 0x0147), "PDP Marvel Xbox One Controller" },
  392. { MAKE_VIDPID(0x0e6f, 0x015c), "PDP Xbox One Arcade Stick" },
  393. { MAKE_VIDPID(0x0e6f, 0x0161), "PDP Xbox One Controller" },
  394. { MAKE_VIDPID(0x0e6f, 0x0162), "PDP Xbox One Controller" },
  395. { MAKE_VIDPID(0x0e6f, 0x0163), "PDP Xbox One Controller" },
  396. { MAKE_VIDPID(0x0e6f, 0x0164), "PDP Battlefield One" },
  397. { MAKE_VIDPID(0x0e6f, 0x0165), "PDP Titanfall 2" },
  398. { MAKE_VIDPID(0x0e6f, 0x0201), "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller" },
  399. { MAKE_VIDPID(0x0e6f, 0x0213), "Afterglow Gamepad for Xbox 360" },
  400. { MAKE_VIDPID(0x0e6f, 0x021f), "Rock Candy Gamepad for Xbox 360" },
  401. { MAKE_VIDPID(0x0e6f, 0x0246), "Rock Candy Gamepad for Xbox One 2015" },
  402. { MAKE_VIDPID(0x0e6f, 0x02a4), "PDP Wired Controller for Xbox One - Stealth Series" },
  403. { MAKE_VIDPID(0x0e6f, 0x02ab), "PDP Controller for Xbox One" },
  404. { MAKE_VIDPID(0x0e6f, 0x0301), "Logic3 Controller" },
  405. { MAKE_VIDPID(0x0e6f, 0x0346), "Rock Candy Gamepad for Xbox One 2016" },
  406. { MAKE_VIDPID(0x0e6f, 0x0401), "Logic3 Controller" },
  407. { MAKE_VIDPID(0x0e6f, 0x0413), "Afterglow AX.1 Gamepad for Xbox 360" },
  408. { MAKE_VIDPID(0x0e6f, 0x0501), "PDP Xbox 360 Controller" },
  409. { MAKE_VIDPID(0x0e6f, 0xf900), "PDP Afterglow AX.1" },
  410. { MAKE_VIDPID(0x0f0d, 0x000a), "Hori Co. DOA4 FightStick" },
  411. { MAKE_VIDPID(0x0f0d, 0x000c), "Hori PadEX Turbo" },
  412. { MAKE_VIDPID(0x0f0d, 0x000d), "Hori Fighting Stick EX2" },
  413. { MAKE_VIDPID(0x0f0d, 0x0016), "Hori Real Arcade Pro.EX" },
  414. { MAKE_VIDPID(0x0f0d, 0x001b), "Hori Real Arcade Pro VX" },
  415. { MAKE_VIDPID(0x0f0d, 0x0063), "Hori Real Arcade Pro Hayabusa (USA) Xbox One" },
  416. { MAKE_VIDPID(0x0f0d, 0x0067), "HORIPAD ONE" },
  417. { MAKE_VIDPID(0x0f0d, 0x0078), "Hori Real Arcade Pro V Kai Xbox One" },
  418. { MAKE_VIDPID(0x11c9, 0x55f0), "Nacon GC-100XF" },
  419. { MAKE_VIDPID(0x12ab, 0x0004), "Honey Bee Xbox360 dancepad" },
  420. { MAKE_VIDPID(0x12ab, 0x0301), "PDP AFTERGLOW AX.1" },
  421. { MAKE_VIDPID(0x12ab, 0x0303), "Mortal Kombat Klassic FightStick" },
  422. { MAKE_VIDPID(0x1430, 0x4748), "RedOctane Guitar Hero X-plorer" },
  423. { MAKE_VIDPID(0x1430, 0xf801), "RedOctane Controller" },
  424. { MAKE_VIDPID(0x146b, 0x0601), "BigBen Interactive XBOX 360 Controller" },
  425. { MAKE_VIDPID(0x1532, 0x0037), "Razer Sabertooth" },
  426. { MAKE_VIDPID(0x1532, 0x0a00), "Razer Atrox Arcade Stick" },
  427. { MAKE_VIDPID(0x1532, 0x0a03), "Razer Wildcat" },
  428. { MAKE_VIDPID(0x15e4, 0x3f00), "Power A Mini Pro Elite" },
  429. { MAKE_VIDPID(0x15e4, 0x3f0a), "Xbox Airflo wired controller" },
  430. { MAKE_VIDPID(0x15e4, 0x3f10), "Batarang Xbox 360 controller" },
  431. { MAKE_VIDPID(0x162e, 0xbeef), "Joytech Neo-Se Take2" },
  432. { MAKE_VIDPID(0x1689, 0xfd00), "Razer Onza Tournament Edition" },
  433. { MAKE_VIDPID(0x1689, 0xfd01), "Razer Onza Classic Edition" },
  434. { MAKE_VIDPID(0x1689, 0xfe00), "Razer Sabertooth" },
  435. { MAKE_VIDPID(0x1bad, 0x0002), "Harmonix Rock Band Guitar" },
  436. { MAKE_VIDPID(0x1bad, 0x0003), "Harmonix Rock Band Drumkit" },
  437. { MAKE_VIDPID(0x1bad, 0x0130), "Ion Drum Rocker" },
  438. { MAKE_VIDPID(0x1bad, 0xf016), "Mad Catz Xbox 360 Controller" },
  439. { MAKE_VIDPID(0x1bad, 0xf018), "Mad Catz Street Fighter IV SE Fighting Stick" },
  440. { MAKE_VIDPID(0x1bad, 0xf019), "Mad Catz Brawlstick for Xbox 360" },
  441. { MAKE_VIDPID(0x1bad, 0xf021), "Mad Cats Ghost Recon FS GamePad" },
  442. { MAKE_VIDPID(0x1bad, 0xf023), "MLG Pro Circuit Controller (Xbox)" },
  443. { MAKE_VIDPID(0x1bad, 0xf025), "Mad Catz Call Of Duty" },
  444. { MAKE_VIDPID(0x1bad, 0xf027), "Mad Catz FPS Pro" },
  445. { MAKE_VIDPID(0x1bad, 0xf028), "Street Fighter IV FightPad" },
  446. { MAKE_VIDPID(0x1bad, 0xf02e), "Mad Catz Fightpad" },
  447. { MAKE_VIDPID(0x1bad, 0xf030), "Mad Catz Xbox 360 MC2 MicroCon Racing Wheel" },
  448. { MAKE_VIDPID(0x1bad, 0xf036), "Mad Catz MicroCon GamePad Pro" },
  449. { MAKE_VIDPID(0x1bad, 0xf038), "Street Fighter IV FightStick TE" },
  450. { MAKE_VIDPID(0x1bad, 0xf039), "Mad Catz MvC2 TE" },
  451. { MAKE_VIDPID(0x1bad, 0xf03a), "Mad Catz SFxT Fightstick Pro" },
  452. { MAKE_VIDPID(0x1bad, 0xf03d), "Street Fighter IV Arcade Stick TE - Chun Li" },
  453. { MAKE_VIDPID(0x1bad, 0xf03e), "Mad Catz MLG FightStick TE" },
  454. { MAKE_VIDPID(0x1bad, 0xf03f), "Mad Catz FightStick SoulCaliber" },
  455. { MAKE_VIDPID(0x1bad, 0xf042), "Mad Catz FightStick TES+" },
  456. { MAKE_VIDPID(0x1bad, 0xf080), "Mad Catz FightStick TE2" },
  457. { MAKE_VIDPID(0x1bad, 0xf501), "HoriPad EX2 Turbo" },
  458. { MAKE_VIDPID(0x1bad, 0xf502), "Hori Real Arcade Pro.VX SA" },
  459. { MAKE_VIDPID(0x1bad, 0xf503), "Hori Fighting Stick VX" },
  460. { MAKE_VIDPID(0x1bad, 0xf504), "Hori Real Arcade Pro. EX" },
  461. { MAKE_VIDPID(0x1bad, 0xf505), "Hori Fighting Stick EX2B" },
  462. { MAKE_VIDPID(0x1bad, 0xf506), "Hori Real Arcade Pro.EX Premium VLX" },
  463. { MAKE_VIDPID(0x1bad, 0xf900), "Harmonix Xbox 360 Controller" },
  464. { MAKE_VIDPID(0x1bad, 0xf901), "Gamestop Xbox 360 Controller" },
  465. { MAKE_VIDPID(0x1bad, 0xf903), "Tron Xbox 360 controller" },
  466. { MAKE_VIDPID(0x1bad, 0xf904), "PDP Versus Fighting Pad" },
  467. { MAKE_VIDPID(0x1bad, 0xf906), "MortalKombat FightStick" },
  468. { MAKE_VIDPID(0x1bad, 0xfa01), "MadCatz GamePad" },
  469. { MAKE_VIDPID(0x1bad, 0xfd00), "Razer Onza TE" },
  470. { MAKE_VIDPID(0x1bad, 0xfd01), "Razer Onza" },
  471. { MAKE_VIDPID(0x24c6, 0x5000), "Razer Atrox Arcade Stick" },
  472. { MAKE_VIDPID(0x24c6, 0x5300), "PowerA MINI PROEX Controller" },
  473. { MAKE_VIDPID(0x24c6, 0x5303), "Xbox Airflo wired controller" },
  474. { MAKE_VIDPID(0x24c6, 0x530a), "Xbox 360 Pro EX Controller" },
  475. { MAKE_VIDPID(0x24c6, 0x531a), "PowerA Pro Ex" },
  476. { MAKE_VIDPID(0x24c6, 0x5397), "FUS1ON Tournament Controller" },
  477. { MAKE_VIDPID(0x24c6, 0x541a), "PowerA Xbox One Mini Wired Controller" },
  478. { MAKE_VIDPID(0x24c6, 0x542a), "Xbox ONE spectra" },
  479. { MAKE_VIDPID(0x24c6, 0x543a), "PowerA Xbox One wired controller" },
  480. { MAKE_VIDPID(0x24c6, 0x5500), "Hori XBOX 360 EX 2 with Turbo" },
  481. { MAKE_VIDPID(0x24c6, 0x5501), "Hori Real Arcade Pro VX-SA" },
  482. { MAKE_VIDPID(0x24c6, 0x5502), "Hori Fighting Stick VX Alt" },
  483. { MAKE_VIDPID(0x24c6, 0x5503), "Hori Fighting Edge" },
  484. { MAKE_VIDPID(0x24c6, 0x5506), "Hori SOULCALIBUR V Stick" },
  485. { MAKE_VIDPID(0x24c6, 0x550d), "Hori GEM Xbox controller" },
  486. { MAKE_VIDPID(0x24c6, 0x550e), "Hori Real Arcade Pro V Kai 360" },
  487. { MAKE_VIDPID(0x24c6, 0x551a), "PowerA FUSION Pro Controller" },
  488. { MAKE_VIDPID(0x24c6, 0x561a), "PowerA FUSION Controller" },
  489. { MAKE_VIDPID(0x24c6, 0x5b00), "ThrustMaster Ferrari 458 Racing Wheel" },
  490. { MAKE_VIDPID(0x24c6, 0x5b02), "Thrustmaster, Inc. GPX Controller" },
  491. { MAKE_VIDPID(0x24c6, 0x5b03), "Thrustmaster Ferrari 458 Racing Wheel" },
  492. { MAKE_VIDPID(0x24c6, 0x5d04), "Razer Sabertooth" },
  493. { MAKE_VIDPID(0x24c6, 0xfafe), "Rock Candy Gamepad for Xbox 360" },
  494. };
  495. int i;
  496. Uint32 vidpid = MAKE_VIDPID(vendor_id, product_id);
  497. for (i = 0; i < SDL_arraysize(names); ++i) {
  498. if (vidpid == names[i].vidpid) {
  499. return names[i].name;
  500. }
  501. }
  502. return NULL;
  503. }
  504. static SDL_bool
  505. HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version)
  506. {
  507. int i;
  508. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  509. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  510. if (driver->enabled && driver->IsSupportedDevice(vendor_id, product_id, version, -1)) {
  511. return SDL_TRUE;
  512. }
  513. }
  514. return SDL_FALSE;
  515. }
  516. static SDL_HIDAPI_DeviceDriver *
  517. HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
  518. {
  519. const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
  520. const Uint16 USAGE_JOYSTICK = 0x0004;
  521. const Uint16 USAGE_GAMEPAD = 0x0005;
  522. const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
  523. int i;
  524. if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
  525. return NULL;
  526. }
  527. if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
  528. return NULL;
  529. }
  530. if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
  531. return NULL;
  532. }
  533. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  534. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  535. if (driver->enabled && driver->IsSupportedDevice(device->vendor_id, device->product_id, device->version, device->interface_number)) {
  536. return driver;
  537. }
  538. }
  539. return NULL;
  540. }
  541. static SDL_HIDAPI_Device *
  542. HIDAPI_GetJoystickByIndex(int device_index)
  543. {
  544. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  545. while (device) {
  546. if (device->driver) {
  547. if (device_index == 0) {
  548. break;
  549. }
  550. --device_index;
  551. }
  552. device = device->next;
  553. }
  554. return device;
  555. }
  556. static SDL_HIDAPI_Device *
  557. HIDAPI_GetJoystickByInfo(const char *path, Uint16 vendor_id, Uint16 product_id)
  558. {
  559. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  560. while (device) {
  561. if (device->vendor_id == vendor_id && device->product_id == product_id &&
  562. SDL_strcmp(device->path, path) == 0) {
  563. break;
  564. }
  565. device = device->next;
  566. }
  567. return device;
  568. }
  569. static void SDLCALL
  570. SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  571. {
  572. int i;
  573. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  574. SDL_bool enabled = (!hint || !*hint || ((*hint != '0') && (SDL_strcasecmp(hint, "false") != 0)));
  575. if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI) == 0) {
  576. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  577. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  578. driver->enabled = SDL_GetHintBoolean(driver->hint, enabled);
  579. }
  580. } else {
  581. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  582. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  583. if (SDL_strcmp(name, driver->hint) == 0) {
  584. driver->enabled = enabled;
  585. break;
  586. }
  587. }
  588. }
  589. /* Update device list if driver availability changes */
  590. while (device) {
  591. if (device->driver) {
  592. if (!device->driver->enabled) {
  593. device->driver = NULL;
  594. --SDL_HIDAPI_numjoysticks;
  595. SDL_PrivateJoystickRemoved(device->instance_id);
  596. }
  597. } else {
  598. device->driver = HIDAPI_GetDeviceDriver(device);
  599. if (device->driver) {
  600. device->instance_id = SDL_GetNextJoystickInstanceID();
  601. ++SDL_HIDAPI_numjoysticks;
  602. SDL_PrivateJoystickAdded(device->instance_id);
  603. }
  604. }
  605. device = device->next;
  606. }
  607. }
  608. static void HIDAPI_JoystickDetect(void);
  609. static int
  610. HIDAPI_JoystickInit(void)
  611. {
  612. int i;
  613. if (hid_init() < 0) {
  614. SDL_SetError("Couldn't initialize hidapi");
  615. return -1;
  616. }
  617. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  618. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  619. SDL_AddHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  620. }
  621. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  622. SDL_HIDAPIDriverHintChanged, NULL);
  623. HIDAPI_InitializeDiscovery();
  624. HIDAPI_JoystickDetect();
  625. return 0;
  626. }
  627. static int
  628. HIDAPI_JoystickGetCount(void)
  629. {
  630. return SDL_HIDAPI_numjoysticks;
  631. }
  632. static void
  633. HIDAPI_AddDevice(struct hid_device_info *info)
  634. {
  635. SDL_HIDAPI_Device *device;
  636. SDL_HIDAPI_Device *curr, *last = NULL;
  637. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  638. continue;
  639. }
  640. device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
  641. if (!device) {
  642. return;
  643. }
  644. device->instance_id = -1;
  645. device->seen = SDL_TRUE;
  646. device->vendor_id = info->vendor_id;
  647. device->product_id = info->product_id;
  648. device->version = info->release_number;
  649. device->interface_number = info->interface_number;
  650. device->usage_page = info->usage_page;
  651. device->usage = info->usage;
  652. {
  653. /* FIXME: Is there any way to tell whether this is a Bluetooth device? */
  654. const Uint16 vendor = device->vendor_id;
  655. const Uint16 product = device->product_id;
  656. const Uint16 version = device->version;
  657. Uint16 *guid16 = (Uint16 *)device->guid.data;
  658. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
  659. *guid16++ = 0;
  660. *guid16++ = SDL_SwapLE16(vendor);
  661. *guid16++ = 0;
  662. *guid16++ = SDL_SwapLE16(product);
  663. *guid16++ = 0;
  664. *guid16++ = SDL_SwapLE16(version);
  665. *guid16++ = 0;
  666. /* Note that this is a HIDAPI device for special handling elsewhere */
  667. device->guid.data[14] = 'h';
  668. device->guid.data[15] = 0;
  669. }
  670. /* Need the device name before getting the driver to know whether to ignore this device */
  671. if (!device->name && info->manufacturer_string && info->product_string) {
  672. char *manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
  673. char *product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
  674. if (!manufacturer_string && !product_string) {
  675. if (sizeof(wchar_t) == sizeof(Uint16)) {
  676. manufacturer_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
  677. product_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
  678. } else if (sizeof(wchar_t) == sizeof(Uint32)) {
  679. manufacturer_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
  680. product_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
  681. }
  682. }
  683. if (manufacturer_string && product_string) {
  684. size_t name_size = (SDL_strlen(manufacturer_string) + 1 + SDL_strlen(product_string) + 1);
  685. device->name = (char *)SDL_malloc(name_size);
  686. if (device->name) {
  687. SDL_snprintf(device->name, name_size, "%s %s", manufacturer_string, product_string);
  688. }
  689. }
  690. if (manufacturer_string) {
  691. SDL_free(manufacturer_string);
  692. }
  693. if (product_string) {
  694. SDL_free(product_string);
  695. }
  696. }
  697. if (!device->name) {
  698. size_t name_size = (6 + 1 + 6 + 1);
  699. device->name = (char *)SDL_malloc(name_size);
  700. if (!device->name) {
  701. SDL_free(device);
  702. return;
  703. }
  704. SDL_snprintf(device->name, name_size, "0x%.4x/0x%.4x", info->vendor_id, info->product_id);
  705. }
  706. device->driver = HIDAPI_GetDeviceDriver(device);
  707. if (device->driver) {
  708. const char *name = device->driver->GetDeviceName(device->vendor_id, device->product_id);
  709. if (name) {
  710. SDL_free(device->name);
  711. device->name = SDL_strdup(name);
  712. }
  713. }
  714. device->path = SDL_strdup(info->path);
  715. if (!device->path) {
  716. SDL_free(device->name);
  717. SDL_free(device);
  718. return;
  719. }
  720. #ifdef DEBUG_HIDAPI
  721. 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");
  722. #endif
  723. /* Add it to the list */
  724. if (last) {
  725. last->next = device;
  726. } else {
  727. SDL_HIDAPI_devices = device;
  728. }
  729. if (device->driver) {
  730. /* It's a joystick! */
  731. device->instance_id = SDL_GetNextJoystickInstanceID();
  732. ++SDL_HIDAPI_numjoysticks;
  733. SDL_PrivateJoystickAdded(device->instance_id);
  734. }
  735. }
  736. static void
  737. HIDAPI_DelDevice(SDL_HIDAPI_Device *device, SDL_bool send_event)
  738. {
  739. SDL_HIDAPI_Device *curr, *last;
  740. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  741. if (curr == device) {
  742. if (last) {
  743. last->next = curr->next;
  744. } else {
  745. SDL_HIDAPI_devices = curr->next;
  746. }
  747. if (device->driver && send_event) {
  748. /* Need to decrement the joystick count before we post the event */
  749. --SDL_HIDAPI_numjoysticks;
  750. SDL_PrivateJoystickRemoved(device->instance_id);
  751. }
  752. SDL_free(device->name);
  753. SDL_free(device->path);
  754. SDL_free(device);
  755. return;
  756. }
  757. }
  758. }
  759. static void
  760. HIDAPI_UpdateDeviceList(void)
  761. {
  762. SDL_HIDAPI_Device *device;
  763. struct hid_device_info *devs, *info;
  764. /* Prepare the existing device list */
  765. device = SDL_HIDAPI_devices;
  766. while (device) {
  767. device->seen = SDL_FALSE;
  768. device = device->next;
  769. }
  770. /* Enumerate the devices */
  771. devs = hid_enumerate(0, 0);
  772. if (devs) {
  773. for (info = devs; info; info = info->next) {
  774. device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
  775. if (device) {
  776. device->seen = SDL_TRUE;
  777. } else {
  778. HIDAPI_AddDevice(info);
  779. }
  780. }
  781. hid_free_enumeration(devs);
  782. }
  783. /* Remove any devices that weren't seen */
  784. device = SDL_HIDAPI_devices;
  785. while (device) {
  786. SDL_HIDAPI_Device *next = device->next;
  787. if (!device->seen) {
  788. HIDAPI_DelDevice(device, SDL_TRUE);
  789. }
  790. device = next;
  791. }
  792. }
  793. SDL_bool
  794. HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version)
  795. {
  796. SDL_HIDAPI_Device *device;
  797. /* Don't update the device list for devices we know aren't supported */
  798. if (!HIDAPI_IsDeviceSupported(vendor_id, product_id, version)) {
  799. return SDL_FALSE;
  800. }
  801. /* Make sure the device list is completely up to date when we check for device presence */
  802. HIDAPI_UpdateDeviceList();
  803. device = SDL_HIDAPI_devices;
  804. while (device) {
  805. if (device->vendor_id == vendor_id && device->product_id == product_id && device->driver) {
  806. return SDL_TRUE;
  807. }
  808. device = device->next;
  809. }
  810. return SDL_FALSE;
  811. }
  812. static void
  813. HIDAPI_JoystickDetect(void)
  814. {
  815. HIDAPI_UpdateDiscovery();
  816. if (SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
  817. /* FIXME: We probably need to schedule an update in a few seconds as well */
  818. HIDAPI_UpdateDeviceList();
  819. SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_FALSE;
  820. }
  821. }
  822. static const char *
  823. HIDAPI_JoystickGetDeviceName(int device_index)
  824. {
  825. return HIDAPI_GetJoystickByIndex(device_index)->name;
  826. }
  827. static int
  828. HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
  829. {
  830. return -1;
  831. }
  832. static SDL_JoystickGUID
  833. HIDAPI_JoystickGetDeviceGUID(int device_index)
  834. {
  835. return HIDAPI_GetJoystickByIndex(device_index)->guid;
  836. }
  837. static SDL_JoystickID
  838. HIDAPI_JoystickGetDeviceInstanceID(int device_index)
  839. {
  840. return HIDAPI_GetJoystickByIndex(device_index)->instance_id;
  841. }
  842. static int
  843. HIDAPI_JoystickOpen(SDL_Joystick * joystick, int device_index)
  844. {
  845. SDL_HIDAPI_Device *device = HIDAPI_GetJoystickByIndex(device_index);
  846. struct joystick_hwdata *hwdata;
  847. hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
  848. if (!hwdata) {
  849. return SDL_OutOfMemory();
  850. }
  851. hwdata->driver = device->driver;
  852. hwdata->dev = hid_open_path(device->path, 0);
  853. if (!hwdata->dev) {
  854. SDL_free(hwdata);
  855. return SDL_SetError("Couldn't open HID device %s", device->path);
  856. }
  857. hwdata->mutex = SDL_CreateMutex();
  858. if (!device->driver->Init(joystick, hwdata->dev, device->vendor_id, device->product_id, &hwdata->context)) {
  859. hid_close(hwdata->dev);
  860. SDL_free(hwdata);
  861. return -1;
  862. }
  863. joystick->hwdata = hwdata;
  864. return 0;
  865. }
  866. static int
  867. HIDAPI_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  868. {
  869. struct joystick_hwdata *hwdata = joystick->hwdata;
  870. SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
  871. int result;
  872. SDL_LockMutex(hwdata->mutex);
  873. result = driver->Rumble(joystick, hwdata->dev, hwdata->context, low_frequency_rumble, high_frequency_rumble, duration_ms);
  874. SDL_UnlockMutex(hwdata->mutex);
  875. return result;
  876. }
  877. static void
  878. HIDAPI_JoystickUpdate(SDL_Joystick * joystick)
  879. {
  880. struct joystick_hwdata *hwdata = joystick->hwdata;
  881. SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
  882. SDL_bool succeeded;
  883. SDL_LockMutex(hwdata->mutex);
  884. succeeded = driver->Update(joystick, hwdata->dev, hwdata->context);
  885. SDL_UnlockMutex(hwdata->mutex);
  886. if (!succeeded) {
  887. SDL_HIDAPI_Device *device;
  888. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  889. if (device->instance_id == joystick->instance_id) {
  890. HIDAPI_DelDevice(device, SDL_TRUE);
  891. break;
  892. }
  893. }
  894. }
  895. }
  896. static void
  897. HIDAPI_JoystickClose(SDL_Joystick * joystick)
  898. {
  899. struct joystick_hwdata *hwdata = joystick->hwdata;
  900. SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
  901. driver->Quit(joystick, hwdata->dev, hwdata->context);
  902. hid_close(hwdata->dev);
  903. SDL_DestroyMutex(hwdata->mutex);
  904. SDL_free(hwdata);
  905. joystick->hwdata = NULL;
  906. }
  907. static void
  908. HIDAPI_JoystickQuit(void)
  909. {
  910. int i;
  911. HIDAPI_ShutdownDiscovery();
  912. while (SDL_HIDAPI_devices) {
  913. HIDAPI_DelDevice(SDL_HIDAPI_devices, SDL_FALSE);
  914. }
  915. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  916. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  917. SDL_DelHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  918. }
  919. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  920. SDL_HIDAPIDriverHintChanged, NULL);
  921. SDL_HIDAPI_numjoysticks = 0;
  922. hid_exit();
  923. }
  924. SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
  925. {
  926. HIDAPI_JoystickInit,
  927. HIDAPI_JoystickGetCount,
  928. HIDAPI_JoystickDetect,
  929. HIDAPI_JoystickGetDeviceName,
  930. HIDAPI_JoystickGetDevicePlayerIndex,
  931. HIDAPI_JoystickGetDeviceGUID,
  932. HIDAPI_JoystickGetDeviceInstanceID,
  933. HIDAPI_JoystickOpen,
  934. HIDAPI_JoystickRumble,
  935. HIDAPI_JoystickUpdate,
  936. HIDAPI_JoystickClose,
  937. HIDAPI_JoystickQuit,
  938. };
  939. #endif /* SDL_JOYSTICK_HIDAPI */
  940. /* vi: set ts=4 sw=4 expandtab: */