src/joystick/hidapi/SDL_hidapi_xbox360.c
author Sam Lantinga
Mon, 02 Mar 2020 09:46:37 -0800
changeset 13571 b2b43ee5f1bf
parent 13568 2c4ec356333e
child 13574 09a0db265f3d
permissions -rw-r--r--
Fixed compile 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_log.h"
slouken@12088
    27
#include "SDL_events.h"
slouken@12088
    28
#include "SDL_timer.h"
slouken@12088
    29
#include "SDL_joystick.h"
slouken@12088
    30
#include "SDL_gamecontroller.h"
slouken@12088
    31
#include "../SDL_sysjoystick.h"
slouken@12088
    32
#include "SDL_hidapijoystick_c.h"
slouken@13481
    33
#include "SDL_hidapi_rumble.h"
slouken@12088
    34
slouken@12088
    35
slouken@12088
    36
#ifdef SDL_JOYSTICK_HIDAPI_XBOX360
slouken@12088
    37
slouken@12118
    38
#ifdef __WIN32__
slouken@12196
    39
#define SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12196
    40
/* This requires the Windows 10 SDK to build */
slouken@12196
    41
/*#define SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT*/
slouken@12196
    42
#endif
slouken@12196
    43
slouken@12196
    44
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12118
    45
#include "../../core/windows/SDL_xinput.h"
slouken@12118
    46
#endif
slouken@12118
    47
slouken@12196
    48
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
    49
#include "../../core/windows/SDL_windows.h"
slouken@12196
    50
#define COBJMACROS
slouken@12196
    51
#include "windows.gaming.input.h"
slouken@12196
    52
#endif
slouken@12196
    53
slouken@12088
    54
slouken@12088
    55
typedef struct {
slouken@12088
    56
    Uint8 last_state[USB_PACKET_LENGTH];
slouken@12196
    57
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12118
    58
    SDL_bool xinput_enabled;
slouken@12118
    59
    Uint8 xinput_slot;
slouken@12118
    60
#endif
slouken@12196
    61
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
    62
    SDL_bool coinitialized;
slouken@12196
    63
    __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics *gamepad_statics;
slouken@12196
    64
    __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
slouken@12196
    65
    struct __x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration;
slouken@12196
    66
#endif
slouken@12088
    67
} SDL_DriverXbox360_Context;
slouken@12088
    68
slouken@12088
    69
slouken@12196
    70
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12118
    71
static Uint8 xinput_slots;
slouken@12118
    72
slouken@12118
    73
static void
slouken@12118
    74
HIDAPI_DriverXbox360_MarkXInputSlotUsed(Uint8 xinput_slot)
slouken@12118
    75
{
slouken@12118
    76
    if (xinput_slot != XUSER_INDEX_ANY) {
slouken@12118
    77
        xinput_slots |= (0x01 << xinput_slot);
slouken@12118
    78
    }
slouken@12118
    79
}
slouken@12118
    80
slouken@12118
    81
static void
slouken@12118
    82
HIDAPI_DriverXbox360_MarkXInputSlotFree(Uint8 xinput_slot)
slouken@12118
    83
{
slouken@12118
    84
    if (xinput_slot != XUSER_INDEX_ANY) {
slouken@12118
    85
        xinput_slots &= ~(0x01 << xinput_slot);
slouken@12118
    86
    }
slouken@12118
    87
}
slouken@12118
    88
slouken@12118
    89
static SDL_bool
slouken@12118
    90
HIDAPI_DriverXbox360_MissingXInputSlot()
slouken@12118
    91
{
slouken@12118
    92
    return xinput_slots != 0x0F;
slouken@12118
    93
}
slouken@12118
    94
slouken@12118
    95
static Uint8
slouken@12118
    96
HIDAPI_DriverXbox360_GuessXInputSlot(WORD wButtons)
slouken@12118
    97
{
slouken@12118
    98
    DWORD user_index;
slouken@12118
    99
    int match_count;
slouken@12118
   100
    Uint8 match_slot;
slouken@12118
   101
slouken@12118
   102
    if (!XINPUTGETSTATE) {
slouken@12118
   103
        return XUSER_INDEX_ANY;
slouken@12118
   104
    }
slouken@12118
   105
slouken@12118
   106
    match_count = 0;
slouken@12118
   107
    for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
slouken@12118
   108
        XINPUT_STATE_EX xinput_state;
slouken@12118
   109
slouken@12118
   110
        if (XINPUTGETSTATE(user_index, &xinput_state) == ERROR_SUCCESS) {
slouken@12118
   111
            if (xinput_state.Gamepad.wButtons == wButtons) {
slouken@12118
   112
                ++match_count;
slouken@12118
   113
                match_slot = (Uint8)user_index;
slouken@12118
   114
            }
slouken@12118
   115
        }
slouken@12118
   116
    }
slouken@12118
   117
    if (match_count == 1) {
slouken@12118
   118
        return match_slot;
slouken@12118
   119
    }
slouken@12118
   120
    return XUSER_INDEX_ANY;
slouken@12118
   121
}
slouken@12118
   122
slouken@12196
   123
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
slouken@12196
   124
slouken@12196
   125
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
   126
slouken@12196
   127
static void
slouken@12196
   128
HIDAPI_DriverXbox360_InitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
slouken@12196
   129
{
slouken@12196
   130
    /* I think this takes care of RoInitialize() in a way that is compatible with the rest of SDL */
slouken@12196
   131
    if (FAILED(WIN_CoInitialize())) {
slouken@12196
   132
        return;
slouken@12196
   133
    }
slouken@12196
   134
    ctx->coinitialized = SDL_TRUE;
slouken@12196
   135
slouken@12196
   136
    {
slouken@12196
   137
        static const IID SDL_IID_IGamepadStatics = { 0x8BBCE529, 0xD49C, 0x39E9, { 0x95, 0x60, 0xE4, 0x7D, 0xDE, 0x96, 0xB7, 0xC8 } };
slouken@12196
   138
        HRESULT hr;
slouken@12196
   139
        HMODULE hModule = LoadLibraryA("combase.dll");
slouken@12196
   140
        if (hModule != NULL) {
slouken@12197
   141
            typedef HRESULT (WINAPI *WindowsCreateString_t)(PCNZWCH sourceString, UINT32 length, HSTRING* string);
slouken@12197
   142
            typedef HRESULT (WINAPI *WindowsDeleteString_t)(HSTRING string);
slouken@12197
   143
            typedef HRESULT (WINAPI *RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void** factory);
slouken@12196
   144
slouken@12196
   145
            WindowsCreateString_t WindowsCreateStringFunc = (WindowsCreateString_t)GetProcAddress(hModule, "WindowsCreateString");
slouken@12197
   146
            WindowsDeleteString_t WindowsDeleteStringFunc = (WindowsDeleteString_t)GetProcAddress(hModule, "WindowsDeleteString");
slouken@12196
   147
            RoGetActivationFactory_t RoGetActivationFactoryFunc = (RoGetActivationFactory_t)GetProcAddress(hModule, "RoGetActivationFactory");
slouken@12197
   148
            if (WindowsCreateStringFunc && WindowsDeleteStringFunc && RoGetActivationFactoryFunc) {
slouken@12196
   149
                LPTSTR pNamespace = L"Windows.Gaming.Input.Gamepad";
slouken@12196
   150
                HSTRING hNamespaceString;
slouken@12196
   151
slouken@12196
   152
                hr = WindowsCreateStringFunc(pNamespace, SDL_wcslen(pNamespace), &hNamespaceString);
slouken@12196
   153
                if (SUCCEEDED(hr)) {
slouken@12197
   154
                    RoGetActivationFactoryFunc(hNamespaceString, &SDL_IID_IGamepadStatics, &ctx->gamepad_statics);
slouken@12197
   155
                    WindowsDeleteStringFunc(hNamespaceString);
slouken@12196
   156
                }
slouken@12196
   157
            }
slouken@12196
   158
            FreeLibrary(hModule);
slouken@12196
   159
        }
slouken@12196
   160
    }
slouken@12196
   161
}
slouken@12196
   162
slouken@12196
   163
static Uint8
slouken@12196
   164
HIDAPI_DriverXbox360_GetGamepadButtonsForMatch(__x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad)
slouken@12196
   165
{
slouken@12196
   166
    HRESULT hr;
slouken@12196
   167
    struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
slouken@12196
   168
    Uint8 buttons = 0;
slouken@12196
   169
slouken@12196
   170
    hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(gamepad, &state);
slouken@12196
   171
    if (SUCCEEDED(hr)) {
slouken@12196
   172
        if (state.Buttons & GamepadButtons_A) {
slouken@12196
   173
            buttons |= (1 << SDL_CONTROLLER_BUTTON_A);
slouken@12196
   174
        }
slouken@12196
   175
        if (state.Buttons & GamepadButtons_B) {
slouken@12196
   176
            buttons |= (1 << SDL_CONTROLLER_BUTTON_B);
slouken@12196
   177
        }
slouken@12196
   178
        if (state.Buttons & GamepadButtons_X) {
slouken@12196
   179
            buttons |= (1 << SDL_CONTROLLER_BUTTON_X);
slouken@12196
   180
        }
slouken@12196
   181
        if (state.Buttons & GamepadButtons_Y) {
slouken@12196
   182
            buttons |= (1 << SDL_CONTROLLER_BUTTON_Y);
slouken@12196
   183
        }
slouken@12196
   184
    }
slouken@12196
   185
    return buttons;
slouken@12196
   186
}
slouken@12196
   187
slouken@12196
   188
static void
slouken@12196
   189
HIDAPI_DriverXbox360_GuessGamepad(SDL_DriverXbox360_Context *ctx, Uint8 buttons)
slouken@12196
   190
{
slouken@12196
   191
    HRESULT hr;
slouken@12196
   192
    __FIVectorView_1_Windows__CGaming__CInput__CGamepad *gamepads;
slouken@12196
   193
slouken@12196
   194
    hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_get_Gamepads(ctx->gamepad_statics, &gamepads);
slouken@12196
   195
    if (SUCCEEDED(hr)) {
slouken@12196
   196
        unsigned int i, num_gamepads;
slouken@12196
   197
slouken@12196
   198
        hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_get_Size(gamepads, &num_gamepads);
slouken@12196
   199
        if (SUCCEEDED(hr)) {
slouken@12196
   200
            int match_count;
slouken@12196
   201
            unsigned int match_slot;
slouken@12196
   202
slouken@12196
   203
            match_count = 0;
slouken@12196
   204
            for (i = 0; i < num_gamepads; ++i) {
slouken@12196
   205
                __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
slouken@12196
   206
slouken@12196
   207
                hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, i, &gamepad);
slouken@12196
   208
                if (SUCCEEDED(hr)) {
slouken@12196
   209
                    Uint8 gamepad_buttons = HIDAPI_DriverXbox360_GetGamepadButtonsForMatch(gamepad);
slouken@12196
   210
                    if (buttons == gamepad_buttons) {
slouken@12196
   211
                        ++match_count;
slouken@12196
   212
                        match_slot = i;
slouken@12196
   213
                    }
slouken@12196
   214
                    __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
slouken@12196
   215
                }
slouken@12196
   216
            }
slouken@12196
   217
            if (match_count == 1) {
slouken@12196
   218
                hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, match_slot, &ctx->gamepad);
slouken@12196
   219
                if (SUCCEEDED(hr)) {
slouken@12196
   220
                }
slouken@12196
   221
            }
slouken@12196
   222
        }
slouken@12196
   223
        __FIVectorView_1_Windows__CGaming__CInput__CGamepad_Release(gamepads);
slouken@12196
   224
    }
slouken@12196
   225
}
slouken@12196
   226
slouken@12196
   227
static void
slouken@12196
   228
HIDAPI_DriverXbox360_QuitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
slouken@12196
   229
{
slouken@12196
   230
    if (ctx->gamepad_statics) {
slouken@12196
   231
        __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_Release(ctx->gamepad_statics);
slouken@12196
   232
        ctx->gamepad_statics = NULL;
slouken@12196
   233
    }
slouken@12196
   234
    if (ctx->gamepad) {
slouken@12196
   235
        __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(ctx->gamepad);
slouken@12196
   236
        ctx->gamepad = NULL;
slouken@12196
   237
    }
slouken@12196
   238
slouken@12196
   239
    if (ctx->coinitialized) {
slouken@12196
   240
        WIN_CoUninitialize();
slouken@12196
   241
        ctx->coinitialized = SDL_FALSE;
slouken@12196
   242
    }
slouken@12196
   243
}
slouken@12196
   244
slouken@12196
   245
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
slouken@12118
   246
slouken@13571
   247
#if defined(__MACOSX__)
slouken@12088
   248
static SDL_bool
slouken@13563
   249
IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
slouken@13563
   250
{
slouken@13563
   251
    /* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
slouken@13563
   252
    if (vendor_id == USB_VENDOR_MICROSOFT) {
slouken@13563
   253
        if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
slouken@13563
   254
            product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
slouken@13563
   255
            product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
slouken@13563
   256
            return SDL_TRUE;
slouken@13563
   257
        }
slouken@13563
   258
    }
slouken@13563
   259
    return SDL_FALSE;
slouken@13563
   260
}
slouken@13571
   261
#endif
slouken@13563
   262
slouken@13563
   263
static SDL_bool
slouken@13431
   264
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
   265
{
slouken@13429
   266
    const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
slouken@13275
   267
slouken@13429
   268
    if (vendor_id == USB_VENDOR_NVIDIA) {
slouken@13337
   269
        /* This is the NVIDIA Shield controller which doesn't talk Xbox controller protocol */
slouken@13297
   270
        return SDL_FALSE;
slouken@13297
   271
    }
slouken@13429
   272
    if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == 0x0291 || product_id == 0x0719)) ||
slouken@13429
   273
        (type == SDL_CONTROLLER_TYPE_XBOX360 && interface_protocol == XB360W_IFACE_PROTOCOL)) {
slouken@13429
   274
        /* This is the wireless dongle, which talks a different protocol */
slouken@13429
   275
        return SDL_FALSE;
slouken@13365
   276
    }
slouken@13350
   277
    if (interface_number > 0) {
slouken@13350
   278
        /* This is the chatpad or other input interface, not the Xbox 360 interface */
slouken@13350
   279
        return SDL_FALSE;
slouken@13350
   280
    }
slouken@12117
   281
