SDL_gamepad.c 126 KB

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