SDL_joystick.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_hints.h"
  25. #if !SDL_EVENTS_DISABLED
  26. #include "../events/SDL_events_c.h"
  27. #endif
  28. #include "../video/SDL_sysvideo.h"
  29. #include "hidapi/SDL_hidapijoystick_c.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. #if SDL_JOYSTICK_VIRTUAL
  39. #include "./virtual/SDL_virtualjoystick_c.h"
  40. #endif
  41. static SDL_JoystickDriver *SDL_joystick_drivers[] = {
  42. #ifdef SDL_JOYSTICK_HIDAPI /* Before WINDOWS_ driver, as WINDOWS wants to check if this driver is handling things */
  43. &SDL_HIDAPI_JoystickDriver,
  44. #endif
  45. #ifdef SDL_JOYSTICK_RAWINPUT /* Before WINDOWS_ driver, as WINDOWS wants to check if this driver is handling things */
  46. &SDL_RAWINPUT_JoystickDriver,
  47. #endif
  48. #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT) /* Before WGI driver, as WGI wants to check if this driver is handling things */
  49. &SDL_WINDOWS_JoystickDriver,
  50. #endif
  51. #if defined(SDL_JOYSTICK_WGI)
  52. &SDL_WGI_JoystickDriver,
  53. #endif
  54. #if defined(SDL_JOYSTICK_WINMM)
  55. &SDL_WINMM_JoystickDriver,
  56. #endif
  57. #ifdef SDL_JOYSTICK_LINUX
  58. &SDL_LINUX_JoystickDriver,
  59. #endif
  60. #ifdef SDL_JOYSTICK_IOKIT
  61. &SDL_DARWIN_JoystickDriver,
  62. #endif
  63. #if (defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__TVOS__)) && !defined(SDL_JOYSTICK_DISABLED)
  64. &SDL_IOS_JoystickDriver,
  65. #endif
  66. #ifdef SDL_JOYSTICK_ANDROID
  67. &SDL_ANDROID_JoystickDriver,
  68. #endif
  69. #ifdef SDL_JOYSTICK_EMSCRIPTEN
  70. &SDL_EMSCRIPTEN_JoystickDriver,
  71. #endif
  72. #ifdef SDL_JOYSTICK_HAIKU
  73. &SDL_HAIKU_JoystickDriver,
  74. #endif
  75. #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. */
  76. &SDL_BSD_JoystickDriver,
  77. #endif
  78. #ifdef SDL_JOYSTICK_OS2
  79. &SDL_OS2_JoystickDriver,
  80. #endif
  81. #ifdef SDL_JOYSTICK_PSP
  82. &SDL_PSP_JoystickDriver,
  83. #endif
  84. #ifdef SDL_JOYSTICK_VIRTUAL
  85. &SDL_VIRTUAL_JoystickDriver,
  86. #endif
  87. #ifdef SDL_JOYSTICK_VITA
  88. &SDL_VITA_JoystickDriver,
  89. #endif
  90. #if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED)
  91. &SDL_DUMMY_JoystickDriver
  92. #endif
  93. };
  94. static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
  95. static SDL_Joystick *SDL_joysticks = NULL;
  96. static SDL_bool SDL_updating_joystick = SDL_FALSE;
  97. static SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
  98. static SDL_atomic_t SDL_next_joystick_instance_id;
  99. static int SDL_joystick_player_count = 0;
  100. static SDL_JoystickID *SDL_joystick_players = NULL;
  101. void
  102. SDL_LockJoysticks(void)
  103. {
  104. if (SDL_joystick_lock) {
  105. SDL_LockMutex(SDL_joystick_lock);
  106. }
  107. }
  108. void
  109. SDL_UnlockJoysticks(void)
  110. {
  111. if (SDL_joystick_lock) {
  112. SDL_UnlockMutex(SDL_joystick_lock);
  113. }
  114. }
  115. static int
  116. SDL_FindFreePlayerIndex()
  117. {
  118. int player_index;
  119. for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {
  120. if (SDL_joystick_players[player_index] == -1) {
  121. return player_index;
  122. }
  123. }
  124. return player_index;
  125. }
  126. static int
  127. SDL_GetPlayerIndexForJoystickID(SDL_JoystickID instance_id)
  128. {
  129. int player_index;
  130. for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {
  131. if (instance_id == SDL_joystick_players[player_index]) {
  132. break;
  133. }
  134. }
  135. if (player_index == SDL_joystick_player_count) {
  136. player_index = -1;
  137. }
  138. return player_index;
  139. }
  140. static SDL_JoystickID
  141. SDL_GetJoystickIDForPlayerIndex(int player_index)
  142. {
  143. if (player_index < 0 || player_index >= SDL_joystick_player_count) {
  144. return -1;
  145. }
  146. return SDL_joystick_players[player_index];
  147. }
  148. static SDL_bool
  149. SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id)
  150. {
  151. SDL_JoystickID existing_instance = SDL_GetJoystickIDForPlayerIndex(player_index);
  152. SDL_JoystickDriver *driver;
  153. int device_index;
  154. int existing_player_index;
  155. if (player_index >= SDL_joystick_player_count) {
  156. SDL_JoystickID *new_players = (SDL_JoystickID *)SDL_realloc(SDL_joystick_players, (player_index + 1)*sizeof(*SDL_joystick_players));
  157. if (!new_players) {
  158. SDL_OutOfMemory();
  159. return SDL_FALSE;
  160. }
  161. SDL_joystick_players = new_players;
  162. SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0xFF, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0]));
  163. SDL_joystick_player_count = player_index + 1;
  164. } else if (SDL_joystick_players[player_index] == instance_id) {
  165. /* Joystick is already assigned the requested player index */
  166. return SDL_TRUE;
  167. }
  168. /* Clear the old player index */
  169. existing_player_index = SDL_GetPlayerIndexForJoystickID(instance_id);
  170. if (existing_player_index >= 0) {
  171. SDL_joystick_players[existing_player_index] = -1;
  172. }
  173. if (player_index >= 0) {
  174. SDL_joystick_players[player_index] = instance_id;
  175. }
  176. /* Update the driver with the new index */
  177. device_index = SDL_JoystickGetDeviceIndexFromInstanceID(instance_id);
  178. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  179. driver->SetDevicePlayerIndex(device_index, player_index);
  180. }
  181. /* Move any existing joystick to another slot */
  182. if (existing_instance >= 0) {
  183. SDL_SetJoystickIDForPlayerIndex(SDL_FindFreePlayerIndex(), existing_instance);
  184. }
  185. return SDL_TRUE;
  186. }
  187. static void SDLCALL
  188. SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  189. {
  190. if (hint && *hint == '1') {
  191. SDL_joystick_allows_background_events = SDL_TRUE;
  192. } else {
  193. SDL_joystick_allows_background_events = SDL_FALSE;
  194. }
  195. }
  196. int
  197. SDL_JoystickInit(void)
  198. {
  199. int i, status;
  200. SDL_GameControllerInitMappings();
  201. /* Create the joystick list lock */
  202. if (!SDL_joystick_lock) {
  203. SDL_joystick_lock = SDL_CreateMutex();
  204. }
  205. /* See if we should allow joystick events while in the background */
  206. SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  207. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  208. #if !SDL_EVENTS_DISABLED
  209. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  210. return -1;
  211. }
  212. #endif /* !SDL_EVENTS_DISABLED */
  213. status = -1;
  214. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  215. if (SDL_joystick_drivers[i]->Init() >= 0) {
  216. status = 0;
  217. }
  218. }
  219. return status;
  220. }
  221. /*
  222. * Count the number of joysticks attached to the system
  223. */
  224. int
  225. SDL_NumJoysticks(void)
  226. {
  227. int i, total_joysticks = 0;
  228. SDL_LockJoysticks();
  229. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  230. total_joysticks += SDL_joystick_drivers[i]->GetCount();
  231. }
  232. SDL_UnlockJoysticks();
  233. return total_joysticks;
  234. }
  235. /*
  236. * Return the next available joystick instance ID
  237. * This may be called by drivers from multiple threads, unprotected by any locks
  238. */
  239. SDL_JoystickID SDL_GetNextJoystickInstanceID()
  240. {
  241. return SDL_AtomicIncRef(&SDL_next_joystick_instance_id);
  242. }
  243. /*
  244. * Get the driver and device index for an API device index
  245. * This should be called while the joystick lock is held, to prevent another thread from updating the list
  246. */
  247. SDL_bool
  248. SDL_GetDriverAndJoystickIndex(int device_index, SDL_JoystickDriver **driver, int *driver_index)
  249. {
  250. int i, num_joysticks, total_joysticks = 0;
  251. if (device_index >= 0) {
  252. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  253. num_joysticks = SDL_joystick_drivers[i]->GetCount();
  254. if (device_index < num_joysticks) {
  255. *driver = SDL_joystick_drivers[i];
  256. *driver_index = device_index;
  257. return SDL_TRUE;
  258. }
  259. device_index -= num_joysticks;
  260. total_joysticks += num_joysticks;
  261. }
  262. }
  263. SDL_SetError("There are %d joysticks available", total_joysticks);
  264. return SDL_FALSE;
  265. }
  266. /*
  267. * Get the implementation dependent name of a joystick
  268. */
  269. const char *
  270. SDL_JoystickNameForIndex(int device_index)
  271. {
  272. SDL_JoystickDriver *driver;
  273. const char *name = NULL;
  274. SDL_LockJoysticks();
  275. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  276. name = driver->GetDeviceName(device_index);
  277. }
  278. SDL_UnlockJoysticks();
  279. /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
  280. return name;
  281. }
  282. /*
  283. * Get the implementation dependent path of a joystick
  284. */
  285. const char *
  286. SDL_JoystickPathForIndex(int device_index)
  287. {
  288. SDL_JoystickDriver *driver;
  289. const char *path = NULL;
  290. SDL_LockJoysticks();
  291. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  292. path = driver->GetDevicePath(device_index);
  293. }
  294. SDL_UnlockJoysticks();
  295. /* FIXME: Really we should reference count this path so it doesn't go away after unlock */
  296. if (!path) {
  297. SDL_Unsupported();
  298. }
  299. return path;
  300. }
  301. /*
  302. * Get the player index of a joystick, or -1 if it's not available
  303. */
  304. int
  305. SDL_JoystickGetDevicePlayerIndex(int device_index)
  306. {
  307. int player_index;
  308. SDL_LockJoysticks();
  309. player_index = SDL_GetPlayerIndexForJoystickID(SDL_JoystickGetDeviceInstanceID(device_index));
  310. SDL_UnlockJoysticks();
  311. return player_index;
  312. }
  313. /*
  314. * Return true if this joystick is known to have all axes centered at zero
  315. * This isn't generally needed unless the joystick never generates an initial axis value near zero,
  316. * e.g. it's emulating axes with digital buttons
  317. */
  318. static SDL_bool
  319. SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
  320. {
  321. #ifdef __WINRT__
  322. return SDL_TRUE;
  323. #else
  324. static Uint32 zero_centered_joysticks[] = {
  325. MAKE_VIDPID(0x0e8f, 0x3013), /* HuiJia SNES USB adapter */
  326. MAKE_VIDPID(0x05a0, 0x3232), /* 8Bitdo Zero Gamepad */
  327. };
  328. int i;
  329. Uint32 id = MAKE_VIDPID(SDL_JoystickGetVendor(joystick),
  330. SDL_JoystickGetProduct(joystick));
  331. /*printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);*/
  332. if (joystick->naxes == 2) {
  333. /* Assume D-pad or thumbstick style axes are centered at 0 */
  334. return SDL_TRUE;
  335. }
  336. for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
  337. if (id == zero_centered_joysticks[i]) {
  338. return SDL_TRUE;
  339. }
  340. }
  341. return SDL_FALSE;
  342. #endif /* __WINRT__ */
  343. }
  344. /*
  345. * Open a joystick for use - the index passed as an argument refers to
  346. * the N'th joystick on the system. This index is the value which will
  347. * identify this joystick in future joystick events.
  348. *
  349. * This function returns a joystick identifier, or NULL if an error occurred.
  350. */
  351. SDL_Joystick *
  352. SDL_JoystickOpen(int device_index)
  353. {
  354. SDL_JoystickDriver *driver;
  355. SDL_JoystickID instance_id;
  356. SDL_Joystick *joystick;
  357. SDL_Joystick *joysticklist;
  358. const char *joystickname = NULL;
  359. const char *joystickpath = NULL;
  360. SDL_JoystickPowerLevel initial_power_level;
  361. SDL_LockJoysticks();
  362. if (!SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  363. SDL_UnlockJoysticks();
  364. return NULL;
  365. }
  366. joysticklist = SDL_joysticks;
  367. /* If the joystick is already open, return it
  368. * it is important that we have a single joystick * for each instance id
  369. */
  370. instance_id = driver->GetDeviceInstanceID(device_index);
  371. while (joysticklist) {
  372. if (instance_id == joysticklist->instance_id) {
  373. joystick = joysticklist;
  374. ++joystick->ref_count;
  375. SDL_UnlockJoysticks();
  376. return joystick;
  377. }
  378. joysticklist = joysticklist->next;
  379. }
  380. /* Create and initialize the joystick */
  381. joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
  382. if (joystick == NULL) {
  383. SDL_OutOfMemory();
  384. SDL_UnlockJoysticks();
  385. return NULL;
  386. }
  387. joystick->driver = driver;
  388. joystick->instance_id = instance_id;
  389. joystick->attached = SDL_TRUE;
  390. joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
  391. joystick->led_expiration = SDL_GetTicks();
  392. if (driver->Open(joystick, device_index) < 0) {
  393. SDL_free(joystick);
  394. SDL_UnlockJoysticks();
  395. return NULL;
  396. }
  397. joystickname = driver->GetDeviceName(device_index);
  398. if (joystickname) {
  399. joystick->name = SDL_strdup(joystickname);
  400. } else {
  401. joystick->name = NULL;
  402. }
  403. joystickpath = driver->GetDevicePath(device_index);
  404. if (joystickpath) {
  405. joystick->path = SDL_strdup(joystickpath);
  406. } else {
  407. joystick->path = NULL;
  408. }
  409. joystick->guid = driver->GetDeviceGUID(device_index);
  410. if (joystick->naxes > 0) {
  411. joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo));
  412. }
  413. if (joystick->nhats > 0) {
  414. joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8));
  415. }
  416. if (joystick->nballs > 0) {
  417. joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls));
  418. }
  419. if (joystick->nbuttons > 0) {
  420. joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8));
  421. }
  422. if (((joystick->naxes > 0) && !joystick->axes)
  423. || ((joystick->nhats > 0) && !joystick->hats)
  424. || ((joystick->nballs > 0) && !joystick->balls)
  425. || ((joystick->nbuttons > 0) && !joystick->buttons)) {
  426. SDL_OutOfMemory();
  427. SDL_JoystickClose(joystick);
  428. SDL_UnlockJoysticks();
  429. return NULL;
  430. }
  431. /* If this joystick is known to have all zero centered axes, skip the auto-centering code */
  432. if (SDL_JoystickAxesCenteredAtZero(joystick)) {
  433. int i;
  434. for (i = 0; i < joystick->naxes; ++i) {
  435. joystick->axes[i].has_initial_value = SDL_TRUE;
  436. }
  437. }
  438. joystick->is_game_controller = SDL_IsGameController(device_index);
  439. /* Add joystick to list */
  440. ++joystick->ref_count;
  441. /* Link the joystick in the list */
  442. joystick->next = SDL_joysticks;
  443. SDL_joysticks = joystick;
  444. SDL_UnlockJoysticks();
  445. /* send initial battery event */
  446. initial_power_level = joystick->epowerlevel;
  447. joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
  448. SDL_PrivateJoystickBatteryLevel(joystick, initial_power_level);
  449. driver->Update(joystick);
  450. return joystick;
  451. }
  452. int
  453. SDL_JoystickAttachVirtual(SDL_JoystickType type,
  454. int naxes, int nbuttons, int nhats)
  455. {
  456. SDL_VirtualJoystickDesc desc;
  457. SDL_zero(desc);
  458. desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
  459. desc.type = (Uint16)type;
  460. desc.naxes = (Uint16)naxes;
  461. desc.nbuttons = (Uint16)nbuttons;
  462. desc.nhats = (Uint16)nhats;
  463. return SDL_JoystickAttachVirtualEx(&desc);
  464. }
  465. int
  466. SDL_JoystickAttachVirtualEx(const SDL_VirtualJoystickDesc *desc)
  467. {
  468. #if SDL_JOYSTICK_VIRTUAL
  469. return SDL_JoystickAttachVirtualInner(desc);
  470. #else
  471. return SDL_SetError("SDL not built with virtual-joystick support");
  472. #endif
  473. }
  474. int
  475. SDL_JoystickDetachVirtual(int device_index)
  476. {
  477. #if SDL_JOYSTICK_VIRTUAL
  478. SDL_JoystickDriver *driver;
  479. SDL_LockJoysticks();
  480. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  481. if (driver == &SDL_VIRTUAL_JoystickDriver) {
  482. const int result = SDL_JoystickDetachVirtualInner(device_index);
  483. SDL_UnlockJoysticks();
  484. return result;
  485. }
  486. }
  487. SDL_UnlockJoysticks();
  488. return SDL_SetError("Virtual joystick not found at provided index");
  489. #else
  490. return SDL_SetError("SDL not built with virtual-joystick support");
  491. #endif
  492. }
  493. SDL_bool
  494. SDL_JoystickIsVirtual(int device_index)
  495. {
  496. #if SDL_JOYSTICK_VIRTUAL
  497. SDL_JoystickDriver *driver;
  498. int driver_device_index;
  499. SDL_bool is_virtual = SDL_FALSE;
  500. SDL_LockJoysticks();
  501. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) {
  502. if (driver == &SDL_VIRTUAL_JoystickDriver) {
  503. is_virtual = SDL_TRUE;
  504. }
  505. }
  506. SDL_UnlockJoysticks();
  507. return is_virtual;
  508. #else
  509. return SDL_FALSE;
  510. #endif
  511. }
  512. int
  513. SDL_JoystickSetVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value)
  514. {
  515. #if SDL_JOYSTICK_VIRTUAL
  516. return SDL_JoystickSetVirtualAxisInner(joystick, axis, value);
  517. #else
  518. return SDL_SetError("SDL not built with virtual-joystick support");
  519. #endif
  520. }
  521. int
  522. SDL_JoystickSetVirtualButton(SDL_Joystick *joystick, int button, Uint8 value)
  523. {
  524. #if SDL_JOYSTICK_VIRTUAL
  525. return SDL_JoystickSetVirtualButtonInner(joystick, button, value);
  526. #else
  527. return SDL_SetError("SDL not built with virtual-joystick support");
  528. #endif
  529. }
  530. int
  531. SDL_JoystickSetVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value)
  532. {
  533. #if SDL_JOYSTICK_VIRTUAL
  534. return SDL_JoystickSetVirtualHatInner(joystick, hat, value);
  535. #else
  536. return SDL_SetError("SDL not built with virtual-joystick support");
  537. #endif
  538. }
  539. /*
  540. * Checks to make sure the joystick is valid.
  541. */
  542. SDL_bool
  543. SDL_PrivateJoystickValid(SDL_Joystick *joystick)
  544. {
  545. SDL_bool valid;
  546. if (joystick == NULL) {
  547. SDL_SetError("Joystick hasn't been opened yet");
  548. valid = SDL_FALSE;
  549. } else {
  550. valid = SDL_TRUE;
  551. }
  552. return valid;
  553. }
  554. SDL_bool
  555. SDL_PrivateJoystickGetAutoGamepadMapping(int device_index, SDL_GamepadMapping * out)
  556. {
  557. SDL_JoystickDriver *driver;
  558. SDL_bool is_ok = SDL_FALSE;
  559. SDL_LockJoysticks();
  560. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  561. is_ok = driver->GetGamepadMapping(device_index, out);
  562. }
  563. SDL_UnlockJoysticks();
  564. return is_ok;
  565. }
  566. /*
  567. * Get the number of multi-dimensional axis controls on a joystick
  568. */
  569. int
  570. SDL_JoystickNumAxes(SDL_Joystick *joystick)
  571. {
  572. if (!SDL_PrivateJoystickValid(joystick)) {
  573. return -1;
  574. }
  575. return joystick->naxes;
  576. }
  577. /*
  578. * Get the number of hats on a joystick
  579. */
  580. int
  581. SDL_JoystickNumHats(SDL_Joystick *joystick)
  582. {
  583. if (!SDL_PrivateJoystickValid(joystick)) {
  584. return -1;
  585. }
  586. return joystick->nhats;
  587. }
  588. /*
  589. * Get the number of trackballs on a joystick
  590. */
  591. int
  592. SDL_JoystickNumBalls(SDL_Joystick *joystick)
  593. {
  594. if (!SDL_PrivateJoystickValid(joystick)) {
  595. return -1;
  596. }
  597. return joystick->nballs;
  598. }
  599. /*
  600. * Get the number of buttons on a joystick
  601. */
  602. int
  603. SDL_JoystickNumButtons(SDL_Joystick *joystick)
  604. {
  605. if (!SDL_PrivateJoystickValid(joystick)) {
  606. return -1;
  607. }
  608. return joystick->nbuttons;
  609. }
  610. /*
  611. * Get the current state of an axis control on a joystick
  612. */
  613. Sint16
  614. SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)
  615. {
  616. Sint16 state;
  617. if (!SDL_PrivateJoystickValid(joystick)) {
  618. return 0;
  619. }
  620. if (axis < joystick->naxes) {
  621. state = joystick->axes[axis].value;
  622. } else {
  623. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  624. state = 0;
  625. }
  626. return state;
  627. }
  628. /*
  629. * Get the initial state of an axis control on a joystick
  630. */
  631. SDL_bool
  632. SDL_JoystickGetAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state)
  633. {
  634. if (!SDL_PrivateJoystickValid(joystick)) {
  635. return SDL_FALSE;
  636. }
  637. if (axis >= joystick->naxes) {
  638. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  639. return SDL_FALSE;
  640. }
  641. if (state) {
  642. *state = joystick->axes[axis].initial_value;
  643. }
  644. return joystick->axes[axis].has_initial_value;
  645. }
  646. /*
  647. * Get the current state of a hat on a joystick
  648. */
  649. Uint8
  650. SDL_JoystickGetHat(SDL_Joystick *joystick, int hat)
  651. {
  652. Uint8 state;
  653. if (!SDL_PrivateJoystickValid(joystick)) {
  654. return 0;
  655. }
  656. if (hat < joystick->nhats) {
  657. state = joystick->hats[hat];
  658. } else {
  659. SDL_SetError("Joystick only has %d hats", joystick->nhats);
  660. state = 0;
  661. }
  662. return state;
  663. }
  664. /*
  665. * Get the ball axis change since the last poll
  666. */
  667. int
  668. SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy)
  669. {
  670. int retval;
  671. if (!SDL_PrivateJoystickValid(joystick)) {
  672. return -1;
  673. }
  674. retval = 0;
  675. if (ball < joystick->nballs) {
  676. if (dx) {
  677. *dx = joystick->balls[ball].dx;
  678. }
  679. if (dy) {
  680. *dy = joystick->balls[ball].dy;
  681. }
  682. joystick->balls[ball].dx = 0;
  683. joystick->balls[ball].dy = 0;
  684. } else {
  685. return SDL_SetError("Joystick only has %d balls", joystick->nballs);
  686. }
  687. return retval;
  688. }
  689. /*
  690. * Get the current state of a button on a joystick
  691. */
  692. Uint8
  693. SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
  694. {
  695. Uint8 state;
  696. if (!SDL_PrivateJoystickValid(joystick)) {
  697. return 0;
  698. }
  699. if (button < joystick->nbuttons) {
  700. state = joystick->buttons[button];
  701. } else {
  702. SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
  703. state = 0;
  704. }
  705. return state;
  706. }
  707. /*
  708. * Return if the joystick in question is currently attached to the system,
  709. * \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
  710. */
  711. SDL_bool
  712. SDL_JoystickGetAttached(SDL_Joystick *joystick)
  713. {
  714. if (!joystick) {
  715. return SDL_FALSE;
  716. }
  717. return joystick->attached;
  718. }
  719. /*
  720. * Get the instance id for this opened joystick
  721. */
  722. SDL_JoystickID
  723. SDL_JoystickInstanceID(SDL_Joystick *joystick)
  724. {
  725. if (!SDL_PrivateJoystickValid(joystick)) {
  726. return -1;
  727. }
  728. return joystick->instance_id;
  729. }
  730. /*
  731. * Return the SDL_Joystick associated with an instance id.
  732. */
  733. SDL_Joystick *
  734. SDL_JoystickFromInstanceID(SDL_JoystickID instance_id)
  735. {
  736. SDL_Joystick *joystick;
  737. SDL_LockJoysticks();
  738. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  739. if (joystick->instance_id == instance_id) {
  740. break;
  741. }
  742. }
  743. SDL_UnlockJoysticks();
  744. return joystick;
  745. }
  746. /**
  747. * Return the SDL_Joystick associated with a player index.
  748. */
  749. SDL_Joystick *
  750. SDL_JoystickFromPlayerIndex(int player_index)
  751. {
  752. SDL_JoystickID instance_id;
  753. SDL_Joystick *joystick;
  754. SDL_LockJoysticks();
  755. instance_id = SDL_GetJoystickIDForPlayerIndex(player_index);
  756. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  757. if (joystick->instance_id == instance_id) {
  758. break;
  759. }
  760. }
  761. SDL_UnlockJoysticks();
  762. return joystick;
  763. }
  764. /*
  765. * Get the friendly name of this joystick
  766. */
  767. const char *
  768. SDL_JoystickName(SDL_Joystick *joystick)
  769. {
  770. if (!SDL_PrivateJoystickValid(joystick)) {
  771. return NULL;
  772. }
  773. return joystick->name;
  774. }
  775. /*
  776. * Get the implementation dependent path of this joystick
  777. */
  778. const char *
  779. SDL_JoystickPath(SDL_Joystick *joystick)
  780. {
  781. if (!SDL_PrivateJoystickValid(joystick)) {
  782. return NULL;
  783. }
  784. if (!joystick->path) {
  785. SDL_Unsupported();
  786. return NULL;
  787. }
  788. return joystick->path;
  789. }
  790. /**
  791. * Get the player index of an opened joystick, or -1 if it's not available
  792. */
  793. int
  794. SDL_JoystickGetPlayerIndex(SDL_Joystick *joystick)
  795. {
  796. int player_index;
  797. if (!SDL_PrivateJoystickValid(joystick)) {
  798. return -1;
  799. }
  800. SDL_LockJoysticks();
  801. player_index = SDL_GetPlayerIndexForJoystickID(joystick->instance_id);
  802. SDL_UnlockJoysticks();
  803. return player_index;
  804. }
  805. /**
  806. * Set the player index of an opened joystick
  807. */
  808. void
  809. SDL_JoystickSetPlayerIndex(SDL_Joystick *joystick, int player_index)
  810. {
  811. if (!SDL_PrivateJoystickValid(joystick)) {
  812. return;
  813. }
  814. SDL_LockJoysticks();
  815. SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id);
  816. SDL_UnlockJoysticks();
  817. }
  818. int
  819. SDL_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  820. {
  821. int result;
  822. if (!SDL_PrivateJoystickValid(joystick)) {
  823. return -1;
  824. }
  825. SDL_LockJoysticks();
  826. if (low_frequency_rumble == joystick->low_frequency_rumble &&
  827. high_frequency_rumble == joystick->high_frequency_rumble) {
  828. /* Just update the expiration */
  829. result = 0;
  830. } else {
  831. result = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble);
  832. }
  833. if (result == 0) {
  834. joystick->low_frequency_rumble = low_frequency_rumble;
  835. joystick->high_frequency_rumble = high_frequency_rumble;
  836. if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
  837. joystick->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
  838. if (!joystick->rumble_expiration) {
  839. joystick->rumble_expiration = 1;
  840. }
  841. } else {
  842. joystick->rumble_expiration = 0;
  843. }
  844. }
  845. SDL_UnlockJoysticks();
  846. return result;
  847. }
  848. int
  849. SDL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
  850. {
  851. int result;
  852. if (!SDL_PrivateJoystickValid(joystick)) {
  853. return -1;
  854. }
  855. SDL_LockJoysticks();
  856. if (left_rumble == joystick->left_trigger_rumble && right_rumble == joystick->right_trigger_rumble) {
  857. /* Just update the expiration */
  858. result = 0;
  859. } else {
  860. result = joystick->driver->RumbleTriggers(joystick, left_rumble, right_rumble);
  861. }
  862. if (result == 0) {
  863. joystick->left_trigger_rumble = left_rumble;
  864. joystick->right_trigger_rumble = right_rumble;
  865. if ((left_rumble || right_rumble) && duration_ms) {
  866. joystick->trigger_rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
  867. if (!joystick->trigger_rumble_expiration) {
  868. joystick->trigger_rumble_expiration = 1;
  869. }
  870. } else {
  871. joystick->trigger_rumble_expiration = 0;
  872. }
  873. }
  874. SDL_UnlockJoysticks();
  875. return result;
  876. }
  877. SDL_bool
  878. SDL_JoystickHasLED(SDL_Joystick *joystick)
  879. {
  880. SDL_bool result;
  881. if (!SDL_PrivateJoystickValid(joystick)) {
  882. return SDL_FALSE;
  883. }
  884. SDL_LockJoysticks();
  885. result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_LED) != 0;
  886. SDL_UnlockJoysticks();
  887. return result;
  888. }
  889. SDL_bool
  890. SDL_JoystickHasRumble(SDL_Joystick *joystick)
  891. {
  892. SDL_bool result;
  893. if (!SDL_PrivateJoystickValid(joystick)) {
  894. return SDL_FALSE;
  895. }
  896. SDL_LockJoysticks();
  897. result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE) != 0;
  898. SDL_UnlockJoysticks();
  899. return result;
  900. }
  901. SDL_bool
  902. SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick)
  903. {
  904. SDL_bool result;
  905. if (!SDL_PrivateJoystickValid(joystick)) {
  906. return SDL_FALSE;
  907. }
  908. SDL_LockJoysticks();
  909. result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE_TRIGGERS) != 0;
  910. SDL_UnlockJoysticks();
  911. return result;
  912. }
  913. int
  914. SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  915. {
  916. int result;
  917. SDL_bool isfreshvalue;
  918. if (!SDL_PrivateJoystickValid(joystick)) {
  919. return -1;
  920. }
  921. SDL_LockJoysticks();
  922. isfreshvalue = red != joystick->led_red ||
  923. green != joystick->led_green ||
  924. blue != joystick->led_blue;
  925. if (isfreshvalue || SDL_TICKS_PASSED(SDL_GetTicks(), joystick->led_expiration)) {
  926. result = joystick->driver->SetLED(joystick, red, green, blue);
  927. joystick->led_expiration = SDL_GetTicks() + SDL_LED_MIN_REPEAT_MS;
  928. } else {
  929. /* Avoid spamming the driver */
  930. result = 0;
  931. }
  932. /* Save the LED value regardless of success, so we don't spam the driver */
  933. joystick->led_red = red;
  934. joystick->led_green = green;
  935. joystick->led_blue = blue;
  936. SDL_UnlockJoysticks();
  937. return result;
  938. }
  939. int
  940. SDL_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  941. {
  942. int result;
  943. if (!SDL_PrivateJoystickValid(joystick)) {
  944. return -1;
  945. }
  946. SDL_LockJoysticks();
  947. result = joystick->driver->SendEffect(joystick, data, size);
  948. SDL_UnlockJoysticks();
  949. return result;
  950. }
  951. /*
  952. * Close a joystick previously opened with SDL_JoystickOpen()
  953. */
  954. void
  955. SDL_JoystickClose(SDL_Joystick *joystick)
  956. {
  957. SDL_Joystick *joysticklist;
  958. SDL_Joystick *joysticklistprev;
  959. int i;
  960. if (!SDL_PrivateJoystickValid(joystick)) {
  961. return;
  962. }
  963. SDL_LockJoysticks();
  964. /* First decrement ref count */
  965. if (--joystick->ref_count > 0) {
  966. SDL_UnlockJoysticks();
  967. return;
  968. }
  969. if (SDL_updating_joystick) {
  970. SDL_UnlockJoysticks();
  971. return;
  972. }
  973. if (joystick->rumble_expiration) {
  974. SDL_JoystickRumble(joystick, 0, 0, 0);
  975. }
  976. if (joystick->trigger_rumble_expiration) {
  977. SDL_JoystickRumbleTriggers(joystick, 0, 0, 0);
  978. }
  979. joystick->driver->Close(joystick);
  980. joystick->hwdata = NULL;
  981. joysticklist = SDL_joysticks;
  982. joysticklistprev = NULL;
  983. while (joysticklist) {
  984. if (joystick == joysticklist) {
  985. if (joysticklistprev) {
  986. /* unlink this entry */
  987. joysticklistprev->next = joysticklist->next;
  988. } else {
  989. SDL_joysticks = joystick->next;
  990. }
  991. break;
  992. }
  993. joysticklistprev = joysticklist;
  994. joysticklist = joysticklist->next;
  995. }
  996. SDL_free(joystick->name);
  997. SDL_free(joystick->path);
  998. SDL_free(joystick->serial);
  999. /* Free the data associated with this joystick */
  1000. SDL_free(joystick->axes);
  1001. SDL_free(joystick->hats);
  1002. SDL_free(joystick->balls);
  1003. SDL_free(joystick->buttons);
  1004. for (i = 0; i < joystick->ntouchpads; i++) {
  1005. SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];
  1006. SDL_free(touchpad->fingers);
  1007. }
  1008. SDL_free(joystick->touchpads);
  1009. SDL_free(joystick->sensors);
  1010. SDL_free(joystick);
  1011. SDL_UnlockJoysticks();
  1012. }
  1013. void
  1014. SDL_JoystickQuit(void)
  1015. {
  1016. int i;
  1017. /* Make sure we're not getting called in the middle of updating joysticks */
  1018. SDL_LockJoysticks();
  1019. while (SDL_updating_joystick) {
  1020. SDL_UnlockJoysticks();
  1021. SDL_Delay(1);
  1022. SDL_LockJoysticks();
  1023. }
  1024. /* Stop the event polling */
  1025. while (SDL_joysticks) {
  1026. SDL_joysticks->ref_count = 1;
  1027. SDL_JoystickClose(SDL_joysticks);
  1028. }
  1029. /* Quit drivers in reverse order to avoid breaking dependencies between drivers */
  1030. for (i = SDL_arraysize(SDL_joystick_drivers) - 1; i >= 0; --i) {
  1031. SDL_joystick_drivers[i]->Quit();
  1032. }
  1033. if (SDL_joystick_players) {
  1034. SDL_free(SDL_joystick_players);
  1035. SDL_joystick_players = NULL;
  1036. SDL_joystick_player_count = 0;
  1037. }
  1038. SDL_UnlockJoysticks();
  1039. #if !SDL_EVENTS_DISABLED
  1040. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  1041. #endif
  1042. SDL_DelHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  1043. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  1044. if (SDL_joystick_lock) {
  1045. SDL_mutex *mutex = SDL_joystick_lock;
  1046. SDL_joystick_lock = NULL;
  1047. SDL_DestroyMutex(mutex);
  1048. }
  1049. SDL_GameControllerQuitMappings();
  1050. }
  1051. static SDL_bool
  1052. SDL_PrivateJoystickShouldIgnoreEvent()
  1053. {
  1054. if (SDL_joystick_allows_background_events) {
  1055. return SDL_FALSE;
  1056. }
  1057. if (SDL_HasWindows() && SDL_GetKeyboardFocus() == NULL) {
  1058. /* We have windows but we don't have focus, ignore the event. */
  1059. return SDL_TRUE;
  1060. }
  1061. return SDL_FALSE;
  1062. }
  1063. /* These are global for SDL_sysjoystick.c and SDL_events.c */
  1064. void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers)
  1065. {
  1066. int ntouchpads = joystick->ntouchpads + 1;
  1067. SDL_JoystickTouchpadInfo *touchpads = (SDL_JoystickTouchpadInfo *)SDL_realloc(joystick->touchpads, (ntouchpads * sizeof(SDL_JoystickTouchpadInfo)));
  1068. if (touchpads) {
  1069. SDL_JoystickTouchpadInfo *touchpad = &touchpads[ntouchpads - 1];
  1070. SDL_JoystickTouchpadFingerInfo *fingers = (SDL_JoystickTouchpadFingerInfo *)SDL_calloc(nfingers, sizeof(SDL_JoystickTouchpadFingerInfo));
  1071. if (fingers) {
  1072. touchpad->nfingers = nfingers;
  1073. touchpad->fingers = fingers;
  1074. } else {
  1075. /* Out of memory, this touchpad won't be active */
  1076. touchpad->nfingers = 0;
  1077. touchpad->fingers = NULL;
  1078. }
  1079. joystick->ntouchpads = ntouchpads;
  1080. joystick->touchpads = touchpads;
  1081. }
  1082. }
  1083. void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, float rate)
  1084. {
  1085. int nsensors = joystick->nsensors + 1;
  1086. SDL_JoystickSensorInfo *sensors = (SDL_JoystickSensorInfo *)SDL_realloc(joystick->sensors, (nsensors * sizeof(SDL_JoystickSensorInfo)));
  1087. if (sensors) {
  1088. SDL_JoystickSensorInfo *sensor = &sensors[nsensors - 1];
  1089. SDL_zerop(sensor);
  1090. sensor->type = type;
  1091. sensor->rate = rate;
  1092. joystick->nsensors = nsensors;
  1093. joystick->sensors = sensors;
  1094. }
  1095. }
  1096. void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
  1097. {
  1098. SDL_JoystickDriver *driver;
  1099. int driver_device_index;
  1100. int player_index = -1;
  1101. int device_index = SDL_JoystickGetDeviceIndexFromInstanceID(device_instance);
  1102. if (device_index < 0) {
  1103. return;
  1104. }
  1105. SDL_LockJoysticks();
  1106. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) {
  1107. player_index = driver->GetDevicePlayerIndex(driver_device_index);
  1108. }
  1109. if (player_index < 0 && SDL_IsGameController(device_index)) {
  1110. player_index = SDL_FindFreePlayerIndex();
  1111. }
  1112. if (player_index >= 0) {
  1113. SDL_SetJoystickIDForPlayerIndex(player_index, device_instance);
  1114. }
  1115. SDL_UnlockJoysticks();
  1116. #if !SDL_EVENTS_DISABLED
  1117. {
  1118. SDL_Event event;
  1119. event.type = SDL_JOYDEVICEADDED;
  1120. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1121. event.jdevice.which = device_index;
  1122. SDL_PushEvent(&event);
  1123. }
  1124. }
  1125. #endif /* !SDL_EVENTS_DISABLED */
  1126. }
  1127. /*
  1128. * If there is an existing add event in the queue, it needs to be modified
  1129. * to have the right value for which, because the number of controllers in
  1130. * the system is now one less.
  1131. */
  1132. static void UpdateEventsForDeviceRemoval(int device_index)
  1133. {
  1134. int i, num_events;
  1135. SDL_Event *events;
  1136. SDL_bool isstack;
  1137. num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
  1138. if (num_events <= 0) {
  1139. return;
  1140. }
  1141. events = SDL_small_alloc(SDL_Event, num_events, &isstack);
  1142. if (!events) {
  1143. return;
  1144. }
  1145. num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
  1146. for (i = 0; i < num_events; ++i) {
  1147. if (events[i].cdevice.which < device_index) {
  1148. /* No change for index values lower than the removed device */
  1149. }
  1150. else if (events[i].cdevice.which == device_index) {
  1151. /* Drop this event entirely */
  1152. SDL_memmove(&events[i], &events[i + 1], sizeof(*events) * (num_events - (i + 1)));
  1153. --num_events;
  1154. --i;
  1155. }
  1156. else {
  1157. /* Fix up the device index if greater than the removed device */
  1158. --events[i].cdevice.which;
  1159. }
  1160. }
  1161. SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
  1162. SDL_small_free(events, isstack);
  1163. }
  1164. static void
  1165. SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
  1166. {
  1167. int i, j;
  1168. /* Tell the app that everything is centered/unpressed... */
  1169. for (i = 0; i < joystick->naxes; i++) {
  1170. if (joystick->axes[i].has_initial_value) {
  1171. SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
  1172. }
  1173. }
  1174. for (i = 0; i < joystick->nbuttons; i++) {
  1175. SDL_PrivateJoystickButton(joystick, i, SDL_RELEASED);
  1176. }
  1177. for (i = 0; i < joystick->nhats; i++) {
  1178. SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
  1179. }
  1180. for (i = 0; i < joystick->ntouchpads; i++) {
  1181. SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];
  1182. for (j = 0; j < touchpad->nfingers; ++j) {
  1183. SDL_PrivateJoystickTouchpad(joystick, i, j, SDL_RELEASED, 0.0f, 0.0f, 0.0f);
  1184. }
  1185. }
  1186. }
  1187. void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
  1188. {
  1189. SDL_Joystick *joystick = NULL;
  1190. int player_index;
  1191. int device_index;
  1192. #if !SDL_EVENTS_DISABLED
  1193. SDL_Event event;
  1194. #endif
  1195. /* Find this joystick... */
  1196. device_index = 0;
  1197. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  1198. if (joystick->instance_id == device_instance) {
  1199. SDL_PrivateJoystickForceRecentering(joystick);
  1200. joystick->attached = SDL_FALSE;
  1201. break;
  1202. }
  1203. ++device_index;
  1204. }
  1205. #if !SDL_EVENTS_DISABLED
  1206. SDL_zero(event);
  1207. event.type = SDL_JOYDEVICEREMOVED;
  1208. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1209. event.jdevice.which = device_instance;
  1210. SDL_PushEvent(&event);
  1211. }
  1212. UpdateEventsForDeviceRemoval(device_index);
  1213. #endif /* !SDL_EVENTS_DISABLED */
  1214. SDL_LockJoysticks();
  1215. player_index = SDL_GetPlayerIndexForJoystickID(device_instance);
  1216. if (player_index >= 0) {
  1217. SDL_joystick_players[player_index] = -1;
  1218. }
  1219. SDL_UnlockJoysticks();
  1220. }
  1221. int
  1222. SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
  1223. {
  1224. int posted;
  1225. SDL_JoystickAxisInfo *info;
  1226. /* Make sure we're not getting garbage or duplicate events */
  1227. if (axis >= joystick->naxes) {
  1228. return 0;
  1229. }
  1230. info = &joystick->axes[axis];
  1231. if (!info->has_initial_value ||
  1232. (!info->has_second_value && (info->initial_value <= -32767 || info->initial_value == 32767) && SDL_abs(value) < (SDL_JOYSTICK_AXIS_MAX / 4))) {
  1233. info->initial_value = value;
  1234. info->value = value;
  1235. info->zero = value;
  1236. info->has_initial_value = SDL_TRUE;
  1237. } else if (value == info->value && !info->sending_initial_value) {
  1238. return 0;
  1239. } else {
  1240. info->has_second_value = SDL_TRUE;
  1241. }
  1242. if (!info->sent_initial_value) {
  1243. /* Make sure we don't send motion until there's real activity on this axis */
  1244. const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */
  1245. if (SDL_abs(value - info->value) <= MAX_ALLOWED_JITTER) {
  1246. return 0;
  1247. }
  1248. info->sent_initial_value = SDL_TRUE;
  1249. info->sending_initial_value = SDL_TRUE;
  1250. SDL_PrivateJoystickAxis(joystick, axis, info->initial_value);
  1251. info->sending_initial_value = SDL_FALSE;
  1252. }
  1253. /* We ignore events if we don't have keyboard focus, except for centering
  1254. * events.
  1255. */
  1256. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1257. if (info->sending_initial_value ||
  1258. (value > info->zero && value >= info->value) ||
  1259. (value < info->zero && value <= info->value)) {
  1260. return 0;
  1261. }
  1262. }
  1263. /* Update internal joystick state */
  1264. info->value = value;
  1265. /* Post the event, if desired */
  1266. posted = 0;
  1267. #if !SDL_EVENTS_DISABLED
  1268. if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
  1269. SDL_Event event;
  1270. event.type = SDL_JOYAXISMOTION;
  1271. event.jaxis.which = joystick->instance_id;
  1272. event.jaxis.axis = axis;
  1273. event.jaxis.value = value;
  1274. posted = SDL_PushEvent(&event) == 1;
  1275. }
  1276. #endif /* !SDL_EVENTS_DISABLED */
  1277. return posted;
  1278. }
  1279. int
  1280. SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
  1281. {
  1282. int posted;
  1283. /* Make sure we're not getting garbage or duplicate events */
  1284. if (hat >= joystick->nhats) {
  1285. return 0;
  1286. }
  1287. if (value == joystick->hats[hat]) {
  1288. return 0;
  1289. }
  1290. /* We ignore events if we don't have keyboard focus, except for centering
  1291. * events.
  1292. */
  1293. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1294. if (value != SDL_HAT_CENTERED) {
  1295. return 0;
  1296. }
  1297. }
  1298. /* Update internal joystick state */
  1299. joystick->hats[hat] = value;
  1300. /* Post the event, if desired */
  1301. posted = 0;
  1302. #if !SDL_EVENTS_DISABLED
  1303. if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
  1304. SDL_Event event;
  1305. event.jhat.type = SDL_JOYHATMOTION;
  1306. event.jhat.which = joystick->instance_id;
  1307. event.jhat.hat = hat;
  1308. event.jhat.value = value;
  1309. posted = SDL_PushEvent(&event) == 1;
  1310. }
  1311. #endif /* !SDL_EVENTS_DISABLED */
  1312. return posted;
  1313. }
  1314. int
  1315. SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball,
  1316. Sint16 xrel, Sint16 yrel)
  1317. {
  1318. int posted;
  1319. /* Make sure we're not getting garbage events */
  1320. if (ball >= joystick->nballs) {
  1321. return 0;
  1322. }
  1323. /* We ignore events if we don't have keyboard focus. */
  1324. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1325. return 0;
  1326. }
  1327. /* Update internal mouse state */
  1328. joystick->balls[ball].dx += xrel;
  1329. joystick->balls[ball].dy += yrel;
  1330. /* Post the event, if desired */
  1331. posted = 0;
  1332. #if !SDL_EVENTS_DISABLED
  1333. if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
  1334. SDL_Event event;
  1335. event.jball.type = SDL_JOYBALLMOTION;
  1336. event.jball.which = joystick->instance_id;
  1337. event.jball.ball = ball;
  1338. event.jball.xrel = xrel;
  1339. event.jball.yrel = yrel;
  1340. posted = SDL_PushEvent(&event) == 1;
  1341. }
  1342. #endif /* !SDL_EVENTS_DISABLED */
  1343. return posted;
  1344. }
  1345. int
  1346. SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
  1347. {
  1348. int posted;
  1349. #if !SDL_EVENTS_DISABLED
  1350. SDL_Event event;
  1351. switch (state) {
  1352. case SDL_PRESSED:
  1353. event.type = SDL_JOYBUTTONDOWN;
  1354. break;
  1355. case SDL_RELEASED:
  1356. event.type = SDL_JOYBUTTONUP;
  1357. break;
  1358. default:
  1359. /* Invalid state -- bail */
  1360. return 0;
  1361. }
  1362. #endif /* !SDL_EVENTS_DISABLED */
  1363. /* Make sure we're not getting garbage or duplicate events */
  1364. if (button >= joystick->nbuttons) {
  1365. return 0;
  1366. }
  1367. if (state == joystick->buttons[button]) {
  1368. return 0;
  1369. }
  1370. /* We ignore events if we don't have keyboard focus, except for button
  1371. * release. */
  1372. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1373. if (state == SDL_PRESSED) {
  1374. return 0;
  1375. }
  1376. }
  1377. /* Update internal joystick state */
  1378. joystick->buttons[button] = state;
  1379. /* Post the event, if desired */
  1380. posted = 0;
  1381. #if !SDL_EVENTS_DISABLED
  1382. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1383. event.jbutton.which = joystick->instance_id;
  1384. event.jbutton.button = button;
  1385. event.jbutton.state = state;
  1386. posted = SDL_PushEvent(&event) == 1;
  1387. }
  1388. #endif /* !SDL_EVENTS_DISABLED */
  1389. return posted;
  1390. }
  1391. void
  1392. SDL_JoystickUpdate(void)
  1393. {
  1394. int i;
  1395. SDL_Joystick *joystick, *next;
  1396. if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
  1397. return;
  1398. }
  1399. SDL_LockJoysticks();
  1400. if (SDL_updating_joystick) {
  1401. /* The joysticks are already being updated */
  1402. SDL_UnlockJoysticks();
  1403. return;
  1404. }
  1405. SDL_updating_joystick = SDL_TRUE;
  1406. /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
  1407. SDL_UnlockJoysticks();
  1408. #ifdef SDL_JOYSTICK_HIDAPI
  1409. /* Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks */
  1410. HIDAPI_UpdateDevices();
  1411. #endif /* SDL_JOYSTICK_HIDAPI */
  1412. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  1413. if (joystick->attached) {
  1414. /* This driver should always be != NULL, but seeing a crash in the wild...? */
  1415. if (!joystick->driver) {
  1416. continue; /* nothing we can do, and other things use joystick->driver below here. */
  1417. }
  1418. joystick->driver->Update(joystick);
  1419. if (joystick->delayed_guide_button) {
  1420. SDL_GameControllerHandleDelayedGuideButton(joystick);
  1421. }
  1422. }
  1423. if (joystick->rumble_expiration) {
  1424. SDL_LockJoysticks();
  1425. /* Double check now that the lock is held */
  1426. if (joystick->rumble_expiration &&
  1427. SDL_TICKS_PASSED(SDL_GetTicks(), joystick->rumble_expiration)) {
  1428. SDL_JoystickRumble(joystick, 0, 0, 0);
  1429. }
  1430. SDL_UnlockJoysticks();
  1431. }
  1432. if (joystick->trigger_rumble_expiration) {
  1433. SDL_LockJoysticks();
  1434. /* Double check now that the lock is held */
  1435. if (joystick->trigger_rumble_expiration &&
  1436. SDL_TICKS_PASSED(SDL_GetTicks(), joystick->trigger_rumble_expiration)) {
  1437. SDL_JoystickRumbleTriggers(joystick, 0, 0, 0);
  1438. }
  1439. SDL_UnlockJoysticks();
  1440. }
  1441. }
  1442. SDL_LockJoysticks();
  1443. SDL_updating_joystick = SDL_FALSE;
  1444. /* If any joysticks were closed while updating, free them here */
  1445. for (joystick = SDL_joysticks; joystick; joystick = next) {
  1446. next = joystick->next;
  1447. if (joystick->ref_count <= 0) {
  1448. SDL_JoystickClose(joystick);
  1449. }
  1450. }
  1451. /* this needs to happen AFTER walking the joystick list above, so that any
  1452. dangling hardware data from removed devices can be free'd
  1453. */
  1454. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  1455. SDL_joystick_drivers[i]->Detect();
  1456. }
  1457. SDL_UnlockJoysticks();
  1458. }
  1459. int
  1460. SDL_JoystickEventState(int state)
  1461. {
  1462. #if SDL_EVENTS_DISABLED
  1463. return SDL_DISABLE;
  1464. #else
  1465. const Uint32 event_list[] = {
  1466. SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
  1467. SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
  1468. };
  1469. unsigned int i;
  1470. switch (state) {
  1471. case SDL_QUERY:
  1472. state = SDL_DISABLE;
  1473. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1474. state = SDL_EventState(event_list[i], SDL_QUERY);
  1475. if (state == SDL_ENABLE) {
  1476. break;
  1477. }
  1478. }
  1479. break;
  1480. default:
  1481. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1482. SDL_EventState(event_list[i], state);
  1483. }
  1484. break;
  1485. }
  1486. return state;
  1487. #endif /* SDL_EVENTS_DISABLED */
  1488. }
  1489. void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
  1490. {
  1491. Uint16 *guid16 = (Uint16 *)guid.data;
  1492. /* If the GUID fits the form of BUS 0000 VENDOR 0000 PRODUCT 0000, return the data */
  1493. if (/* guid16[0] is device bus type */
  1494. guid16[1] == 0x0000 &&
  1495. /* guid16[2] is vendor ID */
  1496. guid16[3] == 0x0000 &&
  1497. /* guid16[4] is product ID */
  1498. guid16[5] == 0x0000
  1499. /* guid16[6] is product version */
  1500. ) {
  1501. if (vendor) {
  1502. *vendor = guid16[2];
  1503. }
  1504. if (product) {
  1505. *product = guid16[4];
  1506. }
  1507. if (version) {
  1508. *version = guid16[6];
  1509. }
  1510. } else {
  1511. if (vendor) {
  1512. *vendor = 0;
  1513. }
  1514. if (product) {
  1515. *product = 0;
  1516. }
  1517. if (version) {
  1518. *version = 0;
  1519. }
  1520. }
  1521. }
  1522. static int
  1523. PrefixMatch(const char *a, const char *b)
  1524. {
  1525. int matchlen = 0;
  1526. while (*a && *b) {
  1527. if (SDL_tolower((unsigned char) *a++) == SDL_tolower((unsigned char) *b++)) {
  1528. ++matchlen;
  1529. } else {
  1530. break;
  1531. }
  1532. }
  1533. return matchlen;
  1534. }
  1535. char *
  1536. SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name)
  1537. {
  1538. static struct {
  1539. const char *prefix;
  1540. const char *replacement;
  1541. } replacements[] = {
  1542. { "NVIDIA Corporation ", "" },
  1543. { "Performance Designed Products", "PDP" },
  1544. { "HORI CO.,LTD.", "HORI" },
  1545. { "HORI CO.,LTD", "HORI" },
  1546. { "Unknown ", "" },
  1547. };
  1548. const char *custom_name;
  1549. char *name;
  1550. size_t i, len;
  1551. /* Use the given name for the Nintendo Online NES Controllers */
  1552. if (SDL_strncmp(product_name, "NES Controller", 14) == 0) {
  1553. return SDL_strdup(product_name);
  1554. }
  1555. custom_name = GuessControllerName(vendor, product);
  1556. if (custom_name) {
  1557. return SDL_strdup(custom_name);
  1558. }
  1559. if (!vendor_name) {
  1560. vendor_name = "";
  1561. }
  1562. if (!product_name) {
  1563. product_name = "";
  1564. }
  1565. while (*vendor_name == ' ') {
  1566. ++vendor_name;
  1567. }
  1568. while (*product_name == ' ') {
  1569. ++product_name;
  1570. }
  1571. if (*vendor_name && *product_name) {
  1572. len = (SDL_strlen(vendor_name) + 1 + SDL_strlen(product_name) + 1);
  1573. name = (char *)SDL_malloc(len);
  1574. if (name) {
  1575. SDL_snprintf(name, len, "%s %s", vendor_name, product_name);
  1576. }
  1577. } else if (*product_name) {
  1578. name = SDL_strdup(product_name);
  1579. } else if (vendor || product) {
  1580. /* Couldn't find a controller name, try to give it one based on device type */
  1581. switch (SDL_GetJoystickGameControllerTypeFromVIDPID(vendor, product, NULL, SDL_TRUE)) {
  1582. case SDL_CONTROLLER_TYPE_XBOX360:
  1583. name = SDL_strdup("Xbox 360 Controller");
  1584. break;
  1585. case SDL_CONTROLLER_TYPE_XBOXONE:
  1586. name = SDL_strdup("Xbox One Controller");
  1587. break;
  1588. case SDL_CONTROLLER_TYPE_PS3:
  1589. name = SDL_strdup("PS3 Controller");
  1590. break;
  1591. case SDL_CONTROLLER_TYPE_PS4:
  1592. name = SDL_strdup("PS4 Controller");
  1593. break;
  1594. case SDL_CONTROLLER_TYPE_PS5:
  1595. name = SDL_strdup("PS5 Controller");
  1596. break;
  1597. case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO:
  1598. name = SDL_strdup("Nintendo Switch Pro Controller");
  1599. break;
  1600. default:
  1601. len = (6 + 1 + 6 + 1);
  1602. name = (char *)SDL_malloc(len);
  1603. if (name) {
  1604. SDL_snprintf(name, len, "0x%.4x/0x%.4x", vendor, product);
  1605. }
  1606. break;
  1607. }
  1608. } else {
  1609. name = SDL_strdup("Controller");
  1610. }
  1611. if (!name) {
  1612. return NULL;
  1613. }
  1614. /* Trim trailing whitespace */
  1615. for (len = SDL_strlen(name); (len > 0 && name[len - 1] == ' '); --len) {
  1616. /* continue */
  1617. }
  1618. name[len] = '\0';
  1619. /* Compress duplicate spaces */
  1620. for (i = 0; i < (len - 1); ) {
  1621. if (name[i] == ' ' && name[i+1] == ' ') {
  1622. SDL_memmove(&name[i], &name[i+1], (len - i));
  1623. --len;
  1624. } else {
  1625. ++i;
  1626. }
  1627. }
  1628. /* Perform any manufacturer replacements */
  1629. for (i = 0; i < SDL_arraysize(replacements); ++i) {
  1630. size_t prefixlen = SDL_strlen(replacements[i].prefix);
  1631. if (SDL_strncasecmp(name, replacements[i].prefix, prefixlen) == 0) {
  1632. size_t replacementlen = SDL_strlen(replacements[i].replacement);
  1633. if (replacementlen <= prefixlen) {
  1634. SDL_memcpy(name, replacements[i].replacement, replacementlen);
  1635. SDL_memmove(name+replacementlen, name+prefixlen, (len-prefixlen)+1);
  1636. len -= (prefixlen - replacementlen);
  1637. } else {
  1638. /* FIXME: Need to handle the expand case by reallocating the string */
  1639. }
  1640. break;
  1641. }
  1642. }
  1643. /* Remove duplicate manufacturer or product in the name */
  1644. for (i = 1; i < (len - 1); ++i) {
  1645. int matchlen = PrefixMatch(name, &name[i]);
  1646. if (matchlen > 0 && name[matchlen-1] == ' ') {
  1647. SDL_memmove(name, name+matchlen, len-matchlen+1);
  1648. break;
  1649. } else if (matchlen > 0 && name[matchlen] == ' ') {
  1650. SDL_memmove(name, name+matchlen+1, len-matchlen);
  1651. break;
  1652. }
  1653. }
  1654. return name;
  1655. }
  1656. SDL_GameControllerType
  1657. SDL_GetJoystickGameControllerTypeFromVIDPID(Uint16 vendor, Uint16 product, const char *name, SDL_bool forUI)
  1658. {
  1659. SDL_GameControllerType type = SDL_CONTROLLER_TYPE_UNKNOWN;
  1660. if (vendor == 0x0000 && product == 0x0000) {
  1661. /* Some devices are only identifiable by their name */
  1662. if (name &&
  1663. (SDL_strcmp(name, "Lic Pro Controller") == 0 ||
  1664. SDL_strcmp(name, "Nintendo Wireless Gamepad") == 0 ||
  1665. SDL_strcmp(name, "Wireless Gamepad") == 0)) {
  1666. /* HORI or PowerA Switch Pro Controller clone */
  1667. type = SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO;
  1668. }
  1669. } else if (vendor == 0x0001 && product == 0x0001) {
  1670. type = SDL_CONTROLLER_TYPE_UNKNOWN;
  1671. } else if (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX_ONE_XINPUT_CONTROLLER) {
  1672. type = SDL_CONTROLLER_TYPE_XBOXONE;
  1673. } else if ((vendor == USB_VENDOR_AMAZON && product == USB_PRODUCT_AMAZON_LUNA_CONTROLLER) ||
  1674. (vendor == BLUETOOTH_VENDOR_AMAZON && product == BLUETOOTH_PRODUCT_LUNA_CONTROLLER)) {
  1675. type = SDL_CONTROLLER_TYPE_AMAZON_LUNA;
  1676. } else if (vendor == USB_VENDOR_GOOGLE && product == USB_PRODUCT_GOOGLE_STADIA_CONTROLLER) {
  1677. type = SDL_CONTROLLER_TYPE_GOOGLE_STADIA;
  1678. } else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOY_CON_GRIP) {
  1679. type = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, SDL_FALSE) ? SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO : SDL_CONTROLLER_TYPE_UNKNOWN;
  1680. } else {
  1681. switch (GuessControllerType(vendor, product)) {
  1682. case k_eControllerType_XBox360Controller:
  1683. type = SDL_CONTROLLER_TYPE_XBOX360;
  1684. break;
  1685. case k_eControllerType_XBoxOneController:
  1686. type = SDL_CONTROLLER_TYPE_XBOXONE;
  1687. break;
  1688. case k_eControllerType_PS3Controller:
  1689. type = SDL_CONTROLLER_TYPE_PS3;
  1690. break;
  1691. case k_eControllerType_PS4Controller:
  1692. type = SDL_CONTROLLER_TYPE_PS4;
  1693. break;
  1694. case k_eControllerType_PS5Controller:
  1695. type = SDL_CONTROLLER_TYPE_PS5;
  1696. break;
  1697. case k_eControllerType_XInputPS4Controller:
  1698. if (forUI) {
  1699. type = SDL_CONTROLLER_TYPE_PS4;
  1700. } else {
  1701. type = SDL_CONTROLLER_TYPE_UNKNOWN;
  1702. }
  1703. break;
  1704. case k_eControllerType_SwitchProController:
  1705. case k_eControllerType_SwitchInputOnlyController:
  1706. type = SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO;
  1707. break;
  1708. case k_eControllerType_XInputSwitchController:
  1709. if (forUI) {
  1710. type = SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO;
  1711. } else {
  1712. type = SDL_CONTROLLER_TYPE_UNKNOWN;
  1713. }
  1714. break;
  1715. case k_eControllerType_SwitchJoyConLeft:
  1716. case k_eControllerType_SwitchJoyConRight:
  1717. /* We always support the Nintendo Online NES Controllers */
  1718. if (name && SDL_strncmp(name, "NES Controller", 14) == 0) {
  1719. type = SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO;
  1720. } else {
  1721. type = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, SDL_FALSE) ? SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO : SDL_CONTROLLER_TYPE_UNKNOWN;
  1722. }
  1723. break;
  1724. default:
  1725. break;
  1726. }
  1727. }
  1728. return type;
  1729. }
  1730. SDL_GameControllerType
  1731. SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name)
  1732. {
  1733. SDL_GameControllerType type;
  1734. Uint16 vendor, product;
  1735. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  1736. type = SDL_GetJoystickGameControllerTypeFromVIDPID(vendor, product, name, SDL_TRUE);
  1737. if (type == SDL_CONTROLLER_TYPE_UNKNOWN) {
  1738. if (SDL_IsJoystickXInput(guid)) {
  1739. /* This is probably an Xbox One controller */
  1740. return SDL_CONTROLLER_TYPE_XBOXONE;
  1741. }
  1742. if (SDL_IsJoystickVirtual(guid)) {
  1743. return SDL_CONTROLLER_TYPE_VIRTUAL;
  1744. }
  1745. }
  1746. return type;
  1747. }
  1748. SDL_bool
  1749. SDL_IsJoystickXboxOne(Uint16 vendor_id, Uint16 product_id)
  1750. {
  1751. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1752. return (eType == k_eControllerType_XBoxOneController);
  1753. }
  1754. SDL_bool
  1755. SDL_IsJoystickXboxOneElite(Uint16 vendor_id, Uint16 product_id)
  1756. {
  1757. if (vendor_id == USB_VENDOR_MICROSOFT) {
  1758. if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 ||
  1759. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2 ||
  1760. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
  1761. return SDL_TRUE;
  1762. }
  1763. }
  1764. return SDL_FALSE;
  1765. }
  1766. SDL_bool
  1767. SDL_IsJoystickXboxSeriesX(Uint16 vendor_id, Uint16 product_id)
  1768. {
  1769. if (vendor_id == USB_VENDOR_MICROSOFT) {
  1770. if (product_id == USB_PRODUCT_XBOX_SERIES_X ||
  1771. product_id == USB_PRODUCT_XBOX_SERIES_X_BLE) {
  1772. return SDL_TRUE;
  1773. }
  1774. }
  1775. if (vendor_id == USB_VENDOR_PDP) {
  1776. if (product_id == USB_PRODUCT_XBOX_SERIES_X_VICTRIX_GAMBIT ||
  1777. product_id == USB_PRODUCT_XBOX_SERIES_X_PDP_BLUE ||
  1778. product_id == USB_PRODUCT_XBOX_SERIES_X_PDP_AFTERGLOW) {
  1779. return SDL_TRUE;
  1780. }
  1781. }
  1782. if (vendor_id == USB_VENDOR_POWERA_ALT) {
  1783. if ((product_id >= 0x2001 && product_id <= 0x201a) ||
  1784. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO2 ||
  1785. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_SPECTRA) {
  1786. return SDL_TRUE;
  1787. }
  1788. }
  1789. return SDL_FALSE;
  1790. }
  1791. SDL_bool
  1792. SDL_IsJoystickBluetoothXboxOne(Uint16 vendor_id, Uint16 product_id)
  1793. {
  1794. if (vendor_id == USB_VENDOR_MICROSOFT) {
  1795. if (product_id == USB_PRODUCT_XBOX_ONE_ADAPTIVE_BLUETOOTH ||
  1796. product_id == USB_PRODUCT_XBOX_ONE_ADAPTIVE_BLE ||
  1797. product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
  1798. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
  1799. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLE ||
  1800. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH ||
  1801. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLE ||
  1802. product_id == USB_PRODUCT_XBOX_SERIES_X_BLE) {
  1803. return SDL_TRUE;
  1804. }
  1805. }
  1806. return SDL_FALSE;
  1807. }
  1808. SDL_bool
  1809. SDL_IsJoystickPS4(Uint16 vendor_id, Uint16 product_id)
  1810. {
  1811. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1812. return (eType == k_eControllerType_PS4Controller);
  1813. }
  1814. SDL_bool
  1815. SDL_IsJoystickPS5(Uint16 vendor_id, Uint16 product_id)
  1816. {
  1817. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1818. return (eType == k_eControllerType_PS5Controller);
  1819. }
  1820. SDL_bool
  1821. SDL_IsJoystickNintendoSwitchPro(Uint16 vendor_id, Uint16 product_id)
  1822. {
  1823. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1824. return (eType == k_eControllerType_SwitchProController ||
  1825. eType == k_eControllerType_SwitchInputOnlyController ||
  1826. (vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_SWITCH_JOY_CON_GRIP));
  1827. }
  1828. SDL_bool
  1829. SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor_id, Uint16 product_id)
  1830. {
  1831. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1832. return (eType == k_eControllerType_SwitchInputOnlyController);
  1833. }
  1834. SDL_bool
  1835. SDL_IsJoystickNintendoSwitchJoyCon(Uint16 vendor_id, Uint16 product_id)
  1836. {
  1837. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1838. return (eType == k_eControllerType_SwitchJoyConLeft ||
  1839. eType == k_eControllerType_SwitchJoyConRight);
  1840. }
  1841. SDL_bool
  1842. SDL_IsJoystickNintendoSwitchJoyConLeft(Uint16 vendor_id, Uint16 product_id)
  1843. {
  1844. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1845. return (eType == k_eControllerType_SwitchJoyConLeft);
  1846. }
  1847. SDL_bool
  1848. SDL_IsJoystickNintendoSwitchJoyConRight(Uint16 vendor_id, Uint16 product_id)
  1849. {
  1850. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1851. return (eType == k_eControllerType_SwitchJoyConRight);
  1852. }
  1853. SDL_bool
  1854. SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id)
  1855. {
  1856. EControllerType eType = GuessControllerType(vendor_id, product_id);
  1857. return (eType == k_eControllerType_SteamController ||
  1858. eType == k_eControllerType_SteamControllerV2);
  1859. }
  1860. SDL_bool
  1861. SDL_IsJoystickXInput(SDL_JoystickGUID guid)
  1862. {
  1863. return (guid.data[14] == 'x') ? SDL_TRUE : SDL_FALSE;
  1864. }
  1865. SDL_bool
  1866. SDL_IsJoystickWGI(SDL_JoystickGUID guid)
  1867. {
  1868. return (guid.data[14] == 'w') ? SDL_TRUE : SDL_FALSE;
  1869. }
  1870. SDL_bool
  1871. SDL_IsJoystickHIDAPI(SDL_JoystickGUID guid)
  1872. {
  1873. return (guid.data[14] == 'h') ? SDL_TRUE : SDL_FALSE;
  1874. }
  1875. SDL_bool
  1876. SDL_IsJoystickRAWINPUT(SDL_JoystickGUID guid)
  1877. {
  1878. return (guid.data[14] == 'r') ? SDL_TRUE : SDL_FALSE;
  1879. }
  1880. SDL_bool
  1881. SDL_IsJoystickVirtual(SDL_JoystickGUID guid)
  1882. {
  1883. return (guid.data[14] == 'v') ? SDL_TRUE : SDL_FALSE;
  1884. }
  1885. static SDL_bool SDL_IsJoystickProductWheel(Uint32 vidpid)
  1886. {
  1887. static Uint32 wheel_joysticks[] = {
  1888. MAKE_VIDPID(0x046d, 0xc294), /* Logitech generic wheel */
  1889. MAKE_VIDPID(0x046d, 0xc295), /* Logitech Momo Force */
  1890. MAKE_VIDPID(0x046d, 0xc298), /* Logitech Driving Force Pro */
  1891. MAKE_VIDPID(0x046d, 0xc299), /* Logitech G25 */
  1892. MAKE_VIDPID(0x046d, 0xc29a), /* Logitech Driving Force GT */
  1893. MAKE_VIDPID(0x046d, 0xc29b), /* Logitech G27 */
  1894. MAKE_VIDPID(0x046d, 0xc24f), /* Logitech G29 (PS3) */
  1895. MAKE_VIDPID(0x046d, 0xc260), /* Logitech G29 (PS4) */
  1896. MAKE_VIDPID(0x046d, 0xc261), /* Logitech G920 (initial mode) */
  1897. MAKE_VIDPID(0x046d, 0xc262), /* Logitech G920 (active mode) */
  1898. MAKE_VIDPID(0x046d, 0xc26e), /* Logitech G923 */
  1899. MAKE_VIDPID(0x046d, 0xca03), /* Logitech Momo Racing */
  1900. MAKE_VIDPID(0x044f, 0xb65d), /* Thrustmaster Wheel FFB */
  1901. MAKE_VIDPID(0x044f, 0xb66d), /* Thrustmaster Wheel FFB */
  1902. MAKE_VIDPID(0x044f, 0xb677), /* Thrustmaster T150 */
  1903. MAKE_VIDPID(0x044f, 0xb696), /* Thrustmaster T248 */
  1904. MAKE_VIDPID(0x044f, 0xb66e), /* Thrustmaster T300RS (normal mode) */
  1905. MAKE_VIDPID(0x044f, 0xb66f), /* Thrustmaster T300RS (advanced mode) */
  1906. MAKE_VIDPID(0x044f, 0xb66d), /* Thrustmaster T300RS (PS4 mode) */
  1907. MAKE_VIDPID(0x044f, 0xb65e), /* Thrustmaster T500RS */
  1908. MAKE_VIDPID(0x044f, 0xb664), /* Thrustmaster TX (initial mode) */
  1909. MAKE_VIDPID(0x044f, 0xb669), /* Thrustmaster TX (active mode) */
  1910. };
  1911. int i;
  1912. for (i = 0; i < SDL_arraysize(wheel_joysticks); ++i) {
  1913. if (vidpid == wheel_joysticks[i]) {
  1914. return SDL_TRUE;
  1915. }
  1916. }
  1917. return SDL_FALSE;
  1918. }
  1919. static SDL_bool SDL_IsJoystickProductArcadeStick(Uint32 vidpid)
  1920. {
  1921. static Uint32 arcadestick_joysticks[] = {
  1922. MAKE_VIDPID(0x0079, 0x181a), /* Venom Arcade Stick */
  1923. MAKE_VIDPID(0x0f0d, 0x006a), /* Real Arcade Pro 4 */
  1924. MAKE_VIDPID(0x0079, 0x181b), /* Venom Arcade Stick */
  1925. MAKE_VIDPID(0x0c12, 0x0ef6), /* Hitbox Arcade Stick */
  1926. MAKE_VIDPID(0x0f0d, 0x008a), /* HORI Real Arcade Pro 4 */
  1927. MAKE_VIDPID(0x0f0d, 0x0016), /* Hori Real Arcade Pro.EX */
  1928. MAKE_VIDPID(0x0f0d, 0x001b), /* Hori Real Arcade Pro VX */
  1929. MAKE_VIDPID(0x0f0d, 0x008c), /* Hori Real Arcade Pro 4 */
  1930. MAKE_VIDPID(0x1bad, 0xf03d), /* Street Fighter IV Arcade Stick TE - Chun Li */
  1931. MAKE_VIDPID(0x1bad, 0xf502), /* Hori Real Arcade Pro.VX SA */
  1932. MAKE_VIDPID(0x1bad, 0xf504), /* Hori Real Arcade Pro. EX */
  1933. MAKE_VIDPID(0x1bad, 0xf506), /* Hori Real Arcade Pro.EX Premium VLX */
  1934. MAKE_VIDPID(0x24c6, 0x5000), /* Razer Atrox Arcade Stick */
  1935. MAKE_VIDPID(0x24c6, 0x5501), /* Hori Real Arcade Pro VX-SA */
  1936. MAKE_VIDPID(0x24c6, 0x550e), /* Hori Real Arcade Pro V Kai 360 */
  1937. MAKE_VIDPID(0x0f0d, 0x0063), /* Hori Real Arcade Pro Hayabusa (USA) Xbox One */
  1938. MAKE_VIDPID(0x0f0d, 0x0078), /* Hori Real Arcade Pro V Kai Xbox One */
  1939. MAKE_VIDPID(0x1532, 0x0a00), /* Razer Atrox Arcade Stick */
  1940. MAKE_VIDPID(0x0f0d, 0x00aa), /* HORI Real Arcade Pro V Hayabusa in Switch Mode */
  1941. MAKE_VIDPID(0x20d6, 0xa715), /* PowerA Nintendo Switch Fusion Arcade Stick */
  1942. MAKE_VIDPID(0x2c22, 0x2300), /* Qanba Obsidian Arcade Joystick in PS4 Mode */
  1943. MAKE_VIDPID(0x2c22, 0x2302), /* Qanba Obsidian Arcade Joystick in PS3 Mode */
  1944. MAKE_VIDPID(0x2c22, 0x2303), /* Qanba Obsidian Arcade Joystick in PC Mode */
  1945. MAKE_VIDPID(0x2c22, 0x2500), /* Qanba Dragon Arcade Joystick in PS4 Mode */
  1946. MAKE_VIDPID(0x2c22, 0x2502), /* Qanba Dragon Arcade Joystick in PS3 Mode */
  1947. MAKE_VIDPID(0x2c22, 0x2503), /* Qanba Dragon Arcade Joystick in PC Mode */
  1948. };
  1949. int i;
  1950. for (i = 0; i < SDL_arraysize(arcadestick_joysticks); ++i) {
  1951. if (vidpid == arcadestick_joysticks[i]) {
  1952. return SDL_TRUE;
  1953. }
  1954. }
  1955. return SDL_FALSE;
  1956. }
  1957. static SDL_bool SDL_IsJoystickProductFlightStick(Uint32 vidpid)
  1958. {
  1959. static Uint32 flightstick_joysticks[] = {
  1960. MAKE_VIDPID(0x044f, 0x0402), /* HOTAS Warthog Joystick */
  1961. MAKE_VIDPID(0x0738, 0x2221), /* Saitek Pro Flight X-56 Rhino Stick */
  1962. MAKE_VIDPID(0x044f, 0xb10a), /* ThrustMaster, Inc. T.16000M Joystick */
  1963. };
  1964. int i;
  1965. for (i = 0; i < SDL_arraysize(flightstick_joysticks); ++i) {
  1966. if (vidpid == flightstick_joysticks[i]) {
  1967. return SDL_TRUE;
  1968. }
  1969. }
  1970. return SDL_FALSE;
  1971. }
  1972. static SDL_bool SDL_IsJoystickProductThrottle(Uint32 vidpid)
  1973. {
  1974. static Uint32 throttle_joysticks[] = {
  1975. MAKE_VIDPID(0x044f, 0x0404), /* HOTAS Warthog Throttle */
  1976. MAKE_VIDPID(0x0738, 0xa221), /* Saitek Pro Flight X-56 Rhino Throttle */
  1977. };
  1978. int i;
  1979. for (i = 0; i < SDL_arraysize(throttle_joysticks); ++i) {
  1980. if (vidpid == throttle_joysticks[i]) {
  1981. return SDL_TRUE;
  1982. }
  1983. }
  1984. return SDL_FALSE;
  1985. }
  1986. static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
  1987. {
  1988. Uint16 vendor;
  1989. Uint16 product;
  1990. Uint32 vidpid;
  1991. if (SDL_IsJoystickXInput(guid)) {
  1992. /* XInput GUID, get the type based on the XInput device subtype */
  1993. switch (guid.data[15]) {
  1994. case 0x01: /* XINPUT_DEVSUBTYPE_GAMEPAD */
  1995. return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  1996. case 0x02: /* XINPUT_DEVSUBTYPE_WHEEL */
  1997. return SDL_JOYSTICK_TYPE_WHEEL;
  1998. case 0x03: /* XINPUT_DEVSUBTYPE_ARCADE_STICK */
  1999. return SDL_JOYSTICK_TYPE_ARCADE_STICK;
  2000. case 0x04: /* XINPUT_DEVSUBTYPE_FLIGHT_STICK */
  2001. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  2002. case 0x05: /* XINPUT_DEVSUBTYPE_DANCE_PAD */
  2003. return SDL_JOYSTICK_TYPE_DANCE_PAD;
  2004. case 0x06: /* XINPUT_DEVSUBTYPE_GUITAR */
  2005. case 0x07: /* XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE */
  2006. case 0x0B: /* XINPUT_DEVSUBTYPE_GUITAR_BASS */
  2007. return SDL_JOYSTICK_TYPE_GUITAR;
  2008. case 0x08: /* XINPUT_DEVSUBTYPE_DRUM_KIT */
  2009. return SDL_JOYSTICK_TYPE_DRUM_KIT;
  2010. case 0x13: /* XINPUT_DEVSUBTYPE_ARCADE_PAD */
  2011. return SDL_JOYSTICK_TYPE_ARCADE_PAD;
  2012. default:
  2013. return SDL_JOYSTICK_TYPE_UNKNOWN;
  2014. }
  2015. }
  2016. if (SDL_IsJoystickWGI(guid)) {
  2017. return (SDL_JoystickType)guid.data[15];
  2018. }
  2019. if (SDL_IsJoystickVirtual(guid)) {
  2020. return (SDL_JoystickType)guid.data[15];
  2021. }
  2022. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  2023. vidpid = MAKE_VIDPID(vendor, product);
  2024. if (SDL_IsJoystickProductWheel(vidpid)) {
  2025. return SDL_JOYSTICK_TYPE_WHEEL;
  2026. }
  2027. if (SDL_IsJoystickProductArcadeStick(vidpid)) {
  2028. return SDL_JOYSTICK_TYPE_ARCADE_STICK;
  2029. }
  2030. if (SDL_IsJoystickProductFlightStick(vidpid)) {
  2031. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  2032. }
  2033. if (SDL_IsJoystickProductThrottle(vidpid)) {
  2034. return SDL_JOYSTICK_TYPE_THROTTLE;
  2035. }
  2036. if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) {
  2037. return SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  2038. }
  2039. return SDL_JOYSTICK_TYPE_UNKNOWN;
  2040. }
  2041. static SDL_bool SDL_IsPS4RemapperRunning(void)
  2042. {
  2043. #ifdef __WIN32__
  2044. const char *mapper_processes[] = {
  2045. "DS4Windows.exe",
  2046. "InputMapper.exe",
  2047. };
  2048. int i;
  2049. PROCESSENTRY32 pe32;
  2050. SDL_bool found = SDL_FALSE;
  2051. /* Take a snapshot of all processes in the system */
  2052. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  2053. if (hProcessSnap != INVALID_HANDLE_VALUE) {
  2054. pe32.dwSize = sizeof(PROCESSENTRY32);
  2055. if (Process32First(hProcessSnap, &pe32)) {
  2056. do
  2057. {
  2058. for (i = 0; i < SDL_arraysize(mapper_processes); ++i) {
  2059. if (SDL_strcasecmp(pe32.szExeFile, mapper_processes[i]) == 0) {
  2060. found = SDL_TRUE;
  2061. }
  2062. }
  2063. } while (Process32Next(hProcessSnap, &pe32) && !found);
  2064. }
  2065. CloseHandle(hProcessSnap);
  2066. }
  2067. return found;
  2068. #else
  2069. return SDL_FALSE;
  2070. #endif
  2071. }
  2072. SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid)
  2073. {
  2074. /* This list is taken from:
  2075. https://raw.githubusercontent.com/denilsonsa/udev-joystick-blacklist/master/generate_rules.py
  2076. */
  2077. static Uint32 joystick_blacklist[] = {
  2078. /* Microsoft Microsoft Wireless Optical Desktop 2.10 */
  2079. /* Microsoft Wireless Desktop - Comfort Edition */
  2080. MAKE_VIDPID(0x045e, 0x009d),
  2081. /* Microsoft Microsoft Digital Media Pro Keyboard */
  2082. /* Microsoft Corp. Digital Media Pro Keyboard */
  2083. MAKE_VIDPID(0x045e, 0x00b0),
  2084. /* Microsoft Microsoft Digital Media Keyboard */
  2085. /* Microsoft Corp. Digital Media Keyboard 1.0A */
  2086. MAKE_VIDPID(0x045e, 0x00b4),
  2087. /* Microsoft Microsoft Digital Media Keyboard 3000 */
  2088. MAKE_VIDPID(0x045e, 0x0730),
  2089. /* Microsoft Microsoft 2.4GHz Transceiver v6.0 */
  2090. /* Microsoft Microsoft 2.4GHz Transceiver v8.0 */
  2091. /* Microsoft Corp. Nano Transceiver v1.0 for Bluetooth */
  2092. /* Microsoft Wireless Mobile Mouse 1000 */
  2093. /* Microsoft Wireless Desktop 3000 */
  2094. MAKE_VIDPID(0x045e, 0x0745),
  2095. /* Microsoft SideWinder(TM) 2.4GHz Transceiver */
  2096. MAKE_VIDPID(0x045e, 0x0748),
  2097. /* Microsoft Corp. Wired Keyboard 600 */
  2098. MAKE_VIDPID(0x045e, 0x0750),
  2099. /* Microsoft Corp. Sidewinder X4 keyboard */
  2100. MAKE_VIDPID(0x045e, 0x0768),
  2101. /* Microsoft Corp. Arc Touch Mouse Transceiver */
  2102. MAKE_VIDPID(0x045e, 0x0773),
  2103. /* Microsoft 2.4GHz Transceiver v9.0 */
  2104. /* Microsoft Nano Transceiver v2.1 */
  2105. /* Microsoft Sculpt Ergonomic Keyboard (5KV-00001) */
  2106. MAKE_VIDPID(0x045e, 0x07a5),
  2107. /* Microsoft Nano Transceiver v1.0 */
  2108. /* Microsoft Wireless Keyboard 800 */
  2109. MAKE_VIDPID(0x045e, 0x07b2),
  2110. /* Microsoft Nano Transceiver v2.0 */
  2111. MAKE_VIDPID(0x045e, 0x0800),
  2112. MAKE_VIDPID(0x046d, 0xc30a), /* Logitech, Inc. iTouch Composite keboard */
  2113. MAKE_VIDPID(0x04d9, 0xa0df), /* Tek Syndicate Mouse (E-Signal USB Gaming Mouse) */
  2114. /* List of Wacom devices at: http://linuxwacom.sourceforge.net/wiki/index.php/Device_IDs */
  2115. MAKE_VIDPID(0x056a, 0x0010), /* Wacom ET-0405 Graphire */
  2116. MAKE_VIDPID(0x056a, 0x0011), /* Wacom ET-0405A Graphire2 (4x5) */
  2117. MAKE_VIDPID(0x056a, 0x0012), /* Wacom ET-0507A Graphire2 (5x7) */
  2118. MAKE_VIDPID(0x056a, 0x0013), /* Wacom CTE-430 Graphire3 (4x5) */
  2119. MAKE_VIDPID(0x056a, 0x0014), /* Wacom CTE-630 Graphire3 (6x8) */
  2120. MAKE_VIDPID(0x056a, 0x0015), /* Wacom CTE-440 Graphire4 (4x5) */
  2121. MAKE_VIDPID(0x056a, 0x0016), /* Wacom CTE-640 Graphire4 (6x8) */
  2122. MAKE_VIDPID(0x056a, 0x0017), /* Wacom CTE-450 Bamboo Fun (4x5) */
  2123. MAKE_VIDPID(0x056a, 0x0018), /* Wacom CTE-650 Bamboo Fun 6x8 */
  2124. MAKE_VIDPID(0x056a, 0x0019), /* Wacom CTE-631 Bamboo One */
  2125. MAKE_VIDPID(0x056a, 0x00d1), /* Wacom Bamboo Pen and Touch CTH-460 */
  2126. MAKE_VIDPID(0x056a, 0x030e), /* Wacom Intuos Pen (S) CTL-480 */
  2127. MAKE_VIDPID(0x09da, 0x054f), /* A4 Tech Co., G7 750 mouse */
  2128. MAKE_VIDPID(0x09da, 0x1410), /* A4 Tech Co., Ltd Bloody AL9 mouse */
  2129. MAKE_VIDPID(0x09da, 0x3043), /* A4 Tech Co., Ltd Bloody R8A Gaming Mouse */
  2130. MAKE_VIDPID(0x09da, 0x31b5), /* A4 Tech Co., Ltd Bloody TL80 Terminator Laser Gaming Mouse */
  2131. MAKE_VIDPID(0x09da, 0x3997), /* A4 Tech Co., Ltd Bloody RT7 Terminator Wireless */
  2132. MAKE_VIDPID(0x09da, 0x3f8b), /* A4 Tech Co., Ltd Bloody V8 mouse */
  2133. MAKE_VIDPID(0x09da, 0x51f4), /* Modecom MC-5006 Keyboard */
  2134. MAKE_VIDPID(0x09da, 0x5589), /* A4 Tech Co., Ltd Terminator TL9 Laser Gaming Mouse */
  2135. MAKE_VIDPID(0x09da, 0x7b22), /* A4 Tech Co., Ltd Bloody V5 */
  2136. MAKE_VIDPID(0x09da, 0x7f2d), /* A4 Tech Co., Ltd Bloody R3 mouse */
  2137. MAKE_VIDPID(0x09da, 0x8090), /* A4 Tech Co., Ltd X-718BK Oscar Optical Gaming Mouse */
  2138. MAKE_VIDPID(0x09da, 0x9033), /* A4 Tech Co., X7 X-705K */
  2139. MAKE_VIDPID(0x09da, 0x9066), /* A4 Tech Co., Sharkoon Fireglider Optical */
  2140. MAKE_VIDPID(0x09da, 0x9090), /* A4 Tech Co., Ltd XL-730K / XL-750BK / XL-755BK Laser Mouse */
  2141. MAKE_VIDPID(0x09da, 0x90c0), /* A4 Tech Co., Ltd X7 G800V keyboard */
  2142. MAKE_VIDPID(0x09da, 0xf012), /* A4 Tech Co., Ltd Bloody V7 mouse */
  2143. MAKE_VIDPID(0x09da, 0xf32a), /* A4 Tech Co., Ltd Bloody B540 keyboard */
  2144. MAKE_VIDPID(0x09da, 0xf613), /* A4 Tech Co., Ltd Bloody V2 mouse */
  2145. MAKE_VIDPID(0x09da, 0xf624), /* A4 Tech Co., Ltd Bloody B120 Keyboard */
  2146. MAKE_VIDPID(0x1b1c, 0x1b3c), /* Corsair Harpoon RGB gaming mouse */
  2147. MAKE_VIDPID(0x1d57, 0xad03), /* [T3] 2.4GHz and IR Air Mouse Remote Control */
  2148. MAKE_VIDPID(0x1e7d, 0x2e4a), /* Roccat Tyon Mouse */
  2149. MAKE_VIDPID(0x20a0, 0x422d), /* Winkeyless.kr Keyboards */
  2150. MAKE_VIDPID(0x2516, 0x001f), /* Cooler Master Storm Mizar Mouse */
  2151. MAKE_VIDPID(0x2516, 0x0028), /* Cooler Master Storm Alcor Mouse */
  2152. /*****************************************************************/
  2153. /* Additional entries */
  2154. /*****************************************************************/
  2155. MAKE_VIDPID(0x04d9, 0x8008), /* OBINLB USB-HID Keyboard (Anne Pro II) */
  2156. MAKE_VIDPID(0x04d9, 0x8009), /* OBINLB USB-HID Keyboard (Anne Pro II) */
  2157. MAKE_VIDPID(0x04d9, 0xa292), /* OBINLB USB-HID Keyboard (Anne Pro II) */
  2158. MAKE_VIDPID(0x04d9, 0xa293), /* OBINLB USB-HID Keyboard (Anne Pro II) */
  2159. MAKE_VIDPID(0x1532, 0x0266), /* Razer Huntman V2 Analog, non-functional DInput device */
  2160. MAKE_VIDPID(0x1532, 0x0282), /* Razer Huntman Mini Analog, non-functional DInput device */
  2161. MAKE_VIDPID(0x26ce, 0x01a2), /* ASRock LED Controller */
  2162. };
  2163. static Uint32 rog_chakram_list[] = {
  2164. MAKE_VIDPID(0x0b05, 0x1958), /* ROG Chakram Core Mouse */
  2165. MAKE_VIDPID(0x0b05, 0x18e3), /* ROG Chakram (wired) Mouse */
  2166. MAKE_VIDPID(0x0b05, 0x18e5), /* ROG Chakram (wireless) Mouse */
  2167. MAKE_VIDPID(0x0b05, 0x1a18), /* ROG Chakram X (wired) Mouse */
  2168. MAKE_VIDPID(0x0b05, 0x1a1a), /* ROG Chakram X (wireless) Mouse */
  2169. MAKE_VIDPID(0x0b05, 0x1a1c), /* ROG Chakram X (Bluetooth) Mouse */
  2170. };
  2171. unsigned int i;
  2172. Uint32 id;
  2173. Uint16 vendor;
  2174. Uint16 product;
  2175. SDL_GameControllerType type;
  2176. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
  2177. /* Check the joystick blacklist */
  2178. id = MAKE_VIDPID(vendor, product);
  2179. for (i = 0; i < SDL_arraysize(joystick_blacklist); ++i) {
  2180. if (id == joystick_blacklist[i]) {
  2181. return SDL_TRUE;
  2182. }
  2183. }
  2184. if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_ROG_CHAKRAM, SDL_FALSE)) {
  2185. for (i = 0; i < SDL_arraysize(rog_chakram_list); ++i) {
  2186. if (id == rog_chakram_list[i]) {
  2187. return SDL_TRUE;
  2188. }
  2189. }
  2190. }
  2191. type = SDL_GetJoystickGameControllerTypeFromVIDPID(vendor, product, name, SDL_FALSE);
  2192. if ((type == SDL_CONTROLLER_TYPE_PS4 || type == SDL_CONTROLLER_TYPE_PS5) && SDL_IsPS4RemapperRunning()) {
  2193. return SDL_TRUE;
  2194. }
  2195. if (SDL_ShouldIgnoreGameController(name, guid)) {
  2196. return SDL_TRUE;
  2197. }
  2198. return SDL_FALSE;
  2199. }
  2200. /* return the guid for this index */
  2201. SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
  2202. {
  2203. SDL_JoystickDriver *driver;
  2204. SDL_JoystickGUID guid;
  2205. SDL_LockJoysticks();
  2206. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  2207. guid = driver->GetDeviceGUID(device_index);
  2208. } else {
  2209. SDL_zero(guid);
  2210. }
  2211. SDL_UnlockJoysticks();
  2212. return guid;
  2213. }
  2214. Uint16 SDL_JoystickGetDeviceVendor(int device_index)
  2215. {
  2216. Uint16 vendor;
  2217. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  2218. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
  2219. return vendor;
  2220. }
  2221. Uint16 SDL_JoystickGetDeviceProduct(int device_index)
  2222. {
  2223. Uint16 product;
  2224. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  2225. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
  2226. return product;
  2227. }
  2228. Uint16 SDL_JoystickGetDeviceProductVersion(int device_index)
  2229. {
  2230. Uint16 version;
  2231. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  2232. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
  2233. return version;
  2234. }
  2235. SDL_JoystickType SDL_JoystickGetDeviceType(int device_index)
  2236. {
  2237. SDL_JoystickType type;
  2238. SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
  2239. type = SDL_GetJoystickGUIDType(guid);
  2240. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  2241. if (SDL_IsGameController(device_index)) {
  2242. type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  2243. }
  2244. }
  2245. return type;
  2246. }
  2247. SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
  2248. {
  2249. SDL_JoystickDriver *driver;
  2250. SDL_JoystickID instance_id = -1;
  2251. SDL_LockJoysticks();
  2252. if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
  2253. instance_id = driver->GetDeviceInstanceID(device_index);
  2254. }
  2255. SDL_UnlockJoysticks();
  2256. return instance_id;
  2257. }
  2258. int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id)
  2259. {
  2260. int i, num_joysticks, device_index = -1;
  2261. SDL_LockJoysticks();
  2262. num_joysticks = SDL_NumJoysticks();
  2263. for (i = 0; i < num_joysticks; ++i) {
  2264. if (SDL_JoystickGetDeviceInstanceID(i) == instance_id) {
  2265. device_index = i;
  2266. break;
  2267. }
  2268. }
  2269. SDL_UnlockJoysticks();
  2270. return device_index;
  2271. }
  2272. SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
  2273. {
  2274. if (!SDL_PrivateJoystickValid(joystick)) {
  2275. SDL_JoystickGUID emptyGUID;
  2276. SDL_zero(emptyGUID);
  2277. return emptyGUID;
  2278. }
  2279. return joystick->guid;
  2280. }
  2281. Uint16 SDL_JoystickGetVendor(SDL_Joystick *joystick)
  2282. {
  2283. Uint16 vendor;
  2284. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  2285. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
  2286. return vendor;
  2287. }
  2288. Uint16 SDL_JoystickGetProduct(SDL_Joystick *joystick)
  2289. {
  2290. Uint16 product;
  2291. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  2292. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
  2293. return product;
  2294. }
  2295. Uint16 SDL_JoystickGetProductVersion(SDL_Joystick *joystick)
  2296. {
  2297. Uint16 version;
  2298. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  2299. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
  2300. return version;
  2301. }
  2302. Uint16 SDL_JoystickGetFirmwareVersion(SDL_Joystick *joystick)
  2303. {
  2304. if (!SDL_PrivateJoystickValid(joystick)) {
  2305. return 0;
  2306. }
  2307. return joystick->firmware_version;
  2308. }
  2309. const char *SDL_JoystickGetSerial(SDL_Joystick *joystick)
  2310. {
  2311. if (!SDL_PrivateJoystickValid(joystick)) {
  2312. return NULL;
  2313. }
  2314. return joystick->serial;
  2315. }
  2316. SDL_JoystickType SDL_JoystickGetType(SDL_Joystick *joystick)
  2317. {
  2318. SDL_JoystickType type;
  2319. SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
  2320. type = SDL_GetJoystickGUIDType(guid);
  2321. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  2322. if (joystick && joystick->is_game_controller) {
  2323. type = SDL_JOYSTICK_TYPE_GAMECONTROLLER;
  2324. }
  2325. }
  2326. return type;
  2327. }
  2328. /* convert the guid to a printable string */
  2329. void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
  2330. {
  2331. SDL_GUIDToString(guid, pszGUID, cbGUID);
  2332. }
  2333. /* convert the string version of a joystick guid to the struct */
  2334. SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
  2335. {
  2336. return SDL_GUIDFromString(pchGUID);
  2337. }
  2338. /* update the power level for this joystick */
  2339. void SDL_PrivateJoystickBatteryLevel(SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel)
  2340. {
  2341. SDL_assert(joystick->ref_count); /* make sure we are calling this only for update, not for initialisation */
  2342. if (ePowerLevel != joystick->epowerlevel) {
  2343. #if !SDL_EVENTS_DISABLED
  2344. if (SDL_GetEventState(SDL_JOYBATTERYUPDATED) == SDL_ENABLE) {
  2345. SDL_Event event;
  2346. event.type = SDL_JOYBATTERYUPDATED;
  2347. event.jbattery.which = joystick->instance_id;
  2348. event.jbattery.level = ePowerLevel;
  2349. SDL_PushEvent(&event);
  2350. }
  2351. #endif /* !SDL_EVENTS_DISABLED */
  2352. joystick->epowerlevel = ePowerLevel;
  2353. }
  2354. }
  2355. /* return its power level */
  2356. SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick *joystick)
  2357. {
  2358. if (!SDL_PrivateJoystickValid(joystick)) {
  2359. return SDL_JOYSTICK_POWER_UNKNOWN;
  2360. }
  2361. return joystick->epowerlevel;
  2362. }
  2363. int SDL_PrivateJoystickTouchpad(SDL_Joystick *joystick, int touchpad, int finger, Uint8 state, float x, float y, float pressure)
  2364. {
  2365. SDL_JoystickTouchpadInfo *touchpad_info;
  2366. SDL_JoystickTouchpadFingerInfo *finger_info;
  2367. int posted;
  2368. Uint32 event_type;
  2369. if (touchpad < 0 || touchpad >= joystick->ntouchpads) {
  2370. return 0;
  2371. }
  2372. touchpad_info = &joystick->touchpads[touchpad];
  2373. if (finger < 0 || finger >= touchpad_info->nfingers) {
  2374. return 0;
  2375. }
  2376. finger_info = &touchpad_info->fingers[finger];
  2377. if (!state) {
  2378. if (x == 0.0f && y == 0.0f) {
  2379. x = finger_info->x;
  2380. y = finger_info->y;
  2381. }
  2382. pressure = 0.0f;
  2383. }
  2384. if (x < 0.0f) {
  2385. x = 0.0f;
  2386. } else if (x > 1.0f) {
  2387. x = 1.0f;
  2388. }
  2389. if (y < 0.0f) {
  2390. y = 0.0f;
  2391. } else if (y > 1.0f) {
  2392. y = 1.0f;
  2393. }
  2394. if (pressure < 0.0f) {
  2395. pressure = 0.0f;
  2396. } else if (pressure > 1.0f) {
  2397. pressure = 1.0f;
  2398. }
  2399. if (state == finger_info->state) {
  2400. if (!state ||
  2401. (x == finger_info->x && y == finger_info->y && pressure == finger_info->pressure)) {
  2402. return 0;
  2403. }
  2404. }
  2405. if (state == finger_info->state) {
  2406. event_type = SDL_CONTROLLERTOUCHPADMOTION;
  2407. } else if (state) {
  2408. event_type = SDL_CONTROLLERTOUCHPADDOWN;
  2409. } else {
  2410. event_type = SDL_CONTROLLERTOUCHPADUP;
  2411. }
  2412. /* We ignore events if we don't have keyboard focus, except for touch release */
  2413. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  2414. if (event_type != SDL_CONTROLLERTOUCHPADUP) {
  2415. return 0;
  2416. }
  2417. }
  2418. /* Update internal joystick state */
  2419. finger_info->state = state;
  2420. finger_info->x = x;
  2421. finger_info->y = y;
  2422. finger_info->pressure = pressure;
  2423. /* Post the event, if desired */
  2424. posted = 0;
  2425. #if !SDL_EVENTS_DISABLED
  2426. if (SDL_GetEventState(event_type) == SDL_ENABLE) {
  2427. SDL_Event event;
  2428. event.type = event_type;
  2429. event.ctouchpad.which = joystick->instance_id;
  2430. event.ctouchpad.touchpad = touchpad;
  2431. event.ctouchpad.finger = finger;
  2432. event.ctouchpad.x = x;
  2433. event.ctouchpad.y = y;
  2434. event.ctouchpad.pressure = pressure;
  2435. posted = SDL_PushEvent(&event) == 1;
  2436. }
  2437. #endif /* !SDL_EVENTS_DISABLED */
  2438. return posted;
  2439. }
  2440. int SDL_PrivateJoystickSensor(SDL_Joystick *joystick, SDL_SensorType type, const float *data, int num_values)
  2441. {
  2442. int i;
  2443. int posted = 0;
  2444. /* We ignore events if we don't have keyboard focus */
  2445. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  2446. return 0;
  2447. }
  2448. for (i = 0; i < joystick->nsensors; ++i) {
  2449. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  2450. if (sensor->type == type) {
  2451. if (sensor->enabled) {
  2452. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  2453. /* Update internal sensor state */
  2454. SDL_memcpy(sensor->data, data, num_values*sizeof(*data));
  2455. /* Post the event, if desired */
  2456. #if !SDL_EVENTS_DISABLED
  2457. if (SDL_GetEventState(SDL_CONTROLLERSENSORUPDATE) == SDL_ENABLE) {
  2458. SDL_Event event;
  2459. event.type = SDL_CONTROLLERSENSORUPDATE;
  2460. event.csensor.which = joystick->instance_id;
  2461. event.csensor.sensor = type;
  2462. num_values = SDL_min(num_values, SDL_arraysize(event.csensor.data));
  2463. SDL_memset(event.csensor.data, 0, sizeof(event.csensor.data));
  2464. SDL_memcpy(event.csensor.data, data, num_values*sizeof(*data));
  2465. posted = SDL_PushEvent(&event) == 1;
  2466. }
  2467. #endif /* !SDL_EVENTS_DISABLED */
  2468. }
  2469. break;
  2470. }
  2471. }
  2472. return posted;
  2473. }
  2474. /* vi: set ts=4 sw=4 expandtab: */