Skip to content

rust: Port the hotkey binding press count in obs-hotkey.c to obs-hotkey #1836

Closed

Description

@rdoupe

Port the hotkey binding press count of libobs/obs-hotkey.c.h to the existing crate rust/obs-hotkey as a new module, binding_press. A hotkey can have several bindings (key combinations), and each binding is pressed and released on its own: by the hotkey thread on each 25 ms poll, by obs_hotkey_inject_event() (the frontend while OBS has focus, and obs-websocket's TriggerHotkeyByKeySequence), and by remove_bindings() when a pressed binding is rebound or its hotkey unregistered. The press count turns those binding events into hotkey events: the hotkey's callback runs on the first press and the last release only, so a hotkey held through two bindings fires once.

The unit, about 25 lines:

  • press_released_binding(): sets the binding's pressed flag, increments the hotkey's pressed count (an int), and calls the hotkey only if the count was 0.
  • release_pressed_binding(): clears the flag, decrements the count, and calls the hotkey only if the count is now 0.
  • The call: the hotkey's func(data, id, hotkey, pressed) when callback rerouting is off; the router (router_func(router_func_data, id, pressed)) when it is on; nothing when it is on and no router is set.
  • The wrappers obs_hotkey_binding_press() and _release(), which the ported press state machine calls (obs-hotkey-press.c.h).

These stay C: remove_bindings() (which calls the release), inject_hotkey(), query_hotkeys() and the binding walk, the hotkey hash, the router and rerouting setters, and the callback call itself, which goes through a hidden C function.

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 ids and sets, the binding codec, the key-name translations, the hotkey press state machine (#1751) and the pair press latch (#1798). It is part of a large file, so it goes in through function extraction (testing-policy.md, "Extracting a function before porting it"), then the port. Two PRs; the seam goes into PR 1 (below). Its Rust tests join the crate's existing tests/hotkey/ binary. No new test binary.

Dependencies and existing tests

  • It writes struct obs_hotkey_binding's pressed (obs-internal.h), reads its hotkey, and reads and writes struct obs_hotkey's pressed count. It reads obs->hotkeys.reroute_hotkeys, router_func and router_func_data only to choose the call.
  • Seams into C, both hidden (declared in obs-internal.h, not EXPORT), as for the press state machine (libobs: Reach hotkey bindings through a hidden accessor #1779) and the pair latch (libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799):
    • obs_hotkey_binding_count_fields(binding), a struct of pointers to the binding's flag and its hotkey's count, plus the hotkey, so Rust never mirrors struct obs_hotkey_binding or struct obs_hotkey;
    • obs_hotkey_dispatch(hotkey, pressed), which makes the rerouting choice and the call, so the callback ABI and the router (the frontend's) stay C.
  • Entry points out of the unit, also hidden: obs_hotkey_binding_press() and obs_hotkey_binding_release(), unchanged.
  • Existing tests: test_hotkey_press_state (18 cases, real libobs, libobs: Move the hotkey press state machine into obs-hotkey-press.c #1752) reaches the unit through the poll and inject paths: two bindings pressed one after the other, a duplicated binding, rebinding and unregistering while pressed, rerouting with and without a router. test_hotkey_press_steps checks the pressed count after each poll. PR 1 adds cases for what is missing (below).
  • Input: local only. The events come from the OS key state, from the frontend and from obs-websocket, but only as presses and releases of bindings libobs already holds. The unit indexes nothing and allocates nothing.

Plan

  1. Characterization, extraction and seam PR (behaviour-neutral):
    • Six new cases in test_hotkey_press_state, their own commit on the unchanged C: two bindings of one hotkey pressed and released in the same poll; one injected event that presses both bindings of a hotkey; unregistering a hotkey held through two bindings; unregistering a pressed hotkey with rerouting on; rerouting switched on or off while a hotkey is pressed; the router removed while a hotkey is pressed.
    • The four functions move verbatim into libobs/obs-hotkey-binding-press.c.h, in the same unity group (libobs.core, just before obs-hotkey.c.h). remove_bindings() now calls obs_hotkey_binding_release() (the static release is no longer in its file). The tests that compile obs-hotkey.c.h (test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts, test_hotkey_press_steps, test_hotkey_pair_steps, and the obs-build manifest entry for test_hotkey_load) compile the new file next to it. No platform conditional moves.
    • New test test_hotkey_binding_press_steps, with the move. It compiles the C into itself like test_hotkey_pair_steps, binds a hotkey through the API, and calls the press and the release with the flag and the count set by hand. It pins the flag, the count and the call after every event in all 24 cases (flag × count −2..3 × press or release), each in the three routing states, the arguments of each call, that the flag and the count are written before the call and nothing after it, two bindings of one hotkey, and remove_bindings() releasing only the pressed bindings it removes.
    • The accessor and the dispatch, as RED (a count_fields_and_dispatch case in test_hotkey_binding_press_steps that does not build) then GREEN: the moved file reaches the binding and the hotkey only through them.
  2. Port PR (RED, then GREEN):
    • obs_hotkey::binding_press gets the safe core: fn press(flag: &mut bool, count: &mut i32) -> bool and fn release(...) -> bool ("call now"). src/ffi/binding_press.rs exports obs_hotkey_binding_press() and _release(), takes the pointers once through the accessor, writes the flag and the count through them, and then, if the core says so, calls obs_hotkey_dispatch() last. The C file moves to its own ported group, libobs.ported-hotkey-binding-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-pair has.
    • The count must use C's int arithmetic (wrapping_add/wrapping_sub on i32 to stay defined at the edges, which libobs never reaches), and the shim must not read or write the binding or the hotkey after the dispatch. A callback can change both from inside its call; test_hotkey_binding_press_steps pins this.
    • Tests:
      • Tier 1 ports every test_hotkey_binding_press_steps expectation 1:1, over the pure core.
      • Tier 3 checks parity against the moved C, exhaustively: flag × count (−2..3) × press or release × the three routing states, plus sequences over two and three bindings of one hotkey and a callback that changes the flag and the count from inside. It compares the dispatch log (hotkey, pressed, and the flag and the count the call sees) and the flag and the count after every step.
      • Tier 2 runs with OBS_LIBOBS_BACKEND=C and in the hybrid with libobs-c-reference, unchanged; test_hotkey_press_state sees the swap.
      • 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-binding-press.c.h).

For the reviewer to decide

  • The seam goes in PR 1, not in a PR of its own, as for the pair latch (libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799): one accessor and one dispatch function, in their own RED and GREEN commits. They can be split out if wanted.
  • A new accessor, not a wider obs_hotkey_binding_fields(). The press state machine's accessor (libobs: Reach hotkey bindings through a hidden accessor #1779) gives const bool *pressed and no hotkey, and the ported press state machine mirrors that struct and gets it by value, so widening it would change the ABI the Rust side already uses. The new obs_hotkey_binding_count_fields() gives a writable flag pointer, the count and the hotkey. Both point at the same pressed flag.
  • The rerouting choice and the call stay C, in obs_hotkey_dispatch(). The port could read the rerouting flag and the router through an accessor and call the function pointers from Rust; a C function keeps the callback ABI and the router (the frontend's) in C, as obs_hotkey_pair_call_func() does for the pair.
  • Threads and the hotkey mutex. Every caller holds obs->hotkeys.mutex (recursive): the hotkey thread, the thread calling obs_hotkey_inject_event(), and any thread rebinding or unregistering. The callback runs synchronously inside and may call back into libobs. So the Rust core keeps no state (it all lives in the binding and the hotkey) and takes no lock.
  • Behaviours the characterization pins, not endorses (none is changed by this plan):
    • With rerouting on, a release while no router is set is lost: the router got the press and never gets its release (the count still drops, so the next press reaches the router again). The frontend reaches this only at exit: it removes its router just before it unregisters its hotkeys, so a hotkey held then gets no release, which no longer matters there.
    • Neither function checks the flag: a press of a pressed binding counts again, and a release at count 0 makes it negative. libobs presses only a released binding and releases only a pressed one, so this is unreachable today.
      None looks like a user-facing bug today. I propose no fix PR before the port.

Activity

  1. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1836

    1. Characterization, move and seam PR (next, ready): the press count moved into libobs/obs-hotkey-binding-press.c.h, a hidden obs_hotkey_binding_count_fields() accessor and obs_hotkey_dispatch() (reroute choice and call stay in C), 6 new cases in test_hotkey_press_state and test_hotkey_binding_press_steps. No behaviour change.
    2. Port to rust/obs-hotkey (module binding_press) with exhaustive oracle parity.
  2. added
    claimed
    and removed
    claimed
    on Oct 22, 2026
  3. added
    P3
    size:M
    on Oct 25, 2026
  4. zackees commented on Oct 25, 2026

    @zackees
    Member

    /ready

    Triage: a scoped port (the binding press count into rust/obs-hotkey as binding_press), claimed by @rdoupe with the characterization/seam PR #1837 open. The usual port acceptance: RED → GREEN, Tier 2 against the hybrid libobs, and a Tier 3 differential test of the press/release/rebind sequences against the C. P3, size:M.

  5. added
    ready
    on Oct 25, 2026
  6. zackees commented on Oct 30, 2026

    @zackees
    Member

    Claiming: Port the hotkey binding press count (obs-hotkey) — PR 1 of the two-PR plan in the issue.

    Plan:

    1. PR 1 (this tick, behavior-neutral C only): six new cases in test_hotkey_press_state, move the four functions verbatim into libobs/obs-hotkey-binding-press.c.h in the libobs.core unity group, new test_hotkey_binding_press_steps (24 flag × count events in the three routing states, plus the accessor and dispatch as RED then GREEN), and the seam declarations in obs-internal.h.
    2. PR 2 (next): the port of that file to obs_hotkey::binding_press with Tier 1 and Tier 3 tests, the libobs.ported-hotkey-binding-press group and the export fragment.

    First PR: this tick.

  7. zackees commented on Oct 30, 2026

    @zackees
    Member

    Progress: PR 1 (characterization + extraction + seam) is #3032, now queued to merge.

    • test_hotkey_binding_press_steps (5 cases) pins the flag, the count and the call after every event in all three routing states, the arguments of each call, that both fields are written before the call, two bindings of one hotkey, and remove_bindings() releasing only the pressed bindings it removes.
    • The four functions moved verbatim into libobs/obs-hotkey-binding-press.c.h (libobs.core, with libobs.pch.h; no group/PCH/source-sets.json change).
    • The seam is in: obs_hotkey_binding_count_fields() and obs_hotkey_dispatch() in obs-hotkey.c.h, declared without EXPORT.
    • PR CI green (CI Gate ✅); Tier 2 Linux 629 tests, the 20 hotkey/Wayland/portal tests pass.

    PR 2 (the port to obs_hotkey::binding_press with Tier 1 + Tier 3 and the libobs.ported-hotkey-binding-press group) follows once #3032 lands, since it needs the moved file on master.

  8. moved this from Ready to In progress in obs-rust Multiviewon Oct 30, 2026
  9. zackees commented on Oct 30, 2026

    @zackees
    Member

    Progress: PR 2 (the port itself) is #3075, on port/1836-binding-press.

    • Safe core obs_hotkey::binding_press (press, release over the flag and the count), the hidden shims obs_hotkey_binding_press/_release through obs_hotkey_binding_count_fields() and obs_hotkey_dispatch(), and the C file moved to libobs.ported-hotkey-binding-press.
    • Tier 1 ports the 5 cases of test_hotkey_binding_press_steps 1:1 (12 tests pass). Tier 3 compares the shims with oracle/hotkey_binding_press.c on fake bindings (6 tests pass): the whole table, two bindings of one hotkey, sequences from a pressed state, the null differences and random runs.
    • Because the wrappers are Rust now, the press state machine's oracle calls oracle_seam_* stand-ins, and press_parity drives them through the ported shim, so both sides still log the same calls.
    • Watching CI; the local Tier 2 lane for test_hotkey_binding_press_steps, test_hotkey_press_steps, test_hotkey_pair_steps and test_hotkey_press_state is running.
  10. moved this from In progress to In review in obs-rust Multiviewon Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3readysize:M

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions