From 68bb8e915ab857639d148b630c106dea49184557 Mon Sep 17 00:00:00 2001 From: alantangok Date: Tue, 17 Nov 2026 17:03:02 +0800 Subject: [PATCH] feat: let a game lock the pointer for mouse-look from launch First-person games on iPad need the pointer locked so the view keeps turning at the screen edges. Automatic lock waits for the game to hide its cursor, and the Relative toggle applies to every game. A game's own MADEIRA_POINTER_LOCK_ON_START=1 now starts that game in relative mouse-look with automatic pointer lock, without a hidden-cursor report. Focus loss and manual unlock still release it. Default off. Signed-off-by: alantangok --- app/Madeira/ConfigCatalog.generated.swift | 1 + app/Madeira/HardwareInput.swift | 15 ++++++++++-- docs/KEYBOARD_MOUSE.md | 6 +++++ tests/host/check-pointer-capture.py | 29 ++++++++++++++++++++++- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/app/Madeira/ConfigCatalog.generated.swift b/app/Madeira/ConfigCatalog.generated.swift index effb09af84..1359dc965e 100644 --- a/app/Madeira/ConfigCatalog.generated.swift +++ b/app/Madeira/ConfigCatalog.generated.swift @@ -690,6 +690,7 @@ extension ConfigCatalog { ConfigOption(key: "env.MADEIRA_POINTER_AUTOLOCK", title: "", kind: .bool, defaultValue: "1", category: "Windows, display & input", note: "MADEIRA_POINTER_AUTOLOCK=0: the pointer locks only on request, not while the program hides its cursor (see `AutoLock`).", choices: [], sources: ["app/Madeira/HardwareInput.swift"]), ConfigOption(key: "env.MADEIRA_POINTER_HIDDEN_ABSOLUTE", title: "", kind: .text, defaultValue: "", category: "Windows, display & input", note: "MADEIRA_POINTER_HIDDEN_ABSOLUTE=1 (a game's own config): the program's cursor follows the iOS pointer's position even while it hides the Windows cursor, and the pointer never locks by itself. For a game that hides the cursor only to draw...", choices: [], sources: ["app/Madeira/HardwareInput.swift"]), ConfigOption(key: "env.MADEIRA_POINTER_LOCK", title: "", kind: .bool, defaultValue: "1", category: "Windows, display & input", note: "MADEIRA_POINTER_LOCK=0: the pointer is never locked and no lock button or chord is offered.", choices: [], sources: ["app/Madeira/HardwareInput.swift"]), + ConfigOption(key: "env.MADEIRA_POINTER_LOCK_ON_START", title: "", kind: .text, defaultValue: "", category: "Windows, display & input", note: "MADEIRA_POINTER_LOCK_ON_START=1 (a game's own config): the game starts in relative mouse-look with automatic pointer lock, as with the Relative toggle, without waiting for a hidden-cursor report. For first-person games whose view must tu...", choices: [], sources: ["app/Madeira/HardwareInput.swift"]), ConfigOption(key: "env.MADEIRA_PROMOTE_KICK", title: "Display link: commit a transaction per tick", kind: .bool, defaultValue: "0", category: "Windows, display & input", note: "ml2021 experiment: 1 adds a one-pixel layer whose opacity alternates on every display-link tick, so each tick commits a CoreAnimation transaction. For a title parked 80 ms a frame in nextDrawable with the 120 Hz link armed: if the waits fall with this on, the compositor needed a transaction, not an intent.", choices: [], sources: ["app/Madeira/FPSOverlay.swift"]), ConfigOption(key: "env.MADEIRA_RESTORE_BORN_MINIMIZED", title: "", kind: .text, defaultValue: "", category: "Windows, display & input", note: "0: while a Dock start's census runs, a window first shown minimized stays minimized", choices: [], sources: ["app/Madeira/Winios/Winios.m"]), ConfigOption(key: "env.MADEIRA_RESTORE_BORN_MINIMIZED_LATE", title: "", kind: .text, defaultValue: "", category: "Windows, display & input", note: "0: a window first shown minimized after a Dock start's starting screen has ended stays minimized", choices: [], sources: ["app/Madeira/Winios/Winios.m"]), diff --git a/app/Madeira/HardwareInput.swift b/app/Madeira/HardwareInput.swift index 4bc102b3ed..cb826f0045 100644 --- a/app/Madeira/HardwareInput.swift +++ b/app/Madeira/HardwareInput.swift @@ -729,6 +729,16 @@ final class HardwareInput: ObservableObject { return String(cString: v) == "1" } + /// MADEIRA_POINTER_LOCK_ON_START=1 (a game's own config): the game starts in + /// relative mouse-look with automatic pointer lock, as with the Relative + /// toggle, without waiting for a hidden-cursor report. For first-person games + /// whose view must turn past the screen edges. Read per use: the app sets a + /// game's env lines for each launch. Default off. + static var lockOnStart: Bool { + guard let v = getenv("MADEIRA_POINTER_LOCK_ON_START") else { return false } + return String(cString: v) == "1" + } + /// The desktop's live size in guest pixels (IOSDisplayShim): the session /// default until a program changes the display mode, which resizes it. private static func desktopSize() -> (w: Int, h: Int) { @@ -1650,7 +1660,8 @@ final class HardwareInput: ObservableObject { private var cursorShownForRoute: Bool { PointerPolicy.cursorShown(desktop: Self.desktopMode, directEnabled: Self.directCursorEnabled, reports: cursorState.reports, shown: cursorState.shown != 0, - routingEnabled: Self.mouseRoutingEnabled, relative: InputSettings.shared.relative, + routingEnabled: Self.mouseRoutingEnabled, + relative: InputSettings.shared.relative || Self.lockOnStart, hiddenAbsolute: Self.hiddenCursorAbsolute) } @@ -1728,7 +1739,7 @@ final class HardwareInput: ObservableObject { let now = CACurrentMediaTime() motionLock.lock(); let lastDelta = lastGCDeltaAt; motionLock.unlock() // Adapted from BCD dd2fe9c (2026-10-27): relative mode need not report a hidden cursor. - let relative = Self.mouseRoutingEnabled && InputSettings.shared.relative + let relative = Self.mouseRoutingEnabled && (InputSettings.shared.relative || Self.lockOnStart) let shown = !relative && cursorState.shown != 0 let hiddenFor = relative ? AutoLock.hiddenDelay : (cursorState.reports == 0 || cursorHiddenSince == 0 ? 0 : now - cursorHiddenSince) diff --git a/docs/KEYBOARD_MOUSE.md b/docs/KEYBOARD_MOUSE.md index 60b1a10c5d..e7692835ba 100644 --- a/docs/KEYBOARD_MOUSE.md +++ b/docs/KEYBOARD_MOUSE.md @@ -210,6 +210,11 @@ that iPadOS granted it. A request alone is not proof of containment; see from the game. A desktop game that hides its cursor also keeps its automatic lock without fresh reports. Focus loss and manual unlock still release it. Hidden desktop mouse-look does not get an extra absolute cursor move. +- **Lock on start:** a game's own `MADEIRA_POINTER_LOCK_ON_START=1` puts that + game in relative pointer mode from launch, so the pointer locks as soon as the + mouse moves over the game view, without waiting for the game to hide its + cursor. For first-person games that turn past the screen edges. The Relative + toggle stays unchanged for other games. - **By hand:** the lock button in the pointer settings or **Ctrl+Alt+P** on the keyboard (P is not sent to the program). Touch keeps working while locked, so the button is always a way out. Releasing an automatic lock by hand keeps it @@ -268,6 +273,7 @@ and restart to enable the fixes again. The same switch is listed in Settings > A | `MADEIRA_POINTER_LOCK` | on | `0`: never lock the pointer; no lock button, Ctrl+Alt+P is sent to the program. | | `MADEIRA_POINTER_AUTOLOCK` | on | `0`: lock only by hand (Ctrl+Alt+P or the lock button). | | `MADEIRA_POINTER_HIDDEN_ABSOLUTE` | off | `1` (a game's own config): the program's cursor follows the pointer's position even while the program hides it, and the pointer never locks by itself. For a game that draws its own cursor at the cursor position, where relative motion lags the pointer and stops short of the screen edges. | +| `MADEIRA_POINTER_LOCK_ON_START` | off | `1` (a game's own config): relative mouse motion and automatic pointer lock from launch, without a hidden-cursor report. For first-person games. | | `MADEIRA_NAV_KEYS_E0` | on | `0`: the driver's previous extended-key flags for the navigation keys. | ## Logs diff --git a/tests/host/check-pointer-capture.py b/tests/host/check-pointer-capture.py index c38d333e3e..21e48f3981 100644 --- a/tests/host/check-pointer-capture.py +++ b/tests/host/check-pointer-capture.py @@ -45,6 +45,7 @@ def member(declaration): static var pointerLockAvailable = true static var desktopMode = false static var hiddenCursorAbsolute = false // MADEIRA_POINTER_HIDDEN_ABSOLUTE, off by default + static var lockOnStart = false // MADEIRA_POINTER_LOCK_ON_START, off by default static let moveFlag: UInt32 = 1 static let absoluteFlag: UInt32 = 0x8000 static func desktopSize() -> (w: Int, h: Int) { (1024, 768) } @@ -68,9 +69,10 @@ def member(declaration): ''' tests = r''' -func reset(relative: Bool = false, desktop: Bool = false) -> HardwareInput { +func reset(relative: Bool = false, desktop: Bool = false, lockOnStart: Bool = false) -> HardwareInput { Clock.now = 100 InputSettings.shared.relative = relative + HardwareInput.lockOnStart = lockOnStart HardwareInput.mouseRoutingEnabled = true HardwareInput.autoLockEnabled = true HardwareInput.directCursorEnabled = true @@ -99,6 +101,31 @@ def member(declaration): assert(!h.pointerLocked, "focus loss did not release capture") } +// A game's lock-on-start setting captures like Relative mode with the toggle off, +// and keeps capture while the game shows a cursor. +do { + let h = reset(lockOnStart: true) + h.cursorState = (1, 1) + assert(!h.cursorShownForRoute) + h.updateAutoLock() + assert(h.pointerLocked && h.lockedByUs, "lock on start did not capture") + Clock.now += 10 + h.lastGCDeltaAt = Clock.now + h.updateAutoLock() + assert(h.pointerLocked, "lock on start released while the game shows a cursor") + h.baseFocused = false + h.updateAutoLock() + assert(!h.pointerLocked, "focus loss did not release lock on start") +} +// Without the setting, the same shown cursor keeps the pointer free. +do { + let h = reset() + h.cursorState = (1, 1) + h.lastReportAt = Clock.now + h.updateAutoLock() + assert(!h.pointerLocked) +} + // Explicit release, unsupported paths and idle motion must not be overridden. for condition in 0..<6 { let h = reset(relative: true)