SDL_gamecontroller.c 95 KB

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