Handle all errors properly

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

@ -2,7 +2,7 @@ use std::error::Error;
use std::fs;
pub fn load_config(filename: &str) -> Result<Config, Box<dyn Error>> {
let yaml = fs::read_to_string(&filename).unwrap();
let yaml = fs::read_to_string(&filename)?;
println!("{}", yaml.len());
return Ok(Config { modmap: vec![], keymap: vec![] })
}

@ -1,9 +1,10 @@
extern crate evdev;
use evdev::Device;
use std::error::Error;
pub fn select_device() -> Device {
pub fn select_device() -> Result<Device, Box<dyn Error>> {
// TODO: stop hard-coding the device
let device = Device::open("/dev/input/event19");
return device.unwrap();
let device = Device::open("/dev/input/event19")?;
return Ok(device);
}

@ -1,4 +1,4 @@
use evdev::{Device, EventType};
use evdev::{EventType};
use std::error::Error;
use std::env;
use std::process::exit;
@ -9,24 +9,30 @@ mod select;
mod transform;
mod config;
fn event_loop(input_device: &mut Device) -> Result<(), Box<dyn Error>> {
let mut output_device = output::build_device(input_device).unwrap();
fn event_loop() -> Result<(), Box<dyn Error>> {
let mut input_device = input::select_device()
.map_err(|e| format!("Failed to open an input device: {}", e))?;
let mut output_device = output::build_device(&input_device)
.map_err(|e| format!("Failed to build an output device: {}", e))?;
input_device.grab()
.map_err(|e| format!("Failed to grab an input device: {}", e))?;
loop {
if !select::is_readable(input_device) {
if !select::is_readable(&mut input_device)? {
continue;
}
for event in input_device.fetch_events().unwrap() {
for event in input_device.fetch_events()? {
if event.event_type() == EventType::KEY {
transform::on_event(event, &mut output_device);
transform::on_event(event, &mut output_device)?;
} else {
output_device.emit(&[event]).unwrap();
output_device.emit(&[event])?;
}
}
}
}
fn main() -> Result<(), Box<dyn Error>> {
fn main() {
let filename = match env::args().nth(1) {
Some(filename) => filename,
None => {
@ -41,11 +47,13 @@ fn main() -> Result<(), Box<dyn Error>> {
exit(1);
},
};
println!("{:?}", config);
println!("{:#?}", config);
let mut device = input::select_device();
device.grab()?;
event_loop(&mut device)?;
device.ungrab()?;
Ok(())
match event_loop() {
Ok(()) => {},
Err(e) => {
println!("Error: {}", e);
exit(1);
},
}
}

@ -3,10 +3,10 @@ use evdev::{Device};
use std::error::Error;
pub fn build_device(base_device: &Device) -> Result<VirtualDevice, Box<dyn Error>> {
let device = VirtualDeviceBuilder::new()?
.name("xremap")
.with_keys(base_device.supported_keys().unwrap())?
.build()
.unwrap();
let builder = VirtualDeviceBuilder::new()?.name("xremap");
let device = match base_device.supported_keys() {
Some(keys) => builder.with_keys(keys)?,
None => builder,
}.build()?;
Ok(device)
}

@ -3,10 +3,11 @@ extern crate evdev;
use evdev::Device;
use std::os::unix::io::{AsRawFd, RawFd};
use std::{io, mem, ptr};
use std::error::Error;
// A call of this function blocks until the argument device becomes readable.
// TODO: support selecting multiple devices
pub fn is_readable(device: &mut Device) -> bool {
pub fn is_readable(device: &mut Device) -> Result<bool, Box<dyn Error>> {
let mut fd_set = FdSet::new(); // TODO: maybe this should be prepared in the caller
fd_set.set(device.as_raw_fd());
@ -21,9 +22,8 @@ pub fn is_readable(device: &mut Device) -> bool {
} {
-1 => Err(io::Error::last_os_error()),
res => Ok(res as usize),
}
.unwrap();
return result == 1 && fd_set.is_set(device.as_raw_fd());
}?;
return Ok(result == 1 && fd_set.is_set(device.as_raw_fd()));
}
fn to_fdset_ptr(opt: Option<&mut FdSet>) -> *mut libc::fd_set {

@ -1,13 +1,15 @@
use std::error::Error;
use evdev::{EventType, InputEvent, InputEventKind};
use evdev::uinput::VirtualDevice;
pub fn on_event(event: InputEvent, device: &mut VirtualDevice) {
pub fn on_event(event: InputEvent, device: &mut VirtualDevice) -> Result<(), Box<dyn Error>> {
println!("event: {:?}", event);
if event.kind() == InputEventKind::Key(evdev::Key::KEY_A) {
device.emit(&[
InputEvent::new(EventType::KEY, evdev::Key::KEY_B.code(), event.value())
]).unwrap();
])?;
} else {
device.emit(&[event]).unwrap();
device.emit(&[event])?;
}
Ok(())
}

Loading…
Cancel
Save