testjoystick.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Copyright (C) 1997-2014 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. #ifndef SDL_JOYSTICK_DISABLED
  16. #ifdef __IPHONEOS__
  17. #define SCREEN_WIDTH 320
  18. #define SCREEN_HEIGHT 480
  19. #else
  20. #define SCREEN_WIDTH 640
  21. #define SCREEN_HEIGHT 480
  22. #endif
  23. static void
  24. DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
  25. {
  26. const SDL_Rect area = { x, y, w, h };
  27. SDL_RenderFillRect(r, &area);
  28. }
  29. static SDL_bool
  30. WatchJoystick(SDL_Joystick * joystick)
  31. {
  32. SDL_Window *window = NULL;
  33. SDL_Renderer *screen = NULL;
  34. const char *name = NULL;
  35. SDL_bool retval = SDL_FALSE;
  36. SDL_bool done = SDL_FALSE;
  37. SDL_Event event;
  38. int i;
  39. /* Create a window to display joystick axis position */
  40. window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
  41. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  42. SCREEN_HEIGHT, 0);
  43. if (window == NULL) {
  44. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  45. return SDL_FALSE;
  46. }
  47. screen = SDL_CreateRenderer(window, -1, 0);
  48. if (screen == NULL) {
  49. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  50. SDL_DestroyWindow(window);
  51. return SDL_FALSE;
  52. }
  53. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  54. SDL_RenderClear(screen);
  55. SDL_RenderPresent(screen);
  56. SDL_RaiseWindow(window);
  57. /* Print info about the joystick we are watching */
  58. name = SDL_JoystickName(joystick);
  59. SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
  60. name ? name : "Unknown Joystick");
  61. SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
  62. SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
  63. SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
  64. /* Loop, getting joystick events! */
  65. while (!done) {
  66. /* blank screen, set up for drawing this frame. */
  67. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  68. SDL_RenderClear(screen);
  69. while (SDL_PollEvent(&event)) {
  70. switch (event.type) {
  71. case SDL_JOYAXISMOTION:
  72. SDL_Log("Joystick %d axis %d value: %d\n",
  73. event.jaxis.which,
  74. event.jaxis.axis, event.jaxis.value);
  75. break;
  76. case SDL_JOYHATMOTION:
  77. SDL_Log("Joystick %d hat %d value:",
  78. event.jhat.which, event.jhat.hat);
  79. if (event.jhat.value == SDL_HAT_CENTERED)
  80. SDL_Log(" centered");
  81. if (event.jhat.value & SDL_HAT_UP)
  82. SDL_Log(" up");
  83. if (event.jhat.value & SDL_HAT_RIGHT)
  84. SDL_Log(" right");
  85. if (event.jhat.value & SDL_HAT_DOWN)
  86. SDL_Log(" down");
  87. if (event.jhat.value & SDL_HAT_LEFT)
  88. SDL_Log(" left");
  89. SDL_Log("\n");
  90. break;
  91. case SDL_JOYBALLMOTION:
  92. SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
  93. event.jball.which,
  94. event.jball.ball, event.jball.xrel, event.jball.yrel);
  95. break;
  96. case SDL_JOYBUTTONDOWN:
  97. SDL_Log("Joystick %d button %d down\n",
  98. event.jbutton.which, event.jbutton.button);
  99. break;
  100. case SDL_JOYBUTTONUP:
  101. SDL_Log("Joystick %d button %d up\n",
  102. event.jbutton.which, event.jbutton.button);
  103. break;
  104. case SDL_KEYDOWN:
  105. if ((event.key.keysym.sym != SDLK_ESCAPE) &&
  106. (event.key.keysym.sym != SDLK_AC_BACK)) {
  107. break;
  108. }
  109. /* Fall through to signal quit */
  110. case SDL_FINGERDOWN:
  111. case SDL_MOUSEBUTTONDOWN:
  112. case SDL_QUIT:
  113. done = SDL_TRUE;
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. /* Update visual joystick state */
  120. SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
  121. for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
  122. if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
  123. DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
  124. }
  125. }
  126. SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  127. for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
  128. /* Draw the X/Y axis */
  129. int x, y;
  130. x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  131. x *= SCREEN_WIDTH;
  132. x /= 65535;
  133. if (x < 0) {
  134. x = 0;
  135. } else if (x > (SCREEN_WIDTH - 16)) {
  136. x = SCREEN_WIDTH - 16;
  137. }
  138. ++i;
  139. if (i < SDL_JoystickNumAxes(joystick)) {
  140. y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
  141. } else {
  142. y = 32768;
  143. }
  144. y *= SCREEN_HEIGHT;
  145. y /= 65535;
  146. if (y < 0) {
  147. y = 0;
  148. } else if (y > (SCREEN_HEIGHT - 16)) {
  149. y = SCREEN_HEIGHT - 16;
  150. }
  151. DrawRect(screen, x, y, 16, 16);
  152. }
  153. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
  154. for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
  155. /* Derive the new position */
  156. int x = SCREEN_WIDTH/2;
  157. int y = SCREEN_HEIGHT/2;
  158. const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
  159. if (hat_pos & SDL_HAT_UP) {
  160. y = 0;
  161. } else if (hat_pos & SDL_HAT_DOWN) {
  162. y = SCREEN_HEIGHT-8;
  163. }
  164. if (hat_pos & SDL_HAT_LEFT) {
  165. x = 0;
  166. } else if (hat_pos & SDL_HAT_RIGHT) {
  167. x = SCREEN_WIDTH-8;
  168. }
  169. DrawRect(screen, x, y, 8, 8);
  170. }
  171. SDL_RenderPresent(screen);
  172. if (SDL_JoystickGetAttached( joystick ) == 0) {
  173. done = SDL_TRUE;
  174. retval = SDL_TRUE; /* keep going, wait for reattach. */
  175. }
  176. }
  177. SDL_DestroyRenderer(screen);
  178. SDL_DestroyWindow(window);
  179. return retval;
  180. }
  181. int
  182. main(int argc, char *argv[])
  183. {
  184. const char *name;
  185. int i;
  186. SDL_Joystick *joystick;
  187. /* Enable standard application logging */
  188. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  189. /* Initialize SDL (Note: video is required to start event loop) */
  190. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  191. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  192. exit(1);
  193. }
  194. /* Print information about the joysticks */
  195. SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
  196. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  197. name = SDL_JoystickNameForIndex(i);
  198. SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
  199. joystick = SDL_JoystickOpen(i);
  200. if (joystick == NULL) {
  201. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
  202. SDL_GetError());
  203. } else {
  204. char guid[64];
  205. SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
  206. guid, sizeof (guid));
  207. SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
  208. SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
  209. SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
  210. SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
  211. SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
  212. SDL_Log(" guid: %s\n", guid);
  213. SDL_JoystickClose(joystick);
  214. }
  215. }
  216. #ifdef ANDROID
  217. if (SDL_NumJoysticks() > 0) {
  218. #else
  219. if (argv[1]) {
  220. #endif
  221. SDL_bool reportederror = SDL_FALSE;
  222. SDL_bool keepGoing = SDL_TRUE;
  223. SDL_Event event;
  224. int device;
  225. #ifdef ANDROID
  226. device = 0;
  227. #else
  228. device = atoi(argv[1]);
  229. #endif
  230. joystick = SDL_JoystickOpen(device);
  231. while ( keepGoing ) {
  232. if (joystick == NULL) {
  233. if ( !reportederror ) {
  234. SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
  235. keepGoing = SDL_FALSE;
  236. reportederror = SDL_TRUE;
  237. }
  238. } else {
  239. reportederror = SDL_FALSE;
  240. keepGoing = WatchJoystick(joystick);
  241. SDL_JoystickClose(joystick);
  242. }
  243. joystick = NULL;
  244. if (keepGoing) {
  245. SDL_Log("Waiting for attach\n");
  246. }
  247. while (keepGoing) {
  248. SDL_WaitEvent(&event);
  249. if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
  250. || (event.type == SDL_MOUSEBUTTONDOWN)) {
  251. keepGoing = SDL_FALSE;
  252. } else if (event.type == SDL_JOYDEVICEADDED) {
  253. joystick = SDL_JoystickOpen(device);
  254. break;
  255. }
  256. }
  257. }
  258. }
  259. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
  260. return 0;
  261. }
  262. #else
  263. int
  264. main(int argc, char *argv[])
  265. {
  266. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  267. exit(1);
  268. }
  269. #endif