use evdev to identify devices, manage config file on (un)install
Reviewed-on: #1
This commit is contained in:
commit
367d4c3c11
6 changed files with 51 additions and 30 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -169,7 +169,7 @@ checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
|||
|
||||
[[package]]
|
||||
name = "kb-quiver"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"evdev",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "kb-quiver"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
edition = "2024"
|
||||
authors = ["Penelope Gwen <support@pogmom.me>", "TerminalLesbian <valeradhd@gmail.com>"]
|
||||
license-file = "LICENSE.md"
|
||||
|
|
|
|||
7
debian/changelog
vendored
7
debian/changelog
vendored
|
|
@ -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 <support@pogmom.me> Wed, 04 Apr 2026 00:18:54 -0700
|
||||
|
||||
kb-quiver 0.2.0-1 bookworm; urgency=medium
|
||||
|
||||
* add systemd service
|
||||
|
|
|
|||
2
debian/postinst
vendored
2
debian/postinst
vendored
|
|
@ -1,4 +1,6 @@
|
|||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
echo "50" | tee /etc/kb-haptic-length
|
||||
|
||||
#DEBHELPER#
|
||||
|
|
|
|||
7
debian/postrm
vendored
Executable file
7
debian/postrm
vendored
Executable file
|
|
@ -0,0 +1,7 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
rm /etc/kb-haptic-length || true
|
||||
|
||||
#DEBHELPER#
|
||||
61
src/main.rs
61
src/main.rs
|
|
@ -1,16 +1,17 @@
|
|||
use std::{path::{Path}, fs::{File, OpenOptions}};
|
||||
use std::io::{Read, Write};
|
||||
use clap::Parser;
|
||||
use regex::Regex;
|
||||
use evdev::*;
|
||||
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";
|
||||
|
||||
#[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 +21,47 @@ 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 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 (?<event>.*?) \n").unwrap();
|
||||
let event_num = &kbd_regex.captures(&devices_list).expect(&format!("No valid keyboard devices found in {}", DEVICES_PATH))["event"];
|
||||
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(Path::new(&device_path)).expect(&format!("Unable to open device at {}", &device_path));
|
||||
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!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue