| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 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 |
|
| 35 |
static SDL_Window *window = NULL; |
| 36 |
static SDL_Renderer *screen = NULL; |
| 37 |
static SDL_Joystick *joystick = NULL; |
| 38 |
static SDL_bool done = SDL_FALSE; |
| 39 |
|
| 40 |
static void |
| 41 |
PrintJoystick(SDL_Joystick *joy) |
| 42 |
{ |
| 43 |
const char *type; |
| 44 |
char guid[64]; |
| 45 |
|
| 46 |
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joy)) == joy); |
| 47 |
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, sizeof(guid)); |
| 48 |
switch (SDL_JoystickGetType(joy)) { |
| 49 |
case SDL_JOYSTICK_TYPE_GAMECONTROLLER: |
| 50 |
type = "Game Controller"; |
| 51 |
break; |
| 52 |
case SDL_JOYSTICK_TYPE_WHEEL: |
| 53 |
type = "Wheel"; |
| 54 |
break; |
| 55 |
case SDL_JOYSTICK_TYPE_ARCADE_STICK: |
| 56 |
type = "Arcade Stick"; |
| 57 |
break; |
| 58 |
case SDL_JOYSTICK_TYPE_FLIGHT_STICK: |
| 59 |
type = "Flight Stick"; |
| 60 |
break; |
| 61 |
case SDL_JOYSTICK_TYPE_DANCE_PAD: |
| 62 |
type = "Dance Pad"; |
| 63 |
break; |
| 64 |
case SDL_JOYSTICK_TYPE_GUITAR: |
| 65 |
type = "Guitar"; |
| 66 |
break; |
| 67 |
case SDL_JOYSTICK_TYPE_DRUM_KIT: |
| 68 |
type = "Drum Kit"; |
| 69 |
break; |
| 70 |
case SDL_JOYSTICK_TYPE_ARCADE_PAD: |
| 71 |
type = "Arcade Pad"; |
| 72 |
break; |
| 73 |
case SDL_JOYSTICK_TYPE_THROTTLE: |
| 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(joy)); |
| 82 |
SDL_Log(" type: %s\n", type); |
| 83 |
SDL_Log(" LED: %s\n", SDL_JoystickHasLED(joy) ? "yes" : "no"); |
| 84 |
SDL_Log(" rumble: %s\n", SDL_JoystickHasRumble(joy) ? "yes" : "no"); |
| 85 |
SDL_Log("trigger rumble: %s\n", SDL_JoystickHasRumbleTriggers(joy) ? "yes" : "no"); |
| 86 |
SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joy)); |
| 87 |
SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joy)); |
| 88 |
SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joy)); |
| 89 |
SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joy)); |
| 90 |
SDL_Log(" instance id: %" SDL_PRIs32 "\n", SDL_JoystickInstanceID(joy)); |
| 91 |
SDL_Log(" guid: %s\n", guid); |
| 92 |
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joy), SDL_JoystickGetProduct(joy)); |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h) |
| 97 |
{ |
| 98 |
SDL_Rect area; |
| 99 |
area.x = x; |
| 100 |
area.y = y; |
| 101 |
area.w = w; |
| 102 |
area.h = h; |
| 103 |
SDL_RenderFillRect(r, &area); |
| 104 |
} |
| 105 |
|
| 106 |
void loop(void *arg) |
| 107 |
{ |
| 108 |
SDL_Event event; |
| 109 |
int i; |
| 110 |
|
| 111 |
/* blank screen, set up for drawing this frame. */ |
| 112 |
SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE); |
| 113 |
SDL_RenderClear(screen); |
| 114 |
|
| 115 |
while (SDL_PollEvent(&event)) { |
| 116 |
switch (event.type) { |
| 117 |
|
| 118 |
case SDL_JOYDEVICEADDED: |
| 119 |
SDL_Log("Joystick device %d added.\n", (int)event.jdevice.which); |
| 120 |
if (!joystick) { |
| 121 |
joystick = SDL_JoystickOpen(event.jdevice.which); |
| 122 |
if (joystick) { |
| 123 |
PrintJoystick(joystick); |
| 124 |
} else { |
| 125 |
SDL_Log("Couldn't open joystick: %s\n", SDL_GetError()); |
| 126 |
} |
| 127 |
} |
| 128 |
break; |
| 129 |
|
| 130 |
case SDL_JOYDEVICEREMOVED: |
| 131 |
SDL_Log("Joystick device %d removed.\n", (int)event.jdevice.which); |
| 132 |
if (event.jdevice.which == SDL_JoystickInstanceID(joystick)) { |
| 133 |
SDL_JoystickClose(joystick); |
| 134 |
joystick = SDL_JoystickOpen(0); |
| 135 |
} |
| 136 |
break; |
| 137 |
|
| 138 |
case SDL_JOYAXISMOTION: |
| 139 |
SDL_Log("Joystick %" SDL_PRIs32 " axis %d value: %d\n", |
| 140 |
event.jaxis.which, |
| 141 |
event.jaxis.axis, event.jaxis.value); |
| 142 |
break; |
| 143 |
case SDL_JOYHATMOTION: |
| 144 |
SDL_Log("Joystick %" SDL_PRIs32 " hat %d value:", |
| 145 |
event.jhat.which, event.jhat.hat); |
| 146 |
if (event.jhat.value == SDL_HAT_CENTERED) { |
| 147 |
SDL_Log(" centered"); |
| 148 |
} |
| 149 |
if (event.jhat.value & SDL_HAT_UP) { |
| 150 |
SDL_Log(" up"); |
| 151 |
} |
| 152 |
if (event.jhat.value & SDL_HAT_RIGHT) { |
| 153 |
SDL_Log(" right"); |
| 154 |
} |
| 155 |
if (event.jhat.value & SDL_HAT_DOWN) { |
| 156 |
SDL_Log(" down"); |
| 157 |
} |
| 158 |
if (event.jhat.value & SDL_HAT_LEFT) { |
| 159 |
SDL_Log(" left"); |
| 160 |
} |
| 161 |
SDL_Log("\n"); |
| 162 |
break; |
| 163 |
case SDL_JOYBALLMOTION: |
| 164 |
SDL_Log("Joystick %" SDL_PRIs32 " ball %d delta: (%d,%d)\n", |
| 165 |
event.jball.which, |
| 166 |
event.jball.ball, event.jball.xrel, event.jball.yrel); |
| 167 |
break; |
| 168 |
case SDL_JOYBUTTONDOWN: |
| 169 |
SDL_Log("Joystick %" SDL_PRIs32 " button %d down\n", |
| 170 |
event.jbutton.which, event.jbutton.button); |
| 171 |
/* First button triggers a 0.5 second full strength rumble */ |
| 172 |
if (event.jbutton.button == 0) { |
| 173 |
SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500); |
| 174 |
} |
| 175 |
break; |
| 176 |
case SDL_JOYBUTTONUP: |
| 177 |
SDL_Log("Joystick %" SDL_PRIs32 " button %d up\n", |
| 178 |
event.jbutton.which, event.jbutton.button); |
| 179 |
break; |
| 180 |
case SDL_KEYDOWN: |
| 181 |
/* Press the L key to lag for 3 seconds, to see what happens |
| 182 |
when SDL doesn't service the event loop quickly. */ |
| 183 |
if (event.key.keysym.sym == SDLK_l) { |
| 184 |
SDL_Log("Lagging for 3 seconds...\n"); |
| 185 |
SDL_Delay(3000); |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
if ((event.key.keysym.sym != SDLK_ESCAPE) && |
| 190 |
(event.key.keysym.sym != SDLK_AC_BACK)) { |
| 191 |
break; |
| 192 |
} |
| 193 |
SDL_FALLTHROUGH; |
| 194 |
case SDL_FINGERDOWN: |
| 195 |
case SDL_MOUSEBUTTONDOWN: |
| 196 |
case SDL_QUIT: |
| 197 |
done = SDL_TRUE; |
| 198 |
break; |
| 199 |
default: |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if (joystick) { |
| 205 |
const int BUTTONS_PER_LINE = ((SCREEN_WIDTH - 4) / 34); |
| 206 |
int x, y; |
| 207 |
|
| 208 |
/* Update visual joystick state */ |
| 209 |
SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE); |
| 210 |
y = SCREEN_HEIGHT - ((((SDL_JoystickNumButtons(joystick) + (BUTTONS_PER_LINE - 1)) / BUTTONS_PER_LINE) + 1) * 34); |
| 211 |
for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) { |
| 212 |
if ((i % BUTTONS_PER_LINE) == 0) { |
| 213 |
y += 34; |
| 214 |
} |
| 215 |
if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) { |
| 216 |
x = 2 + (i % BUTTONS_PER_LINE) * 34; |
| 217 |
DrawRect(screen, x, y, 32, 32); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE); |
| 222 |
for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) { |
| 223 |
/* Draw the X/Y axis */ |
| 224 |
x = (((int)SDL_JoystickGetAxis(joystick, i)) + 32768); |
| 225 |
x *= SCREEN_WIDTH; |
| 226 |
x /= 65535; |
| 227 |
if (x < 0) { |
| 228 |
x = 0; |
| 229 |
} else if (x > (SCREEN_WIDTH - 16)) { |
| 230 |
x = SCREEN_WIDTH - 16; |
| 231 |
} |
| 232 |
++i; |
| 233 |
if (i < SDL_JoystickNumAxes(joystick)) { |
| 234 |
y = (((int)SDL_JoystickGetAxis(joystick, i)) + 32768); |
| 235 |
} else { |
| 236 |
y = 32768; |
| 237 |
} |
| 238 |
y *= SCREEN_HEIGHT; |
| 239 |
y /= 65535; |
| 240 |
if (y < 0) { |
| 241 |
y = 0; |
| 242 |
} else if (y > (SCREEN_HEIGHT - 16)) { |
| 243 |
y = SCREEN_HEIGHT - 16; |
| 244 |
} |
| 245 |
|
| 246 |
DrawRect(screen, x, y, 16, 16); |
| 247 |
} |
| 248 |
|
| 249 |
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE); |
| 250 |
for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) { |
| 251 |
/* Derive the new position */ |
| 252 |
const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i); |
| 253 |
x = SCREEN_WIDTH / 2; |
| 254 |
y = SCREEN_HEIGHT / 2; |
| 255 |
|
| 256 |
if (hat_pos & SDL_HAT_UP) { |
| 257 |
y = 0; |
| 258 |
} else if (hat_pos & SDL_HAT_DOWN) { |
| 259 |
y = SCREEN_HEIGHT - 8; |
| 260 |
} |
| 261 |
|
| 262 |
if (hat_pos & SDL_HAT_LEFT) { |
| 263 |
x = 0; |
| 264 |
} else if (hat_pos & SDL_HAT_RIGHT) { |
| 265 |
x = SCREEN_WIDTH - 8; |
| 266 |
} |
| 267 |
|
| 268 |
DrawRect(screen, x, y, 8, 8); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
SDL_Delay(16); |
| 273 |
SDL_RenderPresent(screen); |
| 274 |
|
| 275 |
#ifdef __EMSCRIPTEN__ |
| 276 |
if (done) { |
| 277 |
emscripten_cancel_main_loop(); |
| 278 |
} |
| 279 |
#endif |
| 280 |
} |
| 281 |
|
| 282 |
int main(int argc, char *argv[]) |
| 283 |
{ |
| 284 |
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); |
| 285 |
|
| 286 |
/* Enable standard application logging */ |
| 287 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 288 |
|
| 289 |
/* Initialize SDL (Note: video is required to start event loop) */ |
| 290 |
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { |
| 291 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 292 |
exit(1); |
| 293 |
} |
| 294 |
|
| 295 |
/* Create a window to display joystick axis position */ |
| 296 |
window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED, |
| 297 |
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, |
| 298 |
SCREEN_HEIGHT, 0); |
| 299 |
if (!window) { |
| 300 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError()); |
| 301 |
return SDL_FALSE; |
| 302 |
} |
| 303 |
|
| 304 |
screen = SDL_CreateRenderer(window, -1, 0); |
| 305 |
if (!screen) { |
| 306 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError()); |
| 307 |
SDL_DestroyWindow(window); |
| 308 |
return SDL_FALSE; |
| 309 |
} |
| 310 |
|
| 311 |
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); |
| 312 |
SDL_RenderClear(screen); |
| 313 |
SDL_RenderPresent(screen); |
| 314 |
|
| 315 |
/* Loop, getting joystick events! */ |
| 316 |
#ifdef __EMSCRIPTEN__ |
| 317 |
emscripten_set_main_loop_arg(loop, NULL, 0, 1); |
| 318 |
#else |
| 319 |
while (!done) { |
| 320 |
loop(NULL); |
| 321 |
} |
| 322 |
#endif |
| 323 |
|
| 324 |
SDL_DestroyRenderer(screen); |
| 325 |
SDL_DestroyWindow(window); |
| 326 |
|
| 327 |
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
| 328 |
|
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
#else |
| 333 |
|
| 334 |
int |
| 335 |
main(int argc, char *argv[]) |
| 336 |
{ |
| 337 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n"); |
| 338 |
return 1; |
| 339 |
} |
| 340 |
|
| 341 |
#endif |
| 342 |
|
| 343 |
/* vi: set ts=4 sw=4 expandtab: */ |