SDL_hidapijoystick.c 54 KB

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