SDL_gamepad.c 133 KB

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