SDL_gamecontroller.c 90 KB

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