SDL_joystick.c 64 KB

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