Skip to content

libobs: Port the hotkey binding codec to obs-hotkey - #1628

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-hotkey-bindings
Oct 21, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-hotkey-bindings

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1621 (PR 2 of 2; PR 1, #1622, moved the codec into libobs/obs-hotkey-bindings.c.h and added test_hotkey_bindings_codec)

What and why

This PR ports libobs/obs-hotkey-bindings.c.h, the hotkey binding codec, to a new bindings module of rust/obs-hotkey (#444). The codec turns one item of a hotkey's saved bindings array into an obs_key_combination_t and back. The item holds the "shift", "control", "alt" and "command" bools and the "key" name. It is the on-disk hotkey format of every profile and scene collection, so the port has to be byte-identical: a different key name, modifier key or key order would lose or rewrite users' bindings. load_binding(), create_binding() and save_bindings_helper() in obs-hotkey.c.h stay C. They call the codec and keep the bindings array and the hotkey_bindings_changed signal. libobs-with-rust drops the C file and links the Rust shims in its place.

  • Safe core (src/bindings.rs), which never sees an obs_data:
    • combo_from_fields() is the parse of load_binding(). It takes the four bools obs_data read (in MODIFIERS order: shift, control, alt, command) and the key name. It sets the INTERACT_*_KEY bit of each true modifier (load_modifier) and looks the key up by name (the ported name map).
    • combo_bindable() is the rule of create_binding(), for JSON and API bindings alike. It rejects OBS_KEY_MOUSE1 and OBS_KEY_MOUSE2 and nothing else.
    • combo_to_fields() is the per-binding body of save_bindings_helper(). It yields one true per set modifier bit (save_modifier), in the order shift, control, alt, command, and then the key.
    • Kept, as test_hotkey_bindings_codec marks it "characterized, not endorsed": no item is dropped. The C check tests the address of the modifiers, which is never NULL. So {} and an unknown, empty or missing key load as OBS_KEY_NONE and are saved back as {"key":"OBS_KEY_NONE"}. The other kept behaviour, that an empty array sends no hotkey_bindings_changed, is in load_bindings(), which stays C, so the port does not touch it.
    • The input comes from scene collections and profiles, which can come from others. So the module denies clippy's indexing_slicing, arithmetic_side_effects, unwrap_used, expect_used, panic, todo, unimplemented and unreachable. I checked that the lints fire: a probe that indexes, adds and unwraps inside the module fails clippy with all four. No host cfg, no unsafe.
  • Shim (src/ffi/bindings.rs): obs_hotkey_combination_from_data, obs_hotkey_combination_bindable and obs_hotkey_combination_to_data, with the C signatures.
    • They make the C's obs_data calls in the C's order: obs_data_get_bool for each modifier and obs_data_get_string for the key; obs_data_set_bool(name, true) for each set modifier and obs_data_set_string of obs_key_to_name for the key. So the "only a JSON true sets a modifier" rule is the C's, and the saved JSON, key order included, is the C's byte for byte.
    • obs_key_combination_t is the existing #[repr(C)] obs_key_combination (its layout test is in nix_hotkey_state_parity).
    • OBS_KEY_MOUSE2 joins the key codes the crate's build.rs names from obs-hotkeys.h.
  • C side: obs-hotkey-bindings.c.h leaves libobs.core for its own group, libobs.ported-hotkey-bindings, listed in source-sets.json (LIBOBS_PORTED_SOURCES). Its PCH, libobs-ported-hotkey-bindings.pch.h, is obs-internal.h. The only change to the file is that include and its header comment. test_hotkey_load, test_wayland_hotkeys and test_portal_shortcuts still compile it into themselves, unchanged.
  • Symbols: the three are in rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt. They are not EXPORT, so there is no rust-exports.d fragment. nm shows t for all three in both libobs.so builds. The exported symbol sets are unchanged.
  • Oracle (oracle/hotkey_bindings.c): the original file, unmodified, with its three functions renamed, plus the key codes and INTERACT_* bits it uses. It reads and writes obs_data through the Rust obs-data port and names keys through the Rust name map, as the shims do; each of those has its own parity test.
  • Intentional differences: one that nothing can see. A null combo_out returns false and writes nothing, where the C writes through it. libobs never passes one.
  • Docs: the rust/README.md rows, the docs/rust-port/naming.md row and the crate's Cargo description.

test_hotkey_bindings_codec (18 cases) links OBS::libobs and goes through the public hotkey API only (obs_hotkey_load/obs_hotkey_save, obs_hotkey_load_bindings, the pair load and save, and a source's hotkey data). That makes it the end-to-end proof: in mode ON it runs the Rust codec through real libobs and compares the saved JSON byte for byte.

RED → GREEN

  • RED commit: c82d737dc libobs: Add Tier 1 tests for the hotkey binding codec port. The core is todo!(). soldr cargo test -p obs-data --test obs-tests-core hotkey::bindings:: fails 24 of 26 with "not yet implemented". The modifier table test passes, and so does empty_save, which never reaches the codec.
    • Tier 1 (tests/hotkey/bindings.rs) ports test_hotkey_bindings_codec 1:1: the 18 cases under the same names, in the same order, with the same JSON and the same expected bytes. The "characterized, not endorsed" cases are kept as such.
      • The C around the codec stays C, so the tests repeat it in a small model: load_bindings, load_binding, create_binding, obs_hotkey_load_bindings, save_bindings_helper and a source's enum_load_bindings, with the signal count.
      • They also repeat what obs_data does with the JSON: only a JSON true reads as a true bool, a missing or non-string "key" reads as "", and array items that are not objects are dropped.
    • 8 new edge cases:
      • the modifier table (names, bits, order) and the "key" field;
      • every modifier set;
      • no binding is ever dropped, for empty, unknown, non-UTF-8 and long key names, with any modifiers;
      • every key name, with and without all modifiers;
      • only MOUSE1 and MOUSE2 are not bindable, for any modifiers and any key code, in range or not;
      • the field order and count for every modifier mask up to 0x1FF and for masks with other bits;
      • any key code, in range or not, and nothing after the key, however often asked;
      • save then load for every key and modifier set.
    • The tests are a module of the existing core category binary, obs-tests-core. No new test binary: ci/test_binary_cap.py passes.
  • GREEN: 13ea3fc5e libobs: Port the hotkey binding codec to obs-hotkey. hotkey::bindings passes 35/35 (26 Tier 1, 9 Tier 3). The whole obs-tests-core binary passes 432/432.
    • Tier 3 (tests/hotkey/bindings_parity.rs), comparing against the C oracle:
      • the key codes and INTERACT_* bits;
      • the parse of each characterization item (through obs_data's JSON parser) and of a null obs_data: the return value and the combination written (both start from the same sentinel, so a missing write shows);
      • the parse of obs_data objects built by random obs_data calls: user, default and autoselect bools and strings, ints, doubles, objects, arrays and erases, on the modifier names, near misses ("Shift", "ctrl", "KEY", "") and other names. The strings are key names, near misses, "", "OBS_KEY_NONE" and arbitrary bytes, non-UTF-8 included;
      • the rule for every key code and the codes around the ends, with any modifier bits;
      • the save of any modifier bits and any key code, in range or not, into two obs_data objects that start the same (empty, or filled by the same random calls). The JSON of each is compared byte for byte, and each saved item is then parsed back on both sides.
    • 2000 cases per proptest. Excluded: the null combo_out only, which has its own test.
    • Deliberate-break check: in the Rust core I made combo_to_fields() skip the alt modifier, then restored it.
      • Tier 1 and 3: 11 of 35 tests failed. 6 of them are ported C cases (each_modifier_alone, modifier_order_and_false, all_modifier_sets, modifiers_only, api_other_modifier_bits_dropped, pair_round_trip). 3 are edge cases (to_fields_order_and_count, to_fields_any_key_code, to_fields_then_from_fields), and 2 are Tier 3 parity tests (to_data_matches_c, to_data_matches_c_for_every_key_and_modifier_set).
      • With libobs-with-rust rebuilt: test_hotkey_bindings_codec failed the same 6 cases in mode ON (12/18 passed) and passed 18/18 in mode OFF.
      • After the restore, all of these pass again (35/35, and 18/18 in mode ON).
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON). Each tree runs the full cmocka suite against libobs (OFF) and against libobs-with-rust (ON):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 351/351 351/351 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 351/351 351/351 IDENTICAL (1766)
  • No test was skipped or not run.

  • test_hotkey_bindings_codec passes 18/18 in all four runs. test_hotkey_load (7), test_wayland_hotkeys (8) and test_portal_shortcuts (28) pass in all four too. ldd shows each run loading the intended libobs.so.

  • libobs.ported-hotkey-bindings+.h.c is linked into libobs and not into libobs-with-rust (Ninja's inputs for each target). The export lists are also identical across the two lanes.

  • The suites ran with an extracted cmocka 2.0.2 (the build host has no system cmocka package): CMake found it through CMocka_DIR, and ctest ran with LD_LIBRARY_PATH pointing at its lib.

  • Environment: headless (private Xvfb, private D-Bus, fresh HOME).

  • ASan: hotkey::bindings and hotkey::bindings_parity (Tier 1 and 3, every proptest) pass 35/35 under AddressSanitizer, with no reports. The Rust side was built with -Zsanitizer=address, and the C oracle with -fsanitize=address (the oracle functions carry ASan checks in the test binary).

  • ci/local_gate.py:

    • --lane lint: passed.
    • --lane cross: passed.
    • --lane cross-release: passed.
    • --lane rust-test: passed (obs-tests-core 432/432, the obs-util harness 744/744, including export_list, plus the windows-gnu --no-run build).
  • ci/test_build_groups.py, ci/test_naming.py, ci/test_ported_sources.py, ci/test_rust_scope.py, ci/test_binary_cap.py, ci/test_cmocka_includes.py: OK.

  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

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 (the characterization, test_hotkey_bindings_codec, landed in libobs: Move the hotkey binding codec into obs-hotkey-bindings.c #1622)
  • 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): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): the null combo_out only

@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 17:29
RED for the port of libobs/obs-hotkey-bindings.c.h to the bindings
module of rust/obs-hotkey. The safe core is a stub (todo!()):
combo_from_fields(), the parse of load_binding() over the four modifier
bools obs_data read (in MODIFIERS order: shift, control, alt, command)
and the key name; combo_bindable(), the MOUSE1/MOUSE2 rule of
create_binding(); and combo_to_fields(), the fields the per-binding body
of save_bindings_helper() writes, in the order it writes them. The core
never sees an obs_data: the reads and writes stay at the C boundary.

tests/hotkey/bindings.rs ports test_hotkey_bindings_codec 1:1: its 18
cases under the same names, in the same order, with the same JSON and
the same expected bytes. The C test drives the public hotkey API of a
real libobs; the C around the codec (load_bindings, load_binding,
create_binding, obs_hotkey_load_bindings, save_bindings_helper and a
source's enum_load_bindings) stays C, so the test repeats it in a small
model, and repeats what obs_data does with the JSON (only a JSON true is
a true bool, a missing or non-string "key" reads as "", array items that
are not objects are dropped). The "characterized, not endorsed" cases
are kept as such.

Edge cases: the modifier table; every modifier set; every key name; no
binding is ever dropped, for empty, unknown, non-UTF-8 and long key
names; only MOUSE1 and MOUSE2 are not bindable, for any modifiers and
any key code, in range or not; the field order and count for every low
modifier mask and for masks with other bits; any key code, and nothing
after the key; and save then load for every key and modifier set.

The tests are a module of the existing core category binary,
obs-tests-core; no new test binary.

soldr cargo test -p obs-data --test obs-tests-core hotkey::bindings::
fails 24 of 26 with "not yet implemented"; the modifier table and
empty_save (which never reaches the codec) pass.

Refs obs-rust#1621
Ports libobs/obs-hotkey-bindings.c.h, the hotkey binding codec, to the
bindings module of rust/obs-hotkey. The codec turns one item of a
hotkey's saved bindings array (the "shift", "control", "alt" and
"command" bools and the "key" name) into an obs_key_combination_t and
back: the on-disk hotkey format of every profile and scene collection.
load_binding(), create_binding() and save_bindings_helper() in
obs-hotkey.c.h (still C) call it and keep the bindings array and the
hotkey_bindings_changed signal; libobs-with-rust drops the C file and
links the Rust shims in its place.

Safe core (src/bindings.rs), free of obs_data: combo_from_fields() reads
the INTERACT_*_KEY bit of each modifier that obs_data read as true
(load_modifier) and the key from its name; combo_bindable() rejects
OBS_KEY_MOUSE1 and OBS_KEY_MOUSE2 and nothing else; combo_to_fields()
yields one true per set modifier bit (save_modifier), in the order
shift, control, alt, command, then the key. Kept, characterized, not
endorsed: no item is dropped, because the C's check tests the address
of the modifiers, so {} and an unknown, empty or missing key load as
OBS_KEY_NONE. The input comes from scene collections and profiles, so
the module denies clippy's indexing_slicing, arithmetic_side_effects,
unwrap_used, expect_used, panic, todo, unimplemented and unreachable.

Shim (src/ffi/bindings.rs): obs_hotkey_combination_from_data, _bindable
and _to_data, with the C signatures. They make the C's obs_data calls in
the C's order (obs_data_get_bool per modifier and obs_data_get_string
for the key; obs_data_set_bool(name, true) per set modifier and
obs_data_set_string of obs_key_to_name for the key), so only a JSON true
sets a modifier, and the saved JSON, key order included, is the C's
byte for byte. A null combo_out returns false (C writes through it).
OBS_KEY_MOUSE2 joins the key codes build.rs names.

C side: the file leaves libobs.core for its own group,
libobs.ported-hotkey-bindings (LIBOBS_PORTED_SOURCES), whose PCH is
obs-internal.h. The three symbols are in rust-hidden.txt,
rust-exports.map and rust-unexports-macos.txt; they are hidden in both
libobs builds and there is no rust-exports.d fragment. The exported
symbol sets are unchanged. test_hotkey_load, test_wayland_hotkeys and
test_portal_shortcuts still compile the C file into themselves,
unchanged.

Oracle (oracle/hotkey_bindings.c): the original file with its three
functions renamed, plus the key codes and INTERACT_* bits it uses. It
reads and writes obs_data through the Rust obs-data port, as the shims
do.

Tier 3 (tests/hotkey/bindings_parity.rs): the values; the parse of the
characterization items, a null obs_data, and obs_data objects built by
random calls (user, default and autoselect bools and strings, ints,
doubles, objects, arrays, erases) on the modifier names, near misses
and others, with key names, near misses and arbitrary bytes; the rule
for every key code and the codes around the ends with any modifiers;
and the save of any modifiers and key code into two obs_data objects
that start the same, compared as JSON byte for byte, then parsed back.
Intentional differences: the null combo_out only.

Docs: rust/README.md rows, the naming.md row and the crate description.

Fixes obs-rust#1621
@zackees
zackees force-pushed the rust/port-hotkey-bindings branch from 13ea3fc to a3bd2fd Compare October 21, 2026 18:13
@zackees
zackees enabled auto-merge October 21, 2026 18:13
@zackees

zackees commented Oct 21, 2026

Copy link
Copy Markdown
Member

Coordinator: I rebased this onto master to clear a rust/README.md conflict with #1588, which renamed libobs-with-rust to hybrid libobs. Your obs-hotkey-bindings row is kept and uses the new wording. I set auto-merge, so it will join the merge queue once it's green.

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.

rust: Port the hotkey binding load/save codec in obs-hotkey.c to obs-hotkey

2 participants