Skip to content

rust: Port the hotkey pair press latch in obs-hotkey.c to obs-hotkey #1798

Closed

Description

@rdoupe

Port the hotkey pair press latch of libobs/obs-hotkey.c.h to the existing crate rust/obs-hotkey as a new module, pair. A hotkey pair (obs_hotkey_pair_register_frontend/_encoder/_output/_service/_source) is two hotkeys, each with a func that returns whether it acted. The frontend registers eight of them: Start/Stop Streaming, Start/Stop Recording, Pause/Unpause Recording, Start/Stop Replay Buffer, Start/Stop Virtual Camera, Enable/Disable Preview, Enable/Disable Studio Mode and Show/Hide Context Bar. libobs registers each half with a callback of its own, which keeps one latch per half, so that one key bound to both halves toggles and a held half blocks the other. Three paths reach these callbacks, always with the hotkey mutex held:

  • the hotkey thread, which presses and releases bindings on each 25 ms poll;
  • obs_hotkey_inject_event() (the frontend while OBS has focus, and obs-websocket's TriggerHotkeyByKeySequence);
  • obs_hotkey_trigger_routed_callback(), which the frontend calls from its router (callback rerouting is on in the frontend) and obs-websocket calls for TriggerHotkeyByName (a press, then a release).

The unit, about 25 lines:

  • obs_hotkey_pair_first_func() and obs_hotkey_pair_second_func(), the obs_hotkey_func callbacks that register_hotkey_pair_internal() registers for the two halves, with the pair as their data. For one half:
    • if the other half is latched, the event is dropped;
    • else if this half is latched and the event is a release, the latch is cleared and the func is not called;
    • else the func is called (with the half's data, the pair id and the half's hotkey), and if it returns true, the latch takes the event's pressed value.

These stay C: create_hotkey_pair(), register_hotkey_pair_internal() and the obs_hotkey_pair_register_* functions, the pair hash (obs->hotkeys.hotkey_pairs), obs_hotkey_pair_load/_save/_set_names/_set_descriptions/_unregister, and the call of the pair's func itself (func[i](data[i], pair_id, hotkey, pressed)), which goes through a hidden C wrapper.

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, the key-name translations and the hotkey press state machine (#1751). 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 accessor 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 reads and writes two fields of struct obs_hotkey_pair (obs-internal.h): pressed0 and pressed1. It calls func[i] with data[i] and pair_id.
  • Seams into C, all hidden (declared in obs-internal.h, not EXPORT), as for the press state machine (libobs: Move the hotkey press state machine into obs-hotkey-press.c #1752, libobs: Reach hotkey bindings through a hidden accessor #1779):
    • obs_hotkey_pair_fields(pair), a struct of pointers to pressed0 and pressed1, so Rust never mirrors struct obs_hotkey_pair;
    • obs_hotkey_pair_call_func(pair, half, hotkey, pressed), which calls func[half](data[half], pair_id, hotkey, pressed) and returns its result.
  • Entry points out of the unit, also hidden: obs_hotkey_pair_first_func() and obs_hotkey_pair_second_func(), which keep the obs_hotkey_func signature because they are registered as the hotkey callbacks.
  • Today only the pair case of test_hotkey_press_state (libobs: Move the hotkey press state machine into obs-hotkey-press.c #1752) touches the unit: one press and release of each half, and the second half blocked while the first is held. PR 1 adds two tests (below).
  • Input: the events come from the OS key state, from the frontend's router and from obs-websocket (TriggerHotkeyByName, TriggerHotkeyByKeySequence), but only as a hotkey id and a pressed bool, which libobs looks up first. The unit indexes nothing and allocates nothing.

Plan

  1. Characterization, extraction and accessor PR (behaviour-neutral):
    • New test test_hotkey_pair_latch, its own commit on the unchanged C. It links the real libobs and uses only the public API: a frontend pair driven by obs_hotkey_trigger_routed_callback() with callback rerouting on, as the frontend does. No key is held and the calls are synchronous, so it needs no XTEST and builds on every OS. It pins press and release of each half, a press not acted on, a release without a press, a second press of a latched half, each half blocked while the other is latched, the second half released after the first, one key on both halves as a toggle, the latch per pair, and a func that triggers its partner from inside its press. Every call is checked for the pair id and the half's own hotkey. It covers the hybrid libobs too, so it sees the port.
    • The two functions move verbatim into libobs/obs-hotkey-pair.c.h, in the same unity group (libobs.core, just before obs-hotkey.c.h), and lose static. The tests that compile obs-hotkey.c.h (test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts, test_hotkey_press_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_pair_steps, with the move. It compiles the C into itself like test_hotkey_press_steps, registers a pair, and calls the two callbacks with the latch set by hand. It pins the latch after every event, in all 32 cases (4 latch states, both latched included, × 2 halves × press or release × func true or false), that the pair registers the two callbacks with itself as their data, and that a half writes only its own latch, after its func returns.
    • The accessor and the call wrapper, as RED (a pair_fields case in test_hotkey_pair_steps that does not build) then GREEN: the moved file reaches the pair only through them.
  2. Port PR (RED, then GREEN):
    • obs_hotkey::pair gets the safe core: one step per event, step(latch, half, pressed, call) -> latch, where call is a closure that calls the func and returns its result. It is called at most once, at the point the C calls it. src/ffi/pair.rs exports the two callbacks with the C signature, and calls back only through the accessor and the wrapper. The C file moves to its own ported group, libobs.ported-hotkey-pair, 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-press has.
    • The shim must read the other half's latch, and its own, before the call, and write only its own latch after the call, through the pointer. A func can change the latch from inside (a func that triggers its partner). Writing back both values from a copy taken before the call would differ from the C. test_hotkey_pair_steps and test_hotkey_pair_latch both pin this.
    • Tests:
      • Tier 1 ports every test_hotkey_pair_steps and test_hotkey_pair_latch expectation 1:1, over the pure core.
      • Tier 3 checks parity against the moved C, exhaustively: all 4 latch states × 2 halves × press or release × func result, plus a func that sets either latch from inside its call. It compares whether the func was called, its arguments, and both latches after the step.
      • Tier 2 runs with OBS_LIBOBS_BACKEND=C and in the hybrid with libobs-c-reference, unchanged; test_hotkey_pair_latch and test_hotkey_press_state see 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-pair.c.h).

For the reviewer to decide

  • The accessor goes in PR 1, not in a PR of its own. For the press state machine the accessor was a separate PR (libobs: Reach hotkey bindings through a hidden accessor #1779), because that plan had three PRs and the field view touched how the moved C read every binding. Here the view is two bool pointers plus one call wrapper, the testing policy allows the adaptation as "a separate, behavior-neutral commit or PR", and a separate PR would only add a round of review and CI for about 20 lines. So it is its own RED and GREEN commits inside PR 1. It could be split out if wanted.
  • The func call stays C. The port could instead get func, data and pair_id through the accessor and call the function pointer from Rust. A C wrapper keeps the pair's callback ABI in C, as obs_hotkey_binding_press() and _release() do for the press state machine.
  • Threads and the hotkey mutex. The callbacks run with obs->hotkeys.mutex (recursive) held, on the hotkey thread, or on the thread that calls obs_hotkey_inject_event() or obs_hotkey_trigger_routed_callback() (the UI thread, an obs-websocket thread). The func runs synchronously inside and may call back into libobs, including the partner half. So the Rust core keeps no state between calls (it all lives in the pair) and takes no lock of its own.
  • Behaviours the characterization pins, not endorses (none is changed by this plan):
    • The second half pressed while the first is latched, and released after it: its press is dropped, its release reaches its func, which so gets a release without a press. One key bound to both halves does this on every press. The frontend's funcs ignore releases.
    • A second press of a latched half calls the func again, and the half stays latched whatever the func returns. libobs's own paths never send two presses without a release (the pressed count), but TriggerHotkeyByName and the router reach the callback directly.
    • A func that triggers its partner's press from inside its own press latches both halves, and the pair then drops every event until it is registered again. The frontend's funcs do not do this.
      None looks like a user-facing bug today. I propose no fix PR before the port. If one is wanted, it should come before PR 2, so the port copies fixed C.

Activity

  1. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1798

    1. Characterization, move and accessor PR (next, ready): the pair latch moved into libobs/obs-hotkey-pair.c.h behind hidden entry points, a hidden obs_hotkey_pair_fields() / obs_hotkey_pair_call_func() accessor, test_hotkey_pair_latch (real libobs, 11 cases) and test_hotkey_pair_steps (all 32 state × input × result combinations). No behaviour change.
    2. Port to rust/obs-hotkey (module pair) with exhaustive oracle parity.
  2. added
    claimed
    on Oct 22, 2026
  3. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Port is up in #1820. The only intentional difference from C is null handling: a null data, or a null latch pointer from obs_hotkey_pair_fields(), does nothing where C would dereference it. The three behaviours pinned as not endorsed in #1799 are kept, and each half still writes only its own latch, through the accessor pointer, after its func returns.

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