src/joystick/hidapi/SDL_hidapi_xbox360.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 20 May 2020 17:01:25 -0400
changeset 13840 df7f6873371a
parent 13758 dcf07006dda6
permissions -rw-r--r--
hidapi: Fix compiler warning.
slouken@12088
     1
/*
slouken@12088
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
slouken@12088
     4
slouken@12088
     5
  This software is provided 'as-is', without any express or implied
slouken@12088
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@12088
     7
  arising from the use of this software.
slouken@12088
     8
slouken@12088
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@12088
    10
  including commercial applications, and to alter it and redistribute it
slouken@12088
    11
  freely, subject to the following restrictions:
slouken@12088
    12
slouken@12088
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@12088
    14
     claim that you wrote the original software. If you use this software
slouken@12088
    15
     in a product, an acknowledgment in the product documentation would be
slouken@12088
    16
     appreciated but is not required.
slouken@12088
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@12088
    18
     misrepresented as being the original software.
slouken@12088
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@12088
    20
*/
slouken@12088
    21
#include "../../SDL_internal.h"
slouken@12088
    22
slouken@12088
    23
#ifdef SDL_JOYSTICK_HIDAPI
slouken@12088
    24
slouken@12088
    25
#include "SDL_hints.h"
slouken@12088
    26
#include "SDL_events.h"
slouken@12088
    27
#include "SDL_timer.h"
slouken@12088
    28
#include "SDL_joystick.h"
slouken@12088
    29
#include "SDL_gamecontroller.h"
slouken@12088
    30
#include "../SDL_sysjoystick.h"
slouken@12088
    31
#include "SDL_hidapijoystick_c.h"
slouken@13481
    32
#include "SDL_hidapi_rumble.h"
slouken@12088
    33
slouken@12088
    34
slouken@12088
    35
#ifdef SDL_JOYSTICK_HIDAPI_XBOX360
slouken@12088
    36
slouken@12118
    37
#ifdef __WIN32__
slouken@12196
    38
#define SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12196
    39
/* This requires the Windows 10 SDK to build */
slouken@12196
    40
/*#define SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT*/
slouken@12196
    41
#endif
slouken@12196
    42
slouken@12196
    43
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12118
    44
#include "../../core/windows/SDL_xinput.h"
slouken@12118
    45
#endif
slouken@12118
    46
slouken@12196
    47
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
    48
#include "../../core/windows/SDL_windows.h"
slouken@13633
    49
typedef struct WindowsGamingInputGamepadState WindowsGamingInputGamepadState;
slouken@13633
    50
#define GamepadButtons_GUIDE 0x40000000
slouken@12196
    51
#define COBJMACROS
slouken@12196
    52
#include "windows.gaming.input.h"
slouken@12196
    53
#endif
slouken@12196
    54
slouken@13633
    55
#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
slouken@13633
    56
#define SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
    57
#endif
slouken@12088
    58
slouken@12088
    59
typedef struct {
slouken@12088
    60
    Uint8 last_state[USB_PACKET_LENGTH];
slouken@13633
    61
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
    62
    Uint32 match_state; /* Low 16 bits for button states, high 16 for 4 4bit axes */
slouken@13633
    63
    Uint32 last_state_packet;
slouken@13633
    64
#endif
slouken@12196
    65
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12118
    66
    SDL_bool xinput_enabled;
slouken@13633
    67
    SDL_bool xinput_correlated;
slouken@13633
    68
    Uint8 xinput_correlation_id;
slouken@13633
    69
    Uint8 xinput_correlation_count;
slouken@13633
    70
    Uint8 xinput_uncorrelate_count;
slouken@12118
    71
    Uint8 xinput_slot;
slouken@12118
    72
#endif
slouken@12196
    73
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
    74
    SDL_bool wgi_correlated;
slouken@13633
    75
    Uint8 wgi_correlation_id;
slouken@13633
    76
    Uint8 wgi_correlation_count;
slouken@13633
    77
    Uint8 wgi_uncorrelate_count;
slouken@13633
    78
    WindowsGamingInputGamepadState *wgi_slot;
slouken@12196
    79
#endif
slouken@12088
    80
} SDL_DriverXbox360_Context;
slouken@12088
    81
slouken@13633
    82
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
    83
static struct {
slouken@13633
    84
    Uint32 last_state_packet;
slouken@13633
    85
    SDL_Joystick *joystick;
slouken@13633
    86
    SDL_Joystick *last_joystick;
slouken@13633
    87
} guide_button_candidate;
slouken@13633
    88
slouken@13633
    89
typedef struct WindowsMatchState {
slouken@13633
    90
    SHORT match_axes[4];
slouken@13633
    91
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
    92
    WORD xinput_buttons;
slouken@13633
    93
#endif
slouken@13633
    94
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
    95
    Uint32 wgi_buttons;
slouken@13633
    96
#endif
slouken@13633
    97
    SDL_bool any_data;
slouken@13633
    98
} WindowsMatchState;
slouken@13633
    99
slouken@13633
   100
static void HIDAPI_DriverXbox360_FillMatchState(WindowsMatchState *state, Uint32 match_state)
slouken@13633
   101
{
slouken@13633
   102
    int ii;
slouken@13633
   103
    state->any_data = SDL_FALSE;
slouken@13633
   104
    /*  SHORT state->match_axes[4] = {
slouken@13633
   105
            (match_state & 0x000F0000) >> 4,
slouken@13633
   106
            (match_state & 0x00F00000) >> 8,
slouken@13633
   107
            (match_state & 0x0F000000) >> 12,
slouken@13633
   108
            (match_state & 0xF0000000) >> 16,
slouken@13633
   109
        }; */
slouken@13633
   110
    for (ii = 0; ii < 4; ii++) {
slouken@13633
   111
        state->match_axes[ii] = (match_state & (0x000F0000 << (ii * 4))) >> (4 + ii * 4);
slouken@13633
   112
        if ((Uint32)(state->match_axes[ii] + 0x1000) > 0x2000) { /* match_state bit is not 0xF, 0x1, or 0x2 */
slouken@13633
   113
            state->any_data = SDL_TRUE;
slouken@13633
   114
        }
slouken@13633
   115
    }
slouken@12088
   116
slouken@12196
   117
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
   118
    /* Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less */
slouken@13633
   119
#define XInputAxesMatch(gamepad) (\
slouken@13633
   120
   (Uint32)(gamepad.sThumbLX - state->match_axes[0] + 0x1000) <= 0x2fff && \
slouken@13633
   121
   (Uint32)(~gamepad.sThumbLY - state->match_axes[1] + 0x1000) <= 0x2fff && \
slouken@13633
   122
   (Uint32)(gamepad.sThumbRX - state->match_axes[2] + 0x1000) <= 0x2fff && \
slouken@13633
   123
   (Uint32)(~gamepad.sThumbRY - state->match_axes[3] + 0x1000) <= 0x2fff)
slouken@13633
   124
    /* Explicit
slouken@13633
   125
#define XInputAxesMatch(gamepad) (\
slouken@13633
   126
    SDL_abs((Sint8)((gamepad.sThumbLX & 0xF000) >> 8) - ((match_state & 0x000F0000) >> 12)) <= 0x10 && \
slouken@13633
   127
    SDL_abs((Sint8)((~gamepad.sThumbLY & 0xF000) >> 8) - ((match_state & 0x00F00000) >> 16)) <= 0x10 && \
slouken@13633
   128
    SDL_abs((Sint8)((gamepad.sThumbRX & 0xF000) >> 8) - ((match_state & 0x0F000000) >> 20)) <= 0x10 && \
slouken@13633
   129
    SDL_abs((Sint8)((~gamepad.sThumbRY & 0xF000) >> 8) - ((match_state & 0xF0000000) >> 24)) <= 0x10) */
slouken@13633
   130
slouken@13633
   131
slouken@13633
   132
    state->xinput_buttons =
slouken@13633
   133
        /* Bitwise map .RLDUWVQTS.KYXBA -> YXBA..WVQTKSRLDU */
slouken@13633
   134
        match_state << 12 | (match_state & 0x0780) >> 1 | (match_state & 0x0010) << 1 | (match_state & 0x0040) >> 2 | (match_state & 0x7800) >> 11;
slouken@13633
   135
    /*  Explicit
slouken@13633
   136
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_A)) ? XINPUT_GAMEPAD_A : 0) |
slouken@13633
   137
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_B)) ? XINPUT_GAMEPAD_B : 0) |
slouken@13633
   138
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_X)) ? XINPUT_GAMEPAD_X : 0) |
slouken@13633
   139
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_Y)) ? XINPUT_GAMEPAD_Y : 0) |
slouken@13633
   140
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_BACK)) ? XINPUT_GAMEPAD_BACK : 0) |
slouken@13633
   141
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_START)) ? XINPUT_GAMEPAD_START : 0) |
slouken@13633
   142
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSTICK)) ? XINPUT_GAMEPAD_LEFT_THUMB : 0) |
slouken@13633
   143
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSTICK)) ? XINPUT_GAMEPAD_RIGHT_THUMB: 0) |
slouken@13633
   144
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) ? XINPUT_GAMEPAD_LEFT_SHOULDER : 0) |
slouken@13633
   145
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) ? XINPUT_GAMEPAD_RIGHT_SHOULDER : 0) |
slouken@13633
   146
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_UP)) ? XINPUT_GAMEPAD_DPAD_UP : 0) |
slouken@13633
   147
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_DOWN)) ? XINPUT_GAMEPAD_DPAD_DOWN : 0) |
slouken@13633
   148
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_LEFT)) ? XINPUT_GAMEPAD_DPAD_LEFT : 0) |
slouken@13633
   149
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) ? XINPUT_GAMEPAD_DPAD_RIGHT : 0);
slouken@13633
   150
    */
slouken@13633
   151
