SDL_gamecontroller.c 59 KB

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