Skip to content

rust: Port the portal shortcut combination sets in obs-nix-portal-shortcuts.c to obs-hotkey #1783

Closed

Description

@rdoupe

Port the combination set logic of the GlobalShortcuts portal client (libobs/obs-nix-portal-shortcuts.c.h, Wayland with GIO, Linux and FreeBSD) to the existing crate rust/obs-hotkey as a new module, portal_shortcut_sets. This logic decides, on every sync of the hotkey bindings (500 ms after the last change), whether the portal session is left as it is or bound again, which keys go into a new BindShortcuts request, and what is bound again after the desktop's dialog is declined. If it is wrong, the desktop's dialog comes back on every sync (GNOME lists every shortcut again in it), or a changed binding is never bound. The id codec in the same client was ported in #1517.

The unit, about 90 lines:

  • same_combo(), has_combo() and covered_by(): combination equality (key and every modifier bit), membership, and "every combination of a is in b".
  • description_of() and same_set(): the same combinations, compared as sets (a duplicate counts once), each with the same description. A description is compared with strcmp, as bytes. A missing or NULL description on either side is "not the same".
  • select_combos(): the combinations in a filter (or not in it), with "OBS" for one without a description.
  • The partition inside bind_approved(): part = current ∩ approved ∩ request. It binds part again only when 0 < |part| < |request|, and the rest of current is declined.
  • The decisions inside sync_shortcuts(): unchanged (with a session, the same set as the one bound; without one, nothing to bind or all of it declined before; and a retry of a partly declined set always binds anew), the request filter (keys declined while others stay bound are left out until Ask again), and partly (the request is only part of the bindings).

