Skip to content

script: Poll gamepad events in a seperate thread#44792

Draft
log101 wants to merge 1 commit into
servo:mainfrom
log101:main
Draft

script: Poll gamepad events in a seperate thread#44792
log101 wants to merge 1 commit into
servo:mainfrom
log101:main

Conversation

@log101
Copy link
Copy Markdown
Contributor

@log101 log101 commented May 7, 2026

In order to reliably listen for gamepad events this PR is polling gamepad events in seperate thread and sending them as AppEvents via event loop proxy. The PR is working but not complete yet, I'd like to ask for a feedback first on some issues before moving further:

  1. I'm polling Gilrs gamepad events in an infinite loop. What should be the exit condition from the loop? Or should it sleep for some time if handle.next_event returns None?
  2. I'm initiating Gilrs handle in a seperate thread, how can I share the handle with the haptic feedback handler (or other functions) while continuously polling it?

Testing: TBD
Fixes: #44575 #41448

};

loop {
while let Some(event) = handle.next_event() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next_event_blocking will make this loop much more efficient.

event_loop_proxy
.clone()
.send_event(AppEvent::Gamepad(event, name.to_owned(), index))
.expect("Coulnt access to event loop proxy!");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking out of the loop when we can no longer communicate with the event loop will avoid errors during shutdown.

let index = request.gamepad_index();
let GamepadHapticEffectType::DualRumble(params) = effect_type;

let mut handle = self.handle.borrow_mut();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My instinct here is to use an Arc<Mutex<Gilrs>> that's shared between the gilrs thread and the main thread.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, that probably interacts poorly with next_event_blocking. Perhaps next_event plus the sleep when there's no event, like you mentioned?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option: use next_event_blocking with a timeout, and add a channel to the gilrs thread which is used to listen for haptic requests. The main thread code that receives a haptic request sends the relevant data through the sender; the gilrs thread uses try_recv on the receiver after each next_event_blocking.

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.

The gamepadconnected event doesn't fire in headed mode

2 participants