SDL_gamecontroller.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2015 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_sysjoystick.h"
  23. #include "SDL_hints.h"
  24. #include "SDL_gamecontrollerdb.h"
  25. #if !SDL_EVENTS_DISABLED
  26. #include "../events/SDL_events_c.h"
  27. #endif
  28. #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
  29. #define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
  30. /* a list of currently opened game controllers */
  31. static SDL_GameController *SDL_gamecontrollers = NULL;
  32. /* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
  33. struct _SDL_HatMapping
  34. {
  35. int hat;
  36. Uint8 mask;
  37. };
  38. #define k_nMaxReverseEntries 20
  39. /**
  40. * We are encoding the "HAT" as 0xhm. where h == hat ID and m == mask
  41. * MAX 4 hats supported
  42. */
  43. #define k_nMaxHatEntries 0x3f + 1
  44. /* our in memory mapping db between joystick objects and controller mappings */
  45. struct _SDL_ControllerMapping
  46. {
  47. SDL_JoystickGUID guid;
  48. const char *name;
  49. /* mapping of axis/button id to controller version */
  50. int axes[SDL_CONTROLLER_AXIS_MAX];
  51. int buttonasaxis[SDL_CONTROLLER_AXIS_MAX];
  52. int buttons[SDL_CONTROLLER_BUTTON_MAX];
  53. int axesasbutton[SDL_CONTROLLER_BUTTON_MAX];
  54. struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX];
  55. /* reverse mapping, joystick indices to buttons */
  56. SDL_GameControllerAxis raxes[k_nMaxReverseEntries];
  57. SDL_GameControllerAxis rbuttonasaxis[k_nMaxReverseEntries];
  58. SDL_GameControllerButton rbuttons[k_nMaxReverseEntries];
  59. SDL_GameControllerButton raxesasbutton[k_nMaxReverseEntries];
  60. SDL_GameControllerButton rhatasbutton[k_nMaxHatEntries];
  61. };
  62. /* our hard coded list of mapping support */
  63. typedef struct _ControllerMapping_t
  64. {
  65. SDL_JoystickGUID guid;
  66. char *name;
  67. char *mapping;
  68. struct _ControllerMapping_t *next;
  69. } ControllerMapping_t;
  70. static ControllerMapping_t *s_pSupportedControllers = NULL;
  71. static ControllerMapping_t *s_pXInputMapping = NULL;
  72. static ControllerMapping_t *s_pEmscriptenMapping = NULL;
  73. /* The SDL game controller structure */
  74. struct _SDL_GameController
  75. {
  76. SDL_Joystick *joystick; /* underlying joystick device */
  77. int ref_count;
  78. Uint8 hatState[4]; /* the current hat state for this controller */
  79. struct _SDL_ControllerMapping mapping; /* the mapping object for this controller */
  80. struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
  81. };
  82. int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
  83. int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
  84. /*
  85. * Event filter to fire controller events from joystick ones
  86. */
  87. int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
  88. {
  89. switch(event->type) {
  90. case SDL_JOYAXISMOTION:
  91. {
  92. SDL_GameController *controllerlist;
  93. if (event->jaxis.axis >= k_nMaxReverseEntries) break;
  94. controllerlist = SDL_gamecontrollers;
  95. while (controllerlist) {
  96. if (controllerlist->joystick->instance_id == event->jaxis.which) {
  97. if (controllerlist->mapping.raxes[event->jaxis.axis] >= 0) /* simple axis to axis, send it through */ {
  98. SDL_GameControllerAxis axis = controllerlist->mapping.raxes[event->jaxis.axis];
  99. Sint16 value = event->jaxis.value;
  100. switch (axis) {
  101. case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
  102. case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
  103. /* Shift it to be 0 - 32767. */
  104. value = value / 2 + 16384;
  105. default:
  106. break;
  107. }
  108. SDL_PrivateGameControllerAxis(controllerlist, axis, value);
  109. } else if (controllerlist->mapping.raxesasbutton[event->jaxis.axis] >= 0) { /* simulate an axis as a button */
  110. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.raxesasbutton[event->jaxis.axis], ABS(event->jaxis.value) > 32768/2 ? SDL_PRESSED : SDL_RELEASED);
  111. }
  112. break;
  113. }
  114. controllerlist = controllerlist->next;
  115. }
  116. }
  117. break;
  118. case SDL_JOYBUTTONDOWN:
  119. case SDL_JOYBUTTONUP:
  120. {
  121. SDL_GameController *controllerlist;
  122. if (event->jbutton.button >= k_nMaxReverseEntries) break;
  123. controllerlist = SDL_gamecontrollers;
  124. while (controllerlist) {
  125. if (controllerlist->joystick->instance_id == event->jbutton.which) {
  126. if (controllerlist->mapping.rbuttons[event->jbutton.button] >= 0) { /* simple button as button */
  127. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rbuttons[event->jbutton.button], event->jbutton.state);
  128. } else if (controllerlist->mapping.rbuttonasaxis[event->jbutton.button] >= 0) { /* an button pretending to be an axis */
  129. SDL_PrivateGameControllerAxis(controllerlist, controllerlist->mapping.rbuttonasaxis[event->jbutton.button], event->jbutton.state > 0 ? 32767 : 0);
  130. }
  131. break;
  132. }
  133. controllerlist = controllerlist->next;
  134. }
  135. }
  136. break;
  137. case SDL_JOYHATMOTION:
  138. {
  139. SDL_GameController *controllerlist;
  140. if (event->jhat.hat >= 4) break;
  141. controllerlist = SDL_gamecontrollers;
  142. while (controllerlist) {
  143. if (controllerlist->joystick->instance_id == event->jhat.which) {
  144. Uint8 bSame = controllerlist->hatState[event->jhat.hat] & event->jhat.value;
  145. /* Get list of removed bits (button release) */
  146. Uint8 bChanged = controllerlist->hatState[event->jhat.hat] ^ bSame;
  147. /* the hat idx in the high nibble */
  148. int bHighHat = event->jhat.hat << 4;
  149. if (bChanged & SDL_HAT_DOWN)
  150. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_RELEASED);
  151. if (bChanged & SDL_HAT_UP)
  152. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_RELEASED);
  153. if (bChanged & SDL_HAT_LEFT)
  154. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_RELEASED);
  155. if (bChanged & SDL_HAT_RIGHT)
  156. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_RELEASED);
  157. /* Get list of added bits (button press) */
  158. bChanged = event->jhat.value ^ bSame;
  159. if (bChanged & SDL_HAT_DOWN)
  160. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_PRESSED);
  161. if (bChanged & SDL_HAT_UP)
  162. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_PRESSED);
  163. if (bChanged & SDL_HAT_LEFT)
  164. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_PRESSED);
  165. if (bChanged & SDL_HAT_RIGHT)
  166. SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_PRESSED);
  167. /* update our state cache */
  168. controllerlist->hatState[event->jhat.hat] = event->jhat.value;
  169. break;
  170. }
  171. controllerlist = controllerlist->next;
  172. }
  173. }
  174. break;
  175. case SDL_JOYDEVICEADDED:
  176. {
  177. if (SDL_IsGameController(event->jdevice.which)) {
  178. SDL_Event deviceevent;
  179. deviceevent.type = SDL_CONTROLLERDEVICEADDED;
  180. deviceevent.cdevice.which = event->jdevice.which;
  181. SDL_PushEvent(&deviceevent);
  182. }
  183. }
  184. break;
  185. case SDL_JOYDEVICEREMOVED:
  186. {
  187. SDL_GameController *controllerlist = SDL_gamecontrollers;
  188. while (controllerlist) {
  189. if (controllerlist->joystick->instance_id == event->jdevice.which) {
  190. SDL_Event deviceevent;
  191. deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
  192. deviceevent.cdevice.which = event->jdevice.which;
  193. SDL_PushEvent(&deviceevent);
  194. break;
  195. }
  196. controllerlist = controllerlist->next;
  197. }
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. return 1;
  204. }
  205. /*
  206. * Helper function to scan the mappings database for a controller with the specified GUID
  207. */
  208. ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
  209. {
  210. ControllerMapping_t *pSupportedController = s_pSupportedControllers;
  211. while (pSupportedController) {
  212. if (SDL_memcmp(guid, &pSupportedController->guid, sizeof(*guid)) == 0) {
  213. return pSupportedController;
  214. }
  215. pSupportedController = pSupportedController->next;
  216. }
  217. return NULL;
  218. }
  219. static const char* map_StringForControllerAxis[] = {
  220. "leftx",
  221. "lefty",
  222. "rightx",
  223. "righty",
  224. "lefttrigger",
  225. "righttrigger",
  226. NULL
  227. };
  228. /*
  229. * convert a string to its enum equivalent
  230. */
  231. SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *pchString)
  232. {
  233. int entry;
  234. if (!pchString || !pchString[0])
  235. return SDL_CONTROLLER_AXIS_INVALID;
  236. for (entry = 0; map_StringForControllerAxis[entry]; ++entry) {
  237. if (!SDL_strcasecmp(pchString, map_StringForControllerAxis[entry]))
  238. return entry;
  239. }
  240. return SDL_CONTROLLER_AXIS_INVALID;
  241. }
  242. /*
  243. * convert an enum to its string equivalent
  244. */
  245. const char* SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
  246. {
  247. if (axis > SDL_CONTROLLER_AXIS_INVALID && axis < SDL_CONTROLLER_AXIS_MAX) {
  248. return map_StringForControllerAxis[axis];
  249. }
  250. return NULL;
  251. }
  252. static const char* map_StringForControllerButton[] = {
  253. "a",
  254. "b",
  255. "x",
  256. "y",
  257. "back",
  258. "guide",
  259. "start",
  260. "leftstick",
  261. "rightstick",
  262. "leftshoulder",
  263. "rightshoulder",
  264. "dpup",
  265. "dpdown",
  266. "dpleft",
  267. "dpright",
  268. NULL
  269. };
  270. /*
  271. * convert a string to its enum equivalent
  272. */
  273. SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *pchString)
  274. {
  275. int entry;
  276. if (!pchString || !pchString[0])
  277. return SDL_CONTROLLER_BUTTON_INVALID;
  278. for (entry = 0; map_StringForControllerButton[entry]; ++entry) {
  279. if (SDL_strcasecmp(pchString, map_StringForControllerButton[entry]) == 0)
  280. return entry;
  281. }
  282. return SDL_CONTROLLER_BUTTON_INVALID;
  283. }
  284. /*
  285. * convert an enum to its string equivalent
  286. */
  287. const char* SDL_GameControllerGetStringForButton(SDL_GameControllerButton axis)
  288. {
  289. if (axis > SDL_CONTROLLER_BUTTON_INVALID && axis < SDL_CONTROLLER_BUTTON_MAX) {
  290. return map_StringForControllerButton[axis];
  291. }
  292. return NULL;
  293. }
  294. /*
  295. * given a controller button name and a joystick name update our mapping structure with it
  296. */
  297. void SDL_PrivateGameControllerParseButton(const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping)
  298. {
  299. int iSDLButton = 0;
  300. SDL_GameControllerButton button;
  301. SDL_GameControllerAxis axis;
  302. button = SDL_GameControllerGetButtonFromString(szGameButton);
  303. axis = SDL_GameControllerGetAxisFromString(szGameButton);
  304. iSDLButton = SDL_atoi(&szJoystickButton[1]);
  305. if (szJoystickButton[0] == 'a') {
  306. if (iSDLButton >= k_nMaxReverseEntries) {
  307. SDL_SetError("Axis index too large: %d", iSDLButton);
  308. return;
  309. }
  310. if (axis != SDL_CONTROLLER_AXIS_INVALID) {
  311. pMapping->axes[ axis ] = iSDLButton;
  312. pMapping->raxes[ iSDLButton ] = axis;
  313. } else if (button != SDL_CONTROLLER_BUTTON_INVALID) {
  314. pMapping->axesasbutton[ button ] = iSDLButton;
  315. pMapping->raxesasbutton[ iSDLButton ] = button;
  316. } else {
  317. SDL_assert(!"How did we get here?");
  318. }
  319. } else if (szJoystickButton[0] == 'b') {
  320. if (iSDLButton >= k_nMaxReverseEntries) {
  321. SDL_SetError("Button index too large: %d", iSDLButton);
  322. return;
  323. }
  324. if (button != SDL_CONTROLLER_BUTTON_INVALID) {
  325. pMapping->buttons[ button ] = iSDLButton;
  326. pMapping->rbuttons[ iSDLButton ] = button;
  327. } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
  328. pMapping->buttonasaxis[ axis ] = iSDLButton;
  329. pMapping->rbuttonasaxis[ iSDLButton ] = axis;
  330. } else {
  331. SDL_assert(!"How did we get here?");
  332. }
  333. } else if (szJoystickButton[0] == 'h') {
  334. int hat = SDL_atoi(&szJoystickButton[1]);
  335. int mask = SDL_atoi(&szJoystickButton[3]);
  336. if (hat >= 4) {
  337. SDL_SetError("Hat index too large: %d", iSDLButton);
  338. }
  339. if (button != SDL_CONTROLLER_BUTTON_INVALID) {
  340. int ridx;
  341. pMapping->hatasbutton[ button ].hat = hat;
  342. pMapping->hatasbutton[ button ].mask = mask;
  343. ridx = (hat << 4) | mask;
  344. pMapping->rhatasbutton[ ridx ] = button;
  345. } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
  346. SDL_assert(!"Support hat as axis");
  347. } else {
  348. SDL_assert(!"How did we get here?");
  349. }
  350. }
  351. }
  352. /*
  353. * given a controller mapping string update our mapping object
  354. */
  355. static void
  356. SDL_PrivateGameControllerParseControllerConfigString(struct _SDL_ControllerMapping *pMapping, const char *pchString)
  357. {
  358. char szGameButton[20];
  359. char szJoystickButton[20];
  360. SDL_bool bGameButton = SDL_TRUE;
  361. int i = 0;
  362. const char *pchPos = pchString;
  363. SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
  364. SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
  365. while (pchPos && *pchPos) {
  366. if (*pchPos == ':') {
  367. i = 0;
  368. bGameButton = SDL_FALSE;
  369. } else if (*pchPos == ' ') {
  370. } else if (*pchPos == ',') {
  371. i = 0;
  372. bGameButton = SDL_TRUE;
  373. SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
  374. SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
  375. SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
  376. } else if (bGameButton) {
  377. if (i >= sizeof(szGameButton)) {
  378. SDL_SetError("Button name too large: %s", szGameButton);
  379. return;
  380. }
  381. szGameButton[i] = *pchPos;
  382. i++;
  383. } else {
  384. if (i >= sizeof(szJoystickButton)) {
  385. SDL_SetError("Joystick button name too large: %s", szJoystickButton);
  386. return;
  387. }
  388. szJoystickButton[i] = *pchPos;
  389. i++;
  390. }
  391. pchPos++;
  392. }
  393. SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
  394. }
  395. /*
  396. * Make a new button mapping struct
  397. */
  398. void SDL_PrivateLoadButtonMapping(struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
  399. {
  400. int j;
  401. pMapping->guid = guid;
  402. pMapping->name = pchName;
  403. /* set all the button mappings to non defaults */
  404. for (j = 0; j < SDL_CONTROLLER_AXIS_MAX; j++) {
  405. pMapping->axes[j] = -1;
  406. pMapping->buttonasaxis[j] = -1;
  407. }
  408. for (j = 0; j < SDL_CONTROLLER_BUTTON_MAX; j++) {
  409. pMapping->buttons[j] = -1;
  410. pMapping->axesasbutton[j] = -1;
  411. pMapping->hatasbutton[j].hat = -1;
  412. }
  413. for (j = 0; j < k_nMaxReverseEntries; j++) {
  414. pMapping->raxes[j] = SDL_CONTROLLER_AXIS_INVALID;
  415. pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID;
  416. pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID;
  417. pMapping->raxesasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
  418. }
  419. for (j = 0; j < k_nMaxHatEntries; j++) {
  420. pMapping->rhatasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
  421. }
  422. SDL_PrivateGameControllerParseControllerConfigString(pMapping, pchMapping);
  423. }
  424. /*
  425. * grab the guid string from a mapping string
  426. */
  427. char *SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
  428. {
  429. const char *pFirstComma = SDL_strchr(pMapping, ',');
  430. if (pFirstComma) {
  431. char *pchGUID = SDL_malloc(pFirstComma - pMapping + 1);
  432. if (!pchGUID) {
  433. SDL_OutOfMemory();
  434. return NULL;
  435. }
  436. SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
  437. pchGUID[ pFirstComma - pMapping ] = 0;
  438. return pchGUID;
  439. }
  440. return NULL;
  441. }
  442. /*
  443. * grab the name string from a mapping string
  444. */
  445. char *SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
  446. {
  447. const char *pFirstComma, *pSecondComma;
  448. char *pchName;
  449. pFirstComma = SDL_strchr(pMapping, ',');
  450. if (!pFirstComma)
  451. return NULL;
  452. pSecondComma = SDL_strchr(pFirstComma + 1, ',');
  453. if (!pSecondComma)
  454. return NULL;
  455. pchName = SDL_malloc(pSecondComma - pFirstComma);
  456. if (!pchName) {
  457. SDL_OutOfMemory();
  458. return NULL;
  459. }
  460. SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
  461. pchName[ pSecondComma - pFirstComma - 1 ] = 0;
  462. return pchName;
  463. }
  464. /*
  465. * grab the button mapping string from a mapping string
  466. */
  467. char *SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
  468. {
  469. const char *pFirstComma, *pSecondComma;
  470. pFirstComma = SDL_strchr(pMapping, ',');
  471. if (!pFirstComma)
  472. return NULL;
  473. pSecondComma = SDL_strchr(pFirstComma + 1, ',');
  474. if (!pSecondComma)
  475. return NULL;
  476. return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
  477. }
  478. /*
  479. * Helper function to refresh a mapping
  480. */
  481. void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
  482. {
  483. SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
  484. while (gamecontrollerlist) {
  485. if (!SDL_memcmp(&gamecontrollerlist->mapping.guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid))) {
  486. SDL_Event event;
  487. event.type = SDL_CONTROLLERDEVICEREMAPPED;
  488. event.cdevice.which = gamecontrollerlist->joystick->instance_id;
  489. SDL_PushEvent(&event);
  490. /* Not really threadsafe. Should this lock access within SDL_GameControllerEventWatcher? */
  491. SDL_PrivateLoadButtonMapping(&gamecontrollerlist->mapping, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
  492. }
  493. gamecontrollerlist = gamecontrollerlist->next;
  494. }
  495. }
  496. /*
  497. * Helper function to add a mapping for a guid
  498. */
  499. static ControllerMapping_t *
  500. SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing)
  501. {
  502. char *pchName;
  503. char *pchMapping;
  504. ControllerMapping_t *pControllerMapping;
  505. pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
  506. if (!pchName) {
  507. SDL_SetError("Couldn't parse name from %s", mappingString);
  508. return NULL;
  509. }
  510. pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
  511. if (!pchMapping) {
  512. SDL_free(pchName);
  513. SDL_SetError("Couldn't parse %s", mappingString);
  514. return NULL;
  515. }
  516. pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
  517. if (pControllerMapping) {
  518. /* Update existing mapping */
  519. SDL_free(pControllerMapping->name);
  520. pControllerMapping->name = pchName;
  521. SDL_free(pControllerMapping->mapping);
  522. pControllerMapping->mapping = pchMapping;
  523. /* refresh open controllers */
  524. SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
  525. *existing = SDL_TRUE;
  526. } else {
  527. pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
  528. if (!pControllerMapping) {
  529. SDL_free(pchName);
  530. SDL_free(pchMapping);
  531. SDL_OutOfMemory();
  532. return NULL;
  533. }
  534. pControllerMapping->guid = jGUID;
  535. pControllerMapping->name = pchName;
  536. pControllerMapping->mapping = pchMapping;
  537. pControllerMapping->next = s_pSupportedControllers;
  538. s_pSupportedControllers = pControllerMapping;
  539. *existing = SDL_FALSE;
  540. }
  541. return pControllerMapping;
  542. }
  543. /*
  544. * Helper function to determine pre-calculated offset to certain joystick mappings
  545. */
  546. ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
  547. {
  548. SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID(device_index);
  549. ControllerMapping_t *mapping;
  550. mapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
  551. #if SDL_JOYSTICK_XINPUT
  552. if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
  553. mapping = s_pXInputMapping;
  554. }
  555. #endif
  556. #if defined(SDL_JOYSTICK_EMSCRIPTEN)
  557. if (!mapping && s_pEmscriptenMapping) {
  558. mapping = s_pEmscriptenMapping;
  559. }
  560. #endif
  561. #ifdef __LINUX__
  562. if (!mapping) {
  563. const char *name = SDL_JoystickNameForIndex(device_index);
  564. if (name) {
  565. if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
  566. /* The Linux driver xpad.c maps the wireless dpad to buttons */
  567. SDL_bool existing;
  568. mapping = SDL_PrivateAddMappingForGUID(jGUID,
  569. "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,",
  570. &existing);
  571. }
  572. }
  573. }
  574. #endif /* __LINUX__ */
  575. if (!mapping) {
  576. const char *name = SDL_JoystickNameForIndex(device_index);
  577. if (name) {
  578. if (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box")) {
  579. mapping = s_pXInputMapping;
  580. }
  581. }
  582. }
  583. return mapping;
  584. }
  585. /*
  586. * Add or update an entry into the Mappings Database
  587. */
  588. int
  589. SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw)
  590. {
  591. const char *platform = SDL_GetPlatform();
  592. int controllers = 0;
  593. char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
  594. size_t db_size, platform_len;
  595. if (rw == NULL) {
  596. return SDL_SetError("Invalid RWops");
  597. }
  598. db_size = (size_t)SDL_RWsize(rw);
  599. buf = (char *)SDL_malloc(db_size + 1);
  600. if (buf == NULL) {
  601. if (freerw) {
  602. SDL_RWclose(rw);
  603. }
  604. return SDL_SetError("Could not allocate space to read DB into memory");
  605. }
  606. if (SDL_RWread(rw, buf, db_size, 1) != 1) {
  607. if (freerw) {
  608. SDL_RWclose(rw);
  609. }
  610. SDL_free(buf);
  611. return SDL_SetError("Could not read DB");
  612. }
  613. if (freerw) {
  614. SDL_RWclose(rw);
  615. }
  616. buf[db_size] = '\0';
  617. line = buf;
  618. while (line < buf + db_size) {
  619. line_end = SDL_strchr(line, '\n');
  620. if (line_end != NULL) {
  621. *line_end = '\0';
  622. } else {
  623. line_end = buf + db_size;
  624. }
  625. /* Extract and verify the platform */
  626. tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
  627. if (tmp != NULL) {
  628. tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
  629. comma = SDL_strchr(tmp, ',');
  630. if (comma != NULL) {
  631. platform_len = comma - tmp + 1;
  632. if (platform_len + 1 < SDL_arraysize(line_platform)) {
  633. SDL_strlcpy(line_platform, tmp, platform_len);
  634. if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
  635. SDL_GameControllerAddMapping(line) > 0) {
  636. controllers++;
  637. }
  638. }
  639. }
  640. }
  641. line = line_end + 1;
  642. }
  643. SDL_free(buf);
  644. return controllers;
  645. }
  646. /*
  647. * Add or update an entry into the Mappings Database
  648. */
  649. int
  650. SDL_GameControllerAddMapping(const char *mappingString)
  651. {
  652. char *pchGUID;
  653. SDL_JoystickGUID jGUID;
  654. SDL_bool is_xinput_mapping = SDL_FALSE;
  655. SDL_bool is_emscripten_mapping = SDL_FALSE;
  656. SDL_bool existing = SDL_FALSE;
  657. ControllerMapping_t *pControllerMapping;
  658. if (!mappingString) {
  659. return SDL_InvalidParamError("mappingString");
  660. }
  661. pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
  662. if (!pchGUID) {
  663. return SDL_SetError("Couldn't parse GUID from %s", mappingString);
  664. }
  665. if (!SDL_strcasecmp(pchGUID, "xinput")) {
  666. is_xinput_mapping = SDL_TRUE;
  667. }
  668. if (!SDL_strcasecmp(pchGUID, "emscripten")) {
  669. is_emscripten_mapping = SDL_TRUE;
  670. }
  671. jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
  672. SDL_free(pchGUID);
  673. pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing);
  674. if (!pControllerMapping) {
  675. return -1;
  676. }
  677. if (existing) {
  678. return 0;
  679. } else {
  680. if (is_xinput_mapping) {
  681. s_pXInputMapping = pControllerMapping;
  682. }
  683. if (is_emscripten_mapping) {
  684. s_pEmscriptenMapping = pControllerMapping;
  685. }
  686. return 1;
  687. }
  688. }
  689. /*
  690. * Get the mapping string for this GUID
  691. */
  692. char *
  693. SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
  694. {
  695. char *pMappingString = NULL;
  696. ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
  697. if (mapping) {
  698. char pchGUID[33];
  699. size_t needed;
  700. SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
  701. /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
  702. needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
  703. pMappingString = SDL_malloc(needed);
  704. if (!pMappingString) {
  705. SDL_OutOfMemory();
  706. return NULL;
  707. }
  708. SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
  709. }
  710. return pMappingString;
  711. }
  712. /*
  713. * Get the mapping string for this device
  714. */
  715. char *
  716. SDL_GameControllerMapping(SDL_GameController * gamecontroller)
  717. {
  718. if (!gamecontroller) {
  719. return NULL;
  720. }
  721. return SDL_GameControllerMappingForGUID(gamecontroller->mapping.guid);
  722. }
  723. static void
  724. SDL_GameControllerLoadHints()
  725. {
  726. const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
  727. if (hint && hint[0]) {
  728. size_t nchHints = SDL_strlen(hint);
  729. char *pUserMappings = SDL_malloc(nchHints + 1);
  730. char *pTempMappings = pUserMappings;
  731. SDL_memcpy(pUserMappings, hint, nchHints);
  732. pUserMappings[nchHints] = '\0';
  733. while (pUserMappings) {
  734. char *pchNewLine = NULL;
  735. pchNewLine = SDL_strchr(pUserMappings, '\n');
  736. if (pchNewLine)
  737. *pchNewLine = '\0';
  738. SDL_GameControllerAddMapping(pUserMappings);
  739. if (pchNewLine) {
  740. pUserMappings = pchNewLine + 1;
  741. } else {
  742. pUserMappings = NULL;
  743. }
  744. }
  745. SDL_free(pTempMappings);
  746. }
  747. }
  748. /*
  749. * Initialize the game controller system, mostly load our DB of controller config mappings
  750. */
  751. int
  752. SDL_GameControllerInit(void)
  753. {
  754. int i = 0;
  755. const char *pMappingString = NULL;
  756. s_pSupportedControllers = NULL;
  757. pMappingString = s_ControllerMappings[i];
  758. while (pMappingString) {
  759. SDL_GameControllerAddMapping(pMappingString);
  760. i++;
  761. pMappingString = s_ControllerMappings[i];
  762. }
  763. /* load in any user supplied config */
  764. SDL_GameControllerLoadHints();
  765. /* watch for joy events and fire controller ones if needed */
  766. SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
  767. /* Send added events for controllers currently attached */
  768. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  769. if (SDL_IsGameController(i)) {
  770. SDL_Event deviceevent;
  771. deviceevent.type = SDL_CONTROLLERDEVICEADDED;
  772. deviceevent.cdevice.which = i;
  773. SDL_PushEvent(&deviceevent);
  774. }
  775. }
  776. return (0);
  777. }
  778. /*
  779. * Get the implementation dependent name of a controller
  780. */
  781. const char *
  782. SDL_GameControllerNameForIndex(int device_index)
  783. {
  784. ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
  785. if (pSupportedController) {
  786. return pSupportedController->name;
  787. }
  788. return NULL;
  789. }
  790. /*
  791. * Return 1 if the joystick at this device index is a supported controller
  792. */
  793. SDL_bool
  794. SDL_IsGameController(int device_index)
  795. {
  796. ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
  797. if (pSupportedController) {
  798. return SDL_TRUE;
  799. }
  800. return SDL_FALSE;
  801. }
  802. /*
  803. * Open a controller for use - the index passed as an argument refers to
  804. * the N'th controller on the system. This index is the value which will
  805. * identify this controller in future controller events.
  806. *
  807. * This function returns a controller identifier, or NULL if an error occurred.
  808. */
  809. SDL_GameController *
  810. SDL_GameControllerOpen(int device_index)
  811. {
  812. SDL_GameController *gamecontroller;
  813. SDL_GameController *gamecontrollerlist;
  814. ControllerMapping_t *pSupportedController = NULL;
  815. if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
  816. SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
  817. return (NULL);
  818. }
  819. gamecontrollerlist = SDL_gamecontrollers;
  820. /* If the controller is already open, return it */
  821. while (gamecontrollerlist) {
  822. if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
  823. gamecontroller = gamecontrollerlist;
  824. ++gamecontroller->ref_count;
  825. return (gamecontroller);
  826. }
  827. gamecontrollerlist = gamecontrollerlist->next;
  828. }
  829. /* Find a controller mapping */
  830. pSupportedController = SDL_PrivateGetControllerMapping(device_index);
  831. if (!pSupportedController) {
  832. SDL_SetError("Couldn't find mapping for device (%d)", device_index);
  833. return (NULL);
  834. }
  835. /* Create and initialize the joystick */
  836. gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
  837. if (gamecontroller == NULL) {
  838. SDL_OutOfMemory();
  839. return NULL;
  840. }
  841. SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
  842. gamecontroller->joystick = SDL_JoystickOpen(device_index);
  843. if (!gamecontroller->joystick) {
  844. SDL_free(gamecontroller);
  845. return NULL;
  846. }
  847. SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
  848. /* Add joystick to list */
  849. ++gamecontroller->ref_count;
  850. /* Link the joystick in the list */
  851. gamecontroller->next = SDL_gamecontrollers;
  852. SDL_gamecontrollers = gamecontroller;
  853. SDL_SYS_JoystickUpdate(gamecontroller->joystick);
  854. return (gamecontroller);
  855. }
  856. /*
  857. * Manually pump for controller updates.
  858. */
  859. void
  860. SDL_GameControllerUpdate(void)
  861. {
  862. /* Just for API completeness; the joystick API does all the work. */
  863. SDL_JoystickUpdate();
  864. }
  865. /*
  866. * Get the current state of an axis control on a controller
  867. */
  868. Sint16
  869. SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
  870. {
  871. if (!gamecontroller)
  872. return 0;
  873. if (gamecontroller->mapping.axes[axis] >= 0) {
  874. Sint16 value = (SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axes[axis]));
  875. switch (axis) {
  876. case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
  877. case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
  878. /* Shift it to be 0 - 32767. */
  879. value = value / 2 + 16384;
  880. default:
  881. break;
  882. }
  883. return value;
  884. } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
  885. Uint8 value;
  886. value = SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis]);
  887. if (value > 0)
  888. return 32767;
  889. return 0;
  890. }
  891. return 0;
  892. }
  893. /*
  894. * Get the current state of a button on a controller
  895. */
  896. Uint8
  897. SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
  898. {
  899. if (!gamecontroller)
  900. return 0;
  901. if (gamecontroller->mapping.buttons[button] >= 0) {
  902. return (SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttons[button]));
  903. } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
  904. Sint16 value;
  905. value = SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button]);
  906. if (ABS(value) > 32768/2)
  907. return 1;
  908. return 0;
  909. } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
  910. Uint8 value;
  911. value = SDL_JoystickGetHat(gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat);
  912. if (value & gamecontroller->mapping.hatasbutton[button].mask)
  913. return 1;
  914. return 0;
  915. }
  916. return 0;
  917. }
  918. /*
  919. * Return if the joystick in question is currently attached to the system,
  920. * \return 0 if not plugged in, 1 if still present.
  921. */
  922. SDL_bool
  923. SDL_GameControllerGetAttached(SDL_GameController * gamecontroller)
  924. {
  925. if (!gamecontroller)
  926. return SDL_FALSE;
  927. return SDL_JoystickGetAttached(gamecontroller->joystick);
  928. }
  929. const char *
  930. SDL_GameControllerName(SDL_GameController * gamecontroller)
  931. {
  932. if (!gamecontroller)
  933. return NULL;
  934. return (gamecontroller->mapping.name);
  935. }
  936. /*
  937. * Get the joystick for this controller
  938. */
  939. SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
  940. {
  941. if (!gamecontroller)
  942. return NULL;
  943. return gamecontroller->joystick;
  944. }
  945. /*
  946. * Find the SDL_GameController that owns this instance id
  947. */
  948. SDL_GameController *
  949. SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
  950. {
  951. SDL_GameController *gamecontroller = SDL_gamecontrollers;
  952. while (gamecontroller) {
  953. if (gamecontroller->joystick->instance_id == joyid) {
  954. return gamecontroller;
  955. }
  956. gamecontroller = gamecontroller->next;
  957. }
  958. return NULL;
  959. }
  960. /**
  961. * Get the SDL joystick layer binding for this controller axis mapping
  962. */
  963. SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
  964. {
  965. SDL_GameControllerButtonBind bind;
  966. SDL_memset(&bind, 0x0, sizeof(bind));
  967. if (!gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID)
  968. return bind;
  969. if (gamecontroller->mapping.axes[axis] >= 0) {
  970. bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
  971. bind.value.button = gamecontroller->mapping.axes[axis];
  972. } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
  973. bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
  974. bind.value.button = gamecontroller->mapping.buttonasaxis[axis];
  975. }
  976. return bind;
  977. }
  978. /**
  979. * Get the SDL joystick layer binding for this controller button mapping
  980. */
  981. SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
  982. {
  983. SDL_GameControllerButtonBind bind;
  984. SDL_memset(&bind, 0x0, sizeof(bind));
  985. if (!gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID)
  986. return bind;
  987. if (gamecontroller->mapping.buttons[button] >= 0) {
  988. bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
  989. bind.value.button = gamecontroller->mapping.buttons[button];
  990. } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
  991. bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
  992. bind.value.axis = gamecontroller->mapping.axesasbutton[button];
  993. } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
  994. bind.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
  995. bind.value.hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
  996. bind.value.hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
  997. }
  998. return bind;
  999. }
  1000. void
  1001. SDL_GameControllerClose(SDL_GameController * gamecontroller)
  1002. {
  1003. SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
  1004. if (!gamecontroller)
  1005. return;
  1006. /* First decrement ref count */
  1007. if (--gamecontroller->ref_count > 0) {
  1008. return;
  1009. }
  1010. SDL_JoystickClose(gamecontroller->joystick);
  1011. gamecontrollerlist = SDL_gamecontrollers;
  1012. gamecontrollerlistprev = NULL;
  1013. while (gamecontrollerlist) {
  1014. if (gamecontroller == gamecontrollerlist) {
  1015. if (gamecontrollerlistprev) {
  1016. /* unlink this entry */
  1017. gamecontrollerlistprev->next = gamecontrollerlist->next;
  1018. } else {
  1019. SDL_gamecontrollers = gamecontroller->next;
  1020. }
  1021. break;
  1022. }
  1023. gamecontrollerlistprev = gamecontrollerlist;
  1024. gamecontrollerlist = gamecontrollerlist->next;
  1025. }
  1026. SDL_free(gamecontroller);
  1027. }
  1028. /*
  1029. * Quit the controller subsystem
  1030. */
  1031. void
  1032. SDL_GameControllerQuit(void)
  1033. {
  1034. ControllerMapping_t *pControllerMap;
  1035. while (SDL_gamecontrollers) {
  1036. SDL_gamecontrollers->ref_count = 1;
  1037. SDL_GameControllerClose(SDL_gamecontrollers);
  1038. }
  1039. while (s_pSupportedControllers) {
  1040. pControllerMap = s_pSupportedControllers;
  1041. s_pSupportedControllers = s_pSupportedControllers->next;
  1042. SDL_free(pControllerMap->name);
  1043. SDL_free(pControllerMap->mapping);
  1044. SDL_free(pControllerMap);
  1045. }
  1046. SDL_DelEventWatch(SDL_GameControllerEventWatcher, NULL);
  1047. }
  1048. /*
  1049. * Event filter to transform joystick events into appropriate game controller ones
  1050. */
  1051. int
  1052. SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
  1053. {
  1054. int posted;
  1055. /* translate the event, if desired */
  1056. posted = 0;
  1057. #if !SDL_EVENTS_DISABLED
  1058. if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
  1059. SDL_Event event;
  1060. event.type = SDL_CONTROLLERAXISMOTION;
  1061. event.caxis.which = gamecontroller->joystick->instance_id;
  1062. event.caxis.axis = axis;
  1063. event.caxis.value = value;
  1064. posted = SDL_PushEvent(&event) == 1;
  1065. }
  1066. #endif /* !SDL_EVENTS_DISABLED */
  1067. return (posted);
  1068. }
  1069. /*
  1070. * Event filter to transform joystick events into appropriate game controller ones
  1071. */
  1072. int
  1073. SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state)
  1074. {
  1075. int posted;
  1076. #if !SDL_EVENTS_DISABLED
  1077. SDL_Event event;
  1078. if (button == SDL_CONTROLLER_BUTTON_INVALID)
  1079. return (0);
  1080. switch (state) {
  1081. case SDL_PRESSED:
  1082. event.type = SDL_CONTROLLERBUTTONDOWN;
  1083. break;
  1084. case SDL_RELEASED:
  1085. event.type = SDL_CONTROLLERBUTTONUP;
  1086. break;
  1087. default:
  1088. /* Invalid state -- bail */
  1089. return (0);
  1090. }
  1091. #endif /* !SDL_EVENTS_DISABLED */
  1092. /* translate the event, if desired */
  1093. posted = 0;
  1094. #if !SDL_EVENTS_DISABLED
  1095. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1096. event.cbutton.which = gamecontroller->joystick->instance_id;
  1097. event.cbutton.button = button;
  1098. event.cbutton.state = state;
  1099. posted = SDL_PushEvent(&event) == 1;
  1100. }
  1101. #endif /* !SDL_EVENTS_DISABLED */
  1102. return (posted);
  1103. }
  1104. /*
  1105. * Turn off controller events
  1106. */
  1107. int
  1108. SDL_GameControllerEventState(int state)
  1109. {
  1110. #if SDL_EVENTS_DISABLED
  1111. return SDL_IGNORE;
  1112. #else
  1113. const Uint32 event_list[] = {
  1114. SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP,
  1115. SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED,
  1116. };
  1117. unsigned int i;
  1118. switch (state) {
  1119. case SDL_QUERY:
  1120. state = SDL_IGNORE;
  1121. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1122. state = SDL_EventState(event_list[i], SDL_QUERY);
  1123. if (state == SDL_ENABLE) {
  1124. break;
  1125. }
  1126. }
  1127. break;
  1128. default:
  1129. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1130. SDL_EventState(event_list[i], state);
  1131. }
  1132. break;
  1133. }
  1134. return (state);
  1135. #endif /* SDL_EVENTS_DISABLED */
  1136. }
  1137. /* vi: set ts=4 sw=4 expandtab: */