Skip to content

libobs: Port the hotkey pair press latch to obs-hotkey - #1820

Merged
zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:port/hotkey-pair-rust
Oct 22, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:port/hotkey-pair-rust

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1798 (PR 2 of 2; #1799 moved the pair press latch into libobs/obs-hotkey-pair.c.h, added test_hotkey_pair_latch and test_hotkey_pair_steps, and added the hidden obs_hotkey_pair_fields() / obs_hotkey_pair_call_func() accessor)

What and why

This PR ports libobs/obs-hotkey-pair.c.h, the hotkey pair press latch, to a new pair module of rust/obs-hotkey (#444, phase 6). The latch is the two callbacks libobs registers for the halves of a hotkey pair: the frontend's Start/Stop pairs (Streaming, Recording, Replay Buffer, Virtual Camera, ...), a source's Mute/Unmute and Monitor On/Off, and a scene item's Show/Hide.

  • While one half is latched, the other half's events are dropped.
  • A press whose func returns true latches its half, and its release then clears the latch without calling the func.
  • Any other event calls the func, and a true result sets the latch to the event's pressed value.

The pair, its hash, register_hotkey_pair_internal(), the obs_hotkey_pair_* API, the funcs and their callback ABI stay C in obs-hotkey.c.h. The hybrid libobs drops the C file and links the Rust shims in its place.

  • Safe core (src/pair.rs):
    • event(latches, half, pressed, call) -> Outcome (Dropped, Cleared, or Called with the func's result).
      • call is the half's func, called at most once.
      • It gets the latches, because a C func can trigger either half from inside its own call.
    • The core reads and writes the two latches only through a small Latches trait, at the C's points:
      • before the func: the other half's latch, then this half's;
      • after the func returns: this half's latch, and only that one, and only on a true result.
    • So it never writes back a value read before the call, and never touches the other half's latch. This is the trap libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799's mutation check names. Tier 1 and Tier 3 both catch a core or a shim that writes from a copy.
    • The issue sketched the core as step(latch, ...) -> latch. It takes the latches by reference instead, so the shim can pass the pair's own latches and no copy exists to write back.
    • It keeps no state, takes no lock, and denies clippy's arithmetic_side_effects, indexing_slicing, unwrap_used, expect_used, panic, todo, unimplemented and unreachable. No host cfg, no unsafe.
  • Shim (src/ffi/pair.rs): obs_hotkey_pair_first_func and obs_hotkey_pair_second_func.
    • C signature. They keep obs_hotkey_func, because register_hotkey_pair_internal() registers them as the hotkeys' callbacks, with the pair as their data.
    • Threads and the hotkey mutex. They run with obs->hotkeys.mutex held: on the hotkey thread, in obs_hotkey_inject_event(), or in obs_hotkey_trigger_routed_callback() (the frontend's router and obs-websocket's TriggerHotkeyByName). The func runs inside them, as before. So the shims keep no state and take no lock.
    • Latches. They take the pair's latch pointers once through the hidden obs_hotkey_pair_fields() (libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799), so struct obs_hotkey_pair is not mirrored. The Latches view over those pointers reads and writes the pair itself on every access. No Rust reference to a latch exists while the func runs, so a nested callback that writes through the same pointers aliases nothing.
    • The func is called in C, through obs_hotkey_pair_call_func().
  • C side:
    • obs-hotkey-pair.c.h leaves libobs.core for its own group, libobs.ported-hotkey-pair, listed in source-sets.json (LIBOBS_PORTED_SOURCES). Its PCH, libobs-ported-hotkey-pair.pch.h, is obs-internal.h. The only change to the file is that include and its header comment.
    • test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts, test_hotkey_press_steps and test_hotkey_pair_steps still compile it into themselves, unchanged.
    • ci/test_ported_sources.py checks the group on Linux, Windows and macOS.
  • Symbols: the two callbacks are declared in obs-internal.h without EXPORT, so the shims are hidden like libobs: Port the hotkey press state machine to obs-hotkey #1786's: rust-hidden.txt, the local: list of rust-exports.map, and rust-unexports-macos.txt. nm shows them as local (t) in all three libraries. The exported symbol sets are unchanged (1766).
  • Oracle (oracle/hotkey_pair.c): the original file, unmodified, with the two callbacks renamed to oracle_*, plus the layout of struct obs_hotkey_pair_fields and the sizes of bool and obs_hotkey_id. The two seams it calls keep their names. Under cargo test the parity test defines them over fake pairs, for the oracle and the shims alike.
  • Intentional differences (listed in the parity test header, src/ffi/pair.rs and the rust/README.md row):
    • A null data, or a null latch pointer from the accessor, does nothing (C dereferences it). libobs never passes one: the data is the pair it registered.
    • Nothing else. The behaviour libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799 pins as "characterized, not endorsed" is kept as it is:
      • a release without a press reaches the func (the second half pressed while the first is latched and released after it; one key bound to both halves does this on every press);
      • a second press of a latched half calls the func again, and the half stays latched whatever the func returns;
      • both halves can end up latched (a func that triggers its partner's press from inside its own), and the pair then drops every event.
  • Docs: the rust/README.md rows, the docs/rust-port/naming.md row and the crate's Cargo description. No docs/features.md line: there is no user-visible change.

test_hotkey_pair_latch (11 cases) links OBS::libobs and goes through the public API only. In the hybrid libobs it runs the Rust shims and core through obs_hotkey_trigger_routed_callback(), so it is the end-to-end proof on every OS. test_hotkey_press_state drives a pair through the real hotkey thread on Linux.

There is no _rust variant of test_hotkey_pair_steps linking libobs_rust-static, as #1786 and #1800 found for theirs: obs-hotkey pulls in obs-data, obs-util and hidden libobs symbols that such a test does not link. So Tier 3 is the direct comparison on every OS.

RED → GREEN

  • RED commit: 0dada4276 libobs: Add Tier 1 tests for the hotkey pair press latch port. The core is todo!(). soldr cargo test -p obs-data --test obs-tests-core hotkey::pair fails 20 of 20 with "not yet implemented".
    • Tier 1 (tests/hotkey/pair.rs) ports both C characterizations 1:1, under the same case names, in the same order, with the same steps and expectations:
      • the 4 cases of test_hotkey_pair_steps (steps_*), all 32 combinations of latch state, half, press or release and func result included;
      • the 11 of test_hotkey_pair_latch (state_*).
    • A small model repeats the C around the core: the pair's latches, a func per half that logs its call and returns a set result for a press and for a release, and the trigger, which runs a half's callback synchronously, so a func can trigger its partner.
    • 5 new edge cases:
      • the order of the latch reads and writes, logged access by access, in all 32 combinations;
      • every latch state a func can leave behind, which is kept, except that a true result then sets this half's latch;
      • a dropped or a cleared event never calling the func;
      • the plain latches per half;
      • a proptest of the invariants over random sequences.
    • The tests are a module of the existing core category binary, obs-tests-core. No new test binary.
  • GREEN: c583b8584 libobs: Port the hotkey pair press latch to obs-hotkey. hotkey::pair passes 20/20.
  • Tier 3: 35f49060f libobs: Check the hotkey pair press latch port against the C (tests/hotkey/pair_parity.rs).
    • What is logged. The two seams log every call, per thread:
      • each accessor call;
      • each func call, with its pair, half, hotkey pointer and pressed value, and both latches as the func sees them;
      • each return, with the result and both latches as the func leaves them.
    • The scripted func. A func follows a script. It returns a set result. Before or after that, it may set either latch of either pair, and it may trigger either half of either pair through the same side's callbacks.
    • What that catches. A latch read after the call instead of before, a write from a copy taken before the call, a write of the other half's latch, or a write made before the func returns each changes the log or the latches. A plain bool * read cannot be logged, so the reads are compared by their effect. The order of the reads is pinned on the core by Tier 1's access log, and the shim reads only through the core.
    • What is compared. After every step, the C and Rust sides must have the same log, one to one (order and count), and the same latches in every pair.
    • Runs:
      • the layout;
      • every single event (2880 runs): 4 latch states × 2 halves × press or release × func result × what the func does while it runs. That is nothing, or either latch set to either value, before or after a nested event of either half, pressed or not, whose own func returns either result;
      • every sequence of four events (half, pressed, func result) from every latch state: 16384 runs;
      • the scripted cases of the C characterizations: a func that triggers its partner from inside its press, then every later event dropped; a func that latches the other half, or its own, while it runs; the latch per pair;
      • 2000 random runs: up to 3 pairs with random latches, and up to 40 steps. A step is an event (any hotkey pointer and id) whose funcs return random results and may set latches and trigger events of any pair, nested, or a latch change from outside.
    • Excluded: only the null pair and the null latch pointer (checked on the Rust side: no func call, no latch written).
    • hotkey:: passes 234/234 (26 of them hotkey::pair).
  • Deliberate-break check (not committed): each mutant was applied, tested and restored. 18 of 18 mutants of the core and the shim fail Tier 3: every_single_event_matches_c and random_sequences_match_c catch every one, and every_sequence_of_four_matches_c all but the four that need a func that changes a latch or triggers an event while it runs. The 13 core mutants also fail Tier 1.
    • core:
      • the other half's latch not checked;
      • this half's latch checked instead;
      • the release of a latched half calling the func;
      • that release dropped, with the latch kept;
      • the latch ignoring the func result;
      • the latch set to true, not to the pressed value;
      • a second press of a latched half dropped;
      • the other half's latch written back from a copy taken before the call;
      • this half's latch written back from a copy taken before the call;
      • this half's latch written before the func;
      • the other half's latch re-read after the func;
      • a cleared release also calling the func;
      • the half indexes swapped;
    • shim (Tier 3 only, as Tier 1 does not see the shim):
      • the second callback acting as the first half;
      • the latch pointers swapped;
      • the func given no hotkey;
      • the func given the other half's index;
      • the latches copied before the event and both written back after it.
    • Tier 2: with "the latch ignoring the func result" built into the hybrid libobs, test_hotkey_pair_latch failed press_not_acted_on and one_key_toggle (9/11) and passed 11/11 against libobs-c-reference. After the restore, 11/11 against both.
  • Local, two build trees: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON, both full builds. Full ctest -j4, headless (private Xvfb with -noreset, private D-Bus, fresh HOME):
Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs 371/371 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 373/373 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 373/373 IDENTICAL (1766)

Nothing was skipped or failed to run. After the final rebase onto bd49ef7db (#1812, CI and docs only), both trees were rebuilt, and the eleven hotkey tests, the symbol comparison, hotkey:: and the lint and cross lanes passed again.

The eleven hotkey tests pass against all three libraries: test_hotkey_pair_latch 11/11, test_hotkey_pair_steps 4/4, test_hotkey_press_state 18/18, test_hotkey_press_steps 11/11, test_hotkey_load 7/7, test_wayland_hotkeys 8/8, test_portal_shortcuts 32/32, test_portal_shortcut_sets 12/12, test_hotkey_bindings_codec 18/18, test_hotkey_translations 16/16, test_x11_hotkey_layout 4/4. The ported group is compiled only into libobs-c-reference (and the C-backend libobs).

  • Also run:
    • uv run ci/local_gate.py --lane lint passed, and --lane cross passed.
    • soldr cargo test -p obs-data --test obs-tests-core hotkey:: passes 234/234, and soldr cargo test -p obs-hotkey passes (the crate keeps its tests in obs-tests-core).
    • ci/test_build_groups.py, ci/test_ported_sources.py and ci/platform_ifdef_ratchet.py pass.
    • clang-format 22.1.3 and rustfmt are clean on the changed files.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change). test_hotkey_pair_latch drives the hybrid libobs's pairs through the same path the frontend's router and obs-websocket use.

Windows/macOS: not built there. The core and shims have no host conditional. test_hotkey_pair_latch and test_hotkey_pair_steps are built on every OS, so on Windows and macOS the swap is covered by test_hotkey_pair_latch against the hybrid libobs, by Tier 3 (the rust-tests job) and by the unchanged C tests (rust-tier2). PR CI covers those builds.

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 (N/A for docs-only; a focused CI fix may push RED and GREEN together) (the characterization merged in libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799)
  • 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): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

RED for the port of libobs/obs-hotkey-pair.c.h to a new module, pair, of
rust/obs-hotkey. The safe core is a stub (todo!()): Half (First, Second,
with other() and the C's index()), the Latches trait the latch reads and
writes the pair's two latches through, PairLatches (pressed0 and
pressed1 by value), and event(), one event of one half, which takes the
half's func as a closure called at most once with the latches, so a func
can trigger either half from inside its call, and returns the Outcome
(Dropped, Cleared or Called with the func's result).

tests/hotkey/pair.rs ports both C characterizations 1:1, under the same
case names, in the same order, with the same steps and expectations: the
4 cases of test_hotkey_pair_steps (steps_*, all 32 combinations of latch
state, half, press or release and func result included) and the 11 of
test_hotkey_pair_latch (state_*). A small model repeats the C around the
core: the pair's latches, a func per half that logs its call and returns
a set result for a press and for a release, and the trigger, which runs a
half's callback synchronously, so a func can trigger its partner.

Edge cases: the order the latches are read and written in (the other
half's latch, then this half's, before the func; only this half's, after
it); every latch state a func can leave behind, kept except that a true
result then sets this half's latch; a dropped or cleared event never
calling the func; the plain latches per half; and a proptest of the
invariants over random sequences.

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

Refs obs-rust#1798
Ports libobs/obs-hotkey-pair.c.h to the pair module of rust/obs-hotkey:
the callbacks libobs registers for the two halves of a hotkey pair, such
as Start and Stop Streaming, a source's Mute and Unmute, or a scene
item's Show and Hide. While one half is latched, the other half's events
are dropped; a press whose func returns true latches its half, and its
release then clears the latch without calling the func; any other event
calls the func, and a true result sets the latch to the event's pressed
value. The pair, its hash, register_hotkey_pair_internal(), the funcs
and their callback ABI stay C in obs-hotkey.c.h; the hybrid libobs drops
the C file and links the Rust shims in its place.

Safe core (src/pair.rs): event() reads the latches only through the
Latches trait, at the C's points: the other half's latch, then this
half's, before the func; this half's latch, and only it, after the func
returns. It never writes back a value read before the call, so a func
that triggers either half from inside its call keeps what it wrote. It
keeps no state and takes no lock, and denies clippy's panic, indexing
and arithmetic lints.

Shim (src/ffi/pair.rs): obs_hotkey_pair_first_func and
obs_hotkey_pair_second_func keep the C signature (obs_hotkey_func), as
register_hotkey_pair_internal() registers them as the hotkeys'
callbacks. They take the pair's latch pointers once through the hidden
obs_hotkey_pair_fields() (obs-rust#1799), so struct obs_hotkey_pair is not
mirrored, and read and write the latches through those pointers, never a
copy. The func is called in C, through obs_hotkey_pair_call_func().

Kept, characterized, not endorsed (obs-rust#1799): a release without a press
reaches the func; a second press of a latched half calls the func again
and the half stays latched; both halves can end up latched. Intentional
difference: a null data, or a null latch pointer from the accessor, does
nothing (C dereferences it); libobs never passes one.

C side: the file leaves libobs.core for its own group, libobs.ported-
hotkey-pair (LIBOBS_PORTED_SOURCES), whose PCH is obs-internal.h. The
two callbacks are not EXPORT, so the shims are hidden: rust-hidden.txt,
the local: list of rust-exports.map and rust-unexports-macos.txt. The
exported symbol sets are unchanged (1766). test_hotkey_load,
test_wayland_hotkeys, test_portal_shortcuts, test_hotkey_press_steps and
test_hotkey_pair_steps still compile the C file into themselves,
unchanged; test_hotkey_pair_latch sees the swap through the public API.
ci/test_ported_sources.py checks the group on every host.

Refs obs-rust#1798
Oracle (oracle/hotkey_pair.c): the original obs-hotkey-pair.c.h,
unmodified, with its two callbacks renamed to oracle_*, plus the layout
of struct obs_hotkey_pair_fields and the sizes of bool and
obs_hotkey_id. The seams it calls keep their names: under cargo test the
test binary defines obs_hotkey_pair_fields() and
obs_hotkey_pair_call_func() over fake pairs, for the oracle and the shims
alike.

Tier 3 (tests/hotkey/pair_parity.rs): the seams log every call, per
thread: each accessor call; each func call with its pair, half, hotkey,
pressed value and both latches as the func sees them; and each return,
with the result and both latches as the func leaves them. A func follows
a script: it returns a set result, and before or after that it may set
either latch of either pair and trigger either half of either pair
through the same side's callbacks, as a func that triggers its partner
does. So a latch read after the call instead of before, a write from a
copy taken before the call, a write of the other half's latch, or a
write before the func returns changes the log or the latches. After
every step the C and Rust sides must have the same log, one to one, and
the same latches in every pair. Runs:
- the layout;
- every single event: 4 latch states x 2 halves x press or release x
  func result x what the func does while it runs (nothing, or either
  latch set to either value, before or after a nested event; a nested
  event of either half, pressed or not, with either result): 2880 runs;
- every sequence of four events (half, pressed, func result) from every
  latch state: 16384 runs;
- the scripted cases of the C characterizations: a func triggering its
  partner from inside its press, a func latching the other half or its
  own while it runs, and the latch per pair;
- 2000 random runs: up to 3 pairs with random latches, up to 40 steps of
  events (any hotkey pointer and id) whose funcs return random results
  and may set latches and trigger events of any pair, nested, and latch
  changes from outside.

Excluded, the intentional difference: a null data, or a null latch
pointer from the accessor, which does nothing in Rust and is
dereferenced in C (checked on the Rust side).

Mutation check (done once, not committed): 18 of 18 mutants of the core
and the shim fail the parity tests: the other half's latch not checked;
this half's latch checked instead; the release of a latched half calling
the func; that release dropped with the latch kept; the latch ignoring
the func result; the latch set to true, not the pressed value; a second
press of a latched half dropped; the other half's latch, or this half's,
written back from a copy taken before the call; this half's latch
written before the func; the other half's latch re-read after the func;
a cleared release also calling the func; the half indexes swapped; and,
in the shim, the second callback acting as the first half, the latch
pointers swapped, the func given no hotkey or the other half's index,
and the latches copied before the event and both written back after it.
The 13 core mutants also fail Tier 1.

Refs obs-rust#1798
The rust/README.md rows for rust/obs-hotkey (crate table and port table),
the naming.md row and the crate description now include
libobs/obs-hotkey-pair.c.h. The port row lists what stays C, the seams the
shims use, the order the latches are read and written in, what the
characterization pins and the port keeps, the intentional difference (a
null data or a null accessor pointer doing nothing) and what Tier 2 and
Tier 3 compare.

Fixes obs-rust#1798
@rdoupe
rdoupe force-pushed the port/hotkey-pair-rust branch from 8d22f1e to 86ca8e3 Compare October 22, 2026 19:41
@zackees
zackees enabled auto-merge October 22, 2026 19:48
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 1751566 Oct 22, 2026
32 checks passed
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 the hotkey pair press latch in obs-hotkey.c to obs-hotkey

2 participants