SDL_hidapijoystick.c 41 KB

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