1
0

SDL_hidapijoystick.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_atomic.h"
  21. #include "SDL_endian.h"
  22. #include "SDL_hints.h"
  23. #include "SDL_timer.h"
  24. #include "SDL_joystick.h"
  25. #include "../SDL_sysjoystick.h"
  26. #include "SDL_hidapijoystick_c.h"
  27. #include "SDL_hidapi_rumble.h"
  28. #include "../../SDL_hints_c.h"
  29. #if defined(__WIN32__) || defined(__WINGDK__)
  30. #include "../windows/SDL_rawinputjoystick_c.h"
  31. #endif
  32. struct joystick_hwdata
  33. {
  34. SDL_HIDAPI_Device *device;
  35. };
  36. static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
  37. #ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
  38. &SDL_HIDAPI_DriverGameCube,
  39. #endif
  40. #ifdef SDL_JOYSTICK_HIDAPI_LUNA
  41. &SDL_HIDAPI_DriverLuna,
  42. #endif
  43. #ifdef SDL_JOYSTICK_HIDAPI_SHIELD
  44. &SDL_HIDAPI_DriverShield,
  45. #endif
  46. #ifdef SDL_JOYSTICK_HIDAPI_PS4
  47. &SDL_HIDAPI_DriverPS4,
  48. #endif
  49. #ifdef SDL_JOYSTICK_HIDAPI_PS5
  50. &SDL_HIDAPI_DriverPS5,
  51. #endif
  52. #ifdef SDL_JOYSTICK_HIDAPI_STADIA
  53. &SDL_HIDAPI_DriverStadia,
  54. #endif
  55. #ifdef SDL_JOYSTICK_HIDAPI_STEAM
  56. &SDL_HIDAPI_DriverSteam,
  57. #endif
  58. #ifdef SDL_JOYSTICK_HIDAPI_SWITCH
  59. &SDL_HIDAPI_DriverNintendoClassic,
  60. &SDL_HIDAPI_DriverJoyCons,
  61. &SDL_HIDAPI_DriverSwitch,
  62. #endif
  63. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  64. &SDL_HIDAPI_DriverXbox360,
  65. &SDL_HIDAPI_DriverXbox360W,
  66. #endif
  67. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  68. &SDL_HIDAPI_DriverXboxOne,
  69. #endif
  70. };
  71. static int SDL_HIDAPI_numdrivers = 0;
  72. static SDL_SpinLock SDL_HIDAPI_spinlock;
  73. static Uint32 SDL_HIDAPI_change_count = 0;
  74. static SDL_HIDAPI_Device *SDL_HIDAPI_devices;
  75. static int SDL_HIDAPI_numjoysticks = 0;
  76. static SDL_bool SDL_HIDAPI_combine_joycons = SDL_TRUE;
  77. static SDL_bool initialized = SDL_FALSE;
  78. static SDL_bool shutting_down = SDL_FALSE;
  79. void
  80. HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size)
  81. {
  82. int i;
  83. char *buffer;
  84. size_t length = SDL_strlen(prefix) + 11*(USB_PACKET_LENGTH/8) + (5*USB_PACKET_LENGTH*2) + 1 + 1;
  85. int start = 0, amount = size;
  86. buffer = (char *)SDL_malloc(length);
  87. SDL_snprintf(buffer, length, prefix, size);
  88. for (i = start; i < start+amount; ++i) {
  89. if ((i % 8) == 0) {
  90. SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), "\n%.2d: ", i);
  91. }
  92. SDL_snprintf(&buffer[SDL_strlen(buffer)], length - SDL_strlen(buffer), " 0x%.2x", data[i]);
  93. }
  94. SDL_strlcat(buffer, "\n", length);
  95. SDL_Log("%s", buffer);
  96. SDL_free(buffer);
  97. }
  98. float
  99. HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max)
  100. {
  101. return output_min + (output_max - output_min) * (val - val_min) / (val_max - val_min);
  102. }
  103. static void HIDAPI_JoystickDetect(void);
  104. static void HIDAPI_JoystickClose(SDL_Joystick *joystick);
  105. static SDL_GameControllerType
  106. SDL_GetJoystickGameControllerProtocol(const char *name, Uint16 vendor, Uint16 product, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  107. {
  108. static const int LIBUSB_CLASS_VENDOR_SPEC = 0xFF;
  109. static const int XB360_IFACE_SUBCLASS = 93;
  110. static const int XB360_IFACE_PROTOCOL = 1; /* Wired */
  111. static const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
  112. static const int XBONE_IFACE_SUBCLASS = 71;
  113. static const int XBONE_IFACE_PROTOCOL = 208;
  114. SDL_GameControllerType type = SDL_CONTROLLER_TYPE_UNKNOWN;
  115. /* This code should match the checks in libusb/hid.c and HIDDeviceManager.java */
  116. if (interface_class == LIBUSB_CLASS_VENDOR_SPEC &&
  117. interface_subclass == XB360_IFACE_SUBCLASS &&
  118. (interface_protocol == XB360_IFACE_PROTOCOL ||
  119. interface_protocol == XB360W_IFACE_PROTOCOL)) {
  120. static const int SUPPORTED_VENDORS[] = {
  121. 0x0079, /* GPD Win 2 */
  122. 0x044f, /* Thrustmaster */
  123. 0x045e, /* Microsoft */
  124. 0x046d, /* Logitech */
  125. 0x056e, /* Elecom */
  126. 0x06a3, /* Saitek */
  127. 0x0738, /* Mad Catz */
  128. 0x07ff, /* Mad Catz */
  129. 0x0e6f, /* PDP */
  130. 0x0f0d, /* Hori */
  131. 0x1038, /* SteelSeries */
  132. 0x11c9, /* Nacon */
  133. 0x12ab, /* Unknown */
  134. 0x1430, /* RedOctane */
  135. 0x146b, /* BigBen */
  136. 0x1532, /* Razer */
  137. 0x15e4, /* Numark */
  138. 0x162e, /* Joytech */
  139. 0x1689, /* Razer Onza */
  140. 0x1949, /* Lab126, Inc. */
  141. 0x1bad, /* Harmonix */
  142. 0x20d6, /* PowerA */
  143. 0x24c6, /* PowerA */
  144. 0x2c22, /* Qanba */
  145. };
  146. int i;
  147. for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) {
  148. if (vendor == SUPPORTED_VENDORS[i]) {
  149. type = SDL_CONTROLLER_TYPE_XBOX360;
  150. break;
  151. }
  152. }
  153. }
  154. if (interface_number == 0 &&
  155. interface_class == LIBUSB_CLASS_VENDOR_SPEC &&
  156. interface_subclass == XBONE_IFACE_SUBCLASS &&
  157. interface_protocol == XBONE_IFACE_PROTOCOL) {
  158. static const int SUPPORTED_VENDORS[] = {
  159. 0x045e, /* Microsoft */
  160. 0x0738, /* Mad Catz */
  161. 0x0e6f, /* PDP */
  162. 0x0f0d, /* Hori */
  163. 0x1532, /* Razer */
  164. 0x20d6, /* PowerA */
  165. 0x24c6, /* PowerA */
  166. 0x2dc8, /* 8BitDo */
  167. 0x2e24, /* Hyperkin */
  168. };
  169. int i;
  170. for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) {
  171. if (vendor == SUPPORTED_VENDORS[i]) {
  172. type = SDL_CONTROLLER_TYPE_XBOXONE;
  173. break;
  174. }
  175. }
  176. }
  177. if (type == SDL_CONTROLLER_TYPE_UNKNOWN) {
  178. type = SDL_GetJoystickGameControllerTypeFromVIDPID(vendor, product, name, SDL_FALSE);
  179. }
  180. return type;
  181. }
  182. static SDL_bool
  183. HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  184. {
  185. int i;
  186. SDL_GameControllerType type = SDL_GetJoystickGameControllerProtocol(name, vendor_id, product_id, -1, 0, 0, 0);
  187. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  188. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  189. if (driver->enabled && driver->IsSupportedDevice(name, type, vendor_id, product_id, version, -1, 0, 0, 0)) {
  190. return SDL_TRUE;
  191. }
  192. }
  193. return SDL_FALSE;
  194. }
  195. static SDL_HIDAPI_DeviceDriver *
  196. HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
  197. {
  198. const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
  199. const Uint16 USAGE_JOYSTICK = 0x0004;
  200. const Uint16 USAGE_GAMEPAD = 0x0005;
  201. const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
  202. int i;
  203. SDL_GameControllerType type;
  204. if (device->num_children > 0) {
  205. return &SDL_HIDAPI_DriverCombined;
  206. }
  207. if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
  208. return NULL;
  209. }
  210. if (device->vendor_id != USB_VENDOR_VALVE) {
  211. if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
  212. return NULL;
  213. }
  214. if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
  215. return NULL;
  216. }
  217. }
  218. type = SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
  219. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  220. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  221. if (driver->enabled && driver->IsSupportedDevice(device->name, type, device->vendor_id, device->product_id, device->version, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol)) {
  222. return driver;
  223. }
  224. }
  225. return NULL;
  226. }
  227. static SDL_HIDAPI_Device *
  228. HIDAPI_GetDeviceByIndex(int device_index, SDL_JoystickID *pJoystickID)
  229. {
  230. SDL_HIDAPI_Device *device;
  231. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  232. if (device->parent) {
  233. continue;
  234. }
  235. if (device->driver) {
  236. if (device_index < device->num_joysticks) {
  237. if (pJoystickID) {
  238. *pJoystickID = device->joysticks[device_index];
  239. }
  240. return device;
  241. }
  242. device_index -= device->num_joysticks;
  243. }
  244. }
  245. return NULL;
  246. }
  247. static SDL_HIDAPI_Device *
  248. HIDAPI_GetJoystickByInfo(const char *path, Uint16 vendor_id, Uint16 product_id)
  249. {
  250. SDL_HIDAPI_Device *device;
  251. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  252. if (device->vendor_id == vendor_id && device->product_id == product_id &&
  253. SDL_strcmp(device->path, path) == 0) {
  254. break;
  255. }
  256. }
  257. return device;
  258. }
  259. static void
  260. HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device)
  261. {
  262. if (!device->driver) {
  263. return; /* Already cleaned up */
  264. }
  265. /* Disconnect any joysticks */
  266. while (device->num_joysticks && device->joysticks) {
  267. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  268. }
  269. device->driver->FreeDevice(device);
  270. device->driver = NULL;
  271. }
  272. static void
  273. HIDAPI_SetupDeviceDriver(SDL_HIDAPI_Device *device)
  274. {
  275. if (device->driver) {
  276. SDL_bool enabled;
  277. if (device->vendor_id == USB_VENDOR_NINTENDO && device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR) {
  278. enabled = SDL_HIDAPI_combine_joycons;
  279. } else {
  280. enabled = device->driver->enabled;
  281. }
  282. if (device->children) {
  283. int i;
  284. for (i = 0; i < device->num_children; ++i) {
  285. SDL_HIDAPI_Device *child = device->children[i];
  286. if (!child->driver || !child->driver->enabled) {
  287. enabled = SDL_FALSE;
  288. break;
  289. }
  290. }
  291. }
  292. if (!enabled) {
  293. HIDAPI_CleanupDeviceDriver(device);
  294. }
  295. return; /* Already setup */
  296. }
  297. device->driver = HIDAPI_GetDeviceDriver(device);
  298. if (device->driver) {
  299. const char *name = device->driver->GetDeviceName(device->name, device->vendor_id, device->product_id);
  300. if (name && name != device->name) {
  301. SDL_free(device->name);
  302. device->name = SDL_strdup(name);
  303. }
  304. }
  305. /* Initialize the device, which may cause a connected event */
  306. if (device->driver && !device->driver->InitDevice(device)) {
  307. device->driver = NULL;
  308. }
  309. }
  310. static void SDLCALL
  311. SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  312. {
  313. int i;
  314. SDL_HIDAPI_Device *device;
  315. SDL_bool enabled = SDL_GetStringBoolean(hint, SDL_TRUE);
  316. if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI) == 0) {
  317. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  318. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  319. driver->enabled = SDL_GetHintBoolean(driver->hint, enabled);
  320. }
  321. } else if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS) == 0) {
  322. SDL_HIDAPI_combine_joycons = enabled;
  323. } else {
  324. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  325. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  326. if (SDL_strcmp(name, driver->hint) == 0) {
  327. driver->enabled = enabled;
  328. }
  329. }
  330. }
  331. SDL_HIDAPI_numdrivers = 0;
  332. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  333. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  334. if (driver->enabled) {
  335. ++SDL_HIDAPI_numdrivers;
  336. }
  337. }
  338. /* Update device list if driver availability changes */
  339. SDL_LockJoysticks();
  340. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  341. HIDAPI_SetupDeviceDriver(device);
  342. }
  343. /* Update the device list again to pick up any new devices */
  344. SDL_HIDAPI_change_count = 0;
  345. SDL_UnlockJoysticks();
  346. }
  347. static int
  348. HIDAPI_JoystickInit(void)
  349. {
  350. int i;
  351. if (initialized) {
  352. return 0;
  353. }
  354. #if defined(SDL_USE_LIBUDEV)
  355. if (linux_enumeration_method == ENUMERATION_UNSET) {
  356. if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
  357. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  358. "udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
  359. linux_enumeration_method = ENUMERATION_FALLBACK;
  360. } else if (access("/.flatpak-info", F_OK) == 0
  361. || access("/run/host/container-manager", F_OK) == 0) {
  362. /* Explicitly check `/.flatpak-info` because, for old versions of
  363. * Flatpak, this was the only available way to tell if we were in
  364. * a Flatpak container. */
  365. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  366. "Container detected, disabling HIDAPI udev integration");
  367. linux_enumeration_method = ENUMERATION_FALLBACK;
  368. } else {
  369. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  370. "Using udev for HIDAPI joystick device discovery");
  371. linux_enumeration_method = ENUMERATION_LIBUDEV;
  372. }
  373. }
  374. #endif
  375. if (SDL_hid_init() < 0) {
  376. return SDL_SetError("Couldn't initialize hidapi");
  377. }
  378. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  379. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  380. SDL_AddHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  381. }
  382. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS,
  383. SDL_HIDAPIDriverHintChanged, NULL);
  384. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  385. SDL_HIDAPIDriverHintChanged, NULL);
  386. HIDAPI_JoystickDetect();
  387. HIDAPI_UpdateDevices();
  388. initialized = SDL_TRUE;
  389. return 0;
  390. }
  391. static SDL_bool
  392. HIDAPI_AddJoystickInstanceToDevice(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  393. {
  394. SDL_JoystickID *joysticks = (SDL_JoystickID *)SDL_realloc(device->joysticks, (device->num_joysticks + 1)*sizeof(*device->joysticks));
  395. if (!joysticks) {
  396. return SDL_FALSE;
  397. }
  398. device->joysticks = joysticks;
  399. device->joysticks[device->num_joysticks++] = joystickID;
  400. return SDL_TRUE;
  401. }
  402. static SDL_bool
  403. HIDAPI_DelJoystickInstanceFromDevice(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  404. {
  405. int i, size;
  406. for (i = 0; i < device->num_joysticks; ++i) {
  407. if (device->joysticks[i] == joystickID) {
  408. size = (device->num_joysticks - i - 1) * sizeof(SDL_JoystickID);
  409. SDL_memmove(&device->joysticks[i], &device->joysticks[i+1], size);
  410. --device->num_joysticks;
  411. if (device->num_joysticks == 0) {
  412. SDL_free(device->joysticks);
  413. device->joysticks = NULL;
  414. }
  415. return SDL_TRUE;
  416. }
  417. }
  418. return SDL_FALSE;
  419. }
  420. static SDL_bool
  421. HIDAPI_JoystickInstanceIsUnique(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  422. {
  423. if (device->parent && device->num_joysticks == 1 && device->parent->num_joysticks == 1 &&
  424. device->joysticks[0] == device->parent->joysticks[0]) {
  425. return SDL_FALSE;
  426. }
  427. return SDL_TRUE;
  428. }
  429. SDL_bool
  430. HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID)
  431. {
  432. int i, j;
  433. SDL_JoystickID joystickID;
  434. for (i = 0; i < device->num_children; ++i) {
  435. SDL_HIDAPI_Device *child = device->children[i];
  436. for (j = child->num_joysticks; j--; ) {
  437. HIDAPI_JoystickDisconnected(child, child->joysticks[j]);
  438. }
  439. }
  440. joystickID = SDL_GetNextJoystickInstanceID();
  441. HIDAPI_AddJoystickInstanceToDevice(device, joystickID);
  442. for (i = 0; i < device->num_children; ++i) {
  443. SDL_HIDAPI_Device *child = device->children[i];
  444. HIDAPI_AddJoystickInstanceToDevice(child, joystickID);
  445. }
  446. ++SDL_HIDAPI_numjoysticks;
  447. SDL_PrivateJoystickAdded(joystickID);
  448. if (pJoystickID) {
  449. *pJoystickID = joystickID;
  450. }
  451. return SDL_TRUE;
  452. }
  453. void
  454. HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  455. {
  456. int i, j;
  457. SDL_bool unique = HIDAPI_JoystickInstanceIsUnique(device, joystickID);
  458. if (!unique) {
  459. /* Disconnecting a child always disconnects the parent */
  460. device = device->parent;
  461. unique = SDL_TRUE;
  462. }
  463. for (i = 0; i < device->num_joysticks; ++i) {
  464. if (device->joysticks[i] == joystickID) {
  465. if (unique) {
  466. SDL_Joystick *joystick = SDL_JoystickFromInstanceID(joystickID);
  467. if (joystick) {
  468. HIDAPI_JoystickClose(joystick);
  469. }
  470. }
  471. HIDAPI_DelJoystickInstanceFromDevice(device, joystickID);
  472. for (j = 0; j < device->num_children; ++j) {
  473. SDL_HIDAPI_Device *child = device->children[j];
  474. HIDAPI_DelJoystickInstanceFromDevice(child, joystickID);
  475. }
  476. if (unique) {
  477. --SDL_HIDAPI_numjoysticks;
  478. if (!shutting_down) {
  479. SDL_PrivateJoystickRemoved(joystickID);
  480. }
  481. }
  482. }
  483. }
  484. /* Rescan the device list in case device state has changed */
  485. SDL_HIDAPI_change_count = 0;
  486. }
  487. static int
  488. HIDAPI_JoystickGetCount(void)
  489. {
  490. return SDL_HIDAPI_numjoysticks;
  491. }
  492. static char *
  493. HIDAPI_ConvertString(const wchar_t *wide_string)
  494. {
  495. char *string = NULL;
  496. if (wide_string) {
  497. string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
  498. if (!string) {
  499. switch (sizeof(wchar_t)) {
  500. case 2:
  501. string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
  502. break;
  503. case 4:
  504. string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
  505. break;
  506. }
  507. }
  508. }
  509. return string;
  510. }
  511. static SDL_HIDAPI_Device *
  512. HIDAPI_AddDevice(const struct SDL_hid_device_info *info, int num_children, SDL_HIDAPI_Device **children)
  513. {
  514. SDL_HIDAPI_Device *device;
  515. SDL_HIDAPI_Device *curr, *last = NULL;
  516. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  517. continue;
  518. }
  519. device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
  520. if (!device) {
  521. return NULL;
  522. }
  523. device->path = SDL_strdup(info->path);
  524. if (!device->path) {
  525. SDL_free(device);
  526. return NULL;
  527. }
  528. device->seen = SDL_TRUE;
  529. device->vendor_id = info->vendor_id;
  530. device->product_id = info->product_id;
  531. device->version = info->release_number;
  532. device->interface_number = info->interface_number;
  533. device->interface_class = info->interface_class;
  534. device->interface_subclass = info->interface_subclass;
  535. device->interface_protocol = info->interface_protocol;
  536. device->usage_page = info->usage_page;
  537. device->usage = info->usage;
  538. {
  539. /* FIXME: Is there any way to tell whether this is a Bluetooth device? */
  540. const Uint16 vendor = device->vendor_id;
  541. const Uint16 product = device->product_id;
  542. const Uint16 version = device->version;
  543. Uint16 *guid16 = (Uint16 *)device->guid.data;
  544. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
  545. *guid16++ = 0;
  546. *guid16++ = SDL_SwapLE16(vendor);
  547. *guid16++ = 0;
  548. *guid16++ = SDL_SwapLE16(product);
  549. *guid16++ = 0;
  550. *guid16++ = SDL_SwapLE16(version);
  551. *guid16++ = 0;
  552. /* Note that this is a HIDAPI device for special handling elsewhere */
  553. device->guid.data[14] = 'h';
  554. device->guid.data[15] = 0;
  555. }
  556. device->dev_lock = SDL_CreateMutex();
  557. /* Need the device name before getting the driver to know whether to ignore this device */
  558. {
  559. char *manufacturer_string = HIDAPI_ConvertString(info->manufacturer_string);
  560. char *product_string = HIDAPI_ConvertString(info->product_string);
  561. char *serial_number = HIDAPI_ConvertString(info->serial_number);
  562. device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string);
  563. if (manufacturer_string) {
  564. SDL_free(manufacturer_string);
  565. }
  566. if (product_string) {
  567. SDL_free(product_string);
  568. }
  569. if (serial_number && *serial_number) {
  570. device->serial = serial_number;
  571. } else {
  572. SDL_free(serial_number);
  573. }
  574. if (!device->name) {
  575. SDL_free(device->serial);
  576. SDL_free(device->path);
  577. SDL_free(device);
  578. return NULL;
  579. }
  580. }
  581. if (num_children > 0) {
  582. int i;
  583. device->num_children = num_children;
  584. device->children = children;
  585. for (i = 0; i < num_children; ++i) {
  586. children[i]->parent = device;
  587. }
  588. }
  589. /* Add it to the list */
  590. if (last) {
  591. last->next = device;
  592. } else {
  593. SDL_HIDAPI_devices = device;
  594. }
  595. HIDAPI_SetupDeviceDriver(device);
  596. #ifdef DEBUG_HIDAPI
  597. SDL_Log("Added HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, serial %s, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->hint : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
  598. #endif
  599. return device;
  600. }
  601. static void
  602. HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
  603. {
  604. SDL_HIDAPI_Device *curr, *last;
  605. #ifdef DEBUG_HIDAPI
  606. SDL_Log("Removing HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, serial %s, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->hint : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
  607. #endif
  608. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  609. if (curr == device) {
  610. if (last) {
  611. last->next = curr->next;
  612. } else {
  613. SDL_HIDAPI_devices = curr->next;
  614. }
  615. HIDAPI_CleanupDeviceDriver(device);
  616. /* Make sure the rumble thread is done with this device */
  617. while (SDL_AtomicGet(&device->rumble_pending) > 0) {
  618. SDL_Delay(10);
  619. }
  620. SDL_DestroyMutex(device->dev_lock);
  621. SDL_free(device->serial);
  622. SDL_free(device->name);
  623. SDL_free(device->path);
  624. SDL_free(device->children);
  625. SDL_free(device);
  626. return;
  627. }
  628. }
  629. }
  630. static SDL_bool
  631. HIDAPI_CreateCombinedJoyCons()
  632. {
  633. SDL_HIDAPI_Device *device, *combined;
  634. SDL_HIDAPI_Device *joycons[2] = { NULL, NULL };
  635. if (!SDL_HIDAPI_combine_joycons) {
  636. return SDL_FALSE;
  637. }
  638. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  639. if (!device->driver) {
  640. /* Unsupported device */
  641. continue;
  642. }
  643. if (device->parent) {
  644. /* This device is already part of a combined device */
  645. continue;
  646. }
  647. if (!joycons[0] &&
  648. (SDL_IsJoystickNintendoSwitchJoyConLeft(device->vendor_id, device->product_id) ||
  649. (SDL_IsJoystickNintendoSwitchJoyConGrip(device->vendor_id, device->product_id) &&
  650. SDL_strstr(device->name, "(L)") != NULL))) {
  651. joycons[0] = device;
  652. }
  653. if (!joycons[1] &&
  654. (SDL_IsJoystickNintendoSwitchJoyConRight(device->vendor_id, device->product_id) ||
  655. (SDL_IsJoystickNintendoSwitchJoyConGrip(device->vendor_id, device->product_id) &&
  656. SDL_strstr(device->name, "(R)") != NULL))) {
  657. joycons[1] = device;
  658. }
  659. if (joycons[0] && joycons[1]) {
  660. SDL_hid_device_info info;
  661. SDL_HIDAPI_Device **children = (SDL_HIDAPI_Device **)SDL_malloc(2 * sizeof(SDL_HIDAPI_Device *));
  662. if (!children) {
  663. return SDL_FALSE;
  664. }
  665. children[0] = joycons[0];
  666. children[1] = joycons[1];
  667. SDL_zero(info);
  668. info.path = "nintendo_joycons_combined";
  669. info.vendor_id = USB_VENDOR_NINTENDO;
  670. info.product_id = USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR;
  671. info.interface_number = -1;
  672. info.usage_page = USB_USAGEPAGE_GENERIC_DESKTOP;
  673. info.usage = USB_USAGE_GENERIC_GAMEPAD;
  674. info.manufacturer_string = L"Nintendo";
  675. info.product_string = L"Switch Joy-Con (L/R)";
  676. combined = HIDAPI_AddDevice(&info, 2, children);
  677. if (combined && combined->driver) {
  678. return SDL_TRUE;
  679. } else {
  680. if (!combined) {
  681. SDL_free(children);
  682. }
  683. return SDL_FALSE;
  684. }
  685. }
  686. }
  687. return SDL_FALSE;
  688. }
  689. static void
  690. HIDAPI_UpdateDeviceList(void)
  691. {
  692. SDL_HIDAPI_Device *device;
  693. struct SDL_hid_device_info *devs, *info;
  694. SDL_LockJoysticks();
  695. /* Prepare the existing device list */
  696. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  697. if (device->children) {
  698. continue;
  699. }
  700. device->seen = SDL_FALSE;
  701. }
  702. /* Enumerate the devices */
  703. if (SDL_HIDAPI_numdrivers > 0) {
  704. devs = SDL_hid_enumerate(0, 0);
  705. if (devs) {
  706. for (info = devs; info; info = info->next) {
  707. device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
  708. if (device) {
  709. device->seen = SDL_TRUE;
  710. } else {
  711. HIDAPI_AddDevice(info, 0, NULL);
  712. }
  713. }
  714. SDL_hid_free_enumeration(devs);
  715. }
  716. }
  717. /* Remove any devices that weren't seen or have been disconnected due to read errors */
  718. check_removed:
  719. device = SDL_HIDAPI_devices;
  720. while (device) {
  721. SDL_HIDAPI_Device *next = device->next;
  722. if (!device->seen ||
  723. ((device->driver || device->children) && device->num_joysticks == 0 && !device->dev)) {
  724. if (device->parent) {
  725. /* When a child device goes away, so does the parent */
  726. int i;
  727. device = device->parent;
  728. for (i = 0; i < device->num_children; ++i) {
  729. HIDAPI_DelDevice(device->children[i]);
  730. }
  731. HIDAPI_DelDevice(device);
  732. /* Update the device list again to pick up any children left */
  733. SDL_HIDAPI_change_count = 0;
  734. /* We deleted more than one device here, restart the loop */
  735. goto check_removed;
  736. } else {
  737. HIDAPI_DelDevice(device);
  738. }
  739. }
  740. device = next;
  741. }
  742. /* See if we can create any combined Joy-Con controllers */
  743. while (HIDAPI_CreateCombinedJoyCons()) {
  744. continue;
  745. }
  746. SDL_UnlockJoysticks();
  747. }
  748. static SDL_bool
  749. HIDAPI_IsEquivalentToDevice(Uint16 vendor_id, Uint16 product_id, SDL_HIDAPI_Device *device)
  750. {
  751. if (vendor_id == device->vendor_id && product_id == device->product_id) {
  752. return SDL_TRUE;
  753. }
  754. if (vendor_id == USB_VENDOR_MICROSOFT) {
  755. /* If we're looking for the wireless XBox 360 controller, also look for the dongle */
  756. if (product_id == USB_PRODUCT_XBOX360_XUSB_CONTROLLER && device->product_id == USB_PRODUCT_XBOX360_WIRELESS_RECEIVER) {
  757. return SDL_TRUE;
  758. }
  759. /* If we're looking for the raw input Xbox One controller, match it against any other Xbox One controller */
  760. if (product_id == USB_PRODUCT_XBOX_ONE_XBOXGIP_CONTROLLER &&
  761. SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol) == SDL_CONTROLLER_TYPE_XBOXONE) {
  762. return SDL_TRUE;
  763. }
  764. /* If we're looking for an XInput controller, match it against any other Xbox controller */
  765. if (product_id == USB_PRODUCT_XBOX_ONE_XINPUT_CONTROLLER) {
  766. SDL_GameControllerType type = SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
  767. if (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE) {
  768. return SDL_TRUE;
  769. }
  770. }
  771. }
  772. return SDL_FALSE;
  773. }
  774. SDL_bool
  775. HIDAPI_IsDeviceTypePresent(SDL_GameControllerType type)
  776. {
  777. SDL_HIDAPI_Device *device;
  778. SDL_bool result = SDL_FALSE;
  779. /* Make sure we're initialized, as this could be called from other drivers during startup */
  780. if (HIDAPI_JoystickInit() < 0) {
  781. return SDL_FALSE;
  782. }
  783. if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
  784. HIDAPI_UpdateDeviceList();
  785. SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
  786. }
  787. SDL_LockJoysticks();
  788. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  789. if (device->driver &&
  790. SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol) == type) {
  791. result = SDL_TRUE;
  792. break;
  793. }
  794. }
  795. SDL_UnlockJoysticks();
  796. #ifdef DEBUG_HIDAPI
  797. SDL_Log("HIDAPI_IsDeviceTypePresent() returning %s for %d\n", result ? "true" : "false", type);
  798. #endif
  799. return result;
  800. }
  801. SDL_bool
  802. HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  803. {
  804. SDL_HIDAPI_Device *device;
  805. SDL_bool supported = SDL_FALSE;
  806. SDL_bool result = SDL_FALSE;
  807. /* Make sure we're initialized, as this could be called from other drivers during startup */
  808. if (HIDAPI_JoystickInit() < 0) {
  809. return SDL_FALSE;
  810. }
  811. /* Only update the device list for devices we know might be supported.
  812. If we did this for every device, it would hit the USB driver too hard and potentially
  813. lock up the system. This won't catch devices that we support but can only detect using
  814. USB interface details, like Xbox controllers, but hopefully the device list update is
  815. responsive enough to catch those.
  816. */
  817. supported = HIDAPI_IsDeviceSupported(vendor_id, product_id, version, name);
  818. #if defined(SDL_JOYSTICK_HIDAPI_XBOX360) || defined(SDL_JOYSTICK_HIDAPI_XBOXONE)
  819. if (!supported &&
  820. (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box") || SDL_strstr(name, "XBOX"))) {
  821. supported = SDL_TRUE;
  822. }
  823. #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 || SDL_JOYSTICK_HIDAPI_XBOXONE */
  824. if (supported) {
  825. if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
  826. HIDAPI_UpdateDeviceList();
  827. SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
  828. }
  829. }
  830. /* Note that this isn't a perfect check - there may be multiple devices with 0 VID/PID,
  831. or a different name than we have it listed here, etc, but if we support the device
  832. and we have something similar in our device list, mark it as present.
  833. */
  834. SDL_LockJoysticks();
  835. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  836. if (device->driver &&
  837. HIDAPI_IsEquivalentToDevice(vendor_id, product_id, device)) {
  838. result = SDL_TRUE;
  839. break;
  840. }
  841. }
  842. SDL_UnlockJoysticks();
  843. #ifdef DEBUG_HIDAPI
  844. SDL_Log("HIDAPI_IsDevicePresent() returning %s for 0x%.4x / 0x%.4x\n", result ? "true" : "false", vendor_id, product_id);
  845. #endif
  846. return result;
  847. }
  848. static void
  849. HIDAPI_JoystickDetect(void)
  850. {
  851. if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
  852. Uint32 count = SDL_hid_device_change_count();
  853. if (SDL_HIDAPI_change_count != count) {
  854. SDL_HIDAPI_change_count = count;
  855. HIDAPI_UpdateDeviceList();
  856. }
  857. SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
  858. }
  859. }
  860. void
  861. HIDAPI_UpdateDevices(void)
  862. {
  863. SDL_HIDAPI_Device *device;
  864. /* Update the devices, which may change connected joysticks and send events */
  865. /* Prepare the existing device list */
  866. if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) {
  867. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  868. if (device->parent) {
  869. continue;
  870. }
  871. if (device->driver) {
  872. if (SDL_TryLockMutex(device->dev_lock) == 0) {
  873. device->updating = SDL_TRUE;
  874. device->driver->UpdateDevice(device);
  875. device->updating = SDL_FALSE;
  876. SDL_UnlockMutex(device->dev_lock);
  877. }
  878. }
  879. }
  880. SDL_AtomicUnlock(&SDL_HIDAPI_spinlock);
  881. }
  882. }
  883. static const char *
  884. HIDAPI_JoystickGetDeviceName(int device_index)
  885. {
  886. SDL_HIDAPI_Device *device;
  887. const char *name = NULL;
  888. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  889. if (device) {
  890. /* FIXME: The device could be freed after this name is returned... */
  891. name = device->name;
  892. }
  893. return name;
  894. }
  895. static const char *
  896. HIDAPI_JoystickGetDevicePath(int device_index)
  897. {
  898. SDL_HIDAPI_Device *device;
  899. const char *path = NULL;
  900. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  901. if (device) {
  902. /* FIXME: The device could be freed after this path is returned... */
  903. path = device->path;
  904. }
  905. return path;
  906. }
  907. static int
  908. HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
  909. {
  910. SDL_HIDAPI_Device *device;
  911. SDL_JoystickID instance_id;
  912. int player_index = -1;
  913. device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
  914. if (device) {
  915. player_index = device->driver->GetDevicePlayerIndex(device, instance_id);
  916. }
  917. return player_index;
  918. }
  919. static void
  920. HIDAPI_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  921. {
  922. SDL_HIDAPI_Device *device;
  923. SDL_JoystickID instance_id;
  924. device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
  925. if (device) {
  926. device->driver->SetDevicePlayerIndex(device, instance_id, player_index);
  927. }
  928. }
  929. static SDL_JoystickGUID
  930. HIDAPI_JoystickGetDeviceGUID(int device_index)
  931. {
  932. SDL_HIDAPI_Device *device;
  933. SDL_JoystickGUID guid;
  934. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  935. if (device) {
  936. SDL_memcpy(&guid, &device->guid, sizeof(guid));
  937. } else {
  938. SDL_zero(guid);
  939. }
  940. return guid;
  941. }
  942. static SDL_JoystickID
  943. HIDAPI_JoystickGetDeviceInstanceID(int device_index)
  944. {
  945. SDL_JoystickID joystickID = -1;
  946. HIDAPI_GetDeviceByIndex(device_index, &joystickID);
  947. return joystickID;
  948. }
  949. static int
  950. HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
  951. {
  952. SDL_JoystickID joystickID = -1;
  953. SDL_HIDAPI_Device *device = HIDAPI_GetDeviceByIndex(device_index, &joystickID);
  954. struct joystick_hwdata *hwdata;
  955. if (!device || !device->driver) {
  956. /* This should never happen - validated before being called */
  957. return SDL_SetError("Couldn't find HIDAPI device at index %d\n", device_index);
  958. }
  959. hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
  960. if (!hwdata) {
  961. return SDL_OutOfMemory();
  962. }
  963. hwdata->device = device;
  964. if (!device->driver->OpenJoystick(device, joystick)) {
  965. /* The open failed, mark this device as disconnected and update devices */
  966. HIDAPI_JoystickDisconnected(device, joystickID);
  967. SDL_free(hwdata);
  968. return -1;
  969. }
  970. if (!joystick->serial && device->serial) {
  971. joystick->serial = SDL_strdup(device->serial);
  972. }
  973. joystick->hwdata = hwdata;
  974. return 0;
  975. }
  976. static int
  977. HIDAPI_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  978. {
  979. int result;
  980. if (joystick->hwdata) {
  981. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  982. result = device->driver->RumbleJoystick(device, joystick, low_frequency_rumble, high_frequency_rumble);
  983. } else {
  984. result = SDL_SetError("Rumble failed, device disconnected");
  985. }
  986. return result;
  987. }
  988. static int
  989. HIDAPI_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  990. {
  991. int result;
  992. if (joystick->hwdata) {
  993. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  994. result = device->driver->RumbleJoystickTriggers(device, joystick, left_rumble, right_rumble);
  995. } else {
  996. result = SDL_SetError("Rumble failed, device disconnected");
  997. }
  998. return result;
  999. }
  1000. static Uint32
  1001. HIDAPI_JoystickGetCapabilities(SDL_Joystick *joystick)
  1002. {
  1003. Uint32 result = 0;
  1004. if (joystick->hwdata) {
  1005. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1006. result = device->driver->GetJoystickCapabilities(device, joystick);
  1007. }
  1008. return result;
  1009. }
  1010. static int
  1011. HIDAPI_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1012. {
  1013. int result;
  1014. if (joystick->hwdata) {
  1015. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1016. result = device->driver->SetJoystickLED(device, joystick, red, green, blue);
  1017. } else {
  1018. result = SDL_SetError("SetLED failed, device disconnected");
  1019. }
  1020. return result;
  1021. }
  1022. static int
  1023. HIDAPI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  1024. {
  1025. int result;
  1026. if (joystick->hwdata) {
  1027. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1028. result = device->driver->SendJoystickEffect(device, joystick, data, size);
  1029. } else {
  1030. result = SDL_SetError("SendEffect failed, device disconnected");
  1031. }
  1032. return result;
  1033. }
  1034. static int
  1035. HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  1036. {
  1037. int result;
  1038. if (joystick->hwdata) {
  1039. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1040. result = device->driver->SetJoystickSensorsEnabled(device, joystick, enabled);
  1041. } else {
  1042. result = SDL_SetError("SetSensorsEnabled failed, device disconnected");
  1043. }
  1044. return result;
  1045. }
  1046. static void
  1047. HIDAPI_JoystickUpdate(SDL_Joystick *joystick)
  1048. {
  1049. /* This is handled in SDL_HIDAPI_UpdateDevices() */
  1050. }
  1051. static void
  1052. HIDAPI_JoystickClose(SDL_Joystick *joystick)
  1053. {
  1054. if (joystick->hwdata) {
  1055. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1056. int i;
  1057. /* Wait up to 30 ms for pending rumble to complete */
  1058. if (device->updating) {
  1059. /* Unlock the device so rumble can complete */
  1060. SDL_UnlockMutex(device->dev_lock);
  1061. }
  1062. for (i = 0; i < 3; ++i) {
  1063. if (SDL_AtomicGet(&device->rumble_pending) > 0) {
  1064. SDL_Delay(10);
  1065. }
  1066. }
  1067. if (device->updating) {
  1068. /* Relock the device */
  1069. SDL_LockMutex(device->dev_lock);
  1070. }
  1071. device->driver->CloseJoystick(device, joystick);
  1072. SDL_free(joystick->hwdata);
  1073. joystick->hwdata = NULL;
  1074. }
  1075. }
  1076. static void
  1077. HIDAPI_JoystickQuit(void)
  1078. {
  1079. int i;
  1080. shutting_down = SDL_TRUE;
  1081. SDL_HIDAPI_QuitRumble();
  1082. while (SDL_HIDAPI_devices) {
  1083. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  1084. if (device->parent) {
  1085. /* When a child device goes away, so does the parent */
  1086. device = device->parent;
  1087. for (i = 0; i < device->num_children; ++i) {
  1088. HIDAPI_DelDevice(device->children[i]);
  1089. }
  1090. HIDAPI_DelDevice(device);
  1091. } else {
  1092. HIDAPI_DelDevice(device);
  1093. }
  1094. }
  1095. /* Make sure the drivers cleaned up properly */
  1096. SDL_assert(SDL_HIDAPI_numjoysticks == 0);
  1097. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  1098. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  1099. SDL_DelHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
  1100. }
  1101. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS,
  1102. SDL_HIDAPIDriverHintChanged, NULL);
  1103. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  1104. SDL_HIDAPIDriverHintChanged, NULL);
  1105. SDL_hid_exit();
  1106. SDL_HIDAPI_change_count = 0;
  1107. shutting_down = SDL_FALSE;
  1108. initialized = SDL_FALSE;
  1109. }
  1110. static SDL_bool
  1111. HIDAPI_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1112. {
  1113. return SDL_FALSE;
  1114. }
  1115. SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
  1116. {
  1117. HIDAPI_JoystickInit,
  1118. HIDAPI_JoystickGetCount,
  1119. HIDAPI_JoystickDetect,
  1120. HIDAPI_JoystickGetDeviceName,
  1121. HIDAPI_JoystickGetDevicePath,
  1122. HIDAPI_JoystickGetDevicePlayerIndex,
  1123. HIDAPI_JoystickSetDevicePlayerIndex,
  1124. HIDAPI_JoystickGetDeviceGUID,
  1125. HIDAPI_JoystickGetDeviceInstanceID,
  1126. HIDAPI_JoystickOpen,
  1127. HIDAPI_JoystickRumble,
  1128. HIDAPI_JoystickRumbleTriggers,
  1129. HIDAPI_JoystickGetCapabilities,
  1130. HIDAPI_JoystickSetLED,
  1131. HIDAPI_JoystickSendEffect,
  1132. HIDAPI_JoystickSetSensorsEnabled,
  1133. HIDAPI_JoystickUpdate,
  1134. HIDAPI_JoystickClose,
  1135. HIDAPI_JoystickQuit,
  1136. HIDAPI_JoystickGetGamepadMapping
  1137. };
  1138. #endif /* SDL_JOYSTICK_HIDAPI */
  1139. /* vi: set ts=4 sw=4 expandtab: */