🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

826 lines
32 KiB

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