SDL_joystick.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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. /* This is included in only one place because it has a large static list of controllers */
  31. #include "controller_type.h"
  32. #ifdef __WIN32__
  33. /* Needed for checking for input remapping programs */
  34. #include "../core/windows/SDL_windows.h"
  35. #undef UNICODE /* We want ASCII functions */
  36. #include <tlhelp32.h>
  37. #endif
  38. static SDL_JoystickDriver *SDL_joystick_drivers[] = {
  39. #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)
  40. &SDL_WINDOWS_JoystickDriver,
  41. #endif
  42. #ifdef SDL_JOYSTICK_LINUX
  43. &SDL_LINUX_JoystickDriver,
  44. #endif
  45. #ifdef SDL_JOYSTICK_IOKIT
  46. &SDL_DARWIN_JoystickDriver,
  47. #endif
  48. #if defined(__IPHONEOS__) || defined(__TVOS__)
  49. &SDL_IOS_JoystickDriver,
  50. #endif
  51. #ifdef SDL_JOYSTICK_ANDROID
  52. &SDL_ANDROID_JoystickDriver,
  53. #endif
  54. #ifdef SDL_JOYSTICK_EMSCRIPTEN
  55. &SDL_EMSCRIPTEN_JoystickDriver,
  56. #endif
  57. #ifdef SDL_JOYSTICK_HAIKU
  58. &SDL_HAIKU_JoystickDriver,
  59. #endif
  60. #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. */
  61. &SDL_BSD_JoystickDriver,
  62. #endif
  63. #ifdef SDL_JOYSTICK_HIDAPI
  64. &SDL_HIDAPI_JoystickDriver,
  65. #endif
  66. #if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED)
  67. &SDL_DUMMY_JoystickDriver
  68. #endif
  69. };
  70. static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
  71. static SDL_Joystick *SDL_joysticks = NULL;
  72. static SDL_bool SDL_updating_joystick = SDL_FALSE;
  73. static SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
  74. static SDL_atomic_t SDL_next_joystick_instance_id;
  75. void
  76. SDL_LockJoysticks(void)
  77. {
  78. if (SDL_joystick_lock) {
  79. SDL_LockMutex(SDL_joystick_lock);
  80. }
  81. }
  82. void
  83. SDL_UnlockJoysticks(void)
  84. {
  85. if (SDL_joystick_lock) {
  86. SDL_UnlockMutex(SDL_joystick_lock);
  87. }
  88. }
  89. static void SDLCALL
  90. SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  91. {
  92. if (hint && *hint == '1') {
  93. SDL_joystick_allows_background_events = SDL_TRUE;
  94. } else {
  95. SDL_joystick_allows_background_events = SDL_FALSE;
  96. }
  97. }
  98. int
  99. SDL_JoystickInit(void)
  100. {
  101. int i, status;
  102. SDL_GameControllerInitMappings();
  103. /* Create the joystick list lock */
  104. if (!SDL_joystick_lock) {
  105. SDL_joystick_lock = SDL_CreateMutex();
  106. }
  107. /* See if we should allow joystick events while in the background */
  108. SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  109. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  110. #if !SDL_EVENTS_DISABLED
  111. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  112. return -1;
  113. }
  114. #endif /* !SDL_EVENTS_DISABLED */
  115. status = -1;
  116. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  117. if (SDL_joystick_drivers[i]->Init() >= 0) {
  118. status = 0;
  119. }
  120. }
  121. return status;
  122. }
  123. /*
  124. * Count the number of joysticks attached to the system
  125. */
  126. int
  127. SDL_NumJoysticks(void)
  128. {
  129. int i, total_joysticks = 0;
  130. SDL_LockJoysticks();
  131. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  132. total_joysticks += SDL_joystick_drivers[i]->GetCount();
  133. }
  134. SDL_UnlockJoysticks();
  135. return total_joysticks;
  136. }
  137. /*
  138. * Return the next available joystick instance ID
  139. * This may be called by drivers from multiple threads, unprotected by any locks
  140. */
  141. SDL_JoystickID SDL_GetNextJoystickInstanceID()
  142. {
  143. return SDL_AtomicIncRef(&SDL_next_joystick_instance_id);
  144. }
  145. /*
  146. * Get the driver and device index for an API device index
  147. * This should be called while the joystick lock is held, to prevent another thread from updating the list
  148. */
  149. SDL_bool
  150. SDL_GetDriverAndJoystickIndex(int device_index, SDL_JoystickDriver **driver, int *driver_index)
  151. {
  152. int i, num_joysticks, total_joysticks = 0;
  153. if (device_index >= 0) {
  154. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  155. num_joysticks = SDL_joystick_drivers[i]->GetCount();
  156. if (device_index < num_joysticks) {
  157. *driver = SDL_joystick_drivers[i];
  158. *driver_index = device_index;
  159. return SDL_TRUE;
  160. }
  161. device_index -= num_joysticks;
  162. total_joysticks += num_joysticks;
  163. }
  164. }
  165. SDL_SetError("There are %d joysticks available", total_joysticks);
  166. return SDL_FALSE;
  167. }
  168. /*
  169. * Perform any needed fixups for joystick names
  170. */
  171. static const char *
  172. SDL_FixupJoystickName(const char *name)
  173. {
  174. if (name) {
  175. const char *skip_prefix = "NVIDIA Corporation ";
  176. if (SDL_strncmp(name, skip_prefix, SDL_strlen(skip_prefix)) == 0) {
  177. name += SDL_strlen(skip_prefix);
  178. }
  179. }
  180. return name;
  181. }
  182. /*
  183. * Get the implementation dependent name of a joystick
  184. */
  185. const char *
  186. SDL_JoystickNameForIndex(int device_index)
  187. {
  188. SDL_JoystickDriver *driver;
  189. const char *name = NULL;
  190. SDL_LockJoysticks();
  191. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  192. name = SDL_FixupJoystickName(driver->GetDeviceName(device_index));
  193. }
  194. SDL_UnlockJoysticks();
  195. /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
  196. return name;
  197. }
  198. /*
  199. * Return true if this joystick is known to have all axes centered at zero
  200. * This isn't generally needed unless the joystick never generates an initial axis value near zero,
  201. * e.g. it's emulating axes with digital buttons
  202. */
  203. static SDL_bool
  204. SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
  205. {
  206. static Uint32 zero_centered_joysticks[] = {
  207. MAKE_VIDPID(0x0e8f, 0x3013), /* HuiJia SNES USB adapter */
  208. MAKE_VIDPID(0x05a0, 0x3232), /* 8Bitdo Zero Gamepad */
  209. };
  210. int i;
  211. Uint32 id = MAKE_VIDPID(SDL_JoystickGetVendor(joystick),
  212. SDL_JoystickGetProduct(joystick));
  213. /*printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);*/
  214. if (joystick->naxes == 2) {
  215. /* Assume D-pad or thumbstick style axes are centered at 0 */
  216. return SDL_TRUE;
  217. }
  218. for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
  219. if (id == zero_centered_joysticks[i]) {
  220. return SDL_TRUE;
  221. }
  222. }
  223. return SDL_FALSE;
  224. }
  225. /*
  226. * Open a joystick for use - the index passed as an argument refers to
  227. * the N'th joystick on the system. This index is the value which will
  228. * identify this joystick in future joystick events.
  229. *
  230. * This function returns a joystick identifier, or NULL if an error occurred.
  231. */
  232. SDL_Joystick *
  233. SDL_JoystickOpen(int device_index)
  234. {
  235. SDL_JoystickDriver *driver;
  236. SDL_JoystickID instance_id;
  237. SDL_Joystick *joystick;
  238. SDL_Joystick *joysticklist;
  239. const char *joystickname = NULL;
  240. SDL_LockJoysticks();
  241. if (!SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  242. SDL_UnlockJoysticks();
  243. return NULL;
  244. }
  245. joysticklist = SDL_joysticks;
  246. /* If the joystick is already open, return it
  247. * it is important that we have a single joystick * for each instance id
  248. */
  249. instance_id = driver->GetDeviceInstanceID(device_index);
  250. while (joysticklist) {
  251. if (instance_id == joysticklist->instance_id) {
  252. joystick = joysticklist;
  253. ++joystick->ref_count;
  254. SDL_UnlockJoysticks();
  255. return joystick;
  256. }
  257. joysticklist = joysticklist->next;
  258. }
  259. /* Create and initialize the joystick */
  260. joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
  261. if (joystick == NULL) {
  262. SDL_OutOfMemory();
  263. SDL_UnlockJoysticks();
  264. return NULL;
  265. }
  266. joystick->driver = driver;
  267. joystick->instance_id = instance_id;
  268. joystick->attached = SDL_TRUE;
  269. if (driver->Open(joystick, device_index) < 0) {
  270. SDL_free(joystick);
  271. SDL_UnlockJoysticks();
  272. return NULL;
  273. }
  274. joystickname = driver->GetDeviceName(device_index);
  275. if (joystickname) {
  276. joystick->name = SDL_strdup(joystickname);
  277. } else {
  278. joystick->name = NULL;
  279. }
  280. joystick->guid = driver->GetDeviceGUID(device_index);
  281. if (joystick->naxes > 0) {
  282. joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo));
  283. }
  284. if (joystick->nhats > 0) {
  285. joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8));
  286. }
  287. if (joystick->nballs > 0) {
  288. joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls));
  289. }
  290. if (joystick->nbuttons > 0) {
  291. joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8));
  292. }
  293. if (((joystick->naxes > 0) && !joystick->axes)
  294. || ((joystick->nhats > 0) && !joystick->hats)
  295. || ((joystick->nballs > 0) && !joystick->balls)
  296. || ((joystick->nbuttons > 0) && !joystick->buttons)) {
  297. SDL_OutOfMemory();
  298. SDL_JoystickClose(joystick);
  299. SDL_UnlockJoysticks();
  300. return NULL;
  301. }
  302. joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
  303. /* If this joystick is known to have all zero centered axes, skip the auto-centering code */
  304. if (SDL_JoystickAxesCenteredAtZero(joystick)) {
  305. int i;
  306. for (i = 0; i < joystick->naxes; ++i) {
  307. joystick->axes[i].has_initial_value = SDL_TRUE;
  308. }
  309. }
  310. joystick->is_game_controller = SDL_IsGameController(device_index);
  311. /* Add joystick to list */
  312. ++joystick->ref_count;
  313. /* Link the joystick in the list */
  314. joystick->next = SDL_joysticks;
  315. SDL_joysticks = joystick;
  316. SDL_UnlockJoysticks();
  317. driver->Update(joystick);
  318. return joystick;
  319. }
  320. /*
  321. * Checks to make sure the joystick is valid.
  322. */
  323. int
  324. SDL_PrivateJoystickValid(SDL_Joystick * joystick)
  325. {
  326. int valid;
  327. if (joystick == NULL) {
  328. SDL_SetError("Joystick hasn't been opened yet");
  329. valid = 0;
  330. } else {
  331. valid = 1;
  332. }
  333. return valid;
  334. }
  335. /*
  336. * Get the number of multi-dimensional axis controls on a joystick
  337. */
  338. int
  339. SDL_JoystickNumAxes(SDL_Joystick * joystick)
  340. {
  341. if (!SDL_PrivateJoystickValid(joystick)) {
  342. return -1;
  343. }
  344. return joystick->naxes;
  345. }
  346. /*
  347. * Get the number of hats on a joystick
  348. */
  349. int
  350. SDL_JoystickNumHats(SDL_Joystick * joystick)
  351. {
  352. if (!SDL_PrivateJoystickValid(joystick)) {
  353. return -1;
  354. }
  355. return joystick->nhats;
  356. }
  357. /*
  358. * Get the number of trackballs on a joystick
  359. */
  360. int
  361. SDL_JoystickNumBalls(SDL_Joystick * joystick)
  362. {
  363. if (!SDL_PrivateJoystickValid(joystick)) {
  364. return -1;
  365. }
  366. return joystick->nballs;
  367. }
  368. /*
  369. * Get the number of buttons on a joystick
  370. */
  371. int
  372. SDL_JoystickNumButtons(SDL_Joystick * joystick)
  373. {
  374. if (!SDL_PrivateJoystickValid(joystick)) {
  375. return -1;
  376. }
  377. return joystick->nbuttons;
  378. }
  379. /*
  380. * Get the current state of an axis control on a joystick
  381. */
  382. Sint16
  383. SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
  384. {
  385. Sint16 state;
  386. if (!SDL_PrivateJoystickValid(joystick)) {
  387. return 0;
  388. }
  389. if (axis < joystick->naxes) {
  390. state = joystick->axes[axis].value;
  391. } else {
  392. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  393. state = 0;
  394. }
  395. return state;
  396. }
  397. /*
  398. * Get the initial state of an axis control on a joystick
  399. */
  400. SDL_bool
  401. SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick, int axis, Sint16 *state)
  402. {
  403. if (!SDL_PrivateJoystickValid(joystick)) {
  404. return SDL_FALSE;
  405. }
  406. if (axis >= joystick->naxes) {
  407. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  408. return SDL_FALSE;
  409. }
  410. if (state) {
  411. *state = joystick->axes[axis].initial_value;
  412. }
  413. return joystick->axes[axis].has_initial_value;
  414. }
  415. /*
  416. * Get the current state of a hat on a joystick
  417. */
  418. Uint8
  419. SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
  420. {
  421. Uint8 state;
  422. if (!SDL_PrivateJoystickValid(joystick)) {
  423. return 0;
  424. }
  425. if (hat < joystick->nhats) {
  426. state = joystick->hats[hat];
  427. } else {
  428. SDL_SetError("Joystick only has %d hats", joystick->nhats);
  429. state = 0;
  430. }
  431. return state;
  432. }
  433. /*
  434. * Get the ball axis change since the last poll
  435. */
  436. int
  437. SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
  438. {
  439. int retval;
  440. if (!SDL_PrivateJoystickValid(joystick)) {
  441. return -1;
  442. }
  443. retval = 0;
  444. if (ball < joystick->nballs) {
  445. if (dx) {
  446. *dx = joystick->balls[ball].dx;
  447. }
  448. if (dy) {
  449. *dy = joystick->balls[ball].dy;
  450. }
  451. joystick->balls[ball].dx = 0;
  452. joystick->balls[ball].dy = 0;
  453. } else {
  454. return SDL_SetError("Joystick only has %d balls", joystick->nballs);
  455. }
  456. return retval;
  457. }
  458. /*
  459. * Get the current state of a button on a joystick
  460. */
  461. Uint8
  462. SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
  463. {
  464. Uint8 state;
  465. if (!SDL_PrivateJoystickValid(joystick)) {
  466. return 0;
  467. }
  468. if (button < joystick->nbuttons) {
  469. state = joystick->buttons[button];
  470. } else {
  471. SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
  472. state = 0;
  473. }
  474. return state;
  475. }
  476. /*
  477. * Return if the joystick in question is currently attached to the system,
  478. * \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
  479. */
  480. SDL_bool
  481. SDL_JoystickGetAttached(SDL_Joystick * joystick)
  482. {
  483. if (!SDL_PrivateJoystickValid(joystick)) {
  484. return SDL_FALSE;
  485. }
  486. return joystick->attached;
  487. }
  488. /*
  489. * Get the instance id for this opened joystick
  490. */
  491. SDL_JoystickID
  492. SDL_JoystickInstanceID(SDL_Joystick * joystick)
  493. {
  494. if (!SDL_PrivateJoystickValid(joystick)) {
  495. return -1;
  496. }
  497. return joystick->instance_id;
  498. }
  499. /*
  500. * Find the SDL_Joystick that owns this instance id
  501. */
  502. SDL_Joystick *
  503. SDL_JoystickFromInstanceID(SDL_JoystickID joyid)
  504. {
  505. SDL_Joystick *joystick;
  506. SDL_LockJoysticks();
  507. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  508. if (joystick->instance_id == joyid) {
  509. break;
  510. }
  511. }
  512. SDL_UnlockJoysticks();
  513. return joystick;
  514. }
  515. /*
  516. * Get the friendly name of this joystick
  517. */
  518. const char *
  519. SDL_JoystickName(SDL_Joystick * joystick)
  520. {
  521. if (!SDL_PrivateJoystickValid(joystick)) {
  522. return NULL;
  523. }
  524. return SDL_FixupJoystickName(joystick->name);
  525. }
  526. int
  527. SDL_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  528. {
  529. if (!SDL_PrivateJoystickValid(joystick)) {
  530. return -1;
  531. }
  532. return joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble, duration_ms);
  533. }
  534. /*
  535. * Close a joystick previously opened with SDL_JoystickOpen()
  536. */
  537. void
  538. SDL_JoystickClose(SDL_Joystick * joystick)
  539. {
  540. SDL_Joystick *joysticklist;
  541. SDL_Joystick *joysticklistprev;
  542. if (!SDL_PrivateJoystickValid(joystick)) {
  543. return;
  544. }
  545. SDL_LockJoysticks();
  546. /* First decrement ref count */
  547. if (--joystick->ref_count > 0) {
  548. SDL_UnlockJoysticks();
  549. return;
  550. }
  551. if (SDL_updating_joystick) {
  552. SDL_UnlockJoysticks();
  553. return;
  554. }
  555. joystick->driver->Close(joystick);
  556. joystick->hwdata = NULL;
  557. joysticklist = SDL_joysticks;
  558. joysticklistprev = NULL;
  559. while (joysticklist) {
  560. if (joystick == joysticklist) {
  561. if (joysticklistprev) {
  562. /* unlink this entry */
  563. joysticklistprev->next = joysticklist->next;
  564. } else {
  565. SDL_joysticks = joystick->next;
  566. }
  567. break;
  568. }
  569. joysticklistprev = joysticklist;
  570. joysticklist = joysticklist->next;
  571. }
  572. SDL_free(joystick->name);
  573. /* Free the data associated with this joystick */
  574. SDL_free(joystick->axes);
  575. SDL_free(joystick->hats);
  576. SDL_free(joystick->balls);
  577. SDL_free(joystick->buttons);
  578. SDL_free(joystick);
  579. SDL_UnlockJoysticks();
  580. }
  581. void
  582. SDL_JoystickQuit(void)
  583. {
  584. int i;
  585. /* Make sure we're not getting called in the middle of updating joysticks */
  586. SDL_assert(!SDL_updating_joystick);
  587. SDL_LockJoysticks();
  588. /* Stop the event polling */
  589. while (SDL_joysticks) {
  590. SDL_joysticks->ref_count = 1;
  591. SDL_JoystickClose(SDL_joysticks);
  592. }
  593. /* Quit the joystick setup */
  594. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  595. SDL_joystick_drivers[i]->Quit();
  596. }
  597. SDL_UnlockJoysticks();
  598. #if !SDL_EVENTS_DISABLED
  599. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  600. #endif
  601. SDL_DelHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  602. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  603. if (SDL_joystick_lock) {
  604. SDL_DestroyMutex(SDL_joystick_lock);
  605. SDL_joystick_lock = NULL;
  606. }
  607. SDL_GameControllerQuitMappings();
  608. }
  609. static SDL_bool
  610. SDL_PrivateJoystickShouldIgnoreEvent()
  611. {
  612. if (SDL_joystick_allows_background_events) {
  613. return SDL_FALSE;
  614. }
  615. if (SDL_HasWindows() && SDL_GetKeyboardFocus() == NULL) {
  616. /* We have windows but we don't have focus, ignore the event. */
  617. return SDL_TRUE;
  618. }
  619. return SDL_FALSE;
  620. }
  621. /* These are global for SDL_sysjoystick.c and SDL_events.c */
  622. void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
  623. {
  624. #if !SDL_EVENTS_DISABLED
  625. SDL_Event event;
  626. int device_index;
  627. device_index = SDL_JoystickGetDeviceIndexFromInstanceID(device_instance);
  628. if (device_index < 0) {
  629. return;
  630. }
  631. event.type = SDL_JOYDEVICEADDED;
  632. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  633. event.jdevice.which = device_index;
  634. SDL_PushEvent(&event);
  635. }
  636. #endif /* !SDL_EVENTS_DISABLED */
  637. }
  638. /*
  639. * If there is an existing add event in the queue, it needs to be modified
  640. * to have the right value for which, because the number of controllers in
  641. * the system is now one less.
  642. */
  643. static void UpdateEventsForDeviceRemoval()
  644. {
  645. int i, num_events;
  646. SDL_Event *events;
  647. SDL_bool isstack;
  648. num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
  649. if (num_events <= 0) {
  650. return;
  651. }
  652. events = SDL_small_alloc(SDL_Event, num_events, &isstack);
  653. if (!events) {
  654. return;
  655. }
  656. num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
  657. for (i = 0; i < num_events; ++i) {
  658. --events[i].jdevice.which;
  659. }
  660. SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
  661. SDL_small_free(events, isstack);
  662. }
  663. void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
  664. {
  665. SDL_Joystick *joystick;
  666. #if !SDL_EVENTS_DISABLED
  667. SDL_Event event;
  668. event.type = SDL_JOYDEVICEREMOVED;
  669. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  670. event.jdevice.which = device_instance;
  671. SDL_PushEvent(&event);
  672. }
  673. UpdateEventsForDeviceRemoval();
  674. #endif /* !SDL_EVENTS_DISABLED */
  675. /* Mark this joystick as no longer attached */
  676. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  677. if (joystick->instance_id == device_instance) {
  678. joystick->attached = SDL_FALSE;
  679. joystick->force_recentering = SDL_TRUE;
  680. break;
  681. }
  682. }
  683. }
  684. int
  685. SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
  686. {
  687. int posted;
  688. /* Make sure we're not getting garbage or duplicate events */
  689. if (axis >= joystick->naxes) {
  690. return 0;
  691. }
  692. if (!joystick->axes[axis].has_initial_value) {
  693. joystick->axes[axis].initial_value = value;
  694. joystick->axes[axis].value = value;
  695. joystick->axes[axis].zero = value;
  696. joystick->axes[axis].has_initial_value = SDL_TRUE;
  697. }
  698. if (value == joystick->axes[axis].value) {
  699. return 0;
  700. }
  701. if (!joystick->axes[axis].sent_initial_value) {
  702. /* Make sure we don't send motion until there's real activity on this axis */
  703. const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */
  704. if (SDL_abs(value - joystick->axes[axis].value) <= MAX_ALLOWED_JITTER) {
  705. return 0;
  706. }
  707. joystick->axes[axis].sent_initial_value = SDL_TRUE;
  708. joystick->axes[axis].value = value; /* Just so we pass the check above */
  709. SDL_PrivateJoystickAxis(joystick, axis, joystick->axes[axis].initial_value);
  710. }
  711. /* We ignore events if we don't have keyboard focus, except for centering
  712. * events.
  713. */
  714. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  715. if ((value > joystick->axes[axis].zero && value >= joystick->axes[axis].value) ||
  716. (value < joystick->axes[axis].zero && value <= joystick->axes[axis].value)) {
  717. return 0;
  718. }
  719. }
  720. /* Update internal joystick state */
  721. joystick->axes[axis].value = value;
  722. /* Post the event, if desired */
  723. posted = 0;
  724. #if !SDL_EVENTS_DISABLED
  725. if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
  726. SDL_Event event;
  727. event.type = SDL_JOYAXISMOTION;
  728. event.jaxis.which = joystick->instance_id;
  729. event.jaxis.axis = axis;
  730. event.jaxis.value = value;
  731. posted = SDL_PushEvent(&event) == 1;
  732. }
  733. #endif /* !SDL_EVENTS_DISABLED */
  734. return posted;
  735. }
  736. int
  737. SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
  738. {
  739. int posted;
  740. /* Make sure we're not getting garbage or duplicate events */
  741. if (hat >= joystick->nhats) {
  742. return 0;
  743. }
  744. if (value == joystick->hats[hat]) {
  745. return 0;
  746. }
  747. /* We ignore events if we don't have keyboard focus, except for centering
  748. * events.
  749. */
  750. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  751. if (value != SDL_HAT_CENTERED) {
  752. return 0;
  753. }
  754. }
  755. /* Update internal joystick state */
  756. joystick->hats[hat] = value;
  757. /* Post the event, if desired */
  758. posted = 0;
  759. #if !SDL_EVENTS_DISABLED
  760. if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
  761. SDL_Event event;
  762. event.jhat.type = SDL_JOYHATMOTION;
  763. event.jhat.which = joystick->instance_id;
  764. event.jhat.hat = hat;
  765. event.jhat.value = value;
  766. posted = SDL_PushEvent(&event) == 1;
  767. }
  768. #endif /* !SDL_EVENTS_DISABLED */
  769. return posted;
  770. }
  771. int
  772. SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
  773. Sint16 xrel, Sint16 yrel)
  774. {
  775. int posted;
  776. /* Make sure we're not getting garbage events */
  777. if (ball >= joystick->nballs) {
  778. return 0;
  779. }
  780. /* We ignore events if we don't have keyboard focus. */
  781. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  782. return 0;
  783. }
  784. /* Update internal mouse state */
  785. joystick->balls[ball].dx += xrel;
  786. joystick->balls[ball].dy += yrel;
  787. /* Post the event, if desired */
  788. posted = 0;
  789. #if !SDL_EVENTS_DISABLED
  790. if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
  791. SDL_Event event;
  792. event.jball.type = SDL_JOYBALLMOTION;
  793. event.jball.which = joystick->instance_id;
  794. event.jball.ball = ball;
  795. event.jball.xrel = xrel;
  796. event.jball.yrel = yrel;
  797. posted = SDL_PushEvent(&event) == 1;
  798. }
  799. #endif /* !SDL_EVENTS_DISABLED */
  800. return posted;
  801. }
  802. int
  803. SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
  804. {
  805. int posted;
  806. #if !SDL_EVENTS_DISABLED
  807. SDL_Event event;
  808. switch (state) {
  809. case SDL_PRESSED:
  810. event.type = SDL_JOYBUTTONDOWN;
  811. break;
  812. case SDL_RELEASED:
  813. event.type = SDL_JOYBUTTONUP;
  814. break;
  815. default:
  816. /* Invalid state -- bail */
  817. return 0;
  818. }
  819. #endif /* !SDL_EVENTS_DISABLED */
  820. /* Make sure we're not getting garbage or duplicate events */
  821. if (button >= joystick->nbuttons) {
  822. return 0;
  823. }
  824. if (state == joystick->buttons[button]) {
  825. return 0;
  826. }
  827. /* We ignore events if we don't have keyboard focus, except for button
  828. * release. */
  829. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  830. if (state == SDL_PRESSED) {
  831. return 0;
  832. }
  833. }
  834. /* Update internal joystick state */
  835. joystick->buttons[button] = state;
  836. /* Post the event, if desired */
  837. posted = 0;
  838. #if !SDL_EVENTS_DISABLED
  839. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  840. event.jbutton.which = joystick->instance_id;
  841. event.jbutton.button = button;
  842. event.jbutton.state = state;
  843. posted = SDL_PushEvent(&event) == 1;
  844. }
  845. #endif /* !SDL_EVENTS_DISABLED */
  846. return posted;
  847. }
  848. void
  849. SDL_JoystickUpdate(void)
  850. {
  851. int i;
  852. SDL_Joystick *joystick;
  853. SDL_LockJoysticks();
  854. if (SDL_updating_joystick) {
  855. /* The joysticks are already being updated */
  856. SDL_UnlockJoysticks();
  857. return;
  858. }
  859. SDL_updating_joystick = SDL_TRUE;
  860. /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
  861. SDL_UnlockJoysticks();
  862. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  863. if (joystick->attached) {
  864. joystick->driver->Update(joystick);
  865. if (joystick->delayed_guide_button) {
  866. SDL_GameControllerHandleDelayedGuideButton(joystick);
  867. }
  868. }
  869. if (joystick->force_recentering) {
  870. /* Tell the app that everything is centered/unpressed... */
  871. for (i = 0; i < joystick->naxes; i++) {
  872. if (joystick->axes[i].has_initial_value) {
  873. SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
  874. }
  875. }
  876. for (i = 0; i < joystick->nbuttons; i++) {
  877. SDL_PrivateJoystickButton(joystick, i, 0);
  878. }
  879. for (i = 0; i < joystick->nhats; i++) {
  880. SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
  881. }
  882. joystick->force_recentering = SDL_FALSE;
  883. }
  884. }
  885. SDL_LockJoysticks();
  886. SDL_updating_joystick = SDL_FALSE;
  887. /* If any joysticks were closed while updating, free them here */
  888. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  889. if (joystick->ref_count <= 0) {
  890. SDL_JoystickClose(joystick);
  891. }
  892. }
  893. /* this needs to happen AFTER walking the joystick list above, so that any
  894. dangling hardware data from removed devices can be free'd
  895. */
  896. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  897. SDL_joystick_drivers[i]->Detect();
  898. }
  899. SDL_UnlockJoysticks();
  900. }
  901. int
  902. SDL_JoystickEventState(int state)
  903. {
  904. #if SDL_EVENTS_DISABLED
  905. return SDL_DISABLE;
  906. #else
  907. const Uint32 event_list[] = {
  908. SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
  909. SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
  910. };
  911. unsigned int i;
  912. switch (state) {
  913. case SDL_QUERY:
  914. state = SDL_DISABLE;
  915. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  916. state = SDL_EventState(event_list[i], SDL_QUERY);
  917. if (state == SDL_ENABLE) {
  918. break;
  919. }
  920. }
  921. break;
  922. default:
  923. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  924. SDL_EventState(event_list[i], state);
  925. }
  926. break;
  927. }
  928. return state;
  929. #endif /* SDL_EVENTS_DISABLED */
  930. }
  931. void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
  932. {
  933. Uint16 *guid16 = (Uint16 *)guid.data;
  934. /* If the GUID fits the form of BUS 0000 VENDOR 0000 PRODUCT 0000, return the data */
  935. if (/* guid16[0] is device bus type */
  936. guid16[1] == 0x0000 &&
  937. /* guid16[2] is vendor ID */
  938. guid16[3] == 0x0000 &&
  939. /* guid16[4] is product ID */
  940. guid16[5] == 0x0000
  941. /* guid16[6] is product version */
  942. ) {
  943. if (vendor) {
  944. *vendor = guid16[2];
  945. }
  946. if (product) {
  947. *product = guid16[4];
  948. }
  949. if (version) {
  950. *version = guid16[6];
  951. }
  952. } else {
  953. if (vendor) {
  954. *vendor = 0;
  955. }
  956. if (product) {
  957. *product = 0;
  958. }
  959. if (version) {
  960. *version = 0;
  961. }
  962. }
  963. }
  964. SDL_bool
  965. SDL_IsJoystickPS4(Uint16 vendor, Uint16 product)
  966. {
  967. return (GuessControllerType(vendor, product) == k_eControllerType_PS4Controller);
  968. }
  969. SDL_bool
  970. SDL_IsJoystickNintendoSwitchPro(Uint16 vendor, Uint16 product)
  971. {
  972. return (GuessControllerType(vendor, product) == k_eControllerType_SwitchProController);
  973. }
  974. SDL_bool
  975. SDL_IsJoystickSteamController(Uint16 vendor, Uint16 product)
  976. {
  977. return BIsSteamController(GuessControllerType(vendor, product)) ? SDL_TRUE : SDL_FALSE;
  978. }
  979. SDL_bool
  980. SDL_IsJoystickXbox360(Uint16 vendor, Uint16 product)
  981. {
  982. /* Filter out some bogus values here */
  983. if (vendor == 0x0000 && product == 0x0000) {
  984. return SDL_FALSE;
  985. }
  986. if (vendor == 0x0001 && product == 0x0001) {
  987. return SDL_FALSE;
  988. }
  989. return (GuessControllerType(vendor, product) == k_eControllerType_XBox360Controller);
  990. }
  991. SDL_bool
  992. SDL_IsJoystickXboxOne(Uint16 vendor, Uint16 product)
  993. {
  994. return (GuessControllerType(vendor, product) == k_eControllerType_XBoxOneController);
  995. }
  996. static SDL_bool SDL_IsJoystickProductWheel(Uint32 vidpid)
  997. {
  998. static Uint32 wheel_joysticks[] = {
  999. MAKE_VIDPID(0x046d, 0xc294), /* Logitech generic wheel */
  1000. MAKE_VIDPID(0x046d, 0xc295), /* Logitech Momo Force */
  1001. MAKE_VIDPID(0x046d, 0xc298), /* Logitech Driving Force Pro */
  1002. MAKE_VIDPID(0x046d, 0xc299), /* Logitech G25 */
  1003. MAKE_VIDPID(0x046d, 0xc29a), /* Logitech Driving Force GT */
  1004. MAKE_VIDPID(0x046d, 0xc29b), /* Logitech G27 */
  1005. MAKE_VIDPID(0x046d, 0xc261), /* Logitech G920 (initial mode) */
  1006. MAKE_VIDPID(0x046d, 0xc262), /* Logitech G920 (active mode) */
  1007. MAKE_VIDPID(0x044f, 0xb65d), /* Thrustmaster Wheel FFB */
  1008. MAKE_VIDPID(0x044f, 0xb66d), /* Thrustmaster Wheel FFB */
  1009. MAKE_VIDPID(0x044f, 0xb677), /* Thrustmaster T150 */
  1010. MAKE_VIDPID(0x044f, 0xb664), /* Thrustmaster TX (initial mode) */
  1011. MAKE_VIDPID(0x044f, 0xb669), /* Thrustmaster TX (active mode) */
  1012. };
  1013. int i;
  1014. for (i = 0; i < SDL_arraysize(wheel_joysticks); ++i) {
  1015. if (vidpid == wheel_joysticks[i]) {
  1016. return SDL_TRUE;
  1017. }
  1018. }
  1019. return SDL_FALSE;
  1020. }
  1021. static SDL_bool SDL_IsJoystickProductFlightStick(Uint32 vidpid)
  1022. {
  1023. static Uint32 flightstick_joysticks[] = {
  1024. MAKE_VIDPID(0x044f, 0x0402), /* HOTAS Warthog Joystick */
  1025. MAKE_VIDPID(0x0738, 0x2221), /* Saitek Pro Flight X-56 Rhino Stick */
  1026. };
  1027. int i;
  1028. for (i = 0; i < SDL_arraysize(flightstick_joysticks); ++i) {
  1029. if (vidpid == flightstick_joysticks[i]) {
  1030. return SDL_TRUE;
  1031. }
  1032. }
  1033. return SDL_FALSE;
  1034. }
  1035. static SDL_bool SDL_IsJoystickProductThrottle(Uint32 vidpid)
  1036. {
  1037. static Uint32 throttle_joysticks[] = {
  1038. MAKE_VIDPID(0x044f, 0x0404), /* HOTAS Warthog Throttle */
  1039. MAKE_VIDPID(0x0738, 0xa221), /* Saitek Pro Flight X-56 Rhino Throttle */
  1040. };
  1041. int i;
  1042. for (i = 0; i < SDL_arraysize(throttle_joysticks); ++i) {
  1043. if (vidpid == throttle_joysticks[i]) {
  1044. return SDL_TRUE;
  1045. }
  1046. }
  1047. return SDL_FALSE;
  1048. }
  1049. static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
  1050. {
  1051. Uint16 vendor;
  1052. Uint16 product;
  1053. Uint32 vidpid;
  1054. if (guid.data[14] == 'x') {
  1055. /* XInput GUID, get the type based on the XInput device subtype */
  1056. switch (guid.data[15]) {
  1057. case 0x01: /* XINPUT_DEVSUBTYPE_GAMEPAD */
  1058. return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1059. case 0x02: /* XINPUT_DEVSUBTYPE_WHEEL */
  1060. return SDL_JOYSTICK_TYPE_WHEEL;
  1061. case 0x03: /* XINPUT_DEVSUBTYPE_ARCADE_STICK */
  1062. return SDL_JOYSTICK_TYPE_ARCADE_STICK;
  1063. case 0x04: /* XINPUT_DEVSUBTYPE_FLIGHT_STICK */
  1064. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  1065. case 0x05: /* XINPUT_DEVSUBTYPE_DANCE_PAD */
  1066. return SDL_JOYSTICK_TYPE_DANCE_PAD;
  1067. case 0x06: /* XINPUT_DEVSUBTYPE_GUITAR */
  1068. case 0x07: /* XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE */
  1069. case 0x0B: /* XINPUT_DEVSUBTYPE_GUITAR_BASS */
  1070. return SDL_JOYSTICK_TYPE_GUITAR;
  1071. case 0x08: /* XINPUT_DEVSUBTYPE_DRUM_KIT */
  1072. return SDL_JOYSTICK_TYPE_DRUM_KIT;
  1073. case 0x13: /* XINPUT_DEVSUBTYPE_ARCADE_PAD */
  1074. return SDL_JOYSTICK_TYPE_ARCADE_PAD;
  1075. default:
  1076. return SDL_JOYSTICK_TYPE_UNKNOWN;
  1077. }
  1078. }
  1079. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  1080. vidpid = MAKE_VIDPID(vendor, product);
  1081. if (SDL_IsJoystickProductWheel(vidpid)) {
  1082. return SDL_JOYSTICK_TYPE_WHEEL;
  1083. }
  1084. if (SDL_IsJoystickProductFlightStick(vidpid)) {
  1085. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  1086. }
  1087. if (SDL_IsJoystickProductThrottle(vidpid)) {
  1088. return SDL_JOYSTICK_TYPE_THROTTLE;
  1089. }
  1090. if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) {
  1091. return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1092. }
  1093. return SDL_JOYSTICK_TYPE_UNKNOWN;
  1094. }
  1095. static SDL_bool SDL_IsPS4RemapperRunning(void)
  1096. {
  1097. #ifdef __WIN32__
  1098. const char *mapper_processes[] = {
  1099. "DS4Windows.exe",
  1100. "InputMapper.exe",
  1101. };
  1102. int i;
  1103. PROCESSENTRY32 pe32;
  1104. SDL_bool found = SDL_FALSE;
  1105. /* Take a snapshot of all processes in the system */
  1106. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  1107. if (hProcessSnap != INVALID_HANDLE_VALUE) {
  1108. pe32.dwSize = sizeof(PROCESSENTRY32);
  1109. if (Process32First(hProcessSnap, &pe32)) {
  1110. do
  1111. {
  1112. for (i = 0; i < SDL_arraysize(mapper_processes); ++i) {
  1113. if (SDL_strcasecmp(pe32.szExeFile, mapper_processes[i]) == 0) {
  1114. found = SDL_TRUE;
  1115. }
  1116. }
  1117. } while (Process32Next(hProcessSnap, &pe32) && !found);
  1118. }
  1119. CloseHandle(hProcessSnap);
  1120. }
  1121. return found;
  1122. #else
  1123. return SDL_FALSE;
  1124. #endif
  1125. }
  1126. SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid)
  1127. {
  1128. Uint16 vendor;
  1129. Uint16 product;
  1130. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  1131. if (SDL_IsJoystickPS4(vendor, product) && SDL_IsPS4RemapperRunning()) {
  1132. return SDL_TRUE;
  1133. }
  1134. if (SDL_IsGameControllerNameAndGUID(name, guid) &&
  1135. SDL_ShouldIgnoreGameController(name, guid)) {
  1136. return SDL_TRUE;
  1137. }
  1138. return SDL_FALSE;
  1139. }
  1140. /* return the guid for this index */
  1141. SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
  1142. {
  1143. SDL_JoystickDriver *driver;
  1144. SDL_JoystickGUID guid;
  1145. SDL_LockJoysticks();
  1146. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  1147. guid = driver->GetDeviceGUID(device_index);
  1148. } else {
  1149. SDL_zero(guid);
  1150. }
  1151. SDL_UnlockJoysticks();
  1152. return guid;
  1153. }
  1154. Uint16 SDL_JoystickGetDeviceVendor(int device_index)
  1155. {
  1156. Uint16 vendor;
  1157. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1158. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
  1159. return vendor;
  1160. }
  1161. Uint16 SDL_JoystickGetDeviceProduct(int device_index)
  1162. {
  1163. Uint16 product;
  1164. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1165. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
  1166. return product;
  1167. }
  1168. Uint16 SDL_JoystickGetDeviceProductVersion(int device_index)
  1169. {
  1170. Uint16 version;
  1171. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1172. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
  1173. return version;
  1174. }
  1175. SDL_JoystickType SDL_JoystickGetDeviceType(int device_index)
  1176. {
  1177. SDL_JoystickType type;
  1178. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  1179. type = SDL_GetJoystickGUIDType(guid);
  1180. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  1181. if (SDL_IsGameController(device_index)) {
  1182. type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1183. }
  1184. }
  1185. return type;
  1186. }
  1187. SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
  1188. {
  1189. SDL_JoystickDriver *driver;
  1190. SDL_JoystickID instance_id = -1;
  1191. SDL_LockJoysticks();
  1192. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  1193. instance_id = driver->GetDeviceInstanceID(device_index);
  1194. }
  1195. SDL_UnlockJoysticks();
  1196. return instance_id;
  1197. }
  1198. int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id)
  1199. {
  1200. int i, num_joysticks, device_index = -1;
  1201. SDL_LockJoysticks();
  1202. num_joysticks = SDL_NumJoysticks();
  1203. for (i = 0; i < num_joysticks; ++i) {
  1204. if (SDL_JoystickGetDeviceInstanceID(i) == instance_id) {
  1205. device_index = i;
  1206. break;
  1207. }
  1208. }
  1209. SDL_UnlockJoysticks();
  1210. return device_index;
  1211. }
  1212. SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
  1213. {
  1214. if (!SDL_PrivateJoystickValid(joystick)) {
  1215. SDL_JoystickGUID emptyGUID;
  1216. SDL_zero(emptyGUID);
  1217. return emptyGUID;
  1218. }
  1219. return joystick->guid;
  1220. }
  1221. Uint16 SDL_JoystickGetVendor(SDL_Joystick * joystick)
  1222. {
  1223. Uint16 vendor;
  1224. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1225. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
  1226. return vendor;
  1227. }
  1228. Uint16 SDL_JoystickGetProduct(SDL_Joystick * joystick)
  1229. {
  1230. Uint16 product;
  1231. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1232. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
  1233. return product;
  1234. }
  1235. Uint16 SDL_JoystickGetProductVersion(SDL_Joystick * joystick)
  1236. {
  1237. Uint16 version;
  1238. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1239. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
  1240. return version;
  1241. }
  1242. SDL_JoystickType SDL_JoystickGetType(SDL_Joystick * joystick)
  1243. {
  1244. SDL_JoystickType type;
  1245. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  1246. type = SDL_GetJoystickGUIDType(guid);
  1247. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  1248. if (joystick && joystick->is_game_controller) {
  1249. type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1250. }
  1251. }
  1252. return type;
  1253. }
  1254. /* convert the guid to a printable string */
  1255. void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
  1256. {
  1257. static const char k_rgchHexToASCII[] = "0123456789abcdef";
  1258. int i;
  1259. if ((pszGUID == NULL) || (cbGUID <= 0)) {
  1260. return;
  1261. }
  1262. for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
  1263. /* each input byte writes 2 ascii chars, and might write a null byte. */
  1264. /* If we don't have room for next input byte, stop */
  1265. unsigned char c = guid.data[i];
  1266. *pszGUID++ = k_rgchHexToASCII[c >> 4];
  1267. *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
  1268. }
  1269. *pszGUID = '\0';
  1270. }
  1271. /*-----------------------------------------------------------------------------
  1272. * Purpose: Returns the 4 bit nibble for a hex character
  1273. * Input : c -
  1274. * Output : unsigned char
  1275. *-----------------------------------------------------------------------------*/
  1276. static unsigned char nibble(char c)
  1277. {
  1278. if ((c >= '0') && (c <= '9')) {
  1279. return (unsigned char)(c - '0');
  1280. }
  1281. if ((c >= 'A') && (c <= 'F')) {
  1282. return (unsigned char)(c - 'A' + 0x0a);
  1283. }
  1284. if ((c >= 'a') && (c <= 'f')) {
  1285. return (unsigned char)(c - 'a' + 0x0a);
  1286. }
  1287. /* received an invalid character, and no real way to return an error */
  1288. /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
  1289. return 0;
  1290. }
  1291. /* convert the string version of a joystick guid to the struct */
  1292. SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
  1293. {
  1294. SDL_JoystickGUID guid;
  1295. int maxoutputbytes= sizeof(guid);
  1296. size_t len = SDL_strlen(pchGUID);
  1297. Uint8 *p;
  1298. size_t i;
  1299. /* Make sure it's even */
  1300. len = (len) & ~0x1;
  1301. SDL_memset(&guid, 0x00, sizeof(guid));
  1302. p = (Uint8 *)&guid;
  1303. for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
  1304. *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
  1305. }
  1306. return guid;
  1307. }
  1308. /* update the power level for this joystick */
  1309. void SDL_PrivateJoystickBatteryLevel(SDL_Joystick * joystick, SDL_JoystickPowerLevel ePowerLevel)
  1310. {
  1311. joystick->epowerlevel = ePowerLevel;
  1312. }
  1313. /* return its power level */
  1314. SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick)
  1315. {
  1316. if (!SDL_PrivateJoystickValid(joystick)) {
  1317. return SDL_JOYSTICK_POWER_UNKNOWN;
  1318. }
  1319. return joystick->epowerlevel;
  1320. }
  1321. /* vi: set ts=4 sw=4 expandtab: */