Support multiple devices with threads, hackily

This commit is contained in:
Travis Burtrum 2017-11-14 21:50:34 -05:00
parent 74d3350743
commit f67973c932

View File

@ -18,6 +18,7 @@ use std::process::{exit, Command};
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::{env, mem}; use std::{env, mem};
use std::thread;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
@ -29,17 +30,24 @@ const EV_KEY_U16: u16 = EV_KEY as u16;
#[derive(Debug)] #[derive(Debug)]
struct Config { struct Config {
device_file: String, device_files: Vec<String>,
config_file: String config_file: String
} }
impl Config { impl Config {
fn new(device_file: String, config_file: String) -> Self { fn new(device_files: Vec<String>, config_file: String) -> Self {
Config { device_file: device_file, config_file: config_file } Config { device_files: device_files, config_file: config_file }
} }
} }
fn main() { fn main() {
let config = parse_args();
//println!("Config: {:?}", config);
for device_file in config.device_files.iter() {
let device_file = device_file.clone();
let config_file = config.config_file.clone();
thread::spawn(move || {
let key_map = KeyMaps::key_map(); let key_map = KeyMaps::key_map();
//println!("key_map: {:?}", key_map); //println!("key_map: {:?}", key_map);
@ -51,15 +59,10 @@ fn main() {
.event(key_map.values()).expect("cannot register events on uinput device") .event(key_map.values()).expect("cannot register events on uinput device")
.create().expect("cannot create uinput device"); .create().expect("cannot create uinput device");
//thread::sleep(Duration::from_secs(1)); let mut input_device = InputDevice::open(&device_file);
let config = parse_args();
//println!("Config: {:?}", config);
let mut input_device = InputDevice::open(&config.device_file);
input_device.grab(); input_device.grab();
let mut key_map = KeyMaps::from_cfg(&key_map, config.config_file); let mut key_map = KeyMaps::from_cfg(&key_map, config_file);
//println!("keymaps: {:?}", keymaps); //println!("keymaps: {:?}", keymaps);
loop { loop {
@ -76,6 +79,12 @@ fn main() {
device.write_event(&mut event).expect("could not write event?"); device.write_event(&mut event).expect("could not write event?");
} }
} }
});
}
// wait for all threads to finish
loop {
thread::sleep(std::time::Duration::from_secs(u64::max_value()));
}
} }
fn parse_args() -> Config { fn parse_args() -> Config {
@ -89,8 +98,8 @@ fn parse_args() -> Config {
let mut opts = Options::new(); let mut opts = Options::new();
opts.optflag("h", "help", "prints this help message"); opts.optflag("h", "help", "prints this help message");
opts.optflag("v", "version", "prints the version"); opts.optflag("v", "version", "prints the version");
opts.optopt("d", "device", "specify the keyboard input device file", "DEVICE");
opts.optopt("c", "config", "specify the keymap config file to use", "FILE"); opts.optopt("c", "config", "specify the keymap config file to use", "FILE");
opts.optmulti("d", "device", "specify the keyboard input device file", "DEVICE");
let matches = opts.parse(&args[1..]).unwrap_or_else(|e| panic!("{}", e)); let matches = opts.parse(&args[1..]).unwrap_or_else(|e| panic!("{}", e));
if matches.opt_present("h") { if matches.opt_present("h") {
@ -103,22 +112,14 @@ fn parse_args() -> Config {
exit(0); exit(0);
} }
let device_file = matches.opt_str("d").unwrap_or_else(|| get_default_device()); let mut device_files = matches.opt_strs("d");
if device_files.len() == 0 {
device_files = get_keyboard_device_filenames();
}
println!("Detected devices: {:?}", device_files);
let config_file = matches.opt_str("c").unwrap_or("keymap.toml".to_owned()); let config_file = matches.opt_str("c").unwrap_or("keymap.toml".to_owned());
Config::new(device_file, config_file) Config::new(device_files, config_file)
}
fn get_default_device() -> String {
let mut filenames = get_keyboard_device_filenames();
println!("Detected devices: {:?}", filenames);
if filenames.len() == 1 {
filenames.swap_remove(0)
} else {
panic!("The following keyboard devices were detected: {:?}. Please select one using \
the `-d` flag", filenames);
}
} }
// Detects and returns the name of the keyboard device file. This function uses // Detects and returns the name of the keyboard device file. This function uses