Skip to content

Commit

Permalink
--hook accepts a send-key clause.
Browse files Browse the repository at this point in the history
  • Loading branch information
KarsMulder committed Jan 5, 2022
1 parent b12c5d8 commit 8633a27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/arguments/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ pub(super) struct HookArg {
pub toggle_action: HookToggleAction,
pub withhold: bool,
pub period: Option<Duration>,
pub send_keys: Vec<Key>,
}

impl HookArg {
pub fn parse(args: Vec<String>) -> Result<HookArg, ArgumentError> {
let arg_group = ComplexArgGroup::parse(args,
&["toggle", "withhold"],
&["exec-shell", "toggle", "period"],
&["exec-shell", "toggle", "period", "send-key"],
false,
true,
)?;
Expand Down Expand Up @@ -58,12 +59,23 @@ impl HookArg {
}
};

// TODO: Enforce that this is EV_KEY.
let send_keys = KeyParser {
allow_transitions: false,
allow_values: false,
allow_ranges: false,
allow_types: false,
default_value: "",
allow_relative_values: false,
namespace: Namespace::User,
}.parse_all(&arg_group.get_clauses("send-key"))?;

if arg_group.keys.is_empty() {
Err(ArgumentError::new("A --hook argument requires at least one key."))
} else {
Ok(HookArg {
exec_shell: arg_group.get_clauses("exec-shell"),
hold_keys, toggle_action, withhold, period,
hold_keys, toggle_action, withhold, period, send_keys,
})
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/arguments/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,13 @@ pub fn implement(args_str: Vec<String>)
},
Argument::HookArg(hook_arg) => {
let mut hook = Hook::new(hook_arg.hold_keys, hook_arg.withhold, hook_arg.period);
for exec_shell in hook_arg.exec_shell.iter().cloned() {
for exec_shell in hook_arg.exec_shell {
hook.add_command("/bin/sh".to_owned(), vec!["-c".to_owned(), exec_shell]);
}

for send_key in hook_arg.send_keys {
hook.add_send_key(send_key)
}

for effect in hook_arg.toggle_action.implement(&state, &toggle_indices)? {
hook.add_effect(effect);
Expand Down
6 changes: 6 additions & 0 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ impl Hook {
})
);
}

/// Adds an effect: send a KEY_DOWN event of the provided key when the hook activates,
/// and a KEY_UP event of the provided key when the hook releases.
pub fn add_send_key(&mut self, key: Key) {
self.send_keys.push(key);
}
}

/// Returns the expiration time of a new event that would activate a tracker given
Expand Down

0 comments on commit 8633a27

Please sign in to comment.