testjoystick.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. Copyright (C) 1997-2022 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 joystick routines */
  11. #include <stdlib.h>
  12. #include <SDL3/SDL.h>
  13. #include <SDL3/SDL_main.h>
  14. #ifdef __EMSCRIPTEN__
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #ifdef __IOS__
  18. #define SCREEN_WIDTH 320
  19. #define SCREEN_HEIGHT 480
  20. #else
  21. #define SCREEN_WIDTH 640
  22. #define SCREEN_HEIGHT 480
  23. #endif
  24. static SDL_Window *window = NULL;
  25. static SDL_Renderer *screen = NULL;
  26. static SDL_Joystick *joystick = NULL;
  27. static SDL_bool done = SDL_FALSE;
  28. static void
  29. PrintJoystick(SDL_Joystick *joy)
  30. {
  31. const char *type;
  32. char guid[64];
  33. SDL_assert(SDL_GetJoystickFromInstanceID(SDL_GetJoystickInstanceID(joy)) == joy);
  34. SDL_GetJoystickGUIDString(SDL_GetJoystickGUID(joy), guid, sizeof(guid));
  35. switch (SDL_GetJoystickType(joy)) {
  36. case SDL_JOYSTICK_TYPE_GAMEPAD:
  37. type = "Game Controller";
  38. break;
  39. case SDL_JOYSTICK_TYPE_WHEEL:
  40. type = "Wheel";
  41. break;
  42. case SDL_JOYSTICK_TYPE_ARCADE_STICK:
  43. type = "Arcade Stick";
  44. break;
  45. case SDL_JOYSTICK_TYPE_FLIGHT_STICK:
  46. type = "Flight Stick";
  47. break;
  48. case SDL_JOYSTICK_TYPE_DANCE_PAD:
  49. type = "Dance Pad";
  50. break;
  51. case SDL_JOYSTICK_TYPE_GUITAR:
  52. type = "Guitar";
  53. break;
  54. case SDL_JOYSTICK_TYPE_DRUM_KIT:
  55. type = "Drum Kit";
  56. break;
  57. case SDL_JOYSTICK_TYPE_ARCADE_PAD:
  58. type = "Arcade Pad";
  59. break;
  60. case SDL_JOYSTICK_TYPE_THROTTLE:
  61. type = "Throttle";
  62. break;
  63. default:
  64. type = "Unknown";
  65. break;
  66. }
  67. SDL_Log("Joystick\n");
  68. SDL_Log(" name: %s\n", SDL_GetJoystickName(joy));
  69. SDL_Log(" type: %s\n", type);
  70. SDL_Log(" LED: %s\n", SDL_JoystickHasLED(joy) ? "yes" : "no");
  71. SDL_Log(" rumble: %s\n", SDL_JoystickHasRumble(joy) ? "yes" : "no");
  72. SDL_Log("trigger rumble: %s\n", SDL_JoystickHasRumbleTriggers(joy) ? "yes" : "no");
  73. SDL_Log(" axes: %d\n", SDL_GetNumJoystickAxes(joy));
  74. SDL_Log(" hats: %d\n", SDL_GetNumJoystickHats(joy));
  75. SDL_Log(" buttons: %d\n", SDL_GetNumJoystickButtons(joy));
  76. SDL_Log(" instance id: %" SDL_PRIu32 "\n", SDL_GetJoystickInstanceID(joy));
  77. SDL_Log(" guid: %s\n", guid);
  78. SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_GetJoystickVendor(joy), SDL_GetJoystickProduct(joy));
  79. }
  80. static void
  81. DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
  82. {
  83. SDL_FRect area;
  84. area.x = (float)x;
  85. area.y = (float)y;
  86. area.w = (float)w;
  87. area.h = (float)h;
  88. SDL_RenderFillRect(r, &area);
  89. }
  90. void loop(void *arg)
  91. {
  92. SDL_Event event;
  93. int i;
  94. /* blank screen, set up for drawing this frame. */
  95. SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE);
  96. SDL_RenderClear(screen);
  97. while (SDL_PollEvent(&event)) {
  98. switch (event.type) {
  99. case SDL_JOYDEVICEADDED:
  100. SDL_Log("Joystick device %" SDL_PRIu32 " added.\n", event.jdevice.which);
  101. if (joystick == NULL) {
  102. joystick = SDL_OpenJoystick(event.jdevice.which);
  103. if (joystick) {
  104. PrintJoystick(joystick);
  105. } else {
  106. SDL_Log("Couldn't open joystick: %s\n", SDL_GetError());
  107. }
  108. }
  109. break;
  110. case SDL_JOYDEVICEREMOVED:
  111. SDL_Log("Joystick device %" SDL_PRIu32 " removed.\n", event.jdevice.which);
  112. if (event.jdevice.which == SDL_GetJoystickInstanceID(joystick)) {
  113. SDL_JoystickID *joysticks;
  114. SDL_CloseJoystick(joystick);
  115. joystick = NULL;
  116. joysticks = SDL_GetJoysticks(NULL);
  117. if (joysticks) {
  118. if (joysticks[0]) {
  119. joystick = SDL_OpenJoystick(joysticks[0]);
  120. if (joystick) {
  121. PrintJoystick(joystick);
  122. } else {
  123. SDL_Log("Couldn't open joystick: %s\n", SDL_GetError());
  124. }
  125. }
  126. SDL_free(joysticks);
  127. }
  128. }
  129. break;
  130. case SDL_JOYAXISMOTION:
  131. SDL_Log("Joystick %" SDL_PRIu32 " axis %d value: %d\n",
  132. event.jaxis.which,
  133. event.jaxis.axis, event.jaxis.value);
  134. break;
  135. case SDL_JOYHATMOTION:
  136. SDL_Log("Joystick %" SDL_PRIu32 " hat %d value:",
  137. event.jhat.which, event.jhat.hat);
  138. if (event.jhat.value == SDL_HAT_CENTERED) {
  139. SDL_Log(" centered");
  140. }
  141. if (event.jhat.value & SDL_HAT_UP) {
  142. SDL_Log(" up");
  143. }
  144. if (event.jhat.value & SDL_HAT_RIGHT) {
  145. SDL_Log(" right");
  146. }
  147. if (event.jhat.value & SDL_HAT_DOWN) {
  148. SDL_Log(" down");
  149. }
  150. if (event.jhat.value & SDL_HAT_LEFT) {
  151. SDL_Log(" left");
  152. }
  153. SDL_Log("\n");
  154. break;
  155. case SDL_JOYBUTTONDOWN:
  156. SDL_Log("Joystick %" SDL_PRIu32 " button %d down\n",
  157. event.jbutton.which, event.jbutton.button);
  158. /* First button triggers a 0.5 second full strength rumble */
  159. if (event.jbutton.button == 0) {
  160. SDL_RumbleJoystick(joystick, 0xFFFF, 0xFFFF, 500);
  161. }
  162. break;
  163. case SDL_JOYBUTTONUP:
  164. SDL_Log("Joystick %" SDL_PRIu32 " button %d up\n",
  165. event.jbutton.which, event.jbutton.button);
  166. break;
  167. case SDL_KEYDOWN:
  168. /* Press the L key to lag for 3 seconds, to see what happens
  169. when SDL doesn't service the event loop quickly. */
  170. if (event.key.keysym.sym == SDLK_l) {
  171. SDL_Log("Lagging for 3 seconds...\n");
  172. SDL_Delay(3000);
  173. break;
  174. }
  175. if ((event.key.keysym.sym != SDLK_ESCAPE) &&
  176. (event.key.keysym.sym != SDLK_AC_BACK)) {
  177. break;
  178. }
  179. SDL_FALLTHROUGH;
  180. case SDL_FINGERDOWN:
  181. case SDL_MOUSEBUTTONDOWN:
  182. case SDL_QUIT:
  183. done = SDL_TRUE;
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. if (joystick) {
  190. const int BUTTONS_PER_LINE = ((SCREEN_WIDTH - 4) / 34);
  191. int x, y;
  192. /* Update visual joystick state */
  193. SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
  194. y = SCREEN_HEIGHT - ((((SDL_GetNumJoystickButtons(joystick) + (BUTTONS_PER_LINE - 1)) / BUTTONS_PER_LINE) + 1) * 34);
  195. for (i = 0; i < SDL_GetNumJoystickButtons(joystick); ++i) {
  196. if ((i % BUTTONS_PER_LINE) == 0) {
  197. y += 34;
  198. }
  199. if (SDL_GetJoystickButton(joystick, i) == SDL_PRESSED) {
  200. x = 2 + (i % BUTTONS_PER_LINE) * 34;
  201. DrawRect(screen, x, y, 32, 32);
  202. }
  203. }
  204. SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  205. for (i = 0; i < SDL_GetNumJoystickAxes(joystick); ++i) {
  206. /* Draw the X/Y axis */
  207. x = (((int)SDL_GetJoystickAxis(joystick, i)) + 32768);
  208. x *= SCREEN_WIDTH;
  209. x /= 65535;
  210. if (x < 0) {
  211. x = 0;
  212. } else if (x > (SCREEN_WIDTH - 16)) {
  213. x = SCREEN_WIDTH - 16;
  214. }
  215. ++i;
  216. if (i < SDL_GetNumJoystickAxes(joystick)) {
  217. y = (((int)SDL_GetJoystickAxis(joystick, i)) + 32768);
  218. } else {
  219. y = 32768;
  220. }
  221. y *= SCREEN_HEIGHT;
  222. y /= 65535;
  223. if (y < 0) {
  224. y = 0;
  225. } else if (y > (SCREEN_HEIGHT - 16)) {
  226. y = SCREEN_HEIGHT - 16;
  227. }
  228. DrawRect(screen, x, y, 16, 16);
  229. }
  230. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
  231. for (i = 0; i < SDL_GetNumJoystickHats(joystick); ++i) {
  232. /* Derive the new position */
  233. const Uint8 hat_pos = SDL_GetJoystickHat(joystick, i);
  234. x = SCREEN_WIDTH / 2;
  235. y = SCREEN_HEIGHT / 2;
  236. if (hat_pos & SDL_HAT_UP) {
  237. y = 0;
  238. } else if (hat_pos & SDL_HAT_DOWN) {
  239. y = SCREEN_HEIGHT - 8;
  240. }
  241. if (hat_pos & SDL_HAT_LEFT) {
  242. x = 0;
  243. } else if (hat_pos & SDL_HAT_RIGHT) {
  244. x = SCREEN_WIDTH - 8;
  245. }
  246. DrawRect(screen, x, y, 8, 8);
  247. }
  248. }
  249. SDL_Delay(16);
  250. SDL_RenderPresent(screen);
  251. #ifdef __EMSCRIPTEN__
  252. if (done) {
  253. emscripten_cancel_main_loop();
  254. }
  255. #endif
  256. }
  257. int main(int argc, char *argv[])
  258. {
  259. SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
  260. /* Enable standard application logging */
  261. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  262. /* Initialize SDL (Note: video is required to start event loop) */
  263. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  264. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  265. exit(1);
  266. }
  267. /* Create a window to display joystick axis position */
  268. window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
  269. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  270. SCREEN_HEIGHT, 0);
  271. if (window == NULL) {
  272. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  273. return SDL_FALSE;
  274. }
  275. screen = SDL_CreateRenderer(window, NULL, 0);
  276. if (screen == NULL) {
  277. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  278. SDL_DestroyWindow(window);
  279. return SDL_FALSE;
  280. }
  281. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  282. SDL_RenderClear(screen);
  283. SDL_RenderPresent(screen);
  284. /* Loop, getting joystick events! */
  285. #ifdef __EMSCRIPTEN__
  286. emscripten_set_main_loop_arg(loop, NULL, 0, 1);
  287. #else
  288. while (!done) {
  289. loop(NULL);
  290. }
  291. #endif
  292. SDL_DestroyRenderer(screen);
  293. SDL_DestroyWindow(window);
  294. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
  295. return 0;
  296. }