Repository navigation
obs-hotkey: Port the Wayland keysym tables - #3072
Merged
Merged
Conversation
Behaviour-neutral extraction before the Rust port. The keysym tables and the keymap and focused-key rules of the Wayland hotkey platform move from libobs/obs-nix-wayland.c.h into libobs/obs-nix-wayland-keys.c.h, as hidden functions on plain values declared without EXPORT in the new libobs/obs-nix-wayland-keys.h: - obs_nix_wayland_keys_from_keysym() and obs_nix_wayland_keys_to_keysym(): the bodies of obs_nix_wayland_key_from_virtual_key() and obs_nix_wayland_key_to_virtual_key(), moved whole; - obs_nix_wayland_keys_row(): load_keymap_data()'s rule for one keycode (the bound, each level's first keysym, the first keycode winning for an obs key), with each level's keysyms from a callback; - obs_nix_wayland_keys_focused(): set_focused_key()'s rule (the evdev scancode plus 8, the bound, the level-0 keysym and its obs key). obs_nix_wayland_keys_focused() also takes the event's pressed flag and a new per-keycode record in the platform, pressed_as, for the port: it will release the key a press reported (#3069). This C rule uses neither, and obs-nix-wayland.c only clears the record where it clears the key state (focus leave, keyboard release), so nothing reads it. obs-nix-wayland.c keeps every xkbcommon and Wayland call, the keymap mmap and compile, the rebuild's memsets, the modifier mask, the seats and registry, the obs_nix_hotkey_state_set_key() call, the key names and the vtable, whose two keysym entries are now one-line wrappers. xkb_keymap_key_get_syms_by_level() stays in obs-nix-wayland.c, in a callback the row rule calls once per level, in order, only for a keycode in range, so it runs where and when it did, reading current_layout each time. The level-0 row crosses by pointer and is read only past the bound. The new file includes only <xkbcommon/xkbcommon-keysyms.h>, which has no code, and obs-nix-wayland.c _Static_asserts the keysym, keycode and level index sizes, the two bounds against xkbcommon and its own, and OBS_KEY_NONE == 0 for a cleared record. It gets its own unity group, libobs.nix-wayland-keys, next to libobs.nix-wayland (ENABLE_WAYLAND; Linux and FreeBSD, CMake and obs-build). test_wayland_keymap, test_wayland_multiseat and test_wayland_volume_keys compile the platform into themselves, so their registrations add the new file (the C tests are unchanged). Checked with a harness: master's functions and the moved ones (with their new callers) extracted over logging stand-ins for the xkbcommon calls, the keymap walk and the hotkey state, built with gcc and clang at -O0, -O2 and under ASan and UBSan, on every keysym from -70000 to 0x20000 and 0x1008FE00 to 0x1008FFFF plus 100,000 random ones, every key from -100 to OBS_KEY_LAST_VALUE + 100, and 2,000 random keymaps (counts -1 to 3 per level, repeated keysyms, keycode ranges up to 319, four layouts, a layout change in each) with 64 focused key events each (scancodes past the bound and near UINT32_MAX), the record dumped with the tables: identical logs, 5,745,639 lines each. 17 planted mutants (in the moved file and in its callers) all change the log. The exported symbols are the same on the C, hybrid and C reference libobs (1767 each); the four new functions are local. The Wayland tests (test_wayland_hotkeys, test_wayland_keymap, test_wayland_multiseat, test_wayland_volume_keys) and test_hotkey_volume_keys pass on the C build. Refs #3069. Co-authored-by: Kurt Kartaltepe <[email protected]>
test_nix_wayland_keys_steps builds the moved libobs/obs-nix-wayland-keys.c.h into itself and calls its four hidden functions on made-up keymaps, so it needs no compositor (11 cases): - the keysym-to-key table: each of its 332 cases, and no other keysym in the Latin-1, keypad, function, vendor (BackTab) and XF86 ranges; 0, -1, INT32_MIN, INT32_MAX, NoSymbol and VoidSymbol give none; - the key-to-keysym table: each of its 275 cases, 0 for every other key and for values outside the enum; - round trips: every key with a keysym comes back as itself, and the 57 lowercase Latin-1 letters come back as their uppercase keysym; - load_keymap_data()'s rule for one keycode: each level asked for once, in order; a level with a count below 1 stores nothing; the first keysym of any other is stored; nothing is asked for or stored at keycode 256 and above (255 is); the first keycode stored for a key wins, at any level and in either case of a letter, a stored 0 counting as none; a keysym with no key stores its keycode as OBS_KEY_NONE's (characterized, not endorsed); - set_focused_key()'s rule: the scancode plus 8, the level-0 keysym's key the same for a press and its release; nothing for a keycode of 256 and above or with no level-0 keysym, and *key untouched then; the scancode plus 8 wrapping as a uint32_t and a keysym with no key giving OBS_KEY_NONE (both characterized, not endorsed); a press and release across a layout change reported as two different keys (characterized, not endorsed: the pressed key stays held); - the pressed flag and the pressed_as record the port will use: this rule reports a release like a press and leaves the record alone (checked after each focused-key case). The test needs only xkbcommon's keysym names, so it is registered in tests.d where the Wayland platform builds (Linux and FreeBSD with ENABLE_WAYLAND), under ASan and UBSan. Passes on the C libobs build. The real-platform Wayland tests (test_wayland_hotkeys, test_wayland_keymap, test_wayland_multiseat, test_wayland_volume_keys) and test_hotkey_volume_keys pass with the move. Refs #3069.
RED for the port of libobs/obs-nix-wayland-keys.c.h to a new module, nix_wayland_keys, of rust/obs-hotkey. The safe core is a stub (todo!()): from_keysym() and to_keysym(), the two tables; keymap_row(), load_keymap_data()'s rule for one keycode, with each level's first keysym from a closure; focused(), set_focused_key()'s rule, with the record of the key each keycode was pressed as. Keys are obs_key_t as i32 and the arrays slices. keysyms.rs holds the 334 XKB keysyms the tables name, with xkbcommon's names and values. The tables name their keys as the C does, through the consts build.rs emits for every upper-case key of obs-hotkeys.h; its comment now lists the Wayland tables among their users. tests/hotkey/nix_wayland_keys.rs ports test_nix_wayland_keys_steps 1:1, its 11 cases under the same names, in the same order, with the same inputs and expectations, the two tables included. Kept as characterized, not endorsed: a keysym with no key stores its keycode as OBS_KEY_NONE's; the scancode plus 8 wraps as a u32; a level-0 keysym with no key reports OBS_KEY_NONE. Intentional difference (#3069): a release reports the key its press reported, from the record, and clears it; a release with nothing recorded reports nothing. The C looks a release up again on the current layout, so after a layout change it releases another key and the pressed one stays held. The four focused-key cases check the record where the C test checks that it is left alone, and focused_across_layout_change releases Q where the C releases A. Edge cases: every int edge of the keysym table, keys outside the enum, short arrays (not written past, every level still asked for), a release with no press, a release after a press with no key, a second press. Proptests check whole keymaps row by row and press and release sequences across layout changes against models written from the C and the fix. The tests are a module of the existing core category binary, obs-tests-core; no new test binary. soldr cargo test -p obs-data --test obs-tests-core hotkey::nix_wayland_keys fails 20 of 20 with "not yet implemented". Refs #3069.
GREEN for #3069. Ports libobs/obs-nix-wayland-keys.c.h, the keysym tables and the keymap and focused-key rules of the Wayland hotkey platform, to the nix_wayland_keys module of rust/obs-hotkey. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/nix_wayland_keys.rs): - from_keysym() and to_keysym(): the two switches, case for case (332 and 275 arms), with xkbcommon's keysym names (keysyms.rs) and the key names of obs-hotkeys.h; a negative int has no key; - keymap_row(): nothing at keycode 256 and above; each of the four levels asked for in order, its first keysym stored for the keycode, and the keycode stored for that keysym's key unless one is (0 is none); - focused(): the scancode plus 8 (wrapping), the bound; a press reports the key of the level-0 keysym (none for 0) and records it for the keycode; a release reports the recorded key and clears it, or nothing when none is recorded. It keeps no state, takes no lock, allocates nothing, reads and writes only the slices it is given and denies clippy's arithmetic, indexing, as-conversion, unwrap, expect, panic, todo and unreachable lints. The two table functions expect clippy::too_many_lines (one arm per C case), and the Tier 1 file allows wildcard_imports on its key and keysym imports. Intentional difference (#3069): C looks a release up again on the current layout, so a key held while the layout changes is released as the key the new layout puts on its keycode (Q pressed on QWERTY is released as A on AZERTY), and the pressed key's hotkey stays held until OBS loses focus. The port releases the key the press reported. Shim (src/ffi/nix_wayland_keys.rs): the four functions with the C signatures. The platform's arrays cross in place: OBS_KEY_LAST_VALUE keycodes, four rows of 256 keysyms, 256 recorded keys. A row's keysyms come from the caller's callback (xkb_keymap_key_get_syms_by_level()), called once per level, in order, only for a keycode in range, and its first keysym is read only for a count of at least 1. *key is written only when true is returned. A null array reads as empty and a null callback as a level with no keysym (C dereferences or calls them). C side: the file leaves libobs.nix-wayland-keys for its own ported group, libobs.ported-nix-wayland-keys, registered in libobs/build/ported-source-sets/nix-wayland-keys.json under LIBOBS_WAYLAND_PORTED_SOURCES (Linux and FreeBSD with ENABLE_WAYLAND, CMake and obs-build). The functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. ci/test_ported_sources.py checks the group is ported on Linux with Wayland only, and tools/conversion/measure.py counts it with the other ported groups. test_nix_wayland_keys_steps still compiles the C file into itself, unchanged. There is no _rust variant: as for the other obs-hotkey shims (test_portal_shortcut_sets), the archive member does not link on its own, so the Tier 3 tests compare the shims with the C. The Wayland platform tests compile obs-nix-wayland.c into themselves with this C file. Tier 1 hotkey::nix_wayland_keys passes 20/20 (hotkey:: 314/314). On the hybrid and the C reference libobs, the 22 hotkey and Wayland tests (test_nix_wayland_keys_steps, test_wayland_hotkeys, test_wayland_keymap, test_wayland_multiseat, test_wayland_volume_keys, test_hotkey_volume_keys and the other hotkey tests) pass; test_x11_hotkey_layout skips its 4 cases without an X display. The exported symbols are identical on the C, hybrid and C reference builds (1767 each), with the four functions local in all three. Refs #3069. Co-authored-by: Kurt Kartaltepe <[email protected]>
Oracle (oracle/nix_wayland_keys.c): the original obs-nix-wayland-keys.c.h, unmodified, with its four functions renamed to oracle_*, and getters for the values the C uses and every keysym the tables name. The C needs xkbcommon's keysym names: build.rs takes <xkbcommon/xkbcommon-keysyms.h> where pkg-config finds xkbcommon (header only, nothing linked), and elsewhere (macOS, Windows, a host without libxkbcommon-dev) writes a stand-in with the same names and the values of the Rust keysym list, as the oracle's SSE stand-in does for SIMDe. The tables then still compare case by case; only the values go unchecked, and the oracle says which header it had. CI's Linux Rust job installs libxkbcommon-dev for it. Tier 3 (tests/hotkey/nix_wayland_keys_parity.rs, 10 tests): - the values, and the names and values of all 334 keysyms against xkbcommon's header; - both tables on the shim, the core and the C: every keysym from 0 to 0x1FFFF, 0x1000FF00 to 0x1000FFFF and 0x1008FE00 to 0x1008FFFF, one above and one below each keysym the tables name, the int edges, and every key from -100 to OBS_KEY_LAST_VALUE + 100; - keymap rows: 200 generated keymaps (keycodes from 0 to 8 up to 255 or 300, counts -1 to 3 per level, keysyms from the tables, 0, random ones and one repeated across keycodes) and one with every key of the to-table, row by row on both sides with the same level callback: the levels asked for and both arrays; - focused keys: every scancode from 0 to 300 and the 17 up to UINT32_MAX pressed, released and released again, a QWERTY to AZERTY switch with keys held, and the shim against the core; - proptests: any int keysym and key, keymaps from any seed, and press and release sequences over random layouts. Intentional difference (#3069), excluded only where it applies: the shim agrees with the C on every press and out-of-range event; on a release it reports what the C reported for the press (a model kept from the C's own results), or nothing, and the C's record stays untouched. The layout switch counts exactly its two differences. port_comparison gets benches/port_comparison/nix_wayland_keys.rs: a whole US-like keymap loaded row by row and 64 presses and releases, folded into a checksum both sides must match. Linux x86_64: Rust 8.5 us, C 7.2 us, about one keymap load per layout change. It allocates nothing with bmalloc. Mutation check (done once, not committed); failing tests per mutant: | Mutant | Tier 1 | Tier 3 | |---|---|---| | from: a gives B | 3 | 6 | | from: AudioMute dropped | 2 | 5 | | to: NUM9 gives KP_8 | 2 | 1 | | to: default 1 | 3 | 2 | | bound: <= 256 | 2 | 3 | | row: three levels | 4 | 3 | | row: last keycode wins | 3 | 2 | | row: level 0 for all | 5 | 3 | | row: keycode + 1 stored | 5 | 3 | | focused: scancode + 7 | 8 | 3 | | focused: release looked up (no fix) | 7 | 3 | | focused: release keeps the record | 4 | 2 | | focused: press not recorded | 7 | 3 | | focused: no zero gate | 3 | 1 | | shim row: count 0 read | 0 | 3 | | shim row: level + 1 asked | 0 | 3 | | shim focused: *key written for none | 0 | 2 | | shim from: sym + 1 | 0 | 1 | All 18 are killed; the shim mutants only by Tier 3, the tier that reaches the shims. The proptest regression files keep the seeds the mutants failed on. Tier 1 and Tier 3 pass (20/20 and 10/10) in 0.5 s, and 10/10 with the stand-in header. Test cost at OBS_PARITY_CASES=reduced: longest 0.56 s (Tier 1's keymap proptest), peak RSS 19 MB over 20 runs, no threads of their own. Refs #3069.
The rust/README.md crate table now lists libobs/obs-nix-wayland-keys.c.h among the rust/obs-hotkey units, and its port table has a row for it, before the X11 key map lookups: the four functions, what stays C in obs-nix-wayland.c (every xkbcommon and Wayland call, the keymap compile, the memsets, the hotkey state, the key names), the level callback, the static asserts, how the arrays cross, the cases kept as characterized, not endorsed, the intentional differences, what Tier 3 compares and how the oracle gets xkbcommon's keysyms. naming.md's rust/obs-hotkey row, the crate's description and its lib.rs module docs add the file and the module. docs/features.md extends the line of the Wayland focused-key state (#210), whose release this fixes: a hold hotkey is released when its key is let go even after the keyboard layout changed while it was held, instead of staying pressed until OBS loses focus. Fixes #3069.
rdoupe
force-pushed
the
fix/rust-port-wayland-keysym-tables-299
branch
from
October 30, 2026 16:30
46ac993 to
db0bb5d
Compare
rdoupe
marked this pull request as ready for review
October 30, 2026 17:01
This was referenced Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3069
What and why
Ports the keysym tables, the keymap row rule and the focused-key rule of the Wayland hotkey platform (
libobs/obs-nix-wayland.c.h) to a new module,nix_wayland_keys, ofrust/obs-hotkey. They map every Wayland hotkey's keysym to its obs key and back (obs_key_from_virtual_key/obs_key_to_virtual_key), name the GlobalShortcuts portal's preferred triggers, build the keycode and keysym maps each time the compositor sends a keymap or the layout changes, and tell the hotkey thread which keys are held while OBS has focus.Commits:
libobs: Move the Wayland keysym tables: behaviour-neutral extraction intolibobs/obs-nix-wayland-keys.c.h, four hidden functions in their own unity group. Every xkbcommon and Wayland call stays inobs-nix-wayland.c; each level's keysyms come through a callback called whereload_keymap_data()calledxkb_keymap_key_get_syms_by_level(), once per level, in order, only for a keycode in range. The focused-key function also takes the event's pressed flag and a new per-keycode record (pressed_as) for the port; the C rule uses neither, and the platform only clears the record with the key state. Checked with a harness: master's and the moved functions (with their callers) over logging stand-ins, gcc and clang at -O0, -O2 and ASan/UBSan, on every keysym in the table ranges, every key from -100 toOBS_KEY_LAST_VALUE+ 100, and 2,000 random keymaps with focused-key events across layout changes: identical logs (5,745,639 lines each); 17 planted mutants all change the log.test: Characterize the Wayland keysym tables:test_nix_wayland_keys_steps(11 cases, no compositor, ASan/UBSan): both tables case by case (332 and 275 cases), their round trips, the row rule and the focused-key rule. Odd cases are marked "characterized, not endorsed" (a keysym with no key stores its keycode asOBS_KEY_NONE's; the scancode plus 8 wraps; a release after a layout change reports another key).obs-hotkey: Add Tier 1 tests for the Wayland keys: RED, the C test ported 1:1 plus edges and proptests againsttodo!().build.rsnow emits a const for every key ofobs-hotkeys.h.obs-hotkey: Port the Wayland keysym tables: GREEN, the safe core and the shims; the file moves to the ported grouplibobs.ported-nix-wayland-keys(LIBOBS_WAYLAND_PORTED_SOURCES), and the four symbols are hidden in the three export lists.obs-hotkey: Check the Wayland key port against C: Tier 3 against the C oracle, with the mutation check, and aport_comparisonbench. The oracle uses xkbcommon's keysym header where pkg-config finds it and a stand-in written from the Rust keysym list elsewhere (macOS, Windows), so the tables compare on every OS; the Linux Rust test job installslibxkbcommon-devso the keysym values are checked against the real header there.docs: Record the Wayland keysym tables port.Review fixes are folded into the commits they belong to (the
naming.mdrow, the shims sorted in the export lists, the Tier 1 proptest counts throughobs_c_oracle::parity::cases).Intentional difference (the bug fixed in the port): a key held while the keyboard layout changed stayed held for hotkeys until OBS lost focus, because its release was looked up on the new layout (Q pressed on QWERTY was released as A after a switch to AZERTY, so a push-to-talk on Q stayed on). The port records the key each keycode was pressed as and releases that key. It is listed in the parity test,
rust/README.mdanddocs/features.md(on the line of the Wayland focused-key state, #210). Not reported by a user; no upstream issue (the focused-key state is this fork's).Upstream authors of the moved code are credited on the move and GREEN commits: Kurt Kartaltepe (the Wayland hotkey tables and keymap handling).
RED → GREEN
soldr cargo test -p obs-data --test obs-tests-core hotkey::nix_wayland_keys::fails 20 of 20 with "not yet implemented"; before the rebase onto current master,--test-diffon it otherwise passed (5,281 passed, 6 skipped) and the lint lane passed.hotkey::314/314), Tier 3 10/10 (also 10/10 with the oracle's stand-in header). Tier 2: the 22 hotkey and Wayland tests (test_nix_wayland_keys_steps,test_wayland_hotkeys,test_wayland_keymap,test_wayland_multiseat,test_wayland_volume_keys,test_hotkey_volume_keysand the other hotkey tests) pass against hybridlibobsandlibobs-c-reference, 0 skipped excepttest_x11_hotkey_layout's 4 cases without an X display. Exported symbols identical on the C, hybrid and C reference builds (1767 each), the four functions local in all three. Mutation check: 18 planted mutants (14 core, 4 shim), all killed; the shim ones by Tier 3. Gates:--test-diff5,311 passed, 6 skipped; lint and cross lanes OK; Windows MSVC all-targets clippy clean. Bench: Rust 8.5 µs, C 7.2 µs for a whole keymap load and 128 key events. Test cost: new tests longest 0.56 s, peak RSS 19 MB over 20 runs, no own threads. Runs were on db0bb5d (rebased onto master with the X11 input port):./lint, the Rust tiers of./unittest(5,368 and 857 passed, 0 failed), the lint and cross lanes, Windows clippy, the bench test, Tier 2 natively and the export sets. The pushed head is not stamped: the local gate's Tier 2 lane needs Docker, which this machine's user can't reach, so PR CI runs the full suite.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC