From 9b18364dafd4d35095a9019ed37394ede449e7dc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 9 Jun 2024 06:04:45 -0700 Subject: device_name can now be overridden by cli arguments The device name is now optional in the config, and a `--device-name` CLI parameter allows specifying the device name that will be used. This allows using the same configuration file with multiple devices, provided that the devices are known up front. Note that you will need to spawn one evremap process per device to remap multiple devices at the same time. Thanks to @innovate-invent for making a first pass at this: closes: https://github.com/wez/evremap/pull/59 refs: https://github.com/wez/evremap/issues/17 --- src/main.rs | 38 ++++++++++++++++++++++++++++++++------ src/mapping.rs | 6 ++++-- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1018aca..5828a37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,14 @@ enum Opt { /// Number of seconds for user to release keys on startup #[arg(short, long, default_value = "2")] delay: f64, + + /// Override the device name specified by the config file + #[arg(long)] + device_name: Option, + + /// Override the phys device specified by the config file + #[arg(long)] + phys: Option, }, } @@ -67,19 +75,37 @@ fn main() -> Result<()> { match opt { Opt::ListDevices => deviceinfo::list_devices(), Opt::ListKeys => list_keys(), - Opt::Remap { config_file, delay } => { - let mapping_config = MappingConfig::from_file(&config_file).context(format!( + Opt::Remap { + config_file, + delay, + device_name, + phys, + } => { + let mut mapping_config = MappingConfig::from_file(&config_file).context(format!( "loading MappingConfig from {}", config_file.display() ))?; + if let Some(device) = device_name { + mapping_config.device_name = Some(device); + } + if let Some(phys) = phys { + mapping_config.phys = Some(phys); + } + + let device_name = mapping_config.device_name.as_deref().ok_or_else(|| { + anyhow::anyhow!( + "device_name is missing; \ + specify it either in the config file or via the --device-name \ + command line option" + ) + })?; + log::warn!("Short delay: release any keys now!"); std::thread::sleep(Duration::from_secs_f64(delay)); - let device_info = deviceinfo::DeviceInfo::with_name( - &mapping_config.device_name, - mapping_config.phys.as_deref(), - )?; + let device_info = + deviceinfo::DeviceInfo::with_name(device_name, mapping_config.phys.as_deref())?; let mut mapper = InputMapper::create_mapper(device_info.path, mapping_config.mappings)?; mapper.run_mapper() diff --git a/src/mapping.rs b/src/mapping.rs index 6ffc274..0574273 100644 --- a/src/mapping.rs +++ b/src/mapping.rs @@ -7,7 +7,7 @@ use thiserror::Error; #[derive(Debug, Clone)] pub struct MappingConfig { - pub device_name: String, + pub device_name: Option, pub phys: Option, pub mappings: Vec, } @@ -114,7 +114,9 @@ impl Into for RemapConfig { #[derive(Debug, Deserialize)] struct ConfigFile { - device_name: String, + #[serde(default)] + device_name: Option, + #[serde(default)] phys: Option, -- cgit v1.2.3