Path: blob/master/thirdparty/sdl/joystick/SDL_joystick.c
21987 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// This is the joystick API for Simple DirectMedia Layer2324#include "SDL_sysjoystick.h"25#include "../SDL_hints_c.h"26#include "SDL_gamepad_c.h"27#include "SDL_joystick_c.h"28#include "SDL_steam_virtual_gamepad.h"2930#include "../events/SDL_events_c.h"31//#include "../video/SDL_sysvideo.h"32#include "../sensor/SDL_sensor_c.h"33#include "hidapi/SDL_hidapijoystick_c.h"3435// This is included in only one place because it has a large static list of controllers36#include "controller_type.h"3738#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)39// Needed for checking for input remapping programs40#include "../core/windows/SDL_windows.h"4142#undef UNICODE // We want ASCII functions43#include <tlhelp32.h>44#endif4546#ifdef SDL_JOYSTICK_VIRTUAL47#include "./virtual/SDL_virtualjoystick_c.h"48#endif4950static SDL_JoystickDriver *SDL_joystick_drivers[] = {51#ifdef SDL_JOYSTICK_HIDAPI // Highest priority driver for supported devices52&SDL_HIDAPI_JoystickDriver,53#endif54#ifdef SDL_JOYSTICK_PRIVATE55&SDL_PRIVATE_JoystickDriver,56#endif57#ifdef SDL_JOYSTICK_GAMEINPUT // Higher priority than other Windows drivers58&SDL_GAMEINPUT_JoystickDriver,59#endif60#ifdef SDL_JOYSTICK_RAWINPUT // Before WINDOWS driver, as WINDOWS wants to check if this driver is handling things61&SDL_RAWINPUT_JoystickDriver,62#endif63#if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT) // Before WGI driver, as WGI wants to check if this driver is handling things64&SDL_WINDOWS_JoystickDriver,65#endif66#ifdef SDL_JOYSTICK_WGI67&SDL_WGI_JoystickDriver,68#endif69#ifdef SDL_JOYSTICK_WINMM70&SDL_WINMM_JoystickDriver,71#endif72#ifdef SDL_JOYSTICK_LINUX73&SDL_LINUX_JoystickDriver,74#endif75#ifdef SDL_JOYSTICK_IOKIT76&SDL_DARWIN_JoystickDriver,77#endif78#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)) && !defined(SDL_JOYSTICK_DISABLED)79&SDL_IOS_JoystickDriver,80#endif81#ifdef SDL_JOYSTICK_ANDROID82&SDL_ANDROID_JoystickDriver,83#endif84#ifdef SDL_JOYSTICK_EMSCRIPTEN85&SDL_EMSCRIPTEN_JoystickDriver,86#endif87#ifdef SDL_JOYSTICK_HAIKU88&SDL_HAIKU_JoystickDriver,89#endif90#ifdef SDL_JOYSTICK_USBHID /* !!! FIXME: "USBHID" is a generic name, and doubly-confusing with HIDAPI next to it. This is the *BSD interface, rename this. */91&SDL_BSD_JoystickDriver,92#endif93#ifdef SDL_JOYSTICK_PS294&SDL_PS2_JoystickDriver,95#endif96#ifdef SDL_JOYSTICK_PSP97&SDL_PSP_JoystickDriver,98#endif99#ifdef SDL_JOYSTICK_VIRTUAL100&SDL_VIRTUAL_JoystickDriver,101#endif102#ifdef SDL_JOYSTICK_VITA103&SDL_VITA_JoystickDriver,104#endif105#ifdef SDL_JOYSTICK_N3DS106&SDL_N3DS_JoystickDriver,107#endif108#if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED)109&SDL_DUMMY_JoystickDriver110#endif111};112113#ifndef SDL_THREAD_SAFETY_ANALYSIS114static115#endif116SDL_Mutex *SDL_joystick_lock = NULL; // This needs to support recursive locks117static SDL_AtomicInt SDL_joystick_lock_pending;118static int SDL_joysticks_locked;119static bool SDL_joysticks_initialized;120static bool SDL_joysticks_quitting;121static bool SDL_joystick_being_added;122static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_joystick_lock) = NULL;123static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_joystick_lock) = 0;124static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_joystick_lock) = NULL;125static bool SDL_joystick_allows_background_events = false;126127static Uint32 initial_arcadestick_devices[] = {128MAKE_VIDPID(0x0079, 0x181a), // Venom Arcade Stick129MAKE_VIDPID(0x0079, 0x181b), // Venom Arcade Stick130MAKE_VIDPID(0x0c12, 0x0ef6), // Hitbox Arcade Stick131MAKE_VIDPID(0x0e6f, 0x0109), // PDP Versus Fighting Pad132MAKE_VIDPID(0x0f0d, 0x0016), // Hori Real Arcade Pro.EX133MAKE_VIDPID(0x0f0d, 0x001b), // Hori Real Arcade Pro VX134MAKE_VIDPID(0x0f0d, 0x0063), // Hori Real Arcade Pro Hayabusa (USA) Xbox One135MAKE_VIDPID(0x0f0d, 0x006a), // Real Arcade Pro 4136MAKE_VIDPID(0x0f0d, 0x0078), // Hori Real Arcade Pro V Kai Xbox One137MAKE_VIDPID(0x0f0d, 0x008a), // HORI Real Arcade Pro 4138MAKE_VIDPID(0x0f0d, 0x008c), // Hori Real Arcade Pro 4139MAKE_VIDPID(0x0f0d, 0x00aa), // HORI Real Arcade Pro V Hayabusa in Switch Mode140MAKE_VIDPID(0x0f0d, 0x00ed), // Hori Fighting Stick mini 4 kai141MAKE_VIDPID(0x0f0d, 0x011c), // Hori Fighting Stick Alpha in PS4 Mode142MAKE_VIDPID(0x0f0d, 0x011e), // Hori Fighting Stick Alpha in PC Mode143MAKE_VIDPID(0x0f0d, 0x0184), // Hori Fighting Stick Alpha in PS5 Mode144MAKE_VIDPID(0x146b, 0x0604), // NACON Daija Arcade Stick145MAKE_VIDPID(0x1532, 0x0a00), // Razer Atrox Arcade Stick146MAKE_VIDPID(0x1bad, 0xf03d), // Street Fighter IV Arcade Stick TE - Chun Li147MAKE_VIDPID(0x1bad, 0xf502), // Hori Real Arcade Pro.VX SA148MAKE_VIDPID(0x1bad, 0xf504), // Hori Real Arcade Pro. EX149MAKE_VIDPID(0x1bad, 0xf506), // Hori Real Arcade Pro.EX Premium VLX150MAKE_VIDPID(0x20d6, 0xa715), // PowerA Nintendo Switch Fusion Arcade Stick151MAKE_VIDPID(0x24c6, 0x5000), // Razer Atrox Arcade Stick152MAKE_VIDPID(0x24c6, 0x5501), // Hori Real Arcade Pro VX-SA153MAKE_VIDPID(0x24c6, 0x550e), // Hori Real Arcade Pro V Kai 360154MAKE_VIDPID(0x2c22, 0x2300), // Qanba Obsidian Arcade Joystick in PS4 Mode155MAKE_VIDPID(0x2c22, 0x2302), // Qanba Obsidian Arcade Joystick in PS3 Mode156MAKE_VIDPID(0x2c22, 0x2303), // Qanba Obsidian Arcade Joystick in PC Mode157MAKE_VIDPID(0x2c22, 0x2500), // Qanba Dragon Arcade Joystick in PS4 Mode158MAKE_VIDPID(0x2c22, 0x2502), // Qanba Dragon Arcade Joystick in PS3 Mode159MAKE_VIDPID(0x2c22, 0x2503), // Qanba Dragon Arcade Joystick in PC Mode160};161static SDL_vidpid_list arcadestick_devices = {162SDL_HINT_JOYSTICK_ARCADESTICK_DEVICES, 0, 0, NULL,163SDL_HINT_JOYSTICK_ARCADESTICK_DEVICES_EXCLUDED, 0, 0, NULL,164SDL_arraysize(initial_arcadestick_devices), initial_arcadestick_devices,165false166};167168/* This list is taken from:169https://raw.githubusercontent.com/denilsonsa/udev-joystick-blacklist/master/generate_rules.py170*/171static Uint32 initial_blacklist_devices[] = {172// Microsoft Microsoft Wireless Optical Desktop 2.10173// Microsoft Wireless Desktop - Comfort Edition174MAKE_VIDPID(0x045e, 0x009d),175176// Microsoft Microsoft Digital Media Pro Keyboard177// Microsoft Corp. Digital Media Pro Keyboard178MAKE_VIDPID(0x045e, 0x00b0),179180// Microsoft Microsoft Digital Media Keyboard181// Microsoft Corp. Digital Media Keyboard 1.0A182MAKE_VIDPID(0x045e, 0x00b4),183184// Microsoft Microsoft Digital Media Keyboard 3000185MAKE_VIDPID(0x045e, 0x0730),186187// Microsoft Microsoft 2.4GHz Transceiver v6.0188// Microsoft Microsoft 2.4GHz Transceiver v8.0189// Microsoft Corp. Nano Transceiver v1.0 for Bluetooth190// Microsoft Wireless Mobile Mouse 1000191// Microsoft Wireless Desktop 3000192MAKE_VIDPID(0x045e, 0x0745),193194// Microsoft SideWinder(TM) 2.4GHz Transceiver195MAKE_VIDPID(0x045e, 0x0748),196197// Microsoft Corp. Wired Keyboard 600198MAKE_VIDPID(0x045e, 0x0750),199200// Microsoft Corp. Sidewinder X4 keyboard201MAKE_VIDPID(0x045e, 0x0768),202203// Microsoft Corp. Arc Touch Mouse Transceiver204MAKE_VIDPID(0x045e, 0x0773),205206// Microsoft 2.4GHz Transceiver v9.0207// Microsoft Nano Transceiver v2.1208// Microsoft Sculpt Ergonomic Keyboard (5KV-00001)209MAKE_VIDPID(0x045e, 0x07a5),210211// Microsoft Nano Transceiver v1.0212// Microsoft Wireless Keyboard 800213MAKE_VIDPID(0x045e, 0x07b2),214215// Microsoft Nano Transceiver v2.0216MAKE_VIDPID(0x045e, 0x0800),217218MAKE_VIDPID(0x046d, 0xc30a), // Logitech, Inc. iTouch Composite keyboard219220MAKE_VIDPID(0x04d9, 0xa0df), // Tek Syndicate Mouse (E-Signal USB Gaming Mouse)221222// List of Wacom devices at: http://linuxwacom.sourceforge.net/wiki/index.php/Device_IDs223MAKE_VIDPID(0x056a, 0x0010), // Wacom ET-0405 Graphire224MAKE_VIDPID(0x056a, 0x0011), // Wacom ET-0405A Graphire2 (4x5)225MAKE_VIDPID(0x056a, 0x0012), // Wacom ET-0507A Graphire2 (5x7)226MAKE_VIDPID(0x056a, 0x0013), // Wacom CTE-430 Graphire3 (4x5)227MAKE_VIDPID(0x056a, 0x0014), // Wacom CTE-630 Graphire3 (6x8)228MAKE_VIDPID(0x056a, 0x0015), // Wacom CTE-440 Graphire4 (4x5)229MAKE_VIDPID(0x056a, 0x0016), // Wacom CTE-640 Graphire4 (6x8)230MAKE_VIDPID(0x056a, 0x0017), // Wacom CTE-450 Bamboo Fun (4x5)231MAKE_VIDPID(0x056a, 0x0018), // Wacom CTE-650 Bamboo Fun 6x8232MAKE_VIDPID(0x056a, 0x0019), // Wacom CTE-631 Bamboo One233MAKE_VIDPID(0x056a, 0x00d1), // Wacom Bamboo Pen and Touch CTH-460234MAKE_VIDPID(0x056a, 0x030e), // Wacom Intuos Pen (S) CTL-480235236MAKE_VIDPID(0x09da, 0x054f), // A4 Tech Co., G7 750 mouse237MAKE_VIDPID(0x09da, 0x1410), // A4 Tech Co., Ltd Bloody AL9 mouse238MAKE_VIDPID(0x09da, 0x3043), // A4 Tech Co., Ltd Bloody R8A Gaming Mouse239MAKE_VIDPID(0x09da, 0x31b5), // A4 Tech Co., Ltd Bloody TL80 Terminator Laser Gaming Mouse240MAKE_VIDPID(0x09da, 0x3997), // A4 Tech Co., Ltd Bloody RT7 Terminator Wireless241MAKE_VIDPID(0x09da, 0x3f8b), // A4 Tech Co., Ltd Bloody V8 mouse242MAKE_VIDPID(0x09da, 0x51f4), // Modecom MC-5006 Keyboard243MAKE_VIDPID(0x09da, 0x5589), // A4 Tech Co., Ltd Terminator TL9 Laser Gaming Mouse244MAKE_VIDPID(0x09da, 0x7b22), // A4 Tech Co., Ltd Bloody V5245MAKE_VIDPID(0x09da, 0x7f2d), // A4 Tech Co., Ltd Bloody R3 mouse246MAKE_VIDPID(0x09da, 0x8090), // A4 Tech Co., Ltd X-718BK Oscar Optical Gaming Mouse247MAKE_VIDPID(0x09da, 0x9033), // A4 Tech Co., X7 X-705K248MAKE_VIDPID(0x09da, 0x9066), // A4 Tech Co., Sharkoon Fireglider Optical249MAKE_VIDPID(0x09da, 0x9090), // A4 Tech Co., Ltd XL-730K / XL-750BK / XL-755BK Laser Mouse250MAKE_VIDPID(0x09da, 0x90c0), // A4 Tech Co., Ltd X7 G800V keyboard251MAKE_VIDPID(0x09da, 0xf012), // A4 Tech Co., Ltd Bloody V7 mouse252MAKE_VIDPID(0x09da, 0xf32a), // A4 Tech Co., Ltd Bloody B540 keyboard253MAKE_VIDPID(0x09da, 0xf613), // A4 Tech Co., Ltd Bloody V2 mouse254MAKE_VIDPID(0x09da, 0xf624), // A4 Tech Co., Ltd Bloody B120 Keyboard255256MAKE_VIDPID(0x1b1c, 0x1b3c), // Corsair Harpoon RGB gaming mouse257258MAKE_VIDPID(0x1d57, 0xad03), // [T3] 2.4GHz and IR Air Mouse Remote Control259260MAKE_VIDPID(0x1e7d, 0x2e4a), // Roccat Tyon Mouse261262MAKE_VIDPID(0x20a0, 0x422d), // Winkeyless.kr Keyboards263264MAKE_VIDPID(0x2516, 0x001f), // Cooler Master Storm Mizar Mouse265MAKE_VIDPID(0x2516, 0x0028), // Cooler Master Storm Alcor Mouse266267/*****************************************************************/268// Additional entries269/*****************************************************************/270271MAKE_VIDPID(0x04d9, 0x8008), // OBINLB USB-HID Keyboard (Anne Pro II)272MAKE_VIDPID(0x04d9, 0x8009), // OBINLB USB-HID Keyboard (Anne Pro II)273MAKE_VIDPID(0x04d9, 0xa292), // OBINLB USB-HID Keyboard (Anne Pro II)274MAKE_VIDPID(0x04d9, 0xa293), // OBINLB USB-HID Keyboard (Anne Pro II)275MAKE_VIDPID(0x1532, 0x0266), // Razer Huntsman V2 Analog, non-functional DInput device276MAKE_VIDPID(0x1532, 0x0282), // Razer Huntsman Mini Analog, non-functional DInput device277MAKE_VIDPID(0x26ce, 0x01a2), // ASRock LED Controller278MAKE_VIDPID(0x20d6, 0x0002), // PowerA Enhanced Wireless Controller for Nintendo Switch (charging port only)279MAKE_VIDPID(0x31e3, 0x1310), // Wooting 60HE (ARM)280MAKE_VIDPID(0x3297, 0x1969), // Moonlander MK1 Keyboard281MAKE_VIDPID(0x3434, 0x0211), // Keychron K1 Pro System Control282MAKE_VIDPID(0x04f2, 0xa13c), // HP Deluxe Webcam KQ246AA283};284static SDL_vidpid_list blacklist_devices = {285SDL_HINT_JOYSTICK_BLACKLIST_DEVICES, 0, 0, NULL,286SDL_HINT_JOYSTICK_BLACKLIST_DEVICES_EXCLUDED, 0, 0, NULL,287SDL_arraysize(initial_blacklist_devices), initial_blacklist_devices,288false289};290291static Uint32 initial_flightstick_devices[] = {292MAKE_VIDPID(0x044f, 0x0402), // HOTAS Warthog Joystick293MAKE_VIDPID(0x044f, 0xb10a), // ThrustMaster, Inc. T.16000M Joystick294MAKE_VIDPID(0x046d, 0xc215), // Logitech Extreme 3D295MAKE_VIDPID(0x0583, 0x6258), // Padix USB joystick with viewfinder296MAKE_VIDPID(0x0583, 0x688f), // Padix QF-688uv Windstorm Pro297MAKE_VIDPID(0x0583, 0x7070), // Padix QF-707u Bazooka298MAKE_VIDPID(0x0583, 0xa019), // Padix USB vibration joystick with viewfinder299MAKE_VIDPID(0x0583, 0xa131), // Padix USB Wireless 2.4GHz300MAKE_VIDPID(0x0583, 0xa209), // Padix MetalStrike ForceFeedback301MAKE_VIDPID(0x0583, 0xb010), // Padix MetalStrike Pro302MAKE_VIDPID(0x0583, 0xb012), // Padix Wireless MetalStrike303MAKE_VIDPID(0x0583, 0xb013), // Padix USB Wireless 2.4GHZ304MAKE_VIDPID(0x0738, 0x2221), // Saitek Pro Flight X-56 Rhino Stick305MAKE_VIDPID(0x10f5, 0x7084), // Turtle Beach VelocityOne306MAKE_VIDPID(0x231d, 0x0126), // Gunfighter Mk.III 'Space Combat Edition' (right)307MAKE_VIDPID(0x231d, 0x0127), // Gunfighter Mk.III 'Space Combat Edition' (left)308MAKE_VIDPID(0x362c, 0x0001), // Yawman Arrow309};310static SDL_vidpid_list flightstick_devices = {311SDL_HINT_JOYSTICK_FLIGHTSTICK_DEVICES, 0, 0, NULL,312SDL_HINT_JOYSTICK_FLIGHTSTICK_DEVICES_EXCLUDED, 0, 0, NULL,313SDL_arraysize(initial_flightstick_devices), initial_flightstick_devices,314false315};316317static Uint32 initial_gamecube_devices[] = {318MAKE_VIDPID(0x0e6f, 0x0185), // PDP Wired Fight Pad Pro for Nintendo Switch319MAKE_VIDPID(0x20d6, 0xa711), // PowerA Wired Controller Nintendo GameCube Style320};321static SDL_vidpid_list gamecube_devices = {322SDL_HINT_JOYSTICK_GAMECUBE_DEVICES, 0, 0, NULL,323SDL_HINT_JOYSTICK_GAMECUBE_DEVICES_EXCLUDED, 0, 0, NULL,324SDL_arraysize(initial_gamecube_devices), initial_gamecube_devices,325false326};327328static Uint32 initial_rog_gamepad_mice[] = {329MAKE_VIDPID(0x0b05, 0x18e3), // ROG Chakram (wired) Mouse330MAKE_VIDPID(0x0b05, 0x18e5), // ROG Chakram (wireless) Mouse331MAKE_VIDPID(0x0b05, 0x1906), // ROG Pugio II332MAKE_VIDPID(0x0b05, 0x1958), // ROG Chakram Core Mouse333MAKE_VIDPID(0x0b05, 0x1a18), // ROG Chakram X (wired) Mouse334MAKE_VIDPID(0x0b05, 0x1a1a), // ROG Chakram X (wireless) Mouse335MAKE_VIDPID(0x0b05, 0x1a1c), // ROG Chakram X (Bluetooth) Mouse336};337static SDL_vidpid_list rog_gamepad_mice = {338SDL_HINT_ROG_GAMEPAD_MICE, 0, 0, NULL,339SDL_HINT_ROG_GAMEPAD_MICE_EXCLUDED, 0, 0, NULL,340SDL_arraysize(initial_rog_gamepad_mice), initial_rog_gamepad_mice,341false342};343344static Uint32 initial_throttle_devices[] = {345MAKE_VIDPID(0x044f, 0x0404), // HOTAS Warthog Throttle346MAKE_VIDPID(0x0738, 0xa221), // Saitek Pro Flight X-56 Rhino Throttle347MAKE_VIDPID(0x10f5, 0x7085), // Turtle Beach VelocityOne Throttle348};349static SDL_vidpid_list throttle_devices = {350SDL_HINT_JOYSTICK_THROTTLE_DEVICES, 0, 0, NULL,351SDL_HINT_JOYSTICK_THROTTLE_DEVICES_EXCLUDED, 0, 0, NULL,352SDL_arraysize(initial_throttle_devices), initial_throttle_devices,353false354};355356static Uint32 initial_wheel_devices[] = {357MAKE_VIDPID(0x0079, 0x1864), // DragonRise Inc. Wired Wheel (active mode) (also known as PXN V900 (PS3), Superdrive SV-750, or a Genesis Seaborg 400)358MAKE_VIDPID(0x044f, 0xb65d), // Thrustmaster Wheel FFB359MAKE_VIDPID(0x044f, 0xb65e), // Thrustmaster T500RS360MAKE_VIDPID(0x044f, 0xb664), // Thrustmaster TX (initial mode)361MAKE_VIDPID(0x044f, 0xb669), // Thrustmaster TX (active mode)362MAKE_VIDPID(0x044f, 0xb66d), // Thrustmaster T300RS (PS4 mode)363MAKE_VIDPID(0x044f, 0xb66d), // Thrustmaster Wheel FFB364MAKE_VIDPID(0x044f, 0xb66e), // Thrustmaster T300RS (normal mode)365MAKE_VIDPID(0x044f, 0xb66f), // Thrustmaster T300RS (advanced mode)366MAKE_VIDPID(0x044f, 0xb677), // Thrustmaster T150367MAKE_VIDPID(0x044f, 0xb67f), // Thrustmaster TMX368MAKE_VIDPID(0x044f, 0xb691), // Thrustmaster TS-XW (initial mode)369MAKE_VIDPID(0x044f, 0xb692), // Thrustmaster TS-XW (active mode)370MAKE_VIDPID(0x044f, 0xb696), // Thrustmaster T248371MAKE_VIDPID(0x046d, 0xc24f), // Logitech G29 (PS3)372MAKE_VIDPID(0x046d, 0xc260), // Logitech G29 (PS4)373MAKE_VIDPID(0x046d, 0xc261), // Logitech G920 (initial mode)374MAKE_VIDPID(0x046d, 0xc262), // Logitech G920 (active mode)375MAKE_VIDPID(0x046d, 0xc266), // Logitech G923 for Playstation 4 and PC (PC mode)376MAKE_VIDPID(0x046d, 0xc267), // Logitech G923 for Playstation 4 and PC (PS4 mode)377MAKE_VIDPID(0x046d, 0xc268), // Logitech PRO Racing Wheel (PC mode)378MAKE_VIDPID(0x046d, 0xc269), // Logitech PRO Racing Wheel (PS4/PS5 mode)379MAKE_VIDPID(0x046d, 0xc26d), // Logitech G923 (Xbox)380MAKE_VIDPID(0x046d, 0xc26e), // Logitech G923381MAKE_VIDPID(0x046d, 0xc272), // Logitech PRO Racing Wheel for Xbox (PC mode)382MAKE_VIDPID(0x046d, 0xc294), // Logitech generic wheel383MAKE_VIDPID(0x046d, 0xc295), // Logitech Momo Force384MAKE_VIDPID(0x046d, 0xc298), // Logitech Driving Force Pro385MAKE_VIDPID(0x046d, 0xc299), // Logitech G25386MAKE_VIDPID(0x046d, 0xc29a), // Logitech Driving Force GT387MAKE_VIDPID(0x046d, 0xc29b), // Logitech G27388MAKE_VIDPID(0x046d, 0xca03), // Logitech Momo Racing389MAKE_VIDPID(0x0483, 0x0522), // Simagic Wheelbase (including M10, Alpha Mini, Alpha, Alpha U)390MAKE_VIDPID(0x0483, 0xa355), // VRS DirectForce Pro Wheel Base391MAKE_VIDPID(0x0583, 0xa132), // Padix USB Wireless 2.4GHz Wheelpad392MAKE_VIDPID(0x0583, 0xa133), // Padix USB Wireless 2.4GHz Wheel393MAKE_VIDPID(0x0583, 0xa202), // Padix Force Feedback Wheel394MAKE_VIDPID(0x0583, 0xb002), // Padix Vibration USB Wheel395MAKE_VIDPID(0x0583, 0xb005), // Padix USB Wheel396MAKE_VIDPID(0x0583, 0xb008), // Padix USB Wireless 2.4GHz Wheel397MAKE_VIDPID(0x0583, 0xb009), // Padix USB Wheel398MAKE_VIDPID(0x0583, 0xb018), // Padix TW6 Wheel399MAKE_VIDPID(0x0eb7, 0x0001), // Fanatec ClubSport Wheel Base V2400MAKE_VIDPID(0x0eb7, 0x0004), // Fanatec ClubSport Wheel Base V2.5401MAKE_VIDPID(0x0eb7, 0x0005), // Fanatec CSL Elite Wheel Base+ (PS4)402MAKE_VIDPID(0x0eb7, 0x0006), // Fanatec Podium Wheel Base DD1403MAKE_VIDPID(0x0eb7, 0x0007), // Fanatec Podium Wheel Base DD2404MAKE_VIDPID(0x0eb7, 0x0011), // Fanatec Forza Motorsport (CSR Wheel / CSR Elite Wheel)405MAKE_VIDPID(0x0eb7, 0x0020), // Fanatec generic wheel / CSL DD / GT DD Pro406MAKE_VIDPID(0x0eb7, 0x0197), // Fanatec Porsche Wheel (Turbo / GT3 RS / Turbo S / GT3 V2 / GT2)407MAKE_VIDPID(0x0eb7, 0x038e), // Fanatec ClubSport Wheel Base V1408MAKE_VIDPID(0x0eb7, 0x0e03), // Fanatec CSL Elite Wheel Base409MAKE_VIDPID(0x11ff, 0x0511), // DragonRise Inc. Wired Wheel (initial mode) (also known as PXN V900 (PS3), Superdrive SV-750, or a Genesis Seaborg 400)410MAKE_VIDPID(0x1209, 0xffb0), // Generic FFBoard OpenFFBoard universal forcefeedback wheel411MAKE_VIDPID(0x16d0, 0x0d5a), // Simucube 1 Wheelbase412MAKE_VIDPID(0x16d0, 0x0d5f), // Simucube 2 Ultimate Wheelbase413MAKE_VIDPID(0x16d0, 0x0d60), // Simucube 2 Pro Wheelbase414MAKE_VIDPID(0x16d0, 0x0d61), // Simucube 2 Sport Wheelbase415MAKE_VIDPID(0x2433, 0xf300), // Asetek SimSports Invicta Wheelbase416MAKE_VIDPID(0x2433, 0xf301), // Asetek SimSports Forte Wheelbase417MAKE_VIDPID(0x2433, 0xf303), // Asetek SimSports La Prima Wheelbase418MAKE_VIDPID(0x2433, 0xf306), // Asetek SimSports Tony Kannan Wheelbase419MAKE_VIDPID(0x3416, 0x0301), // Cammus C5 Wheelbase420MAKE_VIDPID(0x3416, 0x0302), // Cammus C12 Wheelbase421MAKE_VIDPID(0x346e, 0x0000), // Moza R16/R21 Wheelbase422MAKE_VIDPID(0x346e, 0x0002), // Moza R9 Wheelbase423MAKE_VIDPID(0x346e, 0x0004), // Moza R5 Wheelbase424MAKE_VIDPID(0x346e, 0x0005), // Moza R3 Wheelbase425MAKE_VIDPID(0x346e, 0x0006), // Moza R12 Wheelbase426};427static SDL_vidpid_list wheel_devices = {428SDL_HINT_JOYSTICK_WHEEL_DEVICES, 0, 0, NULL,429SDL_HINT_JOYSTICK_WHEEL_DEVICES_EXCLUDED, 0, 0, NULL,430SDL_arraysize(initial_wheel_devices), initial_wheel_devices,431false432};433434static Uint32 initial_zero_centered_devices[] = {435MAKE_VIDPID(0x05a0, 0x3232), // 8Bitdo Zero Gamepad436MAKE_VIDPID(0x0e8f, 0x3013), // HuiJia SNES USB adapter437};438static SDL_vidpid_list zero_centered_devices = {439SDL_HINT_JOYSTICK_ZERO_CENTERED_DEVICES, 0, 0, NULL,440NULL, 0, 0, NULL,441SDL_arraysize(initial_zero_centered_devices), initial_zero_centered_devices,442false443};444445#define CHECK_JOYSTICK_MAGIC(joystick, result) \446if (!SDL_ObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK)) { \447SDL_InvalidParamError("joystick"); \448SDL_UnlockJoysticks(); \449return result; \450}451452#define CHECK_JOYSTICK_VIRTUAL(joystick, result) \453if (!joystick->is_virtual) { \454SDL_SetError("joystick isn't virtual"); \455SDL_UnlockJoysticks(); \456return result; \457}458459bool SDL_JoysticksInitialized(void)460{461return SDL_joysticks_initialized;462}463464bool SDL_JoysticksQuitting(void)465{466return SDL_joysticks_quitting;467}468469void SDL_LockJoysticks(void)470{471(void)SDL_AtomicIncRef(&SDL_joystick_lock_pending);472SDL_LockMutex(SDL_joystick_lock);473(void)SDL_AtomicDecRef(&SDL_joystick_lock_pending);474475++SDL_joysticks_locked;476}477478void SDL_UnlockJoysticks(void)479{480bool last_unlock = false;481482--SDL_joysticks_locked;483484if (!SDL_joysticks_initialized) {485// NOTE: There's a small window here where another thread could lock the mutex after we've checked for pending locks486if (!SDL_joysticks_locked && SDL_GetAtomicInt(&SDL_joystick_lock_pending) == 0) {487last_unlock = true;488}489}490491/* The last unlock after joysticks are uninitialized will cleanup the mutex,492* allowing applications to lock joysticks while reinitializing the system.493*/494if (last_unlock) {495SDL_Mutex *joystick_lock = SDL_joystick_lock;496497SDL_LockMutex(joystick_lock);498{499SDL_UnlockMutex(SDL_joystick_lock);500501SDL_joystick_lock = NULL;502}503SDL_UnlockMutex(joystick_lock);504SDL_DestroyMutex(joystick_lock);505} else {506SDL_UnlockMutex(SDL_joystick_lock);507}508}509510bool SDL_JoysticksLocked(void)511{512return (SDL_joysticks_locked > 0);513}514515void SDL_AssertJoysticksLocked(void)516{517SDL_assert(SDL_JoysticksLocked());518}519520/*521* Get the driver and device index for a joystick instance ID522* This should be called while the joystick lock is held, to prevent another thread from updating the list523*/524static bool SDL_GetDriverAndJoystickIndex(SDL_JoystickID instance_id, SDL_JoystickDriver **driver, int *driver_index)525{526int i, num_joysticks, device_index;527528SDL_AssertJoysticksLocked();529530if (instance_id > 0) {531for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {532num_joysticks = SDL_joystick_drivers[i]->GetCount();533for (device_index = 0; device_index < num_joysticks; ++device_index) {534SDL_JoystickID joystick_id = SDL_joystick_drivers[i]->GetDeviceInstanceID(device_index);535if (joystick_id == instance_id) {536*driver = SDL_joystick_drivers[i];537*driver_index = device_index;538return true;539}540}541}542}543544SDL_SetError("Joystick %" SDL_PRIu32 " not found", instance_id);545return false;546}547548static int SDL_FindFreePlayerIndex(void)549{550int player_index;551552SDL_AssertJoysticksLocked();553554for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {555if (SDL_joystick_players[player_index] == 0) {556break;557}558}559return player_index;560}561562static int SDL_GetPlayerIndexForJoystickID(SDL_JoystickID instance_id)563{564int player_index;565566SDL_AssertJoysticksLocked();567568for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {569if (instance_id == SDL_joystick_players[player_index]) {570break;571}572}573if (player_index == SDL_joystick_player_count) {574player_index = -1;575}576return player_index;577}578579static SDL_JoystickID SDL_GetJoystickIDForPlayerIndex(int player_index)580{581SDL_AssertJoysticksLocked();582583if (player_index < 0 || player_index >= SDL_joystick_player_count) {584return 0;585}586return SDL_joystick_players[player_index];587}588589static bool SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id)590{591SDL_JoystickID existing_instance = SDL_GetJoystickIDForPlayerIndex(player_index);592SDL_JoystickDriver *driver;593int device_index;594int existing_player_index;595596SDL_AssertJoysticksLocked();597598if (player_index >= SDL_joystick_player_count) {599SDL_JoystickID *new_players = (SDL_JoystickID *)SDL_realloc(SDL_joystick_players, (player_index + 1) * sizeof(*SDL_joystick_players));600if (!new_players) {601return false;602}603604SDL_joystick_players = new_players;605SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0]));606SDL_joystick_player_count = player_index + 1;607} else if (player_index >= 0 && SDL_joystick_players[player_index] == instance_id) {608// Joystick is already assigned the requested player index609return true;610}611612// Clear the old player index613existing_player_index = SDL_GetPlayerIndexForJoystickID(instance_id);614if (existing_player_index >= 0) {615SDL_joystick_players[existing_player_index] = 0;616}617618if (player_index >= 0) {619SDL_joystick_players[player_index] = instance_id;620}621622// Update the driver with the new index623if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {624driver->SetDevicePlayerIndex(device_index, player_index);625}626627// Move any existing joystick to another slot628if (existing_instance > 0) {629SDL_SetJoystickIDForPlayerIndex(SDL_FindFreePlayerIndex(), existing_instance);630}631return true;632}633634static void SDLCALL SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)635{636if (SDL_GetStringBoolean(hint, false)) {637SDL_joystick_allows_background_events = true;638} else {639SDL_joystick_allows_background_events = false;640}641}642643bool SDL_InitJoysticks(void)644{645int i;646bool result = false;647648// Create the joystick list lock649if (SDL_joystick_lock == NULL) {650SDL_joystick_lock = SDL_CreateMutex();651}652653if (!SDL_InitSubSystem(SDL_INIT_EVENTS)) {654return false;655}656657SDL_LockJoysticks();658659SDL_joysticks_initialized = true;660661SDL_InitGamepadMappings();662663SDL_LoadVIDPIDList(&arcadestick_devices);664SDL_LoadVIDPIDList(&blacklist_devices);665SDL_LoadVIDPIDList(&flightstick_devices);666SDL_LoadVIDPIDList(&gamecube_devices);667SDL_LoadVIDPIDList(&rog_gamepad_mice);668SDL_LoadVIDPIDList(&throttle_devices);669SDL_LoadVIDPIDList(&wheel_devices);670SDL_LoadVIDPIDList(&zero_centered_devices);671672// See if we should allow joystick events while in the background673SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,674SDL_JoystickAllowBackgroundEventsChanged, NULL);675676SDL_InitSteamVirtualGamepadInfo();677678for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {679if (SDL_joystick_drivers[i]->Init()) {680result = true;681}682}683SDL_UnlockJoysticks();684685if (!result) {686SDL_QuitJoysticks();687}688689return result;690}691692bool SDL_JoysticksOpened(void)693{694bool opened;695696SDL_LockJoysticks();697{698if (SDL_joysticks != NULL) {699opened = true;700} else {701opened = false;702}703}704SDL_UnlockJoysticks();705706return opened;707}708709bool SDL_JoystickHandledByAnotherDriver(struct SDL_JoystickDriver *driver, Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)710{711int i;712bool result = false;713714SDL_LockJoysticks();715{716for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {717if (driver == SDL_joystick_drivers[i]) {718// Higher priority drivers do not have this device719break;720}721if (SDL_joystick_drivers[i]->IsDevicePresent(vendor_id, product_id, version, name)) {722result = true;723break;724}725}726}727SDL_UnlockJoysticks();728729return result;730}731732bool SDL_HasJoystick(void)733{734int i;735int total_joysticks = 0;736737SDL_LockJoysticks();738{739for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {740total_joysticks += SDL_joystick_drivers[i]->GetCount();741}742}743SDL_UnlockJoysticks();744745if (total_joysticks > 0) {746return true;747}748return false;749}750751SDL_JoystickID *SDL_GetJoysticks(int *count)752{753int i, num_joysticks, device_index;754int joystick_index = 0, total_joysticks = 0;755SDL_JoystickID *joysticks;756757SDL_LockJoysticks();758{759for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {760total_joysticks += SDL_joystick_drivers[i]->GetCount();761}762763joysticks = (SDL_JoystickID *)SDL_malloc((total_joysticks + 1) * sizeof(*joysticks));764if (joysticks) {765if (count) {766*count = total_joysticks;767}768769for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {770num_joysticks = SDL_joystick_drivers[i]->GetCount();771for (device_index = 0; device_index < num_joysticks; ++device_index) {772SDL_assert(joystick_index < total_joysticks);773joysticks[joystick_index] = SDL_joystick_drivers[i]->GetDeviceInstanceID(device_index);774SDL_assert(joysticks[joystick_index] > 0);775++joystick_index;776}777}778SDL_assert(joystick_index == total_joysticks);779joysticks[joystick_index] = 0;780} else {781if (count) {782*count = 0;783}784}785}786SDL_UnlockJoysticks();787788return joysticks;789}790791const SDL_SteamVirtualGamepadInfo *SDL_GetJoystickVirtualGamepadInfoForID(SDL_JoystickID instance_id)792{793SDL_JoystickDriver *driver;794int device_index;795const SDL_SteamVirtualGamepadInfo *info = NULL;796797if (SDL_SteamVirtualGamepadEnabled() &&798SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {799info = SDL_GetSteamVirtualGamepadInfo(driver->GetDeviceSteamVirtualGamepadSlot(device_index));800}801return info;802}803804/*805* Get the implementation dependent name of a joystick806*/807const char *SDL_GetJoystickNameForID(SDL_JoystickID instance_id)808{809SDL_JoystickDriver *driver;810int device_index;811const char *name = NULL;812const SDL_SteamVirtualGamepadInfo *info;813814SDL_LockJoysticks();815info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);816if (info) {817name = SDL_GetPersistentString(info->name);818} else if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {819name = SDL_GetPersistentString(driver->GetDeviceName(device_index));820}821SDL_UnlockJoysticks();822823return name;824}825826/*827* Get the implementation dependent path of a joystick828*/829const char *SDL_GetJoystickPathForID(SDL_JoystickID instance_id)830{831SDL_JoystickDriver *driver;832int device_index;833const char *path = NULL;834835SDL_LockJoysticks();836if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {837path = SDL_GetPersistentString(driver->GetDevicePath(device_index));838}839SDL_UnlockJoysticks();840841if (!path) {842SDL_Unsupported();843}844return path;845}846847/*848* Get the player index of a joystick, or -1 if it's not available849*/850int SDL_GetJoystickPlayerIndexForID(SDL_JoystickID instance_id)851{852int player_index;853854SDL_LockJoysticks();855player_index = SDL_GetPlayerIndexForJoystickID(instance_id);856SDL_UnlockJoysticks();857858return player_index;859}860861/*862* Return true if this joystick is known to have all axes centered at zero863* This isn't generally needed unless the joystick never generates an initial axis value near zero,864* e.g. it's emulating axes with digital buttons865*/866static bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)867{868// printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);869870if (joystick->naxes == 2) {871// Assume D-pad or thumbstick style axes are centered at 0872return true;873}874875return SDL_VIDPIDInList(SDL_GetJoystickVendor(joystick), SDL_GetJoystickProduct(joystick), &zero_centered_devices);876}877878static bool IsROGAlly(SDL_Joystick *joystick)879{880Uint16 vendor, product;881SDL_GUID guid = SDL_GetJoystickGUID(joystick);882883// The ROG Ally controller spoofs an Xbox 360 controller884SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);885if (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX360_WIRED_CONTROLLER) {886// Check to see if this system has the expected sensors887bool has_ally_accel = false;888bool has_ally_gyro = false;889890if (SDL_InitSubSystem(SDL_INIT_SENSOR)) {891SDL_SensorID *sensors = SDL_GetSensors(NULL);892if (sensors) {893int i;894for (i = 0; sensors[i]; ++i) {895SDL_SensorID sensor = sensors[i];896897if (!has_ally_accel && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_ACCEL) {898const char *sensor_name = SDL_GetSensorNameForID(sensor);899if (sensor_name && SDL_strcmp(sensor_name, "Sensor BMI320 Acc") == 0) {900has_ally_accel = true;901}902}903if (!has_ally_gyro && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_GYRO) {904const char *sensor_name = SDL_GetSensorNameForID(sensor);905if (sensor_name && SDL_strcmp(sensor_name, "Sensor BMI320 Gyr") == 0) {906has_ally_gyro = true;907}908}909}910SDL_free(sensors);911}912SDL_QuitSubSystem(SDL_INIT_SENSOR);913}914if (has_ally_accel && has_ally_gyro) {915return true;916}917}918return false;919}920921static bool ShouldAttemptSensorFusion(SDL_Joystick *joystick, bool *invert_sensors)922{923SDL_AssertJoysticksLocked();924925*invert_sensors = false;926927// The SDL controller sensor API is only available for gamepads (at the moment)928if (!SDL_IsGamepad(joystick->instance_id)) {929return false;930}931932// If the controller already has sensors, use those933if (joystick->nsensors > 0) {934return false;935}936937const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLER_SENSOR_FUSION);938if (hint && *hint) {939if (*hint == '@' || SDL_strncmp(hint, "0x", 2) == 0) {940SDL_vidpid_list gamepads;941SDL_GUID guid;942Uint16 vendor, product;943bool enabled;944SDL_zero(gamepads);945946// See if the gamepad is in our list of devices to enable947guid = SDL_GetJoystickGUID(joystick);948SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);949SDL_LoadVIDPIDListFromHints(&gamepads, hint, NULL);950enabled = SDL_VIDPIDInList(vendor, product, &gamepads);951SDL_FreeVIDPIDList(&gamepads);952if (enabled) {953return true;954}955} else {956return SDL_GetStringBoolean(hint, false);957}958}959960// See if this is another known wraparound gamepad961if (joystick->name &&962(SDL_strstr(joystick->name, "Backbone One") ||963SDL_strstr(joystick->name, "Kishi"))) {964return true;965}966if (IsROGAlly(joystick)) {967/* I'm not sure if this is a Windows thing, or a quirk for ROG Ally,968* but we need to invert the sensor data on all axes.969*/970*invert_sensors = true;971return true;972}973return false;974}975976static void AttemptSensorFusion(SDL_Joystick *joystick, bool invert_sensors)977{978SDL_SensorID *sensors;979unsigned int i, j;980981SDL_AssertJoysticksLocked();982983if (!SDL_InitSubSystem(SDL_INIT_SENSOR)) {984return;985}986987sensors = SDL_GetSensors(NULL);988if (sensors) {989for (i = 0; sensors[i]; ++i) {990SDL_SensorID sensor = sensors[i];991992if (!joystick->accel_sensor && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_ACCEL) {993// Increment the sensor subsystem reference count994SDL_InitSubSystem(SDL_INIT_SENSOR);995996joystick->accel_sensor = sensor;997SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 0.0f);998}999if (!joystick->gyro_sensor && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_GYRO) {1000// Increment the sensor subsystem reference count1001SDL_InitSubSystem(SDL_INIT_SENSOR);10021003joystick->gyro_sensor = sensor;1004SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f);1005}1006}1007SDL_free(sensors);1008}1009SDL_QuitSubSystem(SDL_INIT_SENSOR);10101011/* SDL defines sensor orientation for phones relative to the natural1012orientation, and for gamepads relative to being held in front of you.1013When a phone is being used as a gamepad, its orientation changes,1014so adjust sensor axes to match.1015*/1016//if (SDL_GetNaturalDisplayOrientation(SDL_GetPrimaryDisplay()) == SDL_ORIENTATION_LANDSCAPE) {1017if (true) {1018/* When a device in landscape orientation is laid flat, the axes change1019orientation as follows:1020-X to +X becomes -X to +X1021-Y to +Y becomes +Z to -Z1022-Z to +Z becomes -Y to +Y1023*/1024joystick->sensor_transform[0][0] = 1.0f;1025joystick->sensor_transform[1][2] = 1.0f;1026joystick->sensor_transform[2][1] = -1.0f;1027} else {1028/* When a device in portrait orientation is rotated left and laid flat,1029the axes change orientation as follows:1030-X to +X becomes +Z to -Z1031-Y to +Y becomes +X to -X1032-Z to +Z becomes -Y to +Y1033*/1034joystick->sensor_transform[0][1] = -1.0f;1035joystick->sensor_transform[1][2] = 1.0f;1036joystick->sensor_transform[2][0] = -1.0f;1037}10381039if (invert_sensors) {1040for (i = 0; i < SDL_arraysize(joystick->sensor_transform); ++i) {1041for (j = 0; j < SDL_arraysize(joystick->sensor_transform[i]); ++j) {1042joystick->sensor_transform[i][j] *= -1.0f;1043}1044}1045}1046}10471048static void CleanupSensorFusion(SDL_Joystick *joystick)1049{1050SDL_AssertJoysticksLocked();10511052if (joystick->accel_sensor || joystick->gyro_sensor) {1053if (joystick->accel_sensor) {1054if (joystick->accel) {1055SDL_CloseSensor(joystick->accel);1056joystick->accel = NULL;1057}1058joystick->accel_sensor = 0;10591060// Decrement the sensor subsystem reference count1061SDL_QuitSubSystem(SDL_INIT_SENSOR);1062}1063if (joystick->gyro_sensor) {1064if (joystick->gyro) {1065SDL_CloseSensor(joystick->gyro);1066joystick->gyro = NULL;1067}1068joystick->gyro_sensor = 0;10691070// Decrement the sensor subsystem reference count1071SDL_QuitSubSystem(SDL_INIT_SENSOR);1072}1073}1074}10751076static bool ShouldSwapFaceButtons(const SDL_SteamVirtualGamepadInfo *info)1077{1078// When "Use Nintendo Button Layout" is enabled under Steam (the default)1079// it will send button 0 for the A (east) button and button 1 for the1080// B (south) button. This is done so that games that interpret the1081// buttons as Xbox input will get button 0 for "A" as they expect.1082//1083// However, SDL reports positional buttons, so we need to swap1084// the buttons so they show up in the correct position. This provides1085// consistent behavior regardless of whether we're running under Steam,1086// under the default settings.1087if (info &&1088(info->type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO ||1089info->type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT ||1090info->type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT ||1091info->type == SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR)) {1092return true;1093}1094return false;1095}10961097/*1098* Open a joystick for use - the index passed as an argument refers to1099* the N'th joystick on the system. This index is the value which will1100* identify this joystick in future joystick events.1101*1102* This function returns a joystick identifier, or NULL if an error occurred.1103*/1104SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)1105{1106SDL_JoystickDriver *driver;1107int device_index;1108SDL_Joystick *joystick;1109SDL_Joystick *joysticklist;1110const char *joystickname = NULL;1111const char *joystickpath = NULL;1112bool invert_sensors = false;1113const SDL_SteamVirtualGamepadInfo *info;11141115SDL_LockJoysticks();11161117if (!SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {1118SDL_UnlockJoysticks();1119return NULL;1120}11211122joysticklist = SDL_joysticks;1123/* If the joystick is already open, return it1124* it is important that we have a single joystick for each instance id1125*/1126while (joysticklist) {1127if (instance_id == joysticklist->instance_id) {1128joystick = joysticklist;1129++joystick->ref_count;1130SDL_UnlockJoysticks();1131return joystick;1132}1133joysticklist = joysticklist->next;1134}11351136// Create and initialize the joystick1137joystick = (SDL_Joystick *)SDL_calloc(1, sizeof(*joystick));1138if (!joystick) {1139SDL_UnlockJoysticks();1140return NULL;1141}1142SDL_SetObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK, true);1143joystick->driver = driver;1144joystick->instance_id = instance_id;1145joystick->attached = true;1146joystick->led_expiration = SDL_GetTicks();1147joystick->battery_percent = -1;1148#ifdef SDL_JOYSTICK_VIRTUAL1149joystick->is_virtual = (driver == &SDL_VIRTUAL_JoystickDriver);1150#else1151joystick->is_virtual = false;1152#endif11531154if (!driver->Open(joystick, device_index)) {1155SDL_SetObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK, false);1156SDL_free(joystick);1157SDL_UnlockJoysticks();1158return NULL;1159}11601161joystickname = driver->GetDeviceName(device_index);1162if (joystickname) {1163joystick->name = SDL_strdup(joystickname);1164}11651166joystickpath = driver->GetDevicePath(device_index);1167if (joystickpath) {1168joystick->path = SDL_strdup(joystickpath);1169}11701171joystick->guid = driver->GetDeviceGUID(device_index);11721173if (joystick->naxes > 0) {1174joystick->axes = (SDL_JoystickAxisInfo *)SDL_calloc(joystick->naxes, sizeof(*joystick->axes));1175}1176if (joystick->nballs > 0) {1177joystick->balls = (SDL_JoystickBallData *)SDL_calloc(joystick->nballs, sizeof(*joystick->balls));1178}1179if (joystick->nhats > 0) {1180joystick->hats = (Uint8 *)SDL_calloc(joystick->nhats, sizeof(*joystick->hats));1181}1182if (joystick->nbuttons > 0) {1183joystick->buttons = (bool *)SDL_calloc(joystick->nbuttons, sizeof(*joystick->buttons));1184}1185if (((joystick->naxes > 0) && !joystick->axes) ||1186((joystick->nballs > 0) && !joystick->balls) ||1187((joystick->nhats > 0) && !joystick->hats) ||1188((joystick->nbuttons > 0) && !joystick->buttons)) {1189SDL_CloseJoystick(joystick);1190SDL_UnlockJoysticks();1191return NULL;1192}11931194// If this joystick is known to have all zero centered axes, skip the auto-centering code1195if (SDL_JoystickAxesCenteredAtZero(joystick)) {1196for (int i = 0; i < joystick->naxes; ++i) {1197joystick->axes[i].has_initial_value = true;1198}1199}12001201// We know the initial values for HIDAPI and XInput joysticks1202if ((SDL_IsJoystickHIDAPI(joystick->guid) ||1203SDL_IsJoystickXInput(joystick->guid) ||1204SDL_IsJoystickRAWINPUT(joystick->guid) ||1205SDL_IsJoystickWGI(joystick->guid)) &&1206joystick->naxes >= SDL_GAMEPAD_AXIS_COUNT) {1207int left_trigger, right_trigger;1208if (SDL_IsJoystickXInput(joystick->guid)) {1209left_trigger = 2;1210right_trigger = 5;1211} else {1212left_trigger = SDL_GAMEPAD_AXIS_LEFT_TRIGGER;1213right_trigger = SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;1214}1215for (int i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {1216int initial_value;1217if (i == left_trigger || i == right_trigger) {1218initial_value = SDL_MIN_SINT16;1219} else {1220initial_value = 0;1221}1222joystick->axes[i].value = initial_value;1223joystick->axes[i].zero = initial_value;1224joystick->axes[i].initial_value = initial_value;1225joystick->axes[i].has_initial_value = true;1226}1227}12281229// Get the Steam Input API handle1230info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);1231if (info) {1232joystick->steam_handle = info->handle;1233joystick->swap_face_buttons = ShouldSwapFaceButtons(info);1234}12351236// Use system gyro and accelerometer if the gamepad doesn't have built-in sensors1237if (ShouldAttemptSensorFusion(joystick, &invert_sensors)) {1238AttemptSensorFusion(joystick, invert_sensors);1239}12401241// Add joystick to list1242++joystick->ref_count;1243// Link the joystick in the list1244joystick->next = SDL_joysticks;1245SDL_joysticks = joystick;12461247driver->Update(joystick);12481249SDL_UnlockJoysticks();12501251return joystick;1252}12531254SDL_JoystickID SDL_AttachVirtualJoystick(const SDL_VirtualJoystickDesc *desc)1255{1256#ifdef SDL_JOYSTICK_VIRTUAL1257SDL_JoystickID result;12581259SDL_LockJoysticks();1260result = SDL_JoystickAttachVirtualInner(desc);1261SDL_UnlockJoysticks();1262return result;1263#else1264SDL_SetError("SDL not built with virtual-joystick support");1265return 0;1266#endif1267}12681269bool SDL_DetachVirtualJoystick(SDL_JoystickID instance_id)1270{1271#ifdef SDL_JOYSTICK_VIRTUAL1272bool result;12731274SDL_LockJoysticks();1275result = SDL_JoystickDetachVirtualInner(instance_id);1276SDL_UnlockJoysticks();1277return result;1278#else1279return SDL_SetError("SDL not built with virtual-joystick support");1280#endif1281}12821283bool SDL_IsJoystickVirtual(SDL_JoystickID instance_id)1284{1285#ifdef SDL_JOYSTICK_VIRTUAL1286SDL_JoystickDriver *driver;1287int device_index;1288bool is_virtual = false;12891290SDL_LockJoysticks();1291if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {1292if (driver == &SDL_VIRTUAL_JoystickDriver) {1293is_virtual = true;1294}1295}1296SDL_UnlockJoysticks();12971298return is_virtual;1299#else1300return false;1301#endif1302}13031304bool SDL_SetJoystickVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value)1305{1306bool result;13071308SDL_LockJoysticks();1309{1310CHECK_JOYSTICK_MAGIC(joystick, false);1311CHECK_JOYSTICK_VIRTUAL(joystick, false);13121313#ifdef SDL_JOYSTICK_VIRTUAL1314result = SDL_SetJoystickVirtualAxisInner(joystick, axis, value);1315#else1316result = SDL_SetError("SDL not built with virtual-joystick support");1317#endif1318}1319SDL_UnlockJoysticks();13201321return result;1322}13231324bool SDL_SetJoystickVirtualBall(SDL_Joystick *joystick, int ball, Sint16 xrel, Sint16 yrel)1325{1326bool result;13271328SDL_LockJoysticks();1329{1330CHECK_JOYSTICK_MAGIC(joystick, false);1331CHECK_JOYSTICK_VIRTUAL(joystick, false);13321333#ifdef SDL_JOYSTICK_VIRTUAL1334result = SDL_SetJoystickVirtualBallInner(joystick, ball, xrel, yrel);1335#else1336result = SDL_SetError("SDL not built with virtual-joystick support");1337#endif1338}1339SDL_UnlockJoysticks();13401341return result;1342}13431344bool SDL_SetJoystickVirtualButton(SDL_Joystick *joystick, int button, bool down)1345{1346bool result;13471348SDL_LockJoysticks();1349{1350CHECK_JOYSTICK_MAGIC(joystick, false);1351CHECK_JOYSTICK_VIRTUAL(joystick, false);13521353#ifdef SDL_JOYSTICK_VIRTUAL1354result = SDL_SetJoystickVirtualButtonInner(joystick, button, down);1355#else1356result = SDL_SetError("SDL not built with virtual-joystick support");1357#endif1358}1359SDL_UnlockJoysticks();13601361return result;1362}13631364bool SDL_SetJoystickVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value)1365{1366bool result;13671368SDL_LockJoysticks();1369{1370CHECK_JOYSTICK_MAGIC(joystick, false);1371CHECK_JOYSTICK_VIRTUAL(joystick, false);13721373#ifdef SDL_JOYSTICK_VIRTUAL1374result = SDL_SetJoystickVirtualHatInner(joystick, hat, value);1375#else1376result = SDL_SetError("SDL not built with virtual-joystick support");1377#endif1378}1379SDL_UnlockJoysticks();13801381return result;1382}13831384bool SDL_SetJoystickVirtualTouchpad(SDL_Joystick *joystick, int touchpad, int finger, bool down, float x, float y, float pressure)1385{1386bool result;13871388SDL_LockJoysticks();1389{1390CHECK_JOYSTICK_MAGIC(joystick, false);1391CHECK_JOYSTICK_VIRTUAL(joystick, false);13921393#ifdef SDL_JOYSTICK_VIRTUAL1394result = SDL_SetJoystickVirtualTouchpadInner(joystick, touchpad, finger, down, x, y, pressure);1395#else1396result = SDL_SetError("SDL not built with virtual-joystick support");1397#endif1398}1399SDL_UnlockJoysticks();14001401return result;1402}14031404bool SDL_SendJoystickVirtualSensorData(SDL_Joystick *joystick, SDL_SensorType type, Uint64 sensor_timestamp, const float *data, int num_values)1405{1406bool result;14071408SDL_LockJoysticks();1409{1410CHECK_JOYSTICK_MAGIC(joystick, false);1411CHECK_JOYSTICK_VIRTUAL(joystick, false);14121413#ifdef SDL_JOYSTICK_VIRTUAL1414result = SDL_SendJoystickVirtualSensorDataInner(joystick, type, sensor_timestamp, data, num_values);1415#else1416result = SDL_SetError("SDL not built with virtual-joystick support");1417#endif1418}1419SDL_UnlockJoysticks();14201421return result;1422}14231424/*1425* Checks to make sure the joystick is valid.1426*/1427bool SDL_IsJoystickValid(SDL_Joystick *joystick)1428{1429SDL_AssertJoysticksLocked();1430return SDL_ObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK);1431}14321433bool SDL_PrivateJoystickGetAutoGamepadMapping(SDL_JoystickID instance_id, SDL_GamepadMapping *out)1434{1435SDL_JoystickDriver *driver;1436int device_index;1437bool is_ok = false;14381439SDL_LockJoysticks();1440if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {1441is_ok = driver->GetGamepadMapping(device_index, out);1442}1443SDL_UnlockJoysticks();14441445return is_ok;1446}14471448/*1449* Get the number of multi-dimensional axis controls on a joystick1450*/1451int SDL_GetNumJoystickAxes(SDL_Joystick *joystick)1452{1453int result;14541455SDL_LockJoysticks();1456{1457CHECK_JOYSTICK_MAGIC(joystick, -1);14581459result = joystick->naxes;1460}1461SDL_UnlockJoysticks();14621463return result;1464}14651466/*1467* Get the number of hats on a joystick1468*/1469int SDL_GetNumJoystickHats(SDL_Joystick *joystick)1470{1471int result;14721473SDL_LockJoysticks();1474{1475CHECK_JOYSTICK_MAGIC(joystick, -1);14761477result = joystick->nhats;1478}1479SDL_UnlockJoysticks();14801481return result;1482}14831484/*1485* Get the number of trackballs on a joystick1486*/1487int SDL_GetNumJoystickBalls(SDL_Joystick *joystick)1488{1489CHECK_JOYSTICK_MAGIC(joystick, -1);14901491return joystick->nballs;1492}14931494/*1495* Get the number of buttons on a joystick1496*/1497int SDL_GetNumJoystickButtons(SDL_Joystick *joystick)1498{1499int result;15001501SDL_LockJoysticks();1502{1503CHECK_JOYSTICK_MAGIC(joystick, -1);15041505result = joystick->nbuttons;1506}1507SDL_UnlockJoysticks();15081509return result;1510}15111512/*1513* Get the current state of an axis control on a joystick1514*/1515Sint16 SDL_GetJoystickAxis(SDL_Joystick *joystick, int axis)1516{1517Sint16 state;15181519SDL_LockJoysticks();1520{1521CHECK_JOYSTICK_MAGIC(joystick, 0);15221523if (axis < joystick->naxes) {1524state = joystick->axes[axis].value;1525} else {1526SDL_SetError("Joystick only has %d axes", joystick->naxes);1527state = 0;1528}1529}1530SDL_UnlockJoysticks();15311532return state;1533}15341535/*1536* Get the initial state of an axis control on a joystick1537*/1538bool SDL_GetJoystickAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state)1539{1540bool result;15411542SDL_LockJoysticks();1543{1544CHECK_JOYSTICK_MAGIC(joystick, false);15451546if (axis >= joystick->naxes) {1547SDL_SetError("Joystick only has %d axes", joystick->naxes);1548result = false;1549} else {1550if (state) {1551*state = joystick->axes[axis].initial_value;1552}1553result = joystick->axes[axis].has_initial_value;1554}1555}1556SDL_UnlockJoysticks();15571558return result;1559}15601561/*1562* Get the current state of a hat on a joystick1563*/1564Uint8 SDL_GetJoystickHat(SDL_Joystick *joystick, int hat)1565{1566Uint8 state;15671568SDL_LockJoysticks();1569{1570CHECK_JOYSTICK_MAGIC(joystick, 0);15711572if (hat < joystick->nhats) {1573state = joystick->hats[hat];1574} else {1575SDL_SetError("Joystick only has %d hats", joystick->nhats);1576state = 0;1577}1578}1579SDL_UnlockJoysticks();15801581return state;1582}15831584/*1585* Get the ball axis change since the last poll1586*/1587bool SDL_GetJoystickBall(SDL_Joystick *joystick, int ball, int *dx, int *dy)1588{1589bool result;15901591SDL_LockJoysticks();1592{1593CHECK_JOYSTICK_MAGIC(joystick, false);15941595if (ball < joystick->nballs) {1596if (dx) {1597*dx = joystick->balls[ball].dx;1598}1599if (dy) {1600*dy = joystick->balls[ball].dy;1601}1602joystick->balls[ball].dx = 0;1603joystick->balls[ball].dy = 0;1604result = true;1605} else {1606result = SDL_SetError("Joystick only has %d balls", joystick->nballs);1607}1608}1609SDL_UnlockJoysticks();16101611return result;1612}16131614/*1615* Get the current state of a button on a joystick1616*/1617bool SDL_GetJoystickButton(SDL_Joystick *joystick, int button)1618{1619bool down = false;16201621SDL_LockJoysticks();1622{1623CHECK_JOYSTICK_MAGIC(joystick, false);16241625if (button < joystick->nbuttons) {1626down = joystick->buttons[button];1627} else {1628SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);1629}1630}1631SDL_UnlockJoysticks();16321633return down;1634}16351636/*1637* Return if the joystick in question is currently attached to the system,1638* \return false if not plugged in, true if still present.1639*/1640bool SDL_JoystickConnected(SDL_Joystick *joystick)1641{1642bool result;16431644SDL_LockJoysticks();1645{1646CHECK_JOYSTICK_MAGIC(joystick, false);16471648result = joystick->attached;1649}1650SDL_UnlockJoysticks();16511652return result;1653}16541655/*1656* Get the instance id for this opened joystick1657*/1658SDL_JoystickID SDL_GetJoystickID(SDL_Joystick *joystick)1659{1660SDL_JoystickID result;16611662SDL_LockJoysticks();1663{1664CHECK_JOYSTICK_MAGIC(joystick, 0);16651666result = joystick->instance_id;1667}1668SDL_UnlockJoysticks();16691670return result;1671}16721673/*1674* Return the SDL_Joystick associated with an instance id.1675*/1676SDL_Joystick *SDL_GetJoystickFromID(SDL_JoystickID instance_id)1677{1678SDL_Joystick *joystick;16791680SDL_LockJoysticks();1681for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {1682if (joystick->instance_id == instance_id) {1683break;1684}1685}1686SDL_UnlockJoysticks();1687return joystick;1688}16891690/**1691* Return the SDL_Joystick associated with a player index.1692*/1693SDL_Joystick *SDL_GetJoystickFromPlayerIndex(int player_index)1694{1695SDL_JoystickID instance_id;1696SDL_Joystick *joystick;16971698SDL_LockJoysticks();1699instance_id = SDL_GetJoystickIDForPlayerIndex(player_index);1700for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {1701if (joystick->instance_id == instance_id) {1702break;1703}1704}1705SDL_UnlockJoysticks();1706return joystick;1707}17081709/*1710* Get the properties associated with a joystick1711*/1712SDL_PropertiesID SDL_GetJoystickProperties(SDL_Joystick *joystick)1713{1714SDL_PropertiesID result;17151716SDL_LockJoysticks();1717{1718CHECK_JOYSTICK_MAGIC(joystick, 0);17191720if (joystick->props == 0) {1721joystick->props = SDL_CreateProperties();1722}1723result = joystick->props;1724}1725SDL_UnlockJoysticks();17261727return result;1728}17291730/*1731* Get the friendly name of this joystick1732*/1733const char *SDL_GetJoystickName(SDL_Joystick *joystick)1734{1735const char *result;1736const SDL_SteamVirtualGamepadInfo *info;17371738SDL_LockJoysticks();1739{1740CHECK_JOYSTICK_MAGIC(joystick, NULL);17411742info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);1743if (info) {1744result = SDL_GetPersistentString(info->name);1745} else {1746result = SDL_GetPersistentString(joystick->name);1747}1748}1749SDL_UnlockJoysticks();17501751return result;1752}17531754/*1755* Get the implementation dependent path of this joystick1756*/1757const char *SDL_GetJoystickPath(SDL_Joystick *joystick)1758{1759const char *result;17601761SDL_LockJoysticks();1762{1763CHECK_JOYSTICK_MAGIC(joystick, NULL);17641765if (joystick->path) {1766result = SDL_GetPersistentString(joystick->path);1767} else {1768SDL_Unsupported();1769result = NULL;1770}1771}1772SDL_UnlockJoysticks();17731774return result;1775}17761777/**1778* Get the player index of an opened joystick, or -1 if it's not available1779*/1780int SDL_GetJoystickPlayerIndex(SDL_Joystick *joystick)1781{1782int result;17831784SDL_LockJoysticks();1785{1786CHECK_JOYSTICK_MAGIC(joystick, -1);17871788result = SDL_GetPlayerIndexForJoystickID(joystick->instance_id);1789}1790SDL_UnlockJoysticks();17911792return result;1793}17941795/**1796* Set the player index of an opened joystick1797*/1798bool SDL_SetJoystickPlayerIndex(SDL_Joystick *joystick, int player_index)1799{1800bool result;18011802SDL_LockJoysticks();1803{1804CHECK_JOYSTICK_MAGIC(joystick, false);18051806result = SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id);1807}1808SDL_UnlockJoysticks();18091810return result;1811}18121813bool SDL_RumbleJoystick(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)1814{1815bool result;18161817SDL_LockJoysticks();1818{1819CHECK_JOYSTICK_MAGIC(joystick, false);18201821if (low_frequency_rumble == joystick->low_frequency_rumble &&1822high_frequency_rumble == joystick->high_frequency_rumble) {1823// Just update the expiration1824result = true;1825} else {1826result = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble);1827if (result) {1828joystick->rumble_resend = SDL_GetTicks() + SDL_RUMBLE_RESEND_MS;1829if (joystick->rumble_resend == 0) {1830joystick->rumble_resend = 1;1831}1832} else {1833joystick->rumble_resend = 0;1834}1835}18361837if (result) {1838joystick->low_frequency_rumble = low_frequency_rumble;1839joystick->high_frequency_rumble = high_frequency_rumble;18401841if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {1842joystick->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);1843if (!joystick->rumble_expiration) {1844joystick->rumble_expiration = 1;1845}1846} else {1847joystick->rumble_expiration = 0;1848joystick->rumble_resend = 0;1849}1850}1851}1852SDL_UnlockJoysticks();18531854return result;1855}18561857bool SDL_RumbleJoystickTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)1858{1859bool result;18601861SDL_LockJoysticks();1862{1863CHECK_JOYSTICK_MAGIC(joystick, false);18641865if (left_rumble == joystick->left_trigger_rumble && right_rumble == joystick->right_trigger_rumble) {1866// Just update the expiration1867result = true;1868} else {1869result = joystick->driver->RumbleTriggers(joystick, left_rumble, right_rumble);1870if (result) {1871joystick->trigger_rumble_resend = SDL_GetTicks() + SDL_RUMBLE_RESEND_MS;1872if (joystick->trigger_rumble_resend == 0) {1873joystick->trigger_rumble_resend = 1;1874}1875} else {1876joystick->trigger_rumble_resend = 0;1877}1878}18791880if (result) {1881joystick->left_trigger_rumble = left_rumble;1882joystick->right_trigger_rumble = right_rumble;18831884if ((left_rumble || right_rumble) && duration_ms) {1885joystick->trigger_rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);1886} else {1887joystick->trigger_rumble_expiration = 0;1888joystick->trigger_rumble_resend = 0;1889}1890}1891}1892SDL_UnlockJoysticks();18931894return result;1895}18961897bool SDL_SetJoystickLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)1898{1899bool result;1900bool isfreshvalue;19011902SDL_LockJoysticks();1903{1904CHECK_JOYSTICK_MAGIC(joystick, false);19051906isfreshvalue = red != joystick->led_red ||1907green != joystick->led_green ||1908blue != joystick->led_blue;19091910if (isfreshvalue || SDL_GetTicks() >= joystick->led_expiration) {1911result = joystick->driver->SetLED(joystick, red, green, blue);1912joystick->led_expiration = SDL_GetTicks() + SDL_LED_MIN_REPEAT_MS;1913} else {1914// Avoid spamming the driver1915result = true;1916}19171918// Save the LED value regardless of success, so we don't spam the driver1919joystick->led_red = red;1920joystick->led_green = green;1921joystick->led_blue = blue;1922}1923SDL_UnlockJoysticks();19241925return result;1926}19271928bool SDL_SendJoystickEffect(SDL_Joystick *joystick, const void *data, int size)1929{1930bool result;19311932SDL_LockJoysticks();1933{1934CHECK_JOYSTICK_MAGIC(joystick, false);19351936result = joystick->driver->SendEffect(joystick, data, size);1937}1938SDL_UnlockJoysticks();19391940return result;1941}19421943/*1944* Close a joystick previously opened with SDL_OpenJoystick()1945*/1946void SDL_CloseJoystick(SDL_Joystick *joystick)1947{1948SDL_Joystick *joysticklist;1949SDL_Joystick *joysticklistprev;1950int i;19511952SDL_LockJoysticks();1953{1954CHECK_JOYSTICK_MAGIC(joystick,);19551956// First decrement ref count1957if (--joystick->ref_count > 0) {1958SDL_UnlockJoysticks();1959return;1960}19611962SDL_DestroyProperties(joystick->props);19631964if (joystick->rumble_expiration) {1965SDL_RumbleJoystick(joystick, 0, 0, 0);1966}1967if (joystick->trigger_rumble_expiration) {1968SDL_RumbleJoystickTriggers(joystick, 0, 0, 0);1969}19701971CleanupSensorFusion(joystick);19721973joystick->driver->Close(joystick);1974joystick->hwdata = NULL;1975SDL_SetObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK, false);19761977joysticklist = SDL_joysticks;1978joysticklistprev = NULL;1979while (joysticklist) {1980if (joystick == joysticklist) {1981if (joysticklistprev) {1982// unlink this entry1983joysticklistprev->next = joysticklist->next;1984} else {1985SDL_joysticks = joystick->next;1986}1987break;1988}1989joysticklistprev = joysticklist;1990joysticklist = joysticklist->next;1991}19921993// Free the data associated with this joystick1994SDL_free(joystick->name);1995SDL_free(joystick->path);1996SDL_free(joystick->serial);1997SDL_free(joystick->axes);1998SDL_free(joystick->balls);1999SDL_free(joystick->hats);2000SDL_free(joystick->buttons);2001for (i = 0; i < joystick->ntouchpads; i++) {2002SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];2003SDL_free(touchpad->fingers);2004}2005SDL_free(joystick->touchpads);2006SDL_free(joystick->sensors);2007SDL_free(joystick);2008}2009SDL_UnlockJoysticks();2010}20112012void SDL_QuitJoysticks(void)2013{2014int i;2015SDL_JoystickID *joysticks;20162017SDL_LockJoysticks();20182019SDL_joysticks_quitting = true;20202021joysticks = SDL_GetJoysticks(NULL);2022if (joysticks) {2023for (i = 0; joysticks[i]; ++i) {2024SDL_PrivateJoystickRemoved(joysticks[i]);2025}2026SDL_free(joysticks);2027}20282029while (SDL_joysticks) {2030SDL_joysticks->ref_count = 1;2031SDL_CloseJoystick(SDL_joysticks);2032}20332034// Quit drivers in reverse order to avoid breaking dependencies between drivers2035for (i = SDL_arraysize(SDL_joystick_drivers) - 1; i >= 0; --i) {2036SDL_joystick_drivers[i]->Quit();2037}20382039if (SDL_joystick_players) {2040SDL_free(SDL_joystick_players);2041SDL_joystick_players = NULL;2042SDL_joystick_player_count = 0;2043}20442045SDL_QuitSubSystem(SDL_INIT_EVENTS);20462047SDL_QuitSteamVirtualGamepadInfo();20482049SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,2050SDL_JoystickAllowBackgroundEventsChanged, NULL);20512052SDL_FreeVIDPIDList(&arcadestick_devices);2053SDL_FreeVIDPIDList(&blacklist_devices);2054SDL_FreeVIDPIDList(&flightstick_devices);2055SDL_FreeVIDPIDList(&gamecube_devices);2056SDL_FreeVIDPIDList(&rog_gamepad_mice);2057SDL_FreeVIDPIDList(&throttle_devices);2058SDL_FreeVIDPIDList(&wheel_devices);2059SDL_FreeVIDPIDList(&zero_centered_devices);20602061SDL_QuitGamepadMappings();20622063SDL_joysticks_quitting = false;2064SDL_joysticks_initialized = false;20652066SDL_UnlockJoysticks();2067}20682069static bool SDL_PrivateJoystickShouldIgnoreEvent(void)2070{2071if (SDL_joystick_allows_background_events) {2072return false;2073}2074return false;2075}20762077// These are global for SDL_sysjoystick.c and SDL_events.c20782079void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers)2080{2081int ntouchpads;2082SDL_JoystickTouchpadInfo *touchpads;20832084SDL_AssertJoysticksLocked();20852086ntouchpads = joystick->ntouchpads + 1;2087touchpads = (SDL_JoystickTouchpadInfo *)SDL_realloc(joystick->touchpads, (ntouchpads * sizeof(SDL_JoystickTouchpadInfo)));2088if (touchpads) {2089SDL_JoystickTouchpadInfo *touchpad = &touchpads[ntouchpads - 1];2090SDL_JoystickTouchpadFingerInfo *fingers = (SDL_JoystickTouchpadFingerInfo *)SDL_calloc(nfingers, sizeof(SDL_JoystickTouchpadFingerInfo));20912092if (fingers) {2093touchpad->nfingers = nfingers;2094touchpad->fingers = fingers;2095} else {2096// Out of memory, this touchpad won't be active2097touchpad->nfingers = 0;2098touchpad->fingers = NULL;2099}21002101joystick->ntouchpads = ntouchpads;2102joystick->touchpads = touchpads;2103}2104}21052106void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, float rate)2107{2108int nsensors;2109SDL_JoystickSensorInfo *sensors;21102111SDL_AssertJoysticksLocked();21122113nsensors = joystick->nsensors + 1;2114sensors = (SDL_JoystickSensorInfo *)SDL_realloc(joystick->sensors, (nsensors * sizeof(SDL_JoystickSensorInfo)));2115if (sensors) {2116SDL_JoystickSensorInfo *sensor = &sensors[nsensors - 1];21172118SDL_zerop(sensor);2119sensor->type = type;2120sensor->rate = rate;21212122joystick->nsensors = nsensors;2123joystick->sensors = sensors;2124}2125}21262127void SDL_PrivateJoystickSensorRate(SDL_Joystick *joystick, SDL_SensorType type, float rate)2128{2129int i;2130SDL_AssertJoysticksLocked();21312132for (i = 0; i < joystick->nsensors; ++i) {2133if (joystick->sensors[i].type == type) {2134joystick->sensors[i].rate = rate;2135}2136}2137}21382139void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)2140{2141SDL_JoystickDriver *driver;2142int device_index;2143int player_index = -1;2144bool is_gamepad;21452146SDL_AssertJoysticksLocked();21472148if (SDL_JoysticksQuitting()) {2149return;2150}21512152SDL_joystick_being_added = true;21532154if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {2155player_index = driver->GetDeviceSteamVirtualGamepadSlot(device_index);2156if (player_index < 0) {2157player_index = driver->GetDevicePlayerIndex(device_index);2158}2159}2160if (player_index < 0 && SDL_IsGamepad(instance_id)) {2161player_index = SDL_FindFreePlayerIndex();2162}2163if (player_index >= 0) {2164SDL_SetJoystickIDForPlayerIndex(player_index, instance_id);2165}21662167{2168SDL_Event event;21692170event.type = SDL_EVENT_JOYSTICK_ADDED;2171event.common.timestamp = 0;21722173if (SDL_EventEnabled(event.type)) {2174event.jdevice.which = instance_id;2175SDL_PushEvent(&event);2176}2177}21782179// This might create an automatic gamepad mapping, so wait to send the event2180is_gamepad = SDL_IsGamepad(instance_id);21812182SDL_joystick_being_added = false;21832184if (is_gamepad) {2185SDL_PrivateGamepadAdded(instance_id);2186}2187}21882189bool SDL_IsJoystickBeingAdded(void)2190{2191return SDL_joystick_being_added;2192}21932194void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)2195{2196Uint8 i, j;2197Uint64 timestamp = SDL_GetTicksNS();21982199SDL_AssertJoysticksLocked();22002201// Tell the app that everything is centered/unpressed...2202for (i = 0; i < joystick->naxes; i++) {2203if (joystick->axes[i].has_initial_value) {2204SDL_SendJoystickAxis(timestamp, joystick, i, joystick->axes[i].zero);2205}2206}22072208for (i = 0; i < joystick->nbuttons; i++) {2209SDL_SendJoystickButton(timestamp, joystick, i, false);2210}22112212for (i = 0; i < joystick->nhats; i++) {2213SDL_SendJoystickHat(timestamp, joystick, i, SDL_HAT_CENTERED);2214}22152216for (i = 0; i < joystick->ntouchpads; i++) {2217SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];22182219for (j = 0; j < touchpad->nfingers; ++j) {2220SDL_SendJoystickTouchpad(timestamp, joystick, i, j, false, 0.0f, 0.0f, 0.0f);2221}2222}2223}22242225void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id)2226{2227SDL_Joystick *joystick = NULL;2228int player_index;2229SDL_Event event;22302231SDL_AssertJoysticksLocked();22322233// Find this joystick...2234for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {2235if (joystick->instance_id == instance_id) {2236SDL_PrivateJoystickForceRecentering(joystick);2237joystick->attached = false;2238break;2239}2240}22412242if (SDL_IsGamepad(instance_id)) {2243SDL_PrivateGamepadRemoved(instance_id);2244}22452246event.type = SDL_EVENT_JOYSTICK_REMOVED;2247event.common.timestamp = 0;22482249if (SDL_EventEnabled(event.type)) {2250event.jdevice.which = instance_id;2251SDL_PushEvent(&event);2252}22532254player_index = SDL_GetPlayerIndexForJoystickID(instance_id);2255if (player_index >= 0) {2256SDL_joystick_players[player_index] = 0;2257}2258}22592260void SDL_SendJoystickAxis(Uint64 timestamp, SDL_Joystick *joystick, Uint8 axis, Sint16 value)2261{2262SDL_JoystickAxisInfo *info;22632264SDL_AssertJoysticksLocked();22652266// Make sure we're not getting garbage or duplicate events2267if (axis >= joystick->naxes) {2268return;2269}22702271info = &joystick->axes[axis];2272if (!info->has_initial_value ||2273(!info->has_second_value && (info->initial_value <= -32767 || info->initial_value == 32767) && SDL_abs(value) < (SDL_JOYSTICK_AXIS_MAX / 4))) {2274info->initial_value = value;2275info->value = value;2276info->zero = value;2277info->has_initial_value = true;2278} else if (value == info->value && !info->sending_initial_value) {2279return;2280} else {2281info->has_second_value = true;2282}2283if (!info->sent_initial_value) {2284// Make sure we don't send motion until there's real activity on this axis2285const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; // ShanWan PS3 controller needed 962286if (SDL_abs(value - info->value) <= MAX_ALLOWED_JITTER &&2287!SDL_IsJoystickVIRTUAL(joystick->guid)) {2288return;2289}2290info->sent_initial_value = true;2291info->sending_initial_value = true;2292SDL_SendJoystickAxis(timestamp, joystick, axis, info->initial_value);2293info->sending_initial_value = false;2294}22952296/* We ignore events if we don't have keyboard focus, except for centering2297* events.2298*/2299if (SDL_PrivateJoystickShouldIgnoreEvent()) {2300if (info->sending_initial_value ||2301(value > info->zero && value >= info->value) ||2302(value < info->zero && value <= info->value)) {2303return;2304}2305}23062307// Update internal joystick state2308SDL_assert(timestamp != 0);2309info->value = value;2310joystick->update_complete = timestamp;23112312// Post the event, if desired2313if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_AXIS_MOTION)) {2314SDL_Event event;2315event.type = SDL_EVENT_JOYSTICK_AXIS_MOTION;2316event.common.timestamp = timestamp;2317event.jaxis.which = joystick->instance_id;2318event.jaxis.axis = axis;2319event.jaxis.value = value;2320SDL_PushEvent(&event);2321}2322}23232324void SDL_SendJoystickBall(Uint64 timestamp, SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)2325{2326SDL_AssertJoysticksLocked();23272328// Make sure we're not getting garbage events2329if (ball >= joystick->nballs) {2330return;2331}23322333// We ignore events if we don't have keyboard focus.2334if (SDL_PrivateJoystickShouldIgnoreEvent()) {2335return;2336}23372338// Update internal mouse state2339joystick->balls[ball].dx += xrel;2340joystick->balls[ball].dy += yrel;23412342// Post the event, if desired2343if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_BALL_MOTION)) {2344SDL_Event event;2345event.type = SDL_EVENT_JOYSTICK_BALL_MOTION;2346event.common.timestamp = timestamp;2347event.jball.which = joystick->instance_id;2348event.jball.ball = ball;2349event.jball.xrel = xrel;2350event.jball.yrel = yrel;2351SDL_PushEvent(&event);2352}2353}23542355void SDL_SendJoystickHat(Uint64 timestamp, SDL_Joystick *joystick, Uint8 hat, Uint8 value)2356{2357SDL_AssertJoysticksLocked();23582359// Make sure we're not getting garbage or duplicate events2360if (hat >= joystick->nhats) {2361return;2362}2363if (value == joystick->hats[hat]) {2364return;2365}23662367/* We ignore events if we don't have keyboard focus, except for centering2368* events.2369*/2370if (SDL_PrivateJoystickShouldIgnoreEvent()) {2371if (value != SDL_HAT_CENTERED) {2372return;2373}2374}23752376// Update internal joystick state2377SDL_assert(timestamp != 0);2378joystick->hats[hat] = value;2379joystick->update_complete = timestamp;23802381// Post the event, if desired2382if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_HAT_MOTION)) {2383SDL_Event event;2384event.type = SDL_EVENT_JOYSTICK_HAT_MOTION;2385event.common.timestamp = timestamp;2386event.jhat.which = joystick->instance_id;2387event.jhat.hat = hat;2388event.jhat.value = value;2389SDL_PushEvent(&event);2390}2391}23922393void SDL_SendJoystickButton(Uint64 timestamp, SDL_Joystick *joystick, Uint8 button, bool down)2394{2395SDL_Event event;23962397SDL_AssertJoysticksLocked();23982399if (down) {2400event.type = SDL_EVENT_JOYSTICK_BUTTON_DOWN;2401} else {2402event.type = SDL_EVENT_JOYSTICK_BUTTON_UP;2403}24042405if (joystick->swap_face_buttons) {2406switch (button) {2407case 0:2408button = 1;2409break;2410case 1:2411button = 0;2412break;2413case 2:2414button = 3;2415break;2416case 3:2417button = 2;2418break;2419default:2420break;2421}2422}24232424// Make sure we're not getting garbage or duplicate events2425if (button >= joystick->nbuttons) {2426return;2427}2428if (down == joystick->buttons[button]) {2429return;2430}24312432/* We ignore events if we don't have keyboard focus, except for button2433* release. */2434if (SDL_PrivateJoystickShouldIgnoreEvent()) {2435if (down) {2436return;2437}2438}24392440// Update internal joystick state2441SDL_assert(timestamp != 0);2442joystick->buttons[button] = down;2443joystick->update_complete = timestamp;24442445// Post the event, if desired2446if (SDL_EventEnabled(event.type)) {2447event.common.timestamp = timestamp;2448event.jbutton.which = joystick->instance_id;2449event.jbutton.button = button;2450event.jbutton.down = down;2451SDL_PushEvent(&event);2452}2453}24542455static void SendSteamHandleUpdateEvents(void)2456{2457SDL_Joystick *joystick;2458const SDL_SteamVirtualGamepadInfo *info;24592460// Check to see if any Steam handles changed2461for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {2462bool changed = false;24632464if (!SDL_IsGamepad(joystick->instance_id)) {2465continue;2466}24672468info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);2469if (info) {2470if (joystick->steam_handle != info->handle) {2471joystick->steam_handle = info->handle;2472joystick->swap_face_buttons = ShouldSwapFaceButtons(info);2473changed = true;2474}2475} else {2476if (joystick->steam_handle != 0) {2477joystick->steam_handle = 0;2478joystick->swap_face_buttons = false;2479changed = true;2480}2481}2482if (changed) {2483SDL_Event event;24842485SDL_zero(event);2486event.type = SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED;2487event.common.timestamp = 0;2488event.gdevice.which = joystick->instance_id;2489SDL_PushEvent(&event);2490}2491}2492}24932494void SDL_UpdateJoysticks(void)2495{2496int i;2497Uint64 now;2498SDL_Joystick *joystick;24992500if (!SDL_joysticks_initialized) {2501return;2502}25032504SDL_LockJoysticks();25052506if (SDL_UpdateSteamVirtualGamepadInfo()) {2507SendSteamHandleUpdateEvents();2508}25092510#ifdef SDL_JOYSTICK_HIDAPI2511// Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks2512HIDAPI_UpdateDevices();2513#endif // SDL_JOYSTICK_HIDAPI25142515for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {2516if (!joystick->attached) {2517continue;2518}25192520joystick->driver->Update(joystick);25212522if (joystick->delayed_guide_button) {2523SDL_GamepadHandleDelayedGuideButton(joystick);2524}25252526now = SDL_GetTicks();2527if (joystick->rumble_expiration && now >= joystick->rumble_expiration) {2528SDL_RumbleJoystick(joystick, 0, 0, 0);2529joystick->rumble_resend = 0;2530}25312532if (joystick->rumble_resend && now >= joystick->rumble_resend) {2533joystick->driver->Rumble(joystick, joystick->low_frequency_rumble, joystick->high_frequency_rumble);2534joystick->rumble_resend = now + SDL_RUMBLE_RESEND_MS;2535if (joystick->rumble_resend == 0) {2536joystick->rumble_resend = 1;2537}2538}25392540if (joystick->trigger_rumble_expiration && now >= joystick->trigger_rumble_expiration) {2541SDL_RumbleJoystickTriggers(joystick, 0, 0, 0);2542joystick->trigger_rumble_resend = 0;2543}25442545if (joystick->trigger_rumble_resend && now >= joystick->trigger_rumble_resend) {2546joystick->driver->RumbleTriggers(joystick, joystick->left_trigger_rumble, joystick->right_trigger_rumble);2547joystick->trigger_rumble_resend = now + SDL_RUMBLE_RESEND_MS;2548if (joystick->trigger_rumble_resend == 0) {2549joystick->trigger_rumble_resend = 1;2550}2551}2552}25532554if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE)) {2555for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {2556if (joystick->update_complete) {2557SDL_Event event;25582559event.type = SDL_EVENT_JOYSTICK_UPDATE_COMPLETE;2560event.common.timestamp = joystick->update_complete;2561event.jdevice.which = joystick->instance_id;2562SDL_PushEvent(&event);25632564joystick->update_complete = 0;2565}2566}2567}25682569/* this needs to happen AFTER walking the joystick list above, so that any2570dangling hardware data from removed devices can be free'd2571*/2572for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {2573SDL_joystick_drivers[i]->Detect();2574}25752576SDL_UnlockJoysticks();2577}25782579static const Uint32 SDL_joystick_event_list[] = {2580SDL_EVENT_JOYSTICK_AXIS_MOTION,2581SDL_EVENT_JOYSTICK_BALL_MOTION,2582SDL_EVENT_JOYSTICK_HAT_MOTION,2583SDL_EVENT_JOYSTICK_BUTTON_DOWN,2584SDL_EVENT_JOYSTICK_BUTTON_UP,2585SDL_EVENT_JOYSTICK_ADDED,2586SDL_EVENT_JOYSTICK_REMOVED,2587SDL_EVENT_JOYSTICK_BATTERY_UPDATED2588};25892590void SDL_SetJoystickEventsEnabled(bool enabled)2591{2592unsigned int i;25932594for (i = 0; i < SDL_arraysize(SDL_joystick_event_list); ++i) {2595SDL_SetEventEnabled(SDL_joystick_event_list[i], enabled);2596}2597}25982599bool SDL_JoystickEventsEnabled(void)2600{2601bool enabled = false;2602unsigned int i;26032604for (i = 0; i < SDL_arraysize(SDL_joystick_event_list); ++i) {2605enabled = SDL_EventEnabled(SDL_joystick_event_list[i]);2606if (enabled) {2607break;2608}2609}2610return enabled;2611}26122613void SDL_GetJoystickGUIDInfo(SDL_GUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version, Uint16 *crc16)2614{2615Uint16 *guid16 = (Uint16 *)guid.data;2616Uint16 bus = SDL_Swap16LE(guid16[0]);26172618if ((bus < ' ' || bus == SDL_HARDWARE_BUS_VIRTUAL) && guid16[3] == 0x0000 && guid16[5] == 0x0000) {2619/* This GUID fits the standard form:2620* 16-bit bus2621* 16-bit CRC16 of the joystick name (can be zero)2622* 16-bit vendor ID2623* 16-bit zero2624* 16-bit product ID2625* 16-bit zero2626* 16-bit version2627* 8-bit driver identifier ('h' for HIDAPI, 'x' for XInput, etc.)2628* 8-bit driver-dependent type info2629*/2630if (vendor) {2631*vendor = SDL_Swap16LE(guid16[2]);2632}2633if (product) {2634*product = SDL_Swap16LE(guid16[4]);2635}2636if (version) {2637*version = SDL_Swap16LE(guid16[6]);2638}2639if (crc16) {2640*crc16 = SDL_Swap16LE(guid16[1]);2641}2642} else if (bus < ' ' || bus == SDL_HARDWARE_BUS_VIRTUAL) {2643/* This GUID fits the unknown VID/PID form:2644* 16-bit bus2645* 16-bit CRC16 of the joystick name (can be zero)2646* 11 characters of the joystick name, null terminated2647*/2648if (vendor) {2649*vendor = 0;2650}2651if (product) {2652*product = 0;2653}2654if (version) {2655*version = 0;2656}2657if (crc16) {2658*crc16 = SDL_Swap16LE(guid16[1]);2659}2660} else {2661if (vendor) {2662*vendor = 0;2663}2664if (product) {2665*product = 0;2666}2667if (version) {2668*version = 0;2669}2670if (crc16) {2671*crc16 = 0;2672}2673}2674}26752676char *SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name)2677{2678const char *custom_name = GuessControllerName(vendor, product);2679if (custom_name) {2680return SDL_strdup(custom_name);2681}26822683return SDL_CreateDeviceName(vendor, product, vendor_name, product_name, "Controller");2684}26852686SDL_GUID SDL_CreateJoystickGUID(Uint16 bus, Uint16 vendor, Uint16 product, Uint16 version, const char *vendor_name, const char *product_name, Uint8 driver_signature, Uint8 driver_data)2687{2688SDL_GUID guid;2689Uint16 *guid16 = (Uint16 *)guid.data;2690Uint16 crc = 0;26912692SDL_zero(guid);26932694if (vendor_name && *vendor_name && product_name && *product_name) {2695crc = SDL_crc16(crc, vendor_name, SDL_strlen(vendor_name));2696crc = SDL_crc16(crc, " ", 1);2697crc = SDL_crc16(crc, product_name, SDL_strlen(product_name));2698} else if (product_name) {2699crc = SDL_crc16(crc, product_name, SDL_strlen(product_name));2700}27012702// We only need 16 bits for each of these; space them out to fill 128.2703// Byteswap so devices get same GUID on little/big endian platforms.2704*guid16++ = SDL_Swap16LE(bus);2705*guid16++ = SDL_Swap16LE(crc);27062707if (vendor) {2708*guid16++ = SDL_Swap16LE(vendor);2709*guid16++ = 0;2710*guid16++ = SDL_Swap16LE(product);2711*guid16++ = 0;2712*guid16++ = SDL_Swap16LE(version);2713guid.data[14] = driver_signature;2714guid.data[15] = driver_data;2715} else {2716size_t available_space = sizeof(guid.data) - 4;27172718if (driver_signature) {2719available_space -= 2;2720guid.data[14] = driver_signature;2721guid.data[15] = driver_data;2722}2723if (product_name) {2724SDL_strlcpy((char *)guid16, product_name, available_space);2725}2726}2727return guid;2728}27292730SDL_GUID SDL_CreateJoystickGUIDForName(const char *name)2731{2732return SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_UNKNOWN, 0, 0, 0, NULL, name, 0, 0);2733}27342735void SDL_SetJoystickGUIDVendor(SDL_GUID *guid, Uint16 vendor)2736{2737Uint16 *guid16 = (Uint16 *)guid->data;27382739guid16[2] = SDL_Swap16LE(vendor);2740}27412742void SDL_SetJoystickGUIDProduct(SDL_GUID *guid, Uint16 product)2743{2744Uint16 *guid16 = (Uint16 *)guid->data;27452746guid16[4] = SDL_Swap16LE(product);2747}27482749void SDL_SetJoystickGUIDVersion(SDL_GUID *guid, Uint16 version)2750{2751Uint16 *guid16 = (Uint16 *)guid->data;27522753guid16[6] = SDL_Swap16LE(version);2754}27552756void SDL_SetJoystickGUIDCRC(SDL_GUID *guid, Uint16 crc)2757{2758Uint16 *guid16 = (Uint16 *)guid->data;27592760guid16[1] = SDL_Swap16LE(crc);2761}27622763SDL_GamepadType SDL_GetGamepadTypeFromVIDPID(Uint16 vendor, Uint16 product, const char *name, bool forUI)2764{2765SDL_GamepadType type = SDL_GAMEPAD_TYPE_STANDARD;27662767if (vendor == 0x0000 && product == 0x0000) {2768// Some devices are only identifiable by their name2769if (name &&2770(SDL_strcmp(name, "Lic Pro Controller") == 0 ||2771SDL_strcmp(name, "Nintendo Wireless Gamepad") == 0 ||2772SDL_strcmp(name, "Wireless Gamepad") == 0)) {2773// HORI or PowerA Switch Pro Controller clone2774type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;2775}27762777} else if (vendor == 0x0001 && product == 0x0001) {2778type = SDL_GAMEPAD_TYPE_STANDARD;27792780} else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT) {2781type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT;27822783} else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT) {2784if (name && SDL_strstr(name, "NES Controller") != NULL) {2785// We don't have a type for the Nintendo Online NES Controller2786type = SDL_GAMEPAD_TYPE_STANDARD;2787} else {2788type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT;2789}27902791} else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {2792if (name && SDL_strstr(name, "(L)") != NULL) {2793type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT;2794} else {2795type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT;2796}27972798} else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR) {2799type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR;28002801} else if (forUI && SDL_IsJoystickGameCube(vendor, product)) {2802// We don't have a type for the Nintendo GameCube controller2803type = SDL_GAMEPAD_TYPE_STANDARD;28042805} else {2806switch (GuessControllerType(vendor, product)) {2807case k_eControllerType_XBox360Controller:2808type = SDL_GAMEPAD_TYPE_XBOX360;2809break;2810case k_eControllerType_XBoxOneController:2811type = SDL_GAMEPAD_TYPE_XBOXONE;2812break;2813case k_eControllerType_PS3Controller:2814type = SDL_GAMEPAD_TYPE_PS3;2815break;2816case k_eControllerType_PS4Controller:2817type = SDL_GAMEPAD_TYPE_PS4;2818break;2819case k_eControllerType_PS5Controller:2820type = SDL_GAMEPAD_TYPE_PS5;2821break;2822case k_eControllerType_XInputPS4Controller:2823if (forUI) {2824type = SDL_GAMEPAD_TYPE_PS4;2825} else {2826type = SDL_GAMEPAD_TYPE_STANDARD;2827}2828break;2829case k_eControllerType_SwitchProController:2830case k_eControllerType_SwitchInputOnlyController:2831type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;2832break;2833case k_eControllerType_XInputSwitchController:2834if (forUI) {2835type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;2836} else {2837type = SDL_GAMEPAD_TYPE_STANDARD;2838}2839break;2840default:2841break;2842}2843}2844return type;2845}28462847SDL_GamepadType SDL_GetGamepadTypeFromGUID(SDL_GUID guid, const char *name)2848{2849SDL_GamepadType type;2850Uint16 vendor, product;28512852SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);2853type = SDL_GetGamepadTypeFromVIDPID(vendor, product, name, true);2854if (type == SDL_GAMEPAD_TYPE_STANDARD) {2855if (SDL_IsJoystickXInput(guid)) {2856// This is probably an Xbox One controller2857return SDL_GAMEPAD_TYPE_XBOXONE;2858}2859#ifdef SDL_JOYSTICK_HIDAPI2860if (SDL_IsJoystickHIDAPI(guid)) {2861return HIDAPI_GetGamepadTypeFromGUID(guid);2862}2863#endif // SDL_JOYSTICK_HIDAPI2864}2865return type;2866}28672868bool SDL_JoystickGUIDUsesVersion(SDL_GUID guid)2869{2870Uint16 vendor, product;28712872if (SDL_IsJoystickMFI(guid)) {2873// The version bits are used as button capability mask2874return false;2875}28762877SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);2878if (vendor && product) {2879return true;2880}2881return false;2882}28832884bool SDL_IsJoystickXboxOne(Uint16 vendor_id, Uint16 product_id)2885{2886EControllerType eType = GuessControllerType(vendor_id, product_id);2887return eType == k_eControllerType_XBoxOneController;2888}28892890bool SDL_IsJoystickXboxOneElite(Uint16 vendor_id, Uint16 product_id)2891{2892if (vendor_id == USB_VENDOR_MICROSOFT) {2893if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 ||2894product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2 ||2895product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH ||2896product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLE) {2897return true;2898}2899}2900return false;2901}29022903bool SDL_IsJoystickXboxSeriesX(Uint16 vendor_id, Uint16 product_id)2904{2905if (vendor_id == USB_VENDOR_MICROSOFT) {2906if (product_id == USB_PRODUCT_XBOX_SERIES_X ||2907product_id == USB_PRODUCT_XBOX_SERIES_X_BLE) {2908return true;2909}2910}2911if (vendor_id == USB_VENDOR_PDP) {2912if (product_id == USB_PRODUCT_XBOX_SERIES_X_VICTRIX_GAMBIT ||2913product_id == USB_PRODUCT_XBOX_SERIES_X_PDP_BLUE ||2914product_id == USB_PRODUCT_XBOX_SERIES_X_PDP_AFTERGLOW) {2915return true;2916}2917}2918if (vendor_id == USB_VENDOR_POWERA_ALT) {2919if ((product_id >= 0x2001 && product_id <= 0x201a) ||2920product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO2 ||2921product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO4 ||2922product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO_WIRELESS_USB ||2923product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO_WIRELESS_DONGLE ||2924product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_MOGA_XP_ULTRA ||2925product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_SPECTRA) {2926return true;2927}2928}2929if (vendor_id == USB_VENDOR_HORI) {2930if (product_id == USB_PRODUCT_HORI_FIGHTING_COMMANDER_OCTA_SERIES_X ||2931product_id == USB_PRODUCT_HORI_HORIPAD_PRO_SERIES_X ||2932product_id == USB_PRODUCT_HORI_TAIKO_DRUM_CONTROLLER) {2933return true;2934}2935}2936if (vendor_id == USB_VENDOR_HP) {2937if (product_id == USB_PRODUCT_XBOX_SERIES_X_HP_HYPERX ||2938product_id == USB_PRODUCT_XBOX_SERIES_X_HP_HYPERX_RGB) {2939return true;2940}2941}2942if (vendor_id == USB_VENDOR_RAZER) {2943if (product_id == USB_PRODUCT_RAZER_WOLVERINE_V2 ||2944product_id == USB_PRODUCT_RAZER_WOLVERINE_V2_CHROMA ||2945product_id == USB_PRODUCT_RAZER_WOLVERINE_V3_PRO) {2946return true;2947}2948}2949if (vendor_id == USB_VENDOR_THRUSTMASTER) {2950if (product_id == USB_PRODUCT_THRUSTMASTER_ESWAPX_PRO_SERIES_X) {2951return true;2952}2953}2954if (vendor_id == USB_VENDOR_TURTLE_BEACH) {2955if (product_id == USB_PRODUCT_TURTLE_BEACH_SERIES_X_REACT_R ||2956product_id == USB_PRODUCT_TURTLE_BEACH_SERIES_X_RECON) {2957return true;2958}2959}2960if (vendor_id == USB_VENDOR_8BITDO) {2961if (product_id == USB_PRODUCT_8BITDO_XBOX_CONTROLLER1 ||2962product_id == USB_PRODUCT_8BITDO_XBOX_CONTROLLER2) {2963return true;2964}2965}2966if (vendor_id == USB_VENDOR_GAMESIR) {2967if (product_id == USB_PRODUCT_GAMESIR_G7) {2968return true;2969}2970}2971if (vendor_id == USB_VENDOR_ASUS) {2972if (product_id == USB_PRODUCT_ROG_RAIKIRI) {2973return true;2974}2975}2976return false;2977}29782979bool SDL_IsJoystickBluetoothXboxOne(Uint16 vendor_id, Uint16 product_id)2980{2981if (vendor_id == USB_VENDOR_MICROSOFT) {2982if (product_id == USB_PRODUCT_XBOX_ONE_ADAPTIVE_BLUETOOTH ||2983product_id == USB_PRODUCT_XBOX_ONE_ADAPTIVE_BLE ||2984product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||2985product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||2986product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLE ||2987product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH ||2988product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLE ||2989product_id == USB_PRODUCT_XBOX_SERIES_X_BLE) {2990return true;2991}2992}2993return false;2994}29952996bool SDL_IsJoystickPS4(Uint16 vendor_id, Uint16 product_id)2997{2998EControllerType eType = GuessControllerType(vendor_id, product_id);2999return eType == k_eControllerType_PS4Controller;3000}30013002bool SDL_IsJoystickPS5(Uint16 vendor_id, Uint16 product_id)3003{3004EControllerType eType = GuessControllerType(vendor_id, product_id);3005return eType == k_eControllerType_PS5Controller;3006}30073008bool SDL_IsJoystickDualSenseEdge(Uint16 vendor_id, Uint16 product_id)3009{3010if (vendor_id == USB_VENDOR_SONY) {3011if (product_id == USB_PRODUCT_SONY_DS5_EDGE) {3012return true;3013}3014}3015return false;3016}30173018bool SDL_IsJoystickNintendoSwitchPro(Uint16 vendor_id, Uint16 product_id)3019{3020EControllerType eType = GuessControllerType(vendor_id, product_id);3021return eType == k_eControllerType_SwitchProController || eType == k_eControllerType_SwitchInputOnlyController;3022}30233024bool SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor_id, Uint16 product_id)3025{3026EControllerType eType = GuessControllerType(vendor_id, product_id);3027return eType == k_eControllerType_SwitchInputOnlyController;3028}30293030bool SDL_IsJoystickNintendoSwitchJoyCon(Uint16 vendor_id, Uint16 product_id)3031{3032EControllerType eType = GuessControllerType(vendor_id, product_id);3033return eType == k_eControllerType_SwitchJoyConLeft || eType == k_eControllerType_SwitchJoyConRight;3034}30353036bool SDL_IsJoystickNintendoSwitchJoyConLeft(Uint16 vendor_id, Uint16 product_id)3037{3038EControllerType eType = GuessControllerType(vendor_id, product_id);3039return eType == k_eControllerType_SwitchJoyConLeft;3040}30413042bool SDL_IsJoystickNintendoSwitchJoyConRight(Uint16 vendor_id, Uint16 product_id)3043{3044EControllerType eType = GuessControllerType(vendor_id, product_id);3045return eType == k_eControllerType_SwitchJoyConRight;3046}30473048bool SDL_IsJoystickNintendoSwitchJoyConGrip(Uint16 vendor_id, Uint16 product_id)3049{3050return vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP;3051}30523053bool SDL_IsJoystickNintendoSwitchJoyConPair(Uint16 vendor_id, Uint16 product_id)3054{3055return vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR;3056}30573058bool SDL_IsJoystickGameCube(Uint16 vendor_id, Uint16 product_id)3059{3060return SDL_VIDPIDInList(vendor_id, product_id, &gamecube_devices);3061}30623063bool SDL_IsJoystickAmazonLunaController(Uint16 vendor_id, Uint16 product_id)3064{3065return ((vendor_id == USB_VENDOR_AMAZON && product_id == USB_PRODUCT_AMAZON_LUNA_CONTROLLER) ||3066(vendor_id == BLUETOOTH_VENDOR_AMAZON && product_id == BLUETOOTH_PRODUCT_LUNA_CONTROLLER));3067}30683069bool SDL_IsJoystickGoogleStadiaController(Uint16 vendor_id, Uint16 product_id)3070{3071return vendor_id == USB_VENDOR_GOOGLE && product_id == USB_PRODUCT_GOOGLE_STADIA_CONTROLLER;3072}30733074bool SDL_IsJoystickNVIDIASHIELDController(Uint16 vendor_id, Uint16 product_id)3075{3076return (vendor_id == USB_VENDOR_NVIDIA &&3077(product_id == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V103 ||3078product_id == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104));3079}30803081bool SDL_IsJoystickSteamVirtualGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version)3082{3083#ifdef SDL_PLATFORM_MACOS3084return (vendor_id == USB_VENDOR_MICROSOFT && product_id == USB_PRODUCT_XBOX360_WIRED_CONTROLLER && version == 0);3085#else3086return (vendor_id == USB_VENDOR_VALVE && product_id == USB_PRODUCT_STEAM_VIRTUAL_GAMEPAD);3087#endif3088}30893090bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id)3091{3092EControllerType eType = GuessControllerType(vendor_id, product_id);3093return eType == k_eControllerType_SteamController || eType == k_eControllerType_SteamControllerV2;3094}30953096bool SDL_IsJoystickHoriSteamController(Uint16 vendor_id, Uint16 product_id)3097{3098return vendor_id == USB_VENDOR_HORI && (product_id == USB_PRODUCT_HORI_STEAM_CONTROLLER || product_id == USB_PRODUCT_HORI_STEAM_CONTROLLER_BT);3099}31003101bool SDL_IsJoystickSteamDeck(Uint16 vendor_id, Uint16 product_id)3102{3103EControllerType eType = GuessControllerType(vendor_id, product_id);3104return eType == k_eControllerType_SteamControllerNeptune;3105}31063107bool SDL_IsJoystickXInput(SDL_GUID guid)3108{3109return (guid.data[14] == 'x') ? true : false;3110}31113112bool SDL_IsJoystickWGI(SDL_GUID guid)3113{3114return (guid.data[14] == 'w') ? true : false;3115}31163117bool SDL_IsJoystickHIDAPI(SDL_GUID guid)3118{3119return (guid.data[14] == 'h') ? true : false;3120}31213122bool SDL_IsJoystickMFI(SDL_GUID guid)3123{3124return (guid.data[14] == 'm') ? true : false;3125}31263127bool SDL_IsJoystickRAWINPUT(SDL_GUID guid)3128{3129return (guid.data[14] == 'r') ? true : false;3130}31313132bool SDL_IsJoystickVIRTUAL(SDL_GUID guid)3133{3134return (guid.data[14] == 'v') ? true : false;3135}31363137static bool SDL_IsJoystickWheel(Uint16 vendor_id, Uint16 product_id)3138{3139return SDL_VIDPIDInList(vendor_id, product_id, &wheel_devices);3140}31413142static bool SDL_IsJoystickArcadeStick(Uint16 vendor_id, Uint16 product_id)3143{3144return SDL_VIDPIDInList(vendor_id, product_id, &arcadestick_devices);3145}31463147static bool SDL_IsJoystickFlightStick(Uint16 vendor_id, Uint16 product_id)3148{3149return SDL_VIDPIDInList(vendor_id, product_id, &flightstick_devices);3150}31513152static bool SDL_IsJoystickThrottle(Uint16 vendor_id, Uint16 product_id)3153{3154return SDL_VIDPIDInList(vendor_id, product_id, &throttle_devices);3155}31563157static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_GUID guid)3158{3159Uint16 vendor;3160Uint16 product;31613162SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);31633164if (SDL_IsJoystickWheel(vendor, product)) {3165return SDL_JOYSTICK_TYPE_WHEEL;3166}31673168if (SDL_IsJoystickArcadeStick(vendor, product)) {3169return SDL_JOYSTICK_TYPE_ARCADE_STICK;3170}31713172if (SDL_IsJoystickFlightStick(vendor, product)) {3173return SDL_JOYSTICK_TYPE_FLIGHT_STICK;3174}31753176if (SDL_IsJoystickThrottle(vendor, product)) {3177return SDL_JOYSTICK_TYPE_THROTTLE;3178}31793180if (SDL_IsJoystickXInput(guid)) {3181// XInput GUID, get the type based on the XInput device subtype3182switch (guid.data[15]) {3183case 0x01: // XINPUT_DEVSUBTYPE_GAMEPAD3184return SDL_JOYSTICK_TYPE_GAMEPAD;3185case 0x02: // XINPUT_DEVSUBTYPE_WHEEL3186return SDL_JOYSTICK_TYPE_WHEEL;3187case 0x03: // XINPUT_DEVSUBTYPE_ARCADE_STICK3188return SDL_JOYSTICK_TYPE_ARCADE_STICK;3189case 0x04: // XINPUT_DEVSUBTYPE_FLIGHT_STICK3190return SDL_JOYSTICK_TYPE_FLIGHT_STICK;3191case 0x05: // XINPUT_DEVSUBTYPE_DANCE_PAD3192return SDL_JOYSTICK_TYPE_DANCE_PAD;3193case 0x06: // XINPUT_DEVSUBTYPE_GUITAR3194case 0x07: // XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE3195case 0x0B: // XINPUT_DEVSUBTYPE_GUITAR_BASS3196return SDL_JOYSTICK_TYPE_GUITAR;3197case 0x08: // XINPUT_DEVSUBTYPE_DRUM_KIT3198return SDL_JOYSTICK_TYPE_DRUM_KIT;3199case 0x13: // XINPUT_DEVSUBTYPE_ARCADE_PAD3200return SDL_JOYSTICK_TYPE_ARCADE_PAD;3201default:3202return SDL_JOYSTICK_TYPE_UNKNOWN;3203}3204}32053206if (SDL_IsJoystickWGI(guid)) {3207return (SDL_JoystickType)guid.data[15];3208}32093210if (SDL_IsJoystickVIRTUAL(guid)) {3211return (SDL_JoystickType)guid.data[15];3212}32133214#ifdef SDL_JOYSTICK_HIDAPI3215if (SDL_IsJoystickHIDAPI(guid)) {3216return HIDAPI_GetJoystickTypeFromGUID(guid);3217}3218#endif // SDL_JOYSTICK_HIDAPI32193220if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) {3221return SDL_JOYSTICK_TYPE_GAMEPAD;3222}32233224return SDL_JOYSTICK_TYPE_UNKNOWN;3225}32263227bool SDL_ShouldIgnoreJoystick(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)3228{3229// Check the joystick blacklist3230if (SDL_VIDPIDInList(vendor_id, product_id, &blacklist_devices)) {3231return true;3232}3233if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_ROG_CHAKRAM, false)) {3234if (SDL_VIDPIDInList(vendor_id, product_id, &rog_gamepad_mice)) {3235return true;3236}3237}32383239if (SDL_ShouldIgnoreGamepad(vendor_id, product_id, version, name)) {3240return true;3241}32423243return false;3244}32453246// return the guid for this index3247SDL_GUID SDL_GetJoystickGUIDForID(SDL_JoystickID instance_id)3248{3249SDL_JoystickDriver *driver;3250int device_index;3251SDL_GUID guid;32523253SDL_LockJoysticks();3254if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {3255guid = driver->GetDeviceGUID(device_index);3256} else {3257SDL_zero(guid);3258}3259SDL_UnlockJoysticks();32603261return guid;3262}32633264Uint16 SDL_GetJoystickVendorForID(SDL_JoystickID instance_id)3265{3266Uint16 vendor;3267const SDL_SteamVirtualGamepadInfo *info;32683269SDL_LockJoysticks();3270info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);3271if (info) {3272vendor = info->vendor_id;3273} else {3274SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);32753276SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL, NULL);3277}3278SDL_UnlockJoysticks();32793280return vendor;3281}32823283Uint16 SDL_GetJoystickProductForID(SDL_JoystickID instance_id)3284{3285Uint16 product;3286const SDL_SteamVirtualGamepadInfo *info;32873288SDL_LockJoysticks();3289info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);3290if (info) {3291product = info->product_id;3292} else {3293SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);32943295SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL, NULL);3296}3297SDL_UnlockJoysticks();32983299return product;3300}33013302Uint16 SDL_GetJoystickProductVersionForID(SDL_JoystickID instance_id)3303{3304Uint16 version;3305SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);33063307SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version, NULL);3308return version;3309}33103311SDL_JoystickType SDL_GetJoystickTypeForID(SDL_JoystickID instance_id)3312{3313SDL_JoystickType type;3314SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);33153316type = SDL_GetJoystickGUIDType(guid);3317if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {3318if (SDL_IsGamepad(instance_id)) {3319type = SDL_JOYSTICK_TYPE_GAMEPAD;3320}3321}3322return type;3323}33243325SDL_GUID SDL_GetJoystickGUID(SDL_Joystick *joystick)3326{3327SDL_GUID result;33283329SDL_LockJoysticks();3330{3331static SDL_GUID emptyGUID;33323333CHECK_JOYSTICK_MAGIC(joystick, emptyGUID);33343335result = joystick->guid;3336}3337SDL_UnlockJoysticks();33383339return result;3340}33413342Uint16 SDL_GetJoystickVendor(SDL_Joystick *joystick)3343{3344Uint16 vendor;3345const SDL_SteamVirtualGamepadInfo *info;33463347SDL_LockJoysticks();3348{3349CHECK_JOYSTICK_MAGIC(joystick, 0);33503351info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);3352if (info) {3353vendor = info->vendor_id;3354} else {3355SDL_GUID guid = SDL_GetJoystickGUID(joystick);33563357SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL, NULL);3358}3359}3360SDL_UnlockJoysticks();33613362return vendor;3363}33643365Uint16 SDL_GetJoystickProduct(SDL_Joystick *joystick)3366{3367Uint16 product;3368const SDL_SteamVirtualGamepadInfo *info;33693370SDL_LockJoysticks();3371{3372CHECK_JOYSTICK_MAGIC(joystick, 0);33733374info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);3375if (info) {3376product = info->product_id;3377} else {3378SDL_GUID guid = SDL_GetJoystickGUID(joystick);33793380SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL, NULL);3381}3382}3383SDL_UnlockJoysticks();33843385return product;3386}33873388Uint16 SDL_GetJoystickProductVersion(SDL_Joystick *joystick)3389{3390Uint16 version;3391SDL_GUID guid = SDL_GetJoystickGUID(joystick);33923393SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version, NULL);3394return version;3395}33963397Uint16 SDL_GetJoystickFirmwareVersion(SDL_Joystick *joystick)3398{3399Uint16 result;34003401SDL_LockJoysticks();3402{3403CHECK_JOYSTICK_MAGIC(joystick, 0);34043405result = joystick->firmware_version;3406}3407SDL_UnlockJoysticks();34083409return result;3410}34113412const char *SDL_GetJoystickSerial(SDL_Joystick *joystick)3413{3414const char *result;34153416SDL_LockJoysticks();3417{3418CHECK_JOYSTICK_MAGIC(joystick, NULL);34193420result = SDL_GetPersistentString(joystick->serial);3421}3422SDL_UnlockJoysticks();34233424return result;3425}34263427SDL_JoystickType SDL_GetJoystickType(SDL_Joystick *joystick)3428{3429SDL_JoystickType type;3430SDL_GUID guid = SDL_GetJoystickGUID(joystick);34313432type = SDL_GetJoystickGUIDType(guid);3433if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {3434SDL_LockJoysticks();3435{3436CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_TYPE_UNKNOWN);34373438if (SDL_IsGamepad(joystick->instance_id)) {3439type = SDL_JOYSTICK_TYPE_GAMEPAD;3440}3441}3442SDL_UnlockJoysticks();3443}3444return type;3445}34463447void SDL_SendJoystickPowerInfo(SDL_Joystick *joystick, SDL_PowerState state, int percent)3448{3449SDL_AssertJoysticksLocked();34503451if (state != joystick->battery_state || percent != joystick->battery_percent) {3452joystick->battery_state = state;3453joystick->battery_percent = percent;34543455if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_BATTERY_UPDATED)) {3456SDL_Event event;3457event.type = SDL_EVENT_JOYSTICK_BATTERY_UPDATED;3458event.common.timestamp = 0;3459event.jbattery.which = joystick->instance_id;3460event.jbattery.state = state;3461event.jbattery.percent = percent;3462SDL_PushEvent(&event);3463}3464}3465}34663467SDL_JoystickConnectionState SDL_GetJoystickConnectionState(SDL_Joystick *joystick)3468{3469SDL_JoystickConnectionState result;34703471SDL_LockJoysticks();3472{3473CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_CONNECTION_INVALID);34743475result = joystick->connection_state;3476}3477SDL_UnlockJoysticks();34783479return result;3480}34813482SDL_PowerState SDL_GetJoystickPowerInfo(SDL_Joystick *joystick, int *percent)3483{3484SDL_PowerState result;34853486if (percent) {3487*percent = -1;3488}34893490SDL_LockJoysticks();3491{3492CHECK_JOYSTICK_MAGIC(joystick, SDL_POWERSTATE_ERROR);34933494result = joystick->battery_state;34953496if (percent) {3497*percent = joystick->battery_percent;3498}3499}3500SDL_UnlockJoysticks();35013502return result;3503}35043505void SDL_SendJoystickTouchpad(Uint64 timestamp, SDL_Joystick *joystick, int touchpad, int finger, bool down, float x, float y, float pressure)3506{3507SDL_JoystickTouchpadInfo *touchpad_info;3508SDL_JoystickTouchpadFingerInfo *finger_info;3509Uint32 event_type;35103511SDL_AssertJoysticksLocked();35123513if (touchpad < 0 || touchpad >= joystick->ntouchpads) {3514return;3515}35163517touchpad_info = &joystick->touchpads[touchpad];3518if (finger < 0 || finger >= touchpad_info->nfingers) {3519return;3520}35213522finger_info = &touchpad_info->fingers[finger];35233524if (!down) {3525if (x == 0.0f && y == 0.0f) {3526x = finger_info->x;3527y = finger_info->y;3528}3529pressure = 0.0f;3530}35313532if (x < 0.0f) {3533x = 0.0f;3534} else if (x > 1.0f) {3535x = 1.0f;3536}3537if (y < 0.0f) {3538y = 0.0f;3539} else if (y > 1.0f) {3540y = 1.0f;3541}3542if (pressure < 0.0f) {3543pressure = 0.0f;3544} else if (pressure > 1.0f) {3545pressure = 1.0f;3546}35473548if (down == finger_info->down) {3549if (!down ||3550(x == finger_info->x && y == finger_info->y && pressure == finger_info->pressure)) {3551return;3552}3553}35543555if (down == finger_info->down) {3556event_type = SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION;3557} else if (down) {3558event_type = SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN;3559} else {3560event_type = SDL_EVENT_GAMEPAD_TOUCHPAD_UP;3561}35623563// We ignore events if we don't have keyboard focus, except for touch release3564if (SDL_PrivateJoystickShouldIgnoreEvent()) {3565if (event_type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP) {3566return;3567}3568}35693570// Update internal joystick state3571SDL_assert(timestamp != 0);3572finger_info->down = down;3573finger_info->x = x;3574finger_info->y = y;3575finger_info->pressure = pressure;3576joystick->update_complete = timestamp;35773578// Post the event, if desired3579if (SDL_EventEnabled(event_type)) {3580SDL_Event event;3581event.type = event_type;3582event.common.timestamp = timestamp;3583event.gtouchpad.which = joystick->instance_id;3584event.gtouchpad.touchpad = touchpad;3585event.gtouchpad.finger = finger;3586event.gtouchpad.x = x;3587event.gtouchpad.y = y;3588event.gtouchpad.pressure = pressure;3589SDL_PushEvent(&event);3590}3591}35923593void SDL_SendJoystickSensor(Uint64 timestamp, SDL_Joystick *joystick, SDL_SensorType type, Uint64 sensor_timestamp, const float *data, int num_values)3594{3595SDL_AssertJoysticksLocked();35963597// We ignore events if we don't have keyboard focus3598if (SDL_PrivateJoystickShouldIgnoreEvent()) {3599return;3600}36013602for (int i = 0; i < joystick->nsensors; ++i) {3603SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];36043605if (sensor->type == type) {3606if (sensor->enabled) {3607num_values = SDL_min(num_values, SDL_arraysize(sensor->data));36083609// Update internal sensor state3610SDL_memcpy(sensor->data, data, num_values * sizeof(*data));3611joystick->update_complete = timestamp;36123613// Post the event, if desired3614if (SDL_EventEnabled(SDL_EVENT_GAMEPAD_SENSOR_UPDATE)) {3615SDL_Event event;3616event.type = SDL_EVENT_GAMEPAD_SENSOR_UPDATE;3617event.common.timestamp = timestamp;3618event.gsensor.which = joystick->instance_id;3619event.gsensor.sensor = type;3620num_values = SDL_min(num_values,3621SDL_arraysize(event.gsensor.data));3622SDL_memset(event.gsensor.data, 0,3623sizeof(event.gsensor.data));3624SDL_memcpy(event.gsensor.data, data,3625num_values * sizeof(*data));3626event.gsensor.sensor_timestamp = sensor_timestamp;3627SDL_PushEvent(&event);3628}3629}3630break;3631}3632}3633}36343635static void SDL_LoadVIDPIDListFromHint(const char *hint, int *num_entries, int *max_entries, Uint32 **entries)3636{3637Uint32 entry;3638char *spot;3639char *file = NULL;36403641if (hint && *hint == '@') {3642spot = file = (char *)SDL_LoadFile(hint + 1, NULL);3643} else {3644spot = (char *)hint;3645}36463647if (!spot) {3648return;3649}36503651while ((spot = SDL_strstr(spot, "0x")) != NULL) {3652entry = (Uint16)SDL_strtol(spot, &spot, 0);3653entry <<= 16;3654spot = SDL_strstr(spot, "0x");3655if (!spot) {3656break;3657}3658entry |= (Uint16)SDL_strtol(spot, &spot, 0);36593660if (*num_entries == *max_entries) {3661int new_max_entries = *max_entries + 16;3662Uint32 *new_entries = (Uint32 *)SDL_realloc(*entries, new_max_entries * sizeof(**entries));3663if (!new_entries) {3664// Out of memory, go with what we have already3665break;3666}3667*entries = new_entries;3668*max_entries = new_max_entries;3669}3670(*entries)[(*num_entries)++] = entry;3671}36723673if (file) {3674SDL_free(file);3675}3676}36773678void SDL_LoadVIDPIDListFromHints(SDL_vidpid_list *list, const char *included_list, const char *excluded_list)3679{3680// Empty the list3681list->num_included_entries = 0;3682list->num_excluded_entries = 0;36833684// Add the initial entries3685if (list->num_initial_entries > 0) {3686if (list->num_included_entries < list->num_initial_entries) {3687Uint32 *entries = (Uint32 *)SDL_malloc(list->num_initial_entries * sizeof(*entries));3688if (entries) {3689SDL_memcpy(entries, list->initial_entries, list->num_initial_entries * sizeof(*entries));3690list->included_entries = entries;3691list->num_included_entries = list->num_initial_entries;3692list->max_included_entries = list->num_initial_entries;3693}3694}3695}36963697// Add the included entries from the hint3698SDL_LoadVIDPIDListFromHint(included_list, &list->num_included_entries, &list->max_included_entries, &list->included_entries);36993700// Add the excluded entries from the hint3701SDL_LoadVIDPIDListFromHint(excluded_list, &list->num_excluded_entries, &list->max_excluded_entries, &list->excluded_entries);3702}37033704static void SDLCALL SDL_VIDPIDIncludedHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)3705{3706SDL_vidpid_list *list = (SDL_vidpid_list *)userdata;3707const char *included_list = hint;3708const char *excluded_list = NULL;37093710if (!list->initialized) {3711return;3712}37133714if (list->excluded_hint_name) {3715excluded_list = SDL_GetHint(list->excluded_hint_name);3716}3717SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);3718}37193720static void SDLCALL SDL_VIDPIDExcludedHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)3721{3722SDL_vidpid_list *list = (SDL_vidpid_list *)userdata;3723const char *included_list = NULL;3724const char *excluded_list = hint;37253726if (!list->initialized) {3727return;3728}37293730if (list->included_hint_name) {3731included_list = SDL_GetHint(list->included_hint_name);3732}3733SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);3734}37353736void SDL_LoadVIDPIDList(SDL_vidpid_list *list)3737{3738const char *included_list = NULL;3739const char *excluded_list = NULL;37403741if (list->included_hint_name) {3742SDL_AddHintCallback(list->included_hint_name, SDL_VIDPIDIncludedHintChanged, list);3743}37443745if (list->excluded_hint_name) {3746SDL_AddHintCallback(list->excluded_hint_name, SDL_VIDPIDExcludedHintChanged, list);3747}37483749list->initialized = true;37503751if (list->included_hint_name) {3752included_list = SDL_GetHint(list->included_hint_name);3753}3754if (list->excluded_hint_name) {3755excluded_list = SDL_GetHint(list->excluded_hint_name);3756}3757SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);3758}37593760bool SDL_VIDPIDInList(Uint16 vendor_id, Uint16 product_id, const SDL_vidpid_list *list)3761{3762int i;3763Uint32 vidpid = MAKE_VIDPID(vendor_id, product_id);37643765for (i = 0; i < list->num_excluded_entries; ++i) {3766if (vidpid == list->excluded_entries[i]) {3767return false;3768}3769}3770for (i = 0; i < list->num_included_entries; ++i) {3771if (vidpid == list->included_entries[i]) {3772return true;3773}3774}3775return false;3776}37773778void SDL_FreeVIDPIDList(SDL_vidpid_list *list)3779{3780if (list->included_hint_name) {3781SDL_RemoveHintCallback(list->included_hint_name, SDL_VIDPIDIncludedHintChanged, list);3782}37833784if (list->excluded_hint_name) {3785SDL_RemoveHintCallback(list->excluded_hint_name, SDL_VIDPIDExcludedHintChanged, list);3786}37873788if (list->included_entries) {3789SDL_free(list->included_entries);3790list->included_entries = NULL;3791list->num_included_entries = 0;3792list->max_included_entries = 0;3793}37943795if (list->excluded_entries) {3796SDL_free(list->excluded_entries);3797list->excluded_entries = NULL;3798list->num_excluded_entries = 0;3799list->max_excluded_entries = 0;3800}38013802list->initialized = false;3803}380438053806