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

340 lines
10 KiB

  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 <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 320
  21. #define SCREEN_HEIGHT 480
  22. #else
  23. #define SCREEN_WIDTH 640
  24. #define SCREEN_HEIGHT 480
  25. #endif
  26. static SDL_Window *window = NULL;
  27. static SDL_Renderer *screen = NULL;
  28. static SDL_Joystick *joystick = NULL;
  29. static SDL_bool done = SDL_FALSE;
  30. static void
  31. PrintJoystick(SDL_Joystick *joy)
  32. {
  33. const char *type;
  34. char guid[64];
  35. SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joy)) == joy);
  36. SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, sizeof (guid));
  37. switch (SDL_JoystickGetType(joy)) {
  38. case SDL_JOYSTICK_TYPE_GAMECONTROLLER:
  39. type = "Game Controller";
  40. break;
  41. case SDL_JOYSTICK_TYPE_WHEEL:
  42. type = "Wheel";
  43. break;
  44. case SDL_JOYSTICK_TYPE_ARCADE_STICK:
  45. type = "Arcade Stick";
  46. break;
  47. case SDL_JOYSTICK_TYPE_FLIGHT_STICK:
  48. type = "Flight Stick";
  49. break;
  50. case SDL_JOYSTICK_TYPE_DANCE_PAD:
  51. type = "Dance Pad";
  52. break;
  53. case SDL_JOYSTICK_TYPE_GUITAR:
  54. type = "Guitar";
  55. break;
  56. case SDL_JOYSTICK_TYPE_DRUM_KIT:
  57. type = "Drum Kit";
  58. break;
  59. case SDL_JOYSTICK_TYPE_ARCADE_PAD:
  60. type = "Arcade Pad";
  61. break;
  62. case SDL_JOYSTICK_TYPE_THROTTLE:
  63. type = "Throttle";
  64. break;
  65. default:
  66. type = "Unknown";
  67. break;
  68. }
  69. SDL_Log("Joystick\n");
  70. SDL_Log(" name: %s\n", SDL_JoystickName(joy));
  71. SDL_Log(" type: %s\n", type);
  72. SDL_Log(" LED: %s\n", SDL_JoystickHasLED(joy) ? "yes" : "no");
  73. SDL_Log(" rumble: %s\n", SDL_JoystickHasRumble(joy) ? "yes" : "no");
  74. SDL_Log("trigger rumble: %s\n", SDL_JoystickHasRumbleTriggers(joy) ? "yes" : "no");
  75. SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joy));
  76. SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joy));
  77. SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joy));
  78. SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joy));
  79. SDL_Log(" instance id: %" SDL_PRIs32 "\n", SDL_JoystickInstanceID(joy));
  80. SDL_Log(" guid: %s\n", guid);
  81. SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joy), SDL_JoystickGetProduct(joy));
  82. }
  83. static void
  84. DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
  85. {
  86. SDL_Rect area;
  87. area.x = x;
  88. area.y = y;
  89. area.w = w;
  90. area.h = h;
  91. SDL_RenderFillRect(r, &area);
  92. }
  93. void
  94. loop(void *arg)
  95. {
  96. SDL_Event event;
  97. int i;
  98. /* blank screen, set up for drawing this frame. */
  99. SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE);
  100. SDL_RenderClear(screen);
  101. while (SDL_PollEvent(&event)) {
  102. switch (event.type) {
  103. case SDL_JOYDEVICEADDED:
  104. SDL_Log("Joystick device %d added.\n", (int) event.jdevice.which);
  105. if (!joystick) {
  106. joystick = SDL_JoystickOpen(event.jdevice.which);
  107. if (joystick) {
  108. PrintJoystick(joystick);
  109. } else {
  110. SDL_Log("Couldn't open joystick: %s\n", SDL_GetError());
  111. }
  112. }
  113. break;
  114. case SDL_JOYDEVICEREMOVED:
  115. SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
  116. if (event.jdevice.which == SDL_JoystickInstanceID(joystick)) {
  117. SDL_JoystickClose(joystick);
  118. joystick = SDL_JoystickOpen(0);
  119. }
  120. break;
  121. case SDL_JOYAXISMOTION:
  122. SDL_Log("Joystick %" SDL_PRIs32 " axis %d value: %d\n",
  123. event.jaxis.which,
  124. event.jaxis.axis, event.jaxis.value);
  125. break;
  126. case SDL_JOYHATMOTION:
  127. SDL_Log("Joystick %" SDL_PRIs32 " hat %d value:",
  128. event.jhat.which, event.jhat.hat);
  129. if (event.jhat.value == SDL_HAT_CENTERED)
  130. SDL_Log(" centered");
  131. if (event.jhat.value & SDL_HAT_UP)
  132. SDL_Log(" up");
  133. if (event.jhat.value & SDL_HAT_RIGHT)
  134. SDL_Log(" right");
  135. if (event.jhat.value & SDL_HAT_DOWN)
  136. SDL_Log(" down");
  137. if (event.jhat.value & SDL_HAT_LEFT)
  138. SDL_Log(" left");
  139. SDL_Log("\n");
  140. break;
  141. case SDL_JOYBALLMOTION:
  142. SDL_Log("Joystick %" SDL_PRIs32 " ball %d delta: (%d,%d)\n",
  143. event.jball.which,
  144. event.jball.ball, event.jball.xrel, event.jball.yrel);
  145. break;
  146. case SDL_JOYBUTTONDOWN:
  147. SDL_Log("Joystick %" SDL_PRIs32 " button %d down\n",
  148. event.jbutton.which, event.jbutton.button);
  149. /* First button triggers a 0.5 second full strength rumble */
  150. if (event.jbutton.button == 0) {
  151. SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500);
  152. }
  153. break;
  154. case SDL_JOYBUTTONUP:
  155. SDL_Log("Joystick %" SDL_PRIs32 " button %d up\n",
  156. event.jbutton.which, event.jbutton.button);
  157. break;
  158. case SDL_KEYDOWN:
  159. /* Press the L key to lag for 3 seconds, to see what happens
  160. when SDL doesn't service the event loop quickly. */
  161. if (event.key.keysym.sym == SDLK_l) {
  162. SDL_Log("Lagging for 3 seconds...\n");
  163. SDL_Delay(3000);
  164. break;
  165. }
  166. if ((event.key.keysym.sym != SDLK_ESCAPE) &&
  167. (event.key.keysym.sym != SDLK_AC_BACK)) {
  168. break;
  169. }
  170. SDL_FALLTHROUGH;
  171. case SDL_FINGERDOWN:
  172. case SDL_MOUSEBUTTONDOWN:
  173. case SDL_QUIT:
  174. done = SDL_TRUE;
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. if (joystick) {
  181. const int BUTTONS_PER_LINE = ((SCREEN_WIDTH - 4) / 34);
  182. int x, y;
  183. /* Update visual joystick state */
  184. SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
  185. y = SCREEN_HEIGHT - ((((SDL_JoystickNumButtons(joystick) + (BUTTONS_PER_LINE - 1)) / BUTTONS_PER_LINE) + 1) * 34);
  186. for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
  187. if ((i % BUTTONS_PER_LINE) == 0) {
  188. y += 34;
  189. }
  190. if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
  191. x = 2 + (i % BUTTONS_PER_LINE) * 34;
  192. DrawRect(screen, x, y, 32, 32);
  193. }
  194. }
  195. SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  196. for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
  197. /* Draw the X/Y axis */
  198. x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  199. x *= SCREEN_WIDTH;
  200. x /= 65535;
  201. if (x < 0) {
  202. x = 0;
  203. } else if (x > (SCREEN_WIDTH - 16)) {
  204. x = SCREEN_WIDTH - 16;
  205. }
  206. ++i;
  207. if (i < SDL_JoystickNumAxes(joystick)) {
  208. y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  209. } else {
  210. y = 32768;
  211. }
  212. y *= SCREEN_HEIGHT;
  213. y /= 65535;
  214. if (y < 0) {
  215. y = 0;
  216. } else if (y > (SCREEN_HEIGHT - 16)) {
  217. y = SCREEN_HEIGHT - 16;
  218. }
  219. DrawRect(screen, x, y, 16, 16);
  220. }
  221. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
  222. for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
  223. /* Derive the new position */
  224. const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
  225. x = SCREEN_WIDTH/2;
  226. y = SCREEN_HEIGHT/2;
  227. if (hat_pos & SDL_HAT_UP) {
  228. y = 0;
  229. } else if (hat_pos & SDL_HAT_DOWN) {
  230. y = SCREEN_HEIGHT-8;
  231. }
  232. if (hat_pos & SDL_HAT_LEFT) {
  233. x = 0;
  234. } else if (hat_pos & SDL_HAT_RIGHT) {
  235. x = SCREEN_WIDTH-8;
  236. }
  237. DrawRect(screen, x, y, 8, 8);
  238. }
  239. }
  240. SDL_Delay(16);
  241. SDL_RenderPresent(screen);
  242. #ifdef __EMSCRIPTEN__
  243. if (done) {
  244. emscripten_cancel_main_loop();
  245. }
  246. #endif
  247. }
  248. int
  249. 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. #else
  290. int
  291. main(int argc, char *argv[])
  292. {
  293. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  294. return 1;
  295. }
  296. #endif
  297. /* vi: set ts=4 sw=4 expandtab: */