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 --allow-other flag for keybindings #7602

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions include/sway/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum binding_flags {
BINDING_INHIBITED = 1 << 7, // keyboard only: ignore shortcut inhibitor
BINDING_NOREPEAT = 1 << 8, // keyboard only; do not trigger when repeating a held key
BINDING_EXACT = 1 << 9, // gesture only; only trigger on exact match
BINDING_ALLOWOTHER = 1 << 10, // keyboard only; allow other keys to be pressed at the same time
};

/**
Expand Down
2 changes: 2 additions & 0 deletions sway/commands/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
warn = false;
} else if (strcmp("--no-repeat", argv[0]) == 0) {
binding->flags |= BINDING_NOREPEAT;
} else if (strcmp("--allow-other", argv[0]) == 0) {
binding->flags |= BINDING_ALLOWOTHER;
} else {
break;
}
Expand Down
40 changes: 38 additions & 2 deletions sway/input/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ static void get_active_binding(const struct sway_shortcut_state *state,
bool binding_locked = (binding->flags & BINDING_LOCKED) != 0;
bool binding_inhibited = (binding->flags & BINDING_INHIBITED) != 0;
bool binding_release = binding->flags & BINDING_RELEASE;
bool binding_allowother = (binding->flags & BINDING_ALLOWOTHER) != 0;

if (modifiers ^ binding->modifiers ||
if ((binding_allowother ? (binding->modifiers & modifiers) : modifiers) ^ binding->modifiers ||
release != binding_release ||
locked > binding_locked ||
inhibited > binding_inhibited ||
Expand All @@ -175,7 +176,42 @@ static void get_active_binding(const struct sway_shortcut_state *state,
}

bool match = false;
if (state->npressed == (size_t)binding->keys->length) {
if (binding_allowother) {
/*
* Make sure all keys match, but also allow other keys to be pressed.
* In case of a press (as opposed to release), make sure at least one
* of the keys is the current key, otherwise the binding would be
* triggered twice. In case of release, the keys are considered released
* all at once so no check is necessary.
*/
bool one_key_is_current = false;

match = binding->keys->length != 0;

for (int j = 0; j < binding->keys->length; j++) {
bool key_match = false;
uint32_t key = *(uint32_t *)binding->keys->items[j];

for (size_t k = 0; k < state->npressed; k++) {
if (key == state->pressed_keys[k]) {
key_match = true;
break;
}
}

if (!key_match) {
match = false;
break;
}
if (key == state->current_key) {
one_key_is_current = true;
}
}

if (!release && !one_key_is_current) {
match = false;
}
} else if (state->npressed == (size_t)binding->keys->length) {
match = true;
for (size_t j = 0; j < state->npressed; j++) {
uint32_t key = *(uint32_t *)binding->keys->items[j];
Expand Down
9 changes: 7 additions & 2 deletions sway/sway.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ runtime.
for_window <criteria> move container to output <output>

*bindsym* [--whole-window] [--border] [--exclude-titlebar] [--release] [--locked] \
[--to-code] [--input-device=<device>] [--no-warn] [--no-repeat] [Group<1-4>+]<key combo> \
<command>
[--to-code] [--input-device=<device>] [--no-warn] [--no-repeat] [--allow-other] \
[Group<1-4>+]<key combo> <command>
Binds _key combo_ to execute the sway command _command_ when pressed. You
may use XKB key names here (*wev*(1) is a good tool for discovering these).
With the flag _--release_, the command is executed when the key combo is
Expand Down Expand Up @@ -419,6 +419,11 @@ runtime.
repeatedly when the key is held, according to the repeat
settings specified in the input configuration.

If _--allow-other_ is set, any key sequence containing this key sequence
may trigger this binding. For example, a single-key binding with
_--allow-other_ set may be executed upon the simultaneous press of one
or more keys, one of which is the binding's key.

Bindings to keysyms are layout-dependent. This can be changed with the
_--to-code_ flag. In this case, the keysyms will be translated into the
corresponding keycodes in the first configured layout.
Expand Down