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:
parent
594e1612f7
commit
148ed01952
6 changed files with 407 additions and 16 deletions
|
|
@ -131,6 +131,7 @@ Heap-only: check `Device::rayTracingSupported`, or expect `std::runtime_error` f
|
|||
|
||||
- `Window` events, `Event<void>` unless noted: `onMouseMove` (then `currentMousePos`, `lastMousePos`, `mouseDelta`), `onMouseEnter` and `onMouseLeave` with `mouseInside` (native only: the DOM build declares the two events but never fires them, and has no `mouseInside`), `onMouseLeftClick`, `onMouseLeftHold`, `onMouseLeftRelease` and the right-button trio (`mouseLeftHeld`, `mouseRightHeld`), `onMouseScroll` (`Event<std::uint32_t>`: whole detents, reinterpret the payload as `int32`, positive is wheel down, every backend normalised to one per notch), `onRawKeyDown`, `onRawKeyHold`, `onRawKeyUp` (`Event<KeyCode>`, raw platform codes; `heldKeys`), `onTextInput` (`Event<const std::string_view>`, UTF-8; repeats are synthesised on Wayland), `onPreedit` and `onTextInputDelete` (input methods), `onTouchDown`, `onTouchMove`, `onTouchUp` (`Event<TouchPoint>`; the first finger also drives the pointer events), `onClose`, `onResize`, `onBeforeUpdate`, `onUpdate` (`Event<FrameTime>`: `now`, `delta`). On Wayland `lastInputTime` is the compositor's `time` of the pointer, touch or scroll event a listener is being invoked for (milliseconds from an undefined base, wrapping at 2^32, set before the event fires; an event the compositor sends without a time — an enter and the move that follows it, a leave, a touch cancel and its release — keeps the last one's): a consumer that dates gestures — a fling's velocity, a tap against a long press — reads it instead of its own clock at dispatch, since events queued while the loop was busy keep the times they happened at. A listener that dispatches Wayland events itself (it makes a `Window`, or calls `Clipboard::GetText`) has them set it too, for itself from there on and for the listeners of its event that come after it. The other backends have no such member. On Wayland `activated` and `suspended` are what the compositor says of the window's place on the screen (xdg_toplevel's states: it counts as the active window; it is not ordinarily being repainted — covered by another window, say, or its outputs switched off for a screen lock — which a compositor with an xdg_wm_base older than version 6 never says), and `onToplevelState` fires after a configure changed either, with both already set. Which of the two a lock screen or another window in front changes is the compositor's choice, so the test for "the user is looking at this window" — is the message it shows read? — is `activated && !suspended`; by it a window that is visible but not the active one is not looked at. The constructor takes the first configure before a listener can be added: read the two once after it, then follow the event. Both are false until a configure sets them, which that first one need not do. The other backends have neither member. `onFrameOwed`, Wayland's too, is the cue of a resize for a window that draws on demand: see "A program, and what the loop does".
|
||||
- Keys: `Key(CrafterKeys::Space)` is `consteval` and folds to the platform's raw code (a Win32 scancode, a Linux keycode), so bindings stay portable in source and compare against `onRawKeyDown`'s `KeyCode` at runtime. Bindings serialised with `Input::BindingToString` are platform-specific.
|
||||
- On Wayland a key's `onTextInput` comes from the compositor's keymap. Without one that loaded, keys still fire `onRawKeyDown` and `onRawKeyUp`, without text; a keymap that does not load, such as one of the `no_keymap` format or one the libxkbcommon the application runs with cannot compile, is ignored and leaves the one there was. `Device::xkb_keymap` and `Device::xkb_state` are that keymap and its state, for reading keysyms or modifiers yourself (the XKB keycode is the `KeyCode` plus 8): null until a keymap loads, and freed when the next one loads, so read them at each use and check for null.
|
||||
- `Input::Map`: `auto& jump = map.AddAction("Jump", Input::ActionType::Button);` (`Button`, `Axis1D`, `Vector2`), `jump.bindings = { Input::KeyBind{Key(CrafterKeys::Space)}, Input::GamepadButtonBind{id, Gamepad::Button::South} };` (also `MouseButtonBind`, `MouseScrollBind`, `MouseDeltaBind{scale}`, `GamepadAxisBind`, `GamepadStickBind{id, Gamepad::Stick::Left}`, `WASDBind{up, down, left, right}`), `map.Attach(window);`, and `map.Tick();` once per frame, after `Gamepad::Tick()`, which the loop runs before `onBeforeUpdate`. Every `Tick` recomputes the mouse delta and drains the scroll accumulator, and on Wayland `onBeforeUpdate` fires on every loop wake, so either tick from `onUpdate`, or tick in `onBeforeUpdate` and consume the delta and scroll actions in that same listener, as the Sponza example does. Read `action.pressed`, `value` or `vector2`, or subscribe to `onPerformed`, `onCanceled`, `onValueChanged`, `onVector2Changed`. `StartRebind(action, CaptureMask, callback)` captures the next input.
|
||||
- `Gamepad::connected`, `onConnected`, `onDisconnected`, per-device `buttons[]`, `axes[]` and events, `Rumble(dev, low, high, duration)`, `FindById(id)`; buttons are named by position (`South`, `East`, `West`, `North`). `Clipboard::SetText(text)` and `GetText()` (`std::optional<std::string>`).
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue