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