2 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple program to test the SDL joystick routines */
22 #include <emscripten/emscripten.h>
25 #ifndef SDL_JOYSTICK_DISABLED
28 #define SCREEN_WIDTH 320
29 #define SCREEN_HEIGHT 480
31 #define SCREEN_WIDTH 640
32 #define SCREEN_HEIGHT 480
35 SDL_Renderer *screen = NULL;
36 SDL_bool retval = SDL_FALSE;
37 SDL_bool done = SDL_FALSE;
40 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
42 const SDL_Rect area = { x, y, w, h };
43 SDL_RenderFillRect(r, &area);
51 SDL_Joystick *joystick = (SDL_Joystick *)arg;
53 /* blank screen, set up for drawing this frame. */
54 SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE);
55 SDL_RenderClear(screen);
57 while (SDL_PollEvent(&event)) {
60 case SDL_JOYDEVICEREMOVED:
61 SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
62 SDL_Log("Our instance ID is %d\n", (int) SDL_JoystickInstanceID(joystick));
65 case SDL_JOYAXISMOTION:
66 SDL_Log("Joystick %d axis %d value: %d\n",
68 event.jaxis.axis, event.jaxis.value);
70 case SDL_JOYHATMOTION:
71 SDL_Log("Joystick %d hat %d value:",
72 event.jhat.which, event.jhat.hat);
73 if (event.jhat.value == SDL_HAT_CENTERED)
75 if (event.jhat.value & SDL_HAT_UP)
77 if (event.jhat.value & SDL_HAT_RIGHT)
79 if (event.jhat.value & SDL_HAT_DOWN)
81 if (event.jhat.value & SDL_HAT_LEFT)
85 case SDL_JOYBALLMOTION:
86 SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
88 event.jball.ball, event.jball.xrel, event.jball.yrel);
90 case SDL_JOYBUTTONDOWN:
91 SDL_Log("Joystick %d button %d down\n",
92 event.jbutton.which, event.jbutton.button);
95 SDL_Log("Joystick %d button %d up\n",
96 event.jbutton.which, event.jbutton.button);
99 if ((event.key.keysym.sym != SDLK_ESCAPE) &&
100 (event.key.keysym.sym != SDLK_AC_BACK)) {
103 /* Fall through to signal quit */
105 case SDL_MOUSEBUTTONDOWN:
113 /* Update visual joystick state */
114 SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
115 for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
116 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
117 DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
121 SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
122 for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
123 /* Draw the X/Y axis */
125 x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
130 } else if (x > (SCREEN_WIDTH - 16)) {
131 x = SCREEN_WIDTH - 16;
134 if (i < SDL_JoystickNumAxes(joystick)) {
135 y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
143 } else if (y > (SCREEN_HEIGHT - 16)) {
144 y = SCREEN_HEIGHT - 16;
147 DrawRect(screen, x, y, 16, 16);
150 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
151 for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
152 /* Derive the new position */
153 int x = SCREEN_WIDTH/2;
154 int y = SCREEN_HEIGHT/2;
155 const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
157 if (hat_pos & SDL_HAT_UP) {
159 } else if (hat_pos & SDL_HAT_DOWN) {
163 if (hat_pos & SDL_HAT_LEFT) {
165 } else if (hat_pos & SDL_HAT_RIGHT) {
169 DrawRect(screen, x, y, 8, 8);
172 SDL_RenderPresent(screen);
174 if (SDL_JoystickGetAttached( joystick ) == 0) {
176 retval = SDL_TRUE; /* keep going, wait for reattach. */
179 #ifdef __EMSCRIPTEN__
181 emscripten_cancel_main_loop();
187 WatchJoystick(SDL_Joystick * joystick)
189 SDL_Window *window = NULL;
190 const char *name = NULL;
195 /* Create a window to display joystick axis position */
196 window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
197 SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
199 if (window == NULL) {
200 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
204 screen = SDL_CreateRenderer(window, -1, 0);
205 if (screen == NULL) {
206 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
207 SDL_DestroyWindow(window);
211 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
212 SDL_RenderClear(screen);
213 SDL_RenderPresent(screen);
214 SDL_RaiseWindow(window);
216 /* Print info about the joystick we are watching */
217 name = SDL_JoystickName(joystick);
218 SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
219 name ? name : "Unknown Joystick");
220 SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
221 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
222 SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
224 /* Loop, getting joystick events! */
225 #ifdef __EMSCRIPTEN__
226 emscripten_set_main_loop_arg(loop, joystick, 0, 1);
233 SDL_DestroyRenderer(screen);
235 SDL_DestroyWindow(window);
240 main(int argc, char *argv[])
244 SDL_Joystick *joystick;
246 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
248 /* Enable standard application logging */
249 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
251 /* Initialize SDL (Note: video is required to start event loop) */
252 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
253 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
257 /* Print information about the joysticks */
258 SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
259 for (i = 0; i < SDL_NumJoysticks(); ++i) {
260 name = SDL_JoystickNameForIndex(i);
261 SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
262 joystick = SDL_JoystickOpen(i);
263 if (joystick == NULL) {
264 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
268 SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
269 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
270 guid, sizeof (guid));
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_JoystickClose(joystick);
281 #if defined(__ANDROID__) || defined(__IPHONEOS__)
282 if (SDL_NumJoysticks() > 0) {
286 SDL_bool reportederror = SDL_FALSE;
287 SDL_bool keepGoing = SDL_TRUE;
290 #if defined(__ANDROID__) || defined(__IPHONEOS__)
293 device = atoi(argv[1]);
295 joystick = SDL_JoystickOpen(device);
296 if (joystick != NULL) {
297 SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
300 while ( keepGoing ) {
301 if (joystick == NULL) {
302 if ( !reportederror ) {
303 SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
304 keepGoing = SDL_FALSE;
305 reportederror = SDL_TRUE;
308 reportederror = SDL_FALSE;
309 keepGoing = WatchJoystick(joystick);
310 SDL_JoystickClose(joystick);
315 SDL_Log("Waiting for attach\n");
318 SDL_WaitEvent(&event);
319 if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
320 || (event.type == SDL_MOUSEBUTTONDOWN)) {
321 keepGoing = SDL_FALSE;
322 } else if (event.type == SDL_JOYDEVICEADDED) {
323 joystick = SDL_JoystickOpen(device);
324 if (joystick != NULL) {
325 SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
332 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
340 main(int argc, char *argv[])
342 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");