mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-16 11:11:30 +03:00
debounce release log
This commit is contained in:
@@ -18,8 +18,10 @@ license = "GPL-3.0-or-later"
|
|||||||
repository = "https://github.com/feschber/lan-mouse"
|
repository = "https://github.com/feschber/lan-mouse"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
strip = true
|
codegen-units = 1
|
||||||
lto = "fat"
|
lto = "fat"
|
||||||
|
strip = true
|
||||||
|
panic = "abort"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
input-event = { path = "input-event", version = "0.3.0" }
|
input-event = { path = "input-event", version = "0.3.0" }
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
use std::{
|
||||||
|
cell::Cell,
|
||||||
|
time::{Duration, Instant},
|
||||||
|
};
|
||||||
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use input_capture::{
|
use input_capture::{
|
||||||
CaptureError, CaptureEvent, CaptureHandle, InputCapture, InputCaptureError, Position,
|
CaptureError, CaptureEvent, CaptureHandle, InputCapture, InputCaptureError, Position,
|
||||||
@@ -117,6 +122,27 @@ async fn do_capture(
|
|||||||
Ok(())
|
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(
|
async fn handle_capture_event(
|
||||||
server: &Server,
|
server: &Server,
|
||||||
capture: &mut InputCapture,
|
capture: &mut InputCapture,
|
||||||
@@ -142,7 +168,8 @@ async fn handle_capture_event(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = conn.send(event, handle).await {
|
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?;
|
capture.release().await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user