Skip to content

libobs: Move the hotkey press state machine into obs-hotkey-press.c - #1752

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

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:refactor/hotkey-press-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Refs #1751 (PR 1 of the plan: the characterization and the extraction. The accessor PR and the port follow.)

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 press state machine, the code that decides when one binding is pressed or released, moves from libobs/obs-hotkey.c.h into libobs/obs-hotkey-press.c.h. It runs for every hotkey on every OS: 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 move comes with two characterization tests, because the unit had none. There is no Rust, no behaviour change and no public header change.

  • What moves: modifiers_match() and handle_binding(), verbatim; the decision of inject_hotkey(); and the modifier gathering of query_hotkeys(). The only edits are the names of the three functions they call (below), and the entry points that wrap them:
    • obs_hotkey_binding_query(binding, modifiers, no_press, strict), the body of query_hotkey();
    • obs_hotkey_binding_inject(binding, combo, pressed, strict), the body of inject_hotkey();
    • obs_hotkeys_query_modifiers(), which queries Shift, Control, Alt and Meta.
  • What stays: query_hotkey(), inject_hotkey() and query_hotkeys() stay in obs-hotkey.c.h and call the entry points once per binding, so the bindings array, its walk (enum_bindings()), the lock, the hotkey thread and obs_hotkey_inject_event() are unchanged. press_released_binding() and release_pressed_binding() (the pressed count and the callbacks) also stay.
  • Why three wrappers already: the tests that compile obs-hotkey.c.h into themselves build each .c.h as its own translation unit, so the moved code cannot call the static functions that stay. It calls obs_hotkey_key_is_pressed() (the former is_pressed(), renamed), and obs_hotkey_binding_press() and obs_hotkey_binding_release(), which wrap the two functions above. libobs: Move the hotkey key-name translations into obs-hotkey-translations.c #1657 did the same with obs_hotkeys_store_key_translation(). All six new functions are declared in obs-internal.h without EXPORT, so -fvisibility=hidden keeps them local. The key state is still queried at the same points, and the callbacks still run with the hotkey mutex held.
  • 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 and test_portal_shortcuts now compile obs-hotkey-press.c.h next to it, as they do obs-hotkey-bindings.c.h and obs-hotkey-translations.c.h (CMake, and the obs-build C test manifest for test_hotkey_load). Their test files are unchanged. test_hotkey_bindings_codec and test_hotkey_translations link the real libobs, so they need no change.
  • Exported symbols: unchanged, 1766 in libobs with the C backend, in the hybrid libobs and in libobs-c-reference. The six new functions are local (t) symbols.

