Skip to content

Introduce new joypad features provided by SDL3 (WIP) #107967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Nintorch
Copy link
Contributor

@Nintorch Nintorch commented Jun 25, 2025

Partially based on #88590

Closes godotengine/godot-proposals#2829
Closes godotengine/godot-proposals#8519
Closes godotengine/godot-proposals#6930
Closes godotengine/godot-proposals#3799
Potentially closes #98008

Supersedes #88590
Supersedes #89193

Since SDL joypad driver was merged recently ( #106218 ), this PR adds support for new SDL joypad features that weren't previously possible due to older custom joypad implementations. These features include accelerometer, gyroscope, LED lights, touchpads and more.

The features that need to be done before this PR is ready for review/merge, i.e. the roadmap:

TODO when Godot starts supporting SDL for other platforms:

  • Android support
  • iOS support
  • Web support?

TODO for separate PRs:

Features that need to be discussed whether they should be implemented or not:

Here's a small project to test new joypad features (in case someone might build this WIP PR from scratch) :
JoypadTest.zip

(P.S. I opened this PR so early, because I want to let people know that this is being worked on and that I'm working on it :D)

@Calinou
Copy link
Member

Calinou commented Jun 25, 2025

Supersedes #88590

Note that #88590 also implements DualSense adaptive triggers, which isn't on the roadmap of this PR currently. This could be done in a separate PR in any case, but I thought I'd mention it as SDL supports adaptive triggers.

@Nintorch
Copy link
Contributor Author

Supersedes #88590

Note that #88590 also implements DualSense adaptive triggers, which isn't on the roadmap of this PR currently. This could be done in a separate PR in any case, but I thought I'd mention it as SDL supports adaptive triggers.

Good catch, thank you, I'll add that to the roadmap!

@Meorge
Copy link
Contributor

Meorge commented Jun 25, 2025

I'm especially excited to see Joy-Con support listed here. I think those are really fun little controllers, so much so that I started making my own C++ library for interfacing with them and a Godot GDExtension based off of that - however, obviously being a GDExtension, it doesn't mesh very well with Godot's built-in APIs. Having the support be more built-in to the engine would be awesome!

I'm not sure how much overlap there is between SDL's Joy-Con support and what my library does, but if a lot of the Joy-Con-specific features don't have SDL APIs, I may be able to help on that front 😄

@Nintorch Nintorch force-pushed the joypad-new-sdl-features branch 2 times, most recently from 9940d41 to 9e6d7d8 Compare June 26, 2025 13:25
@Nintorch
Copy link
Contributor Author

I made a list of features that need discussion before implementing, so if you think any of the listed features are important to have built into the engine, you can let me know!

@Nintorch Nintorch changed the title Add support for new SDL joypad features (gyroscope, accelerometer, LED, etc.) (WIP) Introduce new joypad features provided by SDL3 (WIP) Jun 26, 2025
@Nintorch Nintorch force-pushed the joypad-new-sdl-features branch 2 times, most recently from b39eef9 to e70bbb5 Compare June 27, 2025 18:47
@mubinulhaque
Copy link

mubinulhaque commented Jun 28, 2025

Feature request for a separate PR: querying for separate keyboards and mice (SDL_GetKeyboards and SDL_GetMice respectively)

@Calinou
Copy link
Member

Calinou commented Jun 28, 2025

Feature request for a separate PR: querying for separate keyboards and mice (SDL_GetKeyboards and SDL_GetMice respectively)

Multi-keyboard/mouse support seems subpar in SDL 3 from what I've heard, so it may not be worth the effort. This is based on what I've seen in the Perfect Dark PC port Discord (people have tried implementing it there, but quickly regretted it).

@LunaCapra
Copy link
Contributor

Will the gamepad type for GameCube controllers be included? It seems to be the only one not added from SDL_GamepadType.

@Meorge
Copy link
Contributor

Meorge commented Jun 28, 2025

I'm still very excited for Joy-Con support and would love to contribute where I can, but thinking about it more, I think specialized support for its features would fit better as a separate PR (if not some kind of plugin/addon). 😅

Maintainers should have more of a say in this of course, but I could see it being helpful to split this up into multiple smaller PRs so that each individual one is easier to test and verify.

That all being said, for this PR (whether it ends up being split later or not) I can certainly test its functionality on macOS. Let me know if/when you'd like me to give that a shot! 😄

@Nintorch
Copy link
Contributor Author

Will the gamepad type for GameCube controllers be included? It seems to be the only one not added from SDL_GamepadType.

Godot is currently using the latest stable release of SDL (3.2.16), which doesn't support GameCube controllers :( https://github.com/libsdl-org/SDL/blob/c9a6709bd21750f1ad9597be21abace78c6378c9/include/SDL3/SDL_gamepad.h

@LunaCapra
Copy link
Contributor

Will the gamepad type for GameCube controllers be included? It seems to be the only one not added from SDL_GamepadType.

Godot is currently using the latest stable release of SDL (3.2.16), which doesn't support GameCube controllers :( https://github.com/libsdl-org/SDL/blob/c9a6709bd21750f1ad9597be21abace78c6378c9/include/SDL3/SDL_gamepad.h

That's a shame. Maybe in the future then!

@Nintorch Nintorch force-pushed the joypad-new-sdl-features branch from e70bbb5 to f891f14 Compare July 2, 2025 18:17
@Meorge
Copy link
Contributor

Meorge commented Jul 2, 2025

I know this is still marked as a draft, so these may be things you're aware of and are planning on fixing, but just in case: I got excited and decided to pull it to test on macOS, and ran into some compilation errors.

In joypad_sdl.cpp, a few switch statements don't have default cases or otherwise handle all of the possible values:

drivers/sdl/joypad_sdl.cpp:335:10: error: enumeration values 'INVALID', 'SDL_MAX', and 'MAX' not handled in switch [-Werror,-Wswitch]
  335 |         switch (p_axis) {
      |                 ^~~~~~
drivers/sdl/joypad_sdl.cpp:335:10: note: add missing switch cases
  335 |         switch (p_axis) {
      |                 ^
  336 |                 case JoyAxis::LEFT_X:
drivers/sdl/joypad_sdl.cpp:451:10: error: enumeration values 'INVALID', 'SDL_MAX', and 'MAX' not handled in switch [-Werror,-Wswitch]
  451 |         switch (p_button) {
      |                 ^~~~~~~~
drivers/sdl/joypad_sdl.cpp:451:10: note: add missing switch cases
  451 |         switch (p_button) {
      |                 ^
  452 |                 case JoyButton::A:
2 errors generated.

If I add default cases to those switch statements, I get errors about the parameters in ScePadTriggerEffectParam not all being initialized:

[ 74%] In file included from core/input/.scu/scu_core_input.gen.cpp:1:
./core/input/input.cpp:1224:2: error: missing field 'padding' initializer [-Werror,-Wmissing-field-initializers]
 1224 |         SETUP_TRIGGER_EFFECT(SCE_PAD_TRIGGER_EFFECT_MODE_OFF);
      |         ^
./core/input/input.cpp:1213:40: note: expanded from macro 'SETUP_TRIGGER_EFFECT'
 1213 |         ScePadTriggerEffectParam effect = { 0 };                                                                                                \
      |                                               ^
[ 74%] ./core/input/input.cpp:1236:2: error: missing field 'padding' initializer [-Werror,-Wmissing-field-initializers]
 1236 |         SETUP_TRIGGER_EFFECT(SCE_PAD_TRIGGER_EFFECT_MODE_FEEDBACK);
      |         ^
./core/input/input.cpp:1213:40: note: expanded from macro 'SETUP_TRIGGER_EFFECT'
 1213 |         ScePadTriggerEffectParam effect = { 0 };                                                                                                \
[ 74%]                                               ^
./core/input/input.cpp:1257:2: error: missing field 'padding' initializer [-Werror,-Wmissing-field-initializers]
 1257 |         SETUP_TRIGGER_EFFECT(SCE_PAD_TRIGGER_EFFECT_MODE_WEAPON);
      |         ^
./core/input/input.cpp:1213:40: note: expanded from macro 'SETUP_TRIGGER_EFFECT'
 1213 |         ScePadTriggerEffectParam effect = { 0 };                                                                                                \
      |                                               ^
[ 74%] ./core/input/input.cpp:1279:2: error: missing field 'padding' initializer [-Werror,-Wmissing-field-initializers]
 1279 |         SETUP_TRIGGER_EFFECT(SCE_PAD_TRIGGER_EFFECT_MODE_VIBRATION);
      |         ^
./core/input/input.cpp:1213:40: note: expanded from macro 'SETUP_TRIGGER_EFFECT'
 1213 |         ScePadTriggerEffectParam effect = { 0 };                                                                   [ 74%]                        \
      |                                               ^
[ 74%] ./core/input/input.cpp:1291:2: error: missing field 'padding' initializer [-Werror,-Wmissing-field-initializers]
 1291 |         SETUP_TRIGGER_EFFECT(SCE_PAD_TRIGGER_EFFECT_MODE_MULTIPLE_POSITION_FEEDBACK);
      |         ^
[ 74%]  note: expanded from macro 'SETUP_TRIGGER_EFFECT'
 1213 |         ScePadTriggerEffectParam effect = { 0 };                                                                                                \
      |                                               ^
./core/input/input.cpp:1320:2: error: missing field 'padding' initializer [-Werror,-Wmissing-field-initializers]
[ 74%] E_SLOPE_FEEDBACK);GGER_EFFECT(SCE_PAD_TRIGGER_EFFECT_MOD
      |         ^
./core/input/input.cpp:1213:40: note: expanded from macro 'SETUP_TRIGGER_EFFECT'
 1213 |         ScePadTriggerEffectParam effect = { 0 };                                                                                                \
      |                                               ^
[ 74%] ./core/input/input.cpp:1337:2: error: missing field 'padding' initializer [-Werror,-Wmissing-field-initializers]
 1337 |         SETUP_TRIGGER_EFFECT(SCE_PAD_TRIGGER_EFFECT_MODE_MULTIPLE_POSITION_VIBRATION);
      |         ^
./core/input/input.cpp:1213:40: note: expanded from macro 'SETUP_TRIGGER_EFFECT'
 1213 |         ScePadTriggerEffectParam effect = { 0 };                                                                                                \
      |                                               ^
[ 77%] 7 errors generated.

@Nintorch Nintorch force-pushed the joypad-new-sdl-features branch from f891f14 to 3a143d4 Compare July 2, 2025 18:51
@Nintorch
Copy link
Contributor Author

Nintorch commented Jul 2, 2025

I haven't tried to compile on macOS yet, but I think I fixed these 2 errors, I hope it should now compile!

@Meorge
Copy link
Contributor

Meorge commented Jul 2, 2025

Thanks! It compiles now, but unfortunately I get a segmentation fault immediately upon starting Godot... I may be able to look further into that soon


LLDB is giving this output for the location of the segfault:

Godot Engine v4.5.beta.custom_build.3a143d442 (2025-07-02 18:50:15 UTC) - https://godotengine.org
Process 33797 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16f5ffff8)
    frame #0: 0x000000010307d4e0 godot.macos.editor.dev.arm64`dlmalloc(bytes=80) at SDL_malloc.c:4615:52
   4612     if (bytes <= MAX_SMALL_REQUEST) {
   4613       bindex_t idx;
   4614       binmap_t smallbits;
-> 4615       nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes);
   4616       idx = small_index(nb);
   4617       smallbits = gm->smallmap >> idx;
   4618
Target 0: (godot.macos.editor.dev.arm64) stopped.

The backtrace seems to give an infinite loop?

(lldb) bt
  * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16f5ffff8)
  * frame #0: 0x000000010307d4e0 godot.macos.editor.dev.arm64`dlmalloc(bytes=80) at SDL_malloc.c:4615:52
    frame #1: 0x0000000103081e94 godot.macos.editor.dev.arm64`dlcalloc(n_elements=1, elem_size=80) at SDL_malloc.c:4837:9
    frame #2: 0x0000000103084554 godot.macos.editor.dev.arm64`SDL_calloc(nmemb=1, size=80) at SDL_malloc.c:6471:11
    frame #3: 0x00000001030a14e0 godot.macos.editor.dev.arm64`SDL_CreateMutex at SDL_sysmutex.c:34:26
    frame #4: 0x00000001030a1894 godot.macos.editor.dev.arm64`SDL_CreateSemaphore(initial_value=0) at SDL_syssem.c:75:23
    frame #5: 0x0000000103090bdc godot.macos.editor.dev.arm64`SDL_CreateCondition at SDL_syscond.c:58:21
    frame #6: 0x00000001030a18a0 godot.macos.editor.dev.arm64`SDL_CreateSemaphore(initial_value=0) at SDL_syssem.c:76:26
    frame #7: 0x0000000103090bdc godot.macos.editor.dev.arm64`SDL_CreateCondition at SDL_syscond.c:58:21
    frame #8: 0x00000001030a18a0 godot.macos.editor.dev.arm64`SDL_CreateSemaphore(initial_value=0) at SDL_syssem.c:76:26
    frame #9: 0x0000000103090bdc godot.macos.editor.dev.arm64`SDL_CreateCondition at SDL_syscond.c:58:21
    ...
    frame #1322: 0x00000001030a18a0 godot.macos.editor.dev.arm64`SDL_CreateSemaphore(initial_value=0) at SDL_syssem.c:76:26
    frame #1323: 0x0000000103090bdc godot.macos.editor.dev.arm64`SDL_CreateCondition at SDL_syscond.c:58:21
    frame #1324: 0x00000001030a18a0 godot.macos.editor.dev.arm64`SDL_CreateSemaphore(initial_value=0) at SDL_syssem.c:76:26
    frame #1325: 0x0000000103090bdc godot.macos.editor.dev.arm64`SDL_CreateCondition at SDL_syscond.c:58:21
    frame #1326: 0x00000001030a18a0 godot.macos.editor.dev.arm64`SDL_CreateSemaphore(initial_value=0) at SDL_syssem.c:76:26
    frame #1327: 0x0000000103090bdc godot.macos.editor.dev.arm64`SDL_CreateCondition at SDL_syscond.c:58:21
    ...

@Nintorch
Copy link
Contributor Author

Nintorch commented Jul 3, 2025

This PR should likely fix your issue: #107963
It happened to you because I haven't rebased this PR yet, I will do that once all features are ready!

EDIT: Decided to rebase now!

@Nintorch Nintorch force-pushed the joypad-new-sdl-features branch 2 times, most recently from bf6dd95 to 0e453aa Compare July 3, 2025 06:49
This commit adds support for new SDL joypad features that weren't previously possible due to older custom joypad implementations.
These features include accelerometer, gyroscope, LED lights, touchpads and more.
@Nintorch Nintorch force-pushed the joypad-new-sdl-features branch from 0e453aa to 898c22f Compare July 3, 2025 08:10
@Meorge
Copy link
Contributor

Meorge commented Jul 3, 2025

Thanks for the rebase! With that, the editor is now building, and I'm able to run the demo project.

It looks like the instance of JoypadSDL being used by the game process doesn't receive the joystick/gamepad-added events, which causes the joypads to be registered as not attached, and thus the special methods to not work. Here's my console output with some print_line statements I added to track the state:

> godot --editor --path "/Users/malcolmanderson/Downloads/JoypadTest"
Godot Engine v4.5.beta.custom_build.898c22fd8 (2025-07-03 08:10:03 UTC) - https://godotengine.org
construct joypadsdl (FOR EDITOR)
Metal 3.2 - Forward+ - Using Device #0: Apple - Apple M4 Pro (Apple9)

joystick added event
get_sdl_joystick: p_pad_idx = 0, max is 16, attached = true
Event for joypad 0: 1619 (SDL_EVENT_GAMEPAD_ADDED)
Event for joypad 0: 1543 (SDL_EVENT_JOYSTICK_BATTERY_UPDATED)
[0]
DualSense Wireless Controller 050057564c050000e60c00001be36d04

(GAME STARTED)
Godot Engine v4.5.beta.custom_build.898c22fd8 (2025-07-03 08:10:03 UTC) - https://godotengine.org
construct joypadsdl
Metal 3.2 - Forward+ - Using Device #0: Apple - Apple M4 Pro (Apple9)

get_sdl_joystick: p_pad_idx = 0, max is 16, attached = false
ERROR: JoypadSDL::get_joypad_features: joy == nullptr
   at: get_joypad_features (drivers/sdl/joypad_sdl.cpp:584)
   GDScript backtrace (most recent call first):
       [0] _ready (res://test_2.gd:4)

Note that after GAME STARTED, a new JoypadSDL is constructed (print_line() in JoypadSDL::JoypadSDL() is run), but the SDL_EVENT_GAMEPAD_ADDED event is not caught by it, so it's not able to update its copy of the joypads array.

That's my hypothesis for what's going on at the moment, at least. I know you may still be working on putting together the core functionality for the PR, but please let me know if there's anything else I can test or results I can provide to help with the macOS support!

@Nintorch
Copy link
Contributor Author

Nintorch commented Jul 4, 2025

That's weird, because I haven't changed the "joystick added" event functionality in this PR, so it should work just fine. Do you have this bug if you test Godot 4.5 beta 2?

@Meorge
Copy link
Contributor

Meorge commented Jul 4, 2025

If I run commit efb40c1 (the commit on which this PR appears to currently be based) and comment out most of the code in the test project's test_2.gd file:

CleanShot 2025-07-03 at 22 14 42@2x

No errors appear, and the gamepad does show up as connected:
CleanShot 2025-07-03 at 22 15 07@2x

With this PR's commit, I get the error again:

ERROR: JoypadSDL::get_joypad_features: joy == nullptr
   at: get_joypad_features (drivers/sdl/joypad_sdl.cpp:584)
   GDScript backtrace (most recent call first):
       [0] _ready (res://test_2.gd:4)

Note that it's including line 4 of test_2.gd in the backtrace, but it seems to actually be attempting to point to line 3, where _ready() is defined.

I'm expecting to be busy this weekend, so I don't know how much time I'll have for it the next few days, but soon after I should hopefully be able to dive deeper into it.

@Nintorch
Copy link
Contributor Author

Nintorch commented Jul 4, 2025

Let's hope I will encounter this bug on macOS myself so I can try to fix it 😅

@Meorge
Copy link
Contributor

Meorge commented Jul 4, 2025

Indeed, that may well be more efficient 😅 But please don't hesitate to let me know if there are any specific tests or things you'd like me to take a look at, and I'll do so as soon as I can! I'm really looking forward to having this rich controller support 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment