From d3ad2cf239d6b401ab8f5ec71687f9f4cf46689b Mon Sep 17 00:00:00 2001 From: terminallesbian Date: Thu, 2 Apr 2026 00:06:21 -0500 Subject: [PATCH] Add auto-detection of input device + write directly to haptics --- Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 879bb91..f39b192 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "1.0.0" @@ -164,6 +173,7 @@ version = "0.2.0" dependencies = [ "clap", "evdev", + "regex", ] [[package]] @@ -172,6 +182,12 @@ version = "0.2.184" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48f5d2a454e16a5ea0f4ced81bd44e4cfc7bd3a507b61887c99fd3538b28e4af" +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + [[package]] name = "nix" version = "0.29.0" @@ -214,6 +230,35 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "strsim" version = "0.11.1" diff --git a/Cargo.toml b/Cargo.toml index 7b855e0..b6981e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ description = "time to have fun with your cosmo communicator" [dependencies] clap = { version = "4.6.0", features = ["derive"] } evdev = "0.13.2" +regex = "1.12.3" [package.metadata.deb] changelog = "debian/changelog" diff --git a/src/main.rs b/src/main.rs index 442c666..db53c71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,48 @@ -use std::{path::PathBuf, process::exit}; - +use std::{path::{Path, PathBuf}, fs::{File, OpenOptions}, process::exit}; +use std::io::{Read, Write}; use clap::Parser; +use regex::Regex; use evdev::*; +const DEVICES_PATH: &str = "/proc/bus/input/devices"; +const HAPTIC_PATH: &str = "/sys/class/timed_output/vibrator/enable"; + #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - /// keyboard device - #[arg(short, long)] - device: PathBuf, - /// vibration duration in milliseconds - #[arg(short, long, default_value_t = 100)] + #[arg(short, long, default_value_t = 50)] length: usize, } fn main() { let cli = Args::parse(); - if !cli.device.exists() { - eprintln!("event device does not exist"); - exit(1) - } + let mut haptic = OpenOptions::new() + .append(true) + .open(HAPTIC_PATH) + .expect(&format!("Unable to access haptic drivers at {} for writing!", HAPTIC_PATH)); - let mut device = Device::open(cli.device).unwrap(); + 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)); + + 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 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)); loop { for event in device.fetch_events().unwrap() { if let EventSummary::Key(_, _, 1) = event.destructure() { - println!("{}", cli.length); + match write!(haptic, "{}", cli.length) { + Err(_) => println!("Unable to write to haptics!"), + _ => () + }; } } }