From ecad76560e5ff80143e34d7163a1de6cf8008d56 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Sun, 8 Oct 2023 22:26:26 -0400 Subject: [PATCH] keyboard: add option to enable Num Lock automatically (default=enabled) Co-authored-by: @Consolatis --- docs/labwc-config.5.scd | 4 ++++ docs/rc.xml.all | 1 + include/config/rcxml.h | 1 + include/labwc.h | 1 + src/config/rcxml.c | 3 +++ src/keyboard.c | 28 ++++++++++++++++++++++++++++ src/seat.c | 8 ++++++++ 7 files changed, 46 insertions(+) diff --git a/docs/labwc-config.5.scd b/docs/labwc-config.5.scd index 87d0b352..7bc6cce4 100644 --- a/docs/labwc-config.5.scd +++ b/docs/labwc-config.5.scd @@ -276,6 +276,10 @@ this is for compatibility with Openbox. ## KEYBOARD +** [on|off] + When recognizing a new keyboard enable or disable Num Lock. + Default is on. + ** Define a *key* binding in the format *modifier-key*, where supported modifiers are: diff --git a/docs/rc.xml.all b/docs/rc.xml.all index c0665ee6..7a44ea39 100644 --- a/docs/rc.xml.all +++ b/docs/rc.xml.all @@ -141,6 +141,7 @@ your favourite terminal or application launcher. See rc.xml for an example. --> + on 25 600 diff --git a/include/config/rcxml.h b/include/config/rcxml.h index e15fcabf..b3ef127f 100644 --- a/include/config/rcxml.h +++ b/include/config/rcxml.h @@ -62,6 +62,7 @@ struct rcxml { /* keyboard */ int repeat_rate; int repeat_delay; + bool kb_numlock_enable; struct wl_list keybinds; /* struct keybind.link */ /* mouse */ diff --git a/include/labwc.h b/include/labwc.h index 84d39d4f..ae6867ed 100644 --- a/include/labwc.h +++ b/include/labwc.h @@ -419,6 +419,7 @@ void desktop_focus_topmost_view(struct server *server); void keyboard_cancel_keybind_repeat(struct keyboard *keyboard); void keyboard_key_notify(struct wl_listener *listener, void *data); void keyboard_modifiers_notify(struct wl_listener *listener, void *data); +void keyboard_set_numlock(struct wlr_keyboard *keyboard); void keyboard_init(struct seat *seat); bool keyboard_any_modifiers_pressed(struct wlr_keyboard *keyboard); void keyboard_finish(struct seat *seat); diff --git a/src/config/rcxml.c b/src/config/rcxml.c index ed0ae759..79ea79d8 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -732,6 +732,8 @@ entry(xmlNode *node, char *nodename, char *content) rc.repeat_rate = atoi(content); } else if (!strcasecmp(nodename, "repeatDelay.keyboard")) { rc.repeat_delay = atoi(content); + } else if (!strcasecmp(nodename, "numlock.keyboard")) { + set_bool(content, &rc.kb_numlock_enable); } else if (!strcasecmp(nodename, "screenEdgeStrength.resistance")) { rc.screen_edge_strength = atoi(content); } else if (!strcasecmp(nodename, "range.snapping")) { @@ -949,6 +951,7 @@ rcxml_init(void) rc.scroll_factor = 1.0; rc.repeat_rate = 25; rc.repeat_delay = 600; + rc.kb_numlock_enable = true; rc.screen_edge_strength = 20; rc.snap_edge_range = 1; diff --git a/src/keyboard.c b/src/keyboard.c index aba3c6c9..c55636b0 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "action.h" #include "idle.h" #include "key-state.h" @@ -489,6 +490,33 @@ keyboard_key_notify(struct wl_listener *listener, void *data) } } +void +keyboard_set_numlock(struct wlr_keyboard *keyboard) +{ + xkb_mod_index_t num_idx = + xkb_map_mod_get_index(keyboard->keymap, XKB_MOD_NAME_NUM); + if (num_idx == XKB_MOD_INVALID) { + wlr_log(WLR_INFO, "Failed to set Num Lock: not found in keymap"); + return; + } + + xkb_mod_mask_t locked = keyboard->modifiers.locked; + if (rc.kb_numlock_enable) { + locked |= (xkb_mod_mask_t)1 << num_idx; + } else { + locked &= ~((xkb_mod_mask_t)1 << num_idx); + } + + /* + * This updates the xkb-state + kb->modifiers and also triggers the + * keyboard->events.modifiers signal (the signal has no effect in + * current labwc usage since the keyboard is not part of a + * keyboard-group yet). + */ + wlr_keyboard_notify_modifiers(keyboard, keyboard->modifiers.depressed, + keyboard->modifiers.latched, locked, keyboard->modifiers.group); +} + void keyboard_init(struct seat *seat) { diff --git a/src/seat.c b/src/seat.c index bb6729a0..f36c532c 100644 --- a/src/seat.c +++ b/src/seat.c @@ -211,6 +211,14 @@ new_keyboard(struct seat *seat, struct wlr_input_device *device, bool virtual) wlr_keyboard_set_keymap(kb, seat->keyboard_group->keyboard.keymap); + /* + * This needs to be before wlr_keyboard_group_add_keyboard(). + * For some reason, wlroots takes the modifier state from the + * new keyboard and syncs it to the others in the group, rather + * than the other way around. + */ + keyboard_set_numlock(kb); + if (!virtual) { wlr_keyboard_group_add_keyboard(seat->keyboard_group, kb); } -- 2.52.0