slouken@13633
   152
    if (state->xinput_buttons)
slouken@13633
   153
        state->any_data = SDL_TRUE;
slouken@13633
   154
#endif
slouken@13633
   155
slouken@13633
   156
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
   157
    /* Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less */
slouken@13633
   158
#define WindowsGamingInputAxesMatch(gamepad) (\
slouken@13633
   159
    (Uint16)(((Sint16)(gamepad.LeftThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[0] + 0x1000) <= 0x2fff && \
slouken@13633
   160
    (Uint16)((~(Sint16)(gamepad.LeftThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[1] + 0x1000) <= 0x2fff && \
slouken@13633
   161
    (Uint16)(((Sint16)(gamepad.RightThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[2] + 0x1000) <= 0x2fff && \
slouken@13633
   162
    (Uint16)((~(Sint16)(gamepad.RightThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[3] + 0x1000) <= 0x2fff)
slouken@13633
   163
slouken@13633
   164
slouken@13633
   165
    state->wgi_buttons =
slouken@13633
   166
        /* Bitwise map .RLD UWVQ TS.K YXBA -> ..QT WVRL DUYX BAKS */
slouken@13633
   167
        /*  RStick/LStick (QT)         RShould/LShould  (WV)                 DPad R/L/D/U                          YXBA                         bac(K)                      (S)tart */
slouken@13633
   168
        (match_state & 0x0180) << 5 | (match_state & 0x0600) << 1 | (match_state & 0x7800) >> 5 | (match_state & 0x000F) << 2 | (match_state & 0x0010) >> 3 | (match_state & 0x0040) >> 6;
slouken@13633
   169
    /*  Explicit
slouken@13633
   170
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_A)) ? GamepadButtons_A : 0) |
slouken@13633
   171
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_B)) ? GamepadButtons_B : 0) |
slouken@13633
   172
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_X)) ? GamepadButtons_X : 0) |
slouken@13633
   173
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_Y)) ? GamepadButtons_Y : 0) |
slouken@13633
   174
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_BACK)) ? GamepadButtons_View : 0) |
slouken@13633
   175
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_START)) ? GamepadButtons_Menu : 0) |
slouken@13633
   176
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSTICK)) ? GamepadButtons_LeftThumbstick : 0) |
slouken@13633
   177
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSTICK)) ? GamepadButtons_RightThumbstick: 0) |
slouken@13633
   178
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) ? GamepadButtons_LeftShoulder: 0) |
slouken@13633
   179
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) ? GamepadButtons_RightShoulder: 0) |
slouken@13633
   180
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_UP)) ? GamepadButtons_DPadUp : 0) |
slouken@13633
   181
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_DOWN)) ? GamepadButtons_DPadDown : 0) |
slouken@13633
   182
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_LEFT)) ? GamepadButtons_DPadLeft : 0) |
slouken@13633
   183
        ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) ? GamepadButtons_DPadRight : 0); */
slouken@13633
   184
slouken@13633
   185
    if (state->wgi_buttons)
slouken@13633
   186
        state->any_data = SDL_TRUE;
slouken@13633
   187
#endif
slouken@13633
   188
slouken@13633
   189
}
slouken@13633
   190
slouken@13633
   191
slouken@13633
   192
#endif
slouken@13633
   193
slouken@13633
   194
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
   195
static struct {
slouken@13633
   196
    XINPUT_STATE_EX state;
slouken@13633
   197
    SDL_bool connected; /* Currently has an active XInput device */
slouken@13633
   198
    SDL_bool used; /* Is currently mapped to an SDL device */
slouken@13633
   199
    Uint8 correlation_id;
slouken@13633
   200
} xinput_state[XUSER_MAX_COUNT];
slouken@13633
   201
static SDL_bool xinput_device_change = SDL_TRUE;
slouken@13633
   202
static SDL_bool xinput_state_dirty = SDL_TRUE;
slouken@13633
   203
slouken@13633
   204
static void
slouken@13633
   205
HIDAPI_DriverXbox360_UpdateXInput()
slouken@13633
   206
{
slouken@13633
   207
    DWORD user_index;
slouken@13633
   208
    if (xinput_device_change) {
slouken@13633
   209
        for (user_index = 0; user_index < XUSER_MAX_COUNT; user_index++) {
slouken@13633
   210
            XINPUT_CAPABILITIES capabilities;
slouken@13633
   211
            xinput_state[user_index].connected = XINPUTGETCAPABILITIES(user_index, XINPUT_FLAG_GAMEPAD, &capabilities) == ERROR_SUCCESS;
slouken@13633
   212
        }
slouken@13633
   213
        xinput_device_change = SDL_FALSE;
slouken@13633
   214
        xinput_state_dirty = SDL_TRUE;
slouken@13633
   215
    }
slouken@13633
   216
    if (xinput_state_dirty) {
slouken@13633
   217
        xinput_state_dirty = SDL_FALSE;
slouken@13633
   218
        for (user_index = 0; user_index < SDL_arraysize(xinput_state); ++user_index) {
slouken@13633
   219
            if (xinput_state[user_index].connected) {
slouken@13758
   220
                if (XINPUTGETSTATE(user_index, &xinput_state[user_index].state) != ERROR_SUCCESS) {
slouken@13633
   221
                    xinput_state[user_index].connected = SDL_FALSE;
slouken@13633
   222
                }
slouken@13633
   223
            }
slouken@13633
   224
        }
slouken@13633
   225
    }
slouken@13633
   226
}
slouken@12118
   227
slouken@12118
   228
static void
slouken@12118
   229
HIDAPI_DriverXbox360_MarkXInputSlotUsed(Uint8 xinput_slot)
slouken@12118
   230
{
slouken@12118
   231
    if (xinput_slot != XUSER_INDEX_ANY) {
slouken@13633
   232
        xinput_state[xinput_slot].used = SDL_TRUE;
slouken@12118
   233
    }
slouken@12118
   234
}
slouken@12118
   235
slouken@12118
   236
static void
slouken@12118
   237
HIDAPI_DriverXbox360_MarkXInputSlotFree(Uint8 xinput_slot)
slouken@12118
   238
{
slouken@12118
   239
    if (xinput_slot != XUSER_INDEX_ANY) {
slouken@13633
   240
        xinput_state[xinput_slot].used = SDL_FALSE;
slouken@12118
   241
    }
slouken@12118
   242
}
slouken@12118
   243
static SDL_bool
slouken@12118
   244
HIDAPI_DriverXbox360_MissingXInputSlot()
slouken@12118
   245
{
slouken@13633
   246
    int ii;
slouken@13633
   247
    for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
slouken@13633
   248
        if (xinput_state[ii].connected && !xinput_state[ii].used) {
slouken@13633
   249
            return SDL_TRUE;
slouken@13633
   250
        }
slouken@13633
   251
    }
slouken@13633
   252
    return SDL_FALSE;
slouken@12118
   253
}
slouken@12118
   254
slouken@13633
   255
static SDL_bool
slouken@13633
   256
HIDAPI_DriverXbox360_XInputSlotMatches(const WindowsMatchState *state, Uint8 slot_idx)
slouken@12118
   257
{
slouken@13633
   258
    if (xinput_state[slot_idx].connected) {
slouken@13633
   259
        WORD xinput_buttons = xinput_state[slot_idx].state.Gamepad.wButtons;
slouken@13633
   260
        if ((xinput_buttons & ~XINPUT_GAMEPAD_GUIDE) == state->xinput_buttons && XInputAxesMatch(xinput_state[slot_idx].state.Gamepad)) {
slouken@13633
   261
            return SDL_TRUE;
slouken@13633
   262
        }
slouken@13633
   263
    }
slouken@13633
   264
    return SDL_FALSE;
slouken@13633
   265
}
slouken@13633
   266
slouken@13633
   267
slouken@13633
   268
static SDL_bool
slouken@13633
   269
HIDAPI_DriverXbox360_GuessXInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, Uint8 *slot_idx)
slouken@13633
   270
{
slouken@13633
   271
    int user_index;
slouken@12118
   272
    int match_count;
slouken@12118
   273
icculus@13840
   274
    *slot_idx = 0;
icculus@13840
   275
slouken@12118
   276
    match_count = 0;
slouken@12118
   277
    for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
slouken@13633
   278
        if (!xinput_state[user_index].used && HIDAPI_DriverXbox360_XInputSlotMatches(state, user_index)) {
slouken@13633
   279
            ++match_count;
slouken@13633
   280
            *slot_idx = (Uint8)user_index;
slouken@13633
   281
            /* Incrementing correlation_id for any match, as negative evidence for others being correlated */
slouken@13633
   282
            *correlation_id = ++xinput_state[user_index].correlation_id;
slouken@12118
   283
        }
slouken@12118
   284
    }
slouken@13633
   285
    /* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
slouken@13633
   286
       Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
slouken@13633
   287
       data. */
slouken@13633
   288
    if (match_count == 1 && state->any_data) {
slouken@13633
   289
        return SDL_TRUE;
slouken@12118
   290
    }
slouken@13633
   291
    return SDL_FALSE;
slouken@12118
   292
}
slouken@12118
   293
slouken@12196
   294
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
slouken@12196
   295
slouken@12196
   296
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
   297
slouken@13633
   298
typedef struct WindowsGamingInputGamepadState {
slouken@13633
   299
    __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
slouken@13633
   300
    struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
slouken@13633
   301
    SDL_DriverXbox360_Context *correlated_context;
slouken@13633
   302
    SDL_bool used; /* Is currently mapped to an SDL device */
slouken@13633
   303
    SDL_bool connected; /* Just used during update to track disconnected */
slouken@13633
   304
    Uint8 correlation_id;
slouken@13633
   305
    struct __x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration;
slouken@13633
   306
} WindowsGamingInputGamepadState;
slouken@13633
   307
slouken@13633
   308
static struct {
slouken@13633
   309
    WindowsGamingInputGamepadState **per_gamepad;
slouken@13633
   310
    int per_gamepad_count;
slouken@13633
   311
    SDL_bool initialized;
slouken@13633
   312
    SDL_bool dirty;
slouken@13633
   313
    SDL_bool need_device_list_update;
slouken@13633
   314
    int ref_count;
slouken@13633
   315
    __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics *gamepad_statics;
slouken@13633
   316
} wgi_state;
slouken@13633
   317
slouken@13633
   318
static void
slouken@13633
   319
HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotUsed(WindowsGamingInputGamepadState *wgi_slot, SDL_DriverXbox360_Context *ctx)
slouken@13633
   320
{
slouken@13633
   321
    wgi_slot->used = SDL_TRUE;
slouken@13633
   322
    wgi_slot->correlated_context = ctx;
slouken@13633
   323
}
slouken@13633
   324
slouken@13633
   325
static void
slouken@13633
   326
HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotFree(WindowsGamingInputGamepadState *wgi_slot)
slouken@13633
   327
{
slouken@13633
   328
    wgi_slot->used = SDL_FALSE;
slouken@13633
   329
    wgi_slot->correlated_context = NULL;
slouken@13633
   330
}
slouken@13633
   331
slouken@13633
   332
static SDL_bool
slouken@13633
   333
HIDAPI_DriverXbox360_MissingWindowsGamingInputSlot()
slouken@13633
   334
{
slouken@13633
   335
    int ii;
slouken@13633
   336
    for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
slouken@13633
   337
        if (!wgi_state.per_gamepad[ii]->used) {
slouken@13633
   338
            return SDL_TRUE;
slouken@13633
   339
        }
slouken@13633
   340
    }
slouken@13633
   341
    return SDL_FALSE;
slouken@13633
   342
}
slouken@13633
   343
slouken@13633
   344
static void
slouken@13633
   345
HIDAPI_DriverXbox360_UpdateWindowsGamingInput()
slouken@13633
   346
{
slouken@13633
   347
    int ii;
slouken@13633
   348
    if (!wgi_state.gamepad_statics)
slouken@13633
   349
        return;
slouken@13633
   350
slouken@13633
   351
    if (!wgi_state.dirty)
slouken@13633
   352
        return;
slouken@13633
   353
    wgi_state.dirty = SDL_FALSE;
slouken@13633
   354
slouken@13633
   355
    if (wgi_state.need_device_list_update) {
slouken@13633
   356
        wgi_state.need_device_list_update = SDL_FALSE;
slouken@13633
   357
        for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
slouken@13633
   358
            wgi_state.per_gamepad[ii]->connected = SDL_FALSE;
slouken@13633
   359
        }
slouken@13633
   360
        HRESULT hr;
slouken@13633
   361
        __FIVectorView_1_Windows__CGaming__CInput__CGamepad *gamepads;
slouken@13633
   362
slouken@13633
   363
        hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_get_Gamepads(wgi_state.gamepad_statics, &gamepads);
slouken@13633
   364
        if (SUCCEEDED(hr)) {
slouken@13633
   365
            unsigned int num_gamepads;
slouken@13633
   366
slouken@13633
   367
            hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_get_Size(gamepads, &num_gamepads);
slouken@13633
   368
            if (SUCCEEDED(hr)) {
slouken@13633
   369
                unsigned int i;
slouken@13633
   370
                for (i = 0; i < num_gamepads; ++i) {
slouken@13633
   371
                    __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
slouken@13633
   372
slouken@13633
   373
                    hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, i, &gamepad);
slouken@13633
   374
                    if (SUCCEEDED(hr)) {
slouken@13633
   375
                        SDL_bool found = SDL_FALSE;
slouken@13633
   376
                        int jj;
slouken@13633
   377
                        for (jj = 0; jj < wgi_state.per_gamepad_count ; jj++) {
slouken@13633
   378
                            if (wgi_state.per_gamepad[jj]->gamepad == gamepad) {
slouken@13633
   379
                                found = SDL_TRUE;
slouken@13633
   380
                                wgi_state.per_gamepad[jj]->connected = SDL_TRUE;
slouken@13633
   381
                                break;
slouken@13633
   382
                            }
slouken@13633
   383
                        }
slouken@13633
   384
                        if (!found) {
slouken@13633
   385
                            /* New device, add it */
slouken@13633
   386
                            wgi_state.per_gamepad_count++;
slouken@13633
   387
                            wgi_state.per_gamepad = SDL_realloc(wgi_state.per_gamepad, sizeof(wgi_state.per_gamepad[0]) * wgi_state.per_gamepad_count);
slouken@13633
   388
                            if (!wgi_state.per_gamepad) {
slouken@13633
   389
                                SDL_OutOfMemory();
slouken@13633
   390
                                return;
slouken@13633
   391
                            }
slouken@13633
   392
                            WindowsGamingInputGamepadState *gamepad_state = SDL_calloc(1, sizeof(*gamepad_state));
slouken@13633
   393
                            if (!gamepad_state) {
slouken@13633
   394
                                SDL_OutOfMemory();
slouken@13633
   395
                                return;
slouken@13633
   396
                            }
slouken@13633
   397
                            wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1] = gamepad_state;
slouken@13633
   398
                            gamepad_state->gamepad = gamepad;
slouken@13633
   399
                            gamepad_state->connected = SDL_TRUE;
slouken@13633
   400
                        } else {
slouken@13633
   401
                            /* Already tracked */
slouken@13633
   402
                            __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
slouken@13633
   403
                        }
slouken@13633
   404
                    }
slouken@13633
   405
                }
slouken@13633
   406
                for (ii = wgi_state.per_gamepad_count - 1; ii >= 0; ii--) {
slouken@13633
   407
                    WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
slouken@13633
   408
                    if (!gamepad_state->connected) {
slouken@13633
   409
                        /* Device missing, must be disconnected */
slouken@13633
   410
                        if (gamepad_state->correlated_context) {
slouken@13633
   411
                            gamepad_state->correlated_context->wgi_correlated = SDL_FALSE;
slouken@13633
   412
                            gamepad_state->correlated_context->wgi_slot = NULL;
slouken@13633
   413
                        }
slouken@13633
   414
                        __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad_state->gamepad);
slouken@13633
   415
                        SDL_free(gamepad_state);
slouken@13633
   416
                        wgi_state.per_gamepad[ii] = wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1];
slouken@13633
   417
                        --wgi_state.per_gamepad_count;
slouken@13633
   418
                    }
slouken@13633
   419
                }
slouken@13633
   420
            }
