| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906 |
- /*
- Simple DirectMedia Layer
- Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- #include "SDL_internal.h"
- // This is the gamepad API for Simple DirectMedia Layer
- #include "SDL_sysjoystick.h"
- #include "SDL_joystick_c.h"
- #include "SDL_steam_virtual_gamepad.h"
- #include "SDL_gamepad_c.h"
- #include "SDL_gamepad_db.h"
- #include "controller_type.h"
- #include "usb_ids.h"
- #include "hidapi/SDL_hidapi_nintendo.h"
- #include "../events/SDL_events_c.h"
- #ifdef SDL_PLATFORM_ANDROID
- #endif
- // Many gamepads turn the center button into an instantaneous button press
- #define SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS 250
- #define SDL_GAMEPAD_CRC_FIELD "crc:"
- #define SDL_GAMEPAD_CRC_FIELD_SIZE 4 // hard-coded for speed
- #define SDL_GAMEPAD_TYPE_FIELD "type:"
- #define SDL_GAMEPAD_TYPE_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_TYPE_FIELD)
- #define SDL_GAMEPAD_FACE_FIELD "face:"
- #define SDL_GAMEPAD_FACE_FIELD_SIZE 5 // hard-coded for speed
- #define SDL_GAMEPAD_PLATFORM_FIELD "platform:"
- #define SDL_GAMEPAD_PLATFORM_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_PLATFORM_FIELD)
- #define SDL_GAMEPAD_HINT_FIELD "hint:"
- #define SDL_GAMEPAD_HINT_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_HINT_FIELD)
- #define SDL_GAMEPAD_SDKGE_FIELD "sdk>=:"
- #define SDL_GAMEPAD_SDKGE_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_SDKGE_FIELD)
- #define SDL_GAMEPAD_SDKLE_FIELD "sdk<=:"
- #define SDL_GAMEPAD_SDKLE_FIELD_SIZE SDL_strlen(SDL_GAMEPAD_SDKLE_FIELD)
- static bool SDL_gamepads_initialized;
- static SDL_Gamepad *SDL_gamepads SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
- // The face button style of a gamepad
- typedef enum
- {
- SDL_GAMEPAD_FACE_STYLE_UNKNOWN,
- SDL_GAMEPAD_FACE_STYLE_ABXY,
- SDL_GAMEPAD_FACE_STYLE_BAYX,
- SDL_GAMEPAD_FACE_STYLE_SONY,
- } SDL_GamepadFaceStyle;
- // our hard coded list of mapping support
- typedef enum
- {
- SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT,
- SDL_GAMEPAD_MAPPING_PRIORITY_API,
- SDL_GAMEPAD_MAPPING_PRIORITY_USER,
- } SDL_GamepadMappingPriority;
- #define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
- typedef struct GamepadMapping_t
- {
- SDL_GUID guid _guarded;
- char *name _guarded;
- char *mapping _guarded;
- SDL_GamepadMappingPriority priority _guarded;
- struct GamepadMapping_t *next _guarded;
- } GamepadMapping_t;
- typedef struct
- {
- int refcount _guarded;
- SDL_JoystickID *joysticks _guarded;
- GamepadMapping_t **joystick_mappings _guarded;
- int num_changed_mappings _guarded;
- GamepadMapping_t **changed_mappings _guarded;
- } MappingChangeTracker;
- #undef _guarded
- static SDL_GUID s_zeroGUID;
- static GamepadMapping_t *s_pSupportedGamepads SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
- static GamepadMapping_t *s_pDefaultMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
- static GamepadMapping_t *s_pXInputMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
- static MappingChangeTracker *s_mappingChangeTracker SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
- static SDL_HashTable *s_gamepadInstanceIDs SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
- #define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
- // The SDL gamepad structure
- struct SDL_Gamepad
- {
- SDL_Joystick *joystick _guarded; // underlying joystick device
- int ref_count _guarded;
- const char *name _guarded;
- SDL_GamepadType type _guarded;
- SDL_GamepadFaceStyle face_style _guarded;
- GamepadMapping_t *mapping _guarded;
- int num_bindings _guarded;
- SDL_GamepadBinding *bindings _guarded;
- SDL_GamepadBinding **last_match_axis _guarded;
- Uint8 *last_hat_mask _guarded;
- Uint64 guide_button_down _guarded;
- struct SDL_Gamepad *next _guarded; // pointer to next gamepad we have allocated
- };
- #undef _guarded
- #define CHECK_GAMEPAD_MAGIC(gamepad, result) \
- if (!SDL_ObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD) || \
- !SDL_IsJoystickValid(gamepad->joystick)) { \
- SDL_InvalidParamError("gamepad"); \
- SDL_UnlockJoysticks(); \
- return result; \
- }
- static SDL_vidpid_list SDL_allowed_gamepads = {
- SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT, 0, 0, NULL,
- NULL, 0, 0, NULL,
- 0, NULL,
- false
- };
- static SDL_vidpid_list SDL_ignored_gamepads = {
- SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES, 0, 0, NULL,
- NULL, 0, 0, NULL,
- 0, NULL,
- false
- };
- static GamepadMapping_t *SDL_PrivateAddMappingForGUID(SDL_GUID jGUID, const char *mappingString, bool *existing, SDL_GamepadMappingPriority priority);
- static void SDL_PrivateLoadButtonMapping(SDL_Gamepad *gamepad, GamepadMapping_t *pGamepadMapping);
- static GamepadMapping_t *SDL_PrivateGetGamepadMapping(SDL_JoystickID instance_id, bool create_mapping);
- static void SDL_SendGamepadAxis(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadAxis axis, Sint16 value);
- static void SDL_SendGamepadButton(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadButton button, bool down);
- static bool HasSameOutput(SDL_GamepadBinding *a, SDL_GamepadBinding *b)
- {
- if (a->output_type != b->output_type) {
- return false;
- }
- if (a->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
- return a->output.axis.axis == b->output.axis.axis;
- } else {
- return a->output.button == b->output.button;
- }
- }
- static void ResetOutput(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadBinding *bind)
- {
- if (bind->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
- SDL_SendGamepadAxis(timestamp, gamepad, bind->output.axis.axis, 0);
- } else {
- SDL_SendGamepadButton(timestamp, gamepad, bind->output.button, false);
- }
- }
- static void HandleJoystickAxis(Uint64 timestamp, SDL_Gamepad *gamepad, int axis, int value)
- {
- int i;
- SDL_GamepadBinding *last_match;
- SDL_GamepadBinding *match = NULL;
- SDL_AssertJoysticksLocked();
- last_match = gamepad->last_match_axis[axis];
- for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS &&
- axis == binding->input.axis.axis) {
- if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
- if (value >= binding->input.axis.axis_min &&
- value <= binding->input.axis.axis_max) {
- match = binding;
- break;
- }
- } else {
- if (value >= binding->input.axis.axis_max &&
- value <= binding->input.axis.axis_min) {
- match = binding;
- break;
- }
- }
- }
- }
- if (last_match && (!match || !HasSameOutput(last_match, match))) {
- // Clear the last input that this axis generated
- ResetOutput(timestamp, gamepad, last_match);
- }
- if (match) {
- if (match->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
- if (match->input.axis.axis_min != match->output.axis.axis_min || match->input.axis.axis_max != match->output.axis.axis_max) {
- float normalized_value = (float)(value - match->input.axis.axis_min) / (match->input.axis.axis_max - match->input.axis.axis_min);
- value = match->output.axis.axis_min + (int)(normalized_value * (match->output.axis.axis_max - match->output.axis.axis_min));
- }
- SDL_SendGamepadAxis(timestamp, gamepad, match->output.axis.axis, (Sint16)value);
- } else {
- bool down;
- int threshold = match->input.axis.axis_min + (match->input.axis.axis_max - match->input.axis.axis_min) / 2;
- if (match->input.axis.axis_max < match->input.axis.axis_min) {
- down = (value <= threshold);
- } else {
- down = (value >= threshold);
- }
- SDL_SendGamepadButton(timestamp, gamepad, match->output.button, down);
- }
- }
- gamepad->last_match_axis[axis] = match;
- }
- static void HandleJoystickButton(Uint64 timestamp, SDL_Gamepad *gamepad, int button, bool down)
- {
- int i;
- SDL_AssertJoysticksLocked();
- for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON &&
- button == binding->input.button) {
- if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
- int value = down ? binding->output.axis.axis_max : binding->output.axis.axis_min;
- SDL_SendGamepadAxis(timestamp, gamepad, binding->output.axis.axis, (Sint16)value);
- } else {
- SDL_SendGamepadButton(timestamp, gamepad, binding->output.button, down);
- }
- break;
- }
- }
- }
- static void HandleJoystickHat(Uint64 timestamp, SDL_Gamepad *gamepad, int hat, Uint8 value)
- {
- int i;
- Uint8 last_mask, changed_mask;
- SDL_AssertJoysticksLocked();
- last_mask = gamepad->last_hat_mask[hat];
- changed_mask = (last_mask ^ value);
- for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->input_type == SDL_GAMEPAD_BINDTYPE_HAT && hat == binding->input.hat.hat) {
- if ((changed_mask & binding->input.hat.hat_mask) != 0) {
- if (value & binding->input.hat.hat_mask) {
- if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
- SDL_SendGamepadAxis(timestamp, gamepad, binding->output.axis.axis, (Sint16)binding->output.axis.axis_max);
- } else {
- SDL_SendGamepadButton(timestamp, gamepad, binding->output.button, true);
- }
- } else {
- ResetOutput(timestamp, gamepad, binding);
- }
- }
- }
- }
- gamepad->last_hat_mask[hat] = value;
- }
- /* The joystick layer will _also_ send events to recenter before disconnect,
- but it has to make (sometimes incorrect) guesses at what being "centered"
- is. The gamepad layer, however, can set a definite logical idle
- position, so set them all here. If we happened to already be at the
- center thanks to the joystick layer or idle hands, this won't generate
- duplicate events. */
- static void RecenterGamepad(SDL_Gamepad *gamepad)
- {
- int i;
- Uint64 timestamp = SDL_GetTicksNS();
- for (i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) {
- SDL_GamepadButton button = (SDL_GamepadButton)i;
- if (SDL_GetGamepadButton(gamepad, button)) {
- SDL_SendGamepadButton(timestamp, gamepad, button, false);
- }
- }
- for (i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
- SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
- if (SDL_GetGamepadAxis(gamepad, axis) != 0) {
- SDL_SendGamepadAxis(timestamp, gamepad, axis, 0);
- }
- }
- }
- void SDL_PrivateGamepadAdded(SDL_JoystickID instance_id)
- {
- SDL_Event event;
- if (!SDL_gamepads_initialized) {
- return;
- }
- event.type = SDL_EVENT_GAMEPAD_ADDED;
- event.common.timestamp = 0;
- event.gdevice.which = instance_id;
- SDL_PushEvent(&event);
- }
- void SDL_PrivateGamepadRemoved(SDL_JoystickID instance_id)
- {
- SDL_Event event;
- SDL_Gamepad *gamepad;
- SDL_AssertJoysticksLocked();
- if (!SDL_gamepads_initialized) {
- return;
- }
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- if (gamepad->joystick->instance_id == instance_id) {
- RecenterGamepad(gamepad);
- break;
- }
- }
- event.type = SDL_EVENT_GAMEPAD_REMOVED;
- event.common.timestamp = 0;
- event.gdevice.which = instance_id;
- SDL_PushEvent(&event);
- }
- static void SDL_PrivateGamepadRemapped(SDL_JoystickID instance_id)
- {
- SDL_Event event;
- if (!SDL_gamepads_initialized || SDL_IsJoystickBeingAdded()) {
- return;
- }
- event.type = SDL_EVENT_GAMEPAD_REMAPPED;
- event.common.timestamp = 0;
- event.gdevice.which = instance_id;
- SDL_PushEvent(&event);
- }
- /*
- * Event filter to fire gamepad events from joystick ones
- */
- static bool SDLCALL SDL_GamepadEventWatcher(void *userdata, SDL_Event *event)
- {
- SDL_Gamepad *gamepad;
- switch (event->type) {
- case SDL_EVENT_JOYSTICK_AXIS_MOTION:
- {
- SDL_AssertJoysticksLocked();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- if (gamepad->joystick->instance_id == event->jaxis.which) {
- HandleJoystickAxis(event->common.timestamp, gamepad, event->jaxis.axis, event->jaxis.value);
- break;
- }
- }
- } break;
- case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
- case SDL_EVENT_JOYSTICK_BUTTON_UP:
- {
- SDL_AssertJoysticksLocked();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- if (gamepad->joystick->instance_id == event->jbutton.which) {
- HandleJoystickButton(event->common.timestamp, gamepad, event->jbutton.button, event->jbutton.down);
- break;
- }
- }
- } break;
- case SDL_EVENT_JOYSTICK_HAT_MOTION:
- {
- SDL_AssertJoysticksLocked();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- if (gamepad->joystick->instance_id == event->jhat.which) {
- HandleJoystickHat(event->common.timestamp, gamepad, event->jhat.hat, event->jhat.value);
- break;
- }
- }
- } break;
- case SDL_EVENT_JOYSTICK_UPDATE_COMPLETE:
- {
- SDL_AssertJoysticksLocked();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- if (gamepad->joystick->instance_id == event->jdevice.which) {
- SDL_Event deviceevent;
- deviceevent.type = SDL_EVENT_GAMEPAD_UPDATE_COMPLETE;
- deviceevent.common.timestamp = event->jdevice.timestamp;
- deviceevent.gdevice.which = event->jdevice.which;
- SDL_PushEvent(&deviceevent);
- break;
- }
- }
- } break;
- default:
- break;
- }
- return true;
- }
- /* SDL defines sensor orientation relative to the device natural
- orientation, so when it's changed orientation to be used as a
- gamepad, change the sensor orientation to match.
- */
- static void AdjustSensorOrientation(SDL_Joystick *joystick, float *src, float *dst)
- {
- unsigned int i, j;
- SDL_AssertJoysticksLocked();
- for (i = 0; i < 3; ++i) {
- dst[i] = 0.0f;
- for (j = 0; j < 3; ++j) {
- dst[i] += joystick->sensor_transform[i][j] * src[j];
- }
- }
- }
- /*
- * Event filter to fire gamepad sensor events from system sensor events
- *
- * We don't use SDL_GamepadEventWatcher() for this because we want to
- * deliver gamepad sensor events when system sensor events are disabled,
- * and we also need to avoid a potential deadlock where joystick event
- * delivery locks the joysticks and then the event queue, but sensor
- * event delivery would lock the event queue and then from within the
- * event watcher function lock the joysticks.
- */
- void SDL_GamepadSensorWatcher(Uint64 timestamp, SDL_SensorID sensor, Uint64 sensor_timestamp, float *data, int num_values)
- {
- SDL_Gamepad *gamepad;
- SDL_LockJoysticks();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- if (gamepad->joystick->accel && gamepad->joystick->accel_sensor == sensor) {
- float gamepad_data[3];
- AdjustSensorOrientation(gamepad->joystick, data, gamepad_data);
- SDL_SendJoystickSensor(timestamp, gamepad->joystick, SDL_SENSOR_ACCEL, sensor_timestamp, gamepad_data, SDL_arraysize(gamepad_data));
- }
- if (gamepad->joystick->gyro && gamepad->joystick->gyro_sensor == sensor) {
- float gamepad_data[3];
- AdjustSensorOrientation(gamepad->joystick, data, gamepad_data);
- SDL_SendJoystickSensor(timestamp, gamepad->joystick, SDL_SENSOR_GYRO, sensor_timestamp, gamepad_data, SDL_arraysize(gamepad_data));
- }
- }
- SDL_UnlockJoysticks();
- }
- static void PushMappingChangeTracking(void)
- {
- MappingChangeTracker *tracker;
- int i, num_joysticks;
- SDL_AssertJoysticksLocked();
- if (s_mappingChangeTracker) {
- ++s_mappingChangeTracker->refcount;
- return;
- }
- s_mappingChangeTracker = (MappingChangeTracker *)SDL_calloc(1, sizeof(*tracker));
- s_mappingChangeTracker->refcount = 1;
- // Save the list of joysticks and associated mappings
- tracker = s_mappingChangeTracker;
- tracker->joysticks = SDL_GetJoysticks(&num_joysticks);
- if (!tracker->joysticks) {
- return;
- }
- if (num_joysticks == 0) {
- return;
- }
- tracker->joystick_mappings = (GamepadMapping_t **)SDL_malloc(num_joysticks * sizeof(*tracker->joystick_mappings));
- if (!tracker->joystick_mappings) {
- return;
- }
- for (i = 0; i < num_joysticks; ++i) {
- tracker->joystick_mappings[i] = SDL_PrivateGetGamepadMapping(tracker->joysticks[i], false);
- }
- }
- static void AddMappingChangeTracking(GamepadMapping_t *mapping)
- {
- MappingChangeTracker *tracker;
- int num_mappings;
- GamepadMapping_t **new_mappings;
- SDL_AssertJoysticksLocked();
- SDL_assert(s_mappingChangeTracker != NULL);
- tracker = s_mappingChangeTracker;
- num_mappings = tracker->num_changed_mappings;
- new_mappings = (GamepadMapping_t **)SDL_realloc(tracker->changed_mappings, (num_mappings + 1) * sizeof(*new_mappings));
- if (new_mappings) {
- tracker->changed_mappings = new_mappings;
- tracker->changed_mappings[num_mappings] = mapping;
- tracker->num_changed_mappings = (num_mappings + 1);
- }
- }
- static bool HasMappingChangeTracking(MappingChangeTracker *tracker, GamepadMapping_t *mapping)
- {
- int i;
- SDL_AssertJoysticksLocked();
- for (i = 0; i < tracker->num_changed_mappings; ++i) {
- if (tracker->changed_mappings[i] == mapping) {
- return true;
- }
- }
- return false;
- }
- static void PopMappingChangeTracking(void)
- {
- int i;
- MappingChangeTracker *tracker;
- SDL_AssertJoysticksLocked();
- SDL_assert(s_mappingChangeTracker != NULL);
- tracker = s_mappingChangeTracker;
- --tracker->refcount;
- if (tracker->refcount > 0) {
- return;
- }
- s_mappingChangeTracker = NULL;
- // Now check to see what gamepads changed because of the mapping changes
- if (tracker->joysticks && tracker->joystick_mappings) {
- for (i = 0; tracker->joysticks[i]; ++i) {
- // Looking up the new mapping might create one and associate it with the gamepad (and generate events)
- SDL_JoystickID joystick = tracker->joysticks[i];
- SDL_Gamepad *gamepad = SDL_GetGamepadFromID(joystick);
- GamepadMapping_t *new_mapping = SDL_PrivateGetGamepadMapping(joystick, false);
- GamepadMapping_t *old_mapping = gamepad ? gamepad->mapping : tracker->joystick_mappings[i];
- if (new_mapping && !old_mapping) {
- SDL_RemoveFromHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick);
- SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick, (const void *)true);
- SDL_PrivateGamepadAdded(joystick);
- } else if (old_mapping && !new_mapping) {
- SDL_RemoveFromHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick);
- SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick, (const void *)false);
- SDL_PrivateGamepadRemoved(joystick);
- } else if (old_mapping != new_mapping || HasMappingChangeTracking(tracker, new_mapping)) {
- if (gamepad) {
- SDL_PrivateLoadButtonMapping(gamepad, new_mapping);
- }
- SDL_PrivateGamepadRemapped(joystick);
- }
- }
- }
- SDL_free(tracker->joysticks);
- SDL_free(tracker->joystick_mappings);
- SDL_free(tracker->changed_mappings);
- SDL_free(tracker);
- }
- #ifdef SDL_PLATFORM_ANDROID
- /*
- * Helper function to guess at a mapping based on the elements reported for this gamepad
- */
- static GamepadMapping_t *SDL_CreateMappingForAndroidGamepad(SDL_GUID guid)
- {
- const int face_button_mask = ((1 << SDL_GAMEPAD_BUTTON_SOUTH) |
- (1 << SDL_GAMEPAD_BUTTON_EAST) |
- (1 << SDL_GAMEPAD_BUTTON_WEST) |
- (1 << SDL_GAMEPAD_BUTTON_NORTH));
- bool existing;
- char mapping_string[1024];
- int button_mask;
- int axis_mask;
- button_mask = SDL_Swap16LE(*(Uint16 *)(&guid.data[sizeof(guid.data) - 4]));
- axis_mask = SDL_Swap16LE(*(Uint16 *)(&guid.data[sizeof(guid.data) - 2]));
- if (!button_mask && !axis_mask) {
- // Accelerometer, shouldn't have a gamepad mapping
- return NULL;
- }
- if (!(button_mask & face_button_mask)) {
- // We don't know what buttons or axes are supported, don't make up a mapping
- return NULL;
- }
- SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_SOUTH)) {
- SDL_strlcat(mapping_string, "a:b0,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_EAST)) {
- SDL_strlcat(mapping_string, "b:b1,", sizeof(mapping_string));
- } else if (button_mask & (1 << SDL_GAMEPAD_BUTTON_BACK)) {
- // Use the back button as "B" for easy UI navigation with TV remotes
- SDL_strlcat(mapping_string, "b:b4,", sizeof(mapping_string));
- button_mask &= ~(1 << SDL_GAMEPAD_BUTTON_BACK);
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_WEST)) {
- SDL_strlcat(mapping_string, "x:b2,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_NORTH)) {
- SDL_strlcat(mapping_string, "y:b3,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_BACK)) {
- SDL_strlcat(mapping_string, "back:b4,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_GUIDE)) {
- // The guide button generally isn't functional (or acts as a home button) on most Android gamepads before Android 11
- if (SDL_GetAndroidSDKVersion() >= 30 /* Android 11 */) {
- SDL_strlcat(mapping_string, "guide:b5,", sizeof(mapping_string));
- }
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_START)) {
- SDL_strlcat(mapping_string, "start:b6,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_LEFT_STICK)) {
- SDL_strlcat(mapping_string, "leftstick:b7,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_RIGHT_STICK)) {
- SDL_strlcat(mapping_string, "rightstick:b8,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_LEFT_SHOULDER)) {
- SDL_strlcat(mapping_string, "leftshoulder:b9,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER)) {
- SDL_strlcat(mapping_string, "rightshoulder:b10,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_UP)) {
- SDL_strlcat(mapping_string, "dpup:b11,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_DOWN)) {
- SDL_strlcat(mapping_string, "dpdown:b12,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_LEFT)) {
- SDL_strlcat(mapping_string, "dpleft:b13,", sizeof(mapping_string));
- }
- if (button_mask & (1 << SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) {
- SDL_strlcat(mapping_string, "dpright:b14,", sizeof(mapping_string));
- }
- if (axis_mask & (1 << SDL_GAMEPAD_AXIS_LEFTX)) {
- SDL_strlcat(mapping_string, "leftx:a0,", sizeof(mapping_string));
- }
- if (axis_mask & (1 << SDL_GAMEPAD_AXIS_LEFTY)) {
- SDL_strlcat(mapping_string, "lefty:a1,", sizeof(mapping_string));
- }
- if (axis_mask & (1 << SDL_GAMEPAD_AXIS_RIGHTX)) {
- SDL_strlcat(mapping_string, "rightx:a2,", sizeof(mapping_string));
- }
- if (axis_mask & (1 << SDL_GAMEPAD_AXIS_RIGHTY)) {
- SDL_strlcat(mapping_string, "righty:a3,", sizeof(mapping_string));
- }
- if (axis_mask & (1 << SDL_GAMEPAD_AXIS_LEFT_TRIGGER)) {
- SDL_strlcat(mapping_string, "lefttrigger:a4,", sizeof(mapping_string));
- }
- if (axis_mask & (1 << SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)) {
- SDL_strlcat(mapping_string, "righttrigger:a5,", sizeof(mapping_string));
- }
- return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
- }
- #endif // SDL_PLATFORM_ANDROID
- /*
- * Helper function to guess at a mapping for HIDAPI gamepads
- */
- static GamepadMapping_t *SDL_CreateMappingForHIDAPIGamepad(SDL_GUID guid)
- {
- bool existing;
- char mapping_string[1024];
- Uint16 vendor;
- Uint16 product;
- SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
- SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
- if ((vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) ||
- (vendor == USB_VENDOR_DRAGONRISE &&
- (product == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER1 ||
- product == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER2))) {
- // GameCube driver has 12 buttons and 6 axes
- 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));
- } else if (vendor == USB_VENDOR_NINTENDO &&
- (guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCLeft ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCRight ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_NESLeft ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_NESRight ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_SNES ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_N64 ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_SEGA_Genesis ||
- guid.data[15] == k_eWiiExtensionControllerType_None ||
- guid.data[15] == k_eWiiExtensionControllerType_Nunchuk ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
- guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConRight)) {
- switch (guid.data[15]) {
- case k_eSwitchDeviceInfoControllerType_HVCLeft:
- 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));
- break;
- case k_eSwitchDeviceInfoControllerType_HVCRight:
- 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));
- break;
- case k_eSwitchDeviceInfoControllerType_NESLeft:
- case k_eSwitchDeviceInfoControllerType_NESRight:
- 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));
- break;
- case k_eSwitchDeviceInfoControllerType_SNES:
- 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));
- break;
- case k_eSwitchDeviceInfoControllerType_N64:
- 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));
- break;
- case k_eSwitchDeviceInfoControllerType_SEGA_Genesis:
- 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));
- break;
- case k_eWiiExtensionControllerType_None:
- 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));
- break;
- case k_eWiiExtensionControllerType_Nunchuk:
- {
- // FIXME: Should we map this to the left or right side?
- const bool map_nunchuck_left_side = true;
- if (map_nunchuck_left_side) {
- 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));
- } else {
- 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));
- }
- } break;
- default:
- if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS, false)) {
- // Vertical mode
- if (guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
- 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));
- } else {
- 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));
- }
- } else {
- // Mini gamepad mode
- if (guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
- 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));
- } else {
- 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));
- }
- }
- break;
- }
- } else {
- // All other gamepads have the standard set of 19 buttons and 6 axes
- 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));
- if (SDL_IsJoystickXboxSeriesX(vendor, product)) {
- // XBox Series X Controllers have a share button under the guide button
- SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
- } else if (SDL_IsJoystickXboxOneElite(vendor, product)) {
- // XBox One Elite Controllers have 4 back paddle buttons
- SDL_strlcat(mapping_string, "paddle1:b11,paddle2:b13,paddle3:b12,paddle4:b14,", sizeof(mapping_string));
- } else if (SDL_IsJoystickSteamController(vendor, product)) {
- // Steam controllers have 2 back paddle buttons
- SDL_strlcat(mapping_string, "paddle1:b12,paddle2:b11,", sizeof(mapping_string));
- } else if (SDL_IsJoystickNintendoSwitchPro(vendor, product) ||
- SDL_IsJoystickNintendoSwitchProInputOnly(vendor, product)) {
- // Nintendo Switch Pro controllers have a screenshot button
- SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
- } else if (SDL_IsJoystickNintendoSwitchJoyConPair(vendor, product)) {
- // The Nintendo Switch Joy-Con combined controllers has a share button and paddles
- SDL_strlcat(mapping_string, "misc1:b11,paddle1:b12,paddle2:b13,paddle3:b14,paddle4:b15,", sizeof(mapping_string));
- } else if (SDL_IsJoystickAmazonLunaController(vendor, product)) {
- // Amazon Luna Controller has a mic button under the guide button
- SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
- } else if (SDL_IsJoystickGoogleStadiaController(vendor, product)) {
- // The Google Stadia controller has a share button and a Google Assistant button
- SDL_strlcat(mapping_string, "misc1:b11,misc2:b12", sizeof(mapping_string));
- } else if (SDL_IsJoystickNVIDIASHIELDController(vendor, product)) {
- // The NVIDIA SHIELD controller has a share button between back and start buttons
- SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
- if (product == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V103) {
- // The original SHIELD controller has a touchpad and plus/minus buttons as well
- SDL_strlcat(mapping_string, "touchpad:b12,misc2:b13,misc3:b14", sizeof(mapping_string));
- }
- } else if (SDL_IsJoystickHoriSteamController(vendor, product)) {
- /* The Wireless HORIPad for Steam has QAM, Steam, Capsense L/R Sticks, 2 rear buttons, and 2 misc buttons */
- SDL_strlcat(mapping_string, "paddle1:b13,paddle2:b12,paddle3:b15,paddle4:b14,misc2:b11,misc3:b16,misc4:b17", sizeof(mapping_string));
- } else {
- switch (SDL_GetGamepadTypeFromGUID(guid, NULL)) {
- case SDL_GAMEPAD_TYPE_PS4:
- // PS4 controllers have an additional touchpad button
- SDL_strlcat(mapping_string, "touchpad:b11,", sizeof(mapping_string));
- break;
- case SDL_GAMEPAD_TYPE_PS5:
- // PS5 controllers have a microphone button and an additional touchpad button
- SDL_strlcat(mapping_string, "touchpad:b11,misc1:b12,", sizeof(mapping_string));
- // DualSense Edge controllers have paddles
- if (SDL_IsJoystickDualSenseEdge(vendor, product)) {
- SDL_strlcat(mapping_string, "paddle1:b16,paddle2:b15,paddle3:b14,paddle4:b13,", sizeof(mapping_string));
- }
- break;
- default:
- if (vendor == 0 && product == 0) {
- // This is a Bluetooth Nintendo Switch Pro controller
- SDL_strlcat(mapping_string, "misc1:b11,", sizeof(mapping_string));
- }
- break;
- }
- }
- }
- return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
- }
- /*
- * Helper function to guess at a mapping for RAWINPUT gamepads
- */
- static GamepadMapping_t *SDL_CreateMappingForRAWINPUTGamepad(SDL_GUID guid)
- {
- bool existing;
- char mapping_string[1024];
- SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
- 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));
- return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
- }
- /*
- * Helper function to guess at a mapping for WGI gamepads
- */
- static GamepadMapping_t *SDL_CreateMappingForWGIGamepad(SDL_GUID guid)
- {
- bool existing;
- char mapping_string[1024];
- if (guid.data[15] != SDL_JOYSTICK_TYPE_GAMEPAD) {
- return NULL;
- }
- SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
- 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));
- return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
- }
- /*
- * Helper function to scan the mappings database for a gamepad with the specified GUID
- */
- static GamepadMapping_t *SDL_PrivateMatchGamepadMappingForGUID(SDL_GUID guid, bool match_version, bool exact_match_crc)
- {
- GamepadMapping_t *mapping, *best_match = NULL;
- Uint16 crc = 0;
- SDL_AssertJoysticksLocked();
- SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc);
- // Clear the CRC from the GUID for matching, the mappings never include it in the GUID
- SDL_SetJoystickGUIDCRC(&guid, 0);
- if (!match_version) {
- SDL_SetJoystickGUIDVersion(&guid, 0);
- }
- for (mapping = s_pSupportedGamepads; mapping; mapping = mapping->next) {
- SDL_GUID mapping_guid;
- if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
- continue;
- }
- SDL_memcpy(&mapping_guid, &mapping->guid, sizeof(mapping_guid));
- if (!match_version) {
- SDL_SetJoystickGUIDVersion(&mapping_guid, 0);
- }
- if (SDL_memcmp(&guid, &mapping_guid, sizeof(guid)) == 0) {
- const char *crc_string = SDL_strstr(mapping->mapping, SDL_GAMEPAD_CRC_FIELD);
- if (crc_string) {
- Uint16 mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_GAMEPAD_CRC_FIELD_SIZE, NULL, 16);
- if (mapping_crc != crc) {
- // This mapping specified a CRC and they don't match
- continue;
- }
- // An exact match, including CRC
- return mapping;
- } else if (crc && exact_match_crc) {
- return NULL;
- }
- if (!best_match) {
- best_match = mapping;
- }
- }
- }
- return best_match;
- }
- /*
- * Helper function to scan the mappings database for a gamepad with the specified GUID
- */
- static GamepadMapping_t *SDL_PrivateGetGamepadMappingForGUID(SDL_GUID guid, bool adding_mapping)
- {
- GamepadMapping_t *mapping;
- mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, true, adding_mapping);
- if (mapping) {
- return mapping;
- }
- if (adding_mapping) {
- // We didn't find an existing mapping
- return NULL;
- }
- // Try harder to get the best match, or create a mapping
- if (SDL_JoystickGUIDUsesVersion(guid)) {
- // Try again, ignoring the version
- mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, false, false);
- if (mapping) {
- return mapping;
- }
- }
- #ifdef SDL_JOYSTICK_XINPUT
- if (SDL_IsJoystickXInput(guid)) {
- // This is an XInput device
- return s_pXInputMapping;
- }
- #endif
- if (SDL_IsJoystickHIDAPI(guid)) {
- mapping = SDL_CreateMappingForHIDAPIGamepad(guid);
- } else if (SDL_IsJoystickRAWINPUT(guid)) {
- mapping = SDL_CreateMappingForRAWINPUTGamepad(guid);
- } else if (SDL_IsJoystickWGI(guid)) {
- mapping = SDL_CreateMappingForWGIGamepad(guid);
- } else if (SDL_IsJoystickVIRTUAL(guid)) {
- // We'll pick up a robust mapping in VIRTUAL_JoystickGetGamepadMapping
- #ifdef SDL_PLATFORM_ANDROID
- } else {
- mapping = SDL_CreateMappingForAndroidGamepad(guid);
- #endif
- }
- return mapping;
- }
- static const char *map_StringForGamepadType[] = {
- "unknown",
- "standard",
- "xbox360",
- "xboxone",
- "ps3",
- "ps4",
- "ps5",
- "switchpro",
- "joyconleft",
- "joyconright",
- "joyconpair"
- };
- SDL_COMPILE_TIME_ASSERT(map_StringForGamepadType, SDL_arraysize(map_StringForGamepadType) == SDL_GAMEPAD_TYPE_COUNT);
- /*
- * convert a string to its enum equivalent
- */
- SDL_GamepadType SDL_GetGamepadTypeFromString(const char *str)
- {
- int i;
- if (!str || str[0] == '\0') {
- return SDL_GAMEPAD_TYPE_UNKNOWN;
- }
- if (*str == '+' || *str == '-') {
- ++str;
- }
- for (i = 0; i < SDL_arraysize(map_StringForGamepadType); ++i) {
- if (SDL_strcasecmp(str, map_StringForGamepadType[i]) == 0) {
- return (SDL_GamepadType)i;
- }
- }
- return SDL_GAMEPAD_TYPE_UNKNOWN;
- }
- /*
- * convert an enum to its string equivalent
- */
- const char *SDL_GetGamepadStringForType(SDL_GamepadType type)
- {
- if (type >= SDL_GAMEPAD_TYPE_STANDARD && type < SDL_GAMEPAD_TYPE_COUNT) {
- return map_StringForGamepadType[type];
- }
- return NULL;
- }
- static const char *map_StringForGamepadAxis[] = {
- "leftx",
- "lefty",
- "rightx",
- "righty",
- "lefttrigger",
- "righttrigger"
- };
- SDL_COMPILE_TIME_ASSERT(map_StringForGamepadAxis, SDL_arraysize(map_StringForGamepadAxis) == SDL_GAMEPAD_AXIS_COUNT);
- /*
- * convert a string to its enum equivalent
- */
- SDL_GamepadAxis SDL_GetGamepadAxisFromString(const char *str)
- {
- int i;
- if (!str || str[0] == '\0') {
- return SDL_GAMEPAD_AXIS_INVALID;
- }
- if (*str == '+' || *str == '-') {
- ++str;
- }
- for (i = 0; i < SDL_arraysize(map_StringForGamepadAxis); ++i) {
- if (SDL_strcasecmp(str, map_StringForGamepadAxis[i]) == 0) {
- return (SDL_GamepadAxis)i;
- }
- }
- return SDL_GAMEPAD_AXIS_INVALID;
- }
- /*
- * convert an enum to its string equivalent
- */
- const char *SDL_GetGamepadStringForAxis(SDL_GamepadAxis axis)
- {
- if (axis > SDL_GAMEPAD_AXIS_INVALID && axis < SDL_GAMEPAD_AXIS_COUNT) {
- return map_StringForGamepadAxis[axis];
- }
- return NULL;
- }
- static const char *map_StringForGamepadButton[] = {
- "a",
- "b",
- "x",
- "y",
- "back",
- "guide",
- "start",
- "leftstick",
- "rightstick",
- "leftshoulder",
- "rightshoulder",
- "dpup",
- "dpdown",
- "dpleft",
- "dpright",
- "misc1",
- "paddle1",
- "paddle2",
- "paddle3",
- "paddle4",
- "touchpad",
- "misc2",
- "misc3",
- "misc4",
- "misc5",
- "misc6"
- };
- SDL_COMPILE_TIME_ASSERT(map_StringForGamepadButton, SDL_arraysize(map_StringForGamepadButton) == SDL_GAMEPAD_BUTTON_COUNT);
- /*
- * convert a string to its enum equivalent
- */
- static SDL_GamepadButton SDL_PrivateGetGamepadButtonFromString(const char *str, bool baxy)
- {
- int i;
- if (!str || str[0] == '\0') {
- return SDL_GAMEPAD_BUTTON_INVALID;
- }
- for (i = 0; i < SDL_arraysize(map_StringForGamepadButton); ++i) {
- if (SDL_strcasecmp(str, map_StringForGamepadButton[i]) == 0) {
- if (baxy) {
- // Need to swap face buttons
- switch (i) {
- case SDL_GAMEPAD_BUTTON_SOUTH:
- return SDL_GAMEPAD_BUTTON_EAST;
- case SDL_GAMEPAD_BUTTON_EAST:
- return SDL_GAMEPAD_BUTTON_SOUTH;
- case SDL_GAMEPAD_BUTTON_WEST:
- return SDL_GAMEPAD_BUTTON_NORTH;
- case SDL_GAMEPAD_BUTTON_NORTH:
- return SDL_GAMEPAD_BUTTON_WEST;
- default:
- break;
- }
- }
- return (SDL_GamepadButton)i;
- }
- }
- return SDL_GAMEPAD_BUTTON_INVALID;
- }
- SDL_GamepadButton SDL_GetGamepadButtonFromString(const char *str)
- {
- return SDL_PrivateGetGamepadButtonFromString(str, false);
- }
- /*
- * convert an enum to its string equivalent
- */
- const char *SDL_GetGamepadStringForButton(SDL_GamepadButton button)
- {
- if (button > SDL_GAMEPAD_BUTTON_INVALID && button < SDL_GAMEPAD_BUTTON_COUNT) {
- return map_StringForGamepadButton[button];
- }
- return NULL;
- }
- /*
- * given a gamepad button name and a joystick name update our mapping structure with it
- */
- static bool SDL_PrivateParseGamepadElement(SDL_Gamepad *gamepad, const char *szGameButton, const char *szJoystickButton)
- {
- SDL_GamepadBinding bind;
- SDL_GamepadButton button;
- SDL_GamepadAxis axis;
- bool invert_input = false;
- char half_axis_input = 0;
- char half_axis_output = 0;
- int i;
- SDL_GamepadBinding *new_bindings;
- bool baxy_mapping = false;
- SDL_AssertJoysticksLocked();
- SDL_zero(bind);
- if (*szGameButton == '+' || *szGameButton == '-') {
- half_axis_output = *szGameButton++;
- }
- if (SDL_strstr(gamepad->mapping->mapping, ",hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1") != NULL) {
- baxy_mapping = true;
- }
- axis = SDL_GetGamepadAxisFromString(szGameButton);
- button = SDL_PrivateGetGamepadButtonFromString(szGameButton, baxy_mapping);
- if (axis != SDL_GAMEPAD_AXIS_INVALID) {
- bind.output_type = SDL_GAMEPAD_BINDTYPE_AXIS;
- bind.output.axis.axis = axis;
- if (axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER || axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
- bind.output.axis.axis_min = 0;
- bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
- } else {
- if (half_axis_output == '+') {
- bind.output.axis.axis_min = 0;
- bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
- } else if (half_axis_output == '-') {
- bind.output.axis.axis_min = 0;
- bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
- } else {
- bind.output.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
- bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
- }
- }
- } else if (button != SDL_GAMEPAD_BUTTON_INVALID) {
- bind.output_type = SDL_GAMEPAD_BINDTYPE_BUTTON;
- bind.output.button = button;
- } else {
- return false;
- }
- if (*szJoystickButton == '+' || *szJoystickButton == '-') {
- half_axis_input = *szJoystickButton++;
- }
- if (szJoystickButton[SDL_strlen(szJoystickButton) - 1] == '~') {
- invert_input = true;
- }
- if (szJoystickButton[0] == 'a' && SDL_isdigit((unsigned char)szJoystickButton[1])) {
- bind.input_type = SDL_GAMEPAD_BINDTYPE_AXIS;
- bind.input.axis.axis = SDL_atoi(&szJoystickButton[1]);
- if (half_axis_input == '+') {
- bind.input.axis.axis_min = 0;
- bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
- } else if (half_axis_input == '-') {
- bind.input.axis.axis_min = 0;
- bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
- } else {
- bind.input.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
- bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
- }
- if (invert_input) {
- int tmp = bind.input.axis.axis_min;
- bind.input.axis.axis_min = bind.input.axis.axis_max;
- bind.input.axis.axis_max = tmp;
- }
- } else if (szJoystickButton[0] == 'b' && SDL_isdigit((unsigned char)szJoystickButton[1])) {
- bind.input_type = SDL_GAMEPAD_BINDTYPE_BUTTON;
- bind.input.button = SDL_atoi(&szJoystickButton[1]);
- } else if (szJoystickButton[0] == 'h' && SDL_isdigit((unsigned char)szJoystickButton[1]) &&
- szJoystickButton[2] == '.' && SDL_isdigit((unsigned char)szJoystickButton[3])) {
- int hat = SDL_atoi(&szJoystickButton[1]);
- int mask = SDL_atoi(&szJoystickButton[3]);
- bind.input_type = SDL_GAMEPAD_BINDTYPE_HAT;
- bind.input.hat.hat = hat;
- bind.input.hat.hat_mask = mask;
- } else {
- return false;
- }
- for (i = 0; i < gamepad->num_bindings; ++i) {
- if (SDL_memcmp(&gamepad->bindings[i], &bind, sizeof(bind)) == 0) {
- // We already have this binding, could be different face button names?
- return true;
- }
- }
- ++gamepad->num_bindings;
- new_bindings = (SDL_GamepadBinding *)SDL_realloc(gamepad->bindings, gamepad->num_bindings * sizeof(*gamepad->bindings));
- if (!new_bindings) {
- SDL_free(gamepad->bindings);
- gamepad->num_bindings = 0;
- gamepad->bindings = NULL;
- return false;
- }
- gamepad->bindings = new_bindings;
- gamepad->bindings[gamepad->num_bindings - 1] = bind;
- return true;
- }
- /*
- * given a gamepad mapping string update our mapping object
- */
- static bool SDL_PrivateParseGamepadConfigString(SDL_Gamepad *gamepad, const char *pchString)
- {
- char szGameButton[20];
- char szJoystickButton[20];
- bool bGameButton = true;
- int i = 0;
- const char *pchPos = pchString;
- SDL_zeroa(szGameButton);
- SDL_zeroa(szJoystickButton);
- while (pchPos && *pchPos) {
- if (*pchPos == ':') {
- i = 0;
- bGameButton = false;
- } else if (*pchPos == ' ') {
- } else if (*pchPos == ',') {
- i = 0;
- bGameButton = true;
- SDL_PrivateParseGamepadElement(gamepad, szGameButton, szJoystickButton);
- SDL_zeroa(szGameButton);
- SDL_zeroa(szJoystickButton);
- } else if (bGameButton) {
- if (i >= sizeof(szGameButton)) {
- szGameButton[sizeof(szGameButton) - 1] = '\0';
- return SDL_SetError("Button name too large: %s", szGameButton);
- }
- szGameButton[i] = *pchPos;
- i++;
- } else {
- if (i >= sizeof(szJoystickButton)) {
- szJoystickButton[sizeof(szJoystickButton) - 1] = '\0';
- return SDL_SetError("Joystick button name too large: %s", szJoystickButton);
- }
- szJoystickButton[i] = *pchPos;
- i++;
- }
- pchPos++;
- }
- // No more values if the string was terminated by a comma. Don't report an error.
- if (szGameButton[0] != '\0' || szJoystickButton[0] != '\0') {
- SDL_PrivateParseGamepadElement(gamepad, szGameButton, szJoystickButton);
- }
- return true;
- }
- static void SDL_UpdateGamepadType(SDL_Gamepad *gamepad)
- {
- char *type_string, *comma;
- SDL_AssertJoysticksLocked();
- gamepad->type = SDL_GAMEPAD_TYPE_UNKNOWN;
- type_string = SDL_strstr(gamepad->mapping->mapping, SDL_GAMEPAD_TYPE_FIELD);
- if (type_string) {
- type_string += SDL_GAMEPAD_TYPE_FIELD_SIZE;
- comma = SDL_strchr(type_string, ',');
- if (comma) {
- *comma = '\0';
- gamepad->type = SDL_GetGamepadTypeFromString(type_string);
- *comma = ',';
- } else {
- gamepad->type = SDL_GetGamepadTypeFromString(type_string);
- }
- }
- if (gamepad->type == SDL_GAMEPAD_TYPE_UNKNOWN) {
- gamepad->type = SDL_GetRealGamepadTypeForID(gamepad->joystick->instance_id);
- }
- }
- static SDL_GamepadFaceStyle SDL_GetGamepadFaceStyleFromString(const char *string)
- {
- if (SDL_strcmp(string, "abxy") == 0) {
- return SDL_GAMEPAD_FACE_STYLE_ABXY;
- } else if (SDL_strcmp(string, "bayx") == 0) {
- return SDL_GAMEPAD_FACE_STYLE_BAYX;
- } else if (SDL_strcmp(string, "sony") == 0) {
- return SDL_GAMEPAD_FACE_STYLE_SONY;
- } else {
- return SDL_GAMEPAD_FACE_STYLE_UNKNOWN;
- }
- }
- static SDL_GamepadFaceStyle SDL_GetGamepadFaceStyleForGamepadType(SDL_GamepadType type)
- {
- switch (type) {
- case SDL_GAMEPAD_TYPE_PS3:
- case SDL_GAMEPAD_TYPE_PS4:
- case SDL_GAMEPAD_TYPE_PS5:
- return SDL_GAMEPAD_FACE_STYLE_SONY;
- case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
- case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
- case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
- case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
- return SDL_GAMEPAD_FACE_STYLE_BAYX;
- default:
- return SDL_GAMEPAD_FACE_STYLE_ABXY;
- }
- }
- static void SDL_UpdateGamepadFaceStyle(SDL_Gamepad *gamepad)
- {
- char *face_string, *comma;
- SDL_AssertJoysticksLocked();
- gamepad->face_style = SDL_GAMEPAD_FACE_STYLE_UNKNOWN;
- face_string = SDL_strstr(gamepad->mapping->mapping, SDL_GAMEPAD_FACE_FIELD);
- if (face_string) {
- face_string += SDL_GAMEPAD_TYPE_FIELD_SIZE;
- comma = SDL_strchr(face_string, ',');
- if (comma) {
- *comma = '\0';
- gamepad->face_style = SDL_GetGamepadFaceStyleFromString(face_string);
- *comma = ',';
- } else {
- gamepad->face_style = SDL_GetGamepadFaceStyleFromString(face_string);
- }
- }
- if (gamepad->face_style == SDL_GAMEPAD_FACE_STYLE_UNKNOWN &&
- SDL_strstr(gamepad->mapping->mapping, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS") != NULL) {
- // This controller uses Nintendo button style
- gamepad->face_style = SDL_GAMEPAD_FACE_STYLE_BAYX;
- }
- if (gamepad->face_style == SDL_GAMEPAD_FACE_STYLE_UNKNOWN) {
- gamepad->face_style = SDL_GetGamepadFaceStyleForGamepadType(gamepad->type);
- }
- }
- static void SDL_FixupHIDAPIMapping(SDL_Gamepad *gamepad)
- {
- // Check to see if we need fixup
- for (int i = 0; i < gamepad->num_bindings; ++i) {
- SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON &&
- binding->output.button == SDL_GAMEPAD_BUTTON_DPAD_UP) {
- if (binding->input_type != SDL_GAMEPAD_BINDTYPE_BUTTON ||
- binding->input.button != SDL_GAMEPAD_BUTTON_DPAD_UP) {
- // New style binding
- return;
- }
- }
- }
- for (int i = 0; i < gamepad->num_bindings; ++i) {
- SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON &&
- binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON) {
- switch (binding->output.button) {
- case SDL_GAMEPAD_BUTTON_DPAD_UP:
- binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
- binding->input.hat.hat = 0;
- binding->input.hat.hat_mask = SDL_HAT_UP;
- break;
- case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
- binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
- binding->input.hat.hat = 0;
- binding->input.hat.hat_mask = SDL_HAT_DOWN;
- break;
- case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
- binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
- binding->input.hat.hat = 0;
- binding->input.hat.hat_mask = SDL_HAT_LEFT;
- break;
- case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
- binding->input_type = SDL_GAMEPAD_BINDTYPE_HAT;
- binding->input.hat.hat = 0;
- binding->input.hat.hat_mask = SDL_HAT_RIGHT;
- break;
- default:
- if (binding->output.button > SDL_GAMEPAD_BUTTON_DPAD_RIGHT) {
- binding->input.button -= 4;
- }
- break;
- }
- }
- }
- }
- /*
- * Make a new button mapping struct
- */
- static void SDL_PrivateLoadButtonMapping(SDL_Gamepad *gamepad, GamepadMapping_t *pGamepadMapping)
- {
- int i;
- SDL_AssertJoysticksLocked();
- gamepad->name = pGamepadMapping->name;
- gamepad->num_bindings = 0;
- gamepad->mapping = pGamepadMapping;
- if (gamepad->joystick->naxes != 0 && gamepad->last_match_axis) {
- SDL_memset(gamepad->last_match_axis, 0, gamepad->joystick->naxes * sizeof(*gamepad->last_match_axis));
- }
- SDL_UpdateGamepadType(gamepad);
- SDL_UpdateGamepadFaceStyle(gamepad);
- SDL_PrivateParseGamepadConfigString(gamepad, pGamepadMapping->mapping);
- if (SDL_IsJoystickHIDAPI(pGamepadMapping->guid)) {
- SDL_FixupHIDAPIMapping(gamepad);
- }
- // Set the zero point for triggers
- for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS &&
- binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS &&
- (binding->output.axis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
- binding->output.axis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)) {
- if (binding->input.axis.axis < gamepad->joystick->naxes) {
- gamepad->joystick->axes[binding->input.axis.axis].value =
- gamepad->joystick->axes[binding->input.axis.axis].zero = (Sint16)binding->input.axis.axis_min;
- }
- }
- }
- }
- /*
- * grab the guid string from a mapping string
- */
- static char *SDL_PrivateGetGamepadGUIDFromMappingString(const char *pMapping)
- {
- const char *pFirstComma = SDL_strchr(pMapping, ',');
- if (pFirstComma) {
- char *pchGUID = (char *)SDL_malloc(pFirstComma - pMapping + 1);
- if (!pchGUID) {
- return NULL;
- }
- SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
- pchGUID[pFirstComma - pMapping] = '\0';
- // Convert old style GUIDs to the new style in 2.0.5
- #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
- if (SDL_strlen(pchGUID) == 32 &&
- SDL_memcmp(&pchGUID[20], "504944564944", 12) == 0) {
- SDL_memcpy(&pchGUID[20], "000000000000", 12);
- SDL_memcpy(&pchGUID[16], &pchGUID[4], 4);
- SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
- SDL_memcpy(&pchGUID[0], "03000000", 8);
- }
- #elif defined(SDL_PLATFORM_MACOS)
- if (SDL_strlen(pchGUID) == 32 &&
- SDL_memcmp(&pchGUID[4], "000000000000", 12) == 0 &&
- SDL_memcmp(&pchGUID[20], "000000000000", 12) == 0) {
- SDL_memcpy(&pchGUID[20], "000000000000", 12);
- SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
- SDL_memcpy(&pchGUID[0], "03000000", 8);
- }
- #endif
- return pchGUID;
- }
- return NULL;
- }
- /*
- * grab the name string from a mapping string
- */
- static char *SDL_PrivateGetGamepadNameFromMappingString(const char *pMapping)
- {
- const char *pFirstComma, *pSecondComma;
- char *pchName;
- pFirstComma = SDL_strchr(pMapping, ',');
- if (!pFirstComma) {
- return NULL;
- }
- pSecondComma = SDL_strchr(pFirstComma + 1, ',');
- if (!pSecondComma) {
- return NULL;
- }
- pchName = (char *)SDL_malloc(pSecondComma - pFirstComma);
- if (!pchName) {
- return NULL;
- }
- SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
- pchName[pSecondComma - pFirstComma - 1] = 0;
- return pchName;
- }
- /*
- * grab the button mapping string from a mapping string
- */
- static char *SDL_PrivateGetGamepadMappingFromMappingString(const char *pMapping)
- {
- const char *pFirstComma, *pSecondComma;
- char *result;
- size_t length;
- pFirstComma = SDL_strchr(pMapping, ',');
- if (!pFirstComma) {
- return NULL;
- }
- pSecondComma = SDL_strchr(pFirstComma + 1, ',');
- if (!pSecondComma) {
- return NULL;
- }
- // Skip whitespace
- while (SDL_isspace(pSecondComma[1])) {
- ++pSecondComma;
- }
- result = SDL_strdup(pSecondComma + 1); // mapping is everything after the 3rd comma
- // Trim whitespace
- length = SDL_strlen(result);
- while (length > 0 && SDL_isspace(result[length - 1])) {
- --length;
- }
- result[length] = '\0';
- return result;
- }
- /*
- * Helper function to add a mapping for a guid
- */
- static GamepadMapping_t *SDL_PrivateAddMappingForGUID(SDL_GUID jGUID, const char *mappingString, bool *existing, SDL_GamepadMappingPriority priority)
- {
- char *pchName;
- char *pchMapping;
- GamepadMapping_t *pGamepadMapping;
- Uint16 crc;
- SDL_AssertJoysticksLocked();
- pchName = SDL_PrivateGetGamepadNameFromMappingString(mappingString);
- if (!pchName) {
- SDL_SetError("Couldn't parse name from %s", mappingString);
- return NULL;
- }
- pchMapping = SDL_PrivateGetGamepadMappingFromMappingString(mappingString);
- if (!pchMapping) {
- SDL_free(pchName);
- SDL_SetError("Couldn't parse %s", mappingString);
- return NULL;
- }
- // Fix up the GUID and the mapping with the CRC, if needed
- SDL_GetJoystickGUIDInfo(jGUID, NULL, NULL, NULL, &crc);
- if (crc) {
- // Make sure the mapping has the CRC
- char *new_mapping;
- const char *optional_comma;
- size_t mapping_length;
- char *crc_end = "";
- char *crc_string = SDL_strstr(pchMapping, SDL_GAMEPAD_CRC_FIELD);
- if (crc_string) {
- crc_end = SDL_strchr(crc_string, ',');
- if (crc_end) {
- ++crc_end;
- } else {
- crc_end = "";
- }
- *crc_string = '\0';
- }
- // Make sure there's a comma before the CRC
- mapping_length = SDL_strlen(pchMapping);
- if (mapping_length == 0 || pchMapping[mapping_length - 1] == ',') {
- optional_comma = "";
- } else {
- optional_comma = ",";
- }
- if (SDL_asprintf(&new_mapping, "%s%s%s%.4x,%s", pchMapping, optional_comma, SDL_GAMEPAD_CRC_FIELD, crc, crc_end) >= 0) {
- SDL_free(pchMapping);
- pchMapping = new_mapping;
- }
- } else {
- // Make sure the GUID has the CRC, for matching purposes
- char *crc_string = SDL_strstr(pchMapping, SDL_GAMEPAD_CRC_FIELD);
- if (crc_string) {
- crc = (Uint16)SDL_strtol(crc_string + SDL_GAMEPAD_CRC_FIELD_SIZE, NULL, 16);
- if (crc) {
- SDL_SetJoystickGUIDCRC(&jGUID, crc);
- }
- }
- }
- PushMappingChangeTracking();
- pGamepadMapping = SDL_PrivateGetGamepadMappingForGUID(jGUID, true);
- if (pGamepadMapping) {
- // Only overwrite the mapping if the priority is the same or higher.
- if (pGamepadMapping->priority <= priority) {
- // Update existing mapping
- SDL_free(pGamepadMapping->name);
- pGamepadMapping->name = pchName;
- SDL_free(pGamepadMapping->mapping);
- pGamepadMapping->mapping = pchMapping;
- pGamepadMapping->priority = priority;
- } else {
- SDL_free(pchName);
- SDL_free(pchMapping);
- }
- if (existing) {
- *existing = true;
- }
- AddMappingChangeTracking(pGamepadMapping);
- } else {
- pGamepadMapping = (GamepadMapping_t *)SDL_malloc(sizeof(*pGamepadMapping));
- if (!pGamepadMapping) {
- PopMappingChangeTracking();
- SDL_free(pchName);
- SDL_free(pchMapping);
- return NULL;
- }
- // Clear the CRC, we've already added it to the mapping
- if (crc) {
- SDL_SetJoystickGUIDCRC(&jGUID, 0);
- }
- pGamepadMapping->guid = jGUID;
- pGamepadMapping->name = pchName;
- pGamepadMapping->mapping = pchMapping;
- pGamepadMapping->next = NULL;
- pGamepadMapping->priority = priority;
- if (s_pSupportedGamepads) {
- // Add the mapping to the end of the list
- GamepadMapping_t *pCurrMapping, *pPrevMapping;
- for (pPrevMapping = s_pSupportedGamepads, pCurrMapping = pPrevMapping->next;
- pCurrMapping;
- pPrevMapping = pCurrMapping, pCurrMapping = pCurrMapping->next) {
- // continue;
- }
- pPrevMapping->next = pGamepadMapping;
- } else {
- s_pSupportedGamepads = pGamepadMapping;
- }
- if (existing) {
- *existing = false;
- }
- }
- PopMappingChangeTracking();
- return pGamepadMapping;
- }
- /*
- * Helper function to determine pre-calculated offset to certain joystick mappings
- */
- static GamepadMapping_t *SDL_PrivateGetGamepadMappingForNameAndGUID(const char *name, SDL_GUID guid)
- {
- GamepadMapping_t *mapping;
- SDL_AssertJoysticksLocked();
- mapping = SDL_PrivateGetGamepadMappingForGUID(guid, false);
- #ifdef SDL_PLATFORM_LINUX
- if (!mapping && name) {
- if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
- // The Linux driver xpad.c maps the wireless dpad to buttons
- bool existing;
- mapping = SDL_PrivateAddMappingForGUID(guid,
- "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,",
- &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
- }
- }
- #endif // SDL_PLATFORM_LINUX
- return mapping;
- }
- static void SDL_PrivateAppendToMappingString(char *mapping_string,
- size_t mapping_string_len,
- const char *input_name,
- SDL_InputMapping *mapping)
- {
- char buffer[16];
- if (mapping->kind == EMappingKind_None) {
- return;
- }
- SDL_strlcat(mapping_string, input_name, mapping_string_len);
- SDL_strlcat(mapping_string, ":", mapping_string_len);
- switch (mapping->kind) {
- case EMappingKind_Button:
- (void)SDL_snprintf(buffer, sizeof(buffer), "b%u", mapping->target);
- break;
- case EMappingKind_Axis:
- (void)SDL_snprintf(buffer, sizeof(buffer), "%sa%u%s",
- mapping->half_axis_positive ? "+" :
- mapping->half_axis_negative ? "-" : "",
- mapping->target,
- mapping->axis_reversed ? "~" : "");
- break;
- case EMappingKind_Hat:
- (void)SDL_snprintf(buffer, sizeof(buffer), "h%i.%i", mapping->target >> 4, mapping->target & 0x0F);
- break;
- default:
- SDL_assert(false);
- }
- SDL_strlcat(mapping_string, buffer, mapping_string_len);
- SDL_strlcat(mapping_string, ",", mapping_string_len);
- }
- static GamepadMapping_t *SDL_PrivateGenerateAutomaticGamepadMapping(const char *name,
- SDL_GUID guid,
- SDL_GamepadMapping *raw_map)
- {
- bool existing;
- char name_string[128];
- char mapping[1024];
- // Remove any commas in the name
- SDL_strlcpy(name_string, name, sizeof(name_string));
- {
- char *spot;
- for (spot = name_string; *spot; ++spot) {
- if (*spot == ',') {
- *spot = ' ';
- }
- }
- }
- (void)SDL_snprintf(mapping, sizeof(mapping), "none,%s,", name_string);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "a", &raw_map->a);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "b", &raw_map->b);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "x", &raw_map->x);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "y", &raw_map->y);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "back", &raw_map->back);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "guide", &raw_map->guide);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "start", &raw_map->start);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftstick", &raw_map->leftstick);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightstick", &raw_map->rightstick);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftshoulder", &raw_map->leftshoulder);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightshoulder", &raw_map->rightshoulder);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpup", &raw_map->dpup);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpdown", &raw_map->dpdown);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpleft", &raw_map->dpleft);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpright", &raw_map->dpright);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc1", &raw_map->misc1);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc2", &raw_map->misc2);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc3", &raw_map->misc3);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc4", &raw_map->misc4);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc5", &raw_map->misc5);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc6", &raw_map->misc6);
- /* Keep using paddle1-4 in the generated mapping so that it can be
- * reused with SDL2 */
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle1", &raw_map->right_paddle1);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle2", &raw_map->left_paddle1);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle3", &raw_map->right_paddle2);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle4", &raw_map->left_paddle2);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftx", &raw_map->leftx);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefty", &raw_map->lefty);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightx", &raw_map->rightx);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righty", &raw_map->righty);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefttrigger", &raw_map->lefttrigger);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righttrigger", &raw_map->righttrigger);
- SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "touchpad", &raw_map->touchpad);
- return SDL_PrivateAddMappingForGUID(guid, mapping, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
- }
- static GamepadMapping_t *SDL_PrivateGetGamepadMapping(SDL_JoystickID instance_id, bool create_mapping)
- {
- const char *name;
- SDL_GUID guid;
- GamepadMapping_t *mapping;
- SDL_AssertJoysticksLocked();
- name = SDL_GetJoystickNameForID(instance_id);
- guid = SDL_GetJoystickGUIDForID(instance_id);
- mapping = SDL_PrivateGetGamepadMappingForNameAndGUID(name, guid);
- if (!mapping && create_mapping) {
- SDL_GamepadMapping raw_map;
- SDL_zero(raw_map);
- if (SDL_PrivateJoystickGetAutoGamepadMapping(instance_id, &raw_map)) {
- mapping = SDL_PrivateGenerateAutomaticGamepadMapping(name, guid, &raw_map);
- }
- }
- if (!mapping) {
- mapping = s_pDefaultMapping;
- }
- return mapping;
- }
- /*
- * Add or update an entry into the Mappings Database
- */
- int SDL_AddGamepadMappingsFromIO(SDL_IOStream *src, bool closeio)
- {
- const char *platform = SDL_GetPlatform();
- int gamepads = 0;
- char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
- size_t db_size;
- size_t platform_len;
- buf = (char *)SDL_LoadFile_IO(src, &db_size, closeio);
- if (!buf) {
- SDL_SetError("Could not allocate space to read DB into memory");
- return -1;
- }
- line = buf;
- SDL_LockJoysticks();
- PushMappingChangeTracking();
- while (line < buf + db_size) {
- line_end = SDL_strchr(line, '\n');
- if (line_end) {
- *line_end = '\0';
- } else {
- line_end = buf + db_size;
- }
- // Extract and verify the platform
- tmp = SDL_strstr(line, SDL_GAMEPAD_PLATFORM_FIELD);
- if (tmp) {
- tmp += SDL_GAMEPAD_PLATFORM_FIELD_SIZE;
- comma = SDL_strchr(tmp, ',');
- if (comma) {
- platform_len = comma - tmp + 1;
- if (platform_len + 1 < SDL_arraysize(line_platform)) {
- SDL_strlcpy(line_platform, tmp, platform_len);
- if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
- SDL_AddGamepadMapping(line) > 0) {
- gamepads++;
- }
- }
- }
- }
- line = line_end + 1;
- }
- PopMappingChangeTracking();
- SDL_UnlockJoysticks();
- SDL_free(buf);
- return gamepads;
- }
- int SDL_AddGamepadMappingsFromFile(const char *file)
- {
- return SDL_AddGamepadMappingsFromIO(SDL_IOFromFile(file, "rb"), true);
- }
- bool SDL_ReloadGamepadMappings(void)
- {
- SDL_Gamepad *gamepad;
- SDL_LockJoysticks();
- PushMappingChangeTracking();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- AddMappingChangeTracking(gamepad->mapping);
- }
- SDL_QuitGamepadMappings();
- SDL_InitGamepadMappings();
- PopMappingChangeTracking();
- SDL_UnlockJoysticks();
- return true;
- }
- static char *SDL_ConvertMappingToPositional(const char *mapping)
- {
- // Add space for '!' and null terminator
- size_t length = SDL_strlen(mapping) + 1 + 1;
- char *remapped = (char *)SDL_malloc(length);
- if (remapped) {
- char *button_A;
- char *button_B;
- char *button_X;
- char *button_Y;
- char *hint;
- SDL_strlcpy(remapped, mapping, length);
- button_A = SDL_strstr(remapped, "a:");
- button_B = SDL_strstr(remapped, "b:");
- button_X = SDL_strstr(remapped, "x:");
- button_Y = SDL_strstr(remapped, "y:");
- hint = SDL_strstr(remapped, "hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS");
- if (button_A) {
- *button_A = 'b';
- }
- if (button_B) {
- *button_B = 'a';
- }
- if (button_X) {
- *button_X = 'y';
- }
- if (button_Y) {
- *button_Y = 'x';
- }
- if (hint) {
- hint += 5;
- SDL_memmove(hint + 1, hint, SDL_strlen(hint) + 1);
- *hint = '!';
- }
- }
- return remapped;
- }
- /*
- * Add or update an entry into the Mappings Database with a priority
- */
- static int SDL_PrivateAddGamepadMapping(const char *mappingString, SDL_GamepadMappingPriority priority)
- {
- char *remapped = NULL;
- char *pchGUID;
- SDL_GUID jGUID;
- bool is_default_mapping = false;
- bool is_xinput_mapping = false;
- bool existing = false;
- GamepadMapping_t *pGamepadMapping;
- int result = -1;
- SDL_AssertJoysticksLocked();
- if (!mappingString) {
- SDL_InvalidParamError("mappingString");
- return -1;
- }
- { // Extract and verify the hint field
- const char *tmp;
- tmp = SDL_strstr(mappingString, SDL_GAMEPAD_HINT_FIELD);
- if (tmp) {
- bool default_value, value, negate;
- int len;
- char hint[128];
- tmp += SDL_GAMEPAD_HINT_FIELD_SIZE;
- if (*tmp == '!') {
- negate = true;
- ++tmp;
- } else {
- negate = false;
- }
- len = 0;
- while (*tmp && *tmp != ',' && *tmp != ':' && len < (sizeof(hint) - 1)) {
- hint[len++] = *tmp++;
- }
- hint[len] = '\0';
- if (tmp[0] == ':' && tmp[1] == '=') {
- tmp += 2;
- default_value = SDL_atoi(tmp);
- } else {
- default_value = false;
- }
- if (SDL_strcmp(hint, "SDL_GAMECONTROLLER_USE_BUTTON_LABELS") == 0) {
- // This hint is used to signal whether the mapping uses positional buttons or not
- if (negate) {
- // This mapping uses positional buttons, we can use it as-is
- } else {
- // This mapping uses labeled buttons, we need to swap them to positional
- remapped = SDL_ConvertMappingToPositional(mappingString);
- if (!remapped) {
- goto done;
- }
- mappingString = remapped;
- }
- } else {
- value = SDL_GetHintBoolean(hint, default_value);
- if (negate) {
- value = !value;
- }
- if (!value) {
- result = 0;
- goto done;
- }
- }
- }
- }
- #ifdef ANDROID
- { // Extract and verify the SDK version
- const char *tmp;
- tmp = SDL_strstr(mappingString, SDL_GAMEPAD_SDKGE_FIELD);
- if (tmp) {
- tmp += SDL_GAMEPAD_SDKGE_FIELD_SIZE;
- if (!(SDL_GetAndroidSDKVersion() >= SDL_atoi(tmp))) {
- SDL_SetError("SDK version %d < minimum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
- goto done;
- }
- }
- tmp = SDL_strstr(mappingString, SDL_GAMEPAD_SDKLE_FIELD);
- if (tmp) {
- tmp += SDL_GAMEPAD_SDKLE_FIELD_SIZE;
- if (!(SDL_GetAndroidSDKVersion() <= SDL_atoi(tmp))) {
- SDL_SetError("SDK version %d > maximum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
- goto done;
- }
- }
- }
- #endif
- pchGUID = SDL_PrivateGetGamepadGUIDFromMappingString(mappingString);
- if (!pchGUID) {
- SDL_SetError("Couldn't parse GUID from %s", mappingString);
- goto done;
- }
- if (!SDL_strcasecmp(pchGUID, "default")) {
- is_default_mapping = true;
- } else if (!SDL_strcasecmp(pchGUID, "xinput")) {
- is_xinput_mapping = true;
- }
- jGUID = SDL_StringToGUID(pchGUID);
- SDL_free(pchGUID);
- pGamepadMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing, priority);
- if (!pGamepadMapping) {
- goto done;
- }
- if (existing) {
- result = 0;
- } else {
- if (is_default_mapping) {
- s_pDefaultMapping = pGamepadMapping;
- } else if (is_xinput_mapping) {
- s_pXInputMapping = pGamepadMapping;
- }
- result = 1;
- }
- done:
- if (remapped) {
- SDL_free(remapped);
- }
- return result;
- }
- /*
- * Add or update an entry into the Mappings Database
- */
- int SDL_AddGamepadMapping(const char *mapping)
- {
- int result;
- SDL_LockJoysticks();
- {
- result = SDL_PrivateAddGamepadMapping(mapping, SDL_GAMEPAD_MAPPING_PRIORITY_API);
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Create a mapping string for a mapping
- */
- static char *CreateMappingString(GamepadMapping_t *mapping, SDL_GUID guid)
- {
- char *pMappingString, *pPlatformString;
- char pchGUID[33];
- size_t needed;
- bool need_platform = false;
- const char *platform = NULL;
- SDL_AssertJoysticksLocked();
- SDL_GUIDToString(guid, pchGUID, sizeof(pchGUID));
- // allocate enough memory for GUID + ',' + name + ',' + mapping + \0
- needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
- if (!SDL_strstr(mapping->mapping, SDL_GAMEPAD_PLATFORM_FIELD)) {
- // add memory for ',' + platform:PLATFORM
- need_platform = true;
- if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
- needed += 1;
- }
- platform = SDL_GetPlatform();
- needed += SDL_GAMEPAD_PLATFORM_FIELD_SIZE + SDL_strlen(platform) + 1;
- }
- pMappingString = (char *)SDL_malloc(needed);
- if (!pMappingString) {
- return NULL;
- }
- (void)SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
- if (need_platform) {
- if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
- SDL_strlcat(pMappingString, ",", needed);
- }
- SDL_strlcat(pMappingString, SDL_GAMEPAD_PLATFORM_FIELD, needed);
- SDL_strlcat(pMappingString, platform, needed);
- SDL_strlcat(pMappingString, ",", needed);
- }
- // Make sure multiple platform strings haven't made their way into the mapping
- pPlatformString = SDL_strstr(pMappingString, SDL_GAMEPAD_PLATFORM_FIELD);
- if (pPlatformString) {
- pPlatformString = SDL_strstr(pPlatformString + 1, SDL_GAMEPAD_PLATFORM_FIELD);
- if (pPlatformString) {
- *pPlatformString = '\0';
- }
- }
- return pMappingString;
- }
- char **SDL_GetGamepadMappings(int *count)
- {
- int num_mappings = 0;
- char **result = NULL;
- char **mappings = NULL;
- if (count) {
- *count = 0;
- }
- SDL_LockJoysticks();
- for (GamepadMapping_t *mapping = s_pSupportedGamepads; mapping; mapping = mapping->next) {
- if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
- continue;
- }
- num_mappings++;
- }
- size_t final_allocation = sizeof (char *); // for the NULL terminator element.
- bool failed = false;
- mappings = (char **) SDL_calloc(num_mappings + 1, sizeof (char *));
- if (!mappings) {
- failed = true;
- } else {
- int i = 0;
- for (GamepadMapping_t *mapping = s_pSupportedGamepads; mapping; mapping = mapping->next) {
- if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
- continue;
- }
- char *mappingstr = CreateMappingString(mapping, mapping->guid);
- if (!mappingstr) {
- failed = true;
- break; // error string is already set.
- }
- SDL_assert(i < num_mappings);
- mappings[i++] = mappingstr;
- final_allocation += SDL_strlen(mappingstr) + 1 + sizeof (char *);
- }
- }
- SDL_UnlockJoysticks();
- if (!failed) {
- result = (char **) SDL_malloc(final_allocation);
- if (result) {
- final_allocation -= (sizeof (char *) * num_mappings + 1);
- char *strptr = (char *) (result + (num_mappings + 1));
- for (int i = 0; i < num_mappings; i++) {
- result[i] = strptr;
- const size_t slen = SDL_strlcpy(strptr, mappings[i], final_allocation) + 1;
- SDL_assert(final_allocation >= slen);
- final_allocation -= slen;
- strptr += slen;
- }
- result[num_mappings] = NULL;
- if (count) {
- *count = num_mappings;
- }
- }
- }
- if (mappings) {
- for (int i = 0; i < num_mappings; i++) {
- SDL_free(mappings[i]);
- }
- SDL_free(mappings);
- }
- return result;
- }
- /*
- * Get the mapping string for this GUID
- */
- char *SDL_GetGamepadMappingForGUID(SDL_GUID guid)
- {
- char *result;
- SDL_LockJoysticks();
- {
- GamepadMapping_t *mapping = SDL_PrivateGetGamepadMappingForGUID(guid, false);
- if (mapping) {
- result = CreateMappingString(mapping, guid);
- } else {
- SDL_SetError("Mapping not available");
- result = NULL;
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Get the mapping string for this device
- */
- char *SDL_GetGamepadMapping(SDL_Gamepad *gamepad)
- {
- char *result;
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, NULL);
- result = CreateMappingString(gamepad->mapping, gamepad->joystick->guid);
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Set the mapping string for this device
- */
- bool SDL_SetGamepadMapping(SDL_JoystickID instance_id, const char *mapping)
- {
- SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
- bool result = false;
- if (SDL_memcmp(&guid, &s_zeroGUID, sizeof(guid)) == 0) {
- return SDL_InvalidParamError("instance_id");
- }
- if (!mapping) {
- mapping = "*,*,";
- }
- SDL_LockJoysticks();
- {
- if (SDL_PrivateAddMappingForGUID(guid, mapping, NULL, SDL_GAMEPAD_MAPPING_PRIORITY_API)) {
- result = true;
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- static void SDL_LoadGamepadHints(void)
- {
- const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
- if (hint && hint[0]) {
- char *pTempMappings = SDL_strdup(hint);
- char *pUserMappings = pTempMappings;
- PushMappingChangeTracking();
- while (pUserMappings) {
- char *pchNewLine = NULL;
- pchNewLine = SDL_strchr(pUserMappings, '\n');
- if (pchNewLine) {
- *pchNewLine = '\0';
- }
- SDL_PrivateAddGamepadMapping(pUserMappings, SDL_GAMEPAD_MAPPING_PRIORITY_USER);
- if (pchNewLine) {
- pUserMappings = pchNewLine + 1;
- } else {
- pUserMappings = NULL;
- }
- }
- PopMappingChangeTracking();
- SDL_free(pTempMappings);
- }
- }
- /*
- * Fill the given buffer with the expected gamepad mapping filepath.
- * Usually this will just be SDL_HINT_GAMECONTROLLERCONFIG_FILE, but for
- * Android, we want to get the internal storage path.
- */
- static bool SDL_GetGamepadMappingFilePath(char *path, size_t size)
- {
- const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG_FILE);
- if (hint && *hint) {
- return SDL_strlcpy(path, hint, size) < size;
- }
- #ifdef SDL_PLATFORM_ANDROID
- return SDL_snprintf(path, size, "%s/gamepad_map.txt", SDL_GetAndroidInternalStoragePath()) < size;
- #else
- return false;
- #endif
- }
- /*
- * Initialize the gamepad system, mostly load our DB of gamepad config mappings
- */
- bool SDL_InitGamepadMappings(void)
- {
- char szGamepadMapPath[1024];
- int i = 0;
- const char *pMappingString = NULL;
- SDL_AssertJoysticksLocked();
- PushMappingChangeTracking();
- pMappingString = s_GamepadMappings[i];
- while (pMappingString) {
- SDL_PrivateAddGamepadMapping(pMappingString, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
- i++;
- pMappingString = s_GamepadMappings[i];
- }
- if (SDL_GetGamepadMappingFilePath(szGamepadMapPath, sizeof(szGamepadMapPath))) {
- SDL_AddGamepadMappingsFromFile(szGamepadMapPath);
- }
- // load in any user supplied config
- SDL_LoadGamepadHints();
- SDL_LoadVIDPIDList(&SDL_allowed_gamepads);
- SDL_LoadVIDPIDList(&SDL_ignored_gamepads);
- PopMappingChangeTracking();
- return true;
- }
- bool SDL_InitGamepads(void)
- {
- int i;
- SDL_JoystickID *joysticks;
- SDL_gamepads_initialized = true;
- // Watch for joystick events and fire gamepad ones if needed
- SDL_AddEventWatch(SDL_GamepadEventWatcher, NULL);
- // Send added events for gamepads currently attached
- joysticks = SDL_GetJoysticks(NULL);
- if (joysticks) {
- for (i = 0; joysticks[i]; ++i) {
- if (SDL_IsGamepad(joysticks[i])) {
- SDL_PrivateGamepadAdded(joysticks[i]);
- }
- }
- SDL_free(joysticks);
- }
- return true;
- }
- bool SDL_HasGamepad(void)
- {
- int num_joysticks = 0;
- int num_gamepads = 0;
- SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
- if (joysticks) {
- int i;
- for (i = num_joysticks - 1; i >= 0 && num_gamepads == 0; --i) {
- if (SDL_IsGamepad(joysticks[i])) {
- ++num_gamepads;
- }
- }
- SDL_free(joysticks);
- }
- if (num_gamepads > 0) {
- return true;
- }
- return false;
- }
- SDL_JoystickID *SDL_GetGamepads(int *count)
- {
- int num_joysticks = 0;
- int num_gamepads = 0;
- SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
- if (joysticks) {
- int i;
- for (i = num_joysticks - 1; i >= 0; --i) {
- if (SDL_IsGamepad(joysticks[i])) {
- ++num_gamepads;
- } else {
- SDL_memmove(&joysticks[i], &joysticks[i+1], (num_gamepads + 1) * sizeof(joysticks[i]));
- }
- }
- }
- if (count) {
- *count = num_gamepads;
- }
- return joysticks;
- }
- const char *SDL_GetGamepadNameForID(SDL_JoystickID instance_id)
- {
- const char *result = NULL;
- SDL_LockJoysticks();
- {
- GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id, true);
- if (mapping) {
- if (SDL_strcmp(mapping->name, "*") == 0) {
- result = SDL_GetJoystickNameForID(instance_id);
- } else {
- result = SDL_GetPersistentString(mapping->name);
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- const char *SDL_GetGamepadPathForID(SDL_JoystickID instance_id)
- {
- return SDL_GetJoystickPathForID(instance_id);
- }
- int SDL_GetGamepadPlayerIndexForID(SDL_JoystickID instance_id)
- {
- return SDL_GetJoystickPlayerIndexForID(instance_id);
- }
- SDL_GUID SDL_GetGamepadGUIDForID(SDL_JoystickID instance_id)
- {
- return SDL_GetJoystickGUIDForID(instance_id);
- }
- Uint16 SDL_GetGamepadVendorForID(SDL_JoystickID instance_id)
- {
- return SDL_GetJoystickVendorForID(instance_id);
- }
- Uint16 SDL_GetGamepadProductForID(SDL_JoystickID instance_id)
- {
- return SDL_GetJoystickProductForID(instance_id);
- }
- Uint16 SDL_GetGamepadProductVersionForID(SDL_JoystickID instance_id)
- {
- return SDL_GetJoystickProductVersionForID(instance_id);
- }
- SDL_GamepadType SDL_GetGamepadTypeForID(SDL_JoystickID instance_id)
- {
- SDL_GamepadType type = SDL_GAMEPAD_TYPE_UNKNOWN;
- SDL_LockJoysticks();
- {
- GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id, true);
- if (mapping) {
- char *type_string, *comma;
- type_string = SDL_strstr(mapping->mapping, SDL_GAMEPAD_TYPE_FIELD);
- if (type_string) {
- type_string += SDL_GAMEPAD_TYPE_FIELD_SIZE;
- comma = SDL_strchr(type_string, ',');
- if (comma) {
- *comma = '\0';
- type = SDL_GetGamepadTypeFromString(type_string);
- *comma = ',';
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- if (type != SDL_GAMEPAD_TYPE_UNKNOWN) {
- return type;
- }
- return SDL_GetRealGamepadTypeForID(instance_id);
- }
- SDL_GamepadType SDL_GetRealGamepadTypeForID(SDL_JoystickID instance_id)
- {
- SDL_GamepadType type = SDL_GAMEPAD_TYPE_UNKNOWN;
- const SDL_SteamVirtualGamepadInfo *info;
- SDL_LockJoysticks();
- {
- info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);
- if (info) {
- type = info->type;
- } else {
- type = SDL_GetGamepadTypeFromGUID(SDL_GetJoystickGUIDForID(instance_id), SDL_GetJoystickNameForID(instance_id));
- }
- }
- SDL_UnlockJoysticks();
- return type;
- }
- char *SDL_GetGamepadMappingForID(SDL_JoystickID instance_id)
- {
- char *result = NULL;
- SDL_LockJoysticks();
- {
- GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id, true);
- if (mapping) {
- char pchGUID[33];
- SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
- SDL_GUIDToString(guid, pchGUID, sizeof(pchGUID));
- SDL_asprintf(&result, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Return 1 if the joystick with this name and GUID is a supported gamepad
- */
- bool SDL_IsGamepadNameAndGUID(const char *name, SDL_GUID guid)
- {
- bool result;
- SDL_LockJoysticks();
- {
- if (s_pDefaultMapping || SDL_PrivateGetGamepadMappingForNameAndGUID(name, guid) != NULL) {
- result = true;
- } else {
- result = false;
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Return 1 if the joystick at this device index is a supported gamepad
- */
- bool SDL_IsGamepad(SDL_JoystickID instance_id)
- {
- bool result;
- SDL_LockJoysticks();
- {
- const void *value;
- if (SDL_FindInHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)instance_id, &value)) {
- result = (bool)(uintptr_t)value;
- } else {
- if (SDL_PrivateGetGamepadMapping(instance_id, true) != NULL) {
- result = true;
- } else {
- result = false;
- }
- if (!s_gamepadInstanceIDs) {
- s_gamepadInstanceIDs = SDL_CreateHashTable(NULL, 4, SDL_HashID, SDL_KeyMatchID, NULL, false, false);
- }
- SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)instance_id, (void *)(uintptr_t)result);
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Return 1 if the gamepad should be ignored by SDL
- */
- bool SDL_ShouldIgnoreGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
- {
- #ifdef SDL_PLATFORM_LINUX
- if (SDL_endswith(name, " Motion Sensors")) {
- // Don't treat the PS3 and PS4 motion controls as a separate gamepad
- return true;
- }
- if (SDL_strncmp(name, "Nintendo ", 9) == 0 && SDL_strstr(name, " IMU") != NULL) {
- // Don't treat the Nintendo IMU as a separate gamepad
- return true;
- }
- if (SDL_endswith(name, " Accelerometer") ||
- SDL_endswith(name, " IR") ||
- SDL_endswith(name, " Motion Plus") ||
- SDL_endswith(name, " Nunchuk")) {
- // Don't treat the Wii extension controls as a separate gamepad
- return true;
- }
- #endif
- if (name && SDL_strcmp(name, "uinput-fpc") == 0) {
- // The Google Pixel fingerprint sensor reports itself as a joystick
- return true;
- }
- #ifdef SDL_PLATFORM_WIN32
- if (SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", false) &&
- SDL_GetHintBoolean("STEAM_COMPAT_PROTON", false)) {
- // We are launched by Steam and running under Proton
- // We can't tell whether this controller is a Steam Virtual Gamepad,
- // so assume that Proton is doing the appropriate filtering of controllers
- // and anything we see here is fine to use.
- return false;
- }
- #endif // SDL_PLATFORM_WIN32
- if (SDL_IsJoystickSteamVirtualGamepad(vendor_id, product_id, version)) {
- return !SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", false);
- }
- if (SDL_allowed_gamepads.num_included_entries > 0) {
- if (SDL_VIDPIDInList(vendor_id, product_id, &SDL_allowed_gamepads)) {
- return false;
- }
- return true;
- } else {
- if (SDL_VIDPIDInList(vendor_id, product_id, &SDL_ignored_gamepads)) {
- return true;
- }
- return false;
- }
- }
- /*
- * Open a gamepad for use
- *
- * This function returns a gamepad identifier, or NULL if an error occurred.
- */
- SDL_Gamepad *SDL_OpenGamepad(SDL_JoystickID instance_id)
- {
- SDL_Gamepad *gamepad;
- SDL_Gamepad *gamepadlist;
- GamepadMapping_t *pSupportedGamepad = NULL;
- SDL_LockJoysticks();
- gamepadlist = SDL_gamepads;
- // If the gamepad is already open, return it
- while (gamepadlist) {
- if (instance_id == gamepadlist->joystick->instance_id) {
- gamepad = gamepadlist;
- ++gamepad->ref_count;
- SDL_UnlockJoysticks();
- return gamepad;
- }
- gamepadlist = gamepadlist->next;
- }
- // Find a gamepad mapping
- pSupportedGamepad = SDL_PrivateGetGamepadMapping(instance_id, true);
- if (!pSupportedGamepad) {
- SDL_SetError("Couldn't find mapping for device (%" SDL_PRIu32 ")", instance_id);
- SDL_UnlockJoysticks();
- return NULL;
- }
- // Create and initialize the gamepad
- gamepad = (SDL_Gamepad *)SDL_calloc(1, sizeof(*gamepad));
- if (!gamepad) {
- SDL_UnlockJoysticks();
- return NULL;
- }
- SDL_SetObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD, true);
- gamepad->joystick = SDL_OpenJoystick(instance_id);
- if (!gamepad->joystick) {
- SDL_free(gamepad);
- SDL_UnlockJoysticks();
- return NULL;
- }
- if (gamepad->joystick->naxes) {
- gamepad->last_match_axis = (SDL_GamepadBinding **)SDL_calloc(gamepad->joystick->naxes, sizeof(*gamepad->last_match_axis));
- if (!gamepad->last_match_axis) {
- SDL_CloseJoystick(gamepad->joystick);
- SDL_free(gamepad);
- SDL_UnlockJoysticks();
- return NULL;
- }
- }
- if (gamepad->joystick->nhats) {
- gamepad->last_hat_mask = (Uint8 *)SDL_calloc(gamepad->joystick->nhats, sizeof(*gamepad->last_hat_mask));
- if (!gamepad->last_hat_mask) {
- SDL_CloseJoystick(gamepad->joystick);
- SDL_free(gamepad->last_match_axis);
- SDL_free(gamepad);
- SDL_UnlockJoysticks();
- return NULL;
- }
- }
- SDL_PrivateLoadButtonMapping(gamepad, pSupportedGamepad);
- // Add the gamepad to list
- ++gamepad->ref_count;
- // Link the gamepad in the list
- gamepad->next = SDL_gamepads;
- SDL_gamepads = gamepad;
- SDL_UnlockJoysticks();
- return gamepad;
- }
- /*
- * Manually pump for gamepad updates.
- */
- void SDL_UpdateGamepads(void)
- {
- // Just for API completeness; the joystick API does all the work.
- SDL_UpdateJoysticks();
- }
- /**
- * Return whether a gamepad has a given axis
- */
- bool SDL_GamepadHasAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
- {
- bool result = false;
- SDL_LockJoysticks();
- {
- int i;
- CHECK_GAMEPAD_MAGIC(gamepad, false);
- for (i = 0; i < gamepad->num_bindings; ++i) {
- const SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
- result = true;
- break;
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Get the current state of an axis control on a gamepad
- */
- Sint16 SDL_GetGamepadAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
- {
- Sint16 result = 0;
- SDL_LockJoysticks();
- {
- int i;
- CHECK_GAMEPAD_MAGIC(gamepad, 0);
- for (i = 0; i < gamepad->num_bindings; ++i) {
- const SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->output_type == SDL_GAMEPAD_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
- int value = 0;
- bool valid_input_range;
- bool valid_output_range;
- if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
- value = SDL_GetJoystickAxis(gamepad->joystick, binding->input.axis.axis);
- if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
- valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
- } else {
- valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
- }
- if (valid_input_range) {
- if (binding->input.axis.axis_min != binding->output.axis.axis_min || binding->input.axis.axis_max != binding->output.axis.axis_max) {
- float normalized_value = (float)(value - binding->input.axis.axis_min) / (binding->input.axis.axis_max - binding->input.axis.axis_min);
- value = binding->output.axis.axis_min + (int)(normalized_value * (binding->output.axis.axis_max - binding->output.axis.axis_min));
- }
- } else {
- value = 0;
- }
- } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON) {
- if (SDL_GetJoystickButton(gamepad->joystick, binding->input.button)) {
- value = binding->output.axis.axis_max;
- }
- } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_HAT) {
- int hat_mask = SDL_GetJoystickHat(gamepad->joystick, binding->input.hat.hat);
- if (hat_mask & binding->input.hat.hat_mask) {
- value = binding->output.axis.axis_max;
- }
- }
- if (binding->output.axis.axis_min < binding->output.axis.axis_max) {
- valid_output_range = (value >= binding->output.axis.axis_min && value <= binding->output.axis.axis_max);
- } else {
- valid_output_range = (value >= binding->output.axis.axis_max && value <= binding->output.axis.axis_min);
- }
- // If the value is zero, there might be another binding that makes it non-zero
- if (value != 0 && valid_output_range) {
- result = (Sint16)value;
- break;
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /**
- * Return whether a gamepad has a given button
- */
- bool SDL_GamepadHasButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
- {
- bool result = false;
- SDL_LockJoysticks();
- {
- int i;
- CHECK_GAMEPAD_MAGIC(gamepad, false);
- for (i = 0; i < gamepad->num_bindings; ++i) {
- const SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON && binding->output.button == button) {
- result = true;
- break;
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Get the current state of a button on a gamepad
- */
- bool SDL_GetGamepadButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
- {
- bool result = false;
- SDL_LockJoysticks();
- {
- int i;
- CHECK_GAMEPAD_MAGIC(gamepad, false);
- for (i = 0; i < gamepad->num_bindings; ++i) {
- const SDL_GamepadBinding *binding = &gamepad->bindings[i];
- if (binding->output_type == SDL_GAMEPAD_BINDTYPE_BUTTON && binding->output.button == button) {
- if (binding->input_type == SDL_GAMEPAD_BINDTYPE_AXIS) {
- bool valid_input_range;
- int value = SDL_GetJoystickAxis(gamepad->joystick, binding->input.axis.axis);
- int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
- if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
- valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
- if (valid_input_range) {
- result |= (value >= threshold);
- }
- } else {
- valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
- if (valid_input_range) {
- result |= (value <= threshold);
- }
- }
- } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_BUTTON) {
- result |= SDL_GetJoystickButton(gamepad->joystick, binding->input.button);
- } else if (binding->input_type == SDL_GAMEPAD_BINDTYPE_HAT) {
- int hat_mask = SDL_GetJoystickHat(gamepad->joystick, binding->input.hat.hat);
- result |= ((hat_mask & binding->input.hat.hat_mask) != 0);
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /**
- * Get the label of a button on a gamepad.
- */
- static SDL_GamepadButtonLabel SDL_GetGamepadButtonLabelForFaceStyle(SDL_GamepadFaceStyle face_style, SDL_GamepadButton button)
- {
- SDL_GamepadButtonLabel label = SDL_GAMEPAD_BUTTON_LABEL_UNKNOWN;
- switch (face_style) {
- case SDL_GAMEPAD_FACE_STYLE_ABXY:
- switch (button) {
- case SDL_GAMEPAD_BUTTON_SOUTH:
- label = SDL_GAMEPAD_BUTTON_LABEL_A;
- break;
- case SDL_GAMEPAD_BUTTON_EAST:
- label = SDL_GAMEPAD_BUTTON_LABEL_B;
- break;
- case SDL_GAMEPAD_BUTTON_WEST:
- label = SDL_GAMEPAD_BUTTON_LABEL_X;
- break;
- case SDL_GAMEPAD_BUTTON_NORTH:
- label = SDL_GAMEPAD_BUTTON_LABEL_Y;
- break;
- default:
- break;
- }
- break;
- case SDL_GAMEPAD_FACE_STYLE_BAYX:
- switch (button) {
- case SDL_GAMEPAD_BUTTON_SOUTH:
- label = SDL_GAMEPAD_BUTTON_LABEL_B;
- break;
- case SDL_GAMEPAD_BUTTON_EAST:
- label = SDL_GAMEPAD_BUTTON_LABEL_A;
- break;
- case SDL_GAMEPAD_BUTTON_WEST:
- label = SDL_GAMEPAD_BUTTON_LABEL_Y;
- break;
- case SDL_GAMEPAD_BUTTON_NORTH:
- label = SDL_GAMEPAD_BUTTON_LABEL_X;
- break;
- default:
- break;
- }
- break;
- case SDL_GAMEPAD_FACE_STYLE_SONY:
- switch (button) {
- case SDL_GAMEPAD_BUTTON_SOUTH:
- label = SDL_GAMEPAD_BUTTON_LABEL_CROSS;
- break;
- case SDL_GAMEPAD_BUTTON_EAST:
- label = SDL_GAMEPAD_BUTTON_LABEL_CIRCLE;
- break;
- case SDL_GAMEPAD_BUTTON_WEST:
- label = SDL_GAMEPAD_BUTTON_LABEL_SQUARE;
- break;
- case SDL_GAMEPAD_BUTTON_NORTH:
- label = SDL_GAMEPAD_BUTTON_LABEL_TRIANGLE;
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- return label;
- }
- /**
- * Get the label of a button on a gamepad.
- */
- SDL_GamepadButtonLabel SDL_GetGamepadButtonLabelForType(SDL_GamepadType type, SDL_GamepadButton button)
- {
- return SDL_GetGamepadButtonLabelForFaceStyle(SDL_GetGamepadFaceStyleForGamepadType(type), button);
- }
- /**
- * Get the label of a button on a gamepad.
- */
- SDL_GamepadButtonLabel SDL_GetGamepadButtonLabel(SDL_Gamepad *gamepad, SDL_GamepadButton button)
- {
- SDL_GamepadFaceStyle face_style;
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, SDL_GAMEPAD_BUTTON_LABEL_UNKNOWN);
- face_style = gamepad->face_style;
- }
- SDL_UnlockJoysticks();
- return SDL_GetGamepadButtonLabelForFaceStyle(face_style, button);
- }
- /**
- * Get the number of touchpads on a gamepad.
- */
- int SDL_GetNumGamepadTouchpads(SDL_Gamepad *gamepad)
- {
- int result = true;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- result = joystick->ntouchpads;
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /**
- * Get the number of supported simultaneous fingers on a touchpad on a gamepad.
- */
- int SDL_GetNumGamepadTouchpadFingers(SDL_Gamepad *gamepad, int touchpad)
- {
- int result = true;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
- result = joystick->touchpads[touchpad].nfingers;
- } else {
- result = SDL_InvalidParamError("touchpad");
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /**
- * Get the current state of a finger on a touchpad on a gamepad.
- */
- bool SDL_GetGamepadTouchpadFinger(SDL_Gamepad *gamepad, int touchpad, int finger, bool *down, float *x, float *y, float *pressure)
- {
- bool result = false;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
- SDL_JoystickTouchpadInfo *touchpad_info = &joystick->touchpads[touchpad];
- if (finger >= 0 && finger < touchpad_info->nfingers) {
- SDL_JoystickTouchpadFingerInfo *info = &touchpad_info->fingers[finger];
- if (down) {
- *down = info->down;
- }
- if (x) {
- *x = info->x;
- }
- if (y) {
- *y = info->y;
- }
- if (pressure) {
- *pressure = info->pressure;
- }
- result = true;
- } else {
- result = SDL_InvalidParamError("finger");
- }
- } else {
- result = SDL_InvalidParamError("touchpad");
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /**
- * Return whether a gamepad has a particular sensor.
- */
- bool SDL_GamepadHasSensor(SDL_Gamepad *gamepad, SDL_SensorType type)
- {
- bool result = false;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- int i;
- for (i = 0; i < joystick->nsensors; ++i) {
- if (joystick->sensors[i].type == type) {
- result = true;
- break;
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Set whether data reporting for a gamepad sensor is enabled
- */
- bool SDL_SetGamepadSensorEnabled(SDL_Gamepad *gamepad, SDL_SensorType type, bool enabled)
- {
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- int i;
- for (i = 0; i < joystick->nsensors; ++i) {
- SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
- if (sensor->type == type) {
- if (sensor->enabled == (enabled != false)) {
- SDL_UnlockJoysticks();
- return true;
- }
- if (type == SDL_SENSOR_ACCEL && joystick->accel_sensor) {
- if (enabled) {
- joystick->accel = SDL_OpenSensor(joystick->accel_sensor);
- if (!joystick->accel) {
- SDL_UnlockJoysticks();
- return false;
- }
- } else {
- if (joystick->accel) {
- SDL_CloseSensor(joystick->accel);
- joystick->accel = NULL;
- }
- }
- } else if (type == SDL_SENSOR_GYRO && joystick->gyro_sensor) {
- if (enabled) {
- joystick->gyro = SDL_OpenSensor(joystick->gyro_sensor);
- if (!joystick->gyro) {
- SDL_UnlockJoysticks();
- return false;
- }
- } else {
- if (joystick->gyro) {
- SDL_CloseSensor(joystick->gyro);
- joystick->gyro = NULL;
- }
- }
- } else {
- if (enabled) {
- if (joystick->nsensors_enabled == 0) {
- if (!joystick->driver->SetSensorsEnabled(joystick, true)) {
- SDL_UnlockJoysticks();
- return false;
- }
- }
- ++joystick->nsensors_enabled;
- } else {
- if (joystick->nsensors_enabled == 1) {
- if (!joystick->driver->SetSensorsEnabled(joystick, false)) {
- SDL_UnlockJoysticks();
- return false;
- }
- }
- --joystick->nsensors_enabled;
- }
- }
- sensor->enabled = enabled;
- SDL_UnlockJoysticks();
- return true;
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- return SDL_Unsupported();
- }
- /*
- * Query whether sensor data reporting is enabled for a gamepad
- */
- bool SDL_GamepadSensorEnabled(SDL_Gamepad *gamepad, SDL_SensorType type)
- {
- bool result = false;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- int i;
- for (i = 0; i < joystick->nsensors; ++i) {
- if (joystick->sensors[i].type == type) {
- result = joystick->sensors[i].enabled;
- break;
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Get the data rate of a gamepad sensor.
- */
- float SDL_GetGamepadSensorDataRate(SDL_Gamepad *gamepad, SDL_SensorType type)
- {
- float result = 0.0f;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- int i;
- for (i = 0; i < joystick->nsensors; ++i) {
- SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
- if (sensor->type == type) {
- result = sensor->rate;
- break;
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Get the current state of a gamepad sensor.
- */
- bool SDL_GetGamepadSensorData(SDL_Gamepad *gamepad, SDL_SensorType type, float *data, int num_values)
- {
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- int i;
- for (i = 0; i < joystick->nsensors; ++i) {
- SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
- if (sensor->type == type) {
- num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
- SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
- SDL_UnlockJoysticks();
- return true;
- }
- }
- }
- }
- SDL_UnlockJoysticks();
- return SDL_Unsupported();
- }
- SDL_JoystickID SDL_GetGamepadID(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return 0;
- }
- return SDL_GetJoystickID(joystick);
- }
- SDL_PropertiesID SDL_GetGamepadProperties(SDL_Gamepad *gamepad)
- {
- SDL_PropertiesID result = true;
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, 0);
- result = SDL_GetJoystickProperties(gamepad->joystick);
- }
- SDL_UnlockJoysticks();
- return result;
- }
- const char *SDL_GetGamepadName(SDL_Gamepad *gamepad)
- {
- const char *result = NULL;
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, NULL);
- if (SDL_strcmp(gamepad->name, "*") == 0 ||
- gamepad->joystick->steam_handle != 0) {
- result = SDL_GetJoystickName(gamepad->joystick);
- } else {
- result = SDL_GetPersistentString(gamepad->name);
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- const char *SDL_GetGamepadPath(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return NULL;
- }
- return SDL_GetJoystickPath(joystick);
- }
- SDL_GamepadType SDL_GetGamepadType(SDL_Gamepad *gamepad)
- {
- SDL_GamepadType type;
- const SDL_SteamVirtualGamepadInfo *info;
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, SDL_GAMEPAD_TYPE_UNKNOWN);
- info = SDL_GetJoystickVirtualGamepadInfoForID(gamepad->joystick->instance_id);
- if (info) {
- type = info->type;
- } else {
- type = gamepad->type;
- }
- }
- SDL_UnlockJoysticks();
- return type;
- }
- SDL_GamepadType SDL_GetRealGamepadType(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return SDL_GAMEPAD_TYPE_UNKNOWN;
- }
- return SDL_GetGamepadTypeFromGUID(SDL_GetJoystickGUID(joystick), SDL_GetJoystickName(joystick));
- }
- int SDL_GetGamepadPlayerIndex(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return -1;
- }
- return SDL_GetJoystickPlayerIndex(joystick);
- }
- /**
- * Set the player index of an opened gamepad
- */
- bool SDL_SetGamepadPlayerIndex(SDL_Gamepad *gamepad, int player_index)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- // SDL_SetError() will have been called already by SDL_GetGamepadJoystick()
- return false;
- }
- return SDL_SetJoystickPlayerIndex(joystick, player_index);
- }
- Uint16 SDL_GetGamepadVendor(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return 0;
- }
- return SDL_GetJoystickVendor(joystick);
- }
- Uint16 SDL_GetGamepadProduct(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return 0;
- }
- return SDL_GetJoystickProduct(joystick);
- }
- Uint16 SDL_GetGamepadProductVersion(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return 0;
- }
- return SDL_GetJoystickProductVersion(joystick);
- }
- Uint16 SDL_GetGamepadFirmwareVersion(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return 0;
- }
- return SDL_GetJoystickFirmwareVersion(joystick);
- }
- const char * SDL_GetGamepadSerial(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return NULL;
- }
- return SDL_GetJoystickSerial(joystick);
- }
- Uint64 SDL_GetGamepadSteamHandle(SDL_Gamepad *gamepad)
- {
- Uint64 handle = 0;
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, 0);
- handle = gamepad->joystick->steam_handle;
- }
- SDL_UnlockJoysticks();
- return handle;
- }
- SDL_JoystickConnectionState SDL_GetGamepadConnectionState(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return SDL_JOYSTICK_CONNECTION_INVALID;
- }
- return SDL_GetJoystickConnectionState(joystick);
- }
- SDL_PowerState SDL_GetGamepadPowerInfo(SDL_Gamepad *gamepad, int *percent)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (percent) {
- *percent = -1;
- }
- if (!joystick) {
- return SDL_POWERSTATE_ERROR;
- }
- return SDL_GetJoystickPowerInfo(joystick, percent);
- }
- /*
- * Return if the gamepad in question is currently attached to the system,
- * \return 0 if not plugged in, 1 if still present.
- */
- bool SDL_GamepadConnected(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return false;
- }
- return SDL_JoystickConnected(joystick);
- }
- /*
- * Get the joystick for this gamepad
- */
- SDL_Joystick *SDL_GetGamepadJoystick(SDL_Gamepad *gamepad)
- {
- SDL_Joystick *joystick;
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, NULL);
- joystick = gamepad->joystick;
- }
- SDL_UnlockJoysticks();
- return joystick;
- }
- /*
- * Return the SDL_Gamepad associated with an instance id.
- */
- SDL_Gamepad *SDL_GetGamepadFromID(SDL_JoystickID joyid)
- {
- SDL_Gamepad *gamepad;
- SDL_LockJoysticks();
- gamepad = SDL_gamepads;
- while (gamepad) {
- if (gamepad->joystick->instance_id == joyid) {
- SDL_UnlockJoysticks();
- return gamepad;
- }
- gamepad = gamepad->next;
- }
- SDL_UnlockJoysticks();
- return NULL;
- }
- /**
- * Return the SDL_Gamepad associated with a player index.
- */
- SDL_Gamepad *SDL_GetGamepadFromPlayerIndex(int player_index)
- {
- SDL_Gamepad *result = NULL;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetJoystickFromPlayerIndex(player_index);
- if (joystick) {
- result = SDL_GetGamepadFromID(joystick->instance_id);
- }
- }
- SDL_UnlockJoysticks();
- return result;
- }
- /*
- * Get the SDL joystick layer bindings for this gamepad
- */
- SDL_GamepadBinding **SDL_GetGamepadBindings(SDL_Gamepad *gamepad, int *count)
- {
- SDL_GamepadBinding **bindings = NULL;
- if (count) {
- *count = 0;
- }
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, NULL);
- size_t pointers_size = ((gamepad->num_bindings + 1) * sizeof(SDL_GamepadBinding *));
- size_t elements_size = (gamepad->num_bindings * sizeof(SDL_GamepadBinding));
- bindings = (SDL_GamepadBinding **)SDL_malloc(pointers_size + elements_size);
- if (bindings) {
- SDL_GamepadBinding *binding = (SDL_GamepadBinding *)((Uint8 *)bindings + pointers_size);
- int i;
- for (i = 0; i < gamepad->num_bindings; ++i, ++binding) {
- bindings[i] = binding;
- SDL_copyp(binding, &gamepad->bindings[i]);
- }
- bindings[i] = NULL;
- if (count) {
- *count = gamepad->num_bindings;
- }
- }
- }
- SDL_UnlockJoysticks();
- return bindings;
- }
- bool SDL_RumbleGamepad(SDL_Gamepad *gamepad, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return false;
- }
- return SDL_RumbleJoystick(joystick, low_frequency_rumble, high_frequency_rumble, duration_ms);
- }
- bool SDL_RumbleGamepadTriggers(SDL_Gamepad *gamepad, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return false;
- }
- return SDL_RumbleJoystickTriggers(joystick, left_rumble, right_rumble, duration_ms);
- }
- bool SDL_SetGamepadLED(SDL_Gamepad *gamepad, Uint8 red, Uint8 green, Uint8 blue)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return false;
- }
- return SDL_SetJoystickLED(joystick, red, green, blue);
- }
- bool SDL_SendGamepadEffect(SDL_Gamepad *gamepad, const void *data, int size)
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (!joystick) {
- return false;
- }
- return SDL_SendJoystickEffect(joystick, data, size);
- }
- void SDL_CloseGamepad(SDL_Gamepad *gamepad)
- {
- SDL_Gamepad *gamepadlist, *gamepadlistprev;
- SDL_LockJoysticks();
- if (!SDL_ObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD)) {
- SDL_UnlockJoysticks();
- return;
- }
- // First decrement ref count
- if (--gamepad->ref_count > 0) {
- SDL_UnlockJoysticks();
- return;
- }
- SDL_CloseJoystick(gamepad->joystick);
- gamepadlist = SDL_gamepads;
- gamepadlistprev = NULL;
- while (gamepadlist) {
- if (gamepad == gamepadlist) {
- if (gamepadlistprev) {
- // unlink this entry
- gamepadlistprev->next = gamepadlist->next;
- } else {
- SDL_gamepads = gamepad->next;
- }
- break;
- }
- gamepadlistprev = gamepadlist;
- gamepadlist = gamepadlist->next;
- }
- SDL_SetObjectValid(gamepad, SDL_OBJECT_TYPE_GAMEPAD, false);
- SDL_free(gamepad->bindings);
- SDL_free(gamepad->last_match_axis);
- SDL_free(gamepad->last_hat_mask);
- SDL_free(gamepad);
- SDL_UnlockJoysticks();
- }
- /*
- * Quit the gamepad subsystem
- */
- void SDL_QuitGamepads(void)
- {
- SDL_Gamepad *gamepad;
- SDL_LockJoysticks();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- SDL_PrivateGamepadRemoved(gamepad->joystick->instance_id);
- }
- SDL_gamepads_initialized = false;
- SDL_RemoveEventWatch(SDL_GamepadEventWatcher, NULL);
- while (SDL_gamepads) {
- SDL_gamepads->ref_count = 1;
- SDL_CloseGamepad(SDL_gamepads);
- }
- SDL_UnlockJoysticks();
- }
- void SDL_QuitGamepadMappings(void)
- {
- GamepadMapping_t *pGamepadMap;
- SDL_AssertJoysticksLocked();
- while (s_pSupportedGamepads) {
- pGamepadMap = s_pSupportedGamepads;
- s_pSupportedGamepads = s_pSupportedGamepads->next;
- SDL_free(pGamepadMap->name);
- SDL_free(pGamepadMap->mapping);
- SDL_free(pGamepadMap);
- }
- SDL_FreeVIDPIDList(&SDL_allowed_gamepads);
- SDL_FreeVIDPIDList(&SDL_ignored_gamepads);
- if (s_gamepadInstanceIDs) {
- SDL_DestroyHashTable(s_gamepadInstanceIDs);
- s_gamepadInstanceIDs = NULL;
- }
- }
- /*
- * Event filter to transform joystick events into appropriate gamepad ones
- */
- static void SDL_SendGamepadAxis(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadAxis axis, Sint16 value)
- {
- SDL_AssertJoysticksLocked();
- // translate the event, if desired
- if (SDL_EventEnabled(SDL_EVENT_GAMEPAD_AXIS_MOTION)) {
- SDL_Event event;
- event.type = SDL_EVENT_GAMEPAD_AXIS_MOTION;
- event.common.timestamp = timestamp;
- event.gaxis.which = gamepad->joystick->instance_id;
- event.gaxis.axis = axis;
- event.gaxis.value = value;
- SDL_PushEvent(&event);
- }
- }
- static void SDL_SendGamepadButton(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadButton button, bool down)
- {
- SDL_Event event;
- SDL_AssertJoysticksLocked();
- if (button == SDL_GAMEPAD_BUTTON_INVALID) {
- return;
- }
- if (down) {
- event.type = SDL_EVENT_GAMEPAD_BUTTON_DOWN;
- } else {
- event.type = SDL_EVENT_GAMEPAD_BUTTON_UP;
- }
- if (button == SDL_GAMEPAD_BUTTON_GUIDE) {
- Uint64 now = SDL_GetTicks();
- if (down) {
- gamepad->guide_button_down = now;
- if (gamepad->joystick->delayed_guide_button) {
- // Skip duplicate press
- return;
- }
- } else {
- if (now < (gamepad->guide_button_down + SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS)) {
- gamepad->joystick->delayed_guide_button = true;
- return;
- }
- gamepad->joystick->delayed_guide_button = false;
- }
- }
- // translate the event, if desired
- if (SDL_EventEnabled(event.type)) {
- event.common.timestamp = timestamp;
- event.gbutton.which = gamepad->joystick->instance_id;
- event.gbutton.button = button;
- event.gbutton.down = down;
- SDL_PushEvent(&event);
- }
- }
- static const Uint32 SDL_gamepad_event_list[] = {
- SDL_EVENT_GAMEPAD_AXIS_MOTION,
- SDL_EVENT_GAMEPAD_BUTTON_DOWN,
- SDL_EVENT_GAMEPAD_BUTTON_UP,
- SDL_EVENT_GAMEPAD_ADDED,
- SDL_EVENT_GAMEPAD_REMOVED,
- SDL_EVENT_GAMEPAD_REMAPPED,
- SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN,
- SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION,
- SDL_EVENT_GAMEPAD_TOUCHPAD_UP,
- SDL_EVENT_GAMEPAD_SENSOR_UPDATE,
- };
- void SDL_SetGamepadEventsEnabled(bool enabled)
- {
- unsigned int i;
- for (i = 0; i < SDL_arraysize(SDL_gamepad_event_list); ++i) {
- SDL_SetEventEnabled(SDL_gamepad_event_list[i], enabled);
- }
- }
- bool SDL_GamepadEventsEnabled(void)
- {
- bool enabled = false;
- unsigned int i;
- for (i = 0; i < SDL_arraysize(SDL_gamepad_event_list); ++i) {
- enabled = SDL_EventEnabled(SDL_gamepad_event_list[i]);
- if (enabled) {
- break;
- }
- }
- return enabled;
- }
- void SDL_GamepadHandleDelayedGuideButton(SDL_Joystick *joystick)
- {
- SDL_Gamepad *gamepad;
- SDL_AssertJoysticksLocked();
- for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
- if (gamepad->joystick == joystick) {
- SDL_SendGamepadButton(0, gamepad, SDL_GAMEPAD_BUTTON_GUIDE, false);
- // Make sure we send an update complete event for this change
- if (!gamepad->joystick->update_complete) {
- gamepad->joystick->update_complete = SDL_GetTicksNS();
- }
- break;
- }
- }
- }
- const char *SDL_GetGamepadAppleSFSymbolsNameForButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
- {
- const char *result = NULL;
- #ifdef SDL_JOYSTICK_MFI
- const char *IOS_GetAppleSFSymbolsNameForButton(SDL_Gamepad *gamepad, SDL_GamepadButton button);
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, NULL);
- result = IOS_GetAppleSFSymbolsNameForButton(gamepad, button);
- }
- SDL_UnlockJoysticks();
- #endif
- return result;
- }
- const char *SDL_GetGamepadAppleSFSymbolsNameForAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
- {
- const char *result = NULL;
- #ifdef SDL_JOYSTICK_MFI
- const char *IOS_GetAppleSFSymbolsNameForAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis);
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, NULL);
- result = IOS_GetAppleSFSymbolsNameForAxis(gamepad, axis);
- }
- SDL_UnlockJoysticks();
- #endif
- return result;
- }
|