Skip to main content

input/
input.rs

1//! Prints translated input events for a few seconds.
2//!
3//! Read-only and display-free: it touches no DRM device and sets no mode, so it is
4//! safe to run over SSH while looking at the console.
5//!
6//! ```text
7//! cargo run -p denise-evdev --example input -- [seconds]
8//! ```
9
10#[cfg(target_os = "linux")]
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    use std::time::{Duration, Instant};
13
14    use denise::{InputSource, Size};
15    use denise_evdev::InputBackend;
16
17    let seconds: u64 = std::env::args()
18        .nth(1)
19        .and_then(|a| a.parse().ok())
20        .unwrap_or(5)
21        .clamp(1, 120);
22
23    // Pretend a panel, so absolute devices have something to map onto.
24    let surface = Size::new(1280, 800);
25    let mut input = InputBackend::open_all(surface)?;
26
27    eprintln!("surface {}x{}", surface.width, surface.height);
28    for device in input.devices() {
29        let (ax, ay) = device.abs_ranges();
30        let calibration = match (ax, ay) {
31            (Some(x), Some(y)) => {
32                format!("  abs x {}..{}, y {}..{}", x.min, x.max, y.min, y.max)
33            }
34            _ => String::new(),
35        };
36        eprintln!(
37            "  {}: {} ({}){calibration}",
38            device.capabilities(),
39            device.name(),
40            device.path().display()
41        );
42    }
43    eprintln!("\nlistening for {seconds}s — move the mouse, type, click\n");
44
45    let deadline = Instant::now() + Duration::from_secs(seconds);
46    let mut events = Vec::new();
47    let mut total = 0usize;
48
49    while Instant::now() < deadline {
50        events.clear();
51        input.poll(&mut events);
52        for event in &events {
53            total += 1;
54            eprintln!("  {event:?}");
55        }
56        if events.is_empty() {
57            // A real loop waits on the descriptors instead. This is a probe.
58            std::thread::sleep(Duration::from_millis(4));
59        }
60    }
61
62    eprintln!("\n{total} events, pointer ended at {:?}", input.pointer());
63    Ok(())
64}
65
66#[cfg(not(target_os = "linux"))]
67fn main() {
68    eprintln!("denise-evdev only does anything on Linux");
69}