Skip to content

libobs: Move the hotkey key-name translations into obs-hotkey-translations.c - #1657

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/hotkey-translations-move
Oct 21, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/hotkey-translations-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Refs #1656 (PR 1 of the plan: the characterization and the extraction. The fix PR and the port follow.)

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 hotkey key-name translations move from libobs/obs-hotkey.c.h into libobs/obs-hotkey-translations.c.h. At startup the frontend passes in its translated names of the named keys and two templates, "Numpad %1" and "Mouse %1". Settings > Hotkeys shows the names through obs_key_to_str(). The move comes with a real-libobs characterization test, test_hotkey_translations. There is no Rust, no behaviour change and no public header change.

  • What moves: obs_hotkeys_set_translations_s(), verbatim, with its ADD_TRANSLATION, ADD_NUMPAD_NUM and ADD_MOUSE_NUM macros and their #undefs. The only edit is the name of the store it calls (below).
  • What stays: the store, obs_set_key_translation(), stays in obs-hotkey.c.h under a new name, obs_hotkeys_store_key_translation(), with the same body. It is declared in obs-internal.h without EXPORT, so it is hidden. It is the one accessor the port will call. obs_get_hotkey_translation(), the platform key_to_str functions that read obs->hotkeys.translations[], and the audio and scene item translation setters also stay.
  • Build: the new file joins the existing libobs.core group just before obs-hotkey.c.h, in alphabetical order, with libobs.pch.h like every member of that group. The group, the PCH, source-sets.json and the CMake source lists are unchanged, and ci/test_build_groups.py passes.
  • Platform conditionals: the #ifdef __APPLE__ keypad block moves with the code, so the platform-ifdef baseline goes from 1 to 1: its entry moves from obs-hotkey.c.h to the new file. facade: libobs top-level files (obs-module, obs-display, obs-hotkey, obs-source, obs-internal, gpu-encode, audio-controls, media-remux): move 14 platform conditionals #870 tracks the facade move of this conditional; this PR leaves that to facade: libobs top-level files (obs-module, obs-display, obs-hotkey, obs-source, obs-internal, gpu-encode, audio-controls, media-remux): move 14 platform conditionals #870.
  • Tests that compile obs-hotkey.c.h themselves: test_hotkey_load, test_wayland_hotkeys and test_portal_shortcuts now compile obs-hotkey-translations.c.h next to it, as they do obs-hotkey-bindings.c.h (CMake, and the obs-build C test manifest for test_hotkey_load). Their test files are unchanged.
  • Exported symbols: unchanged, 1766 in libobs with the C backend, in the hybrid libobs and in libobs-c-reference. obs_hotkeys_store_key_translation is a local (t) symbol.

test_hotkey_translations links the real libobs. It calls obs_startup() (under xvfb-run on Linux, like test_hotkey_bindings_codec) and uses only the public API: it sets translations with obs_hotkeys_set_translations_s() and reads them back through obs_key_to_str() and obs_key_combination_to_str(). It is built on Linux and FreeBSD only, because there obs_key_to_str() shows the stored name of every key the struct names. Windows shows only the mouse buttons, Pause and the Windows key, and macOS only some of the keys (Space, the keypad and the mouse buttons, for example). The 14 cases:

  • Defaults: before any set, the platform's English names ("Insert", "Page Up", "Super", "Numpad /", "Numpad 0", "Mouse 29", ...). This case runs first, because the store is per process and the API can't clear it.
  • Every member: each of the 25 named keys, NUM0-NUM9 and MOUSE1-MOUSE29. A second set replaces every name it sets.
  • NULL and size:
    • A NULL member, an all-NULL struct and a NULL struct each keep the old names.
    • size 0 reads nothing.
    • A size cut at left, at mouse_num and at escape reads only the members before the cut, so an older, shorter struct reads as NULL tails.
    • A size larger than the struct reads only the struct.
  • An empty name is stored as it is. The key then shows as nothing, and a combination leaves it out.
  • Numpad and mouse templates:
    • with and without %1, %1 twice, "%1%1", "%%1" and "%11" (replaced left to right, without rescanning), "%2", "% 1" and "100%";
    • padded with ' ', '\t', '\r' and '\n', which are depadded, and \v, which is not;
    • printf-like "%d %s", which is kept as text;
    • UTF-8 (German, Japanese, Arabic) and non-UTF-8 bytes;
    • the two templates are independent of each other.
  • apple_* members: on Linux they change nothing.
  • Combinations: translated modifiers and keys, in the order control, meta, alt, shift, key.

Characterized, not endorsed (a comment in the test, and in the issue):

  • The arrow keys are never translated. The struct has left, right, up and down, and the frontend sets them, but obs_hotkeys_set_translations_s() has no ADD_TRANSLATION for them. So on Linux (X11 and Wayland) the arrow keys always show English "Left", "Right", "Up" and "Down". Upstream has the same code.

