test/controllermap.c
changeset 8043 74fcbf6987ba
child 8046 e7c89b29932a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/controllermap.c	Mon Dec 02 19:35:04 2013 -0300
     1.3 @@ -0,0 +1,432 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +
    1.16 +/* Game controller mapping generator */
    1.17 +/* Gabriel Jacobo <gabomdq@gmail.com> */
    1.18 +
    1.19 +#include <stdio.h>
    1.20 +#include <stdlib.h>
    1.21 +#include <string.h>
    1.22 +
    1.23 +#include "SDL.h"
    1.24 +
    1.25 +#ifndef SDL_JOYSTICK_DISABLED
    1.26 +
    1.27 +#ifdef __IPHONEOS__
    1.28 +#define SCREEN_WIDTH    320
    1.29 +#define SCREEN_HEIGHT   480
    1.30 +#else
    1.31 +#define SCREEN_WIDTH    512
    1.32 +#define SCREEN_HEIGHT   317
    1.33 +#endif
    1.34 +
    1.35 +#define MAP_WIDTH 512
    1.36 +#define MAP_HEIGHT 317
    1.37 +
    1.38 +#define MARKER_BUTTON 1
    1.39 +#define MARKER_AXIS 2
    1.40 +
    1.41 +typedef struct MappingStep
    1.42 +{
    1.43 +    int x, y;
    1.44 +    double angle;
    1.45 +    int marker;
    1.46 +    char *field;
    1.47 +    int axis, button, hat, hat_value;
    1.48 +    char mapping[4096];
    1.49 +}MappingStep;
    1.50 +
    1.51 +
    1.52 +SDL_Texture *
    1.53 +LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    1.54 +{
    1.55 +    SDL_Surface *temp;
    1.56 +    SDL_Texture *texture;
    1.57 +
    1.58 +    /* Load the sprite image */
    1.59 +    temp = SDL_LoadBMP(file);
    1.60 +    if (temp == NULL) {
    1.61 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
    1.62 +        return NULL;
    1.63 +    }
    1.64 +
    1.65 +    /* Set transparent pixel as the pixel at (0,0) */
    1.66 +    if (transparent) {
    1.67 +        if (temp->format->palette) {
    1.68 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    1.69 +        } else {
    1.70 +            switch (temp->format->BitsPerPixel) {
    1.71 +            case 15:
    1.72 +                SDL_SetColorKey(temp, SDL_TRUE,
    1.73 +                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
    1.74 +                break;
    1.75 +            case 16:
    1.76 +                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    1.77 +                break;
    1.78 +            case 24:
    1.79 +                SDL_SetColorKey(temp, SDL_TRUE,
    1.80 +                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    1.81 +                break;
    1.82 +            case 32:
    1.83 +                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    1.84 +                break;
    1.85 +            }
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    /* Create textures from the image */
    1.90 +    texture = SDL_CreateTextureFromSurface(renderer, temp);
    1.91 +    if (!texture) {
    1.92 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
    1.93 +        SDL_FreeSurface(temp);
    1.94 +        return NULL;
    1.95 +    }
    1.96 +    SDL_FreeSurface(temp);
    1.97 +
    1.98 +    /* We're ready to roll. :) */
    1.99 +    return texture;
   1.100 +}
   1.101 +
   1.102 +static void
   1.103 +DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
   1.104 +{
   1.105 +    const SDL_Rect area = { x, y, w, h };
   1.106 +    SDL_RenderFillRect(r, &area);
   1.107 +}
   1.108 +
   1.109 +static SDL_bool
   1.110 +WatchJoystick(SDL_Joystick * joystick)
   1.111 +{
   1.112 +    SDL_Window *window = NULL;
   1.113 +    SDL_Renderer *screen = NULL;
   1.114 +    SDL_Texture *target, *background, *button, *axis, *marker;
   1.115 +    const char *name = NULL;
   1.116 +    SDL_bool retval = SDL_FALSE;
   1.117 +    SDL_bool done = SDL_FALSE, next=SDL_FALSE;
   1.118 +    SDL_Event event;
   1.119 +    SDL_Rect dst;
   1.120 +    int i, s, _s;
   1.121 +    Uint8 alpha=200, alpha_step = -1;
   1.122 +    Uint32 alpha_ticks;
   1.123 +    char mapping[4096], temp[4096];
   1.124 +    MappingStep *step;
   1.125 +    MappingStep steps[] = {
   1.126 +        {342, 132,  0.0,  MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
   1.127 +        {387, 167,  0.0,  MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
   1.128 +        {431, 132,  0.0,  MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
   1.129 +        {389, 101,  0.0,  MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
   1.130 +        {174, 132,  0.0,  MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
   1.131 +        {233, 132,  0.0,  MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
   1.132 +        {289, 132,  0.0,  MARKER_BUTTON, "start", -1, -1, -1, -1, ""},        
   1.133 +        {116, 217,  0.0,  MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
   1.134 +        {154, 249,  0.0,  MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
   1.135 +        {186, 217,  0.0,  MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
   1.136 +        {154, 188,  0.0,  MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
   1.137 +        {77,  40,   0.0,  MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
   1.138 +        {91, 0,    0.0,  MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
   1.139 +        {396, 36,   0.0,  MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
   1.140 +        {375, 0,    0.0,  MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
   1.141 +        {75,  154,  0.0,  MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
   1.142 +        {305, 230,  0.0,  MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
   1.143 +        {75,  154,  0.0,  MARKER_AXIS,   "leftx", -1, -1, -1, -1, ""},
   1.144 +        {75,  154,  90.0, MARKER_AXIS,   "lefty", -1, -1, -1, -1, ""},        
   1.145 +        {305, 230,  0.0,  MARKER_AXIS,   "rightx", -1, -1, -1, -1, ""},
   1.146 +        {305, 230,  90.0, MARKER_AXIS,   "righty", -1, -1, -1, -1, ""},
   1.147 +    };
   1.148 +
   1.149 +    /* Create a window to display joystick axis position */
   1.150 +    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
   1.151 +                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   1.152 +                              SCREEN_HEIGHT, 0);
   1.153 +    if (window == NULL) {
   1.154 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   1.155 +        return SDL_FALSE;
   1.156 +    }
   1.157 +
   1.158 +    screen = SDL_CreateRenderer(window, -1, 0);
   1.159 +    if (screen == NULL) {
   1.160 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   1.161 +        SDL_DestroyWindow(window);
   1.162 +        return SDL_FALSE;
   1.163 +    }
   1.164 +    
   1.165 +    target = SDL_CreateTexture(screen, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, MAP_WIDTH, MAP_HEIGHT);
   1.166 +    background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   1.167 +    button = LoadTexture(screen, "button.bmp", SDL_TRUE);
   1.168 +    axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
   1.169 +    SDL_RaiseWindow(window);
   1.170 +
   1.171 +    /* Print info about the joystick we are watching */
   1.172 +    name = SDL_JoystickName(joystick);
   1.173 +    SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
   1.174 +           name ? name : "Unknown Joystick");
   1.175 +    SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
   1.176 +           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
   1.177 +           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
   1.178 +    
   1.179 +    SDL_Log("\n\n\
   1.180 +    ====================================================================================\n\
   1.181 +    Press the buttons on your controller when indicated\n\
   1.182 +    (Your controller may look different than the picture)\n\
   1.183 +    If you want to correct a mistake, press backspace or the back button on your device\n\
   1.184 +    To exit, press ESC\n\
   1.185 +    ====================================================================================\n");
   1.186 +    
   1.187 +    /* Initialize mapping with GUID and name */
   1.188 +    SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
   1.189 +    SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
   1.190 +        temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
   1.191 +
   1.192 +    /* Loop, getting joystick events! */
   1.193 +    for(s=0; s<SDL_arraysize(steps) && !done;) {
   1.194 +        /* blank screen, set up for drawing this frame. */
   1.195 +        step = &steps[s];
   1.196 +        SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
   1.197 +        step->axis = -1;
   1.198 +        step->button = -1;
   1.199 +        step->hat = -1;
   1.200 +        step->hat_value = -1;
   1.201 +        SDL_SetClipboardText("TESTING TESTING 123");
   1.202 +        
   1.203 +        switch(step->marker) {
   1.204 +            case MARKER_AXIS:
   1.205 +                marker = axis;
   1.206 +                break;
   1.207 +            case MARKER_BUTTON:
   1.208 +                marker = button;
   1.209 +                break;
   1.210 +            default:
   1.211 +                break;
   1.212 +        }
   1.213 +        
   1.214 +        dst.x = step->x;
   1.215 +        dst.y = step->y;
   1.216 +        SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
   1.217 +        next=SDL_FALSE;
   1.218 +        
   1.219 +        while (!done && !next) {
   1.220 +            if (SDL_GetTicks() - alpha_ticks > 5) {
   1.221 +                alpha_ticks = SDL_GetTicks();
   1.222 +                alpha += alpha_step;
   1.223 +                if (alpha == 255) {
   1.224 +                    alpha_step = -1;
   1.225 +                }
   1.226 +                if (alpha < 128) {
   1.227 +                    alpha_step = 1;
   1.228 +                }
   1.229 +            }
   1.230 +            
   1.231 +            SDL_SetRenderTarget(screen, target);
   1.232 +            SDL_RenderCopy(screen, background, NULL, NULL);
   1.233 +            SDL_SetTextureAlphaMod(marker, alpha);
   1.234 +            SDL_SetTextureColorMod(marker, 10, 255, 21);
   1.235 +            SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0);
   1.236 +            SDL_SetRenderTarget(screen, NULL);
   1.237 +            SDL_RenderCopy(screen, target, NULL, NULL);
   1.238 +            SDL_RenderPresent(screen);
   1.239 +            
   1.240 +            if (SDL_PollEvent(&event)) {
   1.241 +                switch (event.type) {
   1.242 +                case SDL_JOYAXISMOTION:
   1.243 +                    if (event.jaxis.value > 20000 || event.jaxis.value < -20000) {
   1.244 +                        for (_s = 0; _s < s; _s++) {
   1.245 +                            if (steps[_s].axis == event.jaxis.axis) {
   1.246 +                                break;
   1.247 +                            }
   1.248 +                        }
   1.249 +                        if (_s == s) {
   1.250 +                            step->axis = event.jaxis.axis;
   1.251 +                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   1.252 +                            SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
   1.253 +                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   1.254 +                            s++;
   1.255 +                            next=SDL_TRUE;
   1.256 +                        }
   1.257 +                    }
   1.258 +                    
   1.259 +                    break;
   1.260 +                case SDL_JOYHATMOTION:
   1.261 +                        for (_s = 0; _s < s; _s++) {
   1.262 +                            if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
   1.263 +                                break;
   1.264 +                            }
   1.265 +                        }
   1.266 +                        if (_s == s) {
   1.267 +                            step->hat = event.jhat.hat;
   1.268 +                            step->hat_value = event.jhat.value;
   1.269 +                            SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   1.270 +                            SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
   1.271 +                            SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   1.272 +                            s++;
   1.273 +                            next=SDL_TRUE;
   1.274 +                        }
   1.275 +                    break;
   1.276 +                case SDL_JOYBALLMOTION:
   1.277 +                    break;
   1.278 +                case SDL_JOYBUTTONUP:
   1.279 +                    for (_s = 0; _s < s; _s++) {
   1.280 +                        if (steps[_s].button == event.jbutton.button) {
   1.281 +                            break;
   1.282 +                        }
   1.283 +                    }
   1.284 +                    if (_s == s) {
   1.285 +                        step->button = event.jbutton.button;
   1.286 +                        SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   1.287 +                        SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
   1.288 +                        SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   1.289 +                        s++;
   1.290 +                        next=SDL_TRUE;
   1.291 +                    }
   1.292 +                    break;
   1.293 +                case SDL_KEYDOWN:
   1.294 +                    if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
   1.295 +                        /* Undo! */
   1.296 +                        if (s > 0) {
   1.297 +                            SDL_strlcpy(mapping, step->mapping, SDL_arraysize(step->mapping));
   1.298 +                            s--;
   1.299 +                            next = SDL_TRUE;
   1.300 +                        }
   1.301 +                        break;
   1.302 +                    }
   1.303 +                    if ((event.key.keysym.sym != SDLK_ESCAPE)) {
   1.304 +                        break;
   1.305 +                    }
   1.306 +                    /* Fall through to signal quit */
   1.307 +                case SDL_FINGERDOWN:
   1.308 +                case SDL_MOUSEBUTTONDOWN:
   1.309 +                case SDL_QUIT:
   1.310 +                    done = SDL_TRUE;
   1.311 +                    break;
   1.312 +                default:
   1.313 +                    break;
   1.314 +                }
   1.315 +            }
   1.316 +        }
   1.317 +
   1.318 +    }
   1.319 +
   1.320 +    if (s == SDL_arraysize(steps) ) {
   1.321 +        SDL_Log("Mapping:\n\n%s\n\n", mapping);
   1.322 +        /* Print to stdout as well so the user can cat the output somewhere */
   1.323 +        printf("%s\n", mapping);
   1.324 +    }
   1.325 +    
   1.326 +    while(SDL_PollEvent(&event)) {};
   1.327 +    
   1.328 +    SDL_DestroyRenderer(screen);
   1.329 +    SDL_DestroyWindow(window);
   1.330 +    return retval;
   1.331 +}
   1.332 +
   1.333 +int
   1.334 +main(int argc, char *argv[])
   1.335 +{
   1.336 +    const char *name;
   1.337 +    int i;
   1.338 +    SDL_Joystick *joystick;
   1.339 +
   1.340 +    /* Enable standard application logging */
   1.341 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
   1.342 +
   1.343 +    /* Initialize SDL (Note: video is required to start event loop) */
   1.344 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   1.345 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.346 +        exit(1);
   1.347 +    }
   1.348 +
   1.349 +    /* Print information about the joysticks */
   1.350 +    SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
   1.351 +    for (i = 0; i < SDL_NumJoysticks(); ++i) {
   1.352 +        name = SDL_JoystickNameForIndex(i);
   1.353 +        SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   1.354 +        joystick = SDL_JoystickOpen(i);
   1.355 +        if (joystick == NULL) {
   1.356 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
   1.357 +                    SDL_GetError());
   1.358 +        } else {
   1.359 +            char guid[64];
   1.360 +            SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
   1.361 +                                      guid, sizeof (guid));
   1.362 +            SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   1.363 +            SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   1.364 +            SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
   1.365 +            SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   1.366 +            SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   1.367 +            SDL_Log("       guid: %s\n", guid);
   1.368 +            SDL_JoystickClose(joystick);
   1.369 +        }
   1.370 +    }
   1.371 +
   1.372 +#ifdef ANDROID
   1.373 +    if (SDL_NumJoysticks() > 0) {
   1.374 +#else
   1.375 +    if (argv[1]) {
   1.376 +#endif
   1.377 +        SDL_bool reportederror = SDL_FALSE;
   1.378 +        SDL_bool keepGoing = SDL_TRUE;
   1.379 +        SDL_Event event;
   1.380 +#ifdef ANDROID
   1.381 +        joystick = SDL_JoystickOpen(0);
   1.382 +#else
   1.383 +        joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.384 +#endif
   1.385 +        while ( keepGoing ) {
   1.386 +            if (joystick == NULL) {
   1.387 +                if ( !reportederror ) {
   1.388 +                    SDL_Log("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   1.389 +                    keepGoing = SDL_FALSE;
   1.390 +                    reportederror = SDL_TRUE;
   1.391 +                }
   1.392 +            } else {
   1.393 +                reportederror = SDL_FALSE;
   1.394 +                keepGoing = WatchJoystick(joystick);
   1.395 +                SDL_JoystickClose(joystick);
   1.396 +            }
   1.397 +
   1.398 +            joystick = NULL;
   1.399 +            if (keepGoing) {
   1.400 +                SDL_Log("Waiting for attach\n");
   1.401 +            }
   1.402 +            while (keepGoing) {
   1.403 +                SDL_WaitEvent(&event);
   1.404 +                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
   1.405 +                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
   1.406 +                    keepGoing = SDL_FALSE;
   1.407 +                } else if (event.type == SDL_JOYDEVICEADDED) {
   1.408 +                    joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.409 +                    break;
   1.410 +                }
   1.411 +            }
   1.412 +        }
   1.413 +    }
   1.414 +    else {
   1.415 +        SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
   1.416 +    }
   1.417 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.418 +
   1.419 +#ifdef ANDROID
   1.420 +    exit(0);
   1.421 +#else
   1.422 +    return 0;
   1.423 +#endif
   1.424 +}
   1.425 +
   1.426 +#else
   1.427 +
   1.428 +int
   1.429 +main(int argc, char *argv[])
   1.430 +{
   1.431 +    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   1.432 +    exit(1);
   1.433 +}
   1.434 +
   1.435 +#endif