Skip to content

libobs: Move the hotkey pair press latch into obs-hotkey-pair.c - #1799

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

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #1798 (PR 1 of the plan: the characterization, the extraction and the accessor. The port follows.)

What and why

This is a behaviour-neutral extraction before the Rust port, as the testing policy asks for part of a large file. The hotkey pair press latch moves from libobs/obs-hotkey.c.h into libobs/obs-hotkey-pair.c.h. The latch is the two callbacks libobs registers for the halves of a hotkey pair, such as Start and Stop Streaming. While one half is latched, the other half's events are dropped. A press whose func returns true latches its half, and its release clears the latch without calling the func. The move comes with two characterization tests, because only one case of test_hotkey_press_state touched the unit. There is no Rust, no behaviour change and no public header change.

  • What moves: obs_hotkey_pair_first_func() and obs_hotkey_pair_second_func(), verbatim. The only change is that they lose static: register_hotkey_pair_internal() in obs-hotkey.c.h registers them, and the tests that compile obs-hotkey.c.h build each .c.h as its own translation unit.

  • What stays: create_hotkey_pair(), register_hotkey_pair_internal(), the obs_hotkey_pair_register_* functions, the pair hash and the rest of the pair API stay in obs-hotkey.c.h.

  • The accessor, in its own RED and GREEN commits: the moved file now reaches the pair only through two new functions in obs-hotkey.c.h:

    • obs_hotkey_pair_fields(pair), pointers to pressed0 and pressed1 (the latch of each half), so the port does not mirror struct obs_hotkey_pair;
    • obs_hotkey_pair_call_func(pair, half, hotkey, pressed), which calls func[half](data[half], pair_id, hotkey, pressed) and returns its result, so the pair's callback ABI stays C, as obs_hotkey_binding_press() does for the press state machine.

    Each callback fetches the fields once. The pointers alias the fields themselves, so each latch is still read and written at the same point, after the func returns, under the same lock.

  • Hidden: all four functions are declared in obs-internal.h without EXPORT, so -fvisibility=hidden keeps them local (t) symbols. The exported symbols are unchanged.

  • Build: the new file joins the existing libobs.core group just before obs-hotkey.c.h, with libobs.pch.h like every member of that group. The group, the PCH, source-sets.json and the CMake source lists of libobs are unchanged. The moved code has no platform conditional.

  • Tests that compile obs-hotkey.c.h themselves: test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts and test_hotkey_press_steps now compile obs-hotkey-pair.c.h next to it, as they do obs-hotkey-press.c.h (CMake, and the obs-build C test manifest for test_hotkey_load). Their test files are unchanged.

