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.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
slouken@0
    13
/* Simple program to test the SDL joystick routines */
slouken@0
    14
slouken@0
    15
#include <stdio.h>
slouken@0
    16
#include <stdlib.h>
slouken@0
    17
#include <string.h>
slouken@0
    18
slouken@0
    19
#include "SDL.h"
slouken@0
    20
slouken@2765
    21
#ifdef __IPHONEOS__
slouken@2765
    22
#define SCREEN_WIDTH	320
slouken@2765
    23
#define SCREEN_HEIGHT	480
slouken@2765
    24
#else
slouken@0
    25
#define SCREEN_WIDTH	640
slouken@0
    26
#define SCREEN_HEIGHT	480
slouken@2765
    27
#endif
slouken@0
    28
slouken@1895
    29
void
slouken@1895
    30
WatchJoystick(SDL_Joystick * joystick)
slouken@0
    31
{
slouken@1895
    32
    SDL_Surface *screen;
slouken@1895
    33
    const char *name;
slouken@1895
    34
    int i, done;
slouken@1895
    35
    SDL_Event event;
slouken@1895
    36
    int x, y, draw;
slouken@1895
    37
    SDL_Rect axis_area[6][2];
slouken@0
    38
slouken@1895
    39
    /* Set a video mode to display joystick axis position */
slouken@1895
    40
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
slouken@1895
    41
    if (screen == NULL) {
slouken@1895
    42
        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
slouken@1895
    43
        return;
slouken@1895
    44
    }
slouken@0
    45
slouken@1895
    46
    /* Print info about the joystick we are watching */
slouken@1895
    47
    name = SDL_JoystickName(SDL_JoystickIndex(joystick));
slouken@1895
    48
    printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
slouken@1895
    49
           name ? name : "Unknown Joystick");
slouken@1895
    50
    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
slouken@1895
    51
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
slouken@1895
    52
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
slouken@0
    53
slouken@1895
    54
    /* Initialize drawing rectangles */
slouken@1895
    55
    memset(axis_area, 0, (sizeof axis_area));
slouken@1895
    56
    draw = 0;
slouken@0
    57
slouken@1895
    58
    /* Loop, getting joystick events! */
slouken@1895
    59
    done = 0;
slouken@1895
    60
    while (!done) {
slouken@1895
    61
        while (SDL_PollEvent(&event)) {
slouken@1895
    62
            switch (event.type) {
slouken@1895
    63
            case SDL_JOYAXISMOTION:
slouken@1895
    64
                printf("Joystick %d axis %d value: %d\n",
slouken@1895
    65
                       event.jaxis.which,
slouken@1895
    66
                       event.jaxis.axis, event.jaxis.value);
slouken@1895
    67
                break;
slouken@1895
    68
            case SDL_JOYHATMOTION:
slouken@1895
    69
                printf("Joystick %d hat %d value:",
slouken@1895
    70
                       event.jhat.which, event.jhat.hat);
slouken@1895
    71
                if (event.jhat.value == SDL_HAT_CENTERED)
slouken@1895
    72
                    printf(" centered");
slouken@1895
    73
                if (event.jhat.value & SDL_HAT_UP)
slouken@1895
    74
                    printf(" up");
slouken@1895
    75
                if (event.jhat.value & SDL_HAT_RIGHT)
slouken@1895
    76
                    printf(" right");
slouken@1895
    77
                if (event.jhat.value & SDL_HAT_DOWN)
slouken@1895
    78
                    printf(" down");
slouken@1895
    79
                if (event.jhat.value & SDL_HAT_LEFT)
slouken@1895
    80
                    printf(" left");
slouken@1895
    81
                printf("\n");
slouken@1895
    82
                break;
slouken@1895
    83
            case SDL_JOYBALLMOTION:
slouken@1895
    84
                printf("Joystick %d ball %d delta: (%d,%d)\n",
slouken@1895
    85
                       event.jball.which,
slouken@1895
    86
                       event.jball.ball, event.jball.xrel, event.jball.yrel);
slouken@1895
    87
                break;
slouken@1895
    88
            case SDL_JOYBUTTONDOWN:
slouken@1895
    89
                printf("Joystick %d button %d down\n",
slouken@1895
    90
                       event.jbutton.which, event.jbutton.button);
slouken@1895
    91
                break;
slouken@1895
    92
            case SDL_JOYBUTTONUP:
slouken@1895
    93
                printf("Joystick %d button %d up\n",
slouken@1895
    94
                       event.jbutton.which, event.jbutton.button);
slouken@1895
    95
                break;
slouken@1895
    96
            case SDL_KEYDOWN:
slouken@1895
    97
                if (event.key.keysym.sym != SDLK_ESCAPE) {
slouken@1895
    98
                    break;
slouken@1895
    99
                }
slouken@1895
   100
                /* Fall through to signal quit */
slouken@1895
   101
            case SDL_QUIT:
slouken@1895
   102
                done = 1;
slouken@1895
   103
                break;
slouken@1895
   104
            default:
slouken@1895
   105
                break;
slouken@1895
   106
            }
slouken@1895
   107
        }
slouken@1895
   108
        /* Update visual joystick state */
slouken@1895
   109
        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
slouken@1895
   110
            SDL_Rect area;
slouken@0
   111
slouken@1895
   112
            area.x = i * 34;
slouken@1895
   113
            area.y = SCREEN_HEIGHT - 34;
slouken@1895
   114
            area.w = 32;
slouken@1895
   115
            area.h = 32;
slouken@1895
   116
            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
slouken@1895
   117
                SDL_FillRect(screen, &area, 0xFFFF);
slouken@1895
   118
            } else {
slouken@1895
   119
                SDL_FillRect(screen, &area, 0x0000);
slouken@1895
   120
            }
