diff --git a/Cargo.toml b/Cargo.toml index 4c529e6..b74d65f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,8 +18,10 @@ license = "GPL-3.0-or-later" repository = "https://github.com/feschber/lan-mouse" [profile.release] -strip = true +codegen-units = 1 lto = "fat" +strip = true +panic = "abort" [dependencies] input-event = { path = "input-event", version = "0.3.0" } diff --git a/src/capture.rs b/src/capture.rs index f19f2ec..4337824 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -1,3 +1,8 @@ +use std::{ + cell::Cell, + time::{Duration, Instant}, +}; + use futures::StreamExt; use input_capture::{ CaptureError, CaptureEvent, CaptureHandle, InputCapture, InputCaptureError, Position, @@ -117,6 +122,27 @@ async fn do_capture( Ok(()) } +thread_local! { + static PREV_LOG: Cell> = Cell::new(None); +} + +/// debounce a statement `$st`, i.e. the statement is executed only if the +/// time since the previous execution is at most `$dur`. +/// `$prev` is used to keep track of this timestamp +macro_rules! debounce { + ($prev:ident, $dur:expr, $st:stmt) => { + let exec = match $prev.get() { + None => true, + Some(instant) if instant.elapsed() > $dur => true, + _ => false, + }; + if exec { + $prev.replace(Some(Instant::now())); + $st + } + }; +} + async fn handle_capture_event( server: &Server, capture: &mut InputCapture, @@ -142,7 +168,8 @@ async fn handle_capture_event( }; if let Err(e) = conn.send(event, handle).await { - log::warn!("releasing capture: {e}"); + const DUR: Duration = Duration::from_millis(500); + debounce!(PREV_LOG, DUR, log::warn!("releasing capture: {e}")); capture.release().await?; } Ok(())