test/testgamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 07 Jul 2019 09:10:56 -0700
changeset 12928 3c4a4b1077cd
parent 12503 806492103856
child 13070 d953f28d33e3
permissions -rw-r--r--
Fixed bug 4710 - audio/alsa: avoid configuring hardware parameters with only a single period

Anthony Pesch

The previous code first configured the period size using snd_pcm_hw_par-
ams_set_period_size_near. Then, it further narrowed the configuration
space by calling snd_pcm_hw_params_set_buffer_size_near using a buffer
size of 2 times the _requested_ period size in order to try and get a
configuration with only 2 periods. If the configured period size was
larger than the requested size, the second call could inadvertently
narrow the configuration space to contain only a single period.

Rather than fixing the call to snd_pcm_hw_params_set_buffer_size_near
to use a size of 2 times the configured period size, the code has been
changed to use snd_pcm_hw_params_set_periods_min in order to more
clearly explain the intent.
slouken@6690
     1
/*
slouken@12503
     2
  Copyright (C) 1997-2019 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[] = {
slouken@10724
    56
    {74,  153, 270.0},  /* LEFTX */
slouken@10724
    57
    {74,  153, 0.0},  /* LEFTY */
slouken@10724
    58
    {306, 231, 270.0},  /* RIGHTX */
slouken@10724
    59
    {306, 231, 0.0},  /* RIGHTY */
slouken@10724
    60
    {91, -20, 0.0},     /* TRIGGERLEFT */
slouken@10724
    61
    {375, -20, 0.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
            }
icculus@8199
    84
        }
icculus@8199
    85
icculus@8199
    86
        texture = SDL_CreateTextureFromSurface(renderer, temp);
icculus@8199
    87
        if (!texture) {
icculus@8199
    88
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
icculus@8199
    89
        }
icculus@8199
    90
    }
icculus@8199
    91
    if (temp) {
icculus@8199
    92
        SDL_FreeSurface(temp);
icculus@8199
    93
    }
icculus@8199
    94
    return texture;
icculus@8199
    95
}
icculus@8199
    96
icculus@9278
    97
void
icculus@9278
    98
loop(void *arg)
icculus@9278
    99
{
icculus@9278
   100
    SDL_Event event;
icculus@9278
   101
    int i;
icculus@9278
   102
    SDL_GameController *gamecontroller = (SDL_GameController *)arg;
icculus@9278
   103
icculus@9278
   104
    /* blank screen, set up for drawing this frame. */
icculus@9278
   105
    SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
icculus@9278
   106
    SDL_RenderClear(screen);
icculus@9278
   107
    SDL_RenderCopy(screen, background, NULL, NULL);
icculus@9278
   108
icculus@9278
   109
    while (SDL_PollEvent(&event)) {
icculus@9278
   110
        switch (event.type) {
slouken@10724
   111
        case SDL_CONTROLLERAXISMOTION:
philipp@10730
   112
            SDL_Log("Controller axis %s changed to %d\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
slouken@10724
   113
            break;
slouken@10724
   114
        case SDL_CONTROLLERBUTTONDOWN:
slouken@10724
   115
        case SDL_CONTROLLERBUTTONUP:
philipp@10730
   116
            SDL_Log("Controller button %s %s\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
slouken@12088
   117
            /* First button triggers a 0.5 second full strength rumble */
slouken@12088
   118
            if (event.type == SDL_CONTROLLERBUTTONDOWN &&
slouken@12088
   119
                event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
slouken@12088
   120
                SDL_GameControllerRumble(gamecontroller, 0xFFFF, 0xFFFF, 500);
slouken@12088
   121
            }
slouken@10724
   122
            break;
icculus@9278
   123
        case SDL_KEYDOWN:
icculus@9278
   124
            if (event.key.keysym.sym != SDLK_ESCAPE) {
icculus@9278
   125
                break;
icculus@9278
   126
            }
icculus@9278
   127
            /* Fall through to signal quit */
icculus@9278
   128
        case SDL_QUIT:
icculus@9278
   129
            done = SDL_TRUE;
icculus@9278
   130
            break;
icculus@9278
   131
        default:
icculus@9278
   132
            break;
icculus@9278
   133
        }
icculus@9278
   134
    }
icculus@9278
   135
icculus@9278
   136
    /* Update visual controller state */
icculus@9278
   137
    for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
icculus@9278
   138
        if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
icculus@9278
   139
            const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
philipp@10359
   140
            SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, SDL_FLIP_NONE);
icculus@9278
   141
        }
icculus@9278
   142
    }
