diff --git a/espanso-detect/src/evdev/hotkey.rs b/espanso-detect/src/evdev/hotkey.rs index b7b2a1f..41ec5a4 100644 --- a/espanso-detect/src/evdev/hotkey.rs +++ b/espanso-detect/src/evdev/hotkey.rs @@ -19,12 +19,13 @@ use log::error; -use crate::{event::{KeyboardEvent, Status}, hotkey::HotKey}; +use crate::{ + event::{KeyboardEvent, Status}, + hotkey::HotKey, +}; use std::{collections::HashMap, time::Instant}; -use super::{ - state::State, -}; +use super::state::State; // Number of milliseconds that define how long the hotkey memory // should retain pressed keys @@ -71,10 +72,7 @@ impl HotKeyFilter { .collect(); } - pub fn process_event( - &mut self, - event: &KeyboardEvent, - ) -> Option { + pub fn process_event(&mut self, event: &KeyboardEvent) -> Option { let mut hotkey = None; let mut key_code = None; @@ -94,13 +92,19 @@ impl HotKeyFilter { } // Remove the old entries - to_be_removed.extend(self.memory.iter().enumerate().filter_map(|(i, (_, instant))| { - if instant.elapsed().as_millis() > HOTKEY_WINDOW_TIMEOUT { - Some(i) - } else { - None - } - })); + to_be_removed.extend( + self + .memory + .iter() + .enumerate() + .filter_map(|(i, (_, instant))| { + if instant.elapsed().as_millis() > HOTKEY_WINDOW_TIMEOUT { + Some(i) + } else { + None + } + }), + ); // Remove duplicates and revert if !to_be_removed.is_empty() { diff --git a/espanso-detect/src/evdev/keymap.rs b/espanso-detect/src/evdev/keymap.rs index 89d1c72..c4e42f3 100644 --- a/espanso-detect/src/evdev/keymap.rs +++ b/espanso-detect/src/evdev/keymap.rs @@ -10,7 +10,13 @@ use thiserror::Error; use crate::KeyboardConfig; -use super::{context::Context, ffi::{XKB_KEYMAP_COMPILE_NO_FLAGS, xkb_keymap, xkb_keymap_new_from_names, xkb_keymap_unref, xkb_rule_names}}; +use super::{ + context::Context, + ffi::{ + xkb_keymap, xkb_keymap_new_from_names, xkb_keymap_unref, xkb_rule_names, + XKB_KEYMAP_COMPILE_NO_FLAGS, + }, +}; pub struct Keymap { keymap: *mut xkb_keymap, diff --git a/espanso-detect/src/evdev/mod.rs b/espanso-detect/src/evdev/mod.rs index 392a764..6b26f67 100644 --- a/espanso-detect/src/evdev/mod.rs +++ b/espanso-detect/src/evdev/mod.rs @@ -27,7 +27,7 @@ mod hotkey; mod keymap; mod state; -use std::{cell::RefCell}; +use std::cell::RefCell; use anyhow::Result; use context::Context; @@ -42,10 +42,14 @@ use thiserror::Error; use crate::event::{InputEvent, Key, KeyboardEvent, Variant}; use crate::event::{Key::*, MouseButton, MouseEvent}; +use crate::{event::HotKeyEvent, event::Variant::*, hotkey::HotKey}; use crate::{event::Status::*, KeyboardConfig, Source, SourceCallback, SourceCreationOptions}; -use crate::{event::Variant::*, event::HotKeyEvent, hotkey::HotKey}; -use self::{device::{DeviceError, RawInputEvent, KEY_STATE_PRESS, KEY_STATE_RELEASE}, hotkey::{HotKeyFilter}, state::State}; +use self::{ + device::{DeviceError, RawInputEvent, KEY_STATE_PRESS, KEY_STATE_RELEASE}, + hotkey::HotKeyFilter, + state::State, +}; const BTN_LEFT: u16 = 0x110; const BTN_RIGHT: u16 = 0x111; @@ -101,7 +105,10 @@ impl Source for EVDEVSource { // Initialize the hotkeys let state = State::new(&keymap)?; - self._hotkey_filter.borrow_mut().initialize(&state, &self.hotkeys); + self + ._hotkey_filter + .borrow_mut() + .initialize(&state, &self.hotkeys); if self._context.fill(context).is_err() { return Err(EVDEVSourceError::InitFailure().into()); @@ -171,9 +178,7 @@ impl Source for EVDEVSource { // On Wayland we need to detect the global shortcuts manually if let InputEvent::Keyboard(key_event) = &event { if let Some(hotkey) = (*hotkey_filter).process_event(&key_event) { - event_callback(InputEvent::HotKey(HotKeyEvent { - hotkey_id: hotkey, - })) + event_callback(InputEvent::HotKey(HotKeyEvent { hotkey_id: hotkey })) } } diff --git a/espanso-detect/src/evdev/state.rs b/espanso-detect/src/evdev/state.rs index 9dcb843..b7edf0e 100644 --- a/espanso-detect/src/evdev/state.rs +++ b/espanso-detect/src/evdev/state.rs @@ -7,9 +7,7 @@ use anyhow::Result; use thiserror::Error; use super::{ - ffi::{ - xkb_state, xkb_state_key_get_one_sym, xkb_state_new, xkb_state_unref, - }, + ffi::{xkb_state, xkb_state_key_get_one_sym, xkb_state_new, xkb_state_unref}, keymap::Keymap, }; diff --git a/espanso-detect/src/event.rs b/espanso-detect/src/event.rs index 98412c4..6eae8ad 100644 --- a/espanso-detect/src/event.rs +++ b/espanso-detect/src/event.rs @@ -127,4 +127,4 @@ pub enum Key { #[derive(Debug, PartialEq)] pub struct HotKeyEvent { pub hotkey_id: i32, -} \ No newline at end of file +} diff --git a/espanso-detect/src/hotkey/mod.rs b/espanso-detect/src/hotkey/mod.rs index 415e0a6..8495e3a 100644 --- a/espanso-detect/src/hotkey/mod.rs +++ b/espanso-detect/src/hotkey/mod.rs @@ -94,12 +94,7 @@ impl Display for HotKey { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let str_modifiers: Vec = self.modifiers.iter().map(|m| m.to_string()).collect(); let modifiers = str_modifiers.join("+"); - write!( - f, - "{}+{}", - &modifiers, - &self.key - ) + write!(f, "{}+{}", &modifiers, &self.key) } } diff --git a/espanso-detect/src/mac/mod.rs b/espanso-detect/src/mac/mod.rs index 979ce5a..0af84be 100644 --- a/espanso-detect/src/mac/mod.rs +++ b/espanso-detect/src/mac/mod.rs @@ -17,10 +17,14 @@ * along with espanso. If not, see . */ -use std::{convert::TryInto, ffi::CStr, sync::{ +use std::{ + convert::TryInto, + ffi::CStr, + sync::{ mpsc::{channel, Receiver, Sender}, Arc, Mutex, - }}; + }, +}; use lazycell::LazyCell; use log::{error, trace, warn}; @@ -265,7 +269,10 @@ impl From for Option { value, status, variant, - code: raw.key_code.try_into().expect("unable to convert keycode to u32"), + code: raw + .key_code + .try_into() + .expect("unable to convert keycode to u32"), })); } // Mouse events @@ -279,9 +286,7 @@ impl From for Option { // HOTKEYS INPUT_EVENT_TYPE_HOTKEY => { let id = raw.key_code; - return Some(InputEvent::HotKey(HotKeyEvent { - hotkey_id: id, - })) + return Some(InputEvent::HotKey(HotKeyEvent { hotkey_id: id })); } _ => {} } diff --git a/espanso-detect/src/win32/mod.rs b/espanso-detect/src/win32/mod.rs index 18b413d..5cada92 100644 --- a/espanso-detect/src/win32/mod.rs +++ b/espanso-detect/src/win32/mod.rs @@ -26,10 +26,13 @@ use widestring::U16CStr; use anyhow::Result; use thiserror::Error; -use crate::{event::{HotKeyEvent, Variant::*}, hotkey::{HotKey}}; use crate::event::{InputEvent, Key, KeyboardEvent, Variant}; use crate::event::{Key::*, MouseButton, MouseEvent}; use crate::{event::Status::*, Source, SourceCallback}; +use crate::{ + event::{HotKeyEvent, Variant::*}, + hotkey::HotKey, +}; const INPUT_LEFT_VARIANT: i32 = 1; const INPUT_RIGHT_VARIANT: i32 = 2; @@ -103,8 +106,6 @@ impl Win32Source { impl Source for Win32Source { fn initialize(&mut self) -> Result<()> { - - let mut error_code = 0; let handle = unsafe { detect_initialize(self as *const Win32Source, &mut error_code) }; @@ -118,21 +119,18 @@ impl Source for Win32Source { } // Register the hotkeys - self - .hotkeys - .iter() - .for_each(|hk| { - let raw = convert_hotkey_to_raw(&hk); - if let Some(raw_hk) = raw { - if unsafe { detect_register_hotkey(handle, raw_hk) } == 0 { - error!("unable to register hotkey: {}", hk); - } else { - debug!("registered hotkey: {}", hk); - } + self.hotkeys.iter().for_each(|hk| { + let raw = convert_hotkey_to_raw(&hk); + if let Some(raw_hk) = raw { + if unsafe { detect_register_hotkey(handle, raw_hk) } == 0 { + error!("unable to register hotkey: {}", hk); } else { - error!("unable to generate raw hotkey mapping: {}", hk); + debug!("registered hotkey: {}", hk); } - }); + } else { + error!("unable to generate raw hotkey mapping: {}", hk); + } + }); self.handle = handle; @@ -190,7 +188,7 @@ fn convert_hotkey_to_raw(hk: &HotKey) -> Option { let key_code = hk.key.to_code()?; let code: Result = key_code.try_into(); if let Ok(code) = code { - let mut flags = 0x4000; // NOREPEAT flags + let mut flags = 0x4000; // NOREPEAT flags if hk.has_ctrl() { flags |= 0x0002; } @@ -274,7 +272,10 @@ impl From for Option { value, status, variant, - code: raw.key_code.try_into().expect("unable to convert keycode to u32"), + code: raw + .key_code + .try_into() + .expect("unable to convert keycode to u32"), })); } // Mouse events @@ -288,7 +289,7 @@ impl From for Option { // Hotkey events INPUT_EVENT_TYPE_HOTKEY => { return Some(InputEvent::HotKey(HotKeyEvent { - hotkey_id: raw.key_code + hotkey_id: raw.key_code, })) } _ => {} diff --git a/espanso-detect/src/x11/mod.rs b/espanso-detect/src/x11/mod.rs index a5887d6..1ef1d0b 100644 --- a/espanso-detect/src/x11/mod.rs +++ b/espanso-detect/src/x11/mod.rs @@ -17,7 +17,11 @@ * along with espanso. If not, see . */ -use std::{collections::HashMap, convert::TryInto, ffi::{c_void, CStr}}; +use std::{ + collections::HashMap, + convert::TryInto, + ffi::{c_void, CStr}, +}; use lazycell::LazyCell; use log::{debug, error, trace, warn}; @@ -306,7 +310,10 @@ fn convert_raw_input_event_to_input_event( value, status, variant, - code: raw.key_code.try_into().expect("invalid keycode conversion to u32"), + code: raw + .key_code + .try_into() + .expect("invalid keycode conversion to u32"), })); } // Mouse events @@ -436,7 +443,8 @@ mod tests { raw.key_sym = 0x4B; raw.key_code = 1; - let result: Option = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); + let result: Option = + convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); assert_eq!( result.unwrap(), InputEvent::Keyboard(KeyboardEvent { @@ -456,7 +464,8 @@ mod tests { raw.status = INPUT_STATUS_RELEASED; raw.key_code = INPUT_MOUSE_RIGHT_BUTTON; - let result: Option = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); + let result: Option = + convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); assert_eq!( result.unwrap(), InputEvent::Mouse(MouseEvent { @@ -476,12 +485,11 @@ mod tests { let mut raw_hotkey_mapping = HashMap::new(); raw_hotkey_mapping.insert((10, 1), 20); - let result: Option = convert_raw_input_event_to_input_event(raw, &raw_hotkey_mapping, 1); + let result: Option = + convert_raw_input_event_to_input_event(raw, &raw_hotkey_mapping, 1); assert_eq!( result.unwrap(), - InputEvent::HotKey(HotKeyEvent { - hotkey_id: 20, - }) + InputEvent::HotKey(HotKeyEvent { hotkey_id: 20 }) ); } @@ -493,7 +501,8 @@ mod tests { raw.buffer = buffer; raw.buffer_len = 5; - let result: Option = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); + let result: Option = + convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); assert!(result.unwrap().into_keyboard().unwrap().value.is_none()); } @@ -501,7 +510,8 @@ mod tests { fn raw_to_input_event_returns_none_when_missing_type() { let mut raw = default_raw_input_event(); raw.event_type = 0; - let result: Option = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); + let result: Option = + convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); assert!(result.is_none()); } } diff --git a/espanso/src/main.rs b/espanso/src/main.rs index 3c2bbd2..918ab8d 100644 --- a/espanso/src/main.rs +++ b/espanso/src/main.rs @@ -1,6 +1,11 @@ use std::time::Duration; -use espanso_detect::{SourceCreationOptions, event::{InputEvent, Status}, get_source, hotkey::HotKey}; +use espanso_detect::{ + event::{InputEvent, Status}, + get_source, + hotkey::HotKey, + SourceCreationOptions, +}; use espanso_inject::{get_injector, keys, Injector}; use espanso_ui::{event::UIEvent::*, icons::TrayIcon, menu::*}; use simplelog::{CombinedLogger, Config, LevelFilter, TermLogger, TerminalMode}; @@ -67,7 +72,8 @@ fn main() { HotKey::new(2, "CTRL+OPTION+3").unwrap(), ], ..Default::default() - }).unwrap(); + }) + .unwrap(); source.initialize().unwrap(); source .eventloop(Box::new(move |event: InputEvent| { @@ -85,8 +91,7 @@ fn main() { //injector.send_key_combination(&[keys::Key::Control, keys::Key::V], Default::default()).unwrap(); } } - InputEvent::HotKey(_) => { - } + InputEvent::HotKey(_) => {} } })) .unwrap();