test/controllermap.c
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 02 Dec 2013 19:35:04 -0300
changeset 8043 74fcbf6987ba
child 8046 e7c89b29932a
permissions -rw-r--r--
Adds controllermap utility to test suite.
gabomdq@8043
     1
/*
gabomdq@8043
     2
  Copyright (C) 1997-2013 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 void
gabomdq@8043
   100
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
gabomdq@8043
   101
{
gabomdq@8043
   102
    const SDL_Rect area = { x, y, w, h };
gabomdq@8043
   103
    SDL_RenderFillRect(r, &area);
gabomdq@8043
   104
}
gabomdq@8043
   105
gabomdq@8043
   106
static SDL_bool
gabomdq@8043
   107
WatchJoystick(SDL_Joystick * joystick)
gabomdq@8043
   108
{
gabomdq@8043
   109
    SDL_Window *window = NULL;
gabomdq@8043
   110
    SDL_Renderer *screen = NULL;
gabomdq@8043
   111
    SDL_Texture *target, *background, *button, *axis, *marker;
gabomdq@8043
   112
    const char *name = NULL;
gabomdq@8043
   113
    SDL_bool retval = SDL_FALSE;
gabomdq@8043
   114
    SDL_bool done = SDL_FALSE, next=SDL_FALSE;
gabomdq@8043
   115
    SDL_Event event;
gabomdq@8043
   116
    SDL_Rect dst;
gabomdq@8043
   117
    int i, s, _s;
gabomdq@8043
   118
    Uint8 alpha=200, alpha_step = -1;
gabomdq@8043
   119
    Uint32 alpha_ticks;
gabomdq@8043
   120
    char mapping[4096], temp[4096];
gabomdq@8043
   121
    MappingStep *step;
gabomdq@8043
   122
    MappingStep steps[] = {
gabomdq@8043
   123
        {342, 132,  0.0,  MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
gabomdq@8043
   124
        {387, 167,  0.0,  MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
gabomdq@8043
   125
        {431, 132,  0.0,  MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
gabomdq@8043
   126
        {389, 101,  0.0,  MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
gabomdq@8043
   127
        {174, 132,  0.0,  MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
gabomdq@8043
   128
        {233, 132,  0.0,  MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
gabomdq@8043
   129
        {289, 132,  0.0,  MARKER_BUTTON, "start", -1, -1, -1, -1, ""},        
gabomdq@8043
   130
        {116, 217,  0.0,  MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
gabomdq@8043
   131
        {154, 249,  0.0,  MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
gabomdq@8043
   132
        {186, 217,  0.0,  MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
gabomdq@8043
   133
        {154, 188,  0.0,  MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
gabomdq@8043
   134
        {77,  40,   0.0,  MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
gabomdq@8043
   135
        {91, 0,    0.0,  MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
gabomdq@8043
   136
        {396, 36,   0.0,  MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
gabomdq@8043
   137
        {375, 0,    0.0,  MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
gabomdq@8043
   138
        {75,  154,  0.0,  MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
gabomdq@8043
   139
        {305, 230,  0.0,  MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
gabomdq@8043
   140
        {75,  154,  0.0,  MARKER_AXIS,   "leftx", -1, -1, -1, -1, ""},
gabomdq@8043
   141
        {75,  154,  90.0, MARKER_AXIS,   "lefty", -1, -1, -1, -1, ""},        
gabomdq@8043
   142
        {305, 230,  0.0,  MARKER_AXIS,   "rightx", -1, -1, -1, -1, ""},
gabomdq@8043
   143
        {305, 230,  90.0, MARKER_AXIS,   "righty", -1, -1, -1, -1, ""},
gabomdq@8043
   144
    };
gabomdq@8043
   145
gabomdq@8043
   146
    /* Create a window to display joystick axis position */
gabomdq@8043
   147
    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
gabomdq@8043
   148
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
gabomdq@8043
   149
                              SCREEN_HEIGHT, 0);
gabomdq@8043
   150
    if (window == NULL) {
gabomdq@8043
   151
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
gabomdq@8043
   152
        return SDL_FALSE;
gabomdq@8043
   153
    }
gabomdq@8043
   154
gabomdq@8043
   155
    screen = SDL_CreateRenderer(window, -1, 0);
gabomdq@8043
   156
    if (screen == NULL) {
gabomdq@8043
   157
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
gabomdq@8043
   158
        SDL_DestroyWindow(window);
gabomdq@8043
   159
        return SDL_FALSE;
gabomdq@8043
   160
    }
gabomdq@8043
   161
    
