src/joystick/windowsrt/SDL_xinputjoystick.c
author David Ludwig <dludwig@pobox.com>
Tue, 21 May 2013 20:08:53 -0400
changeset 8473 218549868b0b
parent 8471 456c005ec72c
permissions -rw-r--r--
WinRT: added hotplug support for joysticks/game-controllers
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@8473
    40
#include "../../events/SDL_events_c.h"
dludwig@8471
    41
dludwig@8471
    42
#include <Windows.h>
dludwig@8471
    43
#include <Xinput.h>
dludwig@8471
    44
dludwig@8471
    45
struct joystick_hwdata {
dludwig@8471
    46
    //Uint8 bXInputHaptic; // Supports force feedback via XInput.
dludwig@8471
    47
    DWORD userIndex;    // The XInput device index, in the range [0, XUSER_MAX_COUNT-1] (probably [0,3]).
dludwig@8471
    48
    XINPUT_STATE XInputState;   // the last-read in XInputState, kept around to compare old and new values
dludwig@8471
    49
    SDL_bool isDeviceConnected; // was the device connected (on the last polling, or during backend-initialization)?
dludwig@8473
    50
    SDL_bool isDeviceRemovalEventPending;   // was the device removed, and is the associated removal event pending?
dludwig@8471
    51
};
dludwig@8471
    52
dludwig@8471
    53
/* Keep track of data on all XInput devices, regardless of whether or not
dludwig@8471
    54
   they've been opened (via SDL_JoystickOpen).
dludwig@8471
    55
 */
dludwig@8471
    56
static struct joystick_hwdata g_XInputData[XUSER_MAX_COUNT];
dludwig@8471
    57
dludwig@8471
    58
/* Function to scan the system for joysticks.
dludwig@8471
    59
 * It should return 0, or -1 on an unrecoverable fatal error.
dludwig@8471
    60
 */
dludwig@8471
    61
int
dludwig@8471
    62
SDL_SYS_JoystickInit(void)
dludwig@8471
    63
{
dludwig@8471
    64
    HRESULT result = S_OK;
dludwig@8471
    65
    XINPUT_STATE tempXInputState;
dludwig@8471
    66
    int i;
dludwig@8471
    67
dludwig@8471
    68
    SDL_zero(g_XInputData);
dludwig@8471
    69
dludwig@8471
    70
    /* Make initial notes on whether or not devices are connected (or not).
dludwig@8471
    71
     */
dludwig@8471
    72
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8471
    73
        result = XInputGetState(i, &tempXInputState);
dludwig@8471
    74
        if (result == ERROR_SUCCESS) {
dludwig@8471
    75
            g_XInputData[i].isDeviceConnected = SDL_TRUE;
dludwig@8471
    76
        }
dludwig@8471
    77
    }
dludwig@8471
    78
dludwig@8471
    79
    return (0);
dludwig@8471
    80
}
dludwig@8471
    81
dludwig@8471
    82
int SDL_SYS_NumJoysticks()
dludwig@8471
    83
{
dludwig@8471
    84
    int joystickCount = 0;
dludwig@8471
    85
    DWORD i;
dludwig@8471
    86
dludwig@8471
    87
    /* Iterate through each possible XInput device and see if something
dludwig@8471
    88
       was connected (at joystick init, or during the last polling).
dludwig@8471
    89
     */
dludwig@8471
    90
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8471
    91
        if (g_XInputData[i].isDeviceConnected) {
dludwig@8471
    92
            ++joystickCount;
dludwig@8471
    93
        }
dludwig@8471
    94
    }
dludwig@8471
    95
dludwig@8471
    96
    return joystickCount;
dludwig@8471
    97
}
dludwig@8471
    98
dludwig@8471
    99