#if defined(__MACOSX__) || defined(__WIN32__)
slouken@13429
   282
    if (vendor_id == USB_VENDOR_MICROSOFT && product_id == 0x028e && version == 1) {
slouken@12118
   283
        /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
slouken@12118
   284
        return SDL_FALSE;
slouken@12118
   285
    }
slouken@13429
   286
    if (vendor_id == USB_VENDOR_MICROSOFT && product_id == 0x02e0) {
aicommander@12769
   287
        /* This is the old Bluetooth Xbox One S firmware, which isn't supported by this driver */
aicommander@12769
   288
        return SDL_FALSE;
aicommander@12769
   289
    }
slouken@13568
   290
#if defined(__MACOSX__)
slouken@13568
   291
    /* Wired Xbox One controllers are handled by this driver, interfacing with
slouken@13568
   292
       the 360Controller driver available from:
slouken@13568
   293
       https://github.com/360Controller/360Controller/releases
slouken@13568
   294
slouken@13568
   295
       Bluetooth Xbox One controllers are handled by the SDL Xbox One driver
slouken@13568
   296
    */
slouken@13568
   297
    if (IsBluetoothXboxOneController(vendor_id, product_id)) {
slouken@13568
   298
        return SDL_FALSE;
slouken@13568
   299
    }
slouken@13568
   300
#endif
slouken@13275
   301
    return (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE);
slouken@12088
   302
#else
slouken@13275
   303
    return (type == SDL_CONTROLLER_TYPE_XBOX360);
slouken@12088
   304
#endif
slouken@12088
   305
}
slouken@12088
   306
slouken@12088
   307
static const char *
slouken@12088
   308
HIDAPI_DriverXbox360_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
slouken@12088
   309
{
slouken@13330
   310
    return NULL;
slouken@12088
   311
}
slouken@12088
   312
slouken@12088
   313
static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
slouken@12088
   314
{
slouken@13365
   315
    Uint8 mode = 0x02 + slot;
slouken@13365
   316
    const Uint8 led_packet[] = { 0x01, 0x03, mode };
slouken@12088
   317
slouken@12088
   318
    if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
slouken@12118
   319
        return SDL_FALSE;
slouken@12118
   320
    }
slouken@12118
   321
    return SDL_TRUE;
slouken@12088
   322
}
slouken@12088
   323
slouken@12088
   324
static SDL_bool
slouken@13354
   325
HIDAPI_DriverXbox360_InitDevice(SDL_HIDAPI_Device *device)
slouken@13354
   326
{
slouken@13354
   327
    return HIDAPI_JoystickConnected(device, NULL);
slouken@13354
   328
}
slouken@13354
   329
slouken@13369
   330
static int
slouken@13369
   331
HIDAPI_DriverXbox360_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
slouken@13369
   332
{
slouken@13369
   333
    return -1;
slouken@13369
   334
}
slouken@13369
   335
slouken@13369
   336
static void
slouken@13369
   337
HIDAPI_DriverXbox360_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
slouken@13369
   338
{
slouken@13369
   339
    if (device->dev) {
slouken@13369
   340
        SetSlotLED(device->dev, (player_index % 4));
slouken@13369
   341
    }
slouken@13369
   342
}
slouken@13369
   343
slouken@13354
   344
static SDL_bool
slouken@13354
   345
HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
slouken@12088
   346
{
slouken@12088
   347
    SDL_DriverXbox360_Context *ctx;
slouken@13369
   348
    int player_index;
slouken@12088
   349
slouken@12088
   350
    ctx = (SDL_DriverXbox360_Context *)SDL_calloc(1, sizeof(*ctx));
slouken@12088
   351
    if (!ctx) {
slouken@12088
   352
        SDL_OutOfMemory();
slouken@12088
   353
        return SDL_FALSE;
slouken@12088
   354
    }
slouken@13354
   355
slouken@13354
   356
    device->dev = hid_open_path(device->path, 0);
slouken@13354
   357
    if (!device->dev) {
slouken@13354
   358
        SDL_free(ctx);
slouken@13354
   359
        SDL_SetError("Couldn't open %s", device->path);
slouken@13354
   360
        return SDL_FALSE;
slouken@13354
   361
    }
slouken@13354
   362
    device->context = ctx;
slouken@13354
   363
slouken@12196
   364
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12118
   365
    ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE);
slouken@12118
   366
    if (ctx->xinput_enabled && WIN_LoadXInputDLL() < 0) {
slouken@12118
   367
        ctx->xinput_enabled = SDL_FALSE;
slouken@12118
   368
    }
slouken@12118
   369
    ctx->xinput_slot = XUSER_INDEX_ANY;
slouken@12118
   370
#endif
slouken@12196
   371
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
   372
    HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
slouken@12196
   373
#endif
slouken@12088
   374
slouken@12088
   375
    /* Set the controller LED */
slouken@13369
   376
    player_index = SDL_JoystickGetPlayerIndex(joystick);
slouken@13369
   377
    if (player_index >= 0) {
slouken@13369
   378
        SetSlotLED(device->dev, (player_index % 4));
slouken@13369
   379
    }
slouken@12088
   380
slouken@12088
   381
    /* Initialize the joystick capabilities */
slouken@12088
   382
    joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
slouken@12088
   383
    joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
slouken@12088
   384
    joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
slouken@12088
   385
slouken@12088
   386
    return SDL_TRUE;
slouken@12088
   387
}
slouken@12088
   388
slouken@12088
   389
static int
slouken@13480
   390
HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
zack@12787
   391
{
slouken@13480
   392
#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT)
slouken@13354
   393
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13480
   394
#endif
slouken@12196
   395
slouken@12118
   396
#ifdef __WIN32__
slouken@12196
   397
    SDL_bool rumbled = SDL_FALSE;
slouken@12196
   398
slouken@12196
   399
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
   400
    if (!rumbled && ctx->gamepad) {
slouken@12196
   401
        HRESULT hr;
slouken@12196
   402
slouken@12196
   403
        ctx->vibration.LeftMotor = (DOUBLE)low_frequency_rumble / SDL_MAX_UINT16;
slouken@12196
   404
        ctx->vibration.RightMotor = (DOUBLE)high_frequency_rumble / SDL_MAX_UINT16;
slouken@12196
   405
        hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(ctx->gamepad, ctx->vibration);
slouken@12196
   406
        if (SUCCEEDED(hr)) {
slouken@12196
   407
            rumbled = SDL_TRUE;
slouken@12196
   408
        }
slouken@12196
   409
    }
slouken@12196
   410
#endif
slouken@12196
   411
slouken@12196
   412
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12196
   413
    if (!rumbled && ctx->xinput_slot != XUSER_INDEX_ANY) {
slouken@12118
   414
        XINPUT_VIBRATION XVibration;
slouken@12118
   415
slouken@12118
   416
        if (!XINPUTSETSTATE) {
slouken@12118
   417
            return SDL_Unsupported();
slouken@12118
   418
        }
slouken@12118
   419
slouken@12118
   420
        XVibration.wLeftMotorSpeed = low_frequency_rumble;
slouken@12118
   421
        XVibration.wRightMotorSpeed = high_frequency_rumble;
slouken@12196
   422
        if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) == ERROR_SUCCESS) {
slouken@12196
   423
            rumbled = SDL_TRUE;
slouken@12196
   424
        } else {
slouken@12118
   425
            return SDL_SetError("XInputSetState() failed");
slouken@12118
   426
        }
slouken@12118
   427
    }
slouken@12196
   428
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
slouken@12196
   429
slouken@12196
   430
#else /* !__WIN32__ */
slouken@12196
   431
slouken@12088
   432
#ifdef __MACOSX__
slouken@13563
   433
    if (IsBluetoothXboxOneController(device->vendor_id, device->product_id)) {
slouken@13563
   434
        Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
slouken@13563
   435
slouken@13563
   436
        rumble_packet[4] = (low_frequency_rumble >> 8);
slouken@13563
   437
        rumble_packet[5] = (high_frequency_rumble >> 8);
slouken@12088
   438
slouken@13563
   439
        if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
slouken@13563
   440
            return SDL_SetError("Couldn't send rumble packet");
slouken@13563
   441
        }
slouken@13563
   442
    } else {
slouken@13563
   443
        /* On Mac OS X the 360Controller driver uses this short report,
slouken@13563
   444
           and we need to prefix it with a magic token so hidapi passes it through untouched
slouken@13563
   445
         */
slouken@13563
   446
        Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
slouken@13563
   447
slouken@13563
   448
        rumble_packet[6+2] = (low_frequency_rumble >> 8);
slouken@13563
   449
        rumble_packet[6+3] = (high_frequency_rumble >> 8);
slouken@13563
   450
slouken@13563
   451
        if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
slouken@13563
   452
            return SDL_SetError("Couldn't send rumble packet");
slouken@13563
   453
        }
slouken@13563
   454
    }
slouken@12088
   455
#else
slouken@12088
   456
    Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
slouken@12088
   457
slouken@12088
   458
    rumble_packet[3] = (low_frequency_rumble >> 8);
slouken@12088
   459
    rumble_packet[4] = (high_frequency_rumble >> 8);
slouken@12088
   460
slouken@13481
   461
    if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
slouken@12088
   462
        return SDL_SetError("Couldn't send rumble packet");
slouken@12088
   463
    }
slouken@13563
   464
#endif
slouken@12118
   465
#endif /* __WIN32__ */
slouken@12088
   466
slouken@12088
   467
    return 0;
slouken@12088
   468
}
slouken@12088
   469
slouken@12117
   470
#ifdef __WIN32__
slouken@12116
   471
 /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
slouken@12116
   472
    however with this interface there is no rumble support, no guide button,
slouken@12116
   473
    and the left and right triggers are tied together as a single axis.
slouken@12196
   474
