Skip to content

libobs: Move the hotkey binding codec into obs-hotkey-bindings.c - #1622

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/hotkey-bindings-move
Oct 21, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/hotkey-bindings-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Refs #1621 (PR 1 of the plan: the characterization and the extraction; the port follows)

What and why

Behaviour-neutral extraction before the Rust port, as the testing policy asks for part of a large file: the hotkey binding codec moves from libobs/obs-hotkey.c.h into libobs/obs-hotkey-bindings.c.h. The codec turns one item of a hotkey's saved bindings array into an obs_key_combination_t and back, so this is the on-disk hotkey format of every profile and scene collection. The move comes with a real-libobs characterization test, test_hotkey_bindings_codec. No Rust, no format or behaviour change, and no public header change.

  • What moves:

    • load_modifier() and save_modifier(), verbatim, still static inline;
    • the parse of load_binding(), verbatim, as obs_hotkey_combination_from_data(). It returns false where load_binding() returned without a binding;
    • the primary mouse button rule of create_binding() (ours), verbatim, as obs_hotkey_combination_bindable(). It still covers JSON and obs_hotkey_load_bindings() bindings alike;
    • the per-binding body of save_bindings_helper() as obs_hotkey_combination_to_data(). The lines are the same, with binding->key.modifiers and binding->key.key read from the combo argument.

    The three functions are declared in obs-internal.h without EXPORT. They are the entry points the port will replace.

  • Not moved: load_binding(), create_binding() (the da_push_back_new), load_bindings() (the hotkey_bindings_changed signal), save_bindings_helper() and every load and save entry point. They keep obs->hotkeys.bindings, the uthash tables and struct obs_hotkey, and now call the three functions.

  • Build: the new file joins the existing libobs.core group just before obs-hotkey.c.h (alphabetical), with libobs.pch.h like every member of that group. It needs nothing that header does not already give. No group, PCH, source-sets.json or CMake source list change. obs-build builds the same group file, so the Rust lane needs no change either. ci/test_build_groups.py passes.

  • Tests that compile obs-hotkey.c.h on their own: test_hotkey_load, test_wayland_hotkeys and test_portal_shortcuts now compile obs-hotkey-bindings.c.h next to it (CMake, and the obs-build C test manifest for test_hotkey_load). Their test files are unchanged.

  • Exported symbols: unchanged (1766 in both lanes, both libraries). The three new functions are hidden.

test_hotkey_load compiles the C into itself, so it cannot see a Rust swap. The new test links the real libobs (obs_startup(), under xvfb-run on Linux like test_missing_files) and uses only the public API: obs_hotkey_load/obs_hotkey_save on a frontend hotkey, obs_hotkey_load_bindings, obs_hotkey_pair_load/obs_hotkey_pair_save, and a source's hotkey data (obs_source_create with hotkey data, obs_hotkeys_save_source). The saved JSON is compared byte for byte, and in most cases save → load → save must give the same bytes again. The 18 cases:

  • modifiers: each alone; all 16 sets on one key; the saved key order is shift, control, alt, command, key whatever the input order; a false modifier is not written; only a JSON true sets one (a string, 1, 1.0, null, {} and [] do not); misspelt or differently cased names are dropped; API bits other than the four are dropped.
  • keys: every key name, MOUSE1 and MOUSE2 aside, in one array (each obs_key_from_name(obs_key_to_name(k)) == k, and the array is a fixpoint); unknown, wrongly cased, padded, empty, numeric, bool, null and object keys; {}; modifier-only bindings.
  • mouse buttons: MOUSE1 and MOUSE2 rejected with and without modifiers, MOUSE3 and MOUSE29 kept, for JSON and API; a load whose every item is rejected still clears the old bindings.
  • arrays: duplicates kept in load order; items that are not objects dropped by the parser; a load replaces only its own hotkey's bindings; an empty save is [] and an unknown id saves nothing; when hotkey_bindings_changed is sent.
  • pairs and sources: both halves of a pair; a source's two hotkeys saved in registration order, a name the source has no hotkey for dropped, a missing name saved as [], and a second source made from the saved data saving the same bytes.

Characterized, not endorsed (comments in the test, and in the issue):

  • load_binding() means to drop an item with no modifiers and no known key, but its check, !modifiers, tests the address of combo.modifiers, which is never NULL. So it drops nothing: {} and an unknown, empty, missing or non-string key load as a binding of OBS_KEY_NONE and are saved back as {"key":"OBS_KEY_NONE"}. Such a binding never fires (handle_binding() resets a modifier-less OBS_KEY_NONE), so the cost is a dead entry that survives every save.
  • An empty or missing array clears the hotkey's bindings but sends no hotkey_bindings_changed, because load_bindings() signals only when the array has items. obs_hotkey_load_bindings() does signal when it removed bindings, and a non-empty array signals even when every item is rejected.

Both are kept here and by the port; each gets its own PR with its own test change after the port.