void SDL_SYS_JoystickDetect()
dludwig@8471
   100
{
dludwig@8473
   101
    DWORD i;
dludwig@8473
   102
    XINPUT_STATE tempXInputState;
dludwig@8473
   103
    HRESULT result;
dludwig@8473
   104
    SDL_Event event;
dludwig@8473
   105
dludwig@8473
   106
    /* Iterate through each possible XInput device, seeing if any devices
dludwig@8473
   107
       have been connected, or if they were removed.
dludwig@8473
   108
     */
dludwig@8473
   109
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8473
   110
        /* See if any new devices are connected. */
dludwig@8473
   111
        if (!g_XInputData[i].isDeviceConnected && !g_XInputData[i].isDeviceRemovalEventPending) {
dludwig@8473
   112
            result = XInputGetState(i, &tempXInputState);
dludwig@8473
   113
            if (result == ERROR_SUCCESS) {
dludwig@8473
   114
                /* Yup, a device is connected.  Mark the device as connected,
dludwig@8473
   115
                   then tell others about it (via an SDL_JOYDEVICEADDED event.)
dludwig@8473
   116
                 */
dludwig@8473
   117
                g_XInputData[i].isDeviceConnected = SDL_TRUE;
dludwig@8473
   118
dludwig@8473
   119
#if !SDL_EVENTS_DISABLED
dludwig@8473
   120
                SDL_zero(event);
dludwig@8473
   121
                event.type = SDL_JOYDEVICEADDED;
dludwig@8473
   122
                
dludwig@8473
   123
                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
dludwig@8473
   124
                    event.jdevice.which = i;
dludwig@8473
   125
                    if ((SDL_EventOK == NULL)
dludwig@8473
   126
                        || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
dludwig@8473
   127
                        SDL_PushEvent(&event);
dludwig@8473
   128
                    }
dludwig@8473
   129
                }
dludwig@8473
   130
#endif
dludwig@8473
   131
            }
dludwig@8473
   132
        } else if (g_XInputData[i].isDeviceRemovalEventPending) {
dludwig@8473
   133
            /* A device was previously marked as removed (by
dludwig@8473
   134
               SDL_SYS_JoystickUpdate).  Tell others about the device removal.
dludwig@8473
   135
            */
dludwig@8473
   136
dludwig@8473
   137
            g_XInputData[i].isDeviceRemovalEventPending = SDL_FALSE;
dludwig@8473
   138
dludwig@8473
   139
#if !SDL_EVENTS_DISABLED
dludwig@8473
   140
            SDL_zero(event);
dludwig@8473
   141
            event.type = SDL_JOYDEVICEREMOVED;
dludwig@8473
   142
                
dludwig@8473
   143
            if (SDL_GetEventState(event.type) == SDL_ENABLE) {
dludwig@8473
   144
                event.jdevice.which = i; //joystick->hwdata->userIndex;
dludwig@8473
   145
                if ((SDL_EventOK == NULL)
dludwig@8473
   146
                    || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
dludwig@8473
   147
                    SDL_PushEvent(&event);
dludwig@8473
   148
                }
dludwig@8473
   149
            }
dludwig@8473
   150
#endif
dludwig@8473
   151
        }
dludwig@8473
   152
    }
dludwig@8471
   153
}
dludwig@8471
   154
dludwig@8471
   155
SDL_bool SDL_SYS_JoystickNeedsPolling()
dludwig@8471
   156
{
dludwig@8473
   157
    /* Since XInput, or WinRT, provides any events to indicate when a game
dludwig@8473
   158
       controller gets connected, and instead indicates device availability
dludwig@8473
   159
       solely through polling, we'll poll (for new devices).
dludwig@8473
   160
     */
dludwig@8473
   161
    return SDL_TRUE;
dludwig@8471
   162
}
dludwig@8471
   163
dludwig@8471
   164
/* Internal function to retreive device capabilities.
dludwig@8471
   165
   This function will return an SDL-standard value of 0 on success
dludwig@8471
   166
   (a device is connected, and data on it was retrieved), or -1
dludwig@8471
   167
   on failure (no device was connected, or some other error
dludwig@8471
   168
   occurred.  SDL_SetError() will be invoked to set an appropriate
dludwig@8471
   169
   error message.
dludwig@8471
   170
 */
dludwig@8471
   171
static int
dludwig@8471
   172
