test/testjoystick.c
author Alex Baines <alex@abaines.me.uk>
Tue, 19 Aug 2014 23:31:50 +0100
changeset 9097 56d712662a82
parent 8762 4b7d26162a2e
child 9278 8900afb78a19
permissions -rw-r--r--
Add a SDL_IM_INTERNAL_EDITING event to make IMs like iBus render editing text in its own UI instead of sending TEXTEDITING events.
This is useful for applications that handle TEXTINPUT events but not TEXTEDITING events.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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@7191
    24
#define SCREEN_WIDTH    320
slouken@7191
    25
#define SCREEN_HEIGHT   480
slouken@6690
    26
#else
slouken@7191
    27
#define SCREEN_WIDTH    640
slouken@7191
    28
#define SCREEN_HEIGHT   480
slouken@6690
    29
#endif
slouken@0
    30
aschiffler@6771
    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,
philipp@7478
    53
                              SCREEN_HEIGHT, 0);
slouken@6690
    54
    if (window == NULL) {
aschiffler@7639
    55
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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) {
aschiffler@7639
    61
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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);
aschiffler@7639
    73
    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
slouken@1895
    74
           name ? name : "Unknown Joystick");
aschiffler@7639
    75
    SDL_Log("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:
aschiffler@7639
    88
                SDL_Log("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:
aschiffler@7639
    93
                SDL_Log("Joystick %d hat %d value:",
slouken@1895
    94
                       event.jhat.which, event.jhat.hat);
slouken@1895
    95
                if (event.jhat.value == SDL_HAT_CENTERED)
aschiffler@7639
    96
                    SDL_Log(" centered");
slouken@1895
    97
                if (event.jhat.value & SDL_HAT_UP)
aschiffler@7639
    98
                    SDL_Log(" up");
slouken@1895
    99
                if (event.jhat.value & SDL_HAT_RIGHT)
aschiffler@7639
   100
                    SDL_Log(" right");
slouken@1895
   101
                if (event.jhat.value & SDL_HAT_DOWN)
aschiffler@7639
   102
                    SDL_Log(" down");
slouken@1895
   103
                if (event.jhat.value & SDL_HAT_LEFT)
aschiffler@7639
   104
                    SDL_Log(" left");
aschiffler@7639
   105
                SDL_Log("\n");
slouken@1895
   106
                break;
slouken@1895
   107
            case SDL_JOYBALLMOTION:
aschiffler@7639
   108
                SDL_Log("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:
aschiffler@7639
   113
                SDL_Log("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:
aschiffler@7639
   117
                SDL_Log("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@7017
   121
                if ((event.key.keysym.sym != SDLK_ESCAPE) &&
slouken@7017
   122
                    (event.key.keysym.sym != SDLK_AC_BACK)) {
slouken@1895
   123
                    break;
slouken@1895
   124
                }
slouken@1895
   125
                /* Fall through to signal quit */
slouken@7017
   126
            case SDL_FINGERDOWN:
slouken@7017
   127
            case SDL_MOUSEBUTTONDOWN:
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@7017
   139
                DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 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@7017
   144
        for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
slouken@6690
   145
            /* Draw the X/Y axis */
slouken@6690
   146
            int x, y;
slouken@7017
   147
            x = (((int) SDL_JoystickGetAxis(joystick, i)) + 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@7017
   155
            ++i;
slouken@7017
   156
            if (i < SDL_JoystickNumAxes(joystick)) {
slouken@7017
   157
                y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
slouken@7017
   158
            } else {
slouken@7017
   159
                y = 32768;
slouken@7017
   160
            }
slouken@6690
   161
            y *= SCREEN_HEIGHT;
slouken@6690
   162
            y /= 65535;
slouken@6690
   163
            if (y < 0) {
slouken@6690
   164
                y = 0;
slouken@6690
   165
            } else if (y > (SCREEN_HEIGHT - 16)) {
slouken@6690
   166
                y = SCREEN_HEIGHT - 16;
slouken@1895
   167
            }
slouken@2765
   168
slouken@6690
   169
            DrawRect(screen, x, y, 16, 16);
slouken@6690
   170
        }
icculus@6397
   171
slouken@6690
   172
        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
slouken@6690
   173
        for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
slouken@6690
   174
            /* Derive the new position */
slouken@6690
   175
            int x = SCREEN_WIDTH/2;
slouken@6690
   176
            int y = SCREEN_HEIGHT/2;
slouken@6690
   177
            const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
slouken@6690
   178
slouken@6690
   179
            if (hat_pos & SDL_HAT_UP) {
slouken@6690
   180
                y = 0;
slouken@6690
   181
            } else if (hat_pos & SDL_HAT_DOWN) {
slouken@6690
   182
                y = SCREEN_HEIGHT-8;
icculus@6397
   183
            }
icculus@6397
   184
slouken@6690
   185
            if (hat_pos & SDL_HAT_LEFT) {
slouken@6690
   186
                x = 0;
slouken@6690
   187
            } else if (hat_pos & SDL_HAT_RIGHT) {
slouken@6690
   188
                x = SCREEN_WIDTH-8;
icculus@6397
   189
            }
icculus@6397
   190
slouken@6690
   191
            DrawRect(screen, x, y, 8, 8);
slouken@1895
   192
        }
slouken@6690
   193
slouken@6690
   194
        SDL_RenderPresent(screen);
icculus@6730
   195
icculus@6730
   196
        if (SDL_JoystickGetAttached( joystick ) == 0) {
icculus@6730
   197
            done = SDL_TRUE;
icculus@6730
   198
            retval = SDL_TRUE;  /* keep going, wait for reattach. */
icculus@6730
   199
        }
slouken@1895
   200
    }
slouken@6690
   201
slouken@6690
   202
    SDL_DestroyRenderer(screen);
slouken@6690
   203
    SDL_DestroyWindow(window);
icculus@6730
   204
    return retval;
slouken@0
   205
}
slouken@0
   206
slouken@1895
   207
int
slouken@1895
   208
main(int argc, char *argv[])
slouken@0
   209
{
slouken@1895
   210
    const char *name;
slouken@6690
   211
    int i;
slouken@1895
   212
    SDL_Joystick *joystick;
slouken@0
   213
aschiffler@7639
   214
    /* Enable standard application logging */
aschiffler@7639
   215
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
aschiffler@7639
   216
slouken@1895
   217
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@6690
   218
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
aschiffler@7639
   219
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   220
        exit(1);
slouken@1895
   221
    }
slouken@0
   222
slouken@1895
   223
    /* Print information about the joysticks */
aschiffler@7639
   224
    SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@1895
   225
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@6690
   226
        name = SDL_JoystickNameForIndex(i);
aschiffler@7639
   227
        SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
icculus@2200
   228
        joystick = SDL_JoystickOpen(i);
icculus@2200
   229
        if (joystick == NULL) {
aschiffler@7639
   230
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
slouken@2201
   231
                    SDL_GetError());
icculus@2200
   232
        } else {
icculus@6747
   233
            char guid[64];
icculus@6747
   234
            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
icculus@6747
   235
                                      guid, sizeof (guid));
aschiffler@7639
   236
            SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
aschiffler@7639
   237
            SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
aschiffler@7639
   238
            SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
aschiffler@7639
   239
            SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
aschiffler@7639
   240
            SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
aschiffler@7639
   241
            SDL_Log("       guid: %s\n", guid);
icculus@2200
   242
            SDL_JoystickClose(joystick);
icculus@2200
   243
        }
