test/testjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 11 Dec 2012 16:53:59 -0500
changeset 6747 b617821795bf
parent 6736 03dd4a1d06e9
child 6771 55337ff4256f
permissions -rw-r--r--
Patched testjoystick.c to compile.
     1 /*
     2   Copyright (C) 1997-2011 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 __IPHONEOS__
    22 #define SCREEN_WIDTH	320
    23 #define SCREEN_HEIGHT	480
    24 #else
    25 #define SCREEN_WIDTH	640
    26 #define SCREEN_HEIGHT	480
    27 #endif
    28 
    29 #define MAX_NUM_AXES 6
    30 #define MAX_NUM_HATS 2
    31 
    32 static void
    33 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    34 {
    35     const SDL_Rect area = { x, y, w, h };
    36     SDL_RenderFillRect(r, &area);
    37 }
    38 
    39 static SDL_bool
    40 WatchJoystick(SDL_Joystick * joystick)
    41 {
    42     SDL_Window *window = NULL;
    43     SDL_Renderer *screen = NULL;
    44     const char *name = NULL;
    45     SDL_bool retval = SDL_FALSE;
    46     SDL_bool done = SDL_FALSE;
    47     SDL_Event event;
    48     int i;
    49 
    50     /* Create a window to display joystick axis position */
    51     window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    52                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    53                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    54     if (window == NULL) {
    55         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    56         return SDL_FALSE;
    57     }
    58 
    59     screen = SDL_CreateRenderer(window, -1, 0);
    60     if (screen == NULL) {
    61         fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    62         SDL_DestroyWindow(window);
    63         return SDL_FALSE;
    64     }
    65 
    66     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    67     SDL_RenderClear(screen);
    68     SDL_RenderPresent(screen);
    69     SDL_RaiseWindow(window);
    70 
    71     /* Print info about the joystick we are watching */
    72     name = SDL_JoystickName(joystick);
    73     printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
    74            name ? name : "Unknown Joystick");
    75     printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    76            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    77            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    78 
    79     /* Loop, getting joystick events! */
    80     while (!done) {
    81         /* blank screen, set up for drawing this frame. */
    82         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    83         SDL_RenderClear(screen);
    84 
    85         while (SDL_PollEvent(&event)) {
    86             switch (event.type) {
    87             case SDL_JOYAXISMOTION:
    88                 printf("Joystick %d axis %d value: %d\n",
    89                        event.jaxis.which,
    90                        event.jaxis.axis, event.jaxis.value);
    91                 break;
    92             case SDL_JOYHATMOTION:
    93                 printf("Joystick %d hat %d value:",
    94                        event.jhat.which, event.jhat.hat);
    95                 if (event.jhat.value == SDL_HAT_CENTERED)
    96                     printf(" centered");
    97                 if (event.jhat.value & SDL_HAT_UP)
    98                     printf(" up");
    99                 if (event.jhat.value & SDL_HAT_RIGHT)
   100                     printf(" right");
   101                 if (event.jhat.value & SDL_HAT_DOWN)
   102                     printf(" down");
   103                 if (event.jhat.value & SDL_HAT_LEFT)
   104                     printf(" left");
   105                 printf("\n");
   106                 break;
   107             case SDL_JOYBALLMOTION:
   108                 printf("Joystick %d ball %d delta: (%d,%d)\n",
   109                        event.jball.which,
   110                        event.jball.ball, event.jball.xrel, event.jball.yrel);
   111                 break;
   112             case SDL_JOYBUTTONDOWN:
   113                 printf("Joystick %d button %d down\n",
   114                        event.jbutton.which, event.jbutton.button);
   115                 break;
   116             case SDL_JOYBUTTONUP:
   117                 printf("Joystick %d button %d up\n",
   118                        event.jbutton.which, event.jbutton.button);
   119                 break;
   120             case SDL_KEYDOWN:
   121                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   122                     break;
   123                 }
   124                 /* Fall through to signal quit */
   125             case SDL_QUIT:
   126                 done = SDL_TRUE;
   127                 break;
   128             default:
   129                 break;
   130             }
   131         }
   132         /* Update visual joystick state */
   133         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   134         for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   135             if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   136                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   137             }
   138         }
   139 
   140         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   141         for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   142             /* Draw the X/Y axis */
   143             int x, y;
   144             x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   145             x *= SCREEN_WIDTH;
   146             x /= 65535;
   147             if (x < 0) {
   148                 x = 0;
   149             } else if (x > (SCREEN_WIDTH - 16)) {
   150                 x = SCREEN_WIDTH - 16;
   151             }
   152             y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   153             y *= SCREEN_HEIGHT;
   154             y /= 65535;
   155             if (y < 0) {
   156                 y = 0;
   157             } else if (y > (SCREEN_HEIGHT - 16)) {
   158                 y = SCREEN_HEIGHT - 16;
   159             }
   160 
   161             DrawRect(screen, x, y, 16, 16);
   162         }
   163 
   164         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   165         for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   166             /* Derive the new position */
   167             int x = SCREEN_WIDTH/2;
   168             int y = SCREEN_HEIGHT/2;
   169             const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   170 
   171             if (hat_pos & SDL_HAT_UP) {
   172                 y = 0;
   173             } else if (hat_pos & SDL_HAT_DOWN) {
   174                 y = SCREEN_HEIGHT-8;
   175             }
   176 
   177             if (hat_pos & SDL_HAT_LEFT) {
   178                 x = 0;
   179             } else if (hat_pos & SDL_HAT_RIGHT) {
   180                 x = SCREEN_WIDTH-8;
   181             }
   182 
   183             DrawRect(screen, x, y, 8, 8);
   184         }
   185 
   186         SDL_RenderPresent(screen);
   187 
   188         if (SDL_JoystickGetAttached( joystick ) == 0) {
   189             done = SDL_TRUE;
   190             retval = SDL_TRUE;  /* keep going, wait for reattach. */
   191         }
   192     }
   193 
   194     SDL_DestroyRenderer(screen);
   195     SDL_DestroyWindow(window);
   196     return retval;
   197 }
   198 
   199 int
   200 main(int argc, char *argv[])
   201 {
   202     const char *name;
   203     int i;
   204     SDL_Joystick *joystick;
   205 
   206     /* Initialize SDL (Note: video is required to start event loop) */
   207     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   208         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   209         exit(1);
   210     }
   211 
   212     /* Print information about the joysticks */
   213     printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   214     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   215         name = SDL_JoystickNameForIndex(i);
   216         printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   217         joystick = SDL_JoystickOpen(i);
   218         if (joystick == NULL) {
   219             fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
   220                     SDL_GetError());
   221         } else {
   222             char guid[64];
   223             SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
   224                                       guid, sizeof (guid));
   225             printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   226             printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   227             printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   228             printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   229             printf("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   230             printf("       guid: %s\n", guid);
   231             SDL_JoystickClose(joystick);
   232         }
   233     }
   234 
   235     if (argv[1]) {
   236         SDL_bool reportederror = SDL_FALSE;
   237         SDL_bool keepGoing = SDL_TRUE;
   238         SDL_Event event;
   239         joystick = SDL_JoystickOpen(atoi(argv[1]));
   240         while ( keepGoing ) {
   241             if (joystick == NULL) {
   242                 if ( !reportederror ) {
   243                     printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   244                     keepGoing = SDL_FALSE;
   245                     reportederror = SDL_TRUE;
   246                 }
   247             } else {
   248                 reportederror = SDL_FALSE;
   249                 keepGoing = WatchJoystick(joystick);
   250                 SDL_JoystickClose(joystick);
   251             }
   252 
   253             joystick = NULL;
   254             if (keepGoing) {
   255                 printf("Waiting for attach\n");
   256             }
   257             while (keepGoing) {
   258                 SDL_WaitEvent(&event);
   259                 if (event.type == SDL_QUIT) {
   260                     keepGoing = SDL_FALSE;
   261                 } else if (event.type == SDL_JOYDEVICEADDED) {
   262                     joystick = SDL_JoystickOpen(atoi(argv[1]));
   263                     break;
   264                 }
   265             }
   266         }
   267     }
   268     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   269 
   270     return 0;
   271 }