Fixed bug 4477 - Support more than 4 XInput-capable devices on Windows
authorSam Lantinga <slouken@libsdl.org>
Mon, 16 Mar 2020 12:25:02 -0700
changeset 136349a8e6854f652
parent 13633 340324c76848
child 13635 701fe6486077
Fixed bug 4477 - Support more than 4 XInput-capable devices on Windows

Jimb Esser

Add new RawInput controller API, and improved correlation with XInput/WGI

Reorder joystick init so drivers can ask the others if they handle a device reliably
Do not poll disconnected XInput devices (major perf issue)
Fix various cases where incorrect correlation could happen
Simple mechanism for propagating unhandled Guide button presses even before guaranteed correlation
Correlate by axis motion as well as button presses
Fix failing to zero other trigger
Fix SDL_HINT_JOYSTICK_HIDAPI not working if set before calling SDL_Init()
Add missing device to device names
Disable RawInput if we have a mismatch of XInput-capable but not RawInput-capable devices

Updated to SDL 2.0.13 code with the following notes:
New HID driver: xbox360w - no idea what that is, hopefully urelated
SDL_hidapijoystick.c had been refactored to couple data handling logic with device opening logic and device lists caused some problems, yields slightly uglier integration than previously when the 360 HID device driver was just handling the data.
SDL_hidapijoystick.c now often pulls the device off of the joystick_hwdata structure for some rumble logic, but it appears that code path is never reached, so probably not a problem.
Looks like joystick_hwdata was refactored to not include a mutex in other drivers, maintainers may want to do the same refactor here if that's useful for some reason.
Something changed in how devices get names, so getting generic names.
Had to fix a (new?) bug where removing an XInput controller caused existing controllers (that moved to a new XInput index) to get identified as 0x045e/0x02fd ("it's probably Bluetooth" in code), rendering the existing HIDAPI_IsDevicePresent and new RAWINPUT_IsDevicePresent unreliable.
src/joystick/windows/SDL_rawinputjoystick.c
src/joystick/windows/SDL_rawinputjoystick_c.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/windows/SDL_rawinputjoystick.c	Mon Mar 16 12:25:02 2020 -0700
     1.3 @@ -0,0 +1,718 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 2019 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +
    1.24 +*/
    1.25 +/*
    1.26 +  RAWINPUT Joystick API for better handling XInput-capable devices on Windows.
    1.27 +
    1.28 +  XInput is limited to 4 devices.
    1.29 +  Windows.Gaming.Input does not get inputs from XBox One controllers when not in the foreground.
    1.30 +  DirectInput does not get inputs from XBox One controllers when not in the foreground, nor rumble or accurate triggers.
    1.31 +  RawInput does not get rumble or accurate triggers.
    1.32 +
    1.33 +  So, combine them as best we can!
    1.34 +*/
    1.35 +#include "../../SDL_internal.h"
    1.36 +
    1.37 +#if SDL_JOYSTICK_RAWINPUT
    1.38 +
    1.39 +#include "SDL_assert.h"
    1.40 +#include "SDL_endian.h"
    1.41 +#include "SDL_hints.h"
    1.42 +#include "SDL_log.h"
    1.43 +#include "SDL_mutex.h"
    1.44 +#include "../SDL_sysjoystick.h"
    1.45 +#include "../../core/windows/SDL_windows.h"
    1.46 +#include "../hidapi/SDL_hidapijoystick_c.h"
    1.47 +
    1.48 +#ifndef SDL_JOYSTICK_HIDAPI_XBOX360
    1.49 +#error RAWINPUT requires the XBOX360 HIDAPI driver
    1.50 +#endif
    1.51 +
    1.52 +#ifndef RIDEV_EXINPUTSINK
    1.53 +#define RIDEV_EXINPUTSINK       0x00001000
    1.54 +#define RIDEV_DEVNOTIFY         0x00002000
    1.55 +#endif
    1.56 +
    1.57 +#ifndef WM_INPUT_DEVICE_CHANGE
    1.58 +#define WM_INPUT_DEVICE_CHANGE          0x00FE
    1.59 +#endif
    1.60 +#ifndef WM_INPUT
    1.61 +#define WM_INPUT                        0x00FF
    1.62 +#endif
    1.63 +#ifndef GIDC_ARRIVAL
    1.64 +#define GIDC_ARRIVAL             1
    1.65 +#define GIDC_REMOVAL             2
    1.66 +#endif
    1.67 +
    1.68 +
    1.69 +/* #define DEBUG_RAWINPUT */
    1.70 +
    1.71 +#define USB_PACKET_LENGTH   64
    1.72 +
    1.73 +#define SDL_callocStruct(type) (type *)SDL_calloc(1, sizeof(type))
    1.74 +#define SDL_callocStructs(type, count) (type *)SDL_calloc((count), sizeof(type))
    1.75 +
    1.76 +#define USAGE_PAGE_GENERIC_DESKTOP 0x0001
    1.77 +#define USAGE_JOYSTICK 0x0004
    1.78 +#define USAGE_GAMEPAD 0x0005
    1.79 +#define USAGE_MULTIAXISCONTROLLER 0x0008
    1.80 +
    1.81 +
    1.82 +/* external variables referenced. */
    1.83 +extern HWND SDL_HelperWindow;
    1.84 +
    1.85 +
    1.86 +static SDL_HIDAPI_DeviceDriver *SDL_RAWINPUT_drivers[] = {
    1.87 +#ifdef SDL_JOYSTICK_HIDAPI_XBOX360
    1.88 +    &SDL_HIDAPI_DriverXbox360,
    1.89 +#endif
    1.90 +};
    1.91 +
    1.92 +static SDL_bool SDL_RAWINPUT_inited = SDL_FALSE;
    1.93 +static int SDL_RAWINPUT_numjoysticks = 0;
    1.94 +static SDL_bool SDL_RAWINPUT_need_pump = SDL_TRUE;
    1.95 +
    1.96 +static void RAWINPUT_JoystickDetect(void);
    1.97 +static void RAWINPUT_PumpMessages(void);
    1.98 +static SDL_bool RAWINPUT_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version);
    1.99 +static void RAWINPUT_JoystickClose(SDL_Joystick * joystick);
   1.100 +
   1.101 +typedef struct _SDL_RAWINPUT_Device
   1.102 +{
   1.103 +    char *name;
   1.104 +    Uint16 vendor_id;
   1.105 +    Uint16 product_id;
   1.106 +    Uint16 version;
   1.107 +    SDL_JoystickGUID guid;
   1.108 +    Uint16 usage_page;
   1.109 +    Uint16 usage;
   1.110 +    SDL_HIDAPI_Device hiddevice;
   1.111 +    SDL_HIDAPI_DeviceDriver *driver;
   1.112 +
   1.113 +    HANDLE hDevice;
   1.114 +    SDL_Joystick *joystick;
   1.115 +    SDL_JoystickID joystick_id;
   1.116 +
   1.117 +    struct _SDL_RAWINPUT_Device *next;
   1.118 +} SDL_RAWINPUT_Device;
   1.119 +
   1.120 +struct joystick_hwdata
   1.121 +{
   1.122 +    void *reserved; /* reserving a value here to ensure the new SDL_hidapijoystick.c code never dereferences this */
   1.123 +    SDL_RAWINPUT_Device *device;
   1.124 +    SDL_mutex *mutex;
   1.125 +};
   1.126 +
   1.127 +SDL_RAWINPUT_Device *SDL_RAWINPUT_devices;
   1.128 +
   1.129 +static const Uint16 subscribed_devices[] = {
   1.130 +    USAGE_GAMEPAD,
   1.131 +    /* Don't need Joystick for any devices we're handling here (XInput-capable)
   1.132 +    USAGE_JOYSTICK,
   1.133 +    USAGE_MULTIAXISCONTROLLER,
   1.134 +    */
   1.135 +};
   1.136 +
   1.137 +SDL_bool RAWINPUT_AllXInputDevicesSupported() {
   1.138 +    UINT i, device_count = 0;
   1.139 +    PRAWINPUTDEVICELIST devices;
   1.140 +    SDL_bool any_unsupported = SDL_FALSE;
   1.141 +
   1.142 +    if ((GetRawInputDeviceList(NULL, &device_count, sizeof(RAWINPUTDEVICELIST)) == -1) || (!device_count)) {
   1.143 +        return SDL_FALSE;
   1.144 +    }
   1.145 +
   1.146 +    devices = (PRAWINPUTDEVICELIST)SDL_malloc(sizeof(RAWINPUTDEVICELIST) * device_count);
   1.147 +    if (devices == NULL) {
   1.148 +        return SDL_FALSE;
   1.149 +    }
   1.150 +
   1.151 +    if (GetRawInputDeviceList(devices, &device_count, sizeof(RAWINPUTDEVICELIST)) == -1) {
   1.152 +        SDL_free(devices);
   1.153 +        return SDL_FALSE;
   1.154 +    }
   1.155 +
   1.156 +    for (i = 0; i < device_count; i++) {
   1.157 +        RID_DEVICE_INFO rdi;
   1.158 +        char devName[128];
   1.159 +        UINT rdiSize = sizeof(rdi);
   1.160 +        UINT nameSize = SDL_arraysize(devName);
   1.161 +
   1.162 +        rdi.cbSize = sizeof(rdi);
   1.163 +        if ((devices[i].dwType == RIM_TYPEHID) &&
   1.164 +            (GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != ((UINT)-1)) &&
   1.165 +            (GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICENAME, devName, &nameSize) != ((UINT)-1)) &&
   1.166 +            (SDL_strstr(devName, "IG_") != NULL)
   1.167 +        ) {
   1.168 +            /* XInput-capable */
   1.169 +            if (!RAWINPUT_IsDeviceSupported((Uint16)rdi.hid.dwVendorId, (Uint16)rdi.hid.dwProductId, (Uint16)rdi.hid.dwVersionNumber)) {
   1.170 +                /* But not supported, probably Valve virtual controller */
   1.171 +                any_unsupported = SDL_TRUE;
   1.172 +            }
   1.173 +        }
   1.174 +    }
   1.175 +    SDL_free(devices);
   1.176 +    if (any_unsupported) {
   1.177 +        /* This happens with Valve virtual controllers that shows up in the RawInputDeviceList, but do not
   1.178 +            generate WM_INPUT events, so we must use XInput or DInput to read from it, and with XInput if we
   1.179 +            have some supported and some not, we can't easily tell which device is actually showing up in
   1.180 +            RawInput, so we must just disable RawInput for now.  Additionally, if these unsupported devices
   1.181 +            are locally connected, they still show up in RawInput under a *different* HID path, with
   1.182 +            different vendor/product IDs, so there's no way to reconcile. */
   1.183 +#ifdef DEBUG_RAWINPUT
   1.184 +        SDL_Log("Found some supported and some unsupported XInput devices, disabling RawInput\n");
   1.185 +#endif
   1.186 +        return SDL_FALSE;
   1.187 +    }
   1.188 +    return SDL_TRUE;
   1.189 +}
   1.190 +
   1.191 +static int
   1.192 +RAWINPUT_JoystickInit(void)
   1.193 +{
   1.194 +    int ii;
   1.195 +    RAWINPUTDEVICE rid[SDL_arraysize(subscribed_devices)];
   1.196 +    SDL_assert(!SDL_RAWINPUT_inited);
   1.197 +    SDL_assert(SDL_HelperWindow);
   1.198 +
   1.199 +    if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_RAWINPUT, SDL_TRUE))
   1.200 +        return -1;
   1.201 +
   1.202 +    if (!RAWINPUT_AllXInputDevicesSupported()) {
   1.203 +        return -1;
   1.204 +    }
   1.205 +
   1.206 +    for (ii = 0; ii < SDL_arraysize(subscribed_devices); ii++) {
   1.207 +        rid[ii].usUsagePage = USAGE_PAGE_GENERIC_DESKTOP;
   1.208 +        rid[ii].usUsage = subscribed_devices[ii];
   1.209 +        rid[ii].dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK; /* Receive messages when in background, including device add/remove */
   1.210 +        rid[ii].hwndTarget = SDL_HelperWindow;
   1.211 +    }
   1.212 +
   1.213 +    if (!RegisterRawInputDevices(rid, SDL_arraysize(rid), sizeof(RAWINPUTDEVICE))) {
   1.214 +        SDL_SetError("Couldn't initialize RAWINPUT");
   1.215 +        return -1;
   1.216 +    }
   1.217 +
   1.218 +    SDL_RAWINPUT_inited = SDL_TRUE;
   1.219 +
   1.220 +    RAWINPUT_JoystickDetect();
   1.221 +    RAWINPUT_PumpMessages();
   1.222 +    return 0;
   1.223 +}
   1.224 +
   1.225 +static int
   1.226 +RAWINPUT_JoystickGetCount(void)
   1.227 +{
   1.228 +    return SDL_RAWINPUT_numjoysticks;
   1.229 +}
   1.230 +
   1.231 +static SDL_RAWINPUT_Device *
   1.232 +RAWINPUT_DeviceFromHandle(HANDLE hDevice)
   1.233 +{
   1.234 +    SDL_RAWINPUT_Device *curr;
   1.235 +
   1.236 +    for (curr = SDL_RAWINPUT_devices; curr; curr = curr->next) {
   1.237 +        if (curr->hDevice == hDevice)
   1.238 +            return curr;
   1.239 +    }
   1.240 +    return NULL;
   1.241 +}
   1.242 +
   1.243 +static SDL_HIDAPI_DeviceDriver *
   1.244 +RAWINPUT_GetDeviceDriver(SDL_RAWINPUT_Device *device)
   1.245 +{
   1.246 +    int i;
   1.247 +
   1.248 +    if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
   1.249 +        return NULL;
   1.250 +    }
   1.251 +
   1.252 +    if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
   1.253 +        return NULL;
   1.254 +    }
   1.255 +    if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
   1.256 +        return NULL;
   1.257 +    }
   1.258 +
   1.259 +    SDL_GameControllerType type = SDL_GetJoystickGameControllerType("", device->vendor_id, device->product_id, -1, 0, 0, 0);
   1.260 +
   1.261 +    for (i = 0; i < SDL_arraysize(SDL_RAWINPUT_drivers); ++i) {
   1.262 +        SDL_HIDAPI_DeviceDriver *driver = SDL_RAWINPUT_drivers[i];
   1.263 +        if (/*driver->enabled && */driver->IsSupportedDevice(NULL, type, device->vendor_id, device->product_id, device->version, -1, 0, 0, 0)) {
   1.264 +            return driver;
   1.265 +        }
   1.266 +    }
   1.267 +    return NULL;
   1.268 +}
   1.269 +
   1.270 +static void
   1.271 +RAWINPUT_AddDevice(HANDLE hDevice)
   1.272 +{
   1.273 +#define CHECK(exp) { if(!(exp)) goto err; }
   1.274 +    SDL_RAWINPUT_Device *device = NULL;
   1.275 +    RID_DEVICE_INFO rdi;
   1.276 +    UINT rdi_size = sizeof(rdi);
   1.277 +    char dev_name[128];
   1.278 +    const char *name;
   1.279 +    UINT name_size = SDL_arraysize(dev_name);
   1.280 +    SDL_RAWINPUT_Device *curr, *last;
   1.281 +
   1.282 +    SDL_assert(!RAWINPUT_DeviceFromHandle(hDevice));
   1.283 +
   1.284 +    /* Figure out what kind of device it is */
   1.285 +    CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_DEVICEINFO, &rdi, &rdi_size) != (UINT)-1);
   1.286 +    CHECK(rdi.dwType == RIM_TYPEHID);
   1.287 +
   1.288 +    /* Get the device "name" (HID Path) */
   1.289 +    CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_DEVICENAME, dev_name, &name_size) != (UINT)-1);
   1.290 +    /* Only take XInput-capable devices */
   1.291 +    CHECK(SDL_strstr(dev_name, "IG_") != NULL);
   1.292 +
   1.293 +    CHECK(device = SDL_callocStruct(SDL_RAWINPUT_Device));
   1.294 +    device->hDevice = hDevice;
   1.295 +    device->vendor_id = (Uint16)rdi.hid.dwVendorId;
   1.296 +    device->product_id = (Uint16)rdi.hid.dwProductId;
   1.297 +    device->version = (Uint16)rdi.hid.dwVersionNumber;
   1.298 +    device->usage = rdi.hid.usUsage;
   1.299 +    device->usage_page = rdi.hid.usUsagePage;
   1.300 +
   1.301 +    {
   1.302 +        const Uint16 vendor = device->vendor_id;
   1.303 +        const Uint16 product = device->product_id;
   1.304 +        const Uint16 version = device->version;
   1.305 +        Uint16 *guid16 = (Uint16 *)device->guid.data;
   1.306 +
   1.307 +        *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
   1.308 +        *guid16++ = 0;
   1.309 +        *guid16++ = SDL_SwapLE16(vendor);
   1.310 +        *guid16++ = 0;
   1.311 +        *guid16++ = SDL_SwapLE16(product);
   1.312 +        *guid16++ = 0;
   1.313 +        *guid16++ = SDL_SwapLE16(version);
   1.314 +        *guid16++ = 0;
   1.315 +
   1.316 +        /* Note that this is a RAWINPUT device for special handling elsewhere */
   1.317 +        device->guid.data[14] = 'r';
   1.318 +        device->guid.data[15] = 0;
   1.319 +    }
   1.320 +
   1.321 +    if (!device->name) {
   1.322 +        size_t name_size = (6 + 1 + 6 + 1);
   1.323 +        CHECK(device->name = SDL_callocStructs(char, name_size));
   1.324 +        SDL_snprintf(device->name, name_size, "0x%.4x/0x%.4x", device->vendor_id, device->product_id);
   1.325 +    }
   1.326 +
   1.327 +    CHECK(device->driver = RAWINPUT_GetDeviceDriver(device));
   1.328 +
   1.329 +    name = device->driver->GetDeviceName(device->vendor_id, device->product_id);
   1.330 +    if (name) {
   1.331 +        SDL_free(device->name);
   1.332 +        device->name = SDL_strdup(name);
   1.333 +    }
   1.334 +
   1.335 +#ifdef DEBUG_RAWINPUT
   1.336 +    SDL_Log("Adding RAWINPUT device '%s' VID 0x%.4x, PID 0x%.4x, version %d, handle 0x%.8x\n", device->name, device->vendor_id, device->product_id, device->version, device->hDevice);
   1.337 +#endif
   1.338 +
   1.339 +    /* Add it to the list */
   1.340 +    for (curr = SDL_RAWINPUT_devices, last = NULL; curr; last = curr, curr = curr->next) {
   1.341 +        continue;
   1.342 +    }
   1.343 +    if (last) {
   1.344 +        last->next = device;
   1.345 +    } else {
   1.346 +        SDL_RAWINPUT_devices = device;
   1.347 +    }
   1.348 +
   1.349 +    ++SDL_RAWINPUT_numjoysticks;
   1.350 +    /* HIDAPI_JoystickConnected calls SDL_GetNextJoystickInstanceID() and SDL_PrivateJoystickAdded(), and calls back in to us, so
   1.351 +      the device list must be updated before calling this. */
   1.352 +    CHECK(HIDAPI_JoystickConnected(&device->hiddevice, &device->joystick_id, SDL_TRUE));
   1.353 +    /* Old: CHECK(device->driver->InitDevice(&device->hiddevice)); But, we need the joystick_id */
   1.354 +
   1.355 +    return;
   1.356 +
   1.357 +err:
   1.358 +    if (device) {
   1.359 +        if (device->name)
   1.360 +            SDL_free(device->name);
   1.361 +        SDL_free(device);
   1.362 +    }
   1.363 +}
   1.364 +
   1.365 +static void
   1.366 +RAWINPUT_DelDevice(SDL_RAWINPUT_Device *device, SDL_bool send_event)
   1.367 +{
   1.368 +    SDL_RAWINPUT_Device *curr, *last;
   1.369 +    for (curr = SDL_RAWINPUT_devices, last = NULL; curr; last = curr, curr = curr->next) {
   1.370 +        if (curr == device) {
   1.371 +            if (last) {
   1.372 +                last->next = curr->next;
   1.373 +            } else {
   1.374 +                SDL_RAWINPUT_devices = curr->next;
   1.375 +            }
   1.376 +            --SDL_RAWINPUT_numjoysticks;
   1.377 +
   1.378 +            SDL_Joystick *joystick = device->joystick;
   1.379 +            if (joystick) {
   1.380 +                /* Detach from joystick */
   1.381 +                RAWINPUT_JoystickClose(joystick);
   1.382 +            }
   1.383 +            /* Calls SDL_PrivateJoystickRemoved() */
   1.384 +            HIDAPI_JoystickDisconnected(&device->hiddevice, device->joystick_id, SDL_TRUE);
   1.385 +
   1.386 +#ifdef DEBUG_RAWINPUT
   1.387 +            SDL_Log("Removing RAWINPUT device '%s' VID 0x%.4x, PID 0x%.4x, version %d, handle 0x%.8x\n", device->name, device->vendor_id, device->product_id, device->version, device->hDevice);
   1.388 +#endif
   1.389 +            SDL_free(device->name);
   1.390 +            SDL_free(device);
   1.391 +            return;
   1.392 +        }
   1.393 +    }
   1.394 +}
   1.395 +
   1.396 +static void
   1.397 +RAWINPUT_PumpMessages(void)
   1.398 +{
   1.399 +    if (SDL_RAWINPUT_need_pump) {
   1.400 +        MSG msg;
   1.401 +        while (PeekMessage(&msg, SDL_HelperWindow, WM_INPUT, WM_INPUT, PM_REMOVE)) {
   1.402 +            TranslateMessage(&msg);
   1.403 +            DispatchMessage(&msg);
   1.404 +        }
   1.405 +        SDL_RAWINPUT_need_pump = SDL_FALSE;
   1.406 +    }
   1.407 +}
   1.408 +
   1.409 +static void
   1.410 +RAWINPUT_UpdateDeviceList(void)
   1.411 +{
   1.412 +    MSG msg;
   1.413 +    /* In theory, want only WM_INPUT_DEVICE_CHANGE messages here, but PeekMessage returns nothing unless you also ask
   1.414 +       for WM_INPUT */
   1.415 +    while (PeekMessage(&msg, SDL_HelperWindow, WM_INPUT_DEVICE_CHANGE, WM_INPUT, PM_REMOVE)) {
   1.416 +        TranslateMessage(&msg);
   1.417 +        DispatchMessage(&msg);
   1.418 +    }
   1.419 +}
   1.420 +
   1.421 +static SDL_bool
   1.422 +RAWINPUT_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version)
   1.423 +{
   1.424 +    int i;
   1.425 +
   1.426 +    SDL_GameControllerType type = SDL_GetJoystickGameControllerType("", vendor_id, product_id, -1, 0, 0, 0);
   1.427 +
   1.428 +    for (i = 0; i < SDL_arraysize(SDL_RAWINPUT_drivers); ++i) {
   1.429 +        SDL_HIDAPI_DeviceDriver *driver = SDL_RAWINPUT_drivers[i];
   1.430 +        /* Ignoring driver->enabled here, and elsewhere in this file, as the if the driver is enabled by disabling HID,
   1.431 +            we still want RawInput to use it.  If we end up with more than one RawInput driver, we may need to rework
   1.432 +            how the hints interact (separate enabled state, perhaps).
   1.433 +        */
   1.434 +        if (/*driver->enabled && */driver->IsSupportedDevice(NULL, type, vendor_id, product_id, version, -1, 0, 0, 0)) {
   1.435 +            return SDL_TRUE;
   1.436 +        }
   1.437 +    }
   1.438 +    return SDL_FALSE;
   1.439 +}
   1.440 +
   1.441 +SDL_bool
   1.442 +RAWINPUT_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version)
   1.443 +{
   1.444 +    SDL_RAWINPUT_Device *device;
   1.445 +
   1.446 +    /* Don't update the device list for devices we know aren't supported */
   1.447 +    if (!RAWINPUT_IsDeviceSupported(vendor_id, product_id, version)) {
   1.448 +        return SDL_FALSE;
   1.449 +    }
   1.450 +
   1.451 +    /* Make sure the device list is completely up to date when we check for device presence */
   1.452 +    RAWINPUT_UpdateDeviceList();
   1.453 +
   1.454 +    device = SDL_RAWINPUT_devices;
   1.455 +    while (device) {
   1.456 +        if (device->vendor_id == vendor_id && device->product_id == product_id) {
   1.457 +            return SDL_TRUE;
   1.458 +        }
   1.459 +        device = device->next;
   1.460 +    }
   1.461 +    return SDL_FALSE;
   1.462 +}
   1.463 +
   1.464 +static void
   1.465 +RAWINPUT_JoystickDetect(void)
   1.466 +{
   1.467 +    int i;
   1.468 +    /* Just ensure the window's add/remove messages have been pumped */
   1.469 +    RAWINPUT_UpdateDeviceList();
   1.470 +
   1.471 +    for (i = 0; i < SDL_arraysize(SDL_RAWINPUT_drivers); ++i) {
   1.472 +        SDL_HIDAPI_DeviceDriver *driver = SDL_RAWINPUT_drivers[i];
   1.473 +        /* Running PostUpdate here only if it's *not* enabled (and ran elsewhere) */
   1.474 +        if (!driver->enabled && driver->PostUpdate) {
   1.475 +            driver->PostUpdate();
   1.476 +        }
   1.477 +    }
   1.478 +    SDL_RAWINPUT_need_pump = SDL_TRUE;
   1.479 +}
   1.480 +
   1.481 +static SDL_RAWINPUT_Device *
   1.482 +RAWINPUT_GetJoystickByIndex(int device_index, SDL_JoystickID *pJoystickID)
   1.483 +{
   1.484 +    SDL_RAWINPUT_Device *device = SDL_RAWINPUT_devices;
   1.485 +    while (device) {
   1.486 +        if (device->driver) {
   1.487 +            SDL_assert(device->hiddevice.num_joysticks == 1);
   1.488 +            if (device_index < device->hiddevice.num_joysticks) {
   1.489 +                if (pJoystickID) {
   1.490 +                    *pJoystickID = device->hiddevice.joysticks[device_index];
   1.491 +                }
   1.492 +                return device;
   1.493 +            }
   1.494 +            device_index -= device->hiddevice.num_joysticks;
   1.495 +        }
   1.496 +        device = device->next;
   1.497 +    }
   1.498 +    return NULL;
   1.499 +}
   1.500 +
   1.501 +static const char *
   1.502 +RAWINPUT_JoystickGetDeviceName(int device_index)
   1.503 +{
   1.504 +    return RAWINPUT_GetJoystickByIndex(device_index, NULL)->name;
   1.505 +}
   1.506 +
   1.507 +static int
   1.508 +RAWINPUT_JoystickGetDevicePlayerIndex(int device_index)
   1.509 +{
   1.510 +    SDL_RAWINPUT_Device *device;
   1.511 +    SDL_JoystickID instance_id;
   1.512 +    int player_index = -1;
   1.513 +
   1.514 +    device = RAWINPUT_GetJoystickByIndex(device_index, &instance_id);
   1.515 +    if (device && device->driver) {
   1.516 +        player_index = device->driver->GetDevicePlayerIndex(&device->hiddevice, instance_id);
   1.517 +    }
   1.518 +
   1.519 +    return player_index;
   1.520 +}
   1.521 +
   1.522 +static void
   1.523 +RAWINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index)
   1.524 +{
   1.525 +    SDL_RAWINPUT_Device *device;
   1.526 +    SDL_JoystickID instance_id;
   1.527 +
   1.528 +    device = RAWINPUT_GetJoystickByIndex(device_index, &instance_id);
   1.529 +    if (device) {
   1.530 +        device->driver->SetDevicePlayerIndex(&device->hiddevice, instance_id, player_index);
   1.531 +    }
   1.532 +}
   1.533 +
   1.534 +
   1.535 +static SDL_JoystickGUID
   1.536 +RAWINPUT_JoystickGetDeviceGUID(int device_index)
   1.537 +{
   1.538 +    return RAWINPUT_GetJoystickByIndex(device_index, NULL)->guid;
   1.539 +}
   1.540 +
   1.541 +static SDL_JoystickID
   1.542 +RAWINPUT_JoystickGetDeviceInstanceID(int device_index)
   1.543 +{
   1.544 +    SDL_JoystickID instance_id = -1;
   1.545 +    RAWINPUT_GetJoystickByIndex(device_index, &instance_id);
   1.546 +    return instance_id;
   1.547 +}
   1.548 +
   1.549 +static int
   1.550 +RAWINPUT_JoystickOpen(SDL_Joystick * joystick, int device_index)
   1.551 +{
   1.552 +    SDL_RAWINPUT_Device *device = RAWINPUT_GetJoystickByIndex(device_index, NULL);
   1.553 +    struct joystick_hwdata *hwdata = SDL_callocStruct(struct joystick_hwdata);
   1.554 +    SDL_assert(!device->joystick);
   1.555 +
   1.556 +    if (!hwdata) {
   1.557 +        return SDL_OutOfMemory();
   1.558 +    }
   1.559 +
   1.560 +    if (!device->driver->OpenJoystick(&device->hiddevice, joystick)) {
   1.561 +        /* Only possible error is out of memory */
   1.562 +        return SDL_OutOfMemory();
   1.563 +    }
   1.564 +
   1.565 +    hwdata->reserved = (void*)-1; /* crash if some code slips by that tries to use this */
   1.566 +    hwdata->device = device;
   1.567 +    device->joystick = joystick;
   1.568 +
   1.569 +    hwdata->mutex = SDL_CreateMutex();
   1.570 +    joystick->hwdata = hwdata;
   1.571 +
   1.572 +    return 0;
   1.573 +}
   1.574 +
   1.575 +static int
   1.576 +RAWINPUT_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
   1.577 +{
   1.578 +    struct joystick_hwdata *hwdata = joystick->hwdata;
   1.579 +    SDL_RAWINPUT_Device *device = hwdata->device;
   1.580 +    int result;
   1.581 +
   1.582 +    SDL_LockMutex(hwdata->mutex);
   1.583 +    result = device->driver->RumbleJoystick(&device->hiddevice, joystick, low_frequency_rumble, high_frequency_rumble);
   1.584 +    SDL_UnlockMutex(hwdata->mutex);
   1.585 +    return result;
   1.586 +}
   1.587 +
   1.588 +static void
   1.589 +RAWINPUT_JoystickUpdate(SDL_Joystick * joystick)
   1.590 +{
   1.591 +    struct joystick_hwdata *hwdata;
   1.592 +    SDL_RAWINPUT_Device *device;
   1.593 +    /* Ensure data messages have been pumped */
   1.594 +    RAWINPUT_PumpMessages();
   1.595 +    hwdata = joystick->hwdata;
   1.596 +    device = hwdata->device;
   1.597 +
   1.598 +    SDL_LockMutex(hwdata->mutex);
   1.599 +    device->driver->UpdateDevice(&device->hiddevice);
   1.600 +    SDL_UnlockMutex(hwdata->mutex);
   1.601 +}
   1.602 +
   1.603 +static void
   1.604 +RAWINPUT_JoystickClose(SDL_Joystick * joystick)
   1.605 +{
   1.606 +    struct joystick_hwdata *hwdata = joystick->hwdata;
   1.607 +
   1.608 +    if (hwdata) {
   1.609 +        SDL_RAWINPUT_Device *device;
   1.610 +
   1.611 +        device = hwdata->device;
   1.612 +        if (device) {
   1.613 +            SDL_assert(device->joystick == joystick);
   1.614 +            device->driver->CloseJoystick(&device->hiddevice, joystick);
   1.615 +            device->joystick = NULL;
   1.616 +        }
   1.617 +
   1.618 +        SDL_DestroyMutex(hwdata->mutex);
   1.619 +        SDL_free(hwdata);
   1.620 +        joystick->hwdata = NULL;
   1.621 +    }
   1.622 +}
   1.623 +
   1.624 +LRESULT RAWINPUT_WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
   1.625 +{
   1.626 +    if (!SDL_RAWINPUT_inited)
   1.627 +        return -1;
   1.628 +
   1.629 +    switch (msg)
   1.630 +    {
   1.631 +        case WM_INPUT_DEVICE_CHANGE:
   1.632 +        {
   1.633 +            HANDLE hDevice = (HANDLE)lParam;
   1.634 +            switch (wParam) {
   1.635 +            case GIDC_ARRIVAL:
   1.636 +                RAWINPUT_AddDevice(hDevice);
   1.637 +                break;
   1.638 +            case GIDC_REMOVAL: {
   1.639 +                SDL_RAWINPUT_Device *device;
   1.640 +                device = RAWINPUT_DeviceFromHandle(hDevice);
   1.641 +                if (device) {
   1.642 +                    RAWINPUT_DelDevice(device, SDL_TRUE);
   1.643 +                }
   1.644 +            } break;
   1.645 +            default:
   1.646 +                return 0;
   1.647 +            }
   1.648 +        }
   1.649 +        return 0;
   1.650 +        case WM_INPUT:
   1.651 +        {
   1.652 +            Uint8 data[sizeof(RAWINPUTHEADER) + sizeof(RAWHID) + USB_PACKET_LENGTH];
   1.653 +            UINT buffer_size = SDL_arraysize(data);
   1.654 +
   1.655 +            if ((int)GetRawInputData((HRAWINPUT)lParam, RID_INPUT, data, &buffer_size, sizeof(RAWINPUTHEADER)) > 0) {
   1.656 +                PRAWINPUT raw_input = (PRAWINPUT)data;
   1.657 +                SDL_RAWINPUT_Device *device = RAWINPUT_DeviceFromHandle(raw_input->header.hDevice);
   1.658 +                if (device) {
   1.659 +                    SDL_HIDAPI_DeviceDriver *driver = device->driver;
   1.660 +                    SDL_Joystick *joystick = device->joystick;
   1.661 +                    if (joystick) {
   1.662 +                        driver->HandleStatePacketFromRAWINPUT(&device->hiddevice, joystick, &raw_input->data.hid.bRawData[1], raw_input->data.hid.dwSizeHid - 1);
   1.663 +                    }
   1.664 +                }
   1.665 +            }
   1.666 +        }
   1.667 +        return 0;
   1.668 +    }
   1.669 +    return -1;
   1.670 +}
   1.671 +
   1.672 +static void
   1.673 +RAWINPUT_JoystickQuit(void)
   1.674 +{
   1.675 +    int ii;
   1.676 +    RAWINPUTDEVICE rid[SDL_arraysize(subscribed_devices)];
   1.677 +    
   1.678 +    if (!SDL_RAWINPUT_inited)
   1.679 +        return;
   1.680 +
   1.681 +    for (ii = 0; ii < SDL_arraysize(subscribed_devices); ii++) {
   1.682 +        rid[ii].usUsagePage = USAGE_PAGE_GENERIC_DESKTOP;
   1.683 +        rid[ii].usUsage = subscribed_devices[ii];
   1.684 +        rid[ii].dwFlags = RIDEV_REMOVE;
   1.685 +        rid[ii].hwndTarget = NULL;
   1.686 +    }
   1.687 +
   1.688 +    if (!RegisterRawInputDevices(rid, SDL_arraysize(rid), sizeof(RAWINPUTDEVICE))) {
   1.689 +        SDL_Log("Couldn't un-register RAWINPUT");
   1.690 +    }
   1.691 +    
   1.692 +    while (SDL_RAWINPUT_devices) {
   1.693 +        RAWINPUT_DelDevice(SDL_RAWINPUT_devices, SDL_FALSE);
   1.694 +    }
   1.695 +
   1.696 +    SDL_RAWINPUT_numjoysticks = 0;
   1.697 +
   1.698 +    SDL_RAWINPUT_inited = SDL_FALSE;
   1.699 +}
   1.700 +
   1.701 +
   1.702 +SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver =
   1.703 +{
   1.704 +    RAWINPUT_JoystickInit,
   1.705 +    RAWINPUT_JoystickGetCount,
   1.706 +    RAWINPUT_JoystickDetect,
   1.707 +    RAWINPUT_JoystickGetDeviceName,
   1.708 +    RAWINPUT_JoystickGetDevicePlayerIndex,
   1.709 +    RAWINPUT_JoystickSetDevicePlayerIndex,
   1.710 +    RAWINPUT_JoystickGetDeviceGUID,
   1.711 +    RAWINPUT_JoystickGetDeviceInstanceID,
   1.712 +    RAWINPUT_JoystickOpen,
   1.713 +    RAWINPUT_JoystickRumble,
   1.714 +    RAWINPUT_JoystickUpdate,
   1.715 +    RAWINPUT_JoystickClose,
   1.716 +    RAWINPUT_JoystickQuit,
   1.717 +};
   1.718 +
   1.719 +#endif /* SDL_JOYSTICK_RAWINPUT */
   1.720 +
   1.721 +/* vi: set ts=4 sw=4 expandtab: */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/joystick/windows/SDL_rawinputjoystick_c.h	Mon Mar 16 12:25:02 2020 -0700
     2.3 @@ -0,0 +1,30 @@
     2.4 +/*
     2.5 +  Simple DirectMedia Layer
     2.6 +  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 +  This software is provided 'as-is', without any express or implied
     2.9 +  warranty.  In no event will the authors be held liable for any damages
    2.10 +  arising from the use of this software.
    2.11 +
    2.12 +  Permission is granted to anyone to use this software for any purpose,
    2.13 +  including commercial applications, and to alter it and redistribute it
    2.14 +  freely, subject to the following restrictions:
    2.15 +
    2.16 +  1. The origin of this software must not be misrepresented; you must not
    2.17 +     claim that you wrote the original software. If you use this software
    2.18 +     in a product, an acknowledgment in the product documentation would be
    2.19 +     appreciated but is not required.
    2.20 +  2. Altered source versions must be plainly marked as such, and must not be
    2.21 +     misrepresented as being the original software.
    2.22 +  3. This notice may not be removed or altered from any source distribution.
    2.23 +*/
    2.24 +#include "../../SDL_internal.h"
    2.25 +
    2.26 +/* Return true if a RawInput device is present and supported as a joystick */
    2.27 +extern SDL_bool RAWINPUT_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version);
    2.28 +
    2.29 +/* Returns 0 if message was handled */
    2.30 +extern LRESULT RAWINPUT_WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
    2.31 +
    2.32 +
    2.33 +/* vi: set ts=4 sw=4 expandtab: */