src/joystick/winrt/SDL_xinputjoystick.c
author David Ludwig <dludwig@pobox.com>
Tue, 24 Dec 2013 21:28:31 -0500
changeset 8552 abd934eac415
parent 8502 78d341895e9f
child 8562 f1aac2d56c76
permissions -rw-r--r--
WinRT: moved ill-performing XInput device-detection calls to a separate thread
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@8552
    41
#include "SDL_timer.h"
dludwig@8471
    42
dludwig@8471
    43
#include <Windows.h>
dludwig@8471
    44
#include <Xinput.h>
dludwig@8471
    45
dludwig@8471
    46
struct joystick_hwdata {
dludwig@8552
    47
    //Uint8 bXInputHaptic; // Supports force feedback via XInput.
dludwig@8552
    48
    DWORD userIndex;    // The XInput device index, in the range [0, XUSER_MAX_COUNT-1] (probably [0,3]).
dludwig@8552
    49
    XINPUT_STATE XInputState;   // the last-read in XInputState, kept around to compare old and new values
dludwig@8552
    50
    SDL_bool isDeviceConnected; // was the device connected (on the last detection-polling, or during backend-initialization)?
dludwig@8552
    51
    SDL_bool isDeviceConnectionEventPending;    // was a device added, and is the associated add-event pending?
dludwig@8552
    52
    SDL_bool isDeviceRemovalEventPending;   // was the device removed, and is the associated remove-event pending?
dludwig@8471
    53
};
dludwig@8471
    54
dludwig@8471
    55
/* Keep track of data on all XInput devices, regardless of whether or not
dludwig@8471
    56
   they've been opened (via SDL_JoystickOpen).
dludwig@8471
    57
 */
dludwig@8471
    58
static struct joystick_hwdata g_XInputData[XUSER_MAX_COUNT];
dludwig@8471
    59
dludwig@8552
    60
/* Device detection can be extremely costly performance-wise, in some cases.
dludwig@8552
    61
   In particular, if no devices are connected, calls to detect a single device,
dludwig@8552
    62
   via either XInputGetState() or XInputGetCapabilities(), can take upwards of
dludwig@8552
    63
   20 ms on a 1st generation Surface RT, more if devices are detected across
dludwig@8552
    64
   all of of XInput's four device slots.  WinRT and XInput do not appear to
dludwig@8552
    65
   have callback-based APIs to notify an app when a device is connected, at
dludwig@8552
    66
   least as of Windows 8.1.  The synchronous XInput calls must be used.
dludwig@8552
    67
dludwig@8552
    68
   Once a device is connected, calling XInputGetState() is a much less costly
dludwig@8552
    69
   operation, with individual calls costing well under 1 ms, and often under
dludwig@8552
    70
   0.1 ms [on a 1st gen Surface RT].
dludwig@8552
    71
dludwig@8552
    72
   With XInput's performance limitations in mind, a separate device-detection
dludwig@8552
    73
   thread will be utilized (by SDL) to try to move costly XInput calls off the
dludwig@8552
    74
   main thread.  Polling of active devices still, however, occurs on the main
dludwig@8552
    75
   thread.
dludwig@8552
    76
 */
dludwig@8552
    77
static SDL_Thread * g_DeviceDetectionThread = NULL;
dludwig@8552
    78
static SDL_mutex * g_DeviceInfoLock = NULL;
dludwig@8552
    79
static SDL_bool g_DeviceDetectionQuit = SDL_FALSE;
dludwig@8552
    80
dludwig@8552
    81
/* Main function for the device-detection thread.
dludwig@8552
    82
 */
dludwig@8552
    83
static int
dludwig@8552
    84
DeviceDetectionThreadMain(void * _data)
dludwig@8552
    85
{
dludwig@8552
    86
    DWORD result;
dludwig@8552
    87
    XINPUT_CAPABILITIES tempXInputCaps;
dludwig@8552
    88
    int i;
dludwig@8552
    89
dludwig@8552
    90
    while (1) {
dludwig@8552
    91
        /* See if the device-detection thread is being asked to shutdown.
dludwig@8552
    92
         */
dludwig@8552
    93
        SDL_LockMutex(g_DeviceInfoLock);
dludwig@8552
    94
        if (g_DeviceDetectionQuit) {
dludwig@8552
    95
            SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8552
    96
            break;
dludwig@8552
    97
        }
dludwig@8552
    98
        SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8552
    99
dludwig@8552
   100
        /* Add a short delay to prevent the device-detection thread from eating
dludwig@8552
   101
           up too much CPU time:
dludwig@8552
   102
         */
dludwig@8552
   103
        SDL_Delay(300);
dludwig@8552
   104
dludwig@8552
   105
        /* TODO, WinRT: try making the device-detection thread wakeup sooner from its CPU-preserving SDL_Delay, if the thread was asked to quit.
dludwig@8552
   106
         */
dludwig@8552
   107
dludwig@8552
   108
        /* See if any new devices are connected. */
dludwig@8552
   109
        for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8552
   110
            if (!g_XInputData[i].isDeviceConnected &&
dludwig@8552
   111
                !g_XInputData[i].isDeviceConnectionEventPending &&
dludwig@8552
   112
                !g_XInputData[i].isDeviceRemovalEventPending)
dludwig@8552
   113
            {
dludwig@8552
   114
                result = XInputGetCapabilities(i, 0, &tempXInputCaps);
dludwig@8552
   115
                if (result == ERROR_SUCCESS) {
dludwig@8552
   116
                    /* Yes, a device is connected.  Mark it as such.
dludwig@8552
   117
                       Others will be told about this (via an
dludwig@8552
   118
                       SDL_JOYDEVICEADDED event) in the next call to
dludwig@8552
   119
                       SDL_SYS_JoystickDetect.
dludwig@8552
   120
                     */
dludwig@8552
   121
                    SDL_LockMutex(g_DeviceInfoLock);
dludwig@8552
   122
                    g_XInputData[i].isDeviceConnected = SDL_TRUE;
dludwig@8552
   123
                    g_XInputData[i].isDeviceConnectionEventPending = SDL_TRUE;
dludwig@8552
   124
                    SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8552
   125
                }
dludwig@8552
   126
            }
dludwig@8552
   127
        }
dludwig@8552
   128
    }
dludwig@8552
   129
dludwig@8552
   130
    return 0;
dludwig@8552
   131
}
dludwig@8552
   132
dludwig@8471
   133
/* Function to scan the system for joysticks.
dludwig@8471
   134
 * It should return 0, or -1 on an unrecoverable fatal error.
dludwig@8471
   135
 */
dludwig@8471
   136
int
dludwig@8471
   137
SDL_SYS_JoystickInit(void)
dludwig@8471
   138
{
dludwig@8471
   139
    HRESULT result = S_OK;
dludwig@8471
   140
    XINPUT_STATE tempXInputState;
dludwig@8471
   141
    int i;
dludwig@8471
   142
dludwig@8471
   143
    SDL_zero(g_XInputData);
dludwig@8471
   144
dludwig@8471
   145
    /* Make initial notes on whether or not devices are connected (or not).
dludwig@8471
   146
     */
dludwig@8471
   147
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8471
   148
        result = XInputGetState(i, &tempXInputState);
dludwig@8471
   149
        if (result == ERROR_SUCCESS) {
dludwig@8471
   150
            g_XInputData[i].isDeviceConnected = SDL_TRUE;
dludwig@8471
   151
        }
dludwig@8471
   152
    }
dludwig@8471
   153
dludwig@8552
   154
    /* Start up the device-detection thread.
dludwig@8552
   155
     */
dludwig@8552
   156
    g_DeviceDetectionQuit = SDL_FALSE;
