style: formatting

This commit is contained in:
Federico Terzi 2021-03-15 19:08:08 +01:00
parent 307599b761
commit 2647f099ad
10 changed files with 101 additions and 72 deletions

View File

@ -19,12 +19,13 @@
use log::error; use log::error;
use crate::{event::{KeyboardEvent, Status}, hotkey::HotKey}; use crate::{
event::{KeyboardEvent, Status},
hotkey::HotKey,
};
use std::{collections::HashMap, time::Instant}; use std::{collections::HashMap, time::Instant};
use super::{ use super::state::State;
state::State,
};
// Number of milliseconds that define how long the hotkey memory // Number of milliseconds that define how long the hotkey memory
// should retain pressed keys // should retain pressed keys
@ -71,10 +72,7 @@ impl HotKeyFilter {
.collect(); .collect();
} }
pub fn process_event( pub fn process_event(&mut self, event: &KeyboardEvent) -> Option<i32> {
&mut self,
event: &KeyboardEvent,
) -> Option<i32> {
let mut hotkey = None; let mut hotkey = None;
let mut key_code = None; let mut key_code = None;
@ -94,13 +92,19 @@ impl HotKeyFilter {
} }
// Remove the old entries // Remove the old entries
to_be_removed.extend(self.memory.iter().enumerate().filter_map(|(i, (_, instant))| { to_be_removed.extend(
self
.memory
.iter()
.enumerate()
.filter_map(|(i, (_, instant))| {
if instant.elapsed().as_millis() > HOTKEY_WINDOW_TIMEOUT { if instant.elapsed().as_millis() > HOTKEY_WINDOW_TIMEOUT {
Some(i) Some(i)
} else { } else {
None None
} }
})); }),
);
// Remove duplicates and revert // Remove duplicates and revert
if !to_be_removed.is_empty() { if !to_be_removed.is_empty() {

View File

@ -10,7 +10,13 @@ use thiserror::Error;
use crate::KeyboardConfig; 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 { pub struct Keymap {
keymap: *mut xkb_keymap, keymap: *mut xkb_keymap,

View File

@ -27,7 +27,7 @@ mod hotkey;
mod keymap; mod keymap;
mod state; mod state;
use std::{cell::RefCell}; use std::cell::RefCell;
use anyhow::Result; use anyhow::Result;
use context::Context; use context::Context;
@ -42,10 +42,14 @@ use thiserror::Error;
use crate::event::{InputEvent, Key, KeyboardEvent, Variant}; use crate::event::{InputEvent, Key, KeyboardEvent, Variant};
use crate::event::{Key::*, MouseButton, MouseEvent}; 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::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_LEFT: u16 = 0x110;
const BTN_RIGHT: u16 = 0x111; const BTN_RIGHT: u16 = 0x111;
@ -101,7 +105,10 @@ impl Source for EVDEVSource {
// Initialize the hotkeys // Initialize the hotkeys
let state = State::new(&keymap)?; 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() { if self._context.fill(context).is_err() {
return Err(EVDEVSourceError::InitFailure().into()); return Err(EVDEVSourceError::InitFailure().into());
@ -171,9 +178,7 @@ impl Source for EVDEVSource {
// On Wayland we need to detect the global shortcuts manually // On Wayland we need to detect the global shortcuts manually
if let InputEvent::Keyboard(key_event) = &event { if let InputEvent::Keyboard(key_event) = &event {
if let Some(hotkey) = (*hotkey_filter).process_event(&key_event) { if let Some(hotkey) = (*hotkey_filter).process_event(&key_event) {
event_callback(InputEvent::HotKey(HotKeyEvent { event_callback(InputEvent::HotKey(HotKeyEvent { hotkey_id: hotkey }))
hotkey_id: hotkey,
}))
} }
} }

View File

@ -7,9 +7,7 @@ use anyhow::Result;
use thiserror::Error; use thiserror::Error;
use super::{ use super::{
ffi::{ ffi::{xkb_state, xkb_state_key_get_one_sym, xkb_state_new, xkb_state_unref},
xkb_state, xkb_state_key_get_one_sym, xkb_state_new, xkb_state_unref,
},
keymap::Keymap, keymap::Keymap,
}; };

View File

@ -94,12 +94,7 @@ impl Display for HotKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str_modifiers: Vec<String> = self.modifiers.iter().map(|m| m.to_string()).collect(); let str_modifiers: Vec<String> = self.modifiers.iter().map(|m| m.to_string()).collect();
let modifiers = str_modifiers.join("+"); let modifiers = str_modifiers.join("+");
write!( write!(f, "{}+{}", &modifiers, &self.key)
f,
"{}+{}",
&modifiers,
&self.key
)
} }
} }

View File

@ -17,10 +17,14 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::{convert::TryInto, ffi::CStr, sync::{ use std::{
convert::TryInto,
ffi::CStr,
sync::{
mpsc::{channel, Receiver, Sender}, mpsc::{channel, Receiver, Sender},
Arc, Mutex, Arc, Mutex,
}}; },
};
use lazycell::LazyCell; use lazycell::LazyCell;
use log::{error, trace, warn}; use log::{error, trace, warn};
@ -265,7 +269,10 @@ impl From<RawInputEvent> for Option<InputEvent> {
value, value,
status, status,
variant, 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 // Mouse events
@ -279,9 +286,7 @@ impl From<RawInputEvent> for Option<InputEvent> {
// HOTKEYS // HOTKEYS
INPUT_EVENT_TYPE_HOTKEY => { INPUT_EVENT_TYPE_HOTKEY => {
let id = raw.key_code; let id = raw.key_code;
return Some(InputEvent::HotKey(HotKeyEvent { return Some(InputEvent::HotKey(HotKeyEvent { hotkey_id: id }));
hotkey_id: id,
}))
} }
_ => {} _ => {}
} }

View File

@ -26,10 +26,13 @@ use widestring::U16CStr;
use anyhow::Result; use anyhow::Result;
use thiserror::Error; use thiserror::Error;
use crate::{event::{HotKeyEvent, Variant::*}, hotkey::{HotKey}};
use crate::event::{InputEvent, Key, KeyboardEvent, Variant}; use crate::event::{InputEvent, Key, KeyboardEvent, Variant};
use crate::event::{Key::*, MouseButton, MouseEvent}; use crate::event::{Key::*, MouseButton, MouseEvent};
use crate::{event::Status::*, Source, SourceCallback}; use crate::{event::Status::*, Source, SourceCallback};
use crate::{
event::{HotKeyEvent, Variant::*},
hotkey::HotKey,
};
const INPUT_LEFT_VARIANT: i32 = 1; const INPUT_LEFT_VARIANT: i32 = 1;
const INPUT_RIGHT_VARIANT: i32 = 2; const INPUT_RIGHT_VARIANT: i32 = 2;
@ -103,8 +106,6 @@ impl Win32Source {
impl Source for Win32Source { impl Source for Win32Source {
fn initialize(&mut self) -> Result<()> { fn initialize(&mut self) -> Result<()> {
let mut error_code = 0; let mut error_code = 0;
let handle = unsafe { detect_initialize(self as *const Win32Source, &mut error_code) }; let handle = unsafe { detect_initialize(self as *const Win32Source, &mut error_code) };
@ -118,10 +119,7 @@ impl Source for Win32Source {
} }
// Register the hotkeys // Register the hotkeys
self self.hotkeys.iter().for_each(|hk| {
.hotkeys
.iter()
.for_each(|hk| {
let raw = convert_hotkey_to_raw(&hk); let raw = convert_hotkey_to_raw(&hk);
if let Some(raw_hk) = raw { if let Some(raw_hk) = raw {
if unsafe { detect_register_hotkey(handle, raw_hk) } == 0 { if unsafe { detect_register_hotkey(handle, raw_hk) } == 0 {
@ -274,7 +272,10 @@ impl From<RawInputEvent> for Option<InputEvent> {
value, value,
status, status,
variant, 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 // Mouse events
@ -288,7 +289,7 @@ impl From<RawInputEvent> for Option<InputEvent> {
// Hotkey events // Hotkey events
INPUT_EVENT_TYPE_HOTKEY => { INPUT_EVENT_TYPE_HOTKEY => {
return Some(InputEvent::HotKey(HotKeyEvent { return Some(InputEvent::HotKey(HotKeyEvent {
hotkey_id: raw.key_code hotkey_id: raw.key_code,
})) }))
} }
_ => {} _ => {}

View File

@ -17,7 +17,11 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::{collections::HashMap, convert::TryInto, ffi::{c_void, CStr}}; use std::{
collections::HashMap,
convert::TryInto,
ffi::{c_void, CStr},
};
use lazycell::LazyCell; use lazycell::LazyCell;
use log::{debug, error, trace, warn}; use log::{debug, error, trace, warn};
@ -306,7 +310,10 @@ fn convert_raw_input_event_to_input_event(
value, value,
status, status,
variant, 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 // Mouse events
@ -436,7 +443,8 @@ mod tests {
raw.key_sym = 0x4B; raw.key_sym = 0x4B;
raw.key_code = 1; raw.key_code = 1;
let result: Option<InputEvent> = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); let result: Option<InputEvent> =
convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0);
assert_eq!( assert_eq!(
result.unwrap(), result.unwrap(),
InputEvent::Keyboard(KeyboardEvent { InputEvent::Keyboard(KeyboardEvent {
@ -456,7 +464,8 @@ mod tests {
raw.status = INPUT_STATUS_RELEASED; raw.status = INPUT_STATUS_RELEASED;
raw.key_code = INPUT_MOUSE_RIGHT_BUTTON; raw.key_code = INPUT_MOUSE_RIGHT_BUTTON;
let result: Option<InputEvent> = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); let result: Option<InputEvent> =
convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0);
assert_eq!( assert_eq!(
result.unwrap(), result.unwrap(),
InputEvent::Mouse(MouseEvent { InputEvent::Mouse(MouseEvent {
@ -476,12 +485,11 @@ mod tests {
let mut raw_hotkey_mapping = HashMap::new(); let mut raw_hotkey_mapping = HashMap::new();
raw_hotkey_mapping.insert((10, 1), 20); raw_hotkey_mapping.insert((10, 1), 20);
let result: Option<InputEvent> = convert_raw_input_event_to_input_event(raw, &raw_hotkey_mapping, 1); let result: Option<InputEvent> =
convert_raw_input_event_to_input_event(raw, &raw_hotkey_mapping, 1);
assert_eq!( assert_eq!(
result.unwrap(), result.unwrap(),
InputEvent::HotKey(HotKeyEvent { InputEvent::HotKey(HotKeyEvent { hotkey_id: 20 })
hotkey_id: 20,
})
); );
} }
@ -493,7 +501,8 @@ mod tests {
raw.buffer = buffer; raw.buffer = buffer;
raw.buffer_len = 5; raw.buffer_len = 5;
let result: Option<InputEvent> = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); let result: Option<InputEvent> =
convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0);
assert!(result.unwrap().into_keyboard().unwrap().value.is_none()); 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() { fn raw_to_input_event_returns_none_when_missing_type() {
let mut raw = default_raw_input_event(); let mut raw = default_raw_input_event();
raw.event_type = 0; raw.event_type = 0;
let result: Option<InputEvent> = convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0); let result: Option<InputEvent> =
convert_raw_input_event_to_input_event(raw, &HashMap::new(), 0);
assert!(result.is_none()); assert!(result.is_none());
} }
} }

View File

@ -1,6 +1,11 @@
use std::time::Duration; 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_inject::{get_injector, keys, Injector};
use espanso_ui::{event::UIEvent::*, icons::TrayIcon, menu::*}; use espanso_ui::{event::UIEvent::*, icons::TrayIcon, menu::*};
use simplelog::{CombinedLogger, Config, LevelFilter, TermLogger, TerminalMode}; use simplelog::{CombinedLogger, Config, LevelFilter, TermLogger, TerminalMode};
@ -67,7 +72,8 @@ fn main() {
HotKey::new(2, "CTRL+OPTION+3").unwrap(), HotKey::new(2, "CTRL+OPTION+3").unwrap(),
], ],
..Default::default() ..Default::default()
}).unwrap(); })
.unwrap();
source.initialize().unwrap(); source.initialize().unwrap();
source source
.eventloop(Box::new(move |event: InputEvent| { .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(); //injector.send_key_combination(&[keys::Key::Control, keys::Key::V], Default::default()).unwrap();
} }
} }
InputEvent::HotKey(_) => { InputEvent::HotKey(_) => {}
}
} }
})) }))
.unwrap(); .unwrap();