Why the accessor is in this PR: for the press state machine it was a PR of its own (#1779), because that plan had three PRs and the field view changed how the moved C read every binding. Here it is two bool pointers and one call wrapper. The testing policy allows the adaptation as "a separate, behavior-neutral commit or PR", so it is two commits here. They can be split out into their own PR if wanted.

test_hotkey_pair_latch (new, its own commit before the move) links the real libobs and uses only the public API, so it also covers the hybrid libobs and will see the port. It drives a frontend pair the way the frontend does: callback rerouting is on, and obs_hotkey_trigger_routed_callback() (which the frontend's router and obs-websocket's TriggerHotkeyByName call) runs a half's callback on the calling thread. No key is held and no hotkey thread timing is involved, so every call is synchronous, and the test builds and runs on every OS, like test_hotkey_bindings_codec. On Linux it runs under xvfb-run, because obs_startup needs a display there. It shares the display with the other tests, because it holds no key. Its 11 cases, each also checking that the func got the pair id and its own half's hotkey:

  • One half: press and release of each half (one call; the release clears the latch without a call); a press the func does not act on (no latch, the release is passed on, the other half is not blocked); a release without a press; a second press of a latched half.
  • Both halves: each half blocked while the other is latched, and working again after; the second half released after the first; one key on both halves used as a toggle, with funcs that act only when they apply, as the frontend's do; the latch per pair; a func that triggers its partner from inside its own press.

test_hotkey_pair_steps (new, with the move) compiles the C into itself like test_hotkey_press_steps, registers a pair through the API, and calls the two callbacks with the latch set by hand. Its 4 cases pin what the real libobs cannot show:

  • the latch after every event in all 32 cases: 4 latch states (both latched included) × 2 halves × press or release × func true or false;
  • that the pair registers the two callbacks, with itself as their data and the two halves as partners;
  • that a half writes only its own latch, after its func returns: a func that sets the other latch from inside its call leaves both set. The port must keep this; a core that writes both latches back from a copy would not;
  • (pair_fields, the RED case) that the accessor points at the pair's own latches and the call wrapper calls the right half's func with its data, the pair id and the hotkey, returns its result and leaves the latch alone.

Characterized, not endorsed (a comment in the tests, and in the issue). None is changed here, and the issue proposes no fix before the port:

  • The second half pressed while the first is latched, and released after it: its press is dropped, but its release reaches its func, which gets a release without a press. One key bound to both halves does this on every press. The frontend's funcs ignore releases.
  • A second press of a latched half calls the func again, and the half stays latched whatever the func returns. libobs's own paths never send two presses without a release, but TriggerHotkeyByName and the router reach the callback directly.
  • A func that triggers its partner's press from inside its own press latches both halves, and the pair then drops every event until it is registered again. The frontend's funcs do not do this.

RED → GREEN

  • RED commit: 020308e89 test: Pin the hotkey pair fields the pair press latch uses adds one case, pair_fields, to test_hotkey_pair_steps. It fails to build there, in both build trees, because obs_hotkey_pair_fields(), its struct and obs_hotkey_pair_call_func() do not exist yet (test_hotkey_pair_steps is the only target that fails). Before it, test_hotkey_pair_latch is its own commit, aab3783c6 test: Characterize the hotkey pair press latch, and passes 11/11 on the unchanged C. The move is 4b1e34c21 libobs: Move the hotkey pair press latch into obs-hotkey-pair.c.
  • GREEN: de16686e0 libobs: Reach hotkey pairs through a hidden accessor. test_hotkey_pair_latch passes 11/11 and test_hotkey_pair_steps 4/4, against libobs with the C backend, the hybrid libobs and libobs-c-reference.
  • Mutation check of the two tests: 15 of 15 mutants fail at least one of them. Each mutant of obs-hotkey-pair.c.h, the accessor or the call wrapper was built into libobs (C backend) and both tests:
    • the first half not checking the second's latch; the same for the second half;
    • the first half checking its own latch instead;
    • the release of a latched half calling the func;
    • the release of a latched half dropped, with the latch kept;
    • the latch ignoring the func result;
    • the latch set to true, not to the event's pressed value (steps only: the real libobs case would need a release the func acts on, on the second half);
    • a second press of a latched half dropped;
    • the other half's latch written back from a copy taken before the call;
    • the second half calling the first half's func;
    • the accessor swapping the two latches (steps only: from outside, a consistent swap behaves the same);
    • the wrapper passing the first half's data, a pair id of 0, or no hotkey;
    • the wrapper ignoring the func result.
  • 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, private D-Bus, fresh HOME):
Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs 369/369 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 371/371 (see below) IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 371/371 IDENTICAL (1766)

Nothing was skipped or failed to run. In the hybrid run, two timing tests outside the hotkey code failed once under -j4 load (test_deinterlace_2x_field_order: a field held one frame too long; test_pulse_monitor_device: the default-sink switch took longer than its 3 s wait) and passed when run again; both pass in the other two runs. 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_hotkey_bindings_codec (18/18), test_hotkey_translations (16/16) and test_x11_hotkey_layout (4/4) pass, unchanged, against all three. After the final rebase onto b6fbbceeb (#1795), both trees were rebuilt, and all ten hotkey tests and the symbol comparison passed again.

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. ci/test_build_groups.py, ci/test_ported_sources.py and ci/platform_ifdef_ratchet.py pass. clang-format 22.1.3 is clean on the changed files.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

Windows/macOS: not built there. The moved code has no host conditional, and the accessor and the wrapper are plain C. test_hotkey_pair_latch and test_hotkey_pair_steps are built on every OS, like test_hotkey_bindings_codec and test_hotkey_press_steps, and are portable C (no cmocka range assertions, no fail_msg()).

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)
  • 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) (not applicable: no port here)

@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 15:28
Characterization before the hotkey pair press latch
(obs_hotkey_pair_first_func and obs_hotkey_pair_second_func in
obs-hotkey.c) moves to its own file and is ported to obs-hotkey. Only
the pair case of test_hotkey_press_state touches it today.

