[SDL3] Adding input and FFB support for Logitech G29(PS3) on hidapi (#11598)

These changes enable the Logitech G29 wheel to run on hidapi with both SDL_Joystick and SDL_Haptic interfaces.

While it is already possible to use the wheel on Linux in WINE + SDL2 thanks to the in-tree evdev driver as well as new-lg4ff, these set of changes allow the G29 to be used with WINE under MacOS and FreeBSD

These wheels should also be supported, but I can only test them from G29's compat modes: G27, G25, DFGT, DFP, DFEX

Haptic and led support are ported from https://github.com/berarma/new-lg4ff
This commit is contained in:
Katharine Chui 2025-03-17 22:24:39 +08:00 committed by GitHub
parent d66483dfcc
commit 35c03774f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 2887 additions and 18 deletions

View file

@ -42,6 +42,7 @@ LOCAL_SRC_FILES := \
$(wildcard $(LOCAL_PATH)/src/haptic/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/dummy/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/hidapi/*.c) \
$(wildcard $(LOCAL_PATH)/src/hidapi/*.c) \
$(wildcard $(LOCAL_PATH)/src/hidapi/android/*.cpp) \
$(wildcard $(LOCAL_PATH)/src/joystick/*.c) \

View file

@ -454,8 +454,10 @@
<ClInclude Include="..\..\src\io\SDL_sysasyncio.h" />
<ClInclude Include="..\..\src\haptic\SDL_haptic_c.h" />
<ClInclude Include="..\..\src\haptic\SDL_syshaptic.h" />
<ClInclude Include="..\..\src\haptic\SDL_hidapihaptic.h" />
<ClInclude Include="..\..\src\haptic\windows\SDL_dinputhaptic_c.h" />
<ClInclude Include="..\..\src\haptic\windows\SDL_windowshaptic_c.h" />
<ClInclude Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_c.h" />
<ClInclude Include="..\..\src\hidapi\hidapi\hidapi.h" />
<ClInclude Include="..\..\src\hidapi\SDL_hidapi_c.h" />
<ClInclude Include="..\..\src\joystick\controller_type.h" />
@ -703,6 +705,8 @@
<LanguageStandard Condition="'$(Configuration)|$(Platform)'=='Release|Gaming.Xbox.Scarlett.x64'">stdcpp17</LanguageStandard>
<LanguageStandard Condition="'$(Configuration)|$(Platform)'=='Release|Gaming.Xbox.XboxOne.x64'">stdcpp17</LanguageStandard>
</ClCompile>
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic.c" />
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_lg4ff.c" />
<ClCompile Include="..\..\src\hidapi\SDL_hidapi.c" />
<ClCompile Include="..\..\src\joystick\controller_type.c" />
<ClCompile Include="..\..\src\joystick\dummy\SDL_sysjoystick.c" />
@ -725,6 +729,7 @@
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360w.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xboxone.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_lg4ff.c" />
<ClCompile Include="..\..\src\joystick\SDL_gamepad.c" />
<ClCompile Include="..\..\src\joystick\SDL_joystick.c" />
<ClCompile Include="..\..\src\joystick\SDL_steam_virtual_gamepad.c" />

View file

@ -56,6 +56,8 @@
<ClCompile Include="..\..\src\haptic\SDL_haptic.c" />
<ClCompile Include="..\..\src\haptic\windows\SDL_dinputhaptic.c" />
<ClCompile Include="..\..\src\haptic\windows\SDL_windowshaptic.c" />
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic.c" />
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_lg4ff.c" />
<ClCompile Include="..\..\src\hidapi\SDL_hidapi.c" />
<ClCompile Include="..\..\src\joystick\controller_type.c" />
<ClCompile Include="..\..\src\joystick\dummy\SDL_sysjoystick.c" />
@ -78,6 +80,7 @@
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360w.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xboxone.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_lg4ff.c" />
<ClCompile Include="..\..\src\joystick\SDL_gamepad.c" />
<ClCompile Include="..\..\src\joystick\SDL_joystick.c" />
<ClCompile Include="..\..\src\joystick\SDL_steam_virtual_gamepad.c" />
@ -343,8 +346,10 @@
<ClInclude Include="..\..\src\gpu\SDL_sysgpu.h" />
<ClInclude Include="..\..\src\haptic\SDL_haptic_c.h" />
<ClInclude Include="..\..\src\haptic\SDL_syshaptic.h" />
<ClInclude Include="..\..\src\haptic\SDL_hidapihaptic.h" />
<ClInclude Include="..\..\src\haptic\windows\SDL_dinputhaptic_c.h" />
<ClInclude Include="..\..\src\haptic\windows\SDL_windowshaptic_c.h" />
<ClInclude Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_c.h" />
<ClInclude Include="..\..\src\hidapi\hidapi\hidapi.h" />
<ClInclude Include="..\..\src\hidapi\SDL_hidapi_c.h" />
<ClInclude Include="..\..\src\joystick\controller_type.h" />

View file

@ -367,8 +367,10 @@
<ClInclude Include="..\..\src\io\SDL_sysasyncio.h" />
<ClInclude Include="..\..\src\haptic\SDL_haptic_c.h" />
<ClInclude Include="..\..\src\haptic\SDL_syshaptic.h" />
<ClInclude Include="..\..\src\haptic\SDL_hidapihaptic.h" />
<ClInclude Include="..\..\src\haptic\windows\SDL_dinputhaptic_c.h" />
<ClInclude Include="..\..\src\haptic\windows\SDL_windowshaptic_c.h" />
<ClInclude Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_c.h" />
<ClInclude Include="..\..\src\hidapi\hidapi\hidapi.h" />
<ClInclude Include="..\..\src\hidapi\SDL_hidapi_c.h" />
<ClInclude Include="..\..\src\joystick\controller_type.h" />
@ -573,6 +575,8 @@
<ClCompile Include="..\..\src\haptic\SDL_haptic.c" />
<ClCompile Include="..\..\src\haptic\windows\SDL_dinputhaptic.c" />
<ClCompile Include="..\..\src\haptic\windows\SDL_windowshaptic.c" />
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic.c" />
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_lg4ff.c" />
<ClCompile Include="..\..\src\hidapi\SDL_hidapi.c" />
<ClCompile Include="..\..\src\joystick\controller_type.c" />
<ClCompile Include="..\..\src\joystick\dummy\SDL_sysjoystick.c" />
@ -595,6 +599,7 @@
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360w.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xboxone.c" />
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_lg4ff.c" />
<ClCompile Include="..\..\src\joystick\SDL_gamepad.c" />
<ClCompile Include="..\..\src\joystick\SDL_joystick.c" />
<ClCompile Include="..\..\src\joystick\SDL_steam_virtual_gamepad.c" />

View file

@ -82,6 +82,9 @@
<Filter Include="haptic\windows">
<UniqueIdentifier>{ebc2fca3-3c26-45e3-815e-3e0581d5e226}</UniqueIdentifier>
</Filter>
<Filter Include="haptic\hidapi">
<UniqueIdentifier>{06DB01C0-65B5-4DE7-8ADC-C0B0CA3A1E69}</UniqueIdentifier>
</Filter>
<Filter Include="haptic\dummy">
<UniqueIdentifier>{47c445a2-7014-4e15-9660-7c89a27dddcf}</UniqueIdentifier>
</Filter>
@ -564,6 +567,9 @@
<ClInclude Include="..\..\src\haptic\SDL_syshaptic.h">
<Filter>haptic</Filter>
</ClInclude>
<ClInclude Include="..\..\src\haptic\SDL_hidapihaptic.h">
<Filter>haptic</Filter>
</ClInclude>
<ClInclude Include="..\..\src\haptic\SDL_haptic_c.h">
<Filter>haptic</Filter>
</ClInclude>
@ -621,6 +627,9 @@
<ClInclude Include="..\..\src\haptic\windows\SDL_windowshaptic_c.h">
<Filter>haptic\windows</Filter>
</ClInclude>
<ClInclude Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_c.h">
<Filter>haptic\hidapi</Filter>
</ClInclude>
<ClInclude Include="..\..\src\joystick\hidapi\SDL_hidapijoystick_c.h">
<Filter>joystick\hidapi</Filter>
</ClInclude>
@ -1163,6 +1172,12 @@
<ClCompile Include="..\..\src\haptic\windows\SDL_windowshaptic.c">
<Filter>haptic\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic.c">
<Filter>haptic\hidapi</Filter>
</ClCompile>
<ClCompile Include="..\..\src\haptic\hidapi\SDL_hidapihaptic_lg4ff.c">
<Filter>haptic\hidapi</Filter>
</ClCompile>
<ClCompile Include="..\..\src\haptic\dummy\SDL_syshaptic.c">
<Filter>haptic\dummy</Filter>
</ClCompile>
@ -1223,6 +1238,9 @@
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xboxone.c">
<Filter>joystick\hidapi</Filter>
</ClCompile>
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_lg4ff.c">
<Filter>joystick\hidapi</Filter>
</ClCompile>
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapijoystick.c">
<Filter>joystick\hidapi</Filter>
</ClCompile>

View file

@ -71,6 +71,10 @@
63134A262A7902FD0021E9A6 /* SDL_pen.c in Sources */ = {isa = PBXBuildFile; fileRef = 63134A242A7902FD0021E9A6 /* SDL_pen.c */; };
75E0915A241EA924004729E1 /* SDL_virtualjoystick.c in Sources */ = {isa = PBXBuildFile; fileRef = 75E09158241EA924004729E1 /* SDL_virtualjoystick.c */; };
75E09163241EA924004729E1 /* SDL_virtualjoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = 75E09159241EA924004729E1 /* SDL_virtualjoystick_c.h */; };
89E5801E2D03602200DAF6D3 /* SDL_hidapi_lg4ff.c in Sources */ = {isa = PBXBuildFile; fileRef = 89E5801D2D03602200DAF6D3 /* SDL_hidapi_lg4ff.c */; };
89E580232D03606400DAF6D3 /* SDL_hidapihaptic.c in Sources */ = {isa = PBXBuildFile; fileRef = 89E5801F2D03606400DAF6D3 /* SDL_hidapihaptic.c */; };
89E580242D03606400DAF6D3 /* SDL_hidapihaptic_lg4ff.c in Sources */ = {isa = PBXBuildFile; fileRef = 89E580212D03606400DAF6D3 /* SDL_hidapihaptic_lg4ff.c */; };
89E580252D03606400DAF6D3 /* SDL_hidapihaptic_c.h in Headers */ = {isa = PBXBuildFile; fileRef = 89E580202D03606400DAF6D3 /* SDL_hidapihaptic_c.h */; };
9846B07C287A9020000C35C8 /* SDL_hidapi_shield.c in Sources */ = {isa = PBXBuildFile; fileRef = 9846B07B287A9020000C35C8 /* SDL_hidapi_shield.c */; };
A1626A3E2617006A003F1973 /* SDL_triangle.c in Sources */ = {isa = PBXBuildFile; fileRef = A1626A3D2617006A003F1973 /* SDL_triangle.c */; };
A1626A522617008D003F1973 /* SDL_triangle.h in Headers */ = {isa = PBXBuildFile; fileRef = A1626A512617008C003F1973 /* SDL_triangle.h */; };
@ -608,6 +612,10 @@
63134A242A7902FD0021E9A6 /* SDL_pen.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_pen.c; sourceTree = "<group>"; };
75E09158241EA924004729E1 /* SDL_virtualjoystick.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_virtualjoystick.c; sourceTree = "<group>"; };
75E09159241EA924004729E1 /* SDL_virtualjoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_virtualjoystick_c.h; sourceTree = "<group>"; };
89E5801D2D03602200DAF6D3 /* SDL_hidapi_lg4ff.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_lg4ff.c; sourceTree = "<group>"; };
89E5801F2D03606400DAF6D3 /* SDL_hidapihaptic.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_hidapihaptic.c; sourceTree = "<group>"; };
89E580202D03606400DAF6D3 /* SDL_hidapihaptic_c.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDL_hidapihaptic_c.h; sourceTree = "<group>"; };
89E580212D03606400DAF6D3 /* SDL_hidapihaptic_lg4ff.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_hidapihaptic_lg4ff.c; sourceTree = "<group>"; };
9846B07B287A9020000C35C8 /* SDL_hidapi_shield.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_shield.c; sourceTree = "<group>"; };
A1626A3D2617006A003F1973 /* SDL_triangle.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_triangle.c; sourceTree = "<group>"; };
A1626A512617008C003F1973 /* SDL_triangle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_triangle.h; sourceTree = "<group>"; };
@ -1477,6 +1485,16 @@
path = virtual;
sourceTree = "<group>";
};
89E580222D03606400DAF6D3 /* hidapi */ = {
isa = PBXGroup;
children = (
89E5801F2D03606400DAF6D3 /* SDL_hidapihaptic.c */,
89E580202D03606400DAF6D3 /* SDL_hidapihaptic_c.h */,
89E580212D03606400DAF6D3 /* SDL_hidapihaptic_lg4ff.c */,
);
path = hidapi;
sourceTree = "<group>";
};
A75FDAA423E2790500529352 /* ios */ = {
isa = PBXGroup;
children = (
@ -1535,6 +1553,7 @@
A7D8A5C223E2513D00DCD162 /* haptic */ = {
isa = PBXGroup;
children = (
89E580222D03606400DAF6D3 /* hidapi */,
A7D8A5CD23E2513D00DCD162 /* darwin */,
A7D8A5C323E2513D00DCD162 /* dummy */,
A7D8A5C623E2513D00DCD162 /* SDL_haptic_c.h */,
@ -1904,6 +1923,7 @@
A7D8A7BE23E2513E00DCD162 /* hidapi */ = {
isa = PBXGroup;
children = (
89E5801D2D03602200DAF6D3 /* SDL_hidapi_lg4ff.c */,
F32305FE28939F6400E66D30 /* SDL_hidapi_combined.c */,
A7D8A7C923E2513E00DCD162 /* SDL_hidapi_gamecube.c */,
F3F07D59269640160074468B /* SDL_hidapi_luna.c */,
@ -2617,6 +2637,7 @@
F37E18642BAA40670098C111 /* SDL_time_c.h in Headers */,
F31013C82C24E98200FBE946 /* SDL_keymap_c.h in Headers */,
63134A252A7902FD0021E9A6 /* SDL_pen_c.h in Headers */,
89E580252D03606400DAF6D3 /* SDL_hidapihaptic_c.h in Headers */,
F36C34312C0F876500991150 /* SDL_offscreenvulkan.h in Headers */,
A7D8B2C023E2514200DCD162 /* SDL_pixels_c.h in Headers */,
F37E18622BAA40090098C111 /* SDL_sysfilesystem.h in Headers */,
@ -2905,6 +2926,8 @@
A7D8BBDD23E2574800DCD162 /* SDL_uikitmodes.m in Sources */,
A7D8BA3723E2514400DCD162 /* SDL_d3dmath.c in Sources */,
F3A9AE9C2C8A13C100AAC390 /* SDL_pipeline_gpu.c in Sources */,
89E580232D03606400DAF6D3 /* SDL_hidapihaptic.c in Sources */,
89E580242D03606400DAF6D3 /* SDL_hidapihaptic_lg4ff.c in Sources */,
75E0915A241EA924004729E1 /* SDL_virtualjoystick.c in Sources */,
F338A11A2D1B37E4007CDFDF /* SDL_tray.c in Sources */,
A7D8ABEB23E2514100DCD162 /* SDL_nullvideo.c in Sources */,
@ -2966,6 +2989,7 @@
A7D8B76423E2514300DCD162 /* SDL_mixer.c in Sources */,
A7D8BB5723E2514500DCD162 /* SDL_events.c in Sources */,
A7D8ADE623E2514100DCD162 /* SDL_blit_0.c in Sources */,
89E5801E2D03602200DAF6D3 /* SDL_hidapi_lg4ff.c in Sources */,
A7D8B8A823E2514400DCD162 /* SDL_diskaudio.c in Sources */,
56A2373329F9C113003CCA5F /* SDL_sysrwlock.c in Sources */,
F3A9AE9A2C8A13C100AAC390 /* SDL_shaders_gpu.c in Sources */,

View file

@ -1136,6 +1136,7 @@ macro(CheckHIDAPI)
set(HAVE_SDL_JOYSTICK TRUE)
set(HAVE_HIDAPI_JOYSTICK TRUE)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/joystick/hidapi/*.c")
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/haptic/hidapi/*.c")
endif()
else()
set(SDL_HIDAPI_DISABLED 1)

View file

@ -1721,6 +1721,19 @@ extern "C" {
*/
#define SDL_HINT_JOYSTICK_HIDAPI_STEAM_HORI "SDL_JOYSTICK_HIDAPI_STEAM_HORI"
/**
* A variable controlling whether the HIDAPI driver for some Logitech wheels
* should be used.
*
* This variable can be set to the following values:
*
* - "0": HIDAPI driver is not used
* - "1": HIDAPI driver is used
*
* The default is the value of SDL_HINT_JOYSTICK_HIDAPI
*/
#define SDL_HINT_JOYSTICK_HIDAPI_LG4FF "SDL_JOYSTICK_HIDAPI_LG4FF"
/**
* A variable controlling whether the HIDAPI driver for Nintendo Switch
* controllers should be used.

View file

@ -21,6 +21,9 @@
#include "SDL_internal.h"
#include "SDL_syshaptic.h"
#ifdef SDL_JOYSTICK_HIDAPI
#include "SDL_hidapihaptic.h"
#endif
#include "SDL_haptic_c.h"
#include "../joystick/SDL_joystick_c.h" // For SDL_IsJoystickValid
#include "../SDL_hints_c.h"
@ -112,7 +115,17 @@ static SDL_Haptic *SDL_haptics = NULL;
bool SDL_InitHaptics(void)
{
return SDL_SYS_HapticInit();
if (!SDL_SYS_HapticInit()) {
return false;
}
#ifdef SDL_JOYSTICK_HIDAPI
if (!SDL_HIDAPI_HapticInit()) {
SDL_SYS_HapticQuit();
return false;
}
#endif
return true;
}
static bool SDL_GetHapticIndex(SDL_HapticID instance_id, int *driver_index)
@ -205,7 +218,6 @@ SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id)
}
// Initialize the haptic device
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
haptic->instance_id = instance_id;
haptic->rumble_id = -1;
if (!SDL_SYS_HapticOpen(haptic)) {
@ -227,6 +239,8 @@ SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id)
haptic->next = SDL_haptics;
SDL_haptics = haptic;
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
// Disable autocenter and set gain to max.
if (haptic->supported & SDL_HAPTIC_GAIN) {
SDL_SetHapticGain(haptic, 100);
@ -295,7 +309,11 @@ bool SDL_IsJoystickHaptic(SDL_Joystick *joystick)
// Must be a valid joystick
if (SDL_IsJoystickValid(joystick) &&
!SDL_IsGamepad(SDL_GetJoystickID(joystick))) {
#ifdef SDL_JOYSTICK_HIDAPI
result = SDL_SYS_JoystickIsHaptic(joystick) || SDL_HIDAPI_JoystickIsHaptic(joystick);
#else
result = SDL_SYS_JoystickIsHaptic(joystick);
#endif
}
}
SDL_UnlockJoysticks();
@ -310,16 +328,8 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
SDL_LockJoysticks();
{
// Must be a valid joystick
if (!SDL_IsJoystickValid(joystick)) {
SDL_SetError("Haptic: Joystick isn't valid.");
SDL_UnlockJoysticks();
return NULL;
}
// Joystick must be haptic
if (SDL_IsGamepad(SDL_GetJoystickID(joystick)) ||
!SDL_SYS_JoystickIsHaptic(joystick)) {
// Joystick must be valid and haptic
if (!SDL_IsJoystickHaptic(joystick)) {
SDL_SetError("Haptic: Joystick isn't a haptic device.");
SDL_UnlockJoysticks();
return NULL;
@ -328,7 +338,11 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
hapticlist = SDL_haptics;
// Check to see if joystick's haptic is already open
while (hapticlist) {
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick) || SDL_HIDAPI_JoystickSameHaptic(hapticlist, joystick)) {
#else
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
#endif
haptic = hapticlist;
++haptic->ref_count;
SDL_UnlockJoysticks();
@ -349,6 +363,16 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
*/
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
haptic->rumble_id = -1;
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_JoystickIsHaptic(joystick)) {
if (!SDL_HIDAPI_HapticOpenFromJoystick(haptic, joystick)) {
SDL_SetError("Haptic: SDL_HIDAPI_HapticOpenFromJoystick failed.");
SDL_free(haptic);
SDL_UnlockJoysticks();
return NULL;
}
} else
#endif
if (!SDL_SYS_HapticOpenFromJoystick(haptic, joystick)) {
SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false);
@ -379,6 +403,16 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
haptic->next = SDL_haptics;
SDL_haptics = haptic;
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
// Disable autocenter and set gain to max.
if (haptic->supported & SDL_HAPTIC_GAIN) {
SDL_SetHapticGain(haptic, 100);
}
if (haptic->supported & SDL_HAPTIC_AUTOCENTER) {
SDL_SetHapticAutocenter(haptic, 0);
}
return haptic;
}
@ -395,13 +429,20 @@ void SDL_CloseHaptic(SDL_Haptic *haptic)
return;
}
// Close it, properly removing effects if needed
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect != NULL) {
SDL_DestroyHapticEffect(haptic, i);
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
SDL_HIDAPI_HapticClose(haptic);
} else
#endif
{
// Close it, properly removing effects if needed
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect != NULL) {
SDL_DestroyHapticEffect(haptic, i);
}
}
SDL_SYS_HapticClose(haptic);
}
SDL_SYS_HapticClose(haptic);
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false);
// Remove from the list
@ -433,6 +474,9 @@ void SDL_QuitHaptics(void)
SDL_CloseHaptic(SDL_haptics);
}
#ifdef SDL_JOYSTICK_HIDAPI
SDL_HIDAPI_HapticQuit();
#endif
SDL_SYS_HapticQuit();
}
@ -495,6 +539,12 @@ int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect)
return -1;
}
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticNewEffect(haptic, effect);
}
#endif
// See if there's a free slot
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect == NULL) {
@ -527,6 +577,12 @@ bool SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffe
{
CHECK_HAPTIC_MAGIC(haptic, false);
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticUpdateEffect(haptic, effect, data);
}
#endif
if (!ValidEffect(haptic, effect)) {
return false;
}
@ -554,6 +610,12 @@ bool SDL_RunHapticEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
{
CHECK_HAPTIC_MAGIC(haptic, false);
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticRunEffect(haptic, effect, iterations);
}
#endif
if (!ValidEffect(haptic, effect)) {
return false;
}
@ -570,6 +632,12 @@ bool SDL_StopHapticEffect(SDL_Haptic *haptic, int effect)
{
CHECK_HAPTIC_MAGIC(haptic, false);
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticStopEffect(haptic, effect);
}
#endif
if (!ValidEffect(haptic, effect)) {
return false;
}
@ -586,6 +654,13 @@ void SDL_DestroyHapticEffect(SDL_Haptic *haptic, int effect)
{
CHECK_HAPTIC_MAGIC(haptic,);
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
SDL_HIDAPI_HapticDestroyEffect(haptic, effect);
return;
}
#endif
if (!ValidEffect(haptic, effect)) {
return;
}
@ -602,6 +677,12 @@ bool SDL_GetHapticEffectStatus(SDL_Haptic *haptic, int effect)
{
CHECK_HAPTIC_MAGIC(haptic, false);
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticGetEffectStatus(haptic, effect);
}
#endif
if (!ValidEffect(haptic, effect)) {
return false;
}
@ -648,6 +729,12 @@ bool SDL_SetHapticGain(SDL_Haptic *haptic, int gain)
real_gain = gain;
}
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticSetGain(haptic, real_gain);
}
#endif
return SDL_SYS_HapticSetGain(haptic, real_gain);
}
@ -663,6 +750,12 @@ bool SDL_SetHapticAutocenter(SDL_Haptic *haptic, int autocenter)
return SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
}
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticSetAutocenter(haptic, autocenter);
}
#endif
return SDL_SYS_HapticSetAutocenter(haptic, autocenter);
}
@ -674,6 +767,12 @@ bool SDL_PauseHaptic(SDL_Haptic *haptic)
return SDL_SetError("Haptic: Device does not support setting pausing.");
}
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticPause(haptic);
}
#endif
return SDL_SYS_HapticPause(haptic);
}
@ -685,6 +784,12 @@ bool SDL_ResumeHaptic(SDL_Haptic *haptic)
return true; // Not going to be paused, so we pretend it's unpaused.
}
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticResume(haptic);
}
#endif
return SDL_SYS_HapticResume(haptic);
}
@ -692,6 +797,12 @@ bool SDL_StopHapticEffects(SDL_Haptic *haptic)
{
CHECK_HAPTIC_MAGIC(haptic, false);
#ifdef SDL_JOYSTICK_HIDAPI
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticStopAll(haptic);
}
#endif
return SDL_SYS_HapticStopAll(haptic);
}

