From 0942fd6a1b32d4b3584206bdb335593f7ac79db3 Mon Sep 17 00:00:00 2001 From: Tommy Brunn Date: Sun, 15 Nov 2026 17:36:37 +0100 Subject: [PATCH] fix(CompositeDevice): Release axis direction keys when crossing center Axis direction mappings only matched events on their own side of center, so a key mapped to a stick direction stayed held when the stick crossed center within a single report. Button targets of these mappings are now translated on both sides of center so they get released, relying on the existing active input tracking to drop redundant releases. Fixes #813 Assisted-by: LLM --- src/input/capability.rs | 16 +++--- src/input/composite_device/mod.rs | 83 ++++++++++++++++++------------- src/input/event/value.rs | 66 +++++------------------- 3 files changed, 72 insertions(+), 93 deletions(-) diff --git a/src/input/capability.rs b/src/input/capability.rs index c9de417ae..81226d7b9 100644 --- a/src/input/capability.rs +++ b/src/input/capability.rs @@ -32,17 +32,21 @@ impl Capability { /// emit only a `1` or a `-1` as do other devices like mouse wheels. pub fn is_momentary_translation(&self, target: &Capability) -> bool { if let Capability::Gamepad(Gamepad::Dial(_)) = self { - matches!( - target, - Capability::Gamepad(Gamepad::Button(_)) - | Capability::Mouse(Mouse::Button(_)) - | Capability::Keyboard(_) - ) + target.is_button() } else { false } } + pub fn is_button(&self) -> bool { + matches!( + self, + Capability::Gamepad(Gamepad::Button(_)) + | Capability::Mouse(Mouse::Button(_)) + | Capability::Keyboard(_) + ) + } + pub fn to_capability_string(&self) -> String { match self { Capability::Gamepad(gamepad) => match gamepad { diff --git a/src/input/composite_device/mod.rs b/src/input/composite_device/mod.rs index b7045ba52..aa170b154 100644 --- a/src/input/composite_device/mod.rs +++ b/src/input/composite_device/mod.rs @@ -951,29 +951,27 @@ impl CompositeDevice { /// Translate and write the given event to the appropriate target devices async fn handle_event(&mut self, event: NativeEvent) -> Result<(), Box> { - // Check if we need to reverse the event list. - let is_pressed = event.pressed(); - // Check if this is is a single event or multiple events. - let mut is_chord = false; + let source_pressed = event.pressed(); // Track the delay for chord events. let mut sleep_time = 0; // Translate the event using the device profile. - let mut events = if self.device_profile.is_some() { - self.translate_event(&event) - .await? + let (mut events, is_chord) = if self.device_profile.is_some() { + let (events, is_chord) = self.translate_event(&event).await?; + let events: Vec = events .into_iter() .filter_map(|event| self.filter_event(event)) - .collect() + .collect(); + let is_chord = is_chord && events.len() > 1; + (events, is_chord) } else { - vec![event] + (vec![event], false) }; // Check if we need to reverse the event list. - if events.len() > 1 { + if is_chord { //log::trace!("Got chord: {events:?}"); - is_chord = true; - if !is_pressed { + if !source_pressed { events = events.into_iter().rev().collect(); // To support on_release events, we need to sleep past the time it takes to emit // the down events. @@ -986,6 +984,9 @@ impl CompositeDevice { for event in events { let cap = event.as_capability(); + // A translated event can be a release even if the source event + // is pressed, such as an axis returning to center. + let is_pressed = event.pressed(); // Track what is currently active so we can ignore extra events. match cap { @@ -1090,12 +1091,7 @@ impl CompositeDevice { }; let target_cap = event.as_capability(); // Handle only button presses - if !matches!( - target_cap, - Capability::Gamepad(Gamepad::Button(_)) - | Capability::Keyboard(_) - | Capability::Mouse(Mouse::Button(_)) - ) { + if !target_cap.is_button() { return Some(event); } let pressed = event.pressed(); @@ -1494,33 +1490,40 @@ impl CompositeDevice { } /// Translates the given event into a Vec of events based on the currently loaded - /// [DeviceProfile] + /// [DeviceProfile]. Also returns true if a single mapping produced multiple + /// events that should be emitted as a chord. async fn translate_event( &self, event: &NativeEvent, - ) -> Result, Box> { + ) -> Result<(Vec, bool), Box> { // Lookup the profile mapping associated with this event capability. If // none is found, return the original un-translated event. let source_cap = event.as_capability(); + let is_axis = matches!(source_cap, Capability::Gamepad(Gamepad::Axis(_))); if let Some(mappings) = self.device_profile_config_map.get(&source_cap) { - // Find which mappings in the device profile matches this source event - let matched_mappings = mappings - .iter() - .filter(|mapping| mapping.source_matches_properties(event)); - let mut events = Vec::new(); - // Based on all found mappings, translate the event - for mapping in matched_mappings { - log::trace!( - "Found translation for event {:?} in profile mapping: {}", - source_cap, - mapping.name - ); + let mut is_chord = false; + for mapping in mappings.iter() { + let mapping_start = events.len(); + let matches = mapping.source_matches_properties(event); + if matches { + log::trace!( + "Found translation for event {:?} in profile mapping: {}", + source_cap, + mapping.name + ); + } // Translate the event into the defined target event(s) for target_event in mapping.target_events.iter() { // TODO: We can cache this conversion for faster translation let target_cap: Capability = target_event.clone().into(); + // Buttons mapped to an axis direction must be released when + // the axis crosses to the other side of center. + let is_axis_button = is_axis && target_cap.is_button(); + if !matches && !is_axis_button { + continue; + } let result = event.get_value().translate( &source_cap, &mapping.source_event, @@ -1583,12 +1586,24 @@ impl CompositeDevice { let event = NativeEvent::new_translated(source_cap.clone(), target_cap, value); events.push(event); } + is_chord |= events.len() - mapping_start > 1; + } + + // When several axis directions map to the same button, a release + // from one direction must not cancel a press from another. + if is_axis { + let pressed: Vec = events + .iter() + .filter(|event| event.pressed()) + .map(|event| event.as_capability()) + .collect(); + events.retain(|event| event.pressed() || !pressed.contains(&event.as_capability())); } - return Ok(events); + return Ok((events, is_chord)); } log::trace!("No translation mapping found for event: {:?}", source_cap); - Ok(vec![event.clone()]) + Ok((vec![event.clone()], false)) } /// Executed whenever a source device is added to this [CompositeDevice]. diff --git a/src/input/event/value.rs b/src/input/event/value.rs index 253579dab..5c860b9e3 100644 --- a/src/input/event/value.rs +++ b/src/input/event/value.rs @@ -950,11 +950,6 @@ impl InputValue { // Get the threshold to consider the axis as 'pressed' or not let threshold = axis.deadzone.unwrap_or(0.3); if let Some(direction) = axis.direction.as_ref() { - // TODO: Axis input is a special case where we need - // to keep track of the state of the axis and only - // emit events whenever the axis passes or falls - // below the defined threshold - // Get the axis value let (x, y) = match self { InputValue::Vector2 { x, y } => (*x, *y), @@ -962,59 +957,24 @@ impl InputValue { _ => (None, None), }; - match direction.as_str() { + // Events that don't include the axis for this direction + // say nothing about whether it is pressed. + let pressed = match direction.as_str() { // Left should be a negative value - "left" => { - if let Some(x) = x { - if x <= -threshold { - Ok(InputValue::Bool(true)) - } else { - Ok(InputValue::Bool(false)) - } - } else { - Ok(InputValue::Bool(false)) - } - } + "left" => x.map(|x| x <= -threshold), // Right should be a positive value - "right" => { - if let Some(x) = x { - if x >= threshold { - Ok(InputValue::Bool(true)) - } else { - Ok(InputValue::Bool(false)) - } - } else { - Ok(InputValue::Bool(false)) - } - } + "right" => x.map(|x| x >= threshold), // Up should be a negative value - "up" => { - if let Some(y) = y { - if y <= -threshold { - Ok(InputValue::Bool(true)) - } else { - Ok(InputValue::Bool(false)) - } - } else { - Ok(InputValue::Bool(false)) - } - } + "up" => y.map(|y| y <= -threshold), // Down should be a positive value - "down" => { - if let Some(y) = y { - if y >= threshold { - Ok(InputValue::Bool(true)) - } else { - Ok(InputValue::Bool(false)) - } - } else { - Ok(InputValue::Bool(false)) - } + "down" => y.map(|y| y >= threshold), + _ => { + return Err(TranslationError::InvalidSourceConfig(format!( + "Invalid axis direction: {direction}" + ))) } - _ => Err(TranslationError::InvalidSourceConfig(format!( - "Invalid axis direction: {direction}" - ))), - } + }; + Ok(pressed.map_or(InputValue::None, InputValue::Bool)) } else { Err(TranslationError::InvalidSourceConfig( "No axis direction defined to translate button to axis".to_string(),