Left out of the test on purpose, and the follow-up fix:

  • An empty or padding-only numpad or mouse template crashes. For "", dstr_copy() leaves the template NULL, and for " " dstr_depad() frees it. dstr_find() then calls strstr(NULL, "%1"). As a test case it would crash the test, so it is described here and in a comment instead. No locale shipped today, here or upstream, has such a template: all 136 Hotkeys.NumpadNum, Hotkeys.AppleKeypadNum and Hotkeys.MouseButton entries in the 77 locales contain %1. The input comes from the frontend's installed locale files, so this is a local crash, not untrusted input.
  • The plan fixes both bugs in one C PR before the port, so the port copies fixed C and its Tier 1 and Tier 3 tests carry no exceptions. That PR skips an empty or padding-only template as if it were NULL, adds the four arrow-key lines, and flips the arrow-key case.

RED → GREEN

  • RED commit: none. This is a move plus a characterization test, which passes on the moved code and on master. The commit is 239924e47 libobs: Move the hotkey key-name translations into obs-hotkey-translations.c.
  • test_hotkey_translations passes 14/14, run 3 times against each of libobs with the C backend, the hybrid libobs and libobs-c-reference. It also passes 14/14 against a libobs built from master's obs-hotkey.c.h (the libobs changes reverted, the test kept), so the move changes nothing the test can see.
  • Mutation check of the new test: 11 of 11 mutants fail it. Each was built into libobs and run against the test. The mutants:
    • an arrow-key line added (the bug 1 fix);
    • the numpad template not depadded;
    • " %1" not appended to the mouse template;
    • size ignored;
    • a NULL member clearing the name;
    • an empty member skipped;
    • Menu not stored;
    • MOUSE29 not expanded;
    • the Apple template used on Linux;
    • the numpad divide and multiply names swapped;
    • the replace looking for "%1 ".
  • 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 356/356 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 356/356 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 356/356 IDENTICAL (1766)

Nothing was skipped or failed to run. test_hotkey_bindings_codec (18/18), test_hotkey_load (7/7), test_wayland_hotkeys (8/8) and test_portal_shortcuts (28/28) pass, unchanged, against all three. After the final rebase onto b133a77 (#1655), both trees were rebuilt, and the hotkey tests and the symbol comparison passed again.

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. clang-format 23.1 and gersemi are clean on the changed files. ci/test_build_groups.py passes, and so does ci/platform_ifdef_ratchet.py.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

Windows/macOS: not built there. The moved code's one host conditional (__APPLE__) is unchanged. The new test is built for Linux and FreeBSD only, and it is portable C (no cmocka range assertions). Without a display, obs_startup() fails, and the test prints a skip and exits 0, as test_hotkey_bindings_codec does.

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 fix and the port follow)
  • 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; the platform-ifdef baseline entry only moves with the code)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: no port here)

…tions.c

Behavior-neutral extraction before the Rust port:
obs_hotkeys_set_translations_s() moves verbatim from libobs/obs-hotkey.c.h
into libobs/obs-hotkey-translations.c.h. It stores the frontend's
translated names of the named keys and expands the numpad and mouse
templates ("Numpad %1", "Mouse %1") into the names of NUM0-NUM9 and
MOUSE1-MOUSE29. The store, obs_set_key_translation(), stays in
obs-hotkey.c.h as obs_hotkeys_store_key_translation(), declared in
obs-internal.h without EXPORT; the moved code calls it under that name.
The ADD_TRANSLATION, ADD_NUMPAD_NUM and ADD_MOUSE_NUM #undefs move with
the macros.

The new file joins libobs.core just before obs-hotkey.c.h, with
libobs.pch.h. No group, PCH or source-sets.json change; the exported
symbols are unchanged and the store is hidden. The __APPLE__ keypad block
moves with the code (platform-ifdef baseline 1 -> 1). test_hotkey_load,
test_wayland_hotkeys and test_portal_shortcuts compile obs-hotkey.c.h on
their own, so they now compile the new file next to it (CMake and the
obs-build manifest).

New characterization test test_hotkey_translations. It links the real
libobs (obs_startup, under xvfb-run) and uses the public API only: it sets
translations with obs_hotkeys_set_translations_s() and reads them through
obs_key_to_str() and obs_key_combination_to_str(). Linux and FreeBSD
only, where obs_key_to_str() shows the stored name of every key the
struct names. It pins the English defaults; every named key, NUM0-NUM9
and MOUSE1-MOUSE29; the numpad and mouse templates with and without %1,
%1 twice, "%1%1", "%%1", "%11", padded, \v (not padding), printf-like
text, UTF-8 and non-UTF-8 bytes; a NULL member and a NULL struct (names
kept); a short and a larger size; setting twice; an empty name; the
apple_* members (ignored off macOS); and combinations.

Characterized, not endorsed: the struct's left, right, up and down are
never stored, so the arrow keys always show English names on Linux.

Not in the test: an empty or padding-only numpad or mouse template leaves
the template string NULL, and strstr(NULL, "%1") then crashes. That gets
its own fix.

Refs obs-rust#1656
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