diff --git a/Cargo.lock b/Cargo.lock index f39b192..c5b7c57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,7 +169,7 @@ checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" [[package]] name = "kb-quiver" -version = "0.2.0" +version = "0.3.0" dependencies = [ "clap", "evdev", diff --git a/Cargo.toml b/Cargo.toml index b6981e9..55b97c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kb-quiver" -version = "0.2.0" +version = "0.3.0" edition = "2024" authors = ["Penelope Gwen ", "TerminalLesbian "] license-file = "LICENSE.md" diff --git a/debian/changelog b/debian/changelog index 9478bb0..57bb81b 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +kb-quiver 0.3.0-1 bookworm; urgency=medium + + * add support for configuring vibration length via config file which is read on start + * detect event device using evdev crate + + -- Penelope Gwen Wed, 04 Apr 2026 00:18:54 -0700 + kb-quiver 0.2.0-1 bookworm; urgency=medium * add systemd service diff --git a/debian/postinst b/debian/postinst index 6060c4f..3ec5b5a 100755 --- a/debian/postinst +++ b/debian/postinst @@ -1,4 +1,6 @@ #!/bin/sh set -e +echo "50" | tee /etc/kb-haptic-length + #DEBHELPER# diff --git a/debian/postrm b/debian/postrm new file mode 100755 index 0000000..62c9e24 --- /dev/null +++ b/debian/postrm @@ -0,0 +1,7 @@ +#!/bin/sh + +set -e + +rm /etc/kb-haptic-length || true + +#DEBHELPER# diff --git a/src/main.rs b/src/main.rs index b3badb5..498863c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,11 @@ -use std::{path::{Path}, fs::{File, OpenOptions}}; -use std::io::{Read, Write}; use clap::Parser; -use regex::Regex; use evdev::*; +use regex::Regex; +use std::io::{Read, Write}; +use std::{ + fs::{File, OpenOptions}, + path::Path, +}; const DEVICES_PATH: &str = "/proc/bus/input/devices"; const HAPTIC_PATH: &str = "/sys/class/timed_output/vibrator/enable"; @@ -10,7 +13,7 @@ const HAPTIC_PATH: &str = "/sys/class/timed_output/vibrator/enable"; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - /// vibration duration in milliseconds + /// config file containing vibration duration in milliseconds #[arg(short, long, default_value_t = String::from("/etc/kb-haptic-length"))] config_path: String, } @@ -20,43 +23,67 @@ fn main() { let haptic_length: String = { let mut str = String::new(); - - match File::open(&Path::new(&cli.config_path)) { + + match File::open(Path::new(&cli.config_path)) { Ok(mut f) => match f.read_to_string(&mut str) { Ok(_) => (), - Err(_) => { str.push_str("50"); } + Err(_) => { + str.push_str("50"); + } + }, + _ => { + str.push_str("50"); } - _ => { str.push_str("50"); } } str }; - + let mut haptic = OpenOptions::new() .append(true) .open(HAPTIC_PATH) - .expect(&format!("Unable to access haptic drivers at {} for writing!", HAPTIC_PATH)); + .expect(&format!( + "Unable to access haptic drivers at {} for writing!", + HAPTIC_PATH + )); - let devices_list: String = { + /*let devices_list: String = { let mut str = String::new(); - - File::open(Path::new(DEVICES_PATH)).expect(&format!("Unable to open {} to read devices list!", DEVICES_PATH)) - .read_to_string(&mut str).expect(&format!("Unable to read devices list at {}!", DEVICES_PATH)); - + + File::open(Path::new(DEVICES_PATH)) + .expect(&format!( + "Unable to open {} to read devices list!", + DEVICES_PATH + )) + .read_to_string(&mut str) + .expect(&format!("Unable to read devices list at {}!", DEVICES_PATH)); + str }; let kbd_regex = Regex::new(r"(?ms)Integrated keyboard(.*?)kbd (?.*?) \n").unwrap(); - let event_num = &kbd_regex.captures(&devices_list).expect(&format!("No valid keyboard devices found in {}", DEVICES_PATH))["event"]; + let event_num = &kbd_regex.captures(&devices_list).expect(&format!( + "No valid keyboard devices found in {}", + DEVICES_PATH + ))["event"];*/ - let device_path = format!("/dev/input/{}", event_num); - let mut device = Device::open(Path::new(&device_path)).expect(&format!("Unable to open device at {}", &device_path)); + let device_path = enumerate() + .find(|x| { + x.1.name() + .unwrap_or_default() + .contains("Integrated keyboard") + }) + .expect("No valid keyboard device found") + .0; + + //let device_path = format!("/dev/input/{}", event_num); + let mut device = + Device::open(&device_path).expect(&format!("Unable to open device at {:?}", &device_path)); loop { for event in device.fetch_events().unwrap() { - if let EventSummary::Key(_, _, 1) = event.destructure() { - match write!(haptic, "{}", haptic_length) { - Err(_) => println!("Unable to write to haptics!"), - _ => () - }; + if let EventSummary::Key(_, _, 1) = event.destructure() + && write!(haptic, "{}", haptic_length).is_err() + { + println!("Unable to write to haptics!") } } }