debounce release log

This commit is contained in:
Ferdinand Schober
2024-09-08 01:33:25 +02:00
parent 7f0815facf
commit 7baacf4546
2 changed files with 31 additions and 2 deletions

View File

@@ -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" }

View File

@@ -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<Option<Instant>> = 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(())