Skip to content

Commit d92d5bf

Browse files
committed
add config option debounce_time
1 parent b07a231 commit d92d5bf

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "debouncer-udevmon"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
edition = "2024"
55
license-file = "LICENSE"
66
description = "linux keyboard debouncer with udevmon"

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ See [input.h](https://github.com/torvalds/linux/blob/master/include/uapi/linux/i
3232

3333
A key event has 3 possible values: 1 (pressed), 0 (released) or 2 (autorepeat).
3434

35-
The role of `debouncer` is that, it can delay the keyboard "release" event for some time (+12ms is ideal for my machine), which is similar to the ["spuious" mode of libinput](https://wayland.freedesktop.org/libinput/doc/latest/button-debouncing.html).
35+
The role of `debouncer` is that, it can delay the keyboard "release" event for some time (see #Configurations), which is similar to the ["spuious" mode of libinput](https://wayland.freedesktop.org/libinput/doc/latest/button-debouncing.html).
3636

3737
Once `debouncer` received a "release" event, it will wait for some time. During this time, if no "press" event of the same key comes, it will write the "release" event to `stdout`; otherwise, it will neglect this event.
3838

@@ -109,9 +109,14 @@ ExecStartPre=/usr/bin/sleep .7 # 700ms is fine on my machine
109109

110110
## Configurations
111111

112-
You can set some configurations in `/etc/debouncer.toml`. Currently supported items are:
112+
You can set some configurations in `/etc/debouncer.toml`. Here is my configurations:
113113

114-
- **`exceptions`: array of `u16` keycodes**. Keys in the exceptions will not delayed. For example, I want to neglect modifier keys such as Ctrl, Alt, Shift and Meta. Here is my config:
115114
```toml
116115
exceptions=[29,42,54,56,97,100,125]
116+
debounce_time=14
117117
```
118+
119+
Currently supported items are:
120+
121+
- **`exceptions`: array of `u16` keycodes**. Keys in the exceptions will not delayed. For example, I want to neglect modifier keys such as Ctrl, Alt, Shift and Meta.
122+
- **`debounce_time`: `u64` value in milliseconds**. Indicates how long should a release event delayed. For example, 14ms is ideal for my machine.

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod utils;
33
fn main(){
44
utils::LOGGER.init();
55
utils::CONFIG.set(utils::Config::init()).unwrap();
6-
#[allow(unused_variables)]
6+
log::info!("Loaded config: {:?}",&utils::CONFIG);
77
trpl::run(async{
88
let (tx,rx)=trpl::channel::<input::InputEvent>();
99
let tx_future=get_events(tx);
@@ -81,7 +81,7 @@ async fn delay_events(
8181
mut mpmc_rx:tokio::sync::broadcast::Receiver<u16>,
8282
keycode:u16
8383
){
84-
static DEBOUNCE_TIME:std::time::Duration=std::time::Duration::from_millis(14);
84+
let debounce_time:std::time::Duration=std::time::Duration::from_millis(config!(debounce_time));
8585
log::debug!("\x1b[37mA new thread on keycode {} created and waiting for press event...\x1b[0m",keycode);
8686
let waiting=async{
8787
while let Ok(recv_keycode)=mpmc_rx.recv().await{
@@ -91,7 +91,7 @@ async fn delay_events(
9191
}
9292
};
9393
let timer=async{
94-
trpl::sleep(DEBOUNCE_TIME).await;
94+
trpl::sleep(debounce_time).await;
9595
};
9696
if let trpl::Either::Right(_)=trpl::race(waiting,timer).await{
9797
event_cache.push(input::InputEvent::new());

src/utils.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ impl Logger{
3131
#[derive(Debug,PartialEq,Eq,PartialOrd,Ord,serde::Deserialize)]
3232
pub struct Config{
3333
pub exceptions:Vec<u16>,
34+
pub debounce_time:u64,
3435
}
3536
impl Config{
3637
pub fn new()->Self{
3738
Config{
3839
exceptions:vec![],
40+
debounce_time:14,
3941
}
4042
}
4143
pub fn init()->Self{
@@ -47,6 +49,13 @@ impl Config{
4749
return Self::new();
4850
}
4951
};
52+
let builder=match builder.set_default("debounce_time",14){
53+
Ok(b)=>b,
54+
Err(e)=>{
55+
log::error!("Failed to set default config: {}",e);
56+
return Self::new();
57+
}
58+
};
5059
let builder=builder.add_source(config::File::with_name("/etc/debouncer.toml"));
5160
let config=match builder.build(){
5261
Ok(c)=>c,
@@ -92,5 +101,8 @@ mod test{
92101
macro_rules! config{
93102
(exceptions)=>{
94103
&$crate::utils::CONFIG.get().unwrap().exceptions
95-
}
104+
};
105+
(debounce_time)=>{
106+
$crate::utils::CONFIG.get().unwrap().debounce_time
107+
};
96108
}

0 commit comments

Comments
 (0)