slouken@12196
   475
    We use XInput and Windows.Gaming.Input to make up for these shortcomings.
slouken@12116
   476
  */
slouken@12116
   477
static void
slouken@12117
   478
HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
slouken@12116
   479
{
slouken@12116
   480
    Sint16 axis;
slouken@12196
   481
    SDL_bool has_trigger_data = SDL_FALSE;
slouken@12116
   482
slouken@12116
   483
    if (ctx->last_state[10] != data[10]) {
slouken@12116
   484
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   485
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[10] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   486
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[10] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   487
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[10] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   488
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[10] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   489
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[10] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   490
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[10] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   491
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[10] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   492
    }
slouken@12116
   493
slouken@12116
   494
    if (ctx->last_state[11] != data[11]) {
slouken@12116
   495
        SDL_bool dpad_up = SDL_FALSE;
slouken@12116
   496
        SDL_bool dpad_down = SDL_FALSE;
slouken@12116
   497
        SDL_bool dpad_left = SDL_FALSE;
slouken@12116
   498
        SDL_bool dpad_right = SDL_FALSE;
slouken@12116
   499
slouken@12116
   500
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[11] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   501
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[11] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12116
   502
slouken@12116
   503
        switch (data[11] & 0x3C) {
slouken@12116
   504
        case 4:
slouken@12116
   505
            dpad_up = SDL_TRUE;
slouken@12116
   506
            break;
slouken@12116
   507
        case 8:
slouken@12116
   508
            dpad_up = SDL_TRUE;
slouken@12116
   509
            dpad_right = SDL_TRUE;
slouken@12116
   510
            break;
slouken@12116
   511
        case 12:
slouken@12116
   512
            dpad_right = SDL_TRUE;
slouken@12116
   513
            break;
slouken@12116
   514
        case 16:
slouken@12116
   515
            dpad_right = SDL_TRUE;
slouken@12116
   516
            dpad_down = SDL_TRUE;
slouken@12116
   517
            break;
slouken@12116
   518
        case 20:
slouken@12116
   519
            dpad_down = SDL_TRUE;
slouken@12116
   520
            break;
slouken@12116
   521
        case 24:
slouken@12116
   522
            dpad_left = SDL_TRUE;
slouken@12116
   523
            dpad_down = SDL_TRUE;
slouken@12116
   524
            break;
slouken@12116
   525
        case 28:
slouken@12116
   526
            dpad_left = SDL_TRUE;
slouken@12116
   527
            break;
slouken@12116
   528
        case 32:
slouken@12116
   529
            dpad_up = SDL_TRUE;
slouken@12116
   530
            dpad_left = SDL_TRUE;
slouken@12116
   531
            break;
slouken@12116
   532
        default:
slouken@12116
   533
            break;
slouken@12116
   534
        }
slouken@12116
   535
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
slouken@12116
   536
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
slouken@12116
   537
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
slouken@12116
   538
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
slouken@12116
   539
    }
slouken@12116
   540
slouken@12116
   541
    axis = (int)*(Uint16*)(&data[0]) - 0x8000;
slouken@12116
   542
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
slouken@12116
   543
    axis = (int)*(Uint16*)(&data[2]) - 0x8000;
slouken@12116
   544
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
slouken@12116
   545
    axis = (int)*(Uint16*)(&data[4]) - 0x8000;
slouken@12116
   546
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
slouken@12116
   547
    axis = (int)*(Uint16*)(&data[6]) - 0x8000;
slouken@12116
   548
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
slouken@12116
   549
slouken@12196
   550
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@12196
   551
    if (ctx->gamepad_statics && !ctx->gamepad) {
slouken@12196
   552
        Uint8 buttons = 0;
slouken@12118
   553
slouken@12118
   554
        if (data[10] & 0x01) {
slouken@12196
   555
            buttons |= (1 << SDL_CONTROLLER_BUTTON_A);
slouken@12118
   556
        }
slouken@12118
   557
        if (data[10] & 0x02) {
slouken@12196
   558
            buttons |= (1 << SDL_CONTROLLER_BUTTON_B);
slouken@12118
   559
        }
slouken@12118
   560
        if (data[10] & 0x04) {
slouken@12196
   561
            buttons |= (1 << SDL_CONTROLLER_BUTTON_X);
slouken@12118
   562
        }
slouken@12118
   563
        if (data[10] & 0x08) {
slouken@12196
   564
            buttons |= (1 << SDL_CONTROLLER_BUTTON_Y);
slouken@12196
   565
        }
slouken@12196
   566
        if (buttons != 0) {
slouken@12196
   567
            HIDAPI_DriverXbox360_GuessGamepad(ctx, buttons);
slouken@12196
   568
        }
slouken@12196
   569
    }
slouken@12196
   570
slouken@12196
   571
    if (ctx->gamepad) {
slouken@12196
   572
        HRESULT hr;
slouken@12196
   573
        struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
slouken@12196
   574
        
slouken@12196
   575
        hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(ctx->gamepad, &state);
slouken@12196
   576
        if (SUCCEEDED(hr)) {
slouken@12196
   577
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (state.Buttons & 0x40000000) ? SDL_PRESSED : SDL_RELEASED);
slouken@12196
   578
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)(state.LeftTrigger * SDL_MAX_UINT16)) - 32768);
slouken@12196
   579
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)(state.RightTrigger * SDL_MAX_UINT16)) - 32768);
slouken@12196
   580
            has_trigger_data = SDL_TRUE;
