Skip to content

rust: Port the hotkey key-name translations in obs-hotkey.c to obs-hotkey #1656

Closed

Description

@rdoupe

Port the hotkey key-name translations of libobs/obs-hotkey.c.h to the existing crate rust/obs-hotkey as a new module, translations. At startup the frontend passes obs_hotkeys_set_translations() 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 these names through obs_key_to_str() and obs_key_combination_to_str().

  • obs_hotkeys_set_translations_s (EXPORT): it copies min(size, sizeof) bytes of the public struct, so an older, shorter struct reads as NULL tails. Then it applies the ADD_TRANSLATION table (with the #ifdef __APPLE__ keypad block) and expands the numpad and mouse templates. The expansion depads the template (only ' ', '\t', '\n' and '\r'), appends " %1" if it has no %1, and replaces every %1, left to right and without rescanning, with the digits.

About 130 lines. These stay C:

  • the store, obs_set_key_translation (it bfrees and bstrdups into obs->hotkeys.translations[]). It becomes a hidden wrapper, obs_hotkeys_store_key_translation();
  • obs_get_hotkey_translation, and the platform key_to_str functions that read the array (obs-nix-x11.c.h, obs-nix-wayland.c.h, obs-windows.c.h, obs-cocoa.m.h);
  • obs_hotkeys_set_audio_hotkeys_translations and obs_hotkeys_set_sceneitem_hotkeys_translations, which are plain setters.

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 and the binding codec (#1621). It is part of a large file, so it goes in through function extraction (testing-policy.md, "Extracting a function before porting it"). Its Rust tests join the crate's existing tests/hotkey/ binary. No new test binary.

Dependencies and existing tests

  • struct obs_hotkeys_translations (obs-hotkey.h) is a public, stable layout of 38 const char *. Its size-prefixed copy is kept as it is. The struct is #ifndef SWIG, so scripts never pass it.
  • One accessor: the hidden store wrapper obs_hotkeys_store_key_translation(obs_key_t, const char *), declared in obs-internal.h without EXPORT. Rust exports obs_hotkeys_set_translations_s and calls the wrapper with each formatted string. No struct internals are exposed.
  • The template expansion needs dstr_depad and dstr_replace semantics. The safe core implements them over bytes, for example fn expand(template: &[u8], digits: &str) -> Option<Vec<u8>> plus the key table. It needs no obs-util dependency. %1 is not a printf format, and the port must keep it that way: no template is ever passed through format! or printf.
  • Today there is no test of the translations. The frontend calls them once, in OBSBasic::InitHotkeys(). test_hotkey_load, test_wayland_hotkeys and test_portal_shortcuts compile obs-hotkey.c.h into themselves but never set translations. PR 1 adds a real-libobs test.
  • The input is trusted: the strings come from the frontend's own installed locale files (frontend/data/locale/*.ini). No locale shipped today has an empty or padding-only numpad or mouse template, here or upstream (all 136 entries in the 77 locales have %1). A plugin could call the exported function, but plugins run in-process anyway.

Plan

  1. Characterization and extraction PR (behaviour-neutral, one PR, as in libobs: Move the hotkey binding codec into obs-hotkey-bindings.c #1622):
    • New test test_hotkey_translations, linking OBS::libobs with obs_startup() (under xvfb-run on Linux) and using only the public API. It sets translations through obs_hotkeys_set_translations_s() and reads them back through obs_key_to_str() and obs_key_combination_to_str(). It pins the English defaults, every named key, NUM0-NUM9 and MOUSE1-MOUSE29, and these template cases: with and without %1, %1 twice, "%1%1", "%%1", "%11", padded, \v (which is not padding), printf-like text, UTF-8 and non-UTF-8 bytes. It also pins a NULL member (the old name is kept), a NULL struct, a short and a larger size, setting twice, an empty name, the apple_* members (ignored off macOS), and combinations.
    • obs_hotkeys_set_translations_s moves verbatim into libobs/obs-hotkey-translations.c.h, in the same unity group (libobs.core, just before obs-hotkey.c.h). The store becomes the hidden obs_hotkeys_store_key_translation(). test_hotkey_load, test_wayland_hotkeys and test_portal_shortcuts compile the new file next to obs-hotkey.c.h. The __APPLE__ conditional moves with the code, so the platform-ifdef baseline goes from 1 to 1.
  2. Fix PR, in C, before the port (RED, then GREEN). It fixes both bugs below. The RED commit flips the arrow-key case in test_hotkey_translations and adds the empty and padding-only template cases, which crash on master. The GREEN commit has two parts:
    • It skips an empty or padding-only numpad or mouse template as if it were NULL. The keys keep their names, so a translation that was never filled in shows the English default.
    • It adds the four arrow-key ADD_TRANSLATION lines.
  3. Port PR (RED, then GREEN):

The plan could also have been four PRs: the crash fix in C (2), the port (3), then the arrow-key fix (4). One fix PR before the port is better, for four reasons:

  • Both bugs are in the same 130-line function and are pinned by the same new test. A fix PR touches the same test cases either way.
  • With both bugs fixed first, the port only has to copy fixed C. Its Tier 1 and Tier 3 tests carry no "characterized, not endorsed" cases. Fixed after the port, the arrow-key fix has to be made twice, in the C (still the libobs-c-reference and OBS_LIBOBS_BACKEND=C copy) and in the Rust, plus the Tier 1 port of the test, as libobs: Drop empty hotkey bindings and signal when a load clears them #1649 had to.
  • Neither fix waits on the other or on the port. The crash fix must not wait for the port, and the arrow-key fix is spec-following (the struct and the frontend already supply the strings).
  • Precedent: libobs: Drop empty hotkey bindings and signal when a load clears them #1649 made two fixes found by the same characterization in one PR.

The cost is a slightly larger fix PR with two independent changes. Each has its own test cases and its own docs/features.md line, so either can be reverted alone.

The move and the port make no intended behaviour change. PR 1 pins two behaviours, and PR 2 fixes them:

  • The arrow keys are never translated. obs_hotkeys_set_translations_s() has no ADD_TRANSLATION for left, right, up or down, though the struct has them and the frontend sets them (Hotkeys.Left and the others). So on Linux (X11 and Wayland) Settings > Hotkeys always shows English "Left", "Right", "Up" and "Down", whatever the language. Windows takes these names from the OS, and macOS shows glyphs for them, so neither is affected. Upstream OBS has the same code. This is cosmetic.
  • An empty or padding-only numpad or mouse template crashes. For "", dstr_copy() leaves the template's dstr NULL, and for a padding-only string dstr_depad() frees it. dstr_find() then calls strstr(NULL, "%1"), which crashes obs_hotkeys_set_translations_s(), and with it the app at startup. This is pinned by a comment only: as a test case it would crash the test. No locale shipped today (here or upstream) has such a template, so users can hit it only through a hand-edited or third-party locale file, or a plugin passing the struct. Upstream OBS has the same code.

Activity

  1. rdoupe commented on Oct 21, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1656

    1. Characterization and move PR: test_hotkey_translations (14 cases, real libobs), and the translations moved verbatim into libobs/obs-hotkey-translations.c.h. Opening it now.
    2. One C fix PR for the two pinned bugs (arrow-key translations never applied; an empty template string leaves the name NULL), before the port.
    3. The port, as rust/obs-hotkey's translations module, as its own PR.
  2. added
    claimed
    on Oct 21, 2026
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