summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWez Furlong <wez@wezfurlong.org>2024-06-09 06:04:45 -0700
committerWez Furlong <wez@wezfurlong.org>2024-06-09 06:07:53 -0700
commit9b18364dafd4d35095a9019ed37394ede449e7dc (patch)
treede51b851a4d125e52658ba8993cb0e50aaee6508
parenta0e636635f0d8feb23ee377be7987ab34edec35e (diff)
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
-rw-r--r--src/main.rs38
-rw-r--r--src/mapping.rs6
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<String>,
+
+ /// Override the phys device specified by the config file
+ #[arg(long)]
+ phys: Option<String>,
},
}
@@ -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<String>,
pub phys: Option<String>,
pub mappings: Vec<Mapping>,
}
@@ -114,7 +114,9 @@ impl Into<Mapping> for RemapConfig {
#[derive(Debug, Deserialize)]
struct ConfigFile {
- device_name: String,
+ #[serde(default)]
+ device_name: Option<String>,
+
#[serde(default)]
phys: Option<String>,