Repository navigation
libobs: Port the hotkey key-name translations to obs-hotkey - #1682
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1656 (PR 3 of 3; #1657 moved the translations into
libobs/obs-hotkey-translations.c.hand addedtest_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 newtranslationsmodule ofrust/obs-hotkey(#444). At startup the frontend passesobs_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 ofNUM0-NUM9andMOUSE1-MOUSE29; Settings > Hotkeys shows them. The store,obs_hotkeys_store_key_translation(), and its readers (obs_get_hotkey_translation()and the platformkey_to_strfunctions) 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.src/translations.rs):Translationsholds the 38 members ofstruct obs_hotkeys_translations(Member, in struct order), each a string orNoneforNULL.named_keys()is theADD_TRANSLATIONtable in the C's order, arrow keys included, for each keypad.Keypad::Standardis every host but macOS.Keypad::Appleis the C's#ifdef __APPLE__block: theapple_keypad_*members,=included.prepare_template()is the C'sdstr_copyanddstr_depadof 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 givesNoneand is skipped like aNULLone (libobs: Translate the arrow key names and skip empty hotkey templates #1673).replace_placeholder()isdstr_replace(&button, "%1", digits): every%1, left to right and without rescanning, is replaced as plain bytes. No template ever goes throughformat!or printf, so%s,%dand%nstay literal.for_each_store()makes the C's store calls in the C's order: the named keys, thenNUM0-NUM9, thenMOUSE1-MOUSE29. A named key's string is stored as it is, empty or not.indexing_slicing,arithmetic_side_effects,unwrap_used,expect_used,panic,todo,unimplementedandunreachable. I checked that the lints fire: a probe that indexes, adds, unwraps and panics inside the module fails clippy with all four. No hostcfg, nounsafe.src/ffi/translations.rs):obs_hotkeys_set_translations_swith the C signature, and the#[repr(C)]struct obs_hotkeys_translations.NULLstruct returns.min(size, sizeof)bytes are copied into a zeroed struct, so an older, shorter struct reads asNULLtails, and a largersizereads only the struct.bstrdups it.cfg(target_os = "macos")(src/ffi/is inside the platform boundary). The core has both layouts, so both are tested on every host. 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 still tracks the__APPLE__conditional in the C.obs-hotkey-translations.c.hleaveslibobs.corefor its own group,libobs.ported-hotkey-translations, listed insource-sets.json(LIBOBS_PORTED_SOURCES). Its PCH,libobs-ported-hotkey-translations.pch.h, isobs-internal.h. The only change to the file is that include and its header comment.test_hotkey_load,test_wayland_hotkeysandtest_portal_shortcutsstill compile it into themselves, unchanged.obs_hotkeys_set_translations_sisEXPORT(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_listpasses). There is nothing to add torust-hidden.txt,rust-exports.maporrust-unexports-macos.txt.obs_hotkeys_store_key_translationstays C and local.nmshowsT obs_hotkeys_set_translations_sandt obs_hotkeys_store_key_translationin all three libraries. The exported symbol sets are unchanged (1766).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 asoracle/dstr.c), so the oracle expands its templates on the originalutil/dstr.c, not on the Rust dstr port. It also reports the struct's size, alignment and member offsets. Undercargo test, the test binary defines the store (hotkey_storeintests/hotkey/main.rs, like the existinghotkey_mutexstand-ins) and records each call for both sides.rust/README.mdrows, thedocs/rust-port/naming.mdrow and the crate's Cargodescription. Nodocs/features.mdline: there is no user-visible change.test_hotkey_translations(16 cases) linksOBS::libobsand goes through the public API only:obs_hotkeys_set_translations_s(), read back withobs_key_to_str()andobs_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
fc9b50371libobs: Add Tier 1 tests for the hotkey translations port. The core istodo!().soldr cargo test -p obs-data --test obs-tests-core hotkey::translationsfails 29 of 32 with "not yet implemented". The member order, the numbered key tables anddefaults_before_any_set(which sets nothing) pass.tests/hotkey/translations.rs) portstest_hotkey_translations1:1: the 16 cases under the same names, in the same order, with the same strings and the same expected names.Store): the shim'smin(size, sizeof)copy (aNULLstruct stores nothing; a cut at a member boundary leaves the members after itNULL), the store,obs_nix_x11_key_to_str()with its English defaults, andobs_key_combination_to_str()(control, meta, alt, shift, key; empty names left out).Keypad::Standard.NUM0-NUM9with"0"-"9",MOUSE1-MOUSE29with"1"-"29";'\v','\f'and U+00A0 are not padding;"", padding only,"%","1","% 1","1%","%%11","\v");"%%1","%%11","%1%","%11%1"with"10", empty digits, digits"%1");%s %n %d %x %p %% %*s %ls,%1$s) stay literal, with and without%1;%1, broken sequences around it, and every byte 1-255 around a placeholder;apple_*name the keypad and=,numpad_*are ignored, and the reverse onStandard;NULLone does, for each template member and keypad;obs-tests-core. No new test binary:ci/test_binary_cap.pypasses.607c9959elibobs: Port the hotkey key-name translations to obs-hotkey(rebased onto1fc26dfc4; nothing sinceb8f97ed04touches 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::translationspasses 37/37 (32 Tier 1, 5 Tier 3). Thehotkey::tests pass 129/129.tests/hotkey/translations_parity.rs), comparing against the C oracle:struct obs_hotkeys_translations: size, alignment and all 38 offsets;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;NULL, and aNULLstruct;%1, and 64 KiB of padding on each side;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). Thesizeis cut at a random member boundary, past the struct, or at any byte (with the member at the cutNULL), and some cases pass aNULLstruct.sizethat cuts a member that is notNULL. Both sides then copy part of a pointer, asmemcpydoes, and read through it, which is undefined behaviour in each.arrow_keys_translatedis a ported C case.named_key_tablesandstore_order_and_countare edge cases.whole_struct_cases_match_c,characterization_templates_match_candrandom_structs_match_care Tier 3 parity tests.test_hotkey_translationsfailedarrow_keys_translated(OBS_KEY_DOWN: got "Down", expected "Runter", 15/16 passed), and passed 16/16 against libobs-c-reference.OBS_LIBOBS_BACKEND=C, andHYBRIDwithOBS_BUILD_C_REFERENCE=ON. Fullctest -j4, 358 tests:OBS_LIBOBS_BACKEND=Ctest_effect_parser_lifecycle_rustnot built (see below)HYBRID,OBS_BUILD_C_REFERENCE=ONHYBRID,OBS_BUILD_C_REFERENCE=ONtest_effect_parser_lifecycle_rust(rust: port effect parser lifecycle to obs-graphics #1650) linkslibobs_rust-staticunconditionally, and that target does not exist withOBS_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_translationspasses 16/16 in all three.test_hotkey_bindings_codec(18),test_hotkey_load(7),test_wayland_hotkeys(8) andtest_portal_shortcuts(28) pass in all three too.lddshows each run loading the intendedlibobs.so.libobs.solinksliblibobs_rust.aand notlibobs.ported-hotkey-translations+.h.c.o.libobs-c-referencelinks that object.CMocka_DIR, and ctest ran withLD_LIBRARY_PATHpointing at itslib.hotkey::translationsandhotkey::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-msvcandx86_64-apple-darwinclippy and dylint passed.aarch64-unknown-linux-gnucould not run on the build host: since rust: Port session D-Bus sleep inhibitor to obs-util #1577,obs-util's build script probesgio-2.0through 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 hostcfgthis PR adds istarget_os = "macos".--lane rust-test: passed (2018 tests, 0 failed:obs-tests-core505/505, the obs-util harness 751/751, includingexport_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.obs_key_to_str(), whichtest_hotkey_translationsreads through the public API of the real hybrid libobs.Windows/macOS: not built there. The Windows export is the new
rust-exports.dfragment. On macOS the shim takesKeypad::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
master(or the description starts with a Stacked: note)test_hotkey_translations, landed in libobs: Move the hotkey key-name translations into obs-hotkey-translations.c #1657)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable