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