SDL_gamecontroller.c 58 KB

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