From d31226fe69908f6c03260cc3c10fe9d3fce283f1 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 9 Jun 2024 06:36:40 -0700 Subject: Add debug-events subcommand This is helpful for figuring out which buttons do what ```console $ evremap debug-events --device-name 'DYGMA DEFY Keyboard' [2024-06-09T13:35:52Z INFO evremap] KEY_H 1 [2024-06-09T13:35:52Z INFO evremap] KEY_H 0 [2024-06-09T13:35:52Z INFO evremap] KEY_E 1 [2024-06-09T13:35:52Z INFO evremap] KEY_E 0 [2024-06-09T13:35:52Z INFO evremap] KEY_L 1 [2024-06-09T13:35:53Z INFO evremap] KEY_L 0 [2024-06-09T13:35:53Z INFO evremap] KEY_L 1 [2024-06-09T13:35:53Z INFO evremap] KEY_L 0 [2024-06-09T13:35:53Z INFO evremap] KEY_O 1 [2024-06-09T13:35:53Z INFO evremap] KEY_O 0 [2024-06-09T13:35:57Z INFO evremap] KEY_LEFTCTRL 1 [2024-06-09T13:35:57Z INFO evremap] KEY_C 1 [2024-06-09T13:35:57Z INFO evremap] KEY_C 0 ``` Thanks to @innovate-invent. closes: #58 closes: #7 closes: #40 --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index d578185..473e608 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,19 @@ enum Opt { /// Show a list of possible KEY_XXX values ListKeys, + /// Listen to events and print them out to facilitate learning + /// which keys/buttons have which labels for your device(s) + DebugEvents { + /// Specify the device name of interest + #[arg(long)] + device_name: String, + + /// Specify the phys device in case multiple devices have + /// the same name + #[arg(long)] + phys: Option, + }, + /// Load a remapper config and run the remapper. /// This usually requires running as root to obtain exclusive access /// to the input devices. @@ -107,6 +120,30 @@ fn get_device( } } +fn debug_events(device: DeviceInfo) -> Result<()> { + let f = + std::fs::File::open(&device.path).context(format!("opening {}", device.path.display()))?; + let input = evdev_rs::Device::new_from_file(f).with_context(|| { + format!( + "failed to create new Device from file {}", + device.path.display() + ) + })?; + + loop { + let (status, event) = + input.next_event(evdev_rs::ReadFlag::NORMAL | evdev_rs::ReadFlag::BLOCKING)?; + match status { + evdev_rs::ReadStatus::Success => { + if let EventCode::EV_KEY(key) = event.event_code { + log::info!("{key:?} {}", event.value); + } + } + evdev_rs::ReadStatus::Sync => anyhow::bail!("ReadStatus::Sync!"), + } + } +} + fn main() -> Result<()> { setup_logger(); let opt = Opt::parse(); @@ -114,6 +151,10 @@ fn main() -> Result<()> { match opt { Opt::ListDevices => deviceinfo::list_devices(), Opt::ListKeys => list_keys(), + Opt::DebugEvents { device_name, phys } => { + let device_info = get_device(&device_name, phys.as_deref(), false)?; + debug_events(device_info) + } Opt::Remap { config_file, delay, @@ -144,11 +185,8 @@ fn main() -> Result<()> { log::warn!("Short delay: release any keys now!"); std::thread::sleep(Duration::from_secs_f64(delay)); - let device_info = get_device( - device_name, - mapping_config.phys.as_deref(), - wait_for_device, - )?; + let device_info = + get_device(device_name, mapping_config.phys.as_deref(), wait_for_device)?; let mut mapper = InputMapper::create_mapper(device_info.path, mapping_config.mappings)?; mapper.run_mapper() -- cgit v1.2.3