Maintain pressed modifiers

pull/39/head
Takashi Kokubun 3 years ago
parent 0d5f37cae3
commit f5695d3bc2
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD

7
Cargo.lock generated

@ -80,6 +80,12 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.111"
@ -220,6 +226,7 @@ version = "0.1.0"
dependencies = [
"evdev",
"getopts",
"lazy_static",
"nix",
"serde",
"serde_yaml",

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
evdev = "0.11.3"
getopts = "0.2"
lazy_static = "1.4.0"
nix = "0.23.1"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"

@ -15,7 +15,7 @@ pub struct KeyPress {
pub windows: bool,
}
enum Modifier {
pub enum Modifier {
Shift,
Control,
Alt,

@ -2,7 +2,7 @@ mod action;
mod actions;
mod key;
mod keymap;
mod keypress;
pub mod keypress;
mod modmap;
mod wm_class;

@ -1,26 +1,49 @@
use evdev::uinput::VirtualDevice;
use evdev::{EventType, InputEvent, Key};
use std::error::Error;
use lazy_static::lazy_static;
use crate::Config;
use crate::config::keypress::Modifier;
use std::collections::HashMap;
pub struct EventHandler {
pub config: Config,
pub device: VirtualDevice,
shift: bool,
control: bool,
alt: bool,
windows: bool,
}
impl EventHandler {
pub fn new(config: Config, device: VirtualDevice) -> EventHandler {
EventHandler {
config,
device,
shift: false,
control: false,
alt: false,
windows: false,
}
}
// Handle EventType::KEY
pub fn on_event(&mut self, event: InputEvent) -> Result<(), Box<dyn Error>> {
let mut key = &Key::new(event.code());
let mut key = Key::new(event.code());
// Perform modmap
for modmap in &self.config.modmap {
if let Some(modmap_key) = modmap.remap.get(&key) {
key = modmap_key;
key = modmap_key.clone();
break;
}
}
// Perform keymap
if let Some(modifier) = MODIFIER_KEYS.get(&key.code()) {
self.update_modifier(modifier, event.value());
}
self.device.emit(&[InputEvent::new(EventType::KEY, key.code(), event.value())])?;
Ok(())
}
@ -29,4 +52,39 @@ impl EventHandler {
self.device.emit(&[event])?;
Ok(())
}
fn update_modifier(&mut self, modifier: &Modifier, value: i32) {
match modifier {
Modifier::Shift => self.shift = is_pressed(value),
Modifier::Control => self.control = is_pressed(value),
Modifier::Alt => self.alt = is_pressed(value),
Modifier::Windows => self.windows = is_pressed(value),
}
}
}
fn is_pressed(value: i32) -> bool {
value == PRESS || value == REPEAT
}
// InputEvent#value
// static RELEASE: i32 = 0;
static PRESS: i32 = 1;
static REPEAT: i32 = 2;
lazy_static! {
static ref MODIFIER_KEYS: HashMap<u16, Modifier> = vec![
// Shift
(Key::KEY_LEFTSHIFT.code(), Modifier::Shift),
(Key::KEY_RIGHTSHIFT.code(), Modifier::Shift),
// Control
(Key::KEY_LEFTCTRL.code(), Modifier::Control),
(Key::KEY_RIGHTCTRL.code(), Modifier::Control),
// Alt
(Key::KEY_LEFTALT.code(), Modifier::Alt),
(Key::KEY_RIGHTALT.code(), Modifier::Alt),
// Windows
(Key::KEY_LEFTMETA.code(), Modifier::Windows),
(Key::KEY_RIGHTMETA.code(), Modifier::Windows),
].into_iter().collect();
}

@ -22,7 +22,7 @@ pub fn event_loop(mut input_devices: Vec<Device>, config: Config) -> Result<(),
let output_device =
build_device().map_err(|e| format!("Failed to build an output device: {}", e))?;
let mut handler = EventHandler { config, device: output_device };
let mut handler = EventHandler::new(config, output_device);
loop {
let readable_fds = select_readable(&input_devices)?;
for input_device in &mut input_devices {

Loading…
Cancel
Save