SDL_gamecontroller.c 40 KB

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