test/controllermap.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 10 Nov 2016 18:53:50 -0800
changeset 10597 7b2a0f5a4380
parent 10542 6a45cad101a4
child 10705 997239dcb197
permissions -rw-r--r--
Added USB VID/PID information to the SDL test programs
gabomdq@8043
     1
/*
slouken@9998
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
gabomdq@8043
     3
gabomdq@8043
     4
  This software is provided 'as-is', without any express or implied
gabomdq@8043
     5
  warranty.  In no event will the authors be held liable for any damages
gabomdq@8043
     6
  arising from the use of this software.
gabomdq@8043
     7
gabomdq@8043
     8
  Permission is granted to anyone to use this software for any purpose,
gabomdq@8043
     9
  including commercial applications, and to alter it and redistribute it
gabomdq@8043
    10
  freely.
gabomdq@8043
    11
*/
gabomdq@8043
    12
gabomdq@8043
    13
/* Game controller mapping generator */
gabomdq@8043
    14
/* Gabriel Jacobo <gabomdq@gmail.com> */
gabomdq@8043
    15
gabomdq@8043
    16
#include <stdio.h>
gabomdq@8043
    17
#include <stdlib.h>
gabomdq@8043
    18
#include <string.h>
gabomdq@8043
    19
gabomdq@8043
    20
#include "SDL.h"
gabomdq@8043
    21
gabomdq@8043
    22
#ifndef SDL_JOYSTICK_DISABLED
gabomdq@8043
    23
gabomdq@8043
    24
#ifdef __IPHONEOS__
gabomdq@8043
    25
#define SCREEN_WIDTH    320
gabomdq@8043
    26
#define SCREEN_HEIGHT   480
gabomdq@8043
    27
#else
gabomdq@8043
    28
#define SCREEN_WIDTH    512
slouken@10526
    29
#define SCREEN_HEIGHT   320
gabomdq@8043
    30
#endif
gabomdq@8043
    31
gabomdq@8043
    32
#define MARKER_BUTTON 1
gabomdq@8043
    33
#define MARKER_AXIS 2
gabomdq@8043
    34
gabomdq@8043
    35
typedef struct MappingStep
gabomdq@8043
    36
{
gabomdq@8043
    37
    int x, y;
gabomdq@8043
    38
    double angle;
gabomdq@8043
    39
    int marker;
gabomdq@8043
    40
    char *field;
gabomdq@8043
    41
    int axis, button, hat, hat_value;
gabomdq@8043
    42
    char mapping[4096];
gabomdq@8043
    43
}MappingStep;
gabomdq@8043
    44
gabomdq@8043
    45
gabomdq@8043
    46
SDL_Texture *
philipp@10359
    47
LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
gabomdq@8043
    48
{
gabomdq@8043
    49
    SDL_Surface *temp;
gabomdq@8043
    50
    SDL_Texture *texture;
gabomdq@8043
    51
gabomdq@8043
    52
    /* Load the sprite image */
gabomdq@8043
    53
    temp = SDL_LoadBMP(file);
gabomdq@8043
    54
    if (temp == NULL) {
gabomdq@8043
    55
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
gabomdq@8043
    56
        return NULL;
gabomdq@8043
    57
    }
gabomdq@8043
    58
gabomdq@8043
    59
    /* Set transparent pixel as the pixel at (0,0) */
gabomdq@8043
    60
    if (transparent) {
gabomdq@8043
    61
        if (temp->format->palette) {
gabomdq@8043
    62
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
gabomdq@8043
    63
        } else {
gabomdq@8043
    64
            switch (temp->format->BitsPerPixel) {
gabomdq@8043
    65
            case 15:
gabomdq@8043
    66
                SDL_SetColorKey(temp, SDL_TRUE,
gabomdq@8043
    67
                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
gabomdq@8043
    68
                break;
gabomdq@8043
    69
            case 16:
gabomdq@8043
    70
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
gabomdq@8043
    71
                break;
gabomdq@8043
    72
            case 24:
gabomdq@8043
    73
                SDL_SetColorKey(temp, SDL_TRUE,
gabomdq@8043
    74
                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
gabomdq@8043
    75
                break;
gabomdq@8043
    76
            case 32:
gabomdq@8043
    77
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
gabomdq@8043
    78
                break;
gabomdq@8043
    79
            }
gabomdq@8043
    80
        }
gabomdq@8043
    81
    }
gabomdq@8043
    82
gabomdq@8043
    83
    /* Create textures from the image */
gabomdq@8043
    84
    texture = SDL_CreateTextureFromSurface(renderer, temp);
gabomdq@8043
    85
    if (!texture) {
gabomdq@8043
    86
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
gabomdq@8043
    87
        SDL_FreeSurface(temp);
gabomdq@8043
    88
        return NULL;
gabomdq@8043
    89
    }
gabomdq@8043
    90
    SDL_FreeSurface(temp);
gabomdq@8043
    91
gabomdq@8043
    92
    /* We're ready to roll. :) */
gabomdq@8043
    93
    return texture;
gabomdq@8043
    94
}
gabomdq@8043
    95
gabomdq@8043
    96
static SDL_bool
gabomdq@8043
    97
WatchJoystick(SDL_Joystick * joystick)
gabomdq@8043
    98
{
gabomdq@8043
    99
    SDL_Window *window = NULL;
gabomdq@8043
   100
    SDL_Renderer *screen = NULL;
icculus@8202
   101
    SDL_Texture *background, *button, *axis, *marker;
gabomdq@8043
   102
    const char *name = NULL;
gabomdq@8043
   103
    SDL_bool retval = SDL_FALSE;
gabomdq@8043
   104
    SDL_bool done = SDL_FALSE, next=SDL_FALSE;
gabomdq@8043
   105
    SDL_Event event;
gabomdq@8043
   106
    SDL_Rect dst;
philipp@8077
   107
    int s, _s;
gabomdq@8043
   108
    Uint8 alpha=200, alpha_step = -1;
philipp@8786
   109
    Uint32 alpha_ticks = 0;
gabomdq@8043
   110
    char mapping[4096], temp[4096];
gabomdq@8677
   111
    MappingStep *step, *prev_step;
gabomdq@8043
   112
    MappingStep steps[] = {
gabomdq@8043
   113
        {342, 132,  0.0,  MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
gabomdq@8043
   114
        {387, 167,  0.0,  MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
gabomdq@8043
   115
        {431, 132,  0.0,  MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
gabomdq@8043
   116
        {389, 101,  0.0,  MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
gabomdq@8043
   117
        {174, 132,  0.0,  MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
gabomdq@8043
   118
        {233, 132,  0.0,  MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
gabomdq@8043
   119
        {289, 132,  0.0,  MARKER_BUTTON, "start", -1, -1, -1, -1, ""},        
gabomdq@8043
   120
        {116, 217,  0.0,  MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
gabomdq@8043
   121
        {154, 249,  0.0,  MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
gabomdq@8043
   122
        {186, 217,  0.0,  MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
gabomdq@8043
   123
        {154, 188,  0.0,  MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
gabomdq@8043
   124
        {77,  40,   0.0,  MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
gabomdq@8043
   125
        {91, 0,    0.0,  MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
gabomdq@8043
   126
        {396, 36,   0.0,  MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
gabomdq@8043
   127
        {375, 0,    0.0,  MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
gabomdq@8043
   128
        {75,  154,  0.0,  MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
gabomdq@8043
   129
        {305, 230,  0.0,  MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
gabomdq@8043
   130
        {75,  154,  0.0,  MARKER_AXIS,   "leftx", -1, -1, -1, -1, ""},
gabomdq@8043
   131
        {75,  154,  90.0, MARKER_AXIS,   "lefty", -1, -1, -1, -1, ""},        
gabomdq@8043
   132
        {305, 230,  0.0,  MARKER_AXIS,   "rightx", -1, -1, -1, -1, ""},
gabomdq@8043
   133
        {305, 230,  90.0, MARKER_AXIS,   "righty", -1, -1, -1, -1, ""},
gabomdq@8043
   134
    };
gabomdq@8043
   135
gabomdq@8043
   136
    /* Create a window to display joystick axis position */
gabomdq@8046
   137
    window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
gabomdq@8043
   138
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
gabomdq@8043
   139
                              SCREEN_HEIGHT, 0);
gabomdq@8043
   140
    if (window == NULL) {
gabomdq@8043
   141
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
gabomdq@8043
   142
        return SDL_FALSE;
gabomdq@8043
   143
    }
gabomdq@8043
   144
gabomdq@8043
   145
    screen = SDL_CreateRenderer(window, -1, 0);
gabomdq@8043
   146
    if (screen == NULL) {
gabomdq@8043
   147
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
gabomdq@8043
   148
        SDL_DestroyWindow(window);
gabomdq@8043
   149
        return SDL_FALSE;
gabomdq@8043
   150
    }
gabomdq@8043
   151
    
gabomdq@8043
   152
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
gabomdq@8043
   153
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
gabomdq@8043
   154
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
gabomdq@8043
   155
    SDL_RaiseWindow(window);
gabomdq@8043
   156
icculus@8202
   157
    /* scale for platforms that don't give you the window size you asked for. */
icculus@8202
   158
    SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
icculus@8202
   159
gabomdq@8043
   160
    /* Print info about the joystick we are watching */
gabomdq@8043
   161
    name = SDL_JoystickName(joystick);
gabomdq@8043
   162
    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
gabomdq@8043
   163
           name ? name : "Unknown Joystick");
gabomdq@8043
   164
    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
gabomdq@8043
   165
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
gabomdq@8043
   166
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
gabomdq@8043
   167
    
gabomdq@8043
   168
    SDL_Log("\n\n\
gabomdq@8043
   169
    ====================================================================================\n\
gabomdq@8043
   170
    Press the buttons on your controller when indicated\n\
gabomdq@8043
   171
    (Your controller may look different than the picture)\n\
gabomdq@8043
   172
    If you want to correct a mistake, press backspace or the back button on your device\n\
gabomdq@8046
   173
    To skip a button, press SPACE or click/touch the screen\n\
gabomdq@8043
   174
    To exit, press ESC\n\
gabomdq@8043
   175
    ====================================================================================\n");
gabomdq@8043
   176
    
gabomdq@8043
   177
    /* Initialize mapping with GUID and name */
gabomdq@8043
   178
    SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
gabomdq@8043
   179
    SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
gabomdq@8043
   180
        temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
gabomdq@8043
   181
gabomdq@8043
   182
    /* Loop, getting joystick events! */
gabomdq@8043
   183
    for(s=0; s<SDL_arraysize(steps) && !done;) {
gabomdq@8043
   184
        /* blank screen, set up for drawing this frame. */
gabomdq@8043
   185
        step = &steps[s];
gabomdq@8043
   186
        SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
gabomdq@8043
   187
        step->axis = -1;
gabomdq@8043
   188
        step->button = -1;
gabomdq@8043
   189
        step->hat = -1;
gabomdq@8043
   190
        step->hat_value = -1;
gabomdq@8043
   191
        
gabomdq@8043
   192
        switch(step->marker) {
gabomdq@8043
   193
            case MARKER_AXIS:
gabomdq@8043
   194
                marker = axis;
gabomdq@8043
   195
                break;
gabomdq@8043
   196
            case MARKER_BUTTON:
gabomdq@8043
   197
                marker = button;
gabomdq@8043
   198
                break;
gabomdq@8043
   199
            default:
gabomdq@8043
   200
                break;
gabomdq@8043
   201
        }
gabomdq@8043
   202
        
gabomdq@8043
   203
        dst.x = step->x;
gabomdq@8043
   204
        dst.y = step->y;
gabomdq@8043
   205
        SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
gabomdq@8043
   206
        next=SDL_FALSE;
icculus@8202
   207
icculus@8202
   208
        SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
icculus@8202
   209
gabomdq@8043
   210
        while (!done && !next) {
gabomdq@8043
   211
            if (SDL_GetTicks() - alpha_ticks > 5) {
gabomdq@8043
   212
                alpha_ticks = SDL_GetTicks();
gabomdq@8043
   213
                alpha += alpha_step;
gabomdq@8043
   214
                if (alpha == 255) {
gabomdq@8043
   215
                    alpha_step = -1;
gabomdq@8043
   216
                }
gabomdq@8043
   217
                if (alpha < 128) {
gabomdq@8043
   218
                    alpha_step = 1;
gabomdq@8043
   219
                }
gabomdq@8043
   220
            }
gabomdq@8043
   221
            
icculus@8202
   222
            SDL_RenderClear(screen);
gabomdq@8043
   223
            SDL_RenderCopy(screen, background, NULL, NULL);
gabomdq@8043
   224
            SDL_SetTextureAlphaMod(marker, alpha);
gabomdq@8043
   225
            SDL_SetTextureColorMod(marker, 10, 255, 21);
philipp@10359
   226
            SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, SDL_FLIP_NONE);
gabomdq@8043
   227
            SDL_RenderPresent(screen);
gabomdq@8043
   228
            
gabomdq@8043
   229
            if (SDL_PollEvent(&event)) {
gabomdq@8043
   230
                switch (event.type) {
gabomdq@8043
   231
                case SDL_JOYAXISMOTION:
slouken@9962
   232
                    if ((event.jaxis.value > 20000 || event.jaxis.value < -20000) && event.jaxis.value != -32768) {
gabomdq@8043
   233
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   234
                            if (steps[_s].axis == event.jaxis.axis) {
gabomdq@8043
   235
                                break;
gabomdq@8043
   236
                            }
gabomdq@8043
   237
                        }
gabomdq@8043
   238
                        if (_s == s) {
gabomdq@8043
   239
                            step->axis = event.jaxis.axis;
gabomdq@8043
   240
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   241
                            SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
gabomdq@8043
   242
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   243
                            s++;
gabomdq@8043
   244
                            next=SDL_TRUE;
gabomdq@8043
   245
                        }
gabomdq@8043
   246
                    }
gabomdq@8043
   247
                    
gabomdq@8043
   248
                    break;
gabomdq@8043
   249
                case SDL_JOYHATMOTION:
icculus@8631
   250
                        if (event.jhat.value == SDL_HAT_CENTERED) {
icculus@8631
   251
                            break;  /* ignore centering, we're probably just coming back to the center from the previous item we set. */
icculus@8631
   252
                        }
gabomdq@8043
   253
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   254
                            if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
gabomdq@8043
   255
                                break;
gabomdq@8043
   256
                            }
gabomdq@8043
   257
                        }
gabomdq@8043
   258
                        if (_s == s) {
gabomdq@8043
   259
                            step->hat = event.jhat.hat;
gabomdq@8043
   260
                            step->hat_value = event.jhat.value;
gabomdq@8043
   261
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   262
                            SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
gabomdq@8043
   263
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   264
                            s++;
gabomdq@8043
   265
                            next=SDL_TRUE;
gabomdq@8043
   266
                        }
gabomdq@8043
   267
                    break;
gabomdq@8043
   268
                case SDL_JOYBALLMOTION:
gabomdq@8043
   269
                    break;
gabomdq@8043
   270
                case SDL_JOYBUTTONUP:
gabomdq@8043
   271
                    for (_s = 0; _s < s; _s++) {
gabomdq@8043
   272
                        if (steps[_s].button == event.jbutton.button) {
gabomdq@8043
   273
                            break;
gabomdq@8043
   274
                        }
gabomdq@8043
   275
                    }
gabomdq@8043
   276
                    if (_s == s) {
gabomdq@8043
   277
                        step->button = event.jbutton.button;
gabomdq@8043
   278
                        SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   279
                        SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
gabomdq@8043
   280
                        SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   281
                        s++;
gabomdq@8043
   282
                        next=SDL_TRUE;
gabomdq@8043
   283
                    }
gabomdq@8043
   284
                    break;
gabomdq@8046
   285
                case SDL_FINGERDOWN:
gabomdq@8046
   286
                case SDL_MOUSEBUTTONDOWN:
gabomdq@8046
   287
                    /* Skip this step */
gabomdq@8046
   288
                    s++;
gabomdq@8046
   289
                    next=SDL_TRUE;
gabomdq@8046
   290
                    break;
gabomdq@8043
   291
                case SDL_KEYDOWN:
gabomdq@8043
   292
                    if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
gabomdq@8043
   293
                        /* Undo! */
gabomdq@8043
   294
                        if (s > 0) {
gabomdq@8677
   295
                            prev_step = &steps[--s];
gabomdq@8677
   296
                            SDL_strlcpy(mapping, prev_step->mapping, SDL_arraysize(prev_step->mapping));
gabomdq@8043
   297
                            next = SDL_TRUE;
gabomdq@8043
   298
                        }
gabomdq@8043
   299
                        break;
gabomdq@8043
   300
                    }
gabomdq@8046
   301
                    if (event.key.keysym.sym == SDLK_SPACE) {
gabomdq@8046
   302
                        /* Skip this step */
gabomdq@8046
   303
                        s++;
gabomdq@8046
   304
                        next=SDL_TRUE;
gabomdq@8046
   305
                        break;
gabomdq@8046
   306
                    }
gabomdq@8046
   307
                    
gabomdq@8043
   308
                    if ((event.key.keysym.sym != SDLK_ESCAPE)) {
gabomdq@8043
   309
                        break;
gabomdq@8043
   310
                    }
gabomdq@8043
   311
                    /* Fall through to signal quit */
gabomdq@8043
   312
                case SDL_QUIT:
gabomdq@8043
   313
                    done = SDL_TRUE;
gabomdq@8043
   314
                    break;
gabomdq@8043
   315
                default:
gabomdq@8043
   316
                    break;
gabomdq@8043
   317
                }
gabomdq@8043
   318
            }
gabomdq@8043
   319
        }
gabomdq@8043
   320
gabomdq@8043
   321
    }
gabomdq@8043
   322
gabomdq@8043
   323
    if (s == SDL_arraysize(steps) ) {
gabomdq@8043
   324
        SDL_Log("Mapping:\n\n%s\n\n", mapping);
gabomdq@8043
   325
        /* Print to stdout as well so the user can cat the output somewhere */
gabomdq@8043
   326
        printf("%s\n", mapping);
gabomdq@8043
   327
    }
gabomdq@8043
   328
    
gabomdq@8043
   329
    while(SDL_PollEvent(&event)) {};
gabomdq@8043
   330
    
gabomdq@8043
   331
    SDL_DestroyRenderer(screen);
gabomdq@8043
   332
    SDL_DestroyWindow(window);
gabomdq@8043
   333
    return retval;
gabomdq@8043
   334
}
gabomdq@8043
   335
gabomdq@8043
   336
int
gabomdq@8043
   337
main(int argc, char *argv[])
gabomdq@8043
   338
{
gabomdq@8043
   339
    const char *name;
gabomdq@8043
   340
    int i;
gabomdq@8043
   341
    SDL_Joystick *joystick;
gabomdq@8043
   342
gabomdq@8043
   343
    /* Enable standard application logging */
philipp@9922
   344
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
gabomdq@8043
   345
gabomdq@8043
   346
    /* Initialize SDL (Note: video is required to start event loop) */
gabomdq@8043
   347
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
gabomdq@8043
   348
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
gabomdq@8043
   349
        exit(1);
gabomdq@8043
   350
    }
gabomdq@8043
   351
gabomdq@8043
   352
    /* Print information about the joysticks */
gabomdq@8043
   353
    SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
gabomdq@8043
   354
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
gabomdq@8043
   355
        name = SDL_JoystickNameForIndex(i);
gabomdq@8043
   356
        SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
gabomdq@8043
   357
        joystick = SDL_JoystickOpen(i);
gabomdq@8043
   358
        if (joystick == NULL) {
gabomdq@8043
   359
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
gabomdq@8043
   360
                    SDL_GetError());
gabomdq@8043
   361
        } else {
gabomdq@8043
   362
            char guid[64];
gabomdq@8043
   363
            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
gabomdq@8043
   364
                                      guid, sizeof (guid));
gabomdq@8043
   365
            SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
gabomdq@8043
   366
            SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
gabomdq@8043
   367
            SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
gabomdq@8043
   368
            SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
gabomdq@8043
   369
            SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
gabomdq@8043
   370
            SDL_Log("       guid: %s\n", guid);
slouken@10597
   371
            SDL_Log("    VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
gabomdq@8043
   372
            SDL_JoystickClose(joystick);
gabomdq@8043
   373
        }
gabomdq@8043
   374
    }
gabomdq@8043
   375
gabomdq@8762
   376
#ifdef __ANDROID__
gabomdq@8043
   377
    if (SDL_NumJoysticks() > 0) {
gabomdq@8043
   378
#else
gabomdq@8043
   379
    if (argv[1]) {
gabomdq@8043
   380
#endif
gabomdq@8043
   381
        SDL_bool reportederror = SDL_FALSE;
gabomdq@8043
   382
        SDL_bool keepGoing = SDL_TRUE;
gabomdq@8043
   383
        SDL_Event event;
philipp@8078
   384
        int device;
gabomdq@8762
   385
#ifdef __ANDROID__
philipp@8078
   386
        device = 0;
gabomdq@8043
   387
#else
philipp@8078
   388
        device = atoi(argv[1]);
gabomdq@8043
   389
#endif
philipp@8078
   390
        joystick = SDL_JoystickOpen(device);
philipp@8078
   391
gabomdq@8043
   392
        while ( keepGoing ) {
gabomdq@8043
   393
            if (joystick == NULL) {
gabomdq@8043
   394
                if ( !reportederror ) {
philipp@8078
   395
                    SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
gabomdq@8043
   396
                    keepGoing = SDL_FALSE;
gabomdq@8043
   397
                    reportederror = SDL_TRUE;
gabomdq@8043
   398
                }
gabomdq@8043
   399
            } else {
gabomdq@8043
   400
                reportederror = SDL_FALSE;
gabomdq@8043
   401
                keepGoing = WatchJoystick(joystick);
gabomdq@8043
   402
                SDL_JoystickClose(joystick);
gabomdq@8043
   403
            }
gabomdq@8043
   404
gabomdq@8043
   405
            joystick = NULL;
gabomdq@8043
   406
            if (keepGoing) {
gabomdq@8043
   407
                SDL_Log("Waiting for attach\n");
gabomdq@8043
   408
            }
gabomdq@8043
   409
            while (keepGoing) {
gabomdq@8043
   410
                SDL_WaitEvent(&event);
gabomdq@8043
   411
                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
gabomdq@8043
   412
                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
gabomdq@8043
   413
                    keepGoing = SDL_FALSE;
gabomdq@8043
   414
                } else if (event.type == SDL_JOYDEVICEADDED) {
philipp@8078
   415
                    joystick = SDL_JoystickOpen(device);
gabomdq@8043
   416
                    break;
gabomdq@8043
   417
                }
gabomdq@8043
   418
            }
gabomdq@8043
   419
        }
gabomdq@8043
   420
    }
gabomdq@8043
   421
    else {
gabomdq@8043
   422
        SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
gabomdq@8043
   423
    }
gabomdq@8043
   424
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
gabomdq@8043
   425
gabomdq@8043
   426
    return 0;
gabomdq@8043
   427
}
gabomdq@8043
   428
gabomdq@8043
   429
#else
gabomdq@8043
   430
gabomdq@8043
   431
int
gabomdq@8043
   432
main(int argc, char *argv[])
gabomdq@8043
   433
{
gabomdq@8043
   434
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
gabomdq@8043
   435
    exit(1);
gabomdq@8043
   436
}
gabomdq@8043
   437
gabomdq@8043
   438
#endif
slouken@10597
   439
slouken@10597
   440
/* vi: set ts=4 sw=4 expandtab: */