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