testjoystick.c 10 KB

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