Skip to content

libobs: Move the hotkey binding press count into obs-hotkey-binding-press.c - #1837

Closed
rdoupe wants to merge 4 commits into
obs-rust:masterfrom
rdoupe:refactor/hotkey-binding-press-move
Closed

rdoupe wants to merge 4 commits into
obs-rust:masterfrom
rdoupe:refactor/hotkey-binding-press-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Refs #1836 (PR 1 of the plan: the characterization, the extraction and the seam. The port follows.)

What and why

This is a behaviour-neutral extraction before the Rust port, as the testing policy asks for part of a large file. The hotkey binding press count moves from libobs/obs-hotkey.c.h into libobs/obs-hotkey-binding-press.c.h. It is what one binding press or release does to its hotkey: set the binding's pressed flag, count the presses per hotkey, and call the hotkey only on the first press and the last release, so a hotkey held through two bindings fires once. The call goes to the hotkey's func, or, with callback rerouting on, to the router (and to nothing when no router is set). The move comes with six new cases in test_hotkey_press_state and a new steps test. There is no Rust, no behaviour change and no public header change.

  • What moves: press_released_binding(), release_pressed_binding() and their wrappers obs_hotkey_binding_press() and obs_hotkey_binding_release(), verbatim. The two static functions stay static in the new file; the wrappers were already hidden (declared in obs-internal.h without EXPORT), and the ported press state machine calls them.

  • The one other change: remove_bindings() in obs-hotkey.c.h called the static release, so it now calls obs_hotkey_binding_release(), and the forward declaration goes. The tests that compile obs-hotkey.c.h build each .c.h as its own translation unit.

  • What stays: remove_bindings(), inject_hotkey(), query_hotkeys() and the binding walk, the hotkey hash, the rerouting and router setters, and the callbacks.

  • The seam, in its own RED and GREEN commits: the moved file now reaches the binding and the hotkey only through two new functions in obs-hotkey.c.h:

    • obs_hotkey_binding_count_fields(binding): pointers to the binding's pressed flag and its hotkey's pressed count, and the hotkey, so the port mirrors neither struct obs_hotkey_binding nor struct obs_hotkey;
    • obs_hotkey_dispatch(hotkey, pressed): the rerouting choice and the call (func(data, id, hotkey, pressed), or router_func(router_func_data, id, pressed), or nothing), so the callback ABI and the router (the frontend's) stay C, as obs_hotkey_pair_call_func() does for the pair latch (libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799).

    The press and the release fetch the fields once, write the flag and the count through them, and call obs_hotkey_dispatch() last: the same writes in the same order as before, nothing read or written after the call, under the same lock.

  • A new accessor, not a wider obs_hotkey_binding_fields() (libobs: Reach hotkey bindings through a hidden accessor #1779): that one gives the press state machine a const bool *pressed and no hotkey, and its Rust port takes the struct by value, so adding fields would change an ABI the hybrid already uses.

  • Hidden: both new functions are declared in obs-internal.h without EXPORT, so -fvisibility=hidden keeps them local (t) symbols. The exported symbols are unchanged.

  • Build: the new file joins the existing libobs.core group just before obs-hotkey.c.h, with libobs.pch.h. The group, the PCH, source-sets.json and the CMake source lists of libobs are unchanged. The moved code has no platform conditional.

  • Tests that compile obs-hotkey.c.h themselves: test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts, test_hotkey_press_steps and test_hotkey_pair_steps now compile obs-hotkey-binding-press.c.h next to it (CMake, and the obs-build C test manifest for test_hotkey_load). Their test files are unchanged.

  • Comments: obs-internal.h and the header comment of obs-hotkey-press.c.h placed the press and release in obs-hotkey.c; they now name the new file.

test_hotkey_press_state (#1752, real libobs on X11, public API only) already reached the unit through the poll and inject paths. Six new cases, their own commit on the unchanged C, pin the rest:

  • two bindings of one hotkey pressed and released in the same poll: one callback each way;
  • one injected event that presses both bindings of a hotkey (Ctrl+A and the modifier-only Ctrl), and the poll that releases both: one callback each way;
  • unregistering a hotkey held through two bindings: one release;
  • unregistering a pressed hotkey with rerouting on: the release goes to the router;
  • rerouting switched on, then off, while a hotkey is pressed: the callback or the router is chosen at each call, not at the press;
  • the router removed while a hotkey is pressed: its release calls nothing but still counts, so the next press reaches the router again.

test_hotkey_binding_press_steps (new, with the move) 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. Its 6 cases pin what the real libobs cannot show:

  • the flag, the count and whether the hotkey is called, after every event in all 24 cases (flag × count −2..3 × press or release), each with rerouting off, on with a router and on without one, and the arguments of each call;
  • that the flag and the count are written before the call (the callback sees them changed);
  • that nothing is read or written after the call: what a callback leaves in the binding and the hotkey stays. The port must keep this;
  • two bindings of one hotkey;
  • that rebinding and unregistering release only the pressed bindings they remove;
  • (count_fields_and_dispatch, the RED case) that the accessor points at the binding's own flag and its hotkey's own count and gives that hotkey, and that the dispatch calls the func, the router or nothing, with the right arguments, leaving the flag and the count alone.

Characterized, not endorsed (a comment in the tests, and in the issue). None is changed here, and the issue proposes no fix before the port:

  • With rerouting on, a release while no router is set is lost: the router got the press and never gets its release. The frontend reaches this only at exit, when it removes its router just before it unregisters its hotkeys.
  • Neither function checks the flag: a press of a pressed binding counts again, and a release at count 0 makes the count negative. libobs presses only a released binding and releases only a pressed one, so this is unreachable today.

RED → GREEN

  • RED commit: 26cc6dba5 test: Pin the hotkey binding fields the binding press count uses adds one case, count_fields_and_dispatch, to test_hotkey_binding_press_steps. It fails to build there, in both build trees, because obs_hotkey_binding_count_fields(), its struct and obs_hotkey_dispatch() do not exist yet (test_hotkey_binding_press_steps is the only target that fails). Before it, the six test_hotkey_press_state cases are their own commit, bf23c3db1 test: Characterize the hotkey binding press count, and pass 24/24 on the unchanged C against all three libraries. The move is 501228655 libobs: Move the hotkey binding press count into obs-hotkey-binding-press.c.
  • GREEN: 2c23e5251 libobs: Reach hotkey bindings for the press count through a hidden accessor. test_hotkey_binding_press_steps passes 6/6 and test_hotkey_press_state 24/24, against libobs with the C backend, the hybrid libobs and libobs-c-reference.
  • Equivalence with the original: a harness compiled the original two functions and wrappers (from origin/master) and the moved, seamed ones side by side and compared the call log (func or router, its arguments, and the flag and the count the call sees), the flags and the count after every step: 560,256 cases, 414,104 calls, no difference. The cases: every single event over flag × count −3..4 × press or release × the three routing states × four callbacks (none; one that changes the flag and the count; one that toggles rerouting; one that removes the router), and every sequence of six events over three bindings of one hotkey in every routing state with each callback.
  • Mutation check of the two tests: 23 of 23 mutants fail at least one of them. Each mutant of obs-hotkey-binding-press.c.h, the accessor, the dispatch or remove_bindings() was built into libobs (C backend) and both tests; test_hotkey_binding_press_steps catches all 23, and test_hotkey_press_state 18:
    • the press: the flag not set, or set false; the count pre-incremented (calls from −1); the count not incremented; a call on every press; the flag written after the call (steps only), or written again after it (steps only); a release dispatched;
    • the release: the flag not cleared; the count post-decremented (calls from 0); the count decremented after the call (steps only); a call on every release; a press dispatched;
    • the two wrappers swapped;
    • the dispatch: rerouting inverted; the func called when no router is set; the func given no data, or no hotkey (steps only); the router given no data (steps only), id 0, or always a press;
    • the accessor's flag pointing at modifiers_match;
    • remove_bindings() not releasing a pressed binding.
  • Local, two build trees: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON, both full builds. Full ctest -j4, headless (private Xvfb, private D-Bus, fresh HOME):
Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs 374/374 (see below) IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 376/376 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 376/376 IDENTICAL (1766)

Nothing was skipped or failed to run. In the C-backend run, one test outside the hotkey code failed once under -j4 load (test_pipewire_screencast_shm, case packed_rows) and passed when run again; it passes in the other two runs. test_hotkey_press_state (24/24), test_hotkey_binding_press_steps (6/6), test_hotkey_press_steps (11/11), test_hotkey_pair_steps (4/4), test_hotkey_pair_latch (11/11), test_hotkey_load (7/7), test_wayland_hotkeys (8/8), test_portal_shortcuts (32/32), test_hotkey_bindings_codec (18/18), test_hotkey_translations (16/16) and test_x11_hotkey_layout (4/4) pass against all three. After the final rebase onto 233db443a (#1826), both trees were rebuilt, and all 15 hotkey tests, ci/test_build_groups.py, ci/test_ported_sources.py and the symbol comparison passed again on all three libraries. The branch was then rebased once more onto 209d977f3 (#1830, docs only, no rebuild needed).

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. ci/test_build_groups.py, ci/test_ported_sources.py and ci/platform_ifdef_ratchet.py pass. clang-format is clean on the changed files.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

Windows/macOS: not built there. The moved code has no host conditional, and the accessor and the dispatch are plain C. test_hotkey_binding_press_steps is built on every OS, like test_hotkey_pair_steps, and is portable C (no cmocka range assertions, no fail_msg()). The new test_hotkey_press_state cases are Linux-only with the rest of that test (XTEST).

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: no port here)

Characterization before the hotkey binding press count
(press_released_binding and release_pressed_binding in obs-hotkey.c,
and their wrappers obs_hotkey_binding_press and _release) moves to its
own file and is ported to obs-hotkey. Each binding press or release
sets the binding's pressed flag and counts presses per hotkey, and the
hotkey's callback, or the router when rerouting, runs only on the first
press and the last release.

test_hotkey_press_state already reaches it through the poll and inject
paths: two bindings pressed and released one after the other, a
duplicated binding, rebinding and unregistering while pressed, and
rerouting with and without a router. Six new cases pin the rest:
- two bindings of one hotkey pressed and released in the same poll (one
  callback each way);
- one injected event that presses both bindings of a hotkey, and the
  poll that releases both (one callback each way);
- unregistering a hotkey held through two bindings (one release);
- unregistering a pressed hotkey with rerouting on (the release goes to
  the router);
- rerouting switched on or off while a hotkey is pressed (the callback
  or the router is chosen at each call, not at the press);
- the router removed while a hotkey is pressed: its release calls
  nothing but still counts, so the next press reaches the router again.

Characterized, not endorsed:
- With rerouting on, a release while no router is set is lost: the
  router got the press and never gets its release.

Refs obs-rust#1836
…ress.c

Behavior-neutral extraction before the Rust port: the hotkey binding
press count, press_released_binding() and release_pressed_binding()
with their wrappers obs_hotkey_binding_press() and
obs_hotkey_binding_release(), moves verbatim from libobs/obs-hotkey.c.h
into libobs/obs-hotkey-binding-press.c.h. The two static functions stay
static in the new file; the wrappers were already declared in
obs-internal.h without EXPORT, so -fvisibility=hidden keeps them local.
The only other change: remove_bindings() in obs-hotkey.c.h now calls
obs_hotkey_binding_release() instead of the static release, and its
forward declaration goes, because the tests that compile obs-hotkey.c.h
build each file as its own translation unit.

The new file joins libobs.core just before obs-hotkey.c.h, with
libobs.pch.h. No group, PCH or source-sets.json change, no platform
conditional moves, and the exported symbols are unchanged (1766). The
tests that compile obs-hotkey.c.h on their own (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) now compile the new file next to it. The comments in
obs-internal.h and obs-hotkey-press.c.h that placed the press and
release in obs-hotkey.c now name the new file.

New test test_hotkey_binding_press_steps 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 binding's flag and the hotkey's
count set by hand. It pins what the real libobs cannot show: the flag,
the count and the call after every event in all 24 cases (flag x count
-2..3 x press or release), each with rerouting off, on with a router
and on without one, and 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.

Characterized, not endorsed (unreachable in libobs, which presses only
a released binding and releases only a pressed one):
- neither function checks the flag, so a press of a pressed binding
  counts again;
- a release at count 0 makes the count negative.

test_hotkey_press_state passes unchanged (24/24).

Refs obs-rust#1836
Before the binding press count reaches its binding and hotkey through
an accessor, one case in test_hotkey_binding_press_steps pins it:
obs_hotkey_binding_count_fields() points at the binding's own pressed
flag and its hotkey's own pressed count, and gives the binding's
hotkey; a count set through it is the one the press and the release
read, and their writes show through it; and obs_hotkey_dispatch()
calls the hotkey's func with rerouting off, the router with rerouting
on, and nothing with rerouting on and no router, with the right
arguments for a press and a release, leaving the flag and the count
alone.

It does not build yet: neither function exists.

Refs obs-rust#1836
…cessor

The hotkey binding press count, libobs/obs-hotkey-binding-press.c.h,
writes a binding's pressed flag and its hotkey's pressed count, both in
private structs, and calls the hotkey's func or the router. The Rust
port should mirror neither struct, and the callback ABI and the router
(the frontend's) should stay C, so the press count now reaches the
binding and the hotkey only through two hidden functions in
obs-hotkey.c.h:
- obs_hotkey_binding_count_fields(binding): pointers to the binding's
  pressed flag and its hotkey's pressed count, and the hotkey;
- obs_hotkey_dispatch(hotkey, pressed): with rerouting off, calls the
  hotkey's func(data, id, hotkey, pressed); with rerouting on, calls
  the router (router_func_data, id, pressed), or nothing when no router
  is set.

A new accessor, not a wider obs_hotkey_binding_fields(): that one gives
the press state machine a const flag pointer and no hotkey, and its
Rust port takes the struct by value.

The press and the release fetch the fields once, write the flag and
the count through them, and then call obs_hotkey_dispatch() last, as
before: the same writes in the same order, nothing touched after the
call, under the same lock. Both functions are declared in
obs-internal.h without EXPORT, so the exported symbols are unchanged.

test_hotkey_binding_press_steps passes 6/6 (count_fields_and_dispatch
included) and test_hotkey_press_state 24/24.

Refs obs-rust#1836
@zackees

zackees commented Oct 25, 2026

Copy link
Copy Markdown
Member

Coordinator check-in: this draft hasn't moved in 72 h and now conflicts with master. @rdoupe, are you still on #1836? If so, a rebase onto master is the next step. If you've moved on, say so here and I'll release the claim on #1836 so someone else can pick it up. With no reply in about 48 h, I'll release it anyway; the extraction here stays in the PR to build on.

@rdoupe

rdoupe commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator Author

Still mine. It's parked on a maintainer decision about a related change outside this PR. I'll rebase it onto master and pick it up as soon as that's settled, so please leave the claim in place.

@zackees

zackees commented Oct 27, 2026

Copy link
Copy Markdown
Member

Coordinator check on this draft. Two things are now stale, both from master moving since 2026-10-22:

1. It conflicts. git merge-tree origin/master pr1837 reports:

CONFLICT (modify/delete): rust/obs-build/manifests/c-tests-linux.json
  deleted in origin/master and modified in pr1837
CONFLICT (content): test/cmocka/CMakeLists.txt
plus test/cmocka/tests.d/test_hotkey_pair_steps.cmake
     test/cmocka/tests.d/test_hotkey_press_steps.cmake

The manifest half is not a stale-pointer fix: master deleted rust/obs-build/manifests/c-tests-linux.json, so the new ctest entries have to be registered the way master registers them now, not by re-adding a line to that file. That needs your judgement on where the entries belong, so I've left the rebase to you rather than resolving it under you.

2. The only red is old. Build 🧱 / Rust Tier 2 macos-26 failed on run 39860815897 (head 2c23e525, 2026-10-22T21:53Z) with:

[  FAILED  ] remove_releases_pressed
1 FAILED TEST(S)   (test_hotkey_binding_press_steps)

No CI has run on this head since, and master has taken ~200 commits in the meantime, so that result says nothing about today.

When you rebase onto current master I'll watch the fresh run. If it is still remove_releases_pressed after the rebase, that is a real failure in the extraction and I'll say so with the log line rather than just re-running it.

@zackees

zackees commented Oct 28, 2026

Copy link
Copy Markdown
Member

Status from a worker read of this branch against master e05b92192 (2026-10-28): the PR is DIRTY — it conflicts because #1805 (build: unify the native C test graph for Cargo and CMake) landed after it and moved the hotkey tests out of test/cmocka/tests.d/*.cmake into test/cmocka/build/test-graph.json, and deleted rust/obs-build/manifests/c-tests-linux.json.

The rebase is small; only the registration shape changes. What I found resolving it locally (on a scratch copy, not pushed to your branch):

  1. rust/obs-build/manifests/c-tests-linux.json — take master's deletion; the manifest no longer exists.
  2. test/cmocka/CMakeLists.txt, test/cmocka/tests.d/test_hotkey_{pair,press}_steps.cmake — keep master's side (a one-line "Defined in ../build/test-graph.json" comment); do not re-add the add_executable blocks.
  3. Add "libobs/obs-hotkey-binding-press.c.h" to the sources of the five graph targets that compile obs-hotkey.c.h: test_hotkey_binding_press_steps (new), test_hotkey_getters_characterization (added on master after this PR — it needs the new file too), test_hotkey_load, test_hotkey_pair_steps, test_hotkey_press_steps.
  4. Define the new test in the graph instead of a tests.d fragment: a targets.test_hotkey_binding_press_steps block (sources, libraries = OBS::libobs, OBS::caption, OBS::native-test-cmocka, definitions = obs=hotkey_test_obs, includes = ["libobs"]) and a tests.test_hotkey_binding_press_steps entry (target, command: ["${target}"]), both in sorted position (test_hotkey_binding_press_steps sorts before test_hotkey_bindings_codec); then drop test/cmocka/tests.d/test_hotkey_binding_press_steps.cmake. ci/test_test_graph_sorted.py and ci/test_sorted_shared_lists.py check that order.
  5. test/cmocka/tests.d/test_wayland_hotkeys.cmake still has hand-written blocks, so its two source lists keep the added obs-hotkey-binding-press.c.h line as written (no change needed).

Refs #1836. If you would rather not rebase, say so and I can open a fresh PR from a maintainer-side branch that carries your four commits with the graph registration.

@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member

Coordinator: closing this draft as superseded, with thanks to rdoupe. What it set out to do is on master. #3032 extracted the hotkey binding press count into libobs/obs-hotkey-binding-press.c.h with its seam (#1836, PR 1), and #3075 ported it to obs-hotkey. The branch has been DIRTY since #1805, with no activity for 9 days. If anything here isn't covered by those two PRs (a test or a registration detail), reopen it or open a small follow-up and I'll review it.

@zackees zackees closed this Oct 31, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Done in obs-rust Multiview Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants