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

@ -72,7 +72,7 @@ Tests are declared in `project.cpp`, not discovered, through the `LibraryTest(na
A test is an `int main()`: exit 0 passes, nonzero fails, 77 skips (for a missing capability, never for a failed check). Each runs in its own process, in parallel, with a 60 s default timeout, and the output of a failed, crashed or timed-out test is in `build/test-logs/<name>.log`; a later pass does not delete the log, so check its timestamp. What the tests need, in three groups:
- Pure logic, no GPU: they stage `Device`'s statics or feed synthetic data to the decision functions that were factored out for them (`Device::ResolveFeatures`, `ChooseDevice`, `CheckShaderCapabilities`, `GetMemoryType`, `PreferDirectDeviceWrite`, `PipelineCacheDataCompatible`, `AlignMappedFlushRange`, `BuildMipChainFinalBarriers`, `SwapchainStageUnion`, `BuildTlasWriteBarrier`, `UIRenderer::ClipFlags`, `Window::SwapchainMinImageCount`, `WorkaroundNvidiaAS::Patch`, `DescriptorHeapVulkan::ComputeLayout`, `FontAtlas::Packer`, the font and input-field helpers). `PushConstantRewrite` and `UIFusedShader` compile GLSL with glslang and check the SPIR-V with spirv-val at runtime, and `ExampleShaders` compiles every GLSL shader under `examples/` the way crafter-build would, the descriptor-set build included, so a shader contract change that breaks an example fails here without building the example; `FontAtlasDecode` reads `ui-shared.glsl` and `dom-webgpu.js` and holds their copies of the SDF encoding constants to `FontAtlas`'s; `MouseScroll`, `PointerEnterLeave` and `InputTime` drive the Wayland listener callbacks directly, `TitleWayland` reads the requests a window sends off a `wl_display` connected to a socketpair, `TextInputContent` reads the text input's the same way and feeds its callbacks, and `ToplevelState` does both: it feeds the `xdg_toplevel` and `xdg_surface` configure callbacks of a window whose surfaces are proxies on such a display, and reads the version `xdg_wm_base` is bound at off the wire. `ConfigureResize` does the same for a configure's size: with a made-up swapchain handle it steps the two ends of a frame (`TakeDeferredSize` and `MatchSizesToScale`, `SendFrameSurfaceState`) and reads which commit gets the viewport's destination and the acknowledgement, and what the destination is when the scale changes in between.
- Pure logic, no GPU: they stage `Device`'s statics or feed synthetic data to the decision functions that were factored out for them (`Device::ResolveFeatures`, `ChooseDevice`, `CheckShaderCapabilities`, `GetMemoryType`, `PreferDirectDeviceWrite`, `PipelineCacheDataCompatible`, `AlignMappedFlushRange`, `BuildMipChainFinalBarriers`, `SwapchainStageUnion`, `BuildTlasWriteBarrier`, `UIRenderer::ClipFlags`, `Window::SwapchainMinImageCount`, `WorkaroundNvidiaAS::Patch`, `DescriptorHeapVulkan::ComputeLayout`, `FontAtlas::Packer`, the font and input-field helpers). `PushConstantRewrite` and `UIFusedShader` compile GLSL with glslang and check the SPIR-V with spirv-val at runtime, and `ExampleShaders` compiles every GLSL shader under `examples/` the way crafter-build would, the descriptor-set build included, so a shader contract change that breaks an example fails here without building the example; `FontAtlasDecode` reads `ui-shared.glsl` and `dom-webgpu.js` and holds their copies of the SDF encoding constants to `FontAtlas`'s; `MouseScroll`, `PointerEnterLeave`, `InputTime` and `KeyboardKeymap` drive the Wayland listener callbacks directly (`KeyboardKeymap` with keymaps in memfds, each case in a child process), `TitleWayland` reads the requests a window sends off a `wl_display` connected to a socketpair, `TextInputContent` reads the text input's the same way and feeds its callbacks, and `ToplevelState` does both: it feeds the `xdg_toplevel` and `xdg_surface` configure callbacks of a window whose surfaces are proxies on such a display, and reads the version `xdg_wm_base` is bound at off the wire. `ConfigureResize` does the same for a configure's size: with a made-up swapchain handle it steps the two ends of a frame (`TakeDeferredSize` and `MatchSizesToScale`, `SendFrameSurfaceState`) and reads which commit gets the viewport's destination and the acknowledgement, and what the destination is when the scale changes in between.
- A headless device: `Device::Initialize()` and no `Window` (`HeapSlotReuse`, `DeferredDeletion`, `MeshLifetime`, `ImageStagingRelease`, `BLASBuildOptions`, the `TLAS*` tests, ...). Headless still means a Wayland display on Linux. Most of the RT ones check `Device::rayTracingSupported` and return 77 without it; `BLASBuildOptions`, `TLASInstanceDirtyTracking` and `MeshDecompressStagingRelease` do not, and crash on a GPU without ray tracing (`Device::RequireRayTracing` throws out of `main`). `DeviceSelection` skips with one GPU.
- A window and a live compositor: `FrameLoopSync`, `SetupCmdBufferReuse`, `UIFallbackParity`, `SwapchainTransferDst`, `WindowUpdateClock`, `HeapASRead`, `HeapDynamicIndexHit`, `RTPassPushData`, `WindowOptionalGlobals` (hides xdg-decoration, viewporter and fractional scale from the window in turn), `SwapchainImageCount` (every present mode, and swapchains with more images than frames in flight, in child processes on llvmpipe where the host's GPU never makes one), `TextAntialiasing` (text through `DispatchText` and `DispatchFused` against stb_truetype's own antialiased rasterizer, at 14 and 64 px and stretched, plus a glyph mirrored by its UVs and empty texels shrunk below 4 px), `ResizeViewport` (a `Resize` and a configured size, got by maximizing its window for a moment, against the connection itself: it runs with `WAYLAND_DEBUG` set and stderr in a file and replays the log, which has the driver's attach and commit too), and `FeatureRequests` for its retry case. `UIFallbackParity`, `SwapchainTransferDst`, `HeapASRead`, `HeapDynamicIndexHit`, `RTPassPushData` and `TextAntialiasing` read the frame back with `Window::CaptureNextFrame` (a PNG, decoded with a private `STB_IMAGE_STATIC` copy of stb_image), `SwapchainImageCount` with a transfer pass of its own, and check pixels. The others read no frame back: they check what the frame loop, the device and the window report (their counts and members, the validation layer's count, the connection's protocol errors), and `ResizeViewport` the connection's log.