slouken@1895
   244
    }
slouken@0
   245
gabomdq@8762
   246
#ifdef __ANDROID__
slouken@7017
   247
    if (SDL_NumJoysticks() > 0) {
slouken@7017
   248
#else
slouken@6690
   249
    if (argv[1]) {
slouken@7017
   250
#endif
icculus@6731
   251
        SDL_bool reportederror = SDL_FALSE;
icculus@6730
   252
        SDL_bool keepGoing = SDL_TRUE;
icculus@6731
   253
        SDL_Event event;
philipp@8078
   254
        int device;
gabomdq@8762
   255
#ifdef __ANDROID__
philipp@8078
   256
        device = 0;
slouken@7017
   257
#else
philipp@8078
   258
        device = atoi(argv[1]);
slouken@7017
   259
#endif
philipp@8078
   260
        joystick = SDL_JoystickOpen(device);
philipp@8078
   261
icculus@6731
   262
        while ( keepGoing ) {
icculus@6731
   263
            if (joystick == NULL) {
icculus@6731
   264
                if ( !reportederror ) {
philipp@8078
   265
                    SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
icculus@6732
   266
                    keepGoing = SDL_FALSE;
icculus@6731
   267
                    reportederror = SDL_TRUE;
icculus@6731
   268
                }
icculus@6731
   269
            } else {
icculus@6731
   270
                reportederror = SDL_FALSE;
icculus@6731
   271
                keepGoing = WatchJoystick(joystick);
icculus@6731
   272
                SDL_JoystickClose(joystick);
icculus@6731
   273
            }
icculus@6731
   274
slouken@6736
   275
            joystick = NULL;
icculus@6730
   276
            if (keepGoing) {
aschiffler@7639
   277
                SDL_Log("Waiting for attach\n");
slouken@6736
   278
            }
slouken@6736
   279
            while (keepGoing) {
slouken@6736
   280
                SDL_WaitEvent(&event);
slouken@7017
   281
                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
slouken@7017
   282
                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
slouken@6736
   283
                    keepGoing = SDL_FALSE;
slouken@6736
   284
                } else if (event.type == SDL_JOYDEVICEADDED) {
philipp@8078
   285
                    joystick = SDL_JoystickOpen(device);
slouken@6736
   286
                    break;
icculus@6731
   287
                }
icculus@6730
   288
            }
icculus@6731
   289
        }
icculus@6731
   290
    }
slouken@6690
   291
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
slouken@0
   292
philipp@8073
   293
    return 0;
slouken@0
   294
}
aschiffler@6771
   295
aschiffler@6771
   296
#else
aschiffler@6771
   297
aschiffler@6771
   298
int
aschiffler@6771
   299
main(int argc, char *argv[])
aschiffler@6771
   300
{
aschiffler@7639
   301
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
aschiffler@6771
   302
    exit(1);
aschiffler@6771
   303
}
aschiffler@6771
   304
aschiffler@6771
   305
#endif