SDL_XInput_GetDeviceCapabilities(int device_index, XINPUT_CAPABILITIES * pDeviceCaps)
dludwig@8471
   173
{
dludwig@8471
   174
    HRESULT dwResult;
dludwig@8471
   175
dludwig@8471
   176
    /* Make sure that the device index is a valid one.  If not, return to the
dludwig@8471
   177
       caller with an error.
dludwig@8471
   178
     */
dludwig@8471
   179
    if (device_index < 0 || device_index >= XUSER_MAX_COUNT) {
dludwig@8471
   180
        return SDL_SetError("invalid/unavailable device index");
dludwig@8471
   181
    }
dludwig@8471
   182
dludwig@8471
   183
    /* See if a device exists, and if so, what its capabilities are.  If a
dludwig@8471
   184
       device is not available, return to the caller with an error.
dludwig@8471
   185
     */
dludwig@8471
   186
    switch ((dwResult = XInputGetCapabilities(device_index, 0, pDeviceCaps))) {
dludwig@8471
   187
        case ERROR_SUCCESS:
dludwig@8471
   188
            /* A device is available, and its capabilities were retrieved! */
dludwig@8471
   189
            return 0;
dludwig@8471
   190
        case ERROR_DEVICE_NOT_CONNECTED:
dludwig@8471
   191
            return SDL_SetError("no device is connected at joystick index, %d", device_index);
dludwig@8471
   192
        default:
dludwig@8471
   193
            return SDL_SetError("an unknown error occurred when retrieving info on a device at joystick index, %d", device_index);
dludwig@8471
   194
    }
dludwig@8471
   195
}
dludwig@8471
   196
dludwig@8471
   197
/* Function to get the device-dependent name of a joystick */
dludwig@8471
   198
const char *
dludwig@8471
   199
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
dludwig@8471
   200
{
dludwig@8471
   201
    XINPUT_CAPABILITIES deviceCaps;
dludwig@8471
   202
dludwig@8471
   203
    if (SDL_XInput_GetDeviceCapabilities(device_index, &deviceCaps) != 0) {
dludwig@8471
   204
        /* Uh oh.  Device capabilities couldn't be retrieved.  Return to the
dludwig@8471
   205
           caller.  SDL_SetError() has already been invoked (with relevant
dludwig@8471
   206
           information).
dludwig@8471
   207
         */
dludwig@8471
   208
        return NULL;
dludwig@8471
   209
    }
dludwig@8471
   210
dludwig@8471
   211
    switch (deviceCaps.SubType) {
dludwig@8471
   212
        default:
dludwig@8471
   213
            if (deviceCaps.Type == XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   214
                return "Undefined game controller";
dludwig@8471
   215
            } else {
dludwig@8471
   216
                return "Undefined controller";
dludwig@8471
   217
            }
dludwig@8471
   218
        case XINPUT_DEVSUBTYPE_UNKNOWN:
dludwig@8471
   219
            if (deviceCaps.Type == XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   220
                return "Unknown game controller";
dludwig@8471
   221
            } else {
dludwig@8471
   222
                return "Unknown controller";
dludwig@8471
   223
            }
dludwig@8471
   224
        case XINPUT_DEVSUBTYPE_GAMEPAD:
dludwig@8471
   225
            return "Gamepad controller";
dludwig@8471
   226
        case XINPUT_DEVSUBTYPE_WHEEL:
dludwig@8471
   227
            return "Racing wheel controller";
dludwig@8471
   228
        case XINPUT_DEVSUBTYPE_ARCADE_STICK:
dludwig@8471
   229
            return "Arcade stick controller";
dludwig@8471
   230
        case XINPUT_DEVSUBTYPE_FLIGHT_STICK:
dludwig@8471
   231
            return "Flight stick controller";
dludwig@8471
   232
        case XINPUT_DEVSUBTYPE_DANCE_PAD:
dludwig@8471
   233
            return "Dance pad controller";
dludwig@8471
   234
        case XINPUT_DEVSUBTYPE_GUITAR:
dludwig@8471
   235
            return "Guitar controller";
dludwig@8471
   236
        case XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE:
dludwig@8471
   237
            return "Guitar controller, Alternate";
dludwig@8471
   238
        case XINPUT_DEVSUBTYPE_GUITAR_BASS:
dludwig@8471
   239
            return "Guitar controller, Bass";
dludwig@8471
   240
        case XINPUT_DEVSUBTYPE_DRUM_KIT:
dludwig@8471
   241
            return "Drum controller";
dludwig@8471
   242
        case XINPUT_DEVSUBTYPE_ARCADE_PAD:
dludwig@8471
   243
            return "Arcade pad controller";
dludwig@8471
   244
    }
dludwig@8471
   245
}
dludwig@8471
   246
