test/controllermap.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 25 Dec 2013 00:04:31 +0100
changeset 8077 05bbcc1056e6
parent 8048 a9c5ddad50b0
child 8078 ef59ad725ac5
permissions -rw-r--r--
Fixed unused local variable warning in test program source.
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;
philipp@8077
   117
    int 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@8046
   147
    window = SDL_CreateWindow("Game Controller Map", 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@8046
   181
    To skip a button, press SPACE or click/touch the screen\n\
gabomdq@8043
   182
    To exit, press ESC\n\
gabomdq@8043
   183
    ====================================================================================\n");
gabomdq@8043
   184
    
gabomdq@8043
   185
    /* Initialize mapping with GUID and name */
gabomdq@8043
   186
    SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
gabomdq@8043
   187
    SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
gabomdq@8043
   188
        temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
gabomdq@8043
   189
gabomdq@8043
   190
    /* Loop, getting joystick events! */
gabomdq@8043
   191
    for(s=0; s<SDL_arraysize(steps) && !done;) {
gabomdq@8043
   192
        /* blank screen, set up for drawing this frame. */
gabomdq@8043
   193
        step = &steps[s];
gabomdq@8043
   194
        SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
gabomdq@8043
   195
        step->axis = -1;
gabomdq@8043
   196
        step->button = -1;
gabomdq@8043
   197
        step->hat = -1;
gabomdq@8043
   198
        step->hat_value = -1;
gabomdq@8043
   199
        SDL_SetClipboardText("TESTING TESTING 123");
gabomdq@8043
   200
        
gabomdq@8043
   201
        switch(step->marker) {
gabomdq@8043
   202
            case MARKER_AXIS:
gabomdq@8043
   203
                marker = axis;
gabomdq@8043
   204
                break;
gabomdq@8043
   205
            case MARKER_BUTTON:
gabomdq@8043
   206
                marker = button;
gabomdq@8043
   207
                break;
gabomdq@8043
   208
            default:
gabomdq@8043
   209
                break;
gabomdq@8043
   210
        }
gabomdq@8043
   211
        
gabomdq@8043
   212
        dst.x = step->x;
gabomdq@8043
   213
        dst.y = step->y;
gabomdq@8043
   214
        SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
gabomdq@8043
   215
        next=SDL_FALSE;
gabomdq@8043
   216
        
gabomdq@8043
   217
        while (!done && !next) {
gabomdq@8043
   218
            if (SDL_GetTicks() - alpha_ticks > 5) {
gabomdq@8043
   219
                alpha_ticks = SDL_GetTicks();
gabomdq@8043
   220
                alpha += alpha_step;
gabomdq@8043
   221
                if (alpha == 255) {
gabomdq@8043
   222
                    alpha_step = -1;
gabomdq@8043
   223
                }
gabomdq@8043
   224
                if (alpha < 128) {
gabomdq@8043
   225
                    alpha_step = 1;
gabomdq@8043
   226
                }
gabomdq@8043
   227
            }
gabomdq@8043
   228
            
gabomdq@8043
   229
            SDL_SetRenderTarget(screen, target);
gabomdq@8043
   230
            SDL_RenderCopy(screen, background, NULL, NULL);
gabomdq@8043
   231
            SDL_SetTextureAlphaMod(marker, alpha);
gabomdq@8043
   232
            SDL_SetTextureColorMod(marker, 10, 255, 21);
gabomdq@8043
   233
            SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0);
gabomdq@8043
   234
            SDL_SetRenderTarget(screen, NULL);
gabomdq@8043
   235
            SDL_RenderCopy(screen, target, NULL, NULL);
gabomdq@8043
   236
            SDL_RenderPresent(screen);
gabomdq@8043
   237
            
gabomdq@8043
   238
            if (SDL_PollEvent(&event)) {
gabomdq@8043
   239
                switch (event.type) {
gabomdq@8043
   240
                case SDL_JOYAXISMOTION:
gabomdq@8043
   241
                    if (event.jaxis.value > 20000 || event.jaxis.value < -20000) {
gabomdq@8043
   242
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   243
                            if (steps[_s].axis == event.jaxis.axis) {
gabomdq@8043
   244
                                break;
gabomdq@8043
   245
                            }
gabomdq@8043
   246
                        }
gabomdq@8043
   247
                        if (_s == s) {
gabomdq@8043
   248
                            step->axis = event.jaxis.axis;
gabomdq@8043
   249
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   250
                            SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
gabomdq@8043
   251
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   252
                            s++;
gabomdq@8043
   253
                            next=SDL_TRUE;
gabomdq@8043
   254
                        }
gabomdq@8043
   255
                    }
gabomdq@8043
   256
                    
gabomdq@8043
   257
                    break;
gabomdq@8043
   258
                case SDL_JOYHATMOTION:
gabomdq@8043
   259
                        for (_s = 0; _s < s; _s++) {
gabomdq@8043
   260
                            if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
gabomdq@8043
   261
                                break;
gabomdq@8043
   262
                            }
gabomdq@8043
   263
                        }
gabomdq@8043
   264
                        if (_s == s) {
gabomdq@8043
   265
                            step->hat = event.jhat.hat;
gabomdq@8043
   266
                            step->hat_value = event.jhat.value;
gabomdq@8043
   267
                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   268
                            SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
gabomdq@8043
   269
                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   270
                            s++;
gabomdq@8043
   271
                            next=SDL_TRUE;
gabomdq@8043
   272
                        }
gabomdq@8043
   273
                    break;
gabomdq@8043
   274
                case SDL_JOYBALLMOTION:
gabomdq@8043
   275
                    break;
gabomdq@8043
   276
                case SDL_JOYBUTTONUP:
gabomdq@8043
   277
                    for (_s = 0; _s < s; _s++) {
gabomdq@8043
   278
                        if (steps[_s].button == event.jbutton.button) {
gabomdq@8043
   279
                            break;
gabomdq@8043
   280
                        }
gabomdq@8043
   281
                    }
gabomdq@8043
   282
                    if (_s == s) {
gabomdq@8043
   283
                        step->button = event.jbutton.button;
gabomdq@8043
   284
                        SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
gabomdq@8043
   285
                        SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
gabomdq@8043
   286
                        SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
gabomdq@8043
   287
                        s++;
gabomdq@8043
   288
                        next=SDL_TRUE;
gabomdq@8043
   289
                    }
gabomdq@8043
   290
                    break;
gabomdq@8046
   291
                case SDL_FINGERDOWN:
gabomdq@8046
   292
                case SDL_MOUSEBUTTONDOWN:
gabomdq@8046
   293
                    /* Skip this step */
gabomdq@8046
   294
                    s++;
gabomdq@8046
   295
                    next=SDL_TRUE;
gabomdq@8046
   296
                    break;
gabomdq@8043
   297
                case SDL_KEYDOWN:
gabomdq@8043
   298
                    if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
gabomdq@8043
   299
                        /* Undo! */
gabomdq@8043
   300
                        if (s > 0) {
gabomdq@8043
   301
                            SDL_strlcpy(mapping, step->mapping, SDL_arraysize(step->mapping));
gabomdq@8043
   302
                            s--;
gabomdq@8043
   303
                            next = SDL_TRUE;
gabomdq@8043
   304
                        }
gabomdq@8043
   305
                        break;
gabomdq@8043
   306
                    }
gabomdq@8046
   307
                    if (event.key.keysym.sym == SDLK_SPACE) {
gabomdq@8046
   308
                        /* Skip this step */
gabomdq@8046
   309
                        s++;
gabomdq@8046
   310
                        next=SDL_TRUE;
gabomdq@8046
   311
                        break;
gabomdq@8046
   312
                    }
gabomdq@8046
   313
                    
gabomdq@8043
   314
                    if ((event.key.keysym.sym != SDLK_ESCAPE)) {
gabomdq@8043
   315
                        break;
gabomdq@8043
   316
                    }
gabomdq@8043
   317
                    /* Fall through to signal quit */
gabomdq@8043
   318
                case SDL_QUIT:
gabomdq@8043
   319
                    done = SDL_TRUE;
gabomdq@8043
   320
                    break;
gabomdq@8043
   321
                default:
gabomdq@8043
   322
                    break;
gabomdq@8043
   323
                }
gabomdq@8043
   324
            }
gabomdq@8043
   325
        }
gabomdq@8043
   326
gabomdq@8043
   327
    }
gabomdq@8043
   328
gabomdq@8043
   329
    if (s == SDL_arraysize(steps) ) {
gabomdq@8043
   330
        SDL_Log("Mapping:\n\n%s\n\n", mapping);
gabomdq@8043
   331
        /* Print to stdout as well so the user can cat the output somewhere */
gabomdq@8043
   332
        printf("%s\n", mapping);
gabomdq@8043
   333
    }
gabomdq@8043
   334
    
gabomdq@8043
   335
    while(SDL_PollEvent(&event)) {};
gabomdq@8043
   336
    
gabomdq@8043
   337
    SDL_DestroyRenderer(screen);
gabomdq@8043
   338
    SDL_DestroyWindow(window);
gabomdq@8043
   339
    return retval;
gabomdq@8043
   340
}
gabomdq@8043
   341
gabomdq@8043
   342
int
gabomdq@8043
   343
main(int argc, char *argv[])
gabomdq@8043
   344
{
gabomdq@8043
   345
    const char *name;
gabomdq@8043
   346
    int i;
gabomdq@8043
   347
    SDL_Joystick *joystick;
gabomdq@8043
   348
gabomdq@8043
   349
    /* Enable standard application logging */
gabomdq@8043
   350
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
gabomdq@8043
   351
gabomdq@8043
   352
    /* Initialize SDL (Note: video is required to start event loop) */
gabomdq@8043
   353
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
gabomdq@8043
   354
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
gabomdq@8043
   355
        exit(1);
gabomdq@8043
   356
    }