dludwig@8552
   157
    g_DeviceInfoLock = SDL_CreateMutex();
dludwig@8552
   158
    g_DeviceDetectionThread = SDL_CreateThread(DeviceDetectionThreadMain, "SDL_joystick", NULL);
dludwig@8552
   159
dludwig@8471
   160
    return (0);
dludwig@8471
   161
}
dludwig@8471
   162
dludwig@8471
   163
int SDL_SYS_NumJoysticks()
dludwig@8471
   164
{
dludwig@8471
   165
    int joystickCount = 0;
dludwig@8471
   166
    DWORD i;
dludwig@8471
   167
dludwig@8471
   168
    /* Iterate through each possible XInput device and see if something
dludwig@8471
   169
       was connected (at joystick init, or during the last polling).
dludwig@8471
   170
     */
dludwig@8552
   171
    SDL_LockMutex(g_DeviceInfoLock);
dludwig@8471
   172
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8471
   173
        if (g_XInputData[i].isDeviceConnected) {
dludwig@8471
   174
            ++joystickCount;
dludwig@8471
   175
        }
dludwig@8471
   176
    }
dludwig@8552
   177
    SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8471
   178
dludwig@8471
   179
    return joystickCount;
dludwig@8471
   180
}
dludwig@8471
   181
dludwig@8471
   182
void SDL_SYS_JoystickDetect()
dludwig@8471
   183
{
dludwig@8473
   184
    DWORD i;
dludwig@8473
   185
    SDL_Event event;
dludwig@8473
   186
dludwig@8473
   187
    /* Iterate through each possible XInput device, seeing if any devices
dludwig@8473
   188
       have been connected, or if they were removed.
dludwig@8473
   189
     */
dludwig@8552
   190
    SDL_LockMutex(g_DeviceInfoLock);
dludwig@8473
   191
    for (i = 0; i < XUSER_MAX_COUNT; ++i) {
dludwig@8473
   192
        /* See if any new devices are connected. */
dludwig@8552
   193
        if (g_XInputData[i].isDeviceConnectionEventPending) {
dludwig@8473
   194
#if !SDL_EVENTS_DISABLED
dludwig@8552
   195
            SDL_zero(event);
dludwig@8552
   196
            event.type = SDL_JOYDEVICEADDED;
dludwig@8552
   197
                
dludwig@8552
   198
            if (SDL_GetEventState(event.type) == SDL_ENABLE) {
dludwig@8552
   199
                event.jdevice.which = i;
dludwig@8552
   200
                if ((SDL_EventOK == NULL)
dludwig@8552
   201
                    || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
dludwig@8552
   202
                    SDL_PushEvent(&event);
dludwig@8473
   203
                }
dludwig@8552
   204
            }
dludwig@8473
   205
#endif
dludwig@8552
   206
            g_XInputData[i].isDeviceConnectionEventPending = SDL_FALSE;
dludwig@8473
   207
        } else if (g_XInputData[i].isDeviceRemovalEventPending) {
dludwig@8473
   208
            /* A device was previously marked as removed (by
dludwig@8473
   209
               SDL_SYS_JoystickUpdate).  Tell others about the device removal.
dludwig@8473
   210
            */
dludwig@8473
   211
dludwig@8473
   212
            g_XInputData[i].isDeviceRemovalEventPending = SDL_FALSE;
dludwig@8473
   213
dludwig@8473
   214
#if !SDL_EVENTS_DISABLED
dludwig@8552
   215
            SDL_zero(event);
dludwig@8552
   216
            event.type = SDL_JOYDEVICEREMOVED;
dludwig@8552
   217
                
dludwig@8552
   218
            if (SDL_GetEventState(event.type) == SDL_ENABLE) {
dludwig@8552
   219
                event.jdevice.which = i; //joystick->hwdata->userIndex;
dludwig@8552
   220
                if ((SDL_EventOK == NULL)
dludwig@8552
   221
                    || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
dludwig@8552
   222
                    SDL_PushEvent(&event);
dludwig@8552
   223
                }
dludwig@8473
   224
            }
dludwig@8552
   225
#endif
dludwig@8473
   226
        }
dludwig@8473
   227
    }
dludwig@8552
   228
    SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8471
   229
}
dludwig@8471
   230
