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

@ -20,6 +20,7 @@ module;
#include <wayland-client-protocol.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h>
#endif
@ -656,26 +657,67 @@ void Device::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t time, s
}
void Device::keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
namespace {
// wl_keyboard's active modifiers and group, as the compositor last sent
// them. The protocol 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. So a state made for a new keymap starts from
// them, not from none (#167).
struct KeyboardModifiers {
std::uint32_t depressed = 0;
std::uint32_t latched = 0;
std::uint32_t locked = 0;
std::uint32_t group = 0;
};
KeyboardModifiers keyboardModifiers;
void ApplyKeyboardModifiers(xkb_state* state) {
xkb_state_update_mask(state, keyboardModifiers.depressed, keyboardModifiers.latched,
keyboardModifiers.locked, 0, 0, keyboardModifiers.group);
}
}
// The keymap a key's text comes from. One that loads replaces the keymap and
// state there were, and frees them. One that does not load, such as one of
// another format than xkb_v1 (no_keymap), one that cannot be mapped or one
// this libxkbcommon cannot compile, is ignored and leaves the ones there
// were, or none: keys then still fire onRawKeyDown and onRawKeyUp with the
// kernel's codes, but bring no text (#167).
void Device::keyboard_keymap(void*, wl_keyboard*, uint32_t format, int fd, uint32_t size) {
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
fprintf(stderr, "Unsupported keymap format\n");
std::println(stderr, "Crafter.Graphics: the compositor's keymap has format {}, not xkb_v1; it is ignored", format);
return;
}
void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("mmap");
return;
}
xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
xkb_keymap = xkb_keymap_new_from_string(xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(map, size);
void* map = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
const int mapError = errno;
close(fd);
if (map == MAP_FAILED) {
std::println(stderr, "Crafter.Graphics: the compositor's keymap could not be mapped ({}); it is ignored", std::strerror(mapError));
return;
}
xkb_state = xkb_state_new(xkb_keymap);
// One context for every keymap, made with the first; xkb_context_new
// returns null when it fails.
if (xkb_context == nullptr) xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
::xkb_keymap* keymap = nullptr;
if (xkb_context != nullptr) {
keymap = xkb_keymap_new_from_string(xkb_context, static_cast<const char*>(map), XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
}
munmap(map, size);
::xkb_state* state = keymap != nullptr ? xkb_state_new(keymap) : nullptr;
if (state == nullptr) {
xkb_keymap_unref(keymap);
std::println(stderr, "Crafter.Graphics: the compositor's keymap did not load; it is ignored");
return;
}
ApplyKeyboardModifiers(state);
xkb_state_unref(xkb_state);
xkb_keymap_unref(xkb_keymap);
xkb_state = state;
xkb_keymap = keymap;
}
void Device::keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) {
@ -696,6 +738,10 @@ void Device::keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial,
keyboardWindow = nullptr;
keyRepeat.active = false;
keyRepeat.utf8.clear();
// A leave resets wl_keyboard's modifiers and group to zero; the
// compositor sends them again after the next enter.
keyboardModifiers = {};
if (xkb_state != nullptr) ApplyKeyboardModifiers(xkb_state);
}
void Device::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
@ -718,9 +764,11 @@ void Device::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, ui
keyboardWindow->onRawKeyDown.Invoke(code);
}
// The text comes from the keymap's state; without a keymap that
// loaded, the key brings its code alone.
std::string buf;
buf.resize(16);
int n = xkb_state_key_get_utf8(xkb_state, xkbKeycode, buf.data(), 16);
int n = xkb_state == nullptr ? 0 : xkb_state_key_get_utf8(xkb_state, xkbKeycode, buf.data(), 16);
std::string utf8;
if (n > 0 && (unsigned char)buf[0] >= 0x20 && buf[0] != 0x7f) {
buf.resize(n);
@ -751,7 +799,10 @@ void Device::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, ui
}
void Device::keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
xkb_state_update_mask(xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
keyboardModifiers = { mods_depressed, mods_latched, mods_locked, group };
// Without a keymap there is no state to set; a state made later starts
// from these.
if (xkb_state != nullptr) ApplyKeyboardModifiers(xkb_state);
}
void Device::keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) {