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