Skip to content

libobs: Port the hotkey press state machine to obs-hotkey - #1786

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

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1751 (PR 3 of 3; #1752 moved the press state machine into libobs/obs-hotkey-press.c.h and added test_hotkey_press_state and test_hotkey_press_steps, #1779 added the hidden obs_hotkey_binding_fields() accessor)

What and why

This PR ports libobs/obs-hotkey-press.c.h, the hotkey press state machine, to a new press module of rust/obs-hotkey (#444, phase 6). It decides when one binding is pressed and when it is released. That runs for every hotkey on every OS, push-to-talk and push-to-mute included:

  • on each 25 ms poll of the hotkey thread;
  • for every obs_hotkey_inject_event(): the frontend while OBS has focus, and obs-websocket's TriggerHotkeyByKeySequence.

The bindings array and its walk (enum_bindings()), 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, the binding's modifiers equal the held ones; not strict, they are a subset.
    • query(): one poll of one binding, the C's handle_binding().
      • An empty binding (no key, no modifiers) is never pressed by a poll.
      • A key binding needs its modifiers to match and its modifiers_match latch to be set. A modifier-only binding skips the latch and the key.
      • The key state is a closure (FnOnce), called at most once: only after those checks pass, 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 either the key is the binding's on a press, or the binding has no key. It never releases and never needs the key state.
    • query_modifiers(): Shift, Control, Alt and Meta, each queried once, in that order (MODIFIER_KEYS). Meta is the command bit.
    • The core returns an Action (None, Press, Release) and changes only the latch: it never sets the pressed flag. It keeps no state between calls and takes no lock.
    • Keys come from the OS, obs-websocket and plugins, so the core only compares them, never indexes by one. The module 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/press.rs): obs_hotkey_binding_query, obs_hotkey_binding_inject and obs_hotkeys_query_modifiers with the C signatures.
    • Threads and the hotkey mutex. The shims run with obs->hotkeys.mutex held (the hotkey thread for a poll, the caller's thread for an inject), and the press and release callbacks run inside them, with the mutex held, as before. So the shims keep no state and take no lock.
    • They take the binding's fields once through the hidden obs_hotkey_binding_fields() (libobs: Reach hotkey bindings through a hidden accessor #1779), so struct obs_hotkey_binding is not mirrored: Rust has only the #[repr(C)] field view, with a layout test against the C.
    • The key state stays lazy. obs_hotkey_key_is_pressed() is called at the point and as often as the C calls it (on X11 each call is a round trip to the X server).
    • A changed latch is written back, then obs_hotkey_binding_press() or obs_hotkey_binding_release() is called last. The binding is not touched after it, as in C, since a callback may rebind or unregister hotkeys and so move the bindings array.
  • C side: obs-hotkey-press.c.h leaves libobs.core for its own group, libobs.ported-hotkey-press, listed in source-sets.json (LIBOBS_PORTED_SOURCES). Its PCH, libobs-ported-hotkey-press.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 and test_hotkey_press_steps still compile it into themselves, unchanged. ci/test_ported_sources.py checks the group on Linux, Windows and macOS.
  • Symbols: the three functions are declared in obs-internal.h without EXPORT, so the shims are hidden like libobs: Port the hotkey binding codec to obs-hotkey #1628's: rust-hidden.txt, the local: list of rust-exports.map, and rust-unexports-macos.txt (export_list passes). nm shows them as local (t) in all three libraries. The exported symbol sets are unchanged (1766).
  • Oracle (oracle/hotkey_press.c): the original file, unmodified, with the 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 four seams it calls keep their names. Under cargo test the parity test defines them over a fake bindings array, for the oracle and the shims alike.
  • Intentional differences (the parity test header, src/ffi/press.rs and the rust/README.md row list them):
    • handle_binding()'s bool *pressed parameter is dropped. Every caller passed NULL, and it was only ever aliased to the modifier-only flag; the core branches on OBS_KEY_NONE, which is the same. handle_binding() was static, so no entry point or symbol changes: this is ABI-internal.
    • A null binding, or a null field pointer from the accessor, does nothing (C dereferences it). libobs never passes one: its bindings are elements of its array.
    • Nothing else. The behaviour libobs: Move the hotkey press state machine into obs-hotkey-press.c #1752 pins as "characterized, not endorsed" is kept as it is:
      • a key pressed before its modifiers still fires;
      • an injected event of any key presses a modifier-only binding with exactly its modifiers, a release as well as a press;
      • an empty binding is pressed by every injected event without modifiers, and released by the next poll;
      • with rerouting on and no router, a press still counts (in the C press wrapper, which stays C).
    • The non-strict branch, unreachable in libobs (obs.c always sets strict_modifiers), is kept and tested.
  • 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_press_state (18 cases) links OBS::libobs, holds keys with XTEST on its own Xvfb, and goes through the public API only. In the hybrid libobs it runs the Rust shims and core through the real hotkey thread and obs_hotkey_inject_event(), so it is the end-to-end proof on Linux.

RED → GREEN

  • RED commit: 14aa4d21f libobs: Add Tier 1 tests for the hotkey press state machine port. The core is todo!(). 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.
    • Tier 1 (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_*).
      • The C around the core stays C, so the tests repeat it in a small model: 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.
    • 12 new 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.
  • GREEN: f7e07e910 libobs: Port the hotkey press state machine to obs-hotkey. hotkey::press passes 41/41.
  • Tier 3: da0a8ff10 libobs: Check the hotkey press port against the C (tests/hotkey/press_parity.rs). The four 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 and 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, the C and Rust sides must have the same log one to one (order and count), the same bindings (key, pressed flag, latch), the same pressed counts and the same returned modifiers.
    • the layout and the values;
    • press, modifier change and release in every order: all 720 orders of Ctrl down, A down, Ctrl up, A up, Shift down and Shift up, with two polls after each, strict and not, presses on and off, with and without an inject of each event (5760 runs), 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: only the null binding (checked on the Rust side: no seam is called).
    • hotkey:: passes 176/176 (47 of them hotkey::press).
  • Deliberate-break check (not committed): 17 of 17 mutants of the core and the shim fail Tier 3 (random_sequences_match_c catches every one; every_press_order_matches_c all but Meta mapped to the Alt bit, which it never holds; characterized_sequences_match_c all but an injected release pressing); 14 of them also fail Tier 1. Each was applied, tested and restored:
    • core: 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;
    • shim (Tier 3 only, as Tier 1 does not see the shim): the latch never written back; the latch written after the press or release (caught because the log records the latch each callback sees); press and release swapped.
    • Tier 2: with no_press ignored built into the hybrid libobs, test_hotkey_press_state failed background_press_disabled (17/18) and passed 18/18 against libobs-c-reference. After the restore, 18/18 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 366/366 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 368/368 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 368/368 IDENTICAL (1766)

Nothing was skipped or failed to run. After the final rebase onto 7e110874f (#1761, frontend and CI only), both trees were rebuilt, and all eight hotkey tests and the symbol comparison passed again.

The eight hotkey tests pass against all three libraries: 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 28/28, 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 176/176, and soldr cargo test -p obs-hotkey passes (the crate keeps its tests in obs-tests-core). export_list, ci/test_build_groups.py, ci/test_ported_sources.py and ci/platform_ifdef_ratchet.py pass. clang-format 23.1 and rustfmt are clean on the changed files.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change). test_hotkey_press_state drives the real hotkey thread of the hybrid libobs.

Windows/macOS: not built there. The core and shims have no host conditional; the key state is the platform's own obs_hotkeys_platform_is_pressed(), reached through the unchanged C seam. test_hotkey_press_state is Linux only, so on Windows and macOS the swap is covered by Tier 3 (the rust-tests job) and 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 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)

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
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
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
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
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 12:33
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 010d610 Oct 22, 2026
31 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 press state machine in obs-hotkey.c to obs-hotkey

2 participants