Pi66

pind
Simple Rust hotkey daemon.
git clone https://git.pi66.xyz/pind

← back to log

improve less code & fix config

author: 6z7y
date: 2025-09-06 14:02
hash: 807749d5b20947fabcf2e76e0d652dbe12b1bc8e

Diffstat:

M

pindrc
4 ++-

M

src/main.rs
46 +++++++++---------------------
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
diff --git a/pindrc b/pindrc
index 4fa92bb..f94d7f2 100644
--- a/pindrc
+++ b/pindrc
@@ -1,5 +1,5 @@
-ctrl + shift +up                  =>   light -A 10 ; notify-send "Light" "Light Up %$(light)"
-Ctrl_left + shiFt + uP            =>   doas light -U 10 ; notify-send "Light" "Light Down %$(light)"
+ctrl + shift +up                  =>   doas light -A 10 ; notify-send "Light" "Light Up %$(light)"
+Ctrl_left + shiFt + Down          =>   doas light -U 10 ; notify-send "Light" "Light Down %$(light)"
 meTa_right + a                    =>   set file (ls ~/learn/book | wmenu); and test -n "<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>"</mi><mi>;</mi><mi>a</mi><mi>n</mi><mi>d</mi><mi>z</mi><mi>a</mi><mi>t</mi><mi>h</mi><mi>u</mi><mi>r</mi><mi>a</mi><mtext>&#x000A0;</mtext><mo>&#x0002F;</mo><mi>l</mi><mi>e</mi><mi>a</mi><mi>r</mi><mi>n</mi><mo>&#x0002F;</mo><mi>b</mi><mi>o</mi><mi>o</mi><mi>k</mi><mo>&#x0002F;</mo><mi>"</mi></mrow></math>file"
 Ctrl_left + shift_right + right   =>   pactl set-sink-volume @DEFAULT_SINK@ +10% ; notify-send "Sound" "Volume Up %$(pamixer --get-volume)"
 ctrl+ shift+left                  =>   pactl set-sink-volume @DEFAULT_SINK@ -10% ; notify-send "Sound" "Volume Down %$(pamixer --get-volume)"
diff --git a/src/main.rs b/src/main.rs
index bef3fed..052ed33 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,7 +8,7 @@ static BINDINGS: OnceLock<Arc<Vec<(AttributeSet<KeyCode>, String)>>> = OnceLock:

 fn get_bindings() -> Arc<Vec<(AttributeSet<KeyCode>, String)>>
 {
-    BINDINGS.get_or_init(|| Arc::new(load_config(CONFIG))).clone()
+    Arc::clone(BINDINGS.get_or_init(|| Arc::new(load_config(CONFIG))))
 }

 fn error(title: &str, message: &str) -> !
@@ -20,15 +20,15 @@ fn error(title: &str, message: &str) -> !
 fn run(command: &str, user: &str)
 {
     let shell = var("SHELL").unwrap_or_else(|_| "sh".into());
-    Command::new("runuser")
+    if let Err(e) = Command::new("runuser")
         .args(["-u", user, "--", &shell, "-c", command])
         .stdin(Stdio::null())
         .stdout(Stdio::null())
         .stderr(Stdio::null())
-        .spawn()
-        .unwrap_or_else(|e| error("Command execution failed", &e.to_string()));
+        .spawn() { error("Command execution failed", &e.to_string()) }
 }

+
 fn key_to_keycode(input: &str) -> AttributeSet<KeyCode> 
 {
     let mut attribute_set = AttributeSet::new();
@@ -161,7 +161,7 @@ fn load_config(config: &str) -> Vec<(AttributeSet<KeyCode>, String)>

 fn read_keys(kc: &[(AttributeSet<KeyCode>, String)], kbs: PathBuf, delay: u64, user: String)
 {
-    let mut state: Vec<(u64, u64)> = vec![(0, 0); kc.len()];
+    let mut state = vec![(0, 0); kc.len()];
     let mut tick = 1;
     let init = (320 + delay - 1) / delay;

@@ -174,43 +174,27 @@ fn read_keys(kc: &[(AttributeSet<KeyCode>, String)], kbs: PathBuf, delay: u64, u
     // First, get all supported keys from the physical device to set up the virtual device properly
     let supported_keys = match device.supported_keys() {
         Some(keys) => keys,
-        _ => {
-            eprintln!("Failed to get supported keys from device");
-            return;
-        }
+        _ => return eprintln!("Failed to get supported keys from device")
     };

     let mut virtual_device = match VirtualDevice::builder() {
         Ok(builder) => builder,
-        Err(e) => {
-            eprintln!("Failed to create virtual device builder: {}", e);
-            return;
-        }
+        Err(e) => return eprintln!("Failed to create virtual device builder: {e}")
     };

-    virtual_device = match virtual_device
-        .name("pind-virtual-keyboard")
-        .with_keys(&supported_keys)
-    {
+    virtual_device = match virtual_device.name("pind-virtual-keyboard").with_keys(&supported_keys) {
         Ok(builder) => builder,
-        Err(e) => {
-            eprintln!("Failed to set up virtual device with keys: {}", e);
-            return;
-        }
+        Err(e) => return eprintln!("Failed to set up virtual device with keys: {e}")
     };

     let mut virtual_device = match virtual_device.build() {
         Ok(dev) => dev,
-        Err(e) => {
-            eprintln!("Failed to build virtual device: {}", e);
-            return;
-        }
+        Err(e) => return eprintln!("Failed to build virtual device: {e}")
     };

     // Grab the physical device to capture all events
     if let Err(e) = device.grab() {
-        eprintln!("Failed to grab device: {}", e);
-        return;
+        return eprintln!("Failed to grab device: {e}");
     }

     loop {
@@ -273,12 +257,10 @@ fn read_keys(kc: &[(AttributeSet<KeyCode>, String)], kbs: PathBuf, delay: u64, u
                 events_to_forward.push(event);
             }
         }
-        if !events_to_forward.is_empty() {
-            if let Err(e) = virtual_device.emit(&events_to_forward) {
-                eprintln!("Failed to emit events: {}", e);
-            }
+        if !events_to_forward.is_empty() && let Err(e) = virtual_device.emit(&events_to_forward) {
+                eprintln!("Failed to emit events: {e}");
         }
-        
+
         thread::sleep(std::time::Duration::from_millis(delay));
         tick = tick.wrapping_add(1);
     }