Make epoll and inotify optional dependencies enabled by default with the epoll_inotify feature
moparisthebest/rusty-keys/pipeline/head This commit looks good Details

This commit is contained in:
Travis Burtrum 2021-09-24 01:03:27 -04:00
parent c420323cdf
commit 60a3f24c86
3 changed files with 93 additions and 81 deletions

View File

@ -32,5 +32,9 @@ lazy_static = "1.4.0"
[target.'cfg(target_os="linux")'.dependencies]
libc = "0.2.102"
nix = "0.22.1"
epoll = "4.3.1"
inotify = { version = "0.9.3", default-features = false, features = [] }
epoll = { version = "4.3.1", optional = true }
inotify = { version = "0.9.3", default-features = false, features = [], optional = true }
[features]
default = ["epoll_inotify"]
epoll_inotify = ["epoll", "inotify"]

View File

@ -5,9 +5,11 @@ use std::os::unix::io::AsRawFd;
use std::os::unix::prelude::RawFd;
use libc::{input_event, c_int};
use nix::{ioctl_write_ptr, ioctl_read_buf};
use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
use nix::fcntl::{OFlag, fcntl, FcntlArg};
#[cfg(feature = "epoll_inotify")]
use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
use crate::{Error,Result};
use crate::linux::{EV_KEY, KEY_MAX, NAME, KEY_W, KEY_A, KEY_S, KEY_D};
@ -105,6 +107,7 @@ impl InputDevice {
Ok(())
}
#[cfg(feature = "epoll_inotify")]
pub fn epoll_add(mut self, epoll_fd: RawFd, data: u64) -> Result<Self> {
if None != self.epoll_fd {
return Err(Error::EpollAlreadyAdded);
@ -122,6 +125,7 @@ impl InputDevice {
Ok(self)
}
#[cfg(feature = "epoll_inotify")]
pub fn epoll_del(&mut self) -> Result<&mut Self> {
if let Some(epoll_fd) = self.epoll_fd {
// set this to None first, if we end up returning an Err early, we can't do anything else anyway...
@ -137,6 +141,7 @@ impl Drop for InputDevice {
fn drop(&mut self) {
// ignore any errors here, what could we do anyhow?
self.release().ok();
#[cfg(feature = "epoll_inotify")]
self.epoll_del().ok();
}
}

View File

@ -9,6 +9,7 @@ pub use device::{Device,InputDevice, Builder};
use libc::input_event;
use std::process::exit;
use std::env;
use std::collections::HashMap;
const INPUT_FOLDER: &str = "/dev/input/";
@ -18,12 +19,6 @@ const UP: i32 = 0;
use getopts::Options;
use inotify::{
Inotify,
WatchMask,
};
use std::collections::HashMap;
const EV_KEY_U16: u16 = EV_KEY as u16;
type LinuxKeyMaps = KeyMaps<Device, u16, input_event>;
@ -127,6 +122,13 @@ pub fn main_res() -> Result<()> {
send_event(&mut key_map, event, &device)?
}
} else {
#[cfg(not(feature = "epoll_inotify"))]
panic!("without epoll_inotify feature, only exactly 1 device is supported");
#[cfg(feature = "epoll_inotify")]
{
use inotify::{Inotify, WatchMask};
let epoll_fd = epoll::create(true)?;
const INOTIFY_DATA: u64 = u64::max_value();
@ -218,6 +220,7 @@ pub fn main_res() -> Result<()> {
}
}
}
}
fn send_event(key_map: &mut LinuxKeyMaps, mut event: input_event, device: &Device) -> Result<()> {
if event.type_ == EV_KEY_U16 {