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_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  break;
94  case SDL_JOYBUTTONUP:
95  SDL_Log("Joystick %d button %d up\n",
96  event.jbutton.which, event.jbutton.button);
97  break;
98  case SDL_KEYDOWN:
99  if ((event.key.keysym.sym != SDLK_ESCAPE) &&
100  (event.key.keysym.sym != SDLK_AC_BACK)) {
101  break;
102  }
103  /* Fall through to signal quit */
104  case SDL_FINGERDOWN:
105  case SDL_MOUSEBUTTONDOWN:
106  case SDL_QUIT:
107  done = SDL_TRUE;
108  break;
109  default:
110  break;
111  }
112  }
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);
118  }
119  }
120 
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 */
124  int x, y;
125  x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
126  x *= SCREEN_WIDTH;
127  x /= 65535;
128  if (x < 0) {
129  x = 0;
130  } else if (x > (SCREEN_WIDTH - 16)) {
131  x = SCREEN_WIDTH - 16;
132  }
133  ++i;
134  if (i < SDL_JoystickNumAxes(joystick)) {
135  y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
136  } else {
137  y = 32768;
138  }
139  y *= SCREEN_HEIGHT;
140  y /= 65535;
141  if (y < 0) {
142  y = 0;
143  } else if (y > (SCREEN_HEIGHT - 16)) {
144  y = SCREEN_HEIGHT - 16;
145  }
146 
147  DrawRect(screen, x, y, 16, 16);
148  }
149 
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);
156 
157  if (hat_pos & SDL_HAT_UP) {
158  y = 0;
159  } else if (hat_pos & SDL_HAT_DOWN) {
160  y = SCREEN_HEIGHT-8;
161  }
162 
163  if (hat_pos & SDL_HAT_LEFT) {
164  x = 0;
165  } else if (hat_pos & SDL_HAT_RIGHT) {
166  x = SCREEN_WIDTH-8;
167  }
168 
169  DrawRect(screen, x, y, 8, 8);
170  }
171 
173 
174  if (SDL_JoystickGetAttached( joystick ) == 0) {
175  done = SDL_TRUE;
176  retval = SDL_TRUE; /* keep going, wait for reattach. */
177  }
178 
179 #ifdef __EMSCRIPTEN__
180  if (done) {
181  emscripten_cancel_main_loop();
182  }
183 #endif
184 }
SDL_bool done
Definition: testjoystick.c:37
SDL_JoystickID which
Definition: SDL_events.h:335
#define SDL_PollEvent
#define SDL_JoystickGetButton
SDL_JoyDeviceEvent jdevice
Definition: SDL_events.h:540
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_JoyButtonEvent jbutton
Definition: SDL_events.h:539
SDL_JoyBallEvent jball
Definition: SDL_events.h:537
SDL_JoystickID which
Definition: SDL_events.h:283
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:318
SDL_JoystickID which
Definition: SDL_events.h:299
#define SDL_Log
#define SDL_HAT_LEFT
Definition: SDL_joystick.h:320
SDL_JoyAxisEvent jaxis
Definition: SDL_events.h:536
uint8_t Uint8
Definition: SDL_stdinc.h:157
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
SDL_Keysym keysym
Definition: SDL_events.h:199
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:530
SDL_Keycode sym
Definition: SDL_keyboard.h:50
GLuint GLfloat x0
General event structure.
Definition: SDL_events.h:525
#define SDL_SetRenderDrawColor
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_JoyHatEvent jhat
Definition: SDL_events.h:538
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:316
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
#define SDL_HAT_UP
Definition: SDL_joystick.h:317
#define SDL_HAT_DOWN
Definition: SDL_joystick.h:319
SDL_JoystickID which
Definition: SDL_events.h:315
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:527

◆ main()

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

Definition at line 240 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().