slouken@1895
   121
            SDL_UpdateRects(screen, 1, &area);
slouken@1895
   122
        }
slouken@0
   123
slouken@1895
   124
        for (i = 0;
slouken@1895
   125
             i < SDL_JoystickNumAxes(joystick) / 2
slouken@1895
   126
             && i < SDL_arraysize(axis_area); ++i) {
slouken@1895
   127
            /* Erase previous axes */
slouken@1895
   128
            SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
slouken@0
   129
slouken@1895
   130
            /* Draw the X/Y axis */
slouken@1895
   131
            draw = !draw;
slouken@1895
   132
            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
slouken@1895
   133
            x *= SCREEN_WIDTH;
slouken@1895
   134
            x /= 65535;
slouken@1895
   135
            if (x < 0) {
slouken@1895
   136
                x = 0;
slouken@1895
   137
            } else if (x > (SCREEN_WIDTH - 16)) {
slouken@1895
   138
                x = SCREEN_WIDTH - 16;
slouken@1895
   139
            }
slouken@1895
   140
            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
slouken@1895
   141
            y *= SCREEN_HEIGHT;
slouken@1895
   142
            y /= 65535;
slouken@1895
   143
            if (y < 0) {
slouken@1895
   144
                y = 0;
slouken@1895
   145
            } else if (y > (SCREEN_HEIGHT - 16)) {
slouken@1895
   146
                y = SCREEN_HEIGHT - 16;
slouken@1895
   147
            }
slouken@2765
   148
slouken@1895
   149
            axis_area[i][draw].x = (Sint16) x;
slouken@1895
   150
            axis_area[i][draw].y = (Sint16) y;
slouken@1895
   151
            axis_area[i][draw].w = 16;
slouken@1895
   152
            axis_area[i][draw].h = 16;
slouken@1895
   153
            SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
slouken@1854
   154
slouken@1895
   155
            SDL_UpdateRects(screen, 2, axis_area[i]);
slouken@1895
   156
        }
slouken@1895
   157
    }
slouken@0
   158
}
slouken@0
   159
slouken@1895
   160
int
slouken@1895
   161
main(int argc, char *argv[])
slouken@0
   162
{
slouken@1895
   163
    const char *name;
slouken@1895
   164
    int i;
slouken@1895
   165
    SDL_Joystick *joystick;
slouken@0
   166
slouken@1895
   167
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@1895
   168
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
slouken@1895
   169
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   170
        exit(1);
slouken@1895
   171
    }
slouken@0
   172
slouken@1895
   173
    /* Print information about the joysticks */
slouken@1895
   174
    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@1895
   175
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@1895
   176
        name = SDL_JoystickName(i);
slouken@2201
   177
        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
icculus@2200
   178
        joystick = SDL_JoystickOpen(i);
icculus@2200
   179
        if (joystick == NULL) {
slouken@2201
   180
            fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
slouken@2201
   181
                    SDL_GetError());
icculus@2200
   182
        } else {
icculus@2200
   183
            printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
icculus@2200
   184
            printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
icculus@2200
   185
            printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
icculus@2200
   186
            printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
icculus@2200
   187
            SDL_JoystickClose(joystick);
icculus@2200
   188
        }
slouken@1895
   189
    }
slouken@0
   190
slouken@1895
   191
    if (argv[1]) {
slouken@1895
   192
        joystick = SDL_JoystickOpen(atoi(argv[1]));
slouken@1895
   193
        if (joystick == NULL) {
slouken@1895
   194
            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
slouken@1895
   195
                   SDL_GetError());
slouken@1895
   196
        } else {
slouken@1895
   197
            WatchJoystick(joystick);
slouken@1895
   198
            SDL_JoystickClose(joystick);
slouken@1895
   199
        }
slouken@1895
   200
    }
slouken@1895
   201
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
slouken@0
   202
slouken@1895
   203
    return (0);
slouken@0
   204
}