RED → GREEN

  • RED commit: none. This is a move plus a characterization test, which passes on the moved code and on master. 909ae93e7 libobs: Move the hotkey binding codec into obs-hotkey-bindings.c.
  • test_hotkey_bindings_codec: 18/18, run 3 times in each lane against both libraries, all passing. It also passes, 18/18, against a libobs built from master's obs-hotkey.c.h (the libobs changes reverted, the test kept), so the move changes nothing it can see.
  • Mutation check of the new test: 10 of 10 mutants fail it, each built into libobs and run against the test. The mutants: the !modifiers check made !*modifiers (the fix of the first quirk); MOUSE1 or MOUSE2 dropped from the rule; the rule bypassed in create_binding(); command loaded as alt; control loaded under another name; shift not saved; control and alt saved in swapped order; key not saved for OBS_KEY_NONE; load_bindings() signalling for an empty array.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON, full builds). Each runs the full cmocka suite against libobs and against libobs-with-rust, headless (private Xvfb, private D-Bus, fresh HOME):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 351/351 351/351 IDENTICAL (1766)
Rust (ENABLE_RUST_LIBOBS=ON) 351/351 351/351 IDENTICAL (1766)

Nothing was skipped or failed to run. These runs are on master at #1619; a final rebase onto #1620 (the caption text port, no hotkey file) was rebuilt in both lanes, and the new test, the hotkey, portal, Wayland and caption tests, the export counts (1766) and ci/test_build_groups.py passed again on it. test_hotkey_load, test_wayland_hotkeys, test_portal_shortcuts and test_portal_shortcut_ids pass in all four, unchanged.

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

Windows/macOS: not built there. The moved code has no host conditionals. The new test is portable C (no cmocka range assertions) and uses only the public libobs API. On Linux it runs under xvfb-run when it is installed; without a display obs_startup() fails and the test prints a skip and exits 0, as test_output_caption_text does.

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 (this is the extraction with its characterization; the port follows)
  • 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 (a flag change under cmake/ or build-aux/ runs the full builds) and cross-checked with clang and MSVC (not applicable: no flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not applicable (no port here)

Behavior-neutral extraction before the Rust port: the hotkey binding
codec, which turns one item of a hotkey's saved bindings array into an
obs_key_combination_t and back, moves from libobs/obs-hotkey.c.h into
libobs/obs-hotkey-bindings.c.h. load_modifier() and save_modifier() move
verbatim and stay static inline. The parse of load_binding(), the
primary mouse button rule of create_binding() and the per-binding body of
save_bindings_helper() move verbatim into three functions, declared in
obs-internal.h without EXPORT:

- obs_hotkey_combination_from_data(): false where load_binding()
  returned without a binding;
- obs_hotkey_combination_bindable(): the MOUSE1/MOUSE2 rule, for JSON
  and API bindings alike;
- obs_hotkey_combination_to_data().

load_binding(), create_binding() and save_bindings_helper() call them
and keep the bindings array, the hotkey and the signals. The new file
joins libobs.core just before obs-hotkey.c.h (alphabetical), with
libobs.pch.h. No group, PCH or CMake source list change; the exported
symbols are unchanged. test_hotkey_load, test_wayland_hotkeys and
test_portal_shortcuts compile obs-hotkey.c.h on its own, so they now
compile the new file next to it (CMake and the obs-build manifest).

New characterization test test_hotkey_bindings_codec. test_hotkey_load
compiles the C into itself, so it cannot see a Rust swap; the new test
links the real libobs (obs_startup) and uses the public API only:
obs_hotkey_load/obs_hotkey_save, obs_hotkey_load_bindings, the pair
load and save, and a source's hotkey data (obs_source_create,
obs_hotkeys_save_source). It compares the saved JSON byte for byte and
checks that save -> load -> save gives the same bytes. It pins each
modifier alone, all 16 modifier sets and the saved key order (shift,
control, alt, command, key); that only a JSON true sets a modifier;
every key name; unknown, empty, missing and non-string keys; {} and
modifier-only bindings; MOUSE1 and MOUSE2 rejected with and without
modifiers, MOUSE3 kept; duplicates and load order; items that are not
objects; that a load replaces only its own hotkey's bindings; an empty
save; pairs; a source's hotkeys; and when hotkey_bindings_changed is
sent.

Characterized, not endorsed:
- load_binding()'s check meant to drop an item with no modifiers and no
  known key tests the address of the modifiers, which is never NULL, so
  it drops nothing: {} and an unknown, empty or missing key load as an
  OBS_KEY_NONE binding and are saved back as {"key":"OBS_KEY_NONE"};
- an empty or missing array clears the bindings without
  hotkey_bindings_changed, since load_bindings() signals only when the
  array has items.

Refs obs-rust#1621
@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 15:01
@zackees
zackees enabled auto-merge October 21, 2026 15:11
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 21, 2026
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit 553bee6 Oct 21, 2026
47 of 49 checks passed
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