Repository navigation
libobs: Reach hotkey bindings through a hidden accessor - #1779
Merged
zackees merged 2 commits intoOct 22, 2026
Merged
Conversation
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
6 of 7 tasks
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 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.
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.hsince #1752, reads and writes three fields ofstruct 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 andobs_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 inobs-internal.h, next to the other press state machine functions, and defined inobs-hotkey.c.h, next to the press and release wrappers. It returns one plainstruct obs_hotkey_binding_fieldsof pointers into the binding:key, the binding'sobs_key_combination_t(const: the state machine only reads it);pressed(const: the state machine only reads it, andobs_hotkey_binding_press()andobs_hotkey_binding_release()set it);modifiers_match, the latch, which the state machine reads and writes.obs-hotkey-press.c.hnow reaches a binding only through it and the two wrappers. It has nobinding->left. Each entry point fetches the struct once and passes it tomodifiers_match(). The pointers alias the fields themselves, so every field is still read and written at the same point, under the same lock.EXPORT, so-fvisibility=hiddenkeeps it out oflibobs.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, sorust-hidden.txt,rust-exports.mapandrust-unexports-macos.txtneed no entries.Design notes, against the plan in #1751:
hotkeyandhotkey_idare 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 leftcontext.signalsout in the same way.keyis one pointer to the publicobs_key_combination_t(obs-hotkey.h), not two pointers tokey.keyandkey.modifiers. Its layout is public and stable, so the port can use it as it is.constwhere the state machine only reads. So the port can only write the latch, which is the one field the state machine writes itself.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.hinto themselves (test_hotkey_load,test_wayland_hotkeys,test_portal_shortcutsandtest_hotkey_press_steps) also compileobs-hotkey.c.h, so they get the accessor with it.RED → GREEN
bf0d78afatest: Pin the hotkey binding fields the press state machine usesadds one case,binding_fields, totest_hotkey_press_steps. It fails to build there, in both build trees, becauseobs_hotkey_binding_fields()and its struct do not exist yet (test_hotkey_press_stepsis the only target that fails). The case pins:32d653dd7libobs: Reach hotkey bindings through a hidden accessor.test_hotkey_press_stepspasses 11/11 andtest_hotkey_press_state18/18, against libobs with the C backend, the hybrid libobs and libobs-c-reference. The other 10 cases oftest_hotkey_press_stepsand all oftest_hotkey_press_stateare unchanged.test_hotkey_press_steps:binding_fieldssees this one);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.obs_hotkey_binding_fieldsis a local (t) symbol in all three libraries, and in none of the dynamic export tables.uv run ci/local_gate.py --lane lintpassed, and--lane crosspassed.ci/test_build_groups.py,ci/platform_ifdef_ratchet.pyandci/test_cmocka_windows_diagnostics.pypass. clang-format 23.1 is clean on the changed files.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 oftest_hotkey_press_steps.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)