From f667a339eb81a0b86e7d496f988fa8bc404c4e35 Mon Sep 17 00:00:00 2001 From: Lahav T Date: Tue, 21 Nov 2023 23:09:36 +0200 Subject: [PATCH] Add an env var for wayland output to grab input Add a new environment variable WLR_WL_GRAB_INPUT_SHORTCUT that lets you specify a keyboard shortcut that when pressed, will disable the keyboard shortcuts and confine the pointer by using the function wlr_wl_backend_set_grab_input_shortcut. --- backend/backend.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ docs/env_vars.md | 1 + 2 files changed, 66 insertions(+) diff --git a/backend/backend.c b/backend/backend.c index e5b8db149..09c7be898 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -13,6 +15,7 @@ #include #include #include +#include #include "backend/backend.h" #include "backend/multi.h" #include "render/allocator/allocator.h" @@ -147,6 +150,37 @@ static size_t parse_outputs_env(const char *name) { return outputs; } +// This code was copied from sway wm source code, +// from https://github.com/swaywm/sway/blob/7cf4e1d/sway/input/keyboard.c +// ____________________________________________________ +static struct modifier_key { + char *name; + uint32_t mod; +} modifiers[] = { + { XKB_MOD_NAME_SHIFT, WLR_MODIFIER_SHIFT }, + { XKB_MOD_NAME_CAPS , WLR_MODIFIER_CAPS }, + { XKB_MOD_NAME_CTRL , WLR_MODIFIER_CTRL }, + { "Ctrl" , WLR_MODIFIER_CTRL }, + { XKB_MOD_NAME_ALT , WLR_MODIFIER_ALT }, + { "Alt" , WLR_MODIFIER_ALT }, + { XKB_MOD_NAME_NUM , WLR_MODIFIER_MOD2 }, + { "Mod3" , WLR_MODIFIER_MOD3 }, + { XKB_MOD_NAME_LOGO , WLR_MODIFIER_LOGO }, + { "Mod5" , WLR_MODIFIER_MOD5 }, +}; + +static uint32_t get_modifier_mask_by_name(const char *name) { + int i; + for (i = 0; i < (int)(sizeof(modifiers) / sizeof(struct modifier_key)); ++i) { + if (strcasecmp(modifiers[i].name, name) == 0) { + return modifiers[i].mod; + } + } + + return 0; +} +// ____________________________________________________) + static struct wlr_backend *attempt_wl_backend(struct wl_display *display) { struct wlr_backend *backend = wlr_wl_backend_create(display, NULL); if (backend == NULL) { @@ -158,6 +192,37 @@ static struct wlr_backend *attempt_wl_backend(struct wl_display *display) { wlr_wl_output_create(backend); } + char* keyboard_shortcut = getenv("WLR_WL_GRAB_INPUT_SHORTCUT"); + if (keyboard_shortcut) { + wlr_log(WLR_INFO, "Loading user-specified input grab keyboard shortcut: %s", + keyboard_shortcut); + xkb_keysym_t input_grab_keysym = 0; + uint32_t input_grab_modifier_mask = 0; + keyboard_shortcut = strdup(keyboard_shortcut); + + char *saveptr; + char *key_name = strtok_r(keyboard_shortcut, "+", &saveptr); + while (key_name != NULL) { + + uint32_t modifier_mask = get_modifier_mask_by_name(key_name); + input_grab_modifier_mask = input_grab_modifier_mask | modifier_mask; + if(modifier_mask == 0) { + input_grab_keysym = xkb_keysym_from_name(key_name,XKB_KEYSYM_CASE_INSENSITIVE); + if(input_grab_keysym == 0) { + wlr_log(WLR_ERROR, + "The key shortcut contains an unrecognized key name. ignoring key shortcut"); + input_grab_keysym = 0; + input_grab_modifier_mask = 0; + free(keyboard_shortcut); + return backend; + } + } + key_name = strtok_r(NULL, "+", &saveptr); + } + free(keyboard_shortcut); + wlr_wl_backend_set_grab_input_shortcut(backend, input_grab_modifier_mask, input_grab_keysym); + } + return backend; } diff --git a/docs/env_vars.md b/docs/env_vars.md index e36cdc781..644f38956 100644 --- a/docs/env_vars.md +++ b/docs/env_vars.md @@ -39,6 +39,7 @@ wlroots reads these environment variables ## Wayland backend * *WLR_WL_OUTPUTS*: when using the wayland backend specifies the number of outputs +* *WLR_WL_GRAB_INPUT_SHORTCUT*: when using the wayland backend, specify the shortcut (mods+key) to toggle input grab of the focused wayland output. Examples: control+shift+alt, control_r, alt_l. ## X11 backend