test/controllermap.c
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 10 Feb 2014 09:26:22 -0300
changeset 8201 4a16c26b6055
parent 8200 17b801b31cd5
child 8202 b84a1f55075e
permissions -rw-r--r--
Backed out 51935d107921

The render target usage in controllermap is required if you are forced to use
the app at a different resolution than the one the art has been made for, for
example on Android, where you don't control the resolution.
(The coordinates for each button are hardcoded to the art size, and appear out
of place otherwise)
gabomdq@8043
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
gabomdq@8043
    29
#define SCREEN_HEIGHT   317
gabomdq@8043
    30
#endif
gabomdq@8043
    31
gabomdq@8043
    32
#define MAP_WIDTH 512
gabomdq@8043
    33
#define MAP_HEIGHT 317
gabomdq@8043
    34
gabomdq@8043
    35
#define MARKER_BUTTON 1
gabomdq@8043
    36
#define MARKER_AXIS 2
gabomdq@8043
    37
gabomdq@8043
    38
typedef struct MappingStep
gabomdq@8043
    39
{
gabomdq@8043
    40
    int x, y;
gabomdq@8043
    41
    double angle;
gabomdq@8043
    42
    int marker;
gabomdq@8043
    43
    char *field;
gabomdq@8043
    44
    int axis, button, hat, hat_value;
gabomdq@8043
    45
    char mapping[4096];
gabomdq@8043
    46
}MappingStep;
gabomdq@8043
    47
gabomdq@8043
    48
gabomdq@8043
    49
SDL_Texture *
gabomdq@8043
    50
LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
gabomdq@8043
    51
{
gabomdq@8043
    52
    SDL_Surface *temp;
gabomdq@8043
    53
    SDL_Texture *texture;
gabomdq@8043
    54
gabomdq@8043
    55
    /* Load the sprite image */
gabomdq@8043
    56
    temp = SDL_LoadBMP(file);
gabomdq@8043
    57
    if (temp == NULL) {
gabomdq@8043
    58
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
gabomdq@8043
    59
        return NULL;
gabomdq@8043
    60
    }
gabomdq@8043
    61
gabomdq@8043
    62
    /* Set transparent pixel as the pixel at (0,0) */
gabomdq@8043
    63
    if (transparent) {
gabomdq@8043
    64
        if (temp->format->palette) {
gabomdq@8043
    65
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
gabomdq@8043
    66
        } else {
gabomdq@8043
    67
            switch (temp->format->BitsPerPixel) {
gabomdq@8043
    68
            case 15:
gabomdq@8043
    69
                SDL_SetColorKey(temp, SDL_TRUE,
gabomdq@8043
    70
                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
gabomdq@8043
    71
                break;
gabomdq@8043
    72
            case 16:
gabomdq@8043
    73
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
gabomdq@8043
    74
                break;
gabomdq@8043
    75
            case 24:
gabomdq@8043
    76
                SDL_SetColorKey(temp, SDL_TRUE,
gabomdq@8043
    77
                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
gabomdq@8043
    78
                break;
gabomdq@8043
    79
            case 32:
gabomdq@8043
    80
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
gabomdq@8043
    81
                break;
gabomdq@8043
    82
            }
gabomdq@8043
    83
        }
gabomdq@8043
    84
    }
gabomdq@8043
    85
gabomdq@8043
    86
    /* Create textures from the image */
gabomdq@8043
    87
    texture = SDL_CreateTextureFromSurface(renderer, temp);
gabomdq@8043
    88
    if (!texture) {
gabomdq@8043
    89
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
gabomdq@8043
    90
        SDL_FreeSurface(temp);
gabomdq@8043
    91
        return NULL;
gabomdq@8043
    92
    }
gabomdq@8043
    93
    SDL_FreeSurface(temp);
gabomdq@8043
    94
gabomdq@8043
    95
    /* We're ready to roll. :) */
gabomdq@8043
    96
    return texture;
gabomdq@8043
    97
}
gabomdq@8043
    98
gabomdq@8043
    99
static SDL_bool
gabomdq@8043
   100
WatchJoystick(SDL_Joystick * joystick)
gabomdq@8043
   101
{
gabomdq@8043
   102
    SDL_Window *window = NULL;
gabomdq@8043
   103
    SDL_Renderer *screen = NULL;
gabomdq@8201
   104
    SDL_Texture *target, *background, *button, *axis, *marker;
gabomdq@8043
   105
    const char *name = NULL;
gabomdq@8043
   106
    SDL_bool retval = SDL_FALSE;
gabomdq@8043
   107
    SDL_bool done = SDL_FALSE, next=SDL_FALSE;
gabomdq@8043
   108
    SDL_Event event;
gabomdq@8043
   109
    SDL_Rect dst;
philipp@8077
   110
    int s, _s;
gabomdq@8043
   111
    Uint8 alpha=200, alpha_step = -1;
gabomdq@8043
   112
    Uint32 alpha_ticks;
gabomdq@8043
   113
    char mapping[4096], temp[4096];
gabomdq@8043
   114
    MappingStep *step;
gabomdq@8043
   115
    MappingStep steps[] = {
gabomdq@8043
   116
        {342, 132,  0.0,  MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
gabomdq@8043
   117
        {387, 167,  0.0,  MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
gabomdq@8043
   118
        {431, 132,  0.0,  MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
gabomdq@8043
   119
        {389, 101,  0.0,  MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
gabomdq@8043
   120
        {174, 132,  0.0,  MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
gabomdq@8043
   121
        {233, 132,  0.0,  MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
gabomdq@8043
   122
        {289, 132,  0.0,  MARKER_BUTTON, "start", -1, -1, -1, -1, ""},        
gabomdq@8043
   123
        {116, 217,  0.0,  MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
gabomdq@8043
   124
        {154, 249,  0.0,  MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
gabomdq@8043
   125
        {186, 217,  0.0,  MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
gabomdq@8043
   126
        {154, 188,  0.0,  MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
gabomdq@8043
   127
        {77,  40,   0.0,  MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
gabomdq@8043
   128
        {91, 0,    0.0,  MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
gabomdq@8043
   129
        {396, 36,   0.0,  MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
gabomdq@8043
   130
        {375, 0,    0.0,  MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
gabomdq@8043
   131
        {75,  154,  0.0,  MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
gabomdq@8043
   132
        {305, 230,  0.0,  MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
gabomdq@8043
   133
        {75,  154,  0.0,  MARKER_AXIS,   "leftx", -1, -1, -1, -1, ""},
gabomdq@8043
   134
        {75,  154,  90.0, MARKER_AXIS,   "lefty", -1, -1, -1, -1, ""},        
gabomdq@8043
   135
        {305, 230,  0.0,  MARKER_AXIS,   "rightx", -1, -1, -1, -1, ""},
gabomdq@8043
   136
        {305, 230,  90.0, MARKER_AXIS,   "righty", -1, -1, -1, -1, ""},
gabomdq@8043
   137
    };
gabomdq@8043
   138
gabomdq@8043
   139
    /* Create a window to display joystick axis position */
gabomdq@8046
   140
    window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
gabomdq@8043
   141
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
gabomdq@8043
   142
                              SCREEN_HEIGHT, 0);
gabomdq@8043
   143
    if (window == NULL) {
gabomdq@8043
   144
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
gabomdq@8043
   145
        return SDL_FALSE;
gabomdq@8043
   146
    }
gabomdq@8043
   147
gabomdq@8043
   148
    screen = SDL_CreateRenderer(window, -1, 0);
gabomdq@8043
   149
    if (screen == NULL) {
gabomdq@8043
   150
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
gabomdq@8043
   151
        SDL_DestroyWindow(window);
gabomdq@8043
   152
        return SDL_FALSE;
gabomdq@8043
   153
    }
gabomdq@8043
   154
    
gabomdq@8201
   155
    target = SDL_CreateTexture(screen, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, MAP_WIDTH, MAP_HEIGHT);
gabomdq@8043
   156
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
gabomdq@8043
   157
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
gabomdq@8043
   158
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
gabomdq@8043
   159
    SDL_RaiseWindow(window);
gabomdq@8043
   160
gabomdq@8043
   161
    /* Print info about the joystick we are watching */
gabomdq@8043
   162
    name = SDL_JoystickName(joystick);
gabomdq@8043
   163
    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
gabomdq@8043
   164
           name ? name : "Unknown Joystick");
gabomdq@8043
   165
    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
gabomdq@8043
   166
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
gabomdq@8043
   167
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
gabomdq@8043
   168
    
gabomdq@8043
   169
    SDL_Log("\n\n\
gabomdq@8043
   170
    ====================================================================================\n\
gabomdq@8043
   171
    Press the buttons on your controller when indicated\n\
gabomdq@8043
   172
    (Your controller may look different than the picture)\n\
gabomdq@8043
   173
    If you want to correct a mistake, press backspace or the back button on your device\n\
gabomdq@8046
   174
    To skip a button, press SPACE or click/touch the screen\n\
gabomdq@8043
   175
    To exit, press ESC\n\
gabomdq@8043
   176
    ====================================================================================\n");
gabomdq@8043
   177
    
gabomdq@8043
   178
    /* Initialize mapping with GUID and name */
gabomdq@8043
   179
    SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
gabomdq@8043
   180
    SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
gabomdq@8043
   181
        temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
gabomdq@8043
   182
gabomdq@8043
   183
    /* Loop, getting joystick events! */
gabomdq@8043
   184
    for(s=0; s<SDL_arraysize(steps) && !done;) {
gabomdq@8043
   185
        /* blank screen, set up for drawing this frame. */
gabomdq@8043
   186
        step = &steps[s];
gabomdq@8043
   187
        SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
gabomdq@8043
   188
        step->axis = -1;
gabomdq@8043
   189
        step->button = -1;
gabomdq@8043
   190
        step->hat = -1;
gabomdq@8043
   191
        step->hat_value = -1;
gabomdq@8043
   192
        SDL_SetClipboardText("TESTING TESTING 123");
gabomdq@8043
   193
        
gabomdq@8043
   194
        switch(step->marker) {
gabomdq@8043
   195
            case MARKER_AXIS:
gabomdq@8043
   196
                marker = axis;
gabomdq@8043
   197
                break;
gabomdq@8043
   198
            case MARKER_BUTTON:
gabomdq@8043
   199
                marker = button;
gabomdq@8043
   200
                break;
gabomdq@8043
   201
            default:
gabomdq@8043
   202
                break;
gabomdq@8043
   203
        }
gabomdq@8043
   204
        
gabomdq@8043
   205
        dst.x = step->x;
gabomdq@8043
   206
        dst.y = step->y;
gabomdq@8043
   207
        SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
gabomdq@8043
   208
        next=SDL_FALSE;
gabomdq@8043
   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
            
gabomdq@8201
   222
            SDL_SetRenderTarget(screen, target);
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);
gabomdq@8043
   226
            SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0);
gabomdq@8201
   227
            SDL_SetRenderTarget(screen, NULL);
gabomdq@8201
   228
            SDL_RenderCopy(screen, target, NULL, NULL);
gabomdq@8043
   229
            SDL_RenderPresent(screen);
gabomdq@8043
   230
            
gabomdq@8043
   231
            if (SDL_PollEvent(&event)) {
gabomdq@8043
   232
                switch (event.type) {
gabomdq@8043
   233
                case SDL_JOYAXISMOTION:
gabomdq@8043
   234
                    if (event.jaxis.value > 20000 || event.jaxis.value < -20000) {
gabomdq@8043
   235
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   236
                            if (steps[_s].axis == event.jaxis.axis) {
gabomdq@8043
   237
                                break;
gabomdq@8043
   238
                            }
gabomdq@8043
   239
                        }
gabomdq@8043
   240
                        if (_s == s) {
gabomdq@8043
   241
                            step->axis = event.jaxis.axis;
gabomdq@8043
   242
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   243
                            SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
gabomdq@8043
   244
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   245
                            s++;
gabomdq@8043
   246
                            next=SDL_TRUE;
gabomdq@8043
   247
                        }
gabomdq@8043
   248
                    }
gabomdq@8043
   249
                    
gabomdq@8043
   250
                    break;
gabomdq@8043
   251
                case SDL_JOYHATMOTION:
gabomdq@8043
   252
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   253
                            if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
gabomdq@8043
   254
                                break;
gabomdq@8043
   255
                            }
gabomdq@8043
   256
                        }
gabomdq@8043
   257
                        if (_s == s) {
gabomdq@8043
   258
                            step->hat = event.jhat.hat;
gabomdq@8043
   259
                            step->hat_value = event.jhat.value;
gabomdq@8043
   260
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   261
                            SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
gabomdq@8043
   262
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   263
                            s++;
gabomdq@8043
   264
                            next=SDL_TRUE;
gabomdq@8043
   265
                        }
gabomdq@8043
   266
                    break;
gabomdq@8043
   267
                case SDL_JOYBALLMOTION:
gabomdq@8043
   268
                    break;
gabomdq@8043
   269
                case SDL_JOYBUTTONUP:
gabomdq@8043
   270
                    for (_s = 0; _s < s; _s++) {
gabomdq@8043
   271
                        if (steps[_s].button == event.jbutton.button) {
gabomdq@8043
   272
                            break;
gabomdq@8043
   273
                        }
gabomdq@8043
   274
                    }
gabomdq@8043
   275
                    if (_s == s) {
gabomdq@8043
   276
                        step->button = event.jbutton.button;
gabomdq@8043
   277
                        SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   278
                        SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
gabomdq@8043
   279
                        SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   280
                        s++;
gabomdq@8043
   281
                        next=SDL_TRUE;
gabomdq@8043
   282
                    }
gabomdq@8043
   283
                    break;
gabomdq@8046
   284
                case SDL_FINGERDOWN:
gabomdq@8046
   285
                case SDL_MOUSEBUTTONDOWN:
gabomdq@8046
   286
                    /* Skip this step */
gabomdq@8046
   287
                    s++;
gabomdq@8046
   288
                    next=SDL_TRUE;
gabomdq@8046
   289
                    break;
gabomdq@8043
   290
                case SDL_KEYDOWN:
gabomdq@8043
   291
                    if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
gabomdq@8043
   292
                        /* Undo! */
gabomdq@8043
   293
                        if (s > 0) {
gabomdq@8043
   294
                            SDL_strlcpy(mapping, step->mapping, SDL_arraysize(step->mapping));
gabomdq@8043
   295
                            s--;
gabomdq@8043
   296
                            next = SDL_TRUE;
gabomdq@8043
   297
                        }
gabomdq@8043
   298
                        break;
gabomdq@8043
   299
                    }
gabomdq@8046
   300
                    if (event.key.keysym.sym == SDLK_SPACE) {
gabomdq@8046
   301
                        /* Skip this step */
gabomdq@8046
   302
                        s++;
gabomdq@8046
   303
                        next=SDL_TRUE;
gabomdq@8046
   304
                        break;
gabomdq@8046
   305
                    }
gabomdq@8046
   306
                    
gabomdq@8043
   307
                    if ((event.key.keysym.sym != SDLK_ESCAPE)) {
gabomdq@8043
   308
                        break;
gabomdq@8043
   309
                    }
gabomdq@8043
   310
                    /* Fall through to signal quit */
gabomdq@8043
   311
                case SDL_QUIT:
gabomdq@8043
   312
                    done = SDL_TRUE;
gabomdq@8043
   313
                    break;
gabomdq@8043
   314
                default:
gabomdq@8043
   315
                    break;
gabomdq@8043
   316
                }
gabomdq@8043
   317
            }
gabomdq@8043
   318
        }
gabomdq@8043
   319
gabomdq@8043
   320
    }
gabomdq@8043
   321
gabomdq@8043
   322
    if (s == SDL_arraysize(steps) ) {
gabomdq@8043
   323
        SDL_Log("Mapping:\n\n%s\n\n", mapping);
gabomdq@8043
   324
        /* Print to stdout as well so the user can cat the output somewhere */
gabomdq@8043
   325
        printf("%s\n", mapping);
gabomdq@8043
   326
    }
gabomdq@8043
   327
    
gabomdq@8043
   328
    while(SDL_PollEvent(&event)) {};
gabomdq@8043
   329
    
gabomdq@8043
   330
    SDL_DestroyRenderer(screen);
gabomdq@8043
   331
    SDL_DestroyWindow(window);
gabomdq@8043
   332
    return retval;
gabomdq@8043
   333
}
gabomdq@8043
   334
gabomdq@8043
   335
int
gabomdq@8043
   336
main(int argc, char *argv[])
gabomdq@8043
   337
{
gabomdq@8043
   338
    const char *name;
gabomdq@8043
   339
    int i;
gabomdq@8043
   340
    SDL_Joystick *joystick;
gabomdq@8043
   341
gabomdq@8043
   342
    /* Enable standard application logging */
gabomdq@8043
   343
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
gabomdq@8043
   344
gabomdq@8043
   345
    /* Initialize SDL (Note: video is required to start event loop) */
gabomdq@8043
   346
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
gabomdq@8043
   347
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
gabomdq@8043
   348
        exit(1);
gabomdq@8043
   349
    }
gabomdq@8043
   350
gabomdq@8043
   351
    /* Print information about the joysticks */
gabomdq@8043
   352
    SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
gabomdq@8043
   353
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
gabomdq@8043
   354
        name = SDL_JoystickNameForIndex(i);
gabomdq@8043
   355
        SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
gabomdq@8043
   356
        joystick = SDL_JoystickOpen(i);
gabomdq@8043
   357
        if (joystick == NULL) {
gabomdq@8043
   358
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
gabomdq@8043
   359
                    SDL_GetError());
gabomdq@8043
   360
        } else {
gabomdq@8043
   361
            char guid[64];
gabomdq@8043
   362
            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
gabomdq@8043
   363
                                      guid, sizeof (guid));
gabomdq@8043
   364
            SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
gabomdq@8043
   365
            SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
gabomdq@8043
   366
            SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
gabomdq@8043
   367
            SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
gabomdq@8043
   368
            SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
gabomdq@8043
   369
            SDL_Log("       guid: %s\n", guid);
gabomdq@8043
   370
            SDL_JoystickClose(joystick);
gabomdq@8043
   371
        }
gabomdq@8043
   372
    }
gabomdq@8043
   373
gabomdq@8043
   374
#ifdef ANDROID
gabomdq@8043
   375
    if (SDL_NumJoysticks() > 0) {
gabomdq@8043
   376
#else
gabomdq@8043
   377
    if (argv[1]) {
gabomdq@8043
   378
#endif
gabomdq@8043
   379
        SDL_bool reportederror = SDL_FALSE;
gabomdq@8043
   380
        SDL_bool keepGoing = SDL_TRUE;
gabomdq@8043
   381
        SDL_Event event;
philipp@8078
   382
        int device;
gabomdq@8043
   383
#ifdef ANDROID
philipp@8078
   384
        device = 0;
gabomdq@8043
   385
#else
philipp@8078
   386
        device = atoi(argv[1]);
gabomdq@8043
   387
#endif
philipp@8078
   388
        joystick = SDL_JoystickOpen(device);
philipp@8078
   389
gabomdq@8043
   390
        while ( keepGoing ) {
gabomdq@8043
   391
            if (joystick == NULL) {
gabomdq@8043
   392
                if ( !reportederror ) {
philipp@8078
   393
                    SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
gabomdq@8043
   394
                    keepGoing = SDL_FALSE;
gabomdq@8043
   395
                    reportederror = SDL_TRUE;
gabomdq@8043
   396
                }
gabomdq@8043
   397
            } else {
gabomdq@8043
   398
                reportederror = SDL_FALSE;
gabomdq@8043
   399
                keepGoing = WatchJoystick(joystick);
gabomdq@8043
   400
                SDL_JoystickClose(joystick);
gabomdq@8043
   401
            }
gabomdq@8043
   402
gabomdq@8043
   403
            joystick = NULL;
gabomdq@8043
   404
            if (keepGoing) {
gabomdq@8043
   405
                SDL_Log("Waiting for attach\n");
gabomdq@8043
   406
            }
gabomdq@8043
   407
            while (keepGoing) {
gabomdq@8043
   408
                SDL_WaitEvent(&event);
gabomdq@8043
   409
                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
gabomdq@8043
   410
                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
gabomdq@8043
   411
                    keepGoing = SDL_FALSE;
gabomdq@8043
   412
                } else if (event.type == SDL_JOYDEVICEADDED) {
philipp@8078
   413
                    joystick = SDL_JoystickOpen(device);
gabomdq@8043
   414
                    break;
gabomdq@8043
   415
                }
gabomdq@8043
   416
            }
gabomdq@8043
   417
        }
gabomdq@8043
   418
    }
gabomdq@8043
   419
    else {
gabomdq@8043
   420
        SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
gabomdq@8043
   421
    }
gabomdq@8043
   422
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
gabomdq@8043
   423
gabomdq@8043
   424
    return 0;
gabomdq@8043
   425
}
gabomdq@8043
   426
gabomdq@8043
   427
#else
gabomdq@8043
   428
gabomdq@8043
   429
int
gabomdq@8043
   430
main(int argc, char *argv[])
gabomdq@8043
   431
{
gabomdq@8043
   432
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
gabomdq@8043
   433
    exit(1);
gabomdq@8043
   434
}
gabomdq@8043
   435
gabomdq@8043
   436
#endif