Repository navigation
Rebind a joystick when it is plugged back in - #7888
Open
dustinspace217 wants to merge 2 commits into
Open
dustinspace217 wants to merge 2 commits into
dustinspace217 wants to merge 2 commits into
Conversation
When a joystick the game already knew about was reconnected, the device handler refreshed it and returned before the slot-binding code ran, so the stick stayed unassigned until the game restarted. Fall through to the binding code instead, which also restarts force feedback on Joy-0. Along with that, in the same handler: - Ignore the add events SDL sends at startup for sticks that init() already set up, so they are not bound a second time. - Only treat a stick that is currently unplugged as one coming back. Two sticks of the same model share a GUID, and plugging in the second one used to take over the first. When several known sticks share a GUID, a saved choice now picks the one that is plugged in. - Drop the shortcut that skipped binding when every slot was full; it skipped the rebind for a stick chosen again while unplugged. - Close the extra handle opened to check the new device, which leaked one handle per plug-in. - Keep the old handles open until the stick has its slot again, and log when a known stick reconnects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
A joystick or gamepad that gets unplugged and plugged back in during the game works again right away, instead of staying dead until you restart.
Why?
If your stick's USB cable gets bumped mid-mission, or you move it to another port, the game sees it come back but never gives it back its control slot. Steering and buttons do nothing until you quit and relaunch.
How?
When SDL reports a device being added,
device_event_handlerchecks whether it already knows that stick by GUID. If it did, it refreshed the stored stick and returned early, skipping the code that puts the stick back in its Joy-0..3 slot. The unplug handler had already emptied that slot, so the stick stayed unassigned. Now a returning stick goes through the same binding code as a new one, which also restarts force feedback on Joy-0.A few things in the same handler came along with it:
init()has already set up. Those are now skipped by instance ID, so a stick isn't bound twice on every launch.Testing?
Linux Debug builds, master against this branch, SM1-01 on retail data. I used a virtual USB joystick that a script can unplug and replug, and measured the player ship's turn rate with the stick pushed:
Joy0GUIDsetting.Not covered: real hardware, force feedback (the virtual stick has none), and Windows/macOS.
Anything Else?
Two older limits are left alone: with two identical sticks plugged in at once, the saved setting can't tell them apart (same as starting the game with both plugged in), and the unplug handler only clears the first slot a stick is in. Written with Claude Code; the investigation and testing ran on my machine.