testjoystick.c 12 KB

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