icculus@9278
   143
icculus@9278
   144
    for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
icculus@9278
   145
        const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
icculus@9278
   146
        const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
icculus@9278
   147
        if (value < -deadzone) {
icculus@9278
   148
            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
icculus@9278
   149
            const double angle = axis_positions[i].angle;
philipp@10359
   150
            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
icculus@9278
   151
        } else if (value > deadzone) {
icculus@9278
   152
            const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
icculus@9278
   153
            const double angle = axis_positions[i].angle + 180.0;
philipp@10359
   154
            SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
icculus@9278
   155
        }
icculus@9278
   156
    }
icculus@9278
   157
icculus@9278
   158
    SDL_RenderPresent(screen);
icculus@9278
   159
icculus@9278
   160
    if (!SDL_GameControllerGetAttached(gamecontroller)) {
icculus@9278
   161
        done = SDL_TRUE;
icculus@9278
   162
        retval = SDL_TRUE;  /* keep going, wait for reattach. */
icculus@9278
   163
    }
philipp@9607
   164
philipp@9607
   165
#ifdef __EMSCRIPTEN__
philipp@9607
   166
    if (done) {
philipp@9607
   167
        emscripten_cancel_main_loop();
philipp@9607
   168
    }
philipp@9607
   169
#endif
icculus@9278
   170
}
icculus@9278
   171
slouken@8068
   172
SDL_bool
slouken@6690
   173
WatchGameController(SDL_GameController * gamecontroller)
slouken@6690
   174
{
icculus@6916
   175
    const char *name = SDL_GameControllerName(gamecontroller);
icculus@6914
   176
    const char *basetitle = "Game Controller Test: ";
icculus@6916
   177
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
aschiffler@7639
   178
    char *title = (char *)SDL_malloc(titlelen);
slouken@6690
   179
    SDL_Window *window = NULL;
philipp@9340
   180
philipp@9340
   181
    retval = SDL_FALSE;
philipp@9340
   182
    done = SDL_FALSE;
philipp@9340
   183
icculus@6914
   184
    if (title) {
icculus@6916
   185
        SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
icculus@6914
   186
    }
icculus@6914
   187
icculus@8199
   188
    /* Create a window to display controller state */
icculus@6914
   189
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
slouken@6690
   190
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
philipp@7478
   191
                              SCREEN_HEIGHT, 0);
philipp@10162
   192
    SDL_free(title);
philipp@10162
   193
    title = NULL;
slouken@6690
   194
    if (window == NULL) {
aschiffler@7639
   195
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
slouken@8068
   196
        return SDL_FALSE;
slouken@6690
   197
    }
slouken@6690
   198
slouken@6690
   199
    screen = SDL_CreateRenderer(window, -1, 0);
slouken@6690
   200
    if (screen == NULL) {
aschiffler@7639
   201
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
slouken@6690
   202
        SDL_DestroyWindow(window);
slouken@8068
   203
        return SDL_FALSE;
slouken@6690
   204
    }
slouken@6690
   205
slouken@6690
   206
    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
slouken@6690
   207
    SDL_RenderClear(screen);
slouken@6690
   208
    SDL_RenderPresent(screen);
jorgen@7056
   209
    SDL_RaiseWindow(window);
slouken@6690
   210
icculus@8202
   211
    /* scale for platforms that don't give you the window size you asked for. */
