Skip to content

Commit

Permalink
fix(CompositeDevice): Only manualy handle intercept mode target event…
Browse files Browse the repository at this point in the history
…s as a chord if it is different from the activation event.
  • Loading branch information
pastaq committed Apr 27, 2024
1 parent 69ff352 commit 59b0ba8
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ impl CompositeDevice {
}
}

for event in events {
for mut event in events {
// Process the event depending on the intercept mode
let mode = self.intercept_mode.clone();
let cap = event.as_capability();
Expand All @@ -1123,17 +1123,23 @@ impl CompositeDevice {
if self.intercept_active_inputs == self.intercept_activation_caps {
log::debug!("Found activation chord!");
self.set_intercept_mode(InterceptMode::Always);
let event = NativeEvent::new(
event = NativeEvent::new(
self.intercept_mode_target_cap.clone(),
InputValue::Bool(true),
);
let event2 = NativeEvent::new(
self.intercept_mode_target_cap.clone(),
InputValue::Bool(false),
);
let chord: Vec<NativeEvent> = vec![event, event2];
self.write_chord_events(chord).await?;
self.intercept_active_inputs.clear();
// Only handle events manually if the event is different than
// this target capability. Otherwise we can let the loop handle
// it normally.
if cap != self.intercept_mode_target_cap {
let event2 = NativeEvent::new(
self.intercept_mode_target_cap.clone(),
InputValue::Bool(false),
);
let chord: Vec<NativeEvent> = vec![event, event2];
self.write_chord_events(chord).await?;
return Ok(());
}
} else {
log::debug!(
"The events do not match what we want. Sending queued events."
Expand All @@ -1150,12 +1156,9 @@ impl CompositeDevice {
}
self.write_chord_events(chord).await?;
self.intercept_active_inputs.clear();
return Ok(());
}
};

log::debug!("Exit loop.");

return Ok(());
} else {
log::debug!("The event is already in the list. Boo!");
return Ok(());
Expand Down Expand Up @@ -1221,6 +1224,7 @@ impl CompositeDevice {
// If this event implements the DBus capability, send the event to DBus devices
if matches!(cap, Capability::DBus(_)) {
let event = TargetCommand::WriteEvent(event);
log::trace!("Emit dbus event: {:?}", event);
#[allow(clippy::for_kv_map)]
for (_, target) in &self.target_dbus_devices {
target.send(event.clone()).await?;
Expand All @@ -1232,6 +1236,7 @@ impl CompositeDevice {
// target devices.
if matches!(self.intercept_mode, InterceptMode::Always) {
let event = TargetCommand::WriteEvent(event);
log::trace!("Emit intercepted event: {:?}", event);
#[allow(clippy::for_kv_map)]
for (_, target) in &self.target_dbus_devices {
target.send(event.clone()).await?;
Expand All @@ -1241,6 +1246,7 @@ impl CompositeDevice {

// TODO: Only write the event to devices that are capabile of handling it
let event = TargetCommand::WriteEvent(event);
log::trace!("Emit passed event: {:?}", event);
#[allow(clippy::for_kv_map)]
for (_, target) in &self.target_devices {
target.send(event.clone()).await?;
Expand Down Expand Up @@ -1292,6 +1298,7 @@ impl CompositeDevice {

for event in events {
let tx = self.tx.clone();
log::debug!("Send event {:?} at sleep time {sleep_time}", event);
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(sleep_time)).await;
if let Err(e) = tx.send(Command::WriteEvent(event)) {
Expand Down

0 comments on commit 59b0ba8

Please sign in to comment.