SDL_joystick.c 85 KB

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