Repository navigation
obs-hotkey: Port the X11 key map lookups - #2903
Merged
Merged
Conversation
Behaviour-neutral extraction before the Rust port. The key map lookups of the X11 hotkey platform move from libobs/obs-nix-x11.c.h into libobs/obs-nix-x11-keys.c.h, as hidden functions on plain values declared without EXPORT in the new libobs/obs-nix-x11-keys.h: - obs_nix_x11_keys_from_base_keysym(): key_from_base_keysym(); - obs_nix_x11_keys_row(): fill_keycodes()'s rule for one keycode's row, returning what to do (the super keycodes, a key, nothing); - obs_nix_x11_keys_pressed(): key_pressed()'s decision on the QueryKeymap reply's key bitmap, NULL for no reply; - obs_nix_x11_keys_from_keycode(): key_from_keycode(); - obs_nix_x11_keys_from_virtual_key() and obs_nix_x11_keys_to_virtual_key(): the two vtable entries, with the key map and the base keysym table passed in. obs-nix-x11.c keeps the keysym table, the base keysym fill, add_key() (its list push and log), the super keycode stores, every xcb and Xlib call, the keyboard map copy and its size, the mouse path and the key names. The new file includes no X header: struct keycode_list moves to its header with uint8_t keycodes, the five keysyms it compares are defined there, and obs-nix-x11.c _Static_asserts them, the keysym and keycode sizes and the 32-byte bitmap against the X headers. Each platform field the original read only under a condition crosses by pointer and is read only there: the super keycodes (left, then right only when the left is not down; neither for other keys), a key's keycode list (not for Meta), num_keysyms past the search's gate and min_keycode once the symbol is found. The base table behind obs->hotkeys.platform_context comes through a getter, obs_nix_x11_keys_table_fn (x11_base_keysyms() in obs-nix-x11.c), called only for a key other than Meta. The new file gets its own unity group, libobs.nix-x11-keys, in the Linux sources (Linux and FreeBSD, CMake and obs-build). Its precompiled header has no X header, so a C test and later the C oracle compile it on their own. Checked with a harness: master's functions and the moved ones (with their new callers) extracted over logging stand-ins for add_key() and the platform state, built with gcc and clang at -O0, -O2 and under ASan and UBSan, on 3,000 random key maps (0 to 7 symbols per keycode, any keycode range, base tables with duplicates, the super and right modifier keysyms, random bitmaps and a missing reply, every key): identical logs, 4,437,999 lines each. 12 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 (1766 each); the new functions are local. Refs #2901. Co-authored-by: Lain <[email protected]> Co-authored-by: Georges Basile Stavracas Neto <[email protected]> Co-authored-by: Norihiro Kamae <[email protected]>
test_nix_x11_keys_steps builds the moved libobs/obs-nix-x11-keys.c.h into itself and calls its six hidden functions on made-up key maps, so it needs no X server and runs on every platform (11 cases): - the base keysym lookup: the first key with the keysym wins, the table wins over the Shift_R, Control_R and Alt_R fallback, the super keysyms have none, and keysym 0 gives the first key without one; - fill_keycodes()'s row rule: a first symbol of 0 ends the row, Super_L in the second column after an unknown symbol, a symbol known only at the second level, the first wanted symbol winning over a later super key and the reverse, a row of unknown symbols, and only syms_per_code symbols read (0 and negative read none); - key_pressed()'s decision: the second keycode of a key with two, Meta on either super keycode and never on its own list, Meta with neither super keycode found (both 0, never down), keycode 255, and a missing reply; - the keycode lookup: the first key whose list holds the code; - the virtual key search on the layout of test_x11_hotkey_layout: a symbol unshifted on one keycode and shifted on a lower one goes to the unshifted one, a symbol only on a shifted level still maps, and the search stops at the first keycode with the symbol even when it has no key; symbol 0, syms_per_code 0 or negative and an empty map give none; the keycode is min_keycode plus the row as a uint8_t (255 + 1 wraps to 0: characterized, not endorsed); - the virtual key of a key: Meta is Super_L whatever the table says, any other key its base keysym, read as an int. - each field read only where obs-nix-x11.c read it: NULL for every super keycode, list, map size and minimum keycode that must not be read, and a counting getter for the base table (not asked for Meta). Not characterized: fill_keycodes() returns true when the mapping request fails, and its caller ignores it; that stays C with the X calls. Passes on the C libobs build. The real-libobs X11 tests (test_x11_hotkey_layout 4/4 under Xvfb, test_x11_volume_keys, test_x11_keymap, test_hotkey_volume_keys) pass with the move. Refs #2901.
RED for the port of libobs/obs-nix-x11-keys.c.h to a new module, nix_x11_keys, of rust/obs-hotkey. The safe core is a stub (todo!()): key_from_base(), the base keysym lookup with the right modifier fallback; row_key(), fill_keycodes()'s rule for one keycode's row (Row: None, SuperL, SuperR or Key); any_pressed(), key_pressed()'s decision on the QueryKeymap bitmap, None for no reply; key_from_keycode(); from_virtual(), the column-major virtual key search; and to_virtual(). Keys are obs_key_t as i32, tables and maps are slices, an input the C read only under a condition is a closure called only under it, and a keycode list is any AsRef<[u8]>, so the shim can pass the C darrays in place. tests/hotkey/nix_x11_keys.rs ports test_nix_x11_keys_steps 1:1, its 11 cases under the same names, in the same order, with the same inputs and expectations. Kept as characterized, not endorsed: the keycode is min_keycode plus the row as a u8, so 255 + 1 wraps to 0. Edge cases: to_virtual() of a key outside the table is 0 (C reads past the table; the one intentional difference), Meta before any table lookup; empty and short tables; a partial last row of the map is not searched; every keycode's bit in the bitmap. Proptests check each function against a model written from the C. 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_x11_keys fails 21 of 21 with "not yet implemented". Refs #2901.
GREEN for #2901. Ports libobs/obs-nix-x11-keys.c.h, the key map lookups of the X11 hotkey platform, to the nix_x11_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_x11_keys.rs): - key_from_base(): the first key with the base keysym, else Shift_R, Control_R and Alt_R give Shift, Control and Alt; - row_key(): the first non-zero symbol of a keycode's row that is Super_L, Super_R or has a key, stopping at a zero; - any_pressed(): Meta on either super keycode, any other key on any of its keycodes, nothing without a reply; - key_from_keycode(): the first key whose list holds the keycode; - from_virtual(): the 0 and empty-map gate, then one column at a time over the whole rows of the map, the keycode wrapping as a uint8_t; - to_virtual(): Super_L for Meta, else the key's base keysym, and 0 for a key outside the table. C indexes its table with any key that reaches obs_key_to_virtual_key(); this is the one intentional difference. It keeps no state, takes no lock, allocates nothing, reads only the slices it is given and denies clippy's arithmetic, indexing, as-conversion, unwrap, expect, panic and unreachable lints. Shim (src/ffi/nix_x11_keys.rs): the six functions with the C signatures. The base keysym table and the keycode lists cross as OBS_KEY_LAST_VALUE entries, the keyboard map with the num_keysyms the C computed, a row with syms_per_code symbols, the bitmap as 32 bytes or NULL; the super keycodes, num_keysyms and min_keycode by pointer and the base table through the caller's getter, each read inside the closure the core calls only under the C's condition. struct keycode_list is mirrored as its darray, read in place. The row result is the C enum's value, and *key is written only for a key, as the C does. A null table, map or list reads as empty where C would dereference it. C side: the file leaves libobs.nix-x11-keys for its own ported group, libobs.ported-nix-x11-keys, registered in libobs/build/ported-source-sets/nix-x11-keys.json for Linux and FreeBSD. 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 only. test_nix_x11_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 real-libobs X11 tests see the swap through the hybrid. Tier 1 hotkey::nix_x11_keys passes 21/21. On the hybrid and the C reference libobs, test_x11_hotkey_layout (4/4 under Xvfb), test_x11_volume_keys, test_x11_keymap, test_hotkey_volume_keys and test_nix_x11_keys_steps pass; the exported symbols are identical on the C, hybrid and C reference builds (1766 each), with the six functions local in all three. Refs #2901. Co-authored-by: Lain <[email protected]> Co-authored-by: Georges Basile Stavracas Neto <[email protected]> Co-authored-by: Norihiro Kamae <[email protected]>
Oracle (oracle/nix_x11_keys.c): the original obs-nix-x11-keys.c.h, unmodified, with its six functions renamed to oracle_*, and getters for the key, keysym and row values and the struct keycode_list layout the C sees. Tier 3 (tests/hotkey/nix_x11_keys_parity.rs, 6 tests): - the values and the keycode_list layout against the C; - each keyboard runs fill_keycodes()'s walk on both sides (the row result, the key written, the super keycode stores and the list pushes) and compares the super keycodes and the lists; then every row at every length, the base keysym lookup of every symbol in the map and of the special keysyms, the keycode lookup of every keycode on those lists and on lists that share keycodes, the pressed decision of every key with and without a reply, the virtual key search of every symbol with the map read at six row sizes (partial last rows included), and the virtual key of every key, with how often each side calls its table getter; - a fixed US-like keyboard (keycodes 8 to 255, four symbols each); - exhaustive over the base table's keys: the virtual key and the base keysym of every key of four tables (distinct, shared, zero and special keysyms); - proptests over random keyboards (1 to 7 symbols per keycode, keycodes 8 to 255 and other ranges; 96 cases) and every int key. Intentional difference, excluded and checked on the Rust side only: the virtual key of a key outside 0 .. OBS_KEY_LAST_VALUE - 1, which is 0 in Rust (C indexes its table with it). port_comparison gets benches/port_comparison/nix_x11_keys.rs: 64 pressed polls and 64 virtual key searches on the US-like map, folded into a checksum both sides must match. Linux x86_64: Rust 14.4 us, C 12.3 us; the difference is in the virtual key search, which runs only when a hotkey is set from a keysym. It allocates nothing with bmalloc; obs-c-oracle gets obs-hotkey as a dev-dependency for it. Mutation check (done once, not committed); failing tests per mutant: | Mutant | Tier 1 | Tier 3 | |---|---|---| | base: no Alt_R fallback | 2 | 1 | | base: last match wins | 4 | 3 | | row: a zero does not end it | 2 | 1 | | row: super keys swapped | 2 | 2 | | pressed: Meta left only | 2 | 1 | | pressed: bit order reversed | 3 | 2 | | keycode: last list wins | 3 | 2 | | virtual: first column only | 3 | 2 | | virtual: partial last row searched | 2 | 1 | | virtual: keycode off by one | 5 | 2 | | virtual: no symbol 0 gate | 2 | 2 | | to_virtual: Meta is Super_R | 2 | 3 | | to_virtual: table off by one | 1 | 3 | | shim: row super results swapped | 0 | 2 | | shim: map one symbol short | 0 | 1 | | shim: pressed ignores the list | 0 | 2 | | shim: row reads one symbol less | 0 | 2 | | shim: key written for every result | 0 | 2 | All 18 are killed; the shim mutants only by Tier 3, the tier that reaches the shims. "keycode: last list wins" first survived Tier 3, whose lists came only from the walk (each keycode in one list), so the check now also runs the keycode lookup on lists that share keycodes. The proptest regression files keep the seeds the mutants failed on. Tier 1 and Tier 3 pass (21/21 and 6/6) in 2.3 s. Refs #2901.
The rust/README.md crate table now lists libobs/obs-nix-x11-keys.c.h among the rust/obs-hotkey units, and its port table has a row for it, before the hotkey binding codec: the six lookups, what stays C in obs-nix-x11.c (the keysym table, add_key(), the super keycode stores, every X call, the map copy, the mouse path and the key names), the static asserts against the X headers, how the tables cross, the two characterized-not-endorsed cases (the search stops at the first keycode with the symbol; the keycode wraps as a uint8_t), the intentional differences, what Tier 3 compares and which C tests see the swap. 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 gets the one change a plugin or script can see, under the fixed upstream bugs for scripting: obs_key_to_virtual_key on X11 gives 0 for a key OBS doesn't have, as for a key with no keysym. Refs #2901.
rdoupe
force-pushed
the
fix/rust-port-x11-key-map-keysym-and-263
branch
from
October 29, 2026 13:23
94a5737 to
79ca608
Compare
rdoupe
marked this pull request as ready for review
October 29, 2026 13:34
This was referenced Oct 29, 2026
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 #2901
What and why
Port of the X11 key map lookups of
libobs/obs-nix-x11.c.htorust/obs-hotkey, as a new modulenix_x11_keys, next tonix_hotkey_state(#1516) and the portal shortcut modules (#1800). On X11 (Linux and FreeBSD) libobs uses them to build the keycode lists at hotkey init, to answer the hotkey thread's "is this key down" poll, and inobs_key_from_virtual_key/obs_key_to_virtual_key.Commits:
libobs: Move the X11 key map lookups(behaviour-neutral): six hidden functions on plain values inlibobs/obs-nix-x11-keys.c.h, declared without EXPORT inlibobs/obs-nix-x11-keys.h, in their own unity group with no X header. Each platform field the original read only under a condition (the super keycodes, a key's keycode list,num_keysyms,min_keycode, the base table behindobs->hotkeys.platform_context) crosses by pointer, or through a getter for the table, and is read only under that condition.obs-nix-x11.c_Static_asserts the five keysyms, the keysym and keycode sizes and the 32-byte keymap bitmap against the X headers.test: Characterize the X11 key map lookups(test_nix_x11_keys_steps, 11 cases, every platform,reads_under_their_guardsamong them).obs-hotkey: Add Tier 1 tests for the X11 key port(RED).obs-hotkey: Port the X11 key map lookups(GREEN: core, shims, grouplibobs.ported-nix-x11-keysregistered inlibobs/build/ported-source-sets/nix-x11-keys.json, the three hidden-symbol lists,ci/test_ported_sources.py).obs-hotkey: Check the X11 key port against the C(oracle, Tier 3,port_comparison, mutation check).docs: Record the X11 key map port.The six lookups:
key_from_base_keysym();fill_keycodes()'s rule for one keycode's row;key_pressed()'s decision on the QueryKeymap bitmap;key_from_keycode(); the column-major virtual key search; the virtual key of a key.obs-nix-x11.ckeeps the keysym table (get_keysym), the base keysym fill,add_key()(its list push and log), the super keycode stores, every xcb and Xlib call, the keyboard map copy and its size, the mouse path and the key names.Characterized, not endorsed (kept): the search stops at the first keycode with the symbol even when that keycode has no key; the keycode is
min_keycodeplus the row as auint8_t, so it wraps.Intentional differences (in the parity test header,
rust/README.mdand, for the first,docs/features.md):obs_key_to_virtual_keyon X11 returns 0 for a key outside0 .. OBS_KEY_LAST_VALUE - 1, which has no keysym (C indexes its table with it); a null table, map or list reads as empty (C dereferences it; libobs never passes one).No
_rustvariant of the steps test: as fortest_portal_shortcut_sets, an obs-hotkey shim's archive member does not link on its own againstlibobs_rust-static, so Tier 3 compares the shims with the C.The moved code is upstream's (Lain Bailey, Georges Basile Stavracas Neto, Norihiro Kamae), credited in the move and port commits. No upstream issue.
RED → GREEN
soldr cargo test -p obs-data --test obs-tests-core hotkey::nix_x11_keysfails 21 of 21 with "not yet implemented". On the RED commit as first pushed (before the review fixes were folded in, 20 tests then),local_gate.py --lane lintpassed and--test-diff origin/masterfailed only those tests.hotkey::nix_x11_keys21/21; Tier 3hotkey::nix_x11_keys_parity6/6 (2.3 s together); allhotkey::tests 254/254 at the GREEN commit. Linux x86_64 (GCC, C build and hybrid + C reference build):test_nix_x11_keys_steps,test_x11_hotkey_layout(4/4 under a private Xvfb, no skips),test_x11_volume_keys,test_x11_keymapandtest_hotkey_volume_keyspass on the C libobs, the hybrid andlibobs-c-reference; onlylibobs-c-referencebuilds the group object. Exported symbols identical on the C, hybrid and C reference builds (1766 each), the six functions local (t) in all three. Runs were on 7b51c97, d7cdb61 and 79ca608.Local gates (runs were on 79ca608, rebased on current master):
./lintok;./unittestRust tiers 5041 + 845 passed, 6 skipped;local_gate.py --lane lintand--lane crossok;soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warningsclean; theport_comparisonbench test passes. The exported symbols are still identical on the C, hybrid and C reference builds (1766, the six functions local). Not run locally: Tier 2 in Docker (./unittest's tier2-linux lane) and the local-gate stamp, which need Docker access this host's build account doesn't have; the head is unstamped, so PR CI runs the full suite.Move check (runs were on 1ce279f): master's functions and the moved ones with their new callers, extracted over logging stand-ins for
add_key()and the platform state, built with gcc and clang at -O0, -O2 and under ASan and UBSan, on 3,000 random key maps: identical logs (4,437,999 lines each); 12 planted mutants in the moved file and its callers all change the log.Mutation check of the port: 18 mutants (13 in the core, 5 in the shims), all killed; the core ones by Tier 1 and Tier 3, the shim ones by Tier 3. Table in commit 5.
port_comparison(nix_x11_keys/poll_and_search/64, Linux x86_64): Rust 14.4 µs, C 12.3 µs for 64 pressed polls and 64 virtual key searches; the difference is in the search, which runs only when a hotkey is set from a keysym.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated-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 (not applicable: no warning or flag change)