Skip to content

libobs: Port the portal shortcut id codec to obs-hotkey - #1519

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-portal-shortcut-ids
Oct 20, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-portal-shortcut-ids

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Base: master

Fixes #1517 (PR 2 of 2; PR 1, #1518, moved the codec into its own file and added its characterization test)

What and why

This PR ports libobs/obs-nix-portal-shortcut-ids.c.h, the GlobalShortcuts portal shortcut id and trigger codec, to the existing rust/obs-hotkey crate. The file has three functions: obs_nix_portal_shortcut_id, obs_nix_portal_shortcut_combo and obs_nix_portal_shortcut_trigger. The portal client obs-nix-portal-shortcuts.c calls them to name a key combination as a shortcut id (ctrl+shift+OBS_KEY_F13), to parse an id back, and to offer the combination as the preferred trigger (CTRL+SHIFT+F13). libobs-with-rust drops the C file and links the Rust shims.

  • Crate: obs-hotkey already holds the name-map (rust: Port libobs/obs-hotkey-name-map.c to obs-hotkey #833) and Wayland key state (libobs: Port the Wayland hotkey key state to obs-hotkey #1516) ports, so the naming.md row now lists obs-nix-portal-shortcut-ids.c.h too. No new crate and no new dependency. build.rs now also emits OBS_KEY_MOUSE1 and OBS_KEY_MOUSE29 from obs-hotkeys.h.
  • Safe core (src/portal_shortcut_ids.rs):
    • bindable, shortcut_id, shortcut_combo and shortcut_trigger, plus the MODIFIER_NAMES table.
    • The id and trigger output is generic over a Text trait (free, cat, cat_text, is_empty: the four dstr operations the C uses). Vec<u8> implements it for Rust callers.
    • Key names come from the name-map core directly (key_name, key_from_name).
    • No host cfg, no unsafe.
  • Shim (src/ffi/portal_shortcut_ids.rs):
  • GIO: the shims call nothing from GIO, so unlike rust: Port util/platform-nix-inhibit.c to obs-util #1500/obs-util: Port Linux sleep inhibition to Rust #1504 they need no gio Cargo feature. They are built on every host and unused where the portal client is not, like the libobs: Port the Wayland hotkey key state to obs-hotkey #1516 shims.
  • Build group:
    • libobs.nix-portal-shortcut-ids (from libobs: Move the portal shortcut id codec into obs-nix-portal-shortcut-ids.c #1518) becomes libobs.ported-nix-portal-shortcut-ids, and its PCH is renamed with it (same contents: no <gio/gio.h>, so the C tests and the oracle compile the file on their own).
    • The group is listed as LIBOBS_WAYLAND_GIO_PORTED_SOURCES in source-sets.json. It is added to libobs only, under ENABLE_WAYLAND with GIO, in os-linux.cmake and os-freebsd.cmake. libobs.nix-portal-shortcuts stays in libobs-objects.
    • obs-build's Linux C variant appends the set under wayland && gio. rust/tools/ported_sources.py gets --without-gio; the set is only included with Wayland, so linux-validate/validate.sh (which builds with ENABLE_WAYLAND=OFF and passes --without-wayland) needs no new flag, only an updated comment. ci/test_ported_sources.py covers both flags.
  • Oracle (oracle/portal_shortcut_ids.c): it renames the three functions with #defines and includes the original file. It also wraps bindable, and exports the key codes, the four INTERACT_*_KEY bits and the modifier_names table. The dstr functions and obs_key_to_name / obs_key_from_name keep their real names, so under cargo test they are the Rust ports (each has its own parity test). That keeps the oracle away from the C name map's global obs toggle, which the name-map parity test changes.
  • Intentional differences (C has undefined behavior there; no caller passes null): a null id or trigger dstr, a null id string, or a null combo out-pointer returns false, and a null trigger does not call key_name. These are listed in the shim docs, the parity test header and the rust/README.md row. Nothing else differs. The two "characterized, not endorsed" behaviours from libobs: Move the portal shortcut id codec into obs-nix-portal-shortcut-ids.c #1518 are kept: modifier bits other than ctrl, alt, shift and super are dropped, and the parser takes modifiers in any order and repeated.

test_portal_shortcut_ids (15) and test_portal_shortcuts compile the C into themselves and are unchanged. They pass in every lane, but they cannot see the swap, so Tier 3 is the swap's proof, as with #1486, #1513 and #1516.

RED → GREEN

  • RED commit: a8d682790 libobs: Add Tier 1 tests for the portal shortcut id codec port. The safe core has todo!() bodies. soldr cargo test -p obs-data --test obs-tests-core hotkey::portal_shortcut_ids fails 19 of 20 with "not yet implemented" (the modifier table case passes).
    • Tier 1 (tests/hotkey/portal_shortcut_ids.rs) mirrors test_portal_shortcut_ids 1:1, with the same fifteen test names and cases. The C test's teardown checks bnum_allocs; here a counting Text checks that every buffer the codec allocates is freed (the callback's name included).
    • New edge cases: the modifier table itself, keys at the ends of the i32 range, Vec<u8> as a Text, key names holding a NUL (copied whole; a leading NUL is an empty name), and segments that only look like known modifiers.
    • The tests are modules of the existing core category binary, obs-tests-core. No new test binary: ci/test_binary_cap.py reports 8 of 8.
  • GREEN: 50fbe9bf8 libobs: Port the portal shortcut id codec to obs-hotkey. hotkey::portal_shortcut_ids passes 20/20, and the whole obs-tests-core binary passes (200/200).
    • Tier 3 (tests/hotkey/portal_shortcut_ids_parity.rs), both sides compared after every call:
      • bindable for every key from -300 to OBS_KEY_LAST_VALUE + 300 and the i32 extremes, with three modifier sets, then 2048 random cases;
      • the key codes, modifier bits and modifier_names table against the oracle;
      • every key from -2 to OBS_KEY_LAST_VALUE + 1 with three modifier sets: the id, then a parse of that id back;
      • fixed trigger cases: the characterized callbacks (none, a name, empty, filled then false, unset, a leading NUL, an inner NUL), five keys at the edges, zeroed and pre-filled output;
      • proptests, 2048 cases each: ids over random keys (in range, near the edges, the mouse range, any i32), modifiers (subsets of the four bits, with extra bits, any u32) and a starting dstr (zeroed, empty, random text); parses of random ids built from known and near-miss modifier names (CTRL, ctr, superr, ...), key names (bindable or not) and random bytes, or plain random bytes; triggers with random callback behaviour (none, write exact bytes NULs included, leave the name, return true or false);
      • compared: the result, every byte of the output with its terminator, its length and capacity, the combo out-value, and the callback's calls (key, and the name's state on entry).
  • Deliberate-break check: eleven breaks, each run against the parity tests only and then restored. Every one made at least one parity test fail:
    • core: the mouse range ending one key early, the OBS_KEY_LAST_VALUE bound off by one, modifiers in reverse order, no dstr_free before an id, no empty-name check, case-insensitive modifier matching, - in place of + in triggers;
    • shim: dstr_free leaving the capacity (the run aborts on the first trigger case), dstr_is_empty ignoring a leading NUL, an extra bit written to the combo out-value.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON, ENABLE_WAYLAND=ON, GIO found). Each tree runs the full cmocka suite against libobs (OFF) and libobs-with-rust (ON):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 330/330 330/330 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 330/330 330/330 IDENTICAL (1766)
  • No test was skipped or not run in any of the four runs. The export lists are also identical across the two lanes, and none of them has a obs_nix_portal_shortcut_* symbol.

  • test_portal_shortcut_ids, test_portal_shortcuts, test_wayland_hotkeys and test_wayland_multiseat pass in all four runs.

  • libobs.ported-nix-portal-shortcut-ids+.h.c is linked into libobs and not into libobs-with-rust; in libobs-with-rust the three symbols are local and come from the Rust core.

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

  • ci/local_gate.py:

    • --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, ifdef ratchet, test-binary cap): passed.
    • --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary, oracle included): passed.
    • --lane cross-release: passed.
    • --lane rust-test (soldr cargo test --workspace --locked, plus a --no-run link for x86_64-pc-windows-gnu): passed. obs-tests-core 200/200. The obs-util harness 668/668, including the export_list check that every shim is exported or hidden.
  • 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: 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 (characterization test_portal_shortcut_ids landed in libobs: Move the portal shortcut id codec into obs-nix-portal-shortcut-ids.c #1518)
  • 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)

