Skip to content

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

Closed

Description

@rdoupe

Unit

Port the hotkey binding codec of libobs/obs-hotkey.c.h to the existing crate rust/obs-hotkey as a new module, bindings. The codec turns one item of a hotkey's saved bindings array into an obs_key_combination_t and back. It is the on-disk hotkey format of every profile and scene collection: the "shift", "control", "alt" and "command" bools and the "key" name.

  • load_modifier and the parse in load_binding: read the four modifier bools and the key name from one obs_data_t item, and decide whether to accept it;
  • the rule in create_binding (ours) that rejects OBS_KEY_MOUSE1 and OBS_KEY_MOUSE2, which the frontend reserves for UI interaction. It covers JSON and obs_hotkey_load_bindings() bindings alike;
  • save_modifier and the per-binding body of save_bindings_helper: write the same keys, in the order shift, control, alt, command, key.

About 50 lines. These stay C: load_bindings (the hotkey_bindings_changed signal), obs_hotkey_load, obs_hotkey_load_bindings, obs_hotkey_pair_load, enum_load_bindings, obs_hotkeys_load_*, save_hotkey, obs_hotkey_save, obs_hotkey_pair_save and save_context_hotkeys. They touch obs->hotkeys.bindings, the uthash tables and struct obs_hotkey.

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 and the portal shortcut id codec. It is part of a large file, so it enters through function extraction (testing-policy.md, "Extracting a function before porting it"). Its Rust tests join the crate's existing tests/hotkey/ binary, next to the name map, key state and portal id tests; no new test binary.

Dependencies and existing tests

  • No struct obs_* internals, no accessors and no obs-util prerequisite. The codec calls the exported obs_data_get_bool, obs_data_get_string, obs_data_set_bool and obs_data_set_string (already in Rust in obs-data), and obs_key_from_name and obs_key_to_name (already in Rust in obs-hotkey::name_map). obs_key_combination_t is a public, stable layout in obs-hotkey.h.
  • The safe core stays free of obs_data: for example combo_from_fields(shift, control, alt, command, key_name) -> Option<Combo>, combo_bindable(&Combo) -> bool and combo_to_fields(&Combo). Only src/ffi/ calls the obs_data_* C ABI, so cargo test needs no obs-data and the crate picks up no Cargo dependency on it.
  • test_hotkey_load (7 cases: the mouse-button rule for JSON, API and pairs, allowed bindings kept, rejected bindings still replacing old ones, no callbacks) compiles obs-hotkey.c.h into itself with obs=hotkey_test_obs, so a Rust swap is invisible to it. There is no real-libobs test of obs_hotkey_save and no round-trip test, so PR 1 adds one.
  • Format compatibility: any difference in a key name, a modifier key or the key order loses or rewrites users' bindings. The characterization compares the saved JSON byte for byte, and a save, load and save again must give the same bytes.
  • The input is semi-trusted: scene collections and profiles can come from others. The codec only reads four bools and a string, and the key name lookup is already ported and fuzzed through the name map.

Plan

  1. Characterization and extraction PR (behaviour-neutral, one PR as in libobs: Move the output packet interleaver into obs-output-interleave.c #1595 and libobs: Move the caption text wrap into obs-output-caption-text.c #1618):
    • New test test_hotkey_bindings_codec, linking OBS::libobs with obs_startup() and using only the public API: obs_hotkey_load and obs_hotkey_save on a frontend hotkey, obs_hotkey_load_bindings, obs_hotkey_pair_load and obs_hotkey_pair_save, and a source's hotkey data (obs_source_create, obs_hotkeys_save_source). It pins each modifier alone, all 16 modifier sets and the saved key order; a modifier that is not a JSON true; every key name, with save, load and save as a byte-for-byte fixpoint; unknown, empty, missing and non-string keys; {} and modifier-only bindings; MOUSE1 and MOUSE2 rejected with and without modifiers, and MOUSE3 kept; duplicates and order; items that are not objects; an empty array; and when hotkey_bindings_changed is sent.
    • The codec moves verbatim into libobs/obs-hotkey-bindings.c.h, in the same unity group (libobs.core, just before obs-hotkey.c.h, alphabetical), behind three hidden entry points declared in obs-internal.h without EXPORT: obs_hotkey_combination_from_data(), obs_hotkey_combination_bindable() and obs_hotkey_combination_to_data(). test_hotkey_load, test_wayland_hotkeys and test_portal_shortcuts compile the new file next to obs-hotkey.c.h.
  2. Port PR (RED, then GREEN):
    • obs_hotkey::bindings: the safe core over fields, and src/ffi/bindings.rs with the three entry points. The C file moves to its own ported group, libobs.ported-hotkey-bindings, with its own .pch.h and export fragment, as libobs.ported-nix-hotkey-state has.
    • Tests: Tier 1 ports every test_hotkey_bindings_codec expectation 1:1. Tier 3 parity of the C file (an oracle over a small fake obs_data) against the Rust core, with a proptest over modifier bits, key names (valid, invalid, empty, non-UTF-8) and combinations. Tier 2 OFF and ON unchanged, where test_hotkey_bindings_codec sees the swap. Tier 4 does not apply (no protocol change).
    • Docs: the rust/README.md rows, the docs/rust-port/naming.md row for rust/obs-hotkey (add obs-hotkey-bindings.c.h) and the crate's Cargo description.

No intended behaviour changes, and no format change. Two behaviours that the test marks "characterized, not endorsed" are kept by the port and fixed later, each in its own PR with its own test change:

  • 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 or missing key load as a binding of OBS_KEY_NONE and are saved back as {"key":"OBS_KEY_NONE"}. Such a binding never fires.
  • 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 also signals when it removed bindings.

Activity

  1. rdoupe commented on Oct 21, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1621

    1. Characterization and move PR: test_hotkey_bindings_codec (18 cases, real libobs, save → load → save compared byte for byte), and the codec moved verbatim into libobs/obs-hotkey-bindings.c.h. Opening it now.
    2. The port, as rust/obs-hotkey's bindings module, as its own PR after 1 merges.
  2. added
    claimed
    on Oct 21, 2026
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