SDL_gamecontroller.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  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. #ifdef SDL_JOYSTICK_DINPUT
  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. #ifdef SDL_JOYSTICK_DINPUT
  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. #ifdef SDL_JOYSTICK_DINPUT
  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. #ifdef SDL_JOYSTICK_DINPUT
  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. #ifdef SDL_JOYSTICK_DINPUT
  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. while ( pUserMappings ) {
  741. char *pchNewLine = NULL;
  742. pchNewLine = SDL_strchr( pUserMappings, '\n' );
  743. if ( pchNewLine )
  744. *pchNewLine = '\0';
  745. SDL_GameControllerAddMapping( pUserMappings );
  746. if ( pchNewLine )
  747. pUserMappings = pchNewLine + 1;
  748. else
  749. pUserMappings = NULL;
  750. }
  751. SDL_free(pTempMappings);
  752. }
  753. }
  754. /*
  755. * Initialize the game controller system, mostly load our DB of controller config mappings
  756. */
  757. int
  758. SDL_GameControllerInit(void)
  759. {
  760. int i = 0;
  761. const char *pMappingString = NULL;
  762. s_pSupportedControllers = NULL;
  763. pMappingString = s_ControllerMappings[i];
  764. while ( pMappingString ) {
  765. SDL_GameControllerAddMapping( pMappingString );
  766. i++;
  767. pMappingString = s_ControllerMappings[i];
  768. }
  769. /* load in any user supplied config */
  770. SDL_GameControllerLoadHints();
  771. /* watch for joy events and fire controller ones if needed */
  772. SDL_AddEventWatch( SDL_GameControllerEventWatcher, NULL );
  773. /* Send added events for controllers currently attached */
  774. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  775. if (SDL_IsGameController(i)) {
  776. SDL_Event deviceevent;
  777. deviceevent.type = SDL_CONTROLLERDEVICEADDED;
  778. deviceevent.cdevice.which = i;
  779. SDL_PushEvent(&deviceevent);
  780. }
  781. }
  782. return (0);
  783. }
  784. /*
  785. * Get the implementation dependent name of a controller
  786. */
  787. const char *
  788. SDL_GameControllerNameForIndex(int device_index)
  789. {
  790. ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
  791. if ( pSupportedController )
  792. {
  793. return pSupportedController->name;
  794. }
  795. return NULL;
  796. }
  797. /*
  798. * Return 1 if the joystick at this device index is a supported controller
  799. */
  800. SDL_bool
  801. SDL_IsGameController(int device_index)
  802. {
  803. ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
  804. if ( pSupportedController )
  805. {
  806. return SDL_TRUE;
  807. }
  808. return SDL_FALSE;
  809. }
  810. /*
  811. * Open a controller for use - the index passed as an argument refers to
  812. * the N'th controller on the system. This index is the value which will
  813. * identify this controller in future controller events.
  814. *
  815. * This function returns a controller identifier, or NULL if an error occurred.
  816. */
  817. SDL_GameController *
  818. SDL_GameControllerOpen(int device_index)
  819. {
  820. SDL_GameController *gamecontroller;
  821. SDL_GameController *gamecontrollerlist;
  822. ControllerMapping_t *pSupportedController = NULL;
  823. if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
  824. SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
  825. return (NULL);
  826. }
  827. gamecontrollerlist = SDL_gamecontrollers;
  828. /* If the controller is already open, return it */
  829. while ( gamecontrollerlist )
  830. {
  831. if ( SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id ) {
  832. gamecontroller = gamecontrollerlist;
  833. ++gamecontroller->ref_count;
  834. return (gamecontroller);
  835. }
  836. gamecontrollerlist = gamecontrollerlist->next;
  837. }
  838. /* Find a controller mapping */
  839. pSupportedController = SDL_PrivateGetControllerMapping(device_index);
  840. if ( !pSupportedController ) {
  841. SDL_SetError("Couldn't find mapping for device (%d)", device_index );
  842. return (NULL);
  843. }
  844. /* Create and initialize the joystick */
  845. gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
  846. if (gamecontroller == NULL) {
  847. SDL_OutOfMemory();
  848. return NULL;
  849. }
  850. SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
  851. gamecontroller->joystick = SDL_JoystickOpen(device_index);
  852. if ( !gamecontroller->joystick ) {
  853. SDL_free(gamecontroller);
  854. return NULL;
  855. }
  856. SDL_PrivateLoadButtonMapping( &gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping );
  857. /* Add joystick to list */
  858. ++gamecontroller->ref_count;
  859. /* Link the joystick in the list */
  860. gamecontroller->next = SDL_gamecontrollers;
  861. SDL_gamecontrollers = gamecontroller;
  862. SDL_SYS_JoystickUpdate( gamecontroller->joystick );
  863. return (gamecontroller);
  864. }
  865. /*
  866. * Manually pump for controller updates.
  867. */
  868. void
  869. SDL_GameControllerUpdate(void)
  870. {
  871. /* Just for API completeness; the joystick API does all the work. */
  872. SDL_JoystickUpdate();
  873. }
  874. /*
  875. * Get the current state of an axis control on a controller
  876. */
  877. Sint16
  878. SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
  879. {
  880. if ( !gamecontroller )
  881. return 0;
  882. if (gamecontroller->mapping.axes[axis] >= 0 )
  883. {
  884. Sint16 value = ( SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axes[axis]) );
  885. switch (axis)
  886. {
  887. case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
  888. case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
  889. /* Shift it to be 0 - 32767. */
  890. value = value / 2 + 16384;
  891. default:
  892. break;
  893. }
  894. return value;
  895. }
  896. else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
  897. {
  898. Uint8 value;
  899. value = SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis] );
  900. if ( value > 0 )
  901. return 32767;
  902. return 0;
  903. }
  904. return 0;
  905. }
  906. /*
  907. * Get the current state of a button on a controller
  908. */
  909. Uint8
  910. SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
  911. {
  912. if ( !gamecontroller )
  913. return 0;
  914. if ( gamecontroller->mapping.buttons[button] >= 0 )
  915. {
  916. return ( SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttons[button] ) );
  917. }
  918. else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
  919. {
  920. Sint16 value;
  921. value = SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button] );
  922. if ( ABS(value) > 32768/2 )
  923. return 1;
  924. return 0;
  925. }
  926. else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
  927. {
  928. Uint8 value;
  929. value = SDL_JoystickGetHat( gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat );
  930. if ( value & gamecontroller->mapping.hatasbutton[button].mask )
  931. return 1;
  932. return 0;
  933. }
  934. return 0;
  935. }
  936. /*
  937. * Return if the joystick in question is currently attached to the system,
  938. * \return 0 if not plugged in, 1 if still present.
  939. */
  940. SDL_bool
  941. SDL_GameControllerGetAttached( SDL_GameController * gamecontroller )
  942. {
  943. if ( !gamecontroller )
  944. return SDL_FALSE;
  945. return SDL_JoystickGetAttached(gamecontroller->joystick);
  946. }
  947. /*
  948. * Get the number of multi-dimensional axis controls on a joystick
  949. */
  950. const char *
  951. SDL_GameControllerName(SDL_GameController * gamecontroller)
  952. {
  953. if ( !gamecontroller )
  954. return NULL;
  955. return (gamecontroller->mapping.name);
  956. }
  957. /*
  958. * Get the joystick for this controller
  959. */
  960. SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
  961. {
  962. if ( !gamecontroller )
  963. return NULL;
  964. return gamecontroller->joystick;
  965. }
  966. /**
  967. * Get the SDL joystick layer binding for this controller axis mapping
  968. */
  969. SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
  970. {
  971. SDL_GameControllerButtonBind bind;
  972. SDL_memset( &bind, 0x0, sizeof(bind) );
  973. if ( !gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID )
  974. return bind;
  975. if (gamecontroller->mapping.axes[axis] >= 0 )
  976. {
  977. bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
  978. bind.value.button = gamecontroller->mapping.axes[axis];
  979. }
  980. else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
  981. {
  982. bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
  983. bind.value.button = gamecontroller->mapping.buttonasaxis[axis];
  984. }
  985. return bind;
  986. }
  987. /**
  988. * Get the SDL joystick layer binding for this controller button mapping
  989. */
  990. SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
  991. {
  992. SDL_GameControllerButtonBind bind;
  993. SDL_memset( &bind, 0x0, sizeof(bind) );
  994. if ( !gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID )
  995. return bind;
  996. if ( gamecontroller->mapping.buttons[button] >= 0 )
  997. {
  998. bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
  999. bind.value.button = gamecontroller->mapping.buttons[button];
  1000. }
  1001. else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
  1002. {
  1003. bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
  1004. bind.value.axis = gamecontroller->mapping.axesasbutton[button];
  1005. }
  1006. else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
  1007. {
  1008. bind.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
  1009. bind.value.hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
  1010. bind.value.hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
  1011. }
  1012. return bind;
  1013. }
  1014. /*
  1015. * Close a joystick previously opened with SDL_JoystickOpen()
  1016. */
  1017. void
  1018. SDL_GameControllerClose(SDL_GameController * gamecontroller)
  1019. {
  1020. SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
  1021. if ( !gamecontroller )
  1022. return;
  1023. /* First decrement ref count */
  1024. if (--gamecontroller->ref_count > 0) {
  1025. return;
  1026. }
  1027. SDL_JoystickClose( gamecontroller->joystick );
  1028. gamecontrollerlist = SDL_gamecontrollers;
  1029. gamecontrollerlistprev = NULL;
  1030. while ( gamecontrollerlist )
  1031. {
  1032. if (gamecontroller == gamecontrollerlist)
  1033. {
  1034. if ( gamecontrollerlistprev )
  1035. {
  1036. /* unlink this entry */
  1037. gamecontrollerlistprev->next = gamecontrollerlist->next;
  1038. }
  1039. else
  1040. {
  1041. SDL_gamecontrollers = gamecontroller->next;
  1042. }
  1043. break;
  1044. }
  1045. gamecontrollerlistprev = gamecontrollerlist;
  1046. gamecontrollerlist = gamecontrollerlist->next;
  1047. }
  1048. SDL_free(gamecontroller);
  1049. }
  1050. /*
  1051. * Quit the controller subsystem
  1052. */
  1053. void
  1054. SDL_GameControllerQuit(void)
  1055. {
  1056. ControllerMapping_t *pControllerMap;
  1057. while ( SDL_gamecontrollers )
  1058. {
  1059. SDL_gamecontrollers->ref_count = 1;
  1060. SDL_GameControllerClose(SDL_gamecontrollers);
  1061. }
  1062. while ( s_pSupportedControllers )
  1063. {
  1064. pControllerMap = s_pSupportedControllers;
  1065. s_pSupportedControllers = s_pSupportedControllers->next;
  1066. SDL_free( pControllerMap->name );
  1067. SDL_free( pControllerMap );
  1068. }
  1069. SDL_DelEventWatch( SDL_GameControllerEventWatcher, NULL );
  1070. }
  1071. /*
  1072. * Event filter to transform joystick events into appropriate game controller ones
  1073. */
  1074. int
  1075. SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
  1076. {
  1077. int posted;
  1078. /* translate the event, if desired */
  1079. posted = 0;
  1080. #if !SDL_EVENTS_DISABLED
  1081. if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
  1082. SDL_Event event;
  1083. event.type = SDL_CONTROLLERAXISMOTION;
  1084. event.caxis.which = gamecontroller->joystick->instance_id;
  1085. event.caxis.axis = axis;
  1086. event.caxis.value = value;
  1087. posted = SDL_PushEvent(&event) == 1;
  1088. }
  1089. #endif /* !SDL_EVENTS_DISABLED */
  1090. return (posted);
  1091. }
  1092. /*
  1093. * Event filter to transform joystick events into appropriate game controller ones
  1094. */
  1095. int
  1096. SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state)
  1097. {
  1098. int posted;
  1099. #if !SDL_EVENTS_DISABLED
  1100. SDL_Event event;
  1101. if ( button == SDL_CONTROLLER_BUTTON_INVALID )
  1102. return (0);
  1103. switch (state) {
  1104. case SDL_PRESSED:
  1105. event.type = SDL_CONTROLLERBUTTONDOWN;
  1106. break;
  1107. case SDL_RELEASED:
  1108. event.type = SDL_CONTROLLERBUTTONUP;
  1109. break;
  1110. default:
  1111. /* Invalid state -- bail */
  1112. return (0);
  1113. }
  1114. #endif /* !SDL_EVENTS_DISABLED */
  1115. /* translate the event, if desired */
  1116. posted = 0;
  1117. #if !SDL_EVENTS_DISABLED
  1118. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1119. event.cbutton.which = gamecontroller->joystick->instance_id;
  1120. event.cbutton.button = button;
  1121. event.cbutton.state = state;
  1122. posted = SDL_PushEvent(&event) == 1;
  1123. }
  1124. #endif /* !SDL_EVENTS_DISABLED */
  1125. return (posted);
  1126. }
  1127. /*
  1128. * Turn off controller events
  1129. */
  1130. int
  1131. SDL_GameControllerEventState(int state)
  1132. {
  1133. #if SDL_EVENTS_DISABLED
  1134. return SDL_IGNORE;
  1135. #else
  1136. const Uint32 event_list[] = {
  1137. SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP,
  1138. SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED,
  1139. };
  1140. unsigned int i;
  1141. switch (state) {
  1142. case SDL_QUERY:
  1143. state = SDL_IGNORE;
  1144. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1145. state = SDL_EventState(event_list[i], SDL_QUERY);
  1146. if (state == SDL_ENABLE) {
  1147. break;
  1148. }
  1149. }
  1150. break;
  1151. default:
  1152. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  1153. SDL_EventState(event_list[i], state);
  1154. }
  1155. break;
  1156. }
  1157. return (state);
  1158. #endif /* SDL_EVENTS_DISABLED */
  1159. }
  1160. /* vi: set ts=4 sw=4 expandtab: */