Repository navigation
libobs: Move the hotkey press state machine into obs-hotkey-press.c - #1752
Merged
Merged
Conversation
zackees
force-pushed
the
refactor/hotkey-press-move
branch
from
October 22, 2026 07:42
8899f65 to
71be5d5
Compare
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
force-pushed
the
refactor/hotkey-press-move
branch
from
October 22, 2026 08:36
71be5d5 to
05cd303
Compare
zackees
enabled auto-merge
October 22, 2026 08:49
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Oct 22, 2026
This was referenced Oct 22, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.hintolibobs/obs-hotkey-press.c.h. It runs for every hotkey on every OS: on each 25 ms poll of the hotkey thread, and for everyobs_hotkey_inject_event()(the frontend while OBS has focus, and obs-websocket'sTriggerHotkeyByKeySequence). The move comes with two characterization tests, because the unit had none. There is no Rust, no behaviour change and no public header change.modifiers_match()andhandle_binding(), verbatim; the decision ofinject_hotkey(); and the modifier gathering ofquery_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 ofquery_hotkey();obs_hotkey_binding_inject(binding, combo, pressed, strict), the body ofinject_hotkey();obs_hotkeys_query_modifiers(), which queries Shift, Control, Alt and Meta.query_hotkey(),inject_hotkey()andquery_hotkeys()stay inobs-hotkey.c.hand call the entry points once per binding, so the bindings array, its walk (enum_bindings()), the lock, the hotkey thread andobs_hotkey_inject_event()are unchanged.press_released_binding()andrelease_pressed_binding()(the pressed count and the callbacks) also stay.obs-hotkey.c.hinto themselves build each.c.has its own translation unit, so the moved code cannot call thestaticfunctions that stay. It callsobs_hotkey_key_is_pressed()(the formeris_pressed(), renamed), andobs_hotkey_binding_press()andobs_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 withobs_hotkeys_store_key_translation(). All six new functions are declared inobs-internal.hwithoutEXPORT, so-fvisibility=hiddenkeeps them local. The key state is still queried at the same points, and the callbacks still run with the hotkey mutex held.libobs.coregroup just beforeobs-hotkey.c.h, withlibobs.pch.hlike every member of that group. The group, the PCH,source-sets.jsonand the CMake source lists of libobs are unchanged. The moved code has no platform conditional.obs-hotkey.c.hthemselves:test_hotkey_load,test_wayland_hotkeysandtest_portal_shortcutsnow compileobs-hotkey-press.c.hnext to it, as they doobs-hotkey-bindings.c.handobs-hotkey-translations.c.h(CMake, and the obs-build C test manifest fortest_hotkey_load). Their test files are unchanged.test_hotkey_bindings_codecandtest_hotkey_translationslink the real libobs, so they need no change.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: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 liketest_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'spressedandmodifiers_match, the hotkey's pressed count, the callbacks, and the exact keys queried. Its 10 cases pin what the real libobs cannot show:modifiers_matchlatch, 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;obs.csetsstrict_modifiers, and no API changes it);no_pressis 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:
obs_hotkey_load_bindings()accepts an empty combination; the frontend filters them and the codec drops them, so only a plugin can create one.RED → GREEN
test_hotkey_press_stateis its own commit,b56b83bd4test: Characterize the hotkey press state machine, and passes 18/18 there on the unchanged C. The move is8899f6570libobs: Move the hotkey press state machine into obs-hotkey-press.c.test_hotkey_press_statepasses 18/18 andtest_hotkey_press_steps10/10, each run 3 times against libobs with the C backend, the hybrid libobs and libobs-c-reference.obs-hotkey-press.c.hwas built into libobs and both tests:test_hotkey_press_stepssees it: in the real libobs the latch only delays a press by one poll);no_pressignored;OBS_LIBOBS_BACKEND=C, andHYBRIDwithOBS_BUILD_C_REFERENCE=ON, both full builds. Fullctest -j4, headless (private Xvfb, private D-Bus, fresh HOME):OBS_LIBOBS_BACKEND=CHYBRID,OBS_BUILD_C_REFERENCE=ONHYBRID,OBS_BUILD_C_REFERENCE=ONNothing 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) andtest_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.uv run ci/local_gate.py --lane lintpassed, and--lane crosspassed.ci/test_build_groups.pyandci/platform_ifdef_ratchet.pypass.test_hotkey_press_statealso passes through ctest inside a shared xvfb-run, on its own server. clang-format 23.1 and gersemi are clean on the changed files.Windows/macOS: not built there. The moved code has no host conditional, and the wrappers are plain C.
test_hotkey_press_stepsis built on every OS, liketest_hotkey_load, and is portable C (no cmocka range assertions).test_hotkey_press_stateis Linux only.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)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.