dludwig@8471
   231
SDL_bool SDL_SYS_JoystickNeedsPolling()
dludwig@8471
   232
{
dludwig@8473
   233
    /* Since XInput, or WinRT, provides any events to indicate when a game
dludwig@8473
   234
       controller gets connected, and instead indicates device availability
dludwig@8473
   235
       solely through polling, we'll poll (for new devices).
dludwig@8473
   236
     */
dludwig@8473
   237
    return SDL_TRUE;
dludwig@8471
   238
}
dludwig@8471
   239
dludwig@8471
   240
/* Internal function to retreive device capabilities.
dludwig@8471
   241
   This function will return an SDL-standard value of 0 on success
dludwig@8471
   242
   (a device is connected, and data on it was retrieved), or -1
dludwig@8471
   243
   on failure (no device was connected, or some other error
dludwig@8471
   244
   occurred.  SDL_SetError() will be invoked to set an appropriate
dludwig@8471
   245
   error message.
dludwig@8471
   246
 */
dludwig@8471
   247
static int
dludwig@8471
   248
SDL_XInput_GetDeviceCapabilities(int device_index, XINPUT_CAPABILITIES * pDeviceCaps)
dludwig@8471
   249
{
dludwig@8471
   250
    HRESULT dwResult;
dludwig@8471
   251
dludwig@8471
   252
    /* Make sure that the device index is a valid one.  If not, return to the
dludwig@8471
   253
       caller with an error.
dludwig@8471
   254
     */
dludwig@8471
   255
    if (device_index < 0 || device_index >= XUSER_MAX_COUNT) {
dludwig@8471
   256
        return SDL_SetError("invalid/unavailable device index");
dludwig@8471
   257
    }
dludwig@8471
   258
dludwig@8471
   259
    /* See if a device exists, and if so, what its capabilities are.  If a
dludwig@8471
   260
       device is not available, return to the caller with an error.
dludwig@8471
   261
     */
dludwig@8471
   262
    switch ((dwResult = XInputGetCapabilities(device_index, 0, pDeviceCaps))) {
dludwig@8471
   263
        case ERROR_SUCCESS:
dludwig@8471
   264
            /* A device is available, and its capabilities were retrieved! */
dludwig@8471
   265
            return 0;
dludwig@8471
   266
        case ERROR_DEVICE_NOT_CONNECTED:
dludwig@8471
   267
            return SDL_SetError("no device is connected at joystick index, %d", device_index);
dludwig@8471
   268
        default:
dludwig@8471
   269
            return SDL_SetError("an unknown error occurred when retrieving info on a device at joystick index, %d", device_index);
dludwig@8471
   270
    }
dludwig@8471
   271
}
dludwig@8471
   272
dludwig@8471
   273
/* Function to get the device-dependent name of a joystick */
dludwig@8471
   274
