Pass input-device-paths config array to input monitor and use it to control which input device to register

This commit is contained in:
Smith Dhumbumroong 2023-11-14 00:29:30 +07:00
parent ca04e96ca3
commit 8c96b40465
3 changed files with 32 additions and 14 deletions

View file

@ -5,7 +5,8 @@ use futures::StreamExt;
use inotify::{Inotify, WatchMask};
use std::ffi::OsStr;
use std::io::{Error, ErrorKind};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::collections::HashSet;
use tokio::fs;
use tokio::sync::mpsc::{self, Receiver, Sender};
@ -16,9 +17,9 @@ pub struct Monitor {
}
impl Monitor {
pub fn new() -> Self {
pub fn new(input_device_paths: &HashSet<String>) -> Self {
let (sender, receiver) = mpsc::channel(1);
tokio::spawn(monitor(sender));
tokio::spawn(monitor(sender, input_device_paths.clone()));
Self { receiver }
}
@ -31,7 +32,7 @@ impl Monitor {
}
}
async fn monitor(sender: Sender<Result<Interceptor, Error>>) {
async fn monitor(sender: Sender<Result<Interceptor, Error>>, input_device_paths: HashSet<String>) {
let run = async {
let registry = Registry::new();
@ -70,14 +71,16 @@ async fn monitor(sender: Sender<Result<Interceptor, Error>>) {
continue;
}
let interceptor = match Interceptor::open(&path, &registry).await {
Ok(interceptor) => interceptor,
Err(OpenError::Io(err)) => return Err(err),
Err(OpenError::NotAppliable) => continue,
};
if register_input_device(&input_device_paths, path.clone()) {
let interceptor = match Interceptor::open(&path, &registry).await {
Ok(interceptor) => interceptor,
Err(OpenError::Io(err)) => return Err(err),
Err(OpenError::NotAppliable) => continue,
};
if sender.send(Ok(interceptor)).await.is_err() {
return Ok(());
if sender.send(Ok(interceptor)).await.is_err() {
return Ok(());
}
}
}
@ -94,3 +97,17 @@ async fn monitor(sender: Sender<Result<Interceptor, Error>>) {
_ = sender.closed() => {}
}
}
fn register_input_device(input_device_paths: &HashSet<String>, input_device_path: PathBuf) -> bool {
if input_device_paths.len() > 0 {
match input_device_path.into_os_string().into_string() {
Ok(path) => return input_device_paths.contains(&path),
Err(err) => {
tracing::error!("Can't convert a path into string! {:?}", err);
return false;
},
}
} else {
return true;
}
}

View file

@ -68,10 +68,10 @@ async fn main() -> ExitCode {
};
let switch_keys = config.switch_keys.into_iter().map(Into::into).collect();
let device_names = config.device_names;
let input_device_paths = config.input_device_paths;
tokio::select! {
result = server::run(config.listen, acceptor, &config.password, &switch_keys, &device_names) => {
result = server::run(config.listen, acceptor, &config.password, &switch_keys, &input_device_paths) => {
if let Err(err) = result {
tracing::error!("Error: {}", err);
return ExitCode::FAILURE;

View file

@ -38,11 +38,12 @@ pub async fn run(
acceptor: TlsAcceptor,
password: &str,
switch_keys: &HashSet<Key>,
input_device_paths: &HashSet<String>,
) -> Result<(), Error> {
let listener = TcpListener::bind(&listen).await.map_err(Error::Network)?;
tracing::info!("Listening on {}", listen);
let mut monitor = Monitor::new();
let mut monitor = Monitor::new(input_device_paths);
let mut devices = Slab::<Device>::new();
let mut clients = Slab::<(Sender<_>, SocketAddr)>::new();
let mut current = 0;