src/joystick/hidapi/SDL_hidapi_xbox360.c
author Sam Lantinga
Mon, 16 Dec 2019 10:20:03 -0800
changeset 13348 448528dc13da
parent 13337 9cbbe45952a4
child 13350 a9ce6aa098b6
permissions -rw-r--r--
Fixed bug 4898 - No rumble because of integer overflow in SDL_JoystickRumble

meyraud705

On a Dualshock 4 controller using hidapi driver, calling SDL_JoystickRumble with a duration too long (SDL_HAPTIC_INFINITY for example) causes the rumble to stop immediately.

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