Skip to content

libobs: Port the Wayland hotkey key state to obs-hotkey - #1516

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-hotkey-state
Oct 20, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-hotkey-state

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Base: master

Fixes #1514 (PR 2 of 2; PR 1, #1515, added the hidden lock accessor this port calls)

What and why

This PR ports libobs/obs-nix-hotkey-state.c.h, the Linux/FreeBSD Wayland key and shortcut state machine, to the existing rust/obs-hotkey crate. The file has seven functions: obs_nix_hotkey_state_free, _set_key, _set_modifiers, _clear_keys, _set_shortcut, _flush and _is_pressed. obs-nix-wayland.c and obs-nix-portal-shortcuts.c call them to tell the hotkey thread which keys and GlobalShortcuts are held. libobs-with-rust drops the C file and links the Rust shims.

  • Crate: obs-hotkey already holds the hotkey name-map port (rust: Port libobs/obs-hotkey-name-map.c to obs-hotkey #833), so the naming.md row now reads libobs/obs-hotkey-name-map.c, obs-nix-hotkey-state.c.h → rust/obs-hotkey. No new crate. obs-util moves from a dev-dependency to a dependency, because the shortcut list is a bmalloc darray (obs_util::ffi::darray). build.rs now also emits OBS_KEY_LAST_VALUE and the four modifier key codes from obs-hotkeys.h, so they cannot drift from the C enum.
  • Safe core (src/nix_hotkey_state.rs):
    • The functions are generic over where the state lives (Store: the owned KeyState, or the C struct through the shim) and over the lock (HotkeyLock).
    • The core takes the lock exactly where the C does: once per call, through an RAII guard. It takes no lock for an out-of-range key (key <= OBS_KEY_NONE || key >= OBS_KEY_LAST_VALUE) or for free.
    • No host cfg, no unsafe.
  • Shim (src/ffi/nix_hotkey_state.rs):
    • #[repr(C)] obs_key_combination, obs_nix_hotkey_shortcut and obs_nix_hotkey_state. The darray field is a #[repr(transparent)] wrapper of struct darray, grown and erased as da_push_back and da_erase do.
    • The lock is obs->hotkeys.mutex, taken through libobs's hidden obs_hotkeys_mutex_lock / obs_hotkeys_mutex_unlock (libobs: Lock the hotkey mutex through a hidden accessor #1515). The Rust side never reads obs.
    • Other threads change the state under that lock, so the shim borrows the struct one field at a time, and only after the core has taken the lock. Two &mut borrows of the same struct never overlap.
    • The C file has no EXPORT, so the seven symbols are hidden: they are listed in rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt, and there is no rust-exports.d fragment (same as libobs: Port obs_bounds_fit_scale to Rust #1486 and libobs: Port the PulseAudio stream flags to obs-audio-monitoring #1513). In both libobs.so builds they are local (t).
    • The shims are built on every host, but only Linux and FreeBSD with ENABLE_WAYLAND call them. obs_hotkeys_mutex_lock is defined in obs-hotkey.c.h on every host, so Windows and macOS link as before (the cross and cross-release lanes pass).
  • Build group:
    • The file leaves libobs.nix-wayland for its own group, libobs.ported-nix-hotkey-state. That group's PCH, libobs-ported-nix-hotkey-state.pch.h, holds only the file's own header. That way the oracle and the C tests compile the file without Wayland. In libobs-wayland.pch.h, the obs-nix-hotkey-state.h include moves to the obs-nix-wayland.c section, because that file uses the struct too.
    • The group is listed as LIBOBS_WAYLAND_PORTED_SOURCES in source-sets.json. It is added to libobs only, under ENABLE_WAYLAND, in os-linux.cmake and os-freebsd.cmake. This mirrors LIBOBS_PULSE_PORTED_SOURCES.
    • Two consumers of the list follow it: obs-build's Linux C variant, and rust/tools/ported_sources.py, which drives the Tier 2 "compiled into libobs, not into libobs-with-rust" object check. ported_sources.py gets a new --without-wayland flag, and ci/test_ported_sources.py is extended to cover it. linux-validate/validate.sh passes the flag, because its build configures ENABLE_WAYLAND=OFF.
  • Oracle (oracle/nix_hotkey_state.c): it renames the seven functions with #defines and includes the original file. It also exports the key codes, the modifier bits, and the size, alignment and field offsets of the three structs. obs_hotkeys_mutex_lock / _unlock and bmalloc stay the real symbols. Under cargo test, the hotkey test module defines the lock pair as per-thread counters. The C oracle and the Rust shim call the same pair, so the parity test can compare their lock calls.
  • Intentional difference: a null state returns (false from flush and is_pressed) without locking. C dereferences it, and no caller passes null. This is listed in the shim docs, the parity test header and the rust/README.md row. Nothing else differs.

test_wayland_hotkeys (8), test_portal_shortcuts (28) and test_wayland_multiseat compile the C into themselves and are unchanged (the multiseat test keeps hotkey_mutex_stub.c from #1515). They pass in every lane, but they cannot see the swap, so Tier 3 is the swap's proof, as with #1486 and #1513.

RED → GREEN

  • RED commit: be58806c4 libobs: Add Tier 1 tests for the Wayland hotkey key state port. The safe core has todo!() bodies. soldr cargo test -p obs-data --test obs-tests-core hotkey::nix_hotkey_state fails 17 of 17 with "not yet implemented".
    • Tier 1 (tests/hotkey/nix_hotkey_state.rs) mirrors test_wayland_hotkeys 1:1, with the same eight test names. The C test runs the real hotkey thread. Here one poll cycle is modelled on query_hotkeys / handle_binding with strict modifiers (the C test's setting): the lock is held for the whole cycle, and the four modifier keys are queried first.
    • New edge cases:
      • out-of-range keys are ignored without locking;
      • every change locks once, and free does not lock;
      • modifier keys read the modifier bits, not their own key slot;
      • the shortcut state machine: a repeated activation clears a pending release, a release stays pending until a poll has observed the entry, and an observed entry is erased at once;
      • flush order and its return value;
      • modifier-only combinations;
      • clear_keys keeps shortcuts, and free keeps keys.
    • The tests are modules of the existing core category binary, obs-tests-core. No new test binary: ci/test_binary_cap.py reports 8 of 8.
  • GREEN: 04a97a980 libobs: Port the Wayland hotkey key state to obs-hotkey. hotkey:: passes 28/28, and the whole obs-tests-core binary passes (171/171).
    • Layout: size, alignment and every field offset of obs_key_combination_t, struct obs_nix_hotkey_shortcut and struct obs_nix_hotkey_state are compared with the C header. OBS_KEY_LAST_VALUE, the four modifier key codes and the four INTERACT_*_KEY bits are compared with the oracle.
    • Tier 3 (tests/hotkey/nix_hotkey_state_parity.rs):
      • fixed sequences: the cmocka scenarios as the key state sees them (portal push-to-talk with and without modifiers, the short tap with flushes, focused keys and modifiers, focus loss, free, then a poll of every code from -1 to OBS_KEY_LAST_VALUE);
      • a proptest, 512 cases of up to 200 random events. The events are set_key, set_modifiers, clear_keys, set_shortcut, flush, is_pressed and free. Keys are drawn so they collide (the modifier keys, a few others, both edges of the valid range, any i32), and modifiers are subsets of the four bits or any u32;
      • after every step it compares the return value, the lock and unlock calls, all focused keys, the modifiers, and the shortcut darray (item count, capacity, whether the buffer is null, every item).
  • Deliberate-break check. Each break made both parity tests fail, and the core was then restored:
    • erase a deactivated shortcut even when no poll has observed it: the first mismatch is the darray count, on a SetShortcut(.., false) step;
    • drop the lock in is_pressed: the first mismatch is the lock calls, on the first IsPressed step.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON, ENABLE_WAYLAND=ON). Each tree runs the full cmocka suite against libobs (OFF) and libobs-with-rust (ON):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 329/329 329/329 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 329/329 329/329 IDENTICAL (1766)
  • No test was skipped or not run in any of the four runs. The export lists are also identical across the two lanes.
  • test_wayland_hotkeys, test_portal_shortcuts and test_wayland_multiseat pass in all four runs.
  • libobs.ported-nix-hotkey-state+.h.c is linked into libobs and not into libobs-with-rust. The libobs-with-rust symbols come from the Rust core.
  • Environment: headless (private Xvfb, private D-Bus, fresh HOME).
  • ci/local_gate.py:
    • --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, ifdef ratchet, test-binary cap): passed.
    • --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary, oracle included): passed.
    • --lane cross-release: passed.
    • --lane rust-test (soldr cargo test --workspace --locked, plus a --no-run link for x86_64-pc-windows-gnu): passed. obs-tests-core 171/171. The obs-util harness 668/668, including the export_list check that every shim is exported or hidden.
  • ci/test_build_groups.py, ci/test_naming.py, ci/test_ported_sources.py, ci/test_rust_scope.py: OK.
  • Real app: not applicable (no behaviour change).

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (characterized already by test_wayland_hotkeys, test_portal_shortcuts and test_wayland_multiseat)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

RED for the port of libobs/obs-nix-hotkey-state.c.h to the obs-hotkey
crate. The safe core (obs_hotkey::nix_hotkey_state) is generic over where
the state lives and over the hotkey lock, so the C ABI shim can run it on
the C struct and take obs->hotkeys.mutex exactly where the C does. Its
functions are todo!() stubs. obs-hotkey's build.rs now also emits
OBS_KEY_LAST_VALUE and the four modifier key codes from obs-hotkeys.h.

The first eight cases mirror test_wayland_hotkeys 1:1, with a model of
one hotkey poll cycle (query_hotkeys and handle_binding with strict
modifiers, the lock held for the cycle) in place of the real thread.
The rest pin the edges: out-of-range keys are ignored without locking,
each change locks once and free not at all, modifier keys read the
modifier bits, the shortcut state machine (repeat activation, release
pending until observed, erase once observed), flush order and its
return value, and modifier-only combinations.

soldr cargo test -p obs-data --test obs-tests-core hotkey::nix_hotkey_state
fails 17 of 17 with "not yet implemented".

Refs obs-rust#1514
libobs/obs-nix-hotkey-state.c.h moves to obs_hotkey::nix_hotkey_state.
Its seven functions (free, set_key, set_modifiers, clear_keys,
set_shortcut, flush, is_pressed) are what obs-nix-wayland.c and
obs-nix-portal-shortcuts.c call to tell the hotkey thread which keys and
GlobalShortcuts are held.

The safe core runs on any Store (the owned KeyState, or the C struct
through the shim) and takes the HotkeyLock exactly where the C takes
obs->hotkeys.mutex: once per call, none for an out-of-range key or for
free. The shim's lock is libobs's hidden obs_hotkeys_mutex_lock/unlock
(obs-rust#1515); it never reads obs. The struct and its bmalloc darray stay the
caller's: the shim borrows the fields only after the lock is taken, and
grows and erases the darray as da_push_back and da_erase do. A null
state returns (false) without locking, where C dereferences it.

The file leaves libobs.nix-wayland for its own group,
libobs.ported-nix-hotkey-state, whose PCH holds only the file's header so
the oracle and the C tests can compile it without Wayland. The group is
LIBOBS_WAYLAND_PORTED_SOURCES, added to libobs only under ENABLE_WAYLAND
(os-linux.cmake, os-freebsd.cmake), and followed by obs-build's Linux C
variant and rust/tools/ported_sources.py (new --without-wayland, which
linux-validate passes as it builds with ENABLE_WAYLAND=OFF). The seven
symbols are not EXPORT, so they go in rust-hidden.txt, rust-exports.map
and rust-unexports-macos.txt. The shims are built on every host and unused
off Wayland.

Tier 3 (oracle/nix_hotkey_state.c) compares the layout of the three
structs, the key codes and modifier bits, and random sequences of key,
modifier, focus, shortcut, flush, poll and free events: after every step
the result, the lock calls, the keys, the modifiers and the darray (count,
capacity, items). test_wayland_hotkeys, test_portal_shortcuts and
test_wayland_multiseat compile the C into themselves and are unchanged.

Fixes obs-rust#1514
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rust: Port obs-nix-hotkey-state.c to obs-hotkey

2 participants