From af323a0f16b0b07ae855e029129c21ded351239c Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Sun, 20 Feb 2022 20:09:44 -0700 Subject: [PATCH] Add consume config --- src/config.rs | 20 ++++++++++++++------ src/daemon.rs | 1 + 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 232e929..3ad0d98 100644 --- a/src/config.rs +++ b/src/config.rs @@ -56,6 +56,7 @@ pub struct Hotkey { pub keysym: evdev::Key, pub modifiers: Vec, pub command: String, + pub consume: bool, } #[derive(Debug, PartialEq, Copy, Clone)] @@ -77,8 +78,8 @@ pub enum Modifier { } impl Hotkey { - pub fn new(mode: String, keysym: evdev::Key, modifiers: Vec, command: String) -> Self { - Hotkey { mode, keysym, modifiers, command } + pub fn new(mode: String, keysym: evdev::Key, modifiers: Vec, command: String, consume: bool) -> Self { + Hotkey { mode, keysym, modifiers, command, consume } } } @@ -282,9 +283,9 @@ fn parse_contents(contents: String) -> Result, Error> { 'hotkey_parse: for (key, command) in extracted_keys.iter().zip(extracted_commands.iter()) { println!("{} {}", key, command); - let (mode, keysym, modifiers) = + let (mode, keysym, modifiers, consume) = parse_keybind(key, line_number + 1, &key_to_evdev_key, &mod_to_mod_enum)?; - let hotkey = Hotkey { mode, keysym, modifiers, command: command.to_string() }; + let hotkey = Hotkey { mode, keysym, modifiers, command: command.to_string(), consume }; // Ignore duplicate hotkeys for i in hotkeys.iter() { @@ -307,7 +308,7 @@ fn parse_keybind( line_nr: u32, key_to_evdev_key: &HashMap<&str, evdev::Key>, mod_to_mod_enum: &HashMap<&str, Modifier>, -) -> Result<(String, evdev::Key, Vec), Error> { +) -> Result<(String, evdev::Key, Vec, bool), Error> { let line = line.split('#').next().unwrap(); let tokens: Vec = line.split('+').map(|s| s.trim().to_lowercase()).filter(|s| s != "_").collect(); @@ -318,6 +319,7 @@ fn parse_keybind( if token.starts_with("[") && token.ends_with("]") { continue; } + if token.to_string() == "!" { continue; } if key_to_evdev_key.contains_key(token.as_str()) { // Can't have a key that's like a modifier if token != last_token { @@ -335,6 +337,12 @@ fn parse_keybind( // Translate keypress into evdev key let keysym = key_to_evdev_key.get(last_token).unwrap(); + let consume = if tokens + .iter() + .filter(|s| s.to_string() == "!") + .map(|s| s.to_string()) + .collect::>() + .len() == 1 { true } else { false }; let mode = tokens .iter() .filter(|s| s.starts_with("[") && s.ends_with("]")) @@ -350,7 +358,7 @@ fn parse_keybind( .map(|token| *mod_to_mod_enum.get(token.as_str()).unwrap()) .collect(); - Ok((mode, *keysym, modifiers)) + Ok((mode, *keysym, modifiers, consume)) } fn extract_curly_brace(line: &str) -> Vec { diff --git a/src/daemon.rs b/src/daemon.rs index 7785d39..f893ca9 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -175,6 +175,7 @@ pub fn key_listener() { evdev::Key::KEY_A, default_test_modifier, String::from("notify-send \"it works\""), + false, ), ran_at: SystemTime::now(), };