Skip to content

rust: Port the macOS key map and pressed check #2914

Closed

Description

@rdoupe

Unit

Port the macOS key map and pressed check of libobs/obs-cocoa.m.h to rust/obs-hotkey, as a new module cocoa_keys, next to 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 public obs_key_from_virtual_key / obs_key_to_virtual_key. These decisions move:

  • KeyboardEventProc's modifier map: the Shift, Option, Command and Control flag bits to OBS_KEY_SHIFT, OBS_KEY_ALT, OBS_KEY_META and OBS_KEY_CONTROL.
  • obs_key_to_virtual_key(): the table code when the entry is valid, else INVALID_KEY (0xFF).
  • obs_key_from_virtual_key(): the first valid table entry with the code, else OBS_KEY_NONE.
  • obs_hotkeys_platform_is_pressed()'s decisions: the mouse key range and its button bit, then the key range gate.

These stay in C: the tables (virtual_keys, key_descriptions, key_glyphs), the key-name functions (obs_key_to_localized_string, key_code_to_string, obs_key_to_str), InputMethodChangedProc, obs_hotkeys_platform_init/_free, every CGEvent*, TIS and CF call, [NSEvent pressedMouseButtons], the writes to the key state, the NULL platform check and every log.

Phase and position

Phase 6, macOS host code; the macOS counterpart of the X11 key map port (#2901). Tracking: #4.

Dependencies and existing tests

  • No dependency to land first.
  • Real libobs: no test covers these on macOS.
  • No characterization: test_cocoa_keys_steps lands first in the same PR, builds the moved C file on its own (plain C, no Carbon or CoreGraphics header) and runs on every OS.

Notes

One PR, separate commits: the behaviour-neutral move into libobs/obs-cocoa-keys.c.h (hidden functions on plain values; the flag bits, the mouse key range and INVALID_KEY are _Static_asserted against the macOS headers in obs-cocoa.m.h), characterization, RED, GREEN (group libobs.ported-cocoa-keys in the macOS ported sources), Tier 3 (every key value, every 7-bit virtual code, proptests over flag words and button masks), docs.

Intentional differences, both fixed in the port:

  • obs_key_to_virtual_key on macOS returns INVALID_KEY for a key outside 0 .. OBS_KEY_LAST_VALUE - 1; C indexes the table with it.
  • A mouse-button hotkey on macOS tests the wrong bit of [NSEvent pressedMouseButtons]: C takes key - 1 (the key's enum value, hundreds) as the bit and shifts an int by it, which is undefined. The port tests bit key - OBS_KEY_MOUSE1 (MOUSE1 is the left button, bit 0).

Activity

  1. added
    enhancement
    port
    on Oct 29, 2026
  2. rdoupe commented on Oct 29, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #2914

    Plan, one PR with separate commits:

    1. libobs: Move the modifier map, the two virtual key lookups and the pressed check's decisions into libobs/obs-cocoa-keys.c.h: hidden functions on plain values, no Carbon or CoreGraphics header, behaviour-neutral (checked by building master's and the moved functions side by side on the same inputs).
    2. Characterization: test_cocoa_keys_steps, building the moved file on its own, on every OS.
    3. RED: Tier 1 obs_hotkey::cocoa_keys against todo!() stubs.
    4. GREEN: the safe core and src/ffi/cocoa_keys.rs, group libobs.ported-cocoa-keys in the macOS ported sources, with the key range check and the mouse button bit fix.
    5. Tier 3 parity against the C with a mutation check.
    6. Docs.

    I expect the draft PR today, within a few hours.

  3. added
    ready
    on Oct 29, 2026
  4. rdoupe commented on Oct 29, 2026

    @rdoupe
    CollaboratorAuthor

    Fixed by #2917 (merged through the merge queue as 9055956). The macOS key map and pressed check now live in rust/obs-hotkey as cocoa_keys; libobs calls them through six hidden functions in libobs/obs-cocoa-keys.c.h. Tier 1 (26 cases), the unchanged C tests, and Tier 3 parity against the C (7 cases, 15 mutants all killed) pass. Two intentional differences are listed in docs/features.md: a mouse-button hotkey now tests bit key - OBS_KEY_MOUSE1 of [NSEvent pressedMouseButtons], and obs_key_to_virtual_key returns INVALID_KEY for a key OBS does not have.

    Left, by design: the tables (virtual_keys, key_descriptions, key_glyphs), the key names, InputMethodChangedProc, the event tap, every CGEvent, TIS, CF and NSEvent call, the key state, the NULL platform check and every log stay in C. There is no follow-up issue for them. Everything ran on Linux plus a mac-arm64 cross-compile; nothing was run on a Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementportready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions