From 13b4b0ca9b0f8c83dcb059a3eac04977bac4cd95 Mon Sep 17 00:00:00 2001 From: itaskaev2 <165683571+itaskaev2@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:50:55 +0100 Subject: [PATCH] Lay the UI out for any screen shape from 4:3 to 32:9, and offer 4:3 and phone sizes (#632, #1453) A game resolution whose shape the fifteen listed sizes did not cover (13:6 on a phone streamed to, 4:3) drew 16:9 with bars: neither the setup's Shapes step (installer/screens.py shape_size) nor the game's background generator (runtime/romfs.c romfs_folders_prepare) made a layout for it, though tools/ultrawide_layout.py handles any aspect ratio and runtime/romfs.c overlay_init already loads a folder of the exact size. cfg.c cfg_shape_size and screens.py shape_size now give an unlisted shape from 4:3 to 32:9 its own size, so both make romfs-patches-x for it; a display of an unlisted shape still keeps the 1920x1080 default and gets no folder. The list gains 4:3 (1440x1080, 1600x1200, 2048x1536, 2732x2048) and 19.5:9 and 20:9 phone sizes (2340x1080, 3120x1440, 2400x1080, 3200x1440), in the menu's game resolution row too. Co-Authored-By: Claude Opus 5.5 --- docs/installer.md | 4 +++- installer/screens.py | 32 ++++++++++++++++++++++++-------- runtime/cfg.c | 19 ++++++++++++++++--- runtime/cfg.h | 5 +++-- runtime/cfg_probe.c | 14 +++++++++++--- runtime/ui/menu.cpp | 13 ++++++++----- tests/installer_test.py | 11 +++++++++-- totk.ini.example | 20 +++++++++++--------- 8 files changed, 85 insertions(+), 33 deletions(-) diff --git a/docs/installer.md b/docs/installer.md index ac0388ac..9953c60a 100644 --- a/docs/installer.md +++ b/docs/installer.md @@ -109,7 +109,9 @@ loads the saves 1.4.3 wrote. file but the UI archive; for `pc` with the same `--binds` as the PC prompts step, whose key the step follows). The shapes are those of every connected display (Windows: `EnumDisplayDevices`; Linux: `xrandr`, else the DRM connectors), of the `output_res` the game defaults to on each, and of the `totk.ini` `output_res` (`installer/screens.py`). One folder per shape, named after the shape's size in `ASPECT_SIZES` (the - closest within 1%; `5120x2160` uses `2560x1080`); `runtime/romfs.c` `overlay_init` maps the game resolution the + closest within 1%; `5120x2160` uses `2560x1080`), or for an unlisted shape from 4:3 to 32:9 its own size (a + `totk.ini` `output_res` of `2560x1200`, say; a display of an unlisted shape gets none, as the game keeps 1920x1080 + there); `runtime/romfs.c` `overlay_init` maps the game resolution the same way, and a folder of the exact size from an older setup still wins. A 16:9 player gets none. The step runs again when the displays, the prompts or the patches change. At a game resolution whose shape has no folder the game draws 16:9 with bars and starts the generator itself, in the background, for the next start: at start and diff --git a/installer/screens.py b/installer/screens.py index 8b96e349..99cccbef 100644 --- a/installer/screens.py +++ b/installer/screens.py @@ -16,22 +16,28 @@ from pathlib import Path # The game resolutions of another shape than 16:9 that runtime/ui/menu.cpp row_output_res lists, in the order of # runtime/cfg.c SHAPED: 21:9 and 32:9 monitors, 16:10 screens (the Steam Deck's 1280x800, handhelds and laptops) and 3:2 -# screens (Surface and Framework laptops and tablets) +# screens (Surface and Framework laptops and tablets), 4:3 screens (CRT monitors, iPads) and 19.5:9 and 20:9 phone screens +# (streamed to a phone) ASPECT_SIZES = ('2560x1080', '3440x1440', '3840x1600', '5120x2160', '3840x1080', '5120x1440', '1280x800', '1680x1050', '1920x1200', '2560x1600', - '1920x1280', '2160x1440', '2256x1504', '2880x1920', '3000x2000') + '1920x1280', '2160x1440', '2256x1504', '2880x1920', '3000x2000', + '1440x1080', '1600x1200', '2048x1536', '2732x2048', + '2340x1080', '3120x1440', '2400x1080', '3200x1440') SHAPED = tuple(tuple(int(v) for v in s.split('x')) for s in ASPECT_SIZES) TOLERANCE = 0.01 # cfg.c and resolution.c res_is_16x9: shapes within 1% are the same +# cfg.c SHAPE_MIN / SHAPE_MAX: the aspect ratios a UI layout can be made for (tools/ultrawide_layout.py), 4:3 to 32:9 +SHAPE_MIN, SHAPE_MAX = 4 / 3 * 0.99, 32 / 9 * 1.01 def is_16x9(w, h): return abs((w / h) / (16 / 9) - 1) <= TOLERANCE -def shape_size(w, h): +def shape_size(w, h, listed=False): """The folder size of w x h's shape: of ASPECT_SIZES within 1% of its aspect ratio, the closest, the first of equal - ones (5120x2160 is 2560x1080's shape). None for 16:9 and for a shape the setup has no layout for. The same rule as - runtime/cfg.c cfg_shape_size.""" + ones (5120x2160 is 2560x1080's shape); another shape from 4:3 to 32:9 its own size (an output_res of a phone's + 3120x1440, say), unless listed. None for 16:9 and for a shape no layout can be made for (portrait, narrower than + 4:3, wider than 32:9). The same rule as runtime/cfg.c cfg_shape_size.""" if not w or not h or is_16x9(w, h): return None r, best, err = w / h, None, None @@ -39,7 +45,15 @@ def shape_size(w, h): e = abs(sw / sh / r - 1) if e <= TOLERANCE and (err is None or e < err): best, err = f'{sw}x{sh}', e - return best + if best or listed or not SHAPE_MIN <= r <= SHAPE_MAX: + return best + return f'{w}x{h}' + + +def size_order(size): + """Sort key: the ASPECT_SIZES order, then the sizes of unlisted shapes by width and height.""" + w, h = (int(v) for v in size.split('x')) + return (ASPECT_SIZES.index(size), 0, 0) if size in ASPECT_SIZES else (len(ASPECT_SIZES), w, h) def default_size(w, h): @@ -199,8 +213,10 @@ def wanted(dest, screens=None): text = '' sizes = set() for w, h in screens: + # A display of an unlisted shape keeps the 1920x1080 default (cfg.c screen_default): no folder for it unless + # output_res asks for its size. w, h = max(w, h), min(w, h) - sizes.add(shape_size(w, h)) + sizes.add(shape_size(w, h, listed=True)) d = default_size(w, h) if d: sizes.add(shape_size(*(int(v) for v in d.split('x')))) @@ -208,4 +224,4 @@ def wanted(dest, screens=None): if m: sizes.add(shape_size(int(m.group(1)), int(m.group(2)))) sizes.discard(None) - return sorted(sizes, key=ASPECT_SIZES.index), prompts_style(text) + return sorted(sizes, key=size_order), prompts_style(text) diff --git a/runtime/cfg.c b/runtime/cfg.c index 45d66eea..d8919a6a 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -410,12 +410,21 @@ static const unsigned SHAPED[][2] = { {3840, 1080}, {5120, 1440}, // 32:9 {1280, 800}, {1680, 1050}, {1920, 1200}, {2560, 1600}, // 16:10 {1920, 1280}, {2160, 1440}, {2256, 1504}, {2880, 1920}, {3000, 2000}, // 3:2 + {1440, 1080}, {1600, 1200}, {2048, 1536}, {2732, 2048}, // 4:3 (CRT monitors, iPads) + {2340, 1080}, {3120, 1440}, {2400, 1080}, {3200, 1440}, // 19.5:9 and 20:9 (phones, streamed) }; +// The shapes a UI layout can be made for (tools/ultrawide_layout.py works on any aspect ratio between these): from 4:3 +// to 32:9, 1% either side. A shape between them that SHAPED does not list takes a folder of its own exact size. +#define SHAPE_MIN (4.0 / 3.0 * 0.99) +#define SHAPE_MAX (32.0 / 9.0 * 1.01) + // The folder size of w x h's shape: of SHAPED within 1% of its aspect ratio, the closest, the first of equal ones // (5120x2160 takes 2560x1080). The UI layout depends on the aspect ratio alone (tools/ultrawide_layout.py apply), so // the setup writes one romfs-patches-x per shape, named after this size (installer/screens.py shape_size, the -// same rule), and romfs.c overlay_init looks for it. 0 for 16:9 and for a shape without a layout. +// same rule), and romfs.c overlay_init looks for it. A shape SHAPED does not list but within SHAPE_MIN..SHAPE_MAX (a +// phone's 13:6 screen streamed to, say) is its own folder, w x h itself, which the setup and the game's background +// generator make as they make the listed ones. 0 for 16:9 and outside that range (portrait screens). int cfg_shape_size(unsigned w, unsigned h, unsigned out[2]) { if (!w || !h) return 0; double r = (double)w / h, err = 0; @@ -425,8 +434,12 @@ int cfg_shape_size(unsigned w, unsigned h, unsigned out[2]) { double e = fabs((double)SHAPED[i][0] / SHAPED[i][1] / r - 1); if (e <= 0.01 && (best < 0 || e < err)) best = i, err = e; } - if (best < 0) return 0; - out[0] = SHAPED[best][0], out[1] = SHAPED[best][1]; + if (best >= 0) { + out[0] = SHAPED[best][0], out[1] = SHAPED[best][1]; + return 1; + } + if (r < SHAPE_MIN || r > SHAPE_MAX) return 0; + out[0] = w, out[1] = h; return 1; } diff --git a/runtime/cfg.h b/runtime/cfg.h index 140a5b9e..93080b7d 100644 --- a/runtime/cfg.h +++ b/runtime/cfg.h @@ -23,8 +23,9 @@ unsigned cfg_version(void); int cfg_from_env(const char *name); // 1 when the player set name (the environment or totk.ini), 0 when cfg_get gives a device profile's default or NULL. int cfg_is_set(const char *name); -// The folder size of w x h's shape (cfg.c SHAPED: the closest aspect ratio within 1%, the first of equal ones), the -// one romfs-patches-x the setup writes for it. 0 for 16:9 and for a shape without a UI layout. +// The folder size of w x h's shape (cfg.c SHAPED: the closest aspect ratio within 1%, the first of equal ones; an +// unlisted shape from 4:3 to 32:9 keeps w x h), the one romfs-patches-x the setup writes for it. 0 for 16:9 and +// for a shape without a UI layout (portrait, narrower than 4:3, wider than 32:9). int cfg_shape_size(unsigned w, unsigned h, unsigned out[2]); // The size output_res defaults to on this display (its shape, cfg.c screen_default) while the player set no // output_res, also when the default stays 1920x1080 because that shape lacks the prompts' folder. The generator the game diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c index 3e7ec18a..fb23a735 100644 --- a/runtime/cfg_probe.c +++ b/runtime/cfg_probe.c @@ -357,7 +357,8 @@ int probe_cfg(void) { "prompts: the PlayStation folder puts A on the circle exactly when the layout puts A on the east button"); } // One folder per screen shape (cfg.c cfg_shape_size, installer/screens.py shape_size): the closest aspect ratio of - // SHAPED within 1%, the first of equal ones; 16:9 and unlisted shapes have none. + // SHAPED within 1%, the first of equal ones; an unlisted shape from 4:3 to 32:9 its own size; 16:9, portrait and + // shapes narrower than 4:3 none. unsigned sz[2] = {0, 0}; CHECK(cfg_shape_size(5120, 2160, sz) && sz[0] == 2560 && sz[1] == 1080, "shape: 5120x2160 takes 2560x1080's folder"); CHECK(cfg_shape_size(3440, 1440, sz) && sz[0] == 3440 && sz[1] == 1440, "shape: 3440x1440 keeps its own (0.8% off 2560x1080)"); @@ -365,8 +366,15 @@ int probe_cfg(void) { CHECK(cfg_shape_size(2560, 1600, sz) && sz[0] == 1280 && sz[1] == 800 && cfg_shape_size(3000, 2000, sz) && sz[0] == 1920 && sz[1] == 1280 && cfg_shape_size(5120, 1440, sz) && sz[0] == 3840 && sz[1] == 1080, "shape: 16:10, 3:2 and 32:9 take their first size"); - CHECK(!cfg_shape_size(1920, 1080, sz) && !cfg_shape_size(2560, 1440, sz) && !cfg_shape_size(1024, 768, sz), - "shape: none for 16:9 and 4:3"); + CHECK(cfg_shape_size(1024, 768, sz) && sz[0] == 1440 && sz[1] == 1080 && cfg_shape_size(2048, 1536, sz) && + sz[0] == 1440 && sz[1] == 1080 && cfg_shape_size(3120, 1440, sz) && sz[0] == 2340 && sz[1] == 1080, + "shape: 4:3 and 19.5:9 take their first size"); + CHECK(cfg_shape_size(2560, 1200, sz) && sz[0] == 2560 && sz[1] == 1200 && cfg_shape_size(2000, 1080, sz) && + sz[0] == 2000 && sz[1] == 1080, + "shape: an unlisted shape between 4:3 and 32:9 keeps its own size"); + CHECK(!cfg_shape_size(1920, 1080, sz) && !cfg_shape_size(2560, 1440, sz) && !cfg_shape_size(1280, 1024, sz) && + !cfg_shape_size(1080, 1920, sz) && !cfg_shape_size(7680, 1440, sz), + "shape: none for 16:9, narrower than 4:3, portrait and wider than 32:9"); // romfs.c shaped_name: the exact size's folder (an older setup wrote every size) wins, else the shape's { char dir[1100], f[1200]; diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index e0973839..280a7692 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -571,15 +571,18 @@ static void folders_retry(const char *id, int need) { } // The game resolution (resolution.c): 16:9 sizes from its smallest, 1280x720 (for weak GPUs), up, the common 21:9 and -// 32:9 monitor sizes, the 16:10 screens of the Steam Deck, handhelds and laptops and the 3:2 screens of laptops and -// tablets (Surface, Framework; the camera and the HUD follow -// another shape, resolution.c res_init; the installer lays the HUD out for each, installer/steps.py ASPECT_SIZES), and -// the current value when it is another size. +// 32:9 monitor sizes, the 16:10 screens of the Steam Deck, handhelds and laptops, the 3:2 screens of laptops and +// tablets (Surface, Framework), the 4:3 screens of CRT monitors and iPads and the 19.5:9 and 20:9 screens of phones a +// game is streamed to (the camera and the HUD follow another shape, resolution.c res_init; the installer lays the HUD +// out for each, installer/screens.py ASPECT_SIZES), and the current value when it is another size: any shape from 4:3 +// to 32:9 gets a layout of its own (cfg.c cfg_shape_size). static void row_output_res(void) { static const char *const vals[] = {"1280x720", "1600x900", "1920x1080", "2560x1440", "3200x1800", "3840x2160", "2560x1080", "3440x1440", "3840x1600", "5120x2160", "3840x1080", "5120x1440", "1280x800", "1680x1050", "1920x1200", "2560x1600", - "1920x1280", "2160x1440", "2256x1504", "2880x1920", "3000x2000"}; + "1920x1280", "2160x1440", "2256x1504", "2880x1920", "3000x2000", + "1440x1080", "1600x1200", "2048x1536", "2732x2048", + "2340x1080", "3120x1440", "2400x1080", "3200x1440"}; enum { N = sizeof vals / sizeof *vals }; char shown[N][32]; const char *names[N]; diff --git a/tests/installer_test.py b/tests/installer_test.py index 44201d08..04cff31b 100644 --- a/tests/installer_test.py +++ b/tests/installer_test.py @@ -1051,8 +1051,11 @@ class ShapesTest(unittest.TestCase): def test_shape_rule(self): self.assertEqual([screens.shape_size(*s) for s in ((5120, 2160), (3440, 1440), (1920, 800), (2560, 1600), - (1920, 1080), (1024, 768))], - ['2560x1080', '3440x1440', '3840x1600', '1280x800', None, None]) + (1920, 1080), (1024, 768), (3120, 1440), (2560, 1200), + (1280, 1024), (1080, 1920), (7680, 1440))], + ['2560x1080', '3440x1440', '3840x1600', '1280x800', None, '1440x1080', '2340x1080', + '2560x1200', None, None, None]) + self.assertIsNone(screens.shape_size(2560, 1200, listed=True)) # a display of an unlisted shape self.assertEqual(screens.default_size(3440, 1440), '2560x1080') # runtime/cfg.c screen_default self.assertEqual(screens.default_size(1280, 800), '1280x800') @@ -1086,6 +1089,10 @@ class ShapesTest(unittest.TestCase): (['2560x1080', '3440x1440', '1280x800', '1920x1280'], 'pc')) self.assertEqual(screens.wanted(self.dest / 'none', [(2560, 1440)]), ([], None)) + def test_wanted_unlisted_output_res_gets_its_own_size(self): + (self.dest / 'totk.ini').write_text('output_res = 2560x1200\n') + self.assertEqual(screens.wanted(self.dest, [(2560, 1200), (1600, 1200)]), (['1440x1080', '2560x1200'], None)) + def test_layout_settings_keep_the_last_nonempty_value(self): for empty in ('', ' ', '""'): with self.subTest(empty=empty): diff --git a/totk.ini.example b/totk.ini.example index 2c2ef109..b5b98cc9 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -220,15 +220,17 @@ # gpu_name = NVIDIA # Game resolution: the size the game draws its picture and interface in (1280x720 to 7680x4320). Above 1920x1080 the # game's texture heap and memory pool grow with it. Another shape than 16:9, such as 3440x1440 on a 21:9 monitor or -# the Steam Deck's 1280x800 (16:10), gives the camera's view that shape and lays the HUD out for it. The installer -# prepares the UI layout for fifteen sizes: 2560x1080, 3440x1440, 3840x1600, 5120x2160 (21:9), 3840x1080, 5120x1440 -# (32:9), 1280x800, 1680x1050, 1920x1200, 2560x1600 (16:10), 1920x1280, 2160x1440, 2256x1504, 2880x1920 and 3000x2000 -# (3:2). For any other size, or a source build that has not made the layout, the game draws the largest 16:9 size that -# fits inside it (3440x1080 gives 1920x1080) and the present adds bars; build the layout with -# tools/make_romfs_patches.py --aspect WxH to use the full shape. Read at start. Default: 1920x1080, or on a primary -# display of one of those shapes the smallest of its sizes at least 1080 high that fits the display (1920x1280 on a 3:2 -# laptop, 2560x1080 on a 21:9 monitor), so the picture fills the screen without black bars, with prompts = xbox or pc -# too (on an install by an older setup, which built the Xbox prompts for 16:9 only, prompts = xbox keeps 1920x1080). +# the Steam Deck's 1280x800 (16:10), gives the camera's view that shape and lays the HUD out for it. Any shape from 4:3 +# to 32:9 works: 21:9 and 32:9 monitors, 16:10 and 3:2 screens, 4:3 (1440x1080, 2048x1536 on an iPad) and phone +# screens streamed to (2340x1080 or 3120x1440 at 19.5:9, 2400x1080 at 20:9). The installer makes the UI layout for the +# shapes of the connected displays and of this setting; at a shape it has none for, the game draws the largest 16:9 +# size that fits inside it with bars and makes the layout in the background for the next start (a source build: make +# it with tools/make_romfs_patches.py --aspect WxH). A shape narrower than 16:9 keeps the game's vertical view, so it +# shows less at the sides; fov widens it. Portrait shapes and shapes outside 4:3..32:9 draw 16:9 with bars. Read at +# start. Default: 1920x1080, or on a primary display of 21:9, 32:9, 16:10, 3:2 or 4:3 the smallest size of that shape +# at least 1080 high that fits the display (1920x1280 on a 3:2 laptop, 2560x1080 on a 21:9 monitor), so the picture +# fills the screen without black bars, with prompts = xbox or pc too (on an install by an older setup, which built the +# Xbox prompts for 16:9 only, prompts = xbox keeps 1920x1080). # output_res = 1920x1080 # HUD width on a game resolution wider than 16:9: full, the default, puts the HUD at the screen's left and right edges; # 21:9 keeps it within the middle 21:9 of the screen and 16:9 within the middle 16:9 (the game's own HUD), so the