Make epoll and inotify optional dependencies enabled by default with the epoll_inotify feature
All checks were successful
moparisthebest/rusty-keys/pipeline/head This commit looks good
All checks were successful
moparisthebest/rusty-keys/pipeline/head This commit looks good
This commit is contained in:
parent
c420323cdf
commit
60a3f24c86
@ -32,5 +32,9 @@ lazy_static = "1.4.0"
|
|||||||
[target.'cfg(target_os="linux")'.dependencies]
|
[target.'cfg(target_os="linux")'.dependencies]
|
||||||
libc = "0.2.102"
|
libc = "0.2.102"
|
||||||
nix = "0.22.1"
|
nix = "0.22.1"
|
||||||
epoll = "4.3.1"
|
epoll = { version = "4.3.1", optional = true }
|
||||||
inotify = { version = "0.9.3", default-features = false, features = [] }
|
inotify = { version = "0.9.3", default-features = false, features = [], optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["epoll_inotify"]
|
||||||
|
epoll_inotify = ["epoll", "inotify"]
|
||||||
|
@ -5,9 +5,11 @@ use std::os::unix::io::AsRawFd;
|
|||||||
use std::os::unix::prelude::RawFd;
|
use std::os::unix::prelude::RawFd;
|
||||||
use libc::{input_event, c_int};
|
use libc::{input_event, c_int};
|
||||||
use nix::{ioctl_write_ptr, ioctl_read_buf};
|
use nix::{ioctl_write_ptr, ioctl_read_buf};
|
||||||
use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
|
|
||||||
use nix::fcntl::{OFlag, fcntl, FcntlArg};
|
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::{Error,Result};
|
||||||
use crate::linux::{EV_KEY, KEY_MAX, NAME, KEY_W, KEY_A, KEY_S, KEY_D};
|
use crate::linux::{EV_KEY, KEY_MAX, NAME, KEY_W, KEY_A, KEY_S, KEY_D};
|
||||||
|
|
||||||
@ -105,6 +107,7 @@ impl InputDevice {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "epoll_inotify")]
|
||||||
pub fn epoll_add(mut self, epoll_fd: RawFd, data: u64) -> Result<Self> {
|
pub fn epoll_add(mut self, epoll_fd: RawFd, data: u64) -> Result<Self> {
|
||||||
if None != self.epoll_fd {
|
if None != self.epoll_fd {
|
||||||
return Err(Error::EpollAlreadyAdded);
|
return Err(Error::EpollAlreadyAdded);
|
||||||
@ -122,6 +125,7 @@ impl InputDevice {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "epoll_inotify")]
|
||||||
pub fn epoll_del(&mut self) -> Result<&mut Self> {
|
pub fn epoll_del(&mut self) -> Result<&mut Self> {
|
||||||
if let Some(epoll_fd) = self.epoll_fd {
|
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...
|
// 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) {
|
fn drop(&mut self) {
|
||||||
// ignore any errors here, what could we do anyhow?
|
// ignore any errors here, what could we do anyhow?
|
||||||
self.release().ok();
|
self.release().ok();
|
||||||
|
#[cfg(feature = "epoll_inotify")]
|
||||||
self.epoll_del().ok();
|
self.epoll_del().ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ pub use device::{Device,InputDevice, Builder};
|
|||||||
use libc::input_event;
|
use libc::input_event;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
const INPUT_FOLDER: &str = "/dev/input/";
|
const INPUT_FOLDER: &str = "/dev/input/";
|
||||||
|
|
||||||
@ -18,12 +19,6 @@ const UP: i32 = 0;
|
|||||||
|
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
|
|
||||||
use inotify::{
|
|
||||||
Inotify,
|
|
||||||
WatchMask,
|
|
||||||
};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
const EV_KEY_U16: u16 = EV_KEY as u16;
|
const EV_KEY_U16: u16 = EV_KEY as u16;
|
||||||
|
|
||||||
type LinuxKeyMaps = KeyMaps<Device, u16, input_event>;
|
type LinuxKeyMaps = KeyMaps<Device, u16, input_event>;
|
||||||
@ -127,6 +122,13 @@ pub fn main_res() -> Result<()> {
|
|||||||
send_event(&mut key_map, event, &device)?
|
send_event(&mut key_map, event, &device)?
|
||||||
}
|
}
|
||||||
} else {
|
} 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)?;
|
let epoll_fd = epoll::create(true)?;
|
||||||
const INOTIFY_DATA: u64 = u64::max_value();
|
const INOTIFY_DATA: u64 = u64::max_value();
|
||||||
|
|
||||||
@ -196,7 +198,7 @@ pub fn main_res() -> Result<()> {
|
|||||||
path.push(INPUT_FOLDER);
|
path.push(INPUT_FOLDER);
|
||||||
path.push(device_file);
|
path.push(device_file);
|
||||||
|
|
||||||
if let Ok(input_device) = InputDevice::open(path).and_then(|id|id.valid_keyboard_device()) {
|
if let Ok(input_device) = InputDevice::open(path).and_then(|id| id.valid_keyboard_device()) {
|
||||||
println!("starting mapping for new keyboard: {}", device_file);
|
println!("starting mapping for new keyboard: {}", device_file);
|
||||||
|
|
||||||
let idx = input_devices.iter().position(|id| id.is_none()).unwrap_or(input_devices.len());
|
let idx = input_devices.iter().position(|id| id.is_none()).unwrap_or(input_devices.len());
|
||||||
@ -217,6 +219,7 @@ pub fn main_res() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_event(key_map: &mut LinuxKeyMaps, mut event: input_event, device: &Device) -> Result<()> {
|
fn send_event(key_map: &mut LinuxKeyMaps, mut event: input_event, device: &Device) -> Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user