gabomdq@8043
   162
    target = SDL_CreateTexture(screen, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, MAP_WIDTH, MAP_HEIGHT);
gabomdq@8043
   163
    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
gabomdq@8043
   164
    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
gabomdq@8043
   165
    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
gabomdq@8043
   166
    SDL_RaiseWindow(window);
gabomdq@8043
   167
gabomdq@8043
   168
    /* Print info about the joystick we are watching */
gabomdq@8043
   169
    name = SDL_JoystickName(joystick);
gabomdq@8043
   170
    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
gabomdq@8043
   171
           name ? name : "Unknown Joystick");
gabomdq@8043
   172
    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
gabomdq@8043
   173
           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
gabomdq@8043
   174
           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
gabomdq@8043
   175
    
gabomdq@8043
   176
    SDL_Log("\n\n\
gabomdq@8043
   177
    ====================================================================================\n\
gabomdq@8043
   178
    Press the buttons on your controller when indicated\n\
gabomdq@8043
   179
    (Your controller may look different than the picture)\n\
gabomdq@8043
   180
    If you want to correct a mistake, press backspace or the back button on your device\n\
gabomdq@8043
   181
    To exit, press ESC\n\
gabomdq@8043
   182
    ====================================================================================\n");
gabomdq@8043
   183
    
gabomdq@8043
   184
    /* Initialize mapping with GUID and name */
gabomdq@8043
   185
    SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
gabomdq@8043
   186
    SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
gabomdq@8043
   187
        temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
gabomdq@8043
   188
gabomdq@8043
   189
    /* Loop, getting joystick events! */
gabomdq@8043
   190
    for(s=0; s<SDL_arraysize(steps) && !done;) {
gabomdq@8043
   191
        /* blank screen, set up for drawing this frame. */
gabomdq@8043
   192
        step = &steps[s];
gabomdq@8043
   193
        SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
gabomdq@8043
   194
        step->axis = -1;
gabomdq@8043
   195
        step->button = -1;
gabomdq@8043
   196
        step->hat = -1;
gabomdq@8043
   197
        step->hat_value = -1;
gabomdq@8043
   198
        SDL_SetClipboardText("TESTING TESTING 123");
gabomdq@8043
   199
        
gabomdq@8043
   200
        switch(step->marker) {
gabomdq@8043
   201
            case MARKER_AXIS:
gabomdq@8043
   202
                marker = axis;
gabomdq@8043
   203
                break;
gabomdq@8043
   204
            case MARKER_BUTTON:
gabomdq@8043
   205
                marker = button;
gabomdq@8043
   206
                break;
gabomdq@8043
   207
            default:
gabomdq@8043
   208
                break;
gabomdq@8043
   209
        }
gabomdq@8043
   210
        
gabomdq@8043
   211
        dst.x = step->x;
gabomdq@8043
   212
        dst.y = step->y;
gabomdq@8043
   213
        SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
gabomdq@8043
   214
        next=SDL_FALSE;
gabomdq@8043
   215
        
gabomdq@8043
   216
        while (!done && !next) {
gabomdq@8043
   217
            if (SDL_GetTicks() - alpha_ticks > 5) {
gabomdq@8043
   218
                alpha_ticks = SDL_GetTicks();
gabomdq@8043
   219
                alpha += alpha_step;
gabomdq@8043
   220
                if (alpha == 255) {
gabomdq@8043
   221
                    alpha_step = -1;
gabomdq@8043
   222
                }
gabomdq@8043
   223
                if (alpha < 128) {
gabomdq@8043
   224
                    alpha_step = 1;
gabomdq@8043
   225
                }
gabomdq@8043
   226
            }
gabomdq@8043
   227
            
gabomdq@8043
   228
            SDL_SetRenderTarget(screen, target);
gabomdq@8043
   229
            SDL_RenderCopy(screen, background, NULL, NULL);
gabomdq@8043
   230
            SDL_SetTextureAlphaMod(marker, alpha);
gabomdq@8043
   231
            SDL_SetTextureColorMod(marker, 10, 255, 21);
gabomdq@8043
   232
            SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0);
gabomdq@8043
   233
            SDL_SetRenderTarget(screen, NULL);
gabomdq@8043
   234
            SDL_RenderCopy(screen, target, NULL, NULL);
gabomdq@8043
   235
            SDL_RenderPresent(screen);
gabomdq@8043
   236
            
gabomdq@8043
   237
            if (SDL_PollEvent(&event)) {
gabomdq@8043
   238
                switch (event.type) {
gabomdq@8043
   239
                case SDL_JOYAXISMOTION:
gabomdq@8043
   240
                    if (event.jaxis.value > 20000 || event.jaxis.value < -20000) {
gabomdq@8043
   241
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   242
                            if (steps[_s].axis == event.jaxis.axis) {
gabomdq@8043
   243
                                break;
gabomdq@8043
   244
                            }
gabomdq@8043
   245
                        }
gabomdq@8043
   246
                        if (_s == s) {
gabomdq@8043
   247
                            step->axis = event.jaxis.axis;
gabomdq@8043
   248
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   249
                            SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
gabomdq@8043
   250
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   251
                            s++;
gabomdq@8043
   252
                            next=SDL_TRUE;
gabomdq@8043
   253
                        }
gabomdq@8043
   254
                    }
gabomdq@8043
   255
                    
gabomdq@8043
   256
                    break;
gabomdq@8043
   257
                case SDL_JOYHATMOTION:
gabomdq@8043
   258
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   259
                            if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
gabomdq@8043
   260
                                break;
gabomdq@8043
   261
                            }
gabomdq@8043
   262
                        }
gabomdq@8043
   263
                        if (_s == s) {
gabomdq@8043
   264
                            step->hat = event.jhat.hat;
gabomdq@8043
   265
                            step->hat_value = event.jhat.value;
gabomdq@8043
   266
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   267
                            SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
gabomdq@8043
   268
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   269
                            s++;
gabomdq@8043
   270
                            next=SDL_TRUE;
gabomdq@8043
   271
                        }
gabomdq@8043
   272
                    break;
gabomdq@8043
   273
                case SDL_JOYBALLMOTION:
gabomdq@8043
   274
                    break;
gabomdq@8043
   275
                case SDL_JOYBUTTONUP:
gabomdq@8043
   276
                    for (_s = 0; _s < s; _s++) {
gabomdq@8043
   277
                        if (steps[_s].button == event.jbutton.button) {
gabomdq@8043
   278
                            break;
gabomdq@8043
   279
                        }
gabomdq@8043
   280
                    }
gabomdq@8043
   281
                    if (_s == s) {
gabomdq@8043
   282
                        step->button = event.jbutton.button;
gabomdq@8043
   283
                        SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   284
                        SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
gabomdq@8043
   285
                        SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   286
                        s++;
gabomdq@8043
   287
                        next=SDL_TRUE;
gabomdq@8043
   288
                    }
gabomdq@8043
   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@8043
   300
                    if ((event.key.keysym.sym != SDLK_ESCAPE)) {
gabomdq@8043
   301
                        break;
gabomdq@8043
   302
                    }
gabomdq@8043
   303
                    /* Fall through to signal quit */
gabomdq@8043
   304
                case SDL_FINGERDOWN:
gabomdq@8043
   305
                case SDL_MOUSEBUTTONDOWN:
gabomdq@8043
   306
                case SDL_QUIT:
gabomdq@8043
   307
                    done = SDL_TRUE;
gabomdq@8043
   308
                    break;
gabomdq@8043
   309
                default:
gabomdq@8043
   310
                    break;
gabomdq@8043
   311
                }