icculus@8202
   212
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
icculus@8202
   213
icculus@8199
   214
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
icculus@8199
   215
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
icculus@8199
   216
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
icculus@8199
   217
icculus@8199
   218
    if (!background || !button || !axis) {
icculus@8199
   219
        SDL_DestroyRenderer(screen);
icculus@8199
   220
        SDL_DestroyWindow(window);
icculus@8199
   221
        return SDL_FALSE;
icculus@8199
   222
    }
icculus@8199
   223
    SDL_SetTextureColorMod(button, 10, 255, 21);
icculus@8199
   224
    SDL_SetTextureColorMod(axis, 10, 255, 21);
icculus@8199
   225
icculus@8199
   226
    /* !!! FIXME: */
icculus@8199
   227
    /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
icculus@8199
   228
slouken@6690
   229
    /* Print info about the controller we are watching */
aschiffler@7639
   230
    SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
slouken@7191
   231
slouken@6690
   232
    /* Loop, getting controller events! */
icculus@9278
   233
#ifdef __EMSCRIPTEN__
icculus@9278
   234
    emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
icculus@9278
   235
#else
slouken@6690
   236
    while (!done) {
philipp@9290
   237
        loop(gamecontroller);
slouken@6690
   238
    }
icculus@9278
   239
#endif
slouken@6690
   240
slouken@6690
   241
    SDL_DestroyRenderer(screen);
philipp@9340
   242
    screen = NULL;
philipp@9340
   243
    background = NULL;
philipp@9340
   244
    button = NULL;
philipp@9340
   245
    axis = NULL;
slouken@6690
   246
    SDL_DestroyWindow(window);
slouken@8068
   247
    return retval;
slouken@6690
   248
}
slouken@6690
   249
slouken@6690
   250
int
slouken@6690
   251
main(int argc, char *argv[])
slouken@6690
   252
{
slouken@6690
   253
    int i;
jorgen@7056
   254
    int nController = 0;
jorgen@7056
   255
    int retcode = 0;
jorgen@7056
   256
    char guid[64];
slouken@6690
   257
    SDL_GameController *gamecontroller;
slouken@6690
   258
aschiffler@7639
   259
    /* Enable standard application logging */
philipp@9922
   260
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   261
slouken@6690
   262
    /* Initialize SDL (Note: video is required to start event loop) */
slouken@6690
   263
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
aschiffler@7639
   264
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
jorgen@7056
   265
        return 1;
slouken@6690
   266
    }
gabomdq@8043
   267
    
gabomdq@8043
   268
    SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
slouken@6690
   269
slouken@10661
   270
    /* Print information about the mappings */
slouken@10691
   271
    if (!argv[1]) {
slouken@10691
   272
        SDL_Log("Supported mappings:\n");
slouken@10691
   273
        for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
slouken@10691
   274
            char *mapping = SDL_GameControllerMappingForIndex(i);
slouken@10691
   275
            if (mapping) {
slouken@10691
   276
                SDL_Log("\t%s\n", mapping);
slouken@10691
   277
                SDL_free(mapping);
slouken@10691
   278
            }
slouken@10661
   279
        }
slouken@10691
   280
        SDL_Log("\n");
slouken@10661
   281
    }
slouken@10661
   282
slouken@6690
   283
    /* Print information about the controller */
slouken@6690
   284
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
jorgen@7056
   285
        const char *name;
icculus@7705
   286
        const char *description;
jorgen@7056
   287
jorgen@7056
   288
        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
jorgen@7056
   289
                                  guid, sizeof (guid));
jorgen@7056
   290
jorgen@7056
   291
        if ( SDL_IsGameController(i) )
jorgen@7056
   292
        {
jorgen@7056
   293
            nController++;
jorgen@7056
   294
            name = SDL_GameControllerNameForIndex(i);
icculus@7705
   295
            description = "Controller";
jorgen@7056
   296
        } else {
jorgen@7056
   297
            name = SDL_JoystickNameForIndex(i);
icculus@7705
   298
            description = "Joystick";
jorgen@7056
   299
        }
