SDL_hidapijoystick.c 54 KB

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