2017-09-21 23:27:49 -04:00
|
|
|
extern crate rusty_keys;
|
2017-09-01 00:24:35 -04:00
|
|
|
extern crate uinput_sys as ffi;
|
2017-09-08 01:16:34 -04:00
|
|
|
extern crate libc;
|
|
|
|
extern crate getopts;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate nix;
|
|
|
|
|
2017-09-21 23:27:49 -04:00
|
|
|
use rusty_keys::KeyMaps;
|
2017-09-01 00:24:35 -04:00
|
|
|
|
|
|
|
use ffi::*;
|
2017-09-08 01:16:34 -04:00
|
|
|
use libc::{c_int, input_event};
|
2016-04-25 11:13:05 -04:00
|
|
|
|
2017-09-22 00:30:56 -04:00
|
|
|
//use std::thread;
|
|
|
|
//use std::time::Duration;
|
2017-09-08 01:16:34 -04:00
|
|
|
|
|
|
|
use std::process::{exit, Command};
|
|
|
|
use std::fs::File;
|
2017-09-19 01:46:36 -04:00
|
|
|
use std::io::Read;
|
2017-09-08 01:16:34 -04:00
|
|
|
use std::{env, mem};
|
2017-11-14 21:50:34 -05:00
|
|
|
use std::thread;
|
2017-11-15 00:03:42 -05:00
|
|
|
use std::sync::mpsc;
|
2017-09-08 01:16:34 -04:00
|
|
|
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
|
|
|
use getopts::Options;
|
|
|
|
|
|
|
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
|
|
|
|
2017-09-19 01:46:36 -04:00
|
|
|
const EV_KEY_U16: u16 = EV_KEY as u16;
|
2017-09-08 01:16:34 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Config {
|
2017-11-14 21:50:34 -05:00
|
|
|
device_files: Vec<String>,
|
2017-09-22 00:30:56 -04:00
|
|
|
config_file: String
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2017-11-14 21:50:34 -05:00
|
|
|
fn new(device_files: Vec<String>, config_file: String) -> Self {
|
|
|
|
Config { device_files: device_files, config_file: config_file }
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 11:13:05 -04:00
|
|
|
|
|
|
|
fn main() {
|
2017-09-08 01:16:34 -04:00
|
|
|
let config = parse_args();
|
2017-09-22 00:30:56 -04:00
|
|
|
//println!("Config: {:?}", config);
|
2017-11-15 00:03:42 -05:00
|
|
|
let (tx, rx) = mpsc::channel();
|
2017-09-08 01:16:34 -04:00
|
|
|
|
2017-11-14 21:50:34 -05:00
|
|
|
for device_file in config.device_files.iter() {
|
|
|
|
let device_file = device_file.clone();
|
|
|
|
let config_file = config.config_file.clone();
|
2017-11-15 00:03:42 -05:00
|
|
|
let tx = tx.clone();
|
2017-11-14 21:50:34 -05:00
|
|
|
thread::spawn(move || {
|
2017-11-15 01:08:38 -05:00
|
|
|
let ret = do_map(&device_file, &config_file).err();
|
|
|
|
if let Some(e) = ret {
|
|
|
|
println!("mapping for {} ended due to error: {}", device_file, e);
|
|
|
|
}
|
2017-11-15 00:03:42 -05:00
|
|
|
tx.send(1).unwrap();
|
2017-11-14 21:50:34 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// wait for all threads to finish
|
2017-11-15 00:03:42 -05:00
|
|
|
let mut num_threads = config.device_files.len();
|
|
|
|
for received in rx {
|
2017-11-15 01:08:38 -05:00
|
|
|
num_threads -= received;
|
2017-11-15 00:03:42 -05:00
|
|
|
if num_threads == 0 {
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
2016-04-25 11:13:05 -04:00
|
|
|
}
|
2017-09-08 01:16:34 -04:00
|
|
|
|
2017-11-15 00:39:00 -05:00
|
|
|
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 {
|
2017-11-15 01:08:38 -05:00
|
|
|
key_map.send_event(&mut event, &device)?
|
2017-11-15 00:39:00 -05:00
|
|
|
} else {
|
|
|
|
device.write_event(&mut event)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 01:16:34 -04:00
|
|
|
fn parse_args() -> Config {
|
|
|
|
fn print_usage(program: &str, opts: Options) {
|
|
|
|
let brief = format!("Usage: {} [options]", program);
|
|
|
|
println!("{}", opts.usage(&brief));
|
|
|
|
}
|
|
|
|
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
|
|
|
|
let mut opts = Options::new();
|
|
|
|
opts.optflag("h", "help", "prints this help message");
|
|
|
|
opts.optflag("v", "version", "prints the version");
|
2017-09-22 00:30:56 -04:00
|
|
|
opts.optopt("c", "config", "specify the keymap config file to use", "FILE");
|
2017-11-14 21:50:34 -05:00
|
|
|
opts.optmulti("d", "device", "specify the keyboard input device file", "DEVICE");
|
2017-09-08 01:16:34 -04:00
|
|
|
|
|
|
|
let matches = opts.parse(&args[1..]).unwrap_or_else(|e| panic!("{}", e));
|
|
|
|
if matches.opt_present("h") {
|
|
|
|
print_usage(&args[0], opts);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("v") {
|
2017-09-22 00:30:56 -04:00
|
|
|
println!("rusty-keys {}", VERSION);
|
2017-09-08 01:16:34 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:50:34 -05:00
|
|
|
let mut device_files = matches.opt_strs("d");
|
|
|
|
if device_files.len() == 0 {
|
|
|
|
device_files = get_keyboard_device_filenames();
|
|
|
|
}
|
|
|
|
println!("Detected devices: {:?}", device_files);
|
2017-09-22 00:30:56 -04:00
|
|
|
let config_file = matches.opt_str("c").unwrap_or("keymap.toml".to_owned());
|
2017-09-08 01:16:34 -04:00
|
|
|
|
2017-11-14 21:50:34 -05:00
|
|
|
Config::new(device_files, config_file)
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detects and returns the name of the keyboard device file. This function uses
|
|
|
|
// the fact that all device information is shown in /proc/bus/input/devices and
|
|
|
|
// the keyboard device file should always have an EV of 120013
|
|
|
|
fn get_keyboard_device_filenames() -> Vec<String> {
|
2017-11-15 00:03:42 -05:00
|
|
|
let command_str = "grep -E 'Handlers|EV' /proc/bus/input/devices | grep -B1 120013 | grep -Eo event[0-9]+".to_string();
|
2017-09-08 01:16:34 -04:00
|
|
|
let res = Command::new("sh").arg("-c").arg(command_str).output().unwrap_or_else(|e| {
|
|
|
|
panic!("{}", e);
|
|
|
|
});
|
|
|
|
let res_str = std::str::from_utf8(&res.stdout).unwrap();
|
|
|
|
|
|
|
|
let mut filenames = Vec::new();
|
|
|
|
for file in res_str.trim().split('\n') {
|
|
|
|
let mut filename = "/dev/input/".to_string();
|
|
|
|
filename.push_str(file);
|
|
|
|
filenames.push(filename);
|
|
|
|
}
|
|
|
|
filenames
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputdevice stuff
|
|
|
|
|
|
|
|
ioctl!(write eviocgrab with b'E', 0x90; c_int);
|
|
|
|
|
|
|
|
// TODO: use size_of_input_event instead of hard-coding 24.
|
2017-09-19 01:46:36 -04:00
|
|
|
const SIZE_OF_INPUT_EVENT: usize = 24;//mem::size_of::<input_event>();
|
2017-09-08 01:16:34 -04:00
|
|
|
|
|
|
|
struct InputDevice {
|
2017-09-19 01:46:36 -04:00
|
|
|
device_file: File,
|
|
|
|
buf: [u8; SIZE_OF_INPUT_EVENT],
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 00:39:00 -05:00
|
|
|
use rusty_keys::{Error,Result};
|
2017-11-15 00:03:42 -05:00
|
|
|
|
2017-09-08 01:16:34 -04:00
|
|
|
impl InputDevice {
|
2017-11-15 00:39:00 -05:00
|
|
|
pub fn open(device_file: &str) -> Result<Self> {
|
|
|
|
let device_file = File::open(device_file)?;
|
|
|
|
Ok(InputDevice {
|
2017-09-08 01:16:34 -04:00
|
|
|
device_file: device_file,
|
|
|
|
buf: [0u8; SIZE_OF_INPUT_EVENT],
|
2017-11-15 00:39:00 -05:00
|
|
|
})
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 00:03:42 -05:00
|
|
|
pub fn read_event(&mut self) -> Result<input_event> {
|
|
|
|
let num_bytes = self.device_file.read(&mut self.buf)?;
|
2017-09-08 01:16:34 -04:00
|
|
|
if num_bytes != SIZE_OF_INPUT_EVENT {
|
2017-11-15 00:03:42 -05:00
|
|
|
return Err(Error::ShortRead);
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
let event: input_event = unsafe { mem::transmute(self.buf) };
|
2017-11-15 00:03:42 -05:00
|
|
|
Ok(event)
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 00:39:00 -05:00
|
|
|
pub fn grab(&mut self) -> Result<()> {
|
2017-09-08 01:16:34 -04:00
|
|
|
unsafe {
|
2017-11-15 00:39:00 -05:00
|
|
|
eviocgrab(self.device_file.as_raw_fd(), 1 as *const c_int)?;
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
2017-11-15 00:39:00 -05:00
|
|
|
Ok(())
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 00:03:42 -05:00
|
|
|
pub fn release(&mut self) -> Result<()> {
|
2017-09-08 01:16:34 -04:00
|
|
|
unsafe {
|
2017-11-15 00:03:42 -05:00
|
|
|
eviocgrab(self.device_file.as_raw_fd(), 0 as *const c_int)?;
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
2017-11-15 00:03:42 -05:00
|
|
|
Ok(())
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for InputDevice {
|
|
|
|
fn drop(&mut self) {
|
2017-11-15 00:03:42 -05:00
|
|
|
self.release().ok();
|
2017-09-08 01:16:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 01:08:17 -04:00
|
|
|
|