Build a virtual device to output events

This commit is contained in:
Takashi Kokubun 2021-12-12 23:43:35 -08:00
parent 0878d52dcb
commit cc178eb575
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD
2 changed files with 34 additions and 7 deletions

View File

@ -1,18 +1,29 @@
use std::error::Error;
use evdev::{Device, EventType};
mod input;
mod select;
mod output;
fn event_loop(input_device: &mut Device) -> Result<(), Box<dyn Error>> {
let mut output_device = output::build_device().unwrap();
for _ in 0..5 {
if !select::is_readable(input_device) { continue }
for event in input_device.fetch_events().unwrap() {
if event.event_type() == EventType::KEY {
println!("event: {:?}", event);
}
output_device.emit(&[event]).unwrap();
}
}
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let mut device = input::select_device();
device.grab()?;
for _ in 0..5 {
if select::is_readable(&mut device) {
for ev in device.fetch_events().unwrap() {
println!("event: {:?}", ev);
}
}
}
event_loop(&mut device)?;
device.ungrab()?;
Ok(())
}

16
src/output.rs Normal file
View File

@ -0,0 +1,16 @@
use std::error::Error;
use evdev::{AttributeSet, Key};
use evdev::uinput::{VirtualDevice, VirtualDeviceBuilder};
pub fn build_device() -> Result<VirtualDevice, Box<dyn Error>> {
let mut keys = AttributeSet::<Key>::new();
keys.insert(Key::KEY_A);
let device = VirtualDeviceBuilder::new()?
.name("xremap")
.with_keys(&keys)?
.build()
.unwrap();
Ok(device)
}