🛠️🐜 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.

375 lines
12 KiB

  1. /*
  2. Copyright (C) 1997-2019 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. /* First button triggers a 0.5 second full strength rumble */
  101. if (event.type == SDL_CONTROLLERBUTTONDOWN &&
  102. event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
  103. SDL_GameControllerRumble(gamecontroller, 0xFFFF, 0xFFFF, 500);
  104. }
  105. break;
  106. case SDL_KEYDOWN:
  107. if (event.key.keysym.sym != SDLK_ESCAPE) {
  108. break;
  109. }
  110. /* Fall through to signal quit */
  111. case SDL_QUIT:
  112. done = SDL_TRUE;
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. /* Update visual controller state */
  119. for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
  120. if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
  121. const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
  122. SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, SDL_FLIP_NONE);
  123. }
  124. }
  125. for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
  126. const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
  127. const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
  128. if (value < -deadzone) {
  129. const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
  130. const double angle = axis_positions[i].angle;
  131. SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  132. } else if (value > deadzone) {
  133. const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
  134. const double angle = axis_positions[i].angle + 180.0;
  135. SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  136. }
  137. }
  138. SDL_RenderPresent(screen);
  139. if (!SDL_GameControllerGetAttached(gamecontroller)) {
  140. done = SDL_TRUE;
  141. retval = SDL_TRUE; /* keep going, wait for reattach. */
  142. }
  143. #ifdef __EMSCRIPTEN__
  144. if (done) {
  145. emscripten_cancel_main_loop();
  146. }
  147. #endif
  148. }
  149. SDL_bool
  150. WatchGameController(SDL_GameController * gamecontroller)
  151. {
  152. const char *name = SDL_GameControllerName(gamecontroller);
  153. const char *basetitle = "Game Controller Test: ";
  154. const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
  155. char *title = (char *)SDL_malloc(titlelen);
  156. SDL_Window *window = NULL;
  157. retval = SDL_FALSE;
  158. done = SDL_FALSE;
  159. if (title) {
  160. SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
  161. }
  162. /* Create a window to display controller state */
  163. window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
  164. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  165. SCREEN_HEIGHT, 0);
  166. SDL_free(title);
  167. title = NULL;
  168. if (window == NULL) {
  169. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  170. return SDL_FALSE;
  171. }
  172. screen = SDL_CreateRenderer(window, -1, 0);
  173. if (screen == NULL) {
  174. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  175. SDL_DestroyWindow(window);
  176. return SDL_FALSE;
  177. }
  178. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  179. SDL_RenderClear(screen);
  180. SDL_RenderPresent(screen);
  181. SDL_RaiseWindow(window);
  182. /* scale for platforms that don't give you the window size you asked for. */
  183. SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
  184. background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
  185. button = LoadTexture(screen, "button.bmp", SDL_TRUE);
  186. axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
  187. if (!background || !button || !axis) {
  188. SDL_DestroyRenderer(screen);
  189. SDL_DestroyWindow(window);
  190. return SDL_FALSE;
  191. }
  192. SDL_SetTextureColorMod(button, 10, 255, 21);
  193. SDL_SetTextureColorMod(axis, 10, 255, 21);
  194. /* !!! FIXME: */
  195. /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
  196. /* Print info about the controller we are watching */
  197. SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
  198. /* Loop, getting controller events! */
  199. #ifdef __EMSCRIPTEN__
  200. emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
  201. #else
  202. while (!done) {
  203. loop(gamecontroller);
  204. }
  205. #endif
  206. SDL_DestroyRenderer(screen);
  207. screen = NULL;
  208. background = NULL;
  209. button = NULL;
  210. axis = NULL;
  211. SDL_DestroyWindow(window);
  212. return retval;
  213. }
  214. int
  215. main(int argc, char *argv[])
  216. {
  217. int i;
  218. int nController = 0;
  219. int retcode = 0;
  220. char guid[64];
  221. SDL_GameController *gamecontroller;
  222. /* Enable standard application logging */
  223. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  224. /* Initialize SDL (Note: video is required to start event loop) */
  225. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
  226. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  227. return 1;
  228. }
  229. SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
  230. /* Print information about the mappings */
  231. if (!argv[1]) {
  232. SDL_Log("Supported mappings:\n");
  233. for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
  234. char *mapping = SDL_GameControllerMappingForIndex(i);
  235. if (mapping) {
  236. SDL_Log("\t%s\n", mapping);
  237. SDL_free(mapping);
  238. }
  239. }
  240. SDL_Log("\n");
  241. }
  242. /* Print information about the controller */
  243. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  244. const char *name;
  245. const char *description;
  246. SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
  247. guid, sizeof (guid));
  248. if ( SDL_IsGameController(i) )
  249. {
  250. nController++;
  251. name = SDL_GameControllerNameForIndex(i);
  252. description = "Controller";
  253. } else {
  254. name = SDL_JoystickNameForIndex(i);
  255. description = "Joystick";
  256. }
  257. SDL_Log("%s %d: %s (guid %s, VID 0x%.4x, PID 0x%.4x)\n",
  258. description, i, name ? name : "Unknown", guid,
  259. SDL_JoystickGetDeviceVendor(i), SDL_JoystickGetDeviceProduct(i));
  260. }
  261. SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
  262. if (argv[1]) {
  263. SDL_bool reportederror = SDL_FALSE;
  264. SDL_bool keepGoing = SDL_TRUE;
  265. SDL_Event event;
  266. int device = atoi(argv[1]);
  267. if (device >= SDL_NumJoysticks()) {
  268. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
  269. retcode = 1;
  270. } else {
  271. SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
  272. guid, sizeof (guid));
  273. SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
  274. gamecontroller = SDL_GameControllerOpen(device);
  275. if (gamecontroller != NULL) {
  276. SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
  277. }
  278. while (keepGoing) {
  279. if (gamecontroller == NULL) {
  280. if (!reportederror) {
  281. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
  282. retcode = 1;
  283. keepGoing = SDL_FALSE;
  284. reportederror = SDL_TRUE;
  285. }
  286. } else {
  287. reportederror = SDL_FALSE;
  288. keepGoing = WatchGameController(gamecontroller);
  289. SDL_GameControllerClose(gamecontroller);
  290. }
  291. gamecontroller = NULL;
  292. if (keepGoing) {
  293. SDL_Log("Waiting for attach\n");
  294. }
  295. while (keepGoing) {
  296. SDL_WaitEvent(&event);
  297. if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
  298. || (event.type == SDL_MOUSEBUTTONDOWN)) {
  299. keepGoing = SDL_FALSE;
  300. } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
  301. gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
  302. if (gamecontroller != NULL) {
  303. SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
  304. }
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
  312. return retcode;
  313. }
  314. #else
  315. int
  316. main(int argc, char *argv[])
  317. {
  318. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  319. exit(1);
  320. }
  321. #endif
  322. /* vi: set ts=4 sw=4 expandtab: */