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

333 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: %d\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 %d 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 %d 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 %d 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 %d 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 %d 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. /* Update visual joystick state */
  182. SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
  183. for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
  184. if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
  185. DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
  186. }
  187. }
  188. SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  189. for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
  190. /* Draw the X/Y axis */
  191. int x, y;
  192. x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  193. x *= SCREEN_WIDTH;
  194. x /= 65535;
  195. if (x < 0) {
  196. x = 0;
  197. } else if (x > (SCREEN_WIDTH - 16)) {
  198. x = SCREEN_WIDTH - 16;
  199. }
  200. ++i;
  201. if (i < SDL_JoystickNumAxes(joystick)) {
  202. y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  203. } else {
  204. y = 32768;
  205. }
  206. y *= SCREEN_HEIGHT;
  207. y /= 65535;
  208. if (y < 0) {
  209. y = 0;
  210. } else if (y > (SCREEN_HEIGHT - 16)) {
  211. y = SCREEN_HEIGHT - 16;
  212. }
  213. DrawRect(screen, x, y, 16, 16);
  214. }
  215. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
  216. for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
  217. /* Derive the new position */
  218. int x = SCREEN_WIDTH/2;
  219. int y = SCREEN_HEIGHT/2;
  220. const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
  221. if (hat_pos & SDL_HAT_UP) {
  222. y = 0;
  223. } else if (hat_pos & SDL_HAT_DOWN) {
  224. y = SCREEN_HEIGHT-8;
  225. }
  226. if (hat_pos & SDL_HAT_LEFT) {
  227. x = 0;
  228. } else if (hat_pos & SDL_HAT_RIGHT) {
  229. x = SCREEN_WIDTH-8;
  230. }
  231. DrawRect(screen, x, y, 8, 8);
  232. }
  233. }
  234. SDL_RenderPresent(screen);
  235. #ifdef __EMSCRIPTEN__
  236. if (done) {
  237. emscripten_cancel_main_loop();
  238. }
  239. #endif
  240. }
  241. int
  242. main(int argc, char *argv[])
  243. {
  244. SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
  245. /* Enable standard application logging */
  246. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  247. /* Initialize SDL (Note: video is required to start event loop) */
  248. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  249. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  250. exit(1);
  251. }
  252. /* Create a window to display joystick axis position */
  253. window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
  254. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  255. SCREEN_HEIGHT, 0);
  256. if (window == NULL) {
  257. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  258. return SDL_FALSE;
  259. }
  260. screen = SDL_CreateRenderer(window, -1, 0);
  261. if (screen == NULL) {
  262. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  263. SDL_DestroyWindow(window);
  264. return SDL_FALSE;
  265. }
  266. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  267. SDL_RenderClear(screen);
  268. SDL_RenderPresent(screen);
  269. /* Loop, getting joystick events! */
  270. #ifdef __EMSCRIPTEN__
  271. emscripten_set_main_loop_arg(loop, NULL, 0, 1);
  272. #else
  273. while (!done) {
  274. loop(NULL);
  275. }
  276. #endif
  277. SDL_DestroyRenderer(screen);
  278. SDL_DestroyWindow(window);
  279. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
  280. return 0;
  281. }
  282. #else
  283. int
  284. main(int argc, char *argv[])
  285. {
  286. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  287. return 1;
  288. }
  289. #endif
  290. /* vi: set ts=4 sw=4 expandtab: */