testgamecontroller.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. Copyright (C) 1997-2014 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. /* 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 512
  21. #define SCREEN_HEIGHT 317
  22. #endif
  23. static const char *
  24. ControllerAxisName(const SDL_GameControllerAxis axis)
  25. {
  26. switch (axis)
  27. {
  28. #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
  29. AXIS_CASE(INVALID);
  30. AXIS_CASE(LEFTX);
  31. AXIS_CASE(LEFTY);
  32. AXIS_CASE(RIGHTX);
  33. AXIS_CASE(RIGHTY);
  34. AXIS_CASE(TRIGGERLEFT);
  35. AXIS_CASE(TRIGGERRIGHT);
  36. #undef AXIS_CASE
  37. default: return "???";
  38. }
  39. }
  40. static const char *
  41. ControllerButtonName(const SDL_GameControllerButton button)
  42. {
  43. switch (button)
  44. {
  45. #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
  46. BUTTON_CASE(INVALID);
  47. BUTTON_CASE(A);
  48. BUTTON_CASE(B);
  49. BUTTON_CASE(X);
  50. BUTTON_CASE(Y);
  51. BUTTON_CASE(BACK);
  52. BUTTON_CASE(GUIDE);
  53. BUTTON_CASE(START);
  54. BUTTON_CASE(LEFTSTICK);
  55. BUTTON_CASE(RIGHTSTICK);
  56. BUTTON_CASE(LEFTSHOULDER);
  57. BUTTON_CASE(RIGHTSHOULDER);
  58. BUTTON_CASE(DPAD_UP);
  59. BUTTON_CASE(DPAD_DOWN);
  60. BUTTON_CASE(DPAD_LEFT);
  61. BUTTON_CASE(DPAD_RIGHT);
  62. #undef BUTTON_CASE
  63. default: return "???";
  64. }
  65. }
  66. static SDL_Texture *
  67. LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
  68. {
  69. SDL_Surface *temp = NULL;
  70. SDL_Texture *texture = NULL;
  71. temp = SDL_LoadBMP(file);
  72. if (temp == NULL) {
  73. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  74. } else {
  75. /* Set transparent pixel as the pixel at (0,0) */
  76. if (transparent) {
  77. SDL_assert(!temp->format->palette);
  78. SDL_assert(temp->format->BitsPerPixel == 24);
  79. SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  80. }
  81. texture = SDL_CreateTextureFromSurface(renderer, temp);
  82. if (!texture) {
  83. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  84. }
  85. }
  86. if (temp) {
  87. SDL_FreeSurface(temp);
  88. }
  89. return texture;
  90. }
  91. SDL_bool
  92. WatchGameController(SDL_GameController * gamecontroller)
  93. {
  94. /* This is indexed by SDL_GameControllerButton. */
  95. static const struct { int x; int y; } button_positions[] = {
  96. {387, 167}, /* A */
  97. {431, 132}, /* B */
  98. {342, 132}, /* X */
  99. {389, 101}, /* Y */
  100. {174, 132}, /* BACK */
  101. {233, 132}, /* GUIDE */
  102. {289, 132}, /* START */
  103. {75, 154}, /* LEFTSTICK */
  104. {305, 230}, /* RIGHTSTICK */
  105. {77, 40}, /* LEFTSHOULDER */
  106. {396, 36}, /* RIGHTSHOULDER */
  107. {154, 188}, /* DPAD_UP */
  108. {154, 249}, /* DPAD_DOWN */
  109. {116, 217}, /* DPAD_LEFT */
  110. {186, 217}, /* DPAD_RIGHT */
  111. };
  112. /* This is indexed by SDL_GameControllerAxis. */
  113. static const struct { int x; int y; double angle; } axis_positions[] = {
  114. {75, 154, 0.0}, /* LEFTX */
  115. {75, 154, 90.0}, /* LEFTY */
  116. {305, 230, 0.0}, /* RIGHTX */
  117. {305, 230, 90.0}, /* RIGHTY */
  118. {91, 0, 90.0}, /* TRIGGERLEFT */
  119. {375, 0, 90.0}, /* TRIGGERRIGHT */
  120. };
  121. const char *name = SDL_GameControllerName(gamecontroller);
  122. const char *basetitle = "Game Controller Test: ";
  123. const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
  124. char *title = (char *)SDL_malloc(titlelen);
  125. SDL_Texture *background, *button, *axis;
  126. SDL_Window *window = NULL;
  127. SDL_Renderer *screen = NULL;
  128. SDL_bool retval = SDL_FALSE;
  129. SDL_bool done = SDL_FALSE;
  130. SDL_Event event;
  131. int i;
  132. if (title) {
  133. SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
  134. }
  135. /* Create a window to display controller state */
  136. window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
  137. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  138. SCREEN_HEIGHT, 0);
  139. if (window == NULL) {
  140. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  141. return SDL_FALSE;
  142. }
  143. screen = SDL_CreateRenderer(window, -1, 0);
  144. if (screen == NULL) {
  145. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  146. SDL_DestroyWindow(window);
  147. return SDL_FALSE;
  148. }
  149. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  150. SDL_RenderClear(screen);
  151. SDL_RenderPresent(screen);
  152. SDL_RaiseWindow(window);
  153. /* scale for platforms that don't give you the window size you asked for. */
  154. SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
  155. background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
  156. button = LoadTexture(screen, "button.bmp", SDL_TRUE);
  157. axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
  158. if (!background || !button || !axis) {
  159. SDL_DestroyRenderer(screen);
  160. SDL_DestroyWindow(window);
  161. return SDL_FALSE;
  162. }
  163. SDL_SetTextureColorMod(button, 10, 255, 21);
  164. SDL_SetTextureColorMod(axis, 10, 255, 21);
  165. /* !!! FIXME: */
  166. /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
  167. /* Print info about the controller we are watching */
  168. SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
  169. /* Loop, getting controller events! */
  170. while (!done) {
  171. /* blank screen, set up for drawing this frame. */
  172. SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
  173. SDL_RenderClear(screen);
  174. SDL_RenderCopy(screen, background, NULL, NULL);
  175. while (SDL_PollEvent(&event)) {
  176. switch (event.type) {
  177. case SDL_KEYDOWN:
  178. if (event.key.keysym.sym != SDLK_ESCAPE) {
  179. break;
  180. }
  181. /* Fall through to signal quit */
  182. case SDL_QUIT:
  183. done = SDL_TRUE;
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. /* Update visual controller state */
  190. for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
  191. if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
  192. const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
  193. SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
  194. }
  195. }
  196. for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
  197. const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
  198. const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
  199. if (value < -deadzone) {
  200. const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
  201. const double angle = axis_positions[i].angle;
  202. SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
  203. } else if (value > deadzone) {
  204. const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
  205. const double angle = axis_positions[i].angle + 180.0;
  206. SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
  207. }
  208. }
  209. SDL_RenderPresent(screen);
  210. if (!SDL_GameControllerGetAttached(gamecontroller)) {
  211. done = SDL_TRUE;
  212. retval = SDL_TRUE; /* keep going, wait for reattach. */
  213. }
  214. }
  215. SDL_DestroyRenderer(screen);
  216. SDL_DestroyWindow(window);
  217. return retval;
  218. }
  219. int
  220. main(int argc, char *argv[])
  221. {
  222. int i;
  223. int nController = 0;
  224. int retcode = 0;
  225. char guid[64];
  226. SDL_GameController *gamecontroller;
  227. /* Enable standard application logging */
  228. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  229. /* Initialize SDL (Note: video is required to start event loop) */
  230. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
  231. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  232. return 1;
  233. }
  234. SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
  235. /* Print information about the controller */
  236. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  237. const char *name;
  238. const char *description;
  239. SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
  240. guid, sizeof (guid));
  241. if ( SDL_IsGameController(i) )
  242. {
  243. nController++;
  244. name = SDL_GameControllerNameForIndex(i);
  245. description = "Controller";
  246. } else {
  247. name = SDL_JoystickNameForIndex(i);
  248. description = "Joystick";
  249. }
  250. SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
  251. }
  252. SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
  253. if (argv[1]) {
  254. SDL_bool reportederror = SDL_FALSE;
  255. SDL_bool keepGoing = SDL_TRUE;
  256. SDL_Event event;
  257. int device = atoi(argv[1]);
  258. if (device >= SDL_NumJoysticks()) {
  259. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
  260. retcode = 1;
  261. } else {
  262. SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
  263. guid, sizeof (guid));
  264. SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
  265. gamecontroller = SDL_GameControllerOpen(device);
  266. while (keepGoing) {
  267. if (gamecontroller == NULL) {
  268. if (!reportederror) {
  269. if (gamecontroller == NULL) {
  270. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
  271. retcode = 1;
  272. }
  273. keepGoing = SDL_FALSE;
  274. reportederror = SDL_TRUE;
  275. }
  276. } else {
  277. reportederror = SDL_FALSE;
  278. keepGoing = WatchGameController(gamecontroller);
  279. SDL_GameControllerClose(gamecontroller);
  280. }
  281. gamecontroller = NULL;
  282. if (keepGoing) {
  283. SDL_Log("Waiting for attach\n");
  284. }
  285. while (keepGoing) {
  286. SDL_WaitEvent(&event);
  287. if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
  288. || (event.type == SDL_MOUSEBUTTONDOWN)) {
  289. keepGoing = SDL_FALSE;
  290. } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
  291. gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
  299. return retcode;
  300. }
  301. #else
  302. int
  303. main(int argc, char *argv[])
  304. {
  305. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  306. exit(1);
  307. }
  308. #endif