Skip to content
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

Don't use a separate thread when polling for gamepad events on DRM platforms #3641

Merged
merged 2 commits into from
Dec 19, 2023
Merged

Don't use a separate thread when polling for gamepad events on DRM platforms #3641

merged 2 commits into from
Dec 19, 2023

Conversation

cinghycreations
Copy link
Contributor

Hi, I recently got a Powkiddy RGB30 and I'm porting a Raylib game of mine on it. I was able to build it (using PLATFORM_DRM) and run it out-of-the-box, that is totally awesome.

The only issue it that raylib wasn't registering gamepad presses - that is, IsGamepadButtonDown was working, but IsGamepadButtonPressed was not. I started debugging it and realized that, on DRM platforms, a separate thread is created to read gamepad inputs; this approach can lead to race conditions, because both the main thread and the worker thread are writing to global variables (CORE.Input.Gamepad.currentButtonState) concurrently without locks, and by the way I wonder if that was necessary at all since PollInputEvents() is reading mouse events synchronously using read() anyway.

This fix removed the separate thread and introduces a PollGamepadEvents() function that reads the gamepad events. Written against 5.0 release

@raysan5
Copy link
Owner

raysan5 commented Dec 15, 2023

@cinghycreations Oh! Nice review! Actually there was undergoing work on that direction by @michaelfiber!

There also seems to be some branch conflicts, could you review them?

Also, I'm curious about the Powkiddy RGB30 setup for raylib, are you using some custom linux distro? Recently some raylib user ported it to the Anbernic RG353M using a custom buildroot distro.

@cinghycreations
Copy link
Contributor Author

Hi, thanks for your quick feedback. I've resolved the conflict, bear with me if I made some mistakes... it's my first pull request ever, on any project 😄

As for the Powkiddy RGB30, it runs a JelOS distro that worked immediately without any fiddling: I took the console out of the box, connected to it using SMB on Windows, and copied by Raylib game to /roms/ports - no custom distros, no hacks. The only annoyance is PortMaster, that requires to write a game.sh script and also doesn't get the button mapping right (when I press "up" it gets "R1" and so on).

By the way, I was able to use WSL (Windows Subsystem for Linux) to build the game for Windows, Linux and Raspberry Pi on Windows 11. Also, I didn't have to change anything in my code or CMake files - everything just built and worked on all three platforms.

@raysan5
Copy link
Owner

raysan5 commented Dec 15, 2023

By the way, I was able to use WSL (Windows Subsystem for Linux) to build the game for Windows, Linux and Raspberry Pi on Windows 11. Also, I didn't have to change anything in my code or CMake files - everything just built and worked on all three platforms.

Nice! That is how it is supposed to work! 😄

@raysan5
Copy link
Owner

raysan5 commented Dec 17, 2023

@michaelfiber Please, could you take a look to this PR before merging?

@raysan5 raysan5 merged commit 57a5ebe into raysan5:master Dec 19, 2023
@raysan5
Copy link
Owner

raysan5 commented Dec 19, 2023

I reviewed the code and I'm merging it. Feel free to comment if some issue is detected.

@cinghycreations
Copy link
Contributor Author

Awesome, thank you!

@SirNate0
Copy link

SirNate0 commented Feb 7, 2024

also doesn't get the button mapping right (when I press "up" it gets "R1" and so on).

@cinghycreations I know your PR is already merged, but I've been working through a very similar issue on a similar device (the R36S), and I believe I know the reason: the DRM platform directly uses the index of the button based as reported by /dev/input/js0 (from the joydev driver, I believe). There is no mapping done to the correct raylib gamepad button enum (see void PollInputEvents(void) in rcore_desktop.c for an example of such a mapping on other platforms, and you can look at the source of jstest for an example of how it can be done specifically for /dev/input/js0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants