Skip to content

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

Touch controls: a size setting for every game (patch 0064)

v0lumehid95dae4ecfa8
diff --git a/frontend-ios/README.md b/frontend-ios/README.md
index f434d0f0..fecaf276 100644
--- a/frontend-ios/README.md
+++ b/frontend-ios/README.md
@@ -63,6 +63,7 @@ The caller returns changed values through `games`. The views keep no second copy
| `FolderPicker` | `DocumentFolderPicker` is a `UIDocumentPickerViewController` for a folder, opened in place. Tests use a fake. | `DocumentPicker` in `dm/ios/Sources/Launcher.swift` |
| Log | Show log reads the tail (256 KB) of `log.txt` in the game's data folder, else the app's folder, into a read-only sheet. | |
| Update | `ShellHostConfig.updateURL` is opened by Update app. | |
+| Touch controls | The shell settings' Touch controls group, for every game: a preview of the controls on a phone held sideways (`TouchControlsPreview`, which mirrors the engine's `DmPadGeometry.h` for that screen) and the Size row (small, medium, large). `ShellSettingsStore.touchSize` stores it, and `gameDefaults` passes it to the engine as `DM_TOUCH_SIZE` (medium is the engine's own size and is not passed). The engine keeps a size off a screen too small for it. `TouchControlsCaptureTests` renders the group on the Mac. | `dm/ios/Sources/DmPadGeometry.h` (patch 0064) |
| Controllers | `ShellHostModel.startControllers()` runs `GamepadAdapter` and fills `controllers` (`ControllerInfo`) and `input` (`ShellInput`). Global mappings are stored in `ShellSettingsStore.padMap`, individual overrides in the game's suite under `GameDefaults.padMapKey`. At launch, each button resolves game > global > default and is passed to the engine as `DM_PAD_MAP`. A profile's own `DM_PAD_MAP` row takes precedence. Touch controls and shell navigation keep their original mapping. | |
```swift
diff --git a/frontend-ios/Sources/ShellUI/Resources/Localizable.xcstrings b/frontend-ios/Sources/ShellUI/Resources/Localizable.xcstrings
index ebecb56c..4147c767 100644
--- a/frontend-ios/Sources/ShellUI/Resources/Localizable.xcstrings
+++ b/frontend-ios/Sources/ShellUI/Resources/Localizable.xcstrings
@@ -2107,6 +2107,46 @@
}
}
},
+ "For every game. On a screen too small for a size, the controls stay smaller.": {
+ "localizations": {
+ "de": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Für alle Spiele. Ist der Bildschirm für eine Größe zu klein, bleiben die Tasten kleiner."
+ }
+ },
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "For every game. On a screen too small for a size, the controls stay smaller."
+ }
+ },
+ "fr": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Pour tous les jeux. Sur un écran trop petit pour une taille, les commandes restent plus petites."
+ }
+ },
+ "it": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Per tutti i giochi. Su uno schermo troppo piccolo per una dimensione, i comandi restano più piccoli."
+ }
+ },
+ "sv": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "För alla spel. På en skärm som är för liten för en storlek förblir kontrollerna mindre."
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "适用于所有游戏。屏幕放不下所选大小时,按键会保持较小。"
+ }
+ }
+ }
+ },
"For this game only. A button left alone does what it does in all games.": {
"localizations": {
"de": {
@@ -2987,6 +3027,46 @@
}
}
},
+ "Large": {
+ "localizations": {
+ "de": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Groß"
+ }
+ },
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Large"
+ }
+ },
+ "fr": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Grande"
+ }
+ },
+ "it": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Grande"
+ }
+ },
+ "sv": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Stor"
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "大"
+ }
+ }
+ }
+ },
"Latin American Spanish": {
"localizations": {
"de": {
@@ -3243,6 +3323,46 @@
}
}
},
+ "Medium": {
+ "localizations": {
+ "de": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Mittel"
+ }
+ },
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Medium"
+ }
+ },
+ "fr": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Moyenne"
+ }
+ },
+ "it": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Media"
+ }
+ },
+ "sv": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Mellan"
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "中"
+ }
+ }
+ }
+ },
"Metal 4 pipelines": {
"localizations": {
"de": {
@@ -4153,6 +4273,46 @@
}
}
},
+ "Preview of the touch controls": {
+ "localizations": {
+ "de": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Vorschau der Touch-Steuerung"
+ }
+ },
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Preview of the touch controls"
+ }
+ },
+ "fr": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Aperçu des commandes tactiles"
+ }
+ },
+ "it": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Anteprima dei comandi touch"
+ }
+ },
+ "sv": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Förhandsvisning av pekkontrollerna"
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "触控按键预览"
+ }
+ }
+ }
+ },
"Puts A on the bottom button, where Xbox and PlayStation controllers confirm.": {
"localizations": {
"de": {
@@ -4985,6 +5145,86 @@
}
}
},
+ "Size": {
+ "localizations": {
+ "de": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Größe"
+ }
+ },
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Size"
+ }
+ },
+ "fr": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Taille"
+ }
+ },
+ "it": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Dimensione"
+ }
+ },
+ "sv": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Storlek"
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "大小"
+ }
+ }
+ }
+ },
+ "Small": {
+ "localizations": {
+ "de": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Klein"
+ }
+ },
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Small"
+ }
+ },
+ "fr": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Petite"
+ }
+ },
+ "it": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Piccola"
+ }
+ },
+ "sv": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Liten"
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "小"
+ }
+ }
+ }
+ },
"Spanish": {
"localizations": {
"de": {
@@ -5297,6 +5537,46 @@
}
}
},
+ "Touch controls": {
+ "localizations": {
+ "de": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Touch-Steuerung"
+ }
+ },
+ "en": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Touch controls"
+ }
+ },
+ "fr": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Commandes tactiles"
+ }
+ },
+ "it": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Comandi touch"
+ }
+ },
+ "sv": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "Pekkontroller"
+ }
+ },
+ "zh-Hans": {
+ "stringUnit": {
+ "state": "translated",
+ "value": "触控按键"
+ }
+ }
+ }
+ },
"Touch feedback": {
"localizations": {
"de": {
diff --git a/frontend-ios/Sources/ShellUI/ShellSettingsStore.swift b/frontend-ios/Sources/ShellUI/ShellSettingsStore.swift
index d0d3a417..5cbffcc5 100644
--- a/frontend-ios/Sources/ShellUI/ShellSettingsStore.swift
+++ b/frontend-ios/Sources/ShellUI/ShellSettingsStore.swift
@@ -1,7 +1,7 @@
import Foundation
-/// The shell-level settings, one suite shared by every game: the game-data folder, the cloud-saves folder
-/// and the default performance HUD. A game's own settings and bookmarks win over these, and
+/// The shell-level settings, one suite shared by every game: the game-data folder, the cloud-saves folder,
+/// the default performance HUD and the touch controls. A game's own settings and bookmarks win over these, and
/// these win over the built-in defaults (ShellSettingsStore priority: game > shell > built-in).
public final class ShellSettingsStore: @unchecked Sendable {
public static let padMapKey = "padMap"
@@ -11,6 +11,9 @@ public final class ShellSettingsStore: @unchecked Sendable {
public static let performanceHUDKey = "performanceHUD" // true/false; a game's own row wins when set
public static let dataFolderNameKey = "globalDataFolderName"
public static let cloudSavesNameKey = "globalCloudSavesFolderName"
+ public static let touchSizeKey = "touchSize" // small, medium or large: DM_TOUCH_SIZE for every game
+ /// The touch controls' sizes, as the engine reads DM_TOUCH_SIZE. Medium is the built-in size and is never stored.
+ public static let touchSizes = ["small", "medium", "large"]
/// The store the app reads and writes. Tests and previews swap it.
nonisolated(unsafe) public static var current = ShellSettingsStore()
@@ -37,6 +40,16 @@ public final class ShellSettingsStore: @unchecked Sendable {
public func clearCloudSavesFolderName() { defaults.removeObject(forKey: Self.cloudSavesNameKey) }
public func storePerformanceHUD(_ on: Bool) { defaults.set(on, forKey: Self.performanceHUDKey) }
+ /// The touch controls' size for every game: small, medium (the built-in size) or large.
+ public var touchSize: String {
+ let value = defaults.string(forKey: Self.touchSizeKey) ?? "medium"
+ return Self.touchSizes.contains(value) ? value : "medium"
+ }
+ public func storeTouchSize(_ size: String) {
+ guard Self.touchSizes.contains(size) else { return }
+ if size == "medium" { defaults.removeObject(forKey: Self.touchSizeKey) } else { defaults.set(size, forKey: Self.touchSizeKey) }
+ }
+
public var padMap: PadMap { PadMap(defaults.dictionary(forKey: Self.padMapKey) as? [String: String] ?? [:]) }
public func setPadButton(_ button: String, to output: String) {
var map = padMap
@@ -53,6 +66,8 @@ public final class ShellSettingsStore: @unchecked Sendable {
/// DM_PAD_MAP is deliberately excluded. The host merges it per button at launch.
/// The shell's value for each `DM_*` switch a game's own settings leave alone.
public var gameDefaults: [String: String] {
- performanceHUD.map { ["DM_METAL_HUD": $0 ? "1" : "0"] } ?? [:]
+ var values = performanceHUD.map { ["DM_METAL_HUD": $0 ? "1" : "0"] } ?? [:]
+ if touchSize != "medium" { values["DM_TOUCH_SIZE"] = touchSize }
+ return values
}
}
diff --git a/frontend-ios/Sources/ShellUI/ShellSettingsView.swift b/frontend-ios/Sources/ShellUI/ShellSettingsView.swift
index 543bc7f7..f03481e4 100644
--- a/frontend-ios/Sources/ShellUI/ShellSettingsView.swift
+++ b/frontend-ios/Sources/ShellUI/ShellSettingsView.swift
@@ -5,6 +5,7 @@ import SwiftUI
enum ShellSettingsRows {
static let gameID = "shell"
static let hud = "shell.hud"
+ static let touchSize = "shell.touchSize"
static let targetPrefix = "shs.row."
static func hudRow(_ store: ShellSettingsStore) -> SettingRow {
@@ -12,16 +13,29 @@ enum ShellSettingsRows {
kind: .toggle(on: "1", off: "0"), value: store.performanceHUD == true ? "1" : "0")
}
+ /// The touch controls' size for every game. The preview above the row shows it.
+ static func touchSizeRow(_ store: ShellSettingsStore) -> SettingRow {
+ SettingRow(id: touchSize, label: ShellStrings.text("Size"),
+ kind: .choice([SettingOption("small", ShellStrings.text("Small")), SettingOption("medium", ShellStrings.text("Medium")),
+ SettingOption("large", ShellStrings.text("Large"))]),
+ value: store.touchSize)
+ }
+
+ /// Under the preview: the rows are for every game, and the engine keeps a size off a screen too small for it. A note of its
+ /// own, not the row's, so the segments keep the card's width.
+ static var touchNote: String { ShellStrings.text("For every game. On a screen too small for a size, the controls stay smaller.") }
+
/// A change of one of the rows, from the row itself or from the list of choices.
static func apply(_ id: String, value: String, to store: ShellSettingsStore) {
switch id {
case hud: store.storePerformanceHUD(value == "1")
+ case touchSize: store.storeTouchSize(value)
default: break
}
}
}
-/// The shell's own settings: the game-data folder, the cloud-saves folder and the default performance
+/// The shell's own settings: the game-data folder, the cloud-saves folder, the touch controls and the default performance
/// overlay. A game's own choices win over these, and these win over the built-in defaults. It looks like a game's
/// settings and takes a controller the same way, as a panel over the library.
struct ShellSettingsView: View {
@@ -76,6 +90,14 @@ struct ShellSettingsView: View {
}
}
}
+ SettingsGroup(title: ShellStrings.text("Touch controls")) {
+ TouchControlsPreview(size: store.touchSize)
+ TouchControlsNote()
+ let size = ShellSettingsRows.touchSizeRow(store)
+ SettingControl(gameID: ShellSettingsRows.gameID, row: size, targetPrefix: ShellSettingsRows.targetPrefix) {
+ ShellSettingsRows.apply(size.id, value: $0, to: store)
+ }.equatable()
+ }
SettingsGroup(title: ShellStrings.text("Interface")) {
let hud = ShellSettingsRows.hudRow(store)
SettingControl(gameID: ShellSettingsRows.gameID, row: hud, targetPrefix: ShellSettingsRows.targetPrefix) {
diff --git a/frontend-ios/Sources/ShellUI/TouchControlsPreview.swift b/frontend-ios/Sources/ShellUI/TouchControlsPreview.swift
new file mode 100644
index 00000000..7feb0c28
--- /dev/null
+++ b/frontend-ios/Sources/ShellUI/TouchControlsPreview.swift
@@ -0,0 +1,133 @@
+import SwiftUI
+
+/// Where the touch controls sit on an iPhone Pro Max held sideways (932 x 430 points, the side insets of the Dynamic Island and the home
+/// indicator), for the preview in the settings. It mirrors the engine's `DmPadGeometry.h` (patch 0064) for that one screen, in the 3D
+/// layout so every control shows: the same constants in u, the bottom corners halfway into the side insets. The engine stays the
+/// truth on the device, where a size that does not fit a screen moves toward medium.
+struct TouchPadPreviewGeometry: Equatable {
+ static let screen = CGSize(width: 932, height: 430)
+ static let insets = (left: 59.0, right: 59.0, bottom: 21.0)
+
+ enum Shape: Equatable {
+ case circle(CGPoint, CGFloat, label: String?)
+ case drop(CGPoint, CGFloat, tipRight: Bool, label: String)
+ case pill(CGRect, label: String)
+ case dpad(CGPoint, CGFloat)
+ case ring(CGPoint, CGFloat, knob: CGFloat)
+ }
+
+ /// DM_TOUCH_SIZE's scale: small 0.8, medium 1, large 1.2.
+ static func scale(_ size: String) -> CGFloat { size == "small" ? 0.8 : size == "large" ? 1.2 : 1 }
+
+ let u: CGFloat
+ init(size: String) { u = Self.scale(size) }
+
+ private var safe: CGRect { CGRect(x: Self.insets.left, y: 0, width: Self.screen.width - Self.insets.left - Self.insets.right, height: Self.screen.height) }
+ private var corner: CGRect {
+ CGRect(x: Self.insets.left / 2, y: 0, width: Self.screen.width - (Self.insets.left + Self.insets.right) / 2, height: Self.screen.height - Self.insets.bottom)
+ }
+ private func side(_ r: CGRect, right: Bool, _ dx: CGFloat, _ y: CGFloat) -> CGPoint { CGPoint(x: right ? r.maxX - dx : r.minX + dx, y: y) }
+
+ var stick: CGPoint { side(corner, right: false, 81 * u, corner.maxY - 82 * u) }
+ var face: CGPoint { side(corner, right: true, 81 * u, corner.maxY - 82 * u) }
+ var dpad: CGPoint { side(corner, right: false, 231 * u, corner.maxY - 64 * u) }
+
+ var shapes: [Shape] {
+ var out: [Shape] = []
+ let f = face, gap = 50 * u
+ for (dx, dy, name) in [(gap, 0.0, "A"), (0, gap, "B"), (0, -gap, "X"), (-gap, 0, "Y")] {
+ out.append(.circle(CGPoint(x: f.x + dx, y: f.y + dy), 27 * u, label: name))
+ }
+ out.append(.ring(stick, 77 * u, knob: 30 * u))
+ out.append(.dpad(dpad, 48 * u))
+ for right in [false, true] {
+ out.append(.drop(side(safe, right: right, 44 * u, 40 * u), 26 * u, tipRight: right, label: right ? "ZR" : "ZL"))
+ out.append(.drop(side(safe, right: right, 44 * u, 108 * u), 24 * u, tipRight: right, label: right ? "R" : "L"))
+ out.append(.circle(side(safe, right: right, 102 * u, 40 * u), 17 * u, label: right ? "R3" : "L3"))
+ let w = 50 * u, h = 28 * u, x = safe.midX + (right ? 52 : -52) * u
+ out.append(.pill(CGRect(x: x - w / 2, y: 22 * u - h / 2, width: w, height: h), label: right ? "+" : "−"))
+ }
+ out.append(.circle(CGPoint(x: safe.midX, y: 22 * u), 15 * u, label: nil))
+ return out
+ }
+
+ /// The bounding box of a shape, for the tests.
+ static func bounds(_ shape: Shape) -> CGRect {
+ switch shape {
+ case .circle(let c, let r, _), .ring(let c, let r, _): CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r)
+ case .drop(let c, let r, _, _): CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r)
+ case .dpad(let c, let r): CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r)
+ case .pill(let rect, _): rect
+ }
+ }
+}
+
+/// The line under the preview: what the touch controls rows apply to.
+struct TouchControlsNote: View {
+ var body: some View {
+ Text(ShellSettingsRows.touchNote).shellType(ShellType.note).foregroundStyle(.white.opacity(0.5))
+ .fixedSize(horizontal: false, vertical: true)
+ }
+}
+
+/// A phone held sideways with the touch controls drawn as the game draws them: white keys of low opacity over a calm picture.
+/// It is a picture, VoiceOver reads it as one element with its size, and the rows below it change it.
+struct TouchControlsPreview: View {
+ var size: String
+
+ var body: some View {
+ let geometry = TouchPadPreviewGeometry(size: size)
+ Canvas { context, canvas in
+ let k = canvas.width / TouchPadPreviewGeometry.screen.width
+ context.scaleBy(x: k, y: k)
+ for shape in geometry.shapes { draw(shape, in: &context) }
+ }
+ .aspectRatio(TouchPadPreviewGeometry.screen.width / TouchPadPreviewGeometry.screen.height, contentMode: .fit)
+ .background(
+ LinearGradient(colors: [Color(red: 0.20, green: 0.24, blue: 0.30), Color(red: 0.11, green: 0.13, blue: 0.12)], startPoint: .top, endPoint: .bottom)
+ )
+ .clipShape(RoundedRectangle(cornerRadius: 22, style: .continuous))
+ .overlay(RoundedRectangle(cornerRadius: 22, style: .continuous).strokeBorder(.white.opacity(0.14), lineWidth: 3))
+ .animation(.smooth(duration: 0.2), value: size)
+ .accessibilityElement()
+ .accessibilityLabel(ShellStrings.text("Preview of the touch controls"))
+ .accessibilityIdentifier("touch.preview")
+ }
+
+ private static let fill = Color.white.opacity(0.14), line = Color.white.opacity(0.38), text = Color.white.opacity(0.6)
+
+ private func draw(_ shape: TouchPadPreviewGeometry.Shape, in context: inout GraphicsContext) {
+ func key(_ path: Path) { context.fill(path, with: .color(Self.fill)); context.stroke(path, with: .color(Self.line), lineWidth: 1.5) }
+ func label(_ s: String, _ c: CGPoint, _ size: CGFloat) {
+ context.draw(Text(s).font(.system(size: size, weight: .semibold)).foregroundStyle(Self.text), at: c)
+ }
+ switch shape {
+ case .circle(let c, let r, let name):
+ key(Path(ellipseIn: CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r)))
+ if let name { label(name, c, r * 0.75) }
+ case .ring(let c, let r, let knob):
+ let ring = Path(ellipseIn: CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r))
+ context.fill(ring, with: .color(.white.opacity(0.06))); context.stroke(ring, with: .color(Self.line), lineWidth: 1.5)
+ key(Path(ellipseIn: CGRect(x: c.x - knob, y: c.y - knob, width: 2 * knob, height: 2 * knob)))
+ case .drop(let c, let r, let right, let name):
+ let angle = right ? -Double.pi / 4 : -3 * Double.pi / 4, d = r * 1.45, th = acos(r / d)
+ var p = Path()
+ p.move(to: CGPoint(x: c.x + d * cos(angle), y: c.y + d * sin(angle)))
+ p.addArc(center: c, radius: r, startAngle: .radians(angle + th), endAngle: .radians(angle - th + 2 * .pi), clockwise: false)
+ p.closeSubpath()
+ key(p)
+ label(name, c, r * 0.62)
+ case .pill(let rect, let name):
+ key(Path(roundedRect: rect, cornerRadius: rect.height / 2))
+ label(name, CGPoint(x: rect.midX, y: rect.midY), rect.height * 0.64)
+ case .dpad(let c, let r):
+ let w = r * 17 / 48, inner = r * 11 / 48
+ for turn in 0..<4 {
+ var p = Path()
+ p.addLines([CGPoint(x: -w, y: -r), CGPoint(x: w, y: -r), CGPoint(x: w, y: -(inner + w)), CGPoint(x: 0, y: -inner), CGPoint(x: -w, y: -(inner + w))])
+ p.closeSubpath()
+ key(p.applying(CGAffineTransform(rotationAngle: CGFloat(turn) * .pi / 2).concatenating(CGAffineTransform(translationX: c.x, y: c.y))))
+ }
+ }
+ }
+}
diff --git a/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift b/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift
index 489a7a44..ed90d600 100644
--- a/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift
+++ b/frontend-ios/Tests/ShellHostTests/ShellHostModelTests.swift
@@ -30,6 +30,23 @@ final class Clock: @unchecked Sendable { var date: Date; init(_ d: Date) { date
try detail(model, game).sections.flatMap(\.rows).first { $0.id == setting }?.value
}
+ @Test func theTouchSizeReachesEveryGameAsDMTouchSize() async throws {
+ let box = try Sandbox()
+ try box.giveAppData()
+ let shell = shellSettings()
+ let launcher = RecordingLauncher()
+ let model = ShellHostModel(config: box.config(), launcher: launcher, shellSettings: shell)
+ await model.playNow(Fixtures.orbit)
+ #expect(launcher.launched.last?.environment["DM_TOUCH_SIZE"] == nil, "medium is the engine's own size")
+ shell.storeTouchSize("large")
+ await model.playNow(Fixtures.orbit)
+ #expect(launcher.launched.last?.environment["DM_TOUCH_SIZE"] == "large")
+ #expect(try !detail(model).hasModifiedSettings, "a shell setting is not a change in this game")
+ shell.storeTouchSize("medium")
+ await model.playNow(Fixtures.orbit)
+ #expect(launcher.launched.last?.environment["DM_TOUCH_SIZE"] == nil)
+ }
+
@Test func padOverridesPersistMergeAndReset() async throws {
let box = try Sandbox()
try box.giveAppData()
diff --git a/frontend-ios/Tests/ShellHostTests/ShellSettingsStoreTests.swift b/frontend-ios/Tests/ShellHostTests/ShellSettingsStoreTests.swift
index a770ce09..7064f63d 100644
--- a/frontend-ios/Tests/ShellHostTests/ShellSettingsStoreTests.swift
+++ b/frontend-ios/Tests/ShellHostTests/ShellSettingsStoreTests.swift
@@ -34,6 +34,22 @@ import ShellUI
#expect(store.gameDefaults == ["DM_METAL_HUD": "0"])
}
+ @Test func theTouchSizeGoesToEveryGameAndMediumIsNotStored() {
+ let store = shell()
+ #expect(store.touchSize == "medium" && store.gameDefaults["DM_TOUCH_SIZE"] == nil)
+ store.storeTouchSize("large")
+ #expect(ShellSettingsStore(defaults: store.defaults).touchSize == "large")
+ #expect(store.gameDefaults == ["DM_TOUCH_SIZE": "large"])
+ store.storeTouchSize("huge")
+ #expect(store.touchSize == "large", "a size the engine does not know is not stored")
+ store.storeTouchSize("small")
+ #expect(store.gameDefaults["DM_TOUCH_SIZE"] == "small")
+ store.storeTouchSize("medium")
+ #expect(store.defaults.object(forKey: ShellSettingsStore.touchSizeKey) == nil && store.gameDefaults.isEmpty)
+ store.defaults.set("giant", forKey: ShellSettingsStore.touchSizeKey)
+ #expect(store.touchSize == "medium" && store.gameDefaults.isEmpty, "a stored value nobody knows reads as medium")
+ }
+
@Test func theFolderNamesAreKept() {
let store = shell()
store.storeDataFolderName("Switch")
diff --git a/frontend-ios/Tests/ShellUITests/TouchControlsCaptureTests.swift b/frontend-ios/Tests/ShellUITests/TouchControlsCaptureTests.swift
new file mode 100644
index 00000000..41fa1bbb
--- /dev/null
+++ b/frontend-ios/Tests/ShellUITests/TouchControlsCaptureTests.swift
@@ -0,0 +1,65 @@
+#if os(macOS)
+import AppKit
+import SwiftUI
+import XCTest
+@testable import ShellUI
+
+/// Visual QA of the touch controls settings (#347), rendered on the Mac: the shell settings page with the Touch controls group, at
+/// each size. Set NX_SHELL_CAPTURE_DIR to write the PNGs, NX_SHELL_CAPTURE_LANG (en, zh, de, ...) for the language.
+@MainActor
+final class TouchControlsCaptureTests: XCTestCase {
+ private var lang: String { ProcessInfo.processInfo.environment["NX_SHELL_CAPTURE_LANG"] ?? "en" }
+
+ override func setUp() async throws {
+ guard ProcessInfo.processInfo.environment["NX_SHELL_CAPTURE_DIR"] != nil else { throw XCTSkip("Set NX_SHELL_CAPTURE_DIR to capture the shell") }
+ if let path = ShellStrings.bundle.path(forResource: lang == "zh" ? "zh-Hans" : lang, ofType: "lproj") {
+ ShellStrings.captureBundle = Bundle(path: path)
+ }
+ }
+ override func tearDown() async throws { ShellStrings.captureBundle = nil }
+
+ private func capture<V: View>(_ name: String, width: CGFloat, height: CGFloat, _ view: V) throws {
+ let dir = try XCTUnwrap(ProcessInfo.processInfo.environment["NX_SHELL_CAPTURE_DIR"])
+ NSApplication.shared.setActivationPolicy(.accessory)
+ let host = NSHostingView(rootView: view.frame(width: width, height: height).background(ShellPalette.background).environment(\.colorScheme, .dark))
+ host.frame = NSRect(x: 0, y: 0, width: width, height: height)
+ let window = NSWindow(contentRect: NSRect(origin: NSPoint(x: -100_000, y: -100_000), size: host.frame.size),
+ styleMask: [.borderless], backing: .buffered, defer: false)
+ window.isReleasedWhenClosed = false
+ window.contentView = host
+ window.orderFront(nil)
+ defer { window.orderOut(nil); window.contentView = nil }
+ RunLoop.main.run(until: Date().addingTimeInterval(0.4))
+ host.layoutSubtreeIfNeeded()
+ let rep = try XCTUnwrap(host.bitmapImageRepForCachingDisplay(in: host.bounds))
+ host.cacheDisplay(in: host.bounds, to: rep)
+ try XCTUnwrap(rep.representation(using: .png, properties: [:])).write(to: URL(fileURLWithPath: dir).appendingPathComponent("\(name)-\(lang).png"))
+ }
+
+ /// A store of its own, so the capture never reads or writes the Mac's settings.
+ private func store(_ values: [String: String] = [:]) -> ShellSettingsStore {
+ let name = "touch-capture-\(UUID().uuidString)"
+ let defaults = UserDefaults(suiteName: name)!
+ defaults.removePersistentDomain(forName: name)
+ for (key, value) in values { defaults.set(value, forKey: key) }
+ return ShellSettingsStore(defaults: defaults)
+ }
+
+ func testCaptureTheSettingsPage() throws {
+ try capture("touch-settings", width: 402, height: 1400, ShellSettingsView(store: store(), actions: ShellActions(), onClose: {}))
+ }
+
+ /// The group alone at each size, at a phone's width and an iPad panel's.
+ func testCaptureEachSize() throws {
+ for size in ShellSettingsStore.touchSizes {
+ let s = store([ShellSettingsStore.touchSizeKey: size])
+ let group = SettingsGroup(title: ShellStrings.text("Touch controls")) {
+ TouchControlsPreview(size: s.touchSize)
+ TouchControlsNote()
+ SettingControl(gameID: ShellSettingsRows.gameID, row: ShellSettingsRows.touchSizeRow(s), change: { _ in })
+ }.padding(24)
+ try capture("touch-size-\(size)", width: 402, height: 520, group)
+ }
+ }
+}
+#endif
diff --git a/frontend-ios/Tests/ShellUITests/TouchControlsTests.swift b/frontend-ios/Tests/ShellUITests/TouchControlsTests.swift
new file mode 100644
index 00000000..99563c09
--- /dev/null
+++ b/frontend-ios/Tests/ShellUITests/TouchControlsTests.swift
@@ -0,0 +1,51 @@
+import Foundation
+import Testing
+@testable import ShellUI
+
+/// The touch controls settings (#347): the size row and the preview's geometry.
+@Suite struct TouchControlsTests {
+ private func store() -> ShellSettingsStore { ShellSettingsStore(defaults: UserDefaults(suiteName: "t-touch-\(UUID().uuidString)")!) }
+
+ @Test func theSizeRowOffersTheEngineSizesAndStoresThem() {
+ let store = store()
+ let row = ShellSettingsRows.touchSizeRow(store)
+ guard case .choice(let options) = row.kind else { Issue.record("not a choice"); return }
+ #expect(options.map(\.id) == ShellSettingsStore.touchSizes && row.value == "medium")
+ ShellSettingsRows.apply(row.id, value: "small", to: store)
+ #expect(ShellSettingsRows.touchSizeRow(store).value == "small" && store.gameDefaults["DM_TOUCH_SIZE"] == "small")
+ }
+
+ @Test func theSizeRowIsSegmentedOnAPhone() {
+ guard case .choice(let options) = ShellSettingsRows.touchSizeRow(store()).kind else { return }
+ #expect(ChoicePresentation.resolve(labels: options.map(\.label), width: 320, textScale: 1) == .segmented)
+ }
+
+ /// The preview mirrors the engine's built-in layout on iPhone Pro Max (DmPadGeometry.h): the same points at medium.
+ @Test func thePreviewAtMediumIsTheEnginesLayout() {
+ let g = TouchPadPreviewGeometry(size: "medium")
+ #expect(g.face == CGPoint(x: 932 - 29.5 - 81, y: 430 - 21 - 82))
+ #expect(g.stick == CGPoint(x: 29.5 + 81, y: 430 - 21 - 82))
+ #expect(g.dpad == CGPoint(x: 29.5 + 231, y: 430 - 21 - 64))
+ }
+
+ @Test(arguments: ShellSettingsStore.touchSizes)
+ func everyPreviewShapeIsOnTheScreenAndClearOfTheOthers(size: String) {
+ let screen = CGRect(origin: .zero, size: TouchPadPreviewGeometry.screen)
+ let boxes = TouchPadPreviewGeometry(size: size).shapes.map(TouchPadPreviewGeometry.bounds)
+ for box in boxes { #expect(screen.contains(box), "\(size): \(box) leaves the screen") }
+ // Bounding boxes of the round keys may touch at their corners, so compare the circles by their centres.
+ let shapes = TouchPadPreviewGeometry(size: size).shapes
+ for i in shapes.indices {
+ for j in shapes.indices where j > i {
+ if case .circle(let a, let ra, _) = shapes[i], case .circle(let b, let rb, _) = shapes[j] {
+ #expect(hypot(a.x - b.x, a.y - b.y) >= ra + rb, "\(size): keys \(i) and \(j) overlap")
+ }
+ }
+ }
+ }
+
+ @Test func theSizesKeepTheirOrder() {
+ #expect(TouchPadPreviewGeometry(size: "small").u < TouchPadPreviewGeometry(size: "medium").u)
+ #expect(TouchPadPreviewGeometry(size: "large").u > TouchPadPreviewGeometry(size: "medium").u)
+ }
+}
diff --git a/frontend/l10n/de.json b/frontend/l10n/de.json
index 42b02c39..64202886 100644
--- a/frontend/l10n/de.json
+++ b/frontend/l10n/de.json
@@ -661,6 +661,13 @@
"ios.ui.padTestNote": "Jede Taste leuchtet auf, wenn du sie drückst.",
"ios.ui.padTopButton": "Obere Taste",
"ios.ui.padUp": "Oben",
+ "ios.ui.touchControls": "Touch-Steuerung",
+ "ios.ui.touchLarge": "Groß",
+ "ios.ui.touchMedium": "Mittel",
+ "ios.ui.touchPreview": "Vorschau der Touch-Steuerung",
+ "ios.ui.touchSize": "Größe",
+ "ios.ui.touchSizeNote": "Für alle Spiele. Ist der Bildschirm für eine Größe zu klein, bleiben die Tasten kleiner.",
+ "ios.ui.touchSmall": "Klein",
"key.clear": "Löschen",
"key.code": "Taste %ld",
"key.delete": "Rückschritt",
diff --git a/frontend/l10n/en.json b/frontend/l10n/en.json
index 32e52638..b4290670 100644
--- a/frontend/l10n/en.json
+++ b/frontend/l10n/en.json
@@ -779,6 +779,13 @@
"ios.ui.showLog": "Show log",
"ios.ui.tagline": "A little room for adventure",
"ios.ui.titleId": "Title ID %@",
+ "ios.ui.touchControls": "Touch controls",
+ "ios.ui.touchLarge": "Large",
+ "ios.ui.touchMedium": "Medium",
+ "ios.ui.touchPreview": "Preview of the touch controls",
+ "ios.ui.touchSize": "Size",
+ "ios.ui.touchSizeNote": "For every game. On a screen too small for a size, the controls stay smaller.",
+ "ios.ui.touchSmall": "Small",
"ios.ui.turnOff": "Turn off",
"ios.ui.turnOffCloudSaves": "Turn off cloud saves",
"ios.ui.unavailable": "Unavailable",
diff --git a/frontend/l10n/fr.json b/frontend/l10n/fr.json
index 7509a532..ae68ea82 100644
--- a/frontend/l10n/fr.json
+++ b/frontend/l10n/fr.json
@@ -663,6 +663,13 @@
"ios.ui.padTestNote": "Chaque bouton s’allume quand vous appuyez dessus.",
"ios.ui.padTopButton": "Bouton du haut",
"ios.ui.padUp": "Haut",
+ "ios.ui.touchControls": "Commandes tactiles",
+ "ios.ui.touchLarge": "Grande",
+ "ios.ui.touchMedium": "Moyenne",
+ "ios.ui.touchPreview": "Aperçu des commandes tactiles",
+ "ios.ui.touchSize": "Taille",
+ "ios.ui.touchSizeNote": "Pour tous les jeux. Sur un écran trop petit pour une taille, les commandes restent plus petites.",
+ "ios.ui.touchSmall": "Petite",
"key.clear": "Effacer",
"key.code": "Touche %ld",
"key.delete": "Supprimer",
diff --git a/frontend/l10n/it.json b/frontend/l10n/it.json
index b8907b36..f10abce3 100644
--- a/frontend/l10n/it.json
+++ b/frontend/l10n/it.json
@@ -664,6 +664,13 @@
"ios.ui.padTestNote": "Ogni tasto si illumina quando lo premi.",
"ios.ui.padTopButton": "Tasto in alto",
"ios.ui.padUp": "Su",
+ "ios.ui.touchControls": "Comandi touch",
+ "ios.ui.touchLarge": "Grande",
+ "ios.ui.touchMedium": "Media",
+ "ios.ui.touchPreview": "Anteprima dei comandi touch",
+ "ios.ui.touchSize": "Dimensione",
+ "ios.ui.touchSizeNote": "Per tutti i giochi. Su uno schermo troppo piccolo per una dimensione, i comandi restano più piccoli.",
+ "ios.ui.touchSmall": "Piccola",
"key.clear": "Cancella",
"key.code": "Tasto %ld",
"key.delete": "Elimina",
diff --git a/frontend/l10n/sv.json b/frontend/l10n/sv.json
index 1312a647..db1ddf5f 100644
--- a/frontend/l10n/sv.json
+++ b/frontend/l10n/sv.json
@@ -506,6 +506,13 @@
"ios.ui.padTestNote": "Se varje knapp lysa upp när du trycker på den.",
"ios.ui.padTopButton": "Övre knappen",
"ios.ui.padUp": "Upp",
+ "ios.ui.touchControls": "Pekkontroller",
+ "ios.ui.touchLarge": "Stor",
+ "ios.ui.touchMedium": "Mellan",
+ "ios.ui.touchPreview": "Förhandsvisning av pekkontrollerna",
+ "ios.ui.touchSize": "Storlek",
+ "ios.ui.touchSizeNote": "För alla spel. På en skärm som är för liten för en storlek förblir kontrollerna mindre.",
+ "ios.ui.touchSmall": "Liten",
"key.clear": "Rensa",
"key.code": "Tangent %ld",
"key.delete": "Radera",
diff --git a/frontend/l10n/zh.json b/frontend/l10n/zh.json
index 4884f1c1..0681a77d 100644
--- a/frontend/l10n/zh.json
+++ b/frontend/l10n/zh.json
@@ -780,6 +780,13 @@
"ios.ui.showLog": "显示日志",
"ios.ui.tagline": "给冒险留一点空间",
"ios.ui.titleId": "Title ID %@",
+ "ios.ui.touchControls": "触控按键",
+ "ios.ui.touchLarge": "大",
+ "ios.ui.touchMedium": "中",
+ "ios.ui.touchPreview": "触控按键预览",
+ "ios.ui.touchSize": "大小",
+ "ios.ui.touchSizeNote": "适用于所有游戏。屏幕放不下所选大小时,按键会保持较小。",
+ "ios.ui.touchSmall": "小",
"ios.ui.turnOff": "关闭",
"ios.ui.turnOffCloudSaves": "关闭云存档",
"ios.ui.unavailable": "不可用",
diff --git a/patches/0064-ios-touch-controls-size.patch b/patches/0064-ios-touch-controls-size.patch
new file mode 100644
index 00000000..646f9f22
--- /dev/null
+++ b/patches/0064-ios-touch-controls-size.patch
@@ -0,0 +1,586 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: v0lumehi <[email protected]>
+Date: Tue, 3 Nov 2026 15:11:04 +0100
+Subject: [PATCH] ios: touch controls size (DM_TOUCH_SIZE) on a tested geometry
+
+The virtual gamepad's placement moves out of DmGamepad.m into
+DmPadGeometry.h: pure functions of the view's bounds, its safe area, the
+DmLayout split and the player's options. Drawing, the hit-test and the
+grab read the same values, as before. The built-in size is unchanged.
+
+DM_TOUCH_SIZE=small|medium|large scales every control (0.8, 1, 1.2). A
+size that does not fit the screen moves toward medium in steps: every
+drawn control stays inside the safe area (the bottom corners reach
+halfway into the side insets, as always) and no two overlap.
+
+dm/ios/tests/test_pad_geometry.py builds the header on the Mac with a
+small UIKit stand-in and checks iPhone and iPad in landscape, iPad and
+split windows in portrait, and iPhone Duo's inner display in portrait,
+landscape and tabletop, in 2D and 3D and every size. It also pins the
+built-in layout on iPhone Pro Max to the points it has always used.
+
+Upstream-Status: pending (on seedds/nxnative#683)
+Reason: Players ask for smaller or larger touch controls (#347), and the placement needs tests without a device
+Issue: LNPANDY/NX2Apple#347
+
+---
+ dm/ios/Sources/DmGamepad.m | 118 +++++-------
+ dm/ios/Sources/DmPadGeometry.h | 176 ++++++++++++++++++
+ dm/ios/tests/pad_geometry/UIKit/UIKit.h | 11 ++
+ .../tests/pad_geometry/pad_geometry_check.m | 94 ++++++++++
+ dm/ios/tests/test_pad_geometry.py | 34 ++++
+ 5 files changed, 361 insertions(+), 72 deletions(-)
+ create mode 100644 dm/ios/Sources/DmPadGeometry.h
+ create mode 100644 dm/ios/tests/pad_geometry/UIKit/UIKit.h
+ create mode 100644 dm/ios/tests/pad_geometry/pad_geometry_check.m
+ create mode 100644 dm/ios/tests/test_pad_geometry.py
+
+diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.m
+index 39ad3f2..ec35bb3 100644
+--- a/dm/ios/Sources/DmGamepad.m
++++ b/dm/ios/Sources/DmGamepad.m
+@@ -9,6 +9,7 @@
+ // Touch-down ownership: locked keys first (shoulders, − / +, the layout button, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad),
+ // then in 3D the right stick for a touch in the right third.
+ // Everything hugs the edges and corners to leave the picture room; controls sit over the picture, one CAShapeLayer each: a press only swaps the fill, a stick only moves its knob, nothing redraws the whole screen.
++// Where each control sits is DmPadGeometry.h (pure, tested on the Mac); the player picks the size there through DM_TOUCH_SIZE (small, medium, large; a size that does not fit the screen moves toward medium).
+ // Touches no control takes (in 3D, those outside the right third) go to the game's touch screen when DmLayout assigns a local game viewport (pushGame; Super Mario Galaxy's pointer in handheld mode).
+ // The virtual gamepad (layout button included) hides while a physical controller is connected and comes back when it is removed; unclaimed touches go to the game only when a local game viewport exists. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller); a Nintendo pad maps by its labels (pad_apple.h).
+ // Each physical controller is a player of its own (local co-op), as on the Mac: the first one connected is player 1, together with the touch gamepad, the next player 2, and so on up to 8; see "Physical controllers" below.
+@@ -30,6 +31,7 @@
+ #include "input_map.h"
+ #include <stdatomic.h>
+ #import "DmLayout.h"
++#import "DmPadGeometry.h"
+
+ enum { B_A = 0, B_B, B_X, B_Y, B_LS, B_RS, B_L, B_R, B_ZL, B_ZR, B_PLUS, B_MINUS, B_LEFT, B_UP, B_RIGHT, B_DOWN, B_N };
+ enum { C_NONE = 0, C_STICK, C_DPAD, C_FACE, C_BTN, C_SWAP, C_GAME }; // which control a touch holds; C_GAME: none, the touch goes to the game's touch screen (finger slot << 8)
+@@ -106,27 +108,19 @@ static void phone_motion_start(UIView *body) {
+ }];
+ }
+
+-// ---- Layout constants (in u; x measured from the outer edge, the right half mirrors) ----
++// ---- Layout constants ----
++// Where each control is drawn (in u, x measured from the outer edge, the right half mirrors) is DmPadGeometry.h: pure functions of the bounds, the safe area,
++// the DmLayout split and the player's options (DM_TOUCH_SIZE), tested on the Mac (dm/ios/tests/test_pad_geometry.py). Drawing, the hit-test and the grab all read it.
++// The bottom-corner controls reach halfway into the side safe-area inset (in landscape there is only the rounded screen corner there; the Dynamic Island is mid-edge);
++// the stick's ring is as large as the whole diamond (key distance + key radius), the knob a little larger than a diamond key; the D-pad sits inward of and below the
++// left stick, as on a Pro Controller. What stays here is how touches are taken:
+ static const CGFloat
+- K_ZL_X = 44, K_ZL_Y = 40, K_ZL_R = 26, // ZL / ZR: centre from the outer edge and the top, radius
+- K_L_X = 44, K_L_Y = 108, K_L_R = 24, // L / R: same column as ZL / ZR
+- K_DROP_TIP = 1.45, // teardrop tip distance from the centre (times the radius)
+- K_CORNER_IN = 0.5, // how far the bottom-corner controls reach into the side safe-area inset (in landscape there is only the rounded screen corner there; the Dynamic Island is mid-edge)
+- K_FACE_X = 81, K_PAD_Y = 82, // diamond centre from the outer edge; diamond and stick centre from the bottom (B's lower edge about 5 above it)
+- K_FACE_GAP = 50, K_FACE_R = 27, // diamond: key distance from the centre, key radius
+- K_PILL_DX = 52, K_PILL_Y = 22, K_PILL_W = 50, K_PILL_H = 28, // − / +: centre from the screen's midline (the middle goes to the layout button) and from the top, size
+- K_SWAP_R = 15, // layout button: top centre, level with − / +
+- K_STICK_X = 81, K_STICK_R = 77, K_KNOB_R = 30, // left stick: mirrors the diamond, the ring as large as the whole diamond (key distance + key radius), the knob a little larger than a diamond key
+ K_STICK_GRAB = 1.4, // how near the ring centre a touch-down grabs a stick (times the ring radius)
+ K_FULL = 0.8, // full tilt at this fraction of the ring radius (the knob still travels to the ring edge)
+ K_DEAD = 0.1, // stick dead zone (times the ring radius)
+- // controls only the 3D layout has
+- K_LOW_X = 231, K_LOW_Y = 64, // D-pad centre from the outer edge and the bottom (inward of and below the left stick, as on a Pro Controller)
+- K_DPAD_R = 48, K_ARM_W = 17, K_ARM_IN = 11, // D-pad: arm outer edge from the centre, half-width, tip from the centre
+ K_DPAD_GRAB = 1.3, // D-pad grab radius (times the outer edge)
+ K_RSTICK_R = 60, // right stick (not drawn): radius around its centre, the touch-down point
+- K_RSWIPE = 1.0 / 3, // right stick area: this fraction of the screen width from the right edge (not in u)
+- K_CLICK_X = 102, K_CLICK_R = 17; // L3 / R3: centre from the outer edge (level with ZL / ZR), radius
++ K_RSWIPE = 1.0 / 3; // right stick area: this fraction of the screen width from the right edge (not in u)
+ static const int k_btns[] = {B_ZL, B_L, B_ZR, B_R, B_MINUS, B_PLUS, B_LS, B_RS}; // keys locked at touch-down (L3 / R3 only in the 3D layout)
+ static NSString *const k_layout_key = @"DmPadLayout"; // NSUserDefaults: 0 = 2D, 1 = 3D, 2 = Tap (DM_GAME_PAD_LAYOUT's numbers)
+ int dm_opmode(void); // hle_oe.c: 0 = handheld
+@@ -153,7 +147,9 @@ @implementation DmGamepad {
+ CALayer *_controls; // holds every control layer: hidden while a physical controller is connected (the view itself stays, for game touches)
+ BOOL _padOff; // a physical controller is connected: every touch goes to the game
+ DmLayout _layout;
+- UITouch *_game[16]; // touches on the game's touch screen by finger slot
++ DmPadOptions _opts; // the player's options from the environment (DM_TOUCH_SIZE)
++ DmPadGeo _geo; CGRect _geoBounds; UIEdgeInsets _geoInsets; BOOL _geoFull, _geoValid; // where the controls are, and what it was computed for
++ UITouch *_game[16]; // touches on the game's touch screen by finger slot
+ }
+
+ static UIColor *k_idle, *k_on, *k_line, *k_ringFill, *k_text, *k_textOn;
+@@ -173,6 +169,9 @@ @implementation DmGamepad {
+ - (instancetype)initWithFrame:(CGRect)f {
+ if ((self = [super initWithFrame:f])) {
+ _layout = dm_layout(self.bounds, UIEdgeInsetsZero, NO, CGRectNull);
++ const char *size = getenv("DM_TOUCH_SIZE");
++ _opts.scale = dm_pad_size_scale(size);
++ if (size && *size) LOG("front end: touch controls size %s (DM_TOUCH_SIZE)", _opts.scale < 1 ? "small" : _opts.scale > 1 ? "large" : "medium");
+ [self registerForTraitChanges:@[UITraitDisplayScale.class] withHandler:^(id<UITraitEnvironment> environment, UITraitCollection *previous) {
+ [(UIView *)environment setNeedsLayout];
+ }];
+@@ -234,68 +233,43 @@ - (void)nextLayout { // 2D → 3D (→ Tap in handheld mode) → 2D: release e
+ - (void)arrange:(DmLayout)layout {
+ if (CGRectEqualToRect(_layout.controls, layout.controls) && CGRectEqualToRect(_layout.game, layout.game) && _layout.separated == layout.separated && _layout.topSafeOnly == layout.topSafeOnly) return;
+ [self reset]; // A finger held across a pose change must not leave a key or game touch stuck.
+- _layout = layout;
++ _layout = layout; _geoValid = NO;
+ [self setNeedsLayout];
+ }
+-- (CGRect)safe { return CGRectIntersection(_layout.controls, UIEdgeInsetsInsetRect(self.bounds, dm_game_safe_insets(self.bounds, self.safeAreaInsets, _layout.topSafeOnly))); }
+-- (CGRect)corner { if (_layout.separated) return self.safe; UIEdgeInsets i = self.safeAreaInsets; i.left *= K_CORNER_IN; i.right *= K_CORNER_IN; return UIEdgeInsetsInsetRect(self.bounds, i); } // the rectangle the bottom-corner controls use
+-- (CGFloat)u { if (_layout.separated) { CGRect s = self.safe; return MIN(MIN(s.size.height / 430.0, s.size.width / 700.0), 1.3); } return MIN(self.bounds.size.height / 430.0, 1.3); } // 1 = 430 points tall (iPhone Pro Max landscape)
+-static CGPoint side(CGRect s, int right, CGFloat dx, CGFloat y) { return CGPointMake(right ? CGRectGetMaxX(s) - dx : CGRectGetMinX(s) + dx, y); }
+-- (BOOL)portraitControls { return _layout.separated && self.bounds.size.height > self.bounds.size.width; }
+-- (CGPoint)portraitThumbCenter:(BOOL)right {
+- CGRect s = self.safe;
+- return CGPointMake(CGRectGetMinX(s) + s.size.width * (right ? 0.72 : 0.28),
+- CGRectGetMinY(s) + s.size.height * 0.58);
+-}
+-- (CGPoint)faceCenter { CGRect s = self.corner; CGFloat u = self.u;
+- if (self.portraitControls) return [self portraitThumbCenter:YES];
+- CGFloat edge = _layout.separated ? self.faceGap + self.faceHitR : 0;
+- return side(s, 1, MAX(K_FACE_X * u, edge), CGRectGetMaxY(s) - MAX(K_PAD_Y * u, edge)); }
+-- (CGFloat)faceGap { return K_FACE_GAP * self.u; }
+-- (CGFloat)faceR { return K_FACE_R * self.u; }
+-- (CGFloat)faceHitR { return MAX(self.faceR * 1.25, _layout.separated ? 22 : 0); }
+-- (CGPoint)facePos:(int)b { CGPoint c = self.faceCenter; CGFloat g = self.faceGap;
+- switch (b) { case B_A: return CGPointMake(c.x + g, c.y); case B_B: return CGPointMake(c.x, c.y + g); case B_X: return CGPointMake(c.x, c.y - g); default: return CGPointMake(c.x - g, c.y); } }
+-- (CGPoint)stickHome {
+- if (self.portraitControls) {
+- CGPoint p = [self portraitThumbCenter:NO];
+- if (_full) p.x -= self.dpadR + 6 * self.u;
+- return p;
++- (const DmPadGeo *)geo { // DmPadGeometry.h, computed again whenever the bounds, the safe area, the split or the layout changed
++ CGRect b = self.bounds; UIEdgeInsets i = self.safeAreaInsets;
++ if (!_geoValid || !CGRectEqualToRect(b, _geoBounds) || !UIEdgeInsetsEqualToEdgeInsets(i, _geoInsets) || _geoFull != _full) {
++ _geo = dm_pad_geo(b, i, _layout, _full, _opts);
++ _geoBounds = b; _geoInsets = i; _geoFull = _full; _geoValid = YES;
+ }
+- CGRect s = self.corner;
+- return side(s, 0, K_STICK_X * self.u, CGRectGetMaxY(s) - K_PAD_Y * self.u);
+-}
++ return &_geo;
++}
++- (CGFloat)u { return self.geo->u; } // 1 = 430 points tall (iPhone Pro Max landscape), times the player's size
++- (CGPoint)faceCenter { return self.geo->face; }
++- (CGFloat)faceGap { return self.geo->faceGap; }
++- (CGFloat)faceR { return self.geo->faceR; }
++- (CGFloat)faceHitR { return self.geo->faceHitR; }
++- (CGPoint)facePos:(int)b { return dm_pad_face_key(self.geo, b - B_A); } // B_A..B_Y are A, B, X, Y: the geometry's order
++- (CGPoint)stickHome { return self.geo->stick; }
+ - (CGPoint)stickCentre:(int)i { return i ? _rCentre : self.stickHome; }
+-- (CGFloat)stickR:(int)i { return (i ? K_RSTICK_R : K_STICK_R) * self.u; }
++- (CGFloat)stickR:(int)i { return i ? K_RSTICK_R * self.u : self.geo->stickR; }
+ - (BOOL)inRightArea:(CGPoint)p { CGRect b = _layout.controls; return p.x >= CGRectGetMaxX(b) - b.size.width * K_RSWIPE; }
+-- (CGPoint)dpadCenter {
+- if (self.portraitControls) {
+- CGPoint p = [self portraitThumbCenter:NO];
+- p.x += [self stickR:0] + 6 * self.u;
+- return p;
+- }
+- CGRect s = self.corner; CGFloat u = self.u;
+- return side(s, 0, K_LOW_X * u, CGRectGetMaxY(s) - K_LOW_Y * u);
+-}
+-- (CGFloat)dpadR { return K_DPAD_R * self.u; }
++- (CGPoint)dpadCenter { return self.geo->dpad; }
++- (CGFloat)dpadR { return self.geo->dpadR; }
+ static BOOL isDrop(int b) { return b == B_ZL || b == B_L || b == B_ZR || b == B_R; }
+ static BOOL isClick(int b) { return b == B_LS || b == B_RS; }
+-- (CGPoint)dropCenter:(int)b { CGRect s = self.safe; CGFloat u = self.u; BOOL z = b == B_ZL || b == B_ZR;
+- CGFloat edge = _layout.separated ? 22 : 0;
+- return side(s, b == B_ZR || b == B_R, MAX((z ? K_ZL_X : K_L_X) * u, edge), CGRectGetMinY(s) + MAX((z ? K_ZL_Y : K_L_Y) * u, edge)); }
+-- (CGFloat)dropR:(int)b { return (b == B_ZL || b == B_ZR ? K_ZL_R : K_L_R) * self.u; }
+-- (CGPoint)clickCenter:(int)b { CGRect s = self.safe; CGFloat u = self.u; CGFloat edge = _layout.separated ? 22 : 0;
+- return side(s, b == B_RS, MAX(K_CLICK_X * u, edge), CGRectGetMinY(s) + MAX(K_ZL_Y * u, edge)); }
+-- (CGRect)pillRect:(int)b { CGRect s = self.safe; CGFloat u = self.u, w = K_PILL_W * u, h = K_PILL_H * u;
+- CGPoint c = CGPointMake(CGRectGetMidX(s) + (b == B_PLUS ? K_PILL_DX : -K_PILL_DX) * u, CGRectGetMinY(s) + MAX(K_PILL_Y * u, _layout.separated ? 14 : 0)); // − on the left, + on the right
+- return CGRectMake(c.x - w / 2, c.y - h / 2, w, h); }
+-- (CGPoint)swapCenter { CGRect s = self.safe; return CGPointMake(CGRectGetMidX(s), CGRectGetMinY(s) + MAX(K_PILL_Y * self.u, _layout.separated ? 14 : 0)); }
++static int dropIndex(int b) { return b == B_ZL ? DM_PAD_ZL : b == B_L ? DM_PAD_L : b == B_ZR ? DM_PAD_ZR : DM_PAD_R; }
++- (CGPoint)dropCenter:(int)b { return self.geo->drop[dropIndex(b)]; }
++- (CGFloat)dropR:(int)b { return self.geo->dropR[dropIndex(b)]; }
++- (CGPoint)clickCenter:(int)b { return self.geo->click[b == B_RS]; }
++- (CGRect)pillRect:(int)b { return self.geo->pill[b == B_PLUS ? DM_PAD_PLUS : DM_PAD_MINUS]; }
++- (CGPoint)swapCenter { return self.geo->swap; }
+
+ static CGFloat dist(CGPoint a, CGPoint b) { return hypot(a.x - b.x, a.y - b.y); }
+ - (CGFloat)buttonDistance:(int)b point:(CGPoint)p {
+ CGFloat u = self.u;
+ if (isDrop(b)) return dist(p, [self dropCenter:b]) / MAX([self dropR:b] * 1.35, _layout.separated ? 22 : 0);
+- if (isClick(b)) return dist(p, [self clickCenter:b]) / MAX(K_CLICK_R * u * 1.35, _layout.separated ? 22 : 0);
++ if (isClick(b)) return dist(p, [self clickCenter:b]) / MAX(self.geo->clickR * 1.35, _layout.separated ? 22 : 0);
+ CGRect r = CGRectInset([self pillRect:b], -8 * u, -8 * u);
+ if (!_layout.separated && !CGRectContainsPoint(r, p)) return CGFLOAT_MAX;
+ CGFloat w = MAX(r.size.width, _layout.separated ? 28 : 0), h = MAX(r.size.height, _layout.separated ? 28 : 0);
+@@ -313,7 +287,7 @@ - (int)btnAt:(CGPoint)p { // Locked at touch-down; nearest normalized target w
+ }
+ return best;
+ }
+-- (CGFloat)swapDistance:(CGPoint)p { return dist(p, self.swapCenter) / MAX(K_SWAP_R * self.u * 1.6, _layout.separated ? 14 : 0); }
++- (CGFloat)swapDistance:(CGPoint)p { return dist(p, self.swapCenter) / MAX(self.geo->swapR * 1.6, _layout.separated ? 14 : 0); }
+ - (BOOL)inSwap:(CGPoint)p { return [self swapDistance:p] <= 1; }
+ - (uint64_t)faceAt:(CGPoint)p { // keys under the finger in the diamond (between two keys both count)
+ uint64_t m = 0; CGFloat r = self.faceHitR;
+@@ -443,7 +417,7 @@ - (void)reset { [_owner removeAllObjects]; [_faceOf removeAllObjects]; _stick[0]
+ // ---- Shapes ----
+ static CGPathRef circlePath(CGPoint c, CGFloat r) { return CGPathCreateWithEllipseInRect(CGRectMake(c.x - r, c.y - r, 2 * r, 2 * r), NULL); }
+ static CGPathRef dropPath(CGPoint c, CGFloat r, CGFloat tipAngle, CGFloat round) { // teardrop: circle + two tangents meeting at the tip, tip slightly rounded
+- CGFloat d = r * K_DROP_TIP, th = acos(r / d);
++ CGFloat d = r * DM_PAD_DROP_TIP, th = acos(r / d);
+ CGPoint tip = CGPointMake(c.x + d * cos(tipAngle), c.y + d * sin(tipAngle));
+ CGPoint p1 = CGPointMake(c.x + r * cos(tipAngle + th), c.y + r * sin(tipAngle + th)), p2 = CGPointMake(c.x + r * cos(tipAngle - th), c.y + r * sin(tipAngle - th));
+ CGMutablePathRef p = CGPathCreateMutable();
+@@ -487,17 +461,17 @@ - (void)layoutSubviews {
+ BOOL right = b == B_ZR || b == B_R; CGPoint c = [self dropCenter:b];
+ setPath(_key[b], dropPath(c, [self dropR:b], right ? -M_PI_4 : -3 * M_PI_4, 4 * u)); placeLabel(_label[b], c, 16 * u); // tip toward the near top corner
+ } else if (isClick(b)) {
+- CGPoint c = [self clickCenter:b]; setPath(_key[b], circlePath(c, K_CLICK_R * u)); placeLabel(_label[b], c, 13 * u);
++ CGPoint c = [self clickCenter:b]; setPath(_key[b], circlePath(c, self.geo->clickR)); placeLabel(_label[b], c, 13 * u);
+ } else {
+ CGRect rc = [self pillRect:b];
+ setPath(_key[b], CGPathCreateWithRoundedRect(rc, rc.size.height / 2, rc.size.height / 2, NULL)); placeLabel(_label[b], CGPointMake(CGRectGetMidX(rc), CGRectGetMidY(rc)), 18 * u);
+ }
+ }
+ static const struct { int b; CGFloat angle; } arms[] = {{B_UP, 0}, {B_RIGHT, M_PI_2}, {B_DOWN, M_PI}, {B_LEFT, -M_PI_2}}; // the same arm in 90° steps
+- for (int i = 0; i < 4; i++) setPath(_key[arms[i].b], armPath(self.dpadCenter, self.dpadR, K_ARM_W * u, K_ARM_IN * u, arms[i].angle, 6 * u));
+- CGPoint sc = self.swapCenter; setPath(_swap, circlePath(sc, K_SWAP_R * u)); placeLabel(_swapLabel, sc, 12 * u);
++ for (int i = 0; i < 4; i++) setPath(_key[arms[i].b], armPath(self.dpadCenter, self.dpadR, self.geo->armW, self.geo->armIn, arms[i].angle, 6 * u));
++ CGPoint sc = self.swapCenter; setPath(_swap, circlePath(sc, self.geo->swapR)); placeLabel(_swapLabel, sc, 12 * u);
+ setPath(_ring, circlePath(self.stickHome, [self stickR:0]));
+- CGFloat k = K_KNOB_R * u; _knob.bounds = CGRectMake(0, 0, 2 * k, 2 * k);
++ CGFloat k = self.geo->knobR; _knob.bounds = CGRectMake(0, 0, 2 * k, 2 * k);
+ setPath(_knob, circlePath(CGPointMake(k, k), k));
+ CAShapeLayer *in = (CAShapeLayer *)_knob.sublayers.firstObject; CGPathRef ip = circlePath(CGPointMake(k, k), k * 0.62);
+ in.frame = _knob.bounds; in.path = ip; CGPathRelease(ip);
+diff --git a/dm/ios/Sources/DmPadGeometry.h b/dm/ios/Sources/DmPadGeometry.h
+new file mode 100644
+index 0000000..a03f3ec
+--- /dev/null
++++ b/dm/ios/Sources/DmPadGeometry.h
+@@ -0,0 +1,176 @@
++// Where the virtual gamepad's controls are drawn (DmGamepad.m), as pure functions of the view's bounds, its safe-area insets, the DmLayout split
++// and the player's options, so the placement is tested without a device (dm/ios/tests/test_pad_geometry.py).
++// Every length is in u (1 = 430 points tall, iPhone Pro Max landscape), x measured from the outer edge, the right half mirrors.
++// The options come from the environment (the shell's settings, Documents/env.txt): DM_TOUCH_SIZE = small | medium | large scales u.
++// An option never moves a control off the screen or onto another one: a size that does not fit falls back step by step to the size that does
++// (dm_pad_geo_fits: every drawn shape inside its rectangle, no two drawn shapes overlap).
++#pragma once
++#import "DmLayout.h"
++#include <math.h>
++#include <string.h>
++
++NS_ASSUME_NONNULL_BEGIN
++
++enum { DM_PAD_ZL = 0, DM_PAD_L, DM_PAD_ZR, DM_PAD_R }; // the order of DmPadGeo.drop
++enum { DM_PAD_MINUS = 0, DM_PAD_PLUS }; // the order of DmPadGeo.pill
++
++typedef struct {
++ CGFloat scale; // DM_TOUCH_SIZE: 0.8 small, 1 medium (the built-in size), 1.2 large
++} DmPadOptions;
++
++typedef struct {
++ CGFloat u; // the unit every size below is a multiple of
++ CGRect safe, corner; // the top row lives in safe, the bottom-corner controls in corner (reaching halfway into the side insets)
++ BOOL portrait; // separated and taller than wide: both thumbs' clusters sit in the controls region's middle band
++ CGPoint face; CGFloat faceGap, faceR, faceHitR; // the A B X Y diamond: centre, key distance from it, key radius, hit radius
++ CGPoint stick; CGFloat stickR, knobR; // the left stick: ring centre and radius, knob radius
++ CGPoint dpad; CGFloat dpadR, armW, armIn; // the D-pad (3D only): centre, arm outer edge, arm half-width, arm tip from the centre
++ CGPoint drop[4]; CGFloat dropR[4]; // ZL, L, ZR, R: teardrop centres and radii
++ CGPoint click[2]; CGFloat clickR; // L3, R3 (3D only)
++ CGRect pill[2]; // −, +
++ CGPoint swap; CGFloat swapR; // the layout button
++} DmPadGeo;
++
++// Layout constants (in u), the values DmGamepad.m has always used.
++static const CGFloat
++ DM_PAD_ZL_X = 44, DM_PAD_ZL_Y = 40, DM_PAD_ZL_R = 26, // ZL / ZR: centre from the outer edge and the top, radius
++ DM_PAD_L_X = 44, DM_PAD_L_Y = 108, DM_PAD_L_R = 24, // L / R: same column as ZL / ZR
++ DM_PAD_DROP_TIP = 1.45, // teardrop tip distance from the centre (times the radius)
++ DM_PAD_CORNER_IN = 0.5, // how far the bottom-corner controls reach into the side safe-area inset
++ DM_PAD_FACE_X = 81, DM_PAD_Y = 82, // diamond centre from the outer edge; diamond and stick centre from the bottom
++ DM_PAD_FACE_GAP = 50, DM_PAD_FACE_R = 27, // diamond: key distance from the centre, key radius
++ DM_PAD_PILL_DX = 52, DM_PAD_PILL_Y = 22, DM_PAD_PILL_W = 50, DM_PAD_PILL_H = 28, // − / +: centre from the midline and from the top, size
++ DM_PAD_SWAP_R = 15, // layout button: top centre, level with − / +
++ DM_PAD_STICK_X = 81, DM_PAD_STICK_R = 77, DM_PAD_KNOB_R = 30, // left stick: mirrors the diamond
++ DM_PAD_LOW_X = 231, DM_PAD_LOW_Y = 64, // D-pad centre from the outer edge and the bottom
++ DM_PAD_DPAD_R = 48, DM_PAD_ARM_W = 17, DM_PAD_ARM_IN = 11,
++ DM_PAD_CLICK_X = 102, DM_PAD_CLICK_R = 17; // L3 / R3: centre from the outer edge (level with ZL / ZR), radius
++
++// DM_TOUCH_SIZE: small, medium (also unset, empty or unknown) or large.
++static inline CGFloat dm_pad_size_scale(const char *_Nullable v) {
++ if (v && !strcmp(v, "small")) return 0.8;
++ if (v && !strcmp(v, "large")) return 1.2;
++ return 1;
++}
++
++static inline CGPoint dm_pad_side(CGRect s, int right, CGFloat dx, CGFloat y) { return CGPointMake(right ? CGRectGetMaxX(s) - dx : CGRectGetMinX(s) + dx, y); }
++
++// The geometry at one unit, with no fitting.
++static inline DmPadGeo dm_pad_geo_at(CGRect bounds, UIEdgeInsets insets, DmLayout layout, BOOL full, CGFloat u) {
++ DmPadGeo g; memset(&g, 0, sizeof g);
++ BOOL sep = layout.separated;
++ g.u = u;
++ g.safe = CGRectIntersection(layout.controls, UIEdgeInsetsInsetRect(bounds, dm_game_safe_insets(bounds, insets, layout.topSafeOnly)));
++ UIEdgeInsets ci = insets; ci.left *= DM_PAD_CORNER_IN; ci.right *= DM_PAD_CORNER_IN;
++ g.corner = sep ? g.safe : UIEdgeInsetsInsetRect(bounds, ci);
++ g.portrait = sep && bounds.size.height > bounds.size.width;
++ CGRect s = g.safe, c = g.corner;
++ CGPoint thumbL = CGPointMake(CGRectGetMinX(s) + s.size.width * 0.28, CGRectGetMinY(s) + s.size.height * 0.58);
++ CGPoint thumbR = CGPointMake(CGRectGetMinX(s) + s.size.width * 0.72, thumbL.y);
++ g.faceGap = DM_PAD_FACE_GAP * u; g.faceR = DM_PAD_FACE_R * u; g.faceHitR = MAX(g.faceR * 1.25, sep ? 22 : 0);
++ g.stickR = DM_PAD_STICK_R * u; g.knobR = DM_PAD_KNOB_R * u;
++ g.dpadR = DM_PAD_DPAD_R * u; g.armW = DM_PAD_ARM_W * u; g.armIn = DM_PAD_ARM_IN * u;
++ if (g.portrait) {
++ g.face = thumbR;
++ g.stick = thumbL; if (full) g.stick.x -= g.dpadR + 6 * u;
++ g.dpad = CGPointMake(thumbL.x + g.stickR + 6 * u, thumbL.y);
++ } else {
++ CGFloat edge = sep ? g.faceGap + g.faceHitR : 0;
++ g.face = dm_pad_side(c, 1, MAX(DM_PAD_FACE_X * u, edge), CGRectGetMaxY(c) - MAX(DM_PAD_Y * u, edge));
++ g.stick = dm_pad_side(c, 0, DM_PAD_STICK_X * u, CGRectGetMaxY(c) - DM_PAD_Y * u);
++ g.dpad = dm_pad_side(c, 0, DM_PAD_LOW_X * u, CGRectGetMaxY(c) - DM_PAD_LOW_Y * u);
++ }
++ CGFloat edge = sep ? 22 : 0;
++ for (int i = 0; i < 4; i++) {
++ BOOL z = i == DM_PAD_ZL || i == DM_PAD_ZR;
++ g.drop[i] = dm_pad_side(s, i >= DM_PAD_ZR, MAX((z ? DM_PAD_ZL_X : DM_PAD_L_X) * u, edge), CGRectGetMinY(s) + MAX((z ? DM_PAD_ZL_Y : DM_PAD_L_Y) * u, edge));
++ g.dropR[i] = (z ? DM_PAD_ZL_R : DM_PAD_L_R) * u;
++ }
++ for (int i = 0; i < 2; i++) g.click[i] = dm_pad_side(s, i, MAX(DM_PAD_CLICK_X * u, edge), CGRectGetMinY(s) + MAX(DM_PAD_ZL_Y * u, edge));
++ g.clickR = DM_PAD_CLICK_R * u;
++ CGFloat top = CGRectGetMinY(s) + MAX(DM_PAD_PILL_Y * u, sep ? 14 : 0), w = DM_PAD_PILL_W * u, h = DM_PAD_PILL_H * u;
++ for (int i = 0; i < 2; i++) {
++ CGFloat x = CGRectGetMidX(s) + (i == DM_PAD_PLUS ? DM_PAD_PILL_DX : -DM_PAD_PILL_DX) * u; // − on the left, + on the right
++ g.pill[i] = CGRectMake(x - w / 2, top - h / 2, w, h);
++ }
++ g.swap = CGPointMake(CGRectGetMidX(s), top); g.swapR = DM_PAD_SWAP_R * u;
++ return g;
++}
++
++// The face key positions: A right, B bottom, X top, Y left (i = 0..3 in that order).
++static inline CGPoint dm_pad_face_key(const DmPadGeo *g, int i) {
++ CGPoint c = g->face; CGFloat d = g->faceGap;
++ switch (i) { case 0: return CGPointMake(c.x + d, c.y); case 1: return CGPointMake(c.x, c.y + d); case 2: return CGPointMake(c.x, c.y - d); default: return CGPointMake(c.x - d, c.y); }
++}
++static inline CGPoint dm_pad_drop_tip(const DmPadGeo *g, int i) {
++ CGFloat a = i >= DM_PAD_ZR ? -M_PI_4 : -3 * M_PI_4, d = g->dropR[i] * DM_PAD_DROP_TIP; // tip toward the near top corner
++ return CGPointMake(g->drop[i].x + d * cos(a), g->drop[i].y + d * sin(a));
++}
++
++// The drawn shapes as circles and rectangles, for the fit test and for tests: each with the rectangle it must stay inside.
++typedef struct { CGPoint c; CGFloat r; CGRect rect; BOOL isRect; CGRect within; int id; } DmPadShape;
++enum { DM_PAD_SHAPE_FACE = 0, DM_PAD_SHAPE_STICK = 4, DM_PAD_SHAPE_DPAD, DM_PAD_SHAPE_DROP, DM_PAD_SHAPE_TIP = 10, DM_PAD_SHAPE_CLICK = 14, DM_PAD_SHAPE_PILL = 16, DM_PAD_SHAPE_SWAP = 18 };
++static inline int dm_pad_shapes(const DmPadGeo *g, BOOL full, DmPadShape out[_Nonnull 20]) {
++ int n = 0;
++ for (int i = 0; i < 4; i++) out[n++] = (DmPadShape){dm_pad_face_key(g, i), g->faceR, CGRectNull, NO, g->corner, DM_PAD_SHAPE_FACE + i};
++ out[n++] = (DmPadShape){g->stick, g->stickR, CGRectNull, NO, g->corner, DM_PAD_SHAPE_STICK};
++ if (full) out[n++] = (DmPadShape){g->dpad, hypot(g->dpadR, g->armW), CGRectNull, NO, g->corner, DM_PAD_SHAPE_DPAD};
++ for (int i = 0; i < 4; i++) {
++ out[n++] = (DmPadShape){g->drop[i], g->dropR[i], CGRectNull, NO, g->safe, DM_PAD_SHAPE_DROP + i};
++ out[n++] = (DmPadShape){dm_pad_drop_tip(g, i), 0, CGRectNull, NO, g->safe, DM_PAD_SHAPE_TIP + i};
++ }
++ if (full) for (int i = 0; i < 2; i++) out[n++] = (DmPadShape){g->click[i], g->clickR, CGRectNull, NO, g->safe, DM_PAD_SHAPE_CLICK + i};
++ for (int i = 0; i < 2; i++) out[n++] = (DmPadShape){CGPointZero, 0, g->pill[i], YES, g->safe, DM_PAD_SHAPE_PILL + i};
++ out[n++] = (DmPadShape){g->swap, g->swapR, CGRectNull, NO, g->safe, DM_PAD_SHAPE_SWAP};
++ return n;
++}
++static inline BOOL dm_pad_shape_inside(const DmPadShape *s) {
++ const CGFloat e = 0.5;
++ CGRect b = s->isRect ? s->rect : CGRectMake(s->c.x - s->r, s->c.y - s->r, 2 * s->r, 2 * s->r);
++ return CGRectGetMinX(b) >= CGRectGetMinX(s->within) - e && CGRectGetMaxX(b) <= CGRectGetMaxX(s->within) + e
++ && CGRectGetMinY(b) >= CGRectGetMinY(s->within) - e && CGRectGetMaxY(b) <= CGRectGetMaxY(s->within) + e;
++}
++static inline BOOL dm_pad_shapes_overlap(const DmPadShape *a, const DmPadShape *b) {
++ const CGFloat e = 0.5;
++ if (a->isRect && b->isRect) return CGRectIntersectsRect(CGRectInset(a->rect, e, e), CGRectInset(b->rect, e, e));
++ if (a->isRect || b->isRect) {
++ const DmPadShape *r = a->isRect ? a : b, *c = a->isRect ? b : a;
++ CGFloat nx = fmax(CGRectGetMinX(r->rect), fmin(c->c.x, CGRectGetMaxX(r->rect))), ny = fmax(CGRectGetMinY(r->rect), fmin(c->c.y, CGRectGetMaxY(r->rect)));
++ return hypot(c->c.x - nx, c->c.y - ny) < c->r - e;
++ }
++ return hypot(a->c.x - b->c.x, a->c.y - b->c.y) < a->r + b->r - e;
++}
++// A teardrop's tip belongs to its own circle; the tips are points, so they only count when they land inside another shape.
++static inline BOOL dm_pad_same_control(const DmPadShape *a, const DmPadShape *b) {
++ return (a->id >= DM_PAD_SHAPE_DROP && a->id < DM_PAD_SHAPE_TIP && b->id == a->id + 4) || (b->id >= DM_PAD_SHAPE_DROP && b->id < DM_PAD_SHAPE_TIP && a->id == b->id + 4);
++}
++// Every drawn shape inside its rectangle and no two overlapping.
++static inline BOOL dm_pad_geo_fits(const DmPadGeo *g, BOOL full) {
++ DmPadShape s[20]; int n = dm_pad_shapes(g, full, s);
++ for (int i = 0; i < n; i++) {
++ if (!dm_pad_shape_inside(&s[i])) return NO;
++ for (int j = i + 1; j < n; j++) if (!dm_pad_same_control(&s[i], &s[j]) && dm_pad_shapes_overlap(&s[i], &s[j])) return NO;
++ }
++ return YES;
++}
++
++// The unit of the built-in size: the screen height (inside the controls region when it is split off), up to 1.3.
++static inline CGFloat dm_pad_base_unit(CGRect bounds, UIEdgeInsets insets, DmLayout layout) {
++ if (!layout.separated) return MIN(bounds.size.height / 430.0, 1.3);
++ CGRect s = CGRectIntersection(layout.controls, UIEdgeInsetsInsetRect(bounds, dm_game_safe_insets(bounds, insets, layout.topSafeOnly)));
++ return MIN(MIN(s.size.height / 430.0, s.size.width / 700.0), 1.3);
++}
++
++// The geometry the gamepad draws: the player's size, moved toward the built-in size in steps until it fits (a large size that does
++// not fit gets smaller; a small one in a split region whose minimum edges crowd it gets larger). The built-in size is never changed.
++static inline DmPadGeo dm_pad_geo(CGRect bounds, UIEdgeInsets insets, DmLayout layout, BOOL full, DmPadOptions o) {
++ CGFloat u0 = dm_pad_base_unit(bounds, insets, layout), scale = o.scale > 0 ? o.scale : 1;
++ DmPadGeo g = dm_pad_geo_at(bounds, insets, layout, full, u0 * scale);
++ while (scale != 1 && !dm_pad_geo_fits(&g, full)) {
++ scale = scale > 1 ? fmax(1, scale - 0.05) : fmin(1, scale + 0.05);
++ g = dm_pad_geo_at(bounds, insets, layout, full, u0 * scale);
++ }
++ return g;
++}
++
++NS_ASSUME_NONNULL_END
+diff --git a/dm/ios/tests/pad_geometry/UIKit/UIKit.h b/dm/ios/tests/pad_geometry/UIKit/UIKit.h
+new file mode 100644
+index 0000000..6643574
+--- /dev/null
++++ b/dm/ios/tests/pad_geometry/UIKit/UIKit.h
+@@ -0,0 +1,11 @@
++// The few UIKit names DmLayout.h and DmPadGeometry.h use, so their pure geometry builds and runs on the Mac (test_pad_geometry.py).
++#pragma once
++#import <Foundation/Foundation.h>
++#import <CoreGraphics/CoreGraphics.h>
++typedef struct { CGFloat top, left, bottom, right; } UIEdgeInsets;
++static const UIEdgeInsets UIEdgeInsetsZero = {0, 0, 0, 0};
++static inline UIEdgeInsets UIEdgeInsetsMake(CGFloat t, CGFloat l, CGFloat b, CGFloat r) { return (UIEdgeInsets){t, l, b, r}; }
++static inline CGRect UIEdgeInsetsInsetRect(CGRect r, UIEdgeInsets i) {
++ r.origin.x += i.left; r.origin.y += i.top; r.size.width -= i.left + i.right; r.size.height -= i.top + i.bottom; return r;
++}
++@class UIView;
+diff --git a/dm/ios/tests/pad_geometry/pad_geometry_check.m b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+new file mode 100644
+index 0000000..f5b7112
+--- /dev/null
++++ b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+@@ -0,0 +1,94 @@
++// The virtual gamepad's placement (DmPadGeometry.h) on the screens it runs on, checked on the Mac: built and run by test_pad_geometry.py.
++// Every line that starts with FAIL is a broken rule; the exit status is the number of them (capped).
++#import <UIKit/UIKit.h>
++#import "DmPadGeometry.h"
++#include <stdio.h>
++
++typedef struct { const char *name; CGRect bounds; UIEdgeInsets insets; BOOL partiallyOpen; CGRect division; } Screen;
++
++static int g_fail;
++#define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); g_fail++; } while (0)
++#define NEAR(a, b) (fabs((a) - (b)) < 0.01)
++
++static const char *k_sizes[] = {"small", "medium", "large"};
++
++static DmPadGeo geo(const Screen *s, BOOL full, DmPadOptions o) {
++ return dm_pad_geo(s->bounds, s->insets, dm_layout(s->bounds, s->insets, s->partiallyOpen, s->division), full, o);
++}
++
++static void check_fits(const Screen *s, BOOL full, const char *size, DmPadOptions o) {
++ DmPadGeo g = geo(s, full, o);
++ DmPadShape sh[20]; int n = dm_pad_shapes(&g, full, sh);
++ for (int i = 0; i < n; i++) {
++ if (!dm_pad_shape_inside(&sh[i])) FAIL("%s %s %s: shape %d leaves its rectangle", s->name, full ? "3D" : "2D", size, sh[i].id);
++ for (int j = i + 1; j < n; j++)
++ if (!dm_pad_same_control(&sh[i], &sh[j]) && dm_pad_shapes_overlap(&sh[i], &sh[j]))
++ FAIL("%s %s %s: shapes %d and %d overlap", s->name, full ? "3D" : "2D", size, sh[i].id, sh[j].id);
++ }
++ // Nothing outside the screen at all, side insets included only up to the corner rule.
++ CGRect b = s->bounds;
++ for (int i = 0; i < n; i++) {
++ CGRect r = sh[i].isRect ? sh[i].rect : CGRectMake(sh[i].c.x - sh[i].r, sh[i].c.y - sh[i].r, 2 * sh[i].r, 2 * sh[i].r);
++ if (!CGRectContainsRect(CGRectInset(b, -0.5, -0.5), r)) FAIL("%s %s %s: shape %d is off the screen", s->name, full ? "3D" : "2D", size, sh[i].id);
++ }
++}
++
++int main(void) {
++ const Screen screens[] = {
++ // iPhone landscape (the phone's only orientation): the Dynamic Island or notch on one side, the home indicator at the bottom.
++ {"iPhone SE landscape", {{0, 0}, {667, 375}}, {0, 0, 0, 0}, NO, CGRectNull},
++ {"iPhone mini landscape", {{0, 0}, {812, 375}}, {0, 50, 21, 50}, NO, CGRectNull},
++ {"iPhone landscape", {{0, 0}, {852, 393}}, {0, 59, 21, 59}, NO, CGRectNull},
++ {"iPhone Pro Max landscape", {{0, 0}, {932, 430}}, {0, 59, 21, 59}, NO, CGRectNull},
++ {"iPhone 17 Pro Max landscape", {{0, 0}, {956, 440}}, {0, 62, 20, 62}, NO, CGRectNull},
++ // iPad: every orientation; portrait splits the picture from the controls.
++ {"iPad mini landscape", {{0, 0}, {1133, 744}}, {24, 0, 20, 0}, NO, CGRectNull},
++ {"iPad Pro 13 landscape", {{0, 0}, {1376, 1032}}, {24, 0, 20, 0}, NO, CGRectNull},
++ {"iPad mini portrait", {{0, 0}, {744, 1133}}, {24, 0, 20, 0}, NO, CGRectNull},
++ {"iPad Pro 13 portrait", {{0, 0}, {1032, 1376}}, {24, 0, 20, 0}, NO, CGRectNull},
++ // Split portrait windows the Duo harness uses, and iPhone Duo's inner display (patch 0061): portrait with the fold, tabletop half open.
++ {"portrait 390x844", {{0, 0}, {390, 844}}, {59, 0, 34, 0}, NO, CGRectNull},
++ {"portrait 320x568", {{0, 0}, {320, 568}}, {50, 0, 24, 0}, NO, CGRectNull},
++ {"Duo inner portrait", {{0, 0}, {669, 951}}, {62, 0, 34, 0}, NO, {{0, 455.5}, {669, 40}}},
++ {"Duo inner landscape", {{0, 0}, {951, 669}}, {0, 62, 20, 62}, NO, CGRectNull},
++ {"Duo tabletop", {{0, 0}, {951, 669}}, {0, 62, 20, 62}, YES, {{0, 314.5}, {951, 40}}},
++ };
++ int nscreens = (int)(sizeof screens / sizeof *screens);
++
++ // Option parsing.
++ if (dm_pad_size_scale(NULL) != 1 || dm_pad_size_scale("") != 1 || dm_pad_size_scale("medium") != 1 || dm_pad_size_scale("huge") != 1) FAIL("DM_TOUCH_SIZE: medium is the default");
++ if (!(dm_pad_size_scale("small") < 1) || !(dm_pad_size_scale("large") > 1)) FAIL("DM_TOUCH_SIZE: small is smaller and large larger");
++
++ // The built-in size is the layout DmGamepad.m has always drawn: iPhone Pro Max landscape, centres in points.
++ {
++ DmPadGeo g = geo(&screens[3], YES, (DmPadOptions){1});
++ if (!NEAR(g.u, 1)) FAIL("Pro Max: u %.3f, want 1", g.u);
++ if (!NEAR(g.face.x, 932 - 29.5 - 81) || !NEAR(g.face.y, 430 - 21 - 82)) FAIL("Pro Max: diamond at %.1f %.1f", g.face.x, g.face.y);
++ if (!NEAR(g.stick.x, 29.5 + 81) || !NEAR(g.stick.y, 430 - 21 - 82)) FAIL("Pro Max: stick at %.1f %.1f", g.stick.x, g.stick.y);
++ if (!NEAR(g.dpad.x, 29.5 + 231) || !NEAR(g.dpad.y, 430 - 21 - 64)) FAIL("Pro Max: D-pad at %.1f %.1f", g.dpad.x, g.dpad.y);
++ if (!NEAR(g.drop[DM_PAD_ZL].x, 59 + 44) || !NEAR(g.drop[DM_PAD_ZL].y, 40) || !NEAR(g.drop[DM_PAD_R].x, 932 - 59 - 44) || !NEAR(g.drop[DM_PAD_R].y, 108)) FAIL("Pro Max: shoulders moved");
++ if (!NEAR(g.click[1].x, 932 - 59 - 102) || !NEAR(g.pill[DM_PAD_PLUS].origin.x, 466 + 52 - 25) || !NEAR(g.swap.x, 466) || !NEAR(g.swap.y, 22)) FAIL("Pro Max: top row moved");
++ if (!NEAR(g.stickR, 77) || !NEAR(g.faceR, 27) || !NEAR(g.dpadR, 48)) FAIL("Pro Max: sizes changed");
++ // Portrait split (the Duo harness's numbers): the thumbs' centres at 28 % and 72 % of the controls, 58 % down.
++ DmLayout l = dm_layout(screens[9].bounds, screens[9].insets, NO, CGRectNull);
++ DmPadGeo p = dm_pad_geo(screens[9].bounds, screens[9].insets, l, NO, (DmPadOptions){1});
++ CGRect s = p.safe;
++ if (!p.portrait || !NEAR(p.face.x, CGRectGetMinX(s) + 0.72 * s.size.width) || !NEAR(p.stick.x, CGRectGetMinX(s) + 0.28 * s.size.width)) FAIL("portrait: thumbs moved");
++ }
++
++ // Every size on every screen, 2D and 3D: inside, and no overlap.
++ for (int i = 0; i < nscreens; i++)
++ for (int full = 0; full < 2; full++)
++ for (int z = 0; z < 3; z++) check_fits(&screens[i], full, k_sizes[z], (DmPadOptions){dm_pad_size_scale(k_sizes[z])});
++
++ // The sizes keep their order everywhere, and on a phone in landscape large really is larger and small smaller.
++ for (int i = 0; i < nscreens; i++)
++ for (int full = 0; full < 2; full++) {
++ CGFloat us = geo(&screens[i], full, (DmPadOptions){0.8}).u, um = geo(&screens[i], full, (DmPadOptions){1}).u, ul = geo(&screens[i], full, (DmPadOptions){1.2}).u;
++ if (!(us < um) || ul < um) FAIL("%s %s: sizes out of order (%.3f %.3f %.3f)", screens[i].name, full ? "3D" : "2D", us, um, ul);
++ if (i <= 4 && !(ul > um * 1.15)) FAIL("%s %s: large is not larger (%.3f vs %.3f)", screens[i].name, full ? "3D" : "2D", ul, um);
++ }
++
++ if (!g_fail) printf("ok: %d screens\n", nscreens);
++ return g_fail > 100 ? 100 : g_fail;
++}
+diff --git a/dm/ios/tests/test_pad_geometry.py b/dm/ios/tests/test_pad_geometry.py
+new file mode 100644
+index 0000000..9854dfd
+--- /dev/null
++++ b/dm/ios/tests/test_pad_geometry.py
+@@ -0,0 +1,34 @@
++"""The virtual gamepad's placement (dm/ios/Sources/DmPadGeometry.h), built and run on the Mac.
++
++pad_geometry/pad_geometry_check.m runs the same pure functions DmGamepad.m draws, hit-tests and grabs with, on the iPhone, iPad
++and iPhone Duo screens (portrait, landscape, tabletop), in the 2D and the 3D layout and every DM_TOUCH_SIZE: every control stays
++inside the safe area (the bottom corners reach halfway into the side insets, as always), no two drawn controls overlap, and the
++built-in size is the layout the gamepad has always drawn. pad_geometry/UIKit/UIKit.h stands in for the few UIKit names the
++headers use, so no simulator is needed.
++"""
++import shutil
++import subprocess
++import sys
++import tempfile
++import unittest
++from pathlib import Path
++
++IOS = Path(__file__).resolve().parents[1]
++HERE = Path(__file__).resolve().parent / "pad_geometry"
++
++
[email protected](sys.platform == "darwin" and shutil.which("clang"), "needs clang and the Mac's CoreGraphics")
++class PadGeometryTests(unittest.TestCase):
++ def test_every_screen_size_and_layout(self):
++ with tempfile.TemporaryDirectory() as tmp:
++ exe = Path(tmp) / "pad_geometry_check"
++ subprocess.run(["clang", "-x", "objective-c", "-fobjc-arc", "-Wall", "-Werror", "-Wno-unused-function",
++ "-I", str(HERE), "-I", str(IOS / "Sources"), str(HERE / "pad_geometry_check.m"),
++ "-framework", "Foundation", "-framework", "CoreGraphics", "-o", str(exe)], check=True)
++ run = subprocess.run([str(exe)], capture_output=True, text=True)
++ self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
++ self.assertIn("ok:", run.stdout)
++
++
++if __name__ == "__main__":
++ unittest.main()
diff --git a/patches/series b/patches/series
index b87ebcc4..bc262895 100644
--- a/patches/series
+++ b/patches/series
@@ -38,3 +38,4 @@
0060-ios-shell-declare-italian-in-cfbundlelocalizatio.patch
0061-ios-adapt-gameplay-to-duo-portrait-and-tabletop.patch
0062-ios-apply-controller-button-mappings-to-physical.patch
+0064-ios-touch-controls-size.patch