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

387 lines
13 KiB

  1. /*
  2. Copyright (C) 1997-2019 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. SDL_Renderer *screen = NULL;
  27. SDL_bool retval = SDL_FALSE;
  28. SDL_bool done = SDL_FALSE;
  29. static void
  30. DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
  31. {
  32. const SDL_Rect area = { x, y, w, h };
  33. SDL_RenderFillRect(r, &area);
  34. }
  35. void
  36. loop(void *arg)
  37. {
  38. SDL_Event event;
  39. int i;
  40. SDL_Joystick *joystick = (SDL_Joystick *)arg;
  41. /* blank screen, set up for drawing this frame. */
  42. SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE);
  43. SDL_RenderClear(screen);
  44. while (SDL_PollEvent(&event)) {
  45. switch (event.type) {
  46. case SDL_JOYDEVICEREMOVED:
  47. SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
  48. SDL_Log("Our instance ID is %d\n", (int) SDL_JoystickInstanceID(joystick));
  49. break;
  50. case SDL_JOYAXISMOTION:
  51. SDL_Log("Joystick %d axis %d value: %d\n",
  52. event.jaxis.which,
  53. event.jaxis.axis, event.jaxis.value);
  54. break;
  55. case SDL_JOYHATMOTION:
  56. SDL_Log("Joystick %d hat %d value:",
  57. event.jhat.which, event.jhat.hat);
  58. if (event.jhat.value == SDL_HAT_CENTERED)
  59. SDL_Log(" centered");
  60. if (event.jhat.value & SDL_HAT_UP)
  61. SDL_Log(" up");
  62. if (event.jhat.value & SDL_HAT_RIGHT)
  63. SDL_Log(" right");
  64. if (event.jhat.value & SDL_HAT_DOWN)
  65. SDL_Log(" down");
  66. if (event.jhat.value & SDL_HAT_LEFT)
  67. SDL_Log(" left");
  68. SDL_Log("\n");
  69. break;
  70. case SDL_JOYBALLMOTION:
  71. SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
  72. event.jball.which,
  73. event.jball.ball, event.jball.xrel, event.jball.yrel);
  74. break;
  75. case SDL_JOYBUTTONDOWN:
  76. SDL_Log("Joystick %d button %d down\n",
  77. event.jbutton.which, event.jbutton.button);
  78. /* First button triggers a 0.5 second full strength rumble */
  79. if (event.jbutton.button == 0) {
  80. SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500);
  81. }
  82. break;
  83. case SDL_JOYBUTTONUP:
  84. SDL_Log("Joystick %d button %d up\n",
  85. event.jbutton.which, event.jbutton.button);
  86. break;
  87. case SDL_KEYDOWN:
  88. if ((event.key.keysym.sym != SDLK_ESCAPE) &&
  89. (event.key.keysym.sym != SDLK_AC_BACK)) {
  90. break;
  91. }
  92. /* Fall through to signal quit */
  93. case SDL_FINGERDOWN:
  94. case SDL_MOUSEBUTTONDOWN:
  95. case SDL_QUIT:
  96. done = SDL_TRUE;
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. /* Update visual joystick state */
  103. SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
  104. for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
  105. if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
  106. DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
  107. }
  108. }
  109. SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  110. for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
  111. /* Draw the X/Y axis */
  112. int x, y;
  113. x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  114. x *= SCREEN_WIDTH;
  115. x /= 65535;
  116. if (x < 0) {
  117. x = 0;
  118. } else if (x > (SCREEN_WIDTH - 16)) {
  119. x = SCREEN_WIDTH - 16;
  120. }
  121. ++i;
  122. if (i < SDL_JoystickNumAxes(joystick)) {
  123. y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  124. } else {
  125. y = 32768;
  126. }
  127. y *= SCREEN_HEIGHT;
  128. y /= 65535;
  129. if (y < 0) {
  130. y = 0;
  131. } else if (y > (SCREEN_HEIGHT - 16)) {
  132. y = SCREEN_HEIGHT - 16;
  133. }
  134. DrawRect(screen, x, y, 16, 16);
  135. }
  136. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
  137. for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
  138. /* Derive the new position */
  139. int x = SCREEN_WIDTH/2;
  140. int y = SCREEN_HEIGHT/2;
  141. const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
  142. if (hat_pos & SDL_HAT_UP) {
  143. y = 0;
  144. } else if (hat_pos & SDL_HAT_DOWN) {
  145. y = SCREEN_HEIGHT-8;
  146. }
  147. if (hat_pos & SDL_HAT_LEFT) {
  148. x = 0;
  149. } else if (hat_pos & SDL_HAT_RIGHT) {
  150. x = SCREEN_WIDTH-8;
  151. }
  152. DrawRect(screen, x, y, 8, 8);
  153. }
  154. SDL_RenderPresent(screen);
  155. if (SDL_JoystickGetAttached( joystick ) == 0) {
  156. done = SDL_TRUE;
  157. retval = SDL_TRUE; /* keep going, wait for reattach. */
  158. }
  159. #ifdef __EMSCRIPTEN__
  160. if (done) {
  161. emscripten_cancel_main_loop();
  162. }
  163. #endif
  164. }
  165. static SDL_bool
  166. WatchJoystick(SDL_Joystick * joystick)
  167. {
  168. SDL_Window *window = NULL;
  169. const char *name = NULL;
  170. retval = SDL_FALSE;
  171. done = SDL_FALSE;
  172. /* Create a window to display joystick axis position */
  173. window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
  174. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  175. SCREEN_HEIGHT, 0);
  176. if (window == NULL) {
  177. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  178. return SDL_FALSE;
  179. }
  180. screen = SDL_CreateRenderer(window, -1, 0);
  181. if (screen == NULL) {
  182. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  183. SDL_DestroyWindow(window);
  184. return SDL_FALSE;
  185. }
  186. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  187. SDL_RenderClear(screen);
  188. SDL_RenderPresent(screen);
  189. SDL_RaiseWindow(window);
  190. /* Print info about the joystick we are watching */
  191. name = SDL_JoystickName(joystick);
  192. SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
  193. name ? name : "Unknown Joystick");
  194. SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
  195. SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
  196. SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
  197. /* Loop, getting joystick events! */
  198. #ifdef __EMSCRIPTEN__
  199. emscripten_set_main_loop_arg(loop, joystick, 0, 1);
  200. #else
  201. while (!done) {
  202. loop(joystick);
  203. }
  204. #endif
  205. SDL_DestroyRenderer(screen);
  206. screen = NULL;
  207. SDL_DestroyWindow(window);
  208. return retval;
  209. }
  210. int
  211. main(int argc, char *argv[])
  212. {
  213. const char *name, *type;
  214. int i;
  215. SDL_Joystick *joystick;
  216. SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
  217. /* Enable standard application logging */
  218. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  219. /* Initialize SDL (Note: video is required to start event loop) */
  220. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  221. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  222. exit(1);
  223. }
  224. /* Print information about the joysticks */
  225. SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
  226. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  227. name = SDL_JoystickNameForIndex(i);
  228. SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
  229. joystick = SDL_JoystickOpen(i);
  230. if (joystick == NULL) {
  231. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
  232. SDL_GetError());
  233. } else {
  234. char guid[64];
  235. SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
  236. SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
  237. guid, sizeof (guid));
  238. switch (SDL_JoystickGetType(joystick)) {
  239. case SDL_JOYSTICK_TYPE_GAMECONTROLLER:
  240. type = "Game Controller";
  241. break;
  242. case SDL_JOYSTICK_TYPE_WHEEL:
  243. type = "Wheel";
  244. break;
  245. case SDL_JOYSTICK_TYPE_ARCADE_STICK:
  246. type = "Arcade Stick";
  247. break;
  248. case SDL_JOYSTICK_TYPE_FLIGHT_STICK:
  249. type = "Flight Stick";
  250. break;
  251. case SDL_JOYSTICK_TYPE_DANCE_PAD:
  252. type = "Dance Pad";
  253. break;
  254. case SDL_JOYSTICK_TYPE_GUITAR:
  255. type = "Guitar";
  256. break;
  257. case SDL_JOYSTICK_TYPE_DRUM_KIT:
  258. type = "Drum Kit";
  259. break;
  260. case SDL_JOYSTICK_TYPE_ARCADE_PAD:
  261. type = "Arcade Pad";
  262. break;
  263. case SDL_JOYSTICK_TYPE_THROTTLE:
  264. type = "Throttle";
  265. break;
  266. default:
  267. type = "Unknown";
  268. break;
  269. }
  270. SDL_Log(" type: %s\n", type);
  271. SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
  272. SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
  273. SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
  274. SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
  275. SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
  276. SDL_Log(" guid: %s\n", guid);
  277. SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
  278. SDL_JoystickClose(joystick);
  279. }
  280. }
  281. #if defined(__ANDROID__) || defined(__IPHONEOS__)
  282. if (SDL_NumJoysticks() > 0) {
  283. #else
  284. if (argv[1]) {
  285. #endif
  286. SDL_bool reportederror = SDL_FALSE;
  287. SDL_bool keepGoing = SDL_TRUE;
  288. SDL_Event event;
  289. int device;
  290. #if defined(__ANDROID__) || defined(__IPHONEOS__)
  291. device = 0;
  292. #else
  293. device = atoi(argv[1]);
  294. #endif
  295. joystick = SDL_JoystickOpen(device);
  296. if (joystick != NULL) {
  297. SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
  298. }
  299. while ( keepGoing ) {
  300. if (joystick == NULL) {
  301. if ( !reportederror ) {
  302. SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
  303. keepGoing = SDL_FALSE;
  304. reportederror = SDL_TRUE;
  305. }
  306. } else {
  307. reportederror = SDL_FALSE;
  308. keepGoing = WatchJoystick(joystick);
  309. SDL_JoystickClose(joystick);
  310. }
  311. joystick = NULL;
  312. if (keepGoing) {
  313. SDL_Log("Waiting for attach\n");
  314. }
  315. while (keepGoing) {
  316. SDL_WaitEvent(&event);
  317. if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
  318. || (event.type == SDL_MOUSEBUTTONDOWN)) {
  319. keepGoing = SDL_FALSE;
  320. } else if (event.type == SDL_JOYDEVICEADDED) {
  321. device = event.jdevice.which;
  322. joystick = SDL_JoystickOpen(device);
  323. if (joystick != NULL) {
  324. SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
  325. }
  326. break;
  327. }
  328. }
  329. }
  330. }
  331. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
  332. return 0;
  333. }
  334. #else
  335. int
  336. main(int argc, char *argv[])
  337. {
  338. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  339. exit(1);
  340. }
  341. #endif
  342. /* vi: set ts=4 sw=4 expandtab: */