Path: blob/master/thirdparty/sdl/joystick/windows/SDL_windowsjoystick.c
9905 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122#if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)2324/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de25* A. Formiga's WINMM driver.26*27* Hats and sliders are completely untested; the app I'm writing this for mostly28* doesn't use them and I don't own any joysticks with them.29*30* We don't bother to use event notification here. It doesn't seem to work31* with polled devices, and it's fine to call IDirectInputDevice8_GetDeviceData and32* let it return 0 events. */3334#include "../SDL_sysjoystick.h"35#include "../../thread/SDL_systhread.h"36#include "../../core/windows/SDL_windows.h"37#include "../../core/windows/SDL_hid.h"38#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)39#include <dbt.h>40#endif4142#define INITGUID // Only set here, if set twice will cause mingw32 to break.43#include "SDL_windowsjoystick_c.h"44#include "SDL_dinputjoystick_c.h"45#include "SDL_xinputjoystick_c.h"46#include "SDL_rawinputjoystick_c.h"4748#include "../../haptic/windows/SDL_dinputhaptic_c.h" // For haptic hot plugging4950#ifndef DEVICE_NOTIFY_WINDOW_HANDLE51#define DEVICE_NOTIFY_WINDOW_HANDLE 0x0000000052#endif5354// local variables55static bool s_bJoystickThread = false;56static SDL_Condition *s_condJoystickThread = NULL;57static SDL_Mutex *s_mutexJoyStickEnum = NULL;58static SDL_Thread *s_joystickThread = NULL;59static bool s_bJoystickThreadQuit = false;60static Uint64 s_lastDeviceChange = 0;61static GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };6263JoyStick_DeviceData *SYS_Joystick; // array to hold joystick ID values646566static bool WindowsDeviceChanged(void)67{68return (s_lastDeviceChange != WIN_GetLastDeviceNotification());69}7071static void SetWindowsDeviceChanged(void)72{73s_lastDeviceChange = 0;74}7576void WINDOWS_RAWINPUTEnabledChanged(void)77{78SetWindowsDeviceChanged();79}8081#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)8283typedef struct84{85HRESULT coinitialized;86WNDCLASSEX wincl;87HWND messageWindow;88HDEVNOTIFY hNotify;89} SDL_DeviceNotificationData;9091#define IDT_SDL_DEVICE_CHANGE_TIMER_1 120092#define IDT_SDL_DEVICE_CHANGE_TIMER_2 12019394// windowproc for our joystick detect thread message only window, to detect any USB device addition/removal95static LRESULT CALLBACK SDL_PrivateJoystickDetectProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)96{97switch (msg) {98case WM_DEVICECHANGE:99switch (wParam) {100case DBT_DEVICEARRIVAL:101case DBT_DEVICEREMOVECOMPLETE:102if (((DEV_BROADCAST_HDR *)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {103// notify 300ms and 2 seconds later to ensure all APIs have updated status104SetTimer(hwnd, IDT_SDL_DEVICE_CHANGE_TIMER_1, 300, NULL);105SetTimer(hwnd, IDT_SDL_DEVICE_CHANGE_TIMER_2, 2000, NULL);106}107break;108}109return true;110case WM_TIMER:111if (wParam == IDT_SDL_DEVICE_CHANGE_TIMER_1 ||112wParam == IDT_SDL_DEVICE_CHANGE_TIMER_2) {113KillTimer(hwnd, wParam);114SetWindowsDeviceChanged();115return true;116}117break;118}119120#ifdef SDL_JOYSTICK_RAWINPUT121return CallWindowProc(RAWINPUT_WindowProc, hwnd, msg, wParam, lParam);122#else123return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);124#endif125}126127static void SDL_CleanupDeviceNotification(SDL_DeviceNotificationData *data)128{129#ifdef SDL_JOYSTICK_RAWINPUT130RAWINPUT_UnregisterNotifications();131#endif132133if (data->hNotify) {134UnregisterDeviceNotification(data->hNotify);135}136137if (data->messageWindow) {138DestroyWindow(data->messageWindow);139}140141UnregisterClass(data->wincl.lpszClassName, data->wincl.hInstance);142143if (data->coinitialized == S_OK) {144WIN_CoUninitialize();145}146}147148static bool SDL_CreateDeviceNotification(SDL_DeviceNotificationData *data)149{150DEV_BROADCAST_DEVICEINTERFACE dbh;151152SDL_zerop(data);153154data->coinitialized = WIN_CoInitialize();155156data->wincl.hInstance = GetModuleHandle(NULL);157data->wincl.lpszClassName = TEXT("Message");158data->wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc; // This function is called by windows159data->wincl.cbSize = sizeof(WNDCLASSEX);160161if (!RegisterClassEx(&data->wincl)) {162WIN_SetError("Failed to create register class for joystick autodetect");163SDL_CleanupDeviceNotification(data);164return false;165}166167data->messageWindow = CreateWindowEx(0, TEXT("Message"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);168if (!data->messageWindow) {169WIN_SetError("Failed to create message window for joystick autodetect");170SDL_CleanupDeviceNotification(data);171return false;172}173174SDL_zero(dbh);175dbh.dbcc_size = sizeof(dbh);176dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;177dbh.dbcc_classguid = GUID_DEVINTERFACE_HID;178179data->hNotify = RegisterDeviceNotification(data->messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE);180if (!data->hNotify) {181WIN_SetError("Failed to create notify device for joystick autodetect");182SDL_CleanupDeviceNotification(data);183return false;184}185186#ifdef SDL_JOYSTICK_RAWINPUT187RAWINPUT_RegisterNotifications(data->messageWindow);188#endif189return true;190}191192static bool SDL_WaitForDeviceNotification(SDL_DeviceNotificationData *data, SDL_Mutex *mutex)193{194MSG msg;195int lastret = 1;196197if (!data->messageWindow) {198return false; // device notifications require a window199}200201SDL_UnlockMutex(mutex);202while (lastret > 0 && !WindowsDeviceChanged()) {203lastret = GetMessage(&msg, NULL, 0, 0); // WM_QUIT causes return value of 0204if (lastret > 0) {205TranslateMessage(&msg);206DispatchMessage(&msg);207}208}209SDL_LockMutex(mutex);210return (lastret != -1);211}212213#endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)214215#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)216static SDL_DeviceNotificationData s_notification_data;217#endif218219// Function/thread to scan the system for joysticks.220static int SDLCALL SDL_JoystickThread(void *_data)221{222#ifdef SDL_JOYSTICK_XINPUT223bool bOpenedXInputDevices[XUSER_MAX_COUNT];224SDL_zeroa(bOpenedXInputDevices);225#endif226227#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)228if (!SDL_CreateDeviceNotification(&s_notification_data)) {229return 0;230}231#endif232233SDL_LockMutex(s_mutexJoyStickEnum);234while (s_bJoystickThreadQuit == false) {235#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)236if (SDL_WaitForDeviceNotification(&s_notification_data, s_mutexJoyStickEnum) == false) {237#else238{239#endif240#ifdef SDL_JOYSTICK_XINPUT241// WM_DEVICECHANGE not working, poll for new XINPUT controllers242SDL_WaitConditionTimeout(s_condJoystickThread, s_mutexJoyStickEnum, 1000);243if (SDL_XINPUT_Enabled()) {244// scan for any change in XInput devices245Uint8 userId;246for (userId = 0; userId < XUSER_MAX_COUNT; userId++) {247XINPUT_CAPABILITIES capabilities;248const DWORD result = XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities);249const bool available = (result == ERROR_SUCCESS);250if (bOpenedXInputDevices[userId] != available) {251SetWindowsDeviceChanged();252bOpenedXInputDevices[userId] = available;253}254}255}256#else257// WM_DEVICECHANGE not working, no XINPUT, no point in keeping thread alive258break;259#endif // SDL_JOYSTICK_XINPUT260}261}262263SDL_UnlockMutex(s_mutexJoyStickEnum);264265#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)266SDL_CleanupDeviceNotification(&s_notification_data);267#endif268269return 1;270}271272// spin up the thread to detect hotplug of devices273static bool SDL_StartJoystickThread(void)274{275s_mutexJoyStickEnum = SDL_CreateMutex();276if (!s_mutexJoyStickEnum) {277return false;278}279280s_condJoystickThread = SDL_CreateCondition();281if (!s_condJoystickThread) {282return false;283}284285s_bJoystickThreadQuit = false;286s_joystickThread = SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL);287if (!s_joystickThread) {288return false;289}290return true;291}292293static void SDL_StopJoystickThread(void)294{295if (!s_joystickThread) {296return;297}298299SDL_LockMutex(s_mutexJoyStickEnum);300s_bJoystickThreadQuit = true;301SDL_BroadcastCondition(s_condJoystickThread); // signal the joystick thread to quit302SDL_UnlockMutex(s_mutexJoyStickEnum);303PostThreadMessage((DWORD)SDL_GetThreadID(s_joystickThread), WM_QUIT, 0, 0);304305// Unlock joysticks while the joystick thread finishes processing messages306SDL_AssertJoysticksLocked();307SDL_UnlockJoysticks();308SDL_WaitThread(s_joystickThread, NULL); // wait for it to bugger off309SDL_LockJoysticks();310311SDL_DestroyCondition(s_condJoystickThread);312s_condJoystickThread = NULL;313314SDL_DestroyMutex(s_mutexJoyStickEnum);315s_mutexJoyStickEnum = NULL;316317s_joystickThread = NULL;318}319320void WINDOWS_AddJoystickDevice(JoyStick_DeviceData *device)321{322device->send_add_event = true;323device->nInstanceID = SDL_GetNextObjectID();324device->pNext = SYS_Joystick;325SYS_Joystick = device;326}327328void WINDOWS_JoystickDetect(void);329void WINDOWS_JoystickQuit(void);330331static bool WINDOWS_JoystickInit(void)332{333if (!SDL_XINPUT_JoystickInit()) {334WINDOWS_JoystickQuit();335return false;336}337338if (!SDL_DINPUT_JoystickInit()) {339WINDOWS_JoystickQuit();340return false;341}342343WIN_InitDeviceNotification();344345#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)346s_bJoystickThread = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_THREAD, true);347if (s_bJoystickThread) {348if (!SDL_StartJoystickThread()) {349return false;350}351} else {352if (!SDL_CreateDeviceNotification(&s_notification_data)) {353return false;354}355}356#endif357358#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)359// On Xbox, force create the joystick thread for device detection (since other methods don't work360s_bJoystickThread = true;361if (!SDL_StartJoystickThread()) {362return false;363}364#endif365366SetWindowsDeviceChanged(); // force a scan of the system for joysticks this first time367368WINDOWS_JoystickDetect();369370return true;371}372373// return the number of joysticks that are connected right now374static int WINDOWS_JoystickGetCount(void)375{376int nJoysticks = 0;377JoyStick_DeviceData *device = SYS_Joystick;378while (device) {379nJoysticks++;380device = device->pNext;381}382383return nJoysticks;384}385386// detect any new joysticks being inserted into the system387void WINDOWS_JoystickDetect(void)388{389JoyStick_DeviceData *pCurList = NULL;390391// only enum the devices if the joystick thread told us something changed392if (!WindowsDeviceChanged()) {393return; // thread hasn't signaled, nothing to do right now.394}395396if (s_mutexJoyStickEnum) {397SDL_LockMutex(s_mutexJoyStickEnum);398}399400s_lastDeviceChange = WIN_GetLastDeviceNotification();401402pCurList = SYS_Joystick;403SYS_Joystick = NULL;404405// Look for DirectInput joysticks, wheels, head trackers, gamepads, etc..406SDL_DINPUT_JoystickDetect(&pCurList);407408// Look for XInput devices. Do this last, so they're first in the final list.409SDL_XINPUT_JoystickDetect(&pCurList);410411if (s_mutexJoyStickEnum) {412SDL_UnlockMutex(s_mutexJoyStickEnum);413}414415while (pCurList) {416JoyStick_DeviceData *pListNext = NULL;417418if (!pCurList->bXInputDevice) {419#ifdef SDL_HAPTIC_DINPUT420SDL_DINPUT_HapticMaybeRemoveDevice(&pCurList->dxdevice);421#endif422}423424SDL_PrivateJoystickRemoved(pCurList->nInstanceID);425426pListNext = pCurList->pNext;427SDL_free(pCurList->joystickname);428SDL_free(pCurList);429pCurList = pListNext;430}431432for (pCurList = SYS_Joystick; pCurList; pCurList = pCurList->pNext) {433if (pCurList->send_add_event) {434if (!pCurList->bXInputDevice) {435#ifdef SDL_HAPTIC_DINPUT436SDL_DINPUT_HapticMaybeAddDevice(&pCurList->dxdevice);437#endif438}439440SDL_PrivateJoystickAdded(pCurList->nInstanceID);441442pCurList->send_add_event = false;443}444}445}446447static bool WINDOWS_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)448{449if (SDL_DINPUT_JoystickPresent(vendor_id, product_id, version)) {450return true;451}452if (SDL_XINPUT_JoystickPresent(vendor_id, product_id, version)) {453return true;454}455return false;456}457458static const char *WINDOWS_JoystickGetDeviceName(int device_index)459{460JoyStick_DeviceData *device = SYS_Joystick;461int index;462463for (index = device_index; index > 0; index--) {464device = device->pNext;465}466467return device->joystickname;468}469470static const char *WINDOWS_JoystickGetDevicePath(int device_index)471{472JoyStick_DeviceData *device = SYS_Joystick;473int index;474475for (index = device_index; index > 0; index--) {476device = device->pNext;477}478479return device->path;480}481482static int WINDOWS_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)483{484JoyStick_DeviceData *device = SYS_Joystick;485int index;486487for (index = device_index; index > 0; index--) {488device = device->pNext;489}490491if (device->bXInputDevice) {492// The slot for XInput devices can change as controllers are seated493return SDL_XINPUT_GetSteamVirtualGamepadSlot(device->XInputUserId);494} else {495return device->steam_virtual_gamepad_slot;496}497}498499static int WINDOWS_JoystickGetDevicePlayerIndex(int device_index)500{501JoyStick_DeviceData *device = SYS_Joystick;502int index;503504for (index = device_index; index > 0; index--) {505device = device->pNext;506}507508return device->bXInputDevice ? (int)device->XInputUserId : -1;509}510511static void WINDOWS_JoystickSetDevicePlayerIndex(int device_index, int player_index)512{513}514515// return the stable device guid for this device index516static SDL_GUID WINDOWS_JoystickGetDeviceGUID(int device_index)517{518JoyStick_DeviceData *device = SYS_Joystick;519int index;520521for (index = device_index; index > 0; index--) {522device = device->pNext;523}524525return device->guid;526}527528// Function to perform the mapping between current device instance and this joysticks instance id529static SDL_JoystickID WINDOWS_JoystickGetDeviceInstanceID(int device_index)530{531JoyStick_DeviceData *device = SYS_Joystick;532int index;533534for (index = device_index; index > 0; index--) {535device = device->pNext;536}537538return device->nInstanceID;539}540541/* Function to open a joystick for use.542The joystick to open is specified by the device index.543This should fill the nbuttons and naxes fields of the joystick structure.544It returns 0, or -1 if there is an error.545*/546static bool WINDOWS_JoystickOpen(SDL_Joystick *joystick, int device_index)547{548JoyStick_DeviceData *device = SYS_Joystick;549int index;550551for (index = device_index; index > 0; index--) {552device = device->pNext;553}554555// allocate memory for system specific hardware data556joystick->hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(struct joystick_hwdata));557if (!joystick->hwdata) {558return false;559}560joystick->hwdata->guid = device->guid;561562if (device->bXInputDevice) {563return SDL_XINPUT_JoystickOpen(joystick, device);564} else {565return SDL_DINPUT_JoystickOpen(joystick, device);566}567}568569static bool WINDOWS_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)570{571if (joystick->hwdata->bXInputDevice) {572return SDL_XINPUT_JoystickRumble(joystick, low_frequency_rumble, high_frequency_rumble);573} else {574return SDL_DINPUT_JoystickRumble(joystick, low_frequency_rumble, high_frequency_rumble);575}576}577578static bool WINDOWS_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)579{580return SDL_Unsupported();581}582583static bool WINDOWS_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)584{585return SDL_Unsupported();586}587588static bool WINDOWS_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)589{590return SDL_Unsupported();591}592593static bool WINDOWS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled)594{595return SDL_Unsupported();596}597598static void WINDOWS_JoystickUpdate(SDL_Joystick *joystick)599{600if (!joystick->hwdata) {601return;602}603604if (joystick->hwdata->bXInputDevice) {605SDL_XINPUT_JoystickUpdate(joystick);606} else {607SDL_DINPUT_JoystickUpdate(joystick);608}609}610611// Function to close a joystick after use612static void WINDOWS_JoystickClose(SDL_Joystick *joystick)613{614if (joystick->hwdata->bXInputDevice) {615SDL_XINPUT_JoystickClose(joystick);616} else {617SDL_DINPUT_JoystickClose(joystick);618}619620SDL_free(joystick->hwdata);621}622623// Function to perform any system-specific joystick related cleanup624void WINDOWS_JoystickQuit(void)625{626JoyStick_DeviceData *device = SYS_Joystick;627628while (device) {629JoyStick_DeviceData *device_next = device->pNext;630SDL_free(device->joystickname);631SDL_free(device);632device = device_next;633}634SYS_Joystick = NULL;635636#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)637if (s_bJoystickThread) {638SDL_StopJoystickThread();639} else {640SDL_CleanupDeviceNotification(&s_notification_data);641}642#endif643644#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)645if (s_bJoystickThread) {646SDL_StopJoystickThread();647}648#endif649650SDL_DINPUT_JoystickQuit();651SDL_XINPUT_JoystickQuit();652653WIN_QuitDeviceNotification();654}655656static bool WINDOWS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)657{658return false;659}660661SDL_JoystickDriver SDL_WINDOWS_JoystickDriver = {662WINDOWS_JoystickInit,663WINDOWS_JoystickGetCount,664WINDOWS_JoystickDetect,665WINDOWS_JoystickIsDevicePresent,666WINDOWS_JoystickGetDeviceName,667WINDOWS_JoystickGetDevicePath,668WINDOWS_JoystickGetDeviceSteamVirtualGamepadSlot,669WINDOWS_JoystickGetDevicePlayerIndex,670WINDOWS_JoystickSetDevicePlayerIndex,671WINDOWS_JoystickGetDeviceGUID,672WINDOWS_JoystickGetDeviceInstanceID,673WINDOWS_JoystickOpen,674WINDOWS_JoystickRumble,675WINDOWS_JoystickRumbleTriggers,676WINDOWS_JoystickSetLED,677WINDOWS_JoystickSendEffect,678WINDOWS_JoystickSetSensorsEnabled,679WINDOWS_JoystickUpdate,680WINDOWS_JoystickClose,681WINDOWS_JoystickQuit,682WINDOWS_JoystickGetGamepadMapping683};684685#else686687#ifdef SDL_JOYSTICK_RAWINPUT688// The RAWINPUT driver needs the device notification setup above689#error SDL_JOYSTICK_RAWINPUT requires SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT690#endif691692#endif // SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT693694695