SDL  2.0
testjoystick.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2020 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 
37 static SDL_Joystick *joystick = NULL;
39 
40 static void
41 PrintJoystick(SDL_Joystick *joystick)
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 }
91 
92 static void
93 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
94 {
95  const SDL_Rect area = { x, y, w, h };
96  SDL_RenderFillRect(r, &area);
97 }
98 
99 void
100 loop(void *arg)
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 }
263 
264 int
265 main(int argc, char *argv[])
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 }
314 
315 #else
316 
317 int
318 main(int argc, char *argv[])
319 {
320  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
321  return 1;
322 }
323 
324 #endif
325 
326 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_INIT_JOYSTICK
Definition: SDL.h:83
#define SDL_INIT_VIDEO
Definition: SDL.h:82
#define SDL_assert(condition)
Definition: SDL_assert.h:171
#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_JoystickGetAxis
#define SDL_JoystickClose
#define SDL_JoystickRumble
#define SDL_JoystickGetHat
#define SDL_JoystickGetGUIDString
#define SDL_JoystickOpen
#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_JoystickGetButton
#define SDL_JoystickNumBalls
#define SDL_Delay
#define SDL_Init
#define SDL_JoystickInstanceID
#define SDL_Log
#define SDL_QuitSubSystem
#define SDL_SetHint
#define SDL_JoystickGetProduct
@ 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_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_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: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
@ 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
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:168
@ SDL_TRUE
Definition: SDL_stdinc.h:170
@ SDL_FALSE
Definition: SDL_stdinc.h:169
uint8_t Uint8
Definition: SDL_stdinc.h:185
#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:163
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
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:75
int main(int argc, char *argv[])
Definition: testjoystick.c:265
#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
void loop(void *arg)
Definition: testjoystick.c:100
General event structure.
Definition: SDL_events.h:592
typedef int(__stdcall *FARPROC)()