SDL_gamecontroller.c 67 KB

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