Skip to content

rust: Port the hotkey press state machine in obs-hotkey.c to obs-hotkey #1751

Closed

Description

@rdoupe

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 for TriggerHotkeyByKeySequence. 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 a modifiers_match latch: 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_NONE with modifiers) skips the latch and the key query (its pressed pointer 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 of query_hotkeys(): Shift, Control, Alt and Meta, queried in that order.

These stay C:

  • press_released_binding() and release_pressed_binding(). They set binding->pressed, count obs_hotkey.pressed (the callback runs on the first press and the last release of a hotkey's bindings), and call func, or router_func when rerouting is on.
  • is_pressed(), which is the platform's obs_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() and obs_hotkey_thread() (lock, profiler, loop), enum_bindings() and the bindings DARRAY, 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 (an obs_key_combination_t), pressed and modifiers_match. C passes in obs->hotkeys.strict_modifiers and thread_disable_press.
  • Seams into C, all hidden (declared in obs-internal.h, not EXPORT), as with obs_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) and obs_hotkey_binding_release(binding), wrapping the two C functions above;
    • obs_hotkey_binding_fields(binding), a struct of pointers to key, pressed and modifiers_match (PR 2), so Rust never mirrors struct 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) and obs_hotkeys_query_modifiers(). query_hotkey(), inject_hotkey() and query_hotkeys() in obs-hotkey.c.h call 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_hotkeys and test_portal_shortcuts compile obs-hotkey.c.h into 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 to obs_hotkeys_platform_is_pressed(), whose tables are sized OBS_KEY_LAST_VALUE.

Plan

  1. 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, and obs_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 like test_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's pressed and modifiers_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 sets strict_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 before obs-hotkey.c.h). Its calls to is_pressed(), press_released_binding() and release_pressed_binding() become calls to the three hidden wrappers; is_pressed() itself becomes obs_hotkey_key_is_pressed(). The tests that compile obs-hotkey.c.h (test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts, and the obs-build manifest entry for test_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.
  2. Accessor PR (behaviour-neutral, hidden): obs_hotkey_binding_fields() in obs-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.
  3. Port PR (RED, then GREEN):
    • obs_hotkey::press gets the safe core: modifiers_match, and one step per binding, query(state, modifiers, no_press, strict, is_pressed) -> Action and inject(state, combo, pressed, strict) -> Action, with Action one of None, Press and Release. is_pressed is a closure the step calls at most once, at the point the C calls it. src/ffi/press.rs exports 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.json entry and export fragment (rust-exports.map, rust-hidden.txt, rust-unexports-macos.txt), as libobs.ported-hotkey-translations has.
    • Tests:
      • Tier 1 ports every test_hotkey_press_steps and test_hotkey_press_state expectation 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_pressed that 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 (including OBS_KEY_NONE and 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=C and in the hybrid with libobs-c-reference, unchanged; test_hotkey_press_state sees the swap. Run it on Linux, Windows and macOS, where is_pressed differs.
      • Tier 4 does not apply (no protocol change).
    • Docs: the rust/README.md row, and the docs/rust-port/naming.md row for rust/obs-hotkey (add obs-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.h compile each .c.h as its own translation unit, so the moved file cannot call the static functions left in obs-hotkey.c.h. The press, release and key-state wrappers have to exist when the file moves, as obs_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 DARRAY and the lock across the seam, for no gain.
  • Threads and the hotkey mutex. The entry points run with obs->hotkeys.mutex (recursive) held: query on the hotkey thread, inject on whatever thread calls obs_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, and test_hotkey_press_steps and the Tier 3 call log pin when it is called.
  • The bool *pressed parameter of handle_binding(). Every caller passes NULL, and it is only ever aliased to &modifiers_only. The port drops it and branches on OBS_KEY_NONE, which is the same behaviour.
  • The non-strict branch is unreachable in libobs. obs.c sets strict_modifiers = true, and no API changes it. The port keeps the branch, because the entry points take strict as 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.

Activity

  1. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1751

    1. Characterization and extraction PR (next, ready): the press state machine moved verbatim into libobs/obs-hotkey-press.c.h behind hidden per-binding entry points, with test_hotkey_press_state (real libobs, XTEST on a private Xvfb) and test_hotkey_press_steps (scripted key state, every OS). No behaviour change.
    2. Accessor PR: obs_hotkey_binding_fields.
    3. Port to rust/obs-hotkey (module press) with oracle parity tests.
  2. added
    claimed
    on Oct 22, 2026
  3. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Port is up in #1786. Intentional differences from C, also listed in the parity test header and rust/README.md:

    1. The always-NULL bool *pressed parameter of the static handle_binding() is dropped (internal; no entry point or symbol changes).
    2. A null binding, or a null field from 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions