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

326 lines
9.8 KiB

  1. /*
  2. Copyright (C) 1997-2020 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 *joystick)
  32. {
  33. const char *type;
  34. char guid[64];
  35. SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
  36. SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), guid, sizeof (guid));
  37. switch (SDL_JoystickGetType(joystick)) {
  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(joystick));
  71. SDL_Log(" type: %s\n", type);
  72. SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
  73. SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
  74. SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
  75. SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
  76. SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
  77. SDL_Log(" guid: %s\n", guid);
  78. SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
  79. }
  80. static void
  81. DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
  82. {
  83. const SDL_Rect area = { x, y, w, h };
  84. SDL_RenderFillRect(r, &area);
  85. }
  86. void
  87. loop(void *arg)
  88. {
  89. SDL_Event event;
  90. int i;
  91. /* blank screen, set up for drawing this frame. */
  92. SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE);
  93. SDL_RenderClear(screen);
  94. while (SDL_PollEvent(&event)) {
  95. switch (event.type) {
  96. case SDL_JOYDEVICEADDED:
  97. SDL_Log("Joystick device %d added.\n", (int) event.jdevice.which);
  98. if (!joystick) {
  99. joystick = SDL_JoystickOpen(event.jdevice.which);
  100. if (joystick) {
  101. PrintJoystick(joystick);
  102. } else {
  103. SDL_Log("Couldn't open joystick: %s\n", SDL_GetError());
  104. }
  105. }
  106. break;
  107. case SDL_JOYDEVICEREMOVED:
  108. SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
  109. if (event.jdevice.which == SDL_JoystickInstanceID(joystick)) {
  110. SDL_JoystickClose(joystick);
  111. joystick = SDL_JoystickOpen(0);
  112. }
  113. break;
  114. case SDL_JOYAXISMOTION:
  115. SDL_Log("Joystick %d axis %d value: %d\n",
  116. event.jaxis.which,
  117. event.jaxis.axis, event.jaxis.value);
  118. break;
  119. case SDL_JOYHATMOTION:
  120. SDL_Log("Joystick %d hat %d value:",
  121. event.jhat.which, event.jhat.hat);
  122. if (event.jhat.value == SDL_HAT_CENTERED)
  123. SDL_Log(" centered");
  124. if (event.jhat.value & SDL_HAT_UP)
  125. SDL_Log(" up");
  126. if (event.jhat.value & SDL_HAT_RIGHT)
  127. SDL_Log(" right");
  128. if (event.jhat.value & SDL_HAT_DOWN)
  129. SDL_Log(" down");
  130. if (event.jhat.value & SDL_HAT_LEFT)
  131. SDL_Log(" left");
  132. SDL_Log("\n");
  133. break;
  134. case SDL_JOYBALLMOTION:
  135. SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
  136. event.jball.which,
  137. event.jball.ball, event.jball.xrel, event.jball.yrel);
  138. break;
  139. case SDL_JOYBUTTONDOWN:
  140. SDL_Log("Joystick %d button %d down\n",
  141. event.jbutton.which, event.jbutton.button);
  142. /* First button triggers a 0.5 second full strength rumble */
  143. if (event.jbutton.button == 0) {
  144. SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500);
  145. }
  146. break;
  147. case SDL_JOYBUTTONUP:
  148. SDL_Log("Joystick %d button %d up\n",
  149. event.jbutton.which, event.jbutton.button);
  150. break;
  151. case SDL_KEYDOWN:
  152. /* Press the L key to lag for 3 seconds, to see what happens
  153. when SDL doesn't service the event loop quickly. */
  154. if (event.key.keysym.sym == SDLK_l) {
  155. SDL_Log("Lagging for 3 seconds...\n");
  156. SDL_Delay(3000);
  157. break;
  158. }
  159. if ((event.key.keysym.sym != SDLK_ESCAPE) &&
  160. (event.key.keysym.sym != SDLK_AC_BACK)) {
  161. break;
  162. }
  163. /* Fall through to signal quit */
  164. case SDL_FINGERDOWN:
  165. case SDL_MOUSEBUTTONDOWN:
  166. case SDL_QUIT:
  167. done = SDL_TRUE;
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. if (joystick) {
  174. /* Update visual joystick state */
  175. SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
  176. for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
  177. if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
  178. DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
  179. }
  180. }
  181. SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  182. for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
  183. /* Draw the X/Y axis */
  184. int x, y;
  185. x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  186. x *= SCREEN_WIDTH;
  187. x /= 65535;
  188. if (x < 0) {
  189. x = 0;
  190. } else if (x > (SCREEN_WIDTH - 16)) {
  191. x = SCREEN_WIDTH - 16;
  192. }
  193. ++i;
  194. if (i < SDL_JoystickNumAxes(joystick)) {
  195. y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  196. } else {
  197. y = 32768;
  198. }
  199. y *= SCREEN_HEIGHT;
  200. y /= 65535;
  201. if (y < 0) {
  202. y = 0;
  203. } else if (y > (SCREEN_HEIGHT - 16)) {
  204. y = SCREEN_HEIGHT - 16;
  205. }
  206. DrawRect(screen, x, y, 16, 16);
  207. }
  208. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
  209. for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
  210. /* Derive the new position */
  211. int x = SCREEN_WIDTH/2;
  212. int y = SCREEN_HEIGHT/2;
  213. const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
  214. if (hat_pos & SDL_HAT_UP) {
  215. y = 0;
  216. } else if (hat_pos & SDL_HAT_DOWN) {
  217. y = SCREEN_HEIGHT-8;
  218. }
  219. if (hat_pos & SDL_HAT_LEFT) {
  220. x = 0;
  221. } else if (hat_pos & SDL_HAT_RIGHT) {
  222. x = SCREEN_WIDTH-8;
  223. }
  224. DrawRect(screen, x, y, 8, 8);
  225. }
  226. }
  227. SDL_RenderPresent(screen);
  228. #ifdef __EMSCRIPTEN__
  229. if (done) {
  230. emscripten_cancel_main_loop();
  231. }
  232. #endif
  233. }
  234. int
  235. main(int argc, char *argv[])
  236. {
  237. SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
  238. /* Enable standard application logging */
  239. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  240. /* Initialize SDL (Note: video is required to start event loop) */
  241. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  242. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  243. exit(1);
  244. }
  245. /* Create a window to display joystick axis position */
  246. window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
  247. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  248. SCREEN_HEIGHT, 0);
  249. if (window == NULL) {
  250. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  251. return SDL_FALSE;
  252. }
  253. screen = SDL_CreateRenderer(window, -1, 0);
  254. if (screen == NULL) {
  255. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  256. SDL_DestroyWindow(window);
  257. return SDL_FALSE;
  258. }
  259. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  260. SDL_RenderClear(screen);
  261. SDL_RenderPresent(screen);
  262. /* Loop, getting joystick events! */
  263. #ifdef __EMSCRIPTEN__
  264. emscripten_set_main_loop_arg(loop, NULL, 0, 1);
  265. #else
  266. while (!done) {
  267. loop(NULL);
  268. }
  269. #endif
  270. SDL_DestroyRenderer(screen);
  271. SDL_DestroyWindow(window);
  272. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
  273. return 0;
  274. }
  275. #else
  276. int
  277. main(int argc, char *argv[])
  278. {
  279. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  280. return 1;
  281. }
  282. #endif
  283. /* vi: set ts=4 sw=4 expandtab: */