Added an API to iterate over game controller mappings
authorSam Lantinga <slouken@libsdl.org>
Tue, 29 Nov 2016 06:36:57 -0800
changeset 1066174b42fb2a9e4
parent 10660 1ae4e8001b73
child 10662 c6756f2a187b
Added an API to iterate over game controller mappings
include/SDL_gamecontroller.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/joystick/SDL_gamecontroller.c
test/testgamecontroller.c
     1.1 --- a/include/SDL_gamecontroller.h	Tue Nov 29 05:34:20 2016 -0800
     1.2 +++ b/include/SDL_gamecontroller.h	Tue Nov 29 06:36:57 2016 -0800
     1.3 @@ -136,6 +136,20 @@
     1.4  extern DECLSPEC int SDLCALL SDL_GameControllerAddMapping(const char* mappingString);
     1.5  
     1.6  /**
     1.7 + *  Get the number of mappings installed
     1.8 + *
     1.9 + *  \return the number of mappings
    1.10 + */
    1.11 +extern DECLSPEC int SDLCALL SDL_GameControllerNumMappings();
    1.12 +
    1.13 +/**
    1.14 + *  Get the mapping at a particular index.
    1.15 + *
    1.16 + *  \return the mapping string.  Must be freed with SDL_free().  Returns NULL if the index is out of range.
    1.17 + */
    1.18 +extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForIndex(int mapping_index);
    1.19 +
    1.20 +/**
    1.21   *  Get a mapping string for a GUID
    1.22   *
    1.23   *  \return the mapping string.  Must be freed with SDL_free().  Returns NULL if no mapping is available
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Tue Nov 29 05:34:20 2016 -0800
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Tue Nov 29 06:36:57 2016 -0800
     2.3 @@ -622,3 +622,5 @@
     2.4  #define SDL_GameControllerGetProduct SDL_GameControllerGetProduct_REAL
     2.5  #define SDL_GameControllerGetProductVersion SDL_GameControllerGetProductVersion_REAL
     2.6  #define SDL_HasNEON SDL_HasNEON_REAL
     2.7 +#define SDL_GameControllerNumMappings SDL_GameControllerNumMappings_REAL
     2.8 +#define SDL_GameControllerMappingForIndex SDL_GameControllerMappingForIndex_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Tue Nov 29 05:34:20 2016 -0800
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Tue Nov 29 06:36:57 2016 -0800
     3.3 @@ -654,3 +654,5 @@
     3.4  SDL_DYNAPI_PROC(Uint16,SDL_GameControllerGetProduct,(SDL_GameController *a),(a),return)
     3.5  SDL_DYNAPI_PROC(Uint16,SDL_GameControllerGetProductVersion,(SDL_GameController *a),(a),return)
     3.6  SDL_DYNAPI_PROC(SDL_bool,SDL_HasNEON,(void),(),return)
     3.7 +SDL_DYNAPI_PROC(int,SDL_GameControllerNumMappings,(void),(),return)
     3.8 +SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForIndex,(int a),(a),return)
     4.1 --- a/src/joystick/SDL_gamecontroller.c	Tue Nov 29 05:34:20 2016 -0800
     4.2 +++ b/src/joystick/SDL_gamecontroller.c	Tue Nov 29 06:36:57 2016 -0800
     4.3 @@ -96,6 +96,7 @@
     4.4      struct _ControllerMapping_t *next;
     4.5  } ControllerMapping_t;
     4.6  
     4.7 +static SDL_JoystickGUID s_zeroGUID;
     4.8  static ControllerMapping_t *s_pSupportedControllers = NULL;
     4.9  static ControllerMapping_t *s_pXInputMapping = NULL;
    4.10  static ControllerMapping_t *s_pEmscriptenMapping = NULL;
    4.11 @@ -872,6 +873,57 @@
    4.12      return SDL_PrivateGameControllerAddMapping(mappingString, SDL_CONTROLLER_MAPPING_PRIORITY_API);
    4.13  }
    4.14  
    4.15 +/**
    4.16 + *  Get the number of mappings installed
    4.17 + */
    4.18 +int
    4.19 +SDL_GameControllerNumMappings()
    4.20 +{
    4.21 +    int num_mappings = 0;
    4.22 +    ControllerMapping_t *mapping;
    4.23 +
    4.24 +    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
    4.25 +        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
    4.26 +            continue;
    4.27 +        }
    4.28 +        ++num_mappings;
    4.29 +    }
    4.30 +    return num_mappings;
    4.31 +}
    4.32 +
    4.33 +/**
    4.34 + *  Get the mapping at a particular index.
    4.35 + */
    4.36 +char *
    4.37 +SDL_GameControllerMappingForIndex(int mapping_index)
    4.38 +{
    4.39 +    ControllerMapping_t *mapping;
    4.40 +
    4.41 +    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
    4.42 +        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
    4.43 +            continue;
    4.44 +        }
    4.45 +        if (mapping_index == 0) {
    4.46 +            char *pMappingString;
    4.47 +            char pchGUID[33];
    4.48 +            size_t needed;
    4.49 +
    4.50 +            SDL_JoystickGetGUIDString(mapping->guid, pchGUID, sizeof(pchGUID));
    4.51 +            /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
    4.52 +            needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
    4.53 +            pMappingString = SDL_malloc(needed);
    4.54 +            if (!pMappingString) {
    4.55 +                SDL_OutOfMemory();
    4.56 +                return NULL;
    4.57 +            }
    4.58 +            SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
    4.59 +            return pMappingString;
    4.60 +        }
    4.61 +        --mapping_index;
    4.62 +    }
    4.63 +    return NULL;
    4.64 +}
    4.65 +
    4.66  /*
    4.67   * Get the mapping string for this GUID
    4.68   */
     5.1 --- a/test/testgamecontroller.c	Tue Nov 29 05:34:20 2016 -0800
     5.2 +++ b/test/testgamecontroller.c	Tue Nov 29 06:36:57 2016 -0800
     5.3 @@ -259,6 +259,17 @@
     5.4      
     5.5      SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
     5.6  
     5.7 +    /* Print information about the mappings */
     5.8 +    SDL_Log("Supported mappings:\n");
     5.9 +    for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
    5.10 +        char *mapping = SDL_GameControllerMappingForIndex(i);
    5.11 +        if (mapping) {
    5.12 +            SDL_Log("\t%s\n", mapping);
    5.13 +            SDL_free(mapping);
    5.14 +        }
    5.15 +    }
    5.16 +    SDL_Log("\n");
    5.17 +
    5.18      /* Print information about the controller */
    5.19      for (i = 0; i < SDL_NumJoysticks(); ++i) {
    5.20          const char *name;