test_hotkey_pair_latch links the real libobs and uses the public API
only. It drives a frontend pair the way the frontend does: callback
rerouting on, and obs_hotkey_trigger_routed_callback() runs a half's
callback on the calling thread. No key is held and no hotkey thread
timing is involved, so it is synchronous and builds on every OS. 11
cases pin: press and release of each half (one func call; the release
of a press acted on clears the latch without a call); a press not acted
on (no latch, its release is passed on); a release without a press; a
second press of a latched half; each half blocked while the other is
latched; the second half released after the first; one key on both
halves used as a toggle; the latch per pair; and a func that triggers
its partner from inside its press. Each call is checked for the pair
id and the half's own hotkey.

Characterized, not endorsed:
- The second half pressed while the first is latched and released
  after it: its press is dropped, its release reaches its func.
- A second press of a latched half calls the func again, and the half
  stays latched whatever the func returns.
- A func that triggers its partner's press from inside its own press
  latches both halves, and the pair then drops every event.

Refs obs-rust#1798
Behavior-neutral extraction before the Rust port: the two callbacks
that register_hotkey_pair_internal() registers for the halves of a
hotkey pair, obs_hotkey_pair_first_func() and
obs_hotkey_pair_second_func(), move verbatim from libobs/obs-hotkey.c.h
into libobs/obs-hotkey-pair.c.h. The only change is that they are no
longer static: obs-hotkey.c.h registers them, and the tests that
compile obs-hotkey.c.h build each file as its own translation unit.
Both are declared in obs-internal.h without EXPORT, so
-fvisibility=hidden keeps them local. create_hotkey_pair(),
register_hotkey_pair_internal() and the pair hash stay where they are.

The new file joins libobs.core just before obs-hotkey.c.h, with
libobs.pch.h. No group, PCH or source-sets.json change, no platform
conditional moves, and the exported symbols are unchanged (1766). The
tests that compile obs-hotkey.c.h on their own (test_hotkey_load,
test_wayland_hotkeys, test_portal_shortcuts, test_hotkey_press_steps,
and the obs-build manifest entry for test_hotkey_load) now compile the
new file next to it.

New test test_hotkey_pair_steps compiles the C into itself like
test_hotkey_press_steps, registers a pair, and calls the two callbacks
with the latch set by hand. It pins what the real libobs cannot show:
the latch after every event in all 32 cases (4 latch states, both
latched included, x 2 halves x press or release x func true or false),
that the pair registers the two callbacks with itself as their data,
and that a half writes only its own latch, after its func returns.
test_hotkey_pair_latch passes unchanged.

Refs obs-rust#1798
Before the pair press latch reaches its pair through an accessor, one
case in test_hotkey_pair_steps pins it: obs_hotkey_pair_fields() points
at the pair's own pressed0 and pressed1, a latch set through it is the
one the callbacks read, and one set by a callback shows through it; and
obs_hotkey_pair_call_func() calls one half's func with its data, the
pair id and the hotkey, returns its result and leaves the latch alone.

It does not build yet: neither function exists.

Refs obs-rust#1798
The hotkey pair press latch, libobs/obs-hotkey-pair.c.h, reads and
writes two fields of struct obs_hotkey_pair, which is private, and
calls the pair's func with its data and id. The Rust port should not
mirror that struct, so the latch now reaches the pair only through two
hidden functions in obs-hotkey.c.h:
- obs_hotkey_pair_fields(pair): pointers to pressed0 and pressed1, the
  latch of each half;
- obs_hotkey_pair_call_func(pair, half, hotkey, pressed): calls
  func[half](data[half], pair_id, hotkey, pressed) and returns its
  result.

Each callback fetches the fields once. The pointers alias the fields
themselves, so each latch is still read and written at the same point,
after the func returns, under the same lock. Both are declared in
obs-internal.h without EXPORT, so the exported symbols are unchanged.

test_hotkey_pair_steps passes 4/4 (pair_fields included) and
test_hotkey_pair_latch 11/11.

Refs obs-rust#1798
@rdoupe
rdoupe force-pushed the refactor/hotkey-pair-move branch from fcb5cf8 to de16686 Compare October 22, 2026 15:49
@zackees
zackees enabled auto-merge October 22, 2026 16:12
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 22, 2026
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 8b2676d Oct 22, 2026
32 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 22, 2026
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
github-merge-queue Bot pushed a commit that referenced this pull request Oct 22, 2026
* libobs: Add Tier 1 tests for the hotkey pair press latch port

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 #1798

* libobs: Port the hotkey pair press latch to obs-hotkey

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() (#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 (#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 #1798

* libobs: Check the hotkey pair press latch port against the C

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 #1798

* docs: Record the hotkey pair press latch port

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 #1798
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