diff --git a/src/device/device.rs b/src/device/device.rs index 7c3eef5..c148471 100644 --- a/src/device/device.rs +++ b/src/device/device.rs @@ -1,6 +1,5 @@ use std::{mem, ptr, slice}; -use libc::c_int; -use libc::{timeval, gettimeofday, input_event}; +use libc::{timeval, gettimeofday, input_event, c_int}; use nix::unistd; use ffi::*; use {Result as Res}; diff --git a/src/device/input_device.rs b/src/device/input_device.rs new file mode 100644 index 0000000..465ea18 --- /dev/null +++ b/src/device/input_device.rs @@ -0,0 +1,55 @@ +use std::mem; +use std::fs::File; +use std::io::Read; +use std::os::unix::io::AsRawFd; +use libc::{input_event, c_int}; +use {Error,Result}; + +ioctl!(write eviocgrab with b'E', 0x90; c_int); + +// TODO: use size_of_input_event instead of hard-coding 24. +const SIZE_OF_INPUT_EVENT: usize = 24;//mem::size_of::(); + +pub struct InputDevice { + device_file: File, + buf: [u8; SIZE_OF_INPUT_EVENT], +} + +impl InputDevice { + pub fn open(device_file: &str) -> Result { + let device_file = File::open(device_file)?; + Ok(InputDevice { + device_file: device_file, + buf: [0u8; SIZE_OF_INPUT_EVENT], + }) + } + + pub fn read_event(&mut self) -> Result { + let num_bytes = self.device_file.read(&mut self.buf)?; + if num_bytes != SIZE_OF_INPUT_EVENT { + return Err(Error::ShortRead); + } + let event: input_event = unsafe { mem::transmute(self.buf) }; + Ok(event) + } + + pub fn grab(&mut self) -> Result<()> { + unsafe { + eviocgrab(self.device_file.as_raw_fd(), 1 as *const c_int)?; + } + Ok(()) + } + + pub fn release(&mut self) -> Result<()> { + unsafe { + eviocgrab(self.device_file.as_raw_fd(), 0 as *const c_int)?; + } + Ok(()) + } +} + +impl Drop for InputDevice { + fn drop(&mut self) { + self.release().ok(); // ignore any errors here, what could we do anyhow? + } +} diff --git a/src/device/mod.rs b/src/device/mod.rs index 31a22b9..b1fb14e 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -3,3 +3,6 @@ pub use self::builder::Builder; mod device; pub use self::device::Device; + +mod input_device; +pub use self::input_device::InputDevice; diff --git a/src/lib.rs b/src/lib.rs index f38529d..c48cf9f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ extern crate libc; extern crate uinput_sys as ffi; +#[macro_use] extern crate nix; #[macro_use] @@ -23,7 +24,7 @@ pub mod keymapper; pub use keymapper::KeyMaps; pub mod device; -pub use device::Device; +pub use device::{Device,InputDevice}; /// Open the default uinput device. pub fn default() -> Result { diff --git a/src/main.rs b/src/main.rs index c37eaf1..f7c01aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,22 +4,15 @@ extern crate libc; extern crate getopts; extern crate inotify; -#[macro_use] -extern crate nix; - -use rusty_keys::{KeyMaps, Device}; +use rusty_keys::{KeyMaps, Device, InputDevice, Result}; use ffi::*; -use libc::{c_int, input_event}; +use libc::input_event; use std::process::{exit, Command}; -use std::fs::File; -use std::io::Read; -use std::{env, mem, thread}; +use std::{env, thread}; use std::sync::mpsc; use std::sync::mpsc::Sender; -use std::os::unix::io::AsRawFd; - use getopts::Options; use inotify::{ @@ -45,7 +38,10 @@ impl Config { } fn main() { - main_res().ok(); + let ret = main_res(); + if let Err(e) = ret { + println!("fatal error: {}", e); + } } fn main_res() -> Result<()> { @@ -218,58 +214,3 @@ fn get_keyboard_device_filenames() -> Vec { } filenames } - -// inputdevice stuff - -ioctl!(write eviocgrab with b'E', 0x90; c_int); - -// TODO: use size_of_input_event instead of hard-coding 24. -const SIZE_OF_INPUT_EVENT: usize = 24;//mem::size_of::(); - -struct InputDevice { - device_file: File, - buf: [u8; SIZE_OF_INPUT_EVENT], -} - -use rusty_keys::{Error,Result}; - -impl InputDevice { - pub fn open(device_file: &str) -> Result { - let device_file = File::open(device_file)?; - Ok(InputDevice { - device_file: device_file, - buf: [0u8; SIZE_OF_INPUT_EVENT], - }) - } - - pub fn read_event(&mut self) -> Result { - let num_bytes = self.device_file.read(&mut self.buf)?; - if num_bytes != SIZE_OF_INPUT_EVENT { - return Err(Error::ShortRead); - } - let event: input_event = unsafe { mem::transmute(self.buf) }; - Ok(event) - } - - pub fn grab(&mut self) -> Result<()> { - unsafe { - eviocgrab(self.device_file.as_raw_fd(), 1 as *const c_int)?; - } - Ok(()) - } - - pub fn release(&mut self) -> Result<()> { - unsafe { - eviocgrab(self.device_file.as_raw_fd(), 0 as *const c_int)?; - } - Ok(()) - } -} - -impl Drop for InputDevice { - fn drop(&mut self) { - self.release().ok(); - } -} - -