52 lines
1.3 KiB
Rust
52 lines
1.3 KiB
Rust
|
|
// client/src/input/mouse.rs
|
||
|
|
|
||
|
|
use evdev::{Device, InputEvent, EventType, RelativeAxisType};
|
||
|
|
|
||
|
|
/// 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) {
|
||
|
|
match self.dev.fetch_events() {
|
||
|
|
Ok(events) => {
|
||
|
|
for ev in events {
|
||
|
|
self.handle_event(ev);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {}
|
||
|
|
Err(e) => {
|
||
|
|
tracing::error!("Mouse device read error: {e}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn handle_event(&mut self, ev: InputEvent) {
|
||
|
|
if ev.event_type() == EventType::REL {
|
||
|
|
match ev.code() {
|
||
|
|
x if x == RelativeAxisType::REL_X.0 => {
|
||
|
|
self.dx += ev.value();
|
||
|
|
}
|
||
|
|
y if y == RelativeAxisType::REL_Y.0 => {
|
||
|
|
self.dy += ev.value();
|
||
|
|
}
|
||
|
|
_ => {}
|
||
|
|
}
|
||
|
|
tracing::debug!("mouse dx={} dy={}", self.dx, self.dy);
|
||
|
|
}
|
||
|
|
// etc. Also handle buttons with KEY_B?
|
||
|
|
}
|
||
|
|
}
|