src/joystick/windowsrt/SDL_xinputjoystick.c
author David Ludwig <dludwig@pobox.com>
Sun, 19 May 2013 23:30:34 -0400
changeset 8471 456c005ec72c
child 8473 218549868b0b
permissions -rw-r--r--
WinRT: added preliminary joystick / game controller support (minus hotplugging)
dludwig@8471
     1
/*
dludwig@8471
     2
  Simple DirectMedia Layer
dludwig@8471
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
dludwig@8471
     4
dludwig@8471
     5
  This software is provided 'as-is', without any express or implied
dludwig@8471
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8471
     7
  arising from the use of this software.
dludwig@8471
     8
dludwig@8471
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8471
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8471
    11
  freely, subject to the following restrictions:
dludwig@8471
    12
dludwig@8471
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8471
    14
     claim that you wrote the original software. If you use this software
dludwig@8471
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8471
    16
     appreciated but is not required.
dludwig@8471
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8471
    18
     misrepresented as being the original software.
dludwig@8471
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8471
    20
*/
dludwig@8471
    21
#include "SDL_config.h"
dludwig@8471
    22
dludwig@8471
    23
#if SDL_JOYSTICK_XINPUT
dludwig@8471
    24
dludwig@8471
    25
/* SDL_xinputjoystick.c implements an XInput-only joystick and game controller
dludwig@8471
    26
   backend that is suitable for use on WinRT.  SDL's DirectInput backend, also
dludwig@8471
    27
   XInput-capable, was not used as DirectInput is not available on WinRT (or,
dludwig@8471
    28
   at least, it isn't a public API).  Some portions of this XInput backend
dludwig@8471
    29
   may copy parts of the XInput-using code from the DirectInput backend.
dludwig@8471
    30
   Refactoring the common parts into one location may be good to-do at some
dludwig@8471
    31
   point.
dludwig@8471
    32
dludwig@8471
    33
   TODO, WinRT: add hotplug support for XInput based game controllers
dludwig@8471
    34
*/
dludwig@8471
    35
dludwig@8471
    36
#include "SDL_joystick.h"
dludwig@8471
    37
#include "../SDL_sysjoystick.h"
dludwig@8471
    38
#include "../SDL_joystick_c.h"
dludwig@8471
    39
#include "SDL_events.h"
dludwig@8471
    40
dludwig@8471
    41
#include <Windows.h>
dludwig@8471
    42
#include <Xinput.h>
dludwig@8471
    43
dludwig@8471
    44
struct joystick_hwdata {
dludwig@8471
    45
    //Uint8 bXInputHaptic; // Supports force feedback via XInput.
dludwig@8471
    46
    DWORD userIndex;    // The XInput device index, in the range [0, XUSER_MAX_COUNT-1] (probably [0,3]).
dludwig@8471
    47
    XINPUT_STATE XInputState;   // the last-read in XInputState, kept around to compare old and new values
dludwig@8471
    48
    SDL_bool isDeviceConnected; // was the device connected (on the last polling, or during backend-initialization)?
dludwig@8471
    49
};
dludwig@8471
    50
dludwig@8471
    51
/* Keep track of data on all XInput devices, regardless of whether or not
dludwig@8471
    52
   they've been opened (via SDL_JoystickOpen).
dludwig@8471
    53
 */
dludwig@8471
    54
static struct joystick_hwdata g_XInputData[XUSER_MAX_COUNT];
dludwig@8471
    55
dludwig@8471
    56
/* Function to scan the system for joysticks.
dludwig@8471
    57
 * It should return 0, or -1 on an unrecoverable fatal error.
dludwig@8471
    58
 */
dludwig@8471
    59
int
dludwig@8471
    60
SDL_SYS_JoystickInit(void)
dludwig@8471
    61
{
dludwig@8471
    62
    HRESULT result = S_OK;
dludwig@8471
    63
    XINPUT_STATE tempXInputState;
dludwig@8471
    64
    int i;
dludwig@8471
    65
dludwig@8471
    66
    SDL_zero(g_XInputData);
dludwig@8471
    67
dludwig@8471
    68
    /* Make initial notes on whether or not devices are connected (or not).
dludwig@8471
    69
     */
dludwig@8471
    70
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8471
    71
        result = XInputGetState(i, &tempXInputState);
dludwig@8471
    72
        if (result == ERROR_SUCCESS) {
dludwig@8471
    73
            g_XInputData[i].isDeviceConnected = SDL_TRUE;
dludwig@8471
    74
        }
dludwig@8471
    75
    }
dludwig@8471
    76
dludwig@8471
    77
    return (0);
dludwig@8471
    78
}
dludwig@8471
    79
dludwig@8471
    80
int SDL_SYS_NumJoysticks()
dludwig@8471
    81
{
dludwig@8471
    82
    int joystickCount = 0;
dludwig@8471
    83
    DWORD i;
dludwig@8471
    84
dludwig@8471
    85
    /* Iterate through each possible XInput device and see if something
dludwig@8471
    86
       was connected (at joystick init, or during the last polling).
dludwig@8471
    87
     */
dludwig@8471
    88
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8471
    89
        if (g_XInputData[i].isDeviceConnected) {
dludwig@8471
    90
            ++joystickCount;
dludwig@8471
    91
        }
dludwig@8471
    92
    }
dludwig@8471
    93
dludwig@8471
    94
    return joystickCount;
dludwig@8471
    95
}
dludwig@8471
    96
dludwig@8471
    97
void SDL_SYS_JoystickDetect()
dludwig@8471
    98
{
dludwig@8471
    99
}
dludwig@8471
   100
dludwig@8471
   101
SDL_bool SDL_SYS_JoystickNeedsPolling()
dludwig@8471
   102
{
dludwig@8471
   103
    return SDL_FALSE;
dludwig@8471
   104
}
dludwig@8471
   105
dludwig@8471
   106
/* Internal function to retreive device capabilities.
dludwig@8471
   107
   This function will return an SDL-standard value of 0 on success
dludwig@8471
   108
   (a device is connected, and data on it was retrieved), or -1
dludwig@8471
   109
   on failure (no device was connected, or some other error
dludwig@8471
   110
   occurred.  SDL_SetError() will be invoked to set an appropriate
dludwig@8471
   111
   error message.
dludwig@8471
   112
 */
dludwig@8471
   113
static int
dludwig@8471
   114
SDL_XInput_GetDeviceCapabilities(int device_index, XINPUT_CAPABILITIES * pDeviceCaps)
dludwig@8471
   115
{
dludwig@8471
   116
    HRESULT dwResult;
dludwig@8471
   117
dludwig@8471
   118
    /* Make sure that the device index is a valid one.  If not, return to the
dludwig@8471
   119
       caller with an error.
dludwig@8471
   120
     */
dludwig@8471
   121
    if (device_index < 0 || device_index >= XUSER_MAX_COUNT) {
dludwig@8471
   122
        return SDL_SetError("invalid/unavailable device index");
dludwig@8471
   123
    }
dludwig@8471
   124
dludwig@8471
   125
    /* See if a device exists, and if so, what its capabilities are.  If a
dludwig@8471
   126
       device is not available, return to the caller with an error.
dludwig@8471
   127
     */
dludwig@8471
   128
    switch ((dwResult = XInputGetCapabilities(device_index, 0, pDeviceCaps))) {
dludwig@8471
   129
        case ERROR_SUCCESS:
dludwig@8471
   130
            /* A device is available, and its capabilities were retrieved! */
dludwig@8471
   131
            return 0;
dludwig@8471
   132
        case ERROR_DEVICE_NOT_CONNECTED:
dludwig@8471
   133
            return SDL_SetError("no device is connected at joystick index, %d", device_index);
dludwig@8471
   134
        default:
dludwig@8471
   135
            return SDL_SetError("an unknown error occurred when retrieving info on a device at joystick index, %d", device_index);
dludwig@8471
   136
    }
dludwig@8471
   137
}
dludwig@8471
   138
dludwig@8471
   139
/* Function to get the device-dependent name of a joystick */
dludwig@8471
   140
const char *
dludwig@8471
   141
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
dludwig@8471
   142
{
dludwig@8471
   143
    XINPUT_CAPABILITIES deviceCaps;
dludwig@8471
   144
dludwig@8471
   145
    if (SDL_XInput_GetDeviceCapabilities(device_index, &deviceCaps) != 0) {
dludwig@8471
   146
        /* Uh oh.  Device capabilities couldn't be retrieved.  Return to the
dludwig@8471
   147
           caller.  SDL_SetError() has already been invoked (with relevant
dludwig@8471
   148
           information).
dludwig@8471
   149
         */
dludwig@8471
   150
        return NULL;
dludwig@8471
   151
    }
dludwig@8471
   152
dludwig@8471
   153
    switch (deviceCaps.SubType) {
dludwig@8471
   154
        default:
dludwig@8471
   155
            if (deviceCaps.Type == XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   156
                return "Undefined game controller";
dludwig@8471
   157
            } else {
dludwig@8471
   158
                return "Undefined controller";
dludwig@8471
   159
            }
dludwig@8471
   160
        case XINPUT_DEVSUBTYPE_UNKNOWN:
dludwig@8471
   161
            if (deviceCaps.Type == XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   162
                return "Unknown game controller";
dludwig@8471
   163
            } else {
dludwig@8471
   164
                return "Unknown controller";
dludwig@8471
   165
            }
dludwig@8471
   166
        case XINPUT_DEVSUBTYPE_GAMEPAD:
dludwig@8471
   167
            return "Gamepad controller";
dludwig@8471
   168
        case XINPUT_DEVSUBTYPE_WHEEL:
dludwig@8471
   169
            return "Racing wheel controller";
dludwig@8471
   170
        case XINPUT_DEVSUBTYPE_ARCADE_STICK:
dludwig@8471
   171
            return "Arcade stick controller";
dludwig@8471
   172
        case XINPUT_DEVSUBTYPE_FLIGHT_STICK:
dludwig@8471
   173
            return "Flight stick controller";
dludwig@8471
   174
        case XINPUT_DEVSUBTYPE_DANCE_PAD:
dludwig@8471
   175
            return "Dance pad controller";
dludwig@8471
   176
        case XINPUT_DEVSUBTYPE_GUITAR:
dludwig@8471
   177
            return "Guitar controller";
dludwig@8471
   178
        case XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE:
dludwig@8471
   179
            return "Guitar controller, Alternate";
dludwig@8471
   180
        case XINPUT_DEVSUBTYPE_GUITAR_BASS:
dludwig@8471
   181
            return "Guitar controller, Bass";
dludwig@8471
   182
        case XINPUT_DEVSUBTYPE_DRUM_KIT:
dludwig@8471
   183
            return "Drum controller";
dludwig@8471
   184
        case XINPUT_DEVSUBTYPE_ARCADE_PAD:
dludwig@8471
   185
            return "Arcade pad controller";
dludwig@8471
   186
    }
dludwig@8471
   187
}
dludwig@8471
   188
dludwig@8471
   189
/* Function to perform the mapping from device index to the instance id for this index */
dludwig@8471
   190
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
dludwig@8471
   191
{
dludwig@8471
   192
    return device_index;
dludwig@8471
   193
}
dludwig@8471
   194
dludwig@8471
   195
/* Function to open a joystick for use.
dludwig@8471
   196
   The joystick to open is specified by the index field of the joystick.
dludwig@8471
   197
   This should fill the nbuttons and naxes fields of the joystick structure.
dludwig@8471
   198
   It returns 0, or -1 if there is an error.
dludwig@8471
   199
 */
dludwig@8471
   200
int
dludwig@8471
   201
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
dludwig@8471
   202
{
dludwig@8471
   203
    XINPUT_CAPABILITIES deviceCaps;
dludwig@8471
   204
dludwig@8471
   205
    if (SDL_XInput_GetDeviceCapabilities(device_index, &deviceCaps) != 0) {
dludwig@8471
   206
        /* Uh oh.  Device capabilities couldn't be retrieved.  Return to the
dludwig@8471
   207
           caller.  SDL_SetError() has already been invoked (with relevant
dludwig@8471
   208
           information).
dludwig@8471
   209
         */
dludwig@8471
   210
        return -1;
dludwig@8471
   211
    }
dludwig@8471
   212
dludwig@8471
   213
    /* For now, only game pads are supported.  If the device is something other
dludwig@8471
   214
       than that, return an error to the caller.
dludwig@8471
   215
     */
dludwig@8471
   216
    if (deviceCaps.Type != XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   217
        return SDL_SetError("a device is connected (at joystick index, %d), but it is of an unknown device type (deviceCaps.Flags=%ul)",
dludwig@8471
   218
            device_index, (unsigned int)deviceCaps.Flags);
dludwig@8471
   219
    }
dludwig@8471
   220
dludwig@8471
   221
    /* Create the joystick data structure */
dludwig@8471
   222
    joystick->instance_id = device_index;
dludwig@8471
   223
    joystick->hwdata = &g_XInputData[device_index];
dludwig@8471
   224
dludwig@8471
   225
    // The XInput API has a hard coded button/axis mapping, so we just match it
dludwig@8471
   226
    joystick->naxes = 6;
dludwig@8471
   227
    joystick->nbuttons = 15;
dludwig@8471
   228
    joystick->nballs = 0;
dludwig@8471
   229
    joystick->nhats = 0;
dludwig@8471
   230
dludwig@8471
   231
    /* We're done! */
dludwig@8471
   232
    return (0);
dludwig@8471
   233
}
dludwig@8471
   234
dludwig@8471
   235
/* Function to determine is this joystick is attached to the system right now */
dludwig@8471
   236
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
dludwig@8471
   237
{
dludwig@8471
   238
    return joystick->hwdata->isDeviceConnected;
dludwig@8471
   239
}
dludwig@8471
   240
dludwig@8471
   241
/* Function to return > 0 if a bit array of buttons differs after applying a mask
dludwig@8471
   242
*/
dludwig@8471
   243
static int ButtonChanged( int ButtonsNow, int ButtonsPrev, int ButtonMask )
dludwig@8471
   244
{
dludwig@8471
   245
    return ( ButtonsNow & ButtonMask ) != ( ButtonsPrev & ButtonMask );
dludwig@8471
   246
}
dludwig@8471
   247
dludwig@8471
   248
/* Function to update the state of a joystick - called as a device poll.
dludwig@8471
   249
 * This function shouldn't update the joystick structure directly,
dludwig@8471
   250
 * but instead should call SDL_PrivateJoystick*() to deliver events
dludwig@8471
   251
 * and update joystick device state.
dludwig@8471
   252
 */
dludwig@8471
   253
void
dludwig@8471
   254
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
dludwig@8471
   255
{
dludwig@8471
   256
    HRESULT result;
dludwig@8471
   257
dludwig@8471
   258
    /* Before polling for new data, make note of the old data */
dludwig@8471
   259
    XINPUT_STATE prevXInputState = joystick->hwdata->XInputState;
dludwig@8471
   260
dludwig@8471
   261
    /* Poll for new data */
dludwig@8471
   262
    result = XInputGetState(joystick->hwdata->userIndex, &joystick->hwdata->XInputState);
dludwig@8471
   263
    if (result == ERROR_DEVICE_NOT_CONNECTED) {
dludwig@8471
   264
        /* TODO, WinRT: set a flag to indicate that a device-removal event
dludwig@8471
   265
           needs to be emitted.
dludwig@8471
   266
         */
dludwig@8471
   267
        //joystick->hwdata->send_remove_event = 1;
dludwig@8471
   268
        //joystick->hwdata->removed = 1;
dludwig@8471
   269
        joystick->hwdata->isDeviceConnected = SDL_FALSE;
dludwig@8471
   270
        return;
dludwig@8471
   271
    }
dludwig@8471
   272
dludwig@8471
   273
    /* Make sure the device is marked as connected */
dludwig@8471
   274
    joystick->hwdata->isDeviceConnected = SDL_TRUE;
dludwig@8471
   275
dludwig@8471
   276
    // only fire events if the data changed from last time
dludwig@8471
   277
    if ( joystick->hwdata->XInputState.dwPacketNumber != 0 
dludwig@8471
   278
        && joystick->hwdata->XInputState.dwPacketNumber != prevXInputState.dwPacketNumber )
dludwig@8471
   279
    {
dludwig@8471
   280
        XINPUT_STATE *pXInputState = &joystick->hwdata->XInputState;
dludwig@8471
   281
        XINPUT_STATE *pXInputStatePrev = &prevXInputState;
dludwig@8471
   282
dludwig@8471
   283
        SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX );
dludwig@8471
   284
        SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-1*pXInputState->Gamepad.sThumbLY-1) );
dludwig@8471
   285
        SDL_PrivateJoystickAxis(joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX );
dludwig@8471
   286
        SDL_PrivateJoystickAxis(joystick, 3, (Sint16)(-1*pXInputState->Gamepad.sThumbRY-1) );
dludwig@8471
   287
        SDL_PrivateJoystickAxis(joystick, 4, (Sint16)((int)pXInputState->Gamepad.bLeftTrigger*32767/255) );
dludwig@8471
   288
        SDL_PrivateJoystickAxis(joystick, 5, (Sint16)((int)pXInputState->Gamepad.bRightTrigger*32767/255) );
dludwig@8471
   289