const char *
dludwig@8471
   275
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
dludwig@8471
   276
{
dludwig@8471
   277
    XINPUT_CAPABILITIES deviceCaps;
dludwig@8471
   278
dludwig@8471
   279
    if (SDL_XInput_GetDeviceCapabilities(device_index, &deviceCaps) != 0) {
dludwig@8471
   280
        /* Uh oh.  Device capabilities couldn't be retrieved.  Return to the
dludwig@8471
   281
           caller.  SDL_SetError() has already been invoked (with relevant
dludwig@8471
   282
           information).
dludwig@8471
   283
         */
dludwig@8471
   284
        return NULL;
dludwig@8471
   285
    }
dludwig@8471
   286
dludwig@8471
   287
    switch (deviceCaps.SubType) {
dludwig@8471
   288
        default:
dludwig@8471
   289
            if (deviceCaps.Type == XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   290
                return "Undefined game controller";
dludwig@8471
   291
            } else {
dludwig@8471
   292
                return "Undefined controller";
dludwig@8471
   293
            }
dludwig@8471
   294
        case XINPUT_DEVSUBTYPE_UNKNOWN:
dludwig@8471
   295
            if (deviceCaps.Type == XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   296
                return "Unknown game controller";
dludwig@8471
   297
            } else {
dludwig@8471
   298
                return "Unknown controller";
dludwig@8471
   299
            }
dludwig@8471
   300
        case XINPUT_DEVSUBTYPE_GAMEPAD:
dludwig@8471
   301
            return "Gamepad controller";
dludwig@8471
   302
        case XINPUT_DEVSUBTYPE_WHEEL:
dludwig@8471
   303
            return "Racing wheel controller";
dludwig@8471
   304
        case XINPUT_DEVSUBTYPE_ARCADE_STICK:
dludwig@8471
   305
            return "Arcade stick controller";
dludwig@8471
   306
        case XINPUT_DEVSUBTYPE_FLIGHT_STICK:
dludwig@8471
   307
            return "Flight stick controller";
dludwig@8471
   308
        case XINPUT_DEVSUBTYPE_DANCE_PAD:
dludwig@8471
   309
            return "Dance pad controller";
dludwig@8471
   310
        case XINPUT_DEVSUBTYPE_GUITAR:
dludwig@8471
   311
            return "Guitar controller";
dludwig@8471
   312
        case XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE:
dludwig@8471
   313
            return "Guitar controller, Alternate";
dludwig@8471
   314
        case XINPUT_DEVSUBTYPE_GUITAR_BASS:
dludwig@8471
   315
            return "Guitar controller, Bass";
dludwig@8471
   316
        case XINPUT_DEVSUBTYPE_DRUM_KIT:
dludwig@8471
   317
            return "Drum controller";
dludwig@8471
   318
        case XINPUT_DEVSUBTYPE_ARCADE_PAD:
dludwig@8471
   319
            return "Arcade pad controller";
dludwig@8471
   320
    }
dludwig@8471
   321
}
dludwig@8471
   322
dludwig@8471
   323
/* Function to perform the mapping from device index to the instance id for this index */
dludwig@8471
   324
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
dludwig@8471
   325
{
dludwig@8471
   326
    return device_index;
dludwig@8471
   327
}
dludwig@8471
   328
dludwig@8471
   329
/* Function to open a joystick for use.
dludwig@8471
   330
   The joystick to open is specified by the index field of the joystick.
dludwig@8471
   331
   This should fill the nbuttons and naxes fields of the joystick structure.
dludwig@8471
   332
   It returns 0, or -1 if there is an error.
dludwig@8471
   333
 */
dludwig@8471
   334
