Implement EventHandler as a mutable struct

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

@ -2,3 +2,7 @@ modmap:
- name: Global
remap:
a: b
keymap:
- name: Global
remap:
C-i: C-h

@ -0,0 +1,32 @@
use evdev::uinput::VirtualDevice;
use evdev::{EventType, InputEvent, Key};
use std::error::Error;
use crate::Config;
pub struct EventHandler {
pub config: Config,
pub device: VirtualDevice,
}
impl EventHandler {
// Handle EventType::KEY
pub fn on_event(&mut self, event: InputEvent) -> Result<(), Box<dyn Error>> {
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;
break;
}
}
self.device.emit(&[InputEvent::new(EventType::KEY, key.code(), event.value())])?;
Ok(())
}
pub fn send_event(&mut self, event: InputEvent) -> Result<(), Box<dyn Error>> {
self.device.emit(&[event])?;
Ok(())
}
}

@ -2,7 +2,7 @@ extern crate evdev;
extern crate nix;
use crate::output::build_device;
use crate::transform::on_event;
use crate::event_handler::{EventHandler};
use crate::Config;
use evdev::{Device, EventType, Key};
use nix::sys::select::select;
@ -13,24 +13,25 @@ use std::fs::read_dir;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::AsRawFd;
pub fn event_loop(mut input_devices: Vec<Device>, config: &Config) -> Result<(), Box<dyn Error>> {
pub fn event_loop(mut input_devices: Vec<Device>, config: Config) -> Result<(), Box<dyn Error>> {
for device in &mut input_devices {
device
.grab()
.map_err(|e| format!("Failed to grab device '{}': {}", device_name(device), e))?;
}
let mut output_device =
let output_device =
build_device().map_err(|e| format!("Failed to build an output device: {}", e))?;
let mut handler = EventHandler { config, device: output_device };
loop {
let readable_fds = select_readable(&input_devices)?;
for input_device in &mut input_devices {
if readable_fds.contains(input_device.as_raw_fd()) {
for event in input_device.fetch_events()? {
if event.event_type() == EventType::KEY {
on_event(event, &mut output_device, &config)?;
handler.on_event(event)?;
} else {
output_device.emit(&[event])?;
handler.send_event(event)?;
}
}
}

@ -9,7 +9,7 @@ extern crate getopts;
mod config;
mod input;
mod output;
mod transform;
mod event_handler;
fn usage(program: &str, opts: Options) -> String {
let brief = format!("Usage: {} CONFIG [options]", program);
@ -53,7 +53,7 @@ fn main() {
Err(e) => abort(&format!("Failed to select devices: {}", e)),
};
if let Err(e) = event_loop(input_devices, &config) {
if let Err(e) = event_loop(input_devices, config) {
abort(&format!("Error: {}", e));
}
}

@ -1,20 +0,0 @@
use evdev::uinput::VirtualDevice;
use evdev::{EventType, InputEvent, Key};
use std::error::Error;
use crate::Config;
// Handle EventType::KEY
pub fn on_event(event: InputEvent, device: &mut VirtualDevice, config: &Config) -> Result<(), Box<dyn Error>> {
let mut key = &Key::new(event.code());
// Perform modmap
for modmap in &config.modmap {
if let Some(modmap_key) = modmap.remap.get(&key) {
key = modmap_key;
break;
}
}
device.emit(&[InputEvent::new(EventType::KEY, key.code(), event.value())])?;
Ok(())
}
Loading…
Cancel
Save