Repository navigation
obs-hotkey: Port the macOS key map - #2917
Conversation
02abdef to
e8d0f71
Compare
Behaviour-neutral extraction before the Rust port. The key map and pressed check of the macOS hotkey platform move from libobs/obs-cocoa.m.h into libobs/obs-cocoa-keys.c.h, as hidden functions on plain values declared without EXPORT in the new libobs/obs-cocoa-keys.h: - obs_cocoa_keys_modifier(): KeyboardEventProc()'s map of the Shift, Alternate, Command and Control flag bits to the four modifier keys; - obs_cocoa_keys_to_virtual_key() and obs_cocoa_keys_from_virtual_key(): the bodies of obs_key_to_virtual_key() and obs_key_from_virtual_key(), with the virtual_keys table passed in; - obs_cocoa_keys_is_mouse(), obs_cocoa_keys_mouse_pressed() and obs_cocoa_keys_in_range(): obs_hotkeys_platform_is_pressed()'s mouse key range, its button bit and the key range gate. obs-cocoa.m keeps the tables, the key names, the event tap and its callback, the input source, every CGEvent, TIS, CF and NSEvent call (pressedMouseButtons is still asked only for a mouse key), the key state and its writes, the NULL platform check and every log. The two public functions stay there as one-line calls, so the exported symbols do not change. The new file includes no macOS header: obs_key_code_t moves to its header, the four flag bits and INVALID_KEY are defined there, and obs-cocoa.m _Static_asserts the bits against CoreGraphics and the sizes of CGEventFlags and NSUInteger. The new file gets its own unity group, libobs.macos-cocoa-keys, in the macOS sources (CMake and obs-build). Its precompiled header has no macOS header, so a C test and later the C oracle compile it on their own. Checked with a harness: master's functions and the moved ones (with their new callers) extracted over logging stand-ins for the CGEvent calls, pressedMouseButtons and the platform state, built with gcc and clang at -O0, -O2 and under ASan and UBSan, on 300 random tables (7-bit and wide codes, repeats, random validity) with 200 random events each (key down, key up, flags changed, tap timeout, other; random flag words and key codes) and the pressed check of every key with and without a platform: identical logs, 1,010,645 lines each. The mouse keys' button bit shifts an int by the key's value less one, past its width; that is undefined, and clang at -O2 returns an unset register for it, so it is compared at -O0 only (1,048,162 lines). 13 of 14 planted mutants (in the moved file and in its callers) change the log; the survivor returns true for a key other than the four modifiers, which no caller passes. The mac-arm64 cross-compile check (cmake/cross) builds the new group and obs-cocoa.m with no new warning. Refs #2914. Co-authored-by: PatTheMav <[email protected]> Co-authored-by: Sebastian Beckmann <[email protected]> Co-authored-by: Palana <[email protected]> Co-authored-by: Theodore Dubois <[email protected]>
test_cocoa_keys_steps builds the moved libobs/obs-cocoa-keys.c.h into itself and calls its hidden functions on made-up tables, so it needs no macOS header and runs on every platform (8 cases): - the modifier map: each flag alone sets only its key (Shift, Option to Alt, Command to Meta, Control), all four set all four, none and every other bit set none, and no other key reads the flags; - the virtual key of a key: a valid entry's code as stored (0, a negative code and INVALID_KEY's value included), INVALID_KEY for an invalid entry whatever its code, and the first and last entries read; - the key of a virtual code: the first valid entry with the code wins, an invalid entry with it is skipped, none for a code no valid entry has (0 in a zeroed table, INVALID_KEY, negative and wide codes), and OBS_KEY_NONE's own entry and the last one are searched; - the mouse key range, MOUSE1 to MOUSE29, and the key range gate, below OBS_KEY_LAST_VALUE. Not characterized: the button bit, which shifts an int by the key's value less one, past its width (undefined; clang at -O2 returns an unset register), and the virtual key of a key outside the table, which reads past its end. The port fixes both; its Tier 3 tests list them. Passes on the C libobs build. Refs #2914.
RED for the port of libobs/obs-cocoa-keys.c.h to a new module, cocoa_keys, of rust/obs-hotkey. The safe core is a stub (todo!()): modifier() and modifiers(), the event tap's flag map; to_virtual() and from_virtual() on a slice of KeyCode (obs_key_code_t, repr(C)); is_mouse(), mouse_bit(), mouse_pressed() and in_range(), the pressed check's decisions. tests/hotkey/cocoa_keys.rs ports test_cocoa_keys_steps 1:1, its 8 cases under the same names, in the same order, with the same inputs and expectations. The rest pin the edges: the four flags in order, the flag values, negative and wide keys, short tables, every code of a full table, and the KeyCode layout. Two pin the fixes: - to_virtual() gives INVALID_KEY for a key outside the table, where C reads past its end; - mouse_pressed() tests bit key - OBS_KEY_MOUSE1 (MOUSE1 the left button, bit 0, to MOUSE29, bit 28, and nothing past it), where C shifts an int by key - 1, past its width. Proptests check each function against a model written from the C. 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::cocoa_keys fails 24 of 26 with "not yet implemented" (the flag values and the layout pass). Refs #2914.
GREEN for #2914. Ports libobs/obs-cocoa-keys.c.h, the key map and pressed check of the macOS hotkey platform, to the cocoa_keys module of rust/obs-hotkey. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/cocoa_keys.rs): - modifier() and modifiers(): Shift, Option (Alt), Command (Meta) and Control from an event's flags; no other key reads them; - to_virtual(): a valid entry's code, else INVALID_KEY, also for a key outside the table; - from_virtual(): the first valid entry with the code, else none; - is_mouse(), mouse_bit(), mouse_pressed() and in_range(): MOUSE1 to MOUSE29, bit key - OBS_KEY_MOUSE1 of the pressed button mask, and 0 .. OBS_KEY_LAST_VALUE. It keeps no state, takes no lock, allocates nothing and denies clippy's arithmetic, indexing, as-conversion, unwrap, expect, panic and unreachable lints. Two intentional differences, both bugs in the C on macOS: - obs_key_to_virtual_key() read virtual_keys[key] for any key, past the table for one outside it; it now returns INVALID_KEY; - a mouse-button hotkey shifted an int by key - 1, the key's enum value less one (432 to 460), past the int's width: undefined, and clang at -O2 compiles the branch to return an unset register, so the hotkey's state was arbitrary. It now tests bit key - OBS_KEY_MOUSE1 of [NSEvent pressedMouseButtons], MOUSE1 being the left button (bit 0). Shim (src/ffi/cocoa_keys.rs): the six functions with the C signatures. The table crosses as OBS_KEY_LAST_VALUE entries of KeyCode, the repr(C) obs_key_code_t; a null table reads as empty, where C would dereference it. C side: the file leaves libobs.macos-cocoa-keys for its own ported group, libobs.ported-cocoa-keys, registered in libobs/build/ported-source-sets/cocoa-keys.json for macOS. The functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. ci/test_ported_sources.py checks the group is ported on macOS only. test_cocoa_keys_steps still compiles the C file into itself, unchanged. There is no _rust variant: as for the other obs-hotkey shims, the archive member does not link on its own, so the Tier 3 tests compare the shims with the C. Tier 1 hotkey::cocoa_keys passes 26/26. test_cocoa_keys_steps passes on the C build and on the hybrid build tree. The exported symbols are identical on the Linux C, hybrid and C reference builds (1766 each), the six shims local (t). The mac-arm64 cross-compile check builds the ported group with no warning. Refs #2914. Co-authored-by: PatTheMav <[email protected]> Co-authored-by: Sebastian Beckmann <[email protected]> Co-authored-by: Palana <[email protected]> Co-authored-by: Theodore Dubois <[email protected]>
Oracle (oracle/cocoa_keys.c): the original obs-cocoa-keys.c.h, unmodified, with its six functions renamed to oracle_*, and getters for the key, flag and INVALID_KEY values and the obs_key_code_t layout the C sees. Tier 3 (tests/hotkey/cocoa_keys_parity.rs, 7 tests): - the values and the obs_key_code_t layout against the C; - the modifier map of every key with each of the 16 combinations of the four flag bits, the other bits clear and set; - the virtual key of every key and the key of every 7-bit code, and of wide and negative codes, on four tables (zeroed, every key distinct, macOS-like with repeated codes, every other entry invalid); - the mouse range and the key range gate of every key up to twice OBS_KEY_LAST_VALUE and of the largest ones; - proptests over random flag words, tables and keys. Keys go in from 0 up only: obs_key_t is unsigned to gcc and clang, but MSVC, which also builds the oracle, makes it signed. Intentional differences, excluded and checked on the Rust side only: the virtual key of a key outside the table (C reads past it; Rust gives INVALID_KEY), and the mouse button bit (C's shift is undefined for every mouse key, so the oracle's mouse_pressed is never called; Rust tests bit key - OBS_KEY_MOUSE1). port_comparison gets benches/port_comparison/cocoa_keys.rs: 64 event tap callbacks (the four modifier reads and the virtual key search) on a macOS-like table, folded into a checksum both sides must match. Linux x86_64: Rust 2.53 us, C 2.70 us. It allocates nothing with bmalloc; obs-c-oracle gets obs-hotkey as a dev-dependency for it. Mutation check (done once, not committed); failing tests per mutant: | Mutant | Tier 1 | Tier 3 | |---|---|---| | Shift reads Control | 3 | 1 | | Alt and Meta swapped | 2 | 1 | | other keys read all flags | 3 | 2 | | modifiers() out of order | 2 | 0 | | to_virtual ignores validity | 2 | 2 | | to_virtual invalid is 0 | 3 | 2 | | from_virtual ignores validity | 4 | 2 | | from_virtual last match wins | 3 | 2 | | mouse range excludes MOUSE29 | 4 | 1 | | mouse bit off by one | 4 | 0 | | in_range includes LAST | 1 | 1 | | shim: modifier key ignored | 0 | 2 | | shim: table one entry short | 0 | 2 | | shim: is_mouse is in_range | 0 | 1 | | shim: from_virtual drops the code's sign | 0 | 2 | All 15 are killed. modifiers() has no C counterpart and the mouse bit is an intentional difference, so those two are Tier 1's alone; the shim mutants are killed by Tier 3, the tier that reaches the shims. The proptest regression files keep the seeds the mutants failed on. Tier 1 and Tier 3 pass (26/26 and 7/7). Refs #2914.
The rust/README.md crate table now lists libobs/obs-cocoa-keys.c.h among the rust/obs-hotkey units, and its port table has a row for it, before the hotkey binding codec: the six functions, what stays C in obs-cocoa.m (the tables, the key names, the event tap, every CGEvent, TIS, CF and NSEvent call, the key state, the NULL platform check), the static asserts against the macOS headers, how the table crosses, the intentional differences, what Tier 3 compares and that only Tier 3 sees the swap. naming.md's rust/obs-hotkey row, the crate's description and its lib.rs module docs add the file and the module. docs/features.md gets the two changes a user or a plugin can see: on macOS a mouse-button hotkey follows its button (a new line under the fixed upstream bugs for scenes and UI, next to the other hotkey fixes), and obs_key_to_virtual_key gives 0xFF for a key OBS doesn't have (the X11 port's scripting line now covers macOS too). Fixes #2914.
e8d0f71 to
740ec35
Compare
zackees
left a comment
There was a problem hiding this comment.
Review: the move, the port and the evidence all hold up
Approving. I read the move (1ea070bd7), the characterization (ff42a52f5), the RED and GREEN commits, the oracle/parity test and the ledger, and re-ran the cheap guards on the head (740ec3519).
Verified locally
ci/test_ported_sources.py(3),build_groups(6),hidden_symbol_scope(7),sorted_shared_lists(5),test_graph_sorted(2),naming(2),oracle_standins(2),oracle_msvc(1),cmocka_compat(2),cmocka_includes(2) — all OK. The new group is registered inported-source-sets/cocoa-keys.json, the union file andsource-sets.json, the macOS header list, both hidden/unexport lists, andtest-graph.jsonwithobs-cocoa-keys.has a watch input.- The move is behaviour-neutral where it claims to be:
obs-cocoa.m.hkeeps the tables, every CGEvent/TIS/CF/NSEvent call, the key state and its writes, the NULL-platform check and the!platformordering; the_Static_asserts pinCGEventFlags/NSUIntegerwidths and the four flag bits against CoreGraphics, which is the right way to guard a header that deliberately includes no macOS header. - CI on the head is the full set, not a subset: Fast Gate, Rust Tests (macos-26), Rust Tier 2 Linux tests ON and OFF, Rust Tier 2 (macos-26), Cross Compile (mac-arm64), Rust Cross Lint (Windows, macOS), Format,
CI Gate ✅— every one green.
The two intentional differences are the right call. C's mouse bit (1 << (key - 1), shift counts 432–460 into an int) is undefined and clang at -O2 returns an unset register, so there is nothing to preserve; testing bit key - OBS_KEY_MOUSE1 is the behaviour Apple documents for pressedMouseButtons, and the ledger line says so plainly. to_virtual returning INVALID_KEY instead of reading past the table closes the same class of bug in the public obs_key_to_virtual_key. Both are excluded from the parity test explicitly, with the reason written in the test header rather than left implicit.
Notes, not blockers
- The moved C keeps the undefined shift, and the port only replaces it in the hybrid libobs. A pure-C macOS build still has the UB. That is defensible for a behaviour-neutral move (changing it here would change what the characterization test pins), but it is worth a follow-up so the C reference build stops carrying it once this group's port is settled.
obs_cocoa_keys_to_virtual_key's declared contract inobs-cocoa-keys.hsays "table[key]is read for any key" — accurate, but it is the one function whose C contract is a precondition callers must meet; the comment could say which caller guarantees it (virtual_keys,OBS_KEY_LAST_VALUEentries) the way the shim's# Safetysection does.modifiers()in the Rust core has no C counterpart and is unused by the shims; it is fine as the event-tap convenience, but nothing in the port exercises it except Tier 1.
Nothing here needs a change before merge. Queueing now.
|
Thanks for the review. On the three notes, with no change to this PR so it keeps its place in the queue:
|
Fixes #2914
What and why
Port of the macOS key map and pressed check of
libobs/obs-cocoa.m.htorust/obs-hotkey, as a new modulecocoa_keys, the macOS counterpart of the X11 key map port (#2901). On macOS, libobs uses them in the event tap callback (which modifier keys are down, which key a key code is), in the hotkey thread's "is this key down" poll, and in the publicobs_key_from_virtual_key/obs_key_to_virtual_key.Commits:
libobs: Move the macOS key map and pressed check(behaviour-neutral): six hidden functions on plain values inlibobs/obs-cocoa-keys.c.h, declared without EXPORT inlibobs/obs-cocoa-keys.h, in their own unity group with no macOS header.obs_key_to_virtual_keyandobs_key_from_virtual_keystay inobs-cocoa.mas one-line calls with thevirtual_keystable, so the exported symbols do not change;[NSEvent pressedMouseButtons]is still asked only for a mouse key, and the NULL platform check and the key state reads and writes stay where they were.obs-cocoa.m_Static_asserts the four flag bits against CoreGraphics and the sizes ofCGEventFlagsandNSUInteger.test: Characterize the macOS key map(test_cocoa_keys_steps, 8 cases, every platform).obs-hotkey: Add Tier 1 tests for macOS keys(RED).obs-hotkey: Port the macOS key map(GREEN: core, shims, grouplibobs.ported-cocoa-keysregistered inlibobs/build/ported-source-sets/cocoa-keys.json, the three hidden-symbol lists,ci/test_ported_sources.py).obs-hotkey: Check the macOS key port against the C(oracle, Tier 3,port_comparison, mutation check).docs: Record the macOS key map port.What the characterization leaves to the move harness: the pressed check's order (a mouse key asks for the buttons before the NULL platform check; a keyboard key with a NULL platform is false) stays in
obs-cocoa.m, which only a Mac builds and whoseobs_hotkeys_platform_is_pressedis hidden, so no C test off macOS can call it. Today's button bit is not pinned either: the shift is undefined, clang at -O2 returns an unset register, and a test of it would pass or fail by compiler and optimization level.The six functions: the modifier flag map (Shift, Option to Alt, Command to Meta, Control); the virtual key of a key; the key of a virtual code (the first valid entry wins); the mouse key range (MOUSE1 to MOUSE29); the button bit; the key range gate.
obs-cocoa.mkeeps the tables (virtual_keys,key_descriptions,key_glyphs), the key names,InputMethodChangedProc, the event tap and its callback, every CGEvent, TIS, CF and NSEvent call, the key state, the NULL platform check and every log.Intentional differences (in the parity test header,
rust/README.mdanddocs/features.md), both bugs in the C on macOS:key - OBS_KEY_MOUSE1of[NSEvent pressedMouseButtons](MOUSE1 the left button, bit 0). C shifts anintbykey - 1, the key's enum value less one (432 to 460), which is undefined; clang at -O2 compiles that branch to return an unset register, so a mouse-button hotkey's state was arbitrary.obs_key_to_virtual_keyreturnsINVALID_KEY(0xFF) for a key outside0 .. OBS_KEY_LAST_VALUE - 1; C reads past the table.virtual_keys).No
_rustvariant of the steps test: as fortest_portal_shortcut_sets, an obs-hotkey shim's archive member does not link on its own againstlibobs_rust-static, so Tier 3 compares the shims with the C.The moved code is upstream's (PatTheMav, Sebastian Beckmann, Palana, Theodore Dubois), credited in the move and port commits. No upstream issue.
RED → GREEN
soldr cargo test -p obs-data --test obs-tests-core hotkey::cocoa_keysfails 24 of 26 with "not yet implemented" (the flag values and theKeyCodelayout pass). On the RED commit,local_gate.py --lane lintpassed and--test-diff origin/masterfailed only those 24 (5016 of 5040 passed, 6 skipped).hotkey::cocoa_keys26/26; Tier 3hotkey::cocoa_keys_parity7/7. Linux x86_64 (GCC, C build and hybrid + C reference build):test_cocoa_keys_stepspasses on both build trees. Exported symbols identical on the C, hybrid and C reference builds (1766 each), the six shims local (t); the group is macOS-only, so the Linux C reference has none. The mac-arm64 cross-compile check (cmake/cross) builds the move's group,obs-cocoa.mwith its static asserts, and the ported group with no new warning. Runs were on e8d0f71 and, after the rebase onto current master, again on 740ec35 (quick checks,./lint, allhotkey::tests 294/294, the bench test,test_cocoa_keys_stepson both trees, the export comparison: 1766 each, the six shims local).Local gates (runs were on e8d0f71):
./lintok;./unittestRust tiers 5113 + 845 passed, 6 skipped;local_gate.py --lane lintand--lane crossok;soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warningsclean; theport_comparisonbench test passes. The CI guard tests (build groups, ported sources, oracle stand-ins and MSVC, sorted shared lists, test graph, cmocka guards, naming) pass; the exported symbols are still identical on the C, hybrid and C reference builds (1766, the six shims local). Not run locally: Tier 2 in Docker (./unittest's tier2-linux lane) and the local-gate stamp, which need Docker access this host's build account doesn't have; the head is unstamped, so PR CI runs the full suite.Move check (runs were on 39aea4e): master's functions and the moved ones with their new callers, extracted over logging stand-ins for the CGEvent calls,
pressedMouseButtonsand the platform state, built with gcc and clang at -O0, -O2 and under ASan and UBSan, on 300 random tables with 200 random events each and the pressed check of every key: identical logs (1,010,645 lines each; the mouse keys' undefined shift compared at -O0 only, 1,048,162 lines). 13 of 14 planted mutants change the log; the survivor is the modifier map returning true for a key no caller passes, whichtest_cocoa_keys_stepspins.Mutation check of the port: 15 mutants (11 in the core, 4 in the shims), all killed; table in commit 5 (69eb969).
modifiers()has no C counterpart and the button bit is an intentional difference, so those are Tier 1's alone; the shim mutants are killed by Tier 3.port_comparison(cocoa_keys/key_events/64, Linux x86_64): Rust 2.53 µs, C 2.70 µs for 64 event tap callbacks (four modifier reads and a virtual key search each).Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)