testgamecontroller.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  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. /* Simple program to test the SDL game controller routines */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "SDL.h"
  15. #ifndef SDL_JOYSTICK_DISABLED
  16. #ifdef __IPHONEOS__
  17. #define SCREEN_WIDTH 320
  18. #define SCREEN_HEIGHT 480
  19. #else
  20. #define SCREEN_WIDTH 640
  21. #define SCREEN_HEIGHT 480
  22. #endif
  23. #define MAX_NUM_AXES 6
  24. #define MAX_NUM_HATS 2
  25. static SDL_bool s_ForceQuit = SDL_FALSE;
  26. static void
  27. DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
  28. {
  29. const SDL_Rect area = { x, y, w, h };
  30. SDL_RenderFillRect(r, &area);
  31. }
  32. static const char *
  33. ControllerAxisName(const SDL_GameControllerAxis axis)
  34. {
  35. switch (axis)
  36. {
  37. #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
  38. AXIS_CASE(INVALID);
  39. AXIS_CASE(LEFTX);
  40. AXIS_CASE(LEFTY);
  41. AXIS_CASE(RIGHTX);
  42. AXIS_CASE(RIGHTY);
  43. AXIS_CASE(TRIGGERLEFT);
  44. AXIS_CASE(TRIGGERRIGHT);
  45. #undef AXIS_CASE
  46. default: return "???";
  47. }
  48. }
  49. static const char *
  50. ControllerButtonName(const SDL_GameControllerButton button)
  51. {
  52. switch (button)
  53. {
  54. #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
  55. BUTTON_CASE(INVALID);
  56. BUTTON_CASE(A);
  57. BUTTON_CASE(B);
  58. BUTTON_CASE(X);
  59. BUTTON_CASE(Y);
  60. BUTTON_CASE(BACK);
  61. BUTTON_CASE(GUIDE);
  62. BUTTON_CASE(START);
  63. BUTTON_CASE(LEFTSTICK);
  64. BUTTON_CASE(RIGHTSTICK);
  65. BUTTON_CASE(LEFTSHOULDER);
  66. BUTTON_CASE(RIGHTSHOULDER);
  67. BUTTON_CASE(DPAD_UP);
  68. BUTTON_CASE(DPAD_DOWN);
  69. BUTTON_CASE(DPAD_LEFT);
  70. BUTTON_CASE(DPAD_RIGHT);
  71. #undef BUTTON_CASE
  72. default: return "???";
  73. }
  74. }
  75. void
  76. WatchGameController(SDL_GameController * gamecontroller)
  77. {
  78. const char *name = SDL_GameControllerName(gamecontroller);
  79. const char *basetitle = "Game Controller Test: ";
  80. const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
  81. char *title = (char *)SDL_malloc(titlelen);
  82. SDL_Window *window = NULL;
  83. SDL_Renderer *screen = NULL;
  84. int done = 0;
  85. SDL_Event event;
  86. int i;
  87. if (title) {
  88. SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
  89. }
  90. /* Create a window to display controller axis position */
  91. window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
  92. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  93. SCREEN_HEIGHT, 0);
  94. if (window == NULL) {
  95. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  96. return;
  97. }
  98. screen = SDL_CreateRenderer(window, -1, 0);
  99. if (screen == NULL) {
  100. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  101. SDL_DestroyWindow(window);
  102. return;
  103. }
  104. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  105. SDL_RenderClear(screen);
  106. SDL_RenderPresent(screen);
  107. SDL_RaiseWindow(window);
  108. /* Print info about the controller we are watching */
  109. SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
  110. /* Loop, getting controller events! */
  111. while (!done) {
  112. /* blank screen, set up for drawing this frame. */
  113. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  114. SDL_RenderClear(screen);
  115. while (SDL_PollEvent(&event)) {
  116. switch (event.type) {
  117. case SDL_CONTROLLERAXISMOTION:
  118. SDL_Log("Controller %d axis %d ('%s') value: %d\n",
  119. event.caxis.which,
  120. event.caxis.axis,
  121. ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
  122. event.caxis.value);
  123. break;
  124. case SDL_CONTROLLERBUTTONDOWN:
  125. SDL_Log("Controller %d button %d ('%s') down\n",
  126. event.cbutton.which, event.cbutton.button,
  127. ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
  128. break;
  129. case SDL_CONTROLLERBUTTONUP:
  130. SDL_Log("Controller %d button %d ('%s') up\n",
  131. event.cbutton.which, event.cbutton.button,
  132. ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
  133. break;
  134. case SDL_KEYDOWN:
  135. if (event.key.keysym.sym != SDLK_ESCAPE) {
  136. break;
  137. }
  138. /* Fall through to signal quit */
  139. case SDL_QUIT:
  140. done = 1;
  141. s_ForceQuit = SDL_TRUE;
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. /* Update visual controller state */
  148. SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
  149. for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
  150. if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
  151. DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
  152. }
  153. }
  154. SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  155. for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
  156. /* Draw the X/Y axis */
  157. int x, y;
  158. x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
  159. x *= SCREEN_WIDTH;
  160. x /= 65535;
  161. if (x < 0) {
  162. x = 0;
  163. } else if (x > (SCREEN_WIDTH - 16)) {
  164. x = SCREEN_WIDTH - 16;
  165. }
  166. y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
  167. y *= SCREEN_HEIGHT;
  168. y /= 65535;
  169. if (y < 0) {
  170. y = 0;
  171. } else if (y > (SCREEN_HEIGHT - 16)) {
  172. y = SCREEN_HEIGHT - 16;
  173. }
  174. DrawRect(screen, x, y, 16, 16);
  175. }
  176. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
  177. SDL_RenderPresent(screen);
  178. if ( !done )
  179. done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
  180. }
  181. SDL_DestroyRenderer(screen);
  182. SDL_DestroyWindow(window);
  183. }
  184. int
  185. main(int argc, char *argv[])
  186. {
  187. int i;
  188. int nController = 0;
  189. int retcode = 0;
  190. char guid[64];
  191. SDL_GameController *gamecontroller;
  192. /* Enable standard application logging */
  193. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  194. /* Initialize SDL (Note: video is required to start event loop) */
  195. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
  196. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  197. return 1;
  198. }
  199. /* Print information about the controller */
  200. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  201. const char *name;
  202. const char *description;
  203. SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
  204. guid, sizeof (guid));
  205. if ( SDL_IsGameController(i) )
  206. {
  207. nController++;
  208. name = SDL_GameControllerNameForIndex(i);
  209. description = "Controller";
  210. } else {
  211. name = SDL_JoystickNameForIndex(i);
  212. description = "Joystick";
  213. }
  214. SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
  215. }
  216. SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
  217. if (argv[1]) {
  218. int device = atoi(argv[1]);
  219. if (device >= SDL_NumJoysticks()) {
  220. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
  221. retcode = 1;
  222. } else {
  223. SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
  224. guid, sizeof (guid));
  225. SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
  226. gamecontroller = SDL_GameControllerOpen(device);
  227. if (gamecontroller == NULL) {
  228. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
  229. retcode = 1;
  230. } else {
  231. WatchGameController(gamecontroller);
  232. SDL_GameControllerClose(gamecontroller);
  233. }
  234. }
  235. }
  236. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
  237. return retcode;
  238. }
  239. #else
  240. int
  241. main(int argc, char *argv[])
  242. {
  243. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  244. exit(1);
  245. }
  246. #endif