slouken@12118
   581
        }
slouken@12196
   582
    }
slouken@12196
   583
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
slouken@12196
   584
slouken@12196
   585
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12196
   586
    if (ctx->xinput_enabled) {
slouken@12196
   587
        if (ctx->xinput_slot == XUSER_INDEX_ANY && HIDAPI_DriverXbox360_MissingXInputSlot()) {
slouken@12196
   588
            WORD wButtons = 0;
slouken@12196
   589
slouken@12196
   590
            if (data[10] & 0x01) {
slouken@12196
   591
                wButtons |= XINPUT_GAMEPAD_A;
slouken@12196
   592
            }
slouken@12196
   593
            if (data[10] & 0x02) {
slouken@12196
   594
                wButtons |= XINPUT_GAMEPAD_B;
slouken@12196
   595
            }
slouken@12196
   596
            if (data[10] & 0x04) {
slouken@12196
   597
                wButtons |= XINPUT_GAMEPAD_X;
slouken@12196
   598
            }
slouken@12196
   599
            if (data[10] & 0x08) {
slouken@12196
   600
                wButtons |= XINPUT_GAMEPAD_Y;
slouken@12196
   601
            }
slouken@12196
   602
            if (wButtons != 0) {
slouken@12196
   603
                Uint8 xinput_slot = HIDAPI_DriverXbox360_GuessXInputSlot(wButtons);
slouken@12196
   604
                if (xinput_slot != XUSER_INDEX_ANY) {
slouken@12196
   605
                    HIDAPI_DriverXbox360_MarkXInputSlotUsed(xinput_slot);
slouken@12196
   606
                    ctx->xinput_slot = xinput_slot;
slouken@12196
   607
                }
slouken@12196
   608
            }
slouken@12196
   609
        }
slouken@12196
   610
slouken@12196
   611
        if (!has_trigger_data && ctx->xinput_slot != XUSER_INDEX_ANY) {
slouken@12196
   612
            XINPUT_STATE_EX xinput_state;
slouken@12196
   613
slouken@12196
   614
            if (XINPUTGETSTATE(ctx->xinput_slot, &xinput_state) == ERROR_SUCCESS) {
slouken@12196
   615
                SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
slouken@12196
   616
                SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state.Gamepad.bLeftTrigger * 257) - 32768);
slouken@12196
   617
                SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state.Gamepad.bRightTrigger * 257) - 32768);
slouken@12196
   618
                has_trigger_data = SDL_TRUE;
slouken@12118
   619
            }
slouken@12118
   620
        }
slouken@12118
   621
    }
slouken@12196
   622
#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
slouken@12118
   623
slouken@12196
   624
    if (!has_trigger_data) {
slouken@12118
   625
        axis = (data[9] * 257) - 32768;
slouken@12118
   626
        if (data[9] < 0x80) {
slouken@12118
   627
            axis = -axis * 2 - 32769;
slouken@12118
   628
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
slouken@12118
   629
        } else if (data[9] > 0x80) {
slouken@12118
   630
            axis = axis * 2 - 32767;
slouken@12118
   631
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
slouken@12118
   632
        } else {
slouken@12118
   633
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
slouken@12118
   634
            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
slouken@12118
   635
        }
slouken@12116
   636
    }
slouken@12116
   637
slouken@12116
   638
    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
slouken@12116
   639
}
slouken@12117
   640
#else
slouken@12116
   641
slouken@12088
   642
static void
slouken@12088
   643
HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
slouken@12088
   644
{
slouken@12088
   645
    Sint16 axis;
slouken@12088
   646
#ifdef __MACOSX__
slouken@12118
   647
    const SDL_bool invert_y_axes = SDL_FALSE;
slouken@12088
   648
#else
slouken@12118
   649
    const SDL_bool invert_y_axes = SDL_TRUE;
slouken@12088
   650
#endif
slouken@12088
   651
slouken@12088
   652
    if (ctx->last_state[2] != data[2]) {
slouken@12088
   653
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   654
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   655
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   656
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   657
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   658
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   659
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   660
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   661
    }
slouken@12088
   662
slouken@12088
   663
    if (ctx->last_state[3] != data[3]) {
slouken@12088
   664
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   665
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   666
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   667
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   668
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   669
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   670
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   671
    }
slouken@12088
   672
slouken@12088
   673
    axis = ((int)data[4] * 257) - 32768;
slouken@12088
   674
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
slouken@12088
   675
    axis = ((int)data[5] * 257) - 32768;
slouken@12088
   676
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
slouken@12088
   677
    axis = *(Sint16*)(&data[6]);
slouken@12088
   678
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
slouken@12088
   679
    axis = *(Sint16*)(&data[8]);
slouken@12118
   680
    if (invert_y_axes) {
slouken@12118
   681
        axis = ~axis;
slouken@12118
   682
    }
slouken@12088
   683
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
slouken@12088
   684
    axis = *(Sint16*)(&data[10]);
slouken@12088
   685
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
slouken@12088
   686
    axis = *(Sint16*)(&data[12]);
slouken@12118
   687
    if (invert_y_axes) {
slouken@12118
   688
        axis = ~axis;
slouken@12118
   689
    }
slouken@12088
   690
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
slouken@12088
   691
slouken@12088
   692
    SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
slouken@12088
   693
}
slouken@12117
   694
