-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy path_pick_device.rs
28 lines (25 loc) · 1 KB
/
_pick_device.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#![allow(dead_code)]
//! An arg parser/prompt shared between the `evtest*` examples. Also demonstrates opening/finding
//! connected devices.
pub fn pick_device() -> evdev::Device {
use std::io::prelude::*;
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().map(|t| t.1).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()
}
}
fn main() {}