Adds SDL_GameControllerAddMappingsFromFile
authorGabriel Jacobo <gabomdq@gmail.com>
Mon, 02 Dec 2013 19:34:08 -0300
changeset 80427863121cb7d5
parent 8041 afd62b3fda31
child 8043 74fcbf6987ba
Adds SDL_GameControllerAddMappingsFromFile
include/SDL_gamecontroller.h
src/joystick/SDL_gamecontroller.c
     1.1 --- a/include/SDL_gamecontroller.h	Mon Dec 02 10:08:57 2013 -0300
     1.2 +++ b/include/SDL_gamecontroller.h	Mon Dec 02 19:34:08 2013 -0300
     1.3 @@ -109,6 +109,14 @@
     1.4   */
     1.5  
     1.6  /**
     1.7 + *  Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
     1.8 + *  A community sourced database of controllers is available at https://raw.github.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt
     1.9 + *
    1.10 + * \return number of mappings added, -1 on error
    1.11 + */
    1.12 +extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromFile( const char* mapDB );
    1.13 +
    1.14 +/**
    1.15   *  Add or update an existing mapping configuration
    1.16   *
    1.17   * \return 1 if mapping is added, 0 if updated, -1 on error
     2.1 --- a/src/joystick/SDL_gamecontroller.c	Mon Dec 02 10:08:57 2013 -0300
     2.2 +++ b/src/joystick/SDL_gamecontroller.c	Mon Dec 02 19:34:08 2013 -0300
     2.3 @@ -33,6 +33,7 @@
     2.4  #endif
     2.5  #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
     2.6  
     2.7 +#define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
     2.8  
     2.9  /* a list of currently opened game controllers */
    2.10  static SDL_GameController *SDL_gamecontrollers = NULL;
    2.11 @@ -657,6 +658,73 @@
    2.12   * Add or update an entry into the Mappings Database
    2.13   */
    2.14  int
    2.15 +SDL_GameControllerAddMappingsFromFile( const char* mapDB )
    2.16 +{
    2.17 +    const char *platform = SDL_GetPlatform();
    2.18 +    SDL_RWops *rw;
    2.19 +    int controllers = 0;
    2.20 +    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
    2.21 +    size_t db_size, platform_len;
    2.22 +    
    2.23 +    rw = SDL_RWFromFile(mapDB, "rb");
    2.24 +    if (rw == NULL) {
    2.25 +        return SDL_SetError("Could not open %s", mapDB);
    2.26 +    }
    2.27 +    db_size = SDL_RWsize(rw);
    2.28 +    
    2.29 +    buf = (char *) SDL_malloc(db_size + 1);
    2.30 +    if (buf == NULL) {
    2.31 +        SDL_RWclose(rw);
    2.32 +        return SDL_SetError("Could allocate space to not read DB into memory");
    2.33 +    }
    2.34 +    
    2.35 +    if (SDL_RWread(rw, buf, db_size, 1) != 1) {
    2.36 +        SDL_RWclose(rw);
    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 +    buf[db_size] = '\0';
    2.43 +    line = buf;
    2.44 +    
    2.45 +    while (line < buf + db_size) {
    2.46 +        line_end = SDL_strchr( line, '\n' );
    2.47 +        if (line_end != NULL) {
    2.48 +            *line_end = '\0';
    2.49 +        }
    2.50 +        else {
    2.51 +            line_end = buf + db_size;
    2.52 +        }
    2.53 +        
    2.54 +        /* Extract and verify the platform */
    2.55 +        tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
    2.56 +        if ( tmp != NULL ) {
    2.57 +            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
    2.58 +            comma = SDL_strchr(tmp, ',');
    2.59 +            if (comma != NULL) {
    2.60 +                platform_len = comma - tmp + 1;
    2.61 +                if (platform_len + 1 < SDL_arraysize(line_platform)) {
    2.62 +                    SDL_strlcpy(line_platform, tmp, platform_len);
    2.63 +                    if(SDL_strncasecmp(line_platform, platform, platform_len) == 0
    2.64 +                        && SDL_GameControllerAddMapping(line) > 0) {
    2.65 +                        controllers++;
    2.66 +                    }
    2.67 +                }
    2.68 +            }
    2.69 +        }
    2.70 +        
    2.71 +        line = line_end + 1;
    2.72 +    }
    2.73 +
    2.74 +    SDL_free(buf);
    2.75 +    return controllers;
    2.76 +}
    2.77 +
    2.78 +/*
    2.79 + * Add or update an entry into the Mappings Database
    2.80 + */
    2.81 +int
    2.82  SDL_GameControllerAddMapping( const char *mappingString )
    2.83  {
    2.84      char *pchGUID;