Skip to content

libobs: Lock the hotkey mutex through a hidden accessor - #1515

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/hotkey-state-lock-accessor
Oct 20, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/hotkey-state-lock-accessor

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Base: master

Refs #1514 (PR 1 of 2: the accessor; the port follows once this lands)

What and why

obs-nix-hotkey-state.c.h locks obs->hotkeys.mutex, a field of obs-internal.h. The testing policy says a C caller that reaches into obs-internal.h gets a behaviour-neutral internal accessor in its own PR before the unit is ported. This is that PR. It has no Rust, and no public header or signature changes.

  • obs_hotkeys_mutex_lock() / obs_hotkeys_mutex_unlock() are declared in obs-internal.h, next to the other hotkey platform-client helpers (obs_hotkeys_background_clear_shortcuts, obs_hotkeys_background_status_changed), and defined in obs-hotkey.c.h beside its file-local lock() / unlock(). They are not EXPORT, so -fvisibility=hidden keeps them out of libobs.so, as with obs_hotkey_name_map_free.
  • obs-nix-hotkey-state.c.h's hotkey_state_lock() / hotkey_state_unlock() call them instead of touching the field. As before, there is no NULL check on obs (unlike lock() in obs-hotkey.c). The mutex is the same recursive one, and the lock is taken and released at the same points.
  • obs-nix-portal-shortcuts.c.h and obs-cocoa.m.h still lock the field directly. They are outside this unit and unchanged.

Tests: test_wayland_hotkeys and test_portal_shortcuts already compile obs-hotkey.c.h with obs=hotkey_test_obs, so the accessor locks their test core with no change. test_wayland_multiseat compiles obs-nix-hotkey-state.c.h without obs-hotkey.c.h, so its harness gets test/cmocka/hotkey_mutex_stub.c: the same two functions, built with the test's obs=wayland_test_obs, which lock that core's mutex as before. It is one extra source line in tests.d/test_wayland_multiseat.cmake. No test case changes.

RED → GREEN

  • No RED commit: this is a behaviour-neutral seam, and the existing tests pass on it unchanged.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON), headless (private Xvfb, private D-Bus, fresh HOME). Each runs the full cmocka suite against libobs and against libobs-with-rust:
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 329/329 329/329 IDENTICAL (1766)
Rust (ENABLE_RUST_LIBOBS=ON) 329/329 329/329 IDENTICAL (1766)

No test was skipped or not run. The export lists are also identical across the two lanes. obs_hotkeys_mutex_lock / _unlock show up in the static symbol table of all four libobs.so builds and in none of the dynamic export tables.

  • test_wayland_hotkeys, test_portal_shortcuts, test_wayland_multiseat: pass in all four runs.
  • uv run ci/local_gate.py --lane lint: ok (42 s).
  • Real app: not applicable (no behaviour change).

Checklist

  • Base is master
  • 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: not applicable (behaviour-neutral seam; the unit is covered by test_wayland_hotkeys and test_portal_shortcuts)
  • 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): not applicable (no port here)

Behavior-neutral seam before the Rust port of obs-nix-hotkey-state:
obs_hotkeys_mutex_lock() and obs_hotkeys_mutex_unlock(), declared in
obs-internal.h next to the other hotkey platform-client helpers and
defined in obs-hotkey.c, lock and unlock obs->hotkeys.mutex. They are
not EXPORT, so -fvisibility=hidden keeps them out of libobs.

obs-nix-hotkey-state.c locks through them instead of reading the
obs_core field, so the port does not need the obs_core layout. Like the
old code, they do not check obs for NULL. Nothing else changes, and the
exported symbol set stays the same.

test_wayland_hotkeys and test_portal_shortcuts already compile
obs-hotkey.c with obs=hotkey_test_obs, so the accessor locks the test
core there. test_wayland_multiseat compiles obs-nix-hotkey-state.c
without obs-hotkey.c, so its harness gains hotkey_mutex_stub.c, which
locks its test core's mutex the same way. No test case changes.

Refs obs-rust#1514
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 11:11
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit 97725e4 Oct 20, 2026
23 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 20, 2026
* libobs: Add Tier 1 tests for the Wayland hotkey key state port

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: Port the Wayland hotkey key state to obs-hotkey

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.

2 participants