241 {
242  const char *name, *type;
243  int i;
244  SDL_Joystick *joystick;
245 
247 
248  /* Enable standard application logging */
250 
251  /* Initialize SDL (Note: video is required to start event loop) */
253  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
254  exit(1);
255  }
256 
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,
265  SDL_GetError());
266  } else {
267  char guid[64];
270  guid, sizeof (guid));
271  switch (SDL_JoystickGetType(joystick)) {
273  type = "Game Controller";
274  break;
276  type = "Wheel";
277  break;
279  type = "Arcade Stick";
280  break;
282  type = "Flight Stick";
283  break;
285  type = "Dance Pad";
286  break;
288  type = "Guitar";
289  break;
291  type = "Drum Kit";
292  break;
294  type = "Arcade Pad";
295  break;
297  type = "Throttle";
298  break;
299  default:
300  type = "Unknown";
301  break;
302  }
303  SDL_Log(" type: %s\n", type);
304  SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
305  SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
306  SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
307  SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
308  SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
309  SDL_Log(" guid: %s\n", guid);
310  SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
311  SDL_JoystickClose(joystick);
312  }
313  }
314 
315 #if defined(__ANDROID__) || defined(__IPHONEOS__)
316  if (SDL_NumJoysticks() > 0) {
317 #else
318  if (argv[1]) {
319 #endif
320  SDL_bool reportederror = SDL_FALSE;
321  SDL_bool keepGoing = SDL_TRUE;
323  int device;
324 #if defined(__ANDROID__) || defined(__IPHONEOS__)
325  device = 0;
326 #else
327  device = atoi(argv[1]);
328 #endif
329  joystick = SDL_JoystickOpen(device);
330  if (joystick != NULL) {
332  }
333 
334  while ( keepGoing ) {
335  if (joystick == NULL) {
336  if ( !reportederror ) {
337  SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
338  keepGoing = SDL_FALSE;
339  reportederror = SDL_TRUE;
340  }
341  } else {
342  reportederror = SDL_FALSE;
343  keepGoing = WatchJoystick(joystick);
344  SDL_JoystickClose(joystick);
345  }
346 
347  joystick = NULL;
348  if (keepGoing) {
349  SDL_Log("Waiting for attach\n");
350  }
351  while (keepGoing) {
352  SDL_WaitEvent(&event);
353  if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
354  || (event.type == SDL_MOUSEBUTTONDOWN)) {
355  keepGoing = SDL_FALSE;
356  } else if (event.type == SDL_JOYDEVICEADDED) {
357  device = event.jdevice.which;
358  joystick = SDL_JoystickOpen(device);
359  if (joystick != NULL) {
361  }
362  break;
363  }
364  }
365  }
366  }
368 
369  return 0;
370 }
#define SDL_GetError
#define SDL_JoystickClose
#define SDL_JoystickGetVendor
#define SDL_JoystickGetGUID
#define SDL_INIT_JOYSTICK
Definition: SDL.h:79
#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:389
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:139
#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:187
General event structure.
Definition: SDL_events.h:525
#define SDL_INIT_VIDEO
Definition: SDL.h:78
Uint32 type
Definition: SDL_events.h:527

◆ WatchJoystick()

static SDL_bool WatchJoystick ( SDL_Joystick *  joystick)
static

Definition at line 187 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().

188 {
190  const char *name = NULL;
191 
192  retval = SDL_FALSE;
193  done = SDL_FALSE;
194 
195  /* Create a window to display joystick axis position */
196  window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
198  SCREEN_HEIGHT, 0);
199  if (window == NULL) {
200  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
201  return SDL_FALSE;
202  }
203 
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);
208  return SDL_FALSE;
209  }
210 
211  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
214  SDL_RaiseWindow(window);
215 
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));
223 
224  /* Loop, getting joystick events! */
225 #ifdef __EMSCRIPTEN__
226  emscripten_set_main_loop_arg(loop, joystick, 0, 1);
227 #else
228  while (!done) {
229  loop(joystick);
230  }
231 #endif
232 
234  screen = NULL;
235  SDL_DestroyWindow(window);
236  return retval;
237 }
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.