Repository navigation
Description
Port the hotkey press state machine of libobs/obs-hotkey.c.h to the existing crate rust/obs-hotkey as a new module, press. It decides when one binding is pressed and when it is released, so it runs for every hotkey on every OS, including push-to-talk and push-to-mute through source hotkeys. Two paths reach it:
- the hotkey thread (
obs_hotkey_thread), which polls the platform key state every 25 ms and walks every binding (query_hotkeys); obs_hotkey_inject_event(), which the frontend calls for key events while OBS has focus, and obs-websocket forTriggerHotkeyByKeySequence. It only ever presses. The next poll releases.
The unit, about 90 lines:
modifiers_match(): strict (the binding's modifiers equal the held ones) or not (they are a subset).handle_binding(): one poll of one binding. It has amodifiers_matchlatch: a key binding is pressed only on a poll where its modifiers match and also matched on the poll before. The key state is queried only after the modifier and latch checks pass. A modifier-only binding (OBS_KEY_NONEwith modifiers) skips the latch and the key query (itspressedpointer is aliased to&modifiers_only). An empty binding (no key, no modifiers) is never pressed by a poll.no_press(background presses off) blocks presses but not releases.- The decision in
inject_hotkey(): the modifiers match, and the key is the binding's (or the binding has no key), and the event is a press (or the binding has no key). Then it sets the latch and presses. It never releases. query_hotkey(), and the modifier gathering ofquery_hotkeys(): Shift, Control, Alt and Meta, queried in that order.
These stay C:
press_released_binding()andrelease_pressed_binding(). They setbinding->pressed, countobs_hotkey.pressed(the callback runs on the first press and the last release of a hotkey's bindings), and callfunc, orrouter_funcwhen rerouting is on.is_pressed(), which is the platform'sobs_hotkeys_platform_is_pressed()(X11 asks the X server on every call; Wayland reads the ported key state; Windows and macOS their own).obs_hotkey_inject_event()andobs_hotkey_thread()(lock, profiler, loop),enum_bindings()and the bindingsDARRAY, and the pair functions.
Phase and position
Phase 6 (#444), libobs core through opaque seams. It joins rust/obs-hotkey, which already holds the key name map, the Wayland key state, the portal shortcut id codec, the binding codec and the key-name translations (#1656). It is part of a large file, so it goes in through function extraction (testing-policy.md, "Extracting a function before porting it"), then an accessor PR, as #1546 (#1550) and #1594 (#1599) did, then the port. Its Rust tests join the crate's existing tests/hotkey/ binary. No new test binary.
Dependencies and existing tests
- It reads and writes three fields of
struct obs_hotkey_binding(obs-internal.h):key(anobs_key_combination_t),pressedandmodifiers_match. C passes inobs->hotkeys.strict_modifiersandthread_disable_press. - Seams into C, all hidden (declared in
obs-internal.h, notEXPORT), as withobs_hotkeys_mutex_lock()(libobs: Lock the hotkey mutex through a hidden accessor #1515):obs_hotkey_key_is_pressed(key), the platform key state;obs_hotkey_binding_press(binding)andobs_hotkey_binding_release(binding), wrapping the two C functions above;obs_hotkey_binding_fields(binding), a struct of pointers tokey,pressedandmodifiers_match(PR 2), so Rust never mirrorsstruct obs_hotkey_binding.
- Entry points out of the unit, also hidden:
obs_hotkey_binding_query(binding, modifiers, no_press, strict),obs_hotkey_binding_inject(binding, combo, pressed, strict)andobs_hotkeys_query_modifiers().query_hotkey(),inject_hotkey()andquery_hotkeys()inobs-hotkey.c.hcall them per binding, so the bindings array and its walk stay C. - Today there is no test of the unit.
test_hotkey_load,test_wayland_hotkeysandtest_portal_shortcutscompileobs-hotkey.c.hinto themselves with a key state that is never pressed (or the Wayland key state), and never check a press, a release or a callback order. PR 1 adds two tests (below). - Input: key events come from the OS (X server, compositor or portal, Windows, macOS) and from obs-websocket (
TriggerHotkeyByKeySequence, which builds the combination from a key name and four bools). Bindings come from profiles and scene collections through the binding codec (rust: Port the hotkey binding load/save codec in obs-hotkey.c to obs-hotkey #1621), which only produces known key names and the four modifier bits. The unit itself indexes nothing; the key goes on toobs_hotkeys_platform_is_pressed(), whose tables are sizedOBS_KEY_LAST_VALUE.
Plan
- Characterization and extraction PR (behaviour-neutral, two commits: the real-libobs test on the unchanged C, then the move):
- New test
test_hotkey_press_state. It links the real libobs on X11 and uses only the public API. It holds keys with XTEST, so the hotkey thread polls a real key state, under xvfb-run on its own X server. A sentinel hotkey pressed and released twice proves that two whole polls ran after a key change, so it needs no sleeps. It pins press and release, a modifier combination, strict modifiers, a key pressed before its modifier, a modifier-only binding, two bindings of one hotkey and a duplicated binding (the pressed count), two hotkeys on one key (binding order), rebinding and unregistering while pressed, callback rerouting with and without a router, background presses off, a pair, andobs_hotkey_inject_event()(press, release, obs-websocket's false/true/false, strict modifiers, presses while background presses are off). It covers the hybrid libobs too, so it sees the port. - New test
test_hotkey_press_steps, which 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 pins the binding'spressedandmodifiers_match, the hotkey's pressed count, the callbacks, and the exact keys queried and their order. It covers what the real libobs cannot show: the latch per poll, the non-strict branch (libobs always setsstrict_modifiers), and that the key state is queried lazily. - The unit moves verbatim into
libobs/obs-hotkey-press.c.h, in the same unity group (libobs.core, just beforeobs-hotkey.c.h). Its calls tois_pressed(),press_released_binding()andrelease_pressed_binding()become calls to the three hidden wrappers;is_pressed()itself becomesobs_hotkey_key_is_pressed(). The tests that compileobs-hotkey.c.h(test_hotkey_load,test_wayland_hotkeys,test_portal_shortcuts, and the obs-build manifest entry fortest_hotkey_load) compile the new file next to it, as libobs: Move the hotkey binding codec into obs-hotkey-bindings.c #1622 and libobs: Move the hotkey key-name translations into obs-hotkey-translations.c #1657 did. No platform conditional moves.
- New test
- Accessor PR (behaviour-neutral, hidden):
obs_hotkey_binding_fields()inobs-hotkey.c.h, and the moved file reads and writes the three fields only through it. No Rust. The exported symbol set is unchanged, and both tests pass unchanged. - Port PR (RED, then GREEN):
obs_hotkey::pressgets the safe core:modifiers_match, and one step per binding,query(state, modifiers, no_press, strict, is_pressed) -> Actionandinject(state, combo, pressed, strict) -> Action, withActionone of None, Press and Release.is_pressedis a closure the step calls at most once, at the point the C calls it.src/ffi/press.rsexports the three entry points and calls back only through the wrappers. The C file moves to its own ported group,libobs.ported-hotkey-press, with its own.pch.h,source-sets.jsonentry and export fragment (rust-exports.map,rust-hidden.txt,rust-unexports-macos.txt), aslibobs.ported-hotkey-translationshas.- Tests:
- Tier 1 ports every
test_hotkey_press_stepsandtest_hotkey_press_stateexpectation 1:1, over the pure core. - Tier 3 checks parity against the moved C, through an oracle over a fake binding array, a scripted
is_pressedthat logs its calls, and counted press and release stubs. The call log (order and count) must match, not only the result. A proptest covers scripted sequences: the four modifiers, keys (includingOBS_KEY_NONEand the mouse buttons), strict on and off,no_press, empty and modifier-only bindings, several bindings of one hotkey, and interleaved polls and injects, including press, modifier change and release in every order. - Tier 2 runs with
OBS_LIBOBS_BACKEND=Cand in the hybrid withlibobs-c-reference, unchanged;test_hotkey_press_statesees the swap. Run it on Linux, Windows and macOS, whereis_presseddiffers. - Tier 4 does not apply (no protocol change).
- Tier 1 ports every
- Docs: the
rust/README.mdrow, and thedocs/rust-port/naming.mdrow forrust/obs-hotkey(addobs-hotkey-press.c.h).
For the reviewer to decide
- Why PR 1 already has three of the four seams. The tests that compile
obs-hotkey.c.hcompile each.c.has its own translation unit, so the moved file cannot call thestaticfunctions left inobs-hotkey.c.h. The press, release and key-state wrappers have to exist when the file moves, asobs_hotkeys_store_key_translation()did in libobs: Move the hotkey key-name translations into obs-hotkey-translations.c #1657. That leaves PR 2 with the field view only. It could be folded into PR 1 or into the port instead. I kept it separate because it is the only change in the plan that touches how the moved C reads the binding, and it can be checked on its own. - Seam granularity. Rust gets one call per binding, and C keeps the bindings array and its walk. Rust owning the whole loop would need the
DARRAYand the lock across the seam, for no gain. - Threads and the hotkey mutex. The entry points run with
obs->hotkeys.mutex(recursive) held:queryon the hotkey thread,injecton whatever thread callsobs_hotkey_inject_event()(the UI thread, an obs-websocket thread). The press and release callbacks run synchronously inside, with the mutex held, as today, and may call back into libobs. So the Rust core keeps no state between calls (it all lives in the binding) and takes no lock of its own. - The key state stays lazy.
obs_hotkeys_platform_is_pressed()is a server round trip on X11, so the port must not precompute it or call it more often. The core takes it as a callback, andtest_hotkey_press_stepsand the Tier 3 call log pin when it is called. - The
bool *pressedparameter ofhandle_binding(). Every caller passes NULL, and it is only ever aliased to&modifiers_only. The port drops it and branches onOBS_KEY_NONE, which is the same behaviour. - The non-strict branch is unreachable in libobs.
obs.csetsstrict_modifiers = true, and no API changes it. The port keeps the branch, because the entry points takestrictas a parameter and Tier 3 can check it. Removing it would be a separate change. - Behaviours the characterization pins, not endorses (none is changed by this plan):
- A key pressed before its modifiers still fires: the latch delays the press by one poll but does not enforce the order.
- An injected event of any key with exactly a modifier-only binding's modifiers presses it, a release as well as a press.
- An empty binding (no key, no modifiers) is pressed by every injected event without modifiers, and released by the next poll.
obs_hotkey_load_bindings()accepts it, but the frontend filters empty combinations 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.
None looks like a user-facing bug today. I propose no fix PR before the port, unlike rust: Port the hotkey key-name translations in obs-hotkey.c to obs-hotkey #1656. If one is wanted, it should come before PR 3, so the port copies fixed C.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 22, 2026
Claiming: #1751
libobs/obs-hotkey-press.c.hbehind hidden per-binding entry points, withtest_hotkey_press_state(real libobs, XTEST on a private Xvfb) andtest_hotkey_press_steps(scripted key state, every OS). No behaviour change.obs_hotkey_binding_fields.rust/obs-hotkey(modulepress) with oracle parity tests.test: Characterize the hotkey press state machine
libobs: Move the hotkey press state machine into obs-hotkey-press.c
test: Characterize the hotkey press state machine
libobs: Move the hotkey press state machine into obs-hotkey-press.c
libobs: Move the hotkey press state machine into obs-hotkey-press.c (o…
libobs: Reach hotkey bindings through a hidden accessor (obs-rust#1779)
rdoupe commentedon Oct 22, 2026
Port is up in #1786. Intentional differences from C, also listed in the parity test header and rust/README.md:
bool *pressedparameter of the statichandle_binding()is dropped (internal; no entry point or symbol changes).obs_hotkey_binding_fields(), does nothing instead of being dereferenced. libobs never passes one.The four "not endorsed" behaviours pinned in #1752 are kept as they are.
libobs: Port the hotkey press state machine to obs-hotkey (obs-rust#1786