Skip to content

Commit d31226f

Browse files
committed
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
1 parent 2d2b476 commit d31226f

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

src/main.rs

+43-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ enum Opt {
2222
/// Show a list of possible KEY_XXX values
2323
ListKeys,
2424

25+
/// Listen to events and print them out to facilitate learning
26+
/// which keys/buttons have which labels for your device(s)
27+
DebugEvents {
28+
/// Specify the device name of interest
29+
#[arg(long)]
30+
device_name: String,
31+
32+
/// Specify the phys device in case multiple devices have
33+
/// the same name
34+
#[arg(long)]
35+
phys: Option<String>,
36+
},
37+
2538
/// Load a remapper config and run the remapper.
2639
/// This usually requires running as root to obtain exclusive access
2740
/// to the input devices.
@@ -107,13 +120,41 @@ fn get_device(
107120
}
108121
}
109122

123+
fn debug_events(device: DeviceInfo) -> Result<()> {
124+
let f =
125+
std::fs::File::open(&device.path).context(format!("opening {}", device.path.display()))?;
126+
let input = evdev_rs::Device::new_from_file(f).with_context(|| {
127+
format!(
128+
"failed to create new Device from file {}",
129+
device.path.display()
130+
)
131+
})?;
132+
133+
loop {
134+
let (status, event) =
135+
input.next_event(evdev_rs::ReadFlag::NORMAL | evdev_rs::ReadFlag::BLOCKING)?;
136+
match status {
137+
evdev_rs::ReadStatus::Success => {
138+
if let EventCode::EV_KEY(key) = event.event_code {
139+
log::info!("{key:?} {}", event.value);
140+
}
141+
}
142+
evdev_rs::ReadStatus::Sync => anyhow::bail!("ReadStatus::Sync!"),
143+
}
144+
}
145+
}
146+
110147
fn main() -> Result<()> {
111148
setup_logger();
112149
let opt = Opt::parse();
113150

114151
match opt {
115152
Opt::ListDevices => deviceinfo::list_devices(),
116153
Opt::ListKeys => list_keys(),
154+
Opt::DebugEvents { device_name, phys } => {
155+
let device_info = get_device(&device_name, phys.as_deref(), false)?;
156+
debug_events(device_info)
157+
}
117158
Opt::Remap {
118159
config_file,
119160
delay,
@@ -144,11 +185,8 @@ fn main() -> Result<()> {
144185
log::warn!("Short delay: release any keys now!");
145186
std::thread::sleep(Duration::from_secs_f64(delay));
146187

147-
let device_info = get_device(
148-
device_name,
149-
mapping_config.phys.as_deref(),
150-
wait_for_device,
151-
)?;
188+
let device_info =
189+
get_device(device_name, mapping_config.phys.as_deref(), wait_for_device)?;
152190

153191
let mut mapper = InputMapper::create_mapper(device_info.path, mapping_config.mappings)?;
154192
mapper.run_mapper()

0 commit comments

Comments
 (0)