dludwig@8471
   290
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_UP ) )
dludwig@8471
   291
            SDL_PrivateJoystickButton(joystick, 0, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   292
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_DOWN ) )
dludwig@8471
   293
            SDL_PrivateJoystickButton(joystick, 1, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   294
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_LEFT ) )
dludwig@8471
   295
            SDL_PrivateJoystickButton(joystick, 2, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   296
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_RIGHT ) )
dludwig@8471
   297
            SDL_PrivateJoystickButton(joystick, 3, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   298
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_START ) )
dludwig@8471
   299
            SDL_PrivateJoystickButton(joystick, 4, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_START ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   300
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_BACK ) )
dludwig@8471
   301
            SDL_PrivateJoystickButton(joystick, 5, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_BACK ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   302
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_THUMB ) )
dludwig@8471
   303
            SDL_PrivateJoystickButton(joystick, 6, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   304
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_THUMB ) )
dludwig@8471
   305
            SDL_PrivateJoystickButton(joystick, 7, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   306
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_SHOULDER ) )
dludwig@8471
   307
            SDL_PrivateJoystickButton(joystick, 8, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   308
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_SHOULDER ) )
dludwig@8471
   309
            SDL_PrivateJoystickButton(joystick, 9, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   310
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_A ) )
dludwig@8471
   311
            SDL_PrivateJoystickButton(joystick, 10, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_A ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   312
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_B ) )
dludwig@8471
   313
            SDL_PrivateJoystickButton(joystick, 11, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_B ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   314
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_X ) )
dludwig@8471
   315
            SDL_PrivateJoystickButton(joystick, 12, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_X ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   316
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_Y ) )
dludwig@8471
   317
            SDL_PrivateJoystickButton(joystick, 13, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_Y ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   318
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons,  0x400 ) )
dludwig@8471
   319
            SDL_PrivateJoystickButton(joystick, 14, pXInputState->Gamepad.wButtons & 0x400 ? SDL_PRESSED :	SDL_RELEASED ); // 0x400 is the undocumented code for the guide button
dludwig@8471
   320
    }
dludwig@8471
   321
}
dludwig@8471
   322
dludwig@8471
   323
/* Function to close a joystick after use */
dludwig@8471
   324
void
dludwig@8471
   325
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
dludwig@8471
   326
{
dludwig@8471
   327
    /* Clear cached button data on the joystick */
dludwig@8471
   328
    SDL_zero(joystick->hwdata->XInputState);
dludwig@8471
   329
dludwig@8471
   330
    /* There's need to free 'hwdata', as it's a pointer to a global array.
dludwig@8471
   331
       The field will be cleared anyways, just to indicate that it's not
dludwig@8471
   332
       currently needed.
dludwig@8471
   333
     */
dludwig@8471
   334
    joystick->hwdata = NULL;
dludwig@8471
   335
}
dludwig@8471
   336
dludwig@8471
   337
/* Function to perform any system-specific joystick related cleanup */
dludwig@8471
   338
void
dludwig@8471
   339
SDL_SYS_JoystickQuit(void)
dludwig@8471
   340
{
dludwig@8471
   341
    return;
dludwig@8471
   342
}
dludwig@8471
   343
dludwig@8471
   344
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
dludwig@8471
   345
{
dludwig@8471
   346
    SDL_JoystickGUID guid;
dludwig@8471
   347
    // the GUID is just the first 16 chars of the name for now
dludwig@8471
   348
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
dludwig@8471
   349
    SDL_zero( guid );
dludwig@8471
   350
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dludwig@8471
   351
    return guid;
dludwig@8471
   352
}
dludwig@8471
   353
dludwig@8471
   354
dludwig@8471
   355
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
dludwig@8471
   356
{
dludwig@8471
   357
    SDL_JoystickGUID guid;
dludwig@8471
   358
    // the GUID is just the first 16 chars of the name for now
dludwig@8471
   359
    const char *name = joystick->name;
dludwig@8471
   360
    SDL_zero( guid );
dludwig@8471
   361
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dludwig@8471
   362
    return guid;
dludwig@8471
   363
}
dludwig@8471
   364
dludwig@8471
   365
SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index)
dludwig@8471
   366
{
dludwig@8471
   367
    /* The XInput-capable DirectInput joystick backend implements the same
dludwig@8471
   368
       function (SDL_SYS_IsXInputDeviceIndex), however in that case, not all
dludwig@8471
   369
       joystick devices are XInput devices.  In this case, with the
dludwig@8471
   370
       WinRT-enabled XInput-only backend, all "joystick" devices are XInput
dludwig@8471
   371
       devices.
dludwig@8471
   372
     */
dludwig@8471
   373
    return SDL_TRUE;
dludwig@8471
   374
}
dludwig@8471
   375
dludwig@8471
   376
#endif /* SDL_JOYSTICK_XINPUT */
dludwig@8471
   377
dludwig@8471
   378
/* vi: set ts=4 sw=4 expandtab: */