Added SDL_GameControllerMappingForDeviceIndex() to get the mapping for a controller before it's opened
authorSam Lantinga <slouken@libsdl.org>
Wed, 07 Mar 2018 13:30:40 -0800
changeset 119225b6c54dc89da
parent 11921 88a2982221ec
child 11923 96f71726c0b2
Added SDL_GameControllerMappingForDeviceIndex() to get the mapping for a controller before it's opened
WhatsNew.txt
include/SDL_gamecontroller.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/joystick/SDL_gamecontroller.c
src/joystick/SDL_sysjoystick.h
src/joystick/windows/SDL_xinputjoystick.c
     1.1 --- a/WhatsNew.txt	Tue Mar 06 14:51:50 2018 -0800
     1.2 +++ b/WhatsNew.txt	Wed Mar 07 13:30:40 2018 -0800
     1.3 @@ -2,6 +2,14 @@
     1.4  This is a list of major changes in SDL's version history.
     1.5  
     1.6  ---------------------------------------------------------------------------
     1.7 +2.0.9:
     1.8 +---------------------------------------------------------------------------
     1.9 +
    1.10 +General:
    1.11 +* Added SDL_GameControllerMappingForDeviceIndex() to get the mapping for a controller before it's opened
    1.12 +
    1.13 +
    1.14 +---------------------------------------------------------------------------
    1.15  2.0.8:
    1.16  ---------------------------------------------------------------------------
    1.17  
     2.1 --- a/include/SDL_gamecontroller.h	Tue Mar 06 14:51:50 2018 -0800
     2.2 +++ b/include/SDL_gamecontroller.h	Wed Mar 07 13:30:40 2018 -0800
     2.3 @@ -176,6 +176,14 @@
     2.4  extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
     2.5  
     2.6  /**
     2.7 + *  Get the mapping of a game controller.
     2.8 + *  This can be called before any controllers are opened.
     2.9 + *
    2.10 + *  \return the mapping string.  Must be freed with SDL_free().  Returns NULL if no mapping is available
    2.11 + */
    2.12 +extern DECLSPEC char *SDLCALL SDL_GameControllerMappingForDeviceIndex(int joystick_index);
    2.13 +
    2.14 +/**
    2.15   *  Open a game controller for use.
    2.16   *  The index passed as an argument refers to the N'th game controller on the system.
    2.17   *  This index is not the value which will identify this controller in future
     3.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Tue Mar 06 14:51:50 2018 -0800
     3.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Wed Mar 07 13:30:40 2018 -0800
     3.3 @@ -669,3 +669,4 @@
     3.4  #define SDL_WinRTGetDeviceFamily SDL_WinRTGetDeviceFamily_REAL
     3.5  #define SDL_log10 SDL_log10_REAL
     3.6  #define SDL_log10f SDL_log10f_REAL
     3.7 +#define SDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex_REAL
     4.1 --- a/src/dynapi/SDL_dynapi_procs.h	Tue Mar 06 14:51:50 2018 -0800
     4.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Wed Mar 07 13:30:40 2018 -0800
     4.3 @@ -707,3 +707,4 @@
     4.4  #endif
     4.5  SDL_DYNAPI_PROC(double,SDL_log10,(double a),(a),return)
     4.6  SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return)
     4.7 +SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForDeviceIndex,(int a),(a),return)
     5.1 --- a/src/joystick/SDL_gamecontroller.c	Tue Mar 06 14:51:50 2018 -0800
     5.2 +++ b/src/joystick/SDL_gamecontroller.c	Wed Mar 07 13:30:40 2018 -0800
     5.3 @@ -425,6 +425,12 @@
     5.4          }
     5.5          pSupportedController = pSupportedController->next;
     5.6      }
     5.7 +#if SDL_JOYSTICK_XINPUT
     5.8 +    if (guid->data[14] == 'x') {
     5.9 +        /* This is an XInput device */
    5.10 +        return s_pXInputMapping;
    5.11 +    }
    5.12 +#endif
    5.13      return NULL;
    5.14  }
    5.15  
    5.16 @@ -1033,11 +1039,6 @@
    5.17      name = SDL_JoystickNameForIndex(device_index);
    5.18      guid = SDL_JoystickGetDeviceGUID(device_index);
    5.19      mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
    5.20 -#if SDL_JOYSTICK_XINPUT
    5.21 -    if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
    5.22 -        mapping = s_pXInputMapping;
    5.23 -    }
    5.24 -#endif
    5.25      SDL_UnlockJoysticks();
    5.26      return mapping;
    5.27  }
    5.28 @@ -1374,6 +1375,40 @@
    5.29  }
    5.30  
    5.31  
    5.32 +/**
    5.33 + *  Get the mapping of a game controller.
    5.34 + *  This can be called before any controllers are opened.
    5.35 + *  If no mapping can be found, this function returns NULL.
    5.36 + */
    5.37 +char *
    5.38 +SDL_GameControllerMappingForDeviceIndex(int joystick_index)
    5.39 +{
    5.40 +    char *pMappingString = NULL;
    5.41 +    ControllerMapping_t *mapping;
    5.42 +
    5.43 +    SDL_LockJoysticks();
    5.44 +    mapping = SDL_PrivateGetControllerMapping(joystick_index);
    5.45 +    if (mapping) {
    5.46 +        SDL_JoystickGUID guid;
    5.47 +        char pchGUID[33];
    5.48 +        size_t needed;
    5.49 +        guid = SDL_JoystickGetDeviceGUID(joystick_index);
    5.50 +        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
    5.51 +        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
    5.52 +        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
    5.53 +        pMappingString = SDL_malloc(needed);
    5.54 +        if (!pMappingString) {
    5.55 +            SDL_OutOfMemory();
    5.56 +            SDL_UnlockJoysticks();
    5.57 +            return NULL;
    5.58 +        }
    5.59 +        SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
    5.60 +    }
    5.61 +    SDL_UnlockJoysticks();
    5.62 +    return pMappingString;
    5.63 +}
    5.64 +
    5.65 +
    5.66  /*
    5.67   * Return 1 if the joystick with this name and GUID is a supported controller
    5.68   */
     6.1 --- a/src/joystick/SDL_sysjoystick.h	Tue Mar 06 14:51:50 2018 -0800
     6.2 +++ b/src/joystick/SDL_sysjoystick.h	Wed Mar 07 13:30:40 2018 -0800
     6.3 @@ -121,11 +121,6 @@
     6.4  /* Function to return the stable GUID for a opened joystick */
     6.5  extern SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick);
     6.6  
     6.7 -#if SDL_JOYSTICK_XINPUT
     6.8 -/* Function returns SDL_TRUE if this device is an XInput gamepad */
     6.9 -extern SDL_bool SDL_SYS_IsXInputGamepad_DeviceIndex(int device_index);
    6.10 -#endif
    6.11 -
    6.12  #endif /* SDL_sysjoystick_h_ */
    6.13  
    6.14  /* vi: set ts=4 sw=4 expandtab: */
     7.1 --- a/src/joystick/windows/SDL_xinputjoystick.c	Tue Mar 06 14:51:50 2018 -0800
     7.2 +++ b/src/joystick/windows/SDL_xinputjoystick.c	Wed Mar 07 13:30:40 2018 -0800
     7.3 @@ -464,18 +464,6 @@
     7.4      }
     7.5  }
     7.6  
     7.7 -SDL_bool
     7.8 -SDL_SYS_IsXInputGamepad_DeviceIndex(int device_index)
     7.9 -{
    7.10 -    JoyStick_DeviceData *device = SYS_Joystick;
    7.11 -    int index;
    7.12 -
    7.13 -    for (index = device_index; index > 0; index--)
    7.14 -        device = device->pNext;
    7.15 -
    7.16 -    return device->bXInputDevice;
    7.17 -}
    7.18 -
    7.19  #else /* !SDL_JOYSTICK_XINPUT */
    7.20  
    7.21  typedef struct JoyStick_DeviceData JoyStick_DeviceData;