SDL 2.0
testjoystick.c
Go to the documentation of this file.
1/*
2 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
3
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.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12
13/* Simple program to test the SDL joystick routines */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include "SDL.h"
20
21#ifdef __EMSCRIPTEN__
22#include <emscripten/emscripten.h>
23#endif
24
25#ifndef SDL_JOYSTICK_DISABLED
26
27#ifdef __IPHONEOS__
28#define SCREEN_WIDTH 320
29#define SCREEN_HEIGHT 480
30#else
31#define SCREEN_WIDTH 640
32#define SCREEN_HEIGHT 480
33#endif
34
38
39static void
40DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
41{
42 const SDL_Rect area = { x, y, w, h };
43 SDL_RenderFillRect(r, &area);
44}
45
46void
47loop(void *arg)
48{
50 int i;
51 SDL_Joystick *joystick = (SDL_Joystick *)arg;
52
53 /* blank screen, set up for drawing this frame. */
56
57 while (SDL_PollEvent(&event)) {
58 switch (event.type) {
59
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));
63 break;
64
66 SDL_Log("Joystick %d axis %d value: %d\n",
67 event.jaxis.which,
68 event.jaxis.axis, event.jaxis.value);
69 break;
71 SDL_Log("Joystick %d hat %d value:",
72 event.jhat.which, event.jhat.hat);
73 if (event.jhat.value == SDL_HAT_CENTERED)
74 SDL_Log(" centered");
75 if (event.jhat.value & SDL_HAT_UP)
76 SDL_Log(" up");
77 if (event.jhat.value & SDL_HAT_RIGHT)
78 SDL_Log(" right");
79 if (event.jhat.value & SDL_HAT_DOWN)
80 SDL_Log(" down");
81 if (event.jhat.value & SDL_HAT_LEFT)
82 SDL_Log(" left");
83 SDL_Log("\n");
84 break;
86 SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
87 event.jball.which,
88 event.jball.ball, event.jball.xrel, event.jball.yrel);
89 break;
91 SDL_Log("Joystick %d button %d down\n",
92 event.jbutton.which, event.jbutton.button);
93 /* First button triggers a 0.5 second full strength rumble */
94 if (event.jbutton.button == 0) {
95 SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500);
96 }
97 break;
98 case SDL_JOYBUTTONUP:
99 SDL_Log("Joystick %d button %d up\n",
100 event.jbutton.which, event.jbutton.button);
101 break;
102 case SDL_KEYDOWN:
103 if ((event.key.keysym.sym != SDLK_ESCAPE) &&
104 (event.key.keysym.sym != SDLK_AC_BACK)) {
105 break;
106 }
107 /* Fall through to signal quit */
108 case SDL_FINGERDOWN:
110 case SDL_QUIT:
111 done = SDL_TRUE;
112 break;
113 default:
114 break;
115 }
116 }
117 /* Update visual joystick state */
119 for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
120 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
121 DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
122 }
123 }
124
126 for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
127 /* Draw the X/Y axis */
128 int x, y;
129 x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
130 x *= SCREEN_WIDTH;
131 x /= 65535;
132 if (x < 0) {
133 x = 0;
134 } else if (x > (SCREEN_WIDTH - 16)) {
135 x = SCREEN_WIDTH - 16;
136 }
137 ++i;
138 if (i < SDL_JoystickNumAxes(joystick)) {
139 y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
140 } else {
141 y = 32768;
142 }
143 y *= SCREEN_HEIGHT;
144 y /= 65535;
145 if (y < 0) {
146 y = 0;
147 } else if (y > (SCREEN_HEIGHT - 16)) {
148 y = SCREEN_HEIGHT - 16;
149 }
150
151 DrawRect(screen, x, y, 16, 16);
152 }
153
155 for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
156 /* Derive the new position */
157 int x = SCREEN_WIDTH/2;
158 int y = SCREEN_HEIGHT/2;
159 const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
160
161 if (hat_pos & SDL_HAT_UP) {
162 y = 0;
163 } else if (hat_pos & SDL_HAT_DOWN) {
164 y = SCREEN_HEIGHT-8;
165 }
166
167 if (hat_pos & SDL_HAT_LEFT) {
168 x = 0;
169 } else if (hat_pos & SDL_HAT_RIGHT) {
170 x = SCREEN_WIDTH-8;
171 }
172
173 DrawRect(screen, x, y, 8, 8);
174 }
175
177
178 if (SDL_JoystickGetAttached( joystick ) == 0) {
179 done = SDL_TRUE;
180 retval = SDL_TRUE; /* keep going, wait for reattach. */
181 }
182
183#ifdef __EMSCRIPTEN__
184 if (done) {
185 emscripten_cancel_main_loop();
186 }
187#endif
188}
189
190static SDL_bool
191WatchJoystick(SDL_Joystick * joystick)
192{
194 const char *name = NULL;
195
197 done = SDL_FALSE;
198
199 /* Create a window to display joystick axis position */
202 SCREEN_HEIGHT, 0);
203 if (window == NULL) {
204 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
205 return SDL_FALSE;
206 }
207
209 if (screen == NULL) {
210 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
212 return SDL_FALSE;
213 }
214
219
220 /* Print info about the joystick we are watching */
221 name = SDL_JoystickName(joystick);
222 SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
223 name ? name : "Unknown Joystick");
224 SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
225 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
226 SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
227
228 /* Loop, getting joystick events! */
229#ifdef __EMSCRIPTEN__
230 emscripten_set_main_loop_arg(loop, joystick, 0, 1);
231#else
232 while (!done) {
233 loop(joystick);
234 }
235#endif
236
238 screen = NULL;
240 return retval;
241}
242
243int
244main(int argc, char *argv[])
245{
246 const char *name, *type;
247 int i;
248 SDL_Joystick *joystick;
249
251
252 /* Enable standard application logging */
254
255 /* Initialize SDL (Note: video is required to start event loop) */
257 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
258 exit(1);
259 }
260
261 /* Print information about the joysticks */
262 SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
263 for (i = 0; i < SDL_NumJoysticks(); ++i) {
265 SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
266 joystick = SDL_JoystickOpen(i);
267 if (joystick == NULL) {
268 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
269 SDL_GetError());
270 } else {
271 char guid[64];
274 guid, sizeof (guid));
275 switch (SDL_JoystickGetType(joystick)) {
277 type = "Game Controller";
278 break;
280 type = "Wheel";
281 break;
283 type = "Arcade Stick";
284 break;
286 type = "Flight Stick";
287 break;
289 type = "Dance Pad";
290 break;
292 type = "Guitar";
293 break;
295 type = "Drum Kit";
296 break;
298 type = "Arcade Pad";
299 break;
301 type = "Throttle";
302 break;
303 default:
304 type = "Unknown";
305 break;
306 }
307 SDL_Log(" type: %s\n", type);
308 SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
309 SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
310 SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
311 SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
312 SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
313 SDL_Log(" guid: %s\n", guid);
314 SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
315 SDL_JoystickClose(joystick);
316 }
317 }
318
319#if defined(__ANDROID__) || defined(__IPHONEOS__)
320 if (SDL_NumJoysticks() > 0) {
321#else
322 if (argv[1]) {
323#endif
324 SDL_bool reportederror = SDL_FALSE;
325 SDL_bool keepGoing = SDL_TRUE;
327 int device;
328#if defined(__ANDROID__) || defined(__IPHONEOS__)
329 device = 0;
330#else
331 device = atoi(argv[1]);
332#endif
333 joystick = SDL_JoystickOpen(device);
334 if (joystick != NULL) {
336 }
337
338 while ( keepGoing ) {
339 if (joystick == NULL) {
340 if ( !reportederror ) {
341 SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
342 keepGoing = SDL_FALSE;
343 reportederror = SDL_TRUE;
344 }
345 } else {
346 reportederror = SDL_FALSE;
347 keepGoing = WatchJoystick(joystick);
348 SDL_JoystickClose(joystick);
349 }
350
351 joystick = NULL;
352 if (keepGoing) {
353 SDL_Log("Waiting for attach\n");
354 }
355 while (keepGoing) {
357 if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
358 || (event.type == SDL_MOUSEBUTTONDOWN)) {
359 keepGoing = SDL_FALSE;
360 } else if (event.type == SDL_JOYDEVICEADDED) {
361 device = event.jdevice.which;
362 joystick = SDL_JoystickOpen(device);
363 if (joystick != NULL) {
365 }
366 break;
367 }
368 }
369 }
370 }
372
373 return 0;
374}
375
376#else
377
378int
379main(int argc, char *argv[])
380{
381 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
382 exit(1);
383}
384
385#endif
386
387/* vi: set ts=4 sw=4 expandtab: */
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define SDL_JoystickName
#define SDL_RenderPresent
#define SDL_DestroyWindow
#define SDL_JoystickNumAxes
#define SDL_GetError
#define SDL_PollEvent
#define SDL_RenderFillRect
#define SDL_DestroyRenderer
#define SDL_SetRenderDrawColor
#define SDL_JoystickGetGUID
#define SDL_CreateWindow
#define SDL_JoystickNumHats
#define SDL_JoystickNameForIndex
#define SDL_JoystickGetAxis
#define SDL_JoystickClose
#define SDL_JoystickRumble
#define SDL_JoystickGetHat
#define SDL_JoystickGetGUIDString
#define SDL_JoystickOpen
#define SDL_NumJoysticks
#define SDL_JoystickGetType
#define SDL_CreateRenderer
#define SDL_JoystickNumButtons
#define SDL_LogSetPriority
#define SDL_JoystickFromInstanceID
#define SDL_JoystickGetVendor
#define SDL_RenderClear
#define SDL_LogError
#define SDL_JoystickGetAttached
#define SDL_RaiseWindow
#define SDL_JoystickGetButton
#define SDL_JoystickNumBalls
#define SDL_Init
#define SDL_JoystickInstanceID
#define SDL_Log
#define SDL_QuitSubSystem
#define SDL_WaitEvent
#define SDL_SetHint
#define SDL_JoystickGetProduct
@ SDL_JOYDEVICEADDED
Definition: SDL_events.h:116
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_JOYBUTTONDOWN
Definition: SDL_events.h:114
@ SDL_JOYDEVICEREMOVED
Definition: SDL_events.h:117
@ SDL_JOYBUTTONUP
Definition: SDL_events.h:115
@ SDL_JOYBALLMOTION
Definition: SDL_events.h:112
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:106
@ SDL_FINGERDOWN
Definition: SDL_events.h:128
@ SDL_KEYDOWN
Definition: SDL_events.h:96
@ SDL_JOYAXISMOTION
Definition: SDL_events.h:111
@ SDL_JOYHATMOTION
Definition: SDL_events.h:113
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_HINT_ACCELEROMETER_AS_JOYSTICK
A variable controlling whether the Android / iOS built-in accelerometer should be listed as a joystic...
Definition: SDL_hints.h:409
@ SDL_JOYSTICK_TYPE_DANCE_PAD
Definition: SDL_joystick.h:90
@ SDL_JOYSTICK_TYPE_ARCADE_PAD
Definition: SDL_joystick.h:93
@ SDL_JOYSTICK_TYPE_ARCADE_STICK
Definition: SDL_joystick.h:88
@ SDL_JOYSTICK_TYPE_WHEEL
Definition: SDL_joystick.h:87
@ SDL_JOYSTICK_TYPE_THROTTLE
Definition: SDL_joystick.h:94
@ SDL_JOYSTICK_TYPE_GUITAR
Definition: SDL_joystick.h:91
@ SDL_JOYSTICK_TYPE_FLIGHT_STICK
Definition: SDL_joystick.h:89
@ SDL_JOYSTICK_TYPE_GAMECONTROLLER
Definition: SDL_joystick.h:86
@ SDL_JOYSTICK_TYPE_DRUM_KIT
Definition: SDL_joystick.h:92
#define SDL_HAT_LEFT
Definition: SDL_joystick.h:333
#define SDL_HAT_RIGHT
Definition: SDL_joystick.h:331
#define SDL_HAT_DOWN
Definition: SDL_joystick.h:332
#define SDL_HAT_UP
Definition: SDL_joystick.h:330
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:329
@ SDLK_AC_BACK
Definition: SDL_keycode.h:299
@ SDLK_ESCAPE
Definition: SDL_keycode.h:55
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
struct _cl_event * event
GLuint const GLchar * name
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
static SDL_AudioDeviceID device
Definition: loopwave.c:37
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
The type used to identify a window.
Definition: SDL_sysvideo.h:74
int main(int argc, char *argv[])
Definition: testjoystick.c:244
#define SCREEN_WIDTH
Definition: testjoystick.c:31
static SDL_bool WatchJoystick(SDL_Joystick *joystick)
Definition: testjoystick.c:191
SDL_Renderer * screen
Definition: testjoystick.c:35
SDL_bool retval
Definition: testjoystick.c:36
#define SCREEN_HEIGHT
Definition: testjoystick.c:32
static void DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
Definition: testjoystick.c:40
SDL_bool done
Definition: testjoystick.c:37
void loop(void *arg)
Definition: testjoystick.c:47
General event structure.
Definition: SDL_events.h:558