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 PrintJoystick (SDL_Joystick *joystick)
 
static void DrawRect (SDL_Renderer *r, const int x, const int y, const int w, const int h)
 
void loop (void *arg)
 
int main (int argc, char *argv[])
 

Variables

static SDL_Windowwindow = NULL
 
static SDL_Rendererscreen = NULL
 
static SDL_Joystick * joystick = NULL
 
static SDL_bool done = SDL_FALSE
 

Macro Definition Documentation

◆ SCREEN_HEIGHT

#define SCREEN_HEIGHT   480

Definition at line 32 of file testjoystick.c.

◆ SCREEN_WIDTH

#define SCREEN_WIDTH   640

Definition at line 31 of file testjoystick.c.

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 93 of file testjoystick.c.

94 {
95  const SDL_Rect area = { x, y, w, h };
96  SDL_RenderFillRect(r, &area);
97 }
#define SDL_RenderFillRect
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
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78

References SDL_RenderFillRect.

Referenced by loop().

◆ loop()

void loop ( void arg)

Definition at line 100 of file testjoystick.c.

101 {
103  int i;
104 
105  /* blank screen, set up for drawing this frame. */
108 
109  while (SDL_PollEvent(&event)) {
110  switch (event.type) {
111 
112  case SDL_JOYDEVICEADDED:
113  SDL_Log("Joystick device %d added.\n", (int) event.jdevice.which);
114  if (!joystick) {
115  joystick = SDL_JoystickOpen(event.jdevice.which);
116  if (joystick) {
118  } else {
119  SDL_Log("Couldn't open joystick: %s\n", SDL_GetError());
120  }
121  }
122  break;
123 
125  SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
126  if (event.jdevice.which == SDL_JoystickInstanceID(joystick)) {
129  }
130  break;
131 
132  case SDL_JOYAXISMOTION:
133  SDL_Log("Joystick %d axis %d value: %d\n",
134  event.jaxis.which,
135  event.jaxis.axis, event.jaxis.value);
136  break;
137  case SDL_JOYHATMOTION:
138  SDL_Log("Joystick %d hat %d value:",
139  event.jhat.which, event.jhat.hat);
140  if (event.jhat.value == SDL_HAT_CENTERED)
141  SDL_Log(" centered");
142  if (event.jhat.value & SDL_HAT_UP)
143  SDL_Log(" up");
144  if (event.jhat.value & SDL_HAT_RIGHT)
145  SDL_Log(" right");
146  if (event.jhat.value & SDL_HAT_DOWN)
147  SDL_Log(" down");
148  if (event.jhat.value & SDL_HAT_LEFT)
149  SDL_Log(" left");
150  SDL_Log("\n");
151  break;
152  case SDL_JOYBALLMOTION:
153  SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
154  event.jball.which,
155  event.jball.ball, event.jball.xrel, event.jball.yrel);
156  break;
157  case SDL_JOYBUTTONDOWN:
158  SDL_Log("Joystick %d button %d down\n",
159  event.jbutton.which, event.jbutton.button);
160  /* First button triggers a 0.5 second full strength rumble */
161  if (event.jbutton.button == 0) {
162  SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500);
163  }
164  break;
165  case SDL_JOYBUTTONUP:
166  SDL_Log("Joystick %d button %d up\n",
167  event.jbutton.which, event.jbutton.button);
168  break;
169  case SDL_KEYDOWN:
170  /* Press the L key to lag for 3 seconds, to see what happens
171  when SDL doesn't service the event loop quickly. */
172  if (event.key.keysym.sym == SDLK_l) {
173  SDL_Log("Lagging for 3 seconds...\n");
174  SDL_Delay(3000);
175  break;
176  }
177 
178  if ((event.key.keysym.sym != SDLK_ESCAPE) &&
179  (event.key.keysym.sym != SDLK_AC_BACK)) {
180  break;
181  }
182  /* Fall through to signal quit */
183  case SDL_FINGERDOWN:
184  case SDL_MOUSEBUTTONDOWN:
185  case SDL_QUIT:
186  done = SDL_TRUE;
187  break;
188  default:
189  break;
190  }
191  }
192 
193  if (joystick) {
194 
195  /* Update visual joystick state */
196  SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
197  for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
199  DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
200  }
201  }
202 
203  SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
204  for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
205  /* Draw the X/Y axis */
206  int x, y;
207  x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
208  x *= SCREEN_WIDTH;
209  x /= 65535;
210  if (x < 0) {
211  x = 0;
212  } else if (x > (SCREEN_WIDTH - 16)) {
213  x = SCREEN_WIDTH - 16;
214  }
215  ++i;
216  if (i < SDL_JoystickNumAxes(joystick)) {
217  y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
218  } else {
219  y = 32768;
220  }
221  y *= SCREEN_HEIGHT;
222  y /= 65535;
223  if (y < 0) {
224  y = 0;
225  } else if (y > (SCREEN_HEIGHT - 16)) {
226  y = SCREEN_HEIGHT - 16;
227  }
228 
229  DrawRect(screen, x, y, 16, 16);
230  }
231 
232  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
233  for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
234  /* Derive the new position */
235  int x = SCREEN_WIDTH/2;
236  int y = SCREEN_HEIGHT/2;
237  const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
238 
239  if (hat_pos & SDL_HAT_UP) {
240  y = 0;
241  } else if (hat_pos & SDL_HAT_DOWN) {
242  y = SCREEN_HEIGHT-8;
243  }
244 
245  if (hat_pos & SDL_HAT_LEFT) {
246  x = 0;
247  } else if (hat_pos & SDL_HAT_RIGHT) {
248  x = SCREEN_WIDTH-8;
249  }
250 
251  DrawRect(screen, x, y, 8, 8);
252  }
253  }
254 
256 
257 #ifdef __EMSCRIPTEN__
258  if (done) {
259  emscripten_cancel_main_loop();
260  }
261 #endif
262 }
#define SDL_RenderPresent
#define SDL_JoystickNumAxes
#define SDL_GetError
#define SDL_PollEvent
#define SDL_SetRenderDrawColor
#define SDL_JoystickNumHats
#define SDL_JoystickGetAxis
#define SDL_JoystickClose
#define SDL_JoystickRumble
#define SDL_JoystickGetHat
#define SDL_JoystickOpen
#define SDL_JoystickNumButtons
#define SDL_RenderClear
#define SDL_JoystickGetButton
#define SDL_Delay
#define SDL_JoystickInstanceID
#define SDL_Log
@ SDL_JOYDEVICEADDED
Definition: SDL_events.h:118
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_JOYBUTTONDOWN
Definition: SDL_events.h:116
@ SDL_JOYDEVICEREMOVED
Definition: SDL_events.h:119
@ SDL_JOYBUTTONUP
Definition: SDL_events.h:117
@ SDL_JOYBALLMOTION
Definition: SDL_events.h:114
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:108
@ SDL_FINGERDOWN
Definition: SDL_events.h:134
@ SDL_KEYDOWN
Definition: SDL_events.h:98
@ SDL_JOYAXISMOTION
Definition: SDL_events.h:113
@ SDL_JOYHATMOTION
Definition: SDL_events.h:115
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_HAT_LEFT
Definition: SDL_joystick.h:390
#define SDL_HAT_RIGHT
Definition: SDL_joystick.h:388
#define SDL_HAT_DOWN
Definition: SDL_joystick.h:389
#define SDL_HAT_UP
Definition: SDL_joystick.h:387
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:386
@ SDLK_AC_BACK
Definition: SDL_keycode.h:301
@ SDLK_ESCAPE
Definition: SDL_keycode.h:55
@ SDLK_l
Definition: SDL_keycode.h:113
struct _cl_event * event
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
@ SDL_TRUE
Definition: SDL_stdinc.h:170
uint8_t Uint8
Definition: SDL_stdinc.h:185
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 SCREEN_WIDTH
Definition: testjoystick.c:31
static void PrintJoystick(SDL_Joystick *joystick)
Definition: testjoystick.c:41
static SDL_Renderer * screen
Definition: testjoystick.c:36
static SDL_Joystick * joystick
Definition: testjoystick.c:37
#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:93
static SDL_bool done
Definition: testjoystick.c:38
General event structure.
Definition: SDL_events.h:592
typedef int(__stdcall *FARPROC)()

References done, DrawRect(), i, int(), joystick, PrintJoystick(), screen, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_ALPHA_OPAQUE, SDL_Delay, SDL_FINGERDOWN, SDL_GetError, SDL_HAT_CENTERED, SDL_HAT_DOWN, SDL_HAT_LEFT, SDL_HAT_RIGHT, SDL_HAT_UP, SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED, SDL_JOYHATMOTION, SDL_JoystickClose, SDL_JoystickGetAxis, SDL_JoystickGetButton, SDL_JoystickGetHat, SDL_JoystickInstanceID, SDL_JoystickNumAxes, SDL_JoystickNumButtons, SDL_JoystickNumHats, SDL_JoystickOpen, 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, and SDLK_l.

Referenced by main().

◆ main()

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

Definition at line 265 of file testjoystick.c.

266 {
268 
269  /* Enable standard application logging */
271 
272  /* Initialize SDL (Note: video is required to start event loop) */
274  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
275  exit(1);
276  }
277 
278  /* Create a window to display joystick axis position */
281  SCREEN_HEIGHT, 0);
282  if (window == NULL) {
283  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
284  return SDL_FALSE;
285  }
286 
287  screen = SDL_CreateRenderer(window, -1, 0);
288  if (screen == NULL) {
289  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
291  return SDL_FALSE;
292  }
293 
294  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
297 
298  /* Loop, getting joystick events! */
299 #ifdef __EMSCRIPTEN__
300  emscripten_set_main_loop_arg(loop, NULL, 0, 1);
301 #else
302  while (!done) {
303  loop(NULL);
304  }
305 #endif
306 
309 
311 
312  return 0;
313 }
#define SDL_INIT_JOYSTICK
Definition: SDL.h:83
#define SDL_INIT_VIDEO
Definition: SDL.h:82
#define SDL_DestroyWindow
#define SDL_DestroyRenderer
#define SDL_CreateWindow
#define SDL_CreateRenderer
#define SDL_LogSetPriority
#define SDL_LogError
#define SDL_Init
#define SDL_QuitSubSystem
#define SDL_SetHint
#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:454
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
@ SDL_FALSE
Definition: SDL_stdinc.h:169
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
#define NULL
Definition: begin_code.h:163
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
void loop(void *arg)
Definition: testjoystick.c:100