gabomdq@8043
   312
            }
gabomdq@8043
   313
        }
gabomdq@8043
   314
gabomdq@8043
   315
    }
gabomdq@8043
   316
gabomdq@8043
   317
    if (s == SDL_arraysize(steps) ) {
gabomdq@8043
   318
        SDL_Log("Mapping:\n\n%s\n\n", mapping);
gabomdq@8043
   319
        /* Print to stdout as well so the user can cat the output somewhere */
gabomdq@8043
   320
        printf("%s\n", mapping);
gabomdq@8043
   321
    }
gabomdq@8043
   322
    
gabomdq@8043
   323
    while(SDL_PollEvent(&event)) {};
gabomdq@8043
   324
    
gabomdq@8043
   325
    SDL_DestroyRenderer(screen);
gabomdq@8043
   326
    SDL_DestroyWindow(window);
gabomdq@8043
   327
    return retval;
gabomdq@8043
   328
}
gabomdq@8043
   329
gabomdq@8043
   330
int
gabomdq@8043
   331
main(int argc, char *argv[])
gabomdq@8043
   332
{
gabomdq@8043
   333
    const char *name;
gabomdq@8043
   334
    int i;
gabomdq@8043
   335
    SDL_Joystick *joystick;
gabomdq@8043
   336
gabomdq@8043
   337
    /* Enable standard application logging */
gabomdq@8043
   338
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
gabomdq@8043
   339
gabomdq@8043
   340
    /* Initialize SDL (Note: video is required to start event loop) */
gabomdq@8043
   341
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
gabomdq@8043
   342
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
gabomdq@8043
   343
        exit(1);
gabomdq@8043
   344
    }
