src/joystick/hidapi/SDL_hidapi_xbox360.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Tue, 12 Mar 2019 20:27:54 -0400
changeset 12641 64597a7e8771
parent 12503 806492103856
child 12769 096f2f3c4bdc
permissions -rw-r--r--
hidapi: Add support for Wii U/Switch USB GameCube controller adapter.

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