test/testjoystick.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 09 May 2015 22:42:23 +0200
changeset 9594 0285b5e1c5ab
parent 9433 bd062398b648
child 9607 7746ab058d12
permissions -rw-r--r--
Android: Fixed empty parameter list in signatures of internal functions.
     1 /*
     2   Copyright (C) 1997-2014 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 
    35 SDL_Renderer *screen = NULL;
    36 SDL_bool retval = SDL_FALSE;
    37 SDL_bool done = SDL_FALSE;
    38 
    39 static void
    40 DrawRect(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 
    46 void
    47 loop(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                 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 
   172         SDL_RenderPresent(screen);
   173 
   174         if (SDL_JoystickGetAttached( joystick ) == 0) {
   175             done = SDL_TRUE;
   176             retval = SDL_TRUE;  /* keep going, wait for reattach. */
   177         }
   178 }
   179 
   180 static SDL_bool
   181 WatchJoystick(SDL_Joystick * joystick)
   182 {
   183     SDL_Window *window = NULL;
   184     const char *name = NULL;
   185 
   186     retval = SDL_FALSE;
   187     done = SDL_FALSE;
   188 
   189     /* Create a window to display joystick axis position */
   190     window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
   191                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   192                               SCREEN_HEIGHT, 0);
   193     if (window == NULL) {
   194         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   195         return SDL_FALSE;
   196     }
   197 
   198     screen = SDL_CreateRenderer(window, -1, 0);
   199     if (screen == NULL) {
   200         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   201         SDL_DestroyWindow(window);
   202         return SDL_FALSE;
   203     }
   204 
   205     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   206     SDL_RenderClear(screen);
   207     SDL_RenderPresent(screen);
   208     SDL_RaiseWindow(window);
   209 
   210     /* Print info about the joystick we are watching */
   211     name = SDL_JoystickName(joystick);
   212     SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
   213            name ? name : "Unknown Joystick");
   214     SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
   215            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
   216            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
   217 
   218     /* Loop, getting joystick events! */
   219 #ifdef __EMSCRIPTEN__
   220     emscripten_set_main_loop_arg(loop, joystick, 0, 1);
   221 #else
   222     while (!done) {
   223         loop(joystick);
   224     }
   225 #endif
   226 
   227     SDL_DestroyRenderer(screen);
   228     screen = NULL;
   229     SDL_DestroyWindow(window);
   230     return retval;
   231 }
   232 
   233 int
   234 main(int argc, char *argv[])
   235 {
   236     const char *name;
   237     int i;
   238     SDL_Joystick *joystick;
   239 
   240     /* Enable standard application logging */
   241     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
   242 
   243     /* Initialize SDL (Note: video is required to start event loop) */
   244     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   245         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   246         exit(1);
   247     }
   248 
   249     /* Print information about the joysticks */
   250     SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
   251     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   252         name = SDL_JoystickNameForIndex(i);
   253         SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   254         joystick = SDL_JoystickOpen(i);
   255         if (joystick == NULL) {
   256             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
   257                     SDL_GetError());
   258         } else {
   259             char guid[64];
   260             SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
   261                                       guid, sizeof (guid));
   262             SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   263             SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   264             SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
   265             SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   266             SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   267             SDL_Log("       guid: %s\n", guid);
   268             SDL_JoystickClose(joystick);
   269         }
   270     }
   271 
   272 #ifdef __ANDROID__
   273     if (SDL_NumJoysticks() > 0) {
   274 #else
   275     if (argv[1]) {
   276 #endif
   277         SDL_bool reportederror = SDL_FALSE;
   278         SDL_bool keepGoing = SDL_TRUE;
   279         SDL_Event event;
   280         int device;
   281 #ifdef __ANDROID__
   282         device = 0;
   283 #else
   284         device = atoi(argv[1]);
   285 #endif
   286         joystick = SDL_JoystickOpen(device);
   287 
   288         while ( keepGoing ) {
   289             if (joystick == NULL) {
   290                 if ( !reportederror ) {
   291                     SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   292                     keepGoing = SDL_FALSE;
   293                     reportederror = SDL_TRUE;
   294                 }
   295             } else {
   296                 reportederror = SDL_FALSE;
   297                 keepGoing = WatchJoystick(joystick);
   298                 SDL_JoystickClose(joystick);
   299             }
   300 
   301             joystick = NULL;
   302             if (keepGoing) {
   303                 SDL_Log("Waiting for attach\n");
   304             }
   305             while (keepGoing) {
   306                 SDL_WaitEvent(&event);
   307                 if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
   308                     || (event.type == SDL_MOUSEBUTTONDOWN)) {
   309                     keepGoing = SDL_FALSE;
   310                 } else if (event.type == SDL_JOYDEVICEADDED) {
   311                     joystick = SDL_JoystickOpen(device);
   312                     break;
   313                 }
   314             }
   315         }
   316     }
   317     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   318 
   319     return 0;
   320 }
   321 
   322 #else
   323 
   324 int
   325 main(int argc, char *argv[])
   326 {
   327     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   328     exit(1);
   329 }
   330 
   331 #endif