dludwig@8471
   247
/* Function to perform the mapping from device index to the instance id for this index */
dludwig@8471
   248
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
dludwig@8471
   249
{
dludwig@8471
   250
    return device_index;
dludwig@8471
   251
}
dludwig@8471
   252
dludwig@8471
   253
/* Function to open a joystick for use.
dludwig@8471
   254
   The joystick to open is specified by the index field of the joystick.
dludwig@8471
   255
   This should fill the nbuttons and naxes fields of the joystick structure.
dludwig@8471
   256
   It returns 0, or -1 if there is an error.
dludwig@8471
   257
 */
dludwig@8471
   258
int
dludwig@8471
   259
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
dludwig@8471
   260
{
dludwig@8471
   261
    XINPUT_CAPABILITIES deviceCaps;
dludwig@8471
   262
dludwig@8471
   263
    if (SDL_XInput_GetDeviceCapabilities(device_index, &deviceCaps) != 0) {
dludwig@8471
   264
        /* Uh oh.  Device capabilities couldn't be retrieved.  Return to the
dludwig@8471
   265
           caller.  SDL_SetError() has already been invoked (with relevant
dludwig@8471
   266
           information).
dludwig@8471
   267
         */
dludwig@8471
   268
        return -1;
dludwig@8471
   269
    }
dludwig@8471
   270
dludwig@8471
   271
    /* For now, only game pads are supported.  If the device is something other
dludwig@8471
   272
       than that, return an error to the caller.
dludwig@8471
   273
     */
dludwig@8471
   274
    if (deviceCaps.Type != XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   275
        return SDL_SetError("a device is connected (at joystick index, %d), but it is of an unknown device type (deviceCaps.Flags=%ul)",
dludwig@8471
   276
            device_index, (unsigned int)deviceCaps.Flags);
dludwig@8471
   277
    }
dludwig@8471
   278
dludwig@8471
   279
    /* Create the joystick data structure */
dludwig@8471
   280
    joystick->instance_id = device_index;
dludwig@8471
   281
    joystick->hwdata = &g_XInputData[device_index];
dludwig@8471
   282
dludwig@8471
   283
    // The XInput API has a hard coded button/axis mapping, so we just match it
dludwig@8471
   284
    joystick->naxes = 6;
dludwig@8471
   285
    joystick->nbuttons = 15;
dludwig@8471
   286
    joystick->nballs = 0;
dludwig@8471
   287
    joystick->nhats = 0;
dludwig@8471
   288
dludwig@8471
   289
    /* We're done! */
dludwig@8471
   290
    return (0);
dludwig@8471
   291
}
dludwig@8471
   292
dludwig@8471
   293
/* Function to determine is this joystick is attached to the system right now */
dludwig@8471
   294
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
dludwig@8471
   295
{
dludwig@8471
   296
    return joystick->hwdata->isDeviceConnected;
dludwig@8471
   297
}
dludwig@8471
   298
dludwig@8471
   299
/* Function to return > 0 if a bit array of buttons differs after applying a mask
dludwig@8471
   300
*/
dludwig@8471
   301
static int ButtonChanged( int ButtonsNow, int ButtonsPrev, int ButtonMask )
dludwig@8471
   302
{
dludwig@8471
   303
    return ( ButtonsNow & ButtonMask ) != ( ButtonsPrev & ButtonMask );
dludwig@8471
   304
}
dludwig@8471
   305
dludwig@8471
   306
