SDL_gamecontroller.c 104 KB

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