Skip to content

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

Merge origin/main (engine pin 641d1480021a): patch 0068 becomes 0070 on the menu button

v0lumehi122fab2e1084
diff --cc frontend-ios/README.md
index 1b3b59c4,30742801..b6058341
--- a/frontend-ios/README.md
+++ b/frontend-ios/README.md
@@@ -63,7 -63,7 +63,7 @@@ The caller returns changed values throu
| `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) the Size row (small, medium, large) and the Left stick row (corner, higher, inward, the preview marks the default place with a dashed ring). `ShellSettingsStore.touchSize` and `touchStick` store them, and `gameDefaults` passes them to the engine as `DM_TOUCH_SIZE` and `DM_TOUCH_STICK` (medium and the corner are the engine's own and are not passed). The engine keeps a choice off a screen too small for it. Every game also gets `DM_TOUCH_HINT`, the words the engine shows once per launch where the 3D layout's undrawn right stick is (`ios.host.touchHint`). The Arrangement row opens the layout editor (`TouchLayoutEditorView`, #414): the controls of one layout (2D or 3D, over the picture, and on an iPad or a foldable also below it) on this device's screen, dragged in steps of a quarter key, or chosen and moved with the arrows, a controller (A picks up, the D-pad moves, A or B puts down) or VoiceOver's actions, with Put back and Reset. A spot the engine would refuse (on another control, outside the area) turns red and sends the control back. `TouchLayout` holds the offsets and writes the engine's value, `ShellSettingsStore.touchLayout` stores it and `gameDefaults` passes it as `DM_TOUCH_LAYOUT` (nothing moved: not passed). `PadGeo` is a Swift copy of the engine's geometry, pinned to the engine's points by `TouchLayoutTests`, so the editor keeps only what the engine draws. `TouchControlsCaptureTests` renders the group and the editor on the Mac. | `dm/ios/Sources/DmPadGeometry.h` (patches 0065 to 0068) |
-| 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) the Size row (small, medium, large) and the Left stick row (corner, higher, inward, the preview marks the default place with a dashed ring). `ShellSettingsStore.touchSize` and `touchStick` store them, and `gameDefaults` passes them to the engine as `DM_TOUCH_SIZE` and `DM_TOUCH_STICK` (medium and the corner are the engine's own and are not passed). The engine keeps a choice off a screen too small for it. Every game also gets `DM_TOUCH_HINT`, the words the engine shows once per launch where the 3D layout's undrawn right stick is (`ios.host.touchHint`). Every game gets the quit prompt's words as `DM_HOME_QUIT_TEXT` (`ios.host.quit*`, five lines in `home_quit.h`'s order, as the Mac launcher passes them): the engine shows the prompt when the player picks Quit in the in-game menu (the gear) or holds a controller's Home button. `TouchControlsCaptureTests` renders the group on the Mac. | `dm/ios/Sources/DmPadGeometry.h`, `quit_prompt.h` (patches 0065 to 0068) |
++| 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) the Size row (small, medium, large) and the Left stick row (corner, higher, inward, the preview marks the default place with a dashed ring). `ShellSettingsStore.touchSize` and `touchStick` store them, and `gameDefaults` passes them to the engine as `DM_TOUCH_SIZE` and `DM_TOUCH_STICK` (medium and the corner are the engine's own and are not passed). The engine keeps a choice off a screen too small for it. Every game also gets `DM_TOUCH_HINT`, the words the engine shows once per launch where the 3D layout's undrawn right stick is (`ios.host.touchHint`). Every game gets the quit prompt's words as `DM_HOME_QUIT_TEXT` (`ios.host.quit*`, five lines in `home_quit.h`'s order, as the Mac launcher passes them): the engine shows the prompt when the player picks Quit in the in-game menu (the gear) or holds a controller's Home button. The Arrangement row opens the layout editor (`TouchLayoutEditorView`, #414): the controls of one layout (2D or 3D, over the picture, and on an iPad or a foldable also below it) on this device's screen, dragged in steps of a quarter key, or chosen and moved with the arrows, a controller (A picks up, the D-pad moves, A or B puts down) or VoiceOver's actions, with Put back and Reset. A spot the engine would refuse (on another control, outside the area) turns red and sends the control back. `TouchLayout` holds the offsets and writes the engine's value, `ShellSettingsStore.touchLayout` stores it and `gameDefaults` passes it as `DM_TOUCH_LAYOUT` (nothing moved: not passed). `PadGeo` is a Swift copy of the engine's geometry, pinned to the engine's points by `TouchLayoutTests`, so the editor keeps only what the engine draws. `TouchControlsCaptureTests` renders the group and the editor on the Mac. | `dm/ios/Sources/DmPadGeometry.h`, `quit_prompt.h` (patches 0065 to 0068, 0070) |
| 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 --cc frontend-ios/Sources/ShellUI/TouchPadGeometry.swift
index 8b53fc0e,00000000..6cc0206d
mode 100644,000000..100644
--- a/frontend-ios/Sources/ShellUI/TouchPadGeometry.swift
+++ b/frontend-ios/Sources/ShellUI/TouchPadGeometry.swift
@@@ -1,369 -1,0 +1,373 @@@
+import CoreGraphics
+import Foundation
+
+/// A screen the touch controls are laid out on, as the engine sees it: the game view's size in points, its safe-area insets and,
+/// on a foldable, the fold.
+public struct PadScreen: Equatable, Sendable {
+ public struct Insets: Equatable, Sendable {
+ public var top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat
+ public init(top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) {
+ self.top = top; self.left = left; self.bottom = bottom; self.right = right
+ }
+ }
+ public var size: CGSize
+ public var insets: Insets
+ public var partiallyOpen = false
+ /// The fold (a division region), nil for none.
+ public var division: CGRect?
+ /// iPhone Duo's inner display upright: the picture keeps only the top inset (front_ios.m's duoPortrait).
+ public var topSafeOnly = false
+
+ public init(size: CGSize, insets: Insets = Insets(), partiallyOpen: Bool = false, division: CGRect? = nil, topSafeOnly: Bool = false) {
+ self.size = size; self.insets = insets; self.partiallyOpen = partiallyOpen; self.division = division; self.topSafeOnly = topSafeOnly
+ }
+
+ /// iPhone Pro Max held sideways, the screen the settings preview has always shown.
+ public static let proMax = PadScreen(size: CGSize(width: 932, height: 430), insets: Insets(left: 59, bottom: 21, right: 59))
+ /// iPad Pro 13 inch upright: the controls below the picture.
+ public static let iPadUpright = PadScreen(size: CGSize(width: 1032, height: 1376), insets: Insets(top: 24, bottom: 20))
+ /// iPhone Duo's inner display upright, with the fold across the middle.
+ public static let duoUpright = PadScreen(size: CGSize(width: 669, height: 951), insets: Insets(top: 62, bottom: 34),
+ division: CGRect(x: 0, y: 455.5, width: 669, height: 40), topSafeOnly: true)
+}
+
+/// The touch controls' geometry, a Swift copy of the engine's `DmPadGeometry.h` (patches 0065 to 0067 and the arrangement of
+/// DM_TOUCH_LAYOUT): `dm_layout`, `dm_pad_geo_at`, the fit test, the size and stick fallbacks and `dm_pad_arrange`, line by line, so the
+/// layout editor shows and accepts exactly what the engine will draw. `TouchPadGeometryTests` pins it to points the engine's own
+/// test program computes. The engine stays the truth on the device.
+struct PadGeo: Equatable {
+ // dm_layout: the overlay layout, or the split one with the controls below the picture.
+ struct Region: Equatable { var game: CGRect, controls: CGRect, separated: Bool, topSafeOnly: Bool }
+
+ struct Shape: Equatable {
+ var c: CGPoint
+ var r: CGFloat
+ var rect: CGRect
+ var isRect: Bool
+ var within: CGRect
+ var id: Int
+ init(c: CGPoint = .zero, r: CGFloat = 0, rect: CGRect = .null, isRect: Bool = false, within: CGRect, id: Int) {
+ self.c = c; self.r = r; self.rect = rect; self.isRect = isRect; self.within = within; self.id = id
+ }
+ var box: CGRect { isRect ? rect : CGRect(x: c.x - r, y: c.y - r, width: 2 * r, height: 2 * r) }
+ }
+ // dm_pad_shapes ids.
+ static let shapeFace = 0, shapeStick = 4, shapeDpad = 5, shapeDrop = 6, shapeTip = 10, shapeClick = 14, shapePill = 16, shapeSwap = 18
+
+ var u: CGFloat = 0
+ var safe: CGRect = .null, corner: CGRect = .null
+ var portrait = false
+ var face: CGPoint = .zero, faceGap: CGFloat = 0, faceR: CGFloat = 0, faceHitR: CGFloat = 0
+ var stick: CGPoint = .zero, stickR: CGFloat = 0, knobR: CGFloat = 0
+ var dpad: CGPoint = .zero, dpadR: CGFloat = 0, armW: CGFloat = 0, armIn: CGFloat = 0
+ var drop: [CGPoint] = Array(repeating: .zero, count: 4), dropR: [CGFloat] = Array(repeating: 0, count: 4) // ZL, L, ZR, R
+ var click: [CGPoint] = [.zero, .zero], clickR: CGFloat = 0 // L3, R3
+ var pill: [CGRect] = [.zero, .zero] // −, +
+ var swap: CGPoint = .zero, swapR: CGFloat = 0
+ var faceKey: [CGPoint] = Array(repeating: .zero, count: 4) // A, B, X, Y
+ var faceSplit = false
+ var moved: Set<TouchLayout.Control> = [], dropped: Set<TouchLayout.Control> = []
+ var full = false
+ var region = Region(game: .zero, controls: .zero, separated: false, topSafeOnly: false)
+
+ /// A quarter of a face key's width: one step of the arrangement.
+ var step: CGFloat { faceR / 2 }
+
+ // MARK: DmLayout.h
+
+ static func aspectFit(_ b: CGRect) -> CGRect {
+ var w = b.width, h = b.height
+ if w <= 0 || h <= 0 { return .zero }
+ if w / h > 16.0 / 9.0 { w = h * 16.0 / 9.0 } else { h = w * 9.0 / 16.0 }
+ return CGRect(x: floor(b.midX - w / 2), y: floor(b.midY - h / 2), width: w, height: h)
+ }
+ static func horizontalDivision(_ b: CGRect, _ division: CGRect?) -> CGRect? {
+ guard let d = division, !d.isNull, d.width > d.height else { return nil }
+ let x = max(b.minX, d.minX), y = max(b.minY, d.minY), right = min(b.maxX, d.maxX), bottom = min(b.maxY, d.maxY)
+ if right - x < b.width * 0.9 || bottom < y || y <= b.minY || bottom >= b.maxY { return nil }
+ return CGRect(x: x, y: y, width: right - x, height: bottom - y)
+ }
+ static func inset(_ r: CGRect, _ i: PadScreen.Insets) -> CGRect {
+ CGRect(x: r.minX + i.left, y: r.minY + i.top, width: r.width - i.left - i.right, height: r.height - i.top - i.bottom)
+ }
+ static func gameSafeInsets(_ b: CGRect, _ i: PadScreen.Insets, _ duo: Bool) -> PadScreen.Insets {
+ duo && b.height > b.width ? PadScreen.Insets(top: i.top) : i
+ }
+ static func region(_ screen: PadScreen) -> Region {
+ let b = CGRect(origin: .zero, size: screen.size)
+ let insets = gameSafeInsets(b, screen.insets, screen.topSafeOnly)
+ var l = Region(game: aspectFit(b), controls: b, separated: false, topSafeOnly: screen.topSafeOnly)
+ if b.isEmpty { return l }
+ let division = horizontalDivision(b, screen.division)
+ let portrait = b.height > b.width
+ if !portrait && (!screen.partiallyOpen || division == nil) { return l }
+ let d = division ?? CGRect(x: b.minX, y: b.midY, width: b.width, height: 0)
+ let safe = inset(b, insets)
+ let upper = CGRect(x: safe.minX, y: safe.minY, width: safe.width, height: d.minY - safe.minY)
+ let lower = CGRect(x: b.minX, y: d.maxY, width: b.width, height: b.maxY - d.maxY)
+ if upper.height <= 0 || safe.maxY <= lower.minY { return l }
+ l.game = aspectFit(upper); l.controls = lower; l.separated = true
+ return l
+ }
+
+ // MARK: dm_pad_geo_at
+
+ static func side(_ s: CGRect, right: Bool, _ dx: CGFloat, _ y: CGFloat) -> CGPoint { CGPoint(x: right ? s.maxX - dx : s.minX + dx, y: y) }
+ static func diamondKey(_ c: CGPoint, _ d: CGFloat, _ i: Int) -> CGPoint {
+ switch i { case 0: CGPoint(x: c.x + d, y: c.y); case 1: CGPoint(x: c.x, y: c.y + d); case 2: CGPoint(x: c.x, y: c.y - d); default: CGPoint(x: c.x - d, y: c.y) }
+ }
+
+ static func at(_ screen: PadScreen, _ region: Region, full: Bool, u: CGFloat) -> PadGeo {
+ let bounds = CGRect(origin: .zero, size: screen.size), insets = screen.insets
+ var g = PadGeo()
+ let sep = region.separated
+ g.u = u; g.full = full; g.region = region
+ g.safe = region.controls.intersection(inset(bounds, gameSafeInsets(bounds, insets, region.topSafeOnly)))
+ var ci = insets; ci.left *= 0.5; ci.right *= 0.5
+ g.corner = sep ? g.safe : inset(bounds, ci)
+ g.portrait = sep && bounds.height > bounds.width
+ let s = g.safe, c = g.corner
+ let thumbL = CGPoint(x: s.minX + s.width * 0.28, y: s.minY + s.height * 0.58)
+ let thumbR = CGPoint(x: s.minX + s.width * 0.72, y: thumbL.y)
+ g.faceGap = 50 * u; g.faceR = 27 * u; g.faceHitR = max(g.faceR * 1.25, sep ? 22 : 0)
+ g.stickR = 77 * u; g.knobR = 30 * u
+ g.dpadR = 48 * u; g.armW = 17 * u; g.armIn = 11 * u
+ if g.portrait {
+ g.face = thumbR
+ g.stick = thumbL; if full { g.stick.x -= g.dpadR + 6 * u }
+ g.dpad = CGPoint(x: thumbL.x + g.stickR + 6 * u, y: thumbL.y)
+ } else {
+ let edge: CGFloat = sep ? g.faceGap + g.faceHitR : 0
+ g.face = side(c, right: true, max(81 * u, edge), c.maxY - max(82 * u, edge))
+ g.stick = side(c, right: false, 81 * u, c.maxY - 82 * u)
+ g.dpad = side(c, right: false, 231 * u, c.maxY - 64 * u)
+ }
+ let edge: CGFloat = sep ? 22 : 0
+ for i in 0..<4 {
+ let z = i == 0 || i == 2
+ g.drop[i] = side(s, right: i >= 2, max((z ? 44 : 44) * u, edge), s.minY + max((z ? 40 : 108) * u, edge))
+ g.dropR[i] = (z ? 26 : 24) * u
+ }
+ for i in 0..<2 { g.click[i] = side(s, right: i == 1, max(102 * u, edge), s.minY + max(40 * u, edge)) }
+ g.clickR = 17 * u
+ let top = s.minY + max(22 * u, sep ? 14 : 0), w = 50 * u, h = 28 * u
+ for i in 0..<2 {
+ let x = s.midX + (i == 1 ? 52 : -52) * u
+ g.pill[i] = CGRect(x: x - w / 2, y: top - h / 2, width: w, height: h)
+ }
+ g.swap = CGPoint(x: s.midX, y: top); g.swapR = 15 * u
+ for i in 0..<4 { g.faceKey[i] = diamondKey(g.face, g.faceGap, i) }
+ return g
+ }
+
+ func dropTip(_ i: Int) -> CGPoint {
+ let a = i >= 2 ? -Double.pi / 4 : -3 * Double.pi / 4, d = dropR[i] * 1.45
+ return CGPoint(x: drop[i].x + d * cos(a), y: drop[i].y + d * sin(a))
+ }
+
+ var shapes: [Shape] {
+ var out: [Shape] = []
+ for i in 0..<4 { out.append(Shape(c: faceKey[i], r: faceR, within: corner, id: Self.shapeFace + i)) }
+ out.append(Shape(c: stick, r: stickR, within: corner, id: Self.shapeStick))
+ if full { out.append(Shape(c: dpad, r: hypot(dpadR, armW), within: corner, id: Self.shapeDpad)) }
+ for i in 0..<4 {
+ out.append(Shape(c: drop[i], r: dropR[i], within: safe, id: Self.shapeDrop + i))
+ out.append(Shape(c: dropTip(i), r: 0, within: safe, id: Self.shapeTip + i))
+ }
+ if full { for i in 0..<2 { out.append(Shape(c: click[i], r: clickR, within: safe, id: Self.shapeClick + i)) } }
+ for i in 0..<2 { out.append(Shape(rect: pill[i], isRect: true, within: safe, id: Self.shapePill + i)) }
+ out.append(Shape(c: swap, r: swapR, within: safe, id: Self.shapeSwap))
+ return out
+ }
+ static func inside(_ s: Shape) -> Bool {
+ let e: CGFloat = 0.5, b = s.box, w = s.within
+ return b.minX >= w.minX - e && b.maxX <= w.maxX + e && b.minY >= w.minY - e && b.maxY <= w.maxY + e
+ }
+ static func overlap(_ a: Shape, _ b: Shape) -> Bool {
+ let e: CGFloat = 0.5
+ if a.isRect && b.isRect { return a.rect.insetBy(dx: e, dy: e).intersects(b.rect.insetBy(dx: e, dy: e)) }
+ if a.isRect || b.isRect {
+ let r = a.isRect ? a : b, c = a.isRect ? b : a
+ let nx = max(r.rect.minX, min(c.c.x, r.rect.maxX)), ny = max(r.rect.minY, min(c.c.y, r.rect.maxY))
+ 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
+ }
+ static func sameControl(_ a: Shape, _ b: Shape) -> Bool {
+ (a.id >= shapeDrop && a.id < shapeTip && b.id == a.id + 4) || (b.id >= shapeDrop && b.id < shapeTip && a.id == b.id + 4)
+ }
+ var fits: Bool {
+ let s = shapes
+ for i in s.indices {
+ if !Self.inside(s[i]) { return false }
+ for j in s.indices where j > i { if !Self.sameControl(s[i], s[j]) && Self.overlap(s[i], s[j]) { return false } }
+ }
+ return true
+ }
+
+ // MARK: dm_pad_geo_placed
+
+ static func baseUnit(_ screen: PadScreen, _ region: Region) -> CGFloat {
+ let bounds = CGRect(origin: .zero, size: screen.size)
+ if !region.separated { return min(bounds.height / 430.0, 1.3) }
+ let s = region.controls.intersection(inset(bounds, gameSafeInsets(bounds, screen.insets, region.topSafeOnly)))
+ return min(min(s.height / 430.0, s.width / 700.0), 1.3)
+ }
+ /// 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 }
+
+ /// The geometry before the arrangement: the size, moved toward medium until it fits, and the left stick's place.
+ static func placed(_ screen: PadScreen, full: Bool, size: String, stick place: String) -> PadGeo {
+ let region = region(screen)
+ let u0 = baseUnit(screen, region)
+ var scale = Self.scale(size)
+ var g = at(screen, region, full: full, u: u0 * scale)
+ while scale != 1 && !g.fits {
+ scale = scale > 1 ? max(1, scale - 0.05) : min(1, scale + 0.05)
+ g = at(screen, region, full: full, u: u0 * scale)
+ }
+ if place == "higher" || place == "inward" {
+ let up = place == "higher"
+ let stick = g.stick, dpad = g.dpad
+ var d = (up ? 70 : 60) * g.u
+ while d > 0 {
+ if up { g.stick = CGPoint(x: stick.x, y: stick.y - d) }
+ else { g.stick = CGPoint(x: stick.x + d, y: stick.y); g.dpad = CGPoint(x: dpad.x + d, y: dpad.y) }
+ if g.fits { return g }
+ d -= 2 * g.u
+ }
+ g.stick = stick; g.dpad = dpad
+ }
+ return g
+ }
+
+ // MARK: dm_pad_arrange
+
+ static func isShape(_ id: Int, of c: TouchLayout.Control) -> Bool {
+ switch c {
+ case .face: id >= shapeFace && id < shapeFace + 4
+ case .a: id == shapeFace
+ case .b: id == shapeFace + 1
+ case .x: id == shapeFace + 2
+ case .y: id == shapeFace + 3
+ case .stick: id == shapeStick
+ case .dpad: id == shapeDpad
+ case .l3: id == shapeClick
+ case .r3: id == shapeClick + 1
+ case .minus: id == shapePill
+ case .plus: id == shapePill + 1
+ case .zl, .l, .zr, .r:
+ id == shapeDrop + dropIndex(c) || id == shapeTip + dropIndex(c)
+ }
+ }
+ static func dropIndex(_ c: TouchLayout.Control) -> Int { c == .zl ? 0 : c == .l ? 1 : c == .zr ? 2 : 3 }
+
+ mutating func move(_ c: TouchLayout.Control, _ dx: CGFloat, _ dy: CGFloat) {
+ func shift(_ p: inout CGPoint) { p.x += dx; p.y += dy }
+ switch c {
+ case .face: shift(&face); for i in 0..<4 { shift(&faceKey[i]) }
+ case .a: shift(&faceKey[0])
+ case .b: shift(&faceKey[1])
+ case .x: shift(&faceKey[2])
+ case .y: shift(&faceKey[3])
+ case .stick: shift(&stick)
+ case .dpad: shift(&dpad)
+ case .l3: shift(&click[0])
+ case .r3: shift(&click[1])
+ case .minus: shift(&pill[0].origin)
+ case .plus: shift(&pill[1].origin)
+ case .zl, .l, .zr, .r: shift(&drop[Self.dropIndex(c)])
+ }
+ }
+ static func near(_ a: Shape, _ b: Shape, _ gap: CGFloat) -> Bool {
+ var x = a
+ if x.isRect { x.rect = x.rect.insetBy(dx: -gap, dy: -gap) } else { x.r += gap }
+ return overlap(x, b)
+ }
- /// The engine's rule for a moved control: inside its area, and every other control at least the gap away.
++ /// The engine's rule for a moved control: inside its area, every other control at least the gap away, out of the menu button's hit circle.
+ func controlFits(_ c: TouchLayout.Control) -> Bool {
+ let s = shapes, gap = max(10 * u, 10)
+ for i in s.indices where Self.isShape(s[i].id, of: c) {
+ if !Self.inside(s[i]) { return false }
- for j in s.indices where !Self.isShape(s[j].id, of: c) { if Self.near(s[i], s[j], gap) { return false } }
++ for j in s.indices where !Self.isShape(s[j].id, of: c) {
++ var o = s[j]
++ if o.id == Self.shapeSwap { o.r = max(o.r + gap, max(swapR * 1.6, 22)) - gap } // the menu button: its whole hit circle
++ if Self.near(s[i], o, gap) { return false }
++ }
+ }
+ return true
+ }
+ static func clamp1(_ lo: CGFloat, _ hi: CGFloat, _ rlo: CGFloat, _ rhi: CGFloat) -> CGFloat {
+ if hi - lo > rhi - rlo { return (rlo + rhi) / 2 - (lo + hi) / 2 }
+ return lo < rlo ? rlo - lo : hi > rhi ? rhi - hi : 0
+ }
+ /// The shapes' box of a control and the area it must stay in.
+ func box(of c: TouchLayout.Control) -> (box: CGRect, within: CGRect)? {
+ var x0 = CGFloat.greatestFiniteMagnitude, y0 = x0, x1 = -x0, y1 = -x0, within: CGRect?
+ for s in shapes where Self.isShape(s.id, of: c) {
+ let b = s.box; within = s.within
+ x0 = min(x0, b.minX); y0 = min(y0, b.minY); x1 = max(x1, b.maxX); y1 = max(y1, b.maxY)
+ }
+ guard let within else { return nil }
+ return (CGRect(x: x0, y: y0, width: x1 - x0, height: y1 - y0), within)
+ }
+ func clamp(_ c: TouchLayout.Control) -> CGVector {
+ guard let (b, w) = box(of: c) else { return .zero }
+ return CGVector(dx: Self.clamp1(b.minX, b.maxX, w.minX, w.maxX), dy: Self.clamp1(b.minY, b.maxY, w.minY, w.maxY))
+ }
+
+ mutating func arrange(_ offsets: [TouchLayout.Control: TouchLayout.Offset]) {
+ let step = self.step
+ let want = TouchLayout.Control.allCases.filter { $0.drawn(full: full) && !(offsets[$0]?.isZero ?? true) }
+ for round in 0..<2 {
+ var todo = want.filter { $0.isFaceKey == (round == 1) }
+ var progress = true
+ while !todo.isEmpty && progress {
+ progress = false
+ for c in TouchLayout.Control.allCases where todo.contains(c) {
+ var t = self
+ let o = offsets[c] ?? .zero
+ t.move(c, CGFloat(o.dx) * step, CGFloat(o.dy) * step)
+ let d = t.clamp(c)
+ t.move(c, d.dx, d.dy)
+ if !t.controlFits(c) { continue }
+ self = t; moved.insert(c); todo.removeAll { $0 == c }; progress = true
+ if c.isFaceKey { faceSplit = true }
+ }
+ }
+ dropped.formUnion(todo)
+ }
+ }
+
+ /// What the engine draws for this layout: `dm_pad_geo` with the player's options.
+ static func make(_ screen: PadScreen, full: Bool, size: String = "medium", stick: String = "corner", layout: TouchLayout = TouchLayout()) -> PadGeo {
+ var g = placed(screen, full: full, size: size, stick: stick)
+ let kind = TouchLayout.Kind.of(full: full, split: g.region.separated)
+ if !layout.isEmpty(kind) { g.arrange(layout.moved(kind)) }
+ return g
+ }
+
+ // MARK: Where a control is, for the editor
+
+ /// The point a control's offset moves: the diamond's centre, a key, the stick's or D-pad's centre, a teardrop's circle, a pill's centre.
+ func anchor(_ c: TouchLayout.Control) -> CGPoint {
+ switch c {
+ case .face: face
+ case .a: faceKey[0]
+ case .b: faceKey[1]
+ case .x: faceKey[2]
+ case .y: faceKey[3]
+ case .stick: stick
+ case .dpad: dpad
+ case .l3: click[0]
+ case .r3: click[1]
+ case .minus: CGPoint(x: pill[0].midX, y: pill[0].midY)
+ case .plus: CGPoint(x: pill[1].midX, y: pill[1].midY)
+ case .zl, .l, .zr, .r: drop[Self.dropIndex(c)]
+ }
+ }
+ /// The controls drawn in this layout, the diamond as its four keys.
+ var drawnControls: [TouchLayout.Control] {
+ TouchLayout.Control.allCases.filter { $0 != .face && $0.drawn(full: full) }
+ }
+}
diff --cc patches/0070-ios-touch-layout.patch
index 10aa29f6,00000000..3c8115d6
mode 100644,000000..100644
--- a/patches/0070-ios-touch-layout.patch
+++ b/patches/0070-ios-touch-layout.patch
@@@ -1,686 -1,0 +1,712 @@@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: v0lumehi <[email protected]>
- Date: Tue, 3 Nov 2026 18:13:58 +0100
++Date: Tue, 3 Nov 2026 20:34:40 +0100
+Subject: [PATCH] ios: the player's arrangement of the touch controls
+ (DM_TOUCH_LAYOUT)
+
+Players want to move the touch controls themselves (LNPANDY/NX2Apple#414,
+#347, seedds/nxnative#740). The earlier editor (patch 0036, reverted in
+#345) scattered the controls and broke the layout on a multi-game
+install. This one is safe by construction.
+
+DM_TOUCH_LAYOUT is one value, written by a front end's layout editor:
+"1", then per layout ";", its name and "/control:dx,dy" for each moved
+control, for example 1;2d/face:0,-2/stick:1,-1;3d/a:-1,0. There is one
+arrangement each for 2D, 3D and both again for the split screen (2s, 3s:
+DmLayout.separated, an iPad upright and iPhone Duo upright or half open).
+The controls are the diamond as a whole (face) and each of its keys (a b x
+y, from where the diamond is), the left stick, the D-pad, ZL L ZR R, L3
- R3, minus and plus. The layout button stays. Offsets are steps of a
++R3, minus and plus. The menu button stays, and a moved control keeps
++out of its hit circle. Offsets are steps of a
+quarter of a face key's width at the player's size, so a layout scales
+with the controls and not with the screen. The engine stores nothing.
+
+The geometry (DmPadGeometry.h) applies it after the size and the left
+stick's place. Each moved control is pushed back inside the area its
+built-in place uses (the safe area, the bottom corners halfway into the
+side insets as always) and placed only if it stays 10 u (at least 10
+points) clear of every other drawn control. Otherwise it keeps its
+built-in place. The diamond goes first, then its single keys. Each round
+places what fits and tries the rest again, so a control can take a place
+another one has left. Whatever the value, the result is a layout with
+every control inside and none on another. An unknown layout or control
+is skipped. Any other malformed value, or one longer than 1024
+characters, is ignored whole. The log says when a value is ignored and
+which moved controls kept their place.
+
- The touch-down decision moves out of DmGamepad.m into dm_pad_hit, a pure
- function of the same geometry, so drawing, hit-test and grab cannot
++The touch-down decision moves out of DmGamepad.m into dm_pad_hit and
++dm_pad_menu_takes (the menu button), pure functions of the same
++geometry, so drawing, hit-test and grab cannot
+disagree. Without an arrangement it decides exactly as before. With one,
+the locked keys go to the nearest, and a diamond whose keys were moved
+one by one is grabbed key by key.
+
+test_pad_geometry.py: a value of zeros gives the built-in layout exactly
+on every screen, the value's form (malformed and too-long values ignored,
+unknown names skipped, random bytes), and seeded random arrangements on
+every screen, layout, size and stick place: every control inside its
+area and on the screen, no two overlapping, a moved one clear by the gap,
+and a touch inside any drawn control taken by that control.
+
+Upstream-Status: pending (on seedds/nxnative#683)
+Reason: Players arrange the touch controls themselves (#414, #347)
+Issue: LNPANDY/NX2Apple#414
+---
- dm/ios/Sources/DmGamepad.m | 63 ++---
- dm/ios/Sources/DmPadGeometry.h | 230 +++++++++++++++++-
- .../tests/pad_geometry/pad_geometry_check.m | 181 +++++++++++++-
++ dm/ios/Sources/DmGamepad.m | 60 ++---
++ dm/ios/Sources/DmPadGeometry.h | 249 +++++++++++++++++-
++ .../tests/pad_geometry/pad_geometry_check.m | 186 ++++++++++++-
+ dm/ios/tests/test_pad_geometry.py | 7 +-
- 4 files changed, 432 insertions(+), 49 deletions(-)
++ 4 files changed, 455 insertions(+), 47 deletions(-)
+
+diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.m
- index 7653b5a..7cd4255 100644
++index 9e559df..9981005 100644
+--- a/dm/ios/Sources/DmGamepad.m
++++ b/dm/ios/Sources/DmGamepad.m
- @@ -7,8 +7,9 @@
++@@ -9,8 +9,9 @@
+ // (a touch landing in the right third of the screen on no control holds it: centred where it lands, the centre dragged along once the finger passes the stick radius) and L3 / R3 (small round keys in the top row, inward of ZL / ZR).
+ // Breath of the Wild crouches with L3 and uses the scope with R3; 3D World and Breath of the Wild turn the camera with the right stick.
+ // The first time the 3D layout shows in a launch, a few words (DM_TOUCH_HINT) say where the undrawn right stick is, for two seconds; see "The right-stick hint".
- -// 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.
- +// Touch-down ownership (dm_pad_hit in DmPadGeometry.h): 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.
++-// Touch-down ownership: the menu button where it is nearer than a locked key (a view of its own, DmMenuButton), locked keys next (shoulders, − / +, 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),
+++// Touch-down ownership (dm_pad_menu_takes and dm_pad_hit in DmPadGeometry.h): the menu button where it is nearer than a locked key (a view of its own, DmMenuButton), locked keys next (shoulders, − / +, 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.
++// The player can move single controls in the shell's settings (DM_TOUCH_LAYOUT, one arrangement per layout): the geometry places each only where it fits and keeps the rest where they were.
+ // 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) and the left stick's place through DM_TOUCH_STICK (corner, higher, inward).
+ // 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).
- @@ -115,11 +116,9 @@ static void phone_motion_start(UIView *body) {
++@@ -118,11 +119,9 @@ static void phone_motion_start(UIView *body) {
+ // 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:
++ // left stick, as on a Pro Controller. The menu button is at the top centre, level with - / +, and takes a touch within 1.6 times its radius (at least 22 points, half of a 44-point target). What stays here is how touches are taken:
+-static const CGFloat
+- K_STICK_GRAB = 1.4, // how near the ring centre a touch-down grabs a stick (times the ring radius)
++static const CGFloat // (the grab radii of the stick and the D-pad are DmPadGeometry.h's DM_PAD_STICK_GRAB and DM_PAD_DPAD_GRAB)
+ 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)
+- 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)
+ 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)
- @@ -149,8 +148,10 @@ @implementation DmGamepad {
++@@ -163,8 +162,10 @@ @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;
+- DmPadOptions _opts; // the player's options from the environment (DM_TOUCH_SIZE, DM_TOUCH_STICK)
++ DmPadOptions _opts; // the player's options from the environment (DM_TOUCH_SIZE, DM_TOUCH_STICK, DM_TOUCH_LAYOUT)
++ DmPadLayout _arrangement; // DM_TOUCH_LAYOUT, which _opts.layout points to
+ DmPadGeo _geo; CGRect _geoBounds; UIEdgeInsets _geoInsets; BOOL _geoFull, _geoValid; // where the controls are, and what it was computed for
++ unsigned _loggedDropped; // the moved controls that kept their place, as last logged
+ UITouch *_game[16]; // touches on the game's touch screen by finger slot
+ }
+
- @@ -177,6 +178,10 @@ - (instancetype)initWithFrame:(CGRect)f {
++@@ -191,6 +192,10 @@ - (instancetype)initWithFrame:(CGRect)f {
+ const char *stick = getenv("DM_TOUCH_STICK");
+ _opts.stick = dm_pad_stick_place(stick);
+ if (stick && *stick) LOG("front end: left stick %s (DM_TOUCH_STICK)", _opts.stick == DM_PAD_STICK_HIGHER ? "higher" : _opts.stick == DM_PAD_STICK_INWARD ? "inward" : "in the corner");
++ const char *arrangement = getenv("DM_TOUCH_LAYOUT");
++ if (!dm_pad_layout_parse(arrangement, &_arrangement)) LOG("front end: DM_TOUCH_LAYOUT ignored (not a layout the engine reads, %zu characters): every control keeps its place", strlen(arrangement));
++ else if (dm_pad_layout_count(&_arrangement)) LOG("front end: touch controls arranged by the player (DM_TOUCH_LAYOUT, %d moved over every layout)", dm_pad_layout_count(&_arrangement));
++ _opts.layout = &_arrangement;
+ [self registerForTraitChanges:@[UITraitDisplayScale.class] withHandler:^(id<UITraitEnvironment> environment, UITraitCollection *previous) {
+ [(UIView *)environment setNeedsLayout];
+ }];
- @@ -251,6 +256,12 @@ - (const DmPadGeo *)geo { // DmPadGeometry.h, computed again whenever the boun
++@@ -280,6 +285,12 @@ - (const DmPadGeo *)geo { // DmPadGeometry.h, computed again whenever the boun
+ 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;
++ if (_geo.dropped != _loggedDropped) { // a moved control with no room on this screen: said once per change, for the log a player sends
++ _loggedDropped = _geo.dropped;
++ NSMutableArray<NSString *> *names = [NSMutableArray array];
++ for (int c = 0; c < DM_PAD_C_N; c++) if (_geo.dropped & (1u << c)) [names addObject:@(dm_pad_control_names[c])];
++ if (names.count) LOG("front end: touch layout: %s kept at the built-in place (no room there on this screen, or on another control)", [names componentsJoinedByString:@" "].UTF8String);
++ }
+ }
+ return &_geo;
+ }
- @@ -276,29 +287,11 @@ - (CGRect)pillRect:(int)b { return self.geo->pill[b == B_PLUS ? DM_PAD_PLUS : DM
- - (CGPoint)swapCenter { return self.geo->swap; }
++@@ -305,33 +316,14 @@ - (CGRect)pillRect:(int)b { return self.geo->pill[b == B_PLUS ? DM_PAD_PLUS : DM
++ - (CGPoint)menuCenter { 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(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);
+- return MAX(fabs(p.x - CGRectGetMidX(r)) / (w / 2), fabs(p.y - CGRectGetMidY(r)) / (h / 2));
+-}
+-- (int)btnAt:(CGPoint)p { // Locked at touch-down; nearest normalized target wins overlapping separated-layout hits.
+- int best = -1; CGFloat distance = 1;
+- for (int i = 0; i < (int)(sizeof k_btns / sizeof *k_btns); i++) { int b = k_btns[i];
+- if (fullOnly(b) && !_full) continue;
+- CGFloat d = [self buttonDistance:b point:p];
+- if (d <= 1 && (best < 0 || d < distance)) {
+- best = b; distance = d;
+- if (!_layout.separated) return b;
+- }
+- }
+- return best;
++static int lockedKey(int c) { // DmPadGeometry.h's control → the key it presses
++ switch (c) { case DM_PAD_C_ZL: return B_ZL; case DM_PAD_C_L: return B_L; case DM_PAD_C_ZR: return B_ZR; case DM_PAD_C_R: return B_R;
++ case DM_PAD_C_MINUS: return B_MINUS; case DM_PAD_C_PLUS: return B_PLUS; case DM_PAD_C_L3: return B_LS; default: return B_RS; }
+ }
- -- (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; }
- +- (BOOL)inSwap:(CGPoint)p { return dm_pad_swap_distance(self.geo, _layout.separated, p) <= 1; }
++-- (CGFloat)menuDistance:(CGPoint)p { return dist(p, self.menuCenter) / MAX(self.geo->swapR * 1.6, 22); }
++ - (BOOL)menuTakes:(CGPoint)p { // the menu button takes a touch in its hit circle unless a locked key's target is nearer (the drawn keys are hidden in Tap and with a controller)
++- CGFloat d = [self menuDistance:p]; if (d > 1) return NO;
+++ if (dm_pad_menu_distance(self.geo, p) > 1) return NO;
++ if (_tap || _padOff || (_layout.separated && !CGRectContainsPoint(_layout.controls, p))) return YES;
++- int hit = [self btnAt:p];
++- return hit < 0 || d < [self buttonDistance:hit point:p];
+++ return dm_pad_menu_takes(self.geo, _full, _layout.separated, p);
++ }
+ - (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;
- for (int b = B_A; b <= B_Y; b++) if (dist(p, [self facePos:b]) <= r) m |= 1ull << b;
- @@ -374,17 +367,11 @@ - (void)touchesBegan:(NSSet<UITouch *> *)ts withEvent:(UIEvent *)e {
++@@ -407,14 +399,10 @@ - (void)touchesBegan:(NSSet<UITouch *> *)ts withEvent:(UIEvent *)e {
+ if (_padOff) { game |= [self gameBegin:t]; continue; }
+ CGPoint p = [t locationInView:self];
+ if (_layout.separated && !CGRectContainsPoint(_layout.controls, p)) { game |= [self gameBegin:t]; continue; }
- - int hit = _tap ? -1 : [self btnAt:p];
- - if ([self inSwap:p] && (hit < 0 || (_layout.separated && [self swapDistance:p] < [self buttonDistance:hit point:p]))) {
- - [_owner setObject:@(C_SWAP) forKey:t]; continue;
- - }
++- int hit = _tap ? -1 : [self btnAt:p]; // the menu button took its own touches (menuTakes)
+- if (hit >= 0) { [_owner setObject:@(C_BTN | (uint64_t)hit << 8) forKey:t]; continue; }
- + DmPadHit hit = dm_pad_hit(self.geo, _full, _layout.separated, _tap, !_stick[0], p); // the same geometry the controls are drawn with
- + if (hit.kind == DM_PAD_HIT_SWAP) { [_owner setObject:@(C_SWAP) forKey:t]; continue; }
+++ DmPadHit hit = dm_pad_hit(self.geo, _full, _layout.separated, _tap, !_stick[0], p); // the same geometry the controls are drawn with (the menu button took its own touches, menuTakes)
++ if (hit.kind == DM_PAD_HIT_KEY) { [_owner setObject:@(C_BTN | (uint64_t)lockedKey(hit.key) << 8) forKey:t]; continue; }
+ if (_tap) { game |= [self gameBegin:t]; continue; }
+- // diamond, D-pad, left stick: take the one with the smallest distance / grab radius, if it is <= 1
+- CGFloat best = 1; int kind = C_NONE, idx = 0;
+- CGFloat f = dist(p, self.faceCenter) / (self.faceGap + MAX(self.faceR * 1.6, self.faceHitR)); if (f <= best) { best = f; kind = C_FACE; }
+- if (_full) { CGFloat d = dist(p, self.dpadCenter) / MAX(self.dpadR * K_DPAD_GRAB, _layout.separated ? 22 : 0); if (d < best) { best = d; kind = C_DPAD; } }
+- if (!_stick[0]) { CGFloat s = dist(p, self.stickHome) / MAX([self stickR:0] * K_STICK_GRAB, _layout.separated ? 22 : 0); if (s < best) { best = s; kind = C_STICK; } }
++ int kind = hit.kind == DM_PAD_HIT_FACE ? C_FACE : hit.kind == DM_PAD_HIT_DPAD ? C_DPAD : hit.kind == DM_PAD_HIT_STICK ? C_STICK : C_NONE, idx = 0;
+ if (kind == C_NONE && _full && !_stick[1] && [self inRightArea:p]) { kind = C_STICK; idx = 1; _rCentre = p; [self hideHint]; } // the right stick: centred where the touch lands (the hint's job is done)
+ if (kind == C_FACE) { [_owner setObject:@(C_FACE) forKey:t]; [_faceOf setObject:@([self faceAt:p]) forKey:t]; }
+ else if (kind == C_DPAD) [_owner setObject:@(C_DPAD) forKey:t];
+diff --git a/dm/ios/Sources/DmPadGeometry.h b/dm/ios/Sources/DmPadGeometry.h
- index e6abdd0..7eeb8bc 100644
++index a0b915b..df8eebf 100644
+--- a/dm/ios/Sources/DmPadGeometry.h
++++ b/dm/ios/Sources/DmPadGeometry.h
+@@ -3,8 +3,9 @@
+ // 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.
+ // DM_TOUCH_STICK = corner | higher | inward places the left stick (inward takes the D-pad along); the right-hand controls never move.
++// DM_TOUCH_LAYOUT moves single controls, one arrangement per layout (2D, 3D, and both again for the split screen): see "The player's arrangement".
+ // 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).
++// (dm_pad_geo_fits: every drawn shape inside its rectangle, no two drawn shapes overlap), and a moved control that does not fit goes back to its place.
+ #pragma once
+ #import "DmLayout.h"
+ #include <math.h>
+@@ -16,9 +17,22 @@ enum { DM_PAD_ZL = 0, DM_PAD_L, DM_PAD_ZR, DM_PAD_R }; // the order of DmPadGe
+ enum { DM_PAD_MINUS = 0, DM_PAD_PLUS }; // the order of DmPadGeo.pill
+
+ enum { DM_PAD_STICK_CORNER = 0, DM_PAD_STICK_HIGHER, DM_PAD_STICK_INWARD }; // DM_TOUCH_STICK
++
++// The controls DM_TOUCH_LAYOUT can move, in the order they are placed, by their names in the value. face is the whole A B X Y diamond,
- +// a b x y one key of it (from where the diamond is). The layout button stays where it is.
+++// a b x y one key of it (from where the diamond is). The menu button stays where it is.
++enum { DM_PAD_C_FACE = 0, DM_PAD_C_A, DM_PAD_C_B, DM_PAD_C_X, DM_PAD_C_Y, DM_PAD_C_STICK, DM_PAD_C_DPAD, DM_PAD_C_ZL, DM_PAD_C_L, DM_PAD_C_ZR, DM_PAD_C_R,
++ DM_PAD_C_L3, DM_PAD_C_R3, DM_PAD_C_MINUS, DM_PAD_C_PLUS, DM_PAD_C_N };
++// The layouts a value holds an arrangement for: 2D, 3D, and the same two when the controls sit below the picture (DmLayout.separated:
++// an iPad held upright, iPhone Duo's inner display upright or half open).
++enum { DM_PAD_SET_2D = 0, DM_PAD_SET_3D, DM_PAD_SET_2D_SPLIT, DM_PAD_SET_3D_SPLIT, DM_PAD_SET_N };
++// Each control's offset from its built-in place, in steps of a quarter of a face key's width at the player's size, so it scales with the
++// controls and not with the screen. 0 0 is the built-in place.
++typedef struct { signed char dx[DM_PAD_SET_N][DM_PAD_C_N], dy[DM_PAD_SET_N][DM_PAD_C_N]; } DmPadLayout;
++
+ typedef struct {
+ CGFloat scale; // DM_TOUCH_SIZE: 0.8 small, 1 medium (the built-in size), 1.2 large
+ int stick; // DM_TOUCH_STICK: where the left stick sits (the D-pad moves with it when it moves inward); the right-hand controls keep their place
++ const DmPadLayout *_Nullable layout; // DM_TOUCH_LAYOUT: the player's arrangement, NULL for none
+ } DmPadOptions;
+
+ typedef struct {
+@@ -32,6 +46,9 @@ typedef struct {
+ CGPoint click[2]; CGFloat clickR; // L3, R3 (3D only)
+ CGRect pill[2]; // −, +
- CGPoint swap; CGFloat swapR; // the layout button
++ CGPoint swap; CGFloat swapR; // the menu button
++ CGPoint faceKey[4]; // A B X Y where they are: the diamond around face, unless a key was moved on its own
++ BOOL faceSplit; // a key of the diamond was moved on its own: the diamond is then grabbed key by key
++ unsigned moved, dropped; // DM_TOUCH_LAYOUT: the controls (1 << DM_PAD_C_*) it moved, and the ones it asked to move that kept their place
+ } DmPadGeo;
+
+ // Layout constants (in u), the values DmGamepad.m has always used.
+@@ -65,6 +82,10 @@ static inline int dm_pad_stick_place(const char *_Nullable v) {
+ static const CGFloat DM_PAD_STICK_UP = 70, DM_PAD_STICK_IN = 60; // how far higher / inward moves the left stick (in u), at most
+
+ static inline CGPoint dm_pad_side(CGRect s, int right, CGFloat dx, CGFloat y) { return CGPointMake(right ? CGRectGetMaxX(s) - dx : CGRectGetMinX(s) + dx, y); }
++// A key of the diamond around c: A right, B bottom, X top, Y left (i = 0..3 in that order).
++static inline CGPoint dm_pad_diamond_key(CGPoint c, CGFloat d, int i) {
++ 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); }
++}
+
+ // 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) {
+@@ -105,14 +126,12 @@ static inline DmPadGeo dm_pad_geo_at(CGRect bounds, UIEdgeInsets insets, DmLayou
+ 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;
++ for (int i = 0; i < 4; i++) g.faceKey[i] = dm_pad_diamond_key(g.face, g.faceGap, i);
+ 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_face_key(const DmPadGeo *g, int i) { return g->faceKey[i]; }
+ 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));
+@@ -172,9 +191,10 @@ static inline CGFloat dm_pad_base_unit(CGRect bounds, UIEdgeInsets insets, DmLay
+ 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) {
++// The geometry before the player's arrangement: 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), and the left stick's place.
++// The built-in size is never changed.
++static inline DmPadGeo dm_pad_geo_placed(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)) {
- @@ -196,6 +216,200 @@ static inline DmPadGeo dm_pad_geo(CGRect bounds, UIEdgeInsets insets, DmLayout l
++@@ -196,6 +216,219 @@ static inline DmPadGeo dm_pad_geo(CGRect bounds, UIEdgeInsets insets, DmLayout l
+ return g;
+ }
+
++// ---- The player's arrangement (DM_TOUCH_LAYOUT) ----
++// One value for every game, written by the shell's layout editor: "1", then for each layout ";", its name and "/control:dx,dy" for each moved
++// control, for example 1;2d/face:0,-2/stick:1,-1;3d/a:-1,0 (layouts 2d 3d 2s 3s, controls as dm_pad_control_names, steps -64 to 64, y down).
++// An unknown layout or control is skipped (a newer shell). Anything else that is not this form, or a value longer than DM_PAD_LAYOUT_MAX,
++// is ignored whole, so every control keeps its built-in place. A value with no field for a control or a layout leaves it where it is.
++// dm_pad_arrange then moves a control only to where it stays inside the area its built-in place uses and clear of every other control.
++static const char *_Nonnull const dm_pad_control_names[DM_PAD_C_N] = {"face", "a", "b", "x", "y", "stick", "dpad", "zl", "l", "zr", "r", "l3", "r3", "minus", "plus"};
++static const char *_Nonnull const dm_pad_set_names[DM_PAD_SET_N] = {"2d", "3d", "2s", "3s"};
++enum { DM_PAD_LAYOUT_MAX = 1024, DM_PAD_STEP_MAX = 64 };
++static const CGFloat DM_PAD_MOVE_GAP = 10; // a moved control keeps this far from every other drawn control: 10 u, and at least 10 points
++
++static inline int dm_pad_name_index(const char *p, size_t n, const char *_Nonnull const *_Nonnull names, int count) {
++ for (int i = 0; i < count; i++) if (strlen(names[i]) == n && !strncmp(p, names[i], n)) return i;
++ return -1;
++}
++static inline BOOL dm_pad_parse_step(const char *_Nonnull *_Nonnull pp, signed char *out) {
++ const char *p = *pp; int sign = 1, v = 0, digits = 0;
++ if (*p == '-') { sign = -1; p++; }
++ while (*p >= '0' && *p <= '9') { if (++digits > 3) return NO; v = v * 10 + (*p++ - '0'); }
++ if (!digits || v > DM_PAD_STEP_MAX) return NO;
++ *out = (signed char)(sign * v); *pp = p;
++ return YES;
++}
++// NO for a value it ignored. out is then the built-in layout, as for an unset or empty value (which give YES).
++static inline BOOL dm_pad_layout_parse(const char *_Nullable v, DmPadLayout *out) {
++ memset(out, 0, sizeof *out);
++ if (!v || !*v) return YES;
++ if (strnlen(v, DM_PAD_LAYOUT_MAX + 1) > DM_PAD_LAYOUT_MAX || v[0] != '1' || (v[1] && v[1] != ';')) return NO;
++ DmPadLayout l; memset(&l, 0, sizeof l);
++ const char *p = v + 1;
++ while (*p == ';') {
++ const char *name = ++p;
++ while (*p && *p != '/' && *p != ';') p++;
++ int set = dm_pad_name_index(name, (size_t)(p - name), dm_pad_set_names, DM_PAD_SET_N);
++ while (*p == '/') {
++ const char *control = ++p;
++ while ((*p >= 'a' && *p <= 'z') || (*p >= '0' && *p <= '9')) p++;
++ size_t n = (size_t)(p - control);
++ signed char dx, dy;
++ if (!n || *p != ':') return NO;
++ p++;
++ if (!dm_pad_parse_step(&p, &dx) || *p != ',') return NO;
++ p++;
++ if (!dm_pad_parse_step(&p, &dy)) return NO;
++ int c = dm_pad_name_index(control, n, dm_pad_control_names, DM_PAD_C_N);
++ if (set >= 0 && c >= 0) { l.dx[set][c] = dx; l.dy[set][c] = dy; }
++ }
++ if (*p && *p != ';') return NO;
++ }
++ if (*p) return NO;
++ *out = l;
++ return YES;
++}
++static inline int dm_pad_layout_count(const DmPadLayout *l) { // the moved controls a value holds, over every layout
++ int n = 0;
++ for (int s = 0; s < DM_PAD_SET_N; s++) for (int c = 0; c < DM_PAD_C_N; c++) n += l->dx[s][c] || l->dy[s][c];
++ return n;
++}
++
++// The drawn shapes (dm_pad_shapes ids) of a control.
++static inline BOOL dm_pad_is_shape_of(int id, int c) {
++ switch (c) {
++ case DM_PAD_C_FACE: return id >= DM_PAD_SHAPE_FACE && id < DM_PAD_SHAPE_FACE + 4;
++ case DM_PAD_C_A: case DM_PAD_C_B: case DM_PAD_C_X: case DM_PAD_C_Y: return id == DM_PAD_SHAPE_FACE + (c - DM_PAD_C_A);
++ case DM_PAD_C_STICK: return id == DM_PAD_SHAPE_STICK;
++ case DM_PAD_C_DPAD: return id == DM_PAD_SHAPE_DPAD;
++ case DM_PAD_C_L3: case DM_PAD_C_R3: return id == DM_PAD_SHAPE_CLICK + (c - DM_PAD_C_L3);
++ case DM_PAD_C_MINUS: case DM_PAD_C_PLUS: return id == DM_PAD_SHAPE_PILL + (c - DM_PAD_C_MINUS);
++ default: return id == DM_PAD_SHAPE_DROP + (c - DM_PAD_C_ZL) || id == DM_PAD_SHAPE_TIP + (c - DM_PAD_C_ZL);
++ }
++}
++static inline BOOL dm_pad_control_drawn(int c, BOOL full) { return full || (c != DM_PAD_C_DPAD && c != DM_PAD_C_L3 && c != DM_PAD_C_R3); }
++static inline void dm_pad_shift(CGPoint *p, CGFloat dx, CGFloat dy) { p->x += dx; p->y += dy; }
++static inline void dm_pad_move(DmPadGeo *g, int c, CGFloat dx, CGFloat dy) {
++ switch (c) {
++ case DM_PAD_C_FACE: dm_pad_shift(&g->face, dx, dy); for (int i = 0; i < 4; i++) dm_pad_shift(&g->faceKey[i], dx, dy); break;
++ case DM_PAD_C_A: case DM_PAD_C_B: case DM_PAD_C_X: case DM_PAD_C_Y: dm_pad_shift(&g->faceKey[c - DM_PAD_C_A], dx, dy); break;
++ case DM_PAD_C_STICK: dm_pad_shift(&g->stick, dx, dy); break;
++ case DM_PAD_C_DPAD: dm_pad_shift(&g->dpad, dx, dy); break;
++ case DM_PAD_C_L3: case DM_PAD_C_R3: dm_pad_shift(&g->click[c - DM_PAD_C_L3], dx, dy); break;
++ case DM_PAD_C_MINUS: case DM_PAD_C_PLUS: dm_pad_shift(&g->pill[c - DM_PAD_C_MINUS].origin, dx, dy); break;
++ default: dm_pad_shift(&g->drop[c - DM_PAD_C_ZL], dx, dy); break;
++ }
++}
++static inline CGRect dm_pad_shape_box(const DmPadShape *s) { return s->isRect ? s->rect : CGRectMake(s->c.x - s->r, s->c.y - s->r, 2 * s->r, 2 * s->r); }
++// a and b closer than gap (a grown by gap all round, a rectangle with square corners: on the safe side).
++static inline BOOL dm_pad_shapes_near(const DmPadShape *a, const DmPadShape *b, CGFloat gap) {
++ DmPadShape x = *a;
++ if (x.isRect) x.rect = CGRectInset(x.rect, -gap, -gap); else x.r += gap;
++ return dm_pad_shapes_overlap(&x, b);
++}
- +// Control c where g has it: every shape of it inside its rectangle (the one its built-in place uses), and none closer than the gap to a shape of
- +// another control.
+++// Control c where g has it: every shape of it inside its rectangle (the one its built-in place uses), none closer than the gap to a shape of
+++// another control, and none inside the menu button's hit circle.
++static inline BOOL dm_pad_control_fits(const DmPadGeo *g, BOOL full, int c) {
++ DmPadShape s[20]; int n = dm_pad_shapes(g, full, s);
++ CGFloat gap = fmax(DM_PAD_MOVE_GAP * g->u, DM_PAD_MOVE_GAP);
++ for (int i = 0; i < n; i++) {
++ if (!dm_pad_is_shape_of(s[i].id, c)) continue;
++ if (!dm_pad_shape_inside(&s[i])) return NO;
- + for (int j = 0; j < n; j++) if (!dm_pad_is_shape_of(s[j].id, c) && dm_pad_shapes_near(&s[i], &s[j], gap)) return NO;
+++ for (int j = 0; j < n; j++) {
+++ if (dm_pad_is_shape_of(s[j].id, c)) continue;
+++ DmPadShape o = s[j];
+++ if (o.id == DM_PAD_SHAPE_SWAP) o.r = fmax(o.r + gap, MAX(g->swapR * 1.6, 22)) - gap; // the menu button: its whole hit circle
+++ if (dm_pad_shapes_near(&s[i], &o, gap)) return NO;
+++ }
++ }
++ return YES;
++}
++static inline CGFloat dm_pad_clamp_1d(CGFloat lo, CGFloat hi, CGFloat rlo, CGFloat rhi) { // the shift that brings lo..hi inside rlo..rhi (centred if wider)
++ if (hi - lo > rhi - rlo) return (rlo + rhi) / 2 - (lo + hi) / 2;
++ return lo < rlo ? rlo - lo : hi > rhi ? rhi - hi : 0;
++}
++// How far control c has to move to be inside its rectangle: a control dragged past the edge stops at the edge.
++static inline CGPoint dm_pad_clamp(const DmPadGeo *g, BOOL full, int c) {
++ DmPadShape s[20]; int n = dm_pad_shapes(g, full, s);
++ CGFloat x0 = CGFLOAT_MAX, y0 = CGFLOAT_MAX, x1 = -CGFLOAT_MAX, y1 = -CGFLOAT_MAX; CGRect within = CGRectNull;
++ for (int i = 0; i < n; i++) if (dm_pad_is_shape_of(s[i].id, c)) {
++ CGRect b = dm_pad_shape_box(&s[i]); within = s[i].within;
++ x0 = fmin(x0, CGRectGetMinX(b)); y0 = fmin(y0, CGRectGetMinY(b)); x1 = fmax(x1, CGRectGetMaxX(b)); y1 = fmax(y1, CGRectGetMaxY(b));
++ }
++ if (CGRectIsNull(within)) return CGPointZero;
++ return CGPointMake(dm_pad_clamp_1d(x0, x1, CGRectGetMinX(within), CGRectGetMaxX(within)), dm_pad_clamp_1d(y0, y1, CGRectGetMinY(within), CGRectGetMaxY(within)));
++}
++// The arrangement of the current layout, on top of the size and the stick's place. Two rounds: the diamond and the other controls, then the
++// keys of the diamond on their own, from where the diamond ended up. Each round places every control that fits and tries the rest again,
++// so a control can take a place another one has left. A control that never fits keeps its place (dropped). Whatever the value, the result
++// is a layout in which every control is inside its area and no two overlap.
++static inline void dm_pad_arrange(DmPadGeo *g, BOOL full, BOOL separated, const DmPadLayout *layout) {
++ int set = (separated ? DM_PAD_SET_2D_SPLIT : DM_PAD_SET_2D) + (full ? 1 : 0);
++ CGFloat step = g->faceR / 2; // a quarter of a face key's width
++ const unsigned keys = 1u << DM_PAD_C_A | 1u << DM_PAD_C_B | 1u << DM_PAD_C_X | 1u << DM_PAD_C_Y;
++ unsigned want = 0;
++ for (int c = 0; c < DM_PAD_C_N; c++) if (dm_pad_control_drawn(c, full) && (layout->dx[set][c] || layout->dy[set][c])) want |= 1u << c;
++ for (int round = 0; round < 2; round++) {
++ unsigned todo = want & (round ? keys : ~keys);
++ for (BOOL progress = YES; todo && progress; ) {
++ progress = NO;
++ for (int c = 0; c < DM_PAD_C_N; c++) if (todo & (1u << c)) {
++ DmPadGeo t = *g;
++ dm_pad_move(&t, c, layout->dx[set][c] * step, layout->dy[set][c] * step);
++ CGPoint d = dm_pad_clamp(&t, full, c);
++ dm_pad_move(&t, c, d.x, d.y);
++ if (!dm_pad_control_fits(&t, full, c)) continue;
++ *g = t; g->moved |= 1u << c; todo &= ~(1u << c); progress = YES;
++ if ((1u << c) & keys) g->faceSplit = YES;
++ }
++ }
++ g->dropped |= todo;
++ }
++}
++
++// The geometry the gamepad draws, hit-tests and grabs with: the size and the stick's place (dm_pad_geo_placed), then the player's arrangement.
++static inline DmPadGeo dm_pad_geo(CGRect bounds, UIEdgeInsets insets, DmLayout layout, BOOL full, DmPadOptions o) {
++ DmPadGeo g = dm_pad_geo_placed(bounds, insets, layout, full, o);
++ if (o.layout) dm_pad_arrange(&g, full, layout.separated, o.layout);
++ return g;
++}
++
++// ---- Which control a touch-down takes ----
- +// Locked keys first (the shoulders, − and +, L3 / R3: in the overlay layout the first in this order that is near enough, in the split one
- +// or with a moved control the nearest), the layout button, then whichever of the diamond, the D-pad and the left stick is nearest in units
- +// of its grab radius. NONE: no control (in 3D the right third then holds the right stick, else the touch goes to the game).
- +enum { DM_PAD_HIT_NONE = 0, DM_PAD_HIT_KEY, DM_PAD_HIT_SWAP, DM_PAD_HIT_FACE, DM_PAD_HIT_DPAD, DM_PAD_HIT_STICK };
+++// The menu button (a view of its own, DmMenuButton) where it is nearer than a locked key (dm_pad_menu_takes). Then locked keys (the
+++// shoulders, − and +, L3 / R3: in the overlay layout the first in this order that is near enough, in the split one or with a moved control
+++// the nearest), then whichever of the diamond, the D-pad and the left stick is nearest in units of its grab radius. NONE: no control (in 3D
+++// the right third then holds the right stick, else the touch goes to the game).
+++enum { DM_PAD_HIT_NONE = 0, DM_PAD_HIT_KEY, DM_PAD_HIT_FACE, DM_PAD_HIT_DPAD, DM_PAD_HIT_STICK };
++typedef struct { int kind, key; } DmPadHit; // key: the DM_PAD_C_* of a locked key
++static const int dm_pad_locked[8] = {DM_PAD_C_ZL, DM_PAD_C_L, DM_PAD_C_ZR, DM_PAD_C_R, DM_PAD_C_MINUS, DM_PAD_C_PLUS, DM_PAD_C_L3, DM_PAD_C_R3};
++static const CGFloat DM_PAD_STICK_GRAB = 1.4, DM_PAD_DPAD_GRAB = 1.3; // grab radius: times the ring radius, times the D-pad's outer edge
++static inline CGFloat dm_pad_dist(CGPoint a, CGPoint b) { return hypot(a.x - b.x, a.y - b.y); }
++// A locked key's distance from p in units of its hit radius (1 or less is a hit).
++static inline CGFloat dm_pad_key_distance(const DmPadGeo *g, BOOL sep, int c, CGPoint p) {
++ if (c >= DM_PAD_C_ZL && c <= DM_PAD_C_R) return dm_pad_dist(p, g->drop[c - DM_PAD_C_ZL]) / MAX(g->dropR[c - DM_PAD_C_ZL] * 1.35, sep ? 22 : 0);
++ if (c == DM_PAD_C_L3 || c == DM_PAD_C_R3) return dm_pad_dist(p, g->click[c - DM_PAD_C_L3]) / MAX(g->clickR * 1.35, sep ? 22 : 0);
++ CGRect r = CGRectInset(g->pill[c - DM_PAD_C_MINUS], -8 * g->u, -8 * g->u);
++ if (!sep && !CGRectContainsPoint(r, p)) return CGFLOAT_MAX;
++ CGFloat w = MAX(r.size.width, sep ? 28 : 0), h = MAX(r.size.height, sep ? 28 : 0);
++ return MAX(fabs(p.x - CGRectGetMidX(r)) / (w / 2), fabs(p.y - CGRectGetMidY(r)) / (h / 2));
++}
- +static inline CGFloat dm_pad_swap_distance(const DmPadGeo *g, BOOL sep, CGPoint p) { return dm_pad_dist(p, g->swap) / MAX(g->swapR * 1.6, sep ? 14 : 0); }
- +// tap: the Tap layout (only the layout button is a control). stickFree: no touch holds the left stick yet.
- +static inline DmPadHit dm_pad_hit(const DmPadGeo *g, BOOL full, BOOL sep, BOOL tap, BOOL stickFree, CGPoint p) {
+++// The locked key a touch-down at p takes, -1 for none, and its distance.
+++static inline int dm_pad_locked_at(const DmPadGeo *g, BOOL full, BOOL sep, CGPoint p, CGFloat *_Nullable distance) {
++ int key = -1; CGFloat kd = 1;
- + if (!tap) for (int i = 0; i < 8; i++) {
+++ for (int i = 0; i < 8; i++) {
++ int c = dm_pad_locked[i];
++ if ((c == DM_PAD_C_L3 || c == DM_PAD_C_R3) && !full) continue;
++ CGFloat d = dm_pad_key_distance(g, sep, c, p);
++ if (d <= 1 && (key < 0 || d < kd)) { key = c; kd = d; if (!sep && !g->moved) break; }
++ }
- + CGFloat sd = dm_pad_swap_distance(g, sep, p);
- + if (sd <= 1 && (key < 0 || (sep && sd < kd))) return (DmPadHit){DM_PAD_HIT_SWAP, -1};
- + if (key >= 0) return (DmPadHit){DM_PAD_HIT_KEY, key};
+++ if (distance) *distance = kd;
+++ return key;
+++}
+++// The menu button's hit circle: 1.6 times its radius, at least 22 points.
+++static inline CGFloat dm_pad_menu_hit_r(const DmPadGeo *g) { return MAX(g->swapR * 1.6, 22); }
+++static inline CGFloat dm_pad_menu_distance(const DmPadGeo *g, CGPoint p) { return dm_pad_dist(p, g->swap) / dm_pad_menu_hit_r(g); }
+++// Whether the menu button takes a touch-down at p (with the controls shown): in its hit circle and nearer than any locked key's target.
+++// A moved control never reaches into that circle (dm_pad_control_fits), so the menu never takes a touch on one.
+++static inline BOOL dm_pad_menu_takes(const DmPadGeo *g, BOOL full, BOOL sep, CGPoint p) {
+++ CGFloat d = dm_pad_menu_distance(g, p), kd;
+++ if (d > 1) return NO;
+++ return dm_pad_locked_at(g, full, sep, p, &kd) < 0 || d < kd;
+++}
+++// A touch-down the menu button did not take. tap: the Tap layout (no control but the menu). stickFree: no touch holds the left stick yet.
+++static inline DmPadHit dm_pad_hit(const DmPadGeo *g, BOOL full, BOOL sep, BOOL tap, BOOL stickFree, CGPoint p) {
++ if (tap) return (DmPadHit){DM_PAD_HIT_NONE, -1};
+++ int key = dm_pad_locked_at(g, full, sep, p, NULL);
+++ if (key >= 0) return (DmPadHit){DM_PAD_HIT_KEY, key};
++ CGFloat best = 1, f = CGFLOAT_MAX; int kind = DM_PAD_HIT_NONE;
++ if (g->faceSplit) for (int i = 0; i < 4; i++) f = fmin(f, dm_pad_dist(p, g->faceKey[i]) / MAX(g->faceR * 1.6, g->faceHitR));
++ else f = dm_pad_dist(p, g->face) / (g->faceGap + MAX(g->faceR * 1.6, g->faceHitR));
++ if (f <= best) { best = f; kind = DM_PAD_HIT_FACE; }
++ if (full) { CGFloat d = dm_pad_dist(p, g->dpad) / MAX(g->dpadR * DM_PAD_DPAD_GRAB, sep ? 22 : 0); if (d < best) { best = d; kind = DM_PAD_HIT_DPAD; } }
++ if (stickFree) { CGFloat s = dm_pad_dist(p, g->stick) / MAX(g->stickR * DM_PAD_STICK_GRAB, sep ? 22 : 0); if (s < best) { best = s; kind = DM_PAD_HIT_STICK; } }
++ return (DmPadHit){kind, -1};
++}
++
+ // Where the 3D layout's right-stick hint goes: a few words, no shape, in the area the undrawn right stick answers in (x from areaMinX to
+ // the right edge), between the top row and the diamond, inside the safe area and clear of every drawn control (a gap of 6 u around it).
+ // text is the label's size including its padding. CGRectNull when there is no room: the hint is then not shown.
+diff --git a/dm/ios/tests/pad_geometry/pad_geometry_check.m b/dm/ios/tests/pad_geometry/pad_geometry_check.m
- index 7bf7e8f..598327d 100644
++index 7bf7e8f..e2a6e3a 100644
+--- a/dm/ios/tests/pad_geometry/pad_geometry_check.m
++++ b/dm/ios/tests/pad_geometry/pad_geometry_check.m
- @@ -33,6 +33,182 @@ static void check_fits(const Screen *s, BOOL full, const char *size, DmPadOption
++@@ -33,6 +33,187 @@ static void check_fits(const Screen *s, BOOL full, const char *size, DmPadOption
+ }
+ }
+
++// ---- DM_TOUCH_LAYOUT ----
++
++static int g_arranged;
++static BOOL layout_empty(const DmPadLayout *l) { return dm_pad_layout_count(l) == 0; }
++
++// The value's form: what is read, what is skipped, what is ignored whole.
++static void check_layout_values(void) {
++ DmPadLayout l;
++ if (!dm_pad_layout_parse(NULL, &l) || !layout_empty(&l)) FAIL("DM_TOUCH_LAYOUT unset: the built-in layout");
++ if (!dm_pad_layout_parse("", &l) || !layout_empty(&l)) FAIL("DM_TOUCH_LAYOUT empty: the built-in layout");
++ if (!dm_pad_layout_parse("1", &l) || !layout_empty(&l)) FAIL("DM_TOUCH_LAYOUT 1: nothing moved");
++ if (!dm_pad_layout_parse("1;3d", &l) || !layout_empty(&l)) FAIL("DM_TOUCH_LAYOUT 1;3d: a layout with no field moves nothing");
++ if (!dm_pad_layout_parse("1;2d/face:0,-2/stick:1,-1;3d/a:-1,0;3s/zl:64,-64", &l)
++ || l.dx[DM_PAD_SET_2D][DM_PAD_C_FACE] != 0 || l.dy[DM_PAD_SET_2D][DM_PAD_C_FACE] != -2 || l.dx[DM_PAD_SET_2D][DM_PAD_C_STICK] != 1
++ || l.dy[DM_PAD_SET_2D][DM_PAD_C_STICK] != -1 || l.dx[DM_PAD_SET_3D][DM_PAD_C_A] != -1 || l.dx[DM_PAD_SET_3D_SPLIT][DM_PAD_C_ZL] != 64
++ || l.dy[DM_PAD_SET_3D_SPLIT][DM_PAD_C_ZL] != -64 || dm_pad_layout_count(&l) != 4) FAIL("DM_TOUCH_LAYOUT: a valid value is not read as written");
++ // A newer shell's layout or control: skipped, the rest is read.
++ if (!dm_pad_layout_parse("1;4d/a:1,1;2d/home:3,3/b:2,0", &l) || dm_pad_layout_count(&l) != 1 || l.dx[DM_PAD_SET_2D][DM_PAD_C_B] != 2)
++ FAIL("DM_TOUCH_LAYOUT: an unknown layout or control is not skipped");
++ // Anything else is ignored whole: the controls keep their built-in places.
++ static const char *bad[] = {
++ "2;2d/a:1,1", "x", "1x", "1;2d/a", "1;2d/a:", "1;2d/a:1", "1;2d/a:1,", "1;2d/a:1,1,1", "1;2d/a:+1,1", "1;2d/a:65,0", "1;2d/a:0,-65",
++ "1;2d/a:1000,0", "1;2d/a:0001,0", "1;2d/A:1,1", "1;2d/a:1,1 ", " 1;2d/a:1,1", "1;2d/a:1,1/", "1;2d/:1,1", "1;2d/a:--1,1", "1;2d/a:1.5,1",
++ "1;2d/a:1,1\n2d", "1;2d/a=1,1", "1;2d//a:1,1",
++ };
++ for (size_t i = 0; i < sizeof bad / sizeof *bad; i++) {
++ memset(&l, 0x55, sizeof l);
++ if (dm_pad_layout_parse(bad[i], &l) || !layout_empty(&l)) FAIL("DM_TOUCH_LAYOUT '%s' is not ignored", bad[i]);
++ }
++ // Too long: ignored whole, even when every part of it is a valid field.
++ char longv[DM_PAD_LAYOUT_MAX + 64]; size_t n = 0;
++ n += (size_t)snprintf(longv + n, sizeof longv - n, "1;2d");
++ while (n < DM_PAD_LAYOUT_MAX + 8) n += (size_t)snprintf(longv + n, sizeof longv - n, "/a:1,1");
++ if (dm_pad_layout_parse(longv, &l) || !layout_empty(&l)) FAIL("DM_TOUCH_LAYOUT of %zu characters is not ignored", n);
++ longv[DM_PAD_LAYOUT_MAX - (DM_PAD_LAYOUT_MAX - 4) % 6] = 0; // the same fields, cut at a field's end below the limit
++ if (!dm_pad_layout_parse(longv, &l) || l.dx[DM_PAD_SET_2D][DM_PAD_C_A] != 1) FAIL("DM_TOUCH_LAYOUT: a value just under the limit is not read");
++ // Random bytes never crash the reader, and what it does not reject is a layout like any other (checked below by the arrangement).
++ uint32_t r = 7;
++ for (int k = 0; k < 20000; k++) {
++ char v[48]; int len = 1 + (int)(r % 46);
++ static const char alphabet[] = "1;/:,-0123456789abcdfilmnprstuvxyz23d2s3s";
++ v[0] = '1';
++ for (int i = 1; i < len; i++) { r ^= r << 13; r ^= r >> 17; r ^= r << 5; v[i] = alphabet[r % (sizeof alphabet - 1)]; }
++ v[len] = 0;
++ BOOL ok = dm_pad_layout_parse(v, &l);
++ if (!ok && !layout_empty(&l)) FAIL("DM_TOUCH_LAYOUT '%s': rejected but not empty", v);
++ for (int s = 0; s < DM_PAD_SET_N; s++) for (int c = 0; c < DM_PAD_C_N; c++)
++ if (abs(l.dx[s][c]) > DM_PAD_STEP_MAX || abs(l.dy[s][c]) > DM_PAD_STEP_MAX) FAIL("DM_TOUCH_LAYOUT '%s': a step out of range", v);
++ }
++}
++
++static BOOL geo_equal(const DmPadGeo *a, const DmPadGeo *b) {
++ #define SAME_PT(p, q) (CGPointEqualToPoint((p), (q)))
++ if (a->u != b->u || !SAME_PT(a->face, b->face) || !SAME_PT(a->stick, b->stick) || !SAME_PT(a->dpad, b->dpad) || !SAME_PT(a->swap, b->swap)) return NO;
++ for (int i = 0; i < 4; i++) if (!SAME_PT(a->drop[i], b->drop[i]) || !SAME_PT(a->faceKey[i], b->faceKey[i]) || a->dropR[i] != b->dropR[i]) return NO;
++ for (int i = 0; i < 2; i++) if (!SAME_PT(a->click[i], b->click[i]) || !CGRectEqualToRect(a->pill[i], b->pill[i])) return NO;
++ return a->faceSplit == b->faceSplit && a->moved == b->moved && a->dropped == b->dropped && a->faceR == b->faceR && a->stickR == b->stickR;
++ #undef SAME_PT
++}
++
+++enum { DM_PAD_HIT_MENU = 100 }; // the menu button: dm_pad_menu_takes, not dm_pad_hit
++// Which control a shape id belongs to, as dm_pad_hit names it.
++static void shape_owner(int id, int *kind, int *key) {
++ *key = -1;
++ if (id < DM_PAD_SHAPE_STICK) *kind = DM_PAD_HIT_FACE;
++ else if (id == DM_PAD_SHAPE_STICK) *kind = DM_PAD_HIT_STICK;
++ else if (id == DM_PAD_SHAPE_DPAD) *kind = DM_PAD_HIT_DPAD;
++ else if (id < DM_PAD_SHAPE_TIP) { *kind = DM_PAD_HIT_KEY; *key = DM_PAD_C_ZL + (id - DM_PAD_SHAPE_DROP); }
++ else if (id < DM_PAD_SHAPE_CLICK) *kind = -1; // a teardrop's tip: a point, no touch target of its own
++ else if (id < DM_PAD_SHAPE_PILL) { *kind = DM_PAD_HIT_KEY; *key = DM_PAD_C_L3 + (id - DM_PAD_SHAPE_CLICK); }
++ else if (id < DM_PAD_SHAPE_SWAP) { *kind = DM_PAD_HIT_KEY; *key = DM_PAD_C_MINUS + (id - DM_PAD_SHAPE_PILL); }
- + else *kind = DM_PAD_HIT_SWAP;
+++ else *kind = DM_PAD_HIT_MENU;
++}
++
++// Every rule of a geometry the gamepad may draw: every shape inside its rectangle and on the screen, no two overlapping, a moved control
- +// clear of every other by the gap, and a touch well inside a drawn control taken by that control (the hit-test reads the same geometry).
+++// clear of every other by the gap and of the menu button's hit circle, and a touch well inside a drawn control taken by that control (the
+++// menu button's decision and the hit-test read the same geometry).
++static void check_geo(const Screen *s, BOOL full, const DmPadGeo *g, const char *what) {
++ DmLayout l = dm_layout(s->bounds, s->insets, s->partiallyOpen, s->division);
++ 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", what, sh[i].id);
++ CGRect b = dm_pad_shape_box(&sh[i]);
++ if (!CGRectContainsRect(CGRectInset(s->bounds, -0.5, -0.5), b)) FAIL("%s %s %s: shape %d is off the screen", s->name, full ? "3D" : "2D", what, 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", what, sh[i].id, sh[j].id);
++ }
++ for (int c = 0; c < DM_PAD_C_N; c++)
++ if ((g->moved & (1u << c)) && !dm_pad_control_fits(g, full, c)) FAIL("%s %s %s: moved control %s is too close to another", s->name, full ? "3D" : "2D", what, dm_pad_control_names[c]);
++ if (g->moved & g->dropped) FAIL("%s %s: a control is both moved and dropped", s->name, what);
++ // Touches: the centre of each control and points halfway to its edge (the D-pad on its arms, a pill along its length).
++ for (int i = 0; i < n; i++) {
++ int kind, key; shape_owner(sh[i].id, &kind, &key);
++ if (kind < 0) continue;
++ CGPoint pts[5]; int np = 0; pts[np++] = sh[i].isRect ? CGPointMake(CGRectGetMidX(sh[i].rect), CGRectGetMidY(sh[i].rect)) : sh[i].c;
++ if (sh[i].isRect) { CGFloat w = sh[i].rect.size.width * 0.3; pts[np++] = CGPointMake(pts[0].x - w, pts[0].y); pts[np++] = CGPointMake(pts[0].x + w, pts[0].y); }
++ else {
++ CGFloat r = sh[i].id == DM_PAD_SHAPE_DPAD ? g->dpadR * 0.6 : sh[i].r * 0.5;
++ for (int k = 0; k < 4; k++) pts[np++] = CGPointMake(sh[i].c.x + (k == 0 ? r : k == 1 ? -r : 0), sh[i].c.y + (k == 2 ? r : k == 3 ? -r : 0));
++ }
++ for (int k = 0; k < np; k++) {
+++ BOOL menu = dm_pad_menu_takes(g, full, l.separated, pts[k]);
+++ if (kind == DM_PAD_HIT_MENU) { if (!menu) FAIL("%s %s %s: a touch on the menu button at %.0f %.0f is not the menu's", s->name, full ? "3D" : "2D", what, pts[k].x, pts[k].y); continue; }
+++ if (menu) { FAIL("%s %s %s: the menu button takes a touch at %.0f %.0f inside shape %d (moved %#x)", s->name, full ? "3D" : "2D", what, pts[k].x, pts[k].y, sh[i].id, g->moved); continue; }
++ DmPadHit h = dm_pad_hit(g, full, l.separated, NO, YES, pts[k]);
++ if (h.kind != kind || (kind == DM_PAD_HIT_KEY && h.key != key))
++ FAIL("%s %s %s: a touch at %.0f %.0f inside shape %d goes to %d/%d (moved %#x)", s->name, full ? "3D" : "2D", what, pts[k].x, pts[k].y, sh[i].id, h.kind, h.key, g->moved);
++ }
++ }
++}
++
++static uint32_t g_rng = 2026;
++static uint32_t rnd(void) { g_rng ^= g_rng << 13; g_rng ^= g_rng >> 17; g_rng ^= g_rng << 5; return g_rng; }
++
++static void check_arrangement(const Screen *screens, int nscreens) {
++ // Reset: a layout of zeros, a value that names a control at 0 0, and a value for other layouts are the built-in geometry exactly,
++ // the points the gamepad has always used (pinned above on the Pro Max).
++ DmPadLayout zero, named, other; memset(&zero, 0, sizeof zero);
++ dm_pad_layout_parse("1;2d/face:0,0/stick:0,0;3d/a:0,0/zl:0,0", &named);
++ dm_pad_layout_parse("1;2s/face:3,-2/stick:2,-2;3s/a:-2,0", &other); // only the split layouts
++ for (int i = 0; i < nscreens; i++)
++ for (int full = 0; full < 2; full++)
++ for (int z = 0; z < 3; z++) {
++ DmLayout l = dm_layout(screens[i].bounds, screens[i].insets, screens[i].partiallyOpen, screens[i].division);
++ DmPadOptions o = {dm_pad_size_scale(k_sizes[z]), 0, NULL};
++ DmPadGeo none = dm_pad_geo(screens[i].bounds, screens[i].insets, l, full, o);
++ o.layout = &zero; DmPadGeo a = dm_pad_geo(screens[i].bounds, screens[i].insets, l, full, o);
++ o.layout = &named; DmPadGeo b = dm_pad_geo(screens[i].bounds, screens[i].insets, l, full, o);
++ if (!geo_equal(&none, &a) || !geo_equal(&none, &b)) FAIL("%s %s %s: an arrangement of zeros is not the built-in layout", screens[i].name, full ? "3D" : "2D", k_sizes[z]);
++ o.layout = &other; DmPadGeo c = dm_pad_geo(screens[i].bounds, screens[i].insets, l, full, o);
++ if (!l.separated && !geo_equal(&none, &c)) FAIL("%s %s: the split layouts' arrangement moved the overlay layout", screens[i].name, full ? "3D" : "2D");
++ check_geo(&screens[i], full, &none, k_sizes[z]);
++ }
++ { // On the Pro Max: a move that fits lands where asked, a move onto another control is dropped, a move past the edge stops at it.
++ const Screen *s = &screens[3];
++ DmLayout l = dm_layout(s->bounds, s->insets, NO, CGRectNull);
++ DmPadLayout m; dm_pad_layout_parse("1;3d/face:-4,-4/stick:0,-3/minus:-2,2/b:0,1", &m);
++ DmPadOptions o = {1, 0, &m};
++ DmPadGeo base = dm_pad_geo(s->bounds, s->insets, l, YES, (DmPadOptions){1}), g = dm_pad_geo(s->bounds, s->insets, l, YES, o);
++ CGFloat step = 13.5; // a quarter of the 54-point face key
++ if (!NEAR(g.face.x, base.face.x - 4 * step) || !NEAR(g.face.y, base.face.y - 4 * step)) FAIL("Pro Max: the diamond did not move by 4 4 steps");
++ if (!NEAR(g.faceKey[0].x, base.faceKey[0].x - 4 * step) || !NEAR(g.faceKey[1].y, base.faceKey[1].y - 3 * step)) FAIL("Pro Max: the keys did not follow the diamond, B its own step");
++ if (!NEAR(g.stick.x, base.stick.x) || !NEAR(g.stick.y, base.stick.y - 3 * step)) FAIL("Pro Max: the stick did not move");
++ if (!NEAR(g.pill[DM_PAD_MINUS].origin.x, base.pill[DM_PAD_MINUS].origin.x - 2 * step) || !NEAR(g.pill[DM_PAD_MINUS].origin.y, base.pill[DM_PAD_MINUS].origin.y + 2 * step)) FAIL("Pro Max: minus did not move");
++ if (g.dropped || !g.faceSplit) FAIL("Pro Max: a move that fits was dropped (%#x)", g.dropped);
++ dm_pad_layout_parse("1;3d/stick:52,0/zl:0,-40/dpad:-30,0", &m); // the stick onto the diamond, ZL past the top, the D-pad past the left edge
++ g = dm_pad_geo(s->bounds, s->insets, l, YES, o);
++ if (!CGPointEqualToPoint(g.stick, base.stick) || !(g.dropped & 1u << DM_PAD_C_STICK)) FAIL("Pro Max: the stick moved onto the diamond");
++ if (!NEAR(g.drop[DM_PAD_ZL].y - g.dropR[DM_PAD_ZL] * DM_PAD_DROP_TIP * sin(M_PI_4), CGRectGetMinY(g.safe))) FAIL("Pro Max: ZL past the top does not stop at the edge (%.1f)", g.drop[DM_PAD_ZL].y);
++ if (g.moved & 1u << DM_PAD_C_DPAD) {
++ if (!NEAR(g.dpad.x - hypot(g.dpadR, g.armW), CGRectGetMinX(g.corner))) FAIL("Pro Max: the D-pad past the edge does not stop at it");
++ }
++ // A control can take the place another one left: the stick into the D-pad's place while the D-pad goes elsewhere (ordered after the stick).
++ dm_pad_layout_parse("1;3d/stick:11,1/dpad:0,-12", &m);
++ g = dm_pad_geo(s->bounds, s->insets, l, YES, o);
++ if (g.dropped) FAIL("Pro Max: the stick could not take the D-pad's old place (%#x)", g.dropped);
++ check_geo(s, YES, &g, "swap");
++ }
++ // The fuzz: seeded random arrangements on every screen, layout, size and stick place. Whatever is asked, the result keeps every rule.
++ for (int i = 0; i < nscreens; i++)
++ for (int full = 0; full < 2; full++)
++ for (int z = 0; z < 3; z++)
++ for (int p = 0; p < 3; p++)
++ for (int k = 0; k < 40; k++) {
++ DmPadLayout m; memset(&m, 0, sizeof m);
++ int moves = 1 + (int)(rnd() % 10);
++ for (int j = 0; j < moves; j++) {
++ int set = (int)(rnd() % DM_PAD_SET_N), c = (int)(rnd() % DM_PAD_C_N), wide = rnd() % 3 == 0;
++ int range = wide ? DM_PAD_STEP_MAX : 8;
++ m.dx[set][c] = (signed char)((int)(rnd() % (2 * range + 1)) - range);
++ m.dy[set][c] = (signed char)((int)(rnd() % (2 * range + 1)) - range);
++ }
++ DmLayout l = dm_layout(screens[i].bounds, screens[i].insets, screens[i].partiallyOpen, screens[i].division);
++ DmPadGeo g = dm_pad_geo(screens[i].bounds, screens[i].insets, l, full, (DmPadOptions){dm_pad_size_scale(k_sizes[z]), p, &m});
++ char what[64]; snprintf(what, sizeof what, "%s stick %d fuzz %d", k_sizes[z], p, k);
++ check_geo(&screens[i], full, &g, what);
++ g_arranged++;
++ }
++}
++
+ 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.
- @@ -136,6 +312,9 @@ int main(void) {
++@@ -136,6 +317,9 @@ int main(void) {
+ for (int k = 0; k < n; k++) if (dm_pad_shapes_overlap(&me, &sh[k])) FAIL("%s %s: the hint covers shape %d", screens[i].name, k_sizes[z], sh[k].id);
+ }
+
+- if (!g_fail) printf("ok: %d screens\n", nscreens);
++ check_layout_values();
++ check_arrangement(screens, nscreens);
++
++ if (!g_fail) printf("ok: %d screens, %d arrangements\n", nscreens, g_arranged);
+ 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
+index 9854dfd..44d0344 100644
+--- a/dm/ios/tests/test_pad_geometry.py
++++ b/dm/ios/tests/test_pad_geometry.py
+@@ -3,8 +3,11 @@
+ 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.
++built-in size is the layout the gamepad has always drawn. The player's arrangement (DM_TOUCH_LAYOUT): a value of zeros is the
++built-in layout exactly, a malformed or too-long value is ignored whole, and seeded random arrangements on every screen, layout,
++size and stick place never put a control outside its area, onto another or nearer than the gap, and a touch inside a drawn control
++always goes to that control (dm_pad_hit, the gamepad's own hit-test). pad_geometry/UIKit/UIKit.h stands in for the few UIKit names
++the headers use, so no simulator is needed.
+ """
+ import shutil
+ import subprocess
diff --cc patches/series
index 25aaf86e,b144b065..0577aa3d
--- a/patches/series
+++ b/patches/series
@@@ -41,6 -40,5 +40,6 @@@
0065-ios-touch-controls-size.patch
0066-ios-touch-stick-place.patch
0067-ios-touch-right-stick-hint.patch
- 0068-ios-touch-layout.patch
+ 0068-ios-quit-a-game-from-the-in-game-menu-or-by-hold.patch
+ 0069-ios-shell-compile-the-in-game-menu-so-the-gear-o.patch
++0070-ios-touch-layout.patch