RED for the port of libobs/obs-nix-portal-shortcut-ids.c.h to the
obs-hotkey crate. The safe core (obs_hotkey::portal_shortcut_ids) has
bindable, shortcut_id, shortcut_combo and shortcut_trigger, generic over
the Text an id or trigger is written to, so the C ABI shim can run them
on the caller's dstr with the same dstr calls as the C. Its functions
are todo!() stubs. obs-hotkey's build.rs now also emits OBS_KEY_MOUSE1
and OBS_KEY_MOUSE29 from obs-hotkeys.h.

The first fifteen cases mirror test_portal_shortcut_ids 1:1, including
its "characterized, not endorsed" cases, with a counting Text in place
of the C test's bnum_allocs teardown. The rest pin the edges: the
modifier table, keys at the ends of the range, Vec<u8> as a Text, key
names holding a NUL, and segments that only look like known modifiers.

soldr cargo test -p obs-data --test obs-tests-core
hotkey::portal_shortcut_ids fails 19 of 20 with "not yet implemented"
(the modifier table case passes).

Refs obs-rust#1517
libobs/obs-nix-portal-shortcut-ids.c.h moves to
obs_hotkey::portal_shortcut_ids. Its three functions,
obs_nix_portal_shortcut_id, obs_nix_portal_shortcut_combo and
obs_nix_portal_shortcut_trigger, are what obs-nix-portal-shortcuts.c
calls to name a key combination as a GlobalShortcuts shortcut id, parse
an id back, and offer the combination as the preferred trigger. bindable()
stays the static inline in obs-nix-portal-shortcuts.h; the core has the
same check, and the oracle compares the two.

The core is generic over the Text an id or trigger is written to. The
shim writes the caller's dstr with the C's calls in the C's order
(dstr_free, one dstr_cat per piece, dstr_cat_dstr for the key name), so
bytes, length and capacity match; the key name comes from the caller's
callback into a zeroed dstr, as in C. Key names come from the name map
port. A null id or trigger dstr, a null id string or a null combo
out-pointer returns false, where C dereferences them.

The file moves from libobs.nix-portal-shortcut-ids to its own ported
group, libobs.ported-nix-portal-shortcut-ids (same PCH contents, renamed
with it). The group is LIBOBS_WAYLAND_GIO_PORTED_SOURCES, added to libobs
only under ENABLE_WAYLAND with GIO (os-linux.cmake, os-freebsd.cmake),
and followed by obs-build's Linux C variant and
rust/tools/ported_sources.py (new --without-gio; the group also needs
Wayland, which linux-validate already turns off). The shims call nothing
from GIO, so they are built on every host with no Cargo feature. The three
symbols are not EXPORT, so they go in rust-hidden.txt, rust-exports.map
and rust-unexports-macos.txt.

Tier 3 (oracle/portal_shortcut_ids.c) compares bindable over every key and
random ones, the key codes and modifier bits, the modifier table, and
random combinations, id strings (known and near-miss modifier names, key
names, arbitrary bytes), key name callbacks and starting dstrs: the
result, the output bytes, length and capacity, the combo out-value and
the callback calls. test_portal_shortcut_ids and test_portal_shortcuts
compile the C into themselves and are unchanged.

Fixes obs-rust#1517
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 15:25
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit 57be407 Oct 20, 2026
25 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.

rust: Port the portal shortcut id codec in obs-nix-portal-shortcuts.c to obs-hotkey

2 participants