slouken@13633
   421
            __FIVectorView_1_Windows__CGaming__CInput__CGamepad_Release(gamepads);
slouken@13633
   422
        }
slouken@13633
   423
    } /* need_device_list_update */
slouken@13633
   424
slouken@13633
   425
    for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
slouken@13633
   426
        HRESULT hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(wgi_state.per_gamepad[ii]->gamepad, &wgi_state.per_gamepad[ii]->state);
slouken@13633
   427
        if (!SUCCEEDED(hr)) {
slouken@13633
   428
            wgi_state.per_gamepad[ii]->connected = SDL_FALSE; /* Not used by anything, currently */
slouken@13633
   429
        }
slouken@13633
   430
    }
slouken@13633
   431
}
slouken@12196
   432
static void
slouken@12196
   433
HIDAPI_DriverXbox360_InitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
slouken@12196
   434
{
slouken@13633
   435
    wgi_state.need_device_list_update = SDL_TRUE;
slouken@13633
   436
    wgi_state.ref_count++;
slouken@13633
   437
    if (!wgi_state.initialized) {
slouken@13633
   438
        /* I think this takes care of RoInitialize() in a way that is compatible with the rest of SDL */
slouken@13633
   439
        if (FAILED(WIN_CoInitialize())) {
slouken@13633
   440
            return;
slouken@13633
   441
        }
slouken@13633
   442
        wgi_state.initialized = SDL_TRUE;
slouken@13633
   443
        wgi_state.dirty = SDL_TRUE;
slouken@12196
   444
slouken@12196
   445
        static const IID SDL_IID_IGamepadStatics = { 0x8BBCE529, 0xD49C, 0x39E9, { 0x95, 0x60, 0xE4, 0x7D, 0xDE, 0x96, 0xB7, 0xC8 } };
slouken@12196
   446
        HRESULT hr;
slouken@12196
   447
        HMODULE hModule = LoadLibraryA("combase.dll");
slouken@12196
   448
        if (hModule != NULL) {
slouken@12197
   449
            typedef HRESULT (WINAPI *WindowsCreateString_t)(PCNZWCH sourceString, UINT32 length, HSTRING* string);
slouken@12197
   450
            typedef HRESULT (WINAPI *WindowsDeleteString_t)(HSTRING string);
slouken@12197
   451
            typedef HRESULT (WINAPI *RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void** factory);
slouken@12196
   452
slouken@12196
   453
            WindowsCreateString_t WindowsCreateStringFunc = (WindowsCreateString_t)GetProcAddress(hModule, "WindowsCreateString");
slouken@12197
   454
            WindowsDeleteString_t WindowsDeleteStringFunc = (WindowsDeleteString_t)GetProcAddress(hModule, "WindowsDeleteString");
slouken@12196
   455
            RoGetActivationFactory_t RoGetActivationFactoryFunc = (RoGetActivationFactory_t)GetProcAddress(hModule, "RoGetActivationFactory");
slouken@12197
   456
            if (WindowsCreateStringFunc && WindowsDeleteStringFunc && RoGetActivationFactoryFunc) {
slouken@12196
   457
                LPTSTR pNamespace = L"Windows.Gaming.Input.Gamepad";
slouken@12196
   458
                HSTRING hNamespaceString;
slouken@12196
   459
slouken@12196
   460
                hr = WindowsCreateStringFunc(pNamespace, SDL_wcslen(pNamespace), &hNamespaceString);
slouken@12196
   461
                if (SUCCEEDED(hr)) {
slouken@13633
   462
                    RoGetActivationFactoryFunc(hNamespaceString, &SDL_IID_IGamepadStatics, &wgi_state.gamepad_statics);
slouken@12197
   463
                    WindowsDeleteStringFunc(hNamespaceString);
slouken@12196
   464
                }
slouken@12196
   465
            }
slouken@12196
   466
            FreeLibrary(hModule);
slouken@12196
   467
        }
slouken@12196
   468
    }
slouken@12196
   469
}
slouken@12196
   470
slouken@13633
   471
static SDL_bool
slouken@13633
   472
HIDAPI_DriverXbox360_WindowsGamingInputSlotMatches(const WindowsMatchState *state, WindowsGamingInputGamepadState *slot)
slouken@12196
   473
{
slouken@13633
   474
    Uint32 wgi_buttons = slot->state.Buttons;
slouken@13633
   475
    if ((wgi_buttons & 0x3FFF) == state->wgi_buttons && WindowsGamingInputAxesMatch(slot->state)) {
slouken@13633
   476
        return SDL_TRUE;
slouken@12196
   477
    }
slouken@13633
   478
    return SDL_FALSE;
slouken@12196
   479
}
slouken@12196
   480
slouken@13633
   481
static SDL_bool
slouken@13633
   482
HIDAPI_DriverXbox360_GuessWindowsGamingInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, WindowsGamingInputGamepadState **slot)
slouken@12196
   483
{
slouken@13633
   484
    int match_count;
slouken@12196
   485
slouken@13633
   486
    match_count = 0;
slouken@13633
   487
    for (int user_index = 0; user_index < wgi_state.per_gamepad_count; ++user_index) {
slouken@13633
   488
        WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[user_index];
slouken@13633
   489
        if (HIDAPI_DriverXbox360_WindowsGamingInputSlotMatches(state, gamepad_state)) {
slouken@13633
   490
            ++match_count;
slouken@13633
   491
            *slot = gamepad_state;
slouken@13633
   492
            /* Incrementing correlation_id for any match, as negative evidence for others being correlated */
slouken@13633
   493
            *correlation_id = ++gamepad_state->correlation_id;
slouken@12196
   494
        }
slouken@12196
   495
    }
slouken@13633
   496
    /* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
slouken@13633
   497
       Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
slouken@13633
   498
       data. */
slouken@13633
   499
    if (match_count == 1 && state->any_data) {
slouken@13633
   500
        return SDL_TRUE;
slouken@13633
   501
    }
slouken@13633
   502
    return SDL_FALSE;
slouken@12196
   503
}
slouken@12196
   504
slouken@12196
   505
static void
slouken@12196
   506
HIDAPI_DriverXbox360_QuitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
slouken@12196
   507
{
slouken@13633
   508
    wgi_state.need_device_list_update = SDL_TRUE;
slouken@13633
   509
    --wgi_state.ref_count;
slouken@13633
   510
    if (!wgi_state.ref_count && wgi_state.initialized) {
slouken@13633
   511
        for (int ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
slouken@13633
   512
            __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(wgi_state.per_gamepad[ii]->gamepad);
slouken@13633
   513
        }
slouken@13633
   514
        if (wgi_state.per_gamepad) {
slouken@13633
   515
            SDL_free(wgi_state.per_gamepad);
slouken@13633
   516
            wgi_state.per_gamepad = NULL;
slouken@13633
   517
        }
slouken@13633
   518
        wgi_state.per_gamepad_count = 0;
slouken@13633
   519
        if (wgi_state.gamepad_statics) {
slouken@13633
   520
            __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_Release(wgi_state.gamepad_statics);
slouken@13633
   521
            wgi_state.gamepad_statics = NULL;
slouken@13633
   522
        }
slouken@12196
   523
        WIN_CoUninitialize();
slouken@13633
   524
        wgi_state.initialized = SDL_FALSE;
slouken@12196
   525
    }
slouken@12196
   526
}
slouken@12196
   527
slouken@12196
   528
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
slouken@12118
   529
slouken@13633
   530
static void
slouken@13633
   531
HIDAPI_DriverXbox360_PostUpdate(void)
slouken@13633
   532
{
slouken@13633
   533
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
   534
    SDL_bool unmapped_guide_pressed = SDL_FALSE;
slouken@13633
   535
slouken@13633
   536
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
   537
    if (!wgi_state.dirty) {
slouken@13633
   538
        int ii;
slouken@13633
   539
        for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
slouken@13633
   540
            WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
slouken@13633
   541
            if (!gamepad_state->used && (gamepad_state->state.Buttons & GamepadButtons_GUIDE)) {
slouken@13633
   542
                unmapped_guide_pressed = SDL_TRUE;
slouken@13633
   543
                break;
slouken@13633
   544
            }
slouken@13633
   545
        }
slouken@13633
   546
    }
slouken@13633
   547
    wgi_state.dirty = SDL_TRUE;
slouken@13633
   548
#endif
slouken@13633
   549
slouken@13633
   550
slouken@13633
   551
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
   552
    if (!xinput_state_dirty) {
slouken@13633
   553
        int ii;
slouken@13633
   554
        for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
slouken@13633
   555
            if (xinput_state[ii].connected && !xinput_state[ii].used && (xinput_state[ii].state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE)) {
slouken@13633
   556
                unmapped_guide_pressed = SDL_TRUE;
slouken@13633
   557
                break;
slouken@13633
   558
            }
slouken@13633
   559
        }
slouken@13633
   560
    }
slouken@13633
   561
    xinput_state_dirty = SDL_TRUE;
slouken@13633
   562
#endif
slouken@13633
   563
slouken@13633
   564
    if (unmapped_guide_pressed) {
slouken@13633
   565
        if (guide_button_candidate.joystick && !guide_button_candidate.last_joystick) {
slouken@13633
   566
            SDL_PrivateJoystickButton(guide_button_candidate.joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_PRESSED);
slouken@13633
   567
            guide_button_candidate.last_joystick = guide_button_candidate.joystick;
slouken@13633
   568
        }
slouken@13633
   569
    } else if (guide_button_candidate.last_joystick) {
slouken@13633
   570
        SDL_PrivateJoystickButton(guide_button_candidate.last_joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
slouken@13633
   571
        guide_button_candidate.last_joystick = NULL;
slouken@13633
   572
    }
slouken@13633
   573
    guide_button_candidate.joystick = NULL;
slouken@13633
   574
#endif
slouken@13633
   575
}
slouken@13633
   576
slouken@13571
   577
#if defined(__MACOSX__)
slouken@12088
   578
static SDL_bool
slouken@13563
   579
IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
slouken@13563
   580
{
slouken@13563
   581
    /* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
slouken@13563
   582
    if (vendor_id == USB_VENDOR_MICROSOFT) {
slouken@13563
   583
        if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
slouken@13563
   584
            product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
slouken@13563
   585
            product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
slouken@13563
   586
            return SDL_TRUE;
slouken@13563
   587
        }
slouken@13563
   588
    }
slouken@13563
   589
    return SDL_FALSE;
slouken@13563
   590
}
slouken@13571
   591
#endif
slouken@13563
   592
slouken@13563
   593
static SDL_bool
slouken@13431
   594
HIDAPI_DriverXbox360_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
slouken@12088
   595
{
slouken@13429
   596
    const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
slouken@13275
   597
slouken@13429
   598
    if (vendor_id == USB_VENDOR_NVIDIA) {
slouken@13337
   599
        /* This is the NVIDIA Shield controller which doesn't talk Xbox controller protocol */
slouken@13297
   600
        return SDL_FALSE;
slouken@13297
   601
    }
slouken@13429
   602
    if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == 0x0291 || product_id == 0x0719)) ||
slouken@13429
   603
        (type == SDL_CONTROLLER_TYPE_XBOX360 && interface_protocol == XB360W_IFACE_PROTOCOL)) {
slouken@13429
   604
        /* This is the wireless dongle, which talks a different protocol */
slouken@13429
   605
        return SDL_FALSE;
slouken@13365
   606
    }
slouken@13350
   607
    if (interface_number > 0) {
slouken@13350
   608
        /* This is the chatpad or other input interface, not the Xbox 360 interface */
slouken@13350
   609
        return SDL_FALSE;
slouken@13350
   610
    }
slouken@12117
   611
#if defined(__MACOSX__) || defined(__WIN32__)
slouken@13429
   612
    if (vendor_id == USB_VENDOR_MICROSOFT && product_id == 0x028e && version == 1) {
slouken@12118
   613
        /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
slouken@12118
   614
        return SDL_FALSE;
slouken@12118
   615
    }
slouken@13568
   616
#if defined(__MACOSX__)
slouken@13568
   617
    /* Wired Xbox One controllers are handled by this driver, interfacing with
slouken@13568
   618
       the 360Controller driver available from:
slouken@13568
   619
       https://github.com/360Controller/360Controller/releases
slouken@13568
   620
slouken@13568
   621
       Bluetooth Xbox One controllers are handled by the SDL Xbox One driver
slouken@13568
   622
    */
slouken@13568
   623
    if (IsBluetoothXboxOneController(vendor_id, product_id)) {
slouken@13568
   624
        return SDL_FALSE;
slouken@13568
   625
    }
slouken@13568
   626
#endif
slouken@13275
   627
    return (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE);
slouken@12088
   628
#else
slouken@13275
   629
    return (type == SDL_CONTROLLER_TYPE_XBOX360);
slouken@12088
   630
#endif
slouken@12088
   631
}
slouken@12088
   632
slouken@12088
   633
static const char *
slouken@12088
   634
