Atlas - testjoystick.c
Home / ext / SDL2 / test Lines: 27 | Size: 12943 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2018 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 35SDL_Renderer *screen = NULL; 36SDL_bool retval = SDL_FALSE; 37SDL_bool done = SDL_FALSE; 38 39static void 40DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h) 41{ 42 const SDL_Rect area = { x, y, w, h }; 43 SDL_RenderFillRect(r, &area); 44} 45 46void 47loop(void *arg) 48{ 49 SDL_Event event; 50 int i; 51 SDL_Joystick *joystick = (SDL_Joystick *)arg; 52 53 /* blank screen, set up for drawing this frame. */ 54 SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE); 55 SDL_RenderClear(screen); 56 57 while (SDL_PollEvent(&event)) { 58 switch (event.type) { 59 60 case SDL_JOYDEVICEREMOVED: 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 176 SDL_RenderPresent(screen); 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} 189 190static SDL_bool 191WatchJoystick(SDL_Joystick * joystick) 192{ 193 SDL_Window *window = NULL; 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, 201 SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, 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); 216 SDL_RenderClear(screen); 217 SDL_RenderPresent(screen); 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 237 SDL_DestroyRenderer(screen); 238 screen = NULL; 239 SDL_DestroyWindow(window); 240 return retval; 241} 242 243int 244main(int argc, char *argv[]) 245{ 246 const char *name, *type; 247 int i; 248 SDL_Joystick *joystick; 249 250 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); 251 252 /* Enable standard application logging */ 253 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); 254 255 /* Initialize SDL (Note: video is required to start event loop) */ 256 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { 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]; 272 SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick); 273 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), 274 guid, sizeof (guid)); 275 switch (SDL_JoystickGetType(joystick)) { 276 case SDL_JOYSTICK_TYPE_GAMECONTROLLER: 277 type = "Game Controller"; 278 break; 279 case SDL_JOYSTICK_TYPE_WHEEL: 280 type = "Wheel"; 281 break; 282 case SDL_JOYSTICK_TYPE_ARCADE_STICK: 283 type = "Arcade Stick"; 284 break; 285 case SDL_JOYSTICK_TYPE_FLIGHT_STICK: 286 type = "Flight Stick"; 287 break; 288 case SDL_JOYSTICK_TYPE_DANCE_PAD: 289 type = "Dance Pad"; 290 break; 291 case SDL_JOYSTICK_TYPE_GUITAR: 292 type = "Guitar"; 293 break; 294 case SDL_JOYSTICK_TYPE_DRUM_KIT: 295 type = "Drum Kit"; 296 break; 297 case SDL_JOYSTICK_TYPE_ARCADE_PAD: 298 type = "Arcade Pad"; 299 break; 300 case SDL_JOYSTICK_TYPE_THROTTLE: 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; 326 SDL_Event event; 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) { 335 SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick); 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) { 364 SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick); 365 } 366 break; 367 } 368 } 369 } 370 } 371 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); 372 373 return 0; 374} 375 376#else 377 378int 379main(int argc, char *argv[]) 380{ 381 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n"); 382 exit(1); 383} 384 385#endif 386 387/* vi: set ts=4 sw=4 expandtab: */ 388[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.