slouken@10597
   300
        SDL_Log("%s %d: %s (guid %s, VID 0x%.4x, PID 0x%.4x)\n",
slouken@10597
   301
            description, i, name ? name : "Unknown", guid,
slouken@10597
   302
            SDL_JoystickGetDeviceVendor(i), SDL_JoystickGetDeviceProduct(i));
slouken@6690
   303
    }
aschiffler@7639
   304
    SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
slouken@6690
   305
slouken@6690
   306
    if (argv[1]) {
slouken@8068
   307
        SDL_bool reportederror = SDL_FALSE;
slouken@8068
   308
        SDL_bool keepGoing = SDL_TRUE;
slouken@8068
   309
        SDL_Event event;
jorgen@7056
   310
        int device = atoi(argv[1]);
jorgen@7056
   311
        if (device >= SDL_NumJoysticks()) {
philipp@9922
   312
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
jorgen@7056
   313
            retcode = 1;
jorgen@7056
   314
        } else {
jorgen@7056
   315
            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
jorgen@7056
   316
                                      guid, sizeof (guid));
aschiffler@7639
   317
            SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
jorgen@7056
   318
            gamecontroller = SDL_GameControllerOpen(device);
icculus@9916
   319
icculus@9916
   320
            if (gamecontroller != NULL) {
icculus@9916
   321
                SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
icculus@9916
   322
            }
icculus@9916
   323
slouken@8068
   324
            while (keepGoing) {
slouken@8068
   325
                if (gamecontroller == NULL) {
slouken@8068
   326
                    if (!reportederror) {
philipp@9611
   327
                        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
philipp@9611
   328
                        retcode = 1;
slouken@8068
   329
                        keepGoing = SDL_FALSE;
slouken@8068
   330
                        reportederror = SDL_TRUE;
slouken@8068
   331
                    }
slouken@8068
   332
                } else {
slouken@8068
   333
                    reportederror = SDL_FALSE;
slouken@8068
   334
                    keepGoing = WatchGameController(gamecontroller);
slouken@8068
   335
                    SDL_GameControllerClose(gamecontroller);
slouken@8068
   336
                }
slouken@8068
   337
slouken@8068
   338
                gamecontroller = NULL;
slouken@8068
   339
                if (keepGoing) {
slouken@8068
   340
                    SDL_Log("Waiting for attach\n");
slouken@8068
   341
                }
slouken@8068
   342
                while (keepGoing) {
slouken@8068
   343
                    SDL_WaitEvent(&event);
slouken@8068
   344
                    if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
slouken@8068
   345
                        || (event.type == SDL_MOUSEBUTTONDOWN)) {
slouken@8068
   346
                        keepGoing = SDL_FALSE;
slouken@8068
   347
                    } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
slouken@8068
   348
                        gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
icculus@9916
   349
                        if (gamecontroller != NULL) {
icculus@9916
   350
                            SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
icculus@9916
   351
                        }
slouken@8068
   352
                        break;
slouken@8068
   353
                    }
slouken@8068
   354
                }
jorgen@7056
   355
            }
jorgen@7056
   356
        }
jorgen@7056
   357
    }
slouken@6690
   358
jorgen@7056
   359
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
jorgen@7056
   360
jorgen@7056
   361
    return retcode;
slouken@6690
   362
}
aschiffler@6771
   363
aschiffler@6771
   364
#else
aschiffler@6771
   365
aschiffler@6771
   366
int
aschiffler@6771
   367
main(int argc, char *argv[])
aschiffler@6771
   368
{
aschiffler@7639
   369
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
aschiffler@6771
   370
    exit(1);
aschiffler@6771
   371
}
aschiffler@6771
   372
aschiffler@6771
   373
#endif
slouken@10597
   374
slouken@10597
   375
/* vi: set ts=4 sw=4 expandtab: */