References done, loop(), NULL, screen, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_ALPHA_OPAQUE, SDL_CreateRenderer, SDL_CreateWindow, SDL_DestroyRenderer, SDL_DestroyWindow, SDL_FALSE, SDL_GetError, SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_Init, SDL_INIT_JOYSTICK, SDL_INIT_VIDEO, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_QuitSubSystem, SDL_RenderClear, SDL_RenderPresent, SDL_SetHint, SDL_SetRenderDrawColor, and SDL_WINDOWPOS_CENTERED.

◆ PrintJoystick()

static void PrintJoystick ( SDL_Joystick *  joystick)
static

Definition at line 41 of file testjoystick.c.

42 {
43  const char *type;
44  char guid[64];
45 
48  switch (SDL_JoystickGetType(joystick)) {
50  type = "Game Controller";
51  break;
53  type = "Wheel";
54  break;
56  type = "Arcade Stick";
57  break;
59  type = "Flight Stick";
60  break;
62  type = "Dance Pad";
63  break;
65  type = "Guitar";
66  break;
68  type = "Drum Kit";
69  break;
71  type = "Arcade Pad";
72  break;
74  type = "Throttle";
75  break;
76  default:
77  type = "Unknown";
78  break;
79  }
80  SDL_Log("Joystick\n");
81  SDL_Log(" name: %s\n", SDL_JoystickName(joystick));
82  SDL_Log(" type: %s\n", type);
83  SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
84  SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
85  SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
86  SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
87  SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
88  SDL_Log(" guid: %s\n", guid);
89  SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
90 }
#define SDL_assert(condition)
Definition: SDL_assert.h:171
#define SDL_JoystickName
#define SDL_JoystickGetGUID
#define SDL_JoystickGetGUIDString
#define SDL_JoystickGetType
#define SDL_JoystickFromInstanceID
#define SDL_JoystickGetVendor
#define SDL_JoystickNumBalls
#define SDL_JoystickGetProduct
@ 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
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571