gabomdq@8043
   345
gabomdq@8043
   346
    /* Print information about the joysticks */
gabomdq@8043
   347
    SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
gabomdq@8043
   348
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
gabomdq@8043
   349
        name = SDL_JoystickNameForIndex(i);
gabomdq@8043
   350
        SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
gabomdq@8043
   351
        joystick = SDL_JoystickOpen(i);
gabomdq@8043
   352
        if (joystick == NULL) {
gabomdq@8043
   353
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
gabomdq@8043
   354
                    SDL_GetError());
gabomdq@8043
   355
        } else {
gabomdq@8043
   356
            char guid[64];
gabomdq@8043
   357
            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
gabomdq@8043
   358
                                      guid, sizeof (guid));
gabomdq@8043
   359
            SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
gabomdq@8043
   360
            SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
gabomdq@8043
   361
            SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
gabomdq@8043
   362
            SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
gabomdq@8043
   363
            SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
gabomdq@8043
   364
            SDL_Log("       guid: %s\n", guid);
gabomdq@8043
   365
            SDL_JoystickClose(joystick);
gabomdq@8043
   366
        }
gabomdq@8043
   367
    }
gabomdq@8043
   368
gabomdq@8043
   369
#ifdef ANDROID
gabomdq@8043
   370
    if (SDL_NumJoysticks() > 0) {
gabomdq@8043
   371
#else
gabomdq@8043
   372
    if (argv[1]) {
gabomdq@8043
   373
#endif
gabomdq@8043
   374
        SDL_bool reportederror = SDL_FALSE;
gabomdq@8043
   375
        SDL_bool keepGoing = SDL_TRUE;
gabomdq@8043
   376
        SDL_Event event;
gabomdq@8043
   377
#ifdef ANDROID
gabomdq@8043
   378
        joystick = SDL_JoystickOpen(0);
gabomdq@8043
   379
#else
gabomdq@8043
   380
        joystick = SDL_JoystickOpen(atoi(argv[1]));
gabomdq@8043
   381
#endif
gabomdq@8043
   382
        while ( keepGoing ) {
gabomdq@8043
   383
            if (joystick == NULL) {
gabomdq@8043
   384
                if ( !reportederror ) {
gabomdq@8043
   385
                    SDL_Log("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
gabomdq@8043
   386
                    keepGoing = SDL_FALSE;
gabomdq@8043
   387
                    reportederror = SDL_TRUE;
gabomdq@8043
   388
                }
gabomdq@8043
   389
            } else {
gabomdq@8043
   390
                reportederror = SDL_FALSE;
gabomdq@8043
   391
                keepGoing = WatchJoystick(joystick);
gabomdq@8043
   392
                SDL_JoystickClose(joystick);
gabomdq@8043
   393
            }
gabomdq@8043
   394
gabomdq@8043
   395
            joystick = NULL;
gabomdq@8043
   396
            if (keepGoing) {
gabomdq@8043
   397
                SDL_Log("Waiting for attach\n");
gabomdq@8043
   398
            }
gabomdq@8043
   399
            while (keepGoing) {
gabomdq@8043
   400
                SDL_WaitEvent(&event);
gabomdq@8043
   401
                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
gabomdq@8043
   402
                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
gabomdq@8043
   403
                    keepGoing = SDL_FALSE;
gabomdq@8043
   404
                } else if (event.type == SDL_JOYDEVICEADDED) {
gabomdq@8043
   405
                    joystick = SDL_JoystickOpen(atoi(argv[1]));
gabomdq@8043
   406
                    break;
gabomdq@8043
   407
                }
gabomdq@8043
   408
            }
gabomdq@8043
   409
        }
gabomdq@8043
   410
    }
gabomdq@8043
   411
    else {
gabomdq@8043
   412
        SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
gabomdq@8043
   413
    }
gabomdq@8043
   414
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
gabomdq@8043
   415
gabomdq@8043
   416
#ifdef ANDROID
gabomdq@8043
   417
    exit(0);
gabomdq@8043
   418
#else
gabomdq@8043
   419
    return 0;
gabomdq@8043
   420
#endif
gabomdq@8043
   421
}
gabomdq@8043
   422
gabomdq@8043
   423
#else
gabomdq@8043
   424
gabomdq@8043
   425
int
gabomdq@8043
   426
main(int argc, char *argv[])
gabomdq@8043
   427
{
gabomdq@8043
   428
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
gabomdq@8043
   429
    exit(1);
gabomdq@8043
   430
}
gabomdq@8043
   431
gabomdq@8043
   432
#endif