From 8ff29be86f10c9f6a19adbdb88b9d7d6cf8e6932 Mon Sep 17 00:00:00 2001 From: local Date: Tue, 3 Nov 2026 19:29:41 +0300 Subject: [PATCH] Use the mouse in the game's menus on 1.4.3 While one of the game's menus has its focus box (the title, the save list, the inventory, Options, shops, dialogue choices), the cursor is released from the mouse camera and shown; the item under the pointer takes the focus as the d-pad would, a left click is A on it and a right click is B. runtime/menu_cursor.c reads eui's screens at each pad read and moves the focus with the writes eui::BoxCursorMgr::moveBoxCursor makes, adapted from Weeping of the Empire's totk_menu_cursor.cpp (1.0.0, MIT), with eui::ScreenMgr's instance placed in 1.4.3 at main+0x3acf960. menu_cursor = 0 turns it off. --- THIRD_PARTY_NOTICES.md | 1 + docs/controls.md | 6 + runtime/cfg.c | 6 +- runtime/gfx.h | 1 + runtime/gfx_present.c | 13 ++ runtime/input.c | 3 +- runtime/input.h | 4 + runtime/input_sdl.c | 2 + runtime/input_win.c | 19 +++ runtime/input_wsys.h | 3 + runtime/menu_cursor.c | 337 +++++++++++++++++++++++++++++++++++++++++ totk.ini.example | 4 + 12 files changed, 395 insertions(+), 4 deletions(-) create mode 100644 runtime/menu_cursor.c diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 2d890f97..8b5e93b7 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -103,3 +103,4 @@ folder's `licenses/` and in each component's own folder. | `totk_xlate` (NativeAOT linux-x64 build of `tools/xlate/`, linked with the folder's clang, lld and sysroot) | built by `tools/build_xlate.py --llvm --sysroot` | MIT | `bin/totk_xlate` | | AppImage type-2 runtime (static; includes libfuse and squashfuse), the AppImage's first part; appimagetool packs the folder and is not distributed | https://github.com/AppImage/type2-runtime | MIT; libfuse LGPL-2.1; squashfuse BSD-2-Clause | the AppImage header | | MacNativeGames shader variant list | as in the installer release row above | MIT | `data/shader_list.tsv` | +| Weeping of the Empire menu cursor: eui's focus box read through ScreenMgr, BoxCursorMgr and the screens' node lists, AnimButton's hit test, the stale-hit-pane fallback to the parts panes, the three writes of moveBoxCursor, the structure offsets (1.0.0; the ScreenMgr instance placed in 1.4.3 by us) | https://github.com/daniel-madrid-07/weeping-of-the-empire @ `df0586cdc5702ca19bbebfa59697e0b6980f6237` (`engine/runtime/totk_menu_cursor.cpp`), Copyright (c) 2026 daniel-madrid-07 | MIT | `runtime/menu_cursor.c` | diff --git a/docs/controls.md b/docs/controls.md index ad2de67c..c59ebbb1 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -122,6 +122,12 @@ In stick-emulation mode, mouse motion (raw input) becomes right-stick deflection The stick direction bits (`StickLLeft` to `StickRDown`) are derived from the sticks at 50% deflection. A gamepad plugged in later is picked up within one second; with none connected the log says `input: no gamepad`. +## Mouse in the game's menus + +On game 1.4.3, while one of the game's menus has its focus box (the frame the d-pad moves: the title, the save list, the inventory and its System tab, Options, shops, dialogue choices), the cursor is released from the mouse camera and shown in the middle of the window. The item under the pointer takes the focus as if the d-pad had moved there, with the game's highlight, sound and description; a left click is A on it, a right click is B. The keys and the pad keep working, and the focus moves only when the pointer moves. The ability wheel, the d-pad strips, Autobuild, the Purah Pad's camera and the map stay gameplay: the mouse keeps the camera there. When the menu closes the cursor is captured again. + +`runtime/menu_cursor.c` reads eui's screens at each pad read and moves the focus with the writes `eui::BoxCursorMgr::moveBoxCursor` makes, so no game code is called. `menu_cursor = 0` (`TOTK_MENU_CURSOR=0`, live) turns it off; `TOTK_MENU_CURSOR_LOG=1` logs every screen it reads (by default the first six), with the layout size, the node count and the hit panes it uses. + ## Nintendo Switch 2 controllers Windows does not present the Switch 2 Pro Controller as a gamepad SDL can open (the log says `input: no gamepad`), so it needs a bridge program that wakes it and creates a virtual gamepad. The DS4Windows fork by hbashton ([github.com/hbashton/DS4Windows](https://github.com/hbashton/DS4Windows), tested with release VIIPERRC4.5.1, a pre-release) with the output set to an emulated DualSense gives buttons, rumble, gyro and accelerometer, and reads the sticks through the controller's calibration (they rest at 0 to 2%). The game reads its virtual DualSense directly; the log then shows `input: gamepad "DualSense Wireless Controller" (rumble, gyro and accelerometer, ...)` and `input: motion sensors on`. Its release notes also list Joy-Con 2 support, which is untested here. diff --git a/runtime/cfg.c b/runtime/cfg.c index d09a2366..79501544 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -43,7 +43,7 @@ static const char *const known[] = { "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST", - "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", + "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", @@ -83,7 +83,7 @@ static const struct { const char *key, *section; } sections[] = { {"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, {"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"}, {"STICK_SENS", "input"}, - {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"}, + {"MENU_CURSOR", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"}, {"BIND_A", "input"}, {"BIND_B", "input"}, {"BIND_X", "input"}, {"BIND_Y", "input"}, {"BIND_LCLICK", "input"}, {"BIND_RCLICK", "input"}, {"BIND_L", "input"}, {"BIND_R", "input"}, {"BIND_ZL", "input"}, {"BIND_ZR", "input"}, {"BIND_PLUS", "input"}, {"BIND_MINUS", "input"}, {"BIND_LEFT", "input"}, {"BIND_UP", "input"}, @@ -148,7 +148,7 @@ static const CfgMeta metas[] = { {"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", RESTART}, {"LSTICK_DEADZONE", "", "0..50", LIVE}, {"RSTICK_DEADZONE", "", "0..50", LIVE}, {"MOUSE_MODE", "stick", "stick|direct", LIVE}, - {"MOUSE_SENS", "1", "0..10", LIVE}, + {"MOUSE_SENS", "1", "0..10", LIVE}, {"MENU_CURSOR", "1", "0|1", LIVE}, {"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE}, {"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE}, {"AMIIBO_DIR", "", NULL, LIVE}, diff --git a/runtime/gfx.h b/runtime/gfx.h index 614e8f65..fae3de28 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -23,6 +23,7 @@ uint64_t gfx_vsync_ticks(void); // ticks since start uint64_t gfx_vsync_time_ns(uint64_t tick); // kern_now_ns() time of a tick (pacing) double gfx_window_refresh_hz(void); // refresh rate of the window's monitor; 0 unknown int gfx_window_focused(void); +int gfx_picture_rect(float out[4]); // where the last present put the picture, fractions of the swapchain (x, y, w, h); 0 before one // ---- shader service client (gfx_shader.c) enum { GFX_STAGE_VS, GFX_STAGE_TCS, GFX_STAGE_TES, GFX_STAGE_GS, GFX_STAGE_FS, GFX_STAGE_CS }; diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 9b5afbbf..1ab71f68 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -543,6 +543,15 @@ static void save_capture(const char *path) { // TOTK_ASPECT=stretch, else (fit, the default) the largest one of the picture's own aspect ratio, centred (a 16:9 picture // on the Steam Deck's 1280x800 screen gets bars of 40 rows above and below; a 16:9 picture in a 21:9 window, bars at the // sides). +// Where the last present put the picture, as fractions of the swapchain (x, y, w, h): menu_cursor.c's pointer +static float g_pic[4]; +static volatile int32_t g_pic_ok; +int gfx_picture_rect(float out[4]) { + if (!__atomic_load_n(&g_pic_ok, __ATOMIC_ACQUIRE)) return 0; + memcpy(out, g_pic, sizeof g_pic); + return 1; +} + void gfx_fit_rect(int cw, int ch, int dw, int dh, int out[4]) { out[0] = out[1] = 0, out[2] = dw, out[3] = dh; if (cw <= 0 || ch <= 0 || !strcmp(cfg_value("TOTK_ASPECT"), "stretch")) return; @@ -616,6 +625,10 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w const int rect[4] = {cx, cy, cw, ch}; int to[4]; // where the picture goes in the swapchain image: {x, y, w, h} gfx_fit_rect(cw, ch, (int)g_sc_ext.width, (int)g_sc_ext.height, to); + if (g_sc_ext.width && g_sc_ext.height) { + for (int i = 0; i < 4; i++) g_pic[i] = (float)to[i] / (float)(i & 1 ? g_sc_ext.height : g_sc_ext.width); + __atomic_store_n(&g_pic_ok, 1, __ATOMIC_RELEASE); + } if (to[2] != (int)g_sc_ext.width || to[3] != (int)g_sc_ext.height) { // black bars beside or above and below it VkClearColorValue black = {.float32 = {0, 0, 0, 1}}; VkImageSubresourceRange all = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; diff --git a/runtime/input.c b/runtime/input.c index 581054d5..aadd2dfe 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -628,7 +628,7 @@ static volatile uint64_t g_block_closed, g_menu_closed; int input_cursor_allowed(void) { const char *in = cfg_get("TOTK_INPUT"); - return g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 && !(in && !strcmp(in, "none")); + return g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 && !(in && !strcmp(in, "none")) && !menu_cursor_pointer(); } void input_block(int on) { @@ -666,6 +666,7 @@ static int blocked(volatile int32_t *flag, volatile uint64_t *closed, const char void input_poll(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { poll_merged(buttons, lx, ly, rx, ry); + if (!g_block && !g_menu) menu_cursor_poll(buttons); const int32_t axes[4] = {*lx, *ly, *rx, *ry}; int text = blocked(&g_block, &g_block_closed, "text entry", *buttons, axes), menu = blocked(&g_menu, &g_menu_closed, "menu", *buttons, axes); diff --git a/runtime/input.h b/runtime/input.h index eea48179..ca4a0def 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -22,6 +22,10 @@ int input_direct_mouse_shake(int allow, int32_t out[2]); // Consume direct mouse counts, including one-shot MOUSE events from an input script. enabled=0 discards scripted // counts while leaving the real mouse for menu input. Live counts require cursor capture and foreground focus. void input_direct_mouse_sample(int enabled, int32_t *dx, int32_t *dy); +// menu_cursor.c: the pointer in the game's menus (1.4.3). poll: once per pad read, adds a click's A or B; pointer: a +// menu has the pointer, so the mouse camera's capture is released +void menu_cursor_poll(uint64_t *buttons); +int menu_cursor_pointer(void); // The same for host UI (the text entry): never blocked. input_block(1) while a text entry is open: input_poll then // reports nothing; input_block(0) when it closes: nothing until all buttons and stick directions are released. void input_poll_ui(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry); diff --git a/runtime/input_sdl.c b/runtime/input_sdl.c index 8ee747ad..7d337532 100644 --- a/runtime/input_sdl.c +++ b/runtime/input_sdl.c @@ -217,6 +217,8 @@ int ikb_foreground(void) { return focused != 0; } int ikb_down(int key) { return key > 0 && key < KEY_COUNT && key_held[key]; } int ikb_escape(void) { return esc_held != 0; } int ikb_captured(void) { return captured != 0; } +int ikb_pointer(float *nx, float *ny) { (void)nx, (void)ny; return 0; } // (menu_cursor.c: Windows only for now) +void ikb_pointer_set(float nx, float ny) { (void)nx, (void)ny; } void ikb_mouse_take(int32_t *dx, int32_t *dy) { *dx = plat_xchg32(&mouse_dx, 0); diff --git a/runtime/input_win.c b/runtime/input_win.c index 064f0169..d0058e8a 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -203,6 +203,25 @@ int ikb_escape(void) { return key_down(VK_ESCAPE); } int ikb_captured(void) { return cur_captured != 0; } +int ikb_pointer(float *nx, float *ny) { + HWND h = gfx_window_handle(); + RECT r; + POINT p; + if (!h || GetForegroundWindow() != h || !GetClientRect(h, &r) || r.right < 1 || r.bottom < 1 || !GetCursorPos(&p) || + !ScreenToClient(h, &p)) + return 0; + *nx = (float)p.x / (float)r.right, *ny = (float)p.y / (float)r.bottom; + return p.x >= 0 && p.y >= 0 && p.x < r.right && p.y < r.bottom; +} + +void ikb_pointer_set(float nx, float ny) { + HWND h = gfx_window_handle(); + RECT r; + if (!h || GetForegroundWindow() != h || !GetClientRect(h, &r)) return; + POINT p = {(LONG)(nx * r.right), (LONG)(ny * r.bottom)}; + if (ClientToScreen(h, &p)) SetCursorPos(p.x, p.y); +} + void ikb_mouse_take(int32_t *dx, int32_t *dy) { *dx = InterlockedExchange(&mouse_dx, 0); *dy = InterlockedExchange(&mouse_dy, 0); diff --git a/runtime/input_wsys.h b/runtime/input_wsys.h index 4f2fbb96..08ff1c1a 100644 --- a/runtime/input_wsys.h +++ b/runtime/input_wsys.h @@ -7,6 +7,9 @@ int ikb_foreground(void); // the game window is in front: keys count int ikb_down(int key); // key id (input_map.h KEY_*) held, mouse buttons included int ikb_escape(void); // Esc held (no key id: it belongs to the menu) +// The pointer in the client area, 0..1 from its top left; 0 when the window is not in front or the pointer is outside +int ikb_pointer(float *nx, float *ny); +void ikb_pointer_set(float nx, float ny); // moves the pointer there (the window in front only) int ikb_captured(void); // the cursor is captured for the mouse camera void ikb_mouse_take(int32_t *dx, int32_t *dy); // raw mouse motion since the last call, in counts void ikb_cursor_update(void); // what input_cursor_allowed says may have changed diff --git a/runtime/menu_cursor.c b/runtime/menu_cursor.c new file mode 100644 index 00000000..d2b0f897 --- /dev/null +++ b/runtime/menu_cursor.c @@ -0,0 +1,337 @@ +// The mouse pointer in TotK 1.4.3's own menus (the title, the inventory, the System tab, Options, shops, dialogue +// choices): while a menu screen has the game's focus box (eui's BoxCursor, the frame the d-pad moves), the Windows +// pointer is released from the mouse camera and shown, the item under it takes the focus as if the d-pad had moved +// there (highlight, sound and description included), a left click on an item is A on it and a right click is B. +// Nothing is drawn and no game code is called: at each pad read (ov_input.c GetNpadStates, the game thread) the open +// screens are read and the focus is moved with the three writes eui::BoxCursorMgr::moveBoxCursor makes, so the game's +// own BoxCursorControl moves the box at its next update. +// +// Adapted from Weeping of the Empire's engine/runtime/totk_menu_cursor.cpp @ df0586cdc5702ca19bbebfa59697e0b6980f6237 +// (https://github.com/daniel-madrid-07/weeping-of-the-empire), Copyright (c) 2026 daniel-madrid-07, MIT License: the +// focus read, the hit test, the stale-hit-pane fallback and the focus writes, which it does for 1.0.0. +// Placed in 1.4.3 by hand: +// - eui::ScreenMgr::sInstance: 1.0.0 main+0x4640010, 1.4.3 main+0x3acf960 (its createInstance main+0xc613d4 and +// constructor main+0xc61450 match 1.0.0's main+0x119c5cc / +0x119c648 instruction for instruction, the object +// 0x450 bytes with the same field stores, so its layout is 1.0.0's); +// - the screen's reserved node (+0x150) and request bits (+0x175, bit 2) and its draw target byte (+0x16c): the same +// stores at 1.4.3 main+0x87903c. +// The other fields are 1.0.0's; menu_cursor logs what it reads the first time a menu opens (TOTK_MENU_CURSOR_LOG=1: +// every screen change), so a field that moved shows as a menu with no nodes or boxes off the layout. +// +// TOTK_MENU_CURSOR (live, default 1): 0 leaves the menus to the keys and the pad. +#include +#include +#include +#include + +#include "cfg.h" +#include "gfx.h" +#include "host.h" +#include "input.h" +#include "input_map.h" +#include "input_wsys.h" +#include "layout.h" +#include "platform/platform.h" + +#define SCREEN_MGR_V143 0x3acf960ull // eui::ScreenMgr::sInstance +#define MAIN_SIZE 0x3adf000ull // main's image (code, data, bss) in 1.4.3 + +// eui::ScreenMgr, BoxCursorMgr, BoxCursorControl +enum { SM_BOX_MGR = 0x3f8, BM_CONTROLS = 0x28, CTL_ACTIVE = 0x58, CTL_RESERVED = 0x60 }; +// eui::Screen +enum { SCR_LAYOUT = 0x28, SCR_DRAW_INFO = 0x60, SCR_NODES = 0x80, SCR_RESERVED = 0x150, SCR_STATE = 0x16e, + SCR_REQUEST = 0x175, SCR_OPENED = 2 }; +// eui::BoxCursorNode, AnimButton +enum { NODE_BUTTON = 0x18, NODE_SCREEN = 0x20, BTN_LAYOUT = 0x20, BTN_FLAGS = 0x40, BTN_HIT_PANE = 0x58 }; +#define BTN_USABLE 0x20010u // both set: active and enabled (ButtonGroup::Update's test) +// nn::ui2d::DrawInfo: projection (4 x 4) and view (3 x 4); nn::ui2d::Layout: root pane, size, name +enum { DI_PROJ = 0x10, DI_VIEW = 0x50, LYT_ROOT = 0x18, LYT_SIZE = 0x28, LYT_NAME = 0x30 }; +// nn::ui2d::Pane +enum { PANE_PARENT = 0x18, PANE_SIZE = 0x50, PANE_FLAG = 0x58, PANE_ALPHA = 0x59, PANE_BASE = 0x5b, PANE_MTX = 0x70 }; + +static uint64_t main_lo, main_hi; + +static int ok_at(uint64_t p, uint64_t n) { + return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && plat_mem_readable(p, n); +} +static uint64_t rd64(uint64_t a) { uint64_t v; memcpy(&v, (const void *)(uintptr_t)a, 8); return v; } +static uint32_t rd32(uint64_t a) { uint32_t v; memcpy(&v, (const void *)(uintptr_t)a, 4); return v; } +static uint8_t rd8(uint64_t a) { return *(const volatile uint8_t *)(uintptr_t)a; } +static float rdf(uint64_t a) { float v; memcpy(&v, (const void *)(uintptr_t)a, 4); return v; } +// An object of the game: readable, its vtable in main's image +static int game_object(uint64_t p, uint64_t n) { + if (!ok_at(p, n)) return 0; + uint64_t vt = rd64(p); + return vt >= main_lo && vt < main_hi; +} + +// ---- panes (AnimButton::HitTest's rules: 1.0.0 main+0x222ea40, +0x223b090) +static int pane_shown(uint64_t pane) { + if (!ok_at(pane, PANE_MTX + 0x20)) return 0; + if (!(rd8(pane + PANE_FLAG) & 1) || !rd8(pane + PANE_ALPHA)) return 0; + int depth = 0; + for (uint64_t p = rd64(pane + PANE_PARENT); p; p = rd64(p + PANE_PARENT)) { + if (!ok_at(p, PANE_MTX) || ++depth > 64) return 0; + uint8_t f = rd8(p + PANE_FLAG); + if (!(f & 1) || (!rd8(p + PANE_ALPHA) && (f & 2))) return 0; + } + return 1; +} +// The pane's box in layout coordinates (origin at the centre, y up): centre and half size +static void pane_box(uint64_t pane, float *cx, float *cy, float *hw, float *hh) { + float w = rdf(pane + PANE_SIZE), h = rdf(pane + PANE_SIZE + 4); + float m00 = rdf(pane + PANE_MTX), m11 = rdf(pane + PANE_MTX + 0x14); + uint8_t base = rd8(pane + PANE_BASE), bh = base & 3, bv = base >> 2 & 3; + *hw = fabsf(w * m00) * 0.5f, *hh = fabsf(h * m11) * 0.5f; + *cx = rdf(pane + PANE_MTX + 0xc), *cy = rdf(pane + PANE_MTX + 0x1c); + if (bh == 1) *cx += *hw; else if (bh == 2) *cx -= *hw; // the translation is the left / right edge + if (bv == 1) *cy -= *hh; else if (bv == 2) *cy += *hh; // ... the top / bottom edge +} + +// The screen's draw transform, for a screen drawn away from its centred layout space (scale and translation only) +typedef struct { int ok; float v[12], p[16]; } Xf; +static Xf screen_xf(uint64_t scr) { + Xf x = {0}; + uint64_t di = rd64(scr + SCR_DRAW_INFO); + if (!ok_at(di, DI_VIEW + sizeof x.v)) return x; + memcpy(x.p, (const void *)(uintptr_t)(di + DI_PROJ), sizeof x.p); + memcpy(x.v, (const void *)(uintptr_t)(di + DI_VIEW), sizeof x.v); + for (int i = 0; i < 16; i++) if (!isfinite(x.p[i])) return x; + for (int i = 0; i < 12; i++) if (!isfinite(x.v[i])) return x; + x.ok = x.p[0] != 0 && x.p[5] != 0 && fabsf(x.p[1]) < 1e-5f && fabsf(x.p[4]) < 1e-5f && fabsf(x.v[1]) < 1e-4f && + fabsf(x.v[4]) < 1e-4f; + return x; +} +static int to_ndc(const Xf *t, float x, float y, float *nx, float *ny) { + float vx = t->v[0] * x + t->v[1] * y + t->v[3], vy = t->v[4] * x + t->v[5] * y + t->v[7], + vz = t->v[8] * x + t->v[9] * y + t->v[11]; + float cx = t->p[0] * vx + t->p[1] * vy + t->p[2] * vz + t->p[3]; + float cy = t->p[4] * vx + t->p[5] * vy + t->p[6] * vz + t->p[7]; + float cw = t->p[12] * vx + t->p[13] * vy + t->p[14] * vz + t->p[15]; + if (!(fabsf(cw) > 1e-6f)) return 0; + *nx = cx / cw, *ny = cy / cw; + return 1; +} +// The transform maps the layout as the plain layout test does (centre to 0,0, corner to 1,1): it adds nothing +static int xf_plain(const Xf *t, float lw, float lh) { + float ax, ay, bx, by; + if (!to_ndc(t, 0, 0, &ax, &ay) || !to_ndc(t, lw / 2, lh / 2, &bx, &by)) return 1; + return fabsf(ax) < 0.01f && fabsf(ay) < 0.01f && fabsf(bx - 1) < 0.01f && fabsf(by - 1) < 0.01f; +} + +// The pointer: layout coordinates (x, y) and device coordinates (nx, ny), and the transform when it is not plain +typedef struct { float x, y, nx, ny; const Xf *t; } Pt; +static int pane_hit(uint64_t pane, const Pt *p) { + if (!pane_shown(pane)) return 0; + float cx, cy, hw, hh; + pane_box(pane, &cx, &cy, &hw, &hh); + if (!p->t) return p->x >= cx - hw && p->x <= cx + hw && p->y >= cy - hh && p->y <= cy + hh; + float ax, ay, bx, by; + if (!to_ndc(p->t, cx - hw, cy - hh, &ax, &ay) || !to_ndc(p->t, cx + hw, cy + hh, &bx, &by)) return 0; + return p->nx >= fminf(ax, bx) && p->nx <= fmaxf(ax, bx) && p->ny >= fminf(ay, by) && p->ny <= fmaxf(ay, by); +} + +// ---- screens and nodes +typedef struct { uint64_t control, node, screen; } Focus; +static uint64_t screen_mgr(void) { + uint64_t slot = MOD_MAIN_BASE + SCREEN_MGR_V143, sm = rd64(slot); + return game_object(sm, SM_BOX_MGR + 8) ? sm : 0; +} +// The focused node of an open screen, from the draw targets' box cursors (all 0: none, as in gameplay) +static Focus focus_now(void) { + Focus none = {0}; + uint64_t sm = screen_mgr(), bm = sm ? rd64(sm + SM_BOX_MGR) : 0; + if (!ok_at(bm, BM_CONTROLS + 16)) return none; + for (int i = 0; i < 2; i++) { + uint64_t ctl = rd64(bm + BM_CONTROLS + 8ull * i); + if (!ok_at(ctl, CTL_RESERVED + 8)) continue; + uint64_t node = rd64(ctl + CTL_ACTIVE); + if (!ok_at(node, NODE_SCREEN + 8)) continue; + uint64_t scr = rd64(node + NODE_SCREEN); + if (!game_object(scr, SCR_REQUEST + 1) || rd8(scr + SCR_STATE) != SCR_OPENED) continue; + return (Focus){ctl, node, scr}; + } + return none; +} +static void layout_name(uint64_t scr, char *out, size_t n) { + snprintf(out, n, "?"); + uint64_t lyt = rd64(scr + SCR_LAYOUT), name = ok_at(lyt, LYT_NAME + 8) ? rd64(lyt + LYT_NAME) : 0; + if (name < 0x10000 || name >= (1ull << 47) || !plat_mem_readable(name, n)) return; + memcpy(out, (const void *)(uintptr_t)name, n - 1); + out[n - 1] = 0; + for (char *q = out; *q; q++) if (*q < 0x20 || *q > 0x7e) { *q = 0; break; } +} +static void layout_size(uint64_t scr, float *lw, float *lh) { + *lw = 1920, *lh = 1080; + uint64_t lyt = rd64(scr + SCR_LAYOUT); + if (!ok_at(lyt, LYT_SIZE + 8)) return; + float w = rdf(lyt + LYT_SIZE), h = rdf(lyt + LYT_SIZE + 4); + if (w >= 64 && w <= 8192 && h >= 64 && h <= 8192) *lw = w, *lh = h; +} +// The screen's focusable nodes: an sead OffsetList, head {prev, next} at +0x80, node-to-link offset at +0x94 +#define MAX_NODES 512 +static int nodes_of(uint64_t scr, uint64_t *node, uint64_t *btn) { + uint64_t head = scr + SCR_NODES; + int32_t off = (int32_t)rd32(head + 0x14), n = 0; + if (off < 0 || off > 0x40) return 0; + for (uint64_t link = rd64(head + 8); link != head && n < MAX_NODES; link = rd64(link + 8)) { + if (!ok_at(link, 16)) break; + uint64_t nd = link - (uint64_t)off; + if (!ok_at(nd, NODE_SCREEN + 8)) continue; + uint64_t b = rd64(nd + NODE_BUTTON); + if (!ok_at(b, BTN_HIT_PANE + 8)) continue; + node[n] = nd, btn[n] = b, n++; + } + return n; +} +static int usable(uint64_t btn) { return !(~rd32(btn + BTN_FLAGS) & BTN_USABLE); } +static uint64_t root_pane(uint64_t btn) { + uint64_t blyt = rd64(btn + BTN_LAYOUT); + return ok_at(blyt, LYT_ROOT + 8) ? rd64(blyt + LYT_ROOT) : 0; +} +// Every usable node's shown hit pane lies off the visible layout: the hit panes are stale (TotK never uses touch; +// the dialogue choices' sit far from where the choices are drawn), so the buttons' own parts panes stand for them +static int stale_hit_panes(int n, const uint64_t *btn, float lw, float lh, const Xf *t) { + int seen = 0; + for (int i = 0; i < n; i++) { + if (!usable(btn[i])) continue; + uint64_t pane = rd64(btn[i] + BTN_HIT_PANE); + if (!ok_at(pane, PANE_MTX + 0x20) || !pane_shown(pane)) continue; + float cx, cy, hw, hh, vx, vy; + pane_box(pane, &cx, &cy, &hw, &hh); + int in = t ? to_ndc(t, cx, cy, &vx, &vy) && fabsf(vx) <= 1 && fabsf(vy) <= 1 + : fabsf(cx) <= lw / 2 && fabsf(cy) <= lh / 2; + seen++; + if (in) return 0; + } + return seen > 0; +} +// The usable node under the pointer, 0: none +static uint64_t node_at(int n, const uint64_t *node, const uint64_t *btn, const Pt *p, float lw, float lh) { + if (!stale_hit_panes(n, btn, lw, lh, p->t)) { + for (int i = 0; i < n; i++) + if (usable(btn[i]) && pane_hit(rd64(btn[i] + BTN_HIT_PANE), p)) return node[i]; + return 0; + } + uint64_t found = 0; + float best = 1e30f; + for (int i = 0; i < n; i++) { // parts panes can be several items wide: the nearest centre + uint64_t pane = root_pane(btn[i]); + if (!usable(btn[i]) || !pane_hit(pane, p)) continue; + float cx, cy, hw, hh, d; + pane_box(pane, &cx, &cy, &hw, &hh); + d = (cx - p->x) * (cx - p->x) + (cy - p->y) * (cy - p->y); + if (d < best) best = d, found = node[i]; + } + return found; +} +// The focus to node: the writes moveBoxCursor makes. The request bit is a byte eui also writes, so it is raised +// again until the focus is there. +static void move_focus(const Focus *f, uint64_t node) { + memcpy((void *)(uintptr_t)(f->control + CTL_RESERVED), &node, 8); + memcpy((void *)(uintptr_t)(f->screen + SCR_RESERVED), &node, 8); + __atomic_fetch_or((uint8_t *)(uintptr_t)(f->screen + SCR_REQUEST), (uint8_t)4, __ATOMIC_SEQ_CST); +} + +// Screens with a focus box that are gameplay, not menus: the L ability wheel, the D-pad strips, Autobuild's list, the +// Purah Pad's camera and the map (the stick moves its own cursor there) +static int gameplay_screen(const char *nm) { + static const char *const names[] = {"ShortCutMainItem_00", "ShortCut_00", "AutoBuilder_00", "CameraPointer_00", + "AppMap_00"}; + for (size_t i = 0; i < sizeof names / sizeof *names; i++) + if (!strcmp(nm, names[i])) return 1; + return 0; +} + +static volatile int32_t g_pointer; // a menu has the pointer: input_cursor_allowed releases the capture + +int menu_cursor_pointer(void) { return __atomic_load_n(&g_pointer, __ATOMIC_RELAXED); } + +static int enabled(void) { + if (!RT_V143) return 0; + const char *v = cfg_get("TOTK_MENU_CURSOR"), *in = cfg_get("TOTK_INPUT"); + return !(v && !strcmp(v, "0")) && !(in && !strcmp(in, "none")); +} + +static void set_pointer(int on) { + if (plat_xchg32(&g_pointer, on) == on) return; + ikb_cursor_update(); + if (on) ikb_pointer_set(0.5f, 0.5f); // the pointer appears in the middle, not where the capture left it +} + +// The pointer on the game's picture, 0..1 from its top left (the present's fit, letterbox or pillarbox) +static int picture_pos(float *px, float *py) { + float nx, ny, r[4]; + if (!ikb_pointer(&nx, &ny) || !gfx_picture_rect(r) || r[2] <= 0 || r[3] <= 0) return 0; + *px = (nx - r[0]) / r[2], *py = (ny - r[1]) / r[3]; + return *px >= 0 && *px <= 1 && *py >= 0 && *py <= 1; +} + +static void log_screen(const Focus *f, const char *nm, int n, float lw, float lh, const Xf *t, int stale) { + static uint64_t last; + static int lines; + const char *e = cfg_get("TOTK_MENU_CURSOR_LOG"); + int verbose = e && !strcmp(e, "1"); + if (f->screen == last || (!verbose && lines >= 6)) return; + last = f->screen, lines++; + rt_log("menu_cursor: screen %s (layout %gx%g, %d nodes, %s hit panes, draw transform %s)", nm, lw, lh, n, + stale ? "stale" : "own", t ? "scaled" : "plain"); +} + +// Once per pad read on the game thread: the pointer's hover and clicks, as pad buttons (A, B) for this read +void menu_cursor_poll(uint64_t *buttons) { + static int inited, pending_a, held_l, held_r; + static uint64_t pending_node, pending_until, hold_until, hold_bit; + static float last_px = -1, last_py = -1; + if (!inited) { + inited = 1; + main_lo = MOD_MAIN_BASE, main_hi = MOD_MAIN_BASE + MAIN_SIZE; + if (RT_V143) rt_log("menu_cursor: on for 1.4.3 (TOTK_MENU_CURSOR=0 turns it off)"); + } + if (!enabled()) { set_pointer(0); return; } + Focus f = focus_now(); + char nm[48] = ""; + if (f.screen) layout_name(f.screen, nm, sizeof nm); + int menu = f.screen && !gameplay_screen(nm) && ikb_foreground(); + set_pointer(menu); + int l = ikb_down(KEY_MOUSE_L), r = ikb_down(KEY_MOUSE_R); + int click_l = l && !held_l, click_r = r && !held_r; + held_l = l, held_r = r; + uint64_t now = plat_now_ms(); + if (now < hold_until) { // a click's button, held for 60 ms so the game sees one press whatever its read rate + *buttons |= hold_bit; + return; + } + if (!menu) { pending_a = 0; last_px = last_py = -1; return; } + if (pending_a) { // the focus asked for by a click: A once it is there + if (rd64(f.control + CTL_ACTIVE) == pending_node) { + pending_a = 0, hold_until = now + 60, hold_bit = 1ull << B_A; + *buttons |= hold_bit; + return; + } + if (now > pending_until) pending_a = 0; + else move_focus(&f, pending_node); + return; + } + if (click_r) { hold_until = now + 60, hold_bit = 1ull << B_B; *buttons |= hold_bit; return; } + float px, py; + if (!picture_pos(&px, &py)) return; + int moved = fabsf(px - last_px) > 0.001f || fabsf(py - last_py) > 0.001f; + if (!moved && !click_l) return; + last_px = px, last_py = py; + float lw, lh; + layout_size(f.screen, &lw, &lh); + Xf xf = screen_xf(f.screen); + Pt p = {(px - 0.5f) * lw, (0.5f - py) * lh, px * 2 - 1, 1 - py * 2, NULL}; + if (xf.ok && !xf_plain(&xf, lw, lh)) p.t = &xf; + static uint64_t node[MAX_NODES], btn[MAX_NODES]; + int n = nodes_of(f.screen, node, btn); + log_screen(&f, nm, n, lw, lh, p.t, n && stale_hit_panes(n, btn, lw, lh, p.t)); + uint64_t at = n ? node_at(n, node, btn, &p, lw, lh) : 0; + if (!at) return; + if (at != f.node) move_focus(&f, at); + if (click_l) { + if (at == f.node) { hold_until = now + 60, hold_bit = 1ull << B_A; *buttons |= hold_bit; } + else pending_a = 1, pending_node = at, pending_until = now + 300; + } +} diff --git a/totk.ini.example b/totk.ini.example index 3ada4ddc..c3cbdcfb 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -302,6 +302,10 @@ # raw displacement with equal horizontal/vertical sensitivity and bypasses stick response curves. Other game versions # keep stick emulation. The Controls tab changes this while the game runs. # mouse_mode = stick +# The mouse in the game's menus, 1.4.3 only (title, inventory, System tab, Options, shops, dialogue choices): while a +# menu has the game's focus box the pointer shows, the item under it takes the focus, a left click is A on it and a +# right click is B. 0 leaves the menus to the keys and the pad. Changes while the game runs. +# menu_cursor = 1 # Mouse camera sensitivity (0 to 10; 0 = no mouse camera and no cursor capture). # mouse_sens = 1 # The keyboard's sticks are all or nothing, so the game only sees a full tilt from them; while this key is held the