lesavka/client/src/input/inputs.rs

310 lines
10 KiB
Rust
Raw Normal View History

2025-06-08 22:24:14 -05:00
// client/src/input/inputs.rs
use anyhow::{Context, Result, bail};
use evdev::{AbsoluteAxisCode, Device, EventType, KeyCode, RelativeAxisCode};
use std::collections::HashSet;
use tokio::{
sync::broadcast::Sender,
time::{Duration, interval},
};
2025-11-30 23:41:29 -03:00
use tracing::{debug, info, warn};
2025-06-08 22:24:14 -05:00
2025-06-23 07:18:26 -05:00
use lesavka_common::lesavka::{KeyboardReport, MouseReport};
2025-07-04 01:56:59 -05:00
use super::{keyboard::KeyboardAggregator, mouse::MouseAggregator};
2025-06-29 04:54:39 -05:00
use crate::layout::{Layout, apply as apply_layout};
use tokio::sync::mpsc::UnboundedSender;
2025-06-08 22:24:14 -05:00
pub struct InputAggregator {
2025-06-17 20:54:31 -05:00
kbd_tx: Sender<KeyboardReport>,
mou_tx: Sender<MouseReport>,
dev_mode: bool,
2025-06-28 15:45:35 -05:00
released: bool,
2025-06-28 17:55:15 -05:00
magic_active: bool,
pending_release: bool,
pending_kill: bool,
pending_keys: HashSet<KeyCode>,
paste_tx: Option<UnboundedSender<String>>,
2025-06-17 20:54:31 -05:00
keyboards: Vec<KeyboardAggregator>,
mice: Vec<MouseAggregator>,
2025-06-08 22:24:14 -05:00
}
impl InputAggregator {
pub fn new(
dev_mode: bool,
kbd_tx: Sender<KeyboardReport>,
mou_tx: Sender<MouseReport>,
paste_tx: Option<UnboundedSender<String>>,
) -> Self {
Self {
kbd_tx,
mou_tx,
dev_mode,
released: false,
magic_active: false,
pending_release: false,
pending_kill: false,
pending_keys: HashSet::new(),
paste_tx,
keyboards: Vec::new(),
mice: Vec::new(),
}
2025-06-08 22:24:14 -05:00
}
/// 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")?;
2025-06-08 22:24:14 -05:00
let mut found_any = false;
for entry in paths {
let entry = entry?;
let path = entry.path();
// skip anything that isn't "event*"
if !path
.file_name()
.map_or(false, |f| f.to_string_lossy().starts_with("event"))
{
2025-06-08 22:24:14 -05:00
continue;
}
2025-06-29 22:39:17 -05:00
// ─── open the event node read-write *without* unsafe ──────────
2025-06-29 04:54:39 -05:00
let mut dev = match Device::open(&path) {
2025-06-08 22:24:14 -05:00
Ok(d) => d,
2025-06-29 04:17:44 -05:00
Err(e) => {
2025-06-29 04:54:39 -05:00
warn!("❌ open {}: {e}", path.display());
2025-06-29 04:17:44 -05:00
continue;
}
2025-06-08 22:24:14 -05:00
};
2025-06-28 15:45:35 -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-11 00:37:01 -05:00
2025-06-08 22:24:14 -05:00
match classify_device(&dev) {
DeviceKind::Keyboard => {
dev.grab()
.with_context(|| format!("grabbing keyboard {path:?}"))?;
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);
let kbd_agg = KeyboardAggregator::new(
dev,
self.dev_mode,
self.kbd_tx.clone(),
self.paste_tx.clone(),
);
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 => {
dev.grab()
.with_context(|| format!("grabbing mouse {path:?}"))?;
2025-06-30 15:45:37 -05:00
info!("🤏⌨️ Grabbed mouse {:?}", dev.name().unwrap_or("UNKNOWN"));
2025-06-11 00:37:01 -05:00
2025-06-17 20:54:31 -05:00
// let mouse_agg = MouseAggregator::new(dev);
2025-06-17 08:17:23 -05:00
let mouse_agg = MouseAggregator::new(dev, self.dev_mode, self.mou_tx.clone());
2025-06-08 22:24:14 -05:00
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 => {
debug!(
"Skipping non-kbd/mouse device: {:?}",
dev.name().unwrap_or("UNKNOWN")
);
2025-06-11 00:37:01 -05:00
continue;
2025-06-08 22:24:14 -05:00
}
}
}
if !found_any {
bail!("No suitable keyboard/mouse devices found or none grabbed.");
}
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));
2025-06-29 04:54:39 -05:00
let mut current = Layout::SideBySide;
2025-06-08 22:24:14 -05:00
loop {
let mut want_kill = false;
2025-06-08 22:24:14 -05:00
for kbd in &mut self.keyboards {
kbd.process_events();
want_kill |= kbd.magic_kill();
2025-06-08 22:24:14 -05:00
}
let magic_now = self.keyboards.iter().any(|k| k.magic_grab());
let magic_left = self.keyboards.iter().any(|k| k.magic_left());
let magic_right = self.keyboards.iter().any(|k| k.magic_right());
2025-06-28 15:45:35 -05:00
if magic_now && !self.magic_active {
self.toggle_grab();
}
2025-06-29 04:54:39 -05:00
if (magic_left || magic_right) && self.magic_active {
current = match current {
Layout::SideBySide => Layout::FullLeft,
Layout::FullLeft => Layout::FullRight,
Layout::FullRight => Layout::SideBySide,
2025-06-29 04:54:39 -05:00
};
apply_layout(current);
}
if want_kill && !self.pending_kill {
2025-06-28 15:45:35 -05:00
warn!("🧙 magic chord - killing 🪄 AVADA KEDAVRA!!! 💥💀⚰️");
for k in &mut self.keyboards {
k.send_empty_report();
k.set_send(false);
}
for m in &mut self.mice {
m.reset_state();
m.set_send(false);
}
self.pending_kill = true;
self.capture_pending_keys();
}
if self.pending_release || self.pending_kill {
let chord_released = if self.pending_keys.is_empty() {
!self
.keyboards
.iter()
.any(|k| k.magic_grab() || k.magic_kill())
} else {
self.pending_keys
.iter()
.all(|key| !self.keyboards.iter().any(|k| k.has_key(*key)))
};
if chord_released {
for k in &mut self.keyboards {
k.set_grab(false);
k.reset_state();
}
for m in &mut self.mice {
m.set_grab(false);
m.reset_state();
}
self.released = true;
if self.pending_kill {
return Ok(());
}
self.pending_release = false;
self.pending_keys.clear();
}
2025-06-28 15:45:35 -05:00
}
2025-06-08 22:24:14 -05:00
for mouse in &mut self.mice {
mouse.process_events();
}
2025-06-28 17:55:15 -05:00
self.magic_active = magic_now;
2025-06-08 22:24:14 -05:00
tick.tick().await;
}
}
2025-06-28 15:45:35 -05:00
fn toggle_grab(&mut self) {
if self.pending_release || self.pending_kill {
return;
}
2025-06-28 15:45:35 -05:00
if self.released {
tracing::info!("🧙 magic chord - restricting devices 🪄 IMPERIUS!!! 🎮🔒");
} else {
tracing::info!("🧙 magic chord - freeing devices 🪄 EXPELLIARMUS!!! 🔓🕊️");
}
if self.released {
// switching to remote control
for k in &mut self.keyboards {
k.reset_state();
k.set_send(true);
k.set_grab(true);
}
for m in &mut self.mice {
m.reset_state();
m.set_send(true);
m.set_grab(true);
}
self.released = false;
self.pending_release = false;
} else {
// switching to local control: stop sending, keep grab until chord released
for k in &mut self.keyboards {
k.send_empty_report();
k.set_send(false);
}
for m in &mut self.mice {
m.reset_state();
m.set_send(false);
}
self.pending_release = true;
self.capture_pending_keys();
}
}
fn capture_pending_keys(&mut self) {
self.pending_keys.clear();
for k in &self.keyboards {
for key in k.pressed_keys_snapshot() {
self.pending_keys.insert(key);
}
}
2025-06-28 15:45:35 -05:00
}
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 (relative)
2025-06-11 00:37:01 -05:00
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);
2025-06-11 00:37:01 -05:00
if has_xy && has_btn {
2025-06-08 22:24:14 -05:00
return DeviceKind::Mouse;
}
}
}
// Touchpad logic (absolute)
if evbits.contains(EventType::ABSOLUTE) {
if let (Some(abs), Some(keys)) = (dev.supported_absolute_axes(), dev.supported_keys()) {
let has_xy = (abs.contains(AbsoluteAxisCode::ABS_X)
&& abs.contains(AbsoluteAxisCode::ABS_Y))
|| (abs.contains(AbsoluteAxisCode::ABS_MT_POSITION_X)
&& abs.contains(AbsoluteAxisCode::ABS_MT_POSITION_Y));
let has_btn = keys.contains(KeyCode::BTN_TOUCH) || keys.contains(KeyCode::BTN_LEFT);
if has_xy && has_btn {
return DeviceKind::Mouse;
}
}
}
2025-06-08 22:24:14 -05:00
DeviceKind::Other
}
/// Internal enum for device classification
#[derive(Debug, Clone, Copy)]
enum DeviceKind {
Keyboard,
Mouse,
Other,
}