test/testgamecontroller.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Fri, 16 Dec 2016 22:58:32 +0100
changeset 10707 28c1a3f9c88b
parent 10691 c02fe6ba1cfd
child 10724 5ea5f198879f
permissions -rw-r--r--
Fixed hotplug with more than one device in testjoystick program.
slouken@6690
     1
/*
slouken@9998
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@6690
     3
slouken@6690
     4
  This software is provided 'as-is', without any express or implied
slouken@6690
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@6690
     6
  arising from the use of this software.
slouken@6690
     7
slouken@6690
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@6690
     9
  including commercial applications, and to alter it and redistribute it
slouken@6690
    10
  freely.
slouken@6690
    11
*/
slouken@6690
    12
slouken@6690
    13
/* Simple program to test the SDL game controller routines */
slouken@6690
    14
slouken@6690
    15
#include <stdio.h>
slouken@6690
    16
#include <stdlib.h>
slouken@6690
    17
#include <string.h>
slouken@6690
    18
slouken@6690
    19
#include "SDL.h"
slouken@6690
    20
icculus@9278
    21
#ifdef __EMSCRIPTEN__
icculus@9278
    22
#include <emscripten/emscripten.h>
icculus@9278
    23
#endif
icculus@9278
    24
aschiffler@6771
    25
#ifndef SDL_JOYSTICK_DISABLED
aschiffler@6771
    26
slouken@6690
    27
#ifdef __IPHONEOS__
slime73@9876
    28
#define SCREEN_WIDTH    480
slime73@9876
    29
#define SCREEN_HEIGHT    320
slouken@6690
    30
#else
icculus@8199
    31
#define SCREEN_WIDTH    512
slouken@10528
    32
#define SCREEN_HEIGHT   320
slouken@6690
    33
#endif
slouken@6690
    34
icculus@9278
    35
/* This is indexed by SDL_GameControllerButton. */
icculus@9278
    36
static const struct { int x; int y; } button_positions[] = {
icculus@9278
    37
    {387, 167},  /* A */
icculus@9278
    38
    {431, 132},  /* B */
icculus@9278
    39
    {342, 132},  /* X */
icculus@9278
    40
    {389, 101},  /* Y */
icculus@9278
    41
    {174, 132},  /* BACK */
icculus@9278
    42
    {233, 132},  /* GUIDE */
icculus@9278
    43
    {289, 132},  /* START */
icculus@9278
    44
    {75,  154},  /* LEFTSTICK */
icculus@9278
    45
    {305, 230},  /* RIGHTSTICK */
icculus@9278
    46
    {77,  40},   /* LEFTSHOULDER */
icculus@9278
    47
    {396, 36},   /* RIGHTSHOULDER */
icculus@9278
    48
    {154, 188},  /* DPAD_UP */
icculus@9278
    49
    {154, 249},  /* DPAD_DOWN */
icculus@9278
    50
    {116, 217},  /* DPAD_LEFT */
icculus@9278
    51
    {186, 217},  /* DPAD_RIGHT */
icculus@9278
    52
};
icculus@9278
    53
icculus@9278
    54
/* This is indexed by SDL_GameControllerAxis. */
icculus@9278
    55
static const struct { int x; int y; double angle; } axis_positions[] = {
icculus@9278
    56
    {75,  154, 0.0},  /* LEFTX */
icculus@9278
    57
    {75,  154, 90.0},  /* LEFTY */
icculus@9278
    58
    {305, 230, 0.0},  /* RIGHTX */
icculus@9278
    59
    {305, 230, 90.0},  /* RIGHTY */
icculus@9278
    60
    {91, 0, 90.0},     /* TRIGGERLEFT */
icculus@9278
    61
    {375, 0, 90.0},    /* TRIGGERRIGHT */
icculus@9278
    62
};
icculus@9278
    63
icculus@9278
    64
SDL_Renderer *screen = NULL;
icculus@9278
    65
SDL_bool retval = SDL_FALSE;
icculus@9278
    66
SDL_bool done = SDL_FALSE;
icculus@9278
    67
SDL_Texture *background, *button, *axis;
icculus@9278
    68
icculus@8199
    69
static SDL_Texture *
philipp@10359
    70
LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
icculus@8199
    71
{
icculus@8199
    72
    SDL_Surface *temp = NULL;
icculus@8199
    73
    SDL_Texture *texture = NULL;
icculus@8199
    74
icculus@8199
    75
    temp = SDL_LoadBMP(file);
icculus@8199
    76
    if (temp == NULL) {
icculus@8199
    77
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
icculus@8199
    78
    } else {
icculus@8199
    79
        /* Set transparent pixel as the pixel at (0,0) */
icculus@8199
    80
        if (transparent) {
slouken@8687
    81
            if (temp->format->BytesPerPixel == 1) {
slouken@8687
    82
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
slouken@8687
    83
            } else {
slouken@8687
    84
                SDL_assert(!temp->format->palette);
slouken@8687
    85
                SDL_assert(temp->format->BitsPerPixel == 24);
slouken@8687
    86
                SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *)temp->pixels) & 0x00FFFFFF);
slouken@8687
    87
            }
icculus@8199
    88
        }
icculus@8199
    89
icculus@8199
    90
        texture = SDL_CreateTextureFromSurface(renderer, temp);
icculus@8199
    91
        if (!texture) {
icculus@8199
    92
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
icculus@8199
    93
        }
icculus@8199
    94
    }
icculus@8199
    95
    if (temp) {
icculus@8199
    96
        SDL_FreeSurface(temp);
icculus@8199
    97
    }
icculus@8199
    98
    return texture;
icculus@8199
    99
}
icculus@8199
   100
icculus@9278
   101
void
icculus@9278
   102
loop(void *arg)
icculus@9278
   103
{
icculus@9278
   104
    SDL_Event event;
icculus@9278
   105
    int i;
icculus@9278
   106
    SDL_GameController *gamecontroller = (SDL_GameController *)arg;
icculus@9278
   107
icculus@9278
   108
    /* blank screen, set up for drawing this frame. */
icculus@9278
   109
    SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
icculus@9278
   110
    SDL_RenderClear(screen);
icculus@9278
   111
    SDL_RenderCopy(screen, background, NULL, NULL);
icculus@9278
   112
icculus@9278
   113
    while (SDL_PollEvent(&event)) {
icculus@9278
   114
        switch (event.type) {
icculus@9278
   115
        case SDL_KEYDOWN:
icculus@9278
   116
            if (event.key.keysym.sym != SDLK_ESCAPE) {
icculus@9278
   117
                break;
icculus@9278
   118
            }
icculus@9278
   119
            /* Fall through to signal quit */
icculus@9278
   120
        case SDL_QUIT:
icculus@9278
   121
            done = SDL_TRUE;
icculus@9278
   122
            break;
icculus@9278
   123
        default:
icculus@9278
   124
            break;
icculus@9278
   125
        }
icculus@9278
   126
    }
icculus@9278
   127
icculus@9278
   128
    /* Update visual controller state */
icculus@9278
   129
    for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
icculus@9278
   130
        if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
icculus@9278
   131
            const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
philipp@10359
   132
            SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, SDL_FLIP_NONE);
icculus@9278
   133
        }
icculus@9278
   134
    }
icculus@9278
   135
icculus@9278
   136
    for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
icculus@9278
   137
        const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
icculus@9278
   138
        const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
icculus@9278
   139
        if (value < -deadzone) {
icculus@9278
   140
            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
icculus@9278
   141
            const double angle = axis_positions[i].angle;
philipp@10359
   142
            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
icculus@9278
   143
        } else if (value > deadzone) {
icculus@9278
   144
            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
icculus@9278
   145
            const double angle = axis_positions[i].angle + 180.0;
philipp@10359
   146
            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
icculus@9278
   147
        }
icculus@9278
   148
    }
icculus@9278
   149
icculus@9278
   150
    SDL_RenderPresent(screen);
icculus@9278
   151
icculus@9278
   152
    if (!SDL_GameControllerGetAttached(gamecontroller)) {
icculus@9278
   153
        done = SDL_TRUE;
icculus@9278
   154
        retval = SDL_TRUE;  /* keep going, wait for reattach. */
icculus@9278
   155
    }
philipp@9607
   156
philipp@9607
   157
#ifdef __EMSCRIPTEN__
philipp@9607
   158
    if (done) {
philipp@9607
   159
        emscripten_cancel_main_loop();
philipp@9607
   160
    }
philipp@9607
   161
#endif
icculus@9278
   162
}
icculus@9278
   163
slouken@8068
   164
SDL_bool
slouken@6690
   165
WatchGameController(SDL_GameController * gamecontroller)
slouken@6690
   166
{
icculus@6916
   167
    const char *name = SDL_GameControllerName(gamecontroller);
icculus@6914
   168
    const char *basetitle = "Game Controller Test: ";
icculus@6916
   169
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
aschiffler@7639
   170
    char *title = (char *)SDL_malloc(titlelen);
slouken@6690
   171
    SDL_Window *window = NULL;
philipp@9340
   172
philipp@9340
   173
    retval = SDL_FALSE;
philipp@9340
   174
    done = SDL_FALSE;
philipp@9340
   175
icculus@6914
   176
    if (title) {
icculus@6916
   177
        SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
icculus@6914
   178
    }
icculus@6914
   179
icculus@8199
   180
    /* Create a window to display controller state */
icculus@6914
   181
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
slouken@6690
   182
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
philipp@7478
   183
                              SCREEN_HEIGHT, 0);
philipp@10162
   184
    SDL_free(title);
philipp@10162
   185
    title = NULL;
slouken@6690
   186
    if (window == NULL) {
aschiffler@7639
   187
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
slouken@8068
   188
        return SDL_FALSE;
slouken@6690
   189
    }
slouken@6690
   190
slouken@6690
   191
    screen = SDL_CreateRenderer(window, -1, 0);
slouken@6690
   192
    if (screen == NULL) {
aschiffler@7639
   193
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
slouken@6690
   194
        SDL_DestroyWindow(window);
slouken@8068
   195
        return SDL_FALSE;
slouken@6690
   196
    }
slouken@6690
   197
slouken@6690
   198
    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
slouken@6690
   199
    SDL_RenderClear(screen);
slouken@6690
   200
    SDL_RenderPresent(screen);
jorgen@7056
   201
    SDL_RaiseWindow(window);
slouken@6690
   202
icculus@8202
   203
    /* scale for platforms that don't give you the window size you asked for. */
icculus@8202
   204
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
icculus@8202
   205
icculus@8199
   206
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
icculus@8199
   207
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
icculus@8199
   208
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
icculus@8199
   209
icculus@8199
   210
    if (!background || !button || !axis) {
icculus@8199
   211
        SDL_DestroyRenderer(screen);
icculus@8199
   212
        SDL_DestroyWindow(window);
icculus@8199
   213
        return SDL_FALSE;
icculus@8199
   214
    }
icculus@8199
   215
    SDL_SetTextureColorMod(button, 10, 255, 21);
icculus@8199
   216
    SDL_SetTextureColorMod(axis, 10, 255, 21);
icculus@8199
   217
icculus@8199
   218
    /* !!! FIXME: */
icculus@8199
   219
    /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
icculus@8199
   220
slouken@6690
   221
    /* Print info about the controller we are watching */
aschiffler@7639
   222
    SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
slouken@7191
   223
slouken@6690
   224
    /* Loop, getting controller events! */
icculus@9278
   225
#ifdef __EMSCRIPTEN__
icculus@9278
   226
    emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
icculus@9278
   227
#else
slouken@6690
   228
    while (!done) {
philipp@9290
   229
        loop(gamecontroller);
slouken@6690
   230
    }
icculus@9278
   231
#endif
slouken@6690
   232
slouken@6690
   233
    SDL_DestroyRenderer(screen);
philipp@9340
   234
    screen = NULL;
philipp@9340
   235
    background = NULL;
philipp@9340
   236
    button = NULL;
philipp@9340
   237
    axis = NULL;
slouken@6690
   238
    SDL_DestroyWindow(window);
slouken@8068
   239
    return retval;
slouken@6690
   240
}
slouken@6690
   241
slouken@6690
   242
int
slouken@6690
   243
main(int argc, char *argv[])
slouken@6690
   244
{
slouken@6690
   245
    int i;
jorgen@7056
   246
    int nController = 0;
jorgen@7056
   247
    int retcode = 0;
jorgen@7056
   248
    char guid[64];
slouken@6690
   249
    SDL_GameController *gamecontroller;
slouken@6690
   250
aschiffler@7639
   251
    /* Enable standard application logging */
philipp@9922
   252
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   253
slouken@6690
   254
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@6690
   255
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
aschiffler@7639
   256
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
jorgen@7056
   257
        return 1;
slouken@6690
   258
    }
gabomdq@8043
   259
    
gabomdq@8043
   260
    SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
slouken@6690
   261
slouken@10661
   262
    /* Print information about the mappings */
slouken@10691
   263
    if (!argv[1]) {
slouken@10691
   264
        SDL_Log("Supported mappings:\n");
slouken@10691
   265
        for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
slouken@10691
   266
            char *mapping = SDL_GameControllerMappingForIndex(i);
slouken@10691
   267
            if (mapping) {
slouken@10691
   268
                SDL_Log("\t%s\n", mapping);
slouken@10691
   269
                SDL_free(mapping);
slouken@10691
   270
            }
slouken@10661
   271
        }
slouken@10691
   272
        SDL_Log("\n");
slouken@10661
   273
    }
slouken@10661
   274
