Panic cleanup part 1
This commit is contained in:
parent
98ffe1b17e
commit
f9a6ed8d00
74
src/main.rs
74
src/main.rs
@ -51,33 +51,7 @@ fn main() {
|
|||||||
let config_file = config.config_file.clone();
|
let config_file = config.config_file.clone();
|
||||||
let tx = tx.clone();
|
let tx = tx.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let key_map = KeyMaps::key_map();
|
do_map(&device_file, &config_file).ok();
|
||||||
//println!("key_map: {:?}", key_map);
|
|
||||||
|
|
||||||
let device = rusty_keys::open("/dev/uinput")
|
|
||||||
.or_else(|_| rusty_keys::open("/dev/input/uinput"))
|
|
||||||
.or_else(|_| rusty_keys::default())
|
|
||||||
.expect("cannot open uinput device")
|
|
||||||
.name("test").expect("cannot name uinput device")
|
|
||||||
.event(key_map.values()).expect("cannot register events on uinput device")
|
|
||||||
.create().expect("cannot create uinput device");
|
|
||||||
|
|
||||||
let mut input_device = InputDevice::open(&device_file);
|
|
||||||
input_device.grab();
|
|
||||||
|
|
||||||
let mut key_map = KeyMaps::from_cfg(&key_map, config_file);
|
|
||||||
//println!("keymaps: {:?}", keymaps);
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let mut event = if let Ok(e) = input_device.read_event() { e } else { break };
|
|
||||||
if event.type_ == EV_KEY_U16 {
|
|
||||||
key_map.send_event(&mut event, &device);
|
|
||||||
} else {
|
|
||||||
if device.write_event(&mut event).is_err() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tx.send(1).unwrap();
|
tx.send(1).unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -92,6 +66,33 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn do_map(device_file: &str, config_file: &str) -> Result<()> {
|
||||||
|
let key_map = KeyMaps::key_map();
|
||||||
|
//println!("key_map: {:?}", key_map);
|
||||||
|
|
||||||
|
let device = rusty_keys::open("/dev/uinput")
|
||||||
|
.or_else(|_| rusty_keys::open("/dev/input/uinput"))
|
||||||
|
.or_else(|_| rusty_keys::default())?
|
||||||
|
.name("rusty-keys")?
|
||||||
|
.event(key_map.values())?
|
||||||
|
.create()?;
|
||||||
|
|
||||||
|
let mut input_device = InputDevice::open(device_file)?;
|
||||||
|
input_device.grab()?;
|
||||||
|
|
||||||
|
let mut key_map = KeyMaps::from_cfg(&key_map, config_file);
|
||||||
|
//println!("keymaps: {:?}", keymaps);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut event = input_device.read_event()?;
|
||||||
|
if event.type_ == EV_KEY_U16 {
|
||||||
|
key_map.send_event(&mut event, &device);
|
||||||
|
} else {
|
||||||
|
device.write_event(&mut event)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_args() -> Config {
|
fn parse_args() -> Config {
|
||||||
fn print_usage(program: &str, opts: Options) {
|
fn print_usage(program: &str, opts: Options) {
|
||||||
let brief = format!("Usage: {} [options]", program);
|
let brief = format!("Usage: {} [options]", program);
|
||||||
@ -158,19 +159,15 @@ struct InputDevice {
|
|||||||
buf: [u8; SIZE_OF_INPUT_EVENT],
|
buf: [u8; SIZE_OF_INPUT_EVENT],
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "udev")]
|
use rusty_keys::{Error,Result};
|
||||||
extern crate libudev as udev;
|
|
||||||
mod error;
|
|
||||||
pub use error::Error;
|
|
||||||
type Result<T> = ::std::result::Result<T, Error>;
|
|
||||||
|
|
||||||
impl InputDevice {
|
impl InputDevice {
|
||||||
pub fn open(device_file: &str) -> Self {
|
pub fn open(device_file: &str) -> Result<Self> {
|
||||||
let device_file = File::open(device_file).unwrap_or_else(|e| panic!("{}", e));
|
let device_file = File::open(device_file)?;
|
||||||
InputDevice {
|
Ok(InputDevice {
|
||||||
device_file: device_file,
|
device_file: device_file,
|
||||||
buf: [0u8; SIZE_OF_INPUT_EVENT],
|
buf: [0u8; SIZE_OF_INPUT_EVENT],
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_event(&mut self) -> Result<input_event> {
|
pub fn read_event(&mut self) -> Result<input_event> {
|
||||||
@ -182,10 +179,11 @@ impl InputDevice {
|
|||||||
Ok(event)
|
Ok(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn grab(&mut self) {
|
pub fn grab(&mut self) -> Result<()> {
|
||||||
unsafe {
|
unsafe {
|
||||||
eviocgrab(self.device_file.as_raw_fd(), 1 as *const c_int).expect("no grab?");
|
eviocgrab(self.device_file.as_raw_fd(), 1 as *const c_int)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn release(&mut self) -> Result<()> {
|
pub fn release(&mut self) -> Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user