SDL_gamepad.c 126 KB

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