evdev-rs/examples/_pick_device.rs

29 lines
1,008 B
Rust
Raw Normal View History

//! An arg parser/prompt shared between the `evtest*` examples. Also demonstrates opening/finding
//! connected devices.
use std::io::prelude::*;
pub fn pick_device() -> evdev::Device {
let mut args = std::env::args_os();
args.next();
if let Some(dev_file) = args.next() {
evdev::Device::open(dev_file).unwrap()
} else {
let mut devices = evdev::enumerate().collect::<Vec<_>>();
// readdir returns them in reverse order from their eventN names for some reason
devices.reverse();
for (i, d) in devices.iter().enumerate() {
println!("{}: {}", i, d.name().unwrap_or("Unnamed device"));
}
print!("Select the device [0-{}]: ", devices.len());
let _ = std::io::stdout().flush();
let mut chosen = String::new();
std::io::stdin().read_line(&mut chosen).unwrap();
let n = chosen.trim().parse::<usize>().unwrap();
devices.into_iter().nth(n).unwrap()
}
}
#[allow(dead_code)]
fn main() {}