Skip to content

libobs: Move the portal shortcut combination sets into obs-nix-portal-shortcut-sets.c - #1784

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:refactor/portal-shortcuts-move
Oct 22, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:refactor/portal-shortcuts-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Refs #1783 (PR 1 of the plan: the characterization and the extraction. 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 combination set logic of the GlobalShortcuts portal client (Wayland with GIO, Linux and FreeBSD) moves from libobs/obs-nix-portal-shortcuts.c.h into libobs/obs-nix-portal-shortcut-sets.c.h. On every sync of the hotkey bindings it decides whether the portal session is bound again, which keys the new request asks for, and what is bound again after the desktop's dialog is declined. The move comes with characterization tests, because nothing tested this logic on its own. There is no Rust, no behaviour change and no public header change (obs-nix-portal-shortcuts.h is internal).

  • What moves: has_combo(), covered_by(), description_of() and same_set() verbatim; select_combos() without its copies; and three decisions as hidden entry points:
    • obs_nix_portal_sets_unchanged(session, retry, partly_declined, current, …, bound, …, declined): sync_shortcuts()'s unchanged, with the retry override;
    • obs_nix_portal_sets_request(current, partly_declined, declined, request, &num): the request filter, returning partly;
    • obs_nix_portal_sets_partition(current, approved, request, part, &num_part, declined, &num_declined): what bind_approved() binds again and what it declines, returning whether to bind;
    • and obs_nix_portal_sets_has(), which the Activated handler uses.
  • How the moved code reads the arrays: through two views declared in obs-nix-portal-shortcuts.h, struct obs_nix_portal_combos (array, num) and struct obs_nix_portal_descriptions (const char *const *array, num). It returns positions in them instead of copies. So the portal client keeps every allocation: the new take_combos() makes the copies, with "OBS" for a missing description, as select_combos() did. The port will need no darray or bmem across the boundary. The view fields are named like the darray's, so the four moved helpers differ from the old ones only in their parameter types. select_combos() and the partition return positions; the partition keeps part in current's order (a forward filter instead of erasing backwards) and declines the same combinations.
  • What stays: copy_descriptions() and free_descriptions(), collect_bindings() and action_label(), everything that calls GDBus, set_state() and set_flag() (they take the hotkeys mutex), and the portal thread. same_combo() moves verbatim into obs-nix-portal-shortcuts.h, still static inline, because the binding collection and the trigger update use it too (as bindable() did in libobs: Move the portal shortcut id codec into obs-nix-portal-shortcut-ids.c #1518).
  • Hidden entry points: declared without EXPORT, so -fvisibility=hidden keeps them local (t symbols in libobs.so). The tests that compile the client into themselves build each .c.h as its own translation unit, so the moved code has to be reachable through functions, not static ones.
  • Build: the new file gets its own unity group, libobs.nix-portal-shortcut-sets, built next to libobs.nix-portal-shortcuts under the same ENABLE_WAYLAND + GIO condition (CMake on Linux and FreeBSD, and obs-build's Linux libobs). Its precompiled header has no <gio/gio.h>, so a C test and later the C oracle compile the file on its own. That is the only reason it is not in the existing group. The moved code has no platform conditional.
  • test_portal_shortcuts compiles the new file next to the client (tests.d/test_wayland_hotkeys.cmake).
  • Exported symbols: unchanged, 1766 in libobs with the C backend, in the hybrid libobs and in libobs-c-reference.

Four new cases in test_portal_shortcuts (their own commit before the move, on the unchanged C, with the same fake portal on a private bus). They pin what its 28 cases did not:

  • the same keys with the same descriptions in another order are not bound again, and a new key then binds them in their new order;
  • descriptions are compared as bytes: the same name in another Unicode normalization binds again, the same bytes do not;
  • partly declined, then the approved key's binding goes away: only declined keys are left, so the session closes, nothing is asked for and the state is declined; Ask again binds the rest;
  • the desktop's dialog for a set with no new key (an action moved onto an approved key) is cancelled: the state is declined and the key is unbound until Ask again.

test_portal_shortcut_sets (new, with the move) compiles the moved file with no GIO, no D-Bus and no libobs core, and calls only the entry points. That is so the port can link the same file against the Rust staticlib (as test_scene_bounds_place_rust does) and run the characterization against the hybrid build: the 32 end-to-end cases compile the C, so they cannot see the swap. Its 12 cases:

  • has: an empty set, the same key with other modifiers, every modifier bit (also ones the portal does not know), only num entries read;
  • the same set: empty on either side, order independence, other modifiers, one more or one fewer, duplicates, fewer descriptions than combinations (the i < num guards on both sides), NULL descriptions, the empty description, and byte-wise compare (precomposed and decomposed é, case, a prefix, bytes that are not UTF-8);
  • without a session: nothing to bind, all declined before (also with duplicates), a new key or other modifiers;
  • every combination of the session, retry and partly-declined flags, for a set that each branch calls unchanged and for one it does not;
  • the request: not partly declined (everything, declined or not), partly declined (the declined left out in current's order, every copy of a duplicate, other modifiers kept), everything or nothing declined, empty;
  • the partition: a new key declined, current's order, nothing approved, other modifiers approved, the whole request approved, an approved key not in the request, nothing current;
  • that nothing past the arrays' size is written, and nothing is allocated.

Characterized, not endorsed (a comment in the tests, and in the issue). None is changed here:

  • same_set() compares combinations as sets, so {A, A} is the same as {A}, and each copy is compared with the description of the first copy on the other side.
  • A NULL description is not the same as another NULL.
  • The partition counts duplicates in |part| and takes |request| as given, so two copies of an approved key can stop the partial rebind. The portal client passes neither (its bindings hold each combination once).
  • When the dialog for a set with no new key is cancelled, the key approved before is not bound again, because bind_approved() binds only a strict part of the request. Every key then stops working until Ask again. The issue lists it as a possible follow-up.

RED → GREEN

  • RED commit: none. This is a move plus characterization, which passes on the moved code and on master. The four end-to-end cases are their own commit, 4bca70285 test: Characterize when the portal client binds its shortcuts again, and test_portal_shortcuts passes 32/32 there on the unchanged C. The move is 8a8516484 libobs: Move the portal shortcut combination sets into obs-nix-portal-shortcut-sets.c.
  • Equivalence of the move: a scratch differential program compiled master's same_combo, has_combo, covered_by, description_of, same_set and select_combos, with master's unchanged, request and partition code, against the new entry points, over 300,000 random cases (five combinations including OBS_KEY_NONE and a CapsLock bit, duplicates, shorter description arrays, NULL, empty and non-UTF-8 descriptions, all flags). It compared the results, the positions with the copies master made, and the descriptions: 0 differences, 0 leaked allocations.
  • test_portal_shortcut_sets 12/12 and test_portal_shortcuts 32/32, each run 4 times (once before and 3 times after the final rebase) against libobs with the C backend, the hybrid libobs and libobs-c-reference.
  • Mutation check: 24 of 24 mutants of the moved code fail test_portal_shortcut_sets; 16 of them also fail test_portal_shortcuts. Each mutant of obs-nix-portal-shortcut-sets.c.h (and same_combo() in the header) was built into both tests:
    • same_combo() ignoring the modifiers; has_combo() skipping the last entry (both);
    • same_set() covering one way only, ignoring descriptions, comparing them by position (the reordered case catches it end to end), case-insensitively, or with an off-by-one description guard; a NULL description equal to NULL; description_of() taking the last match;
    • without a session: ignoring the declined set, or binding an empty set;
    • the retry override dropped, without its session check, or without its partly-declined check;
    • the request always filtered, never filtered, the filter inverted, or partly as <=;
    • the partition without the request filter, binding the whole request, binding an empty part, <= the request, declining all of current, or declining the part.
      The 8 that only the new test sees: same_combo() ignoring the modifiers, NULL equal to NULL, the last match, the case-insensitive compare, the empty set without a session, both weakened retry overrides, and the partition without the request filter. The end-to-end flows don't produce those inputs (the client deduplicates the bindings and never has a NULL description), or the fake portal cannot tell the results apart.
  • 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 366/366 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 368/368 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 368/368 IDENTICAL (1766)

Nothing was skipped or failed to run. test_portal_shortcut_ids (15/15), test_wayland_hotkeys (8/8), test_wayland_multiseat (6/6), test_hotkey_load (7/7), test_hotkey_bindings_codec (18/18), test_hotkey_translations (16/16) and test_x11_hotkey_layout (4/4) pass, unchanged, against all three. After the final rebase onto b39a4e0 (#1775, with #1752), both trees were rebuilt, the full suite and the symbol comparison were run again (the numbers above), and the portal and hotkey tests 3 more times against each library.

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. ci/test_build_groups.py and ci/test_ported_sources.py pass; ci/platform_ifdef_ratchet.py reports only the CrashHandler baseline that master reports too. clang-format 22.1.3 and gersemi are 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 is Linux and FreeBSD only (Wayland with GIO), and both new and changed tests are registered for Linux and FreeBSD only. test_portal_shortcut_sets is portable C (no cmocka range assertions, no fail_msg()).

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 (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 combination set logic of the GlobalShortcuts
portal client (same_set, covered_by, select_combos, the partition in
bind_approved and the decisions in sync_shortcuts) moves to its own file
and is ported to obs-hotkey. test_portal_shortcuts covers the main flows;
four new cases, on the same fake portal and private bus, pin what it did
not:
- the same keys with the same descriptions in another order are not bound
  again, and a new key then binds them in their new order;
- descriptions are compared as bytes: the same name in another Unicode
  normalization binds again, the same bytes do not;
- partly declined, then the approved key's binding goes away: only
  declined keys are left, so the session closes, nothing is asked for and
  the state is declined; Ask again binds the rest;
- the dialog for a set with no new key (an action moved onto an approved
  key) is cancelled: the state is declined and the key is unbound until
  Ask again.

Characterized, not endorsed: in the last case, the key approved before is
not bound again, because bind_approved() binds only a strict part of the
request.

Refs obs-rust#1783
…-shortcut-sets.c

Behavior-neutral extraction before the Rust port. The combination set
logic of the GlobalShortcuts portal client moves from
libobs/obs-nix-portal-shortcuts.c.h into
libobs/obs-nix-portal-shortcut-sets.c.h: has_combo, covered_by,
description_of and same_set verbatim, select_combos without its
allocations, and the three decisions as hidden entry points:
- obs_nix_portal_sets_unchanged(): whether a sync leaves the session as
  it is (sync_shortcuts' unchanged, with the retry override);
- obs_nix_portal_sets_request(): the bindings to ask for, without the
  declined ones while partly declined, and whether that is only part;
- obs_nix_portal_sets_partition(): what bind_approved() binds again after
  a declined dialog, the rest of the bindings, and whether to bind;
plus obs_nix_portal_sets_has() for the Activated handler.

The moved code reads the arrays through two views,
struct obs_nix_portal_combos and struct obs_nix_portal_descriptions
(pointer and count, declared in obs-nix-portal-shortcuts.h), and returns
positions in them, so the portal client keeps every allocation and the
port needs no darray or bmem across the boundary. The only edits to the
moved bodies are those parameter types, and select_combos and the
partition returning positions instead of copies; take_combos() in the
portal client makes the copies, with "OBS" for a missing description, as
before. same_combo() moves verbatim into obs-nix-portal-shortcuts.h,
still static inline, as the binding collection and the trigger update
also use it. The entry points are declared without EXPORT, so
-fvisibility=hidden keeps them local, and the exported symbols do not
change.

The new file gets its own unity group, libobs.nix-portal-shortcut-sets,
built next to libobs.nix-portal-shortcuts under the same ENABLE_WAYLAND
+ GIO condition (CMake on Linux and FreeBSD, and obs-build). Its
precompiled header has no <gio/gio.h>, so a C test and later the C
oracle compile it on their own. test_portal_shortcuts compiles it next
to the portal client.

New characterization test test_portal_shortcut_sets (12 cases), with no
GIO, D-Bus or libobs core. It calls only the entry points, so the port
can run the same file against the Rust staticlib. It pins: empty sets on
either side; order independence; the same key with other modifiers, and
every modifier bit; fewer descriptions than combinations; a NULL
description; byte-wise compare of descriptions (Unicode normalization,
case, bytes that are not UTF-8); the no-session branch; every
combination of the session, retry and partly-declined flags; the request
filter; each branch of the partition; and that nothing past the arrays'
size is written and nothing is allocated.

Characterized, not endorsed:
- same_set compares combinations as sets, so {A, A} is the same as {A},
  and each copy is compared with the description of the first copy on
  the other side;
- a NULL description is not the same as another NULL;
- the partition counts duplicates in |part|, and takes |request| as
  given, so two copies of an approved key can stop the partial rebind.
  The portal client passes neither (its bindings hold each combination
  once).

Refs obs-rust#1783
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 11:35
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit aaf8351 Oct 22, 2026
28 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 22, 2026
…rust#1800)

* libobs: Add Tier 1 tests for the portal shortcut combination sets port

RED for the port of libobs/obs-nix-portal-shortcut-sets.c.h to a new
module, portal_shortcut_sets, of rust/obs-hotkey. The safe core is a
stub (todo!()): same_combo(); has(); unchanged(), whether a sync leaves
the portal session as it is, with the retry override; request(), the
positions of current to ask the desktop for, and whether that is only
part; and partition(), what is bound again after a declined dialog and
what is declined. A combination is any Combination (KeyCombination, or
the C struct in the shim), and a description is any T: PartialEq, None
for a NULL one, so the shim can compare C strings in place.

tests/hotkey/portal_shortcut_sets.rs ports test_portal_shortcut_sets 1:1,
its 12 cases under the same names, in the same order, with the same
inputs and expectations, the sentinel checks past current's size
included. Kept as characterized, not endorsed: {A, A} is the same set as
{A}, a NULL description is not the same as another NULL, and the
partition counts duplicates.

Edge cases: every modifier bit and any key code in same_combo; the
partition's first pass leaving the filtered approved positions after
part, as the C's in-place filter does; short buffers losing only the
positions past their end, never the counts; descriptions of any type;
and proptests of each function against a model.

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::portal_shortcut_sets fails 20 of 20 with "not yet implemented".

Refs obs-rust#1783

* libobs: Port the portal shortcut combination sets to obs-hotkey

Ports libobs/obs-nix-portal-shortcut-sets.c.h to the portal_shortcut_sets
module of rust/obs-hotkey. On every sync of the hotkey bindings the
GlobalShortcuts portal client (Wayland with GIO, Linux and FreeBSD)
asks it whether the session is bound again, which keys the new request
asks for, and what is bound again after the desktop's dialog is
declined; the Activated handler asks whether a set holds a combination.
The portal client, every GDBus call, the portal thread and every
allocation stay C in obs-nix-portal-shortcuts.c.h; the hybrid libobs
drops the C file and links the Rust shims in its place.

Safe core (src/portal_shortcut_sets.rs): same_combo() (every modifier
bit, any key), has(), and the C's covered_by(), description_of(),
same_set() and select_combos(), behind unchanged(), request() and
partition(). Descriptions are compared only where the C compares them,
and a combination without one, or with a NULL one, is never the same.
The partition fills part with every approved position and filters it in
place, as the C does, so the entries past num_part are the C's too. It
allocates nothing, keeps no state and takes no lock, and denies clippy's
panic, indexing and arithmetic lints: every position goes through a
bounds check, and the counts never depend on the room.

Shim (src/ffi/portal_shortcut_sets.rs): obs_nix_portal_sets_has,
_unchanged, _request and _partition with the C signatures. The views
struct obs_nix_portal_combos and struct obs_nix_portal_descriptions are
#[repr(C)]; the arrays are borrowed as slices, a description entry as an
Option<Description> (a transparent NonNull<c_char>, the layout of
const char *) compared with the bytes up to its NUL, as strcmp == 0.
Nothing is copied, so no darray or bmem crosses the boundary.

Intentional differences, where C has undefined behavior only: a null
view, or a view with a null array and a nonzero count, is an empty set,
and a null count out-pointer or position buffer is not written (C
dereferences them); the portal client passes neither. Kept as
characterized, not endorsed (obs-rust#1784): {A, A} is the same set as {A}, a
NULL description is not the same as another NULL, and the partition
counts duplicates in |part| and takes |request| as given.

C side: the group libobs.nix-portal-shortcut-sets becomes the ported
group libobs.ported-nix-portal-shortcut-sets (its PCH renamed to match,
still without <gio/gio.h>), in LIBOBS_WAYLAND_GIO_PORTED_SOURCES next to
the shortcut ids: CMake on Linux and FreeBSD, and obs-build's C variant,
compile it for the C backend and libobs-c-reference only. The four
functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the
local: list of rust-exports.map and rust-unexports-macos.txt. The
exported symbol sets are unchanged (1766). ci/test_ported_sources.py
checks the group.

test_portal_shortcut_sets and test_portal_shortcuts still compile the C
into themselves, unchanged. A test_portal_shortcut_sets_rust against
libobs_rust-static, as test_scene_bounds_place_rust does, does not link
on its own: the shims' archive members pull in the rest of obs-hotkey,
obs-data and obs-util, and with them jansson, libuuid, GIO and libobs's
hidden hotkey seams (obs_hotkeys_mutex_lock, obs_hotkey_binding_fields,
obs_hotkeys_store_key_translation), which no test executable can
resolve. The test registration says so; Tier 3 compares the shims with
the C instead.

Refs obs-rust#1783

* libobs: Check the portal shortcut combination sets port against the C

Oracle (oracle/portal_shortcut_sets.c): the original
obs-nix-portal-shortcut-sets.c.h, unmodified, with its four entry points
renamed to oracle_*, plus the layouts of struct obs_nix_portal_combos and
struct obs_nix_portal_descriptions. It needs no GIO, so it is built on
every host.

Tier 3 (tests/hotkey/portal_shortcut_sets_parity.rs): both sides get the
same arrays through the same views. After each call it compares the
return value, the counts and every entry of the position buffers, which
start as a sentinel and have two more entries than current, so the
entries past the counts (the partition's first pass leaves approved
positions there) and any write past current's size are compared too.
Runs:
- the layouts, and that Option<Description> is a const char *;
- has: every sequence of up to three combinations from a pool with
  OBS_KEY_NONE (alone and with Ctrl), a mouse button with bit 31, a key
  alone, with Ctrl and with Caps Lock and Shift, against every pool entry
  and two more;
- unchanged with a session: every pair of sets up to two entries with
  every description array up to three entries (NULL, "x", another "x" at
  another address, "y"), so fewer and more descriptions than
  combinations, for each flag with a session;
- every pair of descriptions from the whole pool (empty, NFC and NFD,
  case, a prefix, bytes that are not UTF-8, the same bytes at two
  addresses, NULL), on one combination and on two in both orders;
- unchanged without a session: every current and declined set up to
  three entries, with every retry and partly-declined flag;
- request: every current set up to four entries and declined set up to
  two, partly declined or not;
- partition: every current and request set up to three entries and
  approved set up to two;
- NULL arrays for empty views, and NULL position buffers for an empty
  current, which C defines;
- 3000 random cases of each function: up to eight combinations (from the
  pool, small keys with stray bits, or any key and modifiers) and up to
  nine descriptions (the pool, NULL or random bytes), sometimes with
  bound a rotation of current so the descriptions decide.

Excluded, the intentional differences: a null view, a null array with a
nonzero count, and null outputs C would write through (checked on the
Rust side).

Mutation check (done once, not committed): 32 of 32 mutants of the core
and the shim fail the parity tests, 25 of them Tier 1 as well:
same_combo ignoring the modifiers; has skipping the last entry; the same
set covered one way only, ignoring descriptions, NULL equal to NULL, the
last match, the first match with a description, by position, or with an
off-by-one description guard; without a session ignoring the declined
set or binding an empty set; the retry override dropped, without its
session check or without its partly-declined check; the request always
or never filtered, the filter inverted, partly as <=; the partition
without the request filter, binding the whole request, binding an empty
part, <= the request, declining all of current or the part, or without
its first pass; the count stopping at the room; and in the shim,
descriptions compared by address, num_request not written, the request
buffer one short, part and declined swapped, bound's descriptions read
as current's, and a view's count cut short.

Refs obs-rust#1783

* docs: Record the portal shortcut combination sets port

The rust/README.md rows for rust/obs-hotkey (crate table and port
table), the naming.md row and the crate description now include
libobs/obs-nix-portal-shortcut-sets.c.h. The port row lists what stays C
(the portal client, its GDBus calls and allocations), the views the
shims read and the positions they write, the byte-wise description
compare, the in-place partition, what the characterization pins and the
port keeps, the intentional differences (null views and null outputs,
undefined in C), what Tier 3 compares, and why there is no
test_portal_shortcut_sets_rust.

Fixes obs-rust#1783
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