Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub enum InterceptMode {
GamepadOnly,
}

/// The minimum time permitted between DOWN and UP button events for
/// userspace to see a valid input.
const MIN_EVENT_TIME: Duration = Duration::from_millis(16);

/// A [CompositeDevice] represents any number source input devices that
/// can translate input to any target devices
#[derive(Debug)]
Expand Down Expand Up @@ -1138,13 +1142,12 @@ impl CompositeDevice {
self.is_new_active_event(&cap, event.pressed());
// Check to see if the event is in recently translated.
// If it is, spawn a task to delay emit the event.
let sleep_time = Duration::from_millis(4);
let cap = event.as_capability();
if self.translated_recent_events.contains(&cap) {
if self.translated_recent_events.contains(&cap) && !event.pressed() {
log::debug!("Event emitted too quickly. Delaying emission.");
let tx = self.tx.clone();
tokio::task::spawn(async move {
tokio::time::sleep(sleep_time).await;
tokio::time::sleep(MIN_EVENT_TIME).await;
if let Err(e) = tx.send(CompositeCommand::WriteEvent(event)).await {
log::error!("Failed to send delayed event command: {:?}", e);
}
Expand All @@ -1159,7 +1162,7 @@ impl CompositeDevice {
// Spawn a task to remove the event from recent translated
let tx = self.tx.clone();
tokio::task::spawn(async move {
tokio::time::sleep(sleep_time).await;
tokio::time::sleep(MIN_EVENT_TIME).await;
if let Err(e) = tx.send(CompositeCommand::RemoveRecentEvent(cap)).await {
log::error!("Failed to send remove recent event command: {:?}", e);
}
Expand Down Expand Up @@ -1434,16 +1437,15 @@ impl CompositeDevice {

// Emit the translated events. If this translated event has been emitted
// very recently, delay sending subsequent events of the same type.
let sleep_time = Duration::from_millis(4);
for event in emit_queue {
// Check to see if the event is in recently translated.
// If it is, spawn a task to delay emit the event.
let cap = event.as_capability();
if self.translated_recent_events.contains(&cap) {
if self.translated_recent_events.contains(&cap) && !event.pressed() {
log::debug!("Event emitted too quickly. Delaying emission.");
let tx = self.tx.clone();
tokio::task::spawn(async move {
tokio::time::sleep(sleep_time).await;
tokio::time::sleep(MIN_EVENT_TIME).await;
if let Err(e) = tx.send(CompositeCommand::HandleEvent(event)).await {
log::error!("Failed to send delayed event command: {:?}", e);
}
Expand All @@ -1458,7 +1460,7 @@ impl CompositeDevice {
// Spawn a task to remove the event from recent translated
let tx = self.tx.clone();
tokio::task::spawn(async move {
tokio::time::sleep(sleep_time).await;
tokio::time::sleep(MIN_EVENT_TIME).await;
if let Err(e) = tx.send(CompositeCommand::RemoveRecentEvent(cap)).await {
log::error!("Failed to send remove recent event command: {:?}", e);
}
Expand Down
Loading