Repository navigation
libobs: Port the hotkey press state machine to obs-hotkey - #1786
Merged
Merged
Conversation
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
This was referenced Oct 22, 2026
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.
Fixes #1751 (PR 3 of 3; #1752 moved the press state machine into
libobs/obs-hotkey-press.c.hand addedtest_hotkey_press_stateandtest_hotkey_press_steps, #1779 added the hiddenobs_hotkey_binding_fields()accessor)What and why
This PR ports
libobs/obs-hotkey-press.c.h, the hotkey press state machine, to a newpressmodule ofrust/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:obs_hotkey_inject_event(): the frontend while OBS has focus, and obs-websocket'sTriggerHotkeyByKeySequence.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 inobs-hotkey.c.h. The hybrid libobs drops the C file and links the Rust shims in its place.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'shandle_binding().modifiers_matchlatch to be set. A modifier-only binding skips the latch and the key.FnOnce), called at most once: only after those checks pass, beforeno_pressis looked at.inject(): the decision ofinject_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.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.arithmetic_side_effects,indexing_slicing,unwrap_used,expect_used,panic,todo,unimplementedandunreachable. No hostcfg, nounsafe.src/ffi/press.rs):obs_hotkey_binding_query,obs_hotkey_binding_injectandobs_hotkeys_query_modifierswith the C signatures.obs->hotkeys.mutexheld (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.obs_hotkey_binding_fields()(libobs: Reach hotkey bindings through a hidden accessor #1779), sostruct obs_hotkey_bindingis not mirrored: Rust has only the#[repr(C)]field view, with a layout test against the C.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).obs_hotkey_binding_press()orobs_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.obs-hotkey-press.c.hleaveslibobs.corefor its own group,libobs.ported-hotkey-press, listed insource-sets.json(LIBOBS_PORTED_SOURCES). Its PCH,libobs-ported-hotkey-press.pch.h, isobs-internal.h. The only change to the file is that include and its header comment.test_hotkey_load,test_wayland_hotkeys,test_portal_shortcutsandtest_hotkey_press_stepsstill compile it into themselves, unchanged.ci/test_ported_sources.pychecks the group on Linux, Windows and macOS.obs-internal.hwithoutEXPORT, so the shims are hidden like libobs: Port the hotkey binding codec to obs-hotkey #1628's:rust-hidden.txt, thelocal:list ofrust-exports.map, andrust-unexports-macos.txt(export_listpasses).nmshows them as local (t) in all three libraries. The exported symbol sets are unchanged (1766).oracle/hotkey_press.c): the original file, unmodified, with the three entry points renamed tooracle_*, plus the values it uses and the layouts ofstruct obs_hotkey_binding_fieldsandobs_key_combination_t. The four seams it calls keep their names. Undercargo testthe parity test defines them over a fake bindings array, for the oracle and the shims alike.src/ffi/press.rsand therust/README.mdrow list them):handle_binding()'sbool *pressedparameter is dropped. Every caller passedNULL, and it was only ever aliased to the modifier-only flag; the core branches onOBS_KEY_NONE, which is the same.handle_binding()wasstatic, so no entry point or symbol changes: this is ABI-internal.obs.calways setsstrict_modifiers), is kept and tested.rust/README.mdrows, thedocs/rust-port/naming.mdrow and the crate's Cargodescription. Nodocs/features.mdline: there is no user-visible change.test_hotkey_press_state(18 cases) linksOBS::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 andobs_hotkey_inject_event(), so it is the end-to-end proof on Linux.RED → GREEN
14aa4d21flibobs: Add Tier 1 tests for the hotkey press state machine port. The core istodo!().soldr cargo test -p obs-data --test obs-tests-core hotkey::pressfails 40 of 41 with "not yet implemented";modifier_keys_order_and_bits(plain data) passes.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 oftest_hotkey_press_steps(steps_*) and the 18 oftest_hotkey_press_state(state_*).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 ofquery_hotkeys(), the inject walk, and a key state that logs every query.settle()is two strict polls.no_pressafter 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.obs-tests-core. No new test binary.f7e07e910libobs: Port the hotkey press state machine to obs-hotkey.hotkey::presspasses 41/41.da0a8ff10libobs: 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.test_hotkey_press_steps;hotkey::passes 176/176 (47 of themhotkey::press).random_sequences_match_ccatches every one;every_press_order_matches_call but Meta mapped to the Alt bit, which it never holds;characterized_sequences_match_call but an injected release pressing); 14 of them also fail Tier 1. Each was applied, tested and restored:no_pressignored; 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;no_pressignored built into the hybrid libobs,test_hotkey_press_statefailedbackground_press_disabled(17/18) and passed 18/18 against libobs-c-reference. After the restore, 18/18 against both.OBS_LIBOBS_BACKEND=C, andHYBRIDwithOBS_BUILD_C_REFERENCE=ON, both full builds. Fullctest -j4, headless (private Xvfb with-noreset, 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. 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_state18/18,test_hotkey_press_steps11/11,test_hotkey_load7/7,test_wayland_hotkeys8/8,test_portal_shortcuts28/28,test_hotkey_bindings_codec18/18,test_hotkey_translations16/16,test_x11_hotkey_layout4/4. The ported group is compiled only into libobs-c-reference (and the C-backend libobs).uv run ci/local_gate.py --lane lintpassed, and--lane crosspassed.soldr cargo test -p obs-data --test obs-tests-core hotkey::passes 176/176, andsoldr cargo test -p obs-hotkeypasses (the crate keeps its tests inobs-tests-core).export_list,ci/test_build_groups.py,ci/test_ported_sources.pyandci/platform_ifdef_ratchet.pypass. clang-format 23.1 and rustfmt are clean on the changed files.test_hotkey_press_statedrives 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_stateis Linux only, so on Windows and macOS the swap is covered by Tier 3 (therust-testsjob) and the unchanged C tests (rust-tier2). PR CI covers those builds.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)