From fb6bbbe5f69426af049b664a32e535f20dedb1b8 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Thu, 16 Jun 2022 18:23:32 -0500 Subject: [PATCH] Fix Gamepads Not Being Picked Up On Init Gamepads would be missed if they were already plugged in when the game started. --- src/backend/gilrs.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/backend/gilrs.rs b/src/backend/gilrs.rs index d575d8f..ce10e1d 100644 --- a/src/backend/gilrs.rs +++ b/src/backend/gilrs.rs @@ -18,12 +18,24 @@ pub enum OwnedImplementationGamepad {} pub struct ImplementationContext { context: gilrs::Gilrs, + init_gamepads: Vec, } impl ImplementationContext { pub fn new() -> Result { match gilrs::Gilrs::new() { - Ok(context) => Ok(Self { context }), + Ok(context) => { + let mut init_gamepads = Vec::new(); + + for (gamepad_id, _) in context.gamepads() { + init_gamepads.push(GamepadId(gamepad_id)); + } + + Ok(Self { + context, + init_gamepads, + }) + } Err(e) => Err(e.to_string()), } } @@ -31,6 +43,10 @@ impl ImplementationContext { impl super::Backend for ImplementationContext { fn update(&mut self, gamepads: &mut HashMap) -> Result<()> { + for gamepad in self.init_gamepads.drain(..) { + gamepads.insert(gamepad, Gamepad::new(None)); + } + for (_, gamepad) in gamepads.iter_mut() { gamepad.update_inputs(); }