Skip to content

Release Mac input when the game window loses focus - #128

Closed
nocmt wants to merge 1 commit into
seedds:mainfrom
nocmt:fix/macos-focus-input-release
Closed

nocmt wants to merge 1 commit into
seedds:mainfrom
nocmt:fix/macos-focus-input-release

Conversation

@nocmt

@nocmt nocmt commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Release Mac input when the game window loses focus

Draft: actual desktop focus transitions and physical-controller verification are still pending. Based on current main at 7a029f66d92c951ae6a86e38bb34eeb0cb7527e3; the targeted tests and Objective-C syntax check were rerun after updating to this baseline.

Problem

When the Mac game window resigns key status or is minimized while keyboard, touch, or a physical controller input is held, the corresponding input can remain active in the guest. GameController callbacks may also arrive after the window has gone inactive and restore stale physical pad state.

Minimal change

On window focus loss and minimization, clear the Mac keyboard state and publish neutral states for the existing keyboard/touch and per-player controller input paths. Ignore keyboard/controller updates while inactive. When focus returns, resample each connected physical controller so its current state is reflected instead of relying on a callback that may not arrive.

This keeps the existing multi-player slot assignments and input API semantics. The normal input release path is retained, including its existing press-latch behavior. This change does not cover rounded windows, mouse-look, FPS display, language selection, or other local launcher features.

Tests

  • make -C dm/host/tests GAME=010015100B514000 build/t_hid build/t_touch
  • dm/host/tests/build/t_hid — 64 checks passed.
  • dm/host/tests/build/t_touch — 13 checks passed.
  • macOS Objective-C syntax check: clang -fobjc-arc -fsyntax-only -Wall -Wextra -Wno-deprecated-declarations -Wno-unused-but-set-variable -Idm/host -Idm/host/gen/010015100B514000 dm/host/front_mac.m

The automated checks cover neutral state publication through the existing HID release APIs and touch release/latching behavior. They do not exercise the Mac front end's focus gate, controller resampling on focus return, AppKit notifications, or actual desktop/physical-controller behavior.

Manual verification still needed

On macOS, hold keyboard keys, touch/mouse input, and buttons/sticks on multiple connected controllers; then switch away from the game window and minimize it. Confirm guest input stops immediately, background controller activity cannot reinstate input, and returning focus samples the controllers' current state. Repeat with supported one-player and multi-player games, including controller disconnect/reconnect.

Related PRs

seedds added a commit that referenced this pull request Oct 25, 2026
…d (general, from PR #128). A window that is no longer key sees no key-up, mouse-up or controller release, so a key, a click or a controller button held when the window lost focus (another app, the software keyboard's text box, minimizing) stayed pressed in the game. Now front_mac.m releases what the front end itself pressed when the window resigns key or is minimized (input_release): the keyboard buttons and the WASD stick, the touch only while the mouse holds it (g_mouse_down), and the pad of each player a controller holds (in DM_PAD_MERGE mode player 1's, when a controller is connected); while out of focus the keyboard, the mouse and the controllers' value changes are ignored (g_input_on), and when the window is key again each held controller is read as it is now (input_resume), so a button still held or a stick still deflected counts again. From PR #128 (nocmt), with two changes: the PR released player 1's pad and the touch in every case, which are also DM_KEYS' and DM_CTL's paths (hle_hid.c), so a scripted hold or drag ended early at a focus change during a Mac run; and it took input up again only if NSApp.isActive held when the window became key, so if AppKit makes the window key before the app counts as active, the keyboard, the mouse and the controllers stayed off until the next focus change. Also left out: the handler queue set to the main queue (already GameController's default), the focus state 3 on resigning key while minimized (DidMiniaturize sets it), the release on deminiaturizing (nothing is held then), and the t_hid test, which checked only that dm_input_set / dm_pad_set_player with zeros clear the state, not the focus handling. Checked: the host unit tests pass (GAME=010015100B514000), the Mac host builds for Mario vs. Donkey Kong, and a DM_WINDOW run boots it with DM_CTL presses reaching the game. Not checked: an actual focus change (in that run the window never became key or resigned it: the shell's activation requests and Finder's activation left no focus line), a physical controller, the software keyboard's text box by hand

Co-authored-by: nocmt <[email protected]>
@seedds

seedds commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Thank you for this, and for the clear write-up of what was and wasn't tested. It's in main as 787523f, credited to this PR (Co-authored-by), with two changes:

  • Only what the front end pressed itself is released on focus loss: the keyboard and WASD, the touch only while the mouse holds it, and the pad of each player a controller holds. Player 1's pad and the touch are also the paths of the scripted input (DM_KEYS / DM_CTL in hle_hid.c), so releasing them in every case ended a scripted hold or drag early when the window lost focus during a Mac run.
  • Input is taken up again whenever the window becomes key, without the NSApp.isActive check: if AppKit makes the window key before the app counts as active, the keyboard, the mouse and the controllers would have stayed off until the next focus change. Each held controller is still read as it is now on the way back in.

Also left out: setting handlerQueue to the main queue (already GameController's default), focus state 3 on resigning key while minimized (DidMiniaturize sets it), the reset on deminiaturizing, and the t_hid test, which checked the existing release API rather than the focus handling.

Checked: the host unit tests, the Mac host build, and a windowed boot with scripted presses. Still not checked, as in your list: a real focus change by hand, physical controllers, and the software keyboard's text box.

@seedds seedds closed this Oct 25, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…d (general, from PR seedds#128). A window that is no longer key sees no key-up, mouse-up or controller release, so a key, a click or a controller button held when the window lost focus (another app, the software keyboard's text box, minimizing) stayed pressed in the game. Now front_mac.m releases what the front end itself pressed when the window resigns key or is minimized (input_release): the keyboard buttons and the WASD stick, the touch only while the mouse holds it (g_mouse_down), and the pad of each player a controller holds (in DM_PAD_MERGE mode player 1's, when a controller is connected); while out of focus the keyboard, the mouse and the controllers' value changes are ignored (g_input_on), and when the window is key again each held controller is read as it is now (input_resume), so a button still held or a stick still deflected counts again. From PR seedds#128 (nocmt), with two changes: the PR released player 1's pad and the touch in every case, which are also DM_KEYS' and DM_CTL's paths (hle_hid.c), so a scripted hold or drag ended early at a focus change during a Mac run; and it took input up again only if NSApp.isActive held when the window became key, so if AppKit makes the window key before the app counts as active, the keyboard, the mouse and the controllers stayed off until the next focus change. Also left out: the handler queue set to the main queue (already GameController's default), the focus state 3 on resigning key while minimized (DidMiniaturize sets it), the release on deminiaturizing (nothing is held then), and the t_hid test, which checked only that dm_input_set / dm_pad_set_player with zeros clear the state, not the focus handling. Checked: the host unit tests pass (GAME=010015100B514000), the Mac host builds for Mario vs. Donkey Kong, and a DM_WINDOW run boots it with DM_CTL presses reaching the game. Not checked: an actual focus change (in that run the window never became key or resigned it: the shell's activation requests and Finder's activation left no focus line), a physical controller, the software keyboard's text box by hand

Co-authored-by: nocmt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants