SDL_hidapijoystick.c 44 KB

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