SDL_hidapijoystick.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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_sysjoystick.h"
  21. #include "SDL_hidapijoystick_c.h"
  22. #include "SDL_hidapi_rumble.h"
  23. #include "../../SDL_hints_c.h"
  24. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  25. #include "../windows/SDL_rawinputjoystick_c.h"
  26. #endif
  27. struct joystick_hwdata
  28. {
  29. SDL_HIDAPI_Device *device;
  30. };
  31. static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
  32. #ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
  33. &SDL_HIDAPI_DriverGameCube,
  34. #endif
  35. #ifdef SDL_JOYSTICK_HIDAPI_LUNA
  36. &SDL_HIDAPI_DriverLuna,
  37. #endif
  38. #ifdef SDL_JOYSTICK_HIDAPI_SHIELD
  39. &SDL_HIDAPI_DriverShield,
  40. #endif
  41. #ifdef SDL_JOYSTICK_HIDAPI_PS3
  42. &SDL_HIDAPI_DriverPS3,
  43. &SDL_HIDAPI_DriverPS3ThirdParty,
  44. &SDL_HIDAPI_DriverPS3SonySixaxis,
  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_STEAM_HORI
  59. &SDL_HIDAPI_DriverSteamHori,
  60. #endif
  61. #ifdef SDL_JOYSTICK_HIDAPI_STEAMDECK
  62. &SDL_HIDAPI_DriverSteamDeck,
  63. #endif
  64. #ifdef SDL_JOYSTICK_HIDAPI_SWITCH
  65. &SDL_HIDAPI_DriverNintendoClassic,
  66. &SDL_HIDAPI_DriverJoyCons,
  67. &SDL_HIDAPI_DriverSwitch,
  68. #endif
  69. #ifdef SDL_JOYSTICK_HIDAPI_SWITCH2
  70. &SDL_HIDAPI_DriverSwitch2,
  71. #endif
  72. #ifdef SDL_JOYSTICK_HIDAPI_WII
  73. &SDL_HIDAPI_DriverWii,
  74. #endif
  75. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  76. &SDL_HIDAPI_DriverXbox360,
  77. &SDL_HIDAPI_DriverXbox360W,
  78. #endif
  79. #ifdef SDL_JOYSTICK_HIDAPI_GIP
  80. &SDL_HIDAPI_DriverGIP,
  81. #endif
  82. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  83. &SDL_HIDAPI_DriverXboxOne,
  84. #endif
  85. #ifdef SDL_JOYSTICK_HIDAPI_LG4FF
  86. &SDL_HIDAPI_DriverLg4ff,
  87. #endif
  88. #ifdef SDL_JOYSTICK_HIDAPI_8BITDO
  89. &SDL_HIDAPI_Driver8BitDo,
  90. #endif
  91. #ifdef SDL_JOYSTICK_HIDAPI_FLYDIGI
  92. &SDL_HIDAPI_DriverFlydigi,
  93. #endif
  94. #ifdef SDL_JOYSTICK_HIDAPI_SINPUT
  95. &SDL_HIDAPI_DriverSInput,
  96. #endif
  97. };
  98. static int SDL_HIDAPI_numdrivers = 0;
  99. static SDL_AtomicInt SDL_HIDAPI_updating_devices;
  100. static bool SDL_HIDAPI_hints_changed = false;
  101. static Uint32 SDL_HIDAPI_change_count = 0;
  102. static SDL_HIDAPI_Device *SDL_HIDAPI_devices SDL_GUARDED_BY(SDL_joystick_lock);
  103. static int SDL_HIDAPI_numjoysticks = 0;
  104. static bool SDL_HIDAPI_combine_joycons = true;
  105. static bool initialized = false;
  106. static bool shutting_down = false;
  107. static char *HIDAPI_ConvertString(const wchar_t *wide_string)
  108. {
  109. char *string = NULL;
  110. if (wide_string) {
  111. string = SDL_iconv_string("UTF-8", "WCHAR_T", (char *)wide_string, (SDL_wcslen(wide_string) + 1) * sizeof(wchar_t));
  112. if (!string) {
  113. switch (sizeof(wchar_t)) {
  114. case 2:
  115. string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)wide_string, (SDL_wcslen(wide_string) + 1) * sizeof(wchar_t));
  116. break;
  117. case 4:
  118. string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char *)wide_string, (SDL_wcslen(wide_string) + 1) * sizeof(wchar_t));
  119. break;
  120. }
  121. }
  122. }
  123. return string;
  124. }
  125. void HIDAPI_DumpPacket(const char *prefix, const Uint8 *data, int size)
  126. {
  127. int i;
  128. char *buffer;
  129. size_t length = SDL_strlen(prefix) + 11 * (size / 8) + (5 * size * 2) + 1 + 1;
  130. int start = 0, amount = size;
  131. size_t current_len;
  132. buffer = (char *)SDL_malloc(length);
  133. current_len = SDL_snprintf(buffer, length, prefix, size);
  134. for (i = start; i < start + amount; ++i) {
  135. if ((i % 8) == 0) {
  136. current_len += SDL_snprintf(&buffer[current_len], length - current_len, "\n%.2d: ", i);
  137. }
  138. current_len += SDL_snprintf(&buffer[current_len], length - current_len, " 0x%.2x", data[i]);
  139. }
  140. SDL_strlcat(buffer, "\n", length);
  141. SDL_Log("%s", buffer);
  142. SDL_free(buffer);
  143. }
  144. bool HIDAPI_SupportsPlaystationDetection(Uint16 vendor, Uint16 product)
  145. {
  146. /* If we already know the controller is a different type, don't try to detect it.
  147. * This fixes a hang with the HORIPAD for Nintendo Switch (0x0f0d/0x00c1)
  148. */
  149. if (SDL_GetGamepadTypeFromVIDPID(vendor, product, NULL, false) != SDL_GAMEPAD_TYPE_STANDARD) {
  150. return false;
  151. }
  152. switch (vendor) {
  153. case USB_VENDOR_DRAGONRISE:
  154. return true;
  155. case USB_VENDOR_HORI:
  156. return true;
  157. case USB_VENDOR_LOGITECH:
  158. /* Most Logitech devices are not PlayStation controllers, and some of them
  159. * lock up or reset when we send them the Sony third-party query feature
  160. * report, so don't include that vendor here. Instead add devices as
  161. * appropriate to controller_list.h
  162. */
  163. return false;
  164. case USB_VENDOR_MADCATZ:
  165. if (product == USB_PRODUCT_MADCATZ_SAITEK_SIDE_PANEL_CONTROL_DECK) {
  166. // This is not a Playstation compatible device
  167. return false;
  168. }
  169. return true;
  170. case USB_VENDOR_MAYFLASH:
  171. return true;
  172. case USB_VENDOR_NACON:
  173. case USB_VENDOR_NACON_ALT:
  174. return true;
  175. case USB_VENDOR_PDP:
  176. return true;
  177. case USB_VENDOR_POWERA:
  178. return true;
  179. case USB_VENDOR_POWERA_ALT:
  180. return true;
  181. case USB_VENDOR_QANBA:
  182. return true;
  183. case USB_VENDOR_RAZER:
  184. /* Most Razer devices are not PlayStation controllers, and some of them
  185. * lock up or reset when we send them the Sony third-party query feature
  186. * report, so don't include that vendor here. Instead add devices as
  187. * appropriate to controller_list.h
  188. *
  189. * Reference: https://github.com/libsdl-org/SDL/issues/6733
  190. * https://github.com/libsdl-org/SDL/issues/6799
  191. */
  192. return false;
  193. case USB_VENDOR_SHANWAN:
  194. return true;
  195. case USB_VENDOR_SHANWAN_ALT:
  196. return true;
  197. case USB_VENDOR_THRUSTMASTER:
  198. /* Most of these are wheels, don't have the full set of effects, and
  199. * at least in the case of the T248 and T300 RS, the hid-tmff2 driver
  200. * puts them in a non-standard report mode and they can't be read.
  201. *
  202. * If these should use the HIDAPI driver, add them to controller_list.h
  203. */
  204. return false;
  205. case USB_VENDOR_ZEROPLUS:
  206. return true;
  207. case 0x7545 /* SZ-MYPOWER */:
  208. return true;
  209. default:
  210. return false;
  211. }
  212. }
  213. float HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max)
  214. {
  215. return output_min + (output_max - output_min) * (val - val_min) / (val_max - val_min);
  216. }
  217. static void HIDAPI_UpdateDeviceList(void);
  218. static void HIDAPI_JoystickClose(SDL_Joystick *joystick);
  219. static SDL_GamepadType SDL_GetJoystickGameControllerProtocol(const char *name, Uint16 vendor, Uint16 product, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  220. {
  221. static const int LIBUSB_CLASS_VENDOR_SPEC = 0xFF;
  222. static const int XB360_IFACE_SUBCLASS = 93;
  223. static const int XB360_IFACE_PROTOCOL = 1; // Wired
  224. static const int XB360W_IFACE_PROTOCOL = 129; // Wireless
  225. static const int XBONE_IFACE_SUBCLASS = 71;
  226. static const int XBONE_IFACE_PROTOCOL = 208;
  227. SDL_GamepadType type = SDL_GAMEPAD_TYPE_STANDARD;
  228. // This code should match the checks in libusb/hid.c and HIDDeviceManager.java
  229. if (interface_class == LIBUSB_CLASS_VENDOR_SPEC &&
  230. interface_subclass == XB360_IFACE_SUBCLASS &&
  231. (interface_protocol == XB360_IFACE_PROTOCOL ||
  232. interface_protocol == XB360W_IFACE_PROTOCOL)) {
  233. static const int SUPPORTED_VENDORS[] = {
  234. 0x0079, // GPD Win 2
  235. 0x044f, // Thrustmaster
  236. 0x045e, // Microsoft
  237. 0x046d, // Logitech
  238. 0x056e, // Elecom
  239. 0x06a3, // Saitek
  240. 0x0738, // Mad Catz
  241. 0x07ff, // Mad Catz
  242. 0x0e6f, // PDP
  243. 0x0f0d, // Hori
  244. 0x1038, // SteelSeries
  245. 0x11c9, // Nacon
  246. 0x12ab, // Unknown
  247. 0x1430, // RedOctane
  248. 0x146b, // BigBen
  249. 0x1532, // Razer
  250. 0x15e4, // Numark
  251. 0x162e, // Joytech
  252. 0x1689, // Razer Onza
  253. 0x1949, // Lab126, Inc.
  254. 0x1bad, // Harmonix
  255. 0x20d6, // PowerA
  256. 0x24c6, // PowerA
  257. 0x2c22, // Qanba
  258. 0x2dc8, // 8BitDo
  259. 0x9886, // ASTRO Gaming
  260. };
  261. int i;
  262. for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) {
  263. if (vendor == SUPPORTED_VENDORS[i]) {
  264. type = SDL_GAMEPAD_TYPE_XBOX360;
  265. break;
  266. }
  267. }
  268. }
  269. if (interface_number == 0 &&
  270. interface_class == LIBUSB_CLASS_VENDOR_SPEC &&
  271. interface_subclass == XBONE_IFACE_SUBCLASS &&
  272. interface_protocol == XBONE_IFACE_PROTOCOL) {
  273. static const int SUPPORTED_VENDORS[] = {
  274. 0x03f0, // HP
  275. 0x044f, // Thrustmaster
  276. 0x045e, // Microsoft
  277. 0x0738, // Mad Catz
  278. 0x0b05, // ASUS
  279. 0x0e6f, // PDP
  280. 0x0f0d, // Hori
  281. 0x10f5, // Turtle Beach
  282. 0x1532, // Razer
  283. 0x20d6, // PowerA
  284. 0x24c6, // PowerA
  285. 0x294b, // Snakebyte
  286. 0x2dc8, // 8BitDo
  287. 0x2e24, // Hyperkin
  288. 0x2e95, // SCUF
  289. 0x3285, // Nacon
  290. 0x3537, // GameSir
  291. 0x366c, // ByoWave
  292. };
  293. int i;
  294. for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) {
  295. if (vendor == SUPPORTED_VENDORS[i]) {
  296. type = SDL_GAMEPAD_TYPE_XBOXONE;
  297. break;
  298. }
  299. }
  300. }
  301. if (type == SDL_GAMEPAD_TYPE_STANDARD) {
  302. type = SDL_GetGamepadTypeFromVIDPID(vendor, product, name, false);
  303. }
  304. return type;
  305. }
  306. static bool HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  307. {
  308. int i;
  309. SDL_GamepadType type = SDL_GetJoystickGameControllerProtocol(name, vendor_id, product_id, -1, 0, 0, 0);
  310. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  311. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  312. if (driver->enabled && driver->IsSupportedDevice(NULL, name, type, vendor_id, product_id, version, -1, 0, 0, 0)) {
  313. return true;
  314. }
  315. }
  316. return false;
  317. }
  318. static SDL_HIDAPI_DeviceDriver *HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
  319. {
  320. const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
  321. const Uint16 USAGE_JOYSTICK = 0x0004;
  322. const Uint16 USAGE_GAMEPAD = 0x0005;
  323. const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
  324. int i;
  325. if (device->num_children > 0) {
  326. return &SDL_HIDAPI_DriverCombined;
  327. }
  328. if (SDL_ShouldIgnoreJoystick(device->vendor_id, device->product_id, device->version, device->name)) {
  329. return NULL;
  330. }
  331. if (device->vendor_id != USB_VENDOR_VALVE && device->vendor_id != USB_VENDOR_FLYDIGI) {
  332. if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
  333. return NULL;
  334. }
  335. if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
  336. return NULL;
  337. }
  338. }
  339. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  340. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  341. if (driver->enabled && driver->IsSupportedDevice(device, device->name, device->type, device->vendor_id, device->product_id, device->version, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol)) {
  342. return driver;
  343. }
  344. }
  345. return NULL;
  346. }
  347. static SDL_HIDAPI_Device *HIDAPI_GetDeviceByIndex(int device_index, SDL_JoystickID *pJoystickID)
  348. {
  349. SDL_HIDAPI_Device *device;
  350. SDL_AssertJoysticksLocked();
  351. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  352. if (device->parent || device->broken) {
  353. continue;
  354. }
  355. if (device->driver) {
  356. if (device_index < device->num_joysticks) {
  357. if (pJoystickID) {
  358. *pJoystickID = device->joysticks[device_index];
  359. }
  360. return device;
  361. }
  362. device_index -= device->num_joysticks;
  363. }
  364. }
  365. return NULL;
  366. }
  367. static SDL_HIDAPI_Device *HIDAPI_GetJoystickByInfo(const char *path, Uint16 vendor_id, Uint16 product_id)
  368. {
  369. SDL_HIDAPI_Device *device;
  370. SDL_AssertJoysticksLocked();
  371. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  372. if (device->vendor_id == vendor_id && device->product_id == product_id &&
  373. SDL_strcmp(device->path, path) == 0) {
  374. break;
  375. }
  376. }
  377. return device;
  378. }
  379. static void HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device)
  380. {
  381. if (!device->driver) {
  382. return; // Already cleaned up
  383. }
  384. // Disconnect any joysticks
  385. while (device->num_joysticks && device->joysticks) {
  386. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  387. }
  388. device->driver->FreeDevice(device);
  389. device->driver = NULL;
  390. SDL_LockMutex(device->dev_lock);
  391. {
  392. if (device->dev) {
  393. SDL_hid_close(device->dev);
  394. device->dev = NULL;
  395. }
  396. if (device->context) {
  397. SDL_free(device->context);
  398. device->context = NULL;
  399. }
  400. }
  401. SDL_UnlockMutex(device->dev_lock);
  402. }
  403. static void HIDAPI_SetupDeviceDriver(SDL_HIDAPI_Device *device, bool *removed) SDL_NO_THREAD_SAFETY_ANALYSIS // We unlock the joystick lock to be able to open the HID device on Android
  404. {
  405. *removed = false;
  406. if (device->driver) {
  407. bool enabled;
  408. if (device->vendor_id == USB_VENDOR_NINTENDO &&
  409. (device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR ||
  410. device->product_id == USB_PRODUCT_NINTENDO_SWITCH2_JOYCON_PAIR)) {
  411. enabled = SDL_HIDAPI_combine_joycons;
  412. } else {
  413. enabled = device->driver->enabled;
  414. }
  415. if (device->children) {
  416. int i;
  417. for (i = 0; i < device->num_children; ++i) {
  418. SDL_HIDAPI_Device *child = device->children[i];
  419. if (!child->driver || !child->driver->enabled) {
  420. enabled = false;
  421. break;
  422. }
  423. }
  424. }
  425. if (!enabled) {
  426. HIDAPI_CleanupDeviceDriver(device);
  427. }
  428. return; // Already setup
  429. }
  430. if (HIDAPI_GetDeviceDriver(device)) {
  431. // We might have a device driver for this device, try opening it and see
  432. if (device->num_children == 0) {
  433. SDL_hid_device *dev;
  434. // Wait a little bit for the device to initialize
  435. SDL_Delay(10);
  436. dev = SDL_hid_open_path(device->path);
  437. if (dev == NULL) {
  438. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  439. "HIDAPI_SetupDeviceDriver() couldn't open %s: %s",
  440. device->path, SDL_GetError());
  441. return;
  442. }
  443. SDL_hid_set_nonblocking(dev, 1);
  444. device->dev = dev;
  445. }
  446. device->driver = HIDAPI_GetDeviceDriver(device);
  447. // Initialize the device, which may cause a connected event
  448. if (device->driver && !device->driver->InitDevice(device)) {
  449. HIDAPI_CleanupDeviceDriver(device);
  450. }
  451. if (!device->driver && device->dev) {
  452. // No driver claimed this device, go ahead and close it
  453. SDL_hid_close(device->dev);
  454. device->dev = NULL;
  455. }
  456. }
  457. }
  458. static void SDL_HIDAPI_UpdateDrivers(void)
  459. {
  460. int i;
  461. SDL_HIDAPI_Device *device;
  462. bool removed;
  463. SDL_AssertJoysticksLocked();
  464. SDL_HIDAPI_numdrivers = 0;
  465. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  466. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  467. driver->enabled = driver->IsEnabled();
  468. if (driver->enabled && driver != &SDL_HIDAPI_DriverCombined) {
  469. ++SDL_HIDAPI_numdrivers;
  470. }
  471. }
  472. removed = false;
  473. do {
  474. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  475. HIDAPI_SetupDeviceDriver(device, &removed);
  476. if (removed) {
  477. break;
  478. }
  479. }
  480. } while (removed);
  481. }
  482. static void SDLCALL SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  483. {
  484. if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS) == 0) {
  485. SDL_HIDAPI_combine_joycons = SDL_GetStringBoolean(hint, true);
  486. }
  487. SDL_HIDAPI_hints_changed = true;
  488. SDL_HIDAPI_change_count = 0;
  489. }
  490. static bool HIDAPI_JoystickInit(void)
  491. {
  492. int i;
  493. if (initialized) {
  494. return true;
  495. }
  496. if (SDL_hid_init() < 0) {
  497. return SDL_SetError("Couldn't initialize hidapi");
  498. }
  499. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  500. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  501. driver->RegisterHints(SDL_HIDAPIDriverHintChanged, driver);
  502. }
  503. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS,
  504. SDL_HIDAPIDriverHintChanged, NULL);
  505. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  506. SDL_HIDAPIDriverHintChanged, NULL);
  507. SDL_HIDAPI_change_count = SDL_hid_device_change_count();
  508. HIDAPI_UpdateDeviceList();
  509. HIDAPI_UpdateDevices();
  510. initialized = true;
  511. return true;
  512. }
  513. static bool HIDAPI_AddJoystickInstanceToDevice(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  514. {
  515. SDL_JoystickID *joysticks = (SDL_JoystickID *)SDL_realloc(device->joysticks, (device->num_joysticks + 1) * sizeof(*device->joysticks));
  516. if (!joysticks) {
  517. return false;
  518. }
  519. device->joysticks = joysticks;
  520. device->joysticks[device->num_joysticks++] = joystickID;
  521. return true;
  522. }
  523. static bool HIDAPI_DelJoystickInstanceFromDevice(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  524. {
  525. int i, size;
  526. for (i = 0; i < device->num_joysticks; ++i) {
  527. if (device->joysticks[i] == joystickID) {
  528. size = (device->num_joysticks - i - 1) * sizeof(SDL_JoystickID);
  529. SDL_memmove(&device->joysticks[i], &device->joysticks[i + 1], size);
  530. --device->num_joysticks;
  531. if (device->num_joysticks == 0) {
  532. SDL_free(device->joysticks);
  533. device->joysticks = NULL;
  534. }
  535. return true;
  536. }
  537. }
  538. return false;
  539. }
  540. static bool HIDAPI_JoystickInstanceIsUnique(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  541. {
  542. if (device->parent && device->num_joysticks == 1 && device->parent->num_joysticks == 1 &&
  543. device->joysticks[0] == device->parent->joysticks[0]) {
  544. return false;
  545. }
  546. return true;
  547. }
  548. void HIDAPI_SetDeviceName(SDL_HIDAPI_Device *device, const char *name)
  549. {
  550. if (name && *name && SDL_strcmp(name, device->name) != 0) {
  551. SDL_free(device->name);
  552. device->name = SDL_strdup(name);
  553. SDL_SetJoystickGUIDCRC(&device->guid, SDL_crc16(0, name, SDL_strlen(name)));
  554. }
  555. }
  556. void HIDAPI_SetDeviceProduct(SDL_HIDAPI_Device *device, Uint16 vendor_id, Uint16 product_id)
  557. {
  558. // Don't set the device product ID directly, or we'll constantly re-enumerate this device
  559. device->guid = SDL_CreateJoystickGUID(device->guid.data[0], vendor_id, product_id, device->version, device->manufacturer_string, device->product_string, 'h', 0);
  560. }
  561. static void HIDAPI_UpdateJoystickSerial(SDL_HIDAPI_Device *device)
  562. {
  563. int i;
  564. SDL_AssertJoysticksLocked();
  565. for (i = 0; i < device->num_joysticks; ++i) {
  566. SDL_Joystick *joystick = SDL_GetJoystickFromID(device->joysticks[i]);
  567. if (joystick && device->serial) {
  568. SDL_free(joystick->serial);
  569. joystick->serial = SDL_strdup(device->serial);
  570. }
  571. }
  572. }
  573. static bool HIDAPI_SerialIsEmpty(SDL_HIDAPI_Device *device)
  574. {
  575. bool all_zeroes = true;
  576. if (device->serial) {
  577. const char *serial = device->serial;
  578. for (serial = device->serial; *serial; ++serial) {
  579. if (*serial != '0') {
  580. all_zeroes = false;
  581. break;
  582. }
  583. }
  584. }
  585. return all_zeroes;
  586. }
  587. void HIDAPI_SetDeviceSerial(SDL_HIDAPI_Device *device, const char *serial)
  588. {
  589. if (serial && *serial && (!device->serial || SDL_strcmp(serial, device->serial) != 0)) {
  590. SDL_free(device->serial);
  591. device->serial = SDL_strdup(serial);
  592. HIDAPI_UpdateJoystickSerial(device);
  593. }
  594. }
  595. static int wcstrcmp(const wchar_t *str1, const char *str2)
  596. {
  597. int result;
  598. while (1) {
  599. result = (*str1 - *str2);
  600. if (result != 0 || *str1 == 0) {
  601. break;
  602. }
  603. ++str1;
  604. ++str2;
  605. }
  606. return result;
  607. }
  608. static void HIDAPI_SetDeviceSerialW(SDL_HIDAPI_Device *device, const wchar_t *serial)
  609. {
  610. if (serial && *serial && (!device->serial || wcstrcmp(serial, device->serial) != 0)) {
  611. SDL_free(device->serial);
  612. device->serial = HIDAPI_ConvertString(serial);
  613. HIDAPI_UpdateJoystickSerial(device);
  614. }
  615. }
  616. bool HIDAPI_HasConnectedUSBDevice(const char *serial)
  617. {
  618. SDL_HIDAPI_Device *device;
  619. SDL_AssertJoysticksLocked();
  620. if (!serial) {
  621. return false;
  622. }
  623. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  624. if (!device->driver || device->broken) {
  625. continue;
  626. }
  627. if (device->is_bluetooth) {
  628. continue;
  629. }
  630. if (device->serial && SDL_strcmp(serial, device->serial) == 0) {
  631. return true;
  632. }
  633. }
  634. return false;
  635. }
  636. void HIDAPI_DisconnectBluetoothDevice(const char *serial)
  637. {
  638. SDL_HIDAPI_Device *device;
  639. SDL_AssertJoysticksLocked();
  640. if (!serial) {
  641. return;
  642. }
  643. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  644. if (!device->driver || device->broken) {
  645. continue;
  646. }
  647. if (!device->is_bluetooth) {
  648. continue;
  649. }
  650. if (device->serial && SDL_strcmp(serial, device->serial) == 0) {
  651. while (device->num_joysticks && device->joysticks) {
  652. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  653. }
  654. }
  655. }
  656. }
  657. bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID)
  658. {
  659. int i, j;
  660. SDL_JoystickID joystickID;
  661. SDL_AssertJoysticksLocked();
  662. for (i = 0; i < device->num_children; ++i) {
  663. SDL_HIDAPI_Device *child = device->children[i];
  664. for (j = child->num_joysticks; j--;) {
  665. HIDAPI_JoystickDisconnected(child, child->joysticks[j]);
  666. }
  667. }
  668. joystickID = SDL_GetNextObjectID();
  669. HIDAPI_AddJoystickInstanceToDevice(device, joystickID);
  670. for (i = 0; i < device->num_children; ++i) {
  671. SDL_HIDAPI_Device *child = device->children[i];
  672. HIDAPI_AddJoystickInstanceToDevice(child, joystickID);
  673. }
  674. ++SDL_HIDAPI_numjoysticks;
  675. if (pJoystickID) {
  676. *pJoystickID = joystickID;
  677. }
  678. SDL_PrivateJoystickAdded(joystickID);
  679. return true;
  680. }
  681. void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
  682. {
  683. int i, j;
  684. SDL_LockJoysticks();
  685. if (!HIDAPI_JoystickInstanceIsUnique(device, joystickID)) {
  686. // Disconnecting a child always disconnects the parent
  687. device = device->parent;
  688. }
  689. for (i = 0; i < device->num_joysticks; ++i) {
  690. if (device->joysticks[i] == joystickID) {
  691. SDL_Joystick *joystick = SDL_GetJoystickFromID(joystickID);
  692. if (joystick) {
  693. HIDAPI_JoystickClose(joystick);
  694. }
  695. HIDAPI_DelJoystickInstanceFromDevice(device, joystickID);
  696. for (j = 0; j < device->num_children; ++j) {
  697. SDL_HIDAPI_Device *child = device->children[j];
  698. HIDAPI_DelJoystickInstanceFromDevice(child, joystickID);
  699. }
  700. --SDL_HIDAPI_numjoysticks;
  701. if (!shutting_down) {
  702. SDL_PrivateJoystickRemoved(joystickID);
  703. }
  704. }
  705. }
  706. // Rescan the device list in case device state has changed
  707. SDL_HIDAPI_change_count = 0;
  708. SDL_UnlockJoysticks();
  709. }
  710. static void HIDAPI_UpdateJoystickProperties(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  711. {
  712. SDL_PropertiesID props = SDL_GetJoystickProperties(joystick);
  713. Uint32 caps = device->driver->GetJoystickCapabilities(device, joystick);
  714. if (caps & SDL_JOYSTICK_CAP_MONO_LED) {
  715. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN, true);
  716. } else {
  717. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_MONO_LED_BOOLEAN, false);
  718. }
  719. if (caps & SDL_JOYSTICK_CAP_RGB_LED) {
  720. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, true);
  721. } else {
  722. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RGB_LED_BOOLEAN, false);
  723. }
  724. if (caps & SDL_JOYSTICK_CAP_PLAYER_LED) {
  725. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_PLAYER_LED_BOOLEAN, true);
  726. } else {
  727. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_PLAYER_LED_BOOLEAN, false);
  728. }
  729. if (caps & SDL_JOYSTICK_CAP_RUMBLE) {
  730. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);
  731. } else {
  732. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false);
  733. }
  734. if (caps & SDL_JOYSTICK_CAP_TRIGGER_RUMBLE) {
  735. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, true);
  736. } else {
  737. SDL_SetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, false);
  738. }
  739. }
  740. void HIDAPI_UpdateDeviceProperties(SDL_HIDAPI_Device *device)
  741. {
  742. int i;
  743. SDL_LockJoysticks();
  744. for (i = 0; i < device->num_joysticks; ++i) {
  745. SDL_Joystick *joystick = SDL_GetJoystickFromID(device->joysticks[i]);
  746. if (joystick) {
  747. HIDAPI_UpdateJoystickProperties(device, joystick);
  748. }
  749. }
  750. SDL_UnlockJoysticks();
  751. }
  752. static int HIDAPI_JoystickGetCount(void)
  753. {
  754. return SDL_HIDAPI_numjoysticks;
  755. }
  756. static SDL_HIDAPI_Device *HIDAPI_AddDevice(const struct SDL_hid_device_info *info, int num_children, SDL_HIDAPI_Device **children)
  757. {
  758. SDL_HIDAPI_Device *device;
  759. SDL_HIDAPI_Device *curr, *last = NULL;
  760. bool removed;
  761. Uint16 bus;
  762. SDL_AssertJoysticksLocked();
  763. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  764. }
  765. device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
  766. if (!device) {
  767. return NULL;
  768. }
  769. SDL_SetObjectValid(device, SDL_OBJECT_TYPE_HIDAPI_JOYSTICK, true);
  770. if (info->path) {
  771. device->path = SDL_strdup(info->path);
  772. }
  773. device->seen = true;
  774. device->vendor_id = info->vendor_id;
  775. device->product_id = info->product_id;
  776. device->version = info->release_number;
  777. device->interface_number = info->interface_number;
  778. device->interface_class = info->interface_class;
  779. device->interface_subclass = info->interface_subclass;
  780. device->interface_protocol = info->interface_protocol;
  781. device->usage_page = info->usage_page;
  782. device->usage = info->usage;
  783. device->is_bluetooth = (info->bus_type == SDL_HID_API_BUS_BLUETOOTH);
  784. device->dev_lock = SDL_CreateMutex();
  785. // Need the device name before getting the driver to know whether to ignore this device
  786. {
  787. char *serial_number = HIDAPI_ConvertString(info->serial_number);
  788. device->manufacturer_string = HIDAPI_ConvertString(info->manufacturer_string);
  789. device->product_string = HIDAPI_ConvertString(info->product_string);
  790. device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, device->manufacturer_string, device->product_string);
  791. if (serial_number && *serial_number) {
  792. device->serial = serial_number;
  793. } else {
  794. SDL_free(serial_number);
  795. }
  796. if (!device->name) {
  797. SDL_free(device->manufacturer_string);
  798. SDL_free(device->product_string);
  799. SDL_free(device->serial);
  800. SDL_free(device->path);
  801. SDL_free(device);
  802. return NULL;
  803. }
  804. }
  805. if (info->bus_type == SDL_HID_API_BUS_BLUETOOTH) {
  806. bus = SDL_HARDWARE_BUS_BLUETOOTH;
  807. } else {
  808. bus = SDL_HARDWARE_BUS_USB;
  809. }
  810. device->guid = SDL_CreateJoystickGUID(bus, device->vendor_id, device->product_id, device->version, device->manufacturer_string, device->product_string, 'h', 0);
  811. device->joystick_type = SDL_JOYSTICK_TYPE_GAMEPAD;
  812. device->type = SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
  813. device->steam_virtual_gamepad_slot = -1;
  814. if (num_children > 0) {
  815. int i;
  816. device->num_children = num_children;
  817. device->children = children;
  818. for (i = 0; i < num_children; ++i) {
  819. children[i]->parent = device;
  820. }
  821. }
  822. // Add it to the list
  823. if (last) {
  824. last->next = device;
  825. } else {
  826. SDL_HIDAPI_devices = device;
  827. }
  828. removed = false;
  829. HIDAPI_SetupDeviceDriver(device, &removed);
  830. if (removed) {
  831. return NULL;
  832. }
  833. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "Added HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, bluetooth %d, 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)", device->name, device->vendor_id, device->product_id, device->is_bluetooth, device->version,
  834. device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage,
  835. device->path, device->driver ? device->driver->name : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
  836. return device;
  837. }
  838. static void HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
  839. {
  840. SDL_HIDAPI_Device *curr, *last;
  841. int i;
  842. SDL_AssertJoysticksLocked();
  843. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT, "Removing HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, bluetooth %d, 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)", device->name, device->vendor_id, device->product_id, device->is_bluetooth, device->version,
  844. device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage,
  845. device->path, device->driver ? device->driver->name : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
  846. for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
  847. if (curr == device) {
  848. if (last) {
  849. last->next = curr->next;
  850. } else {
  851. SDL_HIDAPI_devices = curr->next;
  852. }
  853. HIDAPI_CleanupDeviceDriver(device);
  854. // Make sure the rumble thread is done with this device
  855. while (SDL_GetAtomicInt(&device->rumble_pending) > 0) {
  856. SDL_Delay(10);
  857. }
  858. for (i = 0; i < device->num_children; ++i) {
  859. device->children[i]->parent = NULL;
  860. }
  861. SDL_SetObjectValid(device, SDL_OBJECT_TYPE_HIDAPI_JOYSTICK, false);
  862. SDL_DestroyMutex(device->dev_lock);
  863. SDL_free(device->manufacturer_string);
  864. SDL_free(device->product_string);
  865. SDL_free(device->serial);
  866. SDL_free(device->name);
  867. SDL_free(device->path);
  868. SDL_free(device->children);
  869. SDL_free(device);
  870. return;
  871. }
  872. }
  873. }
  874. static bool HIDAPI_CreateCombinedJoyCons(void)
  875. {
  876. SDL_HIDAPI_Device *device, *combined;
  877. SDL_HIDAPI_Device *joycons[2] = { NULL, NULL };
  878. SDL_AssertJoysticksLocked();
  879. if (!SDL_HIDAPI_combine_joycons) {
  880. return false;
  881. }
  882. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  883. Uint16 vendor, product;
  884. if (!device->driver) {
  885. // Unsupported device
  886. continue;
  887. }
  888. if (device->parent) {
  889. // This device is already part of a combined device
  890. continue;
  891. }
  892. if (device->broken) {
  893. // This device can't be used
  894. continue;
  895. }
  896. SDL_GetJoystickGUIDInfo(device->guid, &vendor, &product, NULL, NULL);
  897. if (!joycons[0] &&
  898. (SDL_IsJoystickNintendoSwitchJoyConLeft(vendor, product) ||
  899. (SDL_IsJoystickNintendoSwitchJoyConGrip(vendor, product) &&
  900. SDL_strstr(device->name, "(L)") != NULL))) {
  901. joycons[0] = device;
  902. }
  903. if (!joycons[1] &&
  904. (SDL_IsJoystickNintendoSwitchJoyConRight(vendor, product) ||
  905. (SDL_IsJoystickNintendoSwitchJoyConGrip(vendor, product) &&
  906. SDL_strstr(device->name, "(R)") != NULL))) {
  907. joycons[1] = device;
  908. }
  909. if (joycons[0] && joycons[1]) {
  910. SDL_hid_device_info info;
  911. SDL_HIDAPI_Device **children = (SDL_HIDAPI_Device **)SDL_malloc(2 * sizeof(SDL_HIDAPI_Device *));
  912. if (!children) {
  913. return false;
  914. }
  915. children[0] = joycons[0];
  916. children[1] = joycons[1];
  917. SDL_zero(info);
  918. info.path = "nintendo_joycons_combined";
  919. info.vendor_id = USB_VENDOR_NINTENDO;
  920. if (joycons[0]->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT) {
  921. info.product_id = USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR;
  922. } else {
  923. info.product_id = USB_PRODUCT_NINTENDO_SWITCH2_JOYCON_PAIR;
  924. }
  925. info.interface_number = -1;
  926. info.usage_page = USB_USAGEPAGE_GENERIC_DESKTOP;
  927. info.usage = USB_USAGE_GENERIC_GAMEPAD;
  928. info.manufacturer_string = L"Nintendo";
  929. info.product_string = L"Switch Joy-Con (L/R)";
  930. if (children[0]->is_bluetooth || children[1]->is_bluetooth) {
  931. info.bus_type = SDL_HID_API_BUS_BLUETOOTH;
  932. } else {
  933. info.bus_type = SDL_HID_API_BUS_USB;
  934. }
  935. combined = HIDAPI_AddDevice(&info, 2, children);
  936. if (combined && combined->driver) {
  937. return true;
  938. } else {
  939. if (combined) {
  940. HIDAPI_DelDevice(combined);
  941. } else {
  942. SDL_free(children);
  943. }
  944. return false;
  945. }
  946. }
  947. }
  948. return false;
  949. }
  950. static void HIDAPI_UpdateDeviceList(void)
  951. {
  952. SDL_HIDAPI_Device *device;
  953. struct SDL_hid_device_info *devs, *info;
  954. SDL_LockJoysticks();
  955. if (SDL_HIDAPI_hints_changed) {
  956. SDL_HIDAPI_UpdateDrivers();
  957. SDL_HIDAPI_hints_changed = false;
  958. }
  959. // Prepare the existing device list
  960. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  961. if (device->children) {
  962. continue;
  963. }
  964. device->seen = false;
  965. }
  966. // Enumerate the devices
  967. if (SDL_HIDAPI_numdrivers > 0) {
  968. devs = SDL_hid_enumerate(0, 0);
  969. if (devs) {
  970. for (info = devs; info; info = info->next) {
  971. device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
  972. if (device) {
  973. device->seen = true;
  974. // Check to see if the serial number is available now
  975. if(HIDAPI_SerialIsEmpty(device)) {
  976. HIDAPI_SetDeviceSerialW(device, info->serial_number);
  977. }
  978. } else {
  979. HIDAPI_AddDevice(info, 0, NULL);
  980. }
  981. }
  982. SDL_hid_free_enumeration(devs);
  983. }
  984. }
  985. // Remove any devices that weren't seen or have been disconnected due to read errors
  986. check_removed:
  987. device = SDL_HIDAPI_devices;
  988. while (device) {
  989. SDL_HIDAPI_Device *next = device->next;
  990. if (!device->seen ||
  991. ((device->driver || device->children) && device->num_joysticks == 0 && !device->dev)) {
  992. if (device->parent) {
  993. // When a child device goes away, so does the parent
  994. int i;
  995. device = device->parent;
  996. for (i = 0; i < device->num_children; ++i) {
  997. HIDAPI_DelDevice(device->children[i]);
  998. }
  999. HIDAPI_DelDevice(device);
  1000. // Update the device list again to pick up any children left
  1001. SDL_HIDAPI_change_count = 0;
  1002. // We deleted more than one device here, restart the loop
  1003. goto check_removed;
  1004. } else {
  1005. HIDAPI_DelDevice(device);
  1006. device = NULL;
  1007. // Update the device list again in case this device comes back
  1008. SDL_HIDAPI_change_count = 0;
  1009. }
  1010. }
  1011. if (device && device->broken && device->parent) {
  1012. HIDAPI_DelDevice(device->parent);
  1013. // We deleted a different device here, restart the loop
  1014. goto check_removed;
  1015. }
  1016. device = next;
  1017. }
  1018. // See if we can create any combined Joy-Con controllers
  1019. while (HIDAPI_CreateCombinedJoyCons()) {
  1020. }
  1021. SDL_UnlockJoysticks();
  1022. }
  1023. static bool HIDAPI_IsEquivalentToDevice(Uint16 vendor_id, Uint16 product_id, SDL_HIDAPI_Device *device)
  1024. {
  1025. if (vendor_id == device->vendor_id && product_id == device->product_id) {
  1026. return true;
  1027. }
  1028. if (vendor_id == USB_VENDOR_MICROSOFT) {
  1029. // If we're looking for the wireless XBox 360 controller, also look for the dongle
  1030. if (product_id == USB_PRODUCT_XBOX360_XUSB_CONTROLLER && device->product_id == USB_PRODUCT_XBOX360_WIRELESS_RECEIVER) {
  1031. return true;
  1032. }
  1033. // If we're looking for the raw input Xbox One controller, match it against any other Xbox One controller
  1034. if (product_id == USB_PRODUCT_XBOX_ONE_XBOXGIP_CONTROLLER &&
  1035. device->type == SDL_GAMEPAD_TYPE_XBOXONE) {
  1036. return true;
  1037. }
  1038. // If we're looking for an XInput controller, match it against any other Xbox controller
  1039. if (product_id == USB_PRODUCT_XBOX360_XUSB_CONTROLLER) {
  1040. if (device->type == SDL_GAMEPAD_TYPE_XBOX360 || device->type == SDL_GAMEPAD_TYPE_XBOXONE) {
  1041. return true;
  1042. }
  1043. }
  1044. }
  1045. if (vendor_id == USB_VENDOR_NVIDIA) {
  1046. // If we're looking for the NVIDIA SHIELD controller Xbox interface, match it against any NVIDIA SHIELD controller
  1047. if (product_id == 0xb400 &&
  1048. SDL_IsJoystickNVIDIASHIELDController(vendor_id, product_id)) {
  1049. return true;
  1050. }
  1051. }
  1052. return false;
  1053. }
  1054. static bool HIDAPI_StartUpdatingDevices(void)
  1055. {
  1056. return SDL_CompareAndSwapAtomicInt(&SDL_HIDAPI_updating_devices, false, true);
  1057. }
  1058. static void HIDAPI_FinishUpdatingDevices(void)
  1059. {
  1060. SDL_SetAtomicInt(&SDL_HIDAPI_updating_devices, false);
  1061. }
  1062. bool HIDAPI_IsDeviceTypePresent(SDL_GamepadType type)
  1063. {
  1064. SDL_HIDAPI_Device *device;
  1065. bool result = false;
  1066. // Make sure we're initialized, as this could be called from other drivers during startup
  1067. if (!HIDAPI_JoystickInit()) {
  1068. return false;
  1069. }
  1070. if (HIDAPI_StartUpdatingDevices()) {
  1071. HIDAPI_UpdateDeviceList();
  1072. HIDAPI_FinishUpdatingDevices();
  1073. }
  1074. SDL_LockJoysticks();
  1075. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  1076. if (device->driver && device->type == type) {
  1077. result = true;
  1078. break;
  1079. }
  1080. }
  1081. SDL_UnlockJoysticks();
  1082. #ifdef DEBUG_HIDAPI
  1083. SDL_Log("HIDAPI_IsDeviceTypePresent() returning %s for %d", result ? "true" : "false", type);
  1084. #endif
  1085. return result;
  1086. }
  1087. bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  1088. {
  1089. SDL_HIDAPI_Device *device;
  1090. bool supported = false;
  1091. bool result = false;
  1092. // Make sure we're initialized, as this could be called from other drivers during startup
  1093. if (!HIDAPI_JoystickInit()) {
  1094. return false;
  1095. }
  1096. /* Only update the device list for devices we know might be supported.
  1097. If we did this for every device, it would hit the USB driver too hard and potentially
  1098. lock up the system. This won't catch devices that we support but can only detect using
  1099. USB interface details, like Xbox controllers, but hopefully the device list update is
  1100. responsive enough to catch those.
  1101. */
  1102. supported = HIDAPI_IsDeviceSupported(vendor_id, product_id, version, name);
  1103. #if defined(SDL_JOYSTICK_HIDAPI_XBOX360) || defined(SDL_JOYSTICK_HIDAPI_XBOXONE)
  1104. if (!supported &&
  1105. (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box") || SDL_strstr(name, "XBOX"))) {
  1106. supported = true;
  1107. }
  1108. #endif // SDL_JOYSTICK_HIDAPI_XBOX360 || SDL_JOYSTICK_HIDAPI_XBOXONE
  1109. if (supported) {
  1110. if (HIDAPI_StartUpdatingDevices()) {
  1111. HIDAPI_UpdateDeviceList();
  1112. HIDAPI_FinishUpdatingDevices();
  1113. }
  1114. }
  1115. /* Note that this isn't a perfect check - there may be multiple devices with 0 VID/PID,
  1116. or a different name than we have it listed here, etc, but if we support the device
  1117. and we have something similar in our device list, mark it as present.
  1118. */
  1119. SDL_LockJoysticks();
  1120. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  1121. if (device->driver &&
  1122. HIDAPI_IsEquivalentToDevice(vendor_id, product_id, device)) {
  1123. result = true;
  1124. break;
  1125. }
  1126. }
  1127. SDL_UnlockJoysticks();
  1128. #ifdef DEBUG_HIDAPI
  1129. SDL_Log("HIDAPI_IsDevicePresent() returning %s for 0x%.4x / 0x%.4x", result ? "true" : "false", vendor_id, product_id);
  1130. #endif
  1131. return result;
  1132. }
  1133. char *HIDAPI_GetDeviceProductName(Uint16 vendor_id, Uint16 product_id)
  1134. {
  1135. SDL_HIDAPI_Device *device;
  1136. char *name = NULL;
  1137. SDL_LockJoysticks();
  1138. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  1139. if (vendor_id == device->vendor_id && product_id == device->product_id) {
  1140. if (device->product_string) {
  1141. name = SDL_strdup(device->product_string);
  1142. }
  1143. break;
  1144. }
  1145. }
  1146. SDL_UnlockJoysticks();
  1147. return name;
  1148. }
  1149. char *HIDAPI_GetDeviceManufacturerName(Uint16 vendor_id, Uint16 product_id)
  1150. {
  1151. SDL_HIDAPI_Device *device;
  1152. char *name = NULL;
  1153. SDL_LockJoysticks();
  1154. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  1155. if (vendor_id == device->vendor_id && product_id == device->product_id) {
  1156. if (device->manufacturer_string) {
  1157. name = SDL_strdup(device->manufacturer_string);
  1158. }
  1159. break;
  1160. }
  1161. }
  1162. SDL_UnlockJoysticks();
  1163. return name;
  1164. }
  1165. SDL_JoystickType HIDAPI_GetJoystickTypeFromGUID(SDL_GUID guid)
  1166. {
  1167. SDL_HIDAPI_Device *device;
  1168. SDL_JoystickType type = SDL_JOYSTICK_TYPE_UNKNOWN;
  1169. SDL_LockJoysticks();
  1170. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  1171. if (SDL_memcmp(&guid, &device->guid, sizeof(guid)) == 0) {
  1172. type = device->joystick_type;
  1173. break;
  1174. }
  1175. }
  1176. SDL_UnlockJoysticks();
  1177. return type;
  1178. }
  1179. SDL_GamepadType HIDAPI_GetGamepadTypeFromGUID(SDL_GUID guid)
  1180. {
  1181. SDL_HIDAPI_Device *device;
  1182. SDL_GamepadType type = SDL_GAMEPAD_TYPE_STANDARD;
  1183. SDL_LockJoysticks();
  1184. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  1185. if (SDL_memcmp(&guid, &device->guid, sizeof(guid)) == 0) {
  1186. type = device->type;
  1187. break;
  1188. }
  1189. }
  1190. SDL_UnlockJoysticks();
  1191. return type;
  1192. }
  1193. static void HIDAPI_JoystickDetect(void)
  1194. {
  1195. if (HIDAPI_StartUpdatingDevices()) {
  1196. Uint32 count = SDL_hid_device_change_count();
  1197. if (SDL_HIDAPI_change_count != count) {
  1198. SDL_HIDAPI_change_count = count;
  1199. HIDAPI_UpdateDeviceList();
  1200. }
  1201. HIDAPI_FinishUpdatingDevices();
  1202. }
  1203. }
  1204. void HIDAPI_UpdateDevices(void)
  1205. {
  1206. SDL_HIDAPI_Device *device;
  1207. SDL_AssertJoysticksLocked();
  1208. // Update the devices, which may change connected joysticks and send events
  1209. // Prepare the existing device list
  1210. if (HIDAPI_StartUpdatingDevices()) {
  1211. for (device = SDL_HIDAPI_devices; device; device = device->next) {
  1212. if (device->parent) {
  1213. continue;
  1214. }
  1215. if (device->driver) {
  1216. if (SDL_TryLockMutex(device->dev_lock)) {
  1217. device->updating = true;
  1218. device->driver->UpdateDevice(device);
  1219. device->updating = false;
  1220. SDL_UnlockMutex(device->dev_lock);
  1221. }
  1222. }
  1223. }
  1224. HIDAPI_FinishUpdatingDevices();
  1225. }
  1226. }
  1227. static const char *HIDAPI_JoystickGetDeviceName(int device_index)
  1228. {
  1229. SDL_HIDAPI_Device *device;
  1230. const char *name = NULL;
  1231. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  1232. if (device) {
  1233. // FIXME: The device could be freed after this name is returned...
  1234. name = device->name;
  1235. }
  1236. return name;
  1237. }
  1238. static const char *HIDAPI_JoystickGetDevicePath(int device_index)
  1239. {
  1240. SDL_HIDAPI_Device *device;
  1241. const char *path = NULL;
  1242. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  1243. if (device) {
  1244. // FIXME: The device could be freed after this path is returned...
  1245. path = device->path;
  1246. }
  1247. return path;
  1248. }
  1249. static int HIDAPI_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
  1250. {
  1251. SDL_HIDAPI_Device *device;
  1252. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  1253. if (device) {
  1254. return device->steam_virtual_gamepad_slot;
  1255. }
  1256. return -1;
  1257. }
  1258. static int HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
  1259. {
  1260. SDL_HIDAPI_Device *device;
  1261. SDL_JoystickID instance_id;
  1262. int player_index = -1;
  1263. device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
  1264. if (device) {
  1265. player_index = device->driver->GetDevicePlayerIndex(device, instance_id);
  1266. }
  1267. return player_index;
  1268. }
  1269. static void HIDAPI_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  1270. {
  1271. SDL_HIDAPI_Device *device;
  1272. SDL_JoystickID instance_id;
  1273. device = HIDAPI_GetDeviceByIndex(device_index, &instance_id);
  1274. if (device) {
  1275. device->driver->SetDevicePlayerIndex(device, instance_id, player_index);
  1276. }
  1277. }
  1278. static SDL_GUID HIDAPI_JoystickGetDeviceGUID(int device_index)
  1279. {
  1280. SDL_HIDAPI_Device *device;
  1281. SDL_GUID guid;
  1282. device = HIDAPI_GetDeviceByIndex(device_index, NULL);
  1283. if (device) {
  1284. SDL_memcpy(&guid, &device->guid, sizeof(guid));
  1285. } else {
  1286. SDL_zero(guid);
  1287. }
  1288. return guid;
  1289. }
  1290. static SDL_JoystickID HIDAPI_JoystickGetDeviceInstanceID(int device_index)
  1291. {
  1292. SDL_JoystickID joystickID = 0;
  1293. HIDAPI_GetDeviceByIndex(device_index, &joystickID);
  1294. return joystickID;
  1295. }
  1296. static bool HIDAPI_JoystickOpen(SDL_Joystick *joystick, int device_index)
  1297. {
  1298. SDL_JoystickID joystickID = 0;
  1299. SDL_HIDAPI_Device *device = HIDAPI_GetDeviceByIndex(device_index, &joystickID);
  1300. struct joystick_hwdata *hwdata;
  1301. SDL_AssertJoysticksLocked();
  1302. if (!device || !device->driver || device->broken) {
  1303. // This should never happen - validated before being called
  1304. return SDL_SetError("Couldn't find HIDAPI device at index %d", device_index);
  1305. }
  1306. hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
  1307. if (!hwdata) {
  1308. return false;
  1309. }
  1310. hwdata->device = device;
  1311. // Process any pending reports before opening the device
  1312. SDL_LockMutex(device->dev_lock);
  1313. device->updating = true;
  1314. device->driver->UpdateDevice(device);
  1315. device->updating = false;
  1316. SDL_UnlockMutex(device->dev_lock);
  1317. // UpdateDevice() may have called HIDAPI_JoystickDisconnected() if the device went away
  1318. if (device->num_joysticks == 0) {
  1319. SDL_free(hwdata);
  1320. return SDL_SetError("HIDAPI device disconnected while opening");
  1321. }
  1322. // Set the default connection state, can be overridden below
  1323. if (device->is_bluetooth) {
  1324. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
  1325. } else {
  1326. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
  1327. }
  1328. if (!device->driver->OpenJoystick(device, joystick)) {
  1329. // The open failed, mark this device as disconnected and update devices
  1330. HIDAPI_JoystickDisconnected(device, joystickID);
  1331. SDL_free(hwdata);
  1332. return false;
  1333. }
  1334. HIDAPI_UpdateJoystickProperties(device, joystick);
  1335. if (device->serial) {
  1336. joystick->serial = SDL_strdup(device->serial);
  1337. }
  1338. joystick->hwdata = hwdata;
  1339. return true;
  1340. }
  1341. static bool HIDAPI_GetJoystickDevice(SDL_Joystick *joystick, SDL_HIDAPI_Device **device)
  1342. {
  1343. SDL_AssertJoysticksLocked();
  1344. if (joystick && joystick->hwdata) {
  1345. *device = joystick->hwdata->device;
  1346. if (SDL_ObjectValid(*device, SDL_OBJECT_TYPE_HIDAPI_JOYSTICK) && (*device)->driver != NULL) {
  1347. return true;
  1348. }
  1349. }
  1350. return false;
  1351. }
  1352. static bool HIDAPI_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  1353. {
  1354. bool result;
  1355. SDL_HIDAPI_Device *device = NULL;
  1356. if (HIDAPI_GetJoystickDevice(joystick, &device)) {
  1357. result = device->driver->RumbleJoystick(device, joystick, low_frequency_rumble, high_frequency_rumble);
  1358. } else {
  1359. result = SDL_SetError("Rumble failed, device disconnected");
  1360. }
  1361. return result;
  1362. }
  1363. static bool HIDAPI_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  1364. {
  1365. bool result;
  1366. SDL_HIDAPI_Device *device = NULL;
  1367. if (HIDAPI_GetJoystickDevice(joystick, &device)) {
  1368. result = device->driver->RumbleJoystickTriggers(device, joystick, left_rumble, right_rumble);
  1369. } else {
  1370. result = SDL_SetError("Rumble failed, device disconnected");
  1371. }
  1372. return result;
  1373. }
  1374. static bool HIDAPI_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1375. {
  1376. bool result;
  1377. SDL_HIDAPI_Device *device = NULL;
  1378. if (HIDAPI_GetJoystickDevice(joystick, &device)) {
  1379. result = device->driver->SetJoystickLED(device, joystick, red, green, blue);
  1380. } else {
  1381. result = SDL_SetError("SetLED failed, device disconnected");
  1382. }
  1383. return result;
  1384. }
  1385. static bool HIDAPI_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  1386. {
  1387. bool result;
  1388. SDL_HIDAPI_Device *device = NULL;
  1389. if (HIDAPI_GetJoystickDevice(joystick, &device)) {
  1390. result = device->driver->SendJoystickEffect(device, joystick, data, size);
  1391. } else {
  1392. result = SDL_SetError("SendEffect failed, device disconnected");
  1393. }
  1394. return result;
  1395. }
  1396. static bool HIDAPI_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled)
  1397. {
  1398. bool result;
  1399. SDL_HIDAPI_Device *device = NULL;
  1400. if (HIDAPI_GetJoystickDevice(joystick, &device)) {
  1401. result = device->driver->SetJoystickSensorsEnabled(device, joystick, enabled);
  1402. } else {
  1403. result = SDL_SetError("SetSensorsEnabled failed, device disconnected");
  1404. }
  1405. return result;
  1406. }
  1407. static void HIDAPI_JoystickUpdate(SDL_Joystick *joystick)
  1408. {
  1409. // This is handled in SDL_HIDAPI_UpdateDevices()
  1410. }
  1411. static void HIDAPI_JoystickClose(SDL_Joystick *joystick) SDL_NO_THREAD_SAFETY_ANALYSIS // We unlock the device lock so rumble can complete
  1412. {
  1413. SDL_AssertJoysticksLocked();
  1414. if (joystick->hwdata) {
  1415. SDL_HIDAPI_Device *device = joystick->hwdata->device;
  1416. int i;
  1417. // Wait up to 30 ms for pending rumble to complete
  1418. if (device->updating) {
  1419. // Unlock the device so rumble can complete
  1420. SDL_UnlockMutex(device->dev_lock);
  1421. }
  1422. for (i = 0; i < 3; ++i) {
  1423. if (SDL_GetAtomicInt(&device->rumble_pending) > 0) {
  1424. SDL_Delay(10);
  1425. }
  1426. }
  1427. if (device->updating) {
  1428. // Relock the device
  1429. SDL_LockMutex(device->dev_lock);
  1430. }
  1431. device->driver->CloseJoystick(device, joystick);
  1432. SDL_free(joystick->hwdata);
  1433. joystick->hwdata = NULL;
  1434. }
  1435. }
  1436. static void HIDAPI_JoystickQuit(void)
  1437. {
  1438. int i;
  1439. SDL_AssertJoysticksLocked();
  1440. shutting_down = true;
  1441. SDL_HIDAPI_QuitRumble();
  1442. while (SDL_HIDAPI_devices) {
  1443. SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
  1444. if (device->parent) {
  1445. // When a child device goes away, so does the parent
  1446. device = device->parent;
  1447. for (i = 0; i < device->num_children; ++i) {
  1448. HIDAPI_DelDevice(device->children[i]);
  1449. }
  1450. HIDAPI_DelDevice(device);
  1451. } else {
  1452. HIDAPI_DelDevice(device);
  1453. }
  1454. }
  1455. // Make sure the drivers cleaned up properly
  1456. SDL_assert(SDL_HIDAPI_numjoysticks == 0);
  1457. for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
  1458. SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
  1459. driver->UnregisterHints(SDL_HIDAPIDriverHintChanged, driver);
  1460. }
  1461. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS,
  1462. SDL_HIDAPIDriverHintChanged, NULL);
  1463. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
  1464. SDL_HIDAPIDriverHintChanged, NULL);
  1465. SDL_hid_exit();
  1466. SDL_HIDAPI_change_count = 0;
  1467. shutting_down = false;
  1468. initialized = false;
  1469. }
  1470. static bool HIDAPI_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1471. {
  1472. return false;
  1473. }
  1474. SDL_JoystickDriver SDL_HIDAPI_JoystickDriver = {
  1475. HIDAPI_JoystickInit,
  1476. HIDAPI_JoystickGetCount,
  1477. HIDAPI_JoystickDetect,
  1478. HIDAPI_IsDevicePresent,
  1479. HIDAPI_JoystickGetDeviceName,
  1480. HIDAPI_JoystickGetDevicePath,
  1481. HIDAPI_JoystickGetDeviceSteamVirtualGamepadSlot,
  1482. HIDAPI_JoystickGetDevicePlayerIndex,
  1483. HIDAPI_JoystickSetDevicePlayerIndex,
  1484. HIDAPI_JoystickGetDeviceGUID,
  1485. HIDAPI_JoystickGetDeviceInstanceID,
  1486. HIDAPI_JoystickOpen,
  1487. HIDAPI_JoystickRumble,
  1488. HIDAPI_JoystickRumbleTriggers,
  1489. HIDAPI_JoystickSetLED,
  1490. HIDAPI_JoystickSendEffect,
  1491. HIDAPI_JoystickSetSensorsEnabled,
  1492. HIDAPI_JoystickUpdate,
  1493. HIDAPI_JoystickClose,
  1494. HIDAPI_JoystickQuit,
  1495. HIDAPI_JoystickGetGamepadMapping
  1496. };
  1497. #endif // SDL_JOYSTICK_HIDAPI