slouken@6690
   275
    /* Print information about the controller */
slouken@6690
   276
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
jorgen@7056
   277
        const char *name;
icculus@7705
   278
        const char *description;
jorgen@7056
   279
jorgen@7056
   280
        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
jorgen@7056
   281
                                  guid, sizeof (guid));
jorgen@7056
   282
jorgen@7056
   283
        if ( SDL_IsGameController(i) )
jorgen@7056
   284
        {
jorgen@7056
   285
            nController++;
jorgen@7056
   286
            name = SDL_GameControllerNameForIndex(i);
icculus@7705
   287
            description = "Controller";
jorgen@7056
   288
        } else {
jorgen@7056
   289
            name = SDL_JoystickNameForIndex(i);
icculus@7705
   290
            description = "Joystick";
jorgen@7056
   291
        }
slouken@10597
   292
        SDL_Log("%s %d: %s (guid %s, VID 0x%.4x, PID 0x%.4x)\n",
slouken@10597
   293
            description, i, name ? name : "Unknown", guid,
slouken@10597
   294
            SDL_JoystickGetDeviceVendor(i), SDL_JoystickGetDeviceProduct(i));
slouken@6690
   295
    }
aschiffler@7639
   296
    SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
slouken@6690
   297
slouken@6690
   298
    if (argv[1]) {
slouken@8068
   299
        SDL_bool reportederror = SDL_FALSE;
slouken@8068
   300
        SDL_bool keepGoing = SDL_TRUE;
slouken@8068
   301
        SDL_Event event;
jorgen@7056
   302
        int device = atoi(argv[1]);
jorgen@7056
   303
        if (device >= SDL_NumJoysticks()) {
philipp@9922
   304
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
jorgen@7056
   305
            retcode = 1;
jorgen@7056
   306
        } else {
jorgen@7056
   307
            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
jorgen@7056
   308
                                      guid, sizeof (guid));
aschiffler@7639
   309
            SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
jorgen@7056
   310
            gamecontroller = SDL_GameControllerOpen(device);
icculus@9916
   311
icculus@9916
   312
            if (gamecontroller != NULL) {
icculus@9916
   313
                SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
icculus@9916
   314
            }
icculus@9916
   315
slouken@8068
   316
            while (keepGoing) {
slouken@8068
   317
                if (gamecontroller == NULL) {
slouken@8068
   318
                    if (!reportederror) {
philipp@9611
   319
                        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
philipp@9611
   320
                        retcode = 1;
slouken@8068
   321
                        keepGoing = SDL_FALSE;
slouken@8068
   322
                        reportederror = SDL_TRUE;
slouken@8068
   323
                    }
slouken@8068
   324
                } else {
slouken@8068
   325
                    reportederror = SDL_FALSE;
slouken@8068
   326
                    keepGoing = WatchGameController(gamecontroller);
slouken@8068
   327
                    SDL_GameControllerClose(gamecontroller);
slouken@8068
   328
                }
slouken@8068
   329
slouken@8068
   330
                gamecontroller = NULL;
slouken@8068
   331
                if (keepGoing) {
slouken@8068
   332
                    SDL_Log("Waiting for attach\n");
slouken@8068
   333
                }
slouken@8068
   334
                while (keepGoing) {
slouken@8068
   335
                    SDL_WaitEvent(&event);
slouken@8068
   336
                    if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
slouken@8068
   337
                        || (event.type == SDL_MOUSEBUTTONDOWN)) {
slouken@8068
   338
                        keepGoing = SDL_FALSE;
slouken@8068
   339
                    } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
slouken@8068
   340
                        gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
icculus@9916
   341
                        if (gamecontroller != NULL) {
icculus@9916
   342
                            SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
icculus@9916
   343
                        }
slouken@8068
   344
                        break;
slouken@8068
   345
                    }
slouken@8068
   346
                }
jorgen@7056
   347
            }
jorgen@7056
   348
        }
jorgen@7056
   349
    }
slouken@6690
   350
jorgen@7056
   351
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
jorgen@7056
   352
jorgen@7056
   353
    return retcode;
slouken@6690
   354
}
aschiffler@6771
   355
aschiffler@6771
   356
#else
aschiffler@6771
   357
aschiffler@6771
   358
int
aschiffler@6771
   359
main(int argc, char *argv[])
aschiffler@6771
   360
{
aschiffler@7639
   361
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
aschiffler@6771
   362
    exit(1);
aschiffler@6771
   363
}
aschiffler@6771
   364
aschiffler@6771
   365
#endif
slouken@10597
   366
slouken@10597
   367
/* vi: set ts=4 sw=4 expandtab: */