int
dludwig@8471
   335
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
dludwig@8471
   336
{
dludwig@8471
   337
    XINPUT_CAPABILITIES deviceCaps;
dludwig@8471
   338
dludwig@8471
   339
    if (SDL_XInput_GetDeviceCapabilities(device_index, &deviceCaps) != 0) {
dludwig@8471
   340
        /* Uh oh.  Device capabilities couldn't be retrieved.  Return to the
dludwig@8471
   341
           caller.  SDL_SetError() has already been invoked (with relevant
dludwig@8471
   342
           information).
dludwig@8471
   343
         */
dludwig@8471
   344
        return -1;
dludwig@8471
   345
    }
dludwig@8471
   346
dludwig@8471
   347
    /* For now, only game pads are supported.  If the device is something other
dludwig@8471
   348
       than that, return an error to the caller.
dludwig@8471
   349
     */
dludwig@8471
   350
    if (deviceCaps.Type != XINPUT_DEVTYPE_GAMEPAD) {
dludwig@8471
   351
        return SDL_SetError("a device is connected (at joystick index, %d), but it is of an unknown device type (deviceCaps.Flags=%ul)",
dludwig@8471
   352
            device_index, (unsigned int)deviceCaps.Flags);
dludwig@8471
   353
    }
dludwig@8471
   354
dludwig@8552
   355
    /* Create the joystick data structure */
dludwig@8552
   356
    joystick->instance_id = device_index;
dludwig@8552
   357
    joystick->hwdata = &g_XInputData[device_index];
dludwig@8552
   358
dludwig@8552
   359
    // The XInput API has a hard coded button/axis mapping, so we just match it
dludwig@8552
   360
    joystick->naxes = 6;
dludwig@8552
   361
    joystick->nbuttons = 15;
dludwig@8552
   362
    joystick->nballs = 0;
dludwig@8552
   363
    joystick->nhats = 0;
dludwig@8552
   364
dludwig@8552
   365
    /* We're done! */
dludwig@8471
   366
    return (0);
dludwig@8471
   367
}
dludwig@8471
   368
dludwig@8471
   369
/* Function to determine is this joystick is attached to the system right now */
dludwig@8471
   370
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
dludwig@8471
   371
{
dludwig@8552
   372
    SDL_bool isDeviceConnected;
dludwig@8552
   373
    SDL_LockMutex(g_DeviceInfoLock);
dludwig@8552
   374
    isDeviceConnected = joystick->hwdata->isDeviceConnected;
dludwig@8552
   375
    SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8552
   376
    return isDeviceConnected;
dludwig@8471
   377
}
dludwig@8471
   378
dludwig@8552
   379
/* Function to return > 0 if a bit array of buttons differs after applying a mask
dludwig@8552
   380
*/
dludwig@8552
   381
static int ButtonChanged( int ButtonsNow, int ButtonsPrev, int ButtonMask )
dludwig@8552
   382
{
dludwig@8552
   383
    return ( ButtonsNow & ButtonMask ) != ( ButtonsPrev & ButtonMask );
dludwig@8471
   384
}
dludwig@8471
   385
dludwig@8471
   386
/* Function to update the state of a joystick - called as a device poll.
dludwig@8471
   387
 * This function shouldn't update the joystick structure directly,
dludwig@8471
   388
 * but instead should call SDL_PrivateJoystick*() to deliver events
dludwig@8471
   389
 * and update joystick device state.
dludwig@8471
   390
 */
dludwig@8471
   391
void
dludwig@8471
   392
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
dludwig@8471
   393
{
dludwig@8552
   394
    HRESULT result;
dludwig@8552
   395
    XINPUT_STATE prevXInputState;
dludwig@8552
   396
dludwig@8552
   397
    SDL_LockMutex(g_DeviceInfoLock);
dludwig@8552
   398
dludwig@8552
   399
    /* Before polling for new data, make note of the old data */
dludwig@8552
   400
    prevXInputState = joystick->hwdata->XInputState;
dludwig@8552
   401
dludwig@8552
   402
    /* Poll for new data */
dludwig@8552
   403
    result = XInputGetState(joystick->hwdata->userIndex, &joystick->hwdata->XInputState);
dludwig@8552
   404
    if (result == ERROR_DEVICE_NOT_CONNECTED) {
dludwig@8552
   405
        if (joystick->hwdata->isDeviceConnected) {
dludwig@8552
   406
            joystick->hwdata->isDeviceConnected = SDL_FALSE;
dludwig@8552
   407
            joystick->hwdata->isDeviceRemovalEventPending = SDL_TRUE;
dludwig@8552
   408
            /* TODO, WinRT: make sure isDeviceRemovalEventPending gets cleared as appropriate, and that quick re-plugs don't cause trouble */
dludwig@8552
   409
        }
dludwig@8552
   410
        SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8552
   411
        return;
dludwig@8471
   412
    }
dludwig@8552
   413
dludwig@8552
   414
    /* Make sure the device is marked as connected */
dludwig@8552
   415
    joystick->hwdata->isDeviceConnected = SDL_TRUE;
dludwig@8552
   416
dludwig@8552
   417
    // only fire events if the data changed from last time
dludwig@8552
   418
    if ( joystick->hwdata->XInputState.dwPacketNumber != 0 
dludwig@8552
   419
        && joystick->hwdata->XInputState.dwPacketNumber != prevXInputState.dwPacketNumber )
dludwig@8552
   420
    {
dludwig@8552
   421
        XINPUT_STATE *pXInputState = &joystick->hwdata->XInputState;
dludwig@8552
   422
        XINPUT_STATE *pXInputStatePrev = &prevXInputState;
dludwig@8552
   423
dludwig@8552
   424
        SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX );
dludwig@8552
   425
        SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-1*pXInputState->Gamepad.sThumbLY-1) );
dludwig@8552
   426
        SDL_PrivateJoystickAxis(joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX );
dludwig@8552
   427
        SDL_PrivateJoystickAxis(joystick, 3, (Sint16)(-1*pXInputState->Gamepad.sThumbRY-1) );
dludwig@8552
   428
        SDL_PrivateJoystickAxis(joystick, 4, (Sint16)((int)pXInputState->Gamepad.bLeftTrigger*32767/255) );
dludwig@8552
   429
        SDL_PrivateJoystickAxis(joystick, 5, (Sint16)((int)pXInputState->Gamepad.bRightTrigger*32767/255) );
dludwig@8552
   430
dludwig@8552
   431
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_UP ) )
dludwig@8552
   432
            SDL_PrivateJoystickButton(joystick, 0, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   433
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_DOWN ) )
dludwig@8552
   434
            SDL_PrivateJoystickButton(joystick, 1, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   435
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_LEFT ) )
dludwig@8552
   436
            SDL_PrivateJoystickButton(joystick, 2, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   437
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_RIGHT ) )
dludwig@8552
   438
            SDL_PrivateJoystickButton(joystick, 3, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   439
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_START ) )
dludwig@8552
   440
            SDL_PrivateJoystickButton(joystick, 4, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_START ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   441
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_BACK ) )
dludwig@8552
   442
            SDL_PrivateJoystickButton(joystick, 5, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_BACK ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   443
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_THUMB ) )
dludwig@8552
   444
            SDL_PrivateJoystickButton(joystick, 6, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   445
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_THUMB ) )
dludwig@8552
   446
            SDL_PrivateJoystickButton(joystick, 7, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   447
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_SHOULDER ) )
dludwig@8552
   448
            SDL_PrivateJoystickButton(joystick, 8, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   449
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_SHOULDER ) )
dludwig@8552
   450
            SDL_PrivateJoystickButton(joystick, 9, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   451
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_A ) )
dludwig@8552
   452
            SDL_PrivateJoystickButton(joystick, 10, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_A ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   453
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_B ) )
dludwig@8552
   454
            SDL_PrivateJoystickButton(joystick, 11, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_B ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   455
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_X ) )
dludwig@8552
   456
            SDL_PrivateJoystickButton(joystick, 12, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_X ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   457
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_Y ) )
dludwig@8552
   458
            SDL_PrivateJoystickButton(joystick, 13, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_Y ? SDL_PRESSED :	SDL_RELEASED );
dludwig@8552
   459
        if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons,  0x400 ) )
dludwig@8552
   460
            SDL_PrivateJoystickButton(joystick, 14, pXInputState->Gamepad.wButtons & 0x400 ? SDL_PRESSED :	SDL_RELEASED ); // 0x400 is the undocumented code for the guide button
dludwig@8552
   461
    }
dludwig@8552
   462
dludwig@8552
   463
    SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8471
   464
}
dludwig@8471
   465
dludwig@8471
   466
/* Function to close a joystick after use */
dludwig@8471
   467
void
dludwig@8471
   468
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
dludwig@8471
   469
{
dludwig@8471
   470
    /* Clear cached button data on the joystick */
dludwig@8552
   471
    SDL_LockMutex(g_DeviceInfoLock);
dludwig@8471
   472
    SDL_zero(joystick->hwdata->XInputState);
dludwig@8552
   473
    SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8471
   474
dludwig@8471
   475
    /* There's need to free 'hwdata', as it's a pointer to a global array.
dludwig@8471
   476
       The field will be cleared anyways, just to indicate that it's not
dludwig@8471
   477
       currently needed.
dludwig@8471
   478
     */
dludwig@8471
   479
    joystick->hwdata = NULL;
dludwig@8471
   480
}
dludwig@8471
   481
dludwig@8471
   482
/* Function to perform any system-specific joystick related cleanup */
dludwig@8471
   483
void
dludwig@8471
   484
SDL_SYS_JoystickQuit(void)
dludwig@8471
   485
{
dludwig@8552
   486
    /* Tell the joystick detection thread to stop, then wait for it to finish */
dludwig@8552
   487
    SDL_LockMutex(g_DeviceInfoLock);
dludwig@8552
   488
    g_DeviceDetectionQuit = SDL_TRUE;
dludwig@8552
   489
    SDL_UnlockMutex(g_DeviceInfoLock);
dludwig@8552
   490
    SDL_WaitThread(g_DeviceDetectionThread, NULL);
dludwig@8552
   491
dludwig@8552
   492
    /* Clean up device-detection stuff */
dludwig@8552
   493
    SDL_DestroyMutex(g_DeviceInfoLock);
dludwig@8552
   494
    g_DeviceInfoLock = NULL;
dludwig@8552
   495
    g_DeviceDetectionThread = NULL;
dludwig@8552
   496
    g_DeviceDetectionQuit = SDL_FALSE;
dludwig@8552
   497
dludwig@8471
   498
    return;
dludwig@8471
   499
}
dludwig@8471
   500
dludwig@8471
   501
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
dludwig@8471
   502
{
dludwig@8471
   503
    SDL_JoystickGUID guid;
dludwig@8471
   504
    // the GUID is just the first 16 chars of the name for now
dludwig@8471
   505
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
dludwig@8471
   506
    SDL_zero( guid );
dludwig@8471
   507
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dludwig@8471
   508
    return guid;
dludwig@8471
   509
}
dludwig@8471
   510
dludwig@8471
   511
dludwig@8471
   512
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
dludwig@8471
   513
{
dludwig@8471
   514
    SDL_JoystickGUID guid;
dludwig@8471
   515
    // the GUID is just the first 16 chars of the name for now
dludwig@8471
   516
    const char *name = joystick->name;
dludwig@8471
   517
    SDL_zero( guid );
dludwig@8471
   518
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
dludwig@8471
   519
    return guid;
dludwig@8471
   520
}
dludwig@8471
   521
dludwig@8552
   522
SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index)
dludwig@8552
   523
{
dludwig@8552
   524
    /* The XInput-capable DirectInput joystick backend implements the same
dludwig@8552
   525
       function (SDL_SYS_IsXInputDeviceIndex), however in that case, not all
dludwig@8552
   526
       joystick devices are XInput devices.  In this case, with the
dludwig@8552
   527
       WinRT-enabled XInput-only backend, all "joystick" devices are XInput
dludwig@8552
   528
       devices.
dludwig@8552
   529
     */
dludwig@8552
   530
    return SDL_TRUE;
dludwig@8471
   531
}
dludwig@8471
   532
dludwig@8471
   533
#endif /* SDL_JOYSTICK_XINPUT */
dludwig@8471
   534
dludwig@8471
   535
/* vi: set ts=4 sw=4 expandtab: */