fix(wayland): keys come without text while no keymap has loaded, and a keymap that loads frees the one before

Device::keyboard_keymap made no state for a keymap of another format than
xkb_v1 or one it could not map: after such a first keymap the state
stayed null, and keyboard_modifiers and keyboard_key passed it unchecked
to libxkbcommon. For a keymap that did not compile, first or later,
keyboard_keymap called xkb_state_new on the null keymap, which crashed
(SIGSEGV) in the handler itself. Each keymap that loaded also made a
context, a keymap and a state of its own, and released none of the
earlier ones (#167).

Device, keyboard_keymap: a keymap that does not load is ignored, which
leaves the keymap and state there were. #167 has the earlier ones go
once new ones exist, and the handler already left them for a keymap of
another format or one it could not map; a context or a state that
libxkbcommon fails to make is ignored the same way. A keymap that loads
replaces the keymap and state and releases the earlier ones. Every
keymap is compiled in one context, made with the first that is mapped:
Device::xkb_context no longer gets one from a static initializer, and
is null before that.

Device, keyboard_key and keyboard_modifiers: without a state the key
brings no text and the modifiers are only kept, so a key still fires
onRawKeyDown and onRawKeyUp with the kernel's code, and only its
onTextInput is lost.

Device, the modifiers: a new state is set from the modifiers and group
the compositor last sent. wayland.xml (1.26) keeps them in wl_keyboard's
logical state, which the keymap is no part of: the modifiers event sets
them and a leave resets them to zero. A state made for a keymap that
came without a modifiers event started from none, so a Caps Lock that
was on stopped typing capitals until the next modifiers event.
keyboard_modifiers now keeps the values it was sent, keyboard_keymap sets
a new state from them, and keyboard_leave zeroes them and sets the state
from those zeros.

Device, the statics: Device::xkb_keymap and xkb_state are freed when the
next keymap loads, where the earlier ones leaked, so an application that
reads them reads them at each use. Their comment and AGENTS.consumer.md
say so, and when they are null.

Tests: KeyboardKeymap (new) drives the wl_keyboard callbacks directly,
with keymaps in memfds, on a default-constructed Window, each case in a
child process. A first keymap of the no_keymap format, one whose fd
cannot be mapped (a pipe's) and one that does not compile each have
their fd closed and make no state, and a key after modifiers then fires
onRawKeyDown and onRawKeyUp without text. After a keymap that loaded,
each of the three leaves it in place, until one loads. A second keymap
replaces the first in the same context, and on glibc 100 more leave less
than 16 KiB more on the heap. Caps Lock carries into the state of a
keymap that comes without modifiers, a leave clears it from the state
and a state made after the leave has none, and modifiers sent before the
first keymap apply to it. Run against the handlers before this change,
with libxkbcommon 1.13.2, all seven cases fail: five die of signal 11
(SIGSEGV), the 100 keymaps leave 769056 bytes more on the heap and are
not compiled in the first one's context, and the Caps Lock is kept
through the leave and lost to the next keymap.

Docs: the consumer notes say what a key brings without a keymap, that a
keymap that does not load is ignored, and when Device::xkb_keymap and
xkb_state are null and when they are freed. AGENTS.template.md names
KeyboardKeymap among the tests that drive the Wayland listeners
directly.
This commit is contained in:
Jorijn van der Graaf 2026-11-26 09:25:11 +01:00
commit 148ed01952
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 407 additions and 16 deletions

View file

@ -98,7 +98,6 @@ export namespace Crafter {
// request on a null proxy crashes in libwayland, so check first.
inline static wp_fractional_scale_manager_v1* fractionalScaleManager = nullptr;
inline static zxdg_decoration_manager_v1* manager = nullptr;
inline static xkb_keymap* xkb_keymap;
inline static wl_compositor* compositor = nullptr;
inline static Window* focusedWindow = nullptr;
// The window with keyboard focus (wl_keyboard.enter/leave), apart from
@ -108,8 +107,18 @@ export namespace Crafter {
inline static Window* keyboardWindow = nullptr;
inline static wl_shm* shm = nullptr;
inline static wp_viewporter* wpViewporter = nullptr;
inline static xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
inline static xkb_state* xkb_state;
// The keyboard's XKB keymap, from the compositor's wl_keyboard.keymap,
// and the state made from it, for an application that reads keysyms
// or modifiers itself. Null until a keymap loads. One that does not
// load, such as one of another format than xkb_v1, one that cannot be
// mapped or one this libxkbcommon cannot compile, is ignored and
// leaves the ones there were; one that loads frees them, so read these
// at each use rather than keep them. Keys need neither: their codes
// are the kernel's, and only onTextInput's text comes from the state.
inline static xkb_keymap* xkb_keymap = nullptr;
inline static xkb_state* xkb_state = nullptr;
// The one context every keymap is compiled in, made with the first.
inline static xkb_context* xkb_context = nullptr;
inline static std::vector<Window*> windows;
inline static wl_pointer* wlPointer;
inline static wl_pointer* seatPointer = nullptr;