/* Function to update the state of a joystick - called as a device poll.
dludwig@8471
   307
 * This function shouldn't update the joystick structure directly,
dludwig@8471
   308
 * but instead should call SDL_PrivateJoystick*() to deliver events
dludwig@8471
   309
 * and update joystick device state.
dludwig@8471
   310
 */
dludwig@8471
   311
void
dludwig@8471
   312
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
dludwig@8471
   313
{
dludwig@8471
   314
    HRESULT result;
dludwig@8471
   315
dludwig@8471
   316
    /* Before polling for new data, make note of the old data */
dludwig@8471
   317
    XINPUT_STATE prevXInputState = joystick->hwdata->XInputState;
dludwig@8471
   318
dludwig@8471
   319
    /* Poll for new data */
dludwig@8471
   320
    result = XInputGetState(joystick->hwdata->userIndex, &joystick->hwdata->XInputState);
dludwig@8471
   321
    if (result == ERROR_DEVICE_NOT_CONNECTED) {
dludwig@8473
   322
        if (joystick->hwdata->isDeviceConnected) {
dludwig@8473
   323
            joystick->hwdata->isDeviceConnected = SDL_FALSE;
dludwig@8473
   324
            joystick->hwdata->isDeviceRemovalEventPending = SDL_TRUE;
dludwig@8473
   325
            /* TODO, WinRT: make sure isDeviceRemovalEventPending gets cleared as appropriate, and that quick re-plugs don't cause trouble */
dludwig@8473
   326
        }
dludwig@8471
   327
        return;
dludwig@8471
   328
    }
dludwig@8471
   329
dludwig@8471
   330
    /* Make sure the device is marked as connected */
dludwig@8471
   331
    joystick->hwdata->isDeviceConnected = SDL_TRUE;
dludwig@8471
   332
dludwig@8471
   333
    // only fire events if the data changed from last time
dludwig@8471
   334
    if ( joystick->hwdata->XInputState.dwPacketNumber != 0 
dludwig@8471
   335
        && joystick->hwdata->XInputState.dwPacketNumber != prevXInputState.dwPacketNumber )
dludwig@8471
   336
    {
dludwig@8471
   337
        XINPUT_STATE *pXInputState = &joystick->hwdata->XInputState;
dludwig@8471
   338
        XINPUT_STATE *pXInputStatePrev = &prevXInputState;
dludwig@8471
   339
dludwig@8471
   340
        SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX );
dludwig@8471
   341
        SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-1*pXInputState->Gamepad.sThumbLY-1) );
dludwig@8471
   342
        SDL_PrivateJoystickAxis(joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX );
dludwig@8471
   343
        SDL_PrivateJoystickAxis(joystick, 3, (Sint16)(-1*pXInputState->Gamepad.sThumbRY-1) );
dludwig@8471
   344
        SDL_PrivateJoystickAxis(joystick, 4, (Sint16)((int)pXInputState->Gamepad.bLeftTrigger*32767/255) );
dludwig@8471
   345
        SDL_PrivateJoystickAxis(joystick, 5, (Sint16)((int)pXInputState->Gamepad.bRightTrigger*32767/255) );
dludwig@8471
   346
