diff --git a/src/config.rs b/src/config.rs index d6201c9..586a905 100644 --- a/src/config.rs +++ b/src/config.rs @@ -54,7 +54,7 @@ pub enum Modifier { } impl Hotkey { - fn new(keysym: evdev::Key, modifiers: Vec, command: String) -> Self { + pub fn new(keysym: evdev::Key, modifiers: Vec, command: String) -> Self { Hotkey { keysym, modifiers, command } } } diff --git a/src/daemon.rs b/src/daemon.rs index 039d2b7..a61b965 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -21,11 +21,6 @@ pub struct LastHotkey { hotkey: config::Hotkey, ran_at: SystemTime, } -impl LastHotkey { - pub fn new(hotkey: config::Hotkey, ran_at: SystemTime) -> Result> { - Ok(LastHotkey { hotkey, ran_at }) - } -} pub fn main() { let args = set_flags().get_matches(); @@ -96,16 +91,27 @@ pub fn main() { (Key::KEY_RIGHTSHIFT, config::Modifier::Shift), ]); + let repeat_cooldown_duration: u128; if args.is_present("cooldown") { - let cooldown: u32 = args.value_of("cooldown").unwrap().parse::().unwrap(); + repeat_cooldown_duration = args.value_of("cooldown").unwrap().parse::().unwrap(); } else { - let cooldown: u32 = 300; + repeat_cooldown_duration = 300; } let mut key_states: Vec> = Vec::new(); let mut possible_hotkeys: Vec = Vec::new(); - let mut first_run: bool = true; // Check for first iteration of a dectected hotkey. - let mut last_hotkey: LastHotkey; + + let mut default_test_modifier: Vec = Vec::new(); + default_test_modifier.push(config::Modifier::Super); + let mut last_hotkey = LastHotkey { + // just a dummy last_hotkey so I don't need to mess with Option + hotkey: config::Hotkey::new( + evdev::Key::KEY_A, + default_test_modifier, + String::from("notify-send \"it works\""), + ), + ran_at: SystemTime::now(), + }; loop { for device in &keyboard_devices { @@ -138,15 +144,31 @@ pub fn main() { if state_modifiers.iter().all(|x| hotkey.modifiers.contains(x)) && state_keysyms.contains(&hotkey.keysym) { - if first_run == true { - last_hotkey = - LastHotkey::new(hotkey.clone(), SystemTime::now()).unwrap(); - first_run = false; - } else { - if last_hotkey.hotkey == hotkey.clone() { - log::info!(" This was already run!!!"); - break; + if last_hotkey.hotkey == hotkey.clone() { + let time_since_ran_at = + match SystemTime::now().duration_since(last_hotkey.ran_at) { + Ok(n) => n.as_millis(), + Err(e) => { + log::error!("Error: {:#?}", e); + exit(1); + } + }; + if time_since_ran_at <= repeat_cooldown_duration { + log::error!( + "In cooldown: {:#?} \nTime Remaining: {:#?}ms", + hotkey, + repeat_cooldown_duration - time_since_ran_at + ); + continue; + } else { + last_hotkey = LastHotkey { + hotkey: hotkey.clone(), + ran_at: SystemTime::now(), + }; } + } else { + last_hotkey = + LastHotkey { hotkey: hotkey.clone(), ran_at: SystemTime::now() }; } log::info!("Hotkey pressed: {:#?}", hotkey); @@ -203,15 +225,15 @@ pub fn set_flags() -> App<'static> { arg!(-c --config ) .required(false) .takes_value(true) - .help("Set a custom config file path"), + .help("Set a custom config file path."), ) .arg( arg!(-C --cooldown ) .required(false) .takes_value(true) - .help("Set a custom cooldown between each run of the same Hotkey."), + .help("Set a custom repeat cooldown duration."), ) - .arg(arg!(-d - -debug).required(false).help("Enable debug mode")); + .arg(arg!(-d - -debug).required(false).help("Enable debug mode.")); app }