SDL_gamecontroller.c 77 KB

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