References joystick, SDL_assert, 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_JoystickFromInstanceID, SDL_JoystickGetGUID, SDL_JoystickGetGUIDString, SDL_JoystickGetProduct, SDL_JoystickGetType, SDL_JoystickGetVendor, SDL_JoystickInstanceID, SDL_JoystickName, SDL_JoystickNumAxes, SDL_JoystickNumBalls, SDL_JoystickNumButtons, SDL_JoystickNumHats, and SDL_Log.

Referenced by loop().

Variable Documentation

◆ done

SDL_bool done = SDL_FALSE
static

Definition at line 38 of file testjoystick.c.

Referenced by loop(), and main().

◆ joystick

SDL_Joystick* joystick = NULL
static

Definition at line 37 of file testjoystick.c.

Referenced by IOS_AccelerometerUpdate(), IOS_JoystickClose(), IOS_JoystickHasLED(), IOS_JoystickOpen(), IOS_JoystickRumble(), IOS_JoystickRumbleTriggers(), IOS_JoystickSetLED(), IOS_JoystickSetSensorsEnabled(), IOS_JoystickUpdate(), IOS_MFIJoystickUpdate(), loop(), main(), PrintJoystick(), SDL_GameControllerFromPlayerIndex(), SDL_GameControllerGetNumTouchpadFingers(), SDL_GameControllerGetNumTouchpads(), SDL_GameControllerGetSensorData(), SDL_GameControllerGetTouchpadFinger(), SDL_GameControllerHandleDelayedGuideButton(), SDL_GameControllerHasSensor(), SDL_GameControllerIsSensorEnabled(), SDL_GameControllerSetSensorEnabled(), SDL_HapticOpenFromJoystick(), SDL_JoystickAxesCenteredAtZero(), SDL_JoystickClose(), SDL_JoystickCurrentPowerLevel(), SDL_JoystickFromInstanceID(), SDL_JoystickFromPlayerIndex(), SDL_JoystickGetAttached(), SDL_JoystickGetAxis(), SDL_JoystickGetAxisInitialState(), SDL_JoystickGetBall(), SDL_JoystickGetButton(), SDL_JoystickGetGUID(), SDL_JoystickGetHat(), SDL_JoystickGetPlayerIndex(), SDL_JoystickGetProduct(), SDL_JoystickGetProductVersion(), SDL_JoystickGetSerial(), SDL_JoystickGetType(), SDL_JoystickGetVendor(), SDL_JoystickHasLED(), SDL_JoystickInstanceID(), SDL_JoystickIsHaptic(), SDL_JoystickName(), SDL_JoystickNumAxes(), SDL_JoystickNumBalls(), SDL_JoystickNumButtons(), SDL_JoystickNumHats(), SDL_JoystickOpen(), SDL_JoystickRumble(), SDL_JoystickRumbleTriggers(), SDL_JoystickSetLED(), SDL_JoystickSetPlayerIndex(), SDL_JoystickSetVirtualAxis(), SDL_JoystickSetVirtualButton(), SDL_JoystickSetVirtualHat(), SDL_JoystickUpdate(), SDL_PrivateJoystickAddSensor(), SDL_PrivateJoystickAddTouchpad(), SDL_PrivateJoystickAxis(), SDL_PrivateJoystickBall(), SDL_PrivateJoystickBatteryLevel(), SDL_PrivateJoystickButton(), SDL_PrivateJoystickForceRecentering(), SDL_PrivateJoystickHat(), SDL_PrivateJoystickRemoved(), SDL_PrivateJoystickSensor(), SDL_PrivateJoystickTouchpad(), SDL_PrivateJoystickValid(), and WatchJoystick().

◆ screen

SDL_Renderer* screen = NULL
static

Definition at line 36 of file testjoystick.c.

Referenced by loop(), and main().

◆ window

SDL_Window* window = NULL
static

Definition at line 35 of file testjoystick.c.