controllermap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Game controller mapping generator */
  11. /* Gabriel Jacobo <gabomdq@gmail.com> */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "SDL.h"
  16. #ifndef SDL_JOYSTICK_DISABLED
  17. #ifdef __IPHONEOS__
  18. #define SCREEN_WIDTH 320
  19. #define SCREEN_HEIGHT 480
  20. #else
  21. #define SCREEN_WIDTH 512
  22. #define SCREEN_HEIGHT 320
  23. #endif
  24. #define MARKER_BUTTON 1
  25. #define MARKER_AXIS 2
  26. enum
  27. {
  28. SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE,
  29. SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE,
  30. SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE,
  31. SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE,
  32. SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE,
  33. SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE,
  34. SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE,
  35. SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE,
  36. SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT,
  37. SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT,
  38. SDL_CONTROLLER_BINDING_AXIS_MAX,
  39. };
  40. #define BINDING_COUNT (SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_MAX)
  41. static struct
  42. {
  43. int x, y;
  44. double angle;
  45. int marker;
  46. } s_arrBindingDisplay[BINDING_COUNT] = {
  47. { 387, 167, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_A */
  48. { 431, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_B */
  49. { 342, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_X */
  50. { 389, 101, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_Y */
  51. { 174, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_BACK */
  52. { 233, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_GUIDE */
  53. { 289, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_START */
  54. { 75, 154, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSTICK */
  55. { 305, 230, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSTICK */
  56. { 77, 40, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSHOULDER */
  57. { 396, 36, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSHOULDER */
  58. { 154, 188, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_UP */
  59. { 154, 249, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_DOWN */
  60. { 116, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_LEFT */
  61. { 186, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_RIGHT */
  62. { 74, 153, 270.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE */
  63. { 74, 153, 90.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE */
  64. { 74, 153, 0.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE */
  65. { 74, 153, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE */
  66. { 306, 231, 270.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE */
  67. { 306, 231, 90.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE */
  68. { 306, 231, 0.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE */
  69. { 306, 231, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE */
  70. { 91, -20, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT */
  71. { 375, -20, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT */
  72. };
  73. static int s_arrBindingOrder[BINDING_COUNT] = {
  74. SDL_CONTROLLER_BUTTON_A,
  75. SDL_CONTROLLER_BUTTON_B,
  76. SDL_CONTROLLER_BUTTON_Y,
  77. SDL_CONTROLLER_BUTTON_X,
  78. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE,
  79. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE,
  80. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE,
  81. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE,
  82. SDL_CONTROLLER_BUTTON_LEFTSTICK,
  83. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE,
  84. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE,
  85. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE,
  86. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE,
  87. SDL_CONTROLLER_BUTTON_RIGHTSTICK,
  88. SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
  89. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT,
  90. SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
  91. SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT,
  92. SDL_CONTROLLER_BUTTON_DPAD_UP,
  93. SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
  94. SDL_CONTROLLER_BUTTON_DPAD_DOWN,
  95. SDL_CONTROLLER_BUTTON_DPAD_LEFT,
  96. SDL_CONTROLLER_BUTTON_BACK,
  97. SDL_CONTROLLER_BUTTON_GUIDE,
  98. SDL_CONTROLLER_BUTTON_START,
  99. };
  100. typedef struct
  101. {
  102. SDL_GameControllerBindType bindType;
  103. union
  104. {
  105. int button;
  106. struct {
  107. int axis;
  108. int axis_min;
  109. int axis_max;
  110. } axis;
  111. struct {
  112. int hat;
  113. int hat_mask;
  114. } hat;
  115. } value;
  116. } SDL_GameControllerExtendedBind;
  117. static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT];
  118. typedef struct
  119. {
  120. SDL_bool m_bMoving;
  121. int m_nStartingValue;
  122. int m_nFarthestValue;
  123. } AxisState;
  124. static int s_nNumAxes;
  125. static AxisState *s_arrAxisState;
  126. static int s_iCurrentBinding;
  127. static Uint32 s_unPendingAdvanceTime;
  128. static SDL_bool s_bBindingComplete;
  129. SDL_Texture *
  130. LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
  131. {
  132. SDL_Surface *temp;
  133. SDL_Texture *texture;
  134. /* Load the sprite image */
  135. temp = SDL_LoadBMP(file);
  136. if (temp == NULL) {
  137. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  138. return NULL;
  139. }
  140. /* Set transparent pixel as the pixel at (0,0) */
  141. if (transparent) {
  142. if (temp->format->palette) {
  143. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  144. }
  145. }
  146. /* Create textures from the image */
  147. texture = SDL_CreateTextureFromSurface(renderer, temp);
  148. if (!texture) {
  149. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  150. SDL_FreeSurface(temp);
  151. return NULL;
  152. }
  153. SDL_FreeSurface(temp);
  154. /* We're ready to roll. :) */
  155. return texture;
  156. }
  157. static int
  158. StandardizeAxisValue(int nValue)
  159. {
  160. if (nValue > SDL_JOYSTICK_AXIS_MAX/2) {
  161. return SDL_JOYSTICK_AXIS_MAX;
  162. } else if (nValue < SDL_JOYSTICK_AXIS_MIN/2) {
  163. return SDL_JOYSTICK_AXIS_MIN;
  164. } else {
  165. return 0;
  166. }
  167. }
  168. static void
  169. SetCurrentBinding(int iBinding)
  170. {
  171. int iIndex;
  172. SDL_GameControllerExtendedBind *pBinding;
  173. if (iBinding < 0) {
  174. return;
  175. }
  176. if (iBinding == BINDING_COUNT) {
  177. s_bBindingComplete = SDL_TRUE;
  178. return;
  179. }
  180. s_iCurrentBinding = iBinding;
  181. pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
  182. SDL_zerop(pBinding);
  183. for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
  184. s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue;
  185. }
  186. s_unPendingAdvanceTime = 0;
  187. }
  188. static SDL_bool
  189. BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
  190. {
  191. if (pBindingA->bindType != pBindingB->bindType)
  192. {
  193. return SDL_FALSE;
  194. }
  195. switch (pBindingA->bindType)
  196. {
  197. case SDL_CONTROLLER_BINDTYPE_AXIS:
  198. if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
  199. return SDL_FALSE;
  200. }
  201. {
  202. int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
  203. int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
  204. int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
  205. int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
  206. return (minA <= minB && maxA >= maxB);
  207. }
  208. /* Not reached */
  209. default:
  210. return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
  211. }
  212. }
  213. static void
  214. ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
  215. {
  216. SDL_GameControllerExtendedBind *pCurrent;
  217. int iIndex;
  218. int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
  219. /* Do we already have this binding? */
  220. for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
  221. pCurrent = &s_arrBindings[iIndex];
  222. if (BBindingContainsBinding(pCurrent, pBinding)) {
  223. if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
  224. /* Skip to the next binding */
  225. SetCurrentBinding(s_iCurrentBinding + 1);
  226. return;
  227. }
  228. if (iIndex == SDL_CONTROLLER_BUTTON_B) {
  229. /* Go back to the previous binding */
  230. SetCurrentBinding(s_iCurrentBinding - 1);
  231. return;
  232. }
  233. /* Already have this binding, ignore it */
  234. return;
  235. }
  236. }
  237. /* Should the new binding override the existing one? */
  238. pCurrent = &s_arrBindings[iCurrentElement];
  239. if (pCurrent->bindType != SDL_CONTROLLER_BINDTYPE_NONE) {
  240. SDL_bool bNativeDPad, bCurrentDPad;
  241. SDL_bool bNativeAxis, bCurrentAxis;
  242. bNativeDPad = (iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_UP ||
  243. iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_DOWN ||
  244. iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_LEFT ||
  245. iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
  246. bCurrentDPad = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_HAT);
  247. if (bNativeDPad == bCurrentDPad) {
  248. /* We already have a binding of the type we want, ignore the new one */
  249. return;
  250. }
  251. bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
  252. bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
  253. if (bNativeAxis == bCurrentAxis &&
  254. (pBinding->bindType != SDL_CONTROLLER_BINDTYPE_AXIS ||
  255. pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
  256. /* We already have a binding of the type we want, ignore the new one */
  257. return;
  258. }
  259. }
  260. *pCurrent = *pBinding;
  261. s_unPendingAdvanceTime = SDL_GetTicks();
  262. }
  263. static SDL_bool
  264. BMergeAxisBindings(int iIndex)
  265. {
  266. SDL_GameControllerExtendedBind *pBindingA = &s_arrBindings[iIndex];
  267. SDL_GameControllerExtendedBind *pBindingB = &s_arrBindings[iIndex+1];
  268. if (pBindingA->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
  269. pBindingB->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
  270. pBindingA->value.axis.axis == pBindingB->value.axis.axis) {
  271. if (pBindingA->value.axis.axis_min == pBindingB->value.axis.axis_min) {
  272. pBindingA->value.axis.axis_min = pBindingA->value.axis.axis_max;
  273. pBindingA->value.axis.axis_max = pBindingB->value.axis.axis_max;
  274. pBindingB->bindType = SDL_CONTROLLER_BINDTYPE_NONE;
  275. return SDL_TRUE;
  276. }
  277. }
  278. return SDL_FALSE;
  279. }
  280. static void
  281. WatchJoystick(SDL_Joystick * joystick)
  282. {
  283. SDL_Window *window = NULL;
  284. SDL_Renderer *screen = NULL;
  285. SDL_Texture *background, *button, *axis, *marker;
  286. const char *name = NULL;
  287. SDL_bool done = SDL_FALSE;
  288. SDL_Event event;
  289. SDL_Rect dst;
  290. Uint8 alpha=200, alpha_step = -1;
  291. Uint32 alpha_ticks = 0;
  292. SDL_JoystickID nJoystickID;
  293. int iIndex;
  294. /* Create a window to display joystick axis position */
  295. window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
  296. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  297. SCREEN_HEIGHT, 0);
  298. if (window == NULL) {
  299. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  300. return;
  301. }
  302. screen = SDL_CreateRenderer(window, -1, 0);
  303. if (screen == NULL) {
  304. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  305. SDL_DestroyWindow(window);
  306. return;
  307. }
  308. background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
  309. button = LoadTexture(screen, "button.bmp", SDL_TRUE);
  310. axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
  311. SDL_RaiseWindow(window);
  312. /* scale for platforms that don't give you the window size you asked for. */
  313. SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
  314. /* Print info about the joystick we are watching */
  315. name = SDL_JoystickName(joystick);
  316. SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
  317. name ? name : "Unknown Joystick");
  318. SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
  319. SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
  320. SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
  321. SDL_Log("\n\n\
  322. ====================================================================================\n\
  323. Press the buttons on your controller when indicated\n\
  324. (Your controller may look different than the picture)\n\
  325. If you want to correct a mistake, press backspace or the back button on your device\n\
  326. To skip a button, press SPACE or click/touch the screen\n\
  327. To exit, press ESC\n\
  328. ====================================================================================\n");
  329. nJoystickID = SDL_JoystickInstanceID(joystick);
  330. s_nNumAxes = SDL_JoystickNumAxes(joystick);
  331. s_arrAxisState = SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
  332. for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
  333. AxisState *pAxisState = &s_arrAxisState[iIndex];
  334. Sint16 nInitialValue;
  335. pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, iIndex, &nInitialValue);
  336. pAxisState->m_nStartingValue = nInitialValue;
  337. pAxisState->m_nFarthestValue = nInitialValue;
  338. }
  339. /* Loop, getting joystick events! */
  340. while (!done && !s_bBindingComplete) {
  341. int iElement = s_arrBindingOrder[s_iCurrentBinding];
  342. switch (s_arrBindingDisplay[iElement].marker) {
  343. case MARKER_AXIS:
  344. marker = axis;
  345. break;
  346. case MARKER_BUTTON:
  347. marker = button;
  348. break;
  349. default:
  350. break;
  351. }
  352. dst.x = s_arrBindingDisplay[iElement].x;
  353. dst.y = s_arrBindingDisplay[iElement].y;
  354. SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
  355. if (SDL_GetTicks() - alpha_ticks > 5) {
  356. alpha_ticks = SDL_GetTicks();
  357. alpha += alpha_step;
  358. if (alpha == 255) {
  359. alpha_step = -1;
  360. }
  361. if (alpha < 128) {
  362. alpha_step = 1;
  363. }
  364. }
  365. SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
  366. SDL_RenderClear(screen);
  367. SDL_RenderCopy(screen, background, NULL, NULL);
  368. SDL_SetTextureAlphaMod(marker, alpha);
  369. SDL_SetTextureColorMod(marker, 10, 255, 21);
  370. SDL_RenderCopyEx(screen, marker, NULL, &dst, s_arrBindingDisplay[iElement].angle, NULL, SDL_FLIP_NONE);
  371. SDL_RenderPresent(screen);
  372. while (SDL_PollEvent(&event) > 0) {
  373. switch (event.type) {
  374. case SDL_JOYDEVICEREMOVED:
  375. if (event.jaxis.which == nJoystickID) {
  376. done = SDL_TRUE;
  377. }
  378. break;
  379. case SDL_JOYAXISMOTION:
  380. if (event.jaxis.which == nJoystickID) {
  381. AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
  382. int nValue = event.jaxis.value;
  383. int nCurrentDistance, nFarthestDistance;
  384. if (!pAxisState->m_bMoving) {
  385. pAxisState->m_bMoving = SDL_TRUE;
  386. pAxisState->m_nStartingValue = nValue;
  387. pAxisState->m_nFarthestValue = nValue;
  388. }
  389. nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
  390. nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
  391. if (nCurrentDistance > nFarthestDistance) {
  392. pAxisState->m_nFarthestValue = nValue;
  393. }
  394. if (nFarthestDistance >= 16000 && nCurrentDistance <= 10000) {
  395. /* We've gone out far enough and started to come back, let's bind this axis */
  396. SDL_GameControllerExtendedBind binding;
  397. SDL_zero(binding);
  398. binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
  399. binding.value.axis.axis = event.jaxis.axis;
  400. binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
  401. binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
  402. ConfigureBinding(&binding);
  403. }
  404. }
  405. break;
  406. case SDL_JOYHATMOTION:
  407. if (event.jhat.which == nJoystickID) {
  408. if (event.jhat.value != SDL_HAT_CENTERED) {
  409. SDL_GameControllerExtendedBind binding;
  410. SDL_zero(binding);
  411. binding.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
  412. binding.value.hat.hat = event.jhat.hat;
  413. binding.value.hat.hat_mask = event.jhat.value;
  414. ConfigureBinding(&binding);
  415. }
  416. }
  417. break;
  418. case SDL_JOYBALLMOTION:
  419. break;
  420. case SDL_JOYBUTTONDOWN:
  421. if (event.jbutton.which == nJoystickID) {
  422. SDL_GameControllerExtendedBind binding;
  423. SDL_zero(binding);
  424. binding.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
  425. binding.value.button = event.jbutton.button;
  426. ConfigureBinding(&binding);
  427. }
  428. break;
  429. case SDL_FINGERDOWN:
  430. case SDL_MOUSEBUTTONDOWN:
  431. /* Skip this step */
  432. SetCurrentBinding(s_iCurrentBinding + 1);
  433. break;
  434. case SDL_KEYDOWN:
  435. if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
  436. SetCurrentBinding(s_iCurrentBinding - 1);
  437. break;
  438. }
  439. if (event.key.keysym.sym == SDLK_SPACE) {
  440. SetCurrentBinding(s_iCurrentBinding + 1);
  441. break;
  442. }
  443. if ((event.key.keysym.sym != SDLK_ESCAPE)) {
  444. break;
  445. }
  446. /* Fall through to signal quit */
  447. case SDL_QUIT:
  448. done = SDL_TRUE;
  449. break;
  450. default:
  451. break;
  452. }
  453. }
  454. SDL_Delay(15);
  455. /* Wait 100 ms for joystick events to stop coming in,
  456. in case a controller sends multiple events for a single control (e.g. axis and button for trigger)
  457. */
  458. if (s_unPendingAdvanceTime && SDL_GetTicks() - s_unPendingAdvanceTime >= 100) {
  459. SetCurrentBinding(s_iCurrentBinding + 1);
  460. }
  461. }
  462. if (s_bBindingComplete) {
  463. char mapping[1024];
  464. char trimmed_name[128];
  465. char *spot;
  466. int iIndex;
  467. char pszElement[12];
  468. SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name));
  469. while (SDL_isspace(trimmed_name[0])) {
  470. SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name));
  471. }
  472. while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) {
  473. trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0';
  474. }
  475. while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) {
  476. SDL_memmove(spot, spot + 1, SDL_strlen(spot));
  477. }
  478. /* Initialize mapping with GUID and name */
  479. SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), mapping, SDL_arraysize(mapping));
  480. SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
  481. SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping));
  482. SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
  483. SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping));
  484. SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping));
  485. SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
  486. for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
  487. SDL_GameControllerExtendedBind *pBinding = &s_arrBindings[iIndex];
  488. if (pBinding->bindType == SDL_CONTROLLER_BINDTYPE_NONE) {
  489. continue;
  490. }
  491. if (iIndex < SDL_CONTROLLER_BUTTON_MAX) {
  492. SDL_GameControllerButton eButton = (SDL_GameControllerButton)iIndex;
  493. SDL_strlcat(mapping, SDL_GameControllerGetStringForButton(eButton), SDL_arraysize(mapping));
  494. } else {
  495. const char *pszAxisName;
  496. switch (iIndex - SDL_CONTROLLER_BUTTON_MAX) {
  497. case SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE:
  498. if (!BMergeAxisBindings(iIndex)) {
  499. SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
  500. }
  501. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
  502. break;
  503. case SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE:
  504. SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
  505. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX);
  506. break;
  507. case SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE:
  508. if (!BMergeAxisBindings(iIndex)) {
  509. SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
  510. }
  511. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
  512. break;
  513. case SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE:
  514. SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
  515. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY);
  516. break;
  517. case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE:
  518. if (!BMergeAxisBindings(iIndex)) {
  519. SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
  520. }
  521. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
  522. break;
  523. case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE:
  524. SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
  525. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX);
  526. break;
  527. case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE:
  528. if (!BMergeAxisBindings(iIndex)) {
  529. SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
  530. }
  531. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
  532. break;
  533. case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE:
  534. SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
  535. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY);
  536. break;
  537. case SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT:
  538. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
  539. break;
  540. case SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT:
  541. pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
  542. break;
  543. }
  544. SDL_strlcat(mapping, pszAxisName, SDL_arraysize(mapping));
  545. }
  546. SDL_strlcat(mapping, ":", SDL_arraysize(mapping));
  547. pszElement[0] = '\0';
  548. switch (pBinding->bindType) {
  549. case SDL_CONTROLLER_BINDTYPE_BUTTON:
  550. SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button);
  551. break;
  552. case SDL_CONTROLLER_BINDTYPE_AXIS:
  553. if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MIN) {
  554. /* The negative half axis */
  555. SDL_snprintf(pszElement, sizeof(pszElement), "-a%d", pBinding->value.axis.axis);
  556. } else if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MAX) {
  557. /* The positive half axis */
  558. SDL_snprintf(pszElement, sizeof(pszElement), "+a%d", pBinding->value.axis.axis);
  559. } else {
  560. SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis.axis);
  561. if (pBinding->value.axis.axis_min > pBinding->value.axis.axis_max) {
  562. /* Invert the axis */
  563. SDL_strlcat(pszElement, "~", SDL_arraysize(pszElement));
  564. }
  565. }
  566. break;
  567. case SDL_CONTROLLER_BINDTYPE_HAT:
  568. SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
  569. break;
  570. default:
  571. SDL_assert(!"Unknown bind type");
  572. break;
  573. }
  574. SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping));
  575. SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
  576. }
  577. SDL_Log("Mapping:\n\n%s\n\n", mapping);
  578. /* Print to stdout as well so the user can cat the output somewhere */
  579. printf("%s\n", mapping);
  580. }
  581. SDL_free(s_arrAxisState);
  582. s_arrAxisState = NULL;
  583. SDL_DestroyRenderer(screen);
  584. SDL_DestroyWindow(window);
  585. }
  586. int
  587. main(int argc, char *argv[])
  588. {
  589. const char *name;
  590. int i;
  591. SDL_Joystick *joystick;
  592. /* Enable standard application logging */
  593. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  594. /* Initialize SDL (Note: video is required to start event loop) */
  595. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  596. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  597. exit(1);
  598. }
  599. /* Print information about the joysticks */
  600. SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
  601. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  602. name = SDL_JoystickNameForIndex(i);
  603. SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
  604. joystick = SDL_JoystickOpen(i);
  605. if (joystick == NULL) {
  606. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
  607. SDL_GetError());
  608. } else {
  609. char guid[64];
  610. SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
  611. guid, sizeof (guid));
  612. SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
  613. SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
  614. SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
  615. SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
  616. SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
  617. SDL_Log(" guid: %s\n", guid);
  618. SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
  619. SDL_JoystickClose(joystick);
  620. }
  621. }
  622. #ifdef __ANDROID__
  623. if (SDL_NumJoysticks() > 0) {
  624. #else
  625. if (argv[1]) {
  626. #endif
  627. int device;
  628. #ifdef __ANDROID__
  629. device = 0;
  630. #else
  631. device = atoi(argv[1]);
  632. #endif
  633. joystick = SDL_JoystickOpen(device);
  634. if (joystick == NULL) {
  635. SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
  636. } else {
  637. WatchJoystick(joystick);
  638. SDL_JoystickClose(joystick);
  639. }
  640. }
  641. else {
  642. SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
  643. }
  644. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
  645. return 0;
  646. }
  647. #else
  648. int
  649. main(int argc, char *argv[])
  650. {
  651. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  652. exit(1);
  653. }
  654. #endif
  655. /* vi: set ts=4 sw=4 expandtab: */