dludwig@8471
   347
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_UP ) )
dludwig@8471
   348
            SDL_PrivateJoystickButton(joystick, 0, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   349
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_DOWN ) )
dludwig@8471
   350
            SDL_PrivateJoystickButton(joystick, 1, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   351
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_LEFT ) )
dludwig@8471
   352
            SDL_PrivateJoystickButton(joystick, 2, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   353
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_RIGHT ) )
dludwig@8471
   354
            SDL_PrivateJoystickButton(joystick, 3, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   355
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_START ) )
dludwig@8471
   356
            SDL_PrivateJoystickButton(joystick, 4, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_START ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   357
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_BACK ) )
dludwig@8471
   358
            SDL_PrivateJoystickButton(joystick, 5, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_BACK ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   359
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_THUMB ) )
dludwig@8471
   360
            SDL_PrivateJoystickButton(joystick, 6, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   361
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_THUMB ) )
dludwig@8471
   362
            SDL_PrivateJoystickButton(joystick, 7, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   363
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_SHOULDER ) )
dludwig@8471
   364
            SDL_PrivateJoystickButton(joystick, 8, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   365
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_SHOULDER ) )
dludwig@8471
   366
            SDL_PrivateJoystickButton(joystick, 9, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   367
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_A ) )
dludwig@8471
   368
            SDL_PrivateJoystickButton(joystick, 10, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_A ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   369
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_B ) )
dludwig@8471
   370
            SDL_PrivateJoystickButton(joystick, 11, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_B ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   371
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_X ) )
dludwig@8471
   372
            SDL_PrivateJoystickButton(joystick, 12, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_X ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   373
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_Y ) )
dludwig@8471
   374
            SDL_PrivateJoystickButton(joystick, 13, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_Y ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8471
   375
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons,  0x400 ) )
dludwig@8471
   376
            SDL_PrivateJoystickButton(joystick, 14, pXInputState->Gamepad.wButtons & 0x400 ? SDL_PRESSED :	SDL_RELEASED ); // 0x400 is the undocumented code for the guide button
dludwig@8471
   377
    }
dludwig@8471
   378
}
dludwig@8471
   379
dludwig@8471
   380
/* Function to close a joystick after use */
dludwig@8471
   381
void
dludwig@8471
   382
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
dludwig@8471
   383
{
dludwig@8471
   384
    /* Clear cached button data on the joystick */
dludwig@8471
   385
    SDL_zero(joystick->hwdata->XInputState);
dludwig@8471
   386
dludwig@8471
   387
    /* There's need to free 'hwdata', as it's a pointer to a global array.
dludwig@8471
   388
       The field will be cleared anyways, just to indicate that it's not
dludwig@8471
   389
       currently needed.
dludwig@8471
   390
     */
dludwig@8471
   391
    joystick->hwdata = NULL;
dludwig@8471
   392
}
dludwig@8471
   393
dludwig@8471
   394
/* Function to perform any system-specific joystick related cleanup */
dludwig@8471
   395
void
dludwig@8471
   396
SDL_SYS_JoystickQuit(void)
dludwig@8471
   397
{
dludwig@8471
   398
    return;
dludwig@8471
   399
}
dludwig@8471
   400
dludwig@8471
   401
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
dludwig@8471
   402
{
dludwig@8471
   403
    SDL_JoystickGUID guid;
dludwig@8471
   404
    // the GUID is just the first 16 chars of the name for now
dludwig@8471
   405
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
dludwig@8471
   406
    SDL_zero( guid );
dludwig@8471
   407
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dludwig@8471
   408
    return guid;
dludwig@8471
   409
}
dludwig@8471
   410
dludwig@8471
   411
dludwig@8471
   412
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
dludwig@8471
   413
{
dludwig@8471
   414
    SDL_JoystickGUID guid;
dludwig@8471
   415
    // the GUID is just the first 16 chars of the name for now
dludwig@8471
   416
    const char *name = joystick->name;
dludwig@8471
   417
    SDL_zero( guid );
dludwig@8471
   418
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dludwig@8471
   419
    return guid;
dludwig@8471
   420
}
dludwig@8471
   421
dludwig@8471
   422
SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index)
dludwig@8471
   423
{
dludwig@8471
   424
    /* The XInput-capable DirectInput joystick backend implements the same
dludwig@8471
   425
       function (SDL_SYS_IsXInputDeviceIndex), however in that case, not all
dludwig@8471
   426
       joystick devices are XInput devices.  In this case, with the
dludwig@8471
   427
       WinRT-enabled XInput-only backend, all "joystick" devices are XInput
dludwig@8471
   428
       devices.
dludwig@8471
   429
     */
dludwig@8471
   430
    return SDL_TRUE;
dludwig@8471
   431
}
dludwig@8471
   432
dludwig@8471
   433
#endif /* SDL_JOYSTICK_XINPUT */
dludwig@8471
   434
dludwig@8471
   435
/* vi: set ts=4 sw=4 expandtab: */