Add shortcut for mapping 1 device

This commit is contained in:
Travis Burtrum 2017-11-16 22:16:13 -05:00
parent 99cf9c452f
commit 84b176a75e

View File

@ -7,19 +7,14 @@ extern crate inotify;
#[macro_use]
extern crate nix;
use rusty_keys::KeyMaps;
use rusty_keys::{KeyMaps, Device};
use ffi::*;
use libc::{c_int, input_event};
//use std::thread;
//use std::time::Duration;
use std::process::{exit, Command};
use std::fs::File;
use std::io::Read;
use std::{env, mem};
use std::thread;
use std::{env, mem, thread};
use std::sync::mpsc;
use std::sync::mpsc::Sender;
@ -70,6 +65,17 @@ fn main_res() -> Result<()> {
let mut key_map = KeyMaps::from_cfg(&key_map, &config.config_file);
//println!("keymaps: {:?}", keymaps);
if config.device_files.len() == 1 {
// shortcut, don't bother with threads
let mut input_device = InputDevice::open(&config.device_files[0])?;
input_device.grab()?;
loop {
let event = input_device.read_event()?;
send_event(&mut key_map, event, &device)?
}
} else {
// start up some intra thread communication
let (tx, rx) = mpsc::channel();
if config.device_files.len() > 0 {
@ -122,13 +128,19 @@ fn main_res() -> Result<()> {
}
drop(tx); // drop our last one, so when the threads finish, everything stops
// process all events
for mut event in rx {
for event in rx {
send_event(&mut key_map, event, &device)?
}
}
Ok(())
}
fn send_event(key_map: &mut KeyMaps, mut event: input_event, device: &Device) -> Result<()> {
if event.type_ == EV_KEY_U16 {
key_map.send_event(&mut event, &device)?
} else {
device.write_event(&mut event)?
}
}
Ok(())
}