HIDAPI_DriverXbox360_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
slouken@12088
   635
{
slouken@13330
   636
    return NULL;
slouken@12088
   637
}
slouken@12088
   638
slouken@12088
   639
static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
slouken@12088
   640
{
slouken@13365
   641
    Uint8 mode = 0x02 + slot;
slouken@13365
   642
    const Uint8 led_packet[] = { 0x01, 0x03, mode };
slouken@12088
   643
slouken@12088
   644
    if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
slouken@12118
   645
        return SDL_FALSE;
slouken@12118
   646
    }
slouken@12118
   647
    return SDL_TRUE;
slouken@12088
   648
}
slouken@12088
   649
slouken@12088
   650
static SDL_bool
slouken@13354
   651
HIDAPI_DriverXbox360_InitDevice(SDL_HIDAPI_Device *device)
slouken@13354
   652
{
slouken@13633
   653
    return HIDAPI_JoystickConnected(device, NULL, SDL_FALSE);
slouken@13354
   654
}
slouken@13354
   655
slouken@13369
   656
static int
slouken@13369
   657
HIDAPI_DriverXbox360_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
slouken@13369
   658
{
slouken@13369
   659
    return -1;
slouken@13369
   660
}
slouken@13369
   661
slouken@13369
   662
static void
slouken@13369
   663
HIDAPI_DriverXbox360_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
slouken@13369
   664
{
slouken@13369
   665
    if (device->dev) {
slouken@13369
   666
        SetSlotLED(device->dev, (player_index % 4));
slouken@13369
   667
    }
slouken@13369
   668
}
slouken@13369
   669
slouken@13354
   670
static SDL_bool
slouken@13354
   671
HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
slouken@12088
   672
{
slouken@12088
   673
    SDL_DriverXbox360_Context *ctx;
slouken@13369
   674
    int player_index;
slouken@12088
   675
slouken@12088
   676
    ctx = (SDL_DriverXbox360_Context *)SDL_calloc(1, sizeof(*ctx));
slouken@12088
   677
    if (!ctx) {
slouken@12088
   678
        SDL_OutOfMemory();
slouken@12088
   679
        return SDL_FALSE;
slouken@12088
   680
    }
slouken@13354
   681
slouken@13633
   682
    if (device->path) { /* else opened for RAWINPUT driver */
slouken@13633
   683
        device->dev = hid_open_path(device->path, 0);
slouken@13633
   684
        if (!device->dev) {
slouken@13633
   685
            SDL_SetError("Couldn't open %s", device->path);
slouken@13633
   686
            SDL_free(ctx);
slouken@13633
   687
            return SDL_FALSE;
slouken@13633
   688
        }
slouken@13354
   689
    }
slouken@13354
   690
    device->context = ctx;
slouken@13354
   691
slouken@12196
   692
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
   693
    xinput_device_change = SDL_TRUE;
slouken@13633
   694
    ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_CORRELATE_XINPUT, SDL_TRUE);
slouken@13633
   695
    if (ctx->xinput_enabled && (WIN_LoadXInputDLL() < 0 || !XINPUTGETSTATE)) {
slouken@12118
   696
        ctx->xinput_enabled = SDL_FALSE;
slouken@12118
   697
    }
slouken@12118
   698
    ctx->xinput_slot = XUSER_INDEX_ANY;
slouken@12118
   699
#endif
slouken@12196
   700
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
   701
    HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
slouken@12196
   702
#endif
slouken@12088
   703
slouken@12088
   704
    /* Set the controller LED */
slouken@13369
   705
    player_index = SDL_JoystickGetPlayerIndex(joystick);
slouken@13633
   706
    if (player_index >= 0 && device->dev) {
slouken@13369
   707
        SetSlotLED(device->dev, (player_index % 4));
slouken@13369
   708
    }
slouken@12088
   709
slouken@12088
   710
    /* Initialize the joystick capabilities */
slouken@12088
   711
    joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
slouken@12088
   712
    joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
slouken@12088
   713
    joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
slouken@12088
   714
slouken@12088
   715
    return SDL_TRUE;
slouken@12088
   716
}
slouken@12088
   717
slouken@12088
   718
static int
slouken@13480
   719
HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
zack@12787
   720
{
slouken@13480
   721
#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT)
slouken@13354
   722
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13480
   723
#endif
slouken@12196
   724
slouken@12118
   725
#ifdef __WIN32__
slouken@12196
   726
    SDL_bool rumbled = SDL_FALSE;
slouken@12196
   727
slouken@12196
   728
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
   729
    if (!rumbled && ctx->wgi_correlated) {
slouken@13633
   730
        WindowsGamingInputGamepadState *gamepad_state = ctx->wgi_slot;
slouken@12196
   731
        HRESULT hr;
slouken@13633
   732
        gamepad_state->vibration.LeftMotor = (DOUBLE)low_frequency_rumble / SDL_MAX_UINT16;
slouken@13633
   733
        gamepad_state->vibration.RightMotor = (DOUBLE)high_frequency_rumble / SDL_MAX_UINT16;
slouken@13633
   734
        hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(gamepad_state->gamepad, gamepad_state->vibration);
slouken@12196
   735
        if (SUCCEEDED(hr)) {
slouken@12196
   736
            rumbled = SDL_TRUE;
slouken@12196
   737
        }
slouken@12196
   738
    }
slouken@12196
   739
#endif
slouken@12196
   740
slouken@12196
   741
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
   742
    if (!rumbled && ctx->xinput_correlated) {
slouken@12118
   743
        XINPUT_VIBRATION XVibration;
slouken@12118
   744
slouken@12118
   745
        if (!XINPUTSETSTATE) {
slouken@12118
   746
            return SDL_Unsupported();
slouken@12118
   747
        }
slouken@12118
   748
slouken@12118
   749
        XVibration.wLeftMotorSpeed = low_frequency_rumble;
slouken@12118
   750
        XVibration.wRightMotorSpeed = high_frequency_rumble;
slouken@12196
   751
        if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) == ERROR_SUCCESS) {
slouken@12196
   752
            rumbled = SDL_TRUE;
slouken@12196
   753
        } else {
slouken@12118
   754
            return SDL_SetError("XInputSetState() failed");
slouken@12118
   755
        }
slouken@12118
   756
    }
slouken@12196
   757
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
slouken@12196
   758
slouken@12196
   759
#else /* !__WIN32__ */
slouken@12196
   760
slouken@12088
   761
#ifdef __MACOSX__
slouken@13563
   762
    if (IsBluetoothXboxOneController(device->vendor_id, device->product_id)) {
slouken@13563
   763
        Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
slouken@13563
   764
slouken@13563
   765
        rumble_packet[4] = (low_frequency_rumble >> 8);
slouken@13563
   766
        rumble_packet[5] = (high_frequency_rumble >> 8);
slouken@12088
   767
slouken@13563
   768
        if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
slouken@13563
   769
            return SDL_SetError("Couldn't send rumble packet");
slouken@13563
   770
        }
slouken@13563
   771
    } else {
slouken@13563
   772
        /* On Mac OS X the 360Controller driver uses this short report,
slouken@13563
   773
           and we need to prefix it with a magic token so hidapi passes it through untouched
slouken@13563
   774
         */
slouken@13563
   775
        Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
slouken@13563
   776
slouken@13563
   777
        rumble_packet[6+2] = (low_frequency_rumble >> 8);
slouken@13563
   778
        rumble_packet[6+3] = (high_frequency_rumble >> 8);
slouken@13563
   779
slouken@13563
   780
        if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
slouken@13563
   781
            return SDL_SetError("Couldn't send rumble packet");
slouken@13563
   782
        }
slouken@13563
   783
    }
slouken@12088
   784
#else
slouken@12088
   785
    Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
slouken@12088
   786
slouken@12088
   787
    rumble_packet[3] = (low_frequency_rumble >> 8);
slouken@12088
   788
    rumble_packet[4] = (high_frequency_rumble >> 8);
slouken@12088
   789
slouken@13481
   790
    if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
slouken@12088
   791
        return SDL_SetError("Couldn't send rumble packet");
slouken@12088
   792
    }
slouken@13563
   793
#endif
slouken@12118
   794
#endif /* __WIN32__ */
slouken@12088
   795
slouken@12088
   796
    return 0;
slouken@12088
   797
}
slouken@12088
   798
slouken@12117
   799
#ifdef __WIN32__
slouken@12116
   800
 /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
slouken@12116
   801
    however with this interface there is no rumble support, no guide button,
slouken@12116
   802
    and the left and right triggers are tied together as a single axis.
slouken@12196
   803
slouken@12196
   804
    We use XInput and Windows.Gaming.Input to make up for these shortcomings.
slouken@12116
   805
  */
slouken@12116
   806
static void
slouken@12117
   807
HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
slouken@12116
   808
{
slouken@13633
   809
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
   810
    Uint32 match_state = ctx->match_state;
slouken@13633
   811
    /* Update match_state with button bit, then fall through */
slouken@13633
   812
#   define SDL_PrivateJoystickButton(joystick, button, state) if (state) match_state |= 1 << (button); else match_state &=~(1<<(button)); SDL_PrivateJoystickButton(joystick, button, state)
slouken@13633
   813
    /* Grab high 4 bits of value, then fall through */
slouken@13633
   814
#   define SDL_PrivateJoystickAxis(joystick, axis, value) if (axis < 4) match_state = (match_state & ~(0xF << (4 * axis + 16))) | ((value) & 0xF000) << (4 * axis + 4); SDL_PrivateJoystickAxis(joystick, axis, value)
slouken@13633
   815
#endif
slouken@12116
   816
    Sint16 axis;
slouken@12196
   817
    SDL_bool has_trigger_data = SDL_FALSE;
slouken@12116
   818
slouken@12116
   819
    if (ctx->last_state[10] != data[10]) {
slouken@12116
   820
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   821
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[10] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   822
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[10] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   823
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[10] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   824
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[10] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   825
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[10] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   826
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[10] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   827
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[10] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   828
    }
slouken@12116
   829
slouken@12116
   830
    if (ctx->last_state[11] != data[11]) {
slouken@12116
   831
        SDL_bool dpad_up = SDL_FALSE;
slouken@12116
   832
        SDL_bool dpad_down = SDL_FALSE;
slouken@12116
   833
        SDL_bool dpad_left = SDL_FALSE;
slouken@12116
   834
        SDL_bool dpad_right = SDL_FALSE;
slouken@12116
   835
slouken@12116
   836
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[11] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   837
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[11] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   838
slouken@12116
   839
        switch (data[11] & 0x3C) {
slouken@12116
   840
        case 4:
slouken@12116
   841
            dpad_up = SDL_TRUE;
slouken@12116
   842
            break;
slouken@12116
   843
        case 8:
slouken@12116
   844
            dpad_up = SDL_TRUE;
slouken@12116
   845
            dpad_right = SDL_TRUE;
slouken@12116
   846
            break;
slouken@12116
   847
        case 12:
slouken@12116
   848
            dpad_right = SDL_TRUE;
slouken@12116
   849
            break;
slouken@12116
   850
        case 16:
slouken@12116
   851
            dpad_right = SDL_TRUE;
slouken@12116
   852
            dpad_down = SDL_TRUE;
slouken@12116
   853
            break;
slouken@12116
   854
        case 20:
slouken@12116
   855
            dpad_down = SDL_TRUE;
slouken@12116
   856
            break;
slouken@12116
   857
        case 24:
slouken@12116
   858
            dpad_left = SDL_TRUE;
slouken@12116
   859
            dpad_down = SDL_TRUE;
slouken@12116
   860
            break;
slouken@12116
   861
        case 28:
slouken@12116
   862
            dpad_left = SDL_TRUE;
slouken@12116
   863
            break;
slouken@12116
   864
        case 32:
slouken@12116
   865
            dpad_up = SDL_TRUE;
slouken@12116
   866
            dpad_left = SDL_TRUE;
slouken@12116
   867
            break;
slouken@12116
   868
        default:
slouken@12116
   869
            break;
slouken@12116
   870
        }
slouken@12116
   871
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
slouken@12116
   872
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
slouken@12116
   873
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
slouken@12116
   874
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
slouken@12116
   875
    }
slouken@12116
   876
slouken@12116
   877
    axis = (int)*(Uint16*)(&data[0]) - 0x8000;
slouken@12116
   878
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
slouken@12116
   879
    axis = (int)*(Uint16*)(&data[2]) - 0x8000;
slouken@12116
   880
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
slouken@12116
   881
    axis = (int)*(Uint16*)(&data[4]) - 0x8000;
slouken@12116
   882
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
slouken@12116
   883
    axis = (int)*(Uint16*)(&data[6]) - 0x8000;
slouken@12116
   884
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
slouken@12116
   885
slouken@13633
   886
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
   887
#undef SDL_PrivateJoystickAxis
slouken@13633
   888
#endif
slouken@12196
   889
slouken@12196
   890
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
   891
    /* Prefer XInput over WindowsGamingInput, it continues to provide data in the background */
slouken@13633
   892
    if (!has_trigger_data && ctx->xinput_enabled && ctx->xinput_correlated) {
slouken@13633
   893
        has_trigger_data = SDL_TRUE;
slouken@12118
   894
    }
slouken@12196
   895
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
slouken@12118
   896
slouken@13633
   897
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
   898
    if (!has_trigger_data && ctx->wgi_correlated) {
slouken@13633
   899
        has_trigger_data = SDL_TRUE;
slouken@13633
   900
    }
slouken@13633
   901
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
slouken@13633
   902
slouken@12196
   903
    if (!has_trigger_data) {
slouken@12118
   904
        axis = (data[9] * 257) - 32768;
slouken@12118
   905
        if (data[9] < 0x80) {
slouken@12118
   906
            axis = -axis * 2 - 32769;
slouken@13633
   907
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
slouken@12118
   908
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
slouken@12118
   909
        } else if (data[9] > 0x80) {
slouken@12118
   910
            axis = axis * 2 - 32767;
slouken@12118
   911
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
slouken@13633
   912
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
slouken@12118
   913
        } else {
slouken@12118
   914
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
slouken@12118
   915
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
slouken@12118
   916
        }
slouken@12116
   917
    }
slouken@12116
   918
slouken@13633
   919
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
   920
    ctx->match_state = match_state;
slouken@13633
   921
    ctx->last_state_packet = SDL_GetTicks();
slouken@13633
   922
#undef SDL_PrivateJoystickButton
slouken@13633
   923
#endif
slouken@12116
   924
    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
slouken@12116
   925
}
slouken@13633
   926
slouken@13633
   927
#ifdef SDL_JOYSTICK_RAWINPUT
slouken@13633
   928
static void
slouken@13633
   929
HIDAPI_DriverXbox360_HandleStatePacketFromRAWINPUT(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 *data, int size)
slouken@13633
   930
{
slouken@13633
   931
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13633
   932
    HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, data, size);
slouken@13633
   933
}
slouken@13633
   934
#endif
slouken@13633
   935
slouken@12117
   936
#else
slouken@12116
   937
slouken@12088
   938
static void
slouken@12088
   939
HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
slouken@12088
   940
{
slouken@12088
   941
    Sint16 axis;
slouken@12088
   942
#ifdef __MACOSX__
slouken@12118
   943
    const SDL_bool invert_y_axes = SDL_FALSE;
slouken@12088
   944
#else
slouken@12118
   945
    const SDL_bool invert_y_axes = SDL_TRUE;
slouken@12088
   946
#endif
slouken@12088
   947
slouken@12088
   948
    if (ctx->last_state[2] != data[2]) {
slouken@12088
   949
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   950
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   951
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   952
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   953
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   954
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   955
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   956
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   957
    }
slouken@12088
   958
slouken@12088
   959
    if (ctx->last_state[3] != data[3]) {
slouken@12088
   960
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   961
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   962
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   963
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   964
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   965
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   966
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   967
    }
slouken@12088
   968
slouken@12088
   969
    axis = ((int)data[4] * 257) - 32768;
slouken@12088
   970
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
slouken@12088
   971
    axis = ((int)data[5] * 257) - 32768;
slouken@12088
   972
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
slouken@12088
   973
    axis = *(Sint16*)(&data[6]);
slouken@12088
   974
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
slouken@12088
   975
    axis = *(Sint16*)(&data[8]);
slouken@12118
   976
    if (invert_y_axes) {
slouken@12118
   977
        axis = ~axis;
slouken@12118
   978
    }
slouken@12088
   979
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
slouken@12088
   980
    axis = *(Sint16*)(&data[10]);
slouken@12088
   981
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
slouken@12088
   982
    axis = *(Sint16*)(&data[12]);
slouken@12118
   983
    if (invert_y_axes) {
slouken@12118
   984
        axis = ~axis;
slouken@12118
   985
    }
slouken@12088
   986
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
slouken@12088
   987
slouken@12088
   988
    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
slouken@12088
   989
}
slouken@12117
   990
#endif /* __WIN32__ */
slouken@12088
   991
slouken@13633
   992
static void
slouken@13633
   993
HIDAPI_DriverXbox360_UpdateOtherAPIs(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
slouken@13633
   994
{
wasteland@13637
   995
#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
slouken@13633
   996
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13633
   997
    SDL_bool has_trigger_data = SDL_FALSE;
slouken@13633
   998
    SDL_bool correlated = SDL_FALSE;
slouken@13633
   999
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
  1000
    WindowsMatchState match_state_xinput;
slouken@13633
  1001
#endif
slouken@13633
  1002
slouken@13633
  1003
    /* Poll for trigger data once (not per-state-packet) */
slouken@13633
  1004
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
  1005
    /* Prefer XInput over WindowsGamingInput, it continues to provide data in the background */
slouken@13633
  1006
    if (!has_trigger_data && ctx->xinput_enabled && ctx->xinput_correlated) {
slouken@13633
  1007
        HIDAPI_DriverXbox360_UpdateXInput();
slouken@13633
  1008
        if (xinput_state[ctx->xinput_slot].connected) {
slouken@13633
  1009
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state[ctx->xinput_slot].state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
slouken@13633
  1010
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state[ctx->xinput_slot].state.Gamepad.bLeftTrigger * 257) - 32768);
slouken@13633
  1011
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state[ctx->xinput_slot].state.Gamepad.bRightTrigger * 257) - 32768);
slouken@13633
  1012
            has_trigger_data = SDL_TRUE;
slouken@13633
  1013
        }
slouken@13633
  1014
    }
slouken@13633
  1015
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
slouken@13633
  1016
slouken@13633
  1017
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
  1018
    if (!has_trigger_data && ctx->wgi_correlated) {
slouken@13633
  1019
        HIDAPI_DriverXbox360_UpdateWindowsGamingInput(); /* May detect disconnect / cause uncorrelation */
slouken@13633
  1020
        if (ctx->wgi_correlated) { /* Still connected */
slouken@13633
  1021
            struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading *state = &ctx->wgi_slot->state;
slouken@13633
  1022
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (state->Buttons & GamepadButtons_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
slouken@13633
  1023
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)(state->LeftTrigger * SDL_MAX_UINT16)) - 32768);
slouken@13633
  1024
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)(state->RightTrigger * SDL_MAX_UINT16)) - 32768);
slouken@13633
  1025
            has_trigger_data = SDL_TRUE;
slouken@13633
  1026
        }
slouken@13633
  1027
    }
slouken@13633
  1028
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
slouken@13633
  1029
slouken@13633
  1030
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
  1031
    HIDAPI_DriverXbox360_FillMatchState(&match_state_xinput, ctx->match_state);