View file

@ -0,0 +1,48 @@
/*
Simple DirectMedia Layer
Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
All hid command sent and effect rendering are ported from https://github.com/berarma/new-lg4ff
*/
#ifndef SDL_hidapihaptic_h_
#define SDL_hidapihaptic_h_
bool SDL_HIDAPI_HapticInit();
bool SDL_HIDAPI_HapticIsHidapi(SDL_Haptic *haptic);
bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick);
bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick);
bool SDL_HIDAPI_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick);
void SDL_HIDAPI_HapticClose(SDL_Haptic *haptic);
void SDL_HIDAPI_HapticQuit(void);
int SDL_HIDAPI_HapticNewEffect(SDL_Haptic *haptic, const SDL_HapticEffect *base);
bool SDL_HIDAPI_HapticUpdateEffect(SDL_Haptic *haptic, int id, const SDL_HapticEffect *data);
bool SDL_HIDAPI_HapticRunEffect(SDL_Haptic *haptic, int id, Uint32 iterations);
bool SDL_HIDAPI_HapticStopEffect(SDL_Haptic *haptic, int id);
void SDL_HIDAPI_HapticDestroyEffect(SDL_Haptic *haptic, int id);
bool SDL_HIDAPI_HapticGetEffectStatus(SDL_Haptic *haptic, int id);
bool SDL_HIDAPI_HapticSetGain(SDL_Haptic *haptic, int gain);
bool SDL_HIDAPI_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter);
bool SDL_HIDAPI_HapticPause(SDL_Haptic *haptic);
bool SDL_HIDAPI_HapticResume(SDL_Haptic *haptic);
bool SDL_HIDAPI_HapticStopAll(SDL_Haptic *haptic);
#endif //SDL_hidapihaptic_h_

View file

@ -0,0 +1,305 @@
/*
Simple DirectMedia Layer
Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_JOYSTICK_HIDAPI
#include "SDL_hidapihaptic_c.h"
#include "SDL3/SDL_mutex.h"
#include "SDL3/SDL_error.h"
extern struct SDL_JoystickDriver SDL_HIDAPI_JoystickDriver;
typedef struct haptic_list_node
{
SDL_Haptic *haptic;
struct haptic_list_node *next;
} haptic_list_node;
static haptic_list_node *haptic_list_head = NULL;
static SDL_Mutex *haptic_list_mutex = NULL;
static SDL_HIDAPI_HapticDriver *drivers[] = {
#ifdef SDL_HAPTIC_HIDAPI_LG4FF
&SDL_HIDAPI_HapticDriverLg4ff,
#endif
NULL
};
bool SDL_HIDAPI_HapticInit()
{
haptic_list_head = NULL;
haptic_list_mutex = SDL_CreateMutex();
if (haptic_list_mutex == NULL) {
return SDL_OutOfMemory();
}
return true;
}
bool SDL_HIDAPI_HapticIsHidapi(SDL_Haptic *haptic)
{
haptic_list_node *cur;
bool ret = false;
SDL_LockMutex(haptic_list_mutex);
cur = haptic_list_head;
while (cur != NULL) {
if (cur->haptic == haptic) {
ret = true;
break;
}
cur = cur->next;
}
SDL_UnlockMutex(haptic_list_mutex);
return ret;
}
bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick)
{
const int numdrivers = SDL_arraysize(drivers) - 1;
int i;
SDL_AssertJoysticksLocked();
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
return false;
}
for (i = 0; i < numdrivers; ++i) {
if (drivers[i]->JoystickSupported(joystick)) {
return true;
}
}
return false;
}
bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
{
const int numdrivers = SDL_arraysize(drivers) - 1;
int i;
SDL_AssertJoysticksLocked();
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
return SDL_SetError("Cannot open hidapi haptic from non hidapi joystick");
}
for (i = 0; i < numdrivers; ++i) {
if (drivers[i]->JoystickSupported(joystick)) {
SDL_HIDAPI_HapticDevice *device;
haptic_list_node *list_node;
// the driver is responsible for calling SDL_SetError
void *ctx = drivers[i]->Open(joystick);
if (ctx == NULL) {
return false;
}
device = SDL_malloc(sizeof(SDL_HIDAPI_HapticDevice));
if (device == NULL) {
SDL_HIDAPI_HapticDevice temp;
temp.ctx = ctx;
temp.driver = drivers[i];
temp.joystick = joystick;
temp.driver->Close(&temp);
return SDL_OutOfMemory();
}
device->driver = drivers[i];
device->haptic = haptic;
device->joystick = joystick;
device->ctx = ctx;
list_node = SDL_malloc(sizeof(haptic_list_node));
if (list_node == NULL) {
device->driver->Close(device);
SDL_free(device);
return SDL_OutOfMemory();
}
haptic->hwdata = (struct haptic_hwdata *)device;
// this is outside of the syshaptic driver
haptic->neffects = device->driver->NumEffects(device);
haptic->nplaying = device->driver->NumEffectsPlaying(device);
haptic->supported = device->driver->GetFeatures(device);
haptic->naxes = device->driver->NumAxes(device);
// outside of SYS_HAPTIC
haptic->instance_id = 255;
list_node->haptic = haptic;
list_node->next = NULL;
// grab a joystick ref so that it doesn't get fully destroyed before the haptic is closed
SDL_OpenJoystick(SDL_GetJoystickID(joystick));
SDL_LockMutex(haptic_list_mutex);
if (haptic_list_head == NULL) {
haptic_list_head = list_node;
} else {
haptic_list_node *cur = haptic_list_head;
while(cur->next != NULL) {
cur = cur->next;
}
cur->next = list_node;
}
SDL_UnlockMutex(haptic_list_mutex);
return true;
}
}
return SDL_SetError("No supported HIDAPI haptic driver found for joystick");
}
bool SDL_HIDAPI_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
{
SDL_HIDAPI_HapticDevice *device;
SDL_AssertJoysticksLocked();
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
return false;
}
device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
if (joystick == device->joystick) {
return true;
}
return false;
}
void SDL_HIDAPI_HapticClose(SDL_Haptic *haptic)
{
haptic_list_node *cur, *last;
SDL_LockMutex(haptic_list_mutex);
cur = haptic_list_head;
last = NULL;
while (cur != NULL) {
if (cur->haptic == haptic) {
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
device->driver->Close(device);
// a reference was grabbed during open, now release it
SDL_CloseJoystick(device->joystick);
if (cur == haptic_list_head) {
haptic_list_head = cur->next;
} else {
last->next = cur->next;
}
SDL_free(device->ctx);
SDL_free(device);
SDL_free(cur);
SDL_UnlockMutex(haptic_list_mutex);
return;
}
last = cur;
cur = cur->next;
}
SDL_UnlockMutex(haptic_list_mutex);
}
void SDL_HIDAPI_HapticQuit(void)
{
// the list is cleared in SDL_haptic.c
if (haptic_list_mutex != NULL) {
SDL_DestroyMutex(haptic_list_mutex);
haptic_list_mutex = NULL;
}
}
int SDL_HIDAPI_HapticNewEffect(SDL_Haptic *haptic, const SDL_HapticEffect *base)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->CreateEffect(device, base);
}
bool SDL_HIDAPI_HapticUpdateEffect(SDL_Haptic *haptic, int id, const SDL_HapticEffect *data)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->UpdateEffect(device, id, data);
}
bool SDL_HIDAPI_HapticRunEffect(SDL_Haptic *haptic, int id, Uint32 iterations)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->RunEffect(device, id, iterations);
}
bool SDL_HIDAPI_HapticStopEffect(SDL_Haptic *haptic, int id)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->StopEffect(device, id);
}
void SDL_HIDAPI_HapticDestroyEffect(SDL_Haptic *haptic, int id)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
device->driver->DestroyEffect(device, id);
}
bool SDL_HIDAPI_HapticGetEffectStatus(SDL_Haptic *haptic, int id)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->GetEffectStatus(device, id);
}
bool SDL_HIDAPI_HapticSetGain(SDL_Haptic *haptic, int gain)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->SetGain(device, gain);
}
bool SDL_HIDAPI_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->SetAutocenter(device, autocenter);
}
bool SDL_HIDAPI_HapticPause(SDL_Haptic *haptic)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->Pause(device);
}
bool SDL_HIDAPI_HapticResume(SDL_Haptic *haptic)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->Resume(device);
}
bool SDL_HIDAPI_HapticStopAll(SDL_Haptic *haptic)
{
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
return device->driver->StopEffects(device);
}
#endif //SDL_JOYSTICK_HIDAPI

View file

@ -0,0 +1,70 @@
/*
Simple DirectMedia Layer
Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL_hidapihaptic_c_h_
#define SDL_hidapihaptic_c_h_
#include "SDL3/SDL_haptic.h"
#include "SDL3/SDL_joystick.h"
#include "../SDL_syshaptic.h"
#include "../../joystick/SDL_joystick_c.h" // accessing _SDL_JoystickDriver
#include "../../joystick/SDL_sysjoystick.h" // accessing _SDL_Joystick
#define SDL_HAPTIC_HIDAPI_LG4FF
typedef struct SDL_HIDAPI_HapticDriver SDL_HIDAPI_HapticDriver;
typedef struct SDL_HIDAPI_HapticDevice
{
SDL_Haptic *haptic; /* related haptic ref */
SDL_Joystick *joystick; /* related hidapi joystick */
SDL_HIDAPI_HapticDriver *driver; /* driver to use */
void *ctx; /* driver specific context */
} SDL_HIDAPI_HapticDevice;
struct SDL_HIDAPI_HapticDriver
{
bool (*JoystickSupported)(SDL_Joystick *joystick); /* return SDL_TRUE if haptic can be opened from the joystick */
void *(*Open)(SDL_Joystick *joystick); /* returns a driver context allocated with SDL_malloc, or null if it cannot be allocated */
/* functions below need to handle the possibility of a null joystick instance, indicating the absence of the joystick */
void (*Close)(SDL_HIDAPI_HapticDevice *device); /* cleanup resources allocated during Open, do NOT free driver context created in Open */
/* below mirror SDL_haptic.h effect interfaces */
int (*NumEffects)(SDL_HIDAPI_HapticDevice *device); /* returns supported number of effects the device can store */
int (*NumEffectsPlaying)(SDL_HIDAPI_HapticDevice *device); /* returns supported number of effects the device can play concurrently */
Uint32 (*GetFeatures)(SDL_HIDAPI_HapticDevice *device); /* returns supported effects in a bitmask */
int (*NumAxes)(SDL_HIDAPI_HapticDevice *device); /* returns the number of haptic axes */
int (*CreateEffect)(SDL_HIDAPI_HapticDevice *device, const SDL_HapticEffect *data); /* returns effect id if created correctly, negative number on error */
bool (*UpdateEffect)(SDL_HIDAPI_HapticDevice *device, int id, const SDL_HapticEffect *data); /* returns 0 on success, negative number on error */
bool (*RunEffect)(SDL_HIDAPI_HapticDevice *device, int id, Uint32 iterations); /* returns 0 on success, negative number on error */
bool (*StopEffect)(SDL_HIDAPI_HapticDevice *device, int id); /* returns 0 on success, negative number on error */
void (*DestroyEffect)(SDL_HIDAPI_HapticDevice *device, int id); /* returns 0 on success, negative number on error */
bool (*GetEffectStatus)(SDL_HIDAPI_HapticDevice *device, int id); /* returns 0 if not playing, 1 if playing, negative number on error */
bool (*SetGain)(SDL_HIDAPI_HapticDevice *device, int gain); /* gain 0 - 100, returns 0 on success, negative number on error */
bool (*SetAutocenter)(SDL_HIDAPI_HapticDevice *device, int autocenter); /* gain 0 - 100, returns 0 on success, negative number on error */
bool (*Pause)(SDL_HIDAPI_HapticDevice *device); /* returns 0 on success, negative number on error */
bool (*Resume)(SDL_HIDAPI_HapticDevice *device); /* returns 0 on success, negative number on error */
bool (*StopEffects)(SDL_HIDAPI_HapticDevice *device); /* returns 0 on success, negative number on error */
};
extern SDL_HIDAPI_HapticDriver SDL_HIDAPI_HapticDriverLg4ff;
#endif //SDL_joystick_c_h_

File diff suppressed because it is too large Load diff

View file

@ -2633,7 +2633,11 @@ bool SDL_IsGamepad(SDL_JoystickID instance_id)
if (SDL_FindInHashTable(s_gamepadInstanceIDs, (void *)(uintptr_t)instance_id, &value)) {
result = (bool)(uintptr_t)value;
} else {
if (SDL_PrivateGetGamepadMapping(instance_id, true) != NULL) {
SDL_JoystickType js_type = SDL_GetJoystickTypeForID(instance_id);
if (js_type != SDL_JOYSTICK_TYPE_GAMEPAD && js_type != SDL_JOYSTICK_TYPE_UNKNOWN) {
// avoid creating HIDAPI mapping if SDL_Joystick knows it is not a game pad
result = false;
} else if (SDL_PrivateGetGamepadMapping(instance_id, true) != NULL) {
result = true;
} else {
result = false;

View file

@ -0,0 +1,989 @@
/*
Simple DirectMedia Layer
Copyright (C) 2025 Simon Wood <simon@mungewell.org>
Copyright (C) 2025 Michal Malý <madcatxster@devoid-pointer.net>
Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_JOYSTICK_HIDAPI
#include "../SDL_sysjoystick.h"
#include "SDL3/SDL_events.h"
#include "SDL_hidapijoystick_c.h"
#ifdef SDL_JOYSTICK_HIDAPI_LG4FF
#define USB_VENDOR_ID_LOGITECH 0x046d
#define USB_DEVICE_ID_LOGITECH_G29_WHEEL 0xc24f
#define USB_DEVICE_ID_LOGITECH_G27_WHEEL 0xc29b
#define USB_DEVICE_ID_LOGITECH_G25_WHEEL 0xc299
#define USB_DEVICE_ID_LOGITECH_DFGT_WHEEL 0xc29a
#define USB_DEVICE_ID_LOGITECH_DFP_WHEEL 0xc298
#define USB_DEVICE_ID_LOGITECH_WHEEL 0xc294
static Uint32 supported_device_ids[] = {
USB_DEVICE_ID_LOGITECH_G29_WHEEL,
USB_DEVICE_ID_LOGITECH_G27_WHEEL,
USB_DEVICE_ID_LOGITECH_G25_WHEEL,
USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
USB_DEVICE_ID_LOGITECH_WHEEL
};
// keep the same order as the supported_ids array
static const char *supported_device_names[] = {
"Logitech G29",
"Logitech G27",
"Logitech G25",
"Logitech Driving Force GT",
"Logitech Driving Force Pro",
"Driving Force EX"
};
static const char *HIDAPI_DriverLg4ff_GetDeviceName(Uint32 device_id)
{
for (int i = 0;i < (sizeof supported_device_ids) / sizeof(Uint32);i++) {
if (supported_device_ids[i] == device_id) {
return supported_device_names[i];
}
}
SDL_assert(0);
return "";
}
static int HIDAPI_DriverLg4ff_GetNumberOfButtons(Uint32 device_id)
{
switch (device_id) {
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
return 25;
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
return 22;
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
return 19;
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
return 21;
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
return 14;
case USB_DEVICE_ID_LOGITECH_WHEEL:
return 13;
default:
SDL_assert(0);
return 0;
}
}
typedef struct
{
Uint8 last_report_buf[32];
bool initialized;
bool is_ffex;
Uint16 range;
} SDL_DriverLg4ff_Context;
static void HIDAPI_DriverLg4ff_RegisterHints(SDL_HintCallback callback, void *userdata)
{
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_LG4FF, callback, userdata);
}
static void HIDAPI_DriverLg4ff_UnregisterHints(SDL_HintCallback callback, void *userdata)
{
SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_LG4FF, callback, userdata);
}
static bool HIDAPI_DriverLg4ff_IsEnabled(void)
{
bool enabled = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_LG4FF,
SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
return enabled;
}
/*
Wheel id information by:
Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
Simon Wood <simon@mungewell.org>
`git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
*/
static Uint16 HIDAPI_DriverLg4ff_IdentifyWheel(Uint16 device_id, Uint16 release_number)
{
#define is_device(ret, m, r) { \
if ((release_number & m) == r) { \
return ret; \
} \
}
#define is_dfp { \
is_device(USB_DEVICE_ID_LOGITECH_DFP_WHEEL, 0xf000, 0x1000); \
}
#define is_dfgt { \
is_device(USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, 0xff00, 0x1300); \
}
#define is_g25 { \
is_device(USB_DEVICE_ID_LOGITECH_G25_WHEEL, 0xff00, 0x1200); \
}
#define is_g27 { \
is_device(USB_DEVICE_ID_LOGITECH_G27_WHEEL, 0xfff0, 0x1230); \
}
#define is_g29 { \
is_device(USB_DEVICE_ID_LOGITECH_G29_WHEEL, 0xfff8, 0x1350); \
is_device(USB_DEVICE_ID_LOGITECH_G29_WHEEL, 0xff00, 0x8900); \
}
switch(device_id){
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
case USB_DEVICE_ID_LOGITECH_WHEEL:
is_g29;
is_g27;
is_g25;
is_dfgt;
is_dfp;
break;
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
is_g29;
is_dfgt;
break;
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
is_g29;
is_g27;
is_g25;
break;
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
is_g29;
is_g27;
break;
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
is_g29;
break;
}
return 0;
#undef is_device
#undef is_dfp
#undef is_dfgt
#undef is_g25
#undef is_g27
#undef is_g29
}
static int SDL_HIDAPI_DriverLg4ff_GetEnvInt(const char *env_name, int min, int max, int def)
{
const char *env = SDL_getenv(env_name);
int value = 0;
if(env == NULL) {
return def;
}
value = SDL_atoi(env);
if (value < min) {
value = min;
}
if (value > max) {
value = max;
}
return value;
}
/*
Commands by:
Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
Simon Wood <simon@mungewell.org>
`git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
*/
static bool HIDAPI_DriverLg4ff_SwitchMode(SDL_HIDAPI_Device *device, Uint16 target_product_id){
int ret = 0;
switch(target_product_id){
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:{
Uint8 cmd[] = {0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00};
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
break;
}
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:{
Uint8 cmd[] = {0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00};
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
break;
}
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:{
Uint8 cmd[] = {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00};
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
break;
}
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:{
Uint8 cmd[] = {0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00};
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
break;
}
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:{
Uint8 cmd[] = {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
break;
}
case USB_DEVICE_ID_LOGITECH_WHEEL:{
Uint8 cmd[] = {0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00};
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
break;
}
default:{
SDL_assert(0);
}
}
if(ret == -1){
return false;
}
return true;
}
static bool HIDAPI_DriverLg4ff_IsSupportedDevice(
SDL_HIDAPI_Device *device,
const char *name,
SDL_GamepadType type,
Uint16 vendor_id,
Uint16 product_id,
Uint16 version,
int interface_number,
int interface_class,
int interface_subclass,
int interface_protocol)
{
int i;
if (vendor_id != USB_VENDOR_ID_LOGITECH) {
return false;
}
for (i = 0;i < sizeof(supported_device_ids) / sizeof(Uint32);i++) {
if (supported_device_ids[i] == product_id) {
break;
}
}
if (i == sizeof(supported_device_ids) / sizeof(Uint32)) {
return false;
}
Uint16 real_id = HIDAPI_DriverLg4ff_IdentifyWheel(product_id, version);
if (real_id == product_id || real_id == 0) {
// either it is already in native mode, or we don't know what the native mode is
return true;
}
// a supported native mode is found, send mode change command, then still state that we support the device
if (device != NULL && SDL_HIDAPI_DriverLg4ff_GetEnvInt("SDL_HIDAPI_LG4FF_NO_MODE_SWITCH", 0, 1, 0) == 0) {
HIDAPI_DriverLg4ff_SwitchMode(device, real_id);
}
return true;
}
/*
*Ported*
Original functions by:
Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
lg4ff_set_range_g25 lg4ff_set_range_dfp
`git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
*/
static bool HIDAPI_DriverLg4ff_SetRange(SDL_HIDAPI_Device *device, int range)
{
Uint8 cmd[7] = {0};
int ret = 0;
SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
if (range < 40) {
range = 40;
}
if (range > 900) {
range = 900;
}
ctx->range = (Uint16)range;
switch (device->product_id) {
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:{
cmd[0] = 0xf8;
cmd[1] = 0x81;
cmd[2] = range & 0x00ff;
cmd[3] = (range & 0xff00) >> 8;
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
if (ret == -1) {
return false;
}
break;
}
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:{
int start_left, start_right, full_range;
/* Prepare "coarse" limit command */
cmd[0] = 0xf8;
cmd[1] = 0x00; /* Set later */
cmd[2] = 0x00;
cmd[3] = 0x00;
cmd[4] = 0x00;
cmd[5] = 0x00;
cmd[6] = 0x00;
if (range > 200) {
cmd[1] = 0x03;
full_range = 900;
} else {
cmd[1] = 0x02;
full_range = 200;
}
ret = SDL_hid_write(device->dev, cmd, 7);
if(ret == -1){
return false;
}
/* Prepare "fine" limit command */
cmd[0] = 0x81;
cmd[1] = 0x0b;
cmd[2] = 0x00;
cmd[3] = 0x00;
cmd[4] = 0x00;
cmd[5] = 0x00;
cmd[6] = 0x00;
if (range != 200 && range != 900) {
/* Construct fine limit command */
start_left = (((full_range - range + 1) * 2047) / full_range);
start_right = 0xfff - start_left;
cmd[2] = (Uint8)(start_left >> 4);
cmd[3] = (Uint8)(start_right >> 4);
cmd[4] = 0xff;
cmd[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
cmd[6] = 0xff;
}
ret = SDL_hid_write(device->dev, cmd, 7);
if (ret == -1) {
return false;
}
break;
}
case USB_DEVICE_ID_LOGITECH_WHEEL:
// no range setting for ffex/dfex
break;
default:
SDL_assert(0);
}
return true;
}
/*
*Ported*
Original functions by:
Simon Wood <simon@mungewell.org>
Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
lg4ff_set_autocenter_default lg4ff_set_autocenter_ffex
`git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
*/
static bool HIDAPI_DriverLg4ff_SetAutoCenter(SDL_HIDAPI_Device *device, int magnitude)
{
SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
Uint8 cmd[7] = {0};
int ret;
if (magnitude < 0) {
magnitude = 0;
}
if (magnitude > 65535) {
magnitude = 65535;
}
if (ctx->is_ffex) {
magnitude = magnitude * 90 / 65535;
cmd[0] = 0xfe;
cmd[1] = 0x03;
cmd[2] = (Uint8)((Uint16)magnitude >> 14);
cmd[3] = (Uint8)((Uint16)magnitude >> 14);
cmd[4] = (Uint8)magnitude;
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
if(ret == -1){
return false;
}
} else {
Uint32 expand_a;
Uint32 expand_b;
// first disable
cmd[0] = 0xf5;
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
if (ret == -1) {
return false;
}
if (magnitude == 0) {
return true;
}
// set strength
if (magnitude <= 0xaaaa) {
expand_a = 0x0c * magnitude;
expand_b = 0x80 * magnitude;
} else {
expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
}
// TODO do not adjust for MOMO wheels, when support is added
expand_a = expand_a >> 1;
SDL_memset(cmd, 0x00, sizeof(cmd));
cmd[0] = 0xfe;
cmd[1] = 0x0d;
cmd[2] = (Uint8)(expand_a / 0xaaaa);
cmd[3] = (Uint8)(expand_a / 0xaaaa);
cmd[4] = (Uint8)(expand_b / 0xaaaa);
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
if (ret == -1) {
return false;
}
// enable
SDL_memset(cmd, 0x00, sizeof(cmd));
cmd[0] = 0x14;
ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
if (ret == -1) {
return false;
}
}
return true;
}
/*
ffex identification method by:
Simon Wood <simon@mungewell.org>
Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
lg4ff_init
`git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
*/
static bool HIDAPI_DriverLg4ff_InitDevice(SDL_HIDAPI_Device *device)
{
SDL_DriverLg4ff_Context *ctx;
ctx = (SDL_DriverLg4ff_Context *)SDL_malloc(sizeof(SDL_DriverLg4ff_Context));
if (ctx == NULL) {
SDL_OutOfMemory();
return false;
}
SDL_memset(ctx, 0, sizeof(SDL_DriverLg4ff_Context));
device->context = ctx;
device->joystick_type = SDL_JOYSTICK_TYPE_WHEEL;
HIDAPI_SetDeviceName(device, HIDAPI_DriverLg4ff_GetDeviceName(device->product_id));
if (SDL_hid_set_nonblocking(device->dev, 1) != 0) {
return false;
}
if (!HIDAPI_DriverLg4ff_SetAutoCenter(device, 0)) {
return false;
}
if (device->product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
(device->version >> 8) == 0x21 &&
(device->version & 0xff) == 0x00) {
ctx->is_ffex = true;
} else {
ctx->is_ffex = false;
}
ctx->range = 900;
return HIDAPI_JoystickConnected(device, NULL);
}
static int HIDAPI_DriverLg4ff_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
{
return -1;
}
static void HIDAPI_DriverLg4ff_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
{
}
static bool HIDAPI_DriverLg4ff_GetBit(const Uint8 *buf, int bit_num, size_t buf_len)
{
int byte_offset = bit_num / 8;
int local_bit = bit_num % 8;
Uint8 mask = 1 << local_bit;
if ((size_t)byte_offset >= buf_len) {
SDL_assert(0);
}
return (buf[byte_offset] & mask) ? true : false;
}
/*
*Ported*
Original functions by:
Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
lg4ff_adjust_dfp_x_axis
`git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
*/
static Uint16 lg4ff_adjust_dfp_x_axis(Uint16 value, Uint16 range)
{
Uint16 max_range;
Sint32 new_value;
if (range == 900)
return value;
else if (range == 200)
return value;
else if (range < 200)
max_range = 200;
else
max_range = 900;
new_value = 8192 + ((value - 8192) * max_range / range);
if (new_value < 0)
return 0;
else if (new_value > 16383)
return 16383;
else
return (Uint16)new_value;
}
static bool HIDAPI_DriverLg4ff_HandleState(SDL_HIDAPI_Device *device,
SDL_Joystick *joystick,
Uint8 *report_buf,
size_t report_size)
{
SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
Uint8 hat = 0;
Uint8 last_hat = 0;
int num_buttons = HIDAPI_DriverLg4ff_GetNumberOfButtons(device->product_id);
int bit_offset = 0;
Uint64 timestamp = SDL_GetTicksNS();
bool state_changed = false;
switch (device->product_id) {
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
hat = report_buf[0] & 0x0f;
last_hat = ctx->last_report_buf[0] & 0x0f;
break;
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
hat = report_buf[3] >> 4;
last_hat = ctx->last_report_buf[3] >> 4;
break;
case USB_DEVICE_ID_LOGITECH_WHEEL:
hat = report_buf[2] & 0x0F;
last_hat = ctx->last_report_buf[2] & 0x0F;
break;
default:
SDL_assert(0);
}
if (hat != last_hat) {
Uint8 sdl_hat = 0;
state_changed = true;
switch (hat) {
case 0:
sdl_hat = SDL_HAT_UP;
break;
case 1:
sdl_hat = SDL_HAT_RIGHTUP;
break;
case 2:
sdl_hat = SDL_HAT_RIGHT;
break;
case 3:
sdl_hat = SDL_HAT_RIGHTDOWN;
break;
case 4:
sdl_hat = SDL_HAT_DOWN;
break;
case 5:
sdl_hat = SDL_HAT_LEFTDOWN;
break;
case 6:
sdl_hat = SDL_HAT_LEFT;
break;
case 7:
sdl_hat = SDL_HAT_LEFTUP;
break;
case 8:
sdl_hat = SDL_HAT_CENTERED;
break;
// do not assert out, in case hardware can report weird hat values
}
SDL_SendJoystickHat(timestamp, joystick, 0, sdl_hat);
}
switch (device->product_id) {
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
bit_offset = 4;
break;
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
bit_offset = 14;
break;
case USB_DEVICE_ID_LOGITECH_WHEEL:
bit_offset = 0;
break;
default:
SDL_assert(0);
}
for (int i = 0;i < num_buttons;i++) {
int bit_num = bit_offset + i;
bool button_on = HIDAPI_DriverLg4ff_GetBit(report_buf, bit_num, report_size);
bool button_was_on = HIDAPI_DriverLg4ff_GetBit(ctx->last_report_buf, bit_num, report_size);
if(button_on != button_was_on){
state_changed = true;
SDL_SendJoystickButton(timestamp, joystick, (Uint8)(SDL_GAMEPAD_BUTTON_SOUTH + i), button_on);
}
}
switch (device->product_id) {
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:{
Uint16 x = *(Uint16 *)&report_buf[4];
Uint16 last_x = *(Uint16 *)&ctx->last_report_buf[4];
if (x != last_x) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, x - 32768);
}
if (report_buf[6] != ctx->last_report_buf[6]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[6] * 257 - 32768);
}
if (report_buf[7] != ctx->last_report_buf[7]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, report_buf[7] * 257 - 32768);
}
if (report_buf[8] != ctx->last_report_buf[8]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[8] * 257 - 32768);
}
break;
}
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:{
Uint16 x = report_buf[4] << 6;
Uint16 last_x = ctx->last_report_buf[4] << 6;
x = x | report_buf[3] >> 2;
last_x = last_x | ctx->last_report_buf[3] >> 2;
if (x != last_x) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, x * 4 - 32768);
}
if (report_buf[5] != ctx->last_report_buf[5]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[5] * 257 - 32768);
}
if (report_buf[6] != ctx->last_report_buf[6]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, report_buf[6] * 257 - 32768);
}
if (report_buf[7] != ctx->last_report_buf[7]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[7] * 257 - 32768);
}
break;
}
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:{
Uint16 x = report_buf[4];
Uint16 last_x = ctx->last_report_buf[4];
x = x | (report_buf[5] & 0x3F) << 8;
last_x = last_x | (ctx->last_report_buf[5] & 0x3F) << 8;
if (x != last_x) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, x * 4 - 32768);
}
if (report_buf[6] != ctx->last_report_buf[6]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[6] * 257 - 32768);
}
if (report_buf[7] != ctx->last_report_buf[7]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[7] * 257 - 32768);
}
break;
}
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:{
Uint16 x = report_buf[0];
Uint16 last_x = ctx->last_report_buf[0];
x = x | (report_buf[1] & 0x3F) << 8;
last_x = last_x | (ctx->last_report_buf[1] & 0x3F) << 8;
if (x != last_x) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, lg4ff_adjust_dfp_x_axis(x, ctx->range) * 4 - 32768);
}
if (report_buf[5] != ctx->last_report_buf[5]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[5] * 257 - 32768);
}
if (report_buf[6] != ctx->last_report_buf[6]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[6] * 257 - 32768);
}
break;
}
case USB_DEVICE_ID_LOGITECH_WHEEL:{
if (report_buf[3] != ctx->last_report_buf[3]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, report_buf[3] * 257 - 32768);
}
if (report_buf[4] != ctx->last_report_buf[4]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[4] * 257 - 32768);
}
if (report_buf[5] != ctx->last_report_buf[5]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[5] * 257 - 32768);
}
if (report_buf[6] != ctx->last_report_buf[6]) {
state_changed = true;
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, report_buf[7] * 257 - 32768);
}
break;
}
default:
SDL_assert(0);
}
SDL_memcpy(ctx->last_report_buf, report_buf, report_size);
return state_changed;
}
static bool HIDAPI_DriverLg4ff_UpdateDevice(SDL_HIDAPI_Device *device)
{
SDL_Joystick *joystick = NULL;
int r;
Uint8 report_buf[32] = {0};
size_t report_size = 0;
SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
if (device->num_joysticks > 0) {
joystick = SDL_GetJoystickFromID(device->joysticks[0]);
if (joystick == NULL) {
return false;
}
} else {
return false;
}
switch (device->product_id) {
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
report_size = 12;
break;
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
report_size = 11;
break;
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
report_size = 8;
break;
case USB_DEVICE_ID_LOGITECH_WHEEL:
report_size = 27;
break;
default:
SDL_assert(0);
}
do {
r = SDL_hid_read(device->dev, report_buf, report_size);
if (r < 0) {
/* Failed to read from controller */
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
return false;
} else if ((size_t)r == report_size) {
bool state_changed = HIDAPI_DriverLg4ff_HandleState(device, joystick, report_buf, report_size);
if(state_changed && !ctx->initialized) {
ctx->initialized = true;
HIDAPI_DriverLg4ff_SetRange(device, SDL_HIDAPI_DriverLg4ff_GetEnvInt("SDL_HIDAPI_LG4FF_RANGE", 40, 900, 900));
HIDAPI_DriverLg4ff_SetAutoCenter(device, 0);
}
}
} while (r > 0);
return true;
}
static bool HIDAPI_DriverLg4ff_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_AssertJoysticksLocked();
// Initialize the joystick capabilities
joystick->nhats = 1;
joystick->nbuttons = HIDAPI_DriverLg4ff_GetNumberOfButtons(device->product_id);
switch(device->product_id){
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
case USB_DEVICE_ID_LOGITECH_WHEEL:
joystick->naxes = 4;
break;
case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
joystick->naxes = 3;
break;
case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
joystick->naxes = 3;
break;
default:
SDL_assert(0);
}
return true;
}
static bool HIDAPI_DriverLg4ff_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
return SDL_Unsupported();
}
static bool HIDAPI_DriverLg4ff_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
{
return SDL_Unsupported();
}
static Uint32 HIDAPI_DriverLg4ff_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
switch(device->product_id) {
case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
return SDL_JOYSTICK_CAP_MONO_LED;
default:
return 0;
}
}
/*
Commands by:
Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
Simon Wood <simon@mungewell.org>
lg4ff_led_set_brightness lg4ff_set_leds
`git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
*/
static bool HIDAPI_DriverLg4ff_SendLedCommand(SDL_HIDAPI_Device *device, Uint8 state)
{
Uint8 cmd[7];
Uint8 led_state = 0;
switch (state) {
case 0:
led_state = 0;
break;
case 1:
led_state = 1;
break;
case 2:
led_state = 3;
break;
case 3:
led_state = 7;
break;
case 4:
led_state = 15;
break;
case 5:
led_state = 31;
break;
default:
SDL_assert(0);
}
cmd[0] = 0xf8;
cmd[1] = 0x12;
cmd[2] = led_state;
cmd[3] = 0x00;
cmd[4] = 0x00;
cmd[5] = 0x00;
cmd[6] = 0x00;
return SDL_hid_write(device->dev, cmd, sizeof(cmd)) == sizeof(cmd);
}
static bool HIDAPI_DriverLg4ff_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
int max_led = red;
// only g27/g29, and g923 when supported is added
if (device->product_id != USB_DEVICE_ID_LOGITECH_G29_WHEEL &&
device->product_id != USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
return SDL_Unsupported();
}
if (green > max_led) {
max_led = green;
}
if (blue > max_led) {
max_led = blue;
}
return HIDAPI_DriverLg4ff_SendLedCommand(device, (Uint8)((5 * max_led) / 255));
}
static bool HIDAPI_DriverLg4ff_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
{
// allow programs to send raw commands
return SDL_hid_write(device->dev, data, size) == size;
}
static bool HIDAPI_DriverLg4ff_SetSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
{
// On steam deck, sensors are enabled by default. Nothing to do here.
return SDL_Unsupported();
}
static void HIDAPI_DriverLg4ff_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
// remember to stop effects on haptics close, when implemented
HIDAPI_DriverLg4ff_SetJoystickLED(device, joystick, 0, 0, 0);
}
static void HIDAPI_DriverLg4ff_FreeDevice(SDL_HIDAPI_Device *device)
{
// device context is freed in SDL_hidapijoystick.c
}
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLg4ff = {
SDL_HINT_JOYSTICK_HIDAPI_LG4FF,
true,
HIDAPI_DriverLg4ff_RegisterHints,
HIDAPI_DriverLg4ff_UnregisterHints,
HIDAPI_DriverLg4ff_IsEnabled,
HIDAPI_DriverLg4ff_IsSupportedDevice,
HIDAPI_DriverLg4ff_InitDevice,
HIDAPI_DriverLg4ff_GetDevicePlayerIndex,
HIDAPI_DriverLg4ff_SetDevicePlayerIndex,
HIDAPI_DriverLg4ff_UpdateDevice,
HIDAPI_DriverLg4ff_OpenJoystick,
HIDAPI_DriverLg4ff_RumbleJoystick,
HIDAPI_DriverLg4ff_RumbleJoystickTriggers,
HIDAPI_DriverLg4ff_GetJoystickCapabilities,
HIDAPI_DriverLg4ff_SetJoystickLED,
HIDAPI_DriverLg4ff_SendJoystickEffect,
HIDAPI_DriverLg4ff_SetSensorsEnabled,
HIDAPI_DriverLg4ff_CloseJoystick,
HIDAPI_DriverLg4ff_FreeDevice,
};
#endif /* SDL_JOYSTICK_HIDAPI_LG4FF */
#endif /* SDL_JOYSTICK_HIDAPI */

View file

@ -85,6 +85,9 @@ static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
#ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
&SDL_HIDAPI_DriverXboxOne,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_LG4FF
&SDL_HIDAPI_DriverLg4ff,
#endif
};
static int SDL_HIDAPI_numdrivers = 0;
static SDL_AtomicInt SDL_HIDAPI_updating_devices;

View file

@ -40,6 +40,7 @@
#define SDL_JOYSTICK_HIDAPI_XBOXONE
#define SDL_JOYSTICK_HIDAPI_SHIELD
#define SDL_JOYSTICK_HIDAPI_STEAM_HORI
#define SDL_JOYSTICK_HIDAPI_LG4FF
// Joystick capability definitions
#define SDL_JOYSTICK_CAP_MONO_LED 0x00000001
@ -157,6 +158,7 @@ extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteamHori;
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLg4ff;
// Return true if a HID device is present and supported as a joystick of the given type
extern bool HIDAPI_IsDeviceTypePresent(SDL_GamepadType type);