diff --git a/src/bin/evinfo.rs b/src/bin/evinfo.rs index da52482..4acd9ab 100644 --- a/src/bin/evinfo.rs +++ b/src/bin/evinfo.rs @@ -14,15 +14,15 @@ struct Args { fn main() { let args = Args::parse(); let devices = evdev::raw_stream::enumerate(); - devices.for_each(|(path, dev)| { + + for (path, dev) in devices { if is_joystick_like(&dev) { print_device(path, dev, args.verbose) } - }); + } } -// Todo: can use a macro here... -const JOYSTICK_BUTTONS: Vec { +const JOYSTICK_BUTTONS: &[evdev::KeyCode] = &[ evdev::KeyCode::BTN_TRIGGER_HAPPY1, evdev::KeyCode::BTN_TRIGGER_HAPPY2, evdev::KeyCode::BTN_TRIGGER_HAPPY3, @@ -33,11 +33,21 @@ const JOYSTICK_BUTTONS: Vec { evdev::KeyCode::BTN_TRIGGER_HAPPY8, evdev::KeyCode::BTN_TRIGGER_HAPPY9, evdev::KeyCode::BTN_TRIGGER_HAPPY10, -} + evdev::KeyCode::BTN_TRIGGER_HAPPY11, +]; fn is_joystick_like(device: &RawDevice) -> bool { + if let Some(_) = device.supported_absolute_axes() { + return true; + } - if device.supported_absolute_axes().map_or(false, |axes| axes.contains()); + if let Some(keys) = device.supported_keys() { + for key in keys.iter() { + if JOYSTICK_BUTTONS.contains(&key) { + return true; + } + } + } return false; } @@ -45,23 +55,28 @@ fn is_joystick_like(device: &RawDevice) -> bool { fn print_device(path: PathBuf, device: RawDevice, verbose: u8) { println!( "{}: \"{}\"", - path.to_str().unwrap_or_default(), - device.name().unwrap_or_default() + path.to_str().unwrap_or("unknown_device_path"), + device.name().unwrap_or("unknown_device_name") ); if verbose > 0 { let input_id = device.input_id(); - println!("\tUUID:\t\t'{}'", device.unique_name().unwrap_or_default()); - println!("\tVendor:\t\t'{:x}'", input_id.vendor()); - println!("\tProduct:\t'{:x}'", input_id.product()); + println!("\tUUID:\t\t'{}'", device.unique_name().unwrap_or("n/a")); + println!("\tVendor:\t\t'0x{:x}'", input_id.vendor()); + println!("\tProduct:\t'0x{:x}'", input_id.product()); println!("\tVersion:\t'{}'", input_id.version()); } - // if verbose > 1 { - // let absInfo = device.get_absinfo()?; - // if absInfo.count() > 0 { - // println!("\tAxis Data:"); - // absInfo.for_each(|info| println!("\t\t{} {}")); - // } - // } + if verbose > 1 { + if let Ok(abs_info) = device.get_absinfo() { + if abs_info.count() > 0 { + println!("\tAxis Data:"); + abs_info.for_each(|info| println!("\t\t{} {}")); + } + } + } + + if verbose > 0 { + println!(); + } }