Skip to content

Commit

Permalink
improv: extract filter_map to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
imLinguin committed Feb 3, 2025
1 parent d5e3165 commit 3b18049
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,45 +838,12 @@ impl CompositeDevice {
// Track the delay for chord events.
let mut sleep_time = 0;

let src_cap = event.as_capability();
// Translate the event using the device profile.
let mut events = if self.device_profile.is_some() {
self.translate_event(&event)
.await?
.into_iter()
// Filter out input-cancelling events that do not come from same
// capability as the initiator
.filter_map(|event| {
let target_cap = event.as_capability();
// Handle only button presses
if !matches!(
target_cap,
Capability::Gamepad(Gamepad::Button(_))
| Capability::Keyboard(_)
| Capability::Mouse(Mouse::Button(_))
) {
return Some(event);
}
let pressed = event.pressed();
match self.exclusive_inputs.entry(target_cap) {
Entry::Vacant(e) => {
if pressed {
e.insert(src_cap.clone());
}
Some(event)
}
Entry::Occupied(e) => {
if e.get() == &src_cap {
if !pressed {
e.remove();
}
Some(event)
} else {
None
}
}
}
})
.filter_map(|event| self.filter_event(event))
.collect()
} else {
vec![event]
Expand Down Expand Up @@ -993,6 +960,42 @@ impl CompositeDevice {
log::debug!("No other buttons are pressed and this is not the first in the list. Do not hold input.");
false
}
// Filter out input-cancelling events that do not come from same
// capability as the initiator
fn filter_event(&mut self, event: NativeEvent) -> Option<NativeEvent> {
let Some(src_cap) = event.get_source_capability() else {
return Some(event);
};
let target_cap = event.as_capability();
// Handle only button presses
if !matches!(
target_cap,
Capability::Gamepad(Gamepad::Button(_))
| Capability::Keyboard(_)
| Capability::Mouse(Mouse::Button(_))
) {
return Some(event);
}
let pressed = event.pressed();
match self.exclusive_inputs.entry(target_cap) {
Entry::Vacant(e) => {
if pressed {
e.insert(src_cap.clone());
}
Some(event)
}
Entry::Occupied(e) => {
if e.get() == &src_cap {
if !pressed {
e.remove();
}
Some(event)
} else {
None
}
}
}
}

/// Writes the given event to the appropriate target device.
async fn write_event(&self, event: NativeEvent) -> Result<(), Box<dyn Error>> {
Expand Down

0 comments on commit 3b18049

Please sign in to comment.