Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 03 Dec 2013 12:01:28 -0300
changeset 8046e7c89b29932a
parent 8045 3c2694255705
child 8047 a5270cef21a7
Adds SDL_GameControllerAddMappingsFromRW, updates controllermap

SDL_GameControllerAddMappingsFromFile is now a convenience macro.

controllermap can now skip bindings by pressing space or clicking/touching the
screen.
include/SDL_gamecontroller.h
src/joystick/SDL_gamecontroller.c
test/controllermap.c
     1.1 --- a/include/SDL_gamecontroller.h	Mon Dec 02 23:54:35 2013 -0800
     1.2 +++ b/include/SDL_gamecontroller.h	Tue Dec 03 12:01:28 2013 -0300
     1.3 @@ -109,12 +109,21 @@
     1.4   */
     1.5  
     1.6  /**
     1.7 - *  Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
     1.8 + *  Load a set of mappings from a seekable SDL data stream (memory or file), filtered by the current SDL_GetPlatform()
     1.9   *  A community sourced database of controllers is available at https://raw.github.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt
    1.10   *
    1.11 + *  If \c freerw is non-zero, the stream will be closed after being read.
    1.12 + * 
    1.13   * \return number of mappings added, -1 on error
    1.14   */
    1.15 -extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromFile( const char* mapDB );
    1.16 +extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromRW( SDL_RWops * rw, int freerw );
    1.17 +
    1.18 +/**
    1.19 + *  Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
    1.20 + *
    1.21 + *  Convenience macro.
    1.22 + */
    1.23 +#define SDL_GameControllerAddMappingsFromFile(file)   SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)
    1.24  
    1.25  /**
    1.26   *  Add or update an existing mapping configuration
     2.1 --- a/src/joystick/SDL_gamecontroller.c	Mon Dec 02 23:54:35 2013 -0800
     2.2 +++ b/src/joystick/SDL_gamecontroller.c	Tue Dec 03 12:01:28 2013 -0300
     2.3 @@ -658,32 +658,37 @@
     2.4   * Add or update an entry into the Mappings Database
     2.5   */
     2.6  int
     2.7 -SDL_GameControllerAddMappingsFromFile( const char* mapDB )
     2.8 +SDL_GameControllerAddMappingsFromRW( SDL_RWops * rw, int freerw )
     2.9  {
    2.10      const char *platform = SDL_GetPlatform();
    2.11 -    SDL_RWops *rw;
    2.12      int controllers = 0;
    2.13      char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
    2.14      size_t db_size, platform_len;
    2.15      
    2.16 -    rw = SDL_RWFromFile(mapDB, "rb");
    2.17      if (rw == NULL) {
    2.18 -        return SDL_SetError("Could not open %s", mapDB);
    2.19 +        return SDL_SetError("Invalid RWops");
    2.20      }
    2.21      db_size = SDL_RWsize(rw);
    2.22      
    2.23      buf = (char *) SDL_malloc(db_size + 1);
    2.24      if (buf == NULL) {
    2.25 -        SDL_RWclose(rw);
    2.26 +        if (freerw) {
    2.27 +            SDL_RWclose(rw);
    2.28 +        }
    2.29          return SDL_SetError("Could allocate space to not read DB into memory");
    2.30      }
    2.31      
    2.32      if (SDL_RWread(rw, buf, db_size, 1) != 1) {
    2.33 -        SDL_RWclose(rw);
    2.34 +        if (freerw) {
    2.35 +            SDL_RWclose(rw);
    2.36 +        }
    2.37          SDL_free(buf);
    2.38          return SDL_SetError("Could not read DB");
    2.39      }
    2.40 -    SDL_RWclose(rw);
    2.41 +    
    2.42 +    if (freerw) {
    2.43 +        SDL_RWclose(rw);
    2.44 +    }
    2.45      
    2.46      buf[db_size] = '\0';
    2.47      line = buf;
     3.1 --- a/test/controllermap.c	Mon Dec 02 23:54:35 2013 -0800
     3.2 +++ b/test/controllermap.c	Tue Dec 03 12:01:28 2013 -0300
     3.3 @@ -144,7 +144,7 @@
     3.4      };
     3.5  
     3.6      /* Create a window to display joystick axis position */
     3.7 -    window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
     3.8 +    window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
     3.9                                SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    3.10                                SCREEN_HEIGHT, 0);
    3.11      if (window == NULL) {
    3.12 @@ -178,6 +178,7 @@
    3.13      Press the buttons on your controller when indicated\n\
    3.14      (Your controller may look different than the picture)\n\
    3.15      If you want to correct a mistake, press backspace or the back button on your device\n\
    3.16 +    To skip a button, press SPACE or click/touch the screen\n\
    3.17      To exit, press ESC\n\
    3.18      ====================================================================================\n");
    3.19      
    3.20 @@ -287,6 +288,12 @@
    3.21                          next=SDL_TRUE;
    3.22                      }
    3.23                      break;
    3.24 +                case SDL_FINGERDOWN:
    3.25 +                case SDL_MOUSEBUTTONDOWN:
    3.26 +                    /* Skip this step */
    3.27 +                    s++;
    3.28 +                    next=SDL_TRUE;
    3.29 +                    break;
    3.30                  case SDL_KEYDOWN:
    3.31                      if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
    3.32                          /* Undo! */
    3.33 @@ -297,12 +304,17 @@
    3.34                          }
    3.35                          break;
    3.36                      }
    3.37 +                    if (event.key.keysym.sym == SDLK_SPACE) {
    3.38 +                        /* Skip this step */
    3.39 +                        s++;
    3.40 +                        next=SDL_TRUE;
    3.41 +                        break;
    3.42 +                    }
    3.43 +                    
    3.44                      if ((event.key.keysym.sym != SDLK_ESCAPE)) {
    3.45                          break;
    3.46                      }
    3.47                      /* Fall through to signal quit */
    3.48 -                case SDL_FINGERDOWN:
    3.49 -                case SDL_MOUSEBUTTONDOWN:
    3.50                  case SDL_QUIT:
    3.51                      done = SDL_TRUE;
    3.52                      break;