Skip to content

libobs: Port the hotkey key-name translations to obs-hotkey - #1682

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

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1656 (PR 3 of 3; #1657 moved the translations into libobs/obs-hotkey-translations.c.h and added test_hotkey_translations, #1673 fixed the arrow keys and the empty templates)

What and why

This PR ports libobs/obs-hotkey-translations.c.h, the hotkey key-name translations, to a new translations module of rust/obs-hotkey (#444). At startup the frontend passes obs_hotkeys_set_translations_s() its translated names of the named keys (Insert, Page Up, Shift, Numpad /, ...) and two templates, "Numpad %1" and "Mouse %1". libobs stores each name and expands the templates into the names of NUM0-NUM9 and MOUSE1-MOUSE29; Settings > Hotkeys shows them. The store, obs_hotkeys_store_key_translation(), and its readers (obs_get_hotkey_translation() and the platform key_to_str functions) stay C. The hybrid libobs drops the C file and links the Rust shim in its place. The port copies the C as fixed by #1673, so Tier 1 and Tier 3 carry no "characterized, not endorsed" cases.

  • Safe core (src/translations.rs):
    • Translations holds the 38 members of struct obs_hotkeys_translations (Member, in struct order), each a string or None for NULL.
    • named_keys() is the ADD_TRANSLATION table in the C's order, arrow keys included, for each keypad. Keypad::Standard is every host but macOS. Keypad::Apple is the C's #ifdef __APPLE__ block: the apple_keypad_* members, = included.
    • prepare_template() is the C's dstr_copy and dstr_depad of a template, with " %1" appended when it has no %1. Only ' ', '\t', '\n' and '\r' count as padding, not '\v' or '\f'. An empty or padding-only template gives None and is skipped like a NULL one (libobs: Translate the arrow key names and skip empty hotkey templates #1673).
    • replace_placeholder() is dstr_replace(&button, "%1", digits): every %1, left to right and without rescanning, is replaced as plain bytes. No template ever goes through format! or printf, so %s, %d and %n stay literal.
    • for_each_store() makes the C's store calls in the C's order: the named keys, then NUM0-NUM9, then MOUSE1-MOUSE29. A named key's string is stored as it is, empty or not.
    • The strings come from the frontend's locale files and from plugins, and they are bytes, not UTF-8. 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, unwraps and panics inside the module fails clippy with all four. No host cfg, no unsafe.
  • Shim (src/ffi/translations.rs): obs_hotkeys_set_translations_s with the C signature, and the #[repr(C)] struct obs_hotkeys_translations.
  • C side: obs-hotkey-translations.c.h leaves libobs.core for its own group, libobs.ported-hotkey-translations, listed in source-sets.json (LIBOBS_PORTED_SOURCES). Its PCH, libobs-ported-hotkey-translations.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: obs_hotkeys_set_translations_s is EXPORT (public API), so unlike libobs: Port the hotkey binding codec to obs-hotkey #1628's hidden shims it is exported. It is listed in a new fragment, rust-exports.d/obs-hotkey-translations.txt (the Windows /EXPORT:; export_list passes). There is nothing to add to rust-hidden.txt, rust-exports.map or rust-unexports-macos.txt. obs_hotkeys_store_key_translation stays C and local. nm shows T obs_hotkeys_set_translations_s and t obs_hotkeys_store_key_translation in all three libraries. The exported symbol sets are unchanged (1766).
  • Oracle (oracle/hotkey_translations.c): the original file, unmodified, with the function renamed. Its dstr calls are renamed to the C dstr oracle (the same renames as oracle/dstr.c), so the oracle expands its templates on the original util/dstr.c, not on the Rust dstr port. It also reports the struct's size, alignment and member offsets. Under cargo test, the test binary defines the store (hotkey_store in tests/hotkey/main.rs, like the existing hotkey_mutex stand-ins) and records each call for both sides.
  • Intentional differences: none.
  • Docs: the rust/README.md rows, the docs/rust-port/naming.md row and the crate's Cargo description. No docs/features.md line: there is no user-visible change.

test_hotkey_translations (16 cases) links OBS::libobs and goes through the public API only: obs_hotkeys_set_translations_s(), read back with obs_key_to_str() and obs_key_combination_to_str(). That makes it the end-to-end proof: in the hybrid libobs it runs the Rust shim and core through real libobs.

RED → GREEN

  • RED commit: fc9b50371 libobs: Add Tier 1 tests for the hotkey translations port. The core is todo!(). soldr cargo test -p obs-data --test obs-tests-core hotkey::translations fails 29 of 32 with "not yet implemented". The member order, the numbered key tables and defaults_before_any_set (which sets nothing) pass.
    • Tier 1 (tests/hotkey/translations.rs) ports test_hotkey_translations 1:1: the 16 cases under the same names, in the same order, with the same strings and the same expected names.
      • The C around the core stays C, so the tests repeat it in a small model (Store): the shim's min(size, sizeof) copy (a NULL struct stores nothing; a cut at a member boundary leaves the members after it NULL), the store, obs_nix_x11_key_to_str() with its English defaults, and obs_key_combination_to_str() (control, meta, alt, shift, key; empty names left out).
      • The C test is Linux and FreeBSD only, so these cases use Keypad::Standard.
    • 16 new edge cases:
      • the member order and names against the header, and get and set of each member alone;
      • the named key tables of both keypads, in the C's order, arrow keys included, and the template member of each;
      • NUM0-NUM9 with "0"-"9", MOUSE1-MOUSE29 with "1"-"29";
      • every byte 1-255 on both ends of a string: only the four padding bytes are stripped, inner padding stays, and '\v', '\f' and U+00A0 are not padding;
      • template preparation ("", padding only, "%", "1", "% 1", "1%", "%%11", "\v");
      • the replacement without rescanning ("%%1", "%%11", "%1%", "%11%1" with "10", empty digits, digits "%1");
      • printf conversions (%s %n %d %x %p %% %*s %ls, %1$s) stay literal, with and without %1;
      • 10000 placeholders, a 4 MiB template, and 1 MiB of padding on each side;
      • multi-byte UTF-8 next to %1, broken sequences around it, and every byte 1-255 around a placeholder;
      • the store order and count with every member set (68 calls, 69 on the Apple keypad);
      • the Apple keypad: apple_* name the keypad and =, numpad_* are ignored, and the reverse on Standard;
      • nothing set stores nothing;
      • a named key's string is stored exactly as given (not depadded, not expanded, empty included), and an ignored keypad member stores nothing;
      • an empty or padding-only template stores exactly what a NULL one does, for each template member and keypad;
      • proptests: the expansion of random bytes against a plain reference expansion (no panic, no NUL), and the store calls of random structs against the members.
    • 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: 607c9959e libobs: Port the hotkey key-name translations to obs-hotkey (rebased onto 1fc26dfc4; nothing since b8f97ed04 touches libobs, CMake or rust/ except build: preserve source options and add the macOS Cargo core graph #1669's obs-build changes, which the rust-test lane covers). hotkey::translations passes 37/37 (32 Tier 1, 5 Tier 3). The hotkey:: tests pass 129/129.
    • Tier 3 (tests/hotkey/translations_parity.rs), comparing against the C oracle:
      • the layout of struct obs_hotkeys_translations: size, alignment and all 38 offsets;
      • every template of test_hotkey_translations (and the empty ones of libobs: Translate the arrow key names and skip empty hotkey templates #1673), as each template member, alone and with every other member set, and as every member at once;
      • the whole struct at every member-boundary size, past the struct, at 0, all NULL, and a NULL struct;
      • large templates: 1 MiB, 100000 %1, and 64 KiB of padding on each side;
      • random structs: each member NULL, "" or random pieces (%1, %1%1%1, %, 1, %%, padding, \v, \f, %s%n%d, multi-byte UTF-8, a broken UTF-8 sequence, U+00A0, any single byte). The size is cut at a random member boundary, past the struct, or at any byte (with the member at the cut NULL), and some cases pass a NULL struct.
      • Compared: every store call in order (the key, the bytes, and which of the caller's pointers was passed on, if any), and the struct and the bytes after it left as they were.
    • 2000 cases per proptest. Excluded: a size that cuts a member that is not NULL. Both sides then copy part of a pointer, as memcpy does, and read through it, which is undefined behaviour in each.
    • Deliberate-break check: in the Rust core I dropped the down-arrow entry from the named key table, then restored it.
      • Tier 1 and 3: 6 of 37 tests failed. arrow_keys_translated is a ported C case. named_key_tables and store_order_and_count are edge cases. whole_struct_cases_match_c, characterization_templates_match_c and random_structs_match_c are Tier 3 parity tests.
      • With the hybrid libobs rebuilt: test_hotkey_translations failed arrow_keys_translated (OBS_KEY_DOWN: got "Down", expected "Runter", 15/16 passed), and passed 16/16 against libobs-c-reference.
      • After the restore, all of these pass again (37/37, and 16/16 against the hybrid libobs).
  • Local, two fresh build trees: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON. Full ctest -j4, 358 tests:
Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs 357/358: test_effect_parser_lifecycle_rust not built (see below) 1766
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 358/358 1766, IDENTICAL
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 358/358 1766, IDENTICAL
  • No test was skipped.
  • test_effect_parser_lifecycle_rust (rust: port effect parser lifecycle to obs-graphics #1650) links libobs_rust-static unconditionally, and that target does not exist with OBS_LIBOBS_BACKEND=C. So it fails to link there on master too (cannot find -llibobs_rust-static), as libobs: Translate the arrow key names and skip empty hotkey templates #1673 found. It is outside this change.
  • test_hotkey_translations passes 16/16 in all three. test_hotkey_bindings_codec (18), test_hotkey_load (7), test_wayland_hotkeys (8) and test_portal_shortcuts (28) pass in all three too. ldd shows each run loading the intended libobs.so.
  • Ninja's inputs: the hybrid libobs.so links liblibobs_rust.a and not libobs.ported-hotkey-translations+.h.c.o. libobs-c-reference links that object.
  • 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::translations and hotkey::translations_parity (Tier 1 and 3, every proptest) pass 37/37 under AddressSanitizer, with no reports. The Rust side was built with -Zsanitizer=address, and the C oracle with -fsanitize=address.
  • ci/local_gate.py:
    • --lane lint: passed.
    • --lane cross: passed.
    • --lane cross-release: aarch64-pc-windows-msvc and x86_64-apple-darwin clippy and dylint passed. aarch64-unknown-linux-gnu could not run on the build host: since rust: Port session D-Bus sleep inhibitor to obs-util #1577, obs-util's build script probes gio-2.0 through pkg-config, and soldr's aarch64 sysroot has none (Package 'gio-2.0' not found). That is a host limitation, unrelated to this change; the only host cfg this PR adds is target_os = "macos".
    • --lane rust-test: passed (2018 tests, 0 failed: obs-tests-core 505/505, the obs-util harness 751/751, including export_list).
  • 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, ci/test_platform_ifdef_ratchet.py: OK.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change). The Settings > Hotkeys names come from obs_key_to_str(), which test_hotkey_translations reads through the public API of the real hybrid libobs.

Windows/macOS: not built there. The Windows export is the new rust-exports.d fragment. On macOS the shim takes Keypad::Apple, and the oracle compiles the C's __APPLE__ branch, so Tier 3 compares that layout there. Tier 1 covers both layouts on every host.

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_translations, landed in libobs: Move the hotkey key-name translations into obs-hotkey-translations.c #1657)
  • 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): none

RED for the port of libobs/obs-hotkey-translations.c.h to the
translations module of rust/obs-hotkey. The safe core is a stub
(todo!()): named_keys(), the ADD_TRANSLATION table in the C's order for
each keypad (Standard, and Apple for the C's #ifdef __APPLE__ block);
depad() and prepare_template(), the dstr_copy and dstr_depad of a
template with " %1" appended when it has none, None for an empty or
padding-only one; replace_placeholder(), dstr_replace of every "%1" by
the digits, as plain byte replacement; and for_each_store(), the store
calls in the C's order. The members of struct obs_hotkeys_translations
(Member, Translations) and the NUM0-NUM9 and MOUSE1-MOUSE29 tables are
plain data. build.rs names the key codes the table needs.

tests/hotkey/translations.rs ports test_hotkey_translations 1:1: its 16
cases under the same names, in the same order, with the same strings and
the same expected names. The C test reads the names back through
obs_key_to_str() and obs_key_combination_to_str() of a real libobs on
Linux; the test repeats the C around the core in a small model: the
shim's min(size, sizeof) copy of the struct, the store, the X11 English
defaults and the combination string.

Edge cases: the member order; the named key tables of both keypads; the
numbered key tables; only ' ', '\t', '\n' and '\r' are padding, on
either end; templates; the replacement without rescanning; printf
conversions stay literal; 10000 placeholders and 4 MiB templates; UTF-8
and non-UTF-8 bytes; the store order and count; the Apple keypad;
nothing set stores nothing; named strings are stored as they are; an
empty template stores what a NULL one does; and two proptests, against
a plain reference expansion and over random members.

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::translations
fails 29 of 32 with "not yet implemented"; the member order, the
numbered key tables and defaults_before_any_set (which sets nothing)
pass.

Refs obs-rust#1656
Ports libobs/obs-hotkey-translations.c.h to the translations module of
rust/obs-hotkey. obs_hotkeys_set_translations_s() stores the frontend's
translated names of the named keys, and expands its numpad and mouse
templates ("Numpad %1", "Mouse %1") into the names of NUM0-NUM9 and
MOUSE1-MOUSE29. The store, obs_hotkeys_store_key_translation(), and its
readers stay C in obs-hotkey.c.h and the platform files; the hybrid
libobs drops the C file and links the Rust shim in its place.

Safe core (src/translations.rs): the ADD_TRANSLATION table in the C's
order, arrow keys included, for each keypad (Keypad::Standard, and
Keypad::Apple for the C's #ifdef __APPLE__ block, with the "=" key);
depad() of ' ', '\t', '\n' and '\r' only; prepare_template(), with " %1"
appended when there is none and None for an empty or padding-only
template, which is skipped like a NULL one; replace_placeholder(), every
"%1" left to right without rescanning, as byte replacement, never a
format; and for_each_store(), the store calls in the C's order. The
strings come from locale files and plugins, so the module denies
clippy's indexing_slicing, arithmetic_side_effects, unwrap_used,
expect_used, panic, todo, unimplemented and unreachable; nothing panics
on any bytes.

Shim (src/ffi/translations.rs): obs_hotkeys_set_translations_s with the
C signature, and the #[repr(C)] struct obs_hotkeys_translations. As in
C, a NULL struct returns, and min(size, sizeof) bytes are copied into a
zeroed struct, so an older, shorter struct reads as NULL tails. Each
name goes to the hidden C store through FFI; a named key's string is
passed on as the caller's pointer, as in C. The keypad is chosen with
cfg(target_os = "macos"), in the shim. No intentional differences.

C side: the file leaves libobs.core for its own group,
libobs.ported-hotkey-translations (LIBOBS_PORTED_SOURCES), whose PCH is
obs-internal.h. The function is EXPORT, so the shim is exported:
rust-exports.d/obs-hotkey-translations.txt. The exported symbol sets
are unchanged (1766). test_hotkey_load, test_wayland_hotkeys and
test_portal_shortcuts still compile the C file into themselves,
unchanged.

Oracle (oracle/hotkey_translations.c): the original file with the
function renamed, on the C dstr oracle (the same renames as
oracle/dstr.c), so its expansion runs on the original util/dstr.c; plus
the struct layout. Under cargo test the test binary defines the store
and records each call, for both sides.

Tier 3 (tests/hotkey/translations_parity.rs): the layout; every template
of test_hotkey_translations, alone and with every other member set;
large templates; and random structs (NULL or random strings of "",
padding, repeated "%1", printf conversions, multi-byte UTF-8 and single
bytes), with the size cut at every member boundary, past the struct, or
at any byte with the cut member NULL, and a NULL struct. Compared: every
store call in order (key, bytes, and which caller pointer was passed
on), and the struct left as it was. Excluded: a size that cuts a member
that is not NULL, which reads a partial pointer in both.

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

Fixes obs-rust#1656
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 00:31
@zackees
zackees enabled auto-merge October 22, 2026 00:41
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit fead860 Oct 22, 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 hotkey key-name translations in obs-hotkey.c to obs-hotkey

2 participants