test_hotkey_press_state (new, its own commit before the move) links the real libobs on X11 and uses only the public API, so it also covers the hybrid libobs and will see the port. It holds keys with XTEST, so the hotkey thread polls a real key state, and runs under xvfb-run on its own X server, so no key is ever held on another display. To know that the hotkey thread has seen a key change, it presses and releases a sentinel hotkey (F12) twice and waits for each callback, then takes the hotkey mutex: at least two whole polls ran, and none is running. So there are no sleeps. While background presses are off, the sentinel is injected instead. Linux only (it needs X11 and XTEST); without a display or XTEST the cases skip. The 18 cases:

  • Polls: press and release; Ctrl+A, with Ctrl released and pressed again while A is held; strict modifiers (libobs always uses them), for a binding with modifiers and one without; A pressed before Ctrl; a modifier-only binding (Ctrl, no key).
  • Counting and order: two bindings of one hotkey and a duplicated binding (the callback runs on the first press and the last release); two hotkeys on one key (in binding order, on press and on release); rebinding and unregistering a pressed hotkey (released at once).
  • Routing and focus: callback rerouting (the router gets both, the hotkey's own callback neither), also with no router set; background presses off, the frontend's "disable hotkeys when not focused" (nothing is pressed, a pressed binding is kept until its key is released, a key held when presses come back on is pressed then); a pair (an active half blocks the other, and its release is not passed on).
  • obs_hotkey_inject_event(): an injected press presses at once on the calling thread, once, and the next poll releases it; an injected release does nothing; obs-websocket's false/true/false sequence is one press; strict modifiers; presses while background presses are off; a mouse button.

test_hotkey_press_steps (new, with the move) compiles the C into itself like test_hotkey_load, with a scripted key state that logs every query. It calls the entry points one poll at a time and checks the binding's pressed and modifiers_match, the hotkey's pressed count, the callbacks, and the exact keys queried. Its 10 cases pin what the real libobs cannot show:

  • the modifiers_match latch, poll by poll: a new binding is pressed on the second poll; a poll whose modifiers do not match clears it; a key released with the modifiers still held keeps it;
  • the non-strict branch, which libobs never uses (obs.c sets strict_modifiers, and no API changes it);
  • that the key state is queried lazily: Shift, Control, Alt and Meta, once each and in that order; then a binding's key only once its modifiers and latch match, and before no_press is looked at; never for a modifier-only or an empty binding, and never for an inject;
  • no_press, injects and the latch, and the pressed count with rerouting.

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:

  • A key pressed before its modifiers still fires. The latch makes Ctrl+A wait one poll after Ctrl first matches, but does not require Ctrl first.
  • An injected event presses a modifier-only binding whatever its key, a release as well as a press, if its modifiers are exactly the binding's.
  • An empty binding is pressed by every injected event without modifiers (by every one at all, without strict modifiers), and released by the next poll. obs_hotkey_load_bindings() accepts an empty combination; the frontend filters them and the codec drops them, so only a plugin can create one.
  • With rerouting on and no router set, a press still counts, so a router set later gets the release without a press.

RED → GREEN

  • RED commit: none. This is a move plus characterization, which passes on the moved code and on master. test_hotkey_press_state is its own commit, b56b83bd4 test: Characterize the hotkey press state machine, and passes 18/18 there on the unchanged C. The move is 8899f6570 libobs: Move the hotkey press state machine into obs-hotkey-press.c.
  • test_hotkey_press_state passes 18/18 and test_hotkey_press_steps 10/10, each run 3 times against libobs with the C backend, the hybrid libobs and libobs-c-reference.
  • Mutation check of the two tests: 13 of 13 mutants fail at least one of them. Each mutant of obs-hotkey-press.c.h was built into libobs and both tests:
    • the latch check dropped (only test_hotkey_press_steps sees it: in the real libobs the latch only delays a press by one poll);
    • strict modifiers compared as a subset;
    • the key queried before the modifier and latch checks (steps only: the query log);
    • no_press ignored;
    • the inject rule for a binding with no key dropped;
    • an injected release releasing;
    • an inject that presses not setting the latch (steps only);
    • a reset keeping the latch;
    • Shift and Control queried in the other order (steps only);
    • Meta mapped to the Alt bit (steps only);
    • an empty binding pressed by a poll;
    • the non-strict latch not forced for a binding without modifiers (steps only);
    • a modifier-only binding querying its key.
  • 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 363/363 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 365/365 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 365/365 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. After the final rebase onto 8c69615 (#1735), both trees were rebuilt, and all eight 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 and ci/platform_ifdef_ratchet.py pass. test_hotkey_press_state also passes through ctest inside a shared xvfb-run, on its own server. clang-format 23.1 and gersemi are 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 wrappers are plain C. test_hotkey_press_steps is built on every OS, like test_hotkey_load, and is portable C (no cmocka range assertions). test_hotkey_press_state is Linux only.

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 (this is the extraction with its characterization; the accessor PR and the port follow)
  • 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)

AI note: Claude Code (Claude Opus 5.5) wrote the tests, the move, the commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 07:13
@zackees
zackees force-pushed the refactor/hotkey-press-move branch from 8899f65 to 71be5d5 Compare October 22, 2026 07:42
Characterization before the hotkey press state machine (handle_binding,
inject_hotkey and the modifier gathering of query_hotkeys in
obs-hotkey.c) moves to its own file and is ported to obs-hotkey. It has
no test today: test_hotkey_load and test_wayland_hotkeys compile the C
into themselves with a key state that is never pressed.

test_hotkey_press_state links the real libobs on X11 and uses the
public API only. It holds keys with XTEST, so the hotkey thread polls a
real key state, and it runs under xvfb-run on its own X server. A
sentinel hotkey (F12) pressed and released twice proves that two whole
polls ran after a key change, so there are no sleeps. 18 cases pin:
press and release; Ctrl+A, with a modifier released and pressed again;
strict modifiers (libobs always uses them), for bindings with and
without modifiers; A pressed before Ctrl; a modifier-only binding; two
bindings of one hotkey and a duplicated binding (one callback on the
first press and on the last release); two hotkeys on one key (binding
order); rebinding and unregistering while pressed; callback rerouting,
also with no router set; background presses off (nothing pressed, a
pressed binding kept until its key is released); a pair; and
obs_hotkey_inject_event: an injected press presses at once and the next
poll releases it, an injected release does nothing, obs-websocket's
false/true/false sequence, strict modifiers, and presses while
background presses are off.

Characterized, not endorsed:
- A then Ctrl fires Ctrl+A: the modifiers_match latch only delays the
  press by one poll.
- With rerouting on and no router, a press still counts, so a router
  set later gets the release without a press.
- An injected event of any key with exactly Ctrl, a release as well as
  a press, presses a Ctrl-only binding.
- An empty binding (no key, no modifiers), which
  obs_hotkey_load_bindings accepts, is pressed by every injected event
  without modifiers.

Refs obs-rust#1751
Behavior-neutral extraction before the Rust port: the part of
obs-hotkey.c.h that decides when one binding is pressed or released
moves verbatim into libobs/obs-hotkey-press.c.h: modifiers_match(),
handle_binding(), the decision of inject_hotkey() and the modifier
gathering of query_hotkeys().

The tests that compile obs-hotkey.c.h build each file as its own
translation unit, so the moved code cannot call the static functions
that stay. So it calls three hidden wrappers in obs-hotkey.c.h instead:
obs_hotkey_key_is_pressed() (the former is_pressed()),
obs_hotkey_binding_press() and obs_hotkey_binding_release() (wrapping
press_released_binding() and release_pressed_binding(), which keep the
pressed count and the callbacks). query_hotkey(), inject_hotkey() and
query_hotkeys() stay in obs-hotkey.c.h and call the moved code per
binding through obs_hotkey_binding_query(), obs_hotkey_binding_inject()
and obs_hotkeys_query_modifiers(). All six are declared in
obs-internal.h without EXPORT. The bindings array and its walk, the lock
and the hotkey thread stay where they are, and the key state is still
queried at the same points.

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, and the obs-build manifest
entry for test_hotkey_load) now compile the new file next to it.

New test test_hotkey_press_steps compiles the C into itself like
test_hotkey_load, with a scripted key state that logs every query, and
calls the entry points one poll at a time. It pins what the real libobs
cannot show: the modifiers_match latch per poll, the non-strict branch
(libobs always sets strict_modifiers), the pressed count, and that the
key state is queried lazily: Shift, Control, Alt and Meta in that
order, then a binding's key only once its modifiers and latch match,
never for a modifier-only or empty binding, and never for an inject.
test_hotkey_press_state passes unchanged.

Refs obs-rust#1751
@zackees
zackees force-pushed the refactor/hotkey-press-move branch from 71be5d5 to 05cd303 Compare October 22, 2026 08:36
@zackees
zackees enabled auto-merge October 22, 2026 08:49
@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 1aabe3a Oct 22, 2026
27 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