test/testjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 2765 f55c87ae336b
child 6256 1d905b13b102
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     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 void
    30 WatchJoystick(SDL_Joystick * joystick)
    31 {
    32     SDL_Surface *screen;
    33     const char *name;
    34     int i, done;
    35     SDL_Event event;
    36     int x, y, draw;
    37     SDL_Rect axis_area[6][2];
    38 
    39     /* Set a video mode to display joystick axis position */
    40     screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    41     if (screen == NULL) {
    42         fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
    43         return;
    44     }
    45 
    46     /* Print info about the joystick we are watching */
    47     name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    48     printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    49            name ? name : "Unknown Joystick");
    50     printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    51            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    52            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    53 
    54     /* Initialize drawing rectangles */
    55     memset(axis_area, 0, (sizeof axis_area));
    56     draw = 0;
    57 
    58     /* Loop, getting joystick events! */
    59     done = 0;
    60     while (!done) {
    61         while (SDL_PollEvent(&event)) {
    62             switch (event.type) {
    63             case SDL_JOYAXISMOTION:
    64                 printf("Joystick %d axis %d value: %d\n",
    65                        event.jaxis.which,
    66                        event.jaxis.axis, event.jaxis.value);
    67                 break;
    68             case SDL_JOYHATMOTION:
    69                 printf("Joystick %d hat %d value:",
    70                        event.jhat.which, event.jhat.hat);
    71                 if (event.jhat.value == SDL_HAT_CENTERED)
    72                     printf(" centered");
    73                 if (event.jhat.value & SDL_HAT_UP)
    74                     printf(" up");
    75                 if (event.jhat.value & SDL_HAT_RIGHT)
    76                     printf(" right");
    77                 if (event.jhat.value & SDL_HAT_DOWN)
    78                     printf(" down");
    79                 if (event.jhat.value & SDL_HAT_LEFT)
    80                     printf(" left");
    81                 printf("\n");
    82                 break;
    83             case SDL_JOYBALLMOTION:
    84                 printf("Joystick %d ball %d delta: (%d,%d)\n",
    85                        event.jball.which,
    86                        event.jball.ball, event.jball.xrel, event.jball.yrel);
    87                 break;
    88             case SDL_JOYBUTTONDOWN:
    89                 printf("Joystick %d button %d down\n",
    90                        event.jbutton.which, event.jbutton.button);
    91                 break;
    92             case SDL_JOYBUTTONUP:
    93                 printf("Joystick %d button %d up\n",
    94                        event.jbutton.which, event.jbutton.button);
    95                 break;
    96             case SDL_KEYDOWN:
    97                 if (event.key.keysym.sym != SDLK_ESCAPE) {
    98                     break;
    99                 }
   100                 /* Fall through to signal quit */
   101             case SDL_QUIT:
   102                 done = 1;
   103                 break;
   104             default:
   105                 break;
   106             }
   107         }
   108         /* Update visual joystick state */
   109         for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   110             SDL_Rect area;
   111 
   112             area.x = i * 34;
   113             area.y = SCREEN_HEIGHT - 34;
   114             area.w = 32;
   115             area.h = 32;
   116             if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   117                 SDL_FillRect(screen, &area, 0xFFFF);
   118             } else {
   119                 SDL_FillRect(screen, &area, 0x0000);
   120             }
   121             SDL_UpdateRects(screen, 1, &area);
   122         }
   123 
   124         for (i = 0;
   125              i < SDL_JoystickNumAxes(joystick) / 2
   126              && i < SDL_arraysize(axis_area); ++i) {
   127             /* Erase previous axes */
   128             SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
   129 
   130             /* Draw the X/Y axis */
   131             draw = !draw;
   132             x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   133             x *= SCREEN_WIDTH;
   134             x /= 65535;
   135             if (x < 0) {
   136                 x = 0;
   137             } else if (x > (SCREEN_WIDTH - 16)) {
   138                 x = SCREEN_WIDTH - 16;
   139             }
   140             y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   141             y *= SCREEN_HEIGHT;
   142             y /= 65535;
   143             if (y < 0) {
   144                 y = 0;
   145             } else if (y > (SCREEN_HEIGHT - 16)) {
   146                 y = SCREEN_HEIGHT - 16;
   147             }
   148 
   149             axis_area[i][draw].x = (Sint16) x;
   150             axis_area[i][draw].y = (Sint16) y;
   151             axis_area[i][draw].w = 16;
   152             axis_area[i][draw].h = 16;
   153             SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
   154 
   155             SDL_UpdateRects(screen, 2, axis_area[i]);
   156         }
   157     }
   158 }
   159 
   160 int
   161 main(int argc, char *argv[])
   162 {
   163     const char *name;
   164     int i;
   165     SDL_Joystick *joystick;
   166 
   167     /* Initialize SDL (Note: video is required to start event loop) */
   168     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   169         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   170         exit(1);
   171     }
   172 
   173     /* Print information about the joysticks */
   174     printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   175     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   176         name = SDL_JoystickName(i);
   177         printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   178         joystick = SDL_JoystickOpen(i);
   179         if (joystick == NULL) {
   180             fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
   181                     SDL_GetError());
   182         } else {
   183             printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   184             printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   185             printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   186             printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   187             SDL_JoystickClose(joystick);
   188         }
   189     }
   190 
   191     if (argv[1]) {
   192         joystick = SDL_JoystickOpen(atoi(argv[1]));
   193         if (joystick == NULL) {
   194             printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   195                    SDL_GetError());
   196         } else {
   197             WatchJoystick(joystick);
   198             SDL_JoystickClose(joystick);
   199         }
   200     }
   201     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   202 
   203     return (0);
   204 }