SDL  2.0
testjoystick.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
+ Include dependency graph for testjoystick.c:

Go to the source code of this file.

Macros

#define SCREEN_WIDTH   640
 
#define SCREEN_HEIGHT   480
 

Functions

static void DrawRect (SDL_Renderer *r, const int x, const int y, const int w, const int h)
 
void loop (void *arg)
 
static SDL_bool WatchJoystick (SDL_Joystick *joystick)
 
int main (int argc, char *argv[])
 

Variables

SDL_Rendererscreen = NULL
 
SDL_bool retval = SDL_FALSE
 
SDL_bool done = SDL_FALSE
 

Macro Definition Documentation

§ SCREEN_HEIGHT

#define SCREEN_HEIGHT   480

Definition at line 32 of file testjoystick.c.

Referenced by loop(), and WatchJoystick().

§ SCREEN_WIDTH

#define SCREEN_WIDTH   640

Definition at line 31 of file testjoystick.c.

Referenced by loop(), and WatchJoystick().

Function Documentation

§ DrawRect()

static void DrawRect ( SDL_Renderer r,
const int  x,
const int  y,
const int  w,
const int  h 
)
static

Definition at line 40 of file testjoystick.c.

References SDL_RenderFillRect.

Referenced by loop().

41 {
42  const SDL_Rect area = { x, y, w, h };
43  SDL_RenderFillRect(r, &area);
44 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLfloat GLfloat GLfloat GLfloat h
#define SDL_RenderFillRect
GLubyte GLubyte GLubyte GLubyte w
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

§ loop()

void loop ( void arg)

Definition at line 47 of file testjoystick.c.

References SDL_JoyAxisEvent::axis, SDL_JoyBallEvent::ball, SDL_JoyButtonEvent::button, done, DrawRect(), SDL_JoyHatEvent::hat, i, SDL_Event::jaxis, SDL_Event::jball, SDL_Event::jbutton, SDL_Event::jdevice, SDL_Event::jhat, SDL_Event::key, SDL_KeyboardEvent::keysym, retval, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_ALPHA_OPAQUE, SDL_FINGERDOWN, SDL_HAT_CENTERED, SDL_HAT_DOWN, SDL_HAT_LEFT, SDL_HAT_RIGHT, SDL_HAT_UP, SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEREMOVED, SDL_JOYHATMOTION, SDL_JoystickGetAttached, SDL_JoystickGetAxis, SDL_JoystickGetButton, SDL_JoystickGetHat, SDL_JoystickInstanceID, SDL_JoystickNumAxes, SDL_JoystickNumButtons, SDL_JoystickNumHats, SDL_JoystickRumble, SDL_KEYDOWN, SDL_Log, SDL_MOUSEBUTTONDOWN, SDL_PollEvent, SDL_PRESSED, SDL_QUIT, SDL_RenderClear, SDL_RenderPresent, SDL_SetRenderDrawColor, SDL_TRUE, SDLK_AC_BACK, SDLK_ESCAPE, SDL_Keysym::sym, SDL_Event::type, SDL_JoyAxisEvent::value, SDL_JoyHatEvent::value, SDL_JoyAxisEvent::which, SDL_JoyBallEvent::which, SDL_JoyHatEvent::which, SDL_JoyButtonEvent::which, SDL_JoyDeviceEvent::which, SDL_JoyBallEvent::xrel, and SDL_JoyBallEvent::yrel.

Referenced by WatchJoystick().

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 
65  case SDL_JOYAXISMOTION:
66  SDL_Log("Joystick %d axis %d value: %d\n",
67  event.jaxis.which,
68  event.jaxis.axis, event.jaxis.value);
69  break;
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)
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;
85  case SDL_JOYBALLMOTION:
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;
90  case SDL_JOYBUTTONDOWN:
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:
109  case SDL_MOUSEBUTTONDOWN:
110  case SDL_QUIT:
111  done = SDL_TRUE;
112  break;
113  default:
114  break;
115  }
116  }
117  /* Update visual joystick state */
118  SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
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 
125  SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
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 
154  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
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 }
SDL_bool done
Definition: testjoystick.c:37
SDL_JoystickID which
Definition: SDL_events.h:356
#define SDL_PollEvent
#define SDL_JoystickGetButton
SDL_JoyDeviceEvent jdevice
Definition: SDL_events.h:573
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_JoyButtonEvent jbutton
Definition: SDL_events.h:572
SDL_JoyBallEvent jball
Definition: SDL_events.h:570
SDL_JoystickID which
Definition: SDL_events.h:304
static void DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
Definition: testjoystick.c:40
#define SDL_JoystickNumButtons
#define SDL_JoystickInstanceID
#define SDL_JoystickGetHat
#define SDL_JoystickNumAxes
#define SCREEN_WIDTH
Definition: testjoystick.c:31
#define SDL_HAT_RIGHT
Definition: SDL_joystick.h:331
SDL_JoystickID which
Definition: SDL_events.h:320
#define SDL_Log
#define SDL_HAT_LEFT
Definition: SDL_joystick.h:333
SDL_JoyAxisEvent jaxis
Definition: SDL_events.h:569
uint8_t Uint8
Definition: SDL_stdinc.h:179
struct _cl_event * event
#define SCREEN_HEIGHT
Definition: testjoystick.c:32
#define SDL_JoystickGetAttached
SDL_bool retval
Definition: testjoystick.c:36
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
#define SDL_JoystickRumble
SDL_Keysym keysym
Definition: SDL_events.h:220
SDL_Renderer * screen
Definition: testjoystick.c:35
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 SDL_JoystickNumHats
#define SDL_JoystickGetAxis
#define SDL_RenderClear
SDL_KeyboardEvent key
Definition: SDL_events.h:563
SDL_Keycode sym
Definition: SDL_keyboard.h:50
GLuint GLfloat x0
General event structure.
Definition: SDL_events.h:557
#define SDL_SetRenderDrawColor
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_JoyHatEvent jhat
Definition: SDL_events.h:571
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:329
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
#define SDL_HAT_UP
Definition: SDL_joystick.h:330
#define SDL_HAT_DOWN
Definition: SDL_joystick.h:332
SDL_JoystickID which
Definition: SDL_events.h:336
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:559

§ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 244 of file testjoystick.c.

References device, i, NULL, SDL_assert, SDL_FALSE, SDL_FINGERDOWN, SDL_GetError, SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_Init, SDL_INIT_JOYSTICK, SDL_INIT_VIDEO, SDL_JOYDEVICEADDED, SDL_JOYSTICK_TYPE_ARCADE_PAD, SDL_JOYSTICK_TYPE_ARCADE_STICK, SDL_JOYSTICK_TYPE_DANCE_PAD, SDL_JOYSTICK_TYPE_DRUM_KIT, SDL_JOYSTICK_TYPE_FLIGHT_STICK, SDL_JOYSTICK_TYPE_GAMECONTROLLER, SDL_JOYSTICK_TYPE_GUITAR, SDL_JOYSTICK_TYPE_THROTTLE, SDL_JOYSTICK_TYPE_WHEEL, SDL_JoystickClose, SDL_JoystickFromInstanceID, SDL_JoystickGetGUID, SDL_JoystickGetGUIDString, SDL_JoystickGetProduct, SDL_JoystickGetType, SDL_JoystickGetVendor, SDL_JoystickInstanceID, SDL_JoystickNameForIndex, SDL_JoystickNumAxes, SDL_JoystickNumBalls, SDL_JoystickNumButtons, SDL_JoystickNumHats, SDL_JoystickOpen, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_MOUSEBUTTONDOWN, SDL_NumJoysticks, SDL_QUIT, SDL_QuitSubSystem, SDL_SetHint, SDL_TRUE, SDL_WaitEvent, SDL_Event::type, and WatchJoystick().

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) {
264  name = SDL_JoystickNameForIndex(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) {
356  SDL_WaitEvent(&event);
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 }
#define SDL_GetError
#define SDL_JoystickClose
#define SDL_JoystickGetVendor
#define SDL_JoystickGetGUID
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
#define SDL_QuitSubSystem
#define SDL_JoystickNameForIndex
#define SDL_JoystickOpen
#define SDL_NumJoysticks
#define SDL_JoystickNumButtons
#define SDL_JoystickGetGUIDString
#define SDL_JoystickInstanceID
#define SDL_JoystickGetProduct
GLuint const GLchar * name
#define SDL_LogError
#define SDL_JoystickNumAxes
static SDL_AudioDeviceID device
Definition: loopwave.c:37
#define SDL_JoystickGetType
#define SDL_Log
#define SDL_JoystickFromInstanceID
#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:399
struct _cl_event * event
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 SDL_assert(condition)
Definition: SDL_assert.h:169
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:161
#define SDL_JoystickNumHats
#define SDL_WaitEvent
#define SDL_SetHint
#define SDL_JoystickNumBalls
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
#define SDL_Init
static SDL_bool WatchJoystick(SDL_Joystick *joystick)
Definition: testjoystick.c:191
General event structure.
Definition: SDL_events.h:557
#define SDL_INIT_VIDEO
Definition: SDL.h:79
Uint32 type
Definition: SDL_events.h:559

§ WatchJoystick()

static SDL_bool WatchJoystick ( SDL_Joystick *  joystick)
static

Definition at line 191 of file testjoystick.c.

References done, loop(), NULL, retval, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_ALPHA_OPAQUE, SDL_CreateRenderer, SDL_CreateWindow, SDL_DestroyRenderer, SDL_DestroyWindow, SDL_FALSE, SDL_GetError, SDL_JoystickInstanceID, SDL_JoystickName, SDL_JoystickNumAxes, SDL_JoystickNumBalls, SDL_JoystickNumButtons, SDL_JoystickNumHats, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_RaiseWindow, SDL_RenderClear, SDL_RenderPresent, SDL_SetRenderDrawColor, and SDL_WINDOWPOS_CENTERED.

Referenced by main().

192 {
194  const char *name = NULL;
195 
196  retval = SDL_FALSE;
197  done = SDL_FALSE;
198 
199  /* Create a window to display joystick axis position */
200  window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
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 
208  screen = SDL_CreateRenderer(window, -1, 0);
209  if (screen == NULL) {
210  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
211  SDL_DestroyWindow(window);
212  return SDL_FALSE;
213  }
214 
215  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
218  SDL_RaiseWindow(window);
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;
239  SDL_DestroyWindow(window);
240  return retval;
241 }
SDL_bool done
Definition: testjoystick.c:37
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
#define SDL_GetError
#define SDL_JoystickNumButtons
#define SDL_CreateWindow
#define SDL_JoystickInstanceID
GLuint const GLchar * name
#define SDL_JoystickName
#define SDL_LogError
#define SDL_JoystickNumAxes
#define SCREEN_WIDTH
Definition: testjoystick.c:31
#define SDL_Log
#define SDL_RaiseWindow
#define SCREEN_HEIGHT
Definition: testjoystick.c:32
SDL_bool retval
Definition: testjoystick.c:36
SDL_Renderer * screen
Definition: testjoystick.c:35
#define NULL
Definition: begin_code.h:164
#define SDL_JoystickNumHats
#define SDL_RenderClear
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_JoystickNumBalls
void loop(void *arg)
Definition: testjoystick.c:47
#define SDL_SetRenderDrawColor
#define SDL_DestroyRenderer
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
#define SDL_DestroyWindow
#define SDL_CreateRenderer
#define SDL_RenderPresent

Variable Documentation

§ done

Definition at line 37 of file testjoystick.c.

Referenced by loop(), and WatchJoystick().

§ retval

SDL_bool retval = SDL_FALSE

Definition at line 36 of file testjoystick.c.

Referenced by loop(), and WatchJoystick().

§ screen

SDL_Renderer* screen = NULL

Definition at line 35 of file testjoystick.c.