Skip to content

libobs: Reach hotkey bindings through a hidden accessor - #1779

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:refactor/hotkey-binding-accessor
Oct 22, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:refactor/hotkey-binding-accessor

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Refs #1751 (PR 2 of the plan: the accessor. The port follows.)

What and why

The hotkey press state machine, libobs/obs-hotkey-press.c.h since #1752, reads and writes three fields of struct obs_hotkey_binding, which is private. The Rust port should not mirror that struct, so the testing policy calls for a behaviour-neutral internal accessor in its own PR first, as #1550 and #1599 did for the output delay and the interleaver. This is that PR. It is thin: #1752 already added the press and release wrappers and obs_hotkey_key_is_pressed(), so only the field view is left. There is no Rust, and no public header or signature change.

  • obs_hotkey_binding_fields(binding) is declared in obs-internal.h, next to the other press state machine functions, and defined in obs-hotkey.c.h, next to the press and release wrappers. It returns one plain struct obs_hotkey_binding_fields of pointers into the binding:
    • key, the binding's obs_key_combination_t (const: the state machine only reads it);
    • pressed (const: the state machine only reads it, and obs_hotkey_binding_press() and obs_hotkey_binding_release() set it);
    • modifiers_match, the latch, which the state machine reads and writes.
  • obs-hotkey-press.c.h now reaches a binding only through it and the two wrappers. It has no binding-> left. Each entry point fetches the struct once and passes it to modifiers_match(). The pointers alias the fields themselves, so every field is still read and written at the same point, under the same lock.
  • Hidden: it is not EXPORT, so -fvisibility=hidden keeps it out of libobs.so, like the six functions of libobs: Move the hotkey press state machine into obs-hotkey-press.c #1752. It is C, not a Rust shim, so rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt need no entries.

Design notes, against the plan in #1751:

  • hotkey and hotkey_id are left out. Only the press and release wrappers use the binding's hotkey, and they stay C. libobs: Read the output delay fields through a hidden accessor #1550 left context.signals out in the same way.
  • key is one pointer to the public obs_key_combination_t (obs-hotkey.h), not two pointers to key.key and key.modifiers. Its layout is public and stable, so the port can use it as it is.
  • const where the state machine only reads. So the port can only write the latch, which is the one field the state machine writes itself.
  • One struct rather than per-field accessors: one call per entry point, and one repr(C) struct on the Rust side, as in libobs: Read the output delay fields through a hidden accessor #1550.

Tests: no test build changes. The tests that compile obs-hotkey-press.c.h into themselves (test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts and test_hotkey_press_steps) also compile obs-hotkey.c.h, so they get the accessor with it.

RED → GREEN

  • RED commit: bf0d78afa test: Pin the hotkey binding fields the press state machine uses adds one case, binding_fields, to test_hotkey_press_steps. It fails to build there, in both build trees, because obs_hotkey_binding_fields() and its struct do not exist yet (test_hotkey_press_steps is the only target that fails). The case pins:
    • the pointers, for each of two bindings of one hotkey: at the binding's own key combination, pressed flag and latch, with the bound values;
    • that a latch set through the accessor is the one the next poll reads: Ctrl+A, held, is pressed on the first poll instead of the second;
    • that a release by a poll and a press by an inject show through it.
  • GREEN: 32d653dd7 libobs: Reach hotkey bindings through a hidden accessor. test_hotkey_press_steps passes 11/11 and test_hotkey_press_state 18/18, against libobs with the C backend, the hybrid libobs and libobs-c-reference. The other 10 cases of test_hotkey_press_steps and all of test_hotkey_press_state are unchanged.
  • Mutation check of the new case: 4 of 4 mutants fail it. Each was built into test_hotkey_press_steps:
    • the accessor pointing the latch at a copy;
    • the accessor pointing the pressed flag at a snapshot (only binding_fields sees this one);
    • the accessor pointing the key at the first binding's;
    • an inject that presses clearing the latch instead of setting it.
  • 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 365/365 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 367/367 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 367/367 IDENTICAL (1766)

Nothing was skipped or failed to run. test_hotkey_load (7/7), test_wayland_hotkeys (8/8), test_portal_shortcuts (28/28), test_hotkey_bindings_codec (18/18), test_hotkey_translations (16/16) and test_x11_hotkey_layout (4/4) pass, unchanged, against all three. obs_hotkey_binding_fields is a local (t) symbol in all three libraries, and in none of the dynamic export tables.

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. ci/test_build_groups.py, ci/platform_ifdef_ratchet.py and ci/test_cmocka_windows_diagnostics.py pass. clang-format 23.1 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 accessor is plain C with no host conditional, and the new case uses no cmocka range assertions and no fail_msg(). PR CI covers the Windows and macOS build of test_hotkey_press_steps.

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 press state machine into obs-hotkey-press.c #1752)
  • 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)

