SDL_gamepad.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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 gamepad API for Simple DirectMedia Layer
  20. #include "SDL_sysjoystick.h"
  21. #include "SDL_joystick_c.h"
  22. #include "SDL_steam_virtual_gamepad.h"
  23. #include "SDL_gamepad_c.h"
  24. #include "SDL_gamepad_db.h"
  25. #include "controller_type.h"
  26. #include "usb_ids.h"
  27. #include "hidapi/SDL_hidapi_nintendo.h"
  28. #include "../events/SDL_events_c.h"
  29. #ifdef SDL_PLATFORM_ANDROID
  30. #endif
  31. // Many gamepads turn the center button into an instantaneous button press
  32. #define SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS 250
  33. #define SDL_GAMEPAD_CRC_FIELD "crc:"
  34. #define SDL_GAMEPAD_CRC_FIELD_SIZE 4 // hard-coded for speed
  35. #define SDL_GAMEPAD_TYPE_FIELD "type:"
  36. #define SDL_GAMEPAD_TYPE_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_TYPE_FIELD)
  37. #define SDL_GAMEPAD_FACE_FIELD "face:"
  38. #define SDL_GAMEPAD_FACE_FIELD_SIZE 5 // hard-coded for speed
  39. #define SDL_GAMEPAD_PLATFORM_FIELD "platform:"
  40. #define SDL_GAMEPAD_PLATFORM_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_PLATFORM_FIELD)
  41. #define SDL_GAMEPAD_HINT_FIELD "hint:"
  42. #define SDL_GAMEPAD_HINT_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_HINT_FIELD)
  43. #define SDL_GAMEPAD_SDKGE_FIELD "sdk>=:"
  44. #define SDL_GAMEPAD_SDKGE_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_SDKGE_FIELD)
  45. #define SDL_GAMEPAD_SDKLE_FIELD "sdk<=:"
  46. #define SDL_GAMEPAD_SDKLE_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_SDKLE_FIELD)
  47. static bool SDL_gamepads_initialized;
  48. static SDL_Gamepad *SDL_gamepads SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  49. // The face button style of a gamepad
  50. typedef enum
  51. {
  52. SDL_GAMEPAD_FACE_STYLE_UNKNOWN,
  53. SDL_GAMEPAD_FACE_STYLE_ABXY,
  54. SDL_GAMEPAD_FACE_STYLE_BAYX,
  55. SDL_GAMEPAD_FACE_STYLE_SONY,
  56. } SDL_GamepadFaceStyle;
  57. // our hard coded list of mapping support
  58. typedef enum
  59. {
  60. SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT,
  61. SDL_GAMEPAD_MAPPING_PRIORITY_API,
  62. SDL_GAMEPAD_MAPPING_PRIORITY_USER,
  63. } SDL_GamepadMappingPriority;
  64. #define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
  65. typedef struct GamepadMapping_t
  66. {
  67. SDL_GUID guid _guarded;
  68. char *name _guarded;
  69. char *mapping _guarded;
  70. SDL_GamepadMappingPriority priority _guarded;
  71. struct GamepadMapping_t *next _guarded;
  72. } GamepadMapping_t;
  73. typedef struct
  74. {
  75. int refcount _guarded;
  76. SDL_JoystickID *joysticks _guarded;
  77. GamepadMapping_t **joystick_mappings _guarded;
  78. int num_changed_mappings _guarded;
  79. GamepadMapping_t **changed_mappings _guarded;
  80. } MappingChangeTracker;
  81. #undef _guarded
  82. static SDL_GUID s_zeroGUID;
  83. static GamepadMapping_t *s_pSupportedGamepads SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  84. static GamepadMapping_t *s_pDefaultMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  85. static GamepadMapping_t *s_pXInputMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  86. static MappingChangeTracker *s_mappingChangeTracker SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  87. static SDL_HashTable *s_gamepadInstanceIDs SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  88. #define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
  89. // The SDL gamepad structure
  90. struct SDL_Gamepad
  91. {
  92. SDL_Joystick *joystick _guarded; // underlying joystick device
  93. int ref_count _guarded;
  94. const char *name _guarded;
  95. SDL_GamepadType type _guarded;
  96. SDL_GamepadFaceStyle face_style _guarded;
  97. GamepadMapping_t *mapping _guarded;
  98. int num_bindings _guarded;
  99. SDL_GamepadBinding *bindings _guarded;
  100. SDL_GamepadBinding **last_match_axis _guarded;
  101. Uint8 *last_hat_mask _guarded;
  102. Uint64 guide_button_down _guarded;
  103. struct SDL_Gamepad *next _guarded; // pointer to next gamepad we have allocated
  104. };
  105. #undef _guarded
  106. #define CHECK_GAMEPAD_MAGIC(gamepad, result) \
  107. if (!SDL_ObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD) || \
  108. !SDL_IsJoystickValid(gamepad->joystick)) { \
  109. SDL_InvalidParamError("gamepad"); \
  110. SDL_UnlockJoysticks(); \
  111. return result; \
  112. }
  113. static SDL_vidpid_list SDL_allowed_gamepads = {
  114. SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT, 0, 0, NULL,
  115. NULL, 0, 0, NULL,
  116. 0, NULL,
  117. false
  118. };
  119. static SDL_vidpid_list SDL_ignored_gamepads = {
  120. SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES, 0, 0, NULL,
  121. NULL, 0, 0, NULL,
  122. 0, NULL,
  123. false
  124. };
  125. static GamepadMapping_t *SDL_PrivateAddMappingForGUID(SDL_GUID jGUID, const char *mappingString, bool *existing, SDL_GamepadMappingPriority priority);
  126. static void SDL_PrivateLoadButtonMapping(SDL_Gamepad *gamepad, GamepadMapping_t *pGamepadMapping);
  127. static GamepadMapping_t *SDL_PrivateGetGamepadMapping(SDL_JoystickID instance_id, bool create_mapping);
  128. static void SDL_SendGamepadAxis(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadAxis axis, Sint16 value);
  129. static void SDL_SendGamepadButton(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadButton button, bool down);
  130. static bool HasSameOutput(SDL_GamepadBinding *a, SDL_GamepadBinding *b)
  131. {
  132. if (a->output_type != b->output_type) {
  133. return false;
  134. }
  135. if (a->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
  136. return a->output.axis.axis == b->output.axis.axis;
  137. } else {
  138. return a->output.button == b->output.button;
  139. }
  140. }
  141. static void ResetOutput(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadBinding *bind)
  142. {
  143. if (bind->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
  144. SDL_SendGamepadAxis(timestamp, gamepad, bind->output.axis.axis, 0);
  145. } else {
  146. SDL_SendGamepadButton(timestamp, gamepad, bind->output.button, false);
  147. }
  148. }
  149. static void HandleJoystickAxis(Uint64 timestamp, SDL_Gamepad *gamepad, int axis, int value)
  150. {
  151. int i;
  152. SDL_GamepadBinding *last_match;
  153. SDL_GamepadBinding *match = NULL;
  154. SDL_AssertJoysticksLocked();
  155. last_match = gamepad->last_match_axis[axis];
  156. for (i = 0; i < gamepad->num_bindings; ++i) {
  157. SDL_GamepadBinding *binding = &gamepad->bindings[i];
  158. if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS &&
  159. axis == binding->input.axis.axis) {
  160. if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
  161. if (value >= binding->input.axis.axis_min &&
  162. value <= binding->input.axis.axis_max) {
  163. match = binding;
  164. break;
  165. }
  166. } else {
  167. if (value >= binding->input.axis.axis_max &&
  168. value <= binding->input.axis.axis_min) {
  169. match = binding;
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. if (last_match && (!match || !HasSameOutput(last_match, match))) {
  176. // Clear the last input that this axis generated
  177. ResetOutput(timestamp, gamepad, last_match);
  178. }
  179. if (match) {
  180. if (match->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
  181. if (match->input.axis.axis_min != match->output.axis.axis_min || match->input.axis.axis_max != match->output.axis.axis_max) {
  182. float normalized_value = (float)(value - match->input.axis.axis_min) / (match->input.axis.axis_max - match->input.axis.axis_min);
  183. value = match->output.axis.axis_min + (int)(normalized_value * (match->output.axis.axis_max - match->output.axis.axis_min));
  184. }
  185. SDL_SendGamepadAxis(timestamp, gamepad, match->output.axis.axis, (Sint16)value);
  186. } else {
  187. bool down;
  188. int threshold = match->input.axis.axis_min + (match->input.axis.axis_max - match->input.axis.axis_min) / 2;
  189. if (match->input.axis.axis_max < match->input.axis.axis_min) {
  190. down = (value <= threshold);
  191. } else {
  192. down = (value >= threshold);
  193. }
  194. SDL_SendGamepadButton(timestamp, gamepad, match->output.button, down);
  195. }
  196. }
  197. gamepad->last_match_axis[axis] = match;
  198. }
  199. static void HandleJoystickButton(Uint64 timestamp, SDL_Gamepad *gamepad, int button, bool down)
  200. {
  201. int i;
  202. SDL_AssertJoysticksLocked();
  203. for (i = 0; i < gamepad->num_bindings; ++i) {
  204. SDL_GamepadBinding *binding = &gamepad->bindings[i];
  205. if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON &&
  206. button == binding->input.button) {
  207. if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
  208. int value = down ? binding->output.axis.axis_max : binding->output.axis.axis_min;
  209. SDL_SendGamepadAxis(timestamp, gamepad, binding->output.axis.axis, (Sint16)value);
  210. } else {
  211. SDL_SendGamepadButton(timestamp, gamepad, binding->output.button, down);
  212. }
  213. break;
  214. }
  215. }
  216. }
  217. static void HandleJoystickHat(Uint64 timestamp, SDL_Gamepad *gamepad, int hat, Uint8 value)
  218. {
  219. int i;
  220. Uint8 last_mask, changed_mask;
  221. SDL_AssertJoysticksLocked();
  222. last_mask = gamepad->last_hat_mask[hat];
  223. changed_mask = (last_mask ^ value);
  224. for (i = 0; i < gamepad->num_bindings; ++i) {
  225. SDL_GamepadBinding *binding = &gamepad->bindings[i];
  226. if (binding->input_type == SDL_GAMEPAD_BINDTYPE_HAT && hat == binding->input.hat.hat) {
  227. if ((changed_mask & binding->input.hat.hat_mask) != 0) {
  228. if (value & binding->input.hat.hat_mask) {
  229. if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
  230. SDL_SendGamepadAxis(timestamp, gamepad, binding->output.axis.axis, (Sint16)binding->output.axis.axis_max);
  231. } else {
  232. SDL_SendGamepadButton(timestamp, gamepad, binding->output.button, true);
  233. }
  234. } else {
  235. ResetOutput(timestamp, gamepad, binding);
  236. }
  237. }
  238. }
  239. }
  240. gamepad->last_hat_mask[hat] = value;
  241. }
  242. /* The joystick layer will _also_ send events to recenter before disconnect,
  243. but it has to make (sometimes incorrect) guesses at what being "centered"
  244. is. The gamepad layer, however, can set a definite logical idle
  245. position, so set them all here. If we happened to already be at the
  246. center thanks to the joystick layer or idle hands, this won't generate
  247. duplicate events. */
  248. static void RecenterGamepad(SDL_Gamepad *gamepad)
  249. {
  250. int i;
  251. Uint64 timestamp = SDL_GetTicksNS();
  252. for (i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) {
  253. SDL_GamepadButton button = (SDL_GamepadButton)i;
  254. if (SDL_GetGamepadButton(gamepad, button)) {
  255. SDL_SendGamepadButton(timestamp, gamepad, button, false);
  256. }
  257. }
  258. for (i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
  259. SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
  260. if (SDL_GetGamepadAxis(gamepad, axis) != 0) {
  261. SDL_SendGamepadAxis(timestamp, gamepad, axis, 0);
  262. }
  263. }
  264. }
  265. void SDL_PrivateGamepadAdded(SDL_JoystickID instance_id)
  266. {
  267. SDL_Event event;
  268. if (!SDL_gamepads_initialized) {
  269. return;
  270. }
  271. event.type = SDL_EVENT_GAMEPAD_ADDED;
  272. event.common.timestamp = 0;
  273. event.gdevice.which = instance_id;
  274. SDL_PushEvent(&event);
  275. }
  276. void SDL_PrivateGamepadRemoved(SDL_JoystickID instance_id)
  277. {
  278. SDL_Event event;
  279. SDL_Gamepad *gamepad;
  280. SDL_AssertJoysticksLocked();
  281. if (!SDL_gamepads_initialized) {
  282. return;
  283. }
  284. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  285. if (gamepad->joystick->instance_id == instance_id) {
  286. RecenterGamepad(gamepad);
  287. break;
  288. }
  289. }
  290. event.type = SDL_EVENT_GAMEPAD_REMOVED;
  291. event.common.timestamp = 0;
  292. event.gdevice.which = instance_id;
  293. SDL_PushEvent(&event);
  294. }
  295. static void SDL_PrivateGamepadRemapped(SDL_JoystickID instance_id)
  296. {
  297. SDL_Event event;
  298. if (!SDL_gamepads_initialized || SDL_IsJoystickBeingAdded()) {
  299. return;
  300. }
  301. event.type = SDL_EVENT_GAMEPAD_REMAPPED;
  302. event.common.timestamp = 0;
  303. event.gdevice.which = instance_id;
  304. SDL_PushEvent(&event);
  305. }
  306. /*
  307. * Event filter to fire gamepad events from joystick ones
  308. */
  309. static bool SDLCALL SDL_GamepadEventWatcher(void *userdata, SDL_Event *event)
  310. {
  311. SDL_Gamepad *gamepad;
  312. switch (event->type) {
  313. case SDL_EVENT_JOYSTICK_AXIS_MOTION:
  314. {
  315. SDL_AssertJoysticksLocked();
  316. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  317. if (gamepad->joystick->instance_id == event->jaxis.which) {
  318. HandleJoystickAxis(event->common.timestamp, gamepad, event->jaxis.axis, event->jaxis.value);
  319. break;
  320. }
  321. }
  322. } break;
  323. case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
  324. case SDL_EVENT_JOYSTICK_BUTTON_UP:
  325. {
  326. SDL_AssertJoysticksLocked();
  327. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  328. if (gamepad->joystick->instance_id == event->jbutton.which) {
  329. HandleJoystickButton(event->common.timestamp, gamepad, event->jbutton.button, event->jbutton.down);
  330. break;
  331. }
  332. }
  333. } break;
  334. case SDL_EVENT_JOYSTICK_HAT_MOTION:
  335. {
  336. SDL_AssertJoysticksLocked();
  337. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  338. if (gamepad->joystick->instance_id == event->jhat.which) {
  339. HandleJoystickHat(event->common.timestamp, gamepad, event->jhat.hat, event->jhat.value);
  340. break;
  341. }
  342. }
  343. } break;
  344. case SDL_EVENT_JOYSTICK_UPDATE_COMPLETE:
  345. {
  346. SDL_AssertJoysticksLocked();
  347. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  348. if (gamepad->joystick->instance_id == event->jdevice.which) {
  349. SDL_Event deviceevent;
  350. deviceevent.type = SDL_EVENT_GAMEPAD_UPDATE_COMPLETE;
  351. deviceevent.common.timestamp = event->jdevice.timestamp;
  352. deviceevent.gdevice.which = event->jdevice.which;
  353. SDL_PushEvent(&deviceevent);
  354. break;
  355. }
  356. }
  357. } break;
  358. default:
  359. break;
  360. }
  361. return true;
  362. }
  363. /* SDL defines sensor orientation relative to the device natural
  364. orientation, so when it's changed orientation to be used as a
  365. gamepad, change the sensor orientation to match.
  366. */
  367. static void AdjustSensorOrientation(SDL_Joystick *joystick, float *src, float *dst)
  368. {
  369. unsigned int i, j;
  370. SDL_AssertJoysticksLocked();
  371. for (i = 0; i < 3; ++i) {
  372. dst[i] = 0.0f;
  373. for (j = 0; j < 3; ++j) {
  374. dst[i] += joystick->sensor_transform[i][j] * src[j];
  375. }
  376. }
  377. }
  378. /*
  379. * Event filter to fire gamepad sensor events from system sensor events
  380. *
  381. * We don't use SDL_GamepadEventWatcher() for this because we want to
  382. * deliver gamepad sensor events when system sensor events are disabled,
  383. * and we also need to avoid a potential deadlock where joystick event
  384. * delivery locks the joysticks and then the event queue, but sensor
  385. * event delivery would lock the event queue and then from within the
  386. * event watcher function lock the joysticks.
  387. */
  388. void SDL_GamepadSensorWatcher(Uint64 timestamp, SDL_SensorID sensor, Uint64 sensor_timestamp, float *data, int num_values)
  389. {
  390. SDL_Gamepad *gamepad;
  391. SDL_LockJoysticks();
  392. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  393. if (gamepad->joystick->accel && gamepad->joystick->accel_sensor == sensor) {
  394. float gamepad_data[3];
  395. AdjustSensorOrientation(gamepad->joystick, data, gamepad_data);
  396. SDL_SendJoystickSensor(timestamp, gamepad->joystick, SDL_SENSOR_ACCEL, sensor_timestamp, gamepad_data, SDL_arraysize(gamepad_data));
  397. }
  398. if (gamepad->joystick->gyro && gamepad->joystick->gyro_sensor == sensor) {
  399. float gamepad_data[3];
  400. AdjustSensorOrientation(gamepad->joystick, data, gamepad_data);
  401. SDL_SendJoystickSensor(timestamp, gamepad->joystick, SDL_SENSOR_GYRO, sensor_timestamp, gamepad_data, SDL_arraysize(gamepad_data));
  402. }
  403. }
  404. SDL_UnlockJoysticks();
  405. }
  406. static void PushMappingChangeTracking(void)
  407. {
  408. MappingChangeTracker *tracker;
  409. int i, num_joysticks;
  410. SDL_AssertJoysticksLocked();
  411. if (s_mappingChangeTracker) {
  412. ++s_mappingChangeTracker->refcount;
  413. return;
  414. }
  415. s_mappingChangeTracker = (MappingChangeTracker *)SDL_calloc(1, sizeof(*tracker));
  416. s_mappingChangeTracker->refcount = 1;
  417. // Save the list of joysticks and associated mappings
  418. tracker = s_mappingChangeTracker;
  419. tracker->joysticks = SDL_GetJoysticks(&num_joysticks);
  420. if (!tracker->joysticks) {
  421. return;
  422. }
  423. if (num_joysticks == 0) {
  424. return;
  425. }
  426. tracker->joystick_mappings = (GamepadMapping_t **)SDL_malloc(num_joysticks * sizeof(*tracker->joystick_mappings));
  427. if (!tracker->joystick_mappings) {
  428. return;
  429. }
  430. for (i = 0; i < num_joysticks; ++i) {
  431. tracker->joystick_mappings[i] = SDL_PrivateGetGamepadMapping(tracker->joysticks[i], false);
  432. }
  433. }
  434. static void AddMappingChangeTracking(GamepadMapping_t *mapping)
  435. {
  436. MappingChangeTracker *tracker;
  437. int num_mappings;
  438. GamepadMapping_t **new_mappings;
  439. SDL_AssertJoysticksLocked();
  440. SDL_assert(s_mappingChangeTracker != NULL);
  441. tracker = s_mappingChangeTracker;
  442. num_mappings = tracker->num_changed_mappings;
  443. new_mappings = (GamepadMapping_t **)SDL_realloc(tracker->changed_mappings, (num_mappings + 1) * sizeof(*new_mappings));
  444. if (new_mappings) {
  445. tracker->changed_mappings = new_mappings;
  446. tracker->changed_mappings[num_mappings] = mapping;
  447. tracker->num_changed_mappings = (num_mappings + 1);
  448. }
  449. }
  450. static bool HasMappingChangeTracking(MappingChangeTracker *tracker, GamepadMapping_t *mapping)
  451. {
  452. int i;
  453. SDL_AssertJoysticksLocked();
  454. for (i = 0; i < tracker->num_changed_mappings; ++i) {
  455. if (tracker->changed_mappings[i] == mapping) {
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. static void PopMappingChangeTracking(void)
  462. {
  463. int i;
  464. MappingChangeTracker *tracker;
  465. SDL_AssertJoysticksLocked();
  466. SDL_assert(s_mappingChangeTracker != NULL);
  467. tracker = s_mappingChangeTracker;
  468. --tracker->refcount;
  469. if (tracker->refcount > 0) {
  470. return;
  471. }
  472. s_mappingChangeTracker = NULL;
  473. // Now check to see what gamepads changed because of the mapping changes
  474. if (tracker->joysticks && tracker->joystick_mappings) {
  475. for (i = 0; tracker->joysticks[i]; ++i) {
  476. // Looking up the new mapping might create one and associate it with the gamepad (and generate events)
  477. SDL_JoystickID joystick = tracker->joysticks[i];
  478. SDL_Gamepad *gamepad = SDL_GetGamepadFromID(joystick);
  479. GamepadMapping_t *new_mapping = SDL_PrivateGetGamepadMapping(joystick, false);
  480. GamepadMapping_t *old_mapping = gamepad ? gamepad->mapping : tracker->joystick_mappings[i];
  481. if (new_mapping && !old_mapping) {
  482. SDL_RemoveFromHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick);
  483. SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick, (const void *)true);
  484. SDL_PrivateGamepadAdded(joystick);
  485. } else if (old_mapping && !new_mapping) {
  486. SDL_RemoveFromHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick);
  487. SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick, (const void *)false);
  488. SDL_PrivateGamepadRemoved(joystick);
  489. } else if (old_mapping != new_mapping || HasMappingChangeTracking(tracker, new_mapping)) {
  490. if (gamepad) {
  491. SDL_PrivateLoadButtonMapping(gamepad, new_mapping);
  492. }
  493. SDL_PrivateGamepadRemapped(joystick);
  494. }
  495. }
  496. }
  497. SDL_free(tracker->joysticks);
  498. SDL_free(tracker->joystick_mappings);
  499. SDL_free(tracker->changed_mappings);
  500. SDL_free(tracker);
  501. }
  502. #ifdef SDL_PLATFORM_ANDROID
  503. /*
  504. * Helper function to guess at a mapping based on the elements reported for this gamepad
  505. */
  506. static GamepadMapping_t *SDL_CreateMappingForAndroidGamepad(SDL_GUID guid)
  507. {
  508. const int face_button_mask = ((1 << SDL_GAMEPAD_BUTTON_SOUTH) |
  509. (1 << SDL_GAMEPAD_BUTTON_EAST) |
  510. (1 << SDL_GAMEPAD_BUTTON_WEST) |
  511. (1 << SDL_GAMEPAD_BUTTON_NORTH));
  512. bool existing;
  513. char mapping_string[1024];
  514. int button_mask;
  515. int axis_mask;
  516. button_mask = SDL_Swap16LE(*(Uint16 *)(&guid.data[sizeof(guid.data) - 4]));
  517. axis_mask = SDL_Swap16LE(*(Uint16 *)(&guid.data[sizeof(guid.data) - 2]));
  518. if (!button_mask && !axis_mask) {
  519. // Accelerometer, shouldn't have a gamepad mapping
  520. return NULL;
  521. }
  522. if (!(button_mask & face_button_mask)) {
  523. // We don't know what buttons or axes are supported, don't make up a mapping
  524. return NULL;
  525. }
  526. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  527. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_SOUTH)) {
  528. SDL_strlcat(mapping_string, "a:b0,", sizeof(mapping_string));
  529. }
  530. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_EAST)) {
  531. SDL_strlcat(mapping_string, "b:b1,", sizeof(mapping_string));
  532. } else if (button_mask & (1 << SDL_GAMEPAD_BUTTON_BACK)) {
  533. // Use the back button as "B" for easy UI navigation with TV remotes
  534. SDL_strlcat(mapping_string, "b:b4,", sizeof(mapping_string));
  535. button_mask &= ~(1 << SDL_GAMEPAD_BUTTON_BACK);
  536. }
  537. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_WEST)) {
  538. SDL_strlcat(mapping_string, "x:b2,", sizeof(mapping_string));
  539. }
  540. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_NORTH)) {
  541. SDL_strlcat(mapping_string, "y:b3,", sizeof(mapping_string));
  542. }
  543. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_BACK)) {
  544. SDL_strlcat(mapping_string, "back:b4,", sizeof(mapping_string));
  545. }
  546. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_GUIDE)) {
  547. // The guide button generally isn't functional (or acts as a home button) on most Android gamepads before Android 11
  548. if (SDL_GetAndroidSDKVersion() >= 30 /* Android 11 */) {
  549. SDL_strlcat(mapping_string, "guide:b5,", sizeof(mapping_string));
  550. }
  551. }
  552. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_START)) {
  553. SDL_strlcat(mapping_string, "start:b6,", sizeof(mapping_string));
  554. }
  555. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_LEFT_STICK)) {
  556. SDL_strlcat(mapping_string, "leftstick:b7,", sizeof(mapping_string));
  557. }
  558. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_RIGHT_STICK)) {
  559. SDL_strlcat(mapping_string, "rightstick:b8,", sizeof(mapping_string));
  560. }
  561. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_LEFT_SHOULDER)) {
  562. SDL_strlcat(mapping_string, "leftshoulder:b9,", sizeof(mapping_string));
  563. }
  564. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER)) {
  565. SDL_strlcat(mapping_string, "rightshoulder:b10,", sizeof(mapping_string));
  566. }
  567. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_UP)) {
  568. SDL_strlcat(mapping_string, "dpup:b11,", sizeof(mapping_string));
  569. }
  570. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_DOWN)) {
  571. SDL_strlcat(mapping_string, "dpdown:b12,", sizeof(mapping_string));
  572. }
  573. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_LEFT)) {
  574. SDL_strlcat(mapping_string, "dpleft:b13,", sizeof(mapping_string));
  575. }
  576. if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) {
  577. SDL_strlcat(mapping_string, "dpright:b14,", sizeof(mapping_string));
  578. }
  579. if (axis_mask & (1 << SDL_GAMEPAD_AXIS_LEFTX)) {
  580. SDL_strlcat(mapping_string, "leftx:a0,", sizeof(mapping_string));
  581. }
  582. if (axis_mask & (1 << SDL_GAMEPAD_AXIS_LEFTY)) {
  583. SDL_strlcat(mapping_string, "lefty:a1,", sizeof(mapping_string));
  584. }
  585. if (axis_mask & (1 << SDL_GAMEPAD_AXIS_RIGHTX)) {
  586. SDL_strlcat(mapping_string, "rightx:a2,", sizeof(mapping_string));
  587. }
  588. if (axis_mask & (1 << SDL_GAMEPAD_AXIS_RIGHTY)) {
  589. SDL_strlcat(mapping_string, "righty:a3,", sizeof(mapping_string));
  590. }
  591. if (axis_mask & (1 << SDL_GAMEPAD_AXIS_LEFT_TRIGGER)) {
  592. SDL_strlcat(mapping_string, "lefttrigger:a4,", sizeof(mapping_string));
  593. }
  594. if (axis_mask & (1 << SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)) {
  595. SDL_strlcat(mapping_string, "righttrigger:a5,", sizeof(mapping_string));
  596. }
  597. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
  598. }
  599. #endif // SDL_PLATFORM_ANDROID
  600. /*
  601. * Helper function to guess at a mapping for HIDAPI gamepads
  602. */
  603. static GamepadMapping_t *SDL_CreateMappingForHIDAPIGamepad(SDL_GUID guid)
  604. {
  605. bool existing;
  606. char mapping_string[1024];
  607. Uint16 vendor;
  608. Uint16 product;
  609. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  610. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
  611. if ((vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) ||
  612. (vendor == USB_VENDOR_DRAGONRISE &&
  613. (product == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER1 ||
  614. product == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER2))) {
  615. // GameCube driver has 12 buttons and 6 axes
  616. SDL_strlcat(mapping_string, "a:b0,b:b1,dpdown:b6,dpleft:b4,dpright:b5,dpup:b7,lefttrigger:a4,leftx:a0,lefty:a1~,rightshoulder:b9,righttrigger:a5,rightx:a2,righty:a3~,start:b8,x:b2,y:b3,", sizeof(mapping_string));
  617. } else if (vendor == USB_VENDOR_NINTENDO &&
  618. (guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCLeft ||
  619. guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCRight ||
  620. guid.data[15] == k_eSwitchDeviceInfoControllerType_NESLeft ||
  621. guid.data[15] == k_eSwitchDeviceInfoControllerType_NESRight ||
  622. guid.data[15] == k_eSwitchDeviceInfoControllerType_SNES ||
  623. guid.data[15] == k_eSwitchDeviceInfoControllerType_N64 ||
  624. guid.data[15] == k_eSwitchDeviceInfoControllerType_SEGA_Genesis ||
  625. guid.data[15] == k_eWiiExtensionControllerType_None ||
  626. guid.data[15] == k_eWiiExtensionControllerType_Nunchuk ||
  627. guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
  628. guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConRight)) {
  629. switch (guid.data[15]) {
  630. case k_eSwitchDeviceInfoControllerType_HVCLeft:
  631. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,rightshoulder:b10,start:b6,", sizeof(mapping_string));
  632. break;
  633. case k_eSwitchDeviceInfoControllerType_HVCRight:
  634. SDL_strlcat(mapping_string, "a:b0,b:b1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,rightshoulder:b10,", sizeof(mapping_string));
  635. break;
  636. case k_eSwitchDeviceInfoControllerType_NESLeft:
  637. case k_eSwitchDeviceInfoControllerType_NESRight:
  638. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,rightshoulder:b10,start:b6,", sizeof(mapping_string));
  639. break;
  640. case k_eSwitchDeviceInfoControllerType_SNES:
  641. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,lefttrigger:a4,rightshoulder:b10,righttrigger:a5,start:b6,x:b2,y:b3,", sizeof(mapping_string));
  642. break;
  643. case k_eSwitchDeviceInfoControllerType_N64:
  644. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,righttrigger:a5,start:b6,x:b2,y:b3,misc1:b11,", sizeof(mapping_string));
  645. break;
  646. case k_eSwitchDeviceInfoControllerType_SEGA_Genesis:
  647. SDL_strlcat(mapping_string, "a:b0,b:b1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,rightshoulder:b10,righttrigger:a5,start:b6,x:b2,y:b3,misc1:b11,", sizeof(mapping_string));
  648. break;
  649. case k_eWiiExtensionControllerType_None:
  650. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,start:b6,x:b2,y:b3,", sizeof(mapping_string));
  651. break;
  652. case k_eWiiExtensionControllerType_Nunchuk:
  653. {
  654. // FIXME: Should we map this to the left or right side?
  655. const bool map_nunchuck_left_side = true;
  656. if (map_nunchuck_left_side) {
  657. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,lefttrigger:a4,leftx:a0,lefty:a1,start:b6,x:b2,y:b3,", sizeof(mapping_string));
  658. } else {
  659. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,rightshoulder:b9,righttrigger:a4,rightx:a0,righty:a1,start:b6,x:b2,y:b3,", sizeof(mapping_string));
  660. }
  661. } break;
  662. default:
  663. if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS, false)) {
  664. // Vertical mode
  665. if (guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  666. SDL_strlcat(mapping_string, "back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,misc1:b11,paddle2:b13,paddle4:b15,", sizeof(mapping_string));
  667. } else {
  668. SDL_strlcat(mapping_string, "a:b0,b:b1,guide:b5,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,paddle1:b12,paddle3:b14,", sizeof(mapping_string));
  669. }
  670. } else {
  671. // Mini gamepad mode
  672. if (guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  673. SDL_strlcat(mapping_string, "a:b0,b:b1,guide:b5,leftshoulder:b9,leftstick:b7,leftx:a0,lefty:a1,rightshoulder:b10,start:b6,x:b2,y:b3,paddle2:b13,paddle4:b15,", sizeof(mapping_string));
  674. } else {
  675. SDL_strlcat(mapping_string, "a:b0,b:b1,guide:b5,leftshoulder:b9,leftstick:b7,leftx:a0,lefty:a1,rightshoulder:b10,start:b6,x:b2,y:b3,paddle1:b12,paddle3:b14,", sizeof(mapping_string));
  676. }
  677. }
  678. break;
  679. }
  680. } else {
  681. // All other gamepads have the standard set of 19 buttons and 6 axes
  682. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,", sizeof(mapping_string));
  683. if (SDL_IsJoystickXboxSeriesX(vendor, product)) {
  684. // XBox Series X Controllers have a share button under the guide button
  685. SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
  686. } else if (SDL_IsJoystickXboxOneElite(vendor, product)) {
  687. // XBox One Elite Controllers have 4 back paddle buttons
  688. SDL_strlcat(mapping_string, "paddle1:b11,paddle2:b13,paddle3:b12,paddle4:b14,", sizeof(mapping_string));
  689. } else if (SDL_IsJoystickSteamController(vendor, product)) {
  690. // Steam controllers have 2 back paddle buttons
  691. SDL_strlcat(mapping_string, "paddle1:b12,paddle2:b11,", sizeof(mapping_string));
  692. } else if (SDL_IsJoystickNintendoSwitchPro(vendor, product) ||
  693. SDL_IsJoystickNintendoSwitchProInputOnly(vendor, product)) {
  694. // Nintendo Switch Pro controllers have a screenshot button
  695. SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
  696. } else if (SDL_IsJoystickNintendoSwitchJoyConPair(vendor, product)) {
  697. // The Nintendo Switch Joy-Con combined controllers has a share button and paddles
  698. SDL_strlcat(mapping_string, "misc1:b11,paddle1:b12,paddle2:b13,paddle3:b14,paddle4:b15,", sizeof(mapping_string));
  699. } else if (SDL_IsJoystickAmazonLunaController(vendor, product)) {
  700. // Amazon Luna Controller has a mic button under the guide button
  701. SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
  702. } else if (SDL_IsJoystickGoogleStadiaController(vendor, product)) {
  703. // The Google Stadia controller has a share button and a Google Assistant button
  704. SDL_strlcat(mapping_string, "misc1:b11,misc2:b12", sizeof(mapping_string));
  705. } else if (SDL_IsJoystickNVIDIASHIELDController(vendor, product)) {
  706. // The NVIDIA SHIELD controller has a share button between back and start buttons
  707. SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
  708. if (product == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V103) {
  709. // The original SHIELD controller has a touchpad and plus/minus buttons as well
  710. SDL_strlcat(mapping_string, "touchpad:b12,misc2:b13,misc3:b14", sizeof(mapping_string));
  711. }
  712. } else if (SDL_IsJoystickHoriSteamController(vendor, product)) {
  713. /* The Wireless HORIPad for Steam has QAM, Steam, Capsense L/R Sticks, 2 rear buttons, and 2 misc buttons */
  714. SDL_strlcat(mapping_string, "paddle1:b13,paddle2:b12,paddle3:b15,paddle4:b14,misc2:b11,misc3:b16,misc4:b17", sizeof(mapping_string));
  715. } else {
  716. switch (SDL_GetGamepadTypeFromGUID(guid, NULL)) {
  717. case SDL_GAMEPAD_TYPE_PS4:
  718. // PS4 controllers have an additional touchpad button
  719. SDL_strlcat(mapping_string, "touchpad:b11,", sizeof(mapping_string));
  720. break;
  721. case SDL_GAMEPAD_TYPE_PS5:
  722. // PS5 controllers have a microphone button and an additional touchpad button
  723. SDL_strlcat(mapping_string, "touchpad:b11,misc1:b12,", sizeof(mapping_string));
  724. // DualSense Edge controllers have paddles
  725. if (SDL_IsJoystickDualSenseEdge(vendor, product)) {
  726. SDL_strlcat(mapping_string, "paddle1:b16,paddle2:b15,paddle3:b14,paddle4:b13,", sizeof(mapping_string));
  727. }
  728. break;
  729. default:
  730. if (vendor == 0 && product == 0) {
  731. // This is a Bluetooth Nintendo Switch Pro controller
  732. SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
  733. }
  734. break;
  735. }
  736. }
  737. }
  738. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
  739. }
  740. /*
  741. * Helper function to guess at a mapping for RAWINPUT gamepads
  742. */
  743. static GamepadMapping_t *SDL_CreateMappingForRAWINPUTGamepad(SDL_GUID guid)
  744. {
  745. bool existing;
  746. char mapping_string[1024];
  747. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  748. SDL_strlcat(mapping_string, "a:b0,b:b1,x:b2,y:b3,back:b6,guide:b10,start:b7,leftstick:b8,rightstick:b9,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,", sizeof(mapping_string));
  749. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
  750. }
  751. /*
  752. * Helper function to guess at a mapping for WGI gamepads
  753. */
  754. static GamepadMapping_t *SDL_CreateMappingForWGIGamepad(SDL_GUID guid)
  755. {
  756. bool existing;
  757. char mapping_string[1024];
  758. if (guid.data[15] != SDL_JOYSTICK_TYPE_GAMEPAD) {
  759. return NULL;
  760. }
  761. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  762. SDL_strlcat(mapping_string, "a:b0,b:b1,x:b2,y:b3,back:b6,start:b7,leftstick:b8,rightstick:b9,leftshoulder:b4,rightshoulder:b5,dpup:b10,dpdown:b12,dpleft:b13,dpright:b11,leftx:a1,lefty:a0~,rightx:a3,righty:a2~,lefttrigger:a4,righttrigger:a5,", sizeof(mapping_string));
  763. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
  764. }
  765. /*
  766. * Helper function to scan the mappings database for a gamepad with the specified GUID
  767. */
  768. static GamepadMapping_t *SDL_PrivateMatchGamepadMappingForGUID(SDL_GUID guid, bool match_version, bool exact_match_crc)
  769. {
  770. GamepadMapping_t *mapping, *best_match = NULL;
  771. Uint16 crc = 0;
  772. SDL_AssertJoysticksLocked();
  773. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc);
  774. // Clear the CRC from the GUID for matching, the mappings never include it in the GUID
  775. SDL_SetJoystickGUIDCRC(&guid, 0);
  776. if (!match_version) {
  777. SDL_SetJoystickGUIDVersion(&guid, 0);
  778. }
  779. for (mapping = s_pSupportedGamepads; mapping; mapping = mapping->next) {
  780. SDL_GUID mapping_guid;
  781. if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
  782. continue;
  783. }
  784. SDL_memcpy(&mapping_guid, &mapping->guid, sizeof(mapping_guid));
  785. if (!match_version) {
  786. SDL_SetJoystickGUIDVersion(&mapping_guid, 0);
  787. }
  788. if (SDL_memcmp(&guid, &mapping_guid, sizeof(guid)) == 0) {
  789. const char *crc_string = SDL_strstr(mapping->mapping, SDL_GAMEPAD_CRC_FIELD);
  790. if (crc_string) {
  791. Uint16 mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_GAMEPAD_CRC_FIELD_SIZE, NULL, 16);
  792. if (mapping_crc != crc) {
  793. // This mapping specified a CRC and they don't match
  794. continue;
  795. }
  796. // An exact match, including CRC
  797. return mapping;
  798. } else if (crc && exact_match_crc) {
  799. return NULL;
  800. }
  801. if (!best_match) {
  802. best_match = mapping;
  803. }
  804. }
  805. }
  806. return best_match;
  807. }
  808. /*
  809. * Helper function to scan the mappings database for a gamepad with the specified GUID
  810. */
  811. static GamepadMapping_t *SDL_PrivateGetGamepadMappingForGUID(SDL_GUID guid, bool adding_mapping)
  812. {
  813. GamepadMapping_t *mapping;
  814. mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, true, adding_mapping);
  815. if (mapping) {
  816. return mapping;
  817. }
  818. if (adding_mapping) {
  819. // We didn't find an existing mapping
  820. return NULL;
  821. }
  822. // Try harder to get the best match, or create a mapping
  823. if (SDL_JoystickGUIDUsesVersion(guid)) {
  824. // Try again, ignoring the version
  825. mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, false, false);
  826. if (mapping) {
  827. return mapping;
  828. }
  829. }
  830. #ifdef SDL_JOYSTICK_XINPUT
  831. if (SDL_IsJoystickXInput(guid)) {
  832. // This is an XInput device
  833. return s_pXInputMapping;
  834. }
  835. #endif
  836. if (SDL_IsJoystickHIDAPI(guid)) {
  837. mapping = SDL_CreateMappingForHIDAPIGamepad(guid);
  838. } else if (SDL_IsJoystickRAWINPUT(guid)) {
  839. mapping = SDL_CreateMappingForRAWINPUTGamepad(guid);
  840. } else if (SDL_IsJoystickWGI(guid)) {
  841. mapping = SDL_CreateMappingForWGIGamepad(guid);
  842. } else if (SDL_IsJoystickVIRTUAL(guid)) {
  843. // We'll pick up a robust mapping in VIRTUAL_JoystickGetGamepadMapping
  844. #ifdef SDL_PLATFORM_ANDROID
  845. } else {
  846. mapping = SDL_CreateMappingForAndroidGamepad(guid);
  847. #endif
  848. }
  849. return mapping;
  850. }
  851. static const char *map_StringForGamepadType[] = {
  852. "unknown",
  853. "standard",
  854. "xbox360",
  855. "xboxone",
  856. "ps3",
  857. "ps4",
  858. "ps5",
  859. "switchpro",
  860. "joyconleft",
  861. "joyconright",
  862. "joyconpair"
  863. };
  864. SDL_COMPILE_TIME_ASSERT(map_StringForGamepadType, SDL_arraysize(map_StringForGamepadType) == SDL_GAMEPAD_TYPE_COUNT);
  865. /*
  866. * convert a string to its enum equivalent
  867. */
  868. SDL_GamepadType SDL_GetGamepadTypeFromString(const char *str)
  869. {
  870. int i;
  871. if (!str || str[0] == '\0') {
  872. return SDL_GAMEPAD_TYPE_UNKNOWN;
  873. }
  874. if (*str == '+' || *str == '-') {
  875. ++str;
  876. }
  877. for (i = 0; i < SDL_arraysize(map_StringForGamepadType); ++i) {
  878. if (SDL_strcasecmp(str, map_StringForGamepadType[i]) == 0) {
  879. return (SDL_GamepadType)i;
  880. }
  881. }
  882. return SDL_GAMEPAD_TYPE_UNKNOWN;
  883. }
  884. /*
  885. * convert an enum to its string equivalent
  886. */
  887. const char *SDL_GetGamepadStringForType(SDL_GamepadType type)
  888. {
  889. if (type >= SDL_GAMEPAD_TYPE_STANDARD && type < SDL_GAMEPAD_TYPE_COUNT) {
  890. return map_StringForGamepadType[type];
  891. }
  892. return NULL;
  893. }
  894. static const char *map_StringForGamepadAxis[] = {
  895. "leftx",
  896. "lefty",
  897. "rightx",
  898. "righty",
  899. "lefttrigger",
  900. "righttrigger"
  901. };
  902. SDL_COMPILE_TIME_ASSERT(map_StringForGamepadAxis, SDL_arraysize(map_StringForGamepadAxis) == SDL_GAMEPAD_AXIS_COUNT);
  903. /*
  904. * convert a string to its enum equivalent
  905. */
  906. SDL_GamepadAxis SDL_GetGamepadAxisFromString(const char *str)
  907. {
  908. int i;
  909. if (!str || str[0] == '\0') {
  910. return SDL_GAMEPAD_AXIS_INVALID;
  911. }
  912. if (*str == '+' || *str == '-') {
  913. ++str;
  914. }
  915. for (i = 0; i < SDL_arraysize(map_StringForGamepadAxis); ++i) {
  916. if (SDL_strcasecmp(str, map_StringForGamepadAxis[i]) == 0) {
  917. return (SDL_GamepadAxis)i;
  918. }
  919. }
  920. return SDL_GAMEPAD_AXIS_INVALID;
  921. }
  922. /*
  923. * convert an enum to its string equivalent
  924. */
  925. const char *SDL_GetGamepadStringForAxis(SDL_GamepadAxis axis)
  926. {
  927. if (axis > SDL_GAMEPAD_AXIS_INVALID && axis < SDL_GAMEPAD_AXIS_COUNT) {
  928. return map_StringForGamepadAxis[axis];
  929. }
  930. return NULL;
  931. }
  932. static const char *map_StringForGamepadButton[] = {
  933. "a",
  934. "b",
  935. "x",
  936. "y",
  937. "back",
  938. "guide",
  939. "start",
  940. "leftstick",
  941. "rightstick",
  942. "leftshoulder",
  943. "rightshoulder",
  944. "dpup",
  945. "dpdown",
  946. "dpleft",
  947. "dpright",
  948. "misc1",
  949. "paddle1",
  950. "paddle2",
  951. "paddle3",
  952. "paddle4",
  953. "touchpad",
  954. "misc2",
  955. "misc3",
  956. "misc4",
  957. "misc5",
  958. "misc6"
  959. };
  960. SDL_COMPILE_TIME_ASSERT(map_StringForGamepadButton, SDL_arraysize(map_StringForGamepadButton) == SDL_GAMEPAD_BUTTON_COUNT);
  961. /*
  962. * convert a string to its enum equivalent
  963. */
  964. static SDL_GamepadButton SDL_PrivateGetGamepadButtonFromString(const char *str, bool baxy)
  965. {
  966. int i;
  967. if (!str || str[0] == '\0') {
  968. return SDL_GAMEPAD_BUTTON_INVALID;
  969. }
  970. for (i = 0; i < SDL_arraysize(map_StringForGamepadButton); ++i) {
  971. if (SDL_strcasecmp(str, map_StringForGamepadButton[i]) == 0) {
  972. if (baxy) {
  973. // Need to swap face buttons
  974. switch (i) {
  975. case SDL_GAMEPAD_BUTTON_SOUTH:
  976. return SDL_GAMEPAD_BUTTON_EAST;
  977. case SDL_GAMEPAD_BUTTON_EAST:
  978. return SDL_GAMEPAD_BUTTON_SOUTH;
  979. case SDL_GAMEPAD_BUTTON_WEST:
  980. return SDL_GAMEPAD_BUTTON_NORTH;
  981. case SDL_GAMEPAD_BUTTON_NORTH:
  982. return SDL_GAMEPAD_BUTTON_WEST;
  983. default:
  984. break;
  985. }
  986. }
  987. return (SDL_GamepadButton)i;
  988. }
  989. }
  990. return SDL_GAMEPAD_BUTTON_INVALID;
  991. }
  992. SDL_GamepadButton SDL_GetGamepadButtonFromString(const char *str)
  993. {
  994. return SDL_PrivateGetGamepadButtonFromString(str, false);
  995. }
  996. /*
  997. * convert an enum to its string equivalent
  998. */
  999. const char *SDL_GetGamepadStringForButton(SDL_GamepadButton button)
  1000. {
  1001. if (button > SDL_GAMEPAD_BUTTON_INVALID && button < SDL_GAMEPAD_BUTTON_COUNT) {
  1002. return map_StringForGamepadButton[button];
  1003. }
  1004. return NULL;
  1005. }
  1006. /*
  1007. * given a gamepad button name and a joystick name update our mapping structure with it
  1008. */
  1009. static bool SDL_PrivateParseGamepadElement(SDL_Gamepad *gamepad, const char *szGameButton, const char *szJoystickButton)
  1010. {
  1011. SDL_GamepadBinding bind;
  1012. SDL_GamepadButton button;
  1013. SDL_GamepadAxis axis;
  1014. bool invert_input = false;
  1015. char half_axis_input = 0;
  1016. char half_axis_output = 0;
  1017. int i;
  1018. SDL_GamepadBinding *new_bindings;
  1019. bool baxy_mapping = false;
  1020. SDL_AssertJoysticksLocked();
  1021. SDL_zero(bind);
  1022. if (*szGameButton == '+' || *szGameButton == '-') {
  1023. half_axis_output = *szGameButton++;
  1024. }
  1025. if (SDL_strstr(gamepad->mapping->mapping, ",hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1") != NULL) {
  1026. baxy_mapping = true;
  1027. }
  1028. axis = SDL_GetGamepadAxisFromString(szGameButton);
  1029. button = SDL_PrivateGetGamepadButtonFromString(szGameButton, baxy_mapping);
  1030. if (axis != SDL_GAMEPAD_AXIS_INVALID) {
  1031. bind.output_type = SDL_GAMEPAD_BINDTYPE_AXIS;
  1032. bind.output.axis.axis = axis;
  1033. if (axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER || axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
  1034. bind.output.axis.axis_min = 0;
  1035. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  1036. } else {
  1037. if (half_axis_output == '+') {
  1038. bind.output.axis.axis_min = 0;
  1039. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  1040. } else if (half_axis_output == '-') {
  1041. bind.output.axis.axis_min = 0;
  1042. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
  1043. } else {
  1044. bind.output.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
  1045. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  1046. }
  1047. }
  1048. } else if (button != SDL_GAMEPAD_BUTTON_INVALID) {
  1049. bind.output_type = SDL_GAMEPAD_BINDTYPE_BUTTON;
  1050. bind.output.button = button;
  1051. } else {
  1052. return false;
  1053. }
  1054. if (*szJoystickButton == '+' || *szJoystickButton == '-') {
  1055. half_axis_input = *szJoystickButton++;
  1056. }
  1057. if (szJoystickButton[SDL_strlen(szJoystickButton) - 1] == '~') {
  1058. invert_input = true;
  1059. }
  1060. if (szJoystickButton[0] == 'a' && SDL_isdigit((unsigned char)szJoystickButton[1])) {
  1061. bind.input_type = SDL_GAMEPAD_BINDTYPE_AXIS;
  1062. bind.input.axis.axis = SDL_atoi(&szJoystickButton[1]);
  1063. if (half_axis_input == '+') {
  1064. bind.input.axis.axis_min = 0;
  1065. bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  1066. } else if (half_axis_input == '-') {
  1067. bind.input.axis.axis_min = 0;
  1068. bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
  1069. } else {
  1070. bind.input.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
  1071. bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  1072. }
  1073. if (invert_input) {
  1074. int tmp = bind.input.axis.axis_min;
  1075. bind.input.axis.axis_min = bind.input.axis.axis_max;
  1076. bind.input.axis.axis_max = tmp;
  1077. }
  1078. } else if (szJoystickButton[0] == 'b' && SDL_isdigit((unsigned char)szJoystickButton[1])) {
  1079. bind.input_type = SDL_GAMEPAD_BINDTYPE_BUTTON;
  1080. bind.input.button = SDL_atoi(&szJoystickButton[1]);
  1081. } else if (szJoystickButton[0] == 'h' && SDL_isdigit((unsigned char)szJoystickButton[1]) &&
  1082. szJoystickButton[2] == '.' && SDL_isdigit((unsigned char)szJoystickButton[3])) {
  1083. int hat = SDL_atoi(&szJoystickButton[1]);
  1084. int mask = SDL_atoi(&szJoystickButton[3]);
  1085. bind.input_type = SDL_GAMEPAD_BINDTYPE_HAT;
  1086. bind.input.hat.hat = hat;
  1087. bind.input.hat.hat_mask = mask;
  1088. } else {
  1089. return false;
  1090. }
  1091. for (i = 0; i < gamepad->num_bindings; ++i) {
  1092. if (SDL_memcmp(&gamepad->bindings[i], &bind, sizeof(bind)) == 0) {
  1093. // We already have this binding, could be different face button names?
  1094. return true;
  1095. }
  1096. }
  1097. ++gamepad->num_bindings;
  1098. new_bindings = (SDL_GamepadBinding *)SDL_realloc(gamepad->bindings, gamepad->num_bindings * sizeof(*gamepad->bindings));
  1099. if (!new_bindings) {
  1100. SDL_free(gamepad->bindings);
  1101. gamepad->num_bindings = 0;
  1102. gamepad->bindings = NULL;
  1103. return false;
  1104. }
  1105. gamepad->bindings = new_bindings;
  1106. gamepad->bindings[gamepad->num_bindings - 1] = bind;
  1107. return true;
  1108. }
  1109. /*
  1110. * given a gamepad mapping string update our mapping object
  1111. */
  1112. static bool SDL_PrivateParseGamepadConfigString(SDL_Gamepad *gamepad, const char *pchString)
  1113. {
  1114. char szGameButton[20];
  1115. char szJoystickButton[20];
  1116. bool bGameButton = true;
  1117. int i = 0;
  1118. const char *pchPos = pchString;
  1119. SDL_zeroa(szGameButton);
  1120. SDL_zeroa(szJoystickButton);
  1121. while (pchPos && *pchPos) {
  1122. if (*pchPos == ':') {
  1123. i = 0;
  1124. bGameButton = false;
  1125. } else if (*pchPos == ' ') {
  1126. } else if (*pchPos == ',') {
  1127. i = 0;
  1128. bGameButton = true;
  1129. SDL_PrivateParseGamepadElement(gamepad, szGameButton, szJoystickButton);
  1130. SDL_zeroa(szGameButton);
  1131. SDL_zeroa(szJoystickButton);
  1132. } else if (bGameButton) {
  1133. if (i >= sizeof(szGameButton)) {
  1134. szGameButton[sizeof(szGameButton) - 1] = '\0';
  1135. return SDL_SetError("Button name too large: %s", szGameButton);
  1136. }
  1137. szGameButton[i] = *pchPos;
  1138. i++;
  1139. } else {
  1140. if (i >= sizeof(szJoystickButton)) {
  1141. szJoystickButton[sizeof(szJoystickButton) - 1] = '\0';
  1142. return SDL_SetError("Joystick button name too large: %s", szJoystickButton);
  1143. }
  1144. szJoystickButton[i] = *pchPos;
  1145. i++;
  1146. }
  1147. pchPos++;
  1148. }
  1149. // No more values if the string was terminated by a comma. Don't report an error.
  1150. if (szGameButton[0] != '\0' || szJoystickButton[0] != '\0') {
  1151. SDL_PrivateParseGamepadElement(gamepad, szGameButton, szJoystickButton);
  1152. }
  1153. return true;
  1154. }
  1155. static void SDL_UpdateGamepadType(SDL_Gamepad *gamepad)
  1156. {
  1157. char *type_string, *comma;
  1158. SDL_AssertJoysticksLocked();
  1159. gamepad->type = SDL_GAMEPAD_TYPE_UNKNOWN;
  1160. type_string = SDL_strstr(gamepad->mapping->mapping, SDL_GAMEPAD_TYPE_FIELD);
  1161. if (type_string) {
  1162. type_string += SDL_GAMEPAD_TYPE_FIELD_SIZE;
  1163. comma = SDL_strchr(type_string, ',');
  1164. if (comma) {
  1165. *comma = '\0';
  1166. gamepad->type = SDL_GetGamepadTypeFromString(type_string);
  1167. *comma = ',';
  1168. } else {
  1169. gamepad->type = SDL_GetGamepadTypeFromString(type_string);
  1170. }
  1171. }
  1172. if (gamepad->type == SDL_GAMEPAD_TYPE_UNKNOWN) {
  1173. gamepad->type = SDL_GetRealGamepadTypeForID(gamepad->joystick->instance_id);
  1174. }
  1175. }
  1176. static SDL_GamepadFaceStyle SDL_GetGamepadFaceStyleFromString(const char *string)
  1177. {
  1178. if (SDL_strcmp(string, "abxy") == 0) {
  1179. return SDL_GAMEPAD_FACE_STYLE_ABXY;
  1180. } else if (SDL_strcmp(string, "bayx") == 0) {
  1181. return SDL_GAMEPAD_FACE_STYLE_BAYX;
  1182. } else if (SDL_strcmp(string, "sony") == 0) {
  1183. return SDL_GAMEPAD_FACE_STYLE_SONY;
  1184. } else {
  1185. return SDL_GAMEPAD_FACE_STYLE_UNKNOWN;
  1186. }
  1187. }
  1188. static SDL_GamepadFaceStyle SDL_GetGamepadFaceStyleForGamepadType(SDL_GamepadType type)
  1189. {
  1190. switch (type) {
  1191. case SDL_GAMEPAD_TYPE_PS3:
  1192. case SDL_GAMEPAD_TYPE_PS4:
  1193. case SDL_GAMEPAD_TYPE_PS5:
  1194. return SDL_GAMEPAD_FACE_STYLE_SONY;
  1195. case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
  1196. case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
  1197. case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
  1198. case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
  1199. return SDL_GAMEPAD_FACE_STYLE_BAYX;
  1200. default:
  1201. return SDL_GAMEPAD_FACE_STYLE_ABXY;
  1202. }
  1203. }
  1204. static void SDL_UpdateGamepadFaceStyle(SDL_Gamepad *gamepad)
  1205. {
  1206. char *face_string, *comma;
  1207. SDL_AssertJoysticksLocked();
  1208. gamepad->face_style = SDL_GAMEPAD_FACE_STYLE_UNKNOWN;
  1209. face_string = SDL_strstr(gamepad->mapping->mapping, SDL_GAMEPAD_FACE_FIELD);
  1210. if (face_string) {
  1211. face_string += SDL_GAMEPAD_TYPE_FIELD_SIZE;
  1212. comma = SDL_strchr(face_string, ',');
  1213. if (comma) {
  1214. *comma = '\0';
  1215. gamepad->face_style = SDL_GetGamepadFaceStyleFromString(face_string);
  1216. *comma = ',';
  1217. } else {
  1218. gamepad->face_style = SDL_GetGamepadFaceStyleFromString(face_string);
  1219. }
  1220. }
  1221. if (gamepad->face_style == SDL_GAMEPAD_FACE_STYLE_UNKNOWN &&
  1222. SDL_strstr(gamepad->mapping->mapping, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS") != NULL) {
  1223. // This controller uses Nintendo button style
  1224. gamepad->face_style = SDL_GAMEPAD_FACE_STYLE_BAYX;
  1225. }
  1226. if (gamepad->face_style == SDL_GAMEPAD_FACE_STYLE_UNKNOWN) {
  1227. gamepad->face_style = SDL_GetGamepadFaceStyleForGamepadType(gamepad->type);
  1228. }
  1229. }
  1230. static void SDL_FixupHIDAPIMapping(SDL_Gamepad *gamepad)
  1231. {
  1232. // Check to see if we need fixup
  1233. for (int i = 0; i < gamepad->num_bindings; ++i) {
  1234. SDL_GamepadBinding *binding = &gamepad->bindings[i];
  1235. if (binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON &&
  1236. binding->output.button == SDL_GAMEPAD_BUTTON_DPAD_UP) {
  1237. if (binding->input_type != SDL_GAMEPAD_BINDTYPE_BUTTON ||
  1238. binding->input.button != SDL_GAMEPAD_BUTTON_DPAD_UP) {
  1239. // New style binding
  1240. return;
  1241. }
  1242. }
  1243. }
  1244. for (int i = 0; i < gamepad->num_bindings; ++i) {
  1245. SDL_GamepadBinding *binding = &gamepad->bindings[i];
  1246. if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON &&
  1247. binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON) {
  1248. switch (binding->output.button) {
  1249. case SDL_GAMEPAD_BUTTON_DPAD_UP:
  1250. binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
  1251. binding->input.hat.hat = 0;
  1252. binding->input.hat.hat_mask = SDL_HAT_UP;
  1253. break;
  1254. case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
  1255. binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
  1256. binding->input.hat.hat = 0;
  1257. binding->input.hat.hat_mask = SDL_HAT_DOWN;
  1258. break;
  1259. case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
  1260. binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
  1261. binding->input.hat.hat = 0;
  1262. binding->input.hat.hat_mask = SDL_HAT_LEFT;
  1263. break;
  1264. case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
  1265. binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
  1266. binding->input.hat.hat = 0;
  1267. binding->input.hat.hat_mask = SDL_HAT_RIGHT;
  1268. break;
  1269. default:
  1270. if (binding->output.button > SDL_GAMEPAD_BUTTON_DPAD_RIGHT) {
  1271. binding->input.button -= 4;
  1272. }
  1273. break;
  1274. }
  1275. }
  1276. }
  1277. }
  1278. /*
  1279. * Make a new button mapping struct
  1280. */
  1281. static void SDL_PrivateLoadButtonMapping(SDL_Gamepad *gamepad, GamepadMapping_t *pGamepadMapping)
  1282. {
  1283. int i;
  1284. SDL_AssertJoysticksLocked();
  1285. gamepad->name = pGamepadMapping->name;
  1286. gamepad->num_bindings = 0;
  1287. gamepad->mapping = pGamepadMapping;
  1288. if (gamepad->joystick->naxes != 0 && gamepad->last_match_axis) {
  1289. SDL_memset(gamepad->last_match_axis, 0, gamepad->joystick->naxes * sizeof(*gamepad->last_match_axis));
  1290. }
  1291. SDL_UpdateGamepadType(gamepad);
  1292. SDL_UpdateGamepadFaceStyle(gamepad);
  1293. SDL_PrivateParseGamepadConfigString(gamepad, pGamepadMapping->mapping);
  1294. if (SDL_IsJoystickHIDAPI(pGamepadMapping->guid)) {
  1295. SDL_FixupHIDAPIMapping(gamepad);
  1296. }
  1297. // Set the zero point for triggers
  1298. for (i = 0; i < gamepad->num_bindings; ++i) {
  1299. SDL_GamepadBinding *binding = &gamepad->bindings[i];
  1300. if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS &&
  1301. binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS &&
  1302. (binding->output.axis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
  1303. binding->output.axis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)) {
  1304. if (binding->input.axis.axis < gamepad->joystick->naxes) {
  1305. gamepad->joystick->axes[binding->input.axis.axis].value =
  1306. gamepad->joystick->axes[binding->input.axis.axis].zero = (Sint16)binding->input.axis.axis_min;
  1307. }
  1308. }
  1309. }
  1310. }
  1311. /*
  1312. * grab the guid string from a mapping string
  1313. */
  1314. static char *SDL_PrivateGetGamepadGUIDFromMappingString(const char *pMapping)
  1315. {
  1316. const char *pFirstComma = SDL_strchr(pMapping, ',');
  1317. if (pFirstComma) {
  1318. char *pchGUID = (char *)SDL_malloc(pFirstComma - pMapping + 1);
  1319. if (!pchGUID) {
  1320. return NULL;
  1321. }
  1322. SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
  1323. pchGUID[pFirstComma - pMapping] = '\0';
  1324. // Convert old style GUIDs to the new style in 2.0.5
  1325. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  1326. if (SDL_strlen(pchGUID) == 32 &&
  1327. SDL_memcmp(&pchGUID[20], "504944564944", 12) == 0) {
  1328. SDL_memcpy(&pchGUID[20], "000000000000", 12);
  1329. SDL_memcpy(&pchGUID[16], &pchGUID[4], 4);
  1330. SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
  1331. SDL_memcpy(&pchGUID[0], "03000000", 8);
  1332. }
  1333. #elif defined(SDL_PLATFORM_MACOS)
  1334. if (SDL_strlen(pchGUID) == 32 &&
  1335. SDL_memcmp(&pchGUID[4], "000000000000", 12) == 0 &&
  1336. SDL_memcmp(&pchGUID[20], "000000000000", 12) == 0) {
  1337. SDL_memcpy(&pchGUID[20], "000000000000", 12);
  1338. SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
  1339. SDL_memcpy(&pchGUID[0], "03000000", 8);
  1340. }
  1341. #endif
  1342. return pchGUID;
  1343. }
  1344. return NULL;
  1345. }
  1346. /*
  1347. * grab the name string from a mapping string
  1348. */
  1349. static char *SDL_PrivateGetGamepadNameFromMappingString(const char *pMapping)
  1350. {
  1351. const char *pFirstComma, *pSecondComma;
  1352. char *pchName;
  1353. pFirstComma = SDL_strchr(pMapping, ',');
  1354. if (!pFirstComma) {
  1355. return NULL;
  1356. }
  1357. pSecondComma = SDL_strchr(pFirstComma + 1, ',');
  1358. if (!pSecondComma) {
  1359. return NULL;
  1360. }
  1361. pchName = (char *)SDL_malloc(pSecondComma - pFirstComma);
  1362. if (!pchName) {
  1363. return NULL;
  1364. }
  1365. SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
  1366. pchName[pSecondComma - pFirstComma - 1] = 0;
  1367. return pchName;
  1368. }
  1369. /*
  1370. * grab the button mapping string from a mapping string
  1371. */
  1372. static char *SDL_PrivateGetGamepadMappingFromMappingString(const char *pMapping)
  1373. {
  1374. const char *pFirstComma, *pSecondComma;
  1375. char *result;
  1376. size_t length;
  1377. pFirstComma = SDL_strchr(pMapping, ',');
  1378. if (!pFirstComma) {
  1379. return NULL;
  1380. }
  1381. pSecondComma = SDL_strchr(pFirstComma + 1, ',');
  1382. if (!pSecondComma) {
  1383. return NULL;
  1384. }
  1385. // Skip whitespace
  1386. while (SDL_isspace(pSecondComma[1])) {
  1387. ++pSecondComma;
  1388. }
  1389. result = SDL_strdup(pSecondComma + 1); // mapping is everything after the 3rd comma
  1390. // Trim whitespace
  1391. length = SDL_strlen(result);
  1392. while (length > 0 && SDL_isspace(result[length - 1])) {
  1393. --length;
  1394. }
  1395. result[length] = '\0';
  1396. return result;
  1397. }
  1398. /*
  1399. * Helper function to add a mapping for a guid
  1400. */
  1401. static GamepadMapping_t *SDL_PrivateAddMappingForGUID(SDL_GUID jGUID, const char *mappingString, bool *existing, SDL_GamepadMappingPriority priority)
  1402. {
  1403. char *pchName;
  1404. char *pchMapping;
  1405. GamepadMapping_t *pGamepadMapping;
  1406. Uint16 crc;
  1407. SDL_AssertJoysticksLocked();
  1408. pchName = SDL_PrivateGetGamepadNameFromMappingString(mappingString);
  1409. if (!pchName) {
  1410. SDL_SetError("Couldn't parse name from %s", mappingString);
  1411. return NULL;
  1412. }
  1413. pchMapping = SDL_PrivateGetGamepadMappingFromMappingString(mappingString);
  1414. if (!pchMapping) {
  1415. SDL_free(pchName);
  1416. SDL_SetError("Couldn't parse %s", mappingString);
  1417. return NULL;
  1418. }
  1419. // Fix up the GUID and the mapping with the CRC, if needed
  1420. SDL_GetJoystickGUIDInfo(jGUID, NULL, NULL, NULL, &crc);
  1421. if (crc) {
  1422. // Make sure the mapping has the CRC
  1423. char *new_mapping;
  1424. const char *optional_comma;
  1425. size_t mapping_length;
  1426. char *crc_end = "";
  1427. char *crc_string = SDL_strstr(pchMapping, SDL_GAMEPAD_CRC_FIELD);
  1428. if (crc_string) {
  1429. crc_end = SDL_strchr(crc_string, ',');
  1430. if (crc_end) {
  1431. ++crc_end;
  1432. } else {
  1433. crc_end = "";
  1434. }
  1435. *crc_string = '\0';
  1436. }
  1437. // Make sure there's a comma before the CRC
  1438. mapping_length = SDL_strlen(pchMapping);
  1439. if (mapping_length == 0 || pchMapping[mapping_length - 1] == ',') {
  1440. optional_comma = "";
  1441. } else {
  1442. optional_comma = ",";
  1443. }
  1444. if (SDL_asprintf(&new_mapping, "%s%s%s%.4x,%s", pchMapping, optional_comma, SDL_GAMEPAD_CRC_FIELD, crc, crc_end) >= 0) {
  1445. SDL_free(pchMapping);
  1446. pchMapping = new_mapping;
  1447. }
  1448. } else {
  1449. // Make sure the GUID has the CRC, for matching purposes
  1450. char *crc_string = SDL_strstr(pchMapping, SDL_GAMEPAD_CRC_FIELD);
  1451. if (crc_string) {
  1452. crc = (Uint16)SDL_strtol(crc_string + SDL_GAMEPAD_CRC_FIELD_SIZE, NULL, 16);
  1453. if (crc) {
  1454. SDL_SetJoystickGUIDCRC(&jGUID, crc);
  1455. }
  1456. }
  1457. }
  1458. PushMappingChangeTracking();
  1459. pGamepadMapping = SDL_PrivateGetGamepadMappingForGUID(jGUID, true);
  1460. if (pGamepadMapping) {
  1461. // Only overwrite the mapping if the priority is the same or higher.
  1462. if (pGamepadMapping->priority <= priority) {
  1463. // Update existing mapping
  1464. SDL_free(pGamepadMapping->name);
  1465. pGamepadMapping->name = pchName;
  1466. SDL_free(pGamepadMapping->mapping);
  1467. pGamepadMapping->mapping = pchMapping;
  1468. pGamepadMapping->priority = priority;
  1469. } else {
  1470. SDL_free(pchName);
  1471. SDL_free(pchMapping);
  1472. }
  1473. if (existing) {
  1474. *existing = true;
  1475. }
  1476. AddMappingChangeTracking(pGamepadMapping);
  1477. } else {
  1478. pGamepadMapping = (GamepadMapping_t *)SDL_malloc(sizeof(*pGamepadMapping));
  1479. if (!pGamepadMapping) {
  1480. PopMappingChangeTracking();
  1481. SDL_free(pchName);
  1482. SDL_free(pchMapping);
  1483. return NULL;
  1484. }
  1485. // Clear the CRC, we've already added it to the mapping
  1486. if (crc) {
  1487. SDL_SetJoystickGUIDCRC(&jGUID, 0);
  1488. }
  1489. pGamepadMapping->guid = jGUID;
  1490. pGamepadMapping->name = pchName;
  1491. pGamepadMapping->mapping = pchMapping;
  1492. pGamepadMapping->next = NULL;
  1493. pGamepadMapping->priority = priority;
  1494. if (s_pSupportedGamepads) {
  1495. // Add the mapping to the end of the list
  1496. GamepadMapping_t *pCurrMapping, *pPrevMapping;
  1497. for (pPrevMapping = s_pSupportedGamepads, pCurrMapping = pPrevMapping->next;
  1498. pCurrMapping;
  1499. pPrevMapping = pCurrMapping, pCurrMapping = pCurrMapping->next) {
  1500. // continue;
  1501. }
  1502. pPrevMapping->next = pGamepadMapping;
  1503. } else {
  1504. s_pSupportedGamepads = pGamepadMapping;
  1505. }
  1506. if (existing) {
  1507. *existing = false;
  1508. }
  1509. }
  1510. PopMappingChangeTracking();
  1511. return pGamepadMapping;
  1512. }
  1513. /*
  1514. * Helper function to determine pre-calculated offset to certain joystick mappings
  1515. */
  1516. static GamepadMapping_t *SDL_PrivateGetGamepadMappingForNameAndGUID(const char *name, SDL_GUID guid)
  1517. {
  1518. GamepadMapping_t *mapping;
  1519. SDL_AssertJoysticksLocked();
  1520. mapping = SDL_PrivateGetGamepadMappingForGUID(guid, false);
  1521. #ifdef SDL_PLATFORM_LINUX
  1522. if (!mapping && name) {
  1523. if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
  1524. // The Linux driver xpad.c maps the wireless dpad to buttons
  1525. bool existing;
  1526. mapping = SDL_PrivateAddMappingForGUID(guid,
  1527. "none,X360 Wireless Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b11,dpright:b12,dpup:b13,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
  1528. &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
  1529. }
  1530. }
  1531. #endif // SDL_PLATFORM_LINUX
  1532. return mapping;
  1533. }
  1534. static void SDL_PrivateAppendToMappingString(char *mapping_string,
  1535. size_t mapping_string_len,
  1536. const char *input_name,
  1537. SDL_InputMapping *mapping)
  1538. {
  1539. char buffer[16];
  1540. if (mapping->kind == EMappingKind_None) {
  1541. return;
  1542. }
  1543. SDL_strlcat(mapping_string, input_name, mapping_string_len);
  1544. SDL_strlcat(mapping_string, ":", mapping_string_len);
  1545. switch (mapping->kind) {
  1546. case EMappingKind_Button:
  1547. (void)SDL_snprintf(buffer, sizeof(buffer), "b%u", mapping->target);
  1548. break;
  1549. case EMappingKind_Axis:
  1550. (void)SDL_snprintf(buffer, sizeof(buffer), "%sa%u%s",
  1551. mapping->half_axis_positive ? "+" :
  1552. mapping->half_axis_negative ? "-" : "",
  1553. mapping->target,
  1554. mapping->axis_reversed ? "~" : "");
  1555. break;
  1556. case EMappingKind_Hat:
  1557. (void)SDL_snprintf(buffer, sizeof(buffer), "h%i.%i", mapping->target >> 4, mapping->target & 0x0F);
  1558. break;
  1559. default:
  1560. SDL_assert(false);
  1561. }
  1562. SDL_strlcat(mapping_string, buffer, mapping_string_len);
  1563. SDL_strlcat(mapping_string, ",", mapping_string_len);
  1564. }
  1565. static GamepadMapping_t *SDL_PrivateGenerateAutomaticGamepadMapping(const char *name,
  1566. SDL_GUID guid,
  1567. SDL_GamepadMapping *raw_map)
  1568. {
  1569. bool existing;
  1570. char name_string[128];
  1571. char mapping[1024];
  1572. // Remove any commas in the name
  1573. SDL_strlcpy(name_string, name, sizeof(name_string));
  1574. {
  1575. char *spot;
  1576. for (spot = name_string; *spot; ++spot) {
  1577. if (*spot == ',') {
  1578. *spot = ' ';
  1579. }
  1580. }
  1581. }
  1582. (void)SDL_snprintf(mapping, sizeof(mapping), "none,%s,", name_string);
  1583. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "a", &raw_map->a);
  1584. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "b", &raw_map->b);
  1585. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "x", &raw_map->x);
  1586. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "y", &raw_map->y);
  1587. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "back", &raw_map->back);
  1588. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "guide", &raw_map->guide);
  1589. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "start", &raw_map->start);
  1590. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftstick", &raw_map->leftstick);
  1591. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightstick", &raw_map->rightstick);
  1592. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftshoulder", &raw_map->leftshoulder);
  1593. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightshoulder", &raw_map->rightshoulder);
  1594. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpup", &raw_map->dpup);
  1595. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpdown", &raw_map->dpdown);
  1596. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpleft", &raw_map->dpleft);
  1597. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpright", &raw_map->dpright);
  1598. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc1", &raw_map->misc1);
  1599. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc2", &raw_map->misc2);
  1600. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc3", &raw_map->misc3);
  1601. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc4", &raw_map->misc4);
  1602. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc5", &raw_map->misc5);
  1603. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc6", &raw_map->misc6);
  1604. /* Keep using paddle1-4 in the generated mapping so that it can be
  1605. * reused with SDL2 */
  1606. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle1", &raw_map->right_paddle1);
  1607. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle2", &raw_map->left_paddle1);
  1608. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle3", &raw_map->right_paddle2);
  1609. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle4", &raw_map->left_paddle2);
  1610. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftx", &raw_map->leftx);
  1611. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefty", &raw_map->lefty);
  1612. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightx", &raw_map->rightx);
  1613. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righty", &raw_map->righty);
  1614. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefttrigger", &raw_map->lefttrigger);
  1615. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righttrigger", &raw_map->righttrigger);
  1616. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "touchpad", &raw_map->touchpad);
  1617. return SDL_PrivateAddMappingForGUID(guid, mapping, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
  1618. }
  1619. static GamepadMapping_t *SDL_PrivateGetGamepadMapping(SDL_JoystickID instance_id, bool create_mapping)
  1620. {
  1621. const char *name;
  1622. SDL_GUID guid;
  1623. GamepadMapping_t *mapping;
  1624. SDL_AssertJoysticksLocked();
  1625. name = SDL_GetJoystickNameForID(instance_id);
  1626. guid = SDL_GetJoystickGUIDForID(instance_id);
  1627. mapping = SDL_PrivateGetGamepadMappingForNameAndGUID(name, guid);
  1628. if (!mapping && create_mapping) {
  1629. SDL_GamepadMapping raw_map;
  1630. SDL_zero(raw_map);
  1631. if (SDL_PrivateJoystickGetAutoGamepadMapping(instance_id, &raw_map)) {
  1632. mapping = SDL_PrivateGenerateAutomaticGamepadMapping(name, guid, &raw_map);
  1633. }
  1634. }
  1635. if (!mapping) {
  1636. mapping = s_pDefaultMapping;
  1637. }
  1638. return mapping;
  1639. }
  1640. /*
  1641. * Add or update an entry into the Mappings Database
  1642. */
  1643. int SDL_AddGamepadMappingsFromIO(SDL_IOStream *src, bool closeio)
  1644. {
  1645. const char *platform = SDL_GetPlatform();
  1646. int gamepads = 0;
  1647. char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
  1648. size_t db_size;
  1649. size_t platform_len;
  1650. buf = (char *)SDL_LoadFile_IO(src, &db_size, closeio);
  1651. if (!buf) {
  1652. SDL_SetError("Could not allocate space to read DB into memory");
  1653. return -1;
  1654. }
  1655. line = buf;
  1656. SDL_LockJoysticks();
  1657. PushMappingChangeTracking();
  1658. while (line < buf + db_size) {
  1659. line_end = SDL_strchr(line, '\n');
  1660. if (line_end) {
  1661. *line_end = '\0';
  1662. } else {
  1663. line_end = buf + db_size;
  1664. }
  1665. // Extract and verify the platform
  1666. tmp = SDL_strstr(line, SDL_GAMEPAD_PLATFORM_FIELD);
  1667. if (tmp) {
  1668. tmp += SDL_GAMEPAD_PLATFORM_FIELD_SIZE;
  1669. comma = SDL_strchr(tmp, ',');
  1670. if (comma) {
  1671. platform_len = comma - tmp + 1;
  1672. if (platform_len + 1 < SDL_arraysize(line_platform)) {
  1673. SDL_strlcpy(line_platform, tmp, platform_len);
  1674. if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
  1675. SDL_AddGamepadMapping(line) > 0) {
  1676. gamepads++;
  1677. }
  1678. }
  1679. }
  1680. }
  1681. line = line_end + 1;
  1682. }
  1683. PopMappingChangeTracking();
  1684. SDL_UnlockJoysticks();
  1685. SDL_free(buf);
  1686. return gamepads;
  1687. }
  1688. int SDL_AddGamepadMappingsFromFile(const char *file)
  1689. {
  1690. return SDL_AddGamepadMappingsFromIO(SDL_IOFromFile(file, "rb"), true);
  1691. }
  1692. bool SDL_ReloadGamepadMappings(void)
  1693. {
  1694. SDL_Gamepad *gamepad;
  1695. SDL_LockJoysticks();
  1696. PushMappingChangeTracking();
  1697. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  1698. AddMappingChangeTracking(gamepad->mapping);
  1699. }
  1700. SDL_QuitGamepadMappings();
  1701. SDL_InitGamepadMappings();
  1702. PopMappingChangeTracking();
  1703. SDL_UnlockJoysticks();
  1704. return true;
  1705. }
  1706. static char *SDL_ConvertMappingToPositional(const char *mapping)
  1707. {
  1708. // Add space for '!' and null terminator
  1709. size_t length = SDL_strlen(mapping) + 1 + 1;
  1710. char *remapped = (char *)SDL_malloc(length);
  1711. if (remapped) {
  1712. char *button_A;
  1713. char *button_B;
  1714. char *button_X;
  1715. char *button_Y;
  1716. char *hint;
  1717. SDL_strlcpy(remapped, mapping, length);
  1718. button_A = SDL_strstr(remapped, "a:");
  1719. button_B = SDL_strstr(remapped, "b:");
  1720. button_X = SDL_strstr(remapped, "x:");
  1721. button_Y = SDL_strstr(remapped, "y:");
  1722. hint = SDL_strstr(remapped, "hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS");
  1723. if (button_A) {
  1724. *button_A = 'b';
  1725. }
  1726. if (button_B) {
  1727. *button_B = 'a';
  1728. }
  1729. if (button_X) {
  1730. *button_X = 'y';
  1731. }
  1732. if (button_Y) {
  1733. *button_Y = 'x';
  1734. }
  1735. if (hint) {
  1736. hint += 5;
  1737. SDL_memmove(hint + 1, hint, SDL_strlen(hint) + 1);
  1738. *hint = '!';
  1739. }
  1740. }
  1741. return remapped;
  1742. }
  1743. /*
  1744. * Add or update an entry into the Mappings Database with a priority
  1745. */
  1746. static int SDL_PrivateAddGamepadMapping(const char *mappingString, SDL_GamepadMappingPriority priority)
  1747. {
  1748. char *remapped = NULL;
  1749. char *pchGUID;
  1750. SDL_GUID jGUID;
  1751. bool is_default_mapping = false;
  1752. bool is_xinput_mapping = false;
  1753. bool existing = false;
  1754. GamepadMapping_t *pGamepadMapping;
  1755. int result = -1;
  1756. SDL_AssertJoysticksLocked();
  1757. if (!mappingString) {
  1758. SDL_InvalidParamError("mappingString");
  1759. return -1;
  1760. }
  1761. { // Extract and verify the hint field
  1762. const char *tmp;
  1763. tmp = SDL_strstr(mappingString, SDL_GAMEPAD_HINT_FIELD);
  1764. if (tmp) {
  1765. bool default_value, value, negate;
  1766. int len;
  1767. char hint[128];
  1768. tmp += SDL_GAMEPAD_HINT_FIELD_SIZE;
  1769. if (*tmp == '!') {
  1770. negate = true;
  1771. ++tmp;
  1772. } else {
  1773. negate = false;
  1774. }
  1775. len = 0;
  1776. while (*tmp && *tmp != ',' && *tmp != ':' && len < (sizeof(hint) - 1)) {
  1777. hint[len++] = *tmp++;
  1778. }
  1779. hint[len] = '\0';
  1780. if (tmp[0] == ':' && tmp[1] == '=') {
  1781. tmp += 2;
  1782. default_value = SDL_atoi(tmp);
  1783. } else {
  1784. default_value = false;
  1785. }
  1786. if (SDL_strcmp(hint, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS") == 0) {
  1787. // This hint is used to signal whether the mapping uses positional buttons or not
  1788. if (negate) {
  1789. // This mapping uses positional buttons, we can use it as-is
  1790. } else {
  1791. // This mapping uses labeled buttons, we need to swap them to positional
  1792. remapped = SDL_ConvertMappingToPositional(mappingString);
  1793. if (!remapped) {
  1794. goto done;
  1795. }
  1796. mappingString = remapped;
  1797. }
  1798. } else {
  1799. value = SDL_GetHintBoolean(hint, default_value);
  1800. if (negate) {
  1801. value = !value;
  1802. }
  1803. if (!value) {
  1804. result = 0;
  1805. goto done;
  1806. }
  1807. }
  1808. }
  1809. }
  1810. #ifdef ANDROID
  1811. { // Extract and verify the SDK version
  1812. const char *tmp;
  1813. tmp = SDL_strstr(mappingString, SDL_GAMEPAD_SDKGE_FIELD);
  1814. if (tmp) {
  1815. tmp += SDL_GAMEPAD_SDKGE_FIELD_SIZE;
  1816. if (!(SDL_GetAndroidSDKVersion() >= SDL_atoi(tmp))) {
  1817. SDL_SetError("SDK version %d < minimum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
  1818. goto done;
  1819. }
  1820. }
  1821. tmp = SDL_strstr(mappingString, SDL_GAMEPAD_SDKLE_FIELD);
  1822. if (tmp) {
  1823. tmp += SDL_GAMEPAD_SDKLE_FIELD_SIZE;
  1824. if (!(SDL_GetAndroidSDKVersion() <= SDL_atoi(tmp))) {
  1825. SDL_SetError("SDK version %d > maximum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
  1826. goto done;
  1827. }
  1828. }
  1829. }
  1830. #endif
  1831. pchGUID = SDL_PrivateGetGamepadGUIDFromMappingString(mappingString);
  1832. if (!pchGUID) {
  1833. SDL_SetError("Couldn't parse GUID from %s", mappingString);
  1834. goto done;
  1835. }
  1836. if (!SDL_strcasecmp(pchGUID, "default")) {
  1837. is_default_mapping = true;
  1838. } else if (!SDL_strcasecmp(pchGUID, "xinput")) {
  1839. is_xinput_mapping = true;
  1840. }
  1841. jGUID = SDL_StringToGUID(pchGUID);
  1842. SDL_free(pchGUID);
  1843. pGamepadMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing, priority);
  1844. if (!pGamepadMapping) {
  1845. goto done;
  1846. }
  1847. if (existing) {
  1848. result = 0;
  1849. } else {
  1850. if (is_default_mapping) {
  1851. s_pDefaultMapping = pGamepadMapping;
  1852. } else if (is_xinput_mapping) {
  1853. s_pXInputMapping = pGamepadMapping;
  1854. }
  1855. result = 1;
  1856. }
  1857. done:
  1858. if (remapped) {
  1859. SDL_free(remapped);
  1860. }
  1861. return result;
  1862. }
  1863. /*
  1864. * Add or update an entry into the Mappings Database
  1865. */
  1866. int SDL_AddGamepadMapping(const char *mapping)
  1867. {
  1868. int result;
  1869. SDL_LockJoysticks();
  1870. {
  1871. result = SDL_PrivateAddGamepadMapping(mapping, SDL_GAMEPAD_MAPPING_PRIORITY_API);
  1872. }
  1873. SDL_UnlockJoysticks();
  1874. return result;
  1875. }
  1876. /*
  1877. * Create a mapping string for a mapping
  1878. */
  1879. static char *CreateMappingString(GamepadMapping_t *mapping, SDL_GUID guid)
  1880. {
  1881. char *pMappingString, *pPlatformString;
  1882. char pchGUID[33];
  1883. size_t needed;
  1884. bool need_platform = false;
  1885. const char *platform = NULL;
  1886. SDL_AssertJoysticksLocked();
  1887. SDL_GUIDToString(guid, pchGUID, sizeof(pchGUID));
  1888. // allocate enough memory for GUID + ',' + name + ',' + mapping + \0
  1889. needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
  1890. if (!SDL_strstr(mapping->mapping, SDL_GAMEPAD_PLATFORM_FIELD)) {
  1891. // add memory for ',' + platform:PLATFORM
  1892. need_platform = true;
  1893. if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
  1894. needed += 1;
  1895. }
  1896. platform = SDL_GetPlatform();
  1897. needed += SDL_GAMEPAD_PLATFORM_FIELD_SIZE + SDL_strlen(platform) + 1;
  1898. }
  1899. pMappingString = (char *)SDL_malloc(needed);
  1900. if (!pMappingString) {
  1901. return NULL;
  1902. }
  1903. (void)SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
  1904. if (need_platform) {
  1905. if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
  1906. SDL_strlcat(pMappingString, ",", needed);
  1907. }
  1908. SDL_strlcat(pMappingString, SDL_GAMEPAD_PLATFORM_FIELD, needed);
  1909. SDL_strlcat(pMappingString, platform, needed);
  1910. SDL_strlcat(pMappingString, ",", needed);
  1911. }
  1912. // Make sure multiple platform strings haven't made their way into the mapping
  1913. pPlatformString = SDL_strstr(pMappingString, SDL_GAMEPAD_PLATFORM_FIELD);
  1914. if (pPlatformString) {
  1915. pPlatformString = SDL_strstr(pPlatformString + 1, SDL_GAMEPAD_PLATFORM_FIELD);
  1916. if (pPlatformString) {
  1917. *pPlatformString = '\0';
  1918. }
  1919. }
  1920. return pMappingString;
  1921. }
  1922. char **SDL_GetGamepadMappings(int *count)
  1923. {
  1924. int num_mappings = 0;
  1925. char **result = NULL;
  1926. char **mappings = NULL;
  1927. if (count) {
  1928. *count = 0;
  1929. }
  1930. SDL_LockJoysticks();
  1931. for (GamepadMapping_t *mapping = s_pSupportedGamepads; mapping; mapping = mapping->next) {
  1932. if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
  1933. continue;
  1934. }
  1935. num_mappings++;
  1936. }
  1937. size_t final_allocation = sizeof (char *); // for the NULL terminator element.
  1938. bool failed = false;
  1939. mappings = (char **) SDL_calloc(num_mappings + 1, sizeof (char *));
  1940. if (!mappings) {
  1941. failed = true;
  1942. } else {
  1943. int i = 0;
  1944. for (GamepadMapping_t *mapping = s_pSupportedGamepads; mapping; mapping = mapping->next) {
  1945. if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
  1946. continue;
  1947. }
  1948. char *mappingstr = CreateMappingString(mapping, mapping->guid);
  1949. if (!mappingstr) {
  1950. failed = true;
  1951. break; // error string is already set.
  1952. }
  1953. SDL_assert(i < num_mappings);
  1954. mappings[i++] = mappingstr;
  1955. final_allocation += SDL_strlen(mappingstr) + 1 + sizeof (char *);
  1956. }
  1957. }
  1958. SDL_UnlockJoysticks();
  1959. if (!failed) {
  1960. result = (char **) SDL_malloc(final_allocation);
  1961. if (result) {
  1962. final_allocation -= (sizeof (char *) * num_mappings + 1);
  1963. char *strptr = (char *) (result + (num_mappings + 1));
  1964. for (int i = 0; i < num_mappings; i++) {
  1965. result[i] = strptr;
  1966. const size_t slen = SDL_strlcpy(strptr, mappings[i], final_allocation) + 1;
  1967. SDL_assert(final_allocation >= slen);
  1968. final_allocation -= slen;
  1969. strptr += slen;
  1970. }
  1971. result[num_mappings] = NULL;
  1972. if (count) {
  1973. *count = num_mappings;
  1974. }
  1975. }
  1976. }
  1977. if (mappings) {
  1978. for (int i = 0; i < num_mappings; i++) {
  1979. SDL_free(mappings[i]);
  1980. }
  1981. SDL_free(mappings);
  1982. }
  1983. return result;
  1984. }
  1985. /*
  1986. * Get the mapping string for this GUID
  1987. */
  1988. char *SDL_GetGamepadMappingForGUID(SDL_GUID guid)
  1989. {
  1990. char *result;
  1991. SDL_LockJoysticks();
  1992. {
  1993. GamepadMapping_t *mapping = SDL_PrivateGetGamepadMappingForGUID(guid, false);
  1994. if (mapping) {
  1995. result = CreateMappingString(mapping, guid);
  1996. } else {
  1997. SDL_SetError("Mapping not available");
  1998. result = NULL;
  1999. }
  2000. }
  2001. SDL_UnlockJoysticks();
  2002. return result;
  2003. }
  2004. /*
  2005. * Get the mapping string for this device
  2006. */
  2007. char *SDL_GetGamepadMapping(SDL_Gamepad *gamepad)
  2008. {
  2009. char *result;
  2010. SDL_LockJoysticks();
  2011. {
  2012. CHECK_GAMEPAD_MAGIC(gamepad, NULL);
  2013. result = CreateMappingString(gamepad->mapping, gamepad->joystick->guid);
  2014. }
  2015. SDL_UnlockJoysticks();
  2016. return result;
  2017. }
  2018. /*
  2019. * Set the mapping string for this device
  2020. */
  2021. bool SDL_SetGamepadMapping(SDL_JoystickID instance_id, const char *mapping)
  2022. {
  2023. SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
  2024. bool result = false;
  2025. if (SDL_memcmp(&guid, &s_zeroGUID, sizeof(guid)) == 0) {
  2026. return SDL_InvalidParamError("instance_id");
  2027. }
  2028. if (!mapping) {
  2029. mapping = "*,*,";
  2030. }
  2031. SDL_LockJoysticks();
  2032. {
  2033. if (SDL_PrivateAddMappingForGUID(guid, mapping, NULL, SDL_GAMEPAD_MAPPING_PRIORITY_API)) {
  2034. result = true;
  2035. }
  2036. }
  2037. SDL_UnlockJoysticks();
  2038. return result;
  2039. }
  2040. static void SDL_LoadGamepadHints(void)
  2041. {
  2042. const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
  2043. if (hint && hint[0]) {
  2044. char *pTempMappings = SDL_strdup(hint);
  2045. char *pUserMappings = pTempMappings;
  2046. PushMappingChangeTracking();
  2047. while (pUserMappings) {
  2048. char *pchNewLine = NULL;
  2049. pchNewLine = SDL_strchr(pUserMappings, '\n');
  2050. if (pchNewLine) {
  2051. *pchNewLine = '\0';
  2052. }
  2053. SDL_PrivateAddGamepadMapping(pUserMappings, SDL_GAMEPAD_MAPPING_PRIORITY_USER);
  2054. if (pchNewLine) {
  2055. pUserMappings = pchNewLine + 1;
  2056. } else {
  2057. pUserMappings = NULL;
  2058. }
  2059. }
  2060. PopMappingChangeTracking();
  2061. SDL_free(pTempMappings);
  2062. }
  2063. }
  2064. /*
  2065. * Fill the given buffer with the expected gamepad mapping filepath.
  2066. * Usually this will just be SDL_HINT_GAMECONTROLLERCONFIG_FILE, but for
  2067. * Android, we want to get the internal storage path.
  2068. */
  2069. static bool SDL_GetGamepadMappingFilePath(char *path, size_t size)
  2070. {
  2071. const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG_FILE);
  2072. if (hint && *hint) {
  2073. return SDL_strlcpy(path, hint, size) < size;
  2074. }
  2075. #ifdef SDL_PLATFORM_ANDROID
  2076. return SDL_snprintf(path, size, "%s/gamepad_map.txt", SDL_GetAndroidInternalStoragePath()) < size;
  2077. #else
  2078. return false;
  2079. #endif
  2080. }
  2081. /*
  2082. * Initialize the gamepad system, mostly load our DB of gamepad config mappings
  2083. */
  2084. bool SDL_InitGamepadMappings(void)
  2085. {
  2086. char szGamepadMapPath[1024];
  2087. int i = 0;
  2088. const char *pMappingString = NULL;
  2089. SDL_AssertJoysticksLocked();
  2090. PushMappingChangeTracking();
  2091. pMappingString = s_GamepadMappings[i];
  2092. while (pMappingString) {
  2093. SDL_PrivateAddGamepadMapping(pMappingString, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
  2094. i++;
  2095. pMappingString = s_GamepadMappings[i];
  2096. }
  2097. if (SDL_GetGamepadMappingFilePath(szGamepadMapPath, sizeof(szGamepadMapPath))) {
  2098. SDL_AddGamepadMappingsFromFile(szGamepadMapPath);
  2099. }
  2100. // load in any user supplied config
  2101. SDL_LoadGamepadHints();
  2102. SDL_LoadVIDPIDList(&SDL_allowed_gamepads);
  2103. SDL_LoadVIDPIDList(&SDL_ignored_gamepads);
  2104. PopMappingChangeTracking();
  2105. return true;
  2106. }
  2107. bool SDL_InitGamepads(void)
  2108. {
  2109. int i;
  2110. SDL_JoystickID *joysticks;
  2111. SDL_gamepads_initialized = true;
  2112. // Watch for joystick events and fire gamepad ones if needed
  2113. SDL_AddEventWatch(SDL_GamepadEventWatcher, NULL);
  2114. // Send added events for gamepads currently attached
  2115. joysticks = SDL_GetJoysticks(NULL);
  2116. if (joysticks) {
  2117. for (i = 0; joysticks[i]; ++i) {
  2118. if (SDL_IsGamepad(joysticks[i])) {
  2119. SDL_PrivateGamepadAdded(joysticks[i]);
  2120. }
  2121. }
  2122. SDL_free(joysticks);
  2123. }
  2124. return true;
  2125. }
  2126. bool SDL_HasGamepad(void)
  2127. {
  2128. int num_joysticks = 0;
  2129. int num_gamepads = 0;
  2130. SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
  2131. if (joysticks) {
  2132. int i;
  2133. for (i = num_joysticks - 1; i >= 0 && num_gamepads == 0; --i) {
  2134. if (SDL_IsGamepad(joysticks[i])) {
  2135. ++num_gamepads;
  2136. }
  2137. }
  2138. SDL_free(joysticks);
  2139. }
  2140. if (num_gamepads > 0) {
  2141. return true;
  2142. }
  2143. return false;
  2144. }
  2145. SDL_JoystickID *SDL_GetGamepads(int *count)
  2146. {
  2147. int num_joysticks = 0;
  2148. int num_gamepads = 0;
  2149. SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
  2150. if (joysticks) {
  2151. int i;
  2152. for (i = num_joysticks - 1; i >= 0; --i) {
  2153. if (SDL_IsGamepad(joysticks[i])) {
  2154. ++num_gamepads;
  2155. } else {
  2156. SDL_memmove(&joysticks[i], &joysticks[i+1], (num_gamepads + 1) * sizeof(joysticks[i]));
  2157. }
  2158. }
  2159. }
  2160. if (count) {
  2161. *count = num_gamepads;
  2162. }
  2163. return joysticks;
  2164. }
  2165. const char *SDL_GetGamepadNameForID(SDL_JoystickID instance_id)
  2166. {
  2167. const char *result = NULL;
  2168. SDL_LockJoysticks();
  2169. {
  2170. GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id, true);
  2171. if (mapping) {
  2172. if (SDL_strcmp(mapping->name, "*") == 0) {
  2173. result = SDL_GetJoystickNameForID(instance_id);
  2174. } else {
  2175. result = SDL_GetPersistentString(mapping->name);
  2176. }
  2177. }
  2178. }
  2179. SDL_UnlockJoysticks();
  2180. return result;
  2181. }
  2182. const char *SDL_GetGamepadPathForID(SDL_JoystickID instance_id)
  2183. {
  2184. return SDL_GetJoystickPathForID(instance_id);
  2185. }
  2186. int SDL_GetGamepadPlayerIndexForID(SDL_JoystickID instance_id)
  2187. {
  2188. return SDL_GetJoystickPlayerIndexForID(instance_id);
  2189. }
  2190. SDL_GUID SDL_GetGamepadGUIDForID(SDL_JoystickID instance_id)
  2191. {
  2192. return SDL_GetJoystickGUIDForID(instance_id);
  2193. }
  2194. Uint16 SDL_GetGamepadVendorForID(SDL_JoystickID instance_id)
  2195. {
  2196. return SDL_GetJoystickVendorForID(instance_id);
  2197. }
  2198. Uint16 SDL_GetGamepadProductForID(SDL_JoystickID instance_id)
  2199. {
  2200. return SDL_GetJoystickProductForID(instance_id);
  2201. }
  2202. Uint16 SDL_GetGamepadProductVersionForID(SDL_JoystickID instance_id)
  2203. {
  2204. return SDL_GetJoystickProductVersionForID(instance_id);
  2205. }
  2206. SDL_GamepadType SDL_GetGamepadTypeForID(SDL_JoystickID instance_id)
  2207. {
  2208. SDL_GamepadType type = SDL_GAMEPAD_TYPE_UNKNOWN;
  2209. SDL_LockJoysticks();
  2210. {
  2211. GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id, true);
  2212. if (mapping) {
  2213. char *type_string, *comma;
  2214. type_string = SDL_strstr(mapping->mapping, SDL_GAMEPAD_TYPE_FIELD);
  2215. if (type_string) {
  2216. type_string += SDL_GAMEPAD_TYPE_FIELD_SIZE;
  2217. comma = SDL_strchr(type_string, ',');
  2218. if (comma) {
  2219. *comma = '\0';
  2220. type = SDL_GetGamepadTypeFromString(type_string);
  2221. *comma = ',';
  2222. }
  2223. }
  2224. }
  2225. }
  2226. SDL_UnlockJoysticks();
  2227. if (type != SDL_GAMEPAD_TYPE_UNKNOWN) {
  2228. return type;
  2229. }
  2230. return SDL_GetRealGamepadTypeForID(instance_id);
  2231. }
  2232. SDL_GamepadType SDL_GetRealGamepadTypeForID(SDL_JoystickID instance_id)
  2233. {
  2234. SDL_GamepadType type = SDL_GAMEPAD_TYPE_UNKNOWN;
  2235. const SDL_SteamVirtualGamepadInfo *info;
  2236. SDL_LockJoysticks();
  2237. {
  2238. info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);
  2239. if (info) {
  2240. type = info->type;
  2241. } else {
  2242. type = SDL_GetGamepadTypeFromGUID(SDL_GetJoystickGUIDForID(instance_id), SDL_GetJoystickNameForID(instance_id));
  2243. }
  2244. }
  2245. SDL_UnlockJoysticks();
  2246. return type;
  2247. }
  2248. char *SDL_GetGamepadMappingForID(SDL_JoystickID instance_id)
  2249. {
  2250. char *result = NULL;
  2251. SDL_LockJoysticks();
  2252. {
  2253. GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id, true);
  2254. if (mapping) {
  2255. char pchGUID[33];
  2256. SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
  2257. SDL_GUIDToString(guid, pchGUID, sizeof(pchGUID));
  2258. SDL_asprintf(&result, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
  2259. }
  2260. }
  2261. SDL_UnlockJoysticks();
  2262. return result;
  2263. }
  2264. /*
  2265. * Return 1 if the joystick with this name and GUID is a supported gamepad
  2266. */
  2267. bool SDL_IsGamepadNameAndGUID(const char *name, SDL_GUID guid)
  2268. {
  2269. bool result;
  2270. SDL_LockJoysticks();
  2271. {
  2272. if (s_pDefaultMapping || SDL_PrivateGetGamepadMappingForNameAndGUID(name, guid) != NULL) {
  2273. result = true;
  2274. } else {
  2275. result = false;
  2276. }
  2277. }
  2278. SDL_UnlockJoysticks();
  2279. return result;
  2280. }
  2281. /*
  2282. * Return 1 if the joystick at this device index is a supported gamepad
  2283. */
  2284. bool SDL_IsGamepad(SDL_JoystickID instance_id)
  2285. {
  2286. bool result;
  2287. SDL_LockJoysticks();
  2288. {
  2289. const void *value;
  2290. if (SDL_FindInHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)instance_id, &value)) {
  2291. result = (bool)(uintptr_t)value;
  2292. } else {
  2293. if (SDL_PrivateGetGamepadMapping(instance_id, true) != NULL) {
  2294. result = true;
  2295. } else {
  2296. result = false;
  2297. }
  2298. if (!s_gamepadInstanceIDs) {
  2299. s_gamepadInstanceIDs = SDL_CreateHashTable(NULL, 4, SDL_HashID, SDL_KeyMatchID, NULL, false, false);
  2300. }
  2301. SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)instance_id, (void *)(uintptr_t)result);
  2302. }
  2303. }
  2304. SDL_UnlockJoysticks();
  2305. return result;
  2306. }
  2307. /*
  2308. * Return 1 if the gamepad should be ignored by SDL
  2309. */
  2310. bool SDL_ShouldIgnoreGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  2311. {
  2312. #ifdef SDL_PLATFORM_LINUX
  2313. if (SDL_endswith(name, " Motion Sensors")) {
  2314. // Don't treat the PS3 and PS4 motion controls as a separate gamepad
  2315. return true;
  2316. }
  2317. if (SDL_strncmp(name, "Nintendo ", 9) == 0 && SDL_strstr(name, " IMU") != NULL) {
  2318. // Don't treat the Nintendo IMU as a separate gamepad
  2319. return true;
  2320. }
  2321. if (SDL_endswith(name, " Accelerometer") ||
  2322. SDL_endswith(name, " IR") ||
  2323. SDL_endswith(name, " Motion Plus") ||
  2324. SDL_endswith(name, " Nunchuk")) {
  2325. // Don't treat the Wii extension controls as a separate gamepad
  2326. return true;
  2327. }
  2328. #endif
  2329. if (name && SDL_strcmp(name, "uinput-fpc") == 0) {
  2330. // The Google Pixel fingerprint sensor reports itself as a joystick
  2331. return true;
  2332. }
  2333. #ifdef SDL_PLATFORM_WIN32
  2334. if (SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", false) &&
  2335. SDL_GetHintBoolean("STEAM_COMPAT_PROTON", false)) {
  2336. // We are launched by Steam and running under Proton
  2337. // We can't tell whether this controller is a Steam Virtual Gamepad,
  2338. // so assume that Proton is doing the appropriate filtering of controllers
  2339. // and anything we see here is fine to use.
  2340. return false;
  2341. }
  2342. #endif // SDL_PLATFORM_WIN32
  2343. if (SDL_IsJoystickSteamVirtualGamepad(vendor_id, product_id, version)) {
  2344. return !SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", false);
  2345. }
  2346. if (SDL_allowed_gamepads.num_included_entries > 0) {
  2347. if (SDL_VIDPIDInList(vendor_id, product_id, &SDL_allowed_gamepads)) {
  2348. return false;
  2349. }
  2350. return true;
  2351. } else {
  2352. if (SDL_VIDPIDInList(vendor_id, product_id, &SDL_ignored_gamepads)) {
  2353. return true;
  2354. }
  2355. return false;
  2356. }
  2357. }
  2358. /*
  2359. * Open a gamepad for use
  2360. *
  2361. * This function returns a gamepad identifier, or NULL if an error occurred.
  2362. */
  2363. SDL_Gamepad *SDL_OpenGamepad(SDL_JoystickID instance_id)
  2364. {
  2365. SDL_Gamepad *gamepad;
  2366. SDL_Gamepad *gamepadlist;
  2367. GamepadMapping_t *pSupportedGamepad = NULL;
  2368. SDL_LockJoysticks();
  2369. gamepadlist = SDL_gamepads;
  2370. // If the gamepad is already open, return it
  2371. while (gamepadlist) {
  2372. if (instance_id == gamepadlist->joystick->instance_id) {
  2373. gamepad = gamepadlist;
  2374. ++gamepad->ref_count;
  2375. SDL_UnlockJoysticks();
  2376. return gamepad;
  2377. }
  2378. gamepadlist = gamepadlist->next;
  2379. }
  2380. // Find a gamepad mapping
  2381. pSupportedGamepad = SDL_PrivateGetGamepadMapping(instance_id, true);
  2382. if (!pSupportedGamepad) {
  2383. SDL_SetError("Couldn't find mapping for device (%" SDL_PRIu32 ")", instance_id);
  2384. SDL_UnlockJoysticks();
  2385. return NULL;
  2386. }
  2387. // Create and initialize the gamepad
  2388. gamepad = (SDL_Gamepad *)SDL_calloc(1, sizeof(*gamepad));
  2389. if (!gamepad) {
  2390. SDL_UnlockJoysticks();
  2391. return NULL;
  2392. }
  2393. SDL_SetObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD, true);
  2394. gamepad->joystick = SDL_OpenJoystick(instance_id);
  2395. if (!gamepad->joystick) {
  2396. SDL_free(gamepad);
  2397. SDL_UnlockJoysticks();
  2398. return NULL;
  2399. }
  2400. if (gamepad->joystick->naxes) {
  2401. gamepad->last_match_axis = (SDL_GamepadBinding **)SDL_calloc(gamepad->joystick->naxes, sizeof(*gamepad->last_match_axis));
  2402. if (!gamepad->last_match_axis) {
  2403. SDL_CloseJoystick(gamepad->joystick);
  2404. SDL_free(gamepad);
  2405. SDL_UnlockJoysticks();
  2406. return NULL;
  2407. }
  2408. }
  2409. if (gamepad->joystick->nhats) {
  2410. gamepad->last_hat_mask = (Uint8 *)SDL_calloc(gamepad->joystick->nhats, sizeof(*gamepad->last_hat_mask));
  2411. if (!gamepad->last_hat_mask) {
  2412. SDL_CloseJoystick(gamepad->joystick);
  2413. SDL_free(gamepad->last_match_axis);
  2414. SDL_free(gamepad);
  2415. SDL_UnlockJoysticks();
  2416. return NULL;
  2417. }
  2418. }
  2419. SDL_PrivateLoadButtonMapping(gamepad, pSupportedGamepad);
  2420. // Add the gamepad to list
  2421. ++gamepad->ref_count;
  2422. // Link the gamepad in the list
  2423. gamepad->next = SDL_gamepads;
  2424. SDL_gamepads = gamepad;
  2425. SDL_UnlockJoysticks();
  2426. return gamepad;
  2427. }
  2428. /*
  2429. * Manually pump for gamepad updates.
  2430. */
  2431. void SDL_UpdateGamepads(void)
  2432. {
  2433. // Just for API completeness; the joystick API does all the work.
  2434. SDL_UpdateJoysticks();
  2435. }
  2436. /**
  2437. * Return whether a gamepad has a given axis
  2438. */
  2439. bool SDL_GamepadHasAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
  2440. {
  2441. bool result = false;
  2442. SDL_LockJoysticks();
  2443. {
  2444. int i;
  2445. CHECK_GAMEPAD_MAGIC(gamepad, false);
  2446. for (i = 0; i < gamepad->num_bindings; ++i) {
  2447. const SDL_GamepadBinding *binding = &gamepad->bindings[i];
  2448. if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
  2449. result = true;
  2450. break;
  2451. }
  2452. }
  2453. }
  2454. SDL_UnlockJoysticks();
  2455. return result;
  2456. }
  2457. /*
  2458. * Get the current state of an axis control on a gamepad
  2459. */
  2460. Sint16 SDL_GetGamepadAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
  2461. {
  2462. Sint16 result = 0;
  2463. SDL_LockJoysticks();
  2464. {
  2465. int i;
  2466. CHECK_GAMEPAD_MAGIC(gamepad, 0);
  2467. for (i = 0; i < gamepad->num_bindings; ++i) {
  2468. const SDL_GamepadBinding *binding = &gamepad->bindings[i];
  2469. if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
  2470. int value = 0;
  2471. bool valid_input_range;
  2472. bool valid_output_range;
  2473. if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
  2474. value = SDL_GetJoystickAxis(gamepad->joystick, binding->input.axis.axis);
  2475. if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
  2476. valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
  2477. } else {
  2478. valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
  2479. }
  2480. if (valid_input_range) {
  2481. if (binding->input.axis.axis_min != binding->output.axis.axis_min || binding->input.axis.axis_max != binding->output.axis.axis_max) {
  2482. float normalized_value = (float)(value - binding->input.axis.axis_min) / (binding->input.axis.axis_max - binding->input.axis.axis_min);
  2483. value = binding->output.axis.axis_min + (int)(normalized_value * (binding->output.axis.axis_max - binding->output.axis.axis_min));
  2484. }
  2485. } else {
  2486. value = 0;
  2487. }
  2488. } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON) {
  2489. if (SDL_GetJoystickButton(gamepad->joystick, binding->input.button)) {
  2490. value = binding->output.axis.axis_max;
  2491. }
  2492. } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_HAT) {
  2493. int hat_mask = SDL_GetJoystickHat(gamepad->joystick, binding->input.hat.hat);
  2494. if (hat_mask & binding->input.hat.hat_mask) {
  2495. value = binding->output.axis.axis_max;
  2496. }
  2497. }
  2498. if (binding->output.axis.axis_min < binding->output.axis.axis_max) {
  2499. valid_output_range = (value >= binding->output.axis.axis_min && value <= binding->output.axis.axis_max);
  2500. } else {
  2501. valid_output_range = (value >= binding->output.axis.axis_max && value <= binding->output.axis.axis_min);
  2502. }
  2503. // If the value is zero, there might be another binding that makes it non-zero
  2504. if (value != 0 && valid_output_range) {
  2505. result = (Sint16)value;
  2506. break;
  2507. }
  2508. }
  2509. }
  2510. }
  2511. SDL_UnlockJoysticks();
  2512. return result;
  2513. }
  2514. /**
  2515. * Return whether a gamepad has a given button
  2516. */
  2517. bool SDL_GamepadHasButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
  2518. {
  2519. bool result = false;
  2520. SDL_LockJoysticks();
  2521. {
  2522. int i;
  2523. CHECK_GAMEPAD_MAGIC(gamepad, false);
  2524. for (i = 0; i < gamepad->num_bindings; ++i) {
  2525. const SDL_GamepadBinding *binding = &gamepad->bindings[i];
  2526. if (binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON && binding->output.button == button) {
  2527. result = true;
  2528. break;
  2529. }
  2530. }
  2531. }
  2532. SDL_UnlockJoysticks();
  2533. return result;
  2534. }
  2535. /*
  2536. * Get the current state of a button on a gamepad
  2537. */
  2538. bool SDL_GetGamepadButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
  2539. {
  2540. bool result = false;
  2541. SDL_LockJoysticks();
  2542. {
  2543. int i;
  2544. CHECK_GAMEPAD_MAGIC(gamepad, false);
  2545. for (i = 0; i < gamepad->num_bindings; ++i) {
  2546. const SDL_GamepadBinding *binding = &gamepad->bindings[i];
  2547. if (binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON && binding->output.button == button) {
  2548. if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
  2549. bool valid_input_range;
  2550. int value = SDL_GetJoystickAxis(gamepad->joystick, binding->input.axis.axis);
  2551. int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
  2552. if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
  2553. valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
  2554. if (valid_input_range) {
  2555. result |= (value >= threshold);
  2556. }
  2557. } else {
  2558. valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
  2559. if (valid_input_range) {
  2560. result |= (value <= threshold);
  2561. }
  2562. }
  2563. } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON) {
  2564. result |= SDL_GetJoystickButton(gamepad->joystick, binding->input.button);
  2565. } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_HAT) {
  2566. int hat_mask = SDL_GetJoystickHat(gamepad->joystick, binding->input.hat.hat);
  2567. result |= ((hat_mask & binding->input.hat.hat_mask) != 0);
  2568. }
  2569. }
  2570. }
  2571. }
  2572. SDL_UnlockJoysticks();
  2573. return result;
  2574. }
  2575. /**
  2576. * Get the label of a button on a gamepad.
  2577. */
  2578. static SDL_GamepadButtonLabel SDL_GetGamepadButtonLabelForFaceStyle(SDL_GamepadFaceStyle face_style, SDL_GamepadButton button)
  2579. {
  2580. SDL_GamepadButtonLabel label = SDL_GAMEPAD_BUTTON_LABEL_UNKNOWN;
  2581. switch (face_style) {
  2582. case SDL_GAMEPAD_FACE_STYLE_ABXY:
  2583. switch (button) {
  2584. case SDL_GAMEPAD_BUTTON_SOUTH:
  2585. label = SDL_GAMEPAD_BUTTON_LABEL_A;
  2586. break;
  2587. case SDL_GAMEPAD_BUTTON_EAST:
  2588. label = SDL_GAMEPAD_BUTTON_LABEL_B;
  2589. break;
  2590. case SDL_GAMEPAD_BUTTON_WEST:
  2591. label = SDL_GAMEPAD_BUTTON_LABEL_X;
  2592. break;
  2593. case SDL_GAMEPAD_BUTTON_NORTH:
  2594. label = SDL_GAMEPAD_BUTTON_LABEL_Y;
  2595. break;
  2596. default:
  2597. break;
  2598. }
  2599. break;
  2600. case SDL_GAMEPAD_FACE_STYLE_BAYX:
  2601. switch (button) {
  2602. case SDL_GAMEPAD_BUTTON_SOUTH:
  2603. label = SDL_GAMEPAD_BUTTON_LABEL_B;
  2604. break;
  2605. case SDL_GAMEPAD_BUTTON_EAST:
  2606. label = SDL_GAMEPAD_BUTTON_LABEL_A;
  2607. break;
  2608. case SDL_GAMEPAD_BUTTON_WEST:
  2609. label = SDL_GAMEPAD_BUTTON_LABEL_Y;
  2610. break;
  2611. case SDL_GAMEPAD_BUTTON_NORTH:
  2612. label = SDL_GAMEPAD_BUTTON_LABEL_X;
  2613. break;
  2614. default:
  2615. break;
  2616. }
  2617. break;
  2618. case SDL_GAMEPAD_FACE_STYLE_SONY:
  2619. switch (button) {
  2620. case SDL_GAMEPAD_BUTTON_SOUTH:
  2621. label = SDL_GAMEPAD_BUTTON_LABEL_CROSS;
  2622. break;
  2623. case SDL_GAMEPAD_BUTTON_EAST:
  2624. label = SDL_GAMEPAD_BUTTON_LABEL_CIRCLE;
  2625. break;
  2626. case SDL_GAMEPAD_BUTTON_WEST:
  2627. label = SDL_GAMEPAD_BUTTON_LABEL_SQUARE;
  2628. break;
  2629. case SDL_GAMEPAD_BUTTON_NORTH:
  2630. label = SDL_GAMEPAD_BUTTON_LABEL_TRIANGLE;
  2631. break;
  2632. default:
  2633. break;
  2634. }
  2635. break;
  2636. default:
  2637. break;
  2638. }
  2639. return label;
  2640. }
  2641. /**
  2642. * Get the label of a button on a gamepad.
  2643. */
  2644. SDL_GamepadButtonLabel SDL_GetGamepadButtonLabelForType(SDL_GamepadType type, SDL_GamepadButton button)
  2645. {
  2646. return SDL_GetGamepadButtonLabelForFaceStyle(SDL_GetGamepadFaceStyleForGamepadType(type), button);
  2647. }
  2648. /**
  2649. * Get the label of a button on a gamepad.
  2650. */
  2651. SDL_GamepadButtonLabel SDL_GetGamepadButtonLabel(SDL_Gamepad *gamepad, SDL_GamepadButton button)
  2652. {
  2653. SDL_GamepadFaceStyle face_style;
  2654. SDL_LockJoysticks();
  2655. {
  2656. CHECK_GAMEPAD_MAGIC(gamepad, SDL_GAMEPAD_BUTTON_LABEL_UNKNOWN);
  2657. face_style = gamepad->face_style;
  2658. }
  2659. SDL_UnlockJoysticks();
  2660. return SDL_GetGamepadButtonLabelForFaceStyle(face_style, button);
  2661. }
  2662. /**
  2663. * Get the number of touchpads on a gamepad.
  2664. */
  2665. int SDL_GetNumGamepadTouchpads(SDL_Gamepad *gamepad)
  2666. {
  2667. int result = true;
  2668. SDL_LockJoysticks();
  2669. {
  2670. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2671. if (joystick) {
  2672. result = joystick->ntouchpads;
  2673. }
  2674. }
  2675. SDL_UnlockJoysticks();
  2676. return result;
  2677. }
  2678. /**
  2679. * Get the number of supported simultaneous fingers on a touchpad on a gamepad.
  2680. */
  2681. int SDL_GetNumGamepadTouchpadFingers(SDL_Gamepad *gamepad, int touchpad)
  2682. {
  2683. int result = true;
  2684. SDL_LockJoysticks();
  2685. {
  2686. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2687. if (joystick) {
  2688. if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
  2689. result = joystick->touchpads[touchpad].nfingers;
  2690. } else {
  2691. result = SDL_InvalidParamError("touchpad");
  2692. }
  2693. }
  2694. }
  2695. SDL_UnlockJoysticks();
  2696. return result;
  2697. }
  2698. /**
  2699. * Get the current state of a finger on a touchpad on a gamepad.
  2700. */
  2701. bool SDL_GetGamepadTouchpadFinger(SDL_Gamepad *gamepad, int touchpad, int finger, bool *down, float *x, float *y, float *pressure)
  2702. {
  2703. bool result = false;
  2704. SDL_LockJoysticks();
  2705. {
  2706. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2707. if (joystick) {
  2708. if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
  2709. SDL_JoystickTouchpadInfo *touchpad_info = &joystick->touchpads[touchpad];
  2710. if (finger >= 0 && finger < touchpad_info->nfingers) {
  2711. SDL_JoystickTouchpadFingerInfo *info = &touchpad_info->fingers[finger];
  2712. if (down) {
  2713. *down = info->down;
  2714. }
  2715. if (x) {
  2716. *x = info->x;
  2717. }
  2718. if (y) {
  2719. *y = info->y;
  2720. }
  2721. if (pressure) {
  2722. *pressure = info->pressure;
  2723. }
  2724. result = true;
  2725. } else {
  2726. result = SDL_InvalidParamError("finger");
  2727. }
  2728. } else {
  2729. result = SDL_InvalidParamError("touchpad");
  2730. }
  2731. }
  2732. }
  2733. SDL_UnlockJoysticks();
  2734. return result;
  2735. }
  2736. /**
  2737. * Return whether a gamepad has a particular sensor.
  2738. */
  2739. bool SDL_GamepadHasSensor(SDL_Gamepad *gamepad, SDL_SensorType type)
  2740. {
  2741. bool result = false;
  2742. SDL_LockJoysticks();
  2743. {
  2744. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2745. if (joystick) {
  2746. int i;
  2747. for (i = 0; i < joystick->nsensors; ++i) {
  2748. if (joystick->sensors[i].type == type) {
  2749. result = true;
  2750. break;
  2751. }
  2752. }
  2753. }
  2754. }
  2755. SDL_UnlockJoysticks();
  2756. return result;
  2757. }
  2758. /*
  2759. * Set whether data reporting for a gamepad sensor is enabled
  2760. */
  2761. bool SDL_SetGamepadSensorEnabled(SDL_Gamepad *gamepad, SDL_SensorType type, bool enabled)
  2762. {
  2763. SDL_LockJoysticks();
  2764. {
  2765. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2766. if (joystick) {
  2767. int i;
  2768. for (i = 0; i < joystick->nsensors; ++i) {
  2769. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  2770. if (sensor->type == type) {
  2771. if (sensor->enabled == (enabled != false)) {
  2772. SDL_UnlockJoysticks();
  2773. return true;
  2774. }
  2775. if (type == SDL_SENSOR_ACCEL && joystick->accel_sensor) {
  2776. if (enabled) {
  2777. joystick->accel = SDL_OpenSensor(joystick->accel_sensor);
  2778. if (!joystick->accel) {
  2779. SDL_UnlockJoysticks();
  2780. return false;
  2781. }
  2782. } else {
  2783. if (joystick->accel) {
  2784. SDL_CloseSensor(joystick->accel);
  2785. joystick->accel = NULL;
  2786. }
  2787. }
  2788. } else if (type == SDL_SENSOR_GYRO && joystick->gyro_sensor) {
  2789. if (enabled) {
  2790. joystick->gyro = SDL_OpenSensor(joystick->gyro_sensor);
  2791. if (!joystick->gyro) {
  2792. SDL_UnlockJoysticks();
  2793. return false;
  2794. }
  2795. } else {
  2796. if (joystick->gyro) {
  2797. SDL_CloseSensor(joystick->gyro);
  2798. joystick->gyro = NULL;
  2799. }
  2800. }
  2801. } else {
  2802. if (enabled) {
  2803. if (joystick->nsensors_enabled == 0) {
  2804. if (!joystick->driver->SetSensorsEnabled(joystick, true)) {
  2805. SDL_UnlockJoysticks();
  2806. return false;
  2807. }
  2808. }
  2809. ++joystick->nsensors_enabled;
  2810. } else {
  2811. if (joystick->nsensors_enabled == 1) {
  2812. if (!joystick->driver->SetSensorsEnabled(joystick, false)) {
  2813. SDL_UnlockJoysticks();
  2814. return false;
  2815. }
  2816. }
  2817. --joystick->nsensors_enabled;
  2818. }
  2819. }
  2820. sensor->enabled = enabled;
  2821. SDL_UnlockJoysticks();
  2822. return true;
  2823. }
  2824. }
  2825. }
  2826. }
  2827. SDL_UnlockJoysticks();
  2828. return SDL_Unsupported();
  2829. }
  2830. /*
  2831. * Query whether sensor data reporting is enabled for a gamepad
  2832. */
  2833. bool SDL_GamepadSensorEnabled(SDL_Gamepad *gamepad, SDL_SensorType type)
  2834. {
  2835. bool result = false;
  2836. SDL_LockJoysticks();
  2837. {
  2838. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2839. if (joystick) {
  2840. int i;
  2841. for (i = 0; i < joystick->nsensors; ++i) {
  2842. if (joystick->sensors[i].type == type) {
  2843. result = joystick->sensors[i].enabled;
  2844. break;
  2845. }
  2846. }
  2847. }
  2848. }
  2849. SDL_UnlockJoysticks();
  2850. return result;
  2851. }
  2852. /*
  2853. * Get the data rate of a gamepad sensor.
  2854. */
  2855. float SDL_GetGamepadSensorDataRate(SDL_Gamepad *gamepad, SDL_SensorType type)
  2856. {
  2857. float result = 0.0f;
  2858. SDL_LockJoysticks();
  2859. {
  2860. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2861. if (joystick) {
  2862. int i;
  2863. for (i = 0; i < joystick->nsensors; ++i) {
  2864. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  2865. if (sensor->type == type) {
  2866. result = sensor->rate;
  2867. break;
  2868. }
  2869. }
  2870. }
  2871. }
  2872. SDL_UnlockJoysticks();
  2873. return result;
  2874. }
  2875. /*
  2876. * Get the current state of a gamepad sensor.
  2877. */
  2878. bool SDL_GetGamepadSensorData(SDL_Gamepad *gamepad, SDL_SensorType type, float *data, int num_values)
  2879. {
  2880. SDL_LockJoysticks();
  2881. {
  2882. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2883. if (joystick) {
  2884. int i;
  2885. for (i = 0; i < joystick->nsensors; ++i) {
  2886. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  2887. if (sensor->type == type) {
  2888. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  2889. SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
  2890. SDL_UnlockJoysticks();
  2891. return true;
  2892. }
  2893. }
  2894. }
  2895. }
  2896. SDL_UnlockJoysticks();
  2897. return SDL_Unsupported();
  2898. }
  2899. SDL_JoystickID SDL_GetGamepadID(SDL_Gamepad *gamepad)
  2900. {
  2901. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2902. if (!joystick) {
  2903. return 0;
  2904. }
  2905. return SDL_GetJoystickID(joystick);
  2906. }
  2907. SDL_PropertiesID SDL_GetGamepadProperties(SDL_Gamepad *gamepad)
  2908. {
  2909. SDL_PropertiesID result = true;
  2910. SDL_LockJoysticks();
  2911. {
  2912. CHECK_GAMEPAD_MAGIC(gamepad, 0);
  2913. result = SDL_GetJoystickProperties(gamepad->joystick);
  2914. }
  2915. SDL_UnlockJoysticks();
  2916. return result;
  2917. }
  2918. const char *SDL_GetGamepadName(SDL_Gamepad *gamepad)
  2919. {
  2920. const char *result = NULL;
  2921. SDL_LockJoysticks();
  2922. {
  2923. CHECK_GAMEPAD_MAGIC(gamepad, NULL);
  2924. if (SDL_strcmp(gamepad->name, "*") == 0 ||
  2925. gamepad->joystick->steam_handle != 0) {
  2926. result = SDL_GetJoystickName(gamepad->joystick);
  2927. } else {
  2928. result = SDL_GetPersistentString(gamepad->name);
  2929. }
  2930. }
  2931. SDL_UnlockJoysticks();
  2932. return result;
  2933. }
  2934. const char *SDL_GetGamepadPath(SDL_Gamepad *gamepad)
  2935. {
  2936. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2937. if (!joystick) {
  2938. return NULL;
  2939. }
  2940. return SDL_GetJoystickPath(joystick);
  2941. }
  2942. SDL_GamepadType SDL_GetGamepadType(SDL_Gamepad *gamepad)
  2943. {
  2944. SDL_GamepadType type;
  2945. const SDL_SteamVirtualGamepadInfo *info;
  2946. SDL_LockJoysticks();
  2947. {
  2948. CHECK_GAMEPAD_MAGIC(gamepad, SDL_GAMEPAD_TYPE_UNKNOWN);
  2949. info = SDL_GetJoystickVirtualGamepadInfoForID(gamepad->joystick->instance_id);
  2950. if (info) {
  2951. type = info->type;
  2952. } else {
  2953. type = gamepad->type;
  2954. }
  2955. }
  2956. SDL_UnlockJoysticks();
  2957. return type;
  2958. }
  2959. SDL_GamepadType SDL_GetRealGamepadType(SDL_Gamepad *gamepad)
  2960. {
  2961. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2962. if (!joystick) {
  2963. return SDL_GAMEPAD_TYPE_UNKNOWN;
  2964. }
  2965. return SDL_GetGamepadTypeFromGUID(SDL_GetJoystickGUID(joystick), SDL_GetJoystickName(joystick));
  2966. }
  2967. int SDL_GetGamepadPlayerIndex(SDL_Gamepad *gamepad)
  2968. {
  2969. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2970. if (!joystick) {
  2971. return -1;
  2972. }
  2973. return SDL_GetJoystickPlayerIndex(joystick);
  2974. }
  2975. /**
  2976. * Set the player index of an opened gamepad
  2977. */
  2978. bool SDL_SetGamepadPlayerIndex(SDL_Gamepad *gamepad, int player_index)
  2979. {
  2980. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2981. if (!joystick) {
  2982. // SDL_SetError() will have been called already by SDL_GetGamepadJoystick()
  2983. return false;
  2984. }
  2985. return SDL_SetJoystickPlayerIndex(joystick, player_index);
  2986. }
  2987. Uint16 SDL_GetGamepadVendor(SDL_Gamepad *gamepad)
  2988. {
  2989. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2990. if (!joystick) {
  2991. return 0;
  2992. }
  2993. return SDL_GetJoystickVendor(joystick);
  2994. }
  2995. Uint16 SDL_GetGamepadProduct(SDL_Gamepad *gamepad)
  2996. {
  2997. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  2998. if (!joystick) {
  2999. return 0;
  3000. }
  3001. return SDL_GetJoystickProduct(joystick);
  3002. }
  3003. Uint16 SDL_GetGamepadProductVersion(SDL_Gamepad *gamepad)
  3004. {
  3005. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3006. if (!joystick) {
  3007. return 0;
  3008. }
  3009. return SDL_GetJoystickProductVersion(joystick);
  3010. }
  3011. Uint16 SDL_GetGamepadFirmwareVersion(SDL_Gamepad *gamepad)
  3012. {
  3013. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3014. if (!joystick) {
  3015. return 0;
  3016. }
  3017. return SDL_GetJoystickFirmwareVersion(joystick);
  3018. }
  3019. const char * SDL_GetGamepadSerial(SDL_Gamepad *gamepad)
  3020. {
  3021. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3022. if (!joystick) {
  3023. return NULL;
  3024. }
  3025. return SDL_GetJoystickSerial(joystick);
  3026. }
  3027. Uint64 SDL_GetGamepadSteamHandle(SDL_Gamepad *gamepad)
  3028. {
  3029. Uint64 handle = 0;
  3030. SDL_LockJoysticks();
  3031. {
  3032. CHECK_GAMEPAD_MAGIC(gamepad, 0);
  3033. handle = gamepad->joystick->steam_handle;
  3034. }
  3035. SDL_UnlockJoysticks();
  3036. return handle;
  3037. }
  3038. SDL_JoystickConnectionState SDL_GetGamepadConnectionState(SDL_Gamepad *gamepad)
  3039. {
  3040. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3041. if (!joystick) {
  3042. return SDL_JOYSTICK_CONNECTION_INVALID;
  3043. }
  3044. return SDL_GetJoystickConnectionState(joystick);
  3045. }
  3046. SDL_PowerState SDL_GetGamepadPowerInfo(SDL_Gamepad *gamepad, int *percent)
  3047. {
  3048. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3049. if (percent) {
  3050. *percent = -1;
  3051. }
  3052. if (!joystick) {
  3053. return SDL_POWERSTATE_ERROR;
  3054. }
  3055. return SDL_GetJoystickPowerInfo(joystick, percent);
  3056. }
  3057. /*
  3058. * Return if the gamepad in question is currently attached to the system,
  3059. * \return 0 if not plugged in, 1 if still present.
  3060. */
  3061. bool SDL_GamepadConnected(SDL_Gamepad *gamepad)
  3062. {
  3063. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3064. if (!joystick) {
  3065. return false;
  3066. }
  3067. return SDL_JoystickConnected(joystick);
  3068. }
  3069. /*
  3070. * Get the joystick for this gamepad
  3071. */
  3072. SDL_Joystick *SDL_GetGamepadJoystick(SDL_Gamepad *gamepad)
  3073. {
  3074. SDL_Joystick *joystick;
  3075. SDL_LockJoysticks();
  3076. {
  3077. CHECK_GAMEPAD_MAGIC(gamepad, NULL);
  3078. joystick = gamepad->joystick;
  3079. }
  3080. SDL_UnlockJoysticks();
  3081. return joystick;
  3082. }
  3083. /*
  3084. * Return the SDL_Gamepad associated with an instance id.
  3085. */
  3086. SDL_Gamepad *SDL_GetGamepadFromID(SDL_JoystickID joyid)
  3087. {
  3088. SDL_Gamepad *gamepad;
  3089. SDL_LockJoysticks();
  3090. gamepad = SDL_gamepads;
  3091. while (gamepad) {
  3092. if (gamepad->joystick->instance_id == joyid) {
  3093. SDL_UnlockJoysticks();
  3094. return gamepad;
  3095. }
  3096. gamepad = gamepad->next;
  3097. }
  3098. SDL_UnlockJoysticks();
  3099. return NULL;
  3100. }
  3101. /**
  3102. * Return the SDL_Gamepad associated with a player index.
  3103. */
  3104. SDL_Gamepad *SDL_GetGamepadFromPlayerIndex(int player_index)
  3105. {
  3106. SDL_Gamepad *result = NULL;
  3107. SDL_LockJoysticks();
  3108. {
  3109. SDL_Joystick *joystick = SDL_GetJoystickFromPlayerIndex(player_index);
  3110. if (joystick) {
  3111. result = SDL_GetGamepadFromID(joystick->instance_id);
  3112. }
  3113. }
  3114. SDL_UnlockJoysticks();
  3115. return result;
  3116. }
  3117. /*
  3118. * Get the SDL joystick layer bindings for this gamepad
  3119. */
  3120. SDL_GamepadBinding **SDL_GetGamepadBindings(SDL_Gamepad *gamepad, int *count)
  3121. {
  3122. SDL_GamepadBinding **bindings = NULL;
  3123. if (count) {
  3124. *count = 0;
  3125. }
  3126. SDL_LockJoysticks();
  3127. {
  3128. CHECK_GAMEPAD_MAGIC(gamepad, NULL);
  3129. size_t pointers_size = ((gamepad->num_bindings + 1) * sizeof(SDL_GamepadBinding *));
  3130. size_t elements_size = (gamepad->num_bindings * sizeof(SDL_GamepadBinding));
  3131. bindings = (SDL_GamepadBinding **)SDL_malloc(pointers_size + elements_size);
  3132. if (bindings) {
  3133. SDL_GamepadBinding *binding = (SDL_GamepadBinding *)((Uint8 *)bindings + pointers_size);
  3134. int i;
  3135. for (i = 0; i < gamepad->num_bindings; ++i, ++binding) {
  3136. bindings[i] = binding;
  3137. SDL_copyp(binding, &gamepad->bindings[i]);
  3138. }
  3139. bindings[i] = NULL;
  3140. if (count) {
  3141. *count = gamepad->num_bindings;
  3142. }
  3143. }
  3144. }
  3145. SDL_UnlockJoysticks();
  3146. return bindings;
  3147. }
  3148. bool SDL_RumbleGamepad(SDL_Gamepad *gamepad, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  3149. {
  3150. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3151. if (!joystick) {
  3152. return false;
  3153. }
  3154. return SDL_RumbleJoystick(joystick, low_frequency_rumble, high_frequency_rumble, duration_ms);
  3155. }
  3156. bool SDL_RumbleGamepadTriggers(SDL_Gamepad *gamepad, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
  3157. {
  3158. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3159. if (!joystick) {
  3160. return false;
  3161. }
  3162. return SDL_RumbleJoystickTriggers(joystick, left_rumble, right_rumble, duration_ms);
  3163. }
  3164. bool SDL_SetGamepadLED(SDL_Gamepad *gamepad, Uint8 red, Uint8 green, Uint8 blue)
  3165. {
  3166. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3167. if (!joystick) {
  3168. return false;
  3169. }
  3170. return SDL_SetJoystickLED(joystick, red, green, blue);
  3171. }
  3172. bool SDL_SendGamepadEffect(SDL_Gamepad *gamepad, const void *data, int size)
  3173. {
  3174. SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
  3175. if (!joystick) {
  3176. return false;
  3177. }
  3178. return SDL_SendJoystickEffect(joystick, data, size);
  3179. }
  3180. void SDL_CloseGamepad(SDL_Gamepad *gamepad)
  3181. {
  3182. SDL_Gamepad *gamepadlist, *gamepadlistprev;
  3183. SDL_LockJoysticks();
  3184. if (!SDL_ObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD)) {
  3185. SDL_UnlockJoysticks();
  3186. return;
  3187. }
  3188. // First decrement ref count
  3189. if (--gamepad->ref_count > 0) {
  3190. SDL_UnlockJoysticks();
  3191. return;
  3192. }
  3193. SDL_CloseJoystick(gamepad->joystick);
  3194. gamepadlist = SDL_gamepads;
  3195. gamepadlistprev = NULL;
  3196. while (gamepadlist) {
  3197. if (gamepad == gamepadlist) {
  3198. if (gamepadlistprev) {
  3199. // unlink this entry
  3200. gamepadlistprev->next = gamepadlist->next;
  3201. } else {
  3202. SDL_gamepads = gamepad->next;
  3203. }
  3204. break;
  3205. }
  3206. gamepadlistprev = gamepadlist;
  3207. gamepadlist = gamepadlist->next;
  3208. }
  3209. SDL_SetObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD, false);
  3210. SDL_free(gamepad->bindings);
  3211. SDL_free(gamepad->last_match_axis);
  3212. SDL_free(gamepad->last_hat_mask);
  3213. SDL_free(gamepad);
  3214. SDL_UnlockJoysticks();
  3215. }
  3216. /*
  3217. * Quit the gamepad subsystem
  3218. */
  3219. void SDL_QuitGamepads(void)
  3220. {
  3221. SDL_Gamepad *gamepad;
  3222. SDL_LockJoysticks();
  3223. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  3224. SDL_PrivateGamepadRemoved(gamepad->joystick->instance_id);
  3225. }
  3226. SDL_gamepads_initialized = false;
  3227. SDL_RemoveEventWatch(SDL_GamepadEventWatcher, NULL);
  3228. while (SDL_gamepads) {
  3229. SDL_gamepads->ref_count = 1;
  3230. SDL_CloseGamepad(SDL_gamepads);
  3231. }
  3232. SDL_UnlockJoysticks();
  3233. }
  3234. void SDL_QuitGamepadMappings(void)
  3235. {
  3236. GamepadMapping_t *pGamepadMap;
  3237. SDL_AssertJoysticksLocked();
  3238. while (s_pSupportedGamepads) {
  3239. pGamepadMap = s_pSupportedGamepads;
  3240. s_pSupportedGamepads = s_pSupportedGamepads->next;
  3241. SDL_free(pGamepadMap->name);
  3242. SDL_free(pGamepadMap->mapping);
  3243. SDL_free(pGamepadMap);
  3244. }
  3245. SDL_FreeVIDPIDList(&SDL_allowed_gamepads);
  3246. SDL_FreeVIDPIDList(&SDL_ignored_gamepads);
  3247. if (s_gamepadInstanceIDs) {
  3248. SDL_DestroyHashTable(s_gamepadInstanceIDs);
  3249. s_gamepadInstanceIDs = NULL;
  3250. }
  3251. }
  3252. /*
  3253. * Event filter to transform joystick events into appropriate gamepad ones
  3254. */
  3255. static void SDL_SendGamepadAxis(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadAxis axis, Sint16 value)
  3256. {
  3257. SDL_AssertJoysticksLocked();
  3258. // translate the event, if desired
  3259. if (SDL_EventEnabled(SDL_EVENT_GAMEPAD_AXIS_MOTION)) {
  3260. SDL_Event event;
  3261. event.type = SDL_EVENT_GAMEPAD_AXIS_MOTION;
  3262. event.common.timestamp = timestamp;
  3263. event.gaxis.which = gamepad->joystick->instance_id;
  3264. event.gaxis.axis = axis;
  3265. event.gaxis.value = value;
  3266. SDL_PushEvent(&event);
  3267. }
  3268. }
  3269. static void SDL_SendGamepadButton(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadButton button, bool down)
  3270. {
  3271. SDL_Event event;
  3272. SDL_AssertJoysticksLocked();
  3273. if (button == SDL_GAMEPAD_BUTTON_INVALID) {
  3274. return;
  3275. }
  3276. if (down) {
  3277. event.type = SDL_EVENT_GAMEPAD_BUTTON_DOWN;
  3278. } else {
  3279. event.type = SDL_EVENT_GAMEPAD_BUTTON_UP;
  3280. }
  3281. if (button == SDL_GAMEPAD_BUTTON_GUIDE) {
  3282. Uint64 now = SDL_GetTicks();
  3283. if (down) {
  3284. gamepad->guide_button_down = now;
  3285. if (gamepad->joystick->delayed_guide_button) {
  3286. // Skip duplicate press
  3287. return;
  3288. }
  3289. } else {
  3290. if (now < (gamepad->guide_button_down + SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS)) {
  3291. gamepad->joystick->delayed_guide_button = true;
  3292. return;
  3293. }
  3294. gamepad->joystick->delayed_guide_button = false;
  3295. }
  3296. }
  3297. // translate the event, if desired
  3298. if (SDL_EventEnabled(event.type)) {
  3299. event.common.timestamp = timestamp;
  3300. event.gbutton.which = gamepad->joystick->instance_id;
  3301. event.gbutton.button = button;
  3302. event.gbutton.down = down;
  3303. SDL_PushEvent(&event);
  3304. }
  3305. }
  3306. static const Uint32 SDL_gamepad_event_list[] = {
  3307. SDL_EVENT_GAMEPAD_AXIS_MOTION,
  3308. SDL_EVENT_GAMEPAD_BUTTON_DOWN,
  3309. SDL_EVENT_GAMEPAD_BUTTON_UP,
  3310. SDL_EVENT_GAMEPAD_ADDED,
  3311. SDL_EVENT_GAMEPAD_REMOVED,
  3312. SDL_EVENT_GAMEPAD_REMAPPED,
  3313. SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN,
  3314. SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION,
  3315. SDL_EVENT_GAMEPAD_TOUCHPAD_UP,
  3316. SDL_EVENT_GAMEPAD_SENSOR_UPDATE,
  3317. };
  3318. void SDL_SetGamepadEventsEnabled(bool enabled)
  3319. {
  3320. unsigned int i;
  3321. for (i = 0; i < SDL_arraysize(SDL_gamepad_event_list); ++i) {
  3322. SDL_SetEventEnabled(SDL_gamepad_event_list[i], enabled);
  3323. }
  3324. }
  3325. bool SDL_GamepadEventsEnabled(void)
  3326. {
  3327. bool enabled = false;
  3328. unsigned int i;
  3329. for (i = 0; i < SDL_arraysize(SDL_gamepad_event_list); ++i) {
  3330. enabled = SDL_EventEnabled(SDL_gamepad_event_list[i]);
  3331. if (enabled) {
  3332. break;
  3333. }
  3334. }
  3335. return enabled;
  3336. }
  3337. void SDL_GamepadHandleDelayedGuideButton(SDL_Joystick *joystick)
  3338. {
  3339. SDL_Gamepad *gamepad;
  3340. SDL_AssertJoysticksLocked();
  3341. for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
  3342. if (gamepad->joystick == joystick) {
  3343. SDL_SendGamepadButton(0, gamepad, SDL_GAMEPAD_BUTTON_GUIDE, false);
  3344. // Make sure we send an update complete event for this change
  3345. if (!gamepad->joystick->update_complete) {
  3346. gamepad->joystick->update_complete = SDL_GetTicksNS();
  3347. }
  3348. break;
  3349. }
  3350. }
  3351. }
  3352. const char *SDL_GetGamepadAppleSFSymbolsNameForButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
  3353. {
  3354. const char *result = NULL;
  3355. #ifdef SDL_JOYSTICK_MFI
  3356. const char *IOS_GetAppleSFSymbolsNameForButton(SDL_Gamepad *gamepad, SDL_GamepadButton button);
  3357. SDL_LockJoysticks();
  3358. {
  3359. CHECK_GAMEPAD_MAGIC(gamepad, NULL);
  3360. result = IOS_GetAppleSFSymbolsNameForButton(gamepad, button);
  3361. }
  3362. SDL_UnlockJoysticks();
  3363. #endif
  3364. return result;
  3365. }
  3366. const char *SDL_GetGamepadAppleSFSymbolsNameForAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
  3367. {
  3368. const char *result = NULL;
  3369. #ifdef SDL_JOYSTICK_MFI
  3370. const char *IOS_GetAppleSFSymbolsNameForAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis);
  3371. SDL_LockJoysticks();
  3372. {
  3373. CHECK_GAMEPAD_MAGIC(gamepad, NULL);
  3374. result = IOS_GetAppleSFSymbolsNameForAxis(gamepad, axis);
  3375. }
  3376. SDL_UnlockJoysticks();
  3377. #endif
  3378. return result;
  3379. }