From a0c4d3c4bf89d3cc1aeb1388ff4f26e1c3c21a16 Mon Sep 17 00:00:00 2001 From: shiro Date: Tue, 28 Nov 2023 11:15:04 +0900 Subject: [PATCH] Improve key name resolution --- examples/tests/a_to_b.rs | 19 ++-- examples/tests/wasd_mouse_control.rs | 5 +- src/event_handlers.rs | 32 +++--- src/key_defs.rs | 148 +++++---------------------- src/key_primitives.rs | 17 ++- src/lib.rs | 10 +- src/mapper/mapper.rs | 16 +-- src/mapper/mapping_functions.rs | 80 +++++++-------- src/parsing/key.rs | 114 ++++++++++----------- src/parsing/key_action.rs | 42 ++++---- src/parsing/key_sequence.rs | 24 ++--- src/reader.rs | 6 +- 12 files changed, 213 insertions(+), 300 deletions(-) diff --git a/examples/tests/a_to_b.rs b/examples/tests/a_to_b.rs index cec19f8..a6ae2fb 100644 --- a/examples/tests/a_to_b.rs +++ b/examples/tests/a_to_b.rs @@ -1,5 +1,6 @@ use std::thread; use std::time::Duration; +use map2::key_primitives::Key; use crate::*; @@ -8,18 +9,18 @@ async fn hello_world() -> PyResult<()> { Python::with_gil(|py| -> PyResult<()> { let m = pytests::include_python!(); - reader_send(py, m, "reader", &EvdevInputEvent::new(&Default::default(), &KEY_A.event_code, 1)); - reader_send(py, m, "reader", &EvdevInputEvent::new(&Default::default(), &KEY_A.event_code, 0)); + reader_send(py, m, "reader", &Key::from_str("a").unwrap().to_input_ev(1)); + reader_send(py, m, "reader", &Key::from_str("a").unwrap().to_input_ev(0)); py.allow_threads(|| { thread::sleep(Duration::from_millis(25)); }); - assert_eq!(( - writer_read(py, m, "writer"), - writer_read(py, m, "writer"), - ), ( - Some(EvdevInputEvent::new(&Default::default(), &KEY_B.event_code, 1)), - Some(EvdevInputEvent::new(&Default::default(), &KEY_B.event_code, 0)), - )); + assert_eq!( + writer_read_all(py, m, "writer"), + vec![ + Key::from_str("b").unwrap().to_input_ev(1), + Key::from_str("b").unwrap().to_input_ev(0), + ] + ); Ok(()) })?; diff --git a/examples/tests/wasd_mouse_control.rs b/examples/tests/wasd_mouse_control.rs index 0a8cafe..67a8dd2 100644 --- a/examples/tests/wasd_mouse_control.rs +++ b/examples/tests/wasd_mouse_control.rs @@ -1,6 +1,7 @@ use std::thread; use std::time::Duration; use evdev_rs::enums::{EV_REL, EventCode}; +use map2::key_primitives::Key; use crate::*; @@ -9,12 +10,12 @@ async fn wasd_mouse_control() -> PyResult<()> { Python::with_gil(|py| -> PyResult<()> { let m = pytests::include_python!(); - reader_send(py, m, "reader_kbd", &EvdevInputEvent::new(&Default::default(), &KEY_W.event_code, 1)); + reader_send(py, m, "reader_kbd", &Key::from_str("w").unwrap().to_input_ev(1)); // sleep for long enough to trigger the timeout once py.allow_threads(|| { thread::sleep(Duration::from_millis(25)); }); - reader_send(py, m, "reader_kbd", &EvdevInputEvent::new(&Default::default(), &KEY_W.event_code, 0)); + reader_send(py, m, "reader_kbd", &Key::from_str("w").unwrap().to_input_ev(0)); assert_eq!( writer_read_all(py, m, "writer_mouse"), diff --git a/src/event_handlers.rs b/src/event_handlers.rs index 88c66c7..f584204 100644 --- a/src/event_handlers.rs +++ b/src/event_handlers.rs @@ -5,14 +5,14 @@ use crate::mapper::*; pub(crate) fn update_modifiers(state: &mut State, action: &KeyAction) -> bool { // TODO find a way to do this with a single accessor function let pairs: [(Key, fn(&KeyModifierState) -> bool, fn(&mut KeyModifierState) -> &mut bool); 8] = [ - (*KEY_LEFT_CTRL, |s| s.left_ctrl, |s: &mut KeyModifierState| &mut s.left_ctrl), - (*KEY_RIGHT_CTRL, |s| s.right_ctrl, |s: &mut KeyModifierState| &mut s.right_ctrl), - (*KEY_LEFT_ALT, |s| s.left_alt, |s: &mut KeyModifierState| &mut s.left_alt), - (*KEY_RIGHT_ALT, |s| s.right_alt, |s: &mut KeyModifierState| &mut s.right_alt), - (*KEY_LEFT_SHIFT, |s| s.left_shift, |s: &mut KeyModifierState| &mut s.left_shift), - (*KEY_RIGHT_SHIFT, |s| s.right_shift, |s: &mut KeyModifierState| &mut s.right_shift), - (*KEY_LEFT_META, |s| s.left_meta, |s: &mut KeyModifierState| &mut s.left_meta), - (*KEY_RIGHT_META, |s| s.right_meta, |s: &mut KeyModifierState| &mut s.right_meta), + (*KEY_LEFTCTRL, |s| s.left_ctrl, |s: &mut KeyModifierState| &mut s.left_ctrl), + (*KEY_RIGHTCTRL, |s| s.right_ctrl, |s: &mut KeyModifierState| &mut s.right_ctrl), + (*KEY_LEFTALT, |s| s.left_alt, |s: &mut KeyModifierState| &mut s.left_alt), + (*KEY_RIGHTALT, |s| s.right_alt, |s: &mut KeyModifierState| &mut s.right_alt), + (*KEY_LEFTSHIFT, |s| s.left_shift, |s: &mut KeyModifierState| &mut s.left_shift), + (*KEY_RIGHTSHIFT, |s| s.right_shift, |s: &mut KeyModifierState| &mut s.right_shift), + (*KEY_LEFTMETA, |s| s.left_meta, |s: &mut KeyModifierState| &mut s.left_meta), + (*KEY_RIGHTMETA, |s| s.right_meta, |s: &mut KeyModifierState| &mut s.right_meta), ]; for (key, is_modifier_down, modifier_mut) in pairs.iter() { @@ -59,20 +59,20 @@ fn release_restore_modifiers(state: &mut State, output_device: &mut VirtualOutpu }; if from_flags.ctrl && !to_flags.ctrl { - release_or_restore_modifier(&actual_state.left_ctrl, &*KEY_LEFT_CTRL); - release_or_restore_modifier(&actual_state.right_ctrl, &*KEY_RIGHT_CTRL); + release_or_restore_modifier(&actual_state.left_ctrl, &*KEY_LEFTCTRL); + release_or_restore_modifier(&actual_state.right_ctrl, &*KEY_RIGHTCTRL); } if from_flags.shift && !to_flags.shift { - release_or_restore_modifier(&actual_state.left_shift, &*KEY_LEFT_SHIFT); - release_or_restore_modifier(&actual_state.right_shift, &*KEY_RIGHT_SHIFT); + release_or_restore_modifier(&actual_state.left_shift, &*KEY_LEFTSHIFT); + release_or_restore_modifier(&actual_state.right_shift, &*KEY_RIGHTSHIFT); } if from_flags.alt && !to_flags.alt { - release_or_restore_modifier(&actual_state.left_alt, &*KEY_LEFT_ALT); - release_or_restore_modifier(&actual_state.right_alt, &*KEY_RIGHT_ALT); + release_or_restore_modifier(&actual_state.left_alt, &*KEY_LEFTALT); + release_or_restore_modifier(&actual_state.right_alt, &*KEY_RIGHTALT); } if from_flags.meta && !to_flags.meta { - release_or_restore_modifier(&actual_state.left_meta, &*KEY_LEFT_META); - release_or_restore_modifier(&actual_state.right_meta, &*KEY_RIGHT_META); + release_or_restore_modifier(&actual_state.left_meta, &*KEY_LEFTMETA); + release_or_restore_modifier(&actual_state.right_meta, &*KEY_RIGHTMETA); } // TODO eat keys we just released, un-eat keys we just restored diff --git a/src/key_defs.rs b/src/key_defs.rs index 6d15eab..376ea80 100644 --- a/src/key_defs.rs +++ b/src/key_defs.rs @@ -11,24 +11,9 @@ lazy_static! { pub static ref BTN_LEFT: Key = Key::from_str("BTN_LEFT").unwrap(); pub static ref BTN_MIDDLE: Key = Key::from_str("BTN_MIDDLE").unwrap(); pub static ref BTN_RIGHT: Key = Key::from_str("BTN_RIGHT").unwrap(); -pub static ref KEY_6: Key = Key::from_str("KEY_6").unwrap(); -pub static ref KEY_A: Key = Key::from_str("KEY_A").unwrap(); -pub static ref KEY_B: Key = Key::from_str("KEY_B").unwrap(); pub static ref KEY_BACKSPACE: Key = Key::from_str("KEY_BACKSPACE").unwrap(); -pub static ref KEY_C: Key = Key::from_str("KEY_C").unwrap(); -pub static ref KEY_CAPSLOCK: Key = Key::from_str("KEY_CAPSLOCK").unwrap(); -pub static ref KEY_D: Key = Key::from_str("KEY_D").unwrap(); pub static ref KEY_DOWN: Key = Key::from_str("KEY_DOWN").unwrap(); -pub static ref KEY_E: Key = Key::from_str("KEY_E").unwrap(); -pub static ref KEY_ENTER: Key = Key::from_str("KEY_ENTER").unwrap(); pub static ref KEY_ESC: Key = Key::from_str("KEY_ESC").unwrap(); -pub static ref KEY_F: Key = Key::from_str("KEY_F").unwrap(); -pub static ref KEY_G: Key = Key::from_str("KEY_G").unwrap(); -pub static ref KEY_GRAVE: Key = Key::from_str("KEY_GRAVE").unwrap(); -pub static ref KEY_H: Key = Key::from_str("KEY_H").unwrap(); -pub static ref KEY_I: Key = Key::from_str("KEY_I").unwrap(); -pub static ref KEY_J: Key = Key::from_str("KEY_J").unwrap(); -pub static ref KEY_K: Key = Key::from_str("KEY_K").unwrap(); pub static ref KEY_KPD0: Key = Key::from_str("KEY_KP0").unwrap(); pub static ref KEY_KPD1: Key = Key::from_str("KEY_KP1").unwrap(); pub static ref KEY_KPD2: Key = Key::from_str("KEY_KP2").unwrap(); @@ -39,133 +24,52 @@ pub static ref KEY_KPD6: Key = Key::from_str("KEY_KP6").unwrap(); pub static ref KEY_KPD7: Key = Key::from_str("KEY_KP7").unwrap(); pub static ref KEY_KPD8: Key = Key::from_str("KEY_KP8").unwrap(); pub static ref KEY_KPD9: Key = Key::from_str("KEY_KP9").unwrap(); -pub static ref KEY_F1: Key = Key::from_str("KEY_F1").unwrap(); -pub static ref KEY_F2: Key = Key::from_str("KEY_F2").unwrap(); -pub static ref KEY_F3: Key = Key::from_str("KEY_F3").unwrap(); -pub static ref KEY_F4: Key = Key::from_str("KEY_F4").unwrap(); -pub static ref KEY_F5: Key = Key::from_str("KEY_F5").unwrap(); -pub static ref KEY_F6: Key = Key::from_str("KEY_F6").unwrap(); -pub static ref KEY_F7: Key = Key::from_str("KEY_F7").unwrap(); -pub static ref KEY_F8: Key = Key::from_str("KEY_F8").unwrap(); -pub static ref KEY_F9: Key = Key::from_str("KEY_F9").unwrap(); -pub static ref KEY_F10: Key = Key::from_str("KEY_F10").unwrap(); -pub static ref KEY_F11: Key = Key::from_str("KEY_F11").unwrap(); -pub static ref KEY_F12: Key = Key::from_str("KEY_F12").unwrap(); -pub static ref KEY_F13: Key = Key::from_str("KEY_F13").unwrap(); -pub static ref KEY_F14: Key = Key::from_str("KEY_F14").unwrap(); -pub static ref KEY_F15: Key = Key::from_str("KEY_F15").unwrap(); -pub static ref KEY_F16: Key = Key::from_str("KEY_F16").unwrap(); -pub static ref KEY_F17: Key = Key::from_str("KEY_F17").unwrap(); -pub static ref KEY_F18: Key = Key::from_str("KEY_F18").unwrap(); -pub static ref KEY_F19: Key = Key::from_str("KEY_F19").unwrap(); -pub static ref KEY_F20: Key = Key::from_str("KEY_F20").unwrap(); -pub static ref KEY_F21: Key = Key::from_str("KEY_F21").unwrap(); -pub static ref KEY_F22: Key = Key::from_str("KEY_F22").unwrap(); -pub static ref KEY_F23: Key = Key::from_str("KEY_F23").unwrap(); -pub static ref KEY_F24: Key = Key::from_str("KEY_F24").unwrap(); -pub static ref KEY_L: Key = Key::from_str("KEY_L").unwrap(); pub static ref KEY_LEFT: Key = Key::from_str("KEY_LEFT").unwrap(); pub static ref KEY_LEFTBRACE: Key = Key::from_str("KEY_LEFTBRACE").unwrap(); pub static ref KEY_LEFTSHIFT: Key = Key::from_str("KEY_LEFTSHIFT").unwrap(); -pub static ref KEY_LEFT_ALT: Key = Key::from_str("KEY_LEFTALT").unwrap(); -pub static ref KEY_LEFT_CTRL: Key = Key::from_str("KEY_LEFTCTRL").unwrap(); -pub static ref KEY_LEFT_META: Key = Key::from_str("KEY_LEFTMETA").unwrap(); -pub static ref KEY_LEFT_SHIFT: Key = Key::from_str("KEY_LEFTSHIFT").unwrap(); -pub static ref KEY_M: Key = Key::from_str("KEY_M").unwrap(); -pub static ref KEY_MINUS: Key = Key::from_str("KEY_MINUS").unwrap(); -pub static ref KEY_N: Key = Key::from_str("KEY_N").unwrap(); -pub static ref KEY_O: Key = Key::from_str("KEY_O").unwrap(); -pub static ref KEY_P: Key = Key::from_str("KEY_P").unwrap(); +pub static ref KEY_LEFTALT: Key = Key::from_str("KEY_LEFTALT").unwrap(); +pub static ref KEY_LEFTCTRL: Key = Key::from_str("KEY_LEFTCTRL").unwrap(); +pub static ref KEY_LEFTMETA: Key = Key::from_str("KEY_LEFTMETA").unwrap(); pub static ref KEY_PAGEDOWN: Key = Key::from_str("KEY_PAGEDOWN").unwrap(); pub static ref KEY_PAGEUP: Key = Key::from_str("KEY_PAGEUP").unwrap(); -pub static ref KEY_Q: Key = Key::from_str("KEY_Q").unwrap(); -pub static ref KEY_R: Key = Key::from_str("KEY_R").unwrap(); pub static ref KEY_RIGHT: Key = Key::from_str("KEY_RIGHT").unwrap(); pub static ref KEY_RIGHTBRACE: Key = Key::from_str("KEY_RIGHTBRACE").unwrap(); +pub static ref KEY_RIGHTALT: Key = Key::from_str("KEY_RIGHTALT").unwrap(); +pub static ref KEY_RIGHTCTRL: Key = Key::from_str("KEY_RIGHTCTRL").unwrap(); +pub static ref KEY_RIGHTMETA: Key = Key::from_str("KEY_RIGHTMETA").unwrap(); pub static ref KEY_RIGHTSHIFT: Key = Key::from_str("KEY_RIGHTSHIFT").unwrap(); -pub static ref KEY_RIGHT_ALT: Key = Key::from_str("KEY_RIGHTALT").unwrap(); -pub static ref KEY_RIGHT_CTRL: Key = Key::from_str("KEY_RIGHTCTRL").unwrap(); -pub static ref KEY_RIGHT_META: Key = Key::from_str("KEY_RIGHTMETA").unwrap(); -pub static ref KEY_RIGHT_SHIFT: Key = Key::from_str("KEY_RIGHTSHIFT").unwrap(); -pub static ref KEY_S: Key = Key::from_str("KEY_S").unwrap(); -pub static ref KEY_SEMICOLON: Key = Key::from_str("KEY_SEMICOLON").unwrap(); -pub static ref KEY_SLASH: Key = Key::from_str("KEY_SLASH").unwrap(); -pub static ref KEY_SPACE: Key = Key::from_str("KEY_SPACE").unwrap(); -pub static ref KEY_T: Key = Key::from_str("KEY_T").unwrap(); -pub static ref KEY_TAB: Key = Key::from_str("KEY_TAB").unwrap(); -pub static ref KEY_U: Key = Key::from_str("KEY_U").unwrap(); pub static ref KEY_UP: Key = Key::from_str("KEY_UP").unwrap(); -pub static ref KEY_V: Key = Key::from_str("KEY_V").unwrap(); -pub static ref KEY_W: Key = Key::from_str("KEY_W").unwrap(); -pub static ref KEY_X: Key = Key::from_str("KEY_X").unwrap(); -pub static ref KEY_Y: Key = Key::from_str("KEY_Y").unwrap(); -pub static ref KEY_Z: Key = Key::from_str("KEY_Z").unwrap(); } lazy_static! { pub(crate) static ref KEY_ALIAS_TABLE: HashMap<&'static str, (Key, KeyModifierFlags)> = { let mut m = HashMap::new(); - m.insert("ALT", (*KEY_LEFT_ALT, KeyModifierFlags::new())); - m.insert("BACKSPACE", (*KEY_BACKSPACE, KeyModifierFlags::new())); - m.insert("BTN_LEFT", (*BTN_LEFT, KeyModifierFlags::new())); - m.insert("BTN_MIDDLE", (*BTN_MIDDLE, KeyModifierFlags::new())); - m.insert("BTN_RIGHT", (*BTN_RIGHT, KeyModifierFlags::new())); - m.insert("CAPSLOCK", (*KEY_CAPSLOCK, KeyModifierFlags::new())); - m.insert("CTRL", (*KEY_LEFT_CTRL, KeyModifierFlags::new())); - m.insert("DOWN", (*KEY_DOWN, KeyModifierFlags::new())); - m.insert("ESC", (*KEY_ESC, KeyModifierFlags::new())); - m.insert("F1", (*KEY_F1, KeyModifierFlags::new())); - m.insert("F10", (*KEY_F10, KeyModifierFlags::new())); - m.insert("F11", (*KEY_F11, KeyModifierFlags::new())); - m.insert("F12", (*KEY_F12, KeyModifierFlags::new())); - m.insert("F13", (*KEY_F13, KeyModifierFlags::new())); - m.insert("F14", (*KEY_F14, KeyModifierFlags::new())); - m.insert("F15", (*KEY_F15, KeyModifierFlags::new())); - m.insert("F16", (*KEY_F16, KeyModifierFlags::new())); - m.insert("F17", (*KEY_F17, KeyModifierFlags::new())); - m.insert("F18", (*KEY_F18, KeyModifierFlags::new())); - m.insert("F19", (*KEY_F19, KeyModifierFlags::new())); - m.insert("F2", (*KEY_F2, KeyModifierFlags::new())); - m.insert("F20", (*KEY_F20, KeyModifierFlags::new())); - m.insert("F21", (*KEY_F21, KeyModifierFlags::new())); - m.insert("F22", (*KEY_F22, KeyModifierFlags::new())); - m.insert("F23", (*KEY_F23, KeyModifierFlags::new())); - m.insert("F24", (*KEY_F24, KeyModifierFlags::new())); - m.insert("F3", (*KEY_F3, KeyModifierFlags::new())); - m.insert("F4", (*KEY_F4, KeyModifierFlags::new())); - m.insert("F5", (*KEY_F5, KeyModifierFlags::new())); - m.insert("F6", (*KEY_F6, KeyModifierFlags::new())); - m.insert("F7", (*KEY_F7, KeyModifierFlags::new())); - m.insert("F8", (*KEY_F8, KeyModifierFlags::new())); - m.insert("F9", (*KEY_F9, KeyModifierFlags::new())); - m.insert("KPD0", (*KEY_KPD0, KeyModifierFlags::new())); - m.insert("KPD1", (*KEY_KPD1, KeyModifierFlags::new())); - m.insert("KPD2", (*KEY_KPD2, KeyModifierFlags::new())); - m.insert("KPD3", (*KEY_KPD3, KeyModifierFlags::new())); - m.insert("KPD4", (*KEY_KPD4, KeyModifierFlags::new())); - m.insert("KPD5", (*KEY_KPD5, KeyModifierFlags::new())); - m.insert("KPD6", (*KEY_KPD6, KeyModifierFlags::new())); - m.insert("KPD7", (*KEY_KPD7, KeyModifierFlags::new())); - m.insert("KPD8", (*KEY_KPD8, KeyModifierFlags::new())); - m.insert("KPD9", (*KEY_KPD9, KeyModifierFlags::new())); - m.insert("LEFT", (*KEY_LEFT, KeyModifierFlags::new())); - m.insert("LEFT_ALT", (*KEY_LEFT_ALT, KeyModifierFlags::new())); - m.insert("LEFT_CTRL", (*KEY_RIGHT_CTRL, KeyModifierFlags::new())); - m.insert("LEFT_META", (*KEY_LEFT_META, KeyModifierFlags::new())); + m.insert("ALT", (*KEY_LEFTALT, KeyModifierFlags::new())); + m.insert("CTRL", (*KEY_LEFTCTRL, KeyModifierFlags::new())); + m.insert("ESCAPE", (*KEY_ESC, KeyModifierFlags::new())); + m.insert("KEYPAD_0", (*KEY_KPD0, KeyModifierFlags::new())); + m.insert("KEYPAD_1", (*KEY_KPD1, KeyModifierFlags::new())); + m.insert("KEYPAD_2", (*KEY_KPD2, KeyModifierFlags::new())); + m.insert("KEYPAD_3", (*KEY_KPD3, KeyModifierFlags::new())); + m.insert("KEYPAD_4", (*KEY_KPD4, KeyModifierFlags::new())); + m.insert("KEYPAD_5", (*KEY_KPD5, KeyModifierFlags::new())); + m.insert("KEYPAD_6", (*KEY_KPD6, KeyModifierFlags::new())); + m.insert("KEYPAD_7", (*KEY_KPD7, KeyModifierFlags::new())); + m.insert("KEYPAD_8", (*KEY_KPD8, KeyModifierFlags::new())); + m.insert("KEYPAD_9", (*KEY_KPD9, KeyModifierFlags::new())); + m.insert("LEFT_ALT", (*KEY_LEFTALT, KeyModifierFlags::new())); + m.insert("LEFT_CTRL", (*KEY_RIGHTCTRL, KeyModifierFlags::new())); m.insert("LEFT_SHIFT", (*KEY_LEFTSHIFT, KeyModifierFlags::new())); - m.insert("META", (*KEY_LEFT_META, KeyModifierFlags::new())); + m.insert("META", (*KEY_LEFTMETA, KeyModifierFlags::new())); m.insert("PAGE_DOWN", (*KEY_PAGEDOWN, KeyModifierFlags::new())); m.insert("PAGE_UP", (*KEY_PAGEUP, KeyModifierFlags::new())); - m.insert("RIGHT", (*KEY_RIGHT, KeyModifierFlags::new())); - m.insert("RIGHT_ALT", (*KEY_RIGHT_ALT, KeyModifierFlags::new())); - m.insert("RIGHT_CTRL", (*KEY_RIGHT_CTRL, KeyModifierFlags::new())); - m.insert("RIGHT_META", (*KEY_RIGHT_META, KeyModifierFlags::new())); + m.insert("RIGHT_ALT", (*KEY_RIGHTALT, KeyModifierFlags::new())); + m.insert("RIGHT_BRACE", (*KEY_RIGHTBRACE, KeyModifierFlags::new())); + m.insert("LEFT_BRACE", (*KEY_LEFTBRACE, KeyModifierFlags::new())); + m.insert("RIGHT_CTRL", (*KEY_RIGHTCTRL, KeyModifierFlags::new())); m.insert("RIGHT_SHIFT", (*KEY_RIGHTSHIFT, KeyModifierFlags::new())); m.insert("SHIFT", (*KEY_LEFTSHIFT, KeyModifierFlags::new())); - m.insert("SPACE", (*KEY_SPACE, KeyModifierFlags::new())); - m.insert("TAB", (*KEY_TAB, KeyModifierFlags::new())); - m.insert("UP", (*KEY_UP, KeyModifierFlags::new())); m }; } diff --git a/src/key_primitives.rs b/src/key_primitives.rs index 913cacd..c76efa4 100644 --- a/src/key_primitives.rs +++ b/src/key_primitives.rs @@ -8,12 +8,23 @@ pub struct Key { } impl Key { - pub fn from_str(s: &str) -> Result { - match EventCode::from_str(&EventType::EV_KEY, s) { + pub fn from_str(key_name: &str) -> Result { + let mut key_name = key_name.to_uppercase(); + + // prefix if not already + if !key_name.starts_with("KEY_") && !key_name.starts_with("BTN_") { + key_name = "KEY_".to_string().tap_mut(|s| s.push_str(&key_name)); + } + + match EventCode::from_str(&EventType::EV_KEY, &key_name) { Some(event_code) => { Ok(Key { event_code }) } - None => { Err(anyhow!("key not found: '{}'", s)) } + None => { Err(anyhow!("key not found: '{}'", key_name)) } } } + + pub fn to_input_ev(&self, state: i32) -> EvdevInputEvent { + EvdevInputEvent::new(&Default::default(), &self.event_code, state) + } } diff --git a/src/lib.rs b/src/lib.rs index 65316a0..7c71a2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,9 @@ extern crate regex; use std::{fs, io}; use std::borrow::BorrowMut; +// #[macro_use] +// use subscriber::linkable; +use std::hash::{DefaultHasher, Hash, Hasher}; use std::io::Write; use std::ops::{Deref, DerefMut}; use std::sync::{Arc, mpsc, Mutex, RwLock, Weak}; @@ -19,10 +22,11 @@ use std::thread; use std::time::Duration; pub use anyhow::{anyhow, Result}; -use arc_swap::{ArcSwap, ArcSwapOption}; +use arc_swap::ArcSwapOption; use evdev_rs::enums::EventCode; pub use evdev_rs::InputEvent as EvdevInputEvent; use nom::lib::std::collections::HashMap; +use tap::Tap; use uuid::Uuid; use event_loop::EVENT_LOOP; @@ -39,10 +43,6 @@ pub use crate::key_defs::*; use crate::key_primitives::*; use crate::state::*; -// #[macro_use] -// use subscriber::linkable; -use std::hash::{Hash, Hasher, DefaultHasher}; - pub mod key_defs; pub mod state; pub mod key_primitives; diff --git a/src/mapper/mapper.rs b/src/mapper/mapper.rs index 061c545..52c6e2a 100644 --- a/src/mapper/mapper.rs +++ b/src/mapper/mapper.rs @@ -46,22 +46,22 @@ fn release_restore_modifiers( }; if from_flags.ctrl && !to_flags.ctrl { - release_or_restore_modifier(&actual_state.left_ctrl, &*KEY_LEFT_CTRL); - release_or_restore_modifier(&actual_state.right_ctrl, &*KEY_RIGHT_CTRL); + release_or_restore_modifier(&actual_state.left_ctrl, &*KEY_LEFTCTRL); + release_or_restore_modifier(&actual_state.right_ctrl, &*KEY_RIGHTCTRL); } if from_flags.shift && !to_flags.shift { - release_or_restore_modifier(&actual_state.left_shift, &*KEY_LEFT_SHIFT); - release_or_restore_modifier(&actual_state.right_shift, &*KEY_RIGHT_SHIFT); + release_or_restore_modifier(&actual_state.left_shift, &*KEY_LEFTSHIFT); + release_or_restore_modifier(&actual_state.right_shift, &*KEY_RIGHTSHIFT); } if from_flags.alt && !to_flags.alt { - release_or_restore_modifier(&actual_state.left_alt, &*KEY_LEFT_ALT); + release_or_restore_modifier(&actual_state.left_alt, &*KEY_LEFTALT); } if from_flags.right_alt && !to_flags.right_alt { - release_or_restore_modifier(&actual_state.right_alt, &*KEY_RIGHT_ALT); + release_or_restore_modifier(&actual_state.right_alt, &*KEY_RIGHTALT); } if from_flags.meta && !to_flags.meta { - release_or_restore_modifier(&actual_state.left_meta, &*KEY_LEFT_META); - release_or_restore_modifier(&actual_state.right_meta, &*KEY_RIGHT_META); + release_or_restore_modifier(&actual_state.left_meta, &*KEY_LEFTMETA); + release_or_restore_modifier(&actual_state.right_meta, &*KEY_RIGHTMETA); } if output_events.len() > 0 { diff --git a/src/mapper/mapping_functions.rs b/src/mapper/mapping_functions.rs index 4923954..fad770e 100644 --- a/src/mapper/mapping_functions.rs +++ b/src/mapper/mapping_functions.rs @@ -36,21 +36,21 @@ pub fn map_action_to_click(from: &KeyActionWithMods, to: &KeyClickActionWithMods let mut seq = vec![]; seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_UP)); - if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_DOWN })); } - if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_DOWN })); } - if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_DOWN })); } - if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_DOWN })); } - if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_DOWN })); } + if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_DOWN })); } + if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_DOWN })); } + if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_DOWN })); } + if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_DOWN })); } + if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_DOWN })); } seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: to.key, value: TYPE_DOWN })); seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: to.key, value: TYPE_UP })); // revert to original - if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_UP })); } - if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_UP })); } - if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_UP })); } - if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_UP })); } - if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_UP })); } + if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_UP })); } + if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_UP })); } + if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_UP })); } + if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_UP })); } + if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_UP })); } seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_DOWN)); @@ -61,20 +61,20 @@ pub fn map_action_to_action(from: &KeyActionWithMods, to: &KeyActionWithMods) -> let mut seq = vec![]; seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_UP)); - if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_DOWN })); } - if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_DOWN })); } - if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_DOWN })); } - if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_DOWN })); } - if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_DOWN })); } + if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_DOWN })); } + if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_DOWN })); } + if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_DOWN })); } + if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_DOWN })); } + if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_DOWN })); } seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: to.key, value: to.value })); // revert to original - if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_UP })); } - if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_UP })); } - if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_UP })); } - if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_UP })); } - if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_UP })); } + if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_UP })); } + if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_UP })); } + if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_UP })); } + if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_UP })); } + if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_UP })); } seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_DOWN)); @@ -101,11 +101,11 @@ pub fn map_click_to_click(from: &KeyClickActionWithMods, to: &KeyClickActionWith { let mut seq = vec![]; seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_UP)); - if to.modifiers.ctrl && !from.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_DOWN })); } - if to.modifiers.alt && !from.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_DOWN })); } - if to.modifiers.right_alt && !from.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_DOWN })); } - if to.modifiers.shift && !from.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_DOWN })); } - if to.modifiers.meta && !from.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_DOWN })); } + if to.modifiers.ctrl && !from.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_DOWN })); } + if to.modifiers.alt && !from.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_DOWN })); } + if to.modifiers.right_alt && !from.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_DOWN })); } + if to.modifiers.shift && !from.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_DOWN })); } + if to.modifiers.meta && !from.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_DOWN })); } seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: to.key, value: TYPE_DOWN })); down_mapping = (KeyActionWithMods { key: from.key, value: TYPE_DOWN, modifiers: from.modifiers.clone() }, RuntimeAction::ActionSequence(seq)); } @@ -113,11 +113,11 @@ pub fn map_click_to_click(from: &KeyClickActionWithMods, to: &KeyClickActionWith { let mut seq = vec![]; seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: to.key, value: TYPE_UP })); - if to.modifiers.ctrl && !from.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_UP })); } - if to.modifiers.alt && !from.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_UP })); } - if to.modifiers.right_alt && !from.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_UP })); } - if to.modifiers.shift && !from.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_UP })); } - if to.modifiers.meta && !from.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_UP })); } + if to.modifiers.ctrl && !from.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_UP })); } + if to.modifiers.alt && !from.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_UP })); } + if to.modifiers.right_alt && !from.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_UP })); } + if to.modifiers.shift && !from.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_UP })); } + if to.modifiers.meta && !from.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_UP })); } seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_DOWN)); up_mapping = (KeyActionWithMods { key: from.key, value: TYPE_UP, modifiers: from.modifiers.clone() }, RuntimeAction::ActionSequence(seq)); } @@ -135,20 +135,20 @@ pub fn map_click_to_action(from: &KeyClickActionWithMods, to: &KeyActionWithMods seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_UP)); - if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_DOWN })); } - if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_DOWN })); } - if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_DOWN })); } - if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_DOWN })); } - if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_DOWN })); } + if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_DOWN })); } + if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_DOWN })); } + if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_DOWN })); } + if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_DOWN })); } + if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_DOWN })); } seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: to.key, value: to.value })); // revert to original - if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_CTRL, value: TYPE_UP })); } - if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_ALT, value: TYPE_UP })); } - if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHT_ALT, value: TYPE_UP })); } - if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_SHIFT, value: TYPE_UP })); } - if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFT_META, value: TYPE_UP })); } + if !from.modifiers.ctrl && to.modifiers.ctrl { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTCTRL, value: TYPE_UP })); } + if !from.modifiers.alt && to.modifiers.alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTALT, value: TYPE_UP })); } + if !from.modifiers.right_alt && to.modifiers.right_alt { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_RIGHTALT, value: TYPE_UP })); } + if !from.modifiers.shift && to.modifiers.shift { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTSHIFT, value: TYPE_UP })); } + if !from.modifiers.meta && to.modifiers.meta { seq.push(RuntimeKeyAction::KeyAction(KeyAction { key: *KEY_LEFTMETA, value: TYPE_UP })); } seq.push(RuntimeKeyAction::ReleaseRestoreModifiers(from.modifiers.clone(), to.modifiers.clone(), TYPE_DOWN)); diff --git a/src/parsing/key.rs b/src/parsing/key.rs index 3a14b31..7223c66 100644 --- a/src/parsing/key.rs +++ b/src/parsing/key.rs @@ -28,62 +28,59 @@ pub fn key_utf<'a>( transformer: Option<&'a XKBTransformer> ) -> impl Fn(&'a str) -> ParseResult<&str, (Key, KeyModifierFlags)> { move |input: &str| { - alt(( - // multiple ASCII chars - ident, - - // one arbitrary char - map(take(1usize), |v: &str| v.to_string()) - ))(input) - .and_then(|(next, key_name)| { + map_res( + alt(( + // multiple ASCII chars + ident, + + // one arbitrary char + map(take(1usize), |v: &str| v.to_string()) + )), + |key_name| { let (key, mut flags) = match KEY_ALIAS_TABLE.get(&*key_name.to_uppercase()) { Some(v) => *v, None => { + // try XKB conversion if let Some(transformer) = transformer { - let mut seq = transformer.utf_to_raw(key_name.to_string()) - .map_err(|_| make_generic_nom_err_new(input))?; - - let key = seq.remove(seq.len() - 1); - - let mut flags = KeyModifierFlags::new(); - - for ev in seq.iter() { - match ev { - Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_LEFTALT) } => { flags.alt(); } - Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_RIGHTALT) } => { flags.right_alt(); } - Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_LEFTSHIFT) } => { flags.shift(); } - Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_RIGHTSHIFT) } => { flags.shift(); } - _ => { unreachable!("unhandled modifier") } - } - } - - (key, flags) - } else { - let mut key_name = key_name.to_uppercase(); - if !key_name.starts_with("KEY_") && !key_name.starts_with("BTN_") { - key_name = "KEY_".to_string() - .tap_mut(|s| s.push_str(&key_name)); + if let Ok(res) = resolve_key_utf8(&key_name, transformer) { + return Ok::<_, nom::Err>>(res); } + } - let key = Key::from_str(key_name.as_str()) - .map_err(|_| make_generic_nom_err_new(input))?; + // fall back to libevdev resolution + let key = Key::from_str(&key_name) + .map_err(|_| make_generic_nom_err_new(input))?; - (key, KeyModifierFlags::new()) - } + return Ok((key, KeyModifierFlags::new())) } }; - if transformer.is_none() { - // only 1 char and it's uppercase - let mut it = key_name.chars(); - if it.next().unwrap().is_uppercase() && it.next().is_none() { - flags.shift(); - } - } + Ok((key, flags)) + }, + )(input) + } +} + +fn resolve_key_utf8(key: &str, transformer: &XKBTransformer) -> Result<(Key, KeyModifierFlags)> { + let mut seq = transformer.utf_to_raw(key.to_string())?; + + // the first entry is the key + let key = seq.remove(seq.len() - 1); - Ok((next, (key, flags))) - }) + let mut flags = KeyModifierFlags::new(); + + // the rest are modifiers we have to collect + for ev in seq.iter() { + match ev { + Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_LEFTALT) } => { flags.alt(); } + Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_RIGHTALT) } => { flags.right_alt(); } + Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_LEFTSHIFT) } => { flags.shift(); } + Key { event_code: EventCode::EV_KEY(EV_KEY::KEY_RIGHTSHIFT) } => { flags.shift(); } + _ => { unreachable!("unhandled modifier") } + } } + + Ok((key, flags)) } @@ -128,12 +125,12 @@ mod tests { let t = XKBTransformer::new("pc105", "us", None, None).unwrap(); assert_eq!(key_utf(Some(&t))(":"), nom_ok(( - *KEY_SEMICOLON, + Key::from_str("semicolon").unwrap(), KeyModifierFlags::new().tap_mut(|x| x.shift()) ))); assert_eq!(key_utf(Some(&t))("^"), nom_ok(( - *KEY_6, + Key::from_str("6").unwrap(), KeyModifierFlags::new().tap_mut(|x| x.shift()) ))); } @@ -146,7 +143,7 @@ mod tests { assert_eq!(key_utf(Some(&t))("BTN_LEFT"), nom_ok((*BTN_LEFT, KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("BTN_MIDDLE"), nom_ok((*BTN_MIDDLE, KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("BTN_RIGHT"), nom_ok((*BTN_RIGHT, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("CAPSLOCK"), nom_ok((*KEY_CAPSLOCK, KeyModifierFlags::new()))); + assert_eq!(key_utf(Some(&t))("CAPSLOCK"), nom_ok((Key::from_str("capslock").unwrap(), KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("DOWN"), nom_ok((*KEY_DOWN, KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("ESC"), nom_ok((*KEY_ESC, KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("LEFT"), nom_ok((*KEY_LEFT, KeyModifierFlags::new()))); @@ -154,20 +151,15 @@ mod tests { assert_eq!(key_utf(Some(&t))("PAGE_UP"), nom_ok((*KEY_PAGEUP, KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("RIGHT"), nom_ok((*KEY_RIGHT, KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("SHIFT"), nom_ok((*KEY_LEFTSHIFT, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("SPACE"), nom_ok((*KEY_SPACE, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("TAB"), nom_ok((*KEY_TAB, KeyModifierFlags::new()))); + assert_eq!(key_utf(Some(&t))("SPACE"), nom_ok((Key::from_str("space").unwrap(), KeyModifierFlags::new()))); + assert_eq!(key_utf(Some(&t))("TAB"), nom_ok((Key::from_str("tab").unwrap(), KeyModifierFlags::new()))); assert_eq!(key_utf(Some(&t))("UP"), nom_ok((*KEY_UP, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD1"), nom_ok((*KEY_KPD1, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD2"), nom_ok((*KEY_KPD2, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD3"), nom_ok((*KEY_KPD3, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD4"), nom_ok((*KEY_KPD4, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD5"), nom_ok((*KEY_KPD5, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD6"), nom_ok((*KEY_KPD6, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD7"), nom_ok((*KEY_KPD7, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD8"), nom_ok((*KEY_KPD8, KeyModifierFlags::new()))); - assert_eq!(key_utf(Some(&t))("KPD9"), nom_ok((*KEY_KPD9, KeyModifierFlags::new()))); - - assert_eq!(key_utf(Some(&t))("F11"), nom_ok((*KEY_F11, KeyModifierFlags::new()))); + assert_eq!(key_utf(Some(&t))("keypad_1"), nom_ok((*KEY_KPD1, KeyModifierFlags::new()))); + assert_eq!(key_utf(Some(&t))("kp1"), nom_ok((*KEY_KPD1, KeyModifierFlags::new()))); + assert_eq!(key_utf(Some(&t))("KEYPAD_9"), nom_ok((*KEY_KPD9, KeyModifierFlags::new()))); + assert_eq!(key_utf(Some(&t))("KP9"), nom_ok((*KEY_KPD9, KeyModifierFlags::new()))); + + assert_eq!(key_utf(Some(&t))("F11"), nom_ok((Key::from_str("f11").unwrap(), KeyModifierFlags::new()))); } #[test] @@ -175,6 +167,6 @@ mod tests { let t = XKBTransformer::new("pc105", "us", None, None).unwrap(); // assert_eq!(key_utf(Some(&t))("ab"), nom_err("ab")); - assert_nom_err(key_utf(Some(&t))("ab"), "ab"); + assert_nom_err(key_utf(Some(&t))("abc"), "abc"); } } \ No newline at end of file diff --git a/src/parsing/key_action.rs b/src/parsing/key_action.rs index 9cba67c..a46ece9 100644 --- a/src/parsing/key_action.rs +++ b/src/parsing/key_action.rs @@ -20,28 +20,28 @@ impl ParsedKeyActionVecExt for Vec { self.into_iter() .fold(vec![], |mut acc, v| match v { ParsedKeyAction::KeyAction(action) => { - if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFT_CTRL, TYPE_DOWN)); } - if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFT_SHIFT, TYPE_DOWN)); } - if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFT_ALT, TYPE_DOWN)); } - if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFT_META, TYPE_DOWN)); } + if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFTCTRL, TYPE_DOWN)); } + if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFTSHIFT, TYPE_DOWN)); } + if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFTALT, TYPE_DOWN)); } + if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFTMETA, TYPE_DOWN)); } acc.push(KeyAction::new(action.key, action.value)); - if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFT_CTRL, TYPE_UP)); } - if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFT_SHIFT, TYPE_UP)); } - if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFT_ALT, TYPE_UP)); } - if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFT_META, TYPE_UP)); } + if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFTCTRL, TYPE_UP)); } + if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFTSHIFT, TYPE_UP)); } + if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFTALT, TYPE_UP)); } + if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFTMETA, TYPE_UP)); } acc } ParsedKeyAction::KeyClickAction(action) => { - if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFT_CTRL, TYPE_DOWN)); } - if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFT_SHIFT, TYPE_DOWN)); } - if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFT_ALT, TYPE_DOWN)); } - if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFT_META, TYPE_DOWN)); } + if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFTCTRL, TYPE_DOWN)); } + if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFTSHIFT, TYPE_DOWN)); } + if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFTALT, TYPE_DOWN)); } + if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFTMETA, TYPE_DOWN)); } acc.push(KeyAction::new(action.key, TYPE_DOWN)); acc.push(KeyAction::new(action.key, TYPE_UP)); - if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFT_CTRL, TYPE_UP)); } - if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFT_SHIFT, TYPE_UP)); } - if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFT_ALT, TYPE_UP)); } - if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFT_META, TYPE_UP)); } + if action.modifiers.ctrl { acc.push(KeyAction::new(*KEY_LEFTCTRL, TYPE_UP)); } + if action.modifiers.shift { acc.push(KeyAction::new(*KEY_LEFTSHIFT, TYPE_UP)); } + if action.modifiers.alt { acc.push(KeyAction::new(*KEY_LEFTALT, TYPE_UP)); } + if action.modifiers.meta { acc.push(KeyAction::new(*KEY_LEFTMETA, TYPE_UP)); } acc } ParsedKeyAction::Action(action) => { @@ -246,19 +246,19 @@ mod tests { let t = XKBTransformer::new("pc105", "us", None, None).unwrap(); assert_eq!(key_action_utf(Some(&t))("{: down}"), nom_ok(ParsedKeyAction::KeyAction( - KeyActionWithMods::new(*KEY_SEMICOLON, 1, + KeyActionWithMods::new(Key::from_str("semicolon").unwrap(), 1, KeyModifierFlags::new().tap_mut(|x| x.shift()), ) ))); assert_eq!(key_action_utf(Some(&t))("{^ down}"), nom_ok(ParsedKeyAction::KeyAction( - KeyActionWithMods::new(*KEY_6, 1, + KeyActionWithMods::new(Key::from_str("6").unwrap(), 1, KeyModifierFlags::new().tap_mut(|x| x.shift()), ) ))); assert_eq!(key_action_utf(Some(&t))("^"), nom_ok(ParsedKeyAction::KeyClickAction( - KeyClickActionWithMods::new_with_mods(*KEY_6, + KeyClickActionWithMods::new_with_mods(Key::from_str("6").unwrap(), KeyModifierFlags::new().tap_mut(|x| x.shift()), ) ))); @@ -271,12 +271,12 @@ mod tests { assert_nom_err(key_action_utf(Some(&t))("{"), "{"); assert_eq!(single_key_action_utf_with_flags_utf(Some(&t))("\\^"), nom_ok(ParsedKeyAction::KeyClickAction( - KeyClickActionWithMods::new_with_mods(*KEY_6, + KeyClickActionWithMods::new_with_mods(Key::from_str("6").unwrap(), KeyModifierFlags::new().tap_mut(|v| v.shift())) ))); assert_eq!(single_key_action_utf_with_flags_utf(Some(&t))("\\{"), nom_ok(ParsedKeyAction::KeyClickAction( - KeyClickActionWithMods::new_with_mods(*KEY_LEFTBRACE, + KeyClickActionWithMods::new_with_mods(Key::from_str("leftbrace").unwrap(), KeyModifierFlags::new().tap_mut(|v| v.shift())) ))); diff --git a/src/parsing/key_sequence.rs b/src/parsing/key_sequence.rs index 5454c4e..e9cfe24 100644 --- a/src/parsing/key_sequence.rs +++ b/src/parsing/key_sequence.rs @@ -24,36 +24,36 @@ mod tests { #[test] fn sequence_input() { assert_eq!(key_sequence("abc"), nom_ok(vec![ - ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: *KEY_A, modifiers: KeyModifierFlags::new() }), - ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: *KEY_B, modifiers: KeyModifierFlags::new() }), - ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: *KEY_C, modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: Key::from_str("a").unwrap(), modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: Key::from_str("b").unwrap(), modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: Key::from_str("c").unwrap(), modifiers: KeyModifierFlags::new() }), ])); assert_eq!(key_sequence("a{b down}"), nom_ok(vec![ - ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: *KEY_A, modifiers: KeyModifierFlags::new() }), - ParsedKeyAction::KeyAction(KeyActionWithMods { key: *KEY_B, value: TYPE_DOWN, modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: Key::from_str("a").unwrap(), modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyAction(KeyActionWithMods { key: Key::from_str("b").unwrap(), value: TYPE_DOWN, modifiers: KeyModifierFlags::new() }), ])); } #[test] fn sequence_mixed() { assert_eq!(key_sequence("a{b down}c"), nom_ok(vec![ - ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: *KEY_A, modifiers: KeyModifierFlags::new() }), - ParsedKeyAction::KeyAction(KeyActionWithMods::new(*KEY_B, TYPE_DOWN, KeyModifierFlags::new())), - ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: *KEY_C, modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: Key::from_str("a").unwrap(), modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyAction(KeyActionWithMods::new(Key::from_str("b").unwrap(), TYPE_DOWN, KeyModifierFlags::new())), + ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: Key::from_str("c").unwrap(), modifiers: KeyModifierFlags::new() }), ])); assert_eq!(key_sequence("{shift down}a{shift up}"), nom_ok(vec![ - ParsedKeyAction::KeyAction(KeyActionWithMods::new(*KEY_LEFT_SHIFT, TYPE_DOWN, KeyModifierFlags::new())), - ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: *KEY_A, modifiers: KeyModifierFlags::new() }), - ParsedKeyAction::KeyAction(KeyActionWithMods::new(*KEY_LEFT_SHIFT, TYPE_UP, KeyModifierFlags::new())), + ParsedKeyAction::KeyAction(KeyActionWithMods::new(*KEY_LEFTSHIFT, TYPE_DOWN, KeyModifierFlags::new())), + ParsedKeyAction::KeyClickAction(KeyClickActionWithMods { key: Key::from_str("a").unwrap(), modifiers: KeyModifierFlags::new() }), + ParsedKeyAction::KeyAction(KeyActionWithMods::new(*KEY_LEFTSHIFT, TYPE_UP, KeyModifierFlags::new())), ])); } #[test] fn sequence_special_chars() { assert_eq!(key_sequence("{relative X 55}"), nom_ok(vec![ - ParsedKeyAction::Action(KeyAction::new(Key{event_code: EventCode::EV_REL(EV_REL::REL_X)}, 55)), + ParsedKeyAction::Action(KeyAction::new(Key { event_code: EventCode::EV_REL(EV_REL::REL_X) }, 55)), ])); } diff --git a/src/reader.rs b/src/reader.rs index 3f68854..35f49ab 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -82,8 +82,12 @@ impl Reader { pub fn send(&mut self, ev: String) -> PyResult<()> { let ev: EvdevInputEvent = serde_json::from_str(&ev).unwrap(); + let mut h = DefaultHasher::new(); + vec![self.id.clone()].hash(&mut h); + let path_hash = h.finish(); + if let Some(subscriber) = self.subscriber.load().deref() { - let _ = subscriber.send((vec![self.id.clone()], InputEvent::Raw(ev))); + let _ = subscriber.send((path_hash, InputEvent::Raw(ev))); }; Ok(()) }