#endif /* __WIN32__ */
slouken@12088
   695
slouken@12111
   696
static SDL_bool
slouken@13354
   697
HIDAPI_DriverXbox360_UpdateDevice(SDL_HIDAPI_Device *device)
slouken@12088
   698
{
slouken@13354
   699
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13354
   700
    SDL_Joystick *joystick = NULL;
slouken@12088
   701
    Uint8 data[USB_PACKET_LENGTH];
slouken@12088
   702
    int size;
slouken@12088
   703
slouken@13354
   704
    if (device->num_joysticks > 0) {
slouken@13354
   705
        joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
slouken@13354
   706
    }
slouken@13354
   707
    if (!joystick) {
slouken@13354
   708
        return SDL_FALSE;
slouken@13354
   709
    }
slouken@13354
   710
slouken@13354
   711
    while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
slouken@13354
   712
        HIDAPI_DriverXbox360_HandleStatePacket(joystick, device->dev, ctx, data, size);
slouken@12088
   713
    }
slouken@12088
   714
slouken@13354
   715
    if (size < 0) {
slouken@13354
   716
        /* Read error, device is disconnected */
slouken@13354
   717
        HIDAPI_JoystickDisconnected(device, joystick->instance_id);
slouken@13354
   718
    }
slouken@12118
   719
    return (size >= 0);
slouken@12088
   720
}
slouken@12088
   721
slouken@12088
   722
static void
slouken@13354
   723
HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
slouken@12088
   724
{
slouken@13480
   725
#if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
slouken@13354
   726
    SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
slouken@13480
   727
#endif
slouken@12118
   728
slouken@12196
   729
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
slouken@12118
   730
    if (ctx->xinput_enabled) {
slouken@12118
   731
        HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
slouken@12118
   732
        WIN_UnloadXInputDLL();
slouken@12118
   733
    }
slouken@12118
   734
#endif
slouken@12196
   735
#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
slouken@13354
   736
    HIDAPI_DriverXbox360_QuitWindowsGamingInput(ctx);
slouken@12196
   737
#endif
slouken@13354
   738
slouken@13354
   739
    hid_close(device->dev);
slouken@13354
   740
    device->dev = NULL;
slouken@13354
   741
slouken@13354
   742
    SDL_free(device->context);
slouken@13354
   743
    device->context = NULL;
slouken@13354
   744
}
slouken@13354
   745
slouken@13354
   746
static void
slouken@13354
   747
HIDAPI_DriverXbox360_FreeDevice(SDL_HIDAPI_Device *device)
slouken@13354
   748
{
slouken@12088
   749
}
slouken@12088
   750
slouken@12088
   751
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
slouken@12088
   752
{
slouken@12119
   753
    SDL_HINT_JOYSTICK_HIDAPI_XBOX,
slouken@12088
   754
    SDL_TRUE,
slouken@12088
   755
    HIDAPI_DriverXbox360_IsSupportedDevice,
slouken@12088
   756
    HIDAPI_DriverXbox360_GetDeviceName,
slouken@13354
   757
    HIDAPI_DriverXbox360_InitDevice,
slouken@13369
   758
    HIDAPI_DriverXbox360_GetDevicePlayerIndex,
slouken@13369
   759
    HIDAPI_DriverXbox360_SetDevicePlayerIndex,
slouken@13354
   760
    HIDAPI_DriverXbox360_UpdateDevice,
slouken@13354
   761
    HIDAPI_DriverXbox360_OpenJoystick,
slouken@13354
   762
    HIDAPI_DriverXbox360_RumbleJoystick,
slouken@13354
   763
    HIDAPI_DriverXbox360_CloseJoystick,
slouken@13354
   764
    HIDAPI_DriverXbox360_FreeDevice
slouken@12088
   765
};
slouken@12088
   766
slouken@12088
   767
#endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */
slouken@12088
   768
slouken@12088
   769
#endif /* SDL_JOYSTICK_HIDAPI */
slouken@12088
   770
slouken@12088
   771
/* vi: set ts=4 sw=4 expandtab: */