The Rust port of the hotkey press state machine (obs-hotkey-press.c)
must read and write a binding without mirroring struct
obs_hotkey_binding, which is private. The accessor PR gives it
obs_hotkey_binding_fields(), which returns pointers into one binding.

A new case in test_hotkey_press_steps pins what it returns: pointers
at the binding's own key combination, pressed flag and modifiers_match
latch, for each binding of a hotkey; a latch set through it is the one
the next poll reads (Ctrl+A is pressed on the first poll, not the
second); and a press or release by a poll or an inject shows through
it.

It fails to build until the accessor exists.

Refs obs-rust#1751
The Rust port of the hotkey press state machine (obs-hotkey-press.c)
should not mirror struct obs_hotkey_binding, which is private. As for
the output delay and interleaver (obs_output_delay_fields(),
obs_output_interleave_fields()), it gets a behaviour-neutral accessor
in its own change first.

obs_hotkey_binding_fields(), defined in obs-hotkey.c next to the press
and release wrappers and declared in obs-internal.h, returns one plain
struct obs_hotkey_binding_fields of pointers into the binding: the key
combination and the pressed flag, both const (the state machine only
reads them; obs_hotkey_binding_press() and obs_hotkey_binding_release()
set the flag), and the modifiers_match latch, which it reads and
writes. It is not EXPORT, so -fvisibility=hidden keeps it out of
libobs.so like the other hidden hotkey functions.

obs-hotkey-press.c now reaches a binding only through it and the
press and release wrappers. Each entry point fetches the struct once
and passes it to modifiers_match(). The pointers alias the fields, so
every field is still read and written at the same point, under the
same lock. The binding's hotkey and hotkey_id are left out: only the
press and release wrappers, which stay C, use them.

Refs obs-rust#1751
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 10:50
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit aa4229b Oct 22, 2026
26 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 22, 2026
)

* libobs: Add Tier 1 tests for the hotkey press state machine port

RED for the port of libobs/obs-hotkey-press.c.h to a new module, press,
of rust/obs-hotkey. The safe core is a stub (todo!()):
modifiers_match(), strict or as a subset; query(), one poll of one
binding, which may change its modifiers_match latch and returns the
Action (None, Press or Release) the caller takes, with the key state as
a closure called at most once; inject(), one injected event, which never
releases; and query_modifiers(), Shift, Control, Alt and Meta in that
order (MODIFIER_KEYS). Binding holds the three fields the C reaches
through obs_hotkey_binding_fields(); the core never sets the pressed
flag, which stays with the C press and release wrappers.

tests/hotkey/press.rs ports both C characterizations 1:1, under the same
case names, in the same order, with the same steps and expectations:
the 11 cases of test_hotkey_press_steps (steps_*) and the 18 of
test_hotkey_press_state (state_*). A small model repeats the C around
the core: the bindings array in binding order, obs_hotkey_load_bindings()
(pressed bindings released, then the bindable combinations appended),
unregister, the pressed count and callbacks with rerouting, the pair
trampolines, the poll of query_hotkeys(), the inject walk, and a key
state that logs every query. settle() is two strict polls.

Edge cases: the modifier table; every modifier queried even when the
earlier ones are held; strict and subset matching over all 32 bits; any
key passed to the key state as it is (negative, out of range, mouse
buttons); the reset path's latch; no_press after the key query; a
pressed and held binding left alone; the non-strict latch forcing; a
modifier-only binding ignoring its latch; an empty binding released but
never pressed by a poll; the inject rules; and two proptests of the
invariants of a poll and an inject.

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::press fails
40 of 41 with "not yet implemented"; modifier_keys_order_and_bits (plain
data) passes.

Refs obs-rust#1751

* libobs: Port the hotkey press state machine to obs-hotkey

Ports libobs/obs-hotkey-press.c.h to the press module of
rust/obs-hotkey. It decides when one binding is pressed and when it is
released, for every hotkey on every OS, push-to-talk and push-to-mute
included: on each 25 ms poll of the hotkey thread, and for every
obs_hotkey_inject_event() (the frontend while OBS has focus, and
obs-websocket's TriggerHotkeyByKeySequence). The bindings array and its
walk, the lock, the hotkey thread, obs_hotkey_inject_event(), the
pressed count and the callbacks 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/press.rs): modifiers_match(), strict or as a subset;
query(), one poll of one binding, the C's handle_binding(): an empty
binding is never pressed by a poll; a key binding needs its modifiers
to match and its latch to be set; a modifier-only binding skips the
latch and the key; the key state is a closure called at most once,
only after those checks pass and before no_press is looked at; a reset
sets the latch to whether the modifiers match and releases a pressed
binding; not strict, a binding without modifiers has its latch forced
first. inject(), the decision of inject_hotkey(): it presses, and sets
the latch, when the modifiers match and the key is the binding's on a
press or the binding has no key; it never releases. query_modifiers():
Shift, Control, Alt and Meta, each queried once, in that order. The
core returns an Action (None, Press, Release), changes only the latch,
keeps no state between calls and takes no lock. Keys come from the OS,
obs-websocket and plugins, so it only compares them, never indexes by
one, and denies clippy's panic, indexing and arithmetic lints.

Shim (src/ffi/press.rs): obs_hotkey_binding_query,
obs_hotkey_binding_inject and obs_hotkeys_query_modifiers with the C
signatures. They run with the hotkey mutex held and the callbacks run
inside them, as before. They take the binding's fields once through
the hidden obs_hotkey_binding_fields() (obs-rust#1779), so struct
obs_hotkey_binding is not mirrored; query the key state through
obs_hotkey_key_is_pressed() at the C's point; write a changed latch
back; then call obs_hotkey_binding_press() or _release() last, and do
not touch the binding after it, since a callback may rebind or
unregister hotkeys.

Intentional differences: handle_binding()'s bool *pressed parameter,
always NULL and only aliased to the modifier-only flag, is dropped; it
was static, so no entry point changes. A null binding, or a null
accessor field, does nothing (C dereferences it); libobs never passes
one. Behaviour the characterization pins, not endorses (obs-rust#1752), is
kept: a key pressed before its modifiers still fires, an injected event
of any key presses a modifier-only binding with exactly its modifiers,
and an empty binding is pressed by every injected event without
modifiers. The non-strict branch, unreachable in libobs, is kept.

C side: the file leaves libobs.core for its own group,
libobs.ported-hotkey-press (LIBOBS_PORTED_SOURCES), whose PCH is
obs-internal.h. The three functions 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
and test_hotkey_press_steps still compile the C file into themselves,
unchanged; test_hotkey_press_state sees the swap through the public
API. ci/test_ported_sources.py checks the group on every host.

Refs obs-rust#1751

* libobs: Check the hotkey press port against the C

Oracle (oracle/hotkey_press.c): the original obs-hotkey-press.c.h,
unmodified, with its three entry points renamed to oracle_*, plus the
values it uses and the layouts of struct obs_hotkey_binding_fields and
obs_key_combination_t. The seams it calls keep their names: under cargo
test the test binary defines obs_hotkey_binding_fields(),
obs_hotkey_key_is_pressed(), obs_hotkey_binding_press() and
obs_hotkey_binding_release() over a fake bindings array, for the oracle
and the shims alike.

Tier 3 (tests/hotkey/press_parity.rs): the seams log every call, per
thread: each accessor call, each key queried, each press and release
with the latch the callback would see, and each hotkey callback (first
press, last release, with the pressed count as libobs keeps it). The
key state answers from a script, then from the held keys, so a query
at another point, or one more or one less, changes the answers as well
as the log. After every step of a run the C and Rust sides must have
the same log, one to one, the same bindings (key, pressed flag, latch),
the same pressed counts and the same returned modifiers. Runs:
- the layout and the values;
- every order of Ctrl down, A down, Ctrl up, A up, Shift down and Shift
  up (720), with two polls after each, strict and not, presses on and
  off, with and without an inject of each event, over the bindings of
  the characterization tests (Ctrl+A, A, Ctrl alone, an empty binding,
  Ctrl+Shift+A, two bindings of one hotkey with the middle mouse button,
  a duplicate, two hotkeys on one key);
- the scripted steps of test_hotkey_press_steps;
- 2000 random runs: up to 8 bindings over 3 hotkeys with random
  modifiers (sometimes any bits), keys (none, the modifiers, letters,
  the mouse buttons, any code in range, any int) and starting latches;
  up to 40 steps of polls, polls with given modifiers, injects, modifier
  queries and latch changes from outside, with random held keys and
  key-state scripts.

Excluded, the intentional difference: a null binding, which does
nothing in Rust and is dereferenced in C (checked on the Rust side).

Mutation check (done once, not committed): 17 of 17 mutants of the core
and the shim fail the parity tests: the latch check dropped, strict
compared as a subset, the key queried before the modifier and latch
checks, no_press ignored, the inject rule for a binding with no key
dropped, an injected release pressing, an inject not setting the latch,
a reset keeping the latch, Shift and Control queried in the other
order, Meta mapped to the Alt bit, an empty binding pressed by a poll,
the non-strict latch not forced, a modifier-only binding querying its
key, the modifier query stopping at the first held modifier, the latch
never written back, the latch written after the press or release, and
press and release swapped.

Refs obs-rust#1751

* docs: Record the hotkey press state machine 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-press.c.h. The port row lists what stays C, the seams
the shims use, what the characterization pins and the port keeps, the
intentional differences (the dropped always-NULL bool *pressed of the
static handle_binding(), and a null binding doing nothing) and what
Tier 2 and Tier 3 compare.

Fixes obs-rust#1751
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