Skip to content

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

54.1 KB
Raw Blame
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: v0lumehi <[email protected]>
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 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 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 (builds on seedds/nxnative#683, which landed as e2fadf22)
Reason: Players arrange the touch controls themselves (#414, #347)
Issue: LNPANDY/NX2Apple#414
---
 dm/ios/Sources/DmGamepad.m                    |  65 ++---
 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, 456 insertions(+), 51 deletions(-)

diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.m
index fc375cb..167cc49 100644
--- a/dm/ios/Sources/DmGamepad.m
+++ b/dm/ios/Sources/DmGamepad.m
@@ -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: 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).
@@ -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. 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)
@@ -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
 }
 
@@ -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];
         }];
@@ -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;
 }
@@ -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)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;
@@ -407,10 +399,7 @@ - (BOOL)gameBegin:(UITouch *)t {
 - (BOOL)controlAt:(CGPoint)p {
     if (_padOff || (_layout.separated && !CGRectContainsPoint(_layout.controls, p))) return NO;
     if (_tap) return NO;
-    if ([self btnAt:p] >= 0) return YES;
-    if (dist(p, self.faceCenter) / (self.faceGap + MAX(self.faceR * 1.6, self.faceHitR)) <= 1) return YES;
-    if (_full && dist(p, self.dpadCenter) / MAX(self.dpadR * K_DPAD_GRAB, _layout.separated ? 22 : 0) < 1) return YES;
-    if (!_stick[0] && dist(p, self.stickHome) / MAX([self stickR:0] * K_STICK_GRAB, _layout.separated ? 22 : 0) < 1) return YES;
+    if (dm_pad_hit(self.geo, _full, _layout.separated, _tap, !_stick[0], p).kind != DM_PAD_HIT_NONE) return YES;   // the geometry touchesBegan hits with (DmPadGeometry.h)
     return _full && !_stick[1] && [self inRightArea:p];
 }
 - (UIView *)hitTest:(CGPoint)p withEvent:(UIEvent *)e {
@@ -423,14 +412,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];   // 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 (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 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 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 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,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), 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)) 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 ----
+// 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));
+}
+// 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;
+    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; }
+    }
+    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 12479ac..99925b6 100644
--- a/dm/ios/tests/pad_geometry/pad_geometry_check.m
+++ b/dm/ios/tests/pad_geometry/pad_geometry_check.m
@@ -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_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 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, DM_PICTURE_16_9);
+    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, DM_PICTURE_16_9);
+                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, DM_PICTURE_16_9);
+        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, DM_PICTURE_16_9);
+                        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 +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