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