Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modifiers config option #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ input = ["KEY_F8"]
output = ["KEY_MUTE"]
```

If you want to override the default modifiers (KEY_LEFTALT, KEY_LEFTCTRL, etc.)
you can list all the modifier keys via the `modifiers = []` config option.

```toml
modifiers = [
"KEY_FN",
"KEY_LEFTALT",
"KEY_RIGHTALT",
"KEY_LEFTMETA",
"KEY_RIGHTMETA",
"KEY_LEFTCTRL",
"KEY_RIGHTCTRL",
"KEY_LEFTSHIFT",
"KEY_RIGHTSHIFT",
]
```

* How do I list available input devices?
`sudo evremap list-devices`

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn main() -> Result<()> {
mapping_config.phys.as_deref(),
)?;

let mut mapper = InputMapper::create_mapper(device_info.path, mapping_config.mappings)?;
let mut mapper = InputMapper::create_mapper(device_info.path, mapping_config.mappings, mapping_config.modifiers)?;
mapper.run_mapper()
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct MappingConfig {
pub device_name: String,
pub phys: Option<String>,
pub mappings: Vec<Mapping>,
pub modifiers: HashSet<KeyCode>
}

impl MappingConfig {
Expand All @@ -26,10 +27,35 @@ impl MappingConfig {
for remap in config_file.remap {
mappings.push(remap.into());
}
let modifiers;
if config_file.modifiers.is_empty() {
modifiers = HashSet::from_iter([
KeyCode::KEY_FN,
KeyCode::KEY_LEFTALT,
KeyCode::KEY_RIGHTALT,
KeyCode::KEY_LEFTMETA,
KeyCode::KEY_RIGHTMETA,
KeyCode::KEY_LEFTCTRL,
KeyCode::KEY_RIGHTCTRL,
KeyCode::KEY_LEFTSHIFT,
KeyCode::KEY_RIGHTSHIFT,
]);
} else {
modifiers = config_file.modifiers.into_iter().map(|name|
match EventCode::from_str(&EventType::EV_KEY, &name) {
Some(code) => match code {
EventCode::EV_KEY(code) => code,
_ => panic!("{}", ConfigError::ImpossibleParseKey),
},
None => panic!("{}", ConfigError::InvalidKey(name.to_string())),
}
).collect()
}
Ok(Self {
device_name: config_file.device_name,
phys: config_file.phys,
mappings,
modifiers,
})
}
}
Expand Down Expand Up @@ -123,4 +149,7 @@ struct ConfigFile {

#[serde(default)]
remap: Vec<RemapConfig>,

#[serde(default)]
modifiers: Vec<String>,
}
67 changes: 29 additions & 38 deletions src/remapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct InputMapper {
tapping: Option<KeyCode>,

output_keys: HashSet<KeyCode>,
modifiers: HashSet<KeyCode>,
}

fn enable_key_code(input: &mut Device, key: KeyCode) -> Result<()> {
Expand All @@ -71,7 +72,7 @@ fn enable_key_code(input: &mut Device, key: KeyCode) -> Result<()> {
}

impl InputMapper {
pub fn create_mapper<P: AsRef<Path>>(path: P, mappings: Vec<Mapping>) -> Result<Self> {
pub fn create_mapper<P: AsRef<Path>>(path: P, mappings: Vec<Mapping>, modifiers: HashSet<KeyCode>) -> Result<Self> {
let path = path.as_ref();
let f = std::fs::File::open(path).context(format!("opening {}", path.display()))?;
let mut input = Device::new_from_file(f)
Expand Down Expand Up @@ -112,6 +113,7 @@ impl InputMapper {
output_keys: HashSet::new(),
tapping: None,
mappings,
modifiers,
})
}

Expand Down Expand Up @@ -162,15 +164,15 @@ impl InputMapper {
if input.is_subset(&keys_minus_remapped) {
for i in input {
keys.remove(i);
if !is_modifier(i) {
if !self.is_modifier(i) {
keys_minus_remapped.remove(i);
}
}
for o in output {
keys.insert(o.clone());
// Outputs that apply are not visible as
// inputs for later remap rules
if !is_modifier(o) {
if !self.is_modifier(o) {
keys_minus_remapped.remove(o);
}
}
Expand Down Expand Up @@ -207,11 +209,11 @@ impl InputMapper {
.collect();

if !to_release.is_empty() {
to_release.sort_by(modifiers_last);
to_release.sort_by(|a,b| self.modifiers_last(a,b));
self.emit_keys(&to_release, time, KeyEventType::Release)?;
}
if !to_press.is_empty() {
to_press.sort_by(modifiers_first);
to_press.sort_by(|a,b| self.modifiers_first(a,b));
self.emit_keys(&to_press, time, KeyEventType::Press)?;
}
Ok(())
Expand Down Expand Up @@ -392,45 +394,34 @@ impl InputMapper {
))?;
Ok(())
}
}

fn make_event(key: KeyCode, time: &TimeVal, event_type: KeyEventType) -> InputEvent {
InputEvent::new(time, &EventCode::EV_KEY(key), event_type.value())
}

fn is_modifier(key: &KeyCode) -> bool {
match key {
KeyCode::KEY_FN
| KeyCode::KEY_LEFTALT
| KeyCode::KEY_RIGHTALT
| KeyCode::KEY_LEFTMETA
| KeyCode::KEY_RIGHTMETA
| KeyCode::KEY_LEFTCTRL
| KeyCode::KEY_RIGHTCTRL
| KeyCode::KEY_LEFTSHIFT
| KeyCode::KEY_RIGHTSHIFT => true,
_ => false,
fn is_modifier(&self, key: &KeyCode) -> bool {
self.modifiers.contains(key)
}
}

/// Orders modifier keys ahead of non-modifier keys.
/// Unfortunately the underlying type doesn't allow direct
/// comparison, but that's ok for our purposes.
fn modifiers_first(a: &KeyCode, b: &KeyCode) -> Ordering {
if is_modifier(a) {
if is_modifier(b) {
Ordering::Equal
/// Orders modifier keys ahead of non-modifier keys.
/// Unfortunately the underlying type doesn't allow direct
/// comparison, but that's ok for our purposes.
fn modifiers_first(&self, a: &KeyCode, b: &KeyCode) -> Ordering {
if self.is_modifier(a) {
if self.is_modifier(b) {
Ordering::Equal
} else {
Ordering::Less
}
} else if self.is_modifier(b) {
Ordering::Greater
} else {
Ordering::Less
// Neither are modifiers
Ordering::Equal
}
} else if is_modifier(b) {
Ordering::Greater
} else {
// Neither are modifiers
Ordering::Equal
}

fn modifiers_last(&self, a: &KeyCode, b: &KeyCode) -> Ordering {
self.modifiers_first(a, b).reverse()
}
}

fn modifiers_last(a: &KeyCode, b: &KeyCode) -> Ordering {
modifiers_first(a, b).reverse()
fn make_event(key: KeyCode, time: &TimeVal, event_type: KeyEventType) -> InputEvent {
InputEvent::new(time, &EventCode::EV_KEY(key), event_type.value())
}