SDL_gamecontroller.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. /* This is the game controller API for Simple DirectMedia Layer */
  20. #include "SDL_events.h"
  21. #include "SDL_hints.h"
  22. #include "SDL_timer.h"
  23. #include "SDL_sysjoystick.h"
  24. #include "SDL_joystick_c.h"
  25. #include "SDL_gamecontrollerdb.h"
  26. #include "controller_type.h"
  27. #include "usb_ids.h"
  28. #include "hidapi/SDL_hidapi_nintendo.h"
  29. #if !SDL_EVENTS_DISABLED
  30. #include "../events/SDL_events_c.h"
  31. #endif
  32. #if defined(__ANDROID__)
  33. #include "SDL_system.h"
  34. #endif
  35. /* Many controllers turn the center button into an instantaneous button press */
  36. #define SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS 250
  37. #define SDL_CONTROLLER_CRC_FIELD "crc:"
  38. #define SDL_CONTROLLER_CRC_FIELD_SIZE 4 /* hard-coded for speed */
  39. #define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
  40. #define SDL_CONTROLLER_PLATFORM_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD)
  41. #define SDL_CONTROLLER_HINT_FIELD "hint:"
  42. #define SDL_CONTROLLER_HINT_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_HINT_FIELD)
  43. #define SDL_CONTROLLER_SDKGE_FIELD "sdk>=:"
  44. #define SDL_CONTROLLER_SDKGE_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_SDKGE_FIELD)
  45. #define SDL_CONTROLLER_SDKLE_FIELD "sdk<=:"
  46. #define SDL_CONTROLLER_SDKLE_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_SDKLE_FIELD)
  47. /* a list of currently opened game controllers */
  48. static SDL_GameController *SDL_gamecontrollers SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  49. typedef struct
  50. {
  51. SDL_GameControllerBindType inputType;
  52. union
  53. {
  54. int button;
  55. struct
  56. {
  57. int axis;
  58. int axis_min;
  59. int axis_max;
  60. } axis;
  61. struct
  62. {
  63. int hat;
  64. int hat_mask;
  65. } hat;
  66. } input;
  67. SDL_GameControllerBindType outputType;
  68. union
  69. {
  70. SDL_GameControllerButton button;
  71. struct
  72. {
  73. SDL_GameControllerAxis axis;
  74. int axis_min;
  75. int axis_max;
  76. } axis;
  77. } output;
  78. } SDL_ExtendedGameControllerBind;
  79. /* our hard coded list of mapping support */
  80. typedef enum
  81. {
  82. SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT,
  83. SDL_CONTROLLER_MAPPING_PRIORITY_API,
  84. SDL_CONTROLLER_MAPPING_PRIORITY_USER,
  85. } SDL_ControllerMappingPriority;
  86. #define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
  87. typedef struct _ControllerMapping_t
  88. {
  89. SDL_JoystickGUID guid _guarded;
  90. char *name _guarded;
  91. char *mapping _guarded;
  92. SDL_ControllerMappingPriority priority _guarded;
  93. struct _ControllerMapping_t *next _guarded;
  94. } ControllerMapping_t;
  95. #undef _guarded
  96. static SDL_JoystickGUID s_zeroGUID;
  97. static ControllerMapping_t *s_pSupportedControllers SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  98. static ControllerMapping_t *s_pDefaultMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  99. static ControllerMapping_t *s_pXInputMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  100. static char gamecontroller_magic;
  101. #define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
  102. /* The SDL game controller structure */
  103. struct _SDL_GameController
  104. {
  105. const void *magic _guarded;
  106. SDL_Joystick *joystick _guarded; /* underlying joystick device */
  107. int ref_count _guarded;
  108. const char *name _guarded;
  109. ControllerMapping_t *mapping _guarded;
  110. int num_bindings _guarded;
  111. SDL_ExtendedGameControllerBind *bindings _guarded;
  112. SDL_ExtendedGameControllerBind **last_match_axis _guarded;
  113. Uint8 *last_hat_mask _guarded;
  114. Uint32 guide_button_down _guarded;
  115. struct _SDL_GameController *next _guarded; /* pointer to next game controller we have allocated */
  116. };
  117. #undef _guarded
  118. #define CHECK_GAMECONTROLLER_MAGIC(gamecontroller, retval) \
  119. if (!gamecontroller || gamecontroller->magic != &gamecontroller_magic || \
  120. !SDL_PrivateJoystickValid(gamecontroller->joystick)) { \
  121. SDL_InvalidParamError("gamecontroller"); \
  122. SDL_UnlockJoysticks(); \
  123. return retval; \
  124. }
  125. typedef struct
  126. {
  127. int num_entries;
  128. int max_entries;
  129. Uint32 *entries;
  130. } SDL_vidpid_list;
  131. static SDL_vidpid_list SDL_allowed_controllers;
  132. static SDL_vidpid_list SDL_ignored_controllers;
  133. static void SDL_LoadVIDPIDListFromHint(const char *hint, SDL_vidpid_list *list)
  134. {
  135. Uint32 entry;
  136. char *spot;
  137. char *file = NULL;
  138. list->num_entries = 0;
  139. if (hint && *hint == '@') {
  140. spot = file = (char *)SDL_LoadFile(hint + 1, NULL);
  141. } else {
  142. spot = (char *)hint;
  143. }
  144. if (spot == NULL) {
  145. return;
  146. }
  147. while ((spot = SDL_strstr(spot, "0x")) != NULL) {
  148. entry = (Uint16)SDL_strtol(spot, &spot, 0);
  149. entry <<= 16;
  150. spot = SDL_strstr(spot, "0x");
  151. if (spot == NULL) {
  152. break;
  153. }
  154. entry |= (Uint16)SDL_strtol(spot, &spot, 0);
  155. if (list->num_entries == list->max_entries) {
  156. int max_entries = list->max_entries + 16;
  157. Uint32 *entries = (Uint32 *)SDL_realloc(list->entries, max_entries * sizeof(*list->entries));
  158. if (entries == NULL) {
  159. /* Out of memory, go with what we have already */
  160. break;
  161. }
  162. list->entries = entries;
  163. list->max_entries = max_entries;
  164. }
  165. list->entries[list->num_entries++] = entry;
  166. }
  167. if (file) {
  168. SDL_free(file);
  169. }
  170. }
  171. static void SDLCALL SDL_GameControllerIgnoreDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  172. {
  173. SDL_LoadVIDPIDListFromHint(hint, &SDL_ignored_controllers);
  174. }
  175. static void SDLCALL SDL_GameControllerIgnoreDevicesExceptChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  176. {
  177. SDL_LoadVIDPIDListFromHint(hint, &SDL_allowed_controllers);
  178. }
  179. static ControllerMapping_t *SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing, SDL_ControllerMappingPriority priority);
  180. static int SDL_PrivateGameControllerAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
  181. static int SDL_PrivateGameControllerButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button, Uint8 state);
  182. static SDL_bool HasSameOutput(SDL_ExtendedGameControllerBind *a, SDL_ExtendedGameControllerBind *b)
  183. {
  184. if (a->outputType != b->outputType) {
  185. return SDL_FALSE;
  186. }
  187. if (a->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  188. return a->output.axis.axis == b->output.axis.axis;
  189. } else {
  190. return a->output.button == b->output.button;
  191. }
  192. }
  193. static void ResetOutput(SDL_GameController *gamecontroller, SDL_ExtendedGameControllerBind *bind)
  194. {
  195. if (bind->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  196. SDL_PrivateGameControllerAxis(gamecontroller, bind->output.axis.axis, 0);
  197. } else {
  198. SDL_PrivateGameControllerButton(gamecontroller, bind->output.button, SDL_RELEASED);
  199. }
  200. }
  201. static void HandleJoystickAxis(SDL_GameController *gamecontroller, int axis, int value)
  202. {
  203. int i;
  204. SDL_ExtendedGameControllerBind *last_match;
  205. SDL_ExtendedGameControllerBind *match = NULL;
  206. SDL_AssertJoysticksLocked();
  207. last_match = gamecontroller->last_match_axis[axis];
  208. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  209. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  210. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS &&
  211. axis == binding->input.axis.axis) {
  212. if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
  213. if (value >= binding->input.axis.axis_min &&
  214. value <= binding->input.axis.axis_max) {
  215. match = binding;
  216. break;
  217. }
  218. } else {
  219. if (value >= binding->input.axis.axis_max &&
  220. value <= binding->input.axis.axis_min) {
  221. match = binding;
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. if (last_match && (match == NULL || !HasSameOutput(last_match, match))) {
  228. /* Clear the last input that this axis generated */
  229. ResetOutput(gamecontroller, last_match);
  230. }
  231. if (match) {
  232. if (match->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  233. if (match->input.axis.axis_min != match->output.axis.axis_min || match->input.axis.axis_max != match->output.axis.axis_max) {
  234. float normalized_value = (float)(value - match->input.axis.axis_min) / (match->input.axis.axis_max - match->input.axis.axis_min);
  235. value = match->output.axis.axis_min + (int)(normalized_value * (match->output.axis.axis_max - match->output.axis.axis_min));
  236. }
  237. SDL_PrivateGameControllerAxis(gamecontroller, match->output.axis.axis, (Sint16)value);
  238. } else {
  239. Uint8 state;
  240. int threshold = match->input.axis.axis_min + (match->input.axis.axis_max - match->input.axis.axis_min) / 2;
  241. if (match->input.axis.axis_max < match->input.axis.axis_min) {
  242. state = (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
  243. } else {
  244. state = (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
  245. }
  246. SDL_PrivateGameControllerButton(gamecontroller, match->output.button, state);
  247. }
  248. }
  249. gamecontroller->last_match_axis[axis] = match;
  250. }
  251. static void HandleJoystickButton(SDL_GameController *gamecontroller, int button, Uint8 state)
  252. {
  253. int i;
  254. SDL_AssertJoysticksLocked();
  255. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  256. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  257. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON &&
  258. button == binding->input.button) {
  259. if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  260. int value = state ? binding->output.axis.axis_max : binding->output.axis.axis_min;
  261. SDL_PrivateGameControllerAxis(gamecontroller, binding->output.axis.axis, (Sint16)value);
  262. } else {
  263. SDL_PrivateGameControllerButton(gamecontroller, binding->output.button, state);
  264. }
  265. break;
  266. }
  267. }
  268. }
  269. static void HandleJoystickHat(SDL_GameController *gamecontroller, int hat, Uint8 value)
  270. {
  271. int i;
  272. Uint8 last_mask, changed_mask;
  273. SDL_AssertJoysticksLocked();
  274. last_mask = gamecontroller->last_hat_mask[hat];
  275. changed_mask = (last_mask ^ value);
  276. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  277. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  278. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT && hat == binding->input.hat.hat) {
  279. if ((changed_mask & binding->input.hat.hat_mask) != 0) {
  280. if (value & binding->input.hat.hat_mask) {
  281. if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  282. SDL_PrivateGameControllerAxis(gamecontroller, binding->output.axis.axis, (Sint16)binding->output.axis.axis_max);
  283. } else {
  284. SDL_PrivateGameControllerButton(gamecontroller, binding->output.button, SDL_PRESSED);
  285. }
  286. } else {
  287. ResetOutput(gamecontroller, binding);
  288. }
  289. }
  290. }
  291. }
  292. gamecontroller->last_hat_mask[hat] = value;
  293. }
  294. /* The joystick layer will _also_ send events to recenter before disconnect,
  295. but it has to make (sometimes incorrect) guesses at what being "centered"
  296. is. The game controller layer, however, can set a definite logical idle
  297. position, so set them all here. If we happened to already be at the
  298. center thanks to the joystick layer or idle hands, this won't generate
  299. duplicate events. */
  300. static void RecenterGameController(SDL_GameController *gamecontroller)
  301. {
  302. SDL_GameControllerButton button;
  303. SDL_GameControllerAxis axis;
  304. for (button = (SDL_GameControllerButton)0; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
  305. if (SDL_GameControllerGetButton(gamecontroller, button)) {
  306. SDL_PrivateGameControllerButton(gamecontroller, button, SDL_RELEASED);
  307. }
  308. }
  309. for (axis = (SDL_GameControllerAxis)0; axis < SDL_CONTROLLER_AXIS_MAX; axis++) {
  310. if (SDL_GameControllerGetAxis(gamecontroller, axis) != 0) {
  311. SDL_PrivateGameControllerAxis(gamecontroller, axis, 0);
  312. }
  313. }
  314. }
  315. /*
  316. * Event filter to fire controller events from joystick ones
  317. */
  318. static int SDLCALL SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
  319. {
  320. SDL_GameController *controller;
  321. switch (event->type) {
  322. case SDL_JOYAXISMOTION:
  323. {
  324. SDL_AssertJoysticksLocked();
  325. for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
  326. if (controller->joystick->instance_id == event->jaxis.which) {
  327. HandleJoystickAxis(controller, event->jaxis.axis, event->jaxis.value);
  328. break;
  329. }
  330. }
  331. } break;
  332. case SDL_JOYBUTTONDOWN:
  333. case SDL_JOYBUTTONUP:
  334. {
  335. SDL_AssertJoysticksLocked();
  336. for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
  337. if (controller->joystick->instance_id == event->jbutton.which) {
  338. HandleJoystickButton(controller, event->jbutton.button, event->jbutton.state);
  339. break;
  340. }
  341. }
  342. } break;
  343. case SDL_JOYHATMOTION:
  344. {
  345. SDL_AssertJoysticksLocked();
  346. for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
  347. if (controller->joystick->instance_id == event->jhat.which) {
  348. HandleJoystickHat(controller, event->jhat.hat, event->jhat.value);
  349. break;
  350. }
  351. }
  352. } break;
  353. case SDL_JOYDEVICEADDED:
  354. {
  355. if (SDL_IsGameController(event->jdevice.which)) {
  356. SDL_Event deviceevent;
  357. deviceevent.type = SDL_CONTROLLERDEVICEADDED;
  358. deviceevent.cdevice.which = event->jdevice.which;
  359. SDL_PushEvent(&deviceevent);
  360. }
  361. } break;
  362. case SDL_JOYDEVICEREMOVED:
  363. {
  364. SDL_AssertJoysticksLocked();
  365. for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
  366. if (controller->joystick->instance_id == event->jdevice.which) {
  367. RecenterGameController(controller);
  368. break;
  369. }
  370. }
  371. /* We don't know if this was a game controller, so go ahead and send an event */
  372. {
  373. SDL_Event deviceevent;
  374. deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
  375. deviceevent.cdevice.which = event->jdevice.which;
  376. SDL_PushEvent(&deviceevent);
  377. }
  378. } break;
  379. default:
  380. break;
  381. }
  382. return 1;
  383. }
  384. #ifdef __ANDROID__
  385. /*
  386. * Helper function to guess at a mapping based on the elements reported for this controller
  387. */
  388. static ControllerMapping_t *SDL_CreateMappingForAndroidController(SDL_JoystickGUID guid)
  389. {
  390. const int face_button_mask = ((1 << SDL_CONTROLLER_BUTTON_A) |
  391. (1 << SDL_CONTROLLER_BUTTON_B) |
  392. (1 << SDL_CONTROLLER_BUTTON_X) |
  393. (1 << SDL_CONTROLLER_BUTTON_Y));
  394. SDL_bool existing;
  395. char mapping_string[1024];
  396. int button_mask;
  397. int axis_mask;
  398. button_mask = SDL_SwapLE16(*(Uint16 *)(&guid.data[sizeof(guid.data) - 4]));
  399. axis_mask = SDL_SwapLE16(*(Uint16 *)(&guid.data[sizeof(guid.data) - 2]));
  400. if (!button_mask && !axis_mask) {
  401. /* Accelerometer, shouldn't have a game controller mapping */
  402. return NULL;
  403. }
  404. if (!(button_mask & face_button_mask)) {
  405. /* We don't know what buttons or axes are supported, don't make up a mapping */
  406. return NULL;
  407. }
  408. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  409. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_A)) {
  410. SDL_strlcat(mapping_string, "a:b0,", sizeof(mapping_string));
  411. }
  412. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_B)) {
  413. SDL_strlcat(mapping_string, "b:b1,", sizeof(mapping_string));
  414. } else if (button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) {
  415. /* Use the back button as "B" for easy UI navigation with TV remotes */
  416. SDL_strlcat(mapping_string, "b:b4,", sizeof(mapping_string));
  417. button_mask &= ~(1 << SDL_CONTROLLER_BUTTON_BACK);
  418. }
  419. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_X)) {
  420. SDL_strlcat(mapping_string, "x:b2,", sizeof(mapping_string));
  421. }
  422. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_Y)) {
  423. SDL_strlcat(mapping_string, "y:b3,", sizeof(mapping_string));
  424. }
  425. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) {
  426. SDL_strlcat(mapping_string, "back:b4,", sizeof(mapping_string));
  427. }
  428. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_GUIDE)) {
  429. /* The guide button generally isn't functional (or acts as a home button) on most Android controllers before Android 11 */
  430. if (SDL_GetAndroidSDKVersion() >= 30 /* Android 11 */) {
  431. SDL_strlcat(mapping_string, "guide:b5,", sizeof(mapping_string));
  432. }
  433. }
  434. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
  435. SDL_strlcat(mapping_string, "start:b6,", sizeof(mapping_string));
  436. }
  437. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK)) {
  438. SDL_strlcat(mapping_string, "leftstick:b7,", sizeof(mapping_string));
  439. }
  440. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK)) {
  441. SDL_strlcat(mapping_string, "rightstick:b8,", sizeof(mapping_string));
  442. }
  443. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) {
  444. SDL_strlcat(mapping_string, "leftshoulder:b9,", sizeof(mapping_string));
  445. }
  446. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) {
  447. SDL_strlcat(mapping_string, "rightshoulder:b10,", sizeof(mapping_string));
  448. }
  449. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_UP)) {
  450. SDL_strlcat(mapping_string, "dpup:b11,", sizeof(mapping_string));
  451. }
  452. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_DOWN)) {
  453. SDL_strlcat(mapping_string, "dpdown:b12,", sizeof(mapping_string));
  454. }
  455. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_LEFT)) {
  456. SDL_strlcat(mapping_string, "dpleft:b13,", sizeof(mapping_string));
  457. }
  458. if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) {
  459. SDL_strlcat(mapping_string, "dpright:b14,", sizeof(mapping_string));
  460. }
  461. if (axis_mask & (1 << SDL_CONTROLLER_AXIS_LEFTX)) {
  462. SDL_strlcat(mapping_string, "leftx:a0,", sizeof(mapping_string));
  463. }
  464. if (axis_mask & (1 << SDL_CONTROLLER_AXIS_LEFTY)) {
  465. SDL_strlcat(mapping_string, "lefty:a1,", sizeof(mapping_string));
  466. }
  467. if (axis_mask & (1 << SDL_CONTROLLER_AXIS_RIGHTX)) {
  468. SDL_strlcat(mapping_string, "rightx:a2,", sizeof(mapping_string));
  469. }
  470. if (axis_mask & (1 << SDL_CONTROLLER_AXIS_RIGHTY)) {
  471. SDL_strlcat(mapping_string, "righty:a3,", sizeof(mapping_string));
  472. }
  473. if (axis_mask & (1 << SDL_CONTROLLER_AXIS_TRIGGERLEFT)) {
  474. SDL_strlcat(mapping_string, "lefttrigger:a4,", sizeof(mapping_string));
  475. }
  476. if (axis_mask & (1 << SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) {
  477. SDL_strlcat(mapping_string, "righttrigger:a5,", sizeof(mapping_string));
  478. }
  479. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
  480. }
  481. #endif /* __ANDROID__ */
  482. /*
  483. * Helper function to guess at a mapping for HIDAPI controllers
  484. */
  485. static ControllerMapping_t *SDL_CreateMappingForHIDAPIController(SDL_JoystickGUID guid)
  486. {
  487. SDL_bool existing;
  488. char mapping_string[1024];
  489. Uint16 vendor;
  490. Uint16 product;
  491. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  492. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
  493. if ((vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) ||
  494. (vendor == USB_VENDOR_DRAGONRISE && product == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER)) {
  495. /* GameCube driver has 12 buttons and 6 axes */
  496. 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));
  497. } else if (vendor == USB_VENDOR_NINTENDO &&
  498. (guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCLeft ||
  499. guid.data[15] == k_eSwitchDeviceInfoControllerType_HVCRight ||
  500. guid.data[15] == k_eSwitchDeviceInfoControllerType_NESLeft ||
  501. guid.data[15] == k_eSwitchDeviceInfoControllerType_NESRight ||
  502. guid.data[15] == k_eSwitchDeviceInfoControllerType_SNES ||
  503. guid.data[15] == k_eSwitchDeviceInfoControllerType_N64 ||
  504. guid.data[15] == k_eSwitchDeviceInfoControllerType_SEGA_Genesis ||
  505. guid.data[15] == k_eWiiExtensionControllerType_None ||
  506. guid.data[15] == k_eWiiExtensionControllerType_Nunchuk ||
  507. guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
  508. guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConRight)) {
  509. switch (guid.data[15]) {
  510. case k_eSwitchDeviceInfoControllerType_HVCLeft:
  511. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,leftshoulder:b9,rightshoulder:b10,start:b6,", sizeof(mapping_string));
  512. break;
  513. case k_eSwitchDeviceInfoControllerType_HVCRight:
  514. SDL_strlcat(mapping_string, "a:b0,b:b1,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,leftshoulder:b9,rightshoulder:b10,", sizeof(mapping_string));
  515. break;
  516. case k_eSwitchDeviceInfoControllerType_NESLeft:
  517. case k_eSwitchDeviceInfoControllerType_NESRight:
  518. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,leftshoulder:b9,rightshoulder:b10,start:b6,", sizeof(mapping_string));
  519. break;
  520. case k_eSwitchDeviceInfoControllerType_SNES:
  521. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,leftshoulder:b9,lefttrigger:a4,rightshoulder:b10,righttrigger:a5,start:b6,x:b2,y:b3,", sizeof(mapping_string));
  522. break;
  523. case k_eSwitchDeviceInfoControllerType_N64:
  524. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,righttrigger:a5,start:b6,x:b2,y:b3,misc1:b15,", sizeof(mapping_string));
  525. break;
  526. case k_eSwitchDeviceInfoControllerType_SEGA_Genesis:
  527. SDL_strlcat(mapping_string, "a:b0,b:b1,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,rightshoulder:b10,righttrigger:a5,start:b6,misc1:b15,", sizeof(mapping_string));
  528. break;
  529. case k_eWiiExtensionControllerType_None:
  530. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,start:b6,x:b2,y:b3,", sizeof(mapping_string));
  531. break;
  532. case k_eWiiExtensionControllerType_Nunchuk:
  533. {
  534. /* FIXME: Should we map this to the left or right side? */
  535. const SDL_bool map_nunchuck_left_side = SDL_TRUE;
  536. if (map_nunchuck_left_side) {
  537. 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));
  538. } else {
  539. 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));
  540. }
  541. } break;
  542. default:
  543. if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS, SDL_FALSE)) {
  544. /* Vertical mode */
  545. if (guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  546. SDL_strlcat(mapping_string, "back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,misc1:b15,paddle2:b17,paddle4:b19,", sizeof(mapping_string));
  547. } else {
  548. 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:b16,paddle3:b18,", sizeof(mapping_string));
  549. }
  550. } else {
  551. /* Mini gamepad mode */
  552. if (guid.data[15] == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
  553. 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:b17,paddle4:b19,", sizeof(mapping_string));
  554. } else {
  555. 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:b16,paddle3:b18,", sizeof(mapping_string));
  556. }
  557. }
  558. break;
  559. }
  560. } else {
  561. /* All other controllers have the standard set of 19 buttons and 6 axes */
  562. SDL_strlcat(mapping_string, "a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,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));
  563. if (SDL_IsJoystickXboxSeriesX(vendor, product)) {
  564. /* XBox Series X Controllers have a share button under the guide button */
  565. SDL_strlcat(mapping_string, "misc1:b15,", sizeof(mapping_string));
  566. } else if (SDL_IsJoystickXboxOneElite(vendor, product)) {
  567. /* XBox One Elite Controllers have 4 back paddle buttons */
  568. SDL_strlcat(mapping_string, "paddle1:b15,paddle2:b17,paddle3:b16,paddle4:b18,", sizeof(mapping_string));
  569. } else if (SDL_IsJoystickSteamController(vendor, product)) {
  570. /* Steam controllers have 2 back paddle buttons */
  571. SDL_strlcat(mapping_string, "paddle1:b16,paddle2:b15,", sizeof(mapping_string));
  572. } else if (SDL_IsJoystickNintendoSwitchJoyConPair(vendor, product)) {
  573. /* The Nintendo Switch Joy-Con combined controller has a share button and paddles */
  574. SDL_strlcat(mapping_string, "misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,", sizeof(mapping_string));
  575. } else {
  576. switch (SDL_GetJoystickGameControllerTypeFromGUID(guid, NULL)) {
  577. case SDL_CONTROLLER_TYPE_PS4:
  578. /* PS4 controllers have an additional touchpad button */
  579. SDL_strlcat(mapping_string, "touchpad:b15,", sizeof(mapping_string));
  580. break;
  581. case SDL_CONTROLLER_TYPE_PS5:
  582. /* PS5 controllers have a microphone button and an additional touchpad button */
  583. SDL_strlcat(mapping_string, "touchpad:b15,misc1:b16,", sizeof(mapping_string));
  584. /* DualSense Edge controllers have paddles */
  585. if (SDL_IsJoystickDualSenseEdge(vendor, product)) {
  586. SDL_strlcat(mapping_string, "paddle1:b20,paddle2:b19,paddle3:b18,paddle4:b17,", sizeof(mapping_string));
  587. }
  588. break;
  589. case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO:
  590. /* Nintendo Switch Pro controllers have a screenshot button */
  591. SDL_strlcat(mapping_string, "misc1:b15,", sizeof(mapping_string));
  592. break;
  593. case SDL_CONTROLLER_TYPE_AMAZON_LUNA:
  594. /* Amazon Luna Controller has a mic button under the guide button */
  595. SDL_strlcat(mapping_string, "misc1:b15,", sizeof(mapping_string));
  596. break;
  597. case SDL_CONTROLLER_TYPE_GOOGLE_STADIA:
  598. /* The Google Stadia controller has a share button and a Google Assistant button */
  599. SDL_strlcat(mapping_string, "misc1:b15,", sizeof(mapping_string));
  600. break;
  601. case SDL_CONTROLLER_TYPE_NVIDIA_SHIELD:
  602. /* The NVIDIA SHIELD controller has a share button between back and start buttons */
  603. SDL_strlcat(mapping_string, "misc1:b15,", sizeof(mapping_string));
  604. if (product == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V103) {
  605. /* The original SHIELD controller has a touchpad as well */
  606. SDL_strlcat(mapping_string, "touchpad:b16,", sizeof(mapping_string));
  607. }
  608. break;
  609. default:
  610. if (vendor == 0 && product == 0) {
  611. /* This is a Bluetooth Nintendo Switch Pro controller */
  612. SDL_strlcat(mapping_string, "misc1:b15,", sizeof(mapping_string));
  613. }
  614. break;
  615. }
  616. }
  617. }
  618. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
  619. }
  620. /*
  621. * Helper function to guess at a mapping for RAWINPUT controllers
  622. */
  623. static ControllerMapping_t *SDL_CreateMappingForRAWINPUTController(SDL_JoystickGUID guid)
  624. {
  625. SDL_bool existing;
  626. char mapping_string[1024];
  627. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  628. 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));
  629. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
  630. }
  631. /*
  632. * Helper function to guess at a mapping for WGI controllers
  633. */
  634. static ControllerMapping_t *SDL_CreateMappingForWGIController(SDL_JoystickGUID guid)
  635. {
  636. SDL_bool existing;
  637. char mapping_string[1024];
  638. if (guid.data[15] != SDL_JOYSTICK_TYPE_GAMECONTROLLER) {
  639. return NULL;
  640. }
  641. SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
  642. 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));
  643. return SDL_PrivateAddMappingForGUID(guid, mapping_string, &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
  644. }
  645. /*
  646. * Helper function to scan the mappings database for a controller with the specified GUID
  647. */
  648. static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool match_crc, SDL_bool match_version)
  649. {
  650. ControllerMapping_t *mapping;
  651. Uint16 crc = 0;
  652. SDL_AssertJoysticksLocked();
  653. if (match_crc) {
  654. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc);
  655. }
  656. /* Clear the CRC from the GUID for matching, the mappings never include it in the GUID */
  657. SDL_SetJoystickGUIDCRC(&guid, 0);
  658. if (!match_version) {
  659. SDL_SetJoystickGUIDVersion(&guid, 0);
  660. }
  661. for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
  662. SDL_JoystickGUID mapping_guid;
  663. if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
  664. continue;
  665. }
  666. SDL_memcpy(&mapping_guid, &mapping->guid, sizeof(mapping_guid));
  667. if (!match_version) {
  668. SDL_SetJoystickGUIDVersion(&mapping_guid, 0);
  669. }
  670. if (SDL_memcmp(&guid, &mapping_guid, sizeof(guid)) == 0) {
  671. Uint16 mapping_crc = 0;
  672. if (match_crc) {
  673. const char *crc_string = SDL_strstr(mapping->mapping, SDL_CONTROLLER_CRC_FIELD);
  674. if (crc_string) {
  675. mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16);
  676. }
  677. }
  678. if (crc == mapping_crc) {
  679. return mapping;
  680. }
  681. }
  682. }
  683. return NULL;
  684. }
  685. /*
  686. * Helper function to scan the mappings database for a controller with the specified GUID
  687. */
  688. static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool adding_mapping)
  689. {
  690. ControllerMapping_t *mapping;
  691. Uint16 vendor, product, crc;
  692. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, &crc);
  693. if (crc) {
  694. /* First check for exact CRC matching */
  695. mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE, SDL_TRUE);
  696. if (mapping) {
  697. return mapping;
  698. }
  699. }
  700. /* Now check for a mapping without CRC */
  701. mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE, SDL_TRUE);
  702. if (mapping) {
  703. return mapping;
  704. }
  705. if (adding_mapping) {
  706. /* We didn't find an existing mapping */
  707. return NULL;
  708. }
  709. /* Try harder to get the best match, or create a mapping */
  710. if (vendor && product) {
  711. /* Try again, ignoring the version */
  712. if (crc) {
  713. mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE, SDL_FALSE);
  714. if (mapping) {
  715. return mapping;
  716. }
  717. }
  718. mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE, SDL_FALSE);
  719. if (mapping) {
  720. return mapping;
  721. }
  722. }
  723. #if SDL_JOYSTICK_XINPUT
  724. if (SDL_IsJoystickXInput(guid)) {
  725. /* This is an XInput device */
  726. return s_pXInputMapping;
  727. }
  728. #endif
  729. if (SDL_IsJoystickHIDAPI(guid)) {
  730. mapping = SDL_CreateMappingForHIDAPIController(guid);
  731. } else if (SDL_IsJoystickRAWINPUT(guid)) {
  732. mapping = SDL_CreateMappingForRAWINPUTController(guid);
  733. } else if (SDL_IsJoystickWGI(guid)) {
  734. mapping = SDL_CreateMappingForWGIController(guid);
  735. } else if (SDL_IsJoystickVirtual(guid)) {
  736. /* We'll pick up a robust mapping in VIRTUAL_JoystickGetGamepadMapping */
  737. #ifdef __ANDROID__
  738. } else {
  739. mapping = SDL_CreateMappingForAndroidController(guid);
  740. #endif
  741. }
  742. return mapping;
  743. }
  744. static const char *map_StringForControllerAxis[] = {
  745. "leftx",
  746. "lefty",
  747. "rightx",
  748. "righty",
  749. "lefttrigger",
  750. "righttrigger",
  751. NULL
  752. };
  753. /*
  754. * convert a string to its enum equivalent
  755. */
  756. SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *str)
  757. {
  758. int entry;
  759. if (str == NULL || str[0] == '\0') {
  760. return SDL_CONTROLLER_AXIS_INVALID;
  761. }
  762. if (*str == '+' || *str == '-') {
  763. ++str;
  764. }
  765. for (entry = 0; map_StringForControllerAxis[entry]; ++entry) {
  766. if (SDL_strcasecmp(str, map_StringForControllerAxis[entry]) == 0) {
  767. return (SDL_GameControllerAxis)entry;
  768. }
  769. }
  770. return SDL_CONTROLLER_AXIS_INVALID;
  771. }
  772. /*
  773. * convert an enum to its string equivalent
  774. */
  775. const char *SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
  776. {
  777. if (axis > SDL_CONTROLLER_AXIS_INVALID && axis < SDL_CONTROLLER_AXIS_MAX) {
  778. return map_StringForControllerAxis[axis];
  779. }
  780. return NULL;
  781. }
  782. static const char *map_StringForControllerButton[] = {
  783. "a",
  784. "b",
  785. "x",
  786. "y",
  787. "back",
  788. "guide",
  789. "start",
  790. "leftstick",
  791. "rightstick",
  792. "leftshoulder",
  793. "rightshoulder",
  794. "dpup",
  795. "dpdown",
  796. "dpleft",
  797. "dpright",
  798. "misc1",
  799. "paddle1",
  800. "paddle2",
  801. "paddle3",
  802. "paddle4",
  803. "touchpad",
  804. NULL
  805. };
  806. /*
  807. * convert a string to its enum equivalent
  808. */
  809. SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *str)
  810. {
  811. int entry;
  812. if (str == NULL || str[0] == '\0') {
  813. return SDL_CONTROLLER_BUTTON_INVALID;
  814. }
  815. for (entry = 0; map_StringForControllerButton[entry]; ++entry) {
  816. if (SDL_strcasecmp(str, map_StringForControllerButton[entry]) == 0) {
  817. return (SDL_GameControllerButton)entry;
  818. }
  819. }
  820. return SDL_CONTROLLER_BUTTON_INVALID;
  821. }
  822. /*
  823. * convert an enum to its string equivalent
  824. */
  825. const char *SDL_GameControllerGetStringForButton(SDL_GameControllerButton button)
  826. {
  827. if (button > SDL_CONTROLLER_BUTTON_INVALID && button < SDL_CONTROLLER_BUTTON_MAX) {
  828. return map_StringForControllerButton[button];
  829. }
  830. return NULL;
  831. }
  832. /*
  833. * given a controller button name and a joystick name update our mapping structure with it
  834. */
  835. static void SDL_PrivateGameControllerParseElement(SDL_GameController *gamecontroller, const char *szGameButton, const char *szJoystickButton)
  836. {
  837. SDL_ExtendedGameControllerBind bind;
  838. SDL_GameControllerButton button;
  839. SDL_GameControllerAxis axis;
  840. SDL_bool invert_input = SDL_FALSE;
  841. char half_axis_input = 0;
  842. char half_axis_output = 0;
  843. SDL_AssertJoysticksLocked();
  844. if (*szGameButton == '+' || *szGameButton == '-') {
  845. half_axis_output = *szGameButton++;
  846. }
  847. axis = SDL_GameControllerGetAxisFromString(szGameButton);
  848. button = SDL_GameControllerGetButtonFromString(szGameButton);
  849. if (axis != SDL_CONTROLLER_AXIS_INVALID) {
  850. bind.outputType = SDL_CONTROLLER_BINDTYPE_AXIS;
  851. bind.output.axis.axis = axis;
  852. if (axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) {
  853. bind.output.axis.axis_min = 0;
  854. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  855. } else {
  856. if (half_axis_output == '+') {
  857. bind.output.axis.axis_min = 0;
  858. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  859. } else if (half_axis_output == '-') {
  860. bind.output.axis.axis_min = 0;
  861. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
  862. } else {
  863. bind.output.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
  864. bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  865. }
  866. }
  867. } else if (button != SDL_CONTROLLER_BUTTON_INVALID) {
  868. bind.outputType = SDL_CONTROLLER_BINDTYPE_BUTTON;
  869. bind.output.button = button;
  870. } else {
  871. SDL_SetError("Unexpected controller element %s", szGameButton);
  872. return;
  873. }
  874. if (*szJoystickButton == '+' || *szJoystickButton == '-') {
  875. half_axis_input = *szJoystickButton++;
  876. }
  877. if (szJoystickButton[SDL_strlen(szJoystickButton) - 1] == '~') {
  878. invert_input = SDL_TRUE;
  879. }
  880. if (szJoystickButton[0] == 'a' && SDL_isdigit((unsigned char)szJoystickButton[1])) {
  881. bind.inputType = SDL_CONTROLLER_BINDTYPE_AXIS;
  882. bind.input.axis.axis = SDL_atoi(&szJoystickButton[1]);
  883. if (half_axis_input == '+') {
  884. bind.input.axis.axis_min = 0;
  885. bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  886. } else if (half_axis_input == '-') {
  887. bind.input.axis.axis_min = 0;
  888. bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
  889. } else {
  890. bind.input.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
  891. bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
  892. }
  893. if (invert_input) {
  894. int tmp = bind.input.axis.axis_min;
  895. bind.input.axis.axis_min = bind.input.axis.axis_max;
  896. bind.input.axis.axis_max = tmp;
  897. }
  898. } else if (szJoystickButton[0] == 'b' && SDL_isdigit((unsigned char)szJoystickButton[1])) {
  899. bind.inputType = SDL_CONTROLLER_BINDTYPE_BUTTON;
  900. bind.input.button = SDL_atoi(&szJoystickButton[1]);
  901. } else if (szJoystickButton[0] == 'h' && SDL_isdigit((unsigned char)szJoystickButton[1]) &&
  902. szJoystickButton[2] == '.' && SDL_isdigit((unsigned char)szJoystickButton[3])) {
  903. int hat = SDL_atoi(&szJoystickButton[1]);
  904. int mask = SDL_atoi(&szJoystickButton[3]);
  905. bind.inputType = SDL_CONTROLLER_BINDTYPE_HAT;
  906. bind.input.hat.hat = hat;
  907. bind.input.hat.hat_mask = mask;
  908. } else {
  909. SDL_SetError("Unexpected joystick element: %s", szJoystickButton);
  910. return;
  911. }
  912. ++gamecontroller->num_bindings;
  913. gamecontroller->bindings = (SDL_ExtendedGameControllerBind *)SDL_realloc(gamecontroller->bindings, gamecontroller->num_bindings * sizeof(*gamecontroller->bindings));
  914. if (!gamecontroller->bindings) {
  915. gamecontroller->num_bindings = 0;
  916. SDL_OutOfMemory();
  917. return;
  918. }
  919. gamecontroller->bindings[gamecontroller->num_bindings - 1] = bind;
  920. }
  921. /*
  922. * given a controller mapping string update our mapping object
  923. */
  924. static void SDL_PrivateGameControllerParseControllerConfigString(SDL_GameController *gamecontroller, const char *pchString)
  925. {
  926. char szGameButton[20];
  927. char szJoystickButton[20];
  928. SDL_bool bGameButton = SDL_TRUE;
  929. int i = 0;
  930. const char *pchPos = pchString;
  931. SDL_zeroa(szGameButton);
  932. SDL_zeroa(szJoystickButton);
  933. while (pchPos && *pchPos) {
  934. if (*pchPos == ':') {
  935. i = 0;
  936. bGameButton = SDL_FALSE;
  937. } else if (*pchPos == ' ') {
  938. } else if (*pchPos == ',') {
  939. i = 0;
  940. bGameButton = SDL_TRUE;
  941. SDL_PrivateGameControllerParseElement(gamecontroller, szGameButton, szJoystickButton);
  942. SDL_zeroa(szGameButton);
  943. SDL_zeroa(szJoystickButton);
  944. } else if (bGameButton) {
  945. if (i >= sizeof(szGameButton)) {
  946. SDL_SetError("Button name too large: %s", szGameButton);
  947. return;
  948. }
  949. szGameButton[i] = *pchPos;
  950. i++;
  951. } else {
  952. if (i >= sizeof(szJoystickButton)) {
  953. SDL_SetError("Joystick button name too large: %s", szJoystickButton);
  954. return;
  955. }
  956. szJoystickButton[i] = *pchPos;
  957. i++;
  958. }
  959. pchPos++;
  960. }
  961. /* No more values if the string was terminated by a comma. Don't report an error. */
  962. if (szGameButton[0] != '\0' || szJoystickButton[0] != '\0') {
  963. SDL_PrivateGameControllerParseElement(gamecontroller, szGameButton, szJoystickButton);
  964. }
  965. }
  966. /*
  967. * Make a new button mapping struct
  968. */
  969. static void SDL_PrivateLoadButtonMapping(SDL_GameController *gamecontroller, ControllerMapping_t *pControllerMapping)
  970. {
  971. int i;
  972. SDL_AssertJoysticksLocked();
  973. gamecontroller->name = pControllerMapping->name;
  974. gamecontroller->num_bindings = 0;
  975. gamecontroller->mapping = pControllerMapping;
  976. if (gamecontroller->joystick->naxes != 0 && gamecontroller->last_match_axis != NULL) {
  977. SDL_memset(gamecontroller->last_match_axis, 0, gamecontroller->joystick->naxes * sizeof(*gamecontroller->last_match_axis));
  978. }
  979. SDL_PrivateGameControllerParseControllerConfigString(gamecontroller, pControllerMapping->mapping);
  980. /* Set the zero point for triggers */
  981. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  982. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  983. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS &&
  984. binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS &&
  985. (binding->output.axis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT ||
  986. binding->output.axis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) {
  987. if (binding->input.axis.axis < gamecontroller->joystick->naxes) {
  988. gamecontroller->joystick->axes[binding->input.axis.axis].value =
  989. gamecontroller->joystick->axes[binding->input.axis.axis].zero = (Sint16)binding->input.axis.axis_min;
  990. }
  991. }
  992. }
  993. }
  994. /*
  995. * grab the guid string from a mapping string
  996. */
  997. static char *SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
  998. {
  999. const char *pFirstComma = SDL_strchr(pMapping, ',');
  1000. if (pFirstComma) {
  1001. char *pchGUID = SDL_malloc(pFirstComma - pMapping + 1);
  1002. if (pchGUID == NULL) {
  1003. SDL_OutOfMemory();
  1004. return NULL;
  1005. }
  1006. SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
  1007. pchGUID[pFirstComma - pMapping] = '\0';
  1008. /* Convert old style GUIDs to the new style in 2.0.5 */
  1009. #if defined(__WIN32__) || defined(__WINGDK__)
  1010. if (SDL_strlen(pchGUID) == 32 &&
  1011. SDL_memcmp(&pchGUID[20], "504944564944", 12) == 0) {
  1012. SDL_memcpy(&pchGUID[20], "000000000000", 12);
  1013. SDL_memcpy(&pchGUID[16], &pchGUID[4], 4);
  1014. SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
  1015. SDL_memcpy(&pchGUID[0], "03000000", 8);
  1016. }
  1017. #elif __MACOSX__
  1018. if (SDL_strlen(pchGUID) == 32 &&
  1019. SDL_memcmp(&pchGUID[4], "000000000000", 12) == 0 &&
  1020. SDL_memcmp(&pchGUID[20], "000000000000", 12) == 0) {
  1021. SDL_memcpy(&pchGUID[20], "000000000000", 12);
  1022. SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
  1023. SDL_memcpy(&pchGUID[0], "03000000", 8);
  1024. }
  1025. #endif
  1026. return pchGUID;
  1027. }
  1028. return NULL;
  1029. }
  1030. /*
  1031. * grab the name string from a mapping string
  1032. */
  1033. static char *SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
  1034. {
  1035. const char *pFirstComma, *pSecondComma;
  1036. char *pchName;
  1037. pFirstComma = SDL_strchr(pMapping, ',');
  1038. if (pFirstComma == NULL) {
  1039. return NULL;
  1040. }
  1041. pSecondComma = SDL_strchr(pFirstComma + 1, ',');
  1042. if (pSecondComma == NULL) {
  1043. return NULL;
  1044. }
  1045. pchName = SDL_malloc(pSecondComma - pFirstComma);
  1046. if (pchName == NULL) {
  1047. SDL_OutOfMemory();
  1048. return NULL;
  1049. }
  1050. SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
  1051. pchName[pSecondComma - pFirstComma - 1] = 0;
  1052. return pchName;
  1053. }
  1054. /*
  1055. * grab the button mapping string from a mapping string
  1056. */
  1057. static char *SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
  1058. {
  1059. const char *pFirstComma, *pSecondComma;
  1060. pFirstComma = SDL_strchr(pMapping, ',');
  1061. if (pFirstComma == NULL) {
  1062. return NULL;
  1063. }
  1064. pSecondComma = SDL_strchr(pFirstComma + 1, ',');
  1065. if (pSecondComma == NULL) {
  1066. return NULL;
  1067. }
  1068. return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
  1069. }
  1070. /*
  1071. * Helper function to refresh a mapping
  1072. */
  1073. static void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
  1074. {
  1075. SDL_GameController *controller;
  1076. SDL_AssertJoysticksLocked();
  1077. for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
  1078. if (controller->mapping == pControllerMapping) {
  1079. SDL_PrivateLoadButtonMapping(controller, pControllerMapping);
  1080. {
  1081. SDL_Event event;
  1082. event.type = SDL_CONTROLLERDEVICEREMAPPED;
  1083. event.cdevice.which = controller->joystick->instance_id;
  1084. SDL_PushEvent(&event);
  1085. }
  1086. }
  1087. }
  1088. }
  1089. /*
  1090. * Helper function to add a mapping for a guid
  1091. */
  1092. static ControllerMapping_t *SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing, SDL_ControllerMappingPriority priority)
  1093. {
  1094. char *pchName;
  1095. char *pchMapping;
  1096. ControllerMapping_t *pControllerMapping;
  1097. Uint16 crc;
  1098. SDL_AssertJoysticksLocked();
  1099. pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
  1100. if (pchName == NULL) {
  1101. SDL_SetError("Couldn't parse name from %s", mappingString);
  1102. return NULL;
  1103. }
  1104. pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
  1105. if (pchMapping == NULL) {
  1106. SDL_free(pchName);
  1107. SDL_SetError("Couldn't parse %s", mappingString);
  1108. return NULL;
  1109. }
  1110. /* Fix up the GUID and the mapping with the CRC, if needed */
  1111. SDL_GetJoystickGUIDInfo(jGUID, NULL, NULL, NULL, &crc);
  1112. if (crc) {
  1113. /* Make sure the mapping has the CRC */
  1114. char *new_mapping;
  1115. char *crc_end = "";
  1116. char *crc_string = SDL_strstr(pchMapping, SDL_CONTROLLER_CRC_FIELD);
  1117. if (crc_string) {
  1118. crc_end = SDL_strchr(crc_string, ',');
  1119. if (crc_end) {
  1120. ++crc_end;
  1121. } else {
  1122. crc_end = "";
  1123. }
  1124. *crc_string = '\0';
  1125. }
  1126. if (SDL_asprintf(&new_mapping, "%s%s%.4x,%s", pchMapping, SDL_CONTROLLER_CRC_FIELD, crc, crc_end) >= 0) {
  1127. SDL_free(pchMapping);
  1128. pchMapping = new_mapping;
  1129. }
  1130. } else {
  1131. /* Make sure the GUID has the CRC, for matching purposes */
  1132. char *crc_string = SDL_strstr(pchMapping, SDL_CONTROLLER_CRC_FIELD);
  1133. if (crc_string) {
  1134. crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16);
  1135. if (crc) {
  1136. SDL_SetJoystickGUIDCRC(&jGUID, crc);
  1137. }
  1138. }
  1139. }
  1140. pControllerMapping = SDL_PrivateGetControllerMappingForGUID(jGUID, SDL_TRUE);
  1141. if (pControllerMapping) {
  1142. /* Only overwrite the mapping if the priority is the same or higher. */
  1143. if (pControllerMapping->priority <= priority) {
  1144. /* Update existing mapping */
  1145. SDL_free(pControllerMapping->name);
  1146. pControllerMapping->name = pchName;
  1147. SDL_free(pControllerMapping->mapping);
  1148. pControllerMapping->mapping = pchMapping;
  1149. pControllerMapping->priority = priority;
  1150. /* refresh open controllers */
  1151. SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
  1152. } else {
  1153. SDL_free(pchName);
  1154. SDL_free(pchMapping);
  1155. }
  1156. *existing = SDL_TRUE;
  1157. } else {
  1158. pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
  1159. if (pControllerMapping == NULL) {
  1160. SDL_free(pchName);
  1161. SDL_free(pchMapping);
  1162. SDL_OutOfMemory();
  1163. return NULL;
  1164. }
  1165. /* Clear the CRC, we've already added it to the mapping */
  1166. if (crc) {
  1167. SDL_SetJoystickGUIDCRC(&jGUID, 0);
  1168. }
  1169. pControllerMapping->guid = jGUID;
  1170. pControllerMapping->name = pchName;
  1171. pControllerMapping->mapping = pchMapping;
  1172. pControllerMapping->next = NULL;
  1173. pControllerMapping->priority = priority;
  1174. if (s_pSupportedControllers) {
  1175. /* Add the mapping to the end of the list */
  1176. ControllerMapping_t *pCurrMapping, *pPrevMapping;
  1177. for (pPrevMapping = s_pSupportedControllers, pCurrMapping = pPrevMapping->next;
  1178. pCurrMapping;
  1179. pPrevMapping = pCurrMapping, pCurrMapping = pCurrMapping->next) {
  1180. /* continue; */
  1181. }
  1182. pPrevMapping->next = pControllerMapping;
  1183. } else {
  1184. s_pSupportedControllers = pControllerMapping;
  1185. }
  1186. *existing = SDL_FALSE;
  1187. }
  1188. return pControllerMapping;
  1189. }
  1190. /*
  1191. * Helper function to determine pre-calculated offset to certain joystick mappings
  1192. */
  1193. static ControllerMapping_t *SDL_PrivateGetControllerMappingForNameAndGUID(const char *name, SDL_JoystickGUID guid)
  1194. {
  1195. ControllerMapping_t *mapping;
  1196. SDL_AssertJoysticksLocked();
  1197. mapping = SDL_PrivateGetControllerMappingForGUID(guid, SDL_FALSE);
  1198. #ifdef __LINUX__
  1199. if (mapping == NULL && name) {
  1200. if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
  1201. /* The Linux driver xpad.c maps the wireless dpad to buttons */
  1202. SDL_bool existing;
  1203. mapping = SDL_PrivateAddMappingForGUID(guid,
  1204. "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,",
  1205. &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
  1206. }
  1207. }
  1208. #endif /* __LINUX__ */
  1209. if (mapping == NULL) {
  1210. mapping = s_pDefaultMapping;
  1211. }
  1212. return mapping;
  1213. }
  1214. static void SDL_PrivateAppendToMappingString(char *mapping_string,
  1215. size_t mapping_string_len,
  1216. const char *input_name,
  1217. SDL_InputMapping *mapping)
  1218. {
  1219. char buffer[16];
  1220. if (mapping->kind == EMappingKind_None) {
  1221. return;
  1222. }
  1223. SDL_strlcat(mapping_string, input_name, mapping_string_len);
  1224. SDL_strlcat(mapping_string, ":", mapping_string_len);
  1225. switch (mapping->kind) {
  1226. case EMappingKind_Button:
  1227. (void)SDL_snprintf(buffer, sizeof(buffer), "b%i", mapping->target);
  1228. break;
  1229. case EMappingKind_Axis:
  1230. (void)SDL_snprintf(buffer, sizeof(buffer), "a%i", mapping->target);
  1231. break;
  1232. case EMappingKind_Hat:
  1233. (void)SDL_snprintf(buffer, sizeof(buffer), "h%i.%i", mapping->target >> 4, mapping->target & 0x0F);
  1234. break;
  1235. default:
  1236. SDL_assert(SDL_FALSE);
  1237. }
  1238. SDL_strlcat(mapping_string, buffer, mapping_string_len);
  1239. SDL_strlcat(mapping_string, ",", mapping_string_len);
  1240. }
  1241. static ControllerMapping_t *SDL_PrivateGenerateAutomaticControllerMapping(const char *name,
  1242. SDL_JoystickGUID guid,
  1243. SDL_GamepadMapping *raw_map)
  1244. {
  1245. SDL_bool existing;
  1246. char name_string[128];
  1247. char mapping[1024];
  1248. /* Remove any commas in the name */
  1249. SDL_strlcpy(name_string, name, sizeof(name_string));
  1250. {
  1251. char *spot;
  1252. for (spot = name_string; *spot; ++spot) {
  1253. if (*spot == ',') {
  1254. *spot = ' ';
  1255. }
  1256. }
  1257. }
  1258. (void)SDL_snprintf(mapping, sizeof(mapping), "none,%s,", name_string);
  1259. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "a", &raw_map->a);
  1260. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "b", &raw_map->b);
  1261. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "x", &raw_map->x);
  1262. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "y", &raw_map->y);
  1263. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "back", &raw_map->back);
  1264. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "guide", &raw_map->guide);
  1265. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "start", &raw_map->start);
  1266. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftstick", &raw_map->leftstick);
  1267. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightstick", &raw_map->rightstick);
  1268. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftshoulder", &raw_map->leftshoulder);
  1269. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightshoulder", &raw_map->rightshoulder);
  1270. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpup", &raw_map->dpup);
  1271. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpdown", &raw_map->dpdown);
  1272. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpleft", &raw_map->dpleft);
  1273. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "dpright", &raw_map->dpright);
  1274. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc1", &raw_map->misc1);
  1275. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle1", &raw_map->paddle1);
  1276. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle2", &raw_map->paddle2);
  1277. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle3", &raw_map->paddle3);
  1278. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle4", &raw_map->paddle4);
  1279. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "leftx", &raw_map->leftx);
  1280. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefty", &raw_map->lefty);
  1281. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "rightx", &raw_map->rightx);
  1282. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righty", &raw_map->righty);
  1283. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "lefttrigger", &raw_map->lefttrigger);
  1284. SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "righttrigger", &raw_map->righttrigger);
  1285. return SDL_PrivateAddMappingForGUID(guid, mapping, &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
  1286. }
  1287. static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
  1288. {
  1289. const char *name;
  1290. SDL_JoystickGUID guid;
  1291. ControllerMapping_t *mapping;
  1292. SDL_AssertJoysticksLocked();
  1293. if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
  1294. SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
  1295. return NULL;
  1296. }
  1297. name = SDL_JoystickNameForIndex(device_index);
  1298. guid = SDL_JoystickGetDeviceGUID(device_index);
  1299. mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
  1300. if (mapping == NULL) {
  1301. SDL_GamepadMapping raw_map;
  1302. SDL_zero(raw_map);
  1303. if (SDL_PrivateJoystickGetAutoGamepadMapping(device_index, &raw_map)) {
  1304. mapping = SDL_PrivateGenerateAutomaticControllerMapping(name, guid, &raw_map);
  1305. }
  1306. }
  1307. return mapping;
  1308. }
  1309. /*
  1310. * Add or update an entry into the Mappings Database
  1311. */
  1312. int SDL_GameControllerAddMappingsFromRW(SDL_RWops *rw, int freerw)
  1313. {
  1314. const char *platform = SDL_GetPlatform();
  1315. int controllers = 0;
  1316. char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
  1317. size_t db_size, platform_len;
  1318. if (rw == NULL) {
  1319. return SDL_SetError("Invalid RWops");
  1320. }
  1321. db_size = (size_t)SDL_RWsize(rw);
  1322. buf = (char *)SDL_malloc(db_size + 1);
  1323. if (buf == NULL) {
  1324. if (freerw) {
  1325. SDL_RWclose(rw);
  1326. }
  1327. return SDL_SetError("Could not allocate space to read DB into memory");
  1328. }
  1329. if (SDL_RWread(rw, buf, db_size, 1) != 1) {
  1330. if (freerw) {
  1331. SDL_RWclose(rw);
  1332. }
  1333. SDL_free(buf);
  1334. return SDL_SetError("Could not read DB");
  1335. }
  1336. if (freerw) {
  1337. SDL_RWclose(rw);
  1338. }
  1339. buf[db_size] = '\0';
  1340. line = buf;
  1341. while (line < buf + db_size) {
  1342. line_end = SDL_strchr(line, '\n');
  1343. if (line_end != NULL) {
  1344. *line_end = '\0';
  1345. } else {
  1346. line_end = buf + db_size;
  1347. }
  1348. /* Extract and verify the platform */
  1349. tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
  1350. if (tmp != NULL) {
  1351. tmp += SDL_CONTROLLER_PLATFORM_FIELD_SIZE;
  1352. comma = SDL_strchr(tmp, ',');
  1353. if (comma != NULL) {
  1354. platform_len = comma - tmp + 1;
  1355. if (platform_len + 1 < SDL_arraysize(line_platform)) {
  1356. SDL_strlcpy(line_platform, tmp, platform_len);
  1357. if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
  1358. SDL_GameControllerAddMapping(line) > 0) {
  1359. controllers++;
  1360. }
  1361. }
  1362. }
  1363. }
  1364. line = line_end + 1;
  1365. }
  1366. SDL_free(buf);
  1367. return controllers;
  1368. }
  1369. /*
  1370. * Add or update an entry into the Mappings Database with a priority
  1371. */
  1372. static int SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_ControllerMappingPriority priority)
  1373. {
  1374. char *pchGUID;
  1375. SDL_JoystickGUID jGUID;
  1376. SDL_bool is_default_mapping = SDL_FALSE;
  1377. SDL_bool is_xinput_mapping = SDL_FALSE;
  1378. SDL_bool existing = SDL_FALSE;
  1379. ControllerMapping_t *pControllerMapping;
  1380. SDL_AssertJoysticksLocked();
  1381. if (mappingString == NULL) {
  1382. return SDL_InvalidParamError("mappingString");
  1383. }
  1384. { /* Extract and verify the hint field */
  1385. const char *tmp;
  1386. tmp = SDL_strstr(mappingString, SDL_CONTROLLER_HINT_FIELD);
  1387. if (tmp != NULL) {
  1388. SDL_bool default_value, value, negate;
  1389. int len;
  1390. char hint[128];
  1391. tmp += SDL_CONTROLLER_HINT_FIELD_SIZE;
  1392. if (*tmp == '!') {
  1393. negate = SDL_TRUE;
  1394. ++tmp;
  1395. } else {
  1396. negate = SDL_FALSE;
  1397. }
  1398. len = 0;
  1399. while (*tmp && *tmp != ',' && *tmp != ':' && len < (sizeof(hint) - 1)) {
  1400. hint[len++] = *tmp++;
  1401. }
  1402. hint[len] = '\0';
  1403. if (tmp[0] == ':' && tmp[1] == '=') {
  1404. tmp += 2;
  1405. default_value = SDL_atoi(tmp);
  1406. } else {
  1407. default_value = SDL_FALSE;
  1408. }
  1409. value = SDL_GetHintBoolean(hint, default_value);
  1410. if (negate) {
  1411. value = !value;
  1412. }
  1413. if (!value) {
  1414. return 0;
  1415. }
  1416. }
  1417. }
  1418. #ifdef ANDROID
  1419. { /* Extract and verify the SDK version */
  1420. const char *tmp;
  1421. tmp = SDL_strstr(mappingString, SDL_CONTROLLER_SDKGE_FIELD);
  1422. if (tmp != NULL) {
  1423. tmp += SDL_CONTROLLER_SDKGE_FIELD_SIZE;
  1424. if (!(SDL_GetAndroidSDKVersion() >= SDL_atoi(tmp))) {
  1425. return SDL_SetError("SDK version %d < minimum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
  1426. }
  1427. }
  1428. tmp = SDL_strstr(mappingString, SDL_CONTROLLER_SDKLE_FIELD);
  1429. if (tmp != NULL) {
  1430. tmp += SDL_CONTROLLER_SDKLE_FIELD_SIZE;
  1431. if (!(SDL_GetAndroidSDKVersion() <= SDL_atoi(tmp))) {
  1432. return SDL_SetError("SDK version %d > maximum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
  1433. }
  1434. }
  1435. }
  1436. #endif
  1437. pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
  1438. if (pchGUID == NULL) {
  1439. return SDL_SetError("Couldn't parse GUID from %s", mappingString);
  1440. }
  1441. if (!SDL_strcasecmp(pchGUID, "default")) {
  1442. is_default_mapping = SDL_TRUE;
  1443. } else if (!SDL_strcasecmp(pchGUID, "xinput")) {
  1444. is_xinput_mapping = SDL_TRUE;
  1445. }
  1446. jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
  1447. SDL_free(pchGUID);
  1448. pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing, priority);
  1449. if (pControllerMapping == NULL) {
  1450. return -1;
  1451. }
  1452. if (existing) {
  1453. return 0;
  1454. } else {
  1455. if (is_default_mapping) {
  1456. s_pDefaultMapping = pControllerMapping;
  1457. } else if (is_xinput_mapping) {
  1458. s_pXInputMapping = pControllerMapping;
  1459. }
  1460. return 1;
  1461. }
  1462. }
  1463. /*
  1464. * Add or update an entry into the Mappings Database
  1465. */
  1466. int SDL_GameControllerAddMapping(const char *mappingString)
  1467. {
  1468. int retval;
  1469. SDL_LockJoysticks();
  1470. {
  1471. retval = SDL_PrivateGameControllerAddMapping(mappingString, SDL_CONTROLLER_MAPPING_PRIORITY_API);
  1472. }
  1473. SDL_UnlockJoysticks();
  1474. return retval;
  1475. }
  1476. /*
  1477. * Get the number of mappings installed
  1478. */
  1479. int SDL_GameControllerNumMappings(void)
  1480. {
  1481. int num_mappings = 0;
  1482. SDL_LockJoysticks();
  1483. {
  1484. ControllerMapping_t *mapping;
  1485. for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
  1486. if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
  1487. continue;
  1488. }
  1489. ++num_mappings;
  1490. }
  1491. }
  1492. SDL_UnlockJoysticks();
  1493. return num_mappings;
  1494. }
  1495. /*
  1496. * Create a mapping string for a mapping
  1497. */
  1498. static char *CreateMappingString(ControllerMapping_t *mapping, SDL_JoystickGUID guid)
  1499. {
  1500. char *pMappingString, *pPlatformString;
  1501. char pchGUID[33];
  1502. size_t needed;
  1503. const char *platform = SDL_GetPlatform();
  1504. SDL_AssertJoysticksLocked();
  1505. SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
  1506. /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
  1507. needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
  1508. if (!SDL_strstr(mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD)) {
  1509. /* add memory for ',' + platform:PLATFORM */
  1510. if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
  1511. needed += 1;
  1512. }
  1513. needed += SDL_CONTROLLER_PLATFORM_FIELD_SIZE + SDL_strlen(platform);
  1514. }
  1515. pMappingString = SDL_malloc(needed);
  1516. if (pMappingString == NULL) {
  1517. SDL_OutOfMemory();
  1518. return NULL;
  1519. }
  1520. (void)SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
  1521. if (!SDL_strstr(mapping->mapping, SDL_CONTROLLER_PLATFORM_FIELD)) {
  1522. if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
  1523. SDL_strlcat(pMappingString, ",", needed);
  1524. }
  1525. SDL_strlcat(pMappingString, SDL_CONTROLLER_PLATFORM_FIELD, needed);
  1526. SDL_strlcat(pMappingString, platform, needed);
  1527. }
  1528. /* Make sure multiple platform strings haven't made their way into the mapping */
  1529. pPlatformString = SDL_strstr(pMappingString, SDL_CONTROLLER_PLATFORM_FIELD);
  1530. if (pPlatformString) {
  1531. pPlatformString = SDL_strstr(pPlatformString + 1, SDL_CONTROLLER_PLATFORM_FIELD);
  1532. if (pPlatformString) {
  1533. *pPlatformString = '\0';
  1534. }
  1535. }
  1536. return pMappingString;
  1537. }
  1538. /*
  1539. * Get the mapping at a particular index.
  1540. */
  1541. char *SDL_GameControllerMappingForIndex(int mapping_index)
  1542. {
  1543. char *retval = NULL;
  1544. SDL_LockJoysticks();
  1545. {
  1546. ControllerMapping_t *mapping;
  1547. for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
  1548. if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
  1549. continue;
  1550. }
  1551. if (mapping_index == 0) {
  1552. retval = CreateMappingString(mapping, mapping->guid);
  1553. break;
  1554. }
  1555. --mapping_index;
  1556. }
  1557. }
  1558. SDL_UnlockJoysticks();
  1559. if (retval == NULL) {
  1560. SDL_SetError("Mapping not available");
  1561. }
  1562. return retval;
  1563. }
  1564. /*
  1565. * Get the mapping string for this GUID
  1566. */
  1567. char *SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
  1568. {
  1569. char *retval;
  1570. SDL_LockJoysticks();
  1571. {
  1572. ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid, SDL_FALSE);
  1573. if (mapping) {
  1574. retval = CreateMappingString(mapping, guid);
  1575. } else {
  1576. SDL_SetError("Mapping not available");
  1577. retval = NULL;
  1578. }
  1579. }
  1580. SDL_UnlockJoysticks();
  1581. return retval;
  1582. }
  1583. /*
  1584. * Get the mapping string for this device
  1585. */
  1586. char *SDL_GameControllerMapping(SDL_GameController *gamecontroller)
  1587. {
  1588. char *retval;
  1589. SDL_LockJoysticks();
  1590. {
  1591. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
  1592. retval = CreateMappingString(gamecontroller->mapping, gamecontroller->joystick->guid);
  1593. }
  1594. SDL_UnlockJoysticks();
  1595. return retval;
  1596. }
  1597. static void SDL_GameControllerLoadHints()
  1598. {
  1599. const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
  1600. if (hint && hint[0]) {
  1601. size_t nchHints = SDL_strlen(hint);
  1602. char *pUserMappings = SDL_malloc(nchHints + 1);
  1603. char *pTempMappings = pUserMappings;
  1604. SDL_memcpy(pUserMappings, hint, nchHints);
  1605. pUserMappings[nchHints] = '\0';
  1606. while (pUserMappings) {
  1607. char *pchNewLine = NULL;
  1608. pchNewLine = SDL_strchr(pUserMappings, '\n');
  1609. if (pchNewLine) {
  1610. *pchNewLine = '\0';
  1611. }
  1612. SDL_PrivateGameControllerAddMapping(pUserMappings, SDL_CONTROLLER_MAPPING_PRIORITY_USER);
  1613. if (pchNewLine) {
  1614. pUserMappings = pchNewLine + 1;
  1615. } else {
  1616. pUserMappings = NULL;
  1617. }
  1618. }
  1619. SDL_free(pTempMappings);
  1620. }
  1621. }
  1622. /*
  1623. * Fill the given buffer with the expected controller mapping filepath.
  1624. * Usually this will just be SDL_HINT_GAMECONTROLLERCONFIG_FILE, but for
  1625. * Android, we want to get the internal storage path.
  1626. */
  1627. static SDL_bool SDL_GetControllerMappingFilePath(char *path, size_t size)
  1628. {
  1629. const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG_FILE);
  1630. if (hint && *hint) {
  1631. return SDL_strlcpy(path, hint, size) < size;
  1632. }
  1633. #if defined(__ANDROID__)
  1634. return SDL_snprintf(path, size, "%s/controller_map.txt", SDL_AndroidGetInternalStoragePath()) < size;
  1635. #else
  1636. return SDL_FALSE;
  1637. #endif
  1638. }
  1639. /*
  1640. * Initialize the game controller system, mostly load our DB of controller config mappings
  1641. */
  1642. int SDL_GameControllerInitMappings(void)
  1643. {
  1644. char szControllerMapPath[1024];
  1645. int i = 0;
  1646. const char *pMappingString = NULL;
  1647. SDL_AssertJoysticksLocked();
  1648. pMappingString = s_ControllerMappings[i];
  1649. while (pMappingString) {
  1650. SDL_PrivateGameControllerAddMapping(pMappingString, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
  1651. i++;
  1652. pMappingString = s_ControllerMappings[i];
  1653. }
  1654. if (SDL_GetControllerMappingFilePath(szControllerMapPath, sizeof(szControllerMapPath))) {
  1655. SDL_GameControllerAddMappingsFromFile(szControllerMapPath);
  1656. }
  1657. /* load in any user supplied config */
  1658. SDL_GameControllerLoadHints();
  1659. SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES,
  1660. SDL_GameControllerIgnoreDevicesChanged, NULL);
  1661. SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT,
  1662. SDL_GameControllerIgnoreDevicesExceptChanged, NULL);
  1663. return 0;
  1664. }
  1665. int SDL_GameControllerInit(void)
  1666. {
  1667. int i;
  1668. /* watch for joy events and fire controller ones if needed */
  1669. SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
  1670. /* Send added events for controllers currently attached */
  1671. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  1672. if (SDL_IsGameController(i)) {
  1673. SDL_Event deviceevent;
  1674. deviceevent.type = SDL_CONTROLLERDEVICEADDED;
  1675. deviceevent.cdevice.which = i;
  1676. SDL_PushEvent(&deviceevent);
  1677. }
  1678. }
  1679. return 0;
  1680. }
  1681. /*
  1682. * Get the implementation dependent name of a controller
  1683. */
  1684. const char *SDL_GameControllerNameForIndex(int joystick_index)
  1685. {
  1686. const char *retval = NULL;
  1687. SDL_LockJoysticks();
  1688. {
  1689. ControllerMapping_t *mapping = SDL_PrivateGetControllerMapping(joystick_index);
  1690. if (mapping != NULL) {
  1691. if (SDL_strcmp(mapping->name, "*") == 0) {
  1692. retval = SDL_JoystickNameForIndex(joystick_index);
  1693. } else {
  1694. retval = mapping->name;
  1695. }
  1696. }
  1697. }
  1698. SDL_UnlockJoysticks();
  1699. return retval;
  1700. }
  1701. /*
  1702. * Get the implementation dependent path of a controller
  1703. */
  1704. const char *SDL_GameControllerPathForIndex(int joystick_index)
  1705. {
  1706. const char *retval = NULL;
  1707. SDL_LockJoysticks();
  1708. {
  1709. ControllerMapping_t *mapping = SDL_PrivateGetControllerMapping(joystick_index);
  1710. if (mapping != NULL) {
  1711. retval = SDL_JoystickPathForIndex(joystick_index);
  1712. }
  1713. }
  1714. SDL_UnlockJoysticks();
  1715. return retval;
  1716. }
  1717. /**
  1718. * Get the type of a game controller.
  1719. */
  1720. SDL_GameControllerType SDL_GameControllerTypeForIndex(int joystick_index)
  1721. {
  1722. return SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGetDeviceGUID(joystick_index), SDL_JoystickNameForIndex(joystick_index));
  1723. }
  1724. /**
  1725. * Get the mapping of a game controller.
  1726. * This can be called before any controllers are opened.
  1727. * If no mapping can be found, this function returns NULL.
  1728. */
  1729. char *SDL_GameControllerMappingForDeviceIndex(int joystick_index)
  1730. {
  1731. char *retval = NULL;
  1732. SDL_LockJoysticks();
  1733. {
  1734. ControllerMapping_t *mapping = SDL_PrivateGetControllerMapping(joystick_index);
  1735. if (mapping != NULL) {
  1736. SDL_JoystickGUID guid;
  1737. char pchGUID[33];
  1738. size_t needed;
  1739. guid = SDL_JoystickGetDeviceGUID(joystick_index);
  1740. SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
  1741. /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
  1742. needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
  1743. retval = (char *)SDL_malloc(needed);
  1744. if (retval != NULL) {
  1745. (void)SDL_snprintf(retval, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
  1746. } else {
  1747. SDL_OutOfMemory();
  1748. }
  1749. }
  1750. }
  1751. SDL_UnlockJoysticks();
  1752. return retval;
  1753. }
  1754. /*
  1755. * Return 1 if the joystick with this name and GUID is a supported controller
  1756. */
  1757. SDL_bool SDL_IsGameControllerNameAndGUID(const char *name, SDL_JoystickGUID guid)
  1758. {
  1759. SDL_bool retval;
  1760. SDL_LockJoysticks();
  1761. {
  1762. if (SDL_PrivateGetControllerMappingForNameAndGUID(name, guid) != NULL) {
  1763. retval = SDL_TRUE;
  1764. } else {
  1765. retval = SDL_FALSE;
  1766. }
  1767. }
  1768. SDL_UnlockJoysticks();
  1769. return retval;
  1770. }
  1771. /*
  1772. * Return 1 if the joystick at this device index is a supported controller
  1773. */
  1774. SDL_bool SDL_IsGameController(int joystick_index)
  1775. {
  1776. SDL_bool retval;
  1777. SDL_LockJoysticks();
  1778. {
  1779. if (SDL_PrivateGetControllerMapping(joystick_index) != NULL) {
  1780. retval = SDL_TRUE;
  1781. } else {
  1782. retval = SDL_FALSE;
  1783. }
  1784. }
  1785. SDL_UnlockJoysticks();
  1786. return retval;
  1787. }
  1788. #if defined(__LINUX__)
  1789. static SDL_bool SDL_endswith(const char *string, const char *suffix)
  1790. {
  1791. size_t string_length = string ? SDL_strlen(string) : 0;
  1792. size_t suffix_length = suffix ? SDL_strlen(suffix) : 0;
  1793. if (suffix_length > 0 && suffix_length <= string_length) {
  1794. if (SDL_memcmp(string + string_length - suffix_length, suffix, suffix_length) == 0) {
  1795. return SDL_TRUE;
  1796. }
  1797. }
  1798. return SDL_FALSE;
  1799. }
  1800. #endif
  1801. /*
  1802. * Return 1 if the game controller should be ignored by SDL
  1803. */
  1804. SDL_bool SDL_ShouldIgnoreGameController(const char *name, SDL_JoystickGUID guid)
  1805. {
  1806. int i;
  1807. Uint16 vendor;
  1808. Uint16 product;
  1809. Uint16 version;
  1810. Uint32 vidpid;
  1811. #if defined(__LINUX__)
  1812. if (SDL_endswith(name, " Motion Sensors")) {
  1813. /* Don't treat the PS3 and PS4 motion controls as a separate game controller */
  1814. return SDL_TRUE;
  1815. }
  1816. if (SDL_strncmp(name, "Nintendo ", 9) == 0 && SDL_strstr(name, " IMU") != NULL) {
  1817. /* Don't treat the Nintendo IMU as a separate game controller */
  1818. return SDL_TRUE;
  1819. }
  1820. if (SDL_endswith(name, " Accelerometer") ||
  1821. SDL_endswith(name, " IR") ||
  1822. SDL_endswith(name, " Motion Plus") ||
  1823. SDL_endswith(name, " Nunchuk")) {
  1824. /* Don't treat the Wii extension controls as a separate game controller */
  1825. return SDL_TRUE;
  1826. }
  1827. #endif
  1828. if (name && SDL_strcmp(name, "uinput-fpc") == 0) {
  1829. /* The Google Pixel fingerprint sensor reports itself as a joystick */
  1830. return SDL_TRUE;
  1831. }
  1832. if (SDL_allowed_controllers.num_entries == 0 &&
  1833. SDL_ignored_controllers.num_entries == 0) {
  1834. return SDL_FALSE;
  1835. }
  1836. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, &version, NULL);
  1837. if (SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", SDL_FALSE)) {
  1838. /* We shouldn't ignore Steam's virtual gamepad since it's using the hints to filter out the real controllers so it can remap input for the virtual controller */
  1839. /* https://partner.steamgames.com/doc/features/steam_controller/steam_input_gamepad_emulation_bestpractices */
  1840. SDL_bool bSteamVirtualGamepad = SDL_FALSE;
  1841. #if defined(__LINUX__)
  1842. bSteamVirtualGamepad = (vendor == USB_VENDOR_VALVE && product == USB_PRODUCT_STEAM_VIRTUAL_GAMEPAD);
  1843. #elif defined(__MACOSX__)
  1844. bSteamVirtualGamepad = (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX360_WIRED_CONTROLLER && version == 1);
  1845. #elif defined(__WIN32__)
  1846. /* We can't tell on Windows, but Steam will block others in input hooks */
  1847. bSteamVirtualGamepad = SDL_TRUE;
  1848. #endif
  1849. if (bSteamVirtualGamepad) {
  1850. return SDL_FALSE;
  1851. }
  1852. }
  1853. vidpid = MAKE_VIDPID(vendor, product);
  1854. if (SDL_allowed_controllers.num_entries > 0) {
  1855. for (i = 0; i < SDL_allowed_controllers.num_entries; ++i) {
  1856. if (vidpid == SDL_allowed_controllers.entries[i]) {
  1857. return SDL_FALSE;
  1858. }
  1859. }
  1860. return SDL_TRUE;
  1861. } else {
  1862. for (i = 0; i < SDL_ignored_controllers.num_entries; ++i) {
  1863. if (vidpid == SDL_ignored_controllers.entries[i]) {
  1864. return SDL_TRUE;
  1865. }
  1866. }
  1867. return SDL_FALSE;
  1868. }
  1869. }
  1870. /*
  1871. * Open a controller for use - the index passed as an argument refers to
  1872. * the N'th controller on the system. This index is the value which will
  1873. * identify this controller in future controller events.
  1874. *
  1875. * This function returns a controller identifier, or NULL if an error occurred.
  1876. */
  1877. SDL_GameController *SDL_GameControllerOpen(int joystick_index)
  1878. {
  1879. SDL_JoystickID instance_id;
  1880. SDL_GameController *gamecontroller;
  1881. SDL_GameController *gamecontrollerlist;
  1882. ControllerMapping_t *pSupportedController = NULL;
  1883. SDL_LockJoysticks();
  1884. gamecontrollerlist = SDL_gamecontrollers;
  1885. /* If the controller is already open, return it */
  1886. instance_id = SDL_JoystickGetDeviceInstanceID(joystick_index);
  1887. while (gamecontrollerlist != NULL) {
  1888. if (instance_id == gamecontrollerlist->joystick->instance_id) {
  1889. gamecontroller = gamecontrollerlist;
  1890. ++gamecontroller->ref_count;
  1891. SDL_UnlockJoysticks();
  1892. return gamecontroller;
  1893. }
  1894. gamecontrollerlist = gamecontrollerlist->next;
  1895. }
  1896. /* Find a controller mapping */
  1897. pSupportedController = SDL_PrivateGetControllerMapping(joystick_index);
  1898. if (pSupportedController == NULL) {
  1899. SDL_SetError("Couldn't find mapping for device (%d)", joystick_index);
  1900. SDL_UnlockJoysticks();
  1901. return NULL;
  1902. }
  1903. /* Create and initialize the controller */
  1904. gamecontroller = (SDL_GameController *)SDL_calloc(1, sizeof(*gamecontroller));
  1905. if (gamecontroller == NULL) {
  1906. SDL_OutOfMemory();
  1907. SDL_UnlockJoysticks();
  1908. return NULL;
  1909. }
  1910. gamecontroller->magic = &gamecontroller_magic;
  1911. gamecontroller->joystick = SDL_JoystickOpen(joystick_index);
  1912. if (gamecontroller->joystick == NULL) {
  1913. SDL_free(gamecontroller);
  1914. SDL_UnlockJoysticks();
  1915. return NULL;
  1916. }
  1917. if (gamecontroller->joystick->naxes) {
  1918. gamecontroller->last_match_axis = (SDL_ExtendedGameControllerBind **)SDL_calloc(gamecontroller->joystick->naxes, sizeof(*gamecontroller->last_match_axis));
  1919. if (!gamecontroller->last_match_axis) {
  1920. SDL_OutOfMemory();
  1921. SDL_JoystickClose(gamecontroller->joystick);
  1922. SDL_free(gamecontroller);
  1923. SDL_UnlockJoysticks();
  1924. return NULL;
  1925. }
  1926. }
  1927. if (gamecontroller->joystick->nhats) {
  1928. gamecontroller->last_hat_mask = (Uint8 *)SDL_calloc(gamecontroller->joystick->nhats, sizeof(*gamecontroller->last_hat_mask));
  1929. if (!gamecontroller->last_hat_mask) {
  1930. SDL_OutOfMemory();
  1931. SDL_JoystickClose(gamecontroller->joystick);
  1932. SDL_free(gamecontroller->last_match_axis);
  1933. SDL_free(gamecontroller);
  1934. SDL_UnlockJoysticks();
  1935. return NULL;
  1936. }
  1937. }
  1938. SDL_PrivateLoadButtonMapping(gamecontroller, pSupportedController);
  1939. /* Add the controller to list */
  1940. ++gamecontroller->ref_count;
  1941. /* Link the controller in the list */
  1942. gamecontroller->next = SDL_gamecontrollers;
  1943. SDL_gamecontrollers = gamecontroller;
  1944. SDL_UnlockJoysticks();
  1945. return gamecontroller;
  1946. }
  1947. /*
  1948. * Manually pump for controller updates.
  1949. */
  1950. void SDL_GameControllerUpdate(void)
  1951. {
  1952. /* Just for API completeness; the joystick API does all the work. */
  1953. SDL_JoystickUpdate();
  1954. }
  1955. /**
  1956. * Return whether a game controller has a given axis
  1957. */
  1958. SDL_bool SDL_GameControllerHasAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
  1959. {
  1960. SDL_GameControllerButtonBind bind;
  1961. SDL_LockJoysticks();
  1962. {
  1963. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, SDL_FALSE);
  1964. bind = SDL_GameControllerGetBindForAxis(gamecontroller, axis);
  1965. }
  1966. SDL_UnlockJoysticks();
  1967. return (bind.bindType != SDL_CONTROLLER_BINDTYPE_NONE) ? SDL_TRUE : SDL_FALSE;
  1968. }
  1969. /*
  1970. * Get the current state of an axis control on a controller
  1971. */
  1972. Sint16 SDL_GameControllerGetAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
  1973. {
  1974. Sint16 retval = 0;
  1975. SDL_LockJoysticks();
  1976. {
  1977. int i;
  1978. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
  1979. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  1980. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  1981. if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
  1982. int value = 0;
  1983. SDL_bool valid_input_range;
  1984. SDL_bool valid_output_range;
  1985. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  1986. value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
  1987. if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
  1988. valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
  1989. } else {
  1990. valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
  1991. }
  1992. if (valid_input_range) {
  1993. if (binding->input.axis.axis_min != binding->output.axis.axis_min || binding->input.axis.axis_max != binding->output.axis.axis_max) {
  1994. float normalized_value = (float)(value - binding->input.axis.axis_min) / (binding->input.axis.axis_max - binding->input.axis.axis_min);
  1995. value = binding->output.axis.axis_min + (int)(normalized_value * (binding->output.axis.axis_max - binding->output.axis.axis_min));
  1996. }
  1997. } else {
  1998. value = 0;
  1999. }
  2000. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
  2001. value = SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
  2002. if (value == SDL_PRESSED) {
  2003. value = binding->output.axis.axis_max;
  2004. }
  2005. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
  2006. int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
  2007. if (hat_mask & binding->input.hat.hat_mask) {
  2008. value = binding->output.axis.axis_max;
  2009. }
  2010. }
  2011. if (binding->output.axis.axis_min < binding->output.axis.axis_max) {
  2012. valid_output_range = (value >= binding->output.axis.axis_min && value <= binding->output.axis.axis_max);
  2013. } else {
  2014. valid_output_range = (value >= binding->output.axis.axis_max && value <= binding->output.axis.axis_min);
  2015. }
  2016. /* If the value is zero, there might be another binding that makes it non-zero */
  2017. if (value != 0 && valid_output_range) {
  2018. retval = (Sint16)value;
  2019. break;
  2020. }
  2021. }
  2022. }
  2023. }
  2024. SDL_UnlockJoysticks();
  2025. return retval;
  2026. }
  2027. /**
  2028. * Return whether a game controller has a given button
  2029. */
  2030. SDL_bool SDL_GameControllerHasButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
  2031. {
  2032. SDL_GameControllerButtonBind bind;
  2033. SDL_LockJoysticks();
  2034. {
  2035. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, SDL_FALSE);
  2036. bind = SDL_GameControllerGetBindForButton(gamecontroller, button);
  2037. }
  2038. SDL_UnlockJoysticks();
  2039. return (bind.bindType != SDL_CONTROLLER_BINDTYPE_NONE) ? SDL_TRUE : SDL_FALSE;
  2040. }
  2041. /*
  2042. * Get the current state of a button on a controller
  2043. */
  2044. Uint8 SDL_GameControllerGetButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
  2045. {
  2046. Uint8 retval = SDL_RELEASED;
  2047. SDL_LockJoysticks();
  2048. {
  2049. int i;
  2050. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, 0);
  2051. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  2052. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  2053. if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
  2054. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  2055. SDL_bool valid_input_range;
  2056. int value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
  2057. int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
  2058. if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
  2059. valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
  2060. if (valid_input_range) {
  2061. retval = (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
  2062. break;
  2063. }
  2064. } else {
  2065. valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
  2066. if (valid_input_range) {
  2067. retval = (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
  2068. break;
  2069. }
  2070. }
  2071. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
  2072. retval = SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
  2073. break;
  2074. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
  2075. int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
  2076. retval = (hat_mask & binding->input.hat.hat_mask) ? SDL_PRESSED : SDL_RELEASED;
  2077. break;
  2078. }
  2079. }
  2080. }
  2081. }
  2082. SDL_UnlockJoysticks();
  2083. return retval;
  2084. }
  2085. /**
  2086. * Get the number of touchpads on a game controller.
  2087. */
  2088. int SDL_GameControllerGetNumTouchpads(SDL_GameController *gamecontroller)
  2089. {
  2090. int retval = 0;
  2091. SDL_LockJoysticks();
  2092. {
  2093. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2094. if (joystick) {
  2095. retval = joystick->ntouchpads;
  2096. }
  2097. }
  2098. SDL_UnlockJoysticks();
  2099. return retval;
  2100. }
  2101. /**
  2102. * Get the number of supported simultaneous fingers on a touchpad on a game controller.
  2103. */
  2104. int SDL_GameControllerGetNumTouchpadFingers(SDL_GameController *gamecontroller, int touchpad)
  2105. {
  2106. int retval = 0;
  2107. SDL_LockJoysticks();
  2108. {
  2109. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2110. if (joystick) {
  2111. if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
  2112. retval = joystick->touchpads[touchpad].nfingers;
  2113. } else {
  2114. retval = SDL_InvalidParamError("touchpad");
  2115. }
  2116. }
  2117. }
  2118. SDL_UnlockJoysticks();
  2119. return retval;
  2120. }
  2121. /**
  2122. * Get the current state of a finger on a touchpad on a game controller.
  2123. */
  2124. int SDL_GameControllerGetTouchpadFinger(SDL_GameController *gamecontroller, int touchpad, int finger, Uint8 *state, float *x, float *y, float *pressure)
  2125. {
  2126. int retval = -1;
  2127. SDL_LockJoysticks();
  2128. {
  2129. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2130. if (joystick) {
  2131. if (touchpad >= 0 && touchpad < joystick->ntouchpads) {
  2132. SDL_JoystickTouchpadInfo *touchpad_info = &joystick->touchpads[touchpad];
  2133. if (finger >= 0 && finger < touchpad_info->nfingers) {
  2134. SDL_JoystickTouchpadFingerInfo *info = &touchpad_info->fingers[finger];
  2135. if (state) {
  2136. *state = info->state;
  2137. }
  2138. if (x) {
  2139. *x = info->x;
  2140. }
  2141. if (y) {
  2142. *y = info->y;
  2143. }
  2144. if (pressure) {
  2145. *pressure = info->pressure;
  2146. }
  2147. retval = 0;
  2148. } else {
  2149. retval = SDL_InvalidParamError("finger");
  2150. }
  2151. } else {
  2152. retval = SDL_InvalidParamError("touchpad");
  2153. }
  2154. }
  2155. }
  2156. SDL_UnlockJoysticks();
  2157. return retval;
  2158. }
  2159. /**
  2160. * Return whether a game controller has a particular sensor.
  2161. */
  2162. SDL_bool SDL_GameControllerHasSensor(SDL_GameController *gamecontroller, SDL_SensorType type)
  2163. {
  2164. SDL_bool retval = SDL_FALSE;
  2165. SDL_LockJoysticks();
  2166. {
  2167. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2168. if (joystick) {
  2169. int i;
  2170. for (i = 0; i < joystick->nsensors; ++i) {
  2171. if (joystick->sensors[i].type == type) {
  2172. retval = SDL_TRUE;
  2173. break;
  2174. }
  2175. }
  2176. }
  2177. }
  2178. SDL_UnlockJoysticks();
  2179. return retval;
  2180. }
  2181. /*
  2182. * Set whether data reporting for a game controller sensor is enabled
  2183. */
  2184. int SDL_GameControllerSetSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type, SDL_bool enabled)
  2185. {
  2186. SDL_LockJoysticks();
  2187. {
  2188. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2189. if (joystick) {
  2190. int i;
  2191. for (i = 0; i < joystick->nsensors; ++i) {
  2192. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  2193. if (sensor->type == type) {
  2194. if (sensor->enabled == enabled) {
  2195. SDL_UnlockJoysticks();
  2196. return 0;
  2197. }
  2198. if (enabled) {
  2199. if (joystick->nsensors_enabled == 0) {
  2200. if (joystick->driver->SetSensorsEnabled(joystick, SDL_TRUE) < 0) {
  2201. SDL_UnlockJoysticks();
  2202. return -1;
  2203. }
  2204. }
  2205. ++joystick->nsensors_enabled;
  2206. } else {
  2207. if (joystick->nsensors_enabled == 1) {
  2208. if (joystick->driver->SetSensorsEnabled(joystick, SDL_FALSE) < 0) {
  2209. SDL_UnlockJoysticks();
  2210. return -1;
  2211. }
  2212. }
  2213. --joystick->nsensors_enabled;
  2214. }
  2215. sensor->enabled = enabled;
  2216. SDL_UnlockJoysticks();
  2217. return 0;
  2218. }
  2219. }
  2220. }
  2221. }
  2222. SDL_UnlockJoysticks();
  2223. return SDL_Unsupported();
  2224. }
  2225. /*
  2226. * Query whether sensor data reporting is enabled for a game controller
  2227. */
  2228. SDL_bool SDL_GameControllerIsSensorEnabled(SDL_GameController *gamecontroller, SDL_SensorType type)
  2229. {
  2230. SDL_bool retval = SDL_FALSE;
  2231. SDL_LockJoysticks();
  2232. {
  2233. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2234. if (joystick) {
  2235. int i;
  2236. for (i = 0; i < joystick->nsensors; ++i) {
  2237. if (joystick->sensors[i].type == type) {
  2238. retval = joystick->sensors[i].enabled;
  2239. break;
  2240. }
  2241. }
  2242. }
  2243. }
  2244. SDL_UnlockJoysticks();
  2245. return retval;
  2246. }
  2247. /*
  2248. * Get the data rate of a game controller sensor.
  2249. */
  2250. float SDL_GameControllerGetSensorDataRate(SDL_GameController *gamecontroller, SDL_SensorType type)
  2251. {
  2252. float retval = 0.0f;
  2253. SDL_LockJoysticks();
  2254. {
  2255. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2256. if (joystick) {
  2257. int i;
  2258. for (i = 0; i < joystick->nsensors; ++i) {
  2259. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  2260. if (sensor->type == type) {
  2261. retval = sensor->rate;
  2262. break;
  2263. }
  2264. }
  2265. }
  2266. }
  2267. SDL_UnlockJoysticks();
  2268. return retval;
  2269. }
  2270. /*
  2271. * Get the current state of a game controller sensor.
  2272. */
  2273. int SDL_GameControllerGetSensorData(SDL_GameController *gamecontroller, SDL_SensorType type, float *data, int num_values)
  2274. {
  2275. return SDL_GameControllerGetSensorDataWithTimestamp(gamecontroller, type, NULL, data, num_values);
  2276. }
  2277. /*
  2278. * Get the current state of a game controller sensor.
  2279. */
  2280. int SDL_GameControllerGetSensorDataWithTimestamp(SDL_GameController *gamecontroller, SDL_SensorType type, Uint64 *timestamp, float *data, int num_values)
  2281. {
  2282. SDL_LockJoysticks();
  2283. {
  2284. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2285. if (joystick) {
  2286. int i;
  2287. for (i = 0; i < joystick->nsensors; ++i) {
  2288. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  2289. if (sensor->type == type) {
  2290. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  2291. SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
  2292. if (timestamp) {
  2293. *timestamp = sensor->timestamp_us;
  2294. }
  2295. SDL_UnlockJoysticks();
  2296. return 0;
  2297. }
  2298. }
  2299. }
  2300. }
  2301. SDL_UnlockJoysticks();
  2302. return SDL_Unsupported();
  2303. }
  2304. const char *SDL_GameControllerName(SDL_GameController *gamecontroller)
  2305. {
  2306. const char *retval = NULL;
  2307. SDL_LockJoysticks();
  2308. {
  2309. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
  2310. if (SDL_strcmp(gamecontroller->name, "*") == 0) {
  2311. retval = SDL_JoystickName(gamecontroller->joystick);
  2312. } else {
  2313. retval = gamecontroller->name;
  2314. }
  2315. }
  2316. SDL_UnlockJoysticks();
  2317. return retval;
  2318. }
  2319. const char *SDL_GameControllerPath(SDL_GameController *gamecontroller)
  2320. {
  2321. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2322. if (joystick == NULL) {
  2323. return NULL;
  2324. }
  2325. return SDL_JoystickPath(joystick);
  2326. }
  2327. SDL_GameControllerType SDL_GameControllerGetType(SDL_GameController *gamecontroller)
  2328. {
  2329. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2330. if (joystick == NULL) {
  2331. return SDL_CONTROLLER_TYPE_UNKNOWN;
  2332. }
  2333. return SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGetGUID(joystick), SDL_JoystickName(joystick));
  2334. }
  2335. int SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller)
  2336. {
  2337. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2338. if (joystick == NULL) {
  2339. return -1;
  2340. }
  2341. return SDL_JoystickGetPlayerIndex(joystick);
  2342. }
  2343. /**
  2344. * Set the player index of an opened game controller
  2345. */
  2346. void SDL_GameControllerSetPlayerIndex(SDL_GameController *gamecontroller, int player_index)
  2347. {
  2348. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2349. if (joystick == NULL) {
  2350. return;
  2351. }
  2352. SDL_JoystickSetPlayerIndex(joystick, player_index);
  2353. }
  2354. Uint16 SDL_GameControllerGetVendor(SDL_GameController *gamecontroller)
  2355. {
  2356. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2357. if (joystick == NULL) {
  2358. return 0;
  2359. }
  2360. return SDL_JoystickGetVendor(joystick);
  2361. }
  2362. Uint16 SDL_GameControllerGetProduct(SDL_GameController *gamecontroller)
  2363. {
  2364. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2365. if (joystick == NULL) {
  2366. return 0;
  2367. }
  2368. return SDL_JoystickGetProduct(joystick);
  2369. }
  2370. Uint16 SDL_GameControllerGetProductVersion(SDL_GameController *gamecontroller)
  2371. {
  2372. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2373. if (joystick == NULL) {
  2374. return 0;
  2375. }
  2376. return SDL_JoystickGetProductVersion(joystick);
  2377. }
  2378. Uint16 SDL_GameControllerGetFirmwareVersion(SDL_GameController *gamecontroller)
  2379. {
  2380. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2381. if (joystick == NULL) {
  2382. return 0;
  2383. }
  2384. return SDL_JoystickGetFirmwareVersion(joystick);
  2385. }
  2386. const char * SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
  2387. {
  2388. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2389. if (joystick == NULL) {
  2390. return NULL;
  2391. }
  2392. return SDL_JoystickGetSerial(joystick);
  2393. }
  2394. /*
  2395. * Return if the controller in question is currently attached to the system,
  2396. * \return 0 if not plugged in, 1 if still present.
  2397. */
  2398. SDL_bool SDL_GameControllerGetAttached(SDL_GameController *gamecontroller)
  2399. {
  2400. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2401. if (joystick == NULL) {
  2402. return SDL_FALSE;
  2403. }
  2404. return SDL_JoystickGetAttached(joystick);
  2405. }
  2406. /*
  2407. * Get the joystick for this controller
  2408. */
  2409. SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller)
  2410. {
  2411. SDL_Joystick *joystick;
  2412. SDL_LockJoysticks();
  2413. {
  2414. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
  2415. joystick = gamecontroller->joystick;
  2416. }
  2417. SDL_UnlockJoysticks();
  2418. return joystick;
  2419. }
  2420. /*
  2421. * Return the SDL_GameController associated with an instance id.
  2422. */
  2423. SDL_GameController *SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
  2424. {
  2425. SDL_GameController *gamecontroller;
  2426. SDL_LockJoysticks();
  2427. gamecontroller = SDL_gamecontrollers;
  2428. while (gamecontroller) {
  2429. if (gamecontroller->joystick->instance_id == joyid) {
  2430. SDL_UnlockJoysticks();
  2431. return gamecontroller;
  2432. }
  2433. gamecontroller = gamecontroller->next;
  2434. }
  2435. SDL_UnlockJoysticks();
  2436. return NULL;
  2437. }
  2438. /**
  2439. * Return the SDL_GameController associated with a player index.
  2440. */
  2441. SDL_GameController *SDL_GameControllerFromPlayerIndex(int player_index)
  2442. {
  2443. SDL_GameController *retval = NULL;
  2444. SDL_LockJoysticks();
  2445. {
  2446. SDL_Joystick *joystick = SDL_JoystickFromPlayerIndex(player_index);
  2447. if (joystick) {
  2448. retval = SDL_GameControllerFromInstanceID(joystick->instance_id);
  2449. }
  2450. }
  2451. SDL_UnlockJoysticks();
  2452. return retval;
  2453. }
  2454. /*
  2455. * Get the SDL joystick layer binding for this controller axis mapping
  2456. */
  2457. SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
  2458. {
  2459. SDL_GameControllerButtonBind bind;
  2460. SDL_zero(bind);
  2461. SDL_LockJoysticks();
  2462. {
  2463. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, bind);
  2464. if (axis != SDL_CONTROLLER_AXIS_INVALID) {
  2465. int i;
  2466. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  2467. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  2468. if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
  2469. bind.bindType = binding->inputType;
  2470. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  2471. /* FIXME: There might be multiple axes bound now that we have axis ranges... */
  2472. bind.value.axis = binding->input.axis.axis;
  2473. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
  2474. bind.value.button = binding->input.button;
  2475. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
  2476. bind.value.hat.hat = binding->input.hat.hat;
  2477. bind.value.hat.hat_mask = binding->input.hat.hat_mask;
  2478. }
  2479. break;
  2480. }
  2481. }
  2482. }
  2483. }
  2484. SDL_UnlockJoysticks();
  2485. return bind;
  2486. }
  2487. /*
  2488. * Get the SDL joystick layer binding for this controller button mapping
  2489. */
  2490. SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
  2491. {
  2492. SDL_GameControllerButtonBind bind;
  2493. SDL_zero(bind);
  2494. SDL_LockJoysticks();
  2495. {
  2496. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, bind);
  2497. if (button != SDL_CONTROLLER_BUTTON_INVALID) {
  2498. int i;
  2499. for (i = 0; i < gamecontroller->num_bindings; ++i) {
  2500. SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
  2501. if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
  2502. bind.bindType = binding->inputType;
  2503. if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
  2504. bind.value.axis = binding->input.axis.axis;
  2505. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
  2506. bind.value.button = binding->input.button;
  2507. } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
  2508. bind.value.hat.hat = binding->input.hat.hat;
  2509. bind.value.hat.hat_mask = binding->input.hat.hat_mask;
  2510. }
  2511. break;
  2512. }
  2513. }
  2514. }
  2515. }
  2516. SDL_UnlockJoysticks();
  2517. return bind;
  2518. }
  2519. int SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  2520. {
  2521. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2522. if (joystick == NULL) {
  2523. return -1;
  2524. }
  2525. return SDL_JoystickRumble(joystick, low_frequency_rumble, high_frequency_rumble, duration_ms);
  2526. }
  2527. int SDL_GameControllerRumbleTriggers(SDL_GameController *gamecontroller, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
  2528. {
  2529. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2530. if (joystick == NULL) {
  2531. return -1;
  2532. }
  2533. return SDL_JoystickRumbleTriggers(joystick, left_rumble, right_rumble, duration_ms);
  2534. }
  2535. SDL_bool SDL_GameControllerHasLED(SDL_GameController *gamecontroller)
  2536. {
  2537. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2538. if (joystick == NULL) {
  2539. return SDL_FALSE;
  2540. }
  2541. return SDL_JoystickHasLED(joystick);
  2542. }
  2543. SDL_bool SDL_GameControllerHasRumble(SDL_GameController *gamecontroller)
  2544. {
  2545. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2546. if (joystick == NULL) {
  2547. return SDL_FALSE;
  2548. }
  2549. return SDL_JoystickHasRumble(joystick);
  2550. }
  2551. SDL_bool SDL_GameControllerHasRumbleTriggers(SDL_GameController *gamecontroller)
  2552. {
  2553. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2554. if (joystick == NULL) {
  2555. return SDL_FALSE;
  2556. }
  2557. return SDL_JoystickHasRumbleTriggers(joystick);
  2558. }
  2559. int SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue)
  2560. {
  2561. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2562. if (joystick == NULL) {
  2563. return -1;
  2564. }
  2565. return SDL_JoystickSetLED(joystick, red, green, blue);
  2566. }
  2567. int SDL_GameControllerSendEffect(SDL_GameController *gamecontroller, const void *data, int size)
  2568. {
  2569. SDL_Joystick *joystick = SDL_GameControllerGetJoystick(gamecontroller);
  2570. if (joystick == NULL) {
  2571. return -1;
  2572. }
  2573. return SDL_JoystickSendEffect(joystick, data, size);
  2574. }
  2575. void SDL_GameControllerClose(SDL_GameController *gamecontroller)
  2576. {
  2577. SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
  2578. SDL_LockJoysticks();
  2579. if (gamecontroller == NULL || gamecontroller->magic != &gamecontroller_magic) {
  2580. SDL_UnlockJoysticks();
  2581. return;
  2582. }
  2583. /* First decrement ref count */
  2584. if (--gamecontroller->ref_count > 0) {
  2585. SDL_UnlockJoysticks();
  2586. return;
  2587. }
  2588. SDL_JoystickClose(gamecontroller->joystick);
  2589. gamecontrollerlist = SDL_gamecontrollers;
  2590. gamecontrollerlistprev = NULL;
  2591. while (gamecontrollerlist) {
  2592. if (gamecontroller == gamecontrollerlist) {
  2593. if (gamecontrollerlistprev) {
  2594. /* unlink this entry */
  2595. gamecontrollerlistprev->next = gamecontrollerlist->next;
  2596. } else {
  2597. SDL_gamecontrollers = gamecontroller->next;
  2598. }
  2599. break;
  2600. }
  2601. gamecontrollerlistprev = gamecontrollerlist;
  2602. gamecontrollerlist = gamecontrollerlist->next;
  2603. }
  2604. gamecontroller->magic = NULL;
  2605. SDL_free(gamecontroller->bindings);
  2606. SDL_free(gamecontroller->last_match_axis);
  2607. SDL_free(gamecontroller->last_hat_mask);
  2608. SDL_free(gamecontroller);
  2609. SDL_UnlockJoysticks();
  2610. }
  2611. /*
  2612. * Quit the controller subsystem
  2613. */
  2614. void SDL_GameControllerQuit(void)
  2615. {
  2616. SDL_LockJoysticks();
  2617. while (SDL_gamecontrollers) {
  2618. SDL_gamecontrollers->ref_count = 1;
  2619. SDL_GameControllerClose(SDL_gamecontrollers);
  2620. }
  2621. SDL_UnlockJoysticks();
  2622. }
  2623. void SDL_GameControllerQuitMappings(void)
  2624. {
  2625. ControllerMapping_t *pControllerMap;
  2626. SDL_AssertJoysticksLocked();
  2627. while (s_pSupportedControllers) {
  2628. pControllerMap = s_pSupportedControllers;
  2629. s_pSupportedControllers = s_pSupportedControllers->next;
  2630. SDL_free(pControllerMap->name);
  2631. SDL_free(pControllerMap->mapping);
  2632. SDL_free(pControllerMap);
  2633. }
  2634. SDL_DelEventWatch(SDL_GameControllerEventWatcher, NULL);
  2635. SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES,
  2636. SDL_GameControllerIgnoreDevicesChanged, NULL);
  2637. SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT,
  2638. SDL_GameControllerIgnoreDevicesExceptChanged, NULL);
  2639. if (SDL_allowed_controllers.entries) {
  2640. SDL_free(SDL_allowed_controllers.entries);
  2641. SDL_zero(SDL_allowed_controllers);
  2642. }
  2643. if (SDL_ignored_controllers.entries) {
  2644. SDL_free(SDL_ignored_controllers.entries);
  2645. SDL_zero(SDL_ignored_controllers);
  2646. }
  2647. }
  2648. /*
  2649. * Event filter to transform joystick events into appropriate game controller ones
  2650. */
  2651. static int SDL_PrivateGameControllerAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
  2652. {
  2653. int posted;
  2654. SDL_AssertJoysticksLocked();
  2655. /* translate the event, if desired */
  2656. posted = 0;
  2657. #if !SDL_EVENTS_DISABLED
  2658. if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
  2659. SDL_Event event;
  2660. event.type = SDL_CONTROLLERAXISMOTION;
  2661. event.caxis.which = gamecontroller->joystick->instance_id;
  2662. event.caxis.axis = axis;
  2663. event.caxis.value = value;
  2664. posted = SDL_PushEvent(&event) == 1;
  2665. }
  2666. #endif /* !SDL_EVENTS_DISABLED */
  2667. return posted;
  2668. }
  2669. /*
  2670. * Event filter to transform joystick events into appropriate game controller ones
  2671. */
  2672. static int SDL_PrivateGameControllerButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button, Uint8 state)
  2673. {
  2674. int posted;
  2675. #if !SDL_EVENTS_DISABLED
  2676. SDL_Event event;
  2677. SDL_AssertJoysticksLocked();
  2678. if (button == SDL_CONTROLLER_BUTTON_INVALID) {
  2679. return 0;
  2680. }
  2681. switch (state) {
  2682. case SDL_PRESSED:
  2683. event.type = SDL_CONTROLLERBUTTONDOWN;
  2684. break;
  2685. case SDL_RELEASED:
  2686. event.type = SDL_CONTROLLERBUTTONUP;
  2687. break;
  2688. default:
  2689. /* Invalid state -- bail */
  2690. return 0;
  2691. }
  2692. #endif /* !SDL_EVENTS_DISABLED */
  2693. if (button == SDL_CONTROLLER_BUTTON_GUIDE) {
  2694. Uint32 now = SDL_GetTicks();
  2695. if (state == SDL_PRESSED) {
  2696. gamecontroller->guide_button_down = now;
  2697. if (gamecontroller->joystick->delayed_guide_button) {
  2698. /* Skip duplicate press */
  2699. return 0;
  2700. }
  2701. } else {
  2702. if (!SDL_TICKS_PASSED(now, gamecontroller->guide_button_down + SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS)) {
  2703. gamecontroller->joystick->delayed_guide_button = SDL_TRUE;
  2704. return 0;
  2705. }
  2706. gamecontroller->joystick->delayed_guide_button = SDL_FALSE;
  2707. }
  2708. }
  2709. /* translate the event, if desired */
  2710. posted = 0;
  2711. #if !SDL_EVENTS_DISABLED
  2712. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  2713. event.cbutton.which = gamecontroller->joystick->instance_id;
  2714. event.cbutton.button = button;
  2715. event.cbutton.state = state;
  2716. posted = SDL_PushEvent(&event) == 1;
  2717. }
  2718. #endif /* !SDL_EVENTS_DISABLED */
  2719. return posted;
  2720. }
  2721. /*
  2722. * Turn off controller events
  2723. */
  2724. int SDL_GameControllerEventState(int state)
  2725. {
  2726. #if SDL_EVENTS_DISABLED
  2727. return SDL_IGNORE;
  2728. #else
  2729. const Uint32 event_list[] = {
  2730. SDL_CONTROLLERAXISMOTION,
  2731. SDL_CONTROLLERBUTTONDOWN,
  2732. SDL_CONTROLLERBUTTONUP,
  2733. SDL_CONTROLLERDEVICEADDED,
  2734. SDL_CONTROLLERDEVICEREMOVED,
  2735. SDL_CONTROLLERDEVICEREMAPPED,
  2736. SDL_CONTROLLERTOUCHPADDOWN,
  2737. SDL_CONTROLLERTOUCHPADMOTION,
  2738. SDL_CONTROLLERTOUCHPADUP,
  2739. SDL_CONTROLLERSENSORUPDATE,
  2740. };
  2741. unsigned int i;
  2742. switch (state) {
  2743. case SDL_QUERY:
  2744. state = SDL_IGNORE;
  2745. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  2746. state = SDL_EventState(event_list[i], SDL_QUERY);
  2747. if (state == SDL_ENABLE) {
  2748. break;
  2749. }
  2750. }
  2751. break;
  2752. default:
  2753. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  2754. (void)SDL_EventState(event_list[i], state);
  2755. }
  2756. break;
  2757. }
  2758. return state;
  2759. #endif /* SDL_EVENTS_DISABLED */
  2760. }
  2761. void SDL_GameControllerHandleDelayedGuideButton(SDL_Joystick *joystick)
  2762. {
  2763. SDL_GameController *controller;
  2764. SDL_AssertJoysticksLocked();
  2765. for (controller = SDL_gamecontrollers; controller; controller = controller->next) {
  2766. if (controller->joystick == joystick) {
  2767. SDL_PrivateGameControllerButton(controller, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
  2768. break;
  2769. }
  2770. }
  2771. }
  2772. const char *SDL_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
  2773. {
  2774. #if defined(SDL_JOYSTICK_MFI)
  2775. const char *IOS_GameControllerGetAppleSFSymbolsNameForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button);
  2776. const char *retval;
  2777. SDL_LockJoysticks();
  2778. {
  2779. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
  2780. retval = IOS_GameControllerGetAppleSFSymbolsNameForButton(gamecontroller, button);
  2781. }
  2782. SDL_UnlockJoysticks();
  2783. return retval;
  2784. #else
  2785. return NULL;
  2786. #endif
  2787. }
  2788. const char *SDL_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
  2789. {
  2790. #if defined(SDL_JOYSTICK_MFI)
  2791. const char *IOS_GameControllerGetAppleSFSymbolsNameForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis);
  2792. const char *retval;
  2793. SDL_LockJoysticks();
  2794. {
  2795. CHECK_GAMECONTROLLER_MAGIC(gamecontroller, NULL);
  2796. retval = IOS_GameControllerGetAppleSFSymbolsNameForAxis(gamecontroller, axis);
  2797. }
  2798. SDL_UnlockJoysticks();
  2799. return retval;
  2800. #else
  2801. return NULL;
  2802. #endif
  2803. }
  2804. /* vi: set ts=4 sw=4 expandtab: */