testgamecontroller.c 13 KB

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