mirror of
https://github.com/htrefil/rkvm.git
synced 2025-02-12 08:48:12 +01:00
Pass input-device-paths config array to input monitor and use it to control which input device to register
This commit is contained in:
parent
ca04e96ca3
commit
8c96b40465
3 changed files with 32 additions and 14 deletions
|
@ -5,7 +5,8 @@ use futures::StreamExt;
|
||||||
use inotify::{Inotify, WatchMask};
|
use inotify::{Inotify, WatchMask};
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::collections::HashSet;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use tokio::sync::mpsc::{self, Receiver, Sender};
|
use tokio::sync::mpsc::{self, Receiver, Sender};
|
||||||
|
|
||||||
|
@ -16,9 +17,9 @@ pub struct Monitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Monitor {
|
impl Monitor {
|
||||||
pub fn new() -> Self {
|
pub fn new(input_device_paths: &HashSet<String>) -> Self {
|
||||||
let (sender, receiver) = mpsc::channel(1);
|
let (sender, receiver) = mpsc::channel(1);
|
||||||
tokio::spawn(monitor(sender));
|
tokio::spawn(monitor(sender, input_device_paths.clone()));
|
||||||
|
|
||||||
Self { receiver }
|
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 run = async {
|
||||||
let registry = Registry::new();
|
let registry = Registry::new();
|
||||||
|
|
||||||
|
@ -70,14 +71,16 @@ async fn monitor(sender: Sender<Result<Interceptor, Error>>) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let interceptor = match Interceptor::open(&path, ®istry).await {
|
if register_input_device(&input_device_paths, path.clone()) {
|
||||||
Ok(interceptor) => interceptor,
|
let interceptor = match Interceptor::open(&path, ®istry).await {
|
||||||
Err(OpenError::Io(err)) => return Err(err),
|
Ok(interceptor) => interceptor,
|
||||||
Err(OpenError::NotAppliable) => continue,
|
Err(OpenError::Io(err)) => return Err(err),
|
||||||
};
|
Err(OpenError::NotAppliable) => continue,
|
||||||
|
};
|
||||||
|
|
||||||
if sender.send(Ok(interceptor)).await.is_err() {
|
if sender.send(Ok(interceptor)).await.is_err() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,3 +97,17 @@ async fn monitor(sender: Sender<Result<Interceptor, Error>>) {
|
||||||
_ = sender.closed() => {}
|
_ = 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -68,10 +68,10 @@ async fn main() -> ExitCode {
|
||||||
};
|
};
|
||||||
|
|
||||||
let switch_keys = config.switch_keys.into_iter().map(Into::into).collect();
|
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! {
|
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 {
|
if let Err(err) = result {
|
||||||
tracing::error!("Error: {}", err);
|
tracing::error!("Error: {}", err);
|
||||||
return ExitCode::FAILURE;
|
return ExitCode::FAILURE;
|
||||||
|
|
|
@ -38,11 +38,12 @@ pub async fn run(
|
||||||
acceptor: TlsAcceptor,
|
acceptor: TlsAcceptor,
|
||||||
password: &str,
|
password: &str,
|
||||||
switch_keys: &HashSet<Key>,
|
switch_keys: &HashSet<Key>,
|
||||||
|
input_device_paths: &HashSet<String>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let listener = TcpListener::bind(&listen).await.map_err(Error::Network)?;
|
let listener = TcpListener::bind(&listen).await.map_err(Error::Network)?;
|
||||||
tracing::info!("Listening on {}", listen);
|
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 devices = Slab::<Device>::new();
|
||||||
let mut clients = Slab::<(Sender<_>, SocketAddr)>::new();
|
let mut clients = Slab::<(Sender<_>, SocketAddr)>::new();
|
||||||
let mut current = 0;
|
let mut current = 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue