| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920 |
- /*
- 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();
- if (SDL_EventEnabled(SDL_EVENT_GAMEPAD_UPDATE_COMPLETE)) {
- 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(const SDL_Joystick *joystick, const 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_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick, (const void *)true, true);
- SDL_PrivateGamepadAdded(joystick);
- } else if (old_mapping && !new_mapping) {
- SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)joystick, (const void *)false, true);
- 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 (SDL_IsJoystickWheel(vendor, product)) {
- // We don't want to pick up Logitech FFB wheels here
- // Some versions of WINE will also not treat devices that show up as gamepads as wheels
- return 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,hint:!SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1,", 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_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 if (SDL_IsJoystick8BitDoController(vendor, product)) {
- SDL_strlcat(mapping_string, "paddle1:b12,paddle2:b11,paddle3:b14,paddle4:b13", 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;
- case SDL_GAMEPAD_TYPE_XBOXONE:
- 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_IsJoystickXboxSeriesX(vendor, product)) {
- // XBox Series X Controllers have a share button under the guide button
- SDL_strlcat(mapping_string, "misc1:b11,", 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
- bool need_fixup = false;
- 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 == binding->output.button) {
- // Old style binding
- need_fixup = true;
- }
- break;
- }
- }
- if (!need_fixup) {
- 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(0, false, SDL_HashID, SDL_KeyMatchID, NULL, NULL);
- }
- SDL_InsertIntoHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)instance_id, (void *)(uintptr_t)result, true);
- }
- }
- 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 = 0;
- 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 = 0;
- SDL_LockJoysticks();
- {
- SDL_Joystick *joystick = SDL_GetGamepadJoystick(gamepad);
- if (joystick) {
- if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
- result = joystick->touchpads[touchpad].nfingers;
- }
- }
- }
- 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 = 0;
- 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;
- }
|