mirror of
https://github.com/k0kubun/xremap
synced 2024-11-02 03:40:25 +00:00
Build a virtual device to output events
This commit is contained in:
parent
0878d52dcb
commit
cc178eb575
25
src/main.rs
25
src/main.rs
@ -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
16
src/output.rs
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user