1
0

SDL_joystick.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 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. /* This is the joystick API for Simple DirectMedia Layer */
  20. #include "SDL.h"
  21. #include "SDL_atomic.h"
  22. #include "SDL_events.h"
  23. #include "SDL_sysjoystick.h"
  24. #include "SDL_assert.h"
  25. #include "SDL_hints.h"
  26. #if !SDL_EVENTS_DISABLED
  27. #include "../events/SDL_events_c.h"
  28. #endif
  29. #include "../video/SDL_sysvideo.h"
  30. #include "hidapi/SDL_hidapijoystick_c.h"
  31. /* This is included in only one place because it has a large static list of controllers */
  32. #include "controller_type.h"
  33. #ifdef __WIN32__
  34. /* Needed for checking for input remapping programs */
  35. #include "../core/windows/SDL_windows.h"
  36. #undef UNICODE /* We want ASCII functions */
  37. #include <tlhelp32.h>
  38. #endif
  39. static SDL_JoystickDriver *SDL_joystick_drivers[] = {
  40. #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)
  41. &SDL_WINDOWS_JoystickDriver,
  42. #endif
  43. #ifdef SDL_JOYSTICK_LINUX
  44. &SDL_LINUX_JoystickDriver,
  45. #endif
  46. #ifdef SDL_JOYSTICK_IOKIT
  47. &SDL_DARWIN_JoystickDriver,
  48. #endif
  49. #if defined(__IPHONEOS__) || defined(__TVOS__)
  50. &SDL_IOS_JoystickDriver,
  51. #endif
  52. #ifdef SDL_JOYSTICK_ANDROID
  53. &SDL_ANDROID_JoystickDriver,
  54. #endif
  55. #ifdef SDL_JOYSTICK_EMSCRIPTEN
  56. &SDL_EMSCRIPTEN_JoystickDriver,
  57. #endif
  58. #ifdef SDL_JOYSTICK_HAIKU
  59. &SDL_HAIKU_JoystickDriver,
  60. #endif
  61. #ifdef SDL_JOYSTICK_USBHID /* !!! FIXME: "USBHID" is a generic name, and doubly-confusing with HIDAPI next to it. This is the *BSD interface, rename this. */
  62. &SDL_BSD_JoystickDriver,
  63. #endif
  64. #ifdef SDL_JOYSTICK_HIDAPI
  65. &SDL_HIDAPI_JoystickDriver,
  66. #endif
  67. #if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED)
  68. &SDL_DUMMY_JoystickDriver
  69. #endif
  70. };
  71. static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
  72. static SDL_Joystick *SDL_joysticks = NULL;
  73. static SDL_bool SDL_updating_joystick = SDL_FALSE;
  74. static SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
  75. static SDL_atomic_t SDL_next_joystick_instance_id;
  76. static int SDL_joystick_player_count = 0;
  77. static SDL_JoystickID *SDL_joystick_players = NULL;
  78. void
  79. SDL_LockJoysticks(void)
  80. {
  81. if (SDL_joystick_lock) {
  82. SDL_LockMutex(SDL_joystick_lock);
  83. }
  84. }
  85. void
  86. SDL_UnlockJoysticks(void)
  87. {
  88. if (SDL_joystick_lock) {
  89. SDL_UnlockMutex(SDL_joystick_lock);
  90. }
  91. }
  92. static int
  93. SDL_FindFreePlayerIndex()
  94. {
  95. int player_index;
  96. for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {
  97. if (SDL_joystick_players[player_index] == -1) {
  98. return player_index;
  99. }
  100. }
  101. return player_index;
  102. }
  103. static int
  104. SDL_GetPlayerIndexForJoystickID(SDL_JoystickID instance_id)
  105. {
  106. int player_index;
  107. for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {
  108. if (instance_id == SDL_joystick_players[player_index]) {
  109. break;
  110. }
  111. }
  112. if (player_index == SDL_joystick_player_count) {
  113. player_index = -1;
  114. }
  115. return player_index;
  116. }
  117. static SDL_JoystickID
  118. SDL_GetJoystickIDForPlayerIndex(int player_index)
  119. {
  120. if (player_index < 0 || player_index >= SDL_joystick_player_count) {
  121. return -1;
  122. }
  123. return SDL_joystick_players[player_index];
  124. }
  125. static SDL_bool
  126. SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id)
  127. {
  128. SDL_JoystickID existing_instance = SDL_GetJoystickIDForPlayerIndex(player_index);
  129. SDL_JoystickDriver *driver;
  130. int device_index;
  131. if (player_index < 0) {
  132. return SDL_FALSE;
  133. }
  134. if (player_index >= SDL_joystick_player_count) {
  135. SDL_JoystickID *new_players = (SDL_JoystickID *)SDL_realloc(SDL_joystick_players, (player_index + 1)*sizeof(*SDL_joystick_players));
  136. if (!new_players) {
  137. SDL_OutOfMemory();
  138. return SDL_FALSE;
  139. }
  140. SDL_joystick_players = new_players;
  141. SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0xFF, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0]));
  142. SDL_joystick_player_count = player_index + 1;
  143. }
  144. SDL_joystick_players[player_index] = instance_id;
  145. /* Update the driver with the new index */
  146. device_index = SDL_JoystickGetDeviceIndexFromInstanceID(instance_id);
  147. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  148. driver->SetDevicePlayerIndex(device_index, player_index);
  149. }
  150. /* Move any existing joystick to another slot */
  151. if (existing_instance >= 0) {
  152. SDL_SetJoystickIDForPlayerIndex(SDL_FindFreePlayerIndex(), existing_instance);
  153. }
  154. return SDL_TRUE;
  155. }
  156. static void SDLCALL
  157. SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  158. {
  159. if (hint && *hint == '1') {
  160. SDL_joystick_allows_background_events = SDL_TRUE;
  161. } else {
  162. SDL_joystick_allows_background_events = SDL_FALSE;
  163. }
  164. }
  165. int
  166. SDL_JoystickInit(void)
  167. {
  168. int i, status;
  169. SDL_GameControllerInitMappings();
  170. /* Create the joystick list lock */
  171. if (!SDL_joystick_lock) {
  172. SDL_joystick_lock = SDL_CreateMutex();
  173. }
  174. /* See if we should allow joystick events while in the background */
  175. SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  176. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  177. #if !SDL_EVENTS_DISABLED
  178. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  179. return -1;
  180. }
  181. #endif /* !SDL_EVENTS_DISABLED */
  182. status = -1;
  183. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  184. if (SDL_joystick_drivers[i]->Init() >= 0) {
  185. status = 0;
  186. }
  187. }
  188. return status;
  189. }
  190. /*
  191. * Count the number of joysticks attached to the system
  192. */
  193. int
  194. SDL_NumJoysticks(void)
  195. {
  196. int i, total_joysticks = 0;
  197. SDL_LockJoysticks();
  198. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  199. total_joysticks += SDL_joystick_drivers[i]->GetCount();
  200. }
  201. SDL_UnlockJoysticks();
  202. return total_joysticks;
  203. }
  204. /*
  205. * Return the next available joystick instance ID
  206. * This may be called by drivers from multiple threads, unprotected by any locks
  207. */
  208. SDL_JoystickID SDL_GetNextJoystickInstanceID()
  209. {
  210. return SDL_AtomicIncRef(&SDL_next_joystick_instance_id);
  211. }
  212. /*
  213. * Get the driver and device index for an API device index
  214. * This should be called while the joystick lock is held, to prevent another thread from updating the list
  215. */
  216. SDL_bool
  217. SDL_GetDriverAndJoystickIndex(int device_index, SDL_JoystickDriver **driver, int *driver_index)
  218. {
  219. int i, num_joysticks, total_joysticks = 0;
  220. if (device_index >= 0) {
  221. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  222. num_joysticks = SDL_joystick_drivers[i]->GetCount();
  223. if (device_index < num_joysticks) {
  224. *driver = SDL_joystick_drivers[i];
  225. *driver_index = device_index;
  226. return SDL_TRUE;
  227. }
  228. device_index -= num_joysticks;
  229. total_joysticks += num_joysticks;
  230. }
  231. }
  232. SDL_SetError("There are %d joysticks available", total_joysticks);
  233. return SDL_FALSE;
  234. }
  235. /*
  236. * Perform any needed fixups for joystick names
  237. */
  238. static const char *
  239. SDL_FixupJoystickName(const char *name)
  240. {
  241. if (name) {
  242. const char *skip_prefix = "NVIDIA Corporation ";
  243. if (SDL_strncmp(name, skip_prefix, SDL_strlen(skip_prefix)) == 0) {
  244. name += SDL_strlen(skip_prefix);
  245. }
  246. }
  247. return name;
  248. }
  249. /*
  250. * Get the implementation dependent name of a joystick
  251. */
  252. const char *
  253. SDL_JoystickNameForIndex(int device_index)
  254. {
  255. SDL_JoystickDriver *driver;
  256. const char *name = NULL;
  257. SDL_LockJoysticks();
  258. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  259. name = SDL_FixupJoystickName(driver->GetDeviceName(device_index));
  260. }
  261. SDL_UnlockJoysticks();
  262. /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
  263. return name;
  264. }
  265. /*
  266. * Get the player index of a joystick, or -1 if it's not available
  267. */
  268. int
  269. SDL_JoystickGetDevicePlayerIndex(int device_index)
  270. {
  271. int player_index;
  272. SDL_LockJoysticks();
  273. player_index = SDL_GetPlayerIndexForJoystickID(SDL_JoystickGetDeviceInstanceID(device_index));
  274. SDL_UnlockJoysticks();
  275. return player_index;
  276. }
  277. /*
  278. * Return true if this joystick is known to have all axes centered at zero
  279. * This isn't generally needed unless the joystick never generates an initial axis value near zero,
  280. * e.g. it's emulating axes with digital buttons
  281. */
  282. static SDL_bool
  283. SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
  284. {
  285. static Uint32 zero_centered_joysticks[] = {
  286. MAKE_VIDPID(0x0e8f, 0x3013), /* HuiJia SNES USB adapter */
  287. MAKE_VIDPID(0x05a0, 0x3232), /* 8Bitdo Zero Gamepad */
  288. };
  289. int i;
  290. Uint32 id = MAKE_VIDPID(SDL_JoystickGetVendor(joystick),
  291. SDL_JoystickGetProduct(joystick));
  292. /*printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);*/
  293. if (joystick->naxes == 2) {
  294. /* Assume D-pad or thumbstick style axes are centered at 0 */
  295. return SDL_TRUE;
  296. }
  297. for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
  298. if (id == zero_centered_joysticks[i]) {
  299. return SDL_TRUE;
  300. }
  301. }
  302. return SDL_FALSE;
  303. }
  304. /*
  305. * Open a joystick for use - the index passed as an argument refers to
  306. * the N'th joystick on the system. This index is the value which will
  307. * identify this joystick in future joystick events.
  308. *
  309. * This function returns a joystick identifier, or NULL if an error occurred.
  310. */
  311. SDL_Joystick *
  312. SDL_JoystickOpen(int device_index)
  313. {
  314. SDL_JoystickDriver *driver;
  315. SDL_JoystickID instance_id;
  316. SDL_Joystick *joystick;
  317. SDL_Joystick *joysticklist;
  318. const char *joystickname = NULL;
  319. SDL_LockJoysticks();
  320. if (!SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  321. SDL_UnlockJoysticks();
  322. return NULL;
  323. }
  324. joysticklist = SDL_joysticks;
  325. /* If the joystick is already open, return it
  326. * it is important that we have a single joystick * for each instance id
  327. */
  328. instance_id = driver->GetDeviceInstanceID(device_index);
  329. while (joysticklist) {
  330. if (instance_id == joysticklist->instance_id) {
  331. joystick = joysticklist;
  332. ++joystick->ref_count;
  333. SDL_UnlockJoysticks();
  334. return joystick;
  335. }
  336. joysticklist = joysticklist->next;
  337. }
  338. /* Create and initialize the joystick */
  339. joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
  340. if (joystick == NULL) {
  341. SDL_OutOfMemory();
  342. SDL_UnlockJoysticks();
  343. return NULL;
  344. }
  345. joystick->driver = driver;
  346. joystick->instance_id = instance_id;
  347. joystick->attached = SDL_TRUE;
  348. joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
  349. if (driver->Open(joystick, device_index) < 0) {
  350. SDL_free(joystick);
  351. SDL_UnlockJoysticks();
  352. return NULL;
  353. }
  354. joystickname = driver->GetDeviceName(device_index);
  355. if (joystickname) {
  356. joystick->name = SDL_strdup(joystickname);
  357. } else {
  358. joystick->name = NULL;
  359. }
  360. joystick->guid = driver->GetDeviceGUID(device_index);
  361. if (joystick->naxes > 0) {
  362. joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo));
  363. }
  364. if (joystick->nhats > 0) {
  365. joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8));
  366. }
  367. if (joystick->nballs > 0) {
  368. joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls));
  369. }
  370. if (joystick->nbuttons > 0) {
  371. joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8));
  372. }
  373. if (((joystick->naxes > 0) && !joystick->axes)
  374. || ((joystick->nhats > 0) && !joystick->hats)
  375. || ((joystick->nballs > 0) && !joystick->balls)
  376. || ((joystick->nbuttons > 0) && !joystick->buttons)) {
  377. SDL_OutOfMemory();
  378. SDL_JoystickClose(joystick);
  379. SDL_UnlockJoysticks();
  380. return NULL;
  381. }
  382. /* If this joystick is known to have all zero centered axes, skip the auto-centering code */
  383. if (SDL_JoystickAxesCenteredAtZero(joystick)) {
  384. int i;
  385. for (i = 0; i < joystick->naxes; ++i) {
  386. joystick->axes[i].has_initial_value = SDL_TRUE;
  387. }
  388. }
  389. joystick->is_game_controller = SDL_IsGameController(device_index);
  390. /* Add joystick to list */
  391. ++joystick->ref_count;
  392. /* Link the joystick in the list */
  393. joystick->next = SDL_joysticks;
  394. SDL_joysticks = joystick;
  395. SDL_UnlockJoysticks();
  396. driver->Update(joystick);
  397. return joystick;
  398. }
  399. /*
  400. * Checks to make sure the joystick is valid.
  401. */
  402. SDL_bool
  403. SDL_PrivateJoystickValid(SDL_Joystick * joystick)
  404. {
  405. SDL_bool valid;
  406. if (joystick == NULL) {
  407. SDL_SetError("Joystick hasn't been opened yet");
  408. valid = SDL_FALSE;
  409. } else {
  410. valid = SDL_TRUE;
  411. }
  412. return valid;
  413. }
  414. /*
  415. * Get the number of multi-dimensional axis controls on a joystick
  416. */
  417. int
  418. SDL_JoystickNumAxes(SDL_Joystick * joystick)
  419. {
  420. if (!SDL_PrivateJoystickValid(joystick)) {
  421. return -1;
  422. }
  423. return joystick->naxes;
  424. }
  425. /*
  426. * Get the number of hats on a joystick
  427. */
  428. int
  429. SDL_JoystickNumHats(SDL_Joystick * joystick)
  430. {
  431. if (!SDL_PrivateJoystickValid(joystick)) {
  432. return -1;
  433. }
  434. return joystick->nhats;
  435. }
  436. /*
  437. * Get the number of trackballs on a joystick
  438. */
  439. int
  440. SDL_JoystickNumBalls(SDL_Joystick * joystick)
  441. {
  442. if (!SDL_PrivateJoystickValid(joystick)) {
  443. return -1;
  444. }
  445. return joystick->nballs;
  446. }
  447. /*
  448. * Get the number of buttons on a joystick
  449. */
  450. int
  451. SDL_JoystickNumButtons(SDL_Joystick * joystick)
  452. {
  453. if (!SDL_PrivateJoystickValid(joystick)) {
  454. return -1;
  455. }
  456. return joystick->nbuttons;
  457. }
  458. /*
  459. * Get the current state of an axis control on a joystick
  460. */
  461. Sint16
  462. SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
  463. {
  464. Sint16 state;
  465. if (!SDL_PrivateJoystickValid(joystick)) {
  466. return 0;
  467. }
  468. if (axis < joystick->naxes) {
  469. state = joystick->axes[axis].value;
  470. } else {
  471. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  472. state = 0;
  473. }
  474. return state;
  475. }
  476. /*
  477. * Get the initial state of an axis control on a joystick
  478. */
  479. SDL_bool
  480. SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick, int axis, Sint16 *state)
  481. {
  482. if (!SDL_PrivateJoystickValid(joystick)) {
  483. return SDL_FALSE;
  484. }
  485. if (axis >= joystick->naxes) {
  486. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  487. return SDL_FALSE;
  488. }
  489. if (state) {
  490. *state = joystick->axes[axis].initial_value;
  491. }
  492. return joystick->axes[axis].has_initial_value;
  493. }
  494. /*
  495. * Get the current state of a hat on a joystick
  496. */
  497. Uint8
  498. SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
  499. {
  500. Uint8 state;
  501. if (!SDL_PrivateJoystickValid(joystick)) {
  502. return 0;
  503. }
  504. if (hat < joystick->nhats) {
  505. state = joystick->hats[hat];
  506. } else {
  507. SDL_SetError("Joystick only has %d hats", joystick->nhats);
  508. state = 0;
  509. }
  510. return state;
  511. }
  512. /*
  513. * Get the ball axis change since the last poll
  514. */
  515. int
  516. SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
  517. {
  518. int retval;
  519. if (!SDL_PrivateJoystickValid(joystick)) {
  520. return -1;
  521. }
  522. retval = 0;
  523. if (ball < joystick->nballs) {
  524. if (dx) {
  525. *dx = joystick->balls[ball].dx;
  526. }
  527. if (dy) {
  528. *dy = joystick->balls[ball].dy;
  529. }
  530. joystick->balls[ball].dx = 0;
  531. joystick->balls[ball].dy = 0;
  532. } else {
  533. return SDL_SetError("Joystick only has %d balls", joystick->nballs);
  534. }
  535. return retval;
  536. }
  537. /*
  538. * Get the current state of a button on a joystick
  539. */
  540. Uint8
  541. SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
  542. {
  543. Uint8 state;
  544. if (!SDL_PrivateJoystickValid(joystick)) {
  545. return 0;
  546. }
  547. if (button < joystick->nbuttons) {
  548. state = joystick->buttons[button];
  549. } else {
  550. SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
  551. state = 0;
  552. }
  553. return state;
  554. }
  555. /*
  556. * Return if the joystick in question is currently attached to the system,
  557. * \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
  558. */
  559. SDL_bool
  560. SDL_JoystickGetAttached(SDL_Joystick * joystick)
  561. {
  562. if (!SDL_PrivateJoystickValid(joystick)) {
  563. return SDL_FALSE;
  564. }
  565. return joystick->attached;
  566. }
  567. /*
  568. * Get the instance id for this opened joystick
  569. */
  570. SDL_JoystickID
  571. SDL_JoystickInstanceID(SDL_Joystick * joystick)
  572. {
  573. if (!SDL_PrivateJoystickValid(joystick)) {
  574. return -1;
  575. }
  576. return joystick->instance_id;
  577. }
  578. /*
  579. * Return the SDL_Joystick associated with an instance id.
  580. */
  581. SDL_Joystick *
  582. SDL_JoystickFromInstanceID(SDL_JoystickID instance_id)
  583. {
  584. SDL_Joystick *joystick;
  585. SDL_LockJoysticks();
  586. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  587. if (joystick->instance_id == instance_id) {
  588. break;
  589. }
  590. }
  591. SDL_UnlockJoysticks();
  592. return joystick;
  593. }
  594. /**
  595. * Return the SDL_Joystick associated with a player index.
  596. */
  597. SDL_Joystick *
  598. SDL_JoystickFromPlayerIndex(int player_index)
  599. {
  600. SDL_JoystickID instance_id;
  601. SDL_Joystick *joystick;
  602. SDL_LockJoysticks();
  603. instance_id = SDL_GetJoystickIDForPlayerIndex(player_index);
  604. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  605. if (joystick->instance_id == instance_id) {
  606. break;
  607. }
  608. }
  609. SDL_UnlockJoysticks();
  610. return joystick;
  611. }
  612. /*
  613. * Get the friendly name of this joystick
  614. */
  615. const char *
  616. SDL_JoystickName(SDL_Joystick * joystick)
  617. {
  618. if (!SDL_PrivateJoystickValid(joystick)) {
  619. return NULL;
  620. }
  621. return SDL_FixupJoystickName(joystick->name);
  622. }
  623. /**
  624. * Get the player index of an opened joystick, or -1 if it's not available
  625. */
  626. int
  627. SDL_JoystickGetPlayerIndex(SDL_Joystick * joystick)
  628. {
  629. int player_index;
  630. if (!SDL_PrivateJoystickValid(joystick)) {
  631. return -1;
  632. }
  633. SDL_LockJoysticks();
  634. player_index = SDL_GetPlayerIndexForJoystickID(joystick->instance_id);
  635. SDL_UnlockJoysticks();
  636. return player_index;
  637. }
  638. /**
  639. * Set the player index of an opened joystick
  640. */
  641. void
  642. SDL_JoystickSetPlayerIndex(SDL_Joystick * joystick, int player_index)
  643. {
  644. if (!SDL_PrivateJoystickValid(joystick)) {
  645. return;
  646. }
  647. SDL_LockJoysticks();
  648. SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id);
  649. SDL_UnlockJoysticks();
  650. }
  651. int
  652. SDL_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  653. {
  654. int result;
  655. if (!SDL_PrivateJoystickValid(joystick)) {
  656. return -1;
  657. }
  658. SDL_LockJoysticks();
  659. result = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble, duration_ms);
  660. SDL_UnlockJoysticks();
  661. return result;
  662. }
  663. /*
  664. * Close a joystick previously opened with SDL_JoystickOpen()
  665. */
  666. void
  667. SDL_JoystickClose(SDL_Joystick * joystick)
  668. {
  669. SDL_Joystick *joysticklist;
  670. SDL_Joystick *joysticklistprev;
  671. if (!SDL_PrivateJoystickValid(joystick)) {
  672. return;
  673. }
  674. SDL_LockJoysticks();
  675. /* First decrement ref count */
  676. if (--joystick->ref_count > 0) {
  677. SDL_UnlockJoysticks();
  678. return;
  679. }
  680. if (SDL_updating_joystick) {
  681. SDL_UnlockJoysticks();
  682. return;
  683. }
  684. joystick->driver->Close(joystick);
  685. joystick->hwdata = NULL;
  686. joysticklist = SDL_joysticks;
  687. joysticklistprev = NULL;
  688. while (joysticklist) {
  689. if (joystick == joysticklist) {
  690. if (joysticklistprev) {
  691. /* unlink this entry */
  692. joysticklistprev->next = joysticklist->next;
  693. } else {
  694. SDL_joysticks = joystick->next;
  695. }
  696. break;
  697. }
  698. joysticklistprev = joysticklist;
  699. joysticklist = joysticklist->next;
  700. }
  701. SDL_free(joystick->name);
  702. /* Free the data associated with this joystick */
  703. SDL_free(joystick->axes);
  704. SDL_free(joystick->hats);
  705. SDL_free(joystick->balls);
  706. SDL_free(joystick->buttons);
  707. SDL_free(joystick);
  708. SDL_UnlockJoysticks();
  709. }
  710. void
  711. SDL_JoystickQuit(void)
  712. {
  713. int i;
  714. /* Make sure we're not getting called in the middle of updating joysticks */
  715. SDL_LockJoysticks();
  716. while (SDL_updating_joystick) {
  717. SDL_UnlockJoysticks();
  718. SDL_Delay(1);
  719. SDL_LockJoysticks();
  720. }
  721. /* Stop the event polling */
  722. while (SDL_joysticks) {
  723. SDL_joysticks->ref_count = 1;
  724. SDL_JoystickClose(SDL_joysticks);
  725. }
  726. /* Quit the joystick setup */
  727. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  728. SDL_joystick_drivers[i]->Quit();
  729. }
  730. if (SDL_joystick_players) {
  731. SDL_free(SDL_joystick_players);
  732. SDL_joystick_players = NULL;
  733. SDL_joystick_player_count = 0;
  734. }
  735. SDL_UnlockJoysticks();
  736. #if !SDL_EVENTS_DISABLED
  737. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  738. #endif
  739. SDL_DelHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  740. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  741. if (SDL_joystick_lock) {
  742. SDL_mutex *mutex = SDL_joystick_lock;
  743. SDL_joystick_lock = NULL;
  744. SDL_DestroyMutex(mutex);
  745. }
  746. SDL_GameControllerQuitMappings();
  747. }
  748. static SDL_bool
  749. SDL_PrivateJoystickShouldIgnoreEvent()
  750. {
  751. if (SDL_joystick_allows_background_events) {
  752. return SDL_FALSE;
  753. }
  754. if (SDL_HasWindows() && SDL_GetKeyboardFocus() == NULL) {
  755. /* We have windows but we don't have focus, ignore the event. */
  756. return SDL_TRUE;
  757. }
  758. return SDL_FALSE;
  759. }
  760. /* These are global for SDL_sysjoystick.c and SDL_events.c */
  761. void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
  762. {
  763. SDL_JoystickDriver *driver;
  764. int driver_device_index;
  765. int player_index = -1;
  766. int device_index = SDL_JoystickGetDeviceIndexFromInstanceID(device_instance);
  767. if (device_index < 0) {
  768. return;
  769. }
  770. SDL_LockJoysticks();
  771. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) {
  772. player_index = driver->GetDevicePlayerIndex(driver_device_index);
  773. }
  774. if (player_index < 0 && SDL_IsGameController(device_index)) {
  775. player_index = SDL_FindFreePlayerIndex();
  776. }
  777. if (player_index >= 0) {
  778. SDL_SetJoystickIDForPlayerIndex(player_index, device_instance);
  779. }
  780. SDL_UnlockJoysticks();
  781. #if !SDL_EVENTS_DISABLED
  782. {
  783. SDL_Event event;
  784. event.type = SDL_JOYDEVICEADDED;
  785. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  786. event.jdevice.which = device_index;
  787. SDL_PushEvent(&event);
  788. }
  789. }
  790. #endif /* !SDL_EVENTS_DISABLED */
  791. }
  792. /*
  793. * If there is an existing add event in the queue, it needs to be modified
  794. * to have the right value for which, because the number of controllers in
  795. * the system is now one less.
  796. */
  797. static void UpdateEventsForDeviceRemoval()
  798. {
  799. int i, num_events;
  800. SDL_Event *events;
  801. SDL_bool isstack;
  802. num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
  803. if (num_events <= 0) {
  804. return;
  805. }
  806. events = SDL_small_alloc(SDL_Event, num_events, &isstack);
  807. if (!events) {
  808. return;
  809. }
  810. num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
  811. for (i = 0; i < num_events; ++i) {
  812. --events[i].jdevice.which;
  813. }
  814. SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
  815. SDL_small_free(events, isstack);
  816. }
  817. void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
  818. {
  819. SDL_Joystick *joystick;
  820. int player_index;
  821. #if !SDL_EVENTS_DISABLED
  822. SDL_Event event;
  823. event.type = SDL_JOYDEVICEREMOVED;
  824. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  825. event.jdevice.which = device_instance;
  826. SDL_PushEvent(&event);
  827. }
  828. UpdateEventsForDeviceRemoval();
  829. #endif /* !SDL_EVENTS_DISABLED */
  830. /* Mark this joystick as no longer attached */
  831. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  832. if (joystick->instance_id == device_instance) {
  833. joystick->attached = SDL_FALSE;
  834. joystick->force_recentering = SDL_TRUE;
  835. break;
  836. }
  837. }
  838. SDL_LockJoysticks();
  839. player_index = SDL_GetPlayerIndexForJoystickID(device_instance);
  840. if (player_index >= 0) {
  841. SDL_joystick_players[player_index] = -1;
  842. }
  843. SDL_UnlockJoysticks();
  844. }
  845. int
  846. SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
  847. {
  848. int posted;
  849. SDL_JoystickAxisInfo *info;
  850. /* Make sure we're not getting garbage or duplicate events */
  851. if (axis >= joystick->naxes) {
  852. return 0;
  853. }
  854. info = &joystick->axes[axis];
  855. if (!info->has_initial_value ||
  856. (!info->has_second_value && (info->initial_value == -32768 || info->initial_value == 32767) && SDL_abs(value) < (SDL_JOYSTICK_AXIS_MAX / 4))) {
  857. info->initial_value = value;
  858. info->value = value;
  859. info->zero = value;
  860. info->has_initial_value = SDL_TRUE;
  861. } else {
  862. info->has_second_value = SDL_TRUE;
  863. }
  864. if (value == info->value) {
  865. return 0;
  866. }
  867. if (!info->sent_initial_value) {
  868. /* Make sure we don't send motion until there's real activity on this axis */
  869. const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */
  870. if (SDL_abs(value - info->value) <= MAX_ALLOWED_JITTER) {
  871. return 0;
  872. }
  873. info->sent_initial_value = SDL_TRUE;
  874. info->value = value; /* Just so we pass the check above */
  875. SDL_PrivateJoystickAxis(joystick, axis, info->initial_value);
  876. }
  877. /* We ignore events if we don't have keyboard focus, except for centering
  878. * events.
  879. */
  880. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  881. if ((value > info->zero && value >= info->value) ||
  882. (value < info->zero && value <= info->value)) {
  883. return 0;
  884. }
  885. }
  886. /* Update internal joystick state */
  887. info->value = value;
  888. /* Post the event, if desired */
  889. posted = 0;
  890. #if !SDL_EVENTS_DISABLED
  891. if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
  892. SDL_Event event;
  893. event.type = SDL_JOYAXISMOTION;
  894. event.jaxis.which = joystick->instance_id;
  895. event.jaxis.axis = axis;
  896. event.jaxis.value = value;
  897. posted = SDL_PushEvent(&event) == 1;
  898. }
  899. #endif /* !SDL_EVENTS_DISABLED */
  900. return posted;
  901. }
  902. int
  903. SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
  904. {
  905. int posted;
  906. /* Make sure we're not getting garbage or duplicate events */
  907. if (hat >= joystick->nhats) {
  908. return 0;
  909. }
  910. if (value == joystick->hats[hat]) {
  911. return 0;
  912. }
  913. /* We ignore events if we don't have keyboard focus, except for centering
  914. * events.
  915. */
  916. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  917. if (value != SDL_HAT_CENTERED) {
  918. return 0;
  919. }
  920. }
  921. /* Update internal joystick state */
  922. joystick->hats[hat] = value;
  923. /* Post the event, if desired */
  924. posted = 0;
  925. #if !SDL_EVENTS_DISABLED
  926. if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
  927. SDL_Event event;
  928. event.jhat.type = SDL_JOYHATMOTION;
  929. event.jhat.which = joystick->instance_id;
  930. event.jhat.hat = hat;
  931. event.jhat.value = value;
  932. posted = SDL_PushEvent(&event) == 1;
  933. }
  934. #endif /* !SDL_EVENTS_DISABLED */
  935. return posted;
  936. }
  937. int
  938. SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
  939. Sint16 xrel, Sint16 yrel)
  940. {
  941. int posted;
  942. /* Make sure we're not getting garbage events */
  943. if (ball >= joystick->nballs) {
  944. return 0;
  945. }
  946. /* We ignore events if we don't have keyboard focus. */
  947. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  948. return 0;
  949. }
  950. /* Update internal mouse state */
  951. joystick->balls[ball].dx += xrel;
  952. joystick->balls[ball].dy += yrel;
  953. /* Post the event, if desired */
  954. posted = 0;
  955. #if !SDL_EVENTS_DISABLED
  956. if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
  957. SDL_Event event;
  958. event.jball.type = SDL_JOYBALLMOTION;
  959. event.jball.which = joystick->instance_id;
  960. event.jball.ball = ball;
  961. event.jball.xrel = xrel;
  962. event.jball.yrel = yrel;
  963. posted = SDL_PushEvent(&event) == 1;
  964. }
  965. #endif /* !SDL_EVENTS_DISABLED */
  966. return posted;
  967. }
  968. int
  969. SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
  970. {
  971. int posted;
  972. #if !SDL_EVENTS_DISABLED
  973. SDL_Event event;
  974. switch (state) {
  975. case SDL_PRESSED:
  976. event.type = SDL_JOYBUTTONDOWN;
  977. break;
  978. case SDL_RELEASED:
  979. event.type = SDL_JOYBUTTONUP;
  980. break;
  981. default:
  982. /* Invalid state -- bail */
  983. return 0;
  984. }
  985. #endif /* !SDL_EVENTS_DISABLED */
  986. /* Make sure we're not getting garbage or duplicate events */
  987. if (button >= joystick->nbuttons) {
  988. return 0;
  989. }
  990. if (state == joystick->buttons[button]) {
  991. return 0;
  992. }
  993. /* We ignore events if we don't have keyboard focus, except for button
  994. * release. */
  995. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  996. if (state == SDL_PRESSED) {
  997. return 0;
  998. }
  999. }
  1000. /* Update internal joystick state */
  1001. joystick->buttons[button] = state;
  1002. /* Post the event, if desired */
  1003. posted = 0;
  1004. #if !SDL_EVENTS_DISABLED
  1005. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1006. event.jbutton.which = joystick->instance_id;
  1007. event.jbutton.button = button;
  1008. event.jbutton.state = state;
  1009. posted = SDL_PushEvent(&event) == 1;
  1010. }
  1011. #endif /* !SDL_EVENTS_DISABLED */
  1012. return posted;
  1013. }
  1014. void
  1015. SDL_JoystickUpdate(void)
  1016. {
  1017. int i;
  1018. SDL_Joystick *joystick, *next;
  1019. if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
  1020. return;
  1021. }
  1022. SDL_LockJoysticks();
  1023. if (SDL_updating_joystick) {
  1024. /* The joysticks are already being updated */
  1025. SDL_UnlockJoysticks();
  1026. return;
  1027. }
  1028. SDL_updating_joystick = SDL_TRUE;
  1029. /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
  1030. SDL_UnlockJoysticks();
  1031. #ifdef SDL_JOYSTICK_HIDAPI
  1032. /* Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks */
  1033. HIDAPI_UpdateDevices();
  1034. #endif /* SDL_JOYSTICK_HIDAPI */
  1035. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  1036. if (joystick->attached) {
  1037. /* This should always be true, but seeing a crash in the wild...? */
  1038. if (joystick->driver) {
  1039. joystick->driver->Update(joystick);
  1040. }
  1041. if (joystick->delayed_guide_button) {
  1042. SDL_GameControllerHandleDelayedGuideButton(joystick);
  1043. }
  1044. }
  1045. if (joystick->force_recentering) {
  1046. /* Tell the app that everything is centered/unpressed... */
  1047. for (i = 0; i < joystick->naxes; i++) {
  1048. if (joystick->axes[i].has_initial_value) {
  1049. SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
  1050. }
  1051. }
  1052. for (i = 0; i < joystick->nbuttons; i++) {
  1053. SDL_PrivateJoystickButton(joystick, i, 0);
  1054. }
  1055. for (i = 0; i < joystick->nhats; i++) {
  1056. SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
  1057. }
  1058. joystick->force_recentering = SDL_FALSE;
  1059. }
  1060. }
  1061. SDL_LockJoysticks();
  1062. SDL_updating_joystick = SDL_FALSE;
  1063. /* If any joysticks were closed while updating, free them here */
  1064. for (joystick = SDL_joysticks; joystick; joystick = next) {
  1065. next = joystick->next;
  1066. if (joystick->ref_count <= 0) {
  1067. SDL_JoystickClose(joystick);
  1068. }
  1069. }
  1070. /* this needs to happen AFTER walking the joystick list above, so that any
  1071. dangling hardware data from removed devices can be free'd
  1072. */
  1073. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  1074. SDL_joystick_drivers[i]->Detect();
  1075. }
  1076. SDL_UnlockJoysticks();
  1077. }
  1078. int
  1079. SDL_JoystickEventState(int state)
  1080. {
  1081. #if SDL_EVENTS_DISABLED
  1082. return SDL_DISABLE;
  1083. #else
  1084. const Uint32 event_list[] = {
  1085. SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
  1086. SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
  1087. };
  1088. unsigned int i;
  1089. switch (state) {
  1090. case SDL_QUERY:
  1091. state = SDL_DISABLE;
  1092. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1093. state = SDL_EventState(event_list[i], SDL_QUERY);
  1094. if (state == SDL_ENABLE) {
  1095. break;
  1096. }
  1097. }
  1098. break;
  1099. default:
  1100. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1101. SDL_EventState(event_list[i], state);
  1102. }
  1103. break;
  1104. }
  1105. return state;
  1106. #endif /* SDL_EVENTS_DISABLED */
  1107. }
  1108. void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
  1109. {
  1110. Uint16 *guid16 = (Uint16 *)guid.data;
  1111. /* If the GUID fits the form of BUS 0000 VENDOR 0000 PRODUCT 0000, return the data */
  1112. if (/* guid16[0] is device bus type */
  1113. guid16[1] == 0x0000 &&
  1114. /* guid16[2] is vendor ID */
  1115. guid16[3] == 0x0000 &&
  1116. /* guid16[4] is product ID */
  1117. guid16[5] == 0x0000
  1118. /* guid16[6] is product version */
  1119. ) {
  1120. if (vendor) {
  1121. *vendor = guid16[2];
  1122. }
  1123. if (product) {
  1124. *product = guid16[4];
  1125. }
  1126. if (version) {
  1127. *version = guid16[6];
  1128. }
  1129. } else {
  1130. if (vendor) {
  1131. *vendor = 0;
  1132. }
  1133. if (product) {
  1134. *product = 0;
  1135. }
  1136. if (version) {
  1137. *version = 0;
  1138. }
  1139. }
  1140. }
  1141. const char *
  1142. SDL_GetCustomJoystickManufacturer(const char *manufacturer)
  1143. {
  1144. if (manufacturer) {
  1145. if (SDL_strcmp(manufacturer, "Performance Designed Products") == 0) {
  1146. return "PDP";
  1147. } else if (SDL_strcmp(manufacturer, "HORI CO.,LTD") == 0) {
  1148. return "HORI";
  1149. }
  1150. }
  1151. return manufacturer;
  1152. }
  1153. const char *
  1154. SDL_GetCustomJoystickName(Uint16 vendor, Uint16 product)
  1155. {
  1156. return GuessControllerName(vendor, product);
  1157. }
  1158. SDL_GameControllerType
  1159. SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name)
  1160. {
  1161. SDL_GameControllerType type;
  1162. Uint16 vendor, product;
  1163. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  1164. type = SDL_GetJoystickGameControllerType(vendor, product, name);
  1165. if (type == SDL_CONTROLLER_TYPE_UNKNOWN) {
  1166. if (SDL_IsJoystickXInput(guid)) {
  1167. /* This is probably an Xbox One controller */
  1168. return SDL_CONTROLLER_TYPE_XBOXONE;
  1169. }
  1170. }
  1171. return type;
  1172. }
  1173. SDL_GameControllerType
  1174. SDL_GetJoystickGameControllerType(Uint16 vendor, Uint16 product, const char *name)
  1175. {
  1176. if (vendor == 0x0000 && product == 0x0000) {
  1177. /* Some devices are only identifiable by their name */
  1178. if (SDL_strcmp(name, "Lic Pro Controller") == 0 ||
  1179. SDL_strcmp(name, "Nintendo Wireless Gamepad") == 0 ||
  1180. SDL_strcmp(name, "Wireless Gamepad") == 0) {
  1181. /* HORI or PowerA Switch Pro Controller clone */
  1182. return SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO;
  1183. }
  1184. return SDL_CONTROLLER_TYPE_UNKNOWN;
  1185. }
  1186. if (vendor == 0x0001 && product == 0x0001) {
  1187. return SDL_CONTROLLER_TYPE_UNKNOWN;
  1188. }
  1189. switch (GuessControllerType(vendor, product)) {
  1190. case k_eControllerType_XBox360Controller:
  1191. return SDL_CONTROLLER_TYPE_XBOX360;
  1192. case k_eControllerType_XBoxOneController:
  1193. return SDL_CONTROLLER_TYPE_XBOXONE;
  1194. case k_eControllerType_PS3Controller:
  1195. return SDL_CONTROLLER_TYPE_PS3;
  1196. case k_eControllerType_PS4Controller:
  1197. return SDL_CONTROLLER_TYPE_PS4;
  1198. case k_eControllerType_SwitchProController:
  1199. case k_eControllerType_SwitchInputOnlyController:
  1200. return SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO;
  1201. default:
  1202. return SDL_CONTROLLER_TYPE_UNKNOWN;
  1203. }
  1204. }
  1205. SDL_bool
  1206. SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor, Uint16 product)
  1207. {
  1208. EControllerType eType = GuessControllerType(vendor, product);
  1209. return (eType == k_eControllerType_SwitchInputOnlyController);
  1210. }
  1211. SDL_bool
  1212. SDL_IsJoystickSteamController(Uint16 vendor, Uint16 product)
  1213. {
  1214. EControllerType eType = GuessControllerType(vendor, product);
  1215. return (eType == k_eControllerType_SteamController ||
  1216. eType == k_eControllerType_SteamControllerV2);
  1217. }
  1218. SDL_bool
  1219. SDL_IsJoystickXInput(SDL_JoystickGUID guid)
  1220. {
  1221. return (guid.data[14] == 'x') ? SDL_TRUE : SDL_FALSE;
  1222. }
  1223. SDL_bool
  1224. SDL_IsJoystickHIDAPI(SDL_JoystickGUID guid)
  1225. {
  1226. return (guid.data[14] == 'h') ? SDL_TRUE : SDL_FALSE;
  1227. }
  1228. static SDL_bool SDL_IsJoystickProductWheel(Uint32 vidpid)
  1229. {
  1230. static Uint32 wheel_joysticks[] = {
  1231. MAKE_VIDPID(0x046d, 0xc294), /* Logitech generic wheel */
  1232. MAKE_VIDPID(0x046d, 0xc295), /* Logitech Momo Force */
  1233. MAKE_VIDPID(0x046d, 0xc298), /* Logitech Driving Force Pro */
  1234. MAKE_VIDPID(0x046d, 0xc299), /* Logitech G25 */
  1235. MAKE_VIDPID(0x046d, 0xc29a), /* Logitech Driving Force GT */
  1236. MAKE_VIDPID(0x046d, 0xc29b), /* Logitech G27 */
  1237. MAKE_VIDPID(0x046d, 0xc261), /* Logitech G920 (initial mode) */
  1238. MAKE_VIDPID(0x046d, 0xc262), /* Logitech G920 (active mode) */
  1239. MAKE_VIDPID(0x044f, 0xb65d), /* Thrustmaster Wheel FFB */
  1240. MAKE_VIDPID(0x044f, 0xb66d), /* Thrustmaster Wheel FFB */
  1241. MAKE_VIDPID(0x044f, 0xb677), /* Thrustmaster T150 */
  1242. MAKE_VIDPID(0x044f, 0xb664), /* Thrustmaster TX (initial mode) */
  1243. MAKE_VIDPID(0x044f, 0xb669), /* Thrustmaster TX (active mode) */
  1244. };
  1245. int i;
  1246. for (i = 0; i < SDL_arraysize(wheel_joysticks); ++i) {
  1247. if (vidpid == wheel_joysticks[i]) {
  1248. return SDL_TRUE;
  1249. }
  1250. }
  1251. return SDL_FALSE;
  1252. }
  1253. static SDL_bool SDL_IsJoystickProductFlightStick(Uint32 vidpid)
  1254. {
  1255. static Uint32 flightstick_joysticks[] = {
  1256. MAKE_VIDPID(0x044f, 0x0402), /* HOTAS Warthog Joystick */
  1257. MAKE_VIDPID(0x0738, 0x2221), /* Saitek Pro Flight X-56 Rhino Stick */
  1258. };
  1259. int i;
  1260. for (i = 0; i < SDL_arraysize(flightstick_joysticks); ++i) {
  1261. if (vidpid == flightstick_joysticks[i]) {
  1262. return SDL_TRUE;
  1263. }
  1264. }
  1265. return SDL_FALSE;
  1266. }
  1267. static SDL_bool SDL_IsJoystickProductThrottle(Uint32 vidpid)
  1268. {
  1269. static Uint32 throttle_joysticks[] = {
  1270. MAKE_VIDPID(0x044f, 0x0404), /* HOTAS Warthog Throttle */
  1271. MAKE_VIDPID(0x0738, 0xa221), /* Saitek Pro Flight X-56 Rhino Throttle */
  1272. };
  1273. int i;
  1274. for (i = 0; i < SDL_arraysize(throttle_joysticks); ++i) {
  1275. if (vidpid == throttle_joysticks[i]) {
  1276. return SDL_TRUE;
  1277. }
  1278. }
  1279. return SDL_FALSE;
  1280. }
  1281. static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
  1282. {
  1283. Uint16 vendor;
  1284. Uint16 product;
  1285. Uint32 vidpid;
  1286. if (SDL_IsJoystickXInput(guid)) {
  1287. /* XInput GUID, get the type based on the XInput device subtype */
  1288. switch (guid.data[15]) {
  1289. case 0x01: /* XINPUT_DEVSUBTYPE_GAMEPAD */
  1290. return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1291. case 0x02: /* XINPUT_DEVSUBTYPE_WHEEL */
  1292. return SDL_JOYSTICK_TYPE_WHEEL;
  1293. case 0x03: /* XINPUT_DEVSUBTYPE_ARCADE_STICK */
  1294. return SDL_JOYSTICK_TYPE_ARCADE_STICK;
  1295. case 0x04: /* XINPUT_DEVSUBTYPE_FLIGHT_STICK */
  1296. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  1297. case 0x05: /* XINPUT_DEVSUBTYPE_DANCE_PAD */
  1298. return SDL_JOYSTICK_TYPE_DANCE_PAD;
  1299. case 0x06: /* XINPUT_DEVSUBTYPE_GUITAR */
  1300. case 0x07: /* XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE */
  1301. case 0x0B: /* XINPUT_DEVSUBTYPE_GUITAR_BASS */
  1302. return SDL_JOYSTICK_TYPE_GUITAR;
  1303. case 0x08: /* XINPUT_DEVSUBTYPE_DRUM_KIT */
  1304. return SDL_JOYSTICK_TYPE_DRUM_KIT;
  1305. case 0x13: /* XINPUT_DEVSUBTYPE_ARCADE_PAD */
  1306. return SDL_JOYSTICK_TYPE_ARCADE_PAD;
  1307. default:
  1308. return SDL_JOYSTICK_TYPE_UNKNOWN;
  1309. }
  1310. }
  1311. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  1312. vidpid = MAKE_VIDPID(vendor, product);
  1313. if (SDL_IsJoystickProductWheel(vidpid)) {
  1314. return SDL_JOYSTICK_TYPE_WHEEL;
  1315. }
  1316. if (SDL_IsJoystickProductFlightStick(vidpid)) {
  1317. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  1318. }
  1319. if (SDL_IsJoystickProductThrottle(vidpid)) {
  1320. return SDL_JOYSTICK_TYPE_THROTTLE;
  1321. }
  1322. if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) {
  1323. return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1324. }
  1325. return SDL_JOYSTICK_TYPE_UNKNOWN;
  1326. }
  1327. static SDL_bool SDL_IsPS4RemapperRunning(void)
  1328. {
  1329. #ifdef __WIN32__
  1330. const char *mapper_processes[] = {
  1331. "DS4Windows.exe",
  1332. "InputMapper.exe",
  1333. };
  1334. int i;
  1335. PROCESSENTRY32 pe32;
  1336. SDL_bool found = SDL_FALSE;
  1337. /* Take a snapshot of all processes in the system */
  1338. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  1339. if (hProcessSnap != INVALID_HANDLE_VALUE) {
  1340. pe32.dwSize = sizeof(PROCESSENTRY32);
  1341. if (Process32First(hProcessSnap, &pe32)) {
  1342. do
  1343. {
  1344. for (i = 0; i < SDL_arraysize(mapper_processes); ++i) {
  1345. if (SDL_strcasecmp(pe32.szExeFile, mapper_processes[i]) == 0) {
  1346. found = SDL_TRUE;
  1347. }
  1348. }
  1349. } while (Process32Next(hProcessSnap, &pe32) && !found);
  1350. }
  1351. CloseHandle(hProcessSnap);
  1352. }
  1353. return found;
  1354. #else
  1355. return SDL_FALSE;
  1356. #endif
  1357. }
  1358. SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid)
  1359. {
  1360. /* This list is taken from:
  1361. https://raw.githubusercontent.com/denilsonsa/udev-joystick-blacklist/master/generate_rules.py
  1362. */
  1363. static Uint32 joystick_blacklist[] = {
  1364. /* Microsoft Microsoft Wireless Optical Desktop® 2.10 */
  1365. /* Microsoft Wireless Desktop - Comfort Edition */
  1366. MAKE_VIDPID(0x045e, 0x009d),
  1367. /* Microsoft Microsoft® Digital Media Pro Keyboard */
  1368. /* Microsoft Corp. Digital Media Pro Keyboard */
  1369. MAKE_VIDPID(0x045e, 0x00b0),
  1370. /* Microsoft Microsoft® Digital Media Keyboard */
  1371. /* Microsoft Corp. Digital Media Keyboard 1.0A */
  1372. MAKE_VIDPID(0x045e, 0x00b4),
  1373. /* Microsoft Microsoft® Digital Media Keyboard 3000 */
  1374. MAKE_VIDPID(0x045e, 0x0730),
  1375. /* Microsoft Microsoft® 2.4GHz Transceiver v6.0 */
  1376. /* Microsoft Microsoft® 2.4GHz Transceiver v8.0 */
  1377. /* Microsoft Corp. Nano Transceiver v1.0 for Bluetooth */
  1378. /* Microsoft Wireless Mobile Mouse 1000 */
  1379. /* Microsoft Wireless Desktop 3000 */
  1380. MAKE_VIDPID(0x045e, 0x0745),
  1381. /* Microsoft® SideWinder(TM) 2.4GHz Transceiver */
  1382. MAKE_VIDPID(0x045e, 0x0748),
  1383. /* Microsoft Corp. Wired Keyboard 600 */
  1384. MAKE_VIDPID(0x045e, 0x0750),
  1385. /* Microsoft Corp. Sidewinder X4 keyboard */
  1386. MAKE_VIDPID(0x045e, 0x0768),
  1387. /* Microsoft Corp. Arc Touch Mouse Transceiver */
  1388. MAKE_VIDPID(0x045e, 0x0773),
  1389. /* Microsoft® 2.4GHz Transceiver v9.0 */
  1390. /* Microsoft® Nano Transceiver v2.1 */
  1391. /* Microsoft Sculpt Ergonomic Keyboard (5KV-00001) */
  1392. MAKE_VIDPID(0x045e, 0x07a5),
  1393. /* Microsoft® Nano Transceiver v1.0 */
  1394. /* Microsoft Wireless Keyboard 800 */
  1395. MAKE_VIDPID(0x045e, 0x07b2),
  1396. /* Microsoft® Nano Transceiver v2.0 */
  1397. MAKE_VIDPID(0x045e, 0x0800),
  1398. MAKE_VIDPID(0x046d, 0xc30a), /* Logitech, Inc. iTouch Composite keboard */
  1399. MAKE_VIDPID(0x04d9, 0xa0df), /* Tek Syndicate Mouse (E-Signal USB Gaming Mouse) */
  1400. /* List of Wacom devices at: http://linuxwacom.sourceforge.net/wiki/index.php/Device_IDs */
  1401. MAKE_VIDPID(0x056a, 0x0010), /* Wacom ET-0405 Graphire */
  1402. MAKE_VIDPID(0x056a, 0x0011), /* Wacom ET-0405A Graphire2 (4x5) */
  1403. MAKE_VIDPID(0x056a, 0x0012), /* Wacom ET-0507A Graphire2 (5x7) */
  1404. MAKE_VIDPID(0x056a, 0x0013), /* Wacom CTE-430 Graphire3 (4x5) */
  1405. MAKE_VIDPID(0x056a, 0x0014), /* Wacom CTE-630 Graphire3 (6x8) */
  1406. MAKE_VIDPID(0x056a, 0x0015), /* Wacom CTE-440 Graphire4 (4x5) */
  1407. MAKE_VIDPID(0x056a, 0x0016), /* Wacom CTE-640 Graphire4 (6x8) */
  1408. MAKE_VIDPID(0x056a, 0x0017), /* Wacom CTE-450 Bamboo Fun (4x5) */
  1409. MAKE_VIDPID(0x056a, 0x0018), /* Wacom CTE-650 Bamboo Fun 6x8 */
  1410. MAKE_VIDPID(0x056a, 0x0019), /* Wacom CTE-631 Bamboo One */
  1411. MAKE_VIDPID(0x056a, 0x00d1), /* Wacom Bamboo Pen and Touch CTH-460 */
  1412. MAKE_VIDPID(0x056a, 0x030e), /* Wacom Intuos Pen (S) CTL-480 */
  1413. MAKE_VIDPID(0x09da, 0x054f), /* A4 Tech Co., G7 750 mouse */
  1414. MAKE_VIDPID(0x09da, 0x1410), /* A4 Tech Co., Ltd Bloody AL9 mouse */
  1415. MAKE_VIDPID(0x09da, 0x3043), /* A4 Tech Co., Ltd Bloody R8A Gaming Mouse */
  1416. MAKE_VIDPID(0x09da, 0x31b5), /* A4 Tech Co., Ltd Bloody TL80 Terminator Laser Gaming Mouse */
  1417. MAKE_VIDPID(0x09da, 0x3997), /* A4 Tech Co., Ltd Bloody RT7 Terminator Wireless */
  1418. MAKE_VIDPID(0x09da, 0x3f8b), /* A4 Tech Co., Ltd Bloody V8 mouse */
  1419. MAKE_VIDPID(0x09da, 0x51f4), /* Modecom MC-5006 Keyboard */
  1420. MAKE_VIDPID(0x09da, 0x5589), /* A4 Tech Co., Ltd Terminator TL9 Laser Gaming Mouse */
  1421. MAKE_VIDPID(0x09da, 0x7b22), /* A4 Tech Co., Ltd Bloody V5 */
  1422. MAKE_VIDPID(0x09da, 0x7f2d), /* A4 Tech Co., Ltd Bloody R3 mouse */
  1423. MAKE_VIDPID(0x09da, 0x8090), /* A4 Tech Co., Ltd X-718BK Oscar Optical Gaming Mouse */
  1424. MAKE_VIDPID(0x09da, 0x9033), /* A4 Tech Co., X7 X-705K */
  1425. MAKE_VIDPID(0x09da, 0x9066), /* A4 Tech Co., Sharkoon Fireglider Optical */
  1426. MAKE_VIDPID(0x09da, 0x9090), /* A4 Tech Co., Ltd XL-730K / XL-750BK / XL-755BK Laser Mouse */
  1427. MAKE_VIDPID(0x09da, 0x90c0), /* A4 Tech Co., Ltd X7 G800V keyboard */
  1428. MAKE_VIDPID(0x09da, 0xf012), /* A4 Tech Co., Ltd Bloody V7 mouse */
  1429. MAKE_VIDPID(0x09da, 0xf32a), /* A4 Tech Co., Ltd Bloody B540 keyboard */
  1430. MAKE_VIDPID(0x09da, 0xf613), /* A4 Tech Co., Ltd Bloody V2 mouse */
  1431. MAKE_VIDPID(0x09da, 0xf624), /* A4 Tech Co., Ltd Bloody B120 Keyboard */
  1432. MAKE_VIDPID(0x1b1c, 0x1b3c), /* Corsair Harpoon RGB gaming mouse */
  1433. MAKE_VIDPID(0x1d57, 0xad03), /* [T3] 2.4GHz and IR Air Mouse Remote Control */
  1434. MAKE_VIDPID(0x1e7d, 0x2e4a), /* Roccat Tyon Mouse */
  1435. MAKE_VIDPID(0x20a0, 0x422d), /* Winkeyless.kr Keyboards */
  1436. MAKE_VIDPID(0x2516, 0x001f), /* Cooler Master Storm Mizar Mouse */
  1437. MAKE_VIDPID(0x2516, 0x0028), /* Cooler Master Storm Alcor Mouse */
  1438. };
  1439. unsigned int i;
  1440. Uint32 id;
  1441. Uint16 vendor;
  1442. Uint16 product;
  1443. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  1444. /* Check the joystick blacklist */
  1445. id = MAKE_VIDPID(vendor, product);
  1446. for (i = 0; i < SDL_arraysize(joystick_blacklist); ++i) {
  1447. if (id == joystick_blacklist[i]) {
  1448. return SDL_TRUE;
  1449. }
  1450. }
  1451. if (SDL_GetJoystickGameControllerType(vendor, product, name) == SDL_CONTROLLER_TYPE_PS4 && SDL_IsPS4RemapperRunning()) {
  1452. return SDL_TRUE;
  1453. }
  1454. if (SDL_IsGameControllerNameAndGUID(name, guid) &&
  1455. SDL_ShouldIgnoreGameController(name, guid)) {
  1456. return SDL_TRUE;
  1457. }
  1458. return SDL_FALSE;
  1459. }
  1460. /* return the guid for this index */
  1461. SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
  1462. {
  1463. SDL_JoystickDriver *driver;
  1464. SDL_JoystickGUID guid;
  1465. SDL_LockJoysticks();
  1466. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  1467. guid = driver->GetDeviceGUID(device_index);
  1468. } else {
  1469. SDL_zero(guid);
  1470. }
  1471. SDL_UnlockJoysticks();
  1472. return guid;
  1473. }
  1474. Uint16 SDL_JoystickGetDeviceVendor(int device_index)
  1475. {
  1476. Uint16 vendor;
  1477. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1478. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
  1479. return vendor;
  1480. }
  1481. Uint16 SDL_JoystickGetDeviceProduct(int device_index)
  1482. {
  1483. Uint16 product;
  1484. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1485. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
  1486. return product;
  1487. }
  1488. Uint16 SDL_JoystickGetDeviceProductVersion(int device_index)
  1489. {
  1490. Uint16 version;
  1491. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1492. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
  1493. return version;
  1494. }
  1495. SDL_JoystickType SDL_JoystickGetDeviceType(int device_index)
  1496. {
  1497. SDL_JoystickType type;
  1498. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1499. type = SDL_GetJoystickGUIDType(guid);
  1500. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  1501. if (SDL_IsGameController(device_index)) {
  1502. type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1503. }
  1504. }
  1505. return type;
  1506. }
  1507. SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
  1508. {
  1509. SDL_JoystickDriver *driver;
  1510. SDL_JoystickID instance_id = -1;
  1511. SDL_LockJoysticks();
  1512. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  1513. instance_id = driver->GetDeviceInstanceID(device_index);
  1514. }
  1515. SDL_UnlockJoysticks();
  1516. return instance_id;
  1517. }
  1518. int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id)
  1519. {
  1520. int i, num_joysticks, device_index = -1;
  1521. SDL_LockJoysticks();
  1522. num_joysticks = SDL_NumJoysticks();
  1523. for (i = 0; i < num_joysticks; ++i) {
  1524. if (SDL_JoystickGetDeviceInstanceID(i) == instance_id) {
  1525. device_index = i;
  1526. break;
  1527. }
  1528. }
  1529. SDL_UnlockJoysticks();
  1530. return device_index;
  1531. }
  1532. SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
  1533. {
  1534. if (!SDL_PrivateJoystickValid(joystick)) {
  1535. SDL_JoystickGUID emptyGUID;
  1536. SDL_zero(emptyGUID);
  1537. return emptyGUID;
  1538. }
  1539. return joystick->guid;
  1540. }
  1541. Uint16 SDL_JoystickGetVendor(SDL_Joystick * joystick)
  1542. {
  1543. Uint16 vendor;
  1544. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1545. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
  1546. return vendor;
  1547. }
  1548. Uint16 SDL_JoystickGetProduct(SDL_Joystick * joystick)
  1549. {
  1550. Uint16 product;
  1551. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1552. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
  1553. return product;
  1554. }
  1555. Uint16 SDL_JoystickGetProductVersion(SDL_Joystick * joystick)
  1556. {
  1557. Uint16 version;
  1558. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1559. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
  1560. return version;
  1561. }
  1562. SDL_JoystickType SDL_JoystickGetType(SDL_Joystick * joystick)
  1563. {
  1564. SDL_JoystickType type;
  1565. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1566. type = SDL_GetJoystickGUIDType(guid);
  1567. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  1568. if (joystick && joystick->is_game_controller) {
  1569. type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1570. }
  1571. }
  1572. return type;
  1573. }
  1574. /* convert the guid to a printable string */
  1575. void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
  1576. {
  1577. static const char k_rgchHexToASCII[] = "0123456789abcdef";
  1578. int i;
  1579. if ((pszGUID == NULL) || (cbGUID <= 0)) {
  1580. return;
  1581. }
  1582. for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
  1583. /* each input byte writes 2 ascii chars, and might write a null byte. */
  1584. /* If we don't have room for next input byte, stop */
  1585. unsigned char c = guid.data[i];
  1586. *pszGUID++ = k_rgchHexToASCII[c >> 4];
  1587. *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
  1588. }
  1589. *pszGUID = '\0';
  1590. }
  1591. /*-----------------------------------------------------------------------------
  1592. * Purpose: Returns the 4 bit nibble for a hex character
  1593. * Input : c -
  1594. * Output : unsigned char
  1595. *-----------------------------------------------------------------------------*/
  1596. static unsigned char nibble(char c)
  1597. {
  1598. if ((c >= '0') && (c <= '9')) {
  1599. return (unsigned char)(c - '0');
  1600. }
  1601. if ((c >= 'A') && (c <= 'F')) {
  1602. return (unsigned char)(c - 'A' + 0x0a);
  1603. }
  1604. if ((c >= 'a') && (c <= 'f')) {
  1605. return (unsigned char)(c - 'a' + 0x0a);
  1606. }
  1607. /* received an invalid character, and no real way to return an error */
  1608. /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
  1609. return 0;
  1610. }
  1611. /* convert the string version of a joystick guid to the struct */
  1612. SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
  1613. {
  1614. SDL_JoystickGUID guid;
  1615. int maxoutputbytes= sizeof(guid);
  1616. size_t len = SDL_strlen(pchGUID);
  1617. Uint8 *p;
  1618. size_t i;
  1619. /* Make sure it's even */
  1620. len = (len) & ~0x1;
  1621. SDL_memset(&guid, 0x00, sizeof(guid));
  1622. p = (Uint8 *)&guid;
  1623. for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
  1624. *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
  1625. }
  1626. return guid;
  1627. }
  1628. /* update the power level for this joystick */
  1629. void SDL_PrivateJoystickBatteryLevel(SDL_Joystick * joystick, SDL_JoystickPowerLevel ePowerLevel)
  1630. {
  1631. joystick->epowerlevel = ePowerLevel;
  1632. }
  1633. /* return its power level */
  1634. SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick)
  1635. {
  1636. if (!SDL_PrivateJoystickValid(joystick)) {
  1637. return SDL_JOYSTICK_POWER_UNKNOWN;
  1638. }
  1639. return joystick->epowerlevel;
  1640. }
  1641. /* vi: set ts=4 sw=4 expandtab: */