lesavka/client/src/input/mouse.rs

56 lines
1.4 KiB
Rust
Raw Normal View History

2025-06-08 22:24:14 -05:00
// client/src/input/mouse.rs
2025-06-11 00:37:01 -05:00
use evdev::{Device, InputEvent, EventType, RelativeAxisCode};
use tracing::error;
2025-06-08 22:24:14 -05:00
/// Aggregator for a single mouse device
pub struct MouseAggregator {
dev: Device,
dx: i32,
dy: i32,
// etc
}
impl MouseAggregator {
pub fn new(dev: Device) -> Self {
Self {
dev,
dx: 0,
dy: 0,
}
}
pub fn process_events(&mut self) {
2025-06-11 00:37:01 -05:00
let events_vec: Vec<InputEvent> = match self.dev.fetch_events() {
Ok(it) => it.collect(),
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
return;
2025-06-08 22:24:14 -05:00
}
Err(e) => {
2025-06-11 00:37:01 -05:00
error!("Mouse device read error: {e}");
return;
2025-06-08 22:24:14 -05:00
}
2025-06-11 00:37:01 -05:00
};
for ev in events_vec {
self.handle_event(ev);
2025-06-08 22:24:14 -05:00
}
}
fn handle_event(&mut self, ev: InputEvent) {
2025-06-11 00:37:01 -05:00
if ev.event_type() == EventType::RELATIVE {
match RelativeAxisCode(ev.code()) {
RelativeAxisCode::REL_X => {
2025-06-08 22:24:14 -05:00
self.dx += ev.value();
}
2025-06-11 00:37:01 -05:00
RelativeAxisCode::REL_Y => {
2025-06-08 22:24:14 -05:00
self.dy += ev.value();
}
_ => {}
}
tracing::debug!("mouse dx={} dy={}", self.dx, self.dy);
}
// etc. Also handle buttons with KEY_B?
}
}