These stay C: the description copies (copy_descriptions, free_descriptions, and the bstrdup of select_combos), collect_bindings() and action_label(), everything that calls GDBus, set_state() and set_flag() (they take the hotkeys mutex), and the portal thread.

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, the portal shortcut id codec, the binding codec and the key-name translations; the press state machine (#1751) is in flight there and touches other files. It is part of a large file, so it goes in through function extraction (testing-policy.md, "Extracting a function before porting it"), as the id codec did (#1518, #1519). Its Rust tests join the crate's existing tests/hotkey/ binary. No new test binary.

Dependencies and existing tests

  • combo_array_t is DARRAY(obs_key_combination_t) and description_array_t is DARRAY(char *), both file-local. The moved code reads them through two views declared in obs-nix-portal-shortcuts.h, struct obs_nix_portal_combos (pointer and count) and struct obs_nix_portal_descriptions (pointer to const char * and count), and returns positions in them. C keeps every allocation, so the port needs no darray or bmem across the boundary. obs_key_combination_t is public (obs-hotkey.h) and already has a layout test in obs-hotkey.
  • The moved file calls nothing from GIO. It is built under the same ENABLE_WAYLAND + GIO condition as the portal client, with no Cargo feature, as the id codec is.
  • Today: test_portal_shortcuts (28 cases, a fake portal on a private bus) covers the flows end to end: declined, declined then asked again, a declined new key keeping the approved keys, description changes, pause and probe. It compiles the C into itself (obs=hotkey_test_obs), so it cannot see a swap. Nothing tests the set logic on its own. PR 1 adds tests (below).
  • Input: combinations come from the user's own hotkey bindings (profiles, scene collections, the frontend, plugins), and descriptions are action labels built from hotkey and source names, which may be any bytes. The unit only compares them; it indexes nothing by them.

Plan

  1. Characterization and extraction PR (behaviour-neutral, two commits):
    • Four new end-to-end cases in test_portal_shortcuts, on the unchanged C: a reordered set is not bound again; descriptions are compared as bytes; only declined keys left binds nothing; a declined dialog for a set with no new key unbinds the approved key (characterized, not endorsed, below).
    • The move: the helpers and the three decisions go into libobs/obs-nix-portal-shortcut-sets.c.h, as hidden entry points (declared without EXPORT): obs_nix_portal_sets_unchanged(), obs_nix_portal_sets_request(), obs_nix_portal_sets_partition() and obs_nix_portal_sets_has() (for the Activated handler). same_combo() goes into obs-nix-portal-shortcuts.h, still static inline, as bindable() did. The file gets its own unity group, libobs.nix-portal-shortcut-sets, next to libobs.nix-portal-shortcuts, with a precompiled header without <gio/gio.h>. test_portal_shortcuts compiles it next to the client.
    • New test test_portal_shortcut_sets, which compiles the moved file with no GIO, D-Bus or libobs core and calls only the entry points. It pins empty sets on either side, order independence, the same key with other modifiers (every bit), duplicates, fewer descriptions than combinations, NULL descriptions, byte-wise compare (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 and each branch of the partition.
  2. Port PR (RED, then GREEN):
    • obs_hotkey::portal_shortcut_sets, a safe core over &[obs_key_combination_t] and &[Option<&CStr>] that returns bools and position lists, and src/ffi/portal_shortcut_sets.rs for the four entry points. Descriptions are compared as CStr bytes, never as str. The C file moves to its own ported group, libobs.ported-nix-portal-shortcut-sets, in LIBOBS_WAYLAND_GIO_PORTED_SOURCES, with its .pch.h, source-sets.json entry and export fragment, as libobs.ported-nix-portal-shortcut-ids has; ci/test_ported_sources.py gets the object.
    • Tests:
      • Tier 1 ports every test_portal_shortcut_sets expectation 1:1.
      • Tier 3 checks parity against the moved C through obs-c-oracle (oracle/portal_shortcut_sets.c): a proptest over random combination arrays (keys including OBS_KEY_NONE and mouse buttons, all modifier bits, duplicates), description arrays of other lengths with NULLs and arbitrary bytes, and every flag combination, comparing the results and the position lists.
      • Tier 2: test_portal_shortcut_sets_rust, the unchanged test_portal_shortcut_sets.c linked against libobs_rust-static instead of the C, as test_scene_bounds_place_rust does, so the hybrid build runs the characterization against the Rust code. test_portal_shortcuts keeps running on the C copy.
      • Tier 4 does not apply (no protocol change).
    • Docs: the rust/README.md row, and the docs/rust-port/naming.md row for rust/obs-hotkey (add obs-nix-portal-shortcut-sets.c.h).

For the reviewer to decide

  • Seam shape. The decisions take views of the arrays and return positions, so C keeps the darrays, the copies and the "OBS" fallback, and the Rust core is pure. The alternative, Rust owning the darrays, would need darray and bmem across the boundary for no gain.
  • What reaches the hybrid build. The entry points are hidden, so a test linking libobs.so cannot call them. PR 1's test_portal_shortcut_sets calls nothing else, so the port can link the same file against the Rust staticlib (test_scene_bounds_place_rust pattern). The 28 + 4 end-to-end cases compile the client and keep running on the C copy; they also compile obs-hotkey.c.h and the other ported hotkey files into themselves, so linking them against the staticlib as well could define the same symbols twice. I don't plan that; Tier 3 and the _rust test cover the swap, as for the id codec.
  • Behaviours the characterization pins, not endorses (none is changed by this plan):
    • 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: collect_bindings() holds each combination once, and its request is a subset of it.
    • The desktop's dialog for a set with no new key (an action moved onto an approved key, so only a description changed) is cancelled: the approved key is not bound again, because bind_approved() binds only a strict part of the request, so every key stops working until Ask again. Binding it again with the description it was approved with would keep it working without a dialog. That would change behaviour, so it needs its own issue and a user-visible check on GNOME and KDE first; I propose no fix before the port.

Activity

  1. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1783

    1. Characterization and move PR (next, ready): the combination-set logic moved into libobs/obs-nix-portal-shortcut-sets.c.h behind hidden entry points, with 4 new end-to-end cases in test_portal_shortcuts and a new test_portal_shortcut_sets (12 cases, no GIO/D-Bus). No behaviour change.
    2. Port to rust/obs-hotkey (module portal_shortcut_sets), with test_portal_shortcut_sets_rust linked against the Rust library and oracle parity tests.
  2. added
    claimed
    on Oct 22, 2026
  3. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Port is up in #1800. Intentional differences from C, only for inputs that are undefined there and that the portal client never passes: a null view, or a null array with a nonzero count, is read as an empty set, and a null count or position buffer is not written. The three behaviours pinned as not endorsed in #1784 are kept.

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