slouken@13633
  1032
slouken@13633
  1033
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13633
  1034
    /* Parallel logic to WINDOWS_XINPUT below */
slouken@13633
  1035
    HIDAPI_DriverXbox360_UpdateWindowsGamingInput();
slouken@13633
  1036
    if (ctx->wgi_correlated) {
slouken@13633
  1037
        /* We have been previously correlated, ensure we are still matching, see comments in XINPUT section */
slouken@13633
  1038
        if (HIDAPI_DriverXbox360_WindowsGamingInputSlotMatches(&match_state_xinput, ctx->wgi_slot)) {
slouken@13633
  1039
            ctx->wgi_uncorrelate_count = 0;
slouken@13633
  1040
        } else {
slouken@13633
  1041
            ++ctx->wgi_uncorrelate_count;
slouken@13633
  1042
            /* Only un-correlate if this is consistent over multiple Update() calls - the timing of polling/event
slouken@13633
  1043
              pumping can easily cause this to uncorrelate for a frame.  2 seemed reliable in my testing, but
slouken@13633
  1044
              let's set it to 3 to be safe.  An incorrect un-correlation will simply result in lower precision
slouken@13633
  1045
              triggers for a frame. */
slouken@13633
  1046
            if (ctx->wgi_uncorrelate_count >= 3) {
slouken@13633
  1047
#ifdef DEBUG_JOYSTICK
slouken@13633
  1048
                SDL_Log("UN-Correlated joystick %d to WindowsGamingInput device #%d\n", joystick->instance_id, ctx->wgi_slot);
slouken@13633
  1049
#endif
slouken@13633
  1050
                HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotFree(ctx->wgi_slot);
slouken@13633
  1051
                ctx->wgi_correlated = SDL_FALSE;
slouken@13633
  1052
                ctx->wgi_correlation_count = 0;
slouken@13633
  1053
                /* Force immediate update of triggers */
slouken@13633
  1054
                HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
slouken@13633
  1055
                /* Force release of Guide button, it can't possibly be down on this device now. */
slouken@13633
  1056
                /* It gets left down if we were actually correlated incorrectly and it was released on the WindowsGamingInput
slouken@13633
  1057
                  device but we didn't get a state packet. */
slouken@13633
  1058
                SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
slouken@13633
  1059
            }
slouken@13633
  1060
        }
slouken@13633
  1061
    }
slouken@13633
  1062
    if (!ctx->wgi_correlated) {
slouken@13633
  1063
        SDL_bool new_correlation_count = 0;
slouken@13633
  1064
        if (HIDAPI_DriverXbox360_MissingWindowsGamingInputSlot()) {
slouken@13633
  1065
            Uint8 correlation_id;
slouken@13633
  1066
            WindowsGamingInputGamepadState *slot_idx;
slouken@13633
  1067
            if (HIDAPI_DriverXbox360_GuessWindowsGamingInputSlot(&match_state_xinput, &correlation_id, &slot_idx)) {
slouken@13633
  1068
                /* we match exactly one WindowsGamingInput device */
slouken@13633
  1069
                /* Probably can do without wgi_correlation_count, just check and clear wgi_slot to NULL, unless we need
slouken@13633
  1070
                   even more frames to be sure. */
slouken@13633
  1071
                if (ctx->wgi_correlation_count && ctx->wgi_slot == slot_idx) {
slouken@13633
  1072
                    /* was correlated previously, and still the same device */
slouken@13633
  1073
                    if (ctx->wgi_correlation_id + 1 == correlation_id) {
slouken@13633
  1074
                        /* no one else was correlated in the meantime */
slouken@13633
  1075
                        new_correlation_count = ctx->wgi_correlation_count + 1;
slouken@13633
  1076
                        if (new_correlation_count == 2) {
slouken@13633
  1077
                            /* correlation stayed steady and uncontested across multiple frames, guaranteed match */
slouken@13633
  1078
                            ctx->wgi_correlated = SDL_TRUE;
slouken@13633
  1079
#ifdef DEBUG_JOYSTICK
slouken@13633
  1080
                            SDL_Log("Correlated joystick %d to WindowsGamingInput device #%d\n", joystick->instance_id, slot_idx);
slouken@13633
  1081
#endif
slouken@13633
  1082
                            correlated = SDL_TRUE;
slouken@13633
  1083
                            HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotUsed(ctx->wgi_slot, ctx);
slouken@13633
  1084
                            /* If the generalized Guide button was using us, it doesn't need to anymore */
slouken@13633
  1085
                            if (guide_button_candidate.joystick == joystick)
slouken@13633
  1086
                                guide_button_candidate.joystick = NULL;
slouken@13633
  1087
                            if (guide_button_candidate.last_joystick == joystick)
slouken@13633
  1088
                                guide_button_candidate.last_joystick = NULL;
slouken@13633
  1089
                            /* Force immediate update of guide button / triggers */
slouken@13633
  1090
                            HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
slouken@13633
  1091
                        }
slouken@13633
  1092
                    } else {
slouken@13633
  1093
                        /* someone else also possibly correlated to this device, start over */
slouken@13633
  1094
                        new_correlation_count = 1;
slouken@13633
  1095
                    }
slouken@13633
  1096
                } else {
slouken@13633
  1097
                    /* new possible correlation */
slouken@13633
  1098
                    new_correlation_count = 1;
slouken@13633
  1099
                    ctx->wgi_slot = slot_idx;
slouken@13633
  1100
                }
slouken@13633
  1101
                ctx->wgi_correlation_id = correlation_id;
slouken@13633
  1102
            } else {
slouken@13633
  1103
                /* Match multiple WindowsGamingInput devices, or none (possibly due to no buttons pressed) */
slouken@13633
  1104
            }
slouken@13633
  1105
        }
slouken@13633
  1106
        ctx->wgi_correlation_count = new_correlation_count;
slouken@13633
  1107
    } else {
slouken@13633
  1108
        correlated = SDL_TRUE;
slouken@13633
  1109
    }
slouken@13633
  1110
#endif
slouken@13633
  1111
slouken@13633
  1112
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
  1113
    /* Parallel logic to WINDOWS_GAMING_INPUT above */
slouken@13633
  1114
    if (ctx->xinput_enabled) {
slouken@13633
  1115
        HIDAPI_DriverXbox360_UpdateXInput();
slouken@13633
  1116
        if (ctx->xinput_correlated) {
slouken@13633
  1117
            /* We have been previously correlated, ensure we are still matching */
slouken@13633
  1118
            /* This is required to deal with two (mostly) un-preventable mis-correlation situations:
slouken@13633
  1119
              A) Since the HID data stream does not provide an initial state (but polling XInput does), if we open
slouken@13633
  1120
                 5 controllers (#1-4 XInput mapped, #5 is not), and controller 1 had the A button down (and we don't
slouken@13633
  1121
                 know), and the user presses A on controller #5, we'll see exactly 1 controller with A down (#5) and
slouken@13633
  1122
                 exactly 1 XInput device with A down (#1), and incorrectly correlate.  This code will then un-correlate
slouken@13633
  1123
                 when A is released from either controller #1 or #5.
slouken@13633
  1124
              B) Since the app may not open all controllers, we could have a similar situation where only controller #5
slouken@13633
  1125
                 is opened, and the user holds A on controllers #1 and #5 simultaneously - again we see only 1 controller
slouken@13633
  1126
                 with A down and 1 XInput device with A down, and incorrectly correlate.  This should be very unusual
slouken@13633
  1127
                 (only when apps do not open all controllers, yet are listening to Guide button presses, yet
slouken@13633
  1128
                 for some reason want to ignore guide button presses on the un-opened controllers, yet users are
slouken@13633
  1129
                 pressing buttons on the unopened controllers), and will resolve itself when either button is released
slouken@13633
  1130
                 and we un-correlate.  We could prevent this by processing the state packets for *all* controllers,
slouken@13633
  1131
                 even un-opened ones, as that would allow more precise correlation.
slouken@13633
  1132
            */
slouken@13633
  1133
            if (HIDAPI_DriverXbox360_XInputSlotMatches(&match_state_xinput, ctx->xinput_slot)) {
slouken@13633
  1134
                ctx->xinput_uncorrelate_count = 0;
slouken@13633
  1135
            } else {
slouken@13633
  1136
                ++ctx->xinput_uncorrelate_count;
slouken@13633
  1137
                /* Only un-correlate if this is consistent over multiple Update() calls - the timing of polling/event
slouken@13633
  1138
                  pumping can easily cause this to uncorrelate for a frame.  2 seemed reliable in my testing, but
slouken@13633
  1139
                  let's set it to 3 to be safe.  An incorrect un-correlation will simply result in lower precision
slouken@13633
  1140
                  triggers for a frame. */
slouken@13633
  1141
                if (ctx->xinput_uncorrelate_count >= 3) {
slouken@13633
  1142
#ifdef DEBUG_JOYSTICK
slouken@13633
  1143
                    SDL_Log("UN-Correlated joystick %d to XInput device #%d\n", joystick->instance_id, ctx->xinput_slot);
slouken@13633
  1144
#endif
slouken@13633
  1145
                    HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
slouken@13633
  1146
                    ctx->xinput_correlated = SDL_FALSE;
slouken@13633
  1147
                    ctx->xinput_correlation_count = 0;
slouken@13633
  1148
                    /* Force immediate update of triggers */
slouken@13633
  1149
                    HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
slouken@13633
  1150
                    /* Force release of Guide button, it can't possibly be down on this device now. */
slouken@13633
  1151
                    /* It gets left down if we were actually correlated incorrectly and it was released on the XInput
slouken@13633
  1152
                      device but we didn't get a state packet. */
slouken@13633
  1153
                    SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
slouken@13633
  1154
                }
slouken@13633
  1155
            }
slouken@13633
  1156
        }
slouken@13633
  1157
        if (!ctx->xinput_correlated) {
slouken@13633
  1158
            SDL_bool new_correlation_count = 0;
slouken@13633
  1159
            if (HIDAPI_DriverXbox360_MissingXInputSlot()) {
icculus@13840
  1160
                Uint8 correlation_id = 0;
icculus@13840
  1161
                Uint8 slot_idx = 0;
slouken@13633
  1162
                if (HIDAPI_DriverXbox360_GuessXInputSlot(&match_state_xinput, &correlation_id, &slot_idx)) {
slouken@13633
  1163
                    /* we match exactly one XInput device */
slouken@13633
  1164
                    /* Probably can do without xinput_correlation_count, just check and clear xinput_slot to ANY, unless
slouken@13633
  1165
                       we need even more frames to be sure */
slouken@13633
  1166
                    if (ctx->xinput_correlation_count && ctx->xinput_slot == slot_idx) {
slouken@13633
  1167
                        /* was correlated previously, and still the same device */
slouken@13633
  1168
                        if (ctx->xinput_correlation_id + 1 == correlation_id) {
slouken@13633
  1169
                            /* no one else was correlated in the meantime */
slouken@13633
  1170
                            new_correlation_count = ctx->xinput_correlation_count + 1;
slouken@13633
  1171
                            if (new_correlation_count == 2) {
slouken@13633
  1172
                                /* correlation stayed steady and uncontested across multiple frames, guaranteed match */
slouken@13633
  1173
                                ctx->xinput_correlated = SDL_TRUE;
slouken@13633
  1174
#ifdef DEBUG_JOYSTICK
slouken@13633
  1175
                                SDL_Log("Correlated joystick %d to XInput device #%d\n", joystick->instance_id, slot_idx);
slouken@13633
  1176
#endif
slouken@13633
  1177
                                correlated = SDL_TRUE;
slouken@13633
  1178
                                HIDAPI_DriverXbox360_MarkXInputSlotUsed(ctx->xinput_slot);
slouken@13633
  1179
                                /* If the generalized Guide button was using us, it doesn't need to anymore */
slouken@13633
  1180
                                if (guide_button_candidate.joystick == joystick)
slouken@13633
  1181
                                    guide_button_candidate.joystick = NULL;
slouken@13633
  1182
                                if (guide_button_candidate.last_joystick == joystick)
slouken@13633
  1183
                                    guide_button_candidate.last_joystick = NULL;
slouken@13633
  1184
                                /* Force immediate update of guide button / triggers */
slouken@13633
  1185
                                HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
slouken@13633
  1186
                            }
slouken@13633
  1187
                        } else {
slouken@13633
  1188
                            /* someone else also possibly correlated to this device, start over */
slouken@13633
  1189
                            new_correlation_count = 1;
slouken@13633
  1190
                        }
slouken@13633
  1191
                    } else {
slouken@13633
  1192
                        /* new possible correlation */
slouken@13633
  1193
                        new_correlation_count = 1;
slouken@13633
  1194
                        ctx->xinput_slot = slot_idx;
slouken@13633
  1195
                    }
slouken@13633
  1196
                    ctx->xinput_correlation_id = correlation_id;
slouken@13633
  1197
                } else {
slouken@13633
  1198
                    /* Match multiple XInput devices, or none (possibly due to no buttons pressed) */
slouken@13633
  1199
                }
slouken@13633
  1200
            }
slouken@13633
  1201
            ctx->xinput_correlation_count = new_correlation_count;
slouken@13633
  1202
        } else {
slouken@13633
  1203
            correlated = SDL_TRUE;
slouken@13633
  1204
        }
slouken@13633
  1205
    }
slouken@13633
  1206
#endif
slouken@13633
  1207
slouken@13633
  1208
    if (!correlated) {
slouken@13633
  1209
        if (!guide_button_candidate.joystick ||
slouken@13633
  1210
            (ctx->last_state_packet && (
slouken@13633
  1211
                !guide_button_candidate.last_state_packet ||
slouken@13633
  1212
                SDL_TICKS_PASSED(ctx->last_state_packet, guide_button_candidate.last_state_packet)
slouken@13633
  1213
            ))
slouken@13633
  1214
        ) {
slouken@13633
  1215
            guide_button_candidate.joystick = joystick;
slouken@13633
  1216
            guide_button_candidate.last_state_packet = ctx->last_state_packet;
slouken@13633
  1217
        }
slouken@13633
  1218
    }
slouken@13633
  1219
#endif
wasteland@13637
  1220
#endif
slouken@13633
  1221
}
slouken@13633
  1222
slouken@12111
  1223
static SDL_bool
slouken@13354
  1224
HIDAPI_DriverXbox360_UpdateDevice(SDL_HIDAPI_Device *device)
slouken@12088
  1225
{
slouken@13354
  1226
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13354
  1227
    SDL_Joystick *joystick = NULL;
slouken@12088
  1228
    Uint8 data[USB_PACKET_LENGTH];
slouken@13633
  1229
    int size = 0;
slouken@12088
  1230
slouken@13354
  1231
    if (device->num_joysticks > 0) {
slouken@13354
  1232
        joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
slouken@13354
  1233
    }
slouken@13354
  1234
    if (!joystick) {
slouken@13354
  1235
        return SDL_FALSE;
slouken@13354
  1236
    }
slouken@13354
  1237
slouken@13633
  1238
    while (device->dev && (size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
slouken@13354
  1239
        HIDAPI_DriverXbox360_HandleStatePacket(joystick, device->dev, ctx, data, size);
slouken@12088
  1240
    }
slouken@12088
  1241
slouken@13354
  1242
    if (size < 0) {
slouken@13354
  1243
        /* Read error, device is disconnected */
slouken@13633
  1244
        HIDAPI_JoystickDisconnected(device, joystick->instance_id, SDL_FALSE);
slouken@13633
  1245
    } else {
slouken@13633
  1246
        HIDAPI_DriverXbox360_UpdateOtherAPIs(device, joystick);
slouken@13354
  1247
    }
slouken@13633
  1248
    
slouken@12118
  1249
    return (size >= 0);
slouken@12088
  1250
}
slouken@12088
  1251
slouken@12088
  1252
static void
slouken@13354
  1253
HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
slouken@12088
  1254
{
slouken@13480
  1255
#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
slouken@13354
  1256
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13480
  1257
#endif
slouken@13633
  1258
    
slouken@13633
  1259
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
slouken@13633
  1260
    if (guide_button_candidate.joystick == joystick)
slouken@13633
  1261
        guide_button_candidate.joystick = NULL;
slouken@13633
  1262
    if (guide_button_candidate.last_joystick == joystick)
slouken@13633
  1263
        guide_button_candidate.last_joystick = NULL;
slouken@13633
  1264
#endif
slouken@12118
  1265
slouken@12196
  1266
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@13633
  1267
    xinput_device_change = SDL_TRUE;
slouken@12118
  1268
    if (ctx->xinput_enabled) {
slouken@13633
  1269
        if (ctx->xinput_correlated) {
slouken@13633
  1270
            HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
slouken@13633
  1271
        }
slouken@12118
  1272
        WIN_UnloadXInputDLL();
slouken@12118
  1273
    }
slouken@12118
  1274
#endif
slouken@12196
  1275
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13354
  1276
    HIDAPI_DriverXbox360_QuitWindowsGamingInput(ctx);
slouken@12196
  1277
#endif
slouken@13354
  1278
slouken@13633
  1279
    if (device->dev) {
slouken@13633
  1280
        hid_close(device->dev);
slouken@13633
  1281
        device->dev = NULL;
slouken@13633
  1282
    }
slouken@13354
  1283
slouken@13354
  1284
    SDL_free(device->context);
slouken@13354
  1285
    device->context = NULL;
slouken@13354
  1286
}
slouken@13354
  1287
slouken@13354
  1288
static void
slouken@13354
  1289
HIDAPI_DriverXbox360_FreeDevice(SDL_HIDAPI_Device *device)
slouken@13354
  1290
{
slouken@12088
  1291
}
slouken@12088
  1292
slouken@12088
  1293
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
slouken@12088
  1294
{
slouken@12119
  1295
    SDL_HINT_JOYSTICK_HIDAPI_XBOX,
slouken@12088
  1296
    SDL_TRUE,
slouken@12088
  1297
    HIDAPI_DriverXbox360_IsSupportedDevice,
slouken@12088
  1298
    HIDAPI_DriverXbox360_GetDeviceName,
slouken@13354
  1299
    HIDAPI_DriverXbox360_InitDevice,
slouken@13369
  1300
    HIDAPI_DriverXbox360_GetDevicePlayerIndex,
slouken@13369
  1301
    HIDAPI_DriverXbox360_SetDevicePlayerIndex,
slouken@13354
  1302
    HIDAPI_DriverXbox360_UpdateDevice,
slouken@13354
  1303
    HIDAPI_DriverXbox360_OpenJoystick,
slouken@13354
  1304
    HIDAPI_DriverXbox360_RumbleJoystick,
slouken@13354
  1305
    HIDAPI_DriverXbox360_CloseJoystick,
slouken@13633
  1306
    HIDAPI_DriverXbox360_FreeDevice,
slouken@13633
  1307
    HIDAPI_DriverXbox360_PostUpdate,
slouken@13633
  1308
#ifdef SDL_JOYSTICK_RAWINPUT
slouken@13633
  1309
    HIDAPI_DriverXbox360_HandleStatePacketFromRAWINPUT,
slouken@13633
  1310
#endif
slouken@12088
  1311
};
slouken@12088
  1312
slouken@12088
  1313
#endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */
slouken@12088
  1314
slouken@12088
  1315
#endif /* SDL_JOYSTICK_HIDAPI */
slouken@12088
  1316
slouken@12088
  1317
/* vi: set ts=4 sw=4 expandtab: */