Refactor out InputDevice module

This commit is contained in:
Travis Burtrum 2017-11-16 23:21:21 -05:00
parent 84b176a75e
commit e7fc77773c
5 changed files with 68 additions and 69 deletions

View File

@ -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};

View File

@ -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::<input_event>();
pub struct InputDevice {
device_file: File,
buf: [u8; SIZE_OF_INPUT_EVENT],
}
impl InputDevice {
pub fn open(device_file: &str) -> Result<Self> {
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<input_event> {
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?
}
}

View File

@ -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;

View File

@ -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<device::Builder> {

View File

@ -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<String> {
}
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::<input_event>();
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<Self> {
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<input_event> {
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();
}
}