Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Console mode: the pointer stays hidden while a game starts (#380)

Haseenesssbab1465b631b
diff --git a/frontend/Sources/NX2Apple/ConsoleCoordinator.swift b/frontend/Sources/NX2Apple/ConsoleCoordinator.swift
index 34ad4d82..fd73fad8 100644
--- a/frontend/Sources/NX2Apple/ConsoleCoordinator.swift
+++ b/frontend/Sources/NX2Apple/ConsoleCoordinator.swift
@@ -83,7 +83,9 @@ import AppKit
func toggle() { active ? exit() : enter() }
/// Takes the window out of full screen and then calls `launch`, which starts the game. A window that is not full screen calls it at once.
+ /// A hidden pointer stays hidden until the game has quit, as the smaller window no longer covers it (ConsolePointer.hold).
func leaveFullScreenForGame(then launch: @escaping () -> Void) {
+ pointer.hold(true)
guard active, let w = window(), w.styleMask.contains(.fullScreen) else { launch(); return }
leftForGame = true
setFullScreen(w, false) { launch() }
@@ -92,6 +94,7 @@ import AppKit
/// The game has quit or did not start: the window goes back to full screen if it left for the game and the console is still showing.
/// A launcher that is not the active app waits until it is, so it does not pull the player into a Space from behind another app.
func resumeFullScreenAfterGame() {
+ pointer.hold(false)
guard leftForGame else { return }
leftForGame = false
guard active else { return }
diff --git a/frontend/Sources/NX2Apple/ConsolePointer.swift b/frontend/Sources/NX2Apple/ConsolePointer.swift
index 713451de..9e104a15 100644
--- a/frontend/Sources/NX2Apple/ConsolePointer.swift
+++ b/frontend/Sources/NX2Apple/ConsolePointer.swift
@@ -7,6 +7,9 @@ struct PointerIdle: Equatable {
enum Step { case none, hide, show }
/// The timer looks every `ConsolePointer.pollSeconds`, so the pointer is gone 1.25 to 1.5 seconds after the last movement.
var idleSeconds: TimeInterval = 1.25
+ /// A game is starting from the console: the window leaves full screen and the game takes the front, so a hidden pointer stays
+ /// hidden although it is no longer over the window. Only movement, a click or a scroll shows it then (issue #380).
+ var held = false
private(set) var hidden = false
private var lastLocation: CGPoint?
private var lastActivity: TimeInterval = 0
@@ -15,7 +18,7 @@ struct PointerIdle: Equatable {
mutating func tick(now: TimeInterval, location: CGPoint, buttonDown: Bool, over: Bool) -> Step {
if location != lastLocation || buttonDown { lastActivity = now }
lastLocation = location
- if hidden { return lastActivity == now || !over ? show() : .none }
+ if hidden { return lastActivity == now || (!over && !held) ? show() : .none }
if over, now - lastActivity >= idleSeconds { hidden = true; return .hide }
return .none
}
@@ -27,7 +30,7 @@ struct PointerIdle: Equatable {
}
/// Console mode ended: the pointer is shown if it was hidden.
- mutating func reset() -> Step { hidden ? show() : .none }
+ mutating func reset() -> Step { held = false; return hidden ? show() : .none }
private mutating func show() -> Step { hidden = false; return .show }
}
@@ -52,6 +55,9 @@ struct PointerIdle: Equatable {
/// The pointer is hidden right now.
var isHidden: Bool { idle.hidden }
+ /// Keeps a hidden pointer hidden while a game starts from the console (PointerIdle.held).
+ func hold(_ on: Bool) { idle.held = on }
+
func start() {
guard timer == nil else { return }
timer = Timer.scheduledTimer(withTimeInterval: Self.pollSeconds, repeats: true) { [weak self] _ in
diff --git a/frontend/Sources/NX2Apple/LiveConsoleDriver.swift b/frontend/Sources/NX2Apple/LiveConsoleDriver.swift
index d3b2cca8..e3bd22bd 100644
--- a/frontend/Sources/NX2Apple/LiveConsoleDriver.swift
+++ b/frontend/Sources/NX2Apple/LiveConsoleDriver.swift
@@ -256,9 +256,21 @@ struct ConsolePadSnapshot: Equatable {
}
}
+ /// The app to hand the front to instead of letting LaunchServices activate the game: an app LaunchServices activates shows the pointer for up to
+ /// 150 ms whatever it hid before, one that takes a yielded activation itself keeps it hidden (issue #380). Only with the pointer hidden
+ /// (DM_CURSOR_HIDDEN), as the game hides it before it activates itself then.
+ static func yieldTarget(environment: [String: String], appPath: String) -> String? {
+ guard environment["DM_CURSOR_HIDDEN"] == "1" else { return nil }
+ return Bundle(url: URL(fileURLWithPath: appPath))?.bundleIdentifier
+ }
+
private static func openApplication(_ game: LibraryGame, _ environment: [String: String], _ done: @escaping (Int32?, Error?) -> Void) {
let configuration = NSWorkspace.OpenConfiguration()
if !environment.isEmpty { configuration.environment = environment }
+ if #available(macOS 14, *), let target = yieldTarget(environment: environment, appPath: game.appPath) {
+ configuration.activates = false
+ NSApp.yieldActivation(toApplicationWithBundleIdentifier: target)
+ }
NSWorkspace.shared.openApplication(at: URL(fileURLWithPath: game.appPath), configuration: configuration) { app, error in
done(app?.processIdentifier, error)
}
diff --git a/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift b/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift
index 42c2dd14..22888170 100644
--- a/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift
+++ b/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift
@@ -100,6 +100,14 @@ import SwiftUI
XCTAssertNil(launches.last?.environment["DM_CURSOR_HIDDEN"], "a visible pointer stays the game's default")
}
+ func testAGameStartedWithThePointerHiddenGetsTheFrontYielded() throws {
+ let app = try makeApp().path
+ XCTAssertEqual(LiveConsoleDriver.yieldTarget(environment: ["DM_CURSOR_HIDDEN": "1"], appPath: app), bundleID,
+ "LaunchServices activating the game shows the pointer for a moment")
+ XCTAssertNil(LiveConsoleDriver.yieldTarget(environment: [:], appPath: app), "with the pointer visible LaunchServices activates the game as before")
+ XCTAssertNil(LiveConsoleDriver.yieldTarget(environment: ["DM_CURSOR_HIDDEN": "1"], appPath: temporary.appendingPathComponent("Missing.app").path))
+ }
+
func testConsoleLaunchesFullscreenOverTheSavedDisplayChoice() throws {
var launches: [Launch] = []
let (driver, library, _) = try makeDriver { launches.append($0) }
diff --git a/frontend/Tests/NX2AppleTests/ConsolePointerTests.swift b/frontend/Tests/NX2AppleTests/ConsolePointerTests.swift
index ba4b78c8..16de303a 100644
--- a/frontend/Tests/NX2AppleTests/ConsolePointerTests.swift
+++ b/frontend/Tests/NX2AppleTests/ConsolePointerTests.swift
@@ -52,6 +52,20 @@ final class ConsolePointerTests: IsolatedDefaultsTestCase {
XCTAssertEqual(idle.tick(now: 12, location: here, buttonDown: false, over: false), .show, "the launcher lost the pointer or the focus")
}
+ func testAHeldPointerStaysHiddenOutsideTheWindowUntilItMoves() {
+ var idle = PointerIdle()
+ _ = idle.tick(now: 0, location: here, buttonDown: false, over: true)
+ XCTAssertEqual(idle.tick(now: 1.5, location: here, buttonDown: false, over: true), .hide)
+ idle.held = true
+ XCTAssertEqual(idle.tick(now: 2, location: here, buttonDown: false, over: false), .none, "the window left full screen for a game")
+ XCTAssertEqual(idle.tick(now: 2.25, location: CGPoint(x: 11, y: 10), buttonDown: false, over: false), .show, "the player moved it")
+ idle.held = true
+ _ = idle.tick(now: 3, location: CGPoint(x: 11, y: 10), buttonDown: false, over: true)
+ _ = idle.tick(now: 4.5, location: CGPoint(x: 11, y: 10), buttonDown: false, over: true)
+ XCTAssertEqual(idle.reset(), .show)
+ XCTAssertFalse(idle.held, "the console ended: the hold goes with it")
+ }
+
func testEndingTheConsoleShowsAHiddenPointerOnce() {
var idle = PointerIdle()
XCTAssertEqual(idle.reset(), .none)
diff --git a/patches/0073-front-mac-hide-the-pointer-before-the-game-takes.patch b/patches/0073-front-mac-hide-the-pointer-before-the-game-takes.patch
new file mode 100644
index 00000000..433461f8
--- /dev/null
+++ b/patches/0073-front-mac-hide-the-pointer-before-the-game-takes.patch
@@ -0,0 +1,65 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Haseenesss <[email protected]>
+Date: Thu, 5 Nov 2026 18:22:47 +0900
+Subject: [PATCH] front-mac: hide the pointer before the game takes the front,
+ and activate from the run loop (DM_CURSOR_HIDDEN)
+
+A game started from the launcher's console with DM_CURSOR_HIDDEN=1 still showed the pointer for 35-220 ms. The host hid it only after creating the window and requesting fullscreen, while the app was already active. It now hides it right after NSApplication is created, and activates itself once the run loop runs, as an app that takes the front while its main thread is still busy can show the pointer for a frame. Measured with Balatro from a stand-in for the console: before, 5 of 5 starts showed it for 35-220 ms, and 3 of 3 for a frame with the front yielded to the game; after, with the launcher yielding the front rather than LaunchServices activating the game, none of 10.
+
+Co-Authored-By: Claude Opus 5.5 <[email protected]>
+
+Upstream-Status: pending
+Reason: A game started from the console with the pointer hidden still showed it for a moment when it took the front
+Issue: LNPANDY/NX2Apple#380
+---
+ dm/host/front_mac.m | 20 +++++++++++++-------
+ 1 file changed, 13 insertions(+), 7 deletions(-)
+
+diff --git a/dm/host/front_mac.m b/dm/host/front_mac.m
+index 892df56..6f6491e 100644
+--- a/dm/host/front_mac.m
++++ b/dm/host/front_mac.m
+@@ -1322,6 +1322,12 @@ void front_run(void (*guest)(void)) {
+ if (!getenv("DM_WINDOW")) { guest(); return; }
+ @autoreleasepool {
+ [NSApplication sharedApplication];
++ // DM_CURSOR_HIDDEN=1 (see the pointer below) hides it here, before the app takes the front: a hide after the window left the pointer up until it ran. The
++ // launcher's console also yields the front to the game rather than let LaunchServices activate it when it registers, which shows the pointer whatever the app
++ // hid (issue #380)
++ const char *ch = getenv("DM_CURSOR_HIDE"), *hs = getenv("DM_CURSOR_HIDDEN"); double idle = ch && *ch ? atof(ch) : 3.0;
++ int start_hidden = idle > 0 && hs && !strcmp(hs, "1");
++ if (start_hidden) [NSCursor hide];
+ [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
+ NSRect r = NSMakeRect(0, 0, 1280, 720);
+ NSWindow *win = [[NSWindow alloc] initWithContentRect:r
+@@ -1356,7 +1362,9 @@ void front_run(void (*guest)(void)) {
+ [win makeKeyAndOrderFront:nil];
+ screen_refresh(win.screen ?: NSScreen.mainScreen);
+ g_win = win;
+- [NSApp activateIgnoringOtherApps:YES];
++ // Activated once the run loop below runs, not here: an app that takes the front while its main thread is still busy with the setup below can show
++ // the pointer for a frame although it hid it (DM_CURSOR_HIDDEN, issue #380)
++ dispatch_async(dispatch_get_main_queue(), ^{ [NSApp activateIgnoringOtherApps:YES]; });
+ // A host another process starts (scripts/regress.py) is not activated (its focus state stays 2), so its window opened wherever it landed in the window order, and behind
+ // another app's window macOS fires its display link about once a second: Breath of the Wild stepped up to 3 vsyncs at boot and never came back (20 fps runs).
+ // DM_WINDOW_TOP=1 orders it in front without activation and keeps it above other apps' windows; the log names every change of the window's visibility either way
+@@ -1411,15 +1419,13 @@ void front_run(void (*guest)(void)) {
+ // The pointer hides after DM_CURSOR_HIDE seconds (3 by default, 0 keeps it) without movement over the game, and comes back with the next movement, click or scroll. macOS
+ // hides it only for a game that asks (a game window does not, and a pointer left over a fullscreen game stays on the picture); it is also shown whenever the window is
+ // not key or the pointer is outside the game view, so the title bar and other windows keep theirs
+- const char *ch = getenv("DM_CURSOR_HIDE"); double idle = ch && *ch ? atof(ch) : 3.0;
+ if (idle > 0) {
+ win.acceptsMouseMovedEvents = YES;
+- __block double last = CACurrentMediaTime(); __block int hidden = 0, armed = 0;
++ // DM_CURSOR_HIDDEN=1: the launcher's console had the pointer hidden when it started this game, so it starts hidden too (start_hidden, hidden at the top) and does
++ // not flash up until the first idle poll. The not-over rule below waits (armed) until the game window has been under the pointer once, as a new window is not key
++ // yet when the timer first runs
++ __block double last = CACurrentMediaTime(); __block int hidden = start_hidden, armed = start_hidden;
+ void (^show)(void) = ^{ if (hidden) { hidden = 0; [NSCursor unhide]; } };
+- // DM_CURSOR_HIDDEN=1: the launcher's console had the pointer hidden when it started this game, so it starts hidden here too and does not flash up until the first idle poll.
+- // The not-over rule below waits (armed) until the game window has been under the pointer once, as a new window is not key yet when the timer first runs
+- const char *hs = getenv("DM_CURSOR_HIDDEN");
+- if (hs && !strcmp(hs, "1")) { hidden = 1; armed = 1; [NSCursor hide]; }
+ [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskMouseMoved | NSEventMaskLeftMouseDragged | NSEventMaskRightMouseDragged | NSEventMaskOtherMouseDragged |
+ NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskOtherMouseDown | NSEventMaskScrollWheel handler:^NSEvent *(NSEvent *e) { last = CACurrentMediaTime(); show(); return e; }];
+ [NSTimer scheduledTimerWithTimeInterval:0.25 repeats:YES block:^(NSTimer *t) {
diff --git a/patches/series b/patches/series
index dcf51b4e..892ad2dc 100644
--- a/patches/series
+++ b/patches/series
@@ -40,3 +40,4 @@
0070-ios-touch-layout.patch
0071-ios-shell-restore-native-duo-orientation.patch
0072-nsp-script-the-first-dotnet-with-an-sdk-10-goes.patch
+0073-front-mac-hide-the-pointer-before-the-game-takes.patch