Fixed bug 2553 - Add support to all XInput devices
authorSam Lantinga
Tue, 24 Jun 2014 13:31:25 -0700
changeset 892021ccd40c778a
parent 8919 c9be8299ba6b
child 8921 4a9feef61c85
Fixed bug 2553 - Add support to all XInput devices

This adds support for all XInput devices, exposed through the SDL joystick API.
The button and axis reporting for XInput devices has been changed to match DirectInput and other platforms.
The game controller xinput mapping has been updated so this change is seamless.
There is a new hint, SDL_HINT_XINPUT_USE_OLD_JOYSTICK_MAPPING, for any applications that have hardcoded the old xinput button and axis set. This hint will be removed in SDL 2.1.
include/SDL_hints.h
src/joystick/SDL_gamecontroller.c
src/joystick/SDL_gamecontrollerdb.h
src/joystick/SDL_joystick.c
src/joystick/SDL_sysjoystick.h
src/joystick/windows/SDL_dxjoystick.c
src/joystick/windows/SDL_dxjoystick_c.h
     1.1 --- a/include/SDL_hints.h	Tue Jun 24 01:38:21 2014 -0700
     1.2 +++ b/include/SDL_hints.h	Tue Jun 24 13:31:25 2014 -0700
     1.3 @@ -282,6 +282,16 @@
     1.4  
     1.5  
     1.6  /**
     1.7 + *  \brief  A variable that causes SDL to use the old axis and button mapping for XInput devices.
     1.8 + *
     1.9 + *  This hint is for backwards compatibility only and will be removed in SDL 2.1
    1.10 + *
    1.11 + *  The default value is "0".  This hint must be set before SDL_Init()
    1.12 + */
    1.13 +#define SDL_HINT_XINPUT_USE_OLD_JOYSTICK_MAPPING "SDL_XINPUT_USE_OLD_JOYSTICK_MAPPING"
    1.14 +
    1.15 +
    1.16 +/**
    1.17   *  \brief  A variable that lets you manually hint extra gamecontroller db entries
    1.18   *
    1.19   *  The variable should be newline delimited rows of gamecontroller config data, see SDL_gamecontroller.h
     2.1 --- a/src/joystick/SDL_gamecontroller.c	Tue Jun 24 01:38:21 2014 -0700
     2.2 +++ b/src/joystick/SDL_gamecontroller.c	Tue Jun 24 13:31:25 2014 -0700
     2.3 @@ -261,7 +261,7 @@
     2.4  ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
     2.5  {
     2.6  #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)
     2.7 -    if (SDL_SYS_IsXInputDeviceIndex(device_index) && s_pXInputMapping) {
     2.8 +    if (SDL_SYS_IsXInputGamepad_DeviceIndex(device_index) && s_pXInputMapping) {
     2.9          return s_pXInputMapping;
    2.10      }
    2.11      else
     3.1 --- a/src/joystick/SDL_gamecontrollerdb.h	Tue Jun 24 01:38:21 2014 -0700
     3.2 +++ b/src/joystick/SDL_gamecontrollerdb.h	Tue Jun 24 13:31:25 2014 -0700
     3.3 @@ -41,9 +41,9 @@
     3.4      "4c056802000000000000504944564944,PS3 Controller,a:b14,b:b13,back:b0,dpdown:b6,dpleft:b7,dpright:b5,dpup:b4,guide:b16,leftshoulder:b10,leftstick:b1,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b11,rightstick:b2,righttrigger:b9,rightx:a2,righty:a3,start:b3,x:b15,y:b12,",
     3.5      "25090500000000000000504944564944,PS3 DualShock,a:b2,b:b1,back:b9,dpdown:h0.8,dpleft:h0.4,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b0,y:b3,",
     3.6      "4c05c405000000000000504944564944,PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,",
     3.7 -    "xinput,X360 Controller,a:b10,b:b11,back:b5,dpdown:b1,dpleft:b2,dpright:b3,dpup:b0,guide:b14,leftshoulder:b8,leftstick:b6,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b9,rightstick:b7,righttrigger:a5,rightx:a2,righty:a3,start:b4,x:b12,y:b13,",
     3.8 +    "xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
     3.9  #elif defined(SDL_JOYSTICK_XINPUT)
    3.10 -    "xinput,X360 Controller,a:b10,b:b11,back:b5,dpdown:b1,dpleft:b2,dpright:b3,dpup:b0,guide:b14,leftshoulder:b8,leftstick:b6,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b9,rightstick:b7,righttrigger:a5,rightx:a2,righty:a3,start:b4,x:b12,y:b13,",
    3.11 +	"xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
    3.12  #elif defined(__MACOSX__)
    3.13      "0500000047532047616d657061640000,GameStop Gamepad,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,",
    3.14      "6d0400000000000016c2000000000000,Logitech F310 Gamepad (DInput),a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,", /* Guide button doesn't seem to be sent in DInput mode. */
     4.1 --- a/src/joystick/SDL_joystick.c	Tue Jun 24 01:38:21 2014 -0700
     4.2 +++ b/src/joystick/SDL_joystick.c	Tue Jun 24 13:31:25 2014 -0700
     4.3 @@ -113,9 +113,8 @@
     4.4      /* If the joystick is already open, return it
     4.5      * it is important that we have a single joystick * for each instance id
     4.6      */
     4.7 -    while ( joysticklist )
     4.8 -    {
     4.9 -        if ( SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id ) {
    4.10 +    while (joysticklist) {
    4.11 +        if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) {
    4.12                  joystick = joysticklist;
    4.13                  ++joystick->ref_count;
    4.14                  return (joystick);
    4.15 @@ -136,9 +135,9 @@
    4.16          return NULL;
    4.17      }
    4.18  
    4.19 -    joystickname = SDL_SYS_JoystickNameForDeviceIndex( device_index );
    4.20 -    if ( joystickname )
    4.21 -        joystick->name = SDL_strdup( joystickname );
    4.22 +    joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index);
    4.23 +    if (joystickname)
    4.24 +        joystick->name = SDL_strdup(joystickname);
    4.25      else
    4.26          joystick->name = NULL;
    4.27  
    4.28 @@ -186,7 +185,7 @@
    4.29      joystick->next = SDL_joysticks;
    4.30      SDL_joysticks = joystick;
    4.31  
    4.32 -    SDL_SYS_JoystickUpdate( joystick );
    4.33 +    SDL_SYS_JoystickUpdate(joystick);
    4.34  
    4.35      return (joystick);
    4.36  }
    4.37 @@ -200,15 +199,14 @@
    4.38  {
    4.39      int valid;
    4.40  
    4.41 -    if ( joystick == NULL ) {
    4.42 +    if (joystick == NULL) {
    4.43          SDL_SetError("Joystick hasn't been opened yet");
    4.44          valid = 0;
    4.45      } else {
    4.46          valid = 1;
    4.47      }
    4.48  
    4.49 -    if ( joystick && joystick->closed )
    4.50 -    {
    4.51 +    if (joystick && joystick->closed) {
    4.52          valid = 0;
    4.53      }
    4.54  
    4.55 @@ -417,20 +415,14 @@
    4.56  
    4.57      joysticklist = SDL_joysticks;
    4.58      joysticklistprev = NULL;
    4.59 -    while ( joysticklist )
    4.60 -    {
    4.61 -        if (joystick == joysticklist)
    4.62 -        {
    4.63 -            if ( joysticklistprev )
    4.64 -            {
    4.65 +    while (joysticklist) {
    4.66 +        if (joystick == joysticklist) {
    4.67 +            if (joysticklistprev) {
    4.68                  /* unlink this entry */
    4.69                  joysticklistprev->next = joysticklist->next;
    4.70 -            }
    4.71 -            else
    4.72 -            {
    4.73 +            } else {
    4.74                  SDL_joysticks = joystick->next;
    4.75              }
    4.76 -
    4.77              break;
    4.78          }
    4.79          joysticklistprev = joysticklist;
    4.80 @@ -454,8 +446,7 @@
    4.81      SDL_assert(!SDL_updating_joystick);
    4.82  
    4.83      /* Stop the event polling */
    4.84 -    while ( SDL_joysticks )
    4.85 -    {
    4.86 +    while (SDL_joysticks) {
    4.87          SDL_joysticks->ref_count = 1;
    4.88          SDL_JoystickClose(SDL_joysticks);
    4.89      }
    4.90 @@ -472,8 +463,7 @@
    4.91  static SDL_bool
    4.92  SDL_PrivateJoystickShouldIgnoreEvent()
    4.93  {
    4.94 -    if (SDL_joystick_allows_background_events)
    4.95 -    {
    4.96 +    if (SDL_joystick_allows_background_events) {
    4.97          return SDL_FALSE;
    4.98      }
    4.99  
   4.100 @@ -497,10 +487,13 @@
   4.101  {
   4.102      int posted;
   4.103  
   4.104 -    /* Make sure we're not getting garbage events */
   4.105 +    /* Make sure we're not getting garbage or duplicate events */
   4.106      if (axis >= joystick->naxes) {
   4.107          return 0;
   4.108      }
   4.109 +    if (value == joystick->axes[axis]) {
   4.110 +        return 0;
   4.111 +    }
   4.112  
   4.113      /* We ignore events if we don't have keyboard focus, except for centering
   4.114       * events.
   4.115 @@ -513,9 +506,6 @@
   4.116      }
   4.117  
   4.118      /* Update internal joystick state */
   4.119 -    if (value == joystick->axes[axis]) {
   4.120 -        return 0;
   4.121 -    }
   4.122      joystick->axes[axis] = value;
   4.123  
   4.124      /* Post the event, if desired */
   4.125 @@ -538,10 +528,13 @@
   4.126  {
   4.127      int posted;
   4.128  
   4.129 -    /* Make sure we're not getting garbage events */
   4.130 +    /* Make sure we're not getting garbage or duplicate events */
   4.131      if (hat >= joystick->nhats) {
   4.132          return 0;
   4.133      }
   4.134 +    if (value == joystick->hats[hat]) {
   4.135 +        return 0;
   4.136 +    }
   4.137  
   4.138      /* We ignore events if we don't have keyboard focus, except for centering
   4.139       * events.
   4.140 @@ -553,9 +546,6 @@
   4.141      }
   4.142  
   4.143      /* Update internal joystick state */
   4.144 -    if (value == joystick->hats[hat]) {
   4.145 -        return 0;
   4.146 -    }
   4.147      joystick->hats[hat] = value;
   4.148  
   4.149      /* Post the event, if desired */
   4.150 @@ -629,10 +619,13 @@
   4.151      }
   4.152  #endif /* !SDL_EVENTS_DISABLED */
   4.153  
   4.154 -    /* Make sure we're not getting garbage events */
   4.155 +    /* Make sure we're not getting garbage or duplicate events */
   4.156      if (button >= joystick->nbuttons) {
   4.157          return 0;
   4.158 -    }
   4.159 +	}
   4.160 +	if (state == joystick->buttons[button]) {
   4.161 +		return 0;
   4.162 +	}
   4.163  
   4.164      /* We ignore events if we don't have keyboard focus, except for button
   4.165       * release. */
   4.166 @@ -643,9 +636,6 @@
   4.167      }
   4.168  
   4.169      /* Update internal joystick state */
   4.170 -    if (state == joystick->buttons[button]) {
   4.171 -        return 0;
   4.172 -    }
   4.173      joystick->buttons[button] = state;
   4.174  
   4.175      /* Post the event, if desired */
   4.176 @@ -667,8 +657,7 @@
   4.177      SDL_Joystick *joystick;
   4.178  
   4.179      joystick = SDL_joysticks;
   4.180 -    while ( joystick )
   4.181 -    {
   4.182 +    while (joystick) {
   4.183          SDL_Joystick *joysticknext;
   4.184          /* save off the next pointer, the Update call may cause a joystick removed event
   4.185           * and cause our joystick pointer to be freed
   4.186 @@ -677,10 +666,9 @@
   4.187  
   4.188          SDL_updating_joystick = joystick;
   4.189  
   4.190 -        SDL_SYS_JoystickUpdate( joystick );
   4.191 +        SDL_SYS_JoystickUpdate(joystick);
   4.192  
   4.193 -        if ( joystick->closed && joystick->uncentered )
   4.194 -        {
   4.195 +        if (joystick->closed && joystick->uncentered) {
   4.196              int i;
   4.197  
   4.198              /* Tell the app that everything is centered/unpressed...  */
   4.199 @@ -693,13 +681,13 @@
   4.200              for (i = 0; i < joystick->nhats; i++)
   4.201                  SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
   4.202  
   4.203 -            joystick->uncentered = 0;
   4.204 +            joystick->uncentered = SDL_FALSE;
   4.205          }
   4.206  
   4.207          SDL_updating_joystick = NULL;
   4.208  
   4.209          /* If the joystick was closed while updating, free it here */
   4.210 -        if ( joystick->ref_count <= 0 ) {
   4.211 +        if (joystick->ref_count <= 0) {
   4.212              SDL_JoystickClose(joystick);
   4.213          }
   4.214  
   4.215 @@ -750,10 +738,10 @@
   4.216      if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
   4.217          SDL_JoystickGUID emptyGUID;
   4.218          SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
   4.219 -        SDL_zero( emptyGUID );
   4.220 +        SDL_zero(emptyGUID);
   4.221          return emptyGUID;
   4.222      }
   4.223 -    return SDL_SYS_JoystickGetDeviceGUID( device_index );
   4.224 +    return SDL_SYS_JoystickGetDeviceGUID(device_index);
   4.225  }
   4.226  
   4.227  /* return the guid for this opened device */
   4.228 @@ -761,14 +749,14 @@
   4.229  {
   4.230      if (!SDL_PrivateJoystickValid(joystick)) {
   4.231          SDL_JoystickGUID emptyGUID;
   4.232 -        SDL_zero( emptyGUID );
   4.233 +        SDL_zero(emptyGUID);
   4.234          return emptyGUID;
   4.235      }
   4.236 -    return SDL_SYS_JoystickGetGUID( joystick );
   4.237 +    return SDL_SYS_JoystickGetGUID(joystick);
   4.238  }
   4.239  
   4.240  /* convert the guid to a printable string */
   4.241 -void SDL_JoystickGetGUIDString( SDL_JoystickGUID guid, char *pszGUID, int cbGUID )
   4.242 +void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
   4.243  {
   4.244      static const char k_rgchHexToASCII[] = "0123456789abcdef";
   4.245      int i;
   4.246 @@ -777,14 +765,13 @@
   4.247          return;
   4.248      }
   4.249  
   4.250 -    for ( i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++ )
   4.251 -    {
   4.252 +    for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
   4.253          /* each input byte writes 2 ascii chars, and might write a null byte. */
   4.254          /* If we don't have room for next input byte, stop */
   4.255          unsigned char c = guid.data[i];
   4.256  
   4.257 -        *pszGUID++ = k_rgchHexToASCII[ c >> 4 ];
   4.258 -        *pszGUID++ = k_rgchHexToASCII[ c & 0x0F ];
   4.259 +        *pszGUID++ = k_rgchHexToASCII[c >> 4];
   4.260 +        *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
   4.261      }
   4.262      *pszGUID = '\0';
   4.263  }
   4.264 @@ -795,28 +782,22 @@
   4.265   * Input  : c -
   4.266   * Output : unsigned char
   4.267   *-----------------------------------------------------------------------------*/
   4.268 -static unsigned char nibble( char c )
   4.269 +static unsigned char nibble(char c)
   4.270  {
   4.271 -    if ( ( c >= '0' ) &&
   4.272 -        ( c <= '9' ) )
   4.273 -    {
   4.274 +    if ((c >= '0') && (c <= '9')) {
   4.275          return (unsigned char)(c - '0');
   4.276      }
   4.277  
   4.278 -    if ( ( c >= 'A' ) &&
   4.279 -        ( c <= 'F' ) )
   4.280 -    {
   4.281 +    if ((c >= 'A') && (c <= 'F')) {
   4.282          return (unsigned char)(c - 'A' + 0x0a);
   4.283      }
   4.284  
   4.285 -    if ( ( c >= 'a' ) &&
   4.286 -        ( c <= 'f' ) )
   4.287 -    {
   4.288 +    if ((c >= 'a') && (c <= 'f')) {
   4.289          return (unsigned char)(c - 'a' + 0x0a);
   4.290      }
   4.291  
   4.292      /* received an invalid character, and no real way to return an error */
   4.293 -    /* AssertMsg1( false, "Q_nibble invalid hex character '%c' ", c ); */
   4.294 +    /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
   4.295      return 0;
   4.296  }
   4.297  
   4.298 @@ -826,21 +807,18 @@
   4.299  {
   4.300      SDL_JoystickGUID guid;
   4.301      int maxoutputbytes= sizeof(guid);
   4.302 -    size_t len = SDL_strlen( pchGUID );
   4.303 +    size_t len = SDL_strlen(pchGUID);
   4.304      Uint8 *p;
   4.305      size_t i;
   4.306  
   4.307      /* Make sure it's even */
   4.308 -    len = ( len ) & ~0x1;
   4.309 +    len = (len) & ~0x1;
   4.310  
   4.311 -    SDL_memset( &guid, 0x00, sizeof(guid) );
   4.312 +    SDL_memset(&guid, 0x00, sizeof(guid));
   4.313  
   4.314      p = (Uint8 *)&guid;
   4.315 -    for ( i = 0;
   4.316 -        ( i < len ) && ( ( p - (Uint8 *)&guid ) < maxoutputbytes );
   4.317 -        i+=2, p++ )
   4.318 -    {
   4.319 -        *p = ( nibble( pchGUID[i] ) << 4 ) | nibble( pchGUID[i+1] );
   4.320 +    for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
   4.321 +        *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
   4.322      }
   4.323  
   4.324      return guid;
     5.1 --- a/src/joystick/SDL_sysjoystick.h	Tue Jun 24 01:38:21 2014 -0700
     5.2 +++ b/src/joystick/SDL_sysjoystick.h	Tue Jun 24 13:31:25 2014 -0700
     5.3 @@ -50,8 +50,8 @@
     5.4  
     5.5      int ref_count;              /* Reference count for multiple opens */
     5.6  
     5.7 -    Uint8 closed;               /* 1 if this device is no longer valid */
     5.8 -    Uint8 uncentered;           /* 1 if this device needs to have its state reset to 0 */
     5.9 +    SDL_bool closed;            /* SDL_TRUE if this device is no longer valid */
    5.10 +    SDL_bool uncentered;        /* SDL_TRUE if this device needs to have its state reset to 0 */
    5.11      struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */
    5.12  };
    5.13  
    5.14 @@ -106,9 +106,8 @@
    5.15  extern SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick);
    5.16  
    5.17  #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)
    5.18 -/* Function to get the current instance id of the joystick located at device_index */
    5.19 -extern SDL_bool SDL_SYS_IsXInputDeviceIndex( int device_index );
    5.20 -extern SDL_bool SDL_SYS_IsXInputJoystick(SDL_Joystick * joystick);
    5.21 +/* Function returns SDL_TRUE if this device is an XInput gamepad */
    5.22 +extern SDL_bool SDL_SYS_IsXInputGamepad_DeviceIndex( int device_index );
    5.23  #endif
    5.24  
    5.25  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/joystick/windows/SDL_dxjoystick.c	Tue Jun 24 01:38:21 2014 -0700
     6.2 +++ b/src/joystick/windows/SDL_dxjoystick.c	Tue Jun 24 13:31:25 2014 -0700
     6.3 @@ -102,13 +102,13 @@
     6.4      }
     6.5  
     6.6      version = (1 << 16) | 4;
     6.7 -    s_pXInputDLL = LoadLibrary( L"XInput1_4.dll" );  /* 1.4 Ships with Windows 8. */
     6.8 +    s_pXInputDLL = LoadLibrary(L"XInput1_4.dll");  /* 1.4 Ships with Windows 8. */
     6.9      if (!s_pXInputDLL) {
    6.10          version = (1 << 16) | 3;
    6.11 -        s_pXInputDLL = LoadLibrary( L"XInput1_3.dll" );  /* 1.3 Ships with Vista and Win7, can be installed as a redistributable component. */
    6.12 +        s_pXInputDLL = LoadLibrary(L"XInput1_3.dll");  /* 1.3 Ships with Vista and Win7, can be installed as a redistributable component. */
    6.13      }
    6.14      if (!s_pXInputDLL) {
    6.15 -        s_pXInputDLL = LoadLibrary( L"bin\\XInput1_3.dll" );
    6.16 +        s_pXInputDLL = LoadLibrary(L"bin\\XInput1_3.dll");
    6.17      }
    6.18      if (!s_pXInputDLL) {
    6.19          return -1;
    6.20 @@ -119,10 +119,10 @@
    6.21      s_XInputDLLRefCount = 1;
    6.22  
    6.23      /* 100 is the ordinal for _XInputGetStateEx, which returns the same struct as XinputGetState, but with extra data in wButtons for the guide button, we think... */
    6.24 -    SDL_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, (LPCSTR)100 );
    6.25 -    SDL_XInputSetState = (XInputSetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputSetState" );
    6.26 -    SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetCapabilities" );
    6.27 -    if ( !SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities ) {
    6.28 +    SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
    6.29 +    SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
    6.30 +    SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
    6.31 +    if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
    6.32          WIN_UnloadXInputDLL();
    6.33          return -1;
    6.34      }
    6.35 @@ -133,10 +133,10 @@
    6.36  void
    6.37  WIN_UnloadXInputDLL(void)
    6.38  {
    6.39 -    if ( s_pXInputDLL ) {
    6.40 +    if (s_pXInputDLL) {
    6.41          SDL_assert(s_XInputDLLRefCount > 0);
    6.42          if (--s_XInputDLLRefCount == 0) {
    6.43 -            FreeLibrary( s_pXInputDLL );
    6.44 +            FreeLibrary(s_pXInputDLL);
    6.45              s_pXInputDLL = NULL;
    6.46          }
    6.47      } else {
    6.48 @@ -156,6 +156,7 @@
    6.49      Uint8 send_add_event;
    6.50      SDL_JoystickID nInstanceID;
    6.51      SDL_bool bXInputDevice;
    6.52 +    BYTE SubType;
    6.53      Uint8 XInputUserId;
    6.54      struct JoyStick_DeviceData_ *pNext;
    6.55  };
    6.56 @@ -172,12 +173,6 @@
    6.57                                              LPVOID pvRef);
    6.58  static void SortDevObjects(SDL_Joystick *joystick);
    6.59  static Uint8 TranslatePOV(DWORD value);
    6.60 -static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
    6.61 -                                       Sint16 value);
    6.62 -static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
    6.63 -                                      Uint8 value);
    6.64 -static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
    6.65 -                                         Uint8 button, Uint8 state);
    6.66  
    6.67  /* Taken from Wine - Thanks! */
    6.68  DIOBJECTDATAFORMAT dfDIJoystick2[] = {
    6.69 @@ -377,15 +372,26 @@
    6.70      }                                               \
    6.71  }
    6.72  
    6.73 -DEFINE_GUID(IID_ValveStreamingGamepad,  MAKELONG( 0x28DE, 0x11FF ),0x0000,0x0000,0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44);
    6.74 -DEFINE_GUID(IID_X360WiredGamepad,  MAKELONG( 0x045E, 0x02A1 ),0x0000,0x0000,0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44);
    6.75 -DEFINE_GUID(IID_X360WirelessGamepad,  MAKELONG( 0x045E, 0x028E ),0x0000,0x0000,0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44);
    6.76 +DEFINE_GUID(IID_ValveStreamingGamepad,  MAKELONG(0x28DE, 0x11FF),0x0000,0x0000,0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44);
    6.77 +DEFINE_GUID(IID_X360WiredGamepad,  MAKELONG(0x045E, 0x02A1),0x0000,0x0000,0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44);
    6.78 +DEFINE_GUID(IID_X360WirelessGamepad,  MAKELONG(0x045E, 0x028E),0x0000,0x0000,0x00,0x00,0x50,0x49,0x44,0x56,0x49,0x44);
    6.79  
    6.80  static PRAWINPUTDEVICELIST SDL_RawDevList = NULL;
    6.81  static UINT SDL_RawDevListCount = 0;
    6.82  
    6.83  static SDL_bool
    6.84 -SDL_IsXInputDevice( const GUID* pGuidProductFromDirectInput )
    6.85 +SDL_XInputUseOldJoystickMapping()
    6.86 +{
    6.87 +	static int s_XInputUseOldJoystickMapping = -1;
    6.88 +	if (s_XInputUseOldJoystickMapping < 0) {
    6.89 +		const char *hint = SDL_GetHint(SDL_HINT_XINPUT_USE_OLD_JOYSTICK_MAPPING);
    6.90 +		s_XInputUseOldJoystickMapping = (hint && *hint == '1') ? 1 : 0;
    6.91 +	}
    6.92 +	return (s_XInputUseOldJoystickMapping > 0);
    6.93 +}
    6.94 +
    6.95 +static SDL_bool
    6.96 +SDL_IsXInputDevice(const GUID* pGuidProductFromDirectInput)
    6.97  {
    6.98      static const GUID *s_XInputProductGUID[] = {
    6.99          &IID_ValveStreamingGamepad,
   6.100 @@ -402,7 +408,7 @@
   6.101  
   6.102      /* Check for well known XInput device GUIDs */
   6.103      /* This lets us skip RAWINPUT for popular devices. Also, we need to do this for the Valve Streaming Gamepad because it's virtualized and doesn't show up in the device list. */
   6.104 -    for ( iDevice = 0; iDevice < SDL_arraysize(s_XInputProductGUID); ++iDevice ) {
   6.105 +    for (iDevice = 0; iDevice < SDL_arraysize(s_XInputProductGUID); ++iDevice) {
   6.106          if (SDL_memcmp(pGuidProductFromDirectInput, s_XInputProductGUID[iDevice], sizeof(GUID)) == 0) {
   6.107              return SDL_TRUE;
   6.108          }
   6.109 @@ -435,11 +441,11 @@
   6.110          UINT nameSize = SDL_arraysize(devName);
   6.111  
   6.112          rdi.cbSize = sizeof (rdi);
   6.113 -        if ( (SDL_RawDevList[i].dwType == RIM_TYPEHID) &&
   6.114 +        if ((SDL_RawDevList[i].dwType == RIM_TYPEHID) &&
   6.115               (GetRawInputDeviceInfoA(SDL_RawDevList[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != ((UINT)-1)) &&
   6.116               (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == ((LONG)pGuidProductFromDirectInput->Data1)) &&
   6.117               (GetRawInputDeviceInfoA(SDL_RawDevList[i].hDevice, RIDI_DEVICENAME, devName, &nameSize) != ((UINT)-1)) &&
   6.118 -             (SDL_strstr(devName, "IG_") != NULL) ) {
   6.119 +             (SDL_strstr(devName, "IG_") != NULL)) {
   6.120               return SDL_TRUE;
   6.121          }
   6.122      }
   6.123 @@ -452,17 +458,18 @@
   6.124  
   6.125  /* windowproc for our joystick detect thread message only window, to detect any USB device addition/removal
   6.126   */
   6.127 -LRESULT CALLBACK SDL_PrivateJoystickDetectProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)    {
   6.128 -    switch (message)    {
   6.129 +LRESULT CALLBACK SDL_PrivateJoystickDetectProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
   6.130 +{
   6.131 +    switch (message) {
   6.132      case WM_DEVICECHANGE:
   6.133          switch (wParam) {
   6.134          case DBT_DEVICEARRIVAL:
   6.135 -            if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)    {
   6.136 +            if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
   6.137                  s_bWindowsDeviceChanged = SDL_TRUE;
   6.138              }
   6.139              break;
   6.140          case DBT_DEVICEREMOVECOMPLETE:
   6.141 -            if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)    {
   6.142 +            if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
   6.143                  s_bWindowsDeviceChanged = SDL_TRUE;
   6.144              }
   6.145              break;
   6.146 @@ -492,20 +499,18 @@
   6.147  
   6.148      WIN_CoInitialize();
   6.149  
   6.150 -    SDL_memset( &wincl, 0x0, sizeof(wincl) );
   6.151 -    wincl.hInstance = GetModuleHandle( NULL );
   6.152 +    SDL_memset(&wincl, 0x0, sizeof(wincl));
   6.153 +    wincl.hInstance = GetModuleHandle(NULL);
   6.154      wincl.lpszClassName = L"Message";
   6.155      wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc;      /* This function is called by windows */
   6.156      wincl.cbSize = sizeof (WNDCLASSEX);
   6.157  
   6.158 -    if (!RegisterClassEx (&wincl))
   6.159 -    {
   6.160 -		return WIN_SetError( "Failed to create register class for joystick autodetect");
   6.161 +    if (!RegisterClassEx (&wincl)) {
   6.162 +        return WIN_SetError("Failed to create register class for joystick autodetect");
   6.163      }
   6.164  
   6.165 -    messageWindow = (HWND)CreateWindowEx( 0,  L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL );
   6.166 -    if ( !messageWindow )
   6.167 -    {
   6.168 +    messageWindow = (HWND)CreateWindowEx(0,  L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
   6.169 +    if (!messageWindow) {
   6.170          return WIN_SetError("Failed to create message window for joystick autodetect");
   6.171      }
   6.172  
   6.173 @@ -515,34 +520,31 @@
   6.174      dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
   6.175      dbh.dbcc_classguid = GUID_DEVINTERFACE_HID;
   6.176  
   6.177 -    hNotify = RegisterDeviceNotification( messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE );
   6.178 -    if ( !hNotify )
   6.179 -    {
   6.180 -		return WIN_SetError( "Failed to create notify device for joystick autodetect");
   6.181 +    hNotify = RegisterDeviceNotification(messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE);
   6.182 +    if (!hNotify) {
   6.183 +        return WIN_SetError("Failed to create notify device for joystick autodetect");
   6.184      }
   6.185  
   6.186 -    SDL_LockMutex( s_mutexJoyStickEnum );
   6.187 -    while ( s_bJoystickThreadQuit == SDL_FALSE )
   6.188 -    {
   6.189 +    SDL_LockMutex(s_mutexJoyStickEnum);
   6.190 +    while (s_bJoystickThreadQuit == SDL_FALSE) {
   6.191          MSG messages;
   6.192          SDL_bool bXInputChanged = SDL_FALSE;
   6.193  
   6.194 -        SDL_CondWaitTimeout( s_condJoystickThread, s_mutexJoyStickEnum, 300 );
   6.195 +        SDL_CondWaitTimeout(s_condJoystickThread, s_mutexJoyStickEnum, 300);
   6.196  
   6.197 -        while ( s_bJoystickThreadQuit == SDL_FALSE && PeekMessage(&messages, messageWindow, 0, 0, PM_NOREMOVE) )
   6.198 -        {
   6.199 -            if ( GetMessage(&messages, messageWindow, 0, 0) != 0 )  {
   6.200 +        while (s_bJoystickThreadQuit == SDL_FALSE && PeekMessage(&messages, messageWindow, 0, 0, PM_NOREMOVE)) {
   6.201 +            if (GetMessage(&messages, messageWindow, 0, 0) != 0)  {
   6.202                  TranslateMessage(&messages);
   6.203                  DispatchMessage(&messages);
   6.204              }
   6.205          }
   6.206  
   6.207 -        if ( s_bXInputEnabled && XINPUTGETCAPABILITIES ) {
   6.208 +        if (s_bXInputEnabled && XINPUTGETCAPABILITIES) {
   6.209              /* scan for any change in XInput devices */
   6.210              Uint8 userId;
   6.211              for (userId = 0; userId < SDL_XINPUT_MAX_DEVICES; userId++) {
   6.212                  XINPUT_CAPABILITIES capabilities;
   6.213 -                const DWORD result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities );
   6.214 +                const DWORD result = XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities);
   6.215                  const SDL_bool available = (result == ERROR_SUCCESS);
   6.216                  if (bOpenedXInputDevices[userId] != available) {
   6.217                      bXInputChanged = SDL_TRUE;
   6.218 @@ -552,24 +554,24 @@
   6.219          }
   6.220  
   6.221          if (s_bWindowsDeviceChanged || bXInputChanged) {
   6.222 -            SDL_UnlockMutex( s_mutexJoyStickEnum );  /* let main thread go while we SDL_Delay(). */
   6.223 -            SDL_Delay( 300 ); /* wait for direct input to find out about this device */
   6.224 -            SDL_LockMutex( s_mutexJoyStickEnum );
   6.225 +            SDL_UnlockMutex(s_mutexJoyStickEnum);  /* let main thread go while we SDL_Delay(). */
   6.226 +            SDL_Delay(300); /* wait for direct input to find out about this device */
   6.227 +            SDL_LockMutex(s_mutexJoyStickEnum);
   6.228  
   6.229              s_bDeviceRemoved = SDL_TRUE;
   6.230              s_bDeviceAdded = SDL_TRUE;
   6.231              s_bWindowsDeviceChanged = SDL_FALSE;
   6.232          }
   6.233      }
   6.234 -    SDL_UnlockMutex( s_mutexJoyStickEnum );
   6.235 +    SDL_UnlockMutex(s_mutexJoyStickEnum);
   6.236  
   6.237 -    if ( hNotify )
   6.238 -        UnregisterDeviceNotification( hNotify );
   6.239 +    if (hNotify)
   6.240 +        UnregisterDeviceNotification(hNotify);
   6.241  
   6.242 -    if ( messageWindow )
   6.243 -        DestroyWindow( messageWindow );
   6.244 +    if (messageWindow)
   6.245 +        DestroyWindow(messageWindow);
   6.246  
   6.247 -    UnregisterClass( wincl.lpszClassName, wincl.hInstance );
   6.248 +    UnregisterClass(wincl.lpszClassName, wincl.hInstance);
   6.249      messageWindow = 0;
   6.250      WIN_CoUninitialize();
   6.251      return 1;
   6.252 @@ -629,19 +631,18 @@
   6.253  
   6.254      SDL_SYS_JoystickDetect();
   6.255  
   6.256 -    if ( !s_threadJoystick )
   6.257 -    {
   6.258 +    if (!s_threadJoystick) {
   6.259          s_bJoystickThreadQuit = SDL_FALSE;
   6.260          /* spin up the thread to detect hotplug of devices */
   6.261  #if defined(__WIN32__) && !defined(HAVE_LIBC)
   6.262  #undef SDL_CreateThread
   6.263  #if SDL_DYNAMIC_API
   6.264 -        s_threadJoystick= SDL_CreateThread_REAL( SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL );
   6.265 +        s_threadJoystick= SDL_CreateThread_REAL(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
   6.266  #else
   6.267 -        s_threadJoystick= SDL_CreateThread( SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL );
   6.268 +        s_threadJoystick= SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
   6.269  #endif
   6.270  #else
   6.271 -        s_threadJoystick = SDL_CreateThread( SDL_JoystickThread, "SDL_joystick", NULL );
   6.272 +        s_threadJoystick = SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL);
   6.273  #endif
   6.274      }
   6.275          return SDL_SYS_NumJoysticks();
   6.276 @@ -652,8 +653,7 @@
   6.277  {
   6.278      int nJoysticks = 0;
   6.279      JoyStick_DeviceData *device = SYS_Joystick;
   6.280 -    while ( device )
   6.281 -    {
   6.282 +    while (device) {
   6.283          nJoysticks++;
   6.284          device = device->pNext;
   6.285      }
   6.286 @@ -663,27 +663,22 @@
   6.287  
   6.288  /* helper function for direct input, gets called for each connected joystick */
   6.289  static BOOL CALLBACK
   6.290 -    EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
   6.291 +EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
   6.292  {
   6.293      JoyStick_DeviceData *pNewJoystick;
   6.294      JoyStick_DeviceData *pPrevJoystick = NULL;
   6.295  
   6.296 -    if (SDL_IsXInputDevice( &pdidInstance->guidProduct )) {
   6.297 +    if (SDL_IsXInputDevice(&pdidInstance->guidProduct)) {
   6.298          return DIENUM_CONTINUE;  /* ignore XInput devices here, keep going. */
   6.299      }
   6.300  
   6.301      pNewJoystick = *(JoyStick_DeviceData **)pContext;
   6.302 -    while ( pNewJoystick )
   6.303 -    {
   6.304 -        if ( !SDL_memcmp( &pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance) ) )
   6.305 -        {
   6.306 +    while (pNewJoystick) {
   6.307 +        if (!SDL_memcmp(&pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance))) {
   6.308              /* if we are replacing the front of the list then update it */
   6.309 -            if ( pNewJoystick == *(JoyStick_DeviceData **)pContext )
   6.310 -            {
   6.311 +            if (pNewJoystick == *(JoyStick_DeviceData **)pContext) {
   6.312                  *(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
   6.313 -            }
   6.314 -            else if ( pPrevJoystick )
   6.315 -            {
   6.316 +            } else if (pPrevJoystick) {
   6.317                  pPrevJoystick->pNext = pNewJoystick->pNext;
   6.318              }
   6.319  
   6.320 @@ -697,7 +692,7 @@
   6.321          pNewJoystick = pNewJoystick->pNext;
   6.322      }
   6.323  
   6.324 -    pNewJoystick = (JoyStick_DeviceData *)SDL_malloc( sizeof(JoyStick_DeviceData) );
   6.325 +    pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData));
   6.326      if (!pNewJoystick) {
   6.327          return DIENUM_CONTINUE; /* better luck next time? */
   6.328      }
   6.329 @@ -713,9 +708,9 @@
   6.330          sizeof(DIDEVICEINSTANCE));
   6.331  
   6.332      pNewJoystick->XInputUserId = INVALID_XINPUT_USERID;
   6.333 -    pNewJoystick->send_add_event = 1;
   6.334 +    pNewJoystick->send_add_event = SDL_TRUE;
   6.335      pNewJoystick->nInstanceID = ++s_nInstanceID;
   6.336 -    SDL_memcpy( &pNewJoystick->guid, &pdidInstance->guidProduct, sizeof(pNewJoystick->guid) );
   6.337 +    SDL_memcpy(&pNewJoystick->guid, &pdidInstance->guidProduct, sizeof(pNewJoystick->guid));
   6.338      pNewJoystick->pNext = SYS_Joystick;
   6.339      SYS_Joystick = pNewJoystick;
   6.340  
   6.341 @@ -724,15 +719,63 @@
   6.342      return DIENUM_CONTINUE; /* get next device, please */
   6.343  }
   6.344  
   6.345 -static void
   6.346 -AddXInputDevice(const Uint8 userid, JoyStick_DeviceData **pContext)
   6.347 +static char *
   6.348 +GetXInputName(const Uint8 userid, BYTE SubType)
   6.349  {
   6.350      char name[32];
   6.351 +
   6.352 +	if (SDL_XInputUseOldJoystickMapping()) {
   6.353 +		SDL_snprintf(name, sizeof(name), "X360 Controller #%u", 1 + userid);
   6.354 +	} else {
   6.355 +		switch (SubType) {
   6.356 +		case XINPUT_DEVSUBTYPE_GAMEPAD:
   6.357 +			SDL_snprintf(name, sizeof(name), "XInput Controller #%u", 1 + userid);
   6.358 +			break;
   6.359 +		case XINPUT_DEVSUBTYPE_WHEEL:
   6.360 +			SDL_snprintf(name, sizeof(name), "XInput Wheel #%u", 1 + userid);
   6.361 +			break;
   6.362 +		case XINPUT_DEVSUBTYPE_ARCADE_STICK:
   6.363 +			SDL_snprintf(name, sizeof(name), "XInput ArcadeStick #%u", 1 + userid);
   6.364 +			break;
   6.365 +		case XINPUT_DEVSUBTYPE_FLIGHT_STICK:
   6.366 +			SDL_snprintf(name, sizeof(name), "XInput FlightStick #%u", 1 + userid);
   6.367 +			break;
   6.368 +		case XINPUT_DEVSUBTYPE_DANCE_PAD:
   6.369 +			SDL_snprintf(name, sizeof(name), "XInput DancePad #%u", 1 + userid);
   6.370 +			break;
   6.371 +		case XINPUT_DEVSUBTYPE_GUITAR:
   6.372 +		case XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE:
   6.373 +		case XINPUT_DEVSUBTYPE_GUITAR_BASS:
   6.374 +			SDL_snprintf(name, sizeof(name), "XInput Guitar #%u", 1 + userid);
   6.375 +			break;
   6.376 +		case XINPUT_DEVSUBTYPE_DRUM_KIT:
   6.377 +			SDL_snprintf(name, sizeof(name), "XInput DrumKit #%u", 1 + userid);
   6.378 +			break;
   6.379 +		case XINPUT_DEVSUBTYPE_ARCADE_PAD:
   6.380 +			SDL_snprintf(name, sizeof(name), "XInput ArcadePad #%u", 1 + userid);
   6.381 +			break;
   6.382 +		default:
   6.383 +			SDL_snprintf(name, sizeof(name), "XInput Device #%u", 1 + userid);
   6.384 +			break;
   6.385 +		}
   6.386 +	}
   6.387 +    return SDL_strdup(name);
   6.388 +}
   6.389 +
   6.390 +static void
   6.391 +AddXInputDevice(const Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
   6.392 +{
   6.393      JoyStick_DeviceData *pPrevJoystick = NULL;
   6.394 -    JoyStick_DeviceData *pNewJoystick = *pContext;
   6.395 +	JoyStick_DeviceData *pNewJoystick = *pContext;
   6.396 +
   6.397 +	if (SDL_XInputUseOldJoystickMapping() && SubType != XINPUT_DEVSUBTYPE_GAMEPAD)
   6.398 +		return;
   6.399 +
   6.400 +    if (SubType == XINPUT_DEVSUBTYPE_UNKNOWN)
   6.401 +		return;
   6.402  
   6.403      while (pNewJoystick) {
   6.404 -        if ((pNewJoystick->bXInputDevice) && (pNewJoystick->XInputUserId == userid)) {
   6.405 +        if (pNewJoystick->bXInputDevice && (pNewJoystick->XInputUserId == userid) && (pNewJoystick->SubType == SubType)) {
   6.406              /* if we are replacing the front of the list then update it */
   6.407              if (pNewJoystick == *pContext) {
   6.408                  *pContext = pNewJoystick->pNext;
   6.409 @@ -755,16 +798,27 @@
   6.410      }
   6.411      SDL_zerop(pNewJoystick);
   6.412  
   6.413 -    SDL_snprintf(name, sizeof (name), "XInput Controller #%u", ((unsigned int) userid) + 1);
   6.414 -    pNewJoystick->joystickname = SDL_strdup(name);
   6.415 +    pNewJoystick->joystickname = GetXInputName(userid,SubType);
   6.416      if (!pNewJoystick->joystickname) {
   6.417          SDL_free(pNewJoystick);
   6.418          return; /* better luck next time? */
   6.419      }
   6.420  
   6.421      pNewJoystick->bXInputDevice = SDL_TRUE;
   6.422 -    pNewJoystick->XInputUserId = userid;
   6.423 -    pNewJoystick->send_add_event = 1;
   6.424 +	if (SDL_XInputUseOldJoystickMapping()) {
   6.425 +		SDL_zero(pNewJoystick->guid);
   6.426 +	} else {
   6.427 +		pNewJoystick->guid.data[0] = 'x';
   6.428 +		pNewJoystick->guid.data[1] = 'i';
   6.429 +		pNewJoystick->guid.data[2] = 'n';
   6.430 +		pNewJoystick->guid.data[3] = 'p';
   6.431 +		pNewJoystick->guid.data[4] = 'u';
   6.432 +		pNewJoystick->guid.data[5] = 't';
   6.433 +		pNewJoystick->guid.data[6] = SubType;
   6.434 +	}
   6.435 +	pNewJoystick->SubType = SubType;
   6.436 +	pNewJoystick->XInputUserId = userid;
   6.437 +    pNewJoystick->send_add_event = SDL_TRUE;
   6.438      pNewJoystick->nInstanceID = ++s_nInstanceID;
   6.439      pNewJoystick->pNext = SYS_Joystick;
   6.440      SYS_Joystick = pNewJoystick;
   6.441 @@ -782,11 +836,7 @@
   6.442              const Uint8 userid = (Uint8) iuserid;
   6.443              XINPUT_CAPABILITIES capabilities;
   6.444              if (XINPUTGETCAPABILITIES(userid, XINPUT_FLAG_GAMEPAD, &capabilities) == ERROR_SUCCESS) {
   6.445 -                /* Current version of XInput mistakenly returns 0 as the Type. Ignore it and ensure the subtype is a gamepad. */
   6.446 -                /* !!! FIXME: we might want to support steering wheels or guitars or whatever later. */
   6.447 -                if (capabilities.SubType == XINPUT_DEVSUBTYPE_GAMEPAD) {
   6.448 -                    AddXInputDevice(userid, pContext);
   6.449 -                }
   6.450 +                AddXInputDevice(userid, capabilities.SubType, pContext);
   6.451              }
   6.452          }
   6.453      }
   6.454 @@ -881,7 +931,7 @@
   6.455                      }
   6.456                  }
   6.457  #endif /* !SDL_EVENTS_DISABLED */
   6.458 -                pNewJoystick->send_add_event = 0;
   6.459 +                pNewJoystick->send_add_event = SDL_FALSE;
   6.460              }
   6.461              device_index++;
   6.462              pNewJoystick = pNewJoystick->pNext;
   6.463 @@ -929,13 +979,14 @@
   6.464  
   6.465      /* allocate memory for system specific hardware data */
   6.466      joystick->instance_id = joystickdevice->nInstanceID;
   6.467 -    joystick->closed = 0;
   6.468 +    joystick->closed = SDL_FALSE;
   6.469      joystick->hwdata =
   6.470          (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
   6.471      if (joystick->hwdata == NULL) {
   6.472          return SDL_OutOfMemory();
   6.473      }
   6.474      SDL_zerop(joystick->hwdata);
   6.475 +    joystick->hwdata->guid = joystickdevice->guid;
   6.476  
   6.477      if (joystickdevice->bXInputDevice) {
   6.478          const Uint8 userId = joystickdevice->XInputUserId;
   6.479 @@ -954,27 +1005,26 @@
   6.480              SDL_free(joystick->hwdata);
   6.481              joystick->hwdata = NULL;
   6.482              return SDL_SetError("Failed to obtain XInput device capabilities. Device disconnected?");
   6.483 -        } else {
   6.484 -            /* Current version of XInput mistakenly returns 0 as the Type. Ignore it and ensure the subtype is a gamepad. */
   6.485 -            SDL_assert(capabilities.SubType == XINPUT_DEVSUBTYPE_GAMEPAD);
   6.486 -            SDL_zero(state);
   6.487 -            joystick->hwdata->bXInputHaptic = (XINPUTSETSTATE(userId, &state) == ERROR_SUCCESS);
   6.488 -            joystick->hwdata->userid = userId;
   6.489 +        }
   6.490 +        SDL_zero(state);
   6.491 +        joystick->hwdata->bXInputHaptic = (XINPUTSETSTATE(userId, &state) == ERROR_SUCCESS);
   6.492 +        joystick->hwdata->userid = userId;
   6.493  
   6.494 -            /* The XInput API has a hard coded button/axis mapping, so we just match it */
   6.495 -            joystick->naxes = 6;
   6.496 -            joystick->nbuttons = 15;
   6.497 -            joystick->nballs = 0;
   6.498 -            joystick->nhats = 0;
   6.499 -        }
   6.500 +        /* The XInput API has a hard coded button/axis mapping, so we just match it */
   6.501 +		if (SDL_XInputUseOldJoystickMapping()) {
   6.502 +			joystick->naxes = 6;
   6.503 +			joystick->nbuttons = 15;
   6.504 +		} else {
   6.505 +			joystick->naxes = 6;
   6.506 +			joystick->nbuttons = 11;
   6.507 +			joystick->nhats = 1;
   6.508 +		}
   6.509      } else {  /* use DirectInput, not XInput. */
   6.510          LPDIRECTINPUTDEVICE8 device;
   6.511          DIPROPDWORD dipdw;
   6.512  
   6.513          joystick->hwdata->buffered = 1;
   6.514 -        joystick->hwdata->removed = 0;
   6.515          joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
   6.516 -        joystick->hwdata->guid = joystickdevice->guid;
   6.517  
   6.518          SDL_zero(dipdw);
   6.519          dipdw.diph.dwSize = sizeof(DIPROPDWORD);
   6.520 @@ -1036,7 +1086,7 @@
   6.521                  return SetDIerror("IDirectInputDevice8::Acquire", result);
   6.522              }
   6.523  
   6.524 -            /* reset all accuators. */
   6.525 +            /* reset all actuators. */
   6.526              result =
   6.527                  IDirectInputDevice8_SendForceFeedbackCommand(joystick->hwdata->
   6.528                                                               InputDevice,
   6.529 @@ -1101,9 +1151,9 @@
   6.530  }
   6.531  
   6.532  /* return true if this joystick is plugged in right now */
   6.533 -SDL_bool SDL_SYS_JoystickAttached( SDL_Joystick * joystick )
   6.534 +SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
   6.535  {
   6.536 -    return joystick->closed == 0 && joystick->hwdata->removed == 0;
   6.537 +    return !joystick->closed && !joystick->hwdata->removed;
   6.538  }
   6.539  
   6.540  
   6.541 @@ -1134,10 +1184,8 @@
   6.542  
   6.543      SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc);
   6.544  
   6.545 -    for (n = 0; n < joystick->hwdata->NumInputs; n++)
   6.546 -    {
   6.547 -        switch (inputs[n].type)
   6.548 -        {
   6.549 +    for (n = 0; n < joystick->hwdata->NumInputs; n++) {
   6.550 +        switch (inputs[n].type) {
   6.551          case BUTTON:
   6.552              inputs[n].num = nButtons;
   6.553              nButtons++;
   6.554 @@ -1166,12 +1214,12 @@
   6.555      if (dev->dwType & DIDFT_BUTTON) {
   6.556          in->type = BUTTON;
   6.557          in->num = joystick->nbuttons;
   6.558 -        in->ofs = DIJOFS_BUTTON( in->num );
   6.559 +        in->ofs = DIJOFS_BUTTON(in->num);
   6.560          joystick->nbuttons++;
   6.561      } else if (dev->dwType & DIDFT_POV) {
   6.562          in->type = HAT;
   6.563          in->num = joystick->nhats;
   6.564 -        in->ofs = DIJOFS_POV( in->num );
   6.565 +        in->ofs = DIJOFS_POV(in->num);
   6.566          joystick->nhats++;
   6.567      } else if (dev->dwType & DIDFT_AXIS) {
   6.568          DIPROPRANGE diprg;
   6.569 @@ -1179,26 +1227,22 @@
   6.570  
   6.571          in->type = AXIS;
   6.572          in->num = joystick->naxes;
   6.573 -        /* work our the axis this guy maps too, thanks for the code icculus! */
   6.574 -        if ( !SDL_memcmp( &dev->guidType, &GUID_XAxis, sizeof(dev->guidType) ) )
   6.575 +        if (!SDL_memcmp(&dev->guidType, &GUID_XAxis, sizeof(dev->guidType)))
   6.576              in->ofs = DIJOFS_X;
   6.577 -        else if ( !SDL_memcmp( &dev->guidType, &GUID_YAxis, sizeof(dev->guidType) ) )
   6.578 +        else if (!SDL_memcmp(&dev->guidType, &GUID_YAxis, sizeof(dev->guidType)))
   6.579              in->ofs = DIJOFS_Y;
   6.580 -        else if ( !SDL_memcmp( &dev->guidType, &GUID_ZAxis, sizeof(dev->guidType) ) )
   6.581 +        else if (!SDL_memcmp(&dev->guidType, &GUID_ZAxis, sizeof(dev->guidType)))
   6.582              in->ofs = DIJOFS_Z;
   6.583 -        else if ( !SDL_memcmp( &dev->guidType, &GUID_RxAxis, sizeof(dev->guidType) ) )
   6.584 +        else if (!SDL_memcmp(&dev->guidType, &GUID_RxAxis, sizeof(dev->guidType)))
   6.585              in->ofs = DIJOFS_RX;
   6.586 -        else if ( !SDL_memcmp( &dev->guidType, &GUID_RyAxis, sizeof(dev->guidType) ) )
   6.587 +        else if (!SDL_memcmp(&dev->guidType, &GUID_RyAxis, sizeof(dev->guidType)))
   6.588              in->ofs = DIJOFS_RY;
   6.589 -        else if ( !SDL_memcmp( &dev->guidType, &GUID_RzAxis, sizeof(dev->guidType) ) )
   6.590 +        else if (!SDL_memcmp(&dev->guidType, &GUID_RzAxis, sizeof(dev->guidType)))
   6.591              in->ofs = DIJOFS_RZ;
   6.592 -        else if ( !SDL_memcmp( &dev->guidType, &GUID_Slider, sizeof(dev->guidType) ) )
   6.593 -        {
   6.594 -            in->ofs = DIJOFS_SLIDER( joystick->hwdata->NumSliders );
   6.595 +        else if (!SDL_memcmp(&dev->guidType, &GUID_Slider, sizeof(dev->guidType))) {
   6.596 +            in->ofs = DIJOFS_SLIDER(joystick->hwdata->NumSliders);
   6.597              ++joystick->hwdata->NumSliders;
   6.598 -        }
   6.599 -        else
   6.600 -        {
   6.601 +        } else {
   6.602               return DIENUM_CONTINUE; /* not an axis we can grok */
   6.603          }
   6.604  
   6.605 @@ -1266,10 +1310,9 @@
   6.606                                                 sizeof(DIJOYSTATE2), &state);
   6.607      }
   6.608  
   6.609 -    if ( result != DI_OK )
   6.610 -    {
   6.611 -        joystick->hwdata->send_remove_event = 1;
   6.612 -        joystick->hwdata->removed = 1;
   6.613 +    if (result != DI_OK) {
   6.614 +        joystick->hwdata->send_remove_event = SDL_TRUE;
   6.615 +        joystick->hwdata->removed = SDL_TRUE;
   6.616          return;
   6.617      }
   6.618  
   6.619 @@ -1281,54 +1324,40 @@
   6.620          case AXIS:
   6.621              switch (in->ofs) {
   6.622              case DIJOFS_X:
   6.623 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.624 -                                            (Sint16) state.lX);
   6.625 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.lX);
   6.626                  break;
   6.627              case DIJOFS_Y:
   6.628 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.629 -                                            (Sint16) state.lY);
   6.630 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.lY);
   6.631                  break;
   6.632              case DIJOFS_Z:
   6.633 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.634 -                                            (Sint16) state.lZ);
   6.635 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.lZ);
   6.636                  break;
   6.637              case DIJOFS_RX:
   6.638 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.639 -                                            (Sint16) state.lRx);
   6.640 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.lRx);
   6.641                  break;
   6.642              case DIJOFS_RY:
   6.643 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.644 -                                            (Sint16) state.lRy);
   6.645 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.lRy);
   6.646                  break;
   6.647              case DIJOFS_RZ:
   6.648 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.649 -                                            (Sint16) state.lRz);
   6.650 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.lRz);
   6.651                  break;
   6.652              case DIJOFS_SLIDER(0):
   6.653 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.654 -                                            (Sint16) state.rglSlider[0]);
   6.655 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.rglSlider[0]);
   6.656                  break;
   6.657              case DIJOFS_SLIDER(1):
   6.658 -                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   6.659 -                                            (Sint16) state.rglSlider[1]);
   6.660 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) state.rglSlider[1]);
   6.661                  break;
   6.662              }
   6.663 -
   6.664              break;
   6.665  
   6.666          case BUTTON:
   6.667 -            SDL_PrivateJoystickButton_Int(joystick, in->num,
   6.668 -                                          (Uint8) (state.
   6.669 -                                                   rgbButtons[in->ofs -
   6.670 -                                                              DIJOFS_BUTTON0]
   6.671 -                                                   ? SDL_PRESSED :
   6.672 -                                                   SDL_RELEASED));
   6.673 +            SDL_PrivateJoystickButton(joystick, in->num,
   6.674 +                (Uint8) (state.rgbButtons[in->ofs - DIJOFS_BUTTON0] ? SDL_PRESSED : SDL_RELEASED));
   6.675              break;
   6.676          case HAT:
   6.677              {
   6.678 -                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
   6.679 -                                                       DIJOFS_POV(0)]);
   6.680 -                SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
   6.681 +                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs - DIJOFS_POV(0)]);
   6.682 +                SDL_PrivateJoystickHat(joystick, in->num, pos);
   6.683                  break;
   6.684              }
   6.685          }
   6.686 @@ -1357,10 +1386,9 @@
   6.687      }
   6.688  
   6.689      /* Handle the events or punt */
   6.690 -    if (FAILED(result))
   6.691 -    {
   6.692 -        joystick->hwdata->send_remove_event = 1;
   6.693 -        joystick->hwdata->removed = 1;
   6.694 +    if (FAILED(result)) {
   6.695 +        joystick->hwdata->send_remove_event = SDL_TRUE;
   6.696 +        joystick->hwdata->removed = SDL_TRUE;
   6.697          return;
   6.698      }
   6.699  
   6.700 @@ -1375,14 +1403,11 @@
   6.701  
   6.702              switch (in->type) {
   6.703              case AXIS:
   6.704 -                SDL_PrivateJoystickAxis(joystick, in->num,
   6.705 -                                        (Sint16) evtbuf[i].dwData);
   6.706 +                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16) evtbuf[i].dwData);
   6.707                  break;
   6.708              case BUTTON:
   6.709                  SDL_PrivateJoystickButton(joystick, in->num,
   6.710 -                                          (Uint8) (evtbuf[i].
   6.711 -                                                   dwData ? SDL_PRESSED :
   6.712 -                                                   SDL_RELEASED));
   6.713 +                    (Uint8) (evtbuf[i].dwData ? SDL_PRESSED : SDL_RELEASED));
   6.714                  break;
   6.715              case HAT:
   6.716                  {
   6.717 @@ -1394,12 +1419,69 @@
   6.718      }
   6.719  }
   6.720  
   6.721 +static void
   6.722 +UpdateXInputJoystickState_OLD(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState)
   6.723 +{
   6.724 +	static WORD s_XInputButtons[] = {
   6.725 +		XINPUT_GAMEPAD_DPAD_UP, XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT, XINPUT_GAMEPAD_DPAD_RIGHT,
   6.726 +		XINPUT_GAMEPAD_START, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB,
   6.727 +		XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER,
   6.728 +		XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
   6.729 +		XINPUT_GAMEPAD_GUIDE
   6.730 +	};
   6.731 +	WORD wButtons = pXInputState->Gamepad.wButtons;
   6.732 +	Uint8 button;
   6.733 +	Uint8 hat = SDL_HAT_CENTERED;
   6.734  
   6.735 -/* Function to return > 0 if a bit array of buttons differs after applying a mask
   6.736 -*/
   6.737 -int ButtonChanged( int ButtonsNow, int ButtonsPrev, int ButtonMask )
   6.738 +	SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX);
   6.739 +	SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbLY)));
   6.740 +	SDL_PrivateJoystickAxis(joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX);
   6.741 +	SDL_PrivateJoystickAxis(joystick, 3, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbRY)));
   6.742 +	SDL_PrivateJoystickAxis(joystick, 4, (Sint16)(((int)pXInputState->Gamepad.bLeftTrigger * 65535 / 255) - 32768));
   6.743 +	SDL_PrivateJoystickAxis(joystick, 5, (Sint16)(((int)pXInputState->Gamepad.bRightTrigger * 65535 / 255) - 32768));
   6.744 +
   6.745 +	for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) {
   6.746 +		SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED);
   6.747 +	}
   6.748 +}
   6.749 +
   6.750 +static void
   6.751 +UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState)
   6.752  {
   6.753 -    return ( ButtonsNow & ButtonMask ) != ( ButtonsPrev & ButtonMask );
   6.754 +	static WORD s_XInputButtons[] = {
   6.755 +		XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
   6.756 +		XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_START,
   6.757 +		XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB,
   6.758 +		XINPUT_GAMEPAD_GUIDE
   6.759 +	};
   6.760 +	WORD wButtons = pXInputState->Gamepad.wButtons;
   6.761 +	Uint8 button;
   6.762 +	Uint8 hat = SDL_HAT_CENTERED;
   6.763 +
   6.764 +	SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX);
   6.765 +	SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbLY)));
   6.766 +	SDL_PrivateJoystickAxis(joystick, 2, (Sint16)(((int)pXInputState->Gamepad.bLeftTrigger * 65535 / 255) - 32768));
   6.767 +	SDL_PrivateJoystickAxis(joystick, 3, (Sint16)pXInputState->Gamepad.sThumbRX);
   6.768 +	SDL_PrivateJoystickAxis(joystick, 4, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbRY)));
   6.769 +	SDL_PrivateJoystickAxis(joystick, 5, (Sint16)(((int)pXInputState->Gamepad.bRightTrigger * 65535 / 255) - 32768));
   6.770 +
   6.771 +	for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) {
   6.772 +		SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED);
   6.773 +	}
   6.774 +
   6.775 +	if (wButtons & XINPUT_GAMEPAD_DPAD_UP) {
   6.776 +		hat |= SDL_HAT_UP;
   6.777 +	}
   6.778 +	if (wButtons & XINPUT_GAMEPAD_DPAD_DOWN) {
   6.779 +		hat |= SDL_HAT_DOWN;
   6.780 +	}
   6.781 +	if (wButtons & XINPUT_GAMEPAD_DPAD_LEFT) {
   6.782 +		hat |= SDL_HAT_LEFT;
   6.783 +	}
   6.784 +	if (wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) {
   6.785 +		hat |= SDL_HAT_RIGHT;
   6.786 +	}
   6.787 +	SDL_PrivateJoystickHat(joystick, 0, hat);
   6.788  }
   6.789  
   6.790  /* Function to update the state of a XInput style joystick.
   6.791 @@ -1408,71 +1490,29 @@
   6.792  SDL_SYS_JoystickUpdate_XInput(SDL_Joystick * joystick)
   6.793  {
   6.794      HRESULT result;
   6.795 +	XINPUT_STATE_EX XInputState;
   6.796  
   6.797 -    if ( !XINPUTGETSTATE )
   6.798 +    if (!XINPUTGETSTATE)
   6.799          return;
   6.800  
   6.801 -    result = XINPUTGETSTATE( joystick->hwdata->userid, &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot] );
   6.802 -    if ( result == ERROR_DEVICE_NOT_CONNECTED )
   6.803 -    {
   6.804 -        joystick->hwdata->send_remove_event = 1;
   6.805 -        joystick->hwdata->removed = 1;
   6.806 +	result = XINPUTGETSTATE(joystick->hwdata->userid, &XInputState);
   6.807 +    if (result == ERROR_DEVICE_NOT_CONNECTED) {
   6.808 +        joystick->hwdata->send_remove_event = SDL_TRUE;
   6.809 +        joystick->hwdata->removed = SDL_TRUE;
   6.810          return;
   6.811      }
   6.812  
   6.813      /* only fire events if the data changed from last time */
   6.814 -    if ( joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot].dwPacketNumber != 0
   6.815 -        && joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot].dwPacketNumber != joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot^1].dwPacketNumber )
   6.816 -    {
   6.817 -        XINPUT_STATE_EX *pXInputState = &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot];
   6.818 -        XINPUT_STATE_EX *pXInputStatePrev = &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot ^ 1];
   6.819 -
   6.820 -        /* !!! FIXME: why isn't this just using SDL_PrivateJoystickAxis_Int()? */
   6.821 -        SDL_PrivateJoystickAxis( joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX );
   6.822 -        SDL_PrivateJoystickAxis( joystick, 1, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbLY)) );
   6.823 -        SDL_PrivateJoystickAxis( joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX );
   6.824 -        SDL_PrivateJoystickAxis( joystick, 3, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbRY)) );
   6.825 -        SDL_PrivateJoystickAxis( joystick, 4, (Sint16)(((int)pXInputState->Gamepad.bLeftTrigger*65535/255) - 32768));
   6.826 -        SDL_PrivateJoystickAxis( joystick, 5, (Sint16)(((int)pXInputState->Gamepad.bRightTrigger*65535/255) - 32768));
   6.827 -
   6.828 -        /* !!! FIXME: why isn't this just using SDL_PrivateJoystickButton_Int(), instead of keeping these two alternating state buffers? */
   6.829 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_UP ) )
   6.830 -            SDL_PrivateJoystickButton(joystick, 0, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP ? SDL_PRESSED :  SDL_RELEASED );
   6.831 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_DOWN ) )
   6.832 -            SDL_PrivateJoystickButton(joystick, 1, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN ? SDL_PRESSED :    SDL_RELEASED );
   6.833 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_LEFT ) )
   6.834 -            SDL_PrivateJoystickButton(joystick, 2, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT ? SDL_PRESSED :    SDL_RELEASED );
   6.835 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_RIGHT ) )
   6.836 -            SDL_PrivateJoystickButton(joystick, 3, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT ? SDL_PRESSED :   SDL_RELEASED );
   6.837 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_START ) )
   6.838 -            SDL_PrivateJoystickButton(joystick, 4, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_START ? SDL_PRESSED :    SDL_RELEASED );
   6.839 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_BACK ) )
   6.840 -            SDL_PrivateJoystickButton(joystick, 5, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_BACK ? SDL_PRESSED : SDL_RELEASED );
   6.841 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_THUMB ) )
   6.842 -            SDL_PrivateJoystickButton(joystick, 6, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB ? SDL_PRESSED :   SDL_RELEASED );
   6.843 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_THUMB ) )
   6.844 -            SDL_PrivateJoystickButton(joystick, 7, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB ? SDL_PRESSED :  SDL_RELEASED );
   6.845 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_SHOULDER ) )
   6.846 -            SDL_PrivateJoystickButton(joystick, 8, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER ? SDL_PRESSED :    SDL_RELEASED );
   6.847 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_SHOULDER ) )
   6.848 -            SDL_PrivateJoystickButton(joystick, 9, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER ? SDL_PRESSED :   SDL_RELEASED );
   6.849 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_A ) )
   6.850 -            SDL_PrivateJoystickButton(joystick, 10, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_A ? SDL_PRESSED :   SDL_RELEASED );
   6.851 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_B ) )
   6.852 -            SDL_PrivateJoystickButton(joystick, 11, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_B ? SDL_PRESSED :   SDL_RELEASED );
   6.853 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_X ) )
   6.854 -            SDL_PrivateJoystickButton(joystick, 12, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_X ? SDL_PRESSED :   SDL_RELEASED );
   6.855 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_Y ) )
   6.856 -            SDL_PrivateJoystickButton(joystick, 13, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_Y ? SDL_PRESSED :   SDL_RELEASED );
   6.857 -        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons,  0x400 ) )
   6.858 -            SDL_PrivateJoystickButton(joystick, 14, pXInputState->Gamepad.wButtons & 0x400 ? SDL_PRESSED :  SDL_RELEASED ); /* 0x400 is the undocumented code for the guide button */
   6.859 -
   6.860 -        joystick->hwdata->currentXInputSlot ^= 1;
   6.861 -
   6.862 +	if (XInputState.dwPacketNumber && XInputState.dwPacketNumber != joystick->hwdata->dwPacketNumber) {
   6.863 +		if (SDL_XInputUseOldJoystickMapping()) {
   6.864 +			UpdateXInputJoystickState_OLD(joystick, &XInputState);
   6.865 +		} else {
   6.866 +			UpdateXInputJoystickState(joystick, &XInputState);
   6.867 +		}
   6.868 +		joystick->hwdata->dwPacketNumber = XInputState.dwPacketNumber;
   6.869      }
   6.870  }
   6.871  
   6.872 -
   6.873  static Uint8
   6.874  TranslatePOV(DWORD value)
   6.875  {
   6.876 @@ -1501,48 +1541,17 @@
   6.877      return HAT_VALS[value];
   6.878  }
   6.879  
   6.880 -/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
   6.881 - * do it. */
   6.882 -/* !!! FIXME: SDL_PrivateJoystickAxis _does_ discard duplicate events now. Ditch this code. */
   6.883 -static int
   6.884 -SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
   6.885 -{
   6.886 -    if (joystick->axes[axis] != value)
   6.887 -        return SDL_PrivateJoystickAxis(joystick, axis, value);
   6.888 -    return 0;
   6.889 -}
   6.890 -
   6.891 -static int
   6.892 -SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
   6.893 -{
   6.894 -    if (joystick->hats[hat] != value)
   6.895 -        return SDL_PrivateJoystickHat(joystick, hat, value);
   6.896 -    return 0;
   6.897 -}
   6.898 -
   6.899 -static int
   6.900 -SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
   6.901 -                              Uint8 state)
   6.902 -{
   6.903 -    if (joystick->buttons[button] != state)
   6.904 -        return SDL_PrivateJoystickButton(joystick, button, state);
   6.905 -    return 0;
   6.906 -}
   6.907 -
   6.908  void
   6.909  SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   6.910  {
   6.911      HRESULT result;
   6.912  
   6.913 -    if ( joystick->closed || !joystick->hwdata )
   6.914 +    if (joystick->closed || !joystick->hwdata)
   6.915          return;
   6.916  
   6.917 -    if (joystick->hwdata->bXInputDevice)
   6.918 -    {
   6.919 +    if (joystick->hwdata->bXInputDevice) {
   6.920          SDL_SYS_JoystickUpdate_XInput(joystick);
   6.921 -    }
   6.922 -    else
   6.923 -    {
   6.924 +    } else {
   6.925          result = IDirectInputDevice8_Poll(joystick->hwdata->InputDevice);
   6.926          if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   6.927              IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
   6.928 @@ -1555,10 +1564,9 @@
   6.929              SDL_SYS_JoystickUpdate_Polled(joystick);
   6.930      }
   6.931  
   6.932 -    if ( joystick->hwdata->removed )
   6.933 -    {
   6.934 -        joystick->closed = 1;
   6.935 -        joystick->uncentered = 1;
   6.936 +    if (joystick->hwdata->removed) {
   6.937 +        joystick->closed = SDL_TRUE;
   6.938 +        joystick->uncentered = SDL_TRUE;
   6.939      }
   6.940  }
   6.941  
   6.942 @@ -1574,7 +1582,7 @@
   6.943      /* free system specific hardware data */
   6.944      SDL_free(joystick->hwdata);
   6.945  
   6.946 -    joystick->closed = 1;
   6.947 +    joystick->closed = SDL_TRUE;
   6.948  }
   6.949  
   6.950  /* Function to perform any system-specific joystick related cleanup */
   6.951 @@ -1583,8 +1591,7 @@
   6.952  {
   6.953      JoyStick_DeviceData *device = SYS_Joystick;
   6.954  
   6.955 -    while ( device )
   6.956 -    {
   6.957 +    while (device) {
   6.958          JoyStick_DeviceData *device_next = device->pNext;
   6.959          SDL_free(device->joystickname);
   6.960          SDL_free(device);
   6.961 @@ -1592,16 +1599,15 @@
   6.962      }
   6.963      SYS_Joystick = NULL;
   6.964  
   6.965 -    if ( s_threadJoystick )
   6.966 -    {
   6.967 -        SDL_LockMutex( s_mutexJoyStickEnum );
   6.968 +    if (s_threadJoystick) {
   6.969 +        SDL_LockMutex(s_mutexJoyStickEnum);
   6.970          s_bJoystickThreadQuit = SDL_TRUE;
   6.971 -        SDL_CondBroadcast( s_condJoystickThread ); /* signal the joystick thread to quit */
   6.972 -        SDL_UnlockMutex( s_mutexJoyStickEnum );
   6.973 -        SDL_WaitThread( s_threadJoystick, NULL ); /* wait for it to bugger off */
   6.974 +        SDL_CondBroadcast(s_condJoystickThread); /* signal the joystick thread to quit */
   6.975 +        SDL_UnlockMutex(s_mutexJoyStickEnum);
   6.976 +        SDL_WaitThread(s_threadJoystick, NULL); /* wait for it to bugger off */
   6.977  
   6.978 -        SDL_DestroyMutex( s_mutexJoyStickEnum );
   6.979 -        SDL_DestroyCond( s_condJoystickThread );
   6.980 +        SDL_DestroyMutex(s_mutexJoyStickEnum);
   6.981 +        SDL_DestroyCond(s_condJoystickThread);
   6.982          s_condJoystickThread= NULL;
   6.983          s_mutexJoyStickEnum = NULL;
   6.984          s_threadJoystick = NULL;
   6.985 @@ -1623,7 +1629,7 @@
   6.986  }
   6.987  
   6.988  /* return the stable device guid for this device index */
   6.989 -SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   6.990 +SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)
   6.991  {
   6.992      JoyStick_DeviceData *device = SYS_Joystick;
   6.993      int index;
   6.994 @@ -1639,8 +1645,7 @@
   6.995      return joystick->hwdata->guid;
   6.996  }
   6.997  
   6.998 -/* return SDL_TRUE if this device is using XInput */
   6.999 -SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index)
  6.1000 +SDL_bool SDL_SYS_IsXInputGamepad_DeviceIndex(int device_index)
  6.1001  {
  6.1002      JoyStick_DeviceData *device = SYS_Joystick;
  6.1003      int index;
  6.1004 @@ -1648,13 +1653,7 @@
  6.1005      for (index = device_index; index > 0; index--)
  6.1006          device = device->pNext;
  6.1007  
  6.1008 -    return device->bXInputDevice;
  6.1009 -}
  6.1010 -
  6.1011 -/* return SDL_TRUE if this device was opened with XInput */
  6.1012 -SDL_bool SDL_SYS_IsXInputJoystick(SDL_Joystick * joystick)
  6.1013 -{
  6.1014 -    return joystick->hwdata->bXInputDevice;
  6.1015 +    return (device->SubType == XINPUT_DEVSUBTYPE_GAMEPAD);
  6.1016  }
  6.1017  
  6.1018  #endif /* SDL_JOYSTICK_DINPUT */
     7.1 --- a/src/joystick/windows/SDL_dxjoystick_c.h	Tue Jun 24 01:38:21 2014 -0700
     7.2 +++ b/src/joystick/windows/SDL_dxjoystick_c.h	Tue Jun 24 13:31:25 2014 -0700
     7.3 @@ -54,6 +54,43 @@
     7.4  #define XINPUT_CAPS_FFB_SUPPORTED 0x0001
     7.5  #endif
     7.6  
     7.7 +#ifndef XINPUT_DEVSUBTYPE_UNKNOWN
     7.8 +#define XINPUT_DEVSUBTYPE_UNKNOWN 0x00
     7.9 +#endif
    7.10 +#ifndef XINPUT_DEVSUBTYPE_GAMEPAD
    7.11 +#define XINPUT_DEVSUBTYPE_GAMEPAD 0x01
    7.12 +#endif
    7.13 +#ifndef XINPUT_DEVSUBTYPE_WHEEL
    7.14 +#define XINPUT_DEVSUBTYPE_WHEEL 0x02
    7.15 +#endif
    7.16 +#ifndef XINPUT_DEVSUBTYPE_ARCADE_STICK
    7.17 +#define XINPUT_DEVSUBTYPE_ARCADE_STICK 0x03
    7.18 +#endif
    7.19 +#ifndef XINPUT_DEVSUBTYPE_FLIGHT_STICK
    7.20 +#define XINPUT_DEVSUBTYPE_FLIGHT_STICK 0x04
    7.21 +#endif
    7.22 +#ifndef XINPUT_DEVSUBTYPE_DANCE_PAD
    7.23 +#define XINPUT_DEVSUBTYPE_DANCE_PAD 0x05
    7.24 +#endif
    7.25 +#ifndef XINPUT_DEVSUBTYPE_GUITAR
    7.26 +#define XINPUT_DEVSUBTYPE_GUITAR 0x06
    7.27 +#endif
    7.28 +#ifndef XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE
    7.29 +#define XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE 0x07
    7.30 +#endif
    7.31 +#ifndef XINPUT_DEVSUBTYPE_DRUM_KIT
    7.32 +#define XINPUT_DEVSUBTYPE_DRUM_KIT 0x08
    7.33 +#endif
    7.34 +#ifndef XINPUT_DEVSUBTYPE_GUITAR_BASS
    7.35 +#define XINPUT_DEVSUBTYPE_GUITAR_BASS 0x0B
    7.36 +#endif
    7.37 +#ifndef XINPUT_DEVSUBTYPE_ARCADE_PAD
    7.38 +#define XINPUT_DEVSUBTYPE_ARCADE_PAD 0x13
    7.39 +#endif
    7.40 +
    7.41 +#ifndef XINPUT_GAMEPAD_GUIDE
    7.42 +#define XINPUT_GAMEPAD_GUIDE 0x0400
    7.43 +#endif
    7.44  
    7.45  /* typedef's for XInput structs we use */
    7.46  typedef struct
    7.47 @@ -138,13 +175,12 @@
    7.48      input_t Inputs[MAX_INPUTS];
    7.49      int NumInputs;
    7.50      int NumSliders;
    7.51 -    Uint8 removed;
    7.52 -    Uint8 send_remove_event;
    7.53 -    Uint8 bXInputDevice; /* 1 if this device supports using the xinput API rather than DirectInput */
    7.54 -    Uint8 bXInputHaptic; /* Supports force feedback via XInput. */
    7.55 +	SDL_bool removed;
    7.56 +	SDL_bool send_remove_event;
    7.57 +	SDL_bool bXInputDevice; /* SDL_TRUE if this device supports using the xinput API rather than DirectInput */
    7.58 +	SDL_bool bXInputHaptic; /* Supports force feedback via XInput. */
    7.59      Uint8 userid; /* XInput userid index for this joystick */
    7.60 -    Uint8 currentXInputSlot; /* the current position to write to in XInputState below, used so we can compare old and new values */
    7.61 -    XINPUT_STATE_EX XInputState[2];
    7.62 +	DWORD dwPacketNumber;
    7.63  };
    7.64  
    7.65  #endif /* SDL_JOYSTICK_DINPUT_H */