SDL_gamecontroller.c 37 KB

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