2025-06-08 22:24:14 -05:00
|
|
|
|
// client/src/input/inputs.rs
|
|
|
|
|
|
|
|
|
|
|
|
use anyhow::{bail, Context, Result};
|
2025-06-11 00:37:01 -05:00
|
|
|
|
use evdev::{Device, EventType, KeyCode, RelativeAxisCode};
|
2025-06-08 22:24:14 -05:00
|
|
|
|
use tokio::time::{interval, Duration};
|
2025-06-11 00:37:01 -05:00
|
|
|
|
use tracing::{debug, info};
|
2025-06-08 22:24:14 -05:00
|
|
|
|
|
|
|
|
|
|
use crate::input::keyboard::KeyboardAggregator;
|
|
|
|
|
|
use crate::input::mouse::MouseAggregator;
|
|
|
|
|
|
use crate::input::camera::CameraCapture;
|
|
|
|
|
|
use crate::input::microphone::MicrophoneCapture;
|
|
|
|
|
|
|
|
|
|
|
|
/// A top-level aggregator that enumerates /dev/input/event*,
|
|
|
|
|
|
/// classifies them as keyboard vs. mouse vs. other,
|
|
|
|
|
|
/// spawns specialized aggregator objects, and can also
|
|
|
|
|
|
/// create stubs for camera/microphone logic if needed.
|
|
|
|
|
|
pub struct InputAggregator {
|
2025-06-11 00:37:01 -05:00
|
|
|
|
pub dev_mode: bool,
|
|
|
|
|
|
keyboards: Vec<KeyboardAggregator>,
|
|
|
|
|
|
mice: Vec<MouseAggregator>,
|
|
|
|
|
|
camera: Option<CameraCapture>,
|
|
|
|
|
|
mic: Option<MicrophoneCapture>,
|
2025-06-08 22:24:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl InputAggregator {
|
2025-06-11 00:37:01 -05:00
|
|
|
|
pub fn new(dev_mode: bool) -> Self {
|
2025-06-08 22:24:14 -05:00
|
|
|
|
Self {
|
2025-06-11 00:37:01 -05:00
|
|
|
|
dev_mode,
|
2025-06-08 22:24:14 -05:00
|
|
|
|
keyboards: Vec::new(),
|
|
|
|
|
|
mice: Vec::new(),
|
|
|
|
|
|
camera: None,
|
|
|
|
|
|
mic: None,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Called once at startup: enumerates input devices,
|
|
|
|
|
|
/// classifies them, and constructs a aggregator struct per type.
|
|
|
|
|
|
pub fn init(&mut self) -> Result<()> {
|
|
|
|
|
|
let paths = std::fs::read_dir("/dev/input")
|
|
|
|
|
|
.context("Failed to read /dev/input")?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut found_any = false;
|
|
|
|
|
|
|
|
|
|
|
|
for entry in paths {
|
|
|
|
|
|
let entry = entry?;
|
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
|
|
|
|
|
|
|
// skip anything that isn't "event*"
|
2025-06-11 00:37:01 -05:00
|
|
|
|
if !path.file_name().map_or(false, |f| f.to_string_lossy().starts_with("event")) {
|
2025-06-08 22:24:14 -05:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// attempt open
|
|
|
|
|
|
let mut dev = match Device::open(&path) {
|
|
|
|
|
|
Ok(d) => d,
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
tracing::debug!("Skipping {:?}, open error {e}", path);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-06-11 00:37:01 -05:00
|
|
|
|
// non‑blocking so fetch_events never stalls the whole loop
|
|
|
|
|
|
dev.set_nonblocking(true).with_context(|| format!("set_non_blocking {:?}", path))?;
|
|
|
|
|
|
|
2025-06-08 22:24:14 -05:00
|
|
|
|
match classify_device(&dev) {
|
|
|
|
|
|
DeviceKind::Keyboard => {
|
2025-06-11 00:37:01 -05:00
|
|
|
|
dev.grab().with_context(|| format!("grabbing keyboard {path:?}"))?;
|
2025-06-08 22:24:14 -05:00
|
|
|
|
info!("Grabbed keyboard {:?}", dev.name().unwrap_or("UNKNOWN"));
|
2025-06-11 00:37:01 -05:00
|
|
|
|
|
|
|
|
|
|
// pass dev_mode to aggregator
|
|
|
|
|
|
let kbd_agg = KeyboardAggregator::new(dev, self.dev_mode);
|
2025-06-08 22:24:14 -05:00
|
|
|
|
self.keyboards.push(kbd_agg);
|
|
|
|
|
|
found_any = true;
|
2025-06-11 00:37:01 -05:00
|
|
|
|
continue;
|
2025-06-08 22:24:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
DeviceKind::Mouse => {
|
2025-06-11 00:37:01 -05:00
|
|
|
|
dev.grab().with_context(|| format!("grabbing mouse {path:?}"))?;
|
2025-06-08 22:24:14 -05:00
|
|
|
|
info!("Grabbed mouse {:?}", dev.name().unwrap_or("UNKNOWN"));
|
2025-06-11 00:37:01 -05:00
|
|
|
|
|
2025-06-08 22:24:14 -05:00
|
|
|
|
let mouse_agg = MouseAggregator::new(dev);
|
|
|
|
|
|
self.mice.push(mouse_agg);
|
|
|
|
|
|
found_any = true;
|
2025-06-11 00:37:01 -05:00
|
|
|
|
continue;
|
2025-06-08 22:24:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
DeviceKind::Other => {
|
2025-06-11 00:37:01 -05:00
|
|
|
|
debug!("Skipping non-kbd/mouse device: {:?}", dev.name().unwrap_or("UNKNOWN"));
|
|
|
|
|
|
continue;
|
2025-06-08 22:24:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !found_any {
|
|
|
|
|
|
bail!("No suitable keyboard/mouse devices found or none grabbed.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Stubs for camera / mic:
|
|
|
|
|
|
self.camera = Some(CameraCapture::new_stub());
|
|
|
|
|
|
self.mic = Some(MicrophoneCapture::new_stub());
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// We spawn the sub-aggregators in a loop or using separate tasks.
|
|
|
|
|
|
/// (For a real system: you'd spawn a separate task for each aggregator.)
|
|
|
|
|
|
pub async fn run(&mut self) -> Result<()> {
|
|
|
|
|
|
// Example approach: poll each aggregator in a simple loop
|
|
|
|
|
|
let mut tick = interval(Duration::from_millis(10));
|
|
|
|
|
|
loop {
|
|
|
|
|
|
for kbd in &mut self.keyboards {
|
|
|
|
|
|
kbd.process_events();
|
|
|
|
|
|
}
|
|
|
|
|
|
for mouse in &mut self.mice {
|
|
|
|
|
|
mouse.process_events();
|
|
|
|
|
|
}
|
|
|
|
|
|
// camera / mic stubs could go here
|
|
|
|
|
|
tick.tick().await;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 00:37:01 -05:00
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
struct Classification {
|
|
|
|
|
|
keyboard: Option<()>,
|
|
|
|
|
|
mouse: Option<()>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-08 22:24:14 -05:00
|
|
|
|
/// The classification function
|
|
|
|
|
|
fn classify_device(dev: &Device) -> DeviceKind {
|
|
|
|
|
|
let evbits = dev.supported_events();
|
|
|
|
|
|
|
2025-06-11 00:37:01 -05:00
|
|
|
|
// Keyboard logic
|
2025-06-08 22:24:14 -05:00
|
|
|
|
if evbits.contains(EventType::KEY) {
|
|
|
|
|
|
if let Some(keys) = dev.supported_keys() {
|
|
|
|
|
|
if keys.contains(KeyCode::KEY_A) || keys.contains(KeyCode::KEY_ENTER) {
|
|
|
|
|
|
return DeviceKind::Keyboard;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-11 00:37:01 -05:00
|
|
|
|
|
|
|
|
|
|
// Mouse logic
|
|
|
|
|
|
if evbits.contains(EventType::RELATIVE) {
|
|
|
|
|
|
if let (Some(rel), Some(keys)) =
|
|
|
|
|
|
(dev.supported_relative_axes(), dev.supported_keys())
|
|
|
|
|
|
{
|
|
|
|
|
|
let has_xy = rel.contains(RelativeAxisCode::REL_X)
|
|
|
|
|
|
&& rel.contains(RelativeAxisCode::REL_Y);
|
|
|
|
|
|
let has_btn = keys.contains(KeyCode::BTN_LEFT)
|
|
|
|
|
|
|| keys.contains(KeyCode::BTN_RIGHT);
|
|
|
|
|
|
if has_xy && has_btn {
|
2025-06-08 22:24:14 -05:00
|
|
|
|
return DeviceKind::Mouse;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeviceKind::Other
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Internal enum for device classification
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
|
enum DeviceKind {
|
|
|
|
|
|
Keyboard,
|
|
|
|
|
|
Mouse,
|
|
|
|
|
|
Other,
|
|
|
|
|
|
}
|