gabomdq@8043
   357
gabomdq@8043
   358
    /* Print information about the joysticks */
gabomdq@8043
   359
    SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
gabomdq@8043
   360
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
gabomdq@8043
   361
        name = SDL_JoystickNameForIndex(i);
gabomdq@8043
   362
        SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
gabomdq@8043
   363
        joystick = SDL_JoystickOpen(i);
gabomdq@8043
   364
        if (joystick == NULL) {
gabomdq@8043
   365
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
gabomdq@8043
   366
                    SDL_GetError());
gabomdq@8043
   367
        } else {
gabomdq@8043
   368
            char guid[64];
gabomdq@8043
   369
            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
gabomdq@8043
   370
                                      guid, sizeof (guid));
gabomdq@8043
   371
            SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
gabomdq@8043
   372
            SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
gabomdq@8043
   373
            SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
gabomdq@8043
   374
            SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
gabomdq@8043
   375
            SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
gabomdq@8043
   376
            SDL_Log("       guid: %s\n", guid);
gabomdq@8043
   377
            SDL_JoystickClose(joystick);
gabomdq@8043
   378
        }
gabomdq@8043
   379
    }
gabomdq@8043
   380
gabomdq@8043
   381
#ifdef ANDROID
gabomdq@8043
   382
    if (SDL_NumJoysticks() > 0) {
gabomdq@8043
   383
#else
gabomdq@8043
   384
    if (argv[1]) {
gabomdq@8043
   385
#endif
gabomdq@8043
   386
        SDL_bool reportederror = SDL_FALSE;
gabomdq@8043
   387
        SDL_bool keepGoing = SDL_TRUE;
gabomdq@8043
   388
        SDL_Event event;
gabomdq@8043
   389
#ifdef ANDROID
gabomdq@8043
   390
        joystick = SDL_JoystickOpen(0);
gabomdq@8043
   391
#else
gabomdq@8043
   392
        joystick = SDL_JoystickOpen(atoi(argv[1]));
gabomdq@8043
   393
#endif
gabomdq@8043
   394
        while ( keepGoing ) {
gabomdq@8043
   395
            if (joystick == NULL) {
gabomdq@8043
   396
                if ( !reportederror ) {
gabomdq@8043
   397
                    SDL_Log("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
gabomdq@8043
   398
                    keepGoing = SDL_FALSE;
gabomdq@8043
   399
                    reportederror = SDL_TRUE;
gabomdq@8043
   400
                }
gabomdq@8043
   401
            } else {
gabomdq@8043
   402
                reportederror = SDL_FALSE;
gabomdq@8043
   403
                keepGoing = WatchJoystick(joystick);
gabomdq@8043
   404
                SDL_JoystickClose(joystick);
gabomdq@8043
   405
            }
gabomdq@8043
   406
gabomdq@8043
   407
            joystick = NULL;
gabomdq@8043
   408
            if (keepGoing) {
gabomdq@8043
   409
                SDL_Log("Waiting for attach\n");
gabomdq@8043
   410
            }
gabomdq@8043
   411
            while (keepGoing) {
gabomdq@8043
   412
                SDL_WaitEvent(&event);
gabomdq@8043
   413
                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
gabomdq@8043
   414
                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
gabomdq@8043
   415
                    keepGoing = SDL_FALSE;
gabomdq@8043
   416
                } else if (event.type == SDL_JOYDEVICEADDED) {
gabomdq@8043
   417
                    joystick = SDL_JoystickOpen(atoi(argv[1]));
gabomdq@8043
   418
                    break;
gabomdq@8043
   419
                }
gabomdq@8043
   420
            }
gabomdq@8043
   421
        }
gabomdq@8043
   422
    }
gabomdq@8043
   423
    else {
gabomdq@8043
   424
        SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
gabomdq@8043
   425
    }
gabomdq@8043
   426
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
gabomdq@8043
   427
gabomdq@8043
   428
    return 0;
gabomdq@8043
   429
}
gabomdq@8043
   430
gabomdq@8043
   431
#else
gabomdq@8043
   432
gabomdq@8043
   433
int
gabomdq@8043
   434
main(int argc, char *argv[])
gabomdq@8043
   435
{
gabomdq@8043
   436
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
gabomdq@8043
   437
    exit(1);
gabomdq@8043
   438
}
gabomdq@8043
   439
gabomdq@8043
   440
#endif