From 050563687b031f58a9d75a0215494c9bad5897bf Mon Sep 17 00:00:00 2001
From: walterwhite69420 <351716639+walterwhite69420@users.noreply.github.com>
Date: Tue, 3 Nov 2026 06:49:15 +0700
Subject: [PATCH 01/10] Add experimental direct mouse camera input for game
1.4.3
---
docs/controls.md | 32 ++-
recomp/patches-v143.txt | 10 +
runtime/cfg.c | 5 +-
runtime/input.c | 85 ++++++--
runtime/input.h | 16 ++
runtime/mouse_direct.c | 383 ++++++++++++++++++++++++++++++++++++
runtime/ui/menu.cpp | 2 +
runtime/ui_text.c | 8 +
runtime/ui_text.h | 1 +
tests/mouse_direct_check.py | 311 +++++++++++++++++++++++++++++
tools/build_game.py | 4 +-
totk.ini.example | 4 +
12 files changed, 845 insertions(+), 16 deletions(-)
create mode 100644 runtime/mouse_direct.c
create mode 100644 tests/mouse_direct_check.py
diff --git a/docs/controls.md b/docs/controls.md
index 12c4d7c0..ad2de67c 100644
--- a/docs/controls.md
+++ b/docs/controls.md
@@ -90,7 +90,31 @@ bind_Y = pad:north, key:mouse_l
## Mouse camera
-Mouse motion (raw input) becomes right-stick deflection. Each presented frame takes the counts moved since the last frame, divides by the frame time and sets the deflection to `counts per second * TOTK_MOUSE_SENS / 3000` of full, held for that frame, so the turn rate does not depend on the frame rate. This is not mouselook: the game's stick curve and dead zone still apply, so very slow movements do nothing and a fast flick saturates. The mouse deflection competes with the gamepad and arrow keys by the larger-deflection rule.
+`mouse_mode = direct` enables experimental mouse hooks on game 1.4.3. The free-orbit camera receives raw-count
+angular increments at its final spherical-angle output: 0.08 degrees per count on both axes at sensitivity 1, with
+no time multiplier, curve or carry into later frames. The controller's persistent pitch/yaw move with the output.
+Mouse input is removed from its stick path first, so the camera's later vertical signed-square curve and velocity
+filters cannot process it a second time. Other camera modes receive input after the radial dead zone, unit-circle
+clamp and stick response exponent; Ultrahand has a separate hook after its manipulation input's power curves.
+The game's camera inversion and gamepad response remain in effect. `mouse_sens` scales raw counts; its scale
+differs from the stick-emulation mode. Start at 1.
+The Controls tab can switch between *Stick emulation* (the default, `mouse_mode = stick`) and *Direct (experimental)*.
+The orbit camera keeps its desired pitch at the final angular output, including stationary frames, so a later game
+filter cannot recenter it. The persistent filter state accounts for the camera's current vertical offset instead of
+accumulating corrections. A gamepad vertical input or the game's lock-on/override releases that mouse target.
+While L is held, mouse counts move the ability wheel's virtual cursor, which stays where it was when the mouse stops.
+The horizontal quick lists (D-pad up/left/right: materials, shields and weapons) use separate 32 ms right-stick steps,
+with at least 32 ms of neutral between them. At sensitivity 1, 32 horizontal counts request a step; a large flick
+cannot queue continued scrolling after the mouse stops. Vertical movement is ignored in these lists. Switching or
+releasing a selector resets its cursor/step state without sending the last movement to the camera. Selector input is
+not consumed by the direct camera or Ultrahand hooks.
+With a held object, three substantial reversals on one mouse axis, with at most 400 ms between strokes, begin a
+short stick-shake gesture for the game's own detach detector. Those gesture pulses are excluded from camera and object movement input.
+The mapping below is still used when the camera-input hook is unavailable or the game disables camera input.
+This hook needs in-game checks of aiming, Ultrahand and menu transitions before it can be considered complete mouse
+support. Games 1.1.0 and 1.2.1 have no hook and keep stick emulation.
+
+In stick-emulation mode, mouse motion (raw input) becomes right-stick deflection. Each presented frame takes the counts moved since the last frame, divides by the frame time and sets the deflection to `counts per second * TOTK_MOUSE_SENS / 3000` of full, held for that frame, so the turn rate does not depend on the frame rate. This is not mouselook: the game's stick curve and dead zone still apply, so very slow movements do nothing and a fast flick saturates. The mouse deflection competes with the gamepad and arrow keys by the larger-deflection rule.
`TOTK_MOUSE_SENS` (0 to 10, default 1; higher turns faster, 0 turns the mouse camera and the cursor capture off). While the window is the foreground window the cursor is hidden and confined to the client area. It is released on focus loss, minimise, while F11 changes the window, during a size or move loop and while a text entry is open. Esc releases it by hand: the cursor shows, the mouse camera and mouse buttons stop, and the window can be moved, minimised or closed. A mouse click inside the client area captures it again; that click does not reach the game. Activating the window by a click on its title bar or border does not capture, so it can be dragged or closed right away; Alt+Tab or a click in the client area captures as before. `TOTK_INPUT=none` disables the capture too.
@@ -111,14 +135,20 @@ Bridges that output a ViGEm DualShock 4 (ProCon2Windows, connectyourpro as teste
```
[hold_frames] # A B X Y L R ZL ZR PLUS MINUS LEFT UP RIGHT DOWN LCLICK RCLICK; hold defaults to 4
LSTICK|RSTICK [hold_frames] # -32767..32767, up is +y
+ MOUSE # direct-mode raw counts, once; down is +dy
@ [hold_ms] # timed on the world clock
@ LSTICK|RSTICK [hold_ms]
+@ MOUSE
```
` ` counts 60 Hz ticks of game time since the first frame (presented frames at 60 fps), so the title menus get the presses at the same game time at any frame limit. `@` counts milliseconds of the world clock: game time stepped since the end of the first save load (the player leaves the title screen's origin), so a timed event lands at the same game time at any frame rate and load length. A later load, such as fast travel, still shifts what follows. In a script with timed lines the untimed ones stop when the world clock starts, as the load ends at another tick in each run. Each event logs once (`input: script frame N tick T: A`). `tools/input/load_save.txt` loads the first save from the title screen.
`TOTK_INPUT_RECORD=` (`input_record` in `totk.ini`, or Input recording in the menu) writes the merged input in the same format, one line per button press and per stick segment: events before the world clock starts by tick, the rest timed; a button or stick held when the world clock starts is cut there and goes on as a timed event. The file replays as a script, which makes it the repro to attach to a bug report.
+In direct mouse mode the recording also writes one `MOUSE` event per consumed raw displacement. These are one-shot
+displacements rather than held stick segments. They replay without cursor capture, including with `input = none`;
+the game must be accepting camera input when the event reaches its camera update.
+
`TOTK_TEST_PAD=,,,` attaches an SDL virtual gamepad at start whose sticks rest at these raw SDL axis values (-32768 to 32767, down is +y), so a drifting or held stick can be tested without a controller: the values go through the gamepad mapping and `TOTK_STICK_DEADZONE` like a real pad's, and `TOTK_INPUT_RECORD` shows what reached the game.
## RenderDoc capture (blocked)
diff --git a/recomp/patches-v143.txt b/recomp/patches-v143.txt
index 9c85fb7f..c4b3bffd 100644
--- a/recomp/patches-v143.txt
+++ b/recomp/patches-v143.txt
@@ -103,6 +103,16 @@ set fov TOTK_FOV_PATCH 0
site fov main 628c3c bd413e60 f81f0fea @page:x10 bd413e60 bd403d48 1e280800 f84107ea # ldr s0, [x19, #0x13c] (camera update)
site fov main b4342c bd4066c0 f81f0fea @page:x10 bd4066c0 bd403d48 1e280800 f84107ea # ldr s0, [x22, #0x64] (camera setup)
+# The camera input update, after its stick dead zone, unit-radius clamp and StickExponent. The host callback
+# performs the displaced store and adds raw mouse counts to s9/s10 (runtime/mouse_direct.c camera_input).
+# Its address is page +0xfd0; the page is main+0x3adf000 on this verified build.
+set mouse_direct TOTK_MOUSE_DIRECT_PATCH 1
+site mouse_direct main 874cb4 bd042289 94c9acc7
+site mouse_direct main 874d7c 6d40a3e9 94c9ac91
+site mouse_direct main 18e444 f9400408 94e546db
+site mouse_direct main 18f108 bd4047ec 94e543a6
+site mouse_direct main 1d59460 b9004be8 94761acc
+
# The game's zstd decoder (BSD zstd, statically linked into main) through the host's libzstd (runtime/ov_zstd.c):
# every call of ZSTD_decompressMultiFrame (main+0x3a1f0: dctx, dst, capacity, src, size, dict, dict size, ddict) and of
# its wrapper ZSTD_decompress_usingDDict (main+0x3a1e0, which tail-branches to it) goes to a host function on the patch
diff --git a/runtime/cfg.c b/runtime/cfg.c
index de418f3c..d09a2366 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_SENS", "MOVIE_HOST", "MOVIE_PACE",
+ "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_SENS", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "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"},
@@ -147,6 +147,7 @@ static const CfgMeta metas[] = {
{"PROMPTS", "nintendo", "nintendo|xbox", RESTART},
{"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},
{"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE},
{"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE},
diff --git a/runtime/input.c b/runtime/input.c
index e51a1e54..581054d5 100644
--- a/runtime/input.c
+++ b/runtime/input.c
@@ -26,6 +26,7 @@ enum { STICK_MAX = 32767, STICK_DIR = 16384, TRIGGER_ON = 16384, RESCAN_MS = 100
static PlatMutex lock = PLAT_MUTEX_INIT;
static int state; // 0 not started, 1 running, -1 disabled or SDL failed
+static volatile int32_t g_block, g_menu; // text entry / host menu: 0 off, 1 open, 2 waiting for release
static SDL_Gamepad *pad;
static uint64_t next_scan;
static int pad_rumble; // the open gamepad has rumble motors
@@ -66,6 +67,7 @@ static const struct { const char *name; int bit; } script_buttons[] = {
static ScriptEv *script_ev;
static int script_n, script_state; // 0 not loaded, 1 loaded
static int script_mixed; // the script has timed lines: its untimed ones end where the world clock starts
+static int32_t script_mouse[2]; // one-shot raw counts, consumed by the camera hook; under lock
static void script_load(void) {
script_state = 1;
@@ -88,10 +90,11 @@ static void script_load(void) {
ScriptEv e = {.at = (uint64_t)(at * unit + 0.5), .hold = timed ? 66667 : 4, .bit = -1, .timed = timed};
long a, b;
double c;
- if (!strcmp(name, "LSTICK") || !strcmp(name, "RSTICK")) {
+ if (!strcmp(name, "MOUSE") || !strcmp(name, "LSTICK") || !strcmp(name, "RSTICK")) {
int n = sscanf(line + off, "%ld %ld %lf", &a, &b, &c);
if (n < 2) { rt_log("input: script line %d: %s needs x y [hold]", ln, name); continue; }
- e.stick = name[0] == 'L' ? 1 : 2; e.x = (int32_t)a; e.y = (int32_t)b;
+ e.stick = name[0] == 'M' ? 3 : name[0] == 'L' ? 1 : 2; e.x = (int32_t)a; e.y = (int32_t)b;
+ if (e.stick == 3) e.hold = 1; // a displacement, never a held speed
if (n >= 3 && c > 0) e.hold = (uint64_t)(c * unit + 0.5);
snprintf(e.text, sizeof e.text, "%s %d %d", name, e.x, e.y);
} else {
@@ -123,12 +126,17 @@ static void script_apply(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
if (e->timed ? w < 0 : script_mixed && w >= 0) continue;
uint64_t now = e->timed ? (uint64_t)w : tick;
if (now < e->at || (now >= e->at + e->hold && (e->logged || !e->timed))) continue;
+ if (e->stick == 3 && e->logged) continue;
if (!e->logged) {
e->logged = 1;
if (e->timed) rt_log("input: script frame %llu at %.1f ms: %s", (unsigned long long)fr, w / 1000.0, e->text);
else rt_log("input: script frame %llu tick %llu: %s", (unsigned long long)fr, (unsigned long long)tick, e->text);
}
- if (e->stick) { int32_t *s = e->stick == 1 ? sl : sr; s[0] = e->x; s[1] = e->y; }
+ if (e->stick == 3) {
+ int64_t x = (int64_t)script_mouse[0] + e->x, y = (int64_t)script_mouse[1] + e->y;
+ script_mouse[0] = (int32_t)(x < INT32_MIN ? INT32_MIN : x > INT32_MAX ? INT32_MAX : x);
+ script_mouse[1] = (int32_t)(y < INT32_MIN ? INT32_MIN : y > INT32_MAX ? INT32_MAX : y);
+ } else if (e->stick) { int32_t *s = e->stick == 1 ? sl : sr; s[0] = e->x; s[1] = e->y; }
else *b |= 1ull << e->bit;
}
}
@@ -223,6 +231,30 @@ static void rec_apply(uint64_t b, const int32_t sl[2], const int32_t sr[2]) {
if (wrote) fflush(rec);
}
+void input_direct_mouse_sample(int enabled, int32_t *dx, int32_t *dy) {
+ plat_mutex_lock(&lock);
+ enabled = enabled && !g_block && !g_menu;
+ *dx = script_mouse[0]; *dy = script_mouse[1];
+ script_mouse[0] = script_mouse[1] = 0;
+ if (!enabled) { *dx = *dy = 0; }
+ else if (ikb_captured() && ikb_foreground() && input_cursor_allowed()) {
+ int32_t x, y;
+ ikb_mouse_take(&x, &y);
+ int64_t sx = (int64_t)*dx + x, sy = (int64_t)*dy + y;
+ *dx = (int32_t)(sx < INT32_MIN ? INT32_MIN : sx > INT32_MAX ? INT32_MAX : sx);
+ *dy = (int32_t)(sy < INT32_MIN ? INT32_MIN : sy > INT32_MAX ? INT32_MAX : sy);
+ }
+ if (enabled && (*dx || *dy) && rec_state == 1) {
+ char what[80];
+ snprintf(what, sizeof what, "MOUSE %d %d", *dx, *dy);
+ int64_t w = frame_step_world_us();
+ uint64_t tick = frame_step_ticks();
+ rec_line(tick, w, tick + 1, w, what);
+ fflush(rec);
+ }
+ plat_mutex_unlock(&lock);
+}
+
static float mouse_sens = -1; // TOTK_MOUSE_SENS, 0 = mouse camera off; live (cfg_version)
static unsigned mouse_sens_seen;
@@ -282,15 +314,41 @@ static void walk_get(void) {
}
}
-// The mouse camera as a right-stick sample, held for the whole presented frame. Under `lock`. The sample is divided by
-// the camera speed factor the game runs with (camera_speed.c, cam_speed_applied), which multiplies every right-stick
-// turn rate, so TOTK_MOUSE_SENS alone sets the mouse's speed; below 1 the sample stays within the stick's range, so the
-// mouse's top speed drops with the factor.
-static void mouse_camera(int32_t out[2]) {
+static int mouse_selector;
+int input_direct_mouse_selector_active(void) { return __atomic_load_n(&mouse_selector, __ATOMIC_RELAXED) != 0; }
+
+// Selector 1 is a radial wheel, 2..4 are distinct horizontal quick lists; only a wheel retains a virtual cursor.
+// Under `lock`.
+static void mouse_camera(int32_t out[2], int selector) {
static uint64_t frame = ~0ull, t0;
static int32_t held[2];
+ static int was_selector;
+ static float cursor[2];
+ static InputMouseRow row;
uint64_t now = plat_now_ns();
int32_t dx, dy;
+ if (!input_direct_mouse_enabled() || !ikb_captured()) selector = 0;
+ __atomic_store_n(&mouse_selector, selector, __ATOMIC_RELAXED);
+ if (selector) {
+ if (was_selector != selector) cursor[0] = cursor[1] = 0, row = (InputMouseRow){0};
+ was_selector = selector;
+ ikb_mouse_take(&dx, &dy);
+ if (selector == 1) input_direct_mouse_radial(dx, dy, mouse_sens_get(), cursor, out);
+ else input_direct_mouse_row(dx, mouse_sens_get(), now, &row, out);
+ held[0] = held[1] = 0; frame = ~0ull;
+ return;
+ }
+ if (was_selector) {
+ was_selector = 0; cursor[0] = cursor[1] = 0; row = (InputMouseRow){0};
+ ikb_mouse_take(&dx, &dy); // release the wheel without sending its last movement to the camera
+ held[0] = held[1] = out[0] = out[1] = 0; frame = ~0ull;
+ return;
+ }
+ if (input_direct_mouse_active()) {
+ held[0] = held[1] = out[0] = out[1] = 0;
+ frame = ~0ull;
+ return; // the camera update takes the raw counts itself, once, outside the HID stick path
+ }
if (!ikb_captured()) {
ikb_mouse_take(&dx, &dy);
held[0] = held[1] = 0;
@@ -530,11 +588,18 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
input_map_stick(down, 1, k);
if (walk) k[0] = (int32_t)(k[0] * walk_scale), k[1] = (int32_t)(k[1] * walk_scale);
pick(&sr[0], &sr[1], k[0], k[1]);
- mouse_camera(k);
+ int selector = b & (1ull << B_L) ? 1 : b & (1ull << B_UP) ? 2 :
+ b & (1ull << B_LEFT) ? 3 : b & (1ull << B_RIGHT) ? 4 : 0;
+ mouse_camera(k, selector);
pick(&sr[0], &sr[1], k[0], k[1]);
}
}
script_apply(&b, sl, sr);
+ int32_t shake[2];
+ int wheel = (b & ((1ull << B_L) | (1ull << B_UP) | (1ull << B_LEFT) | (1ull << B_RIGHT))) != 0;
+ if (input_direct_mouse_shake(!wheel && !sr[0] && !sr[1] && !g_menu && !g_block, shake)) {
+ sr[0] = shake[0]; sr[1] = shake[1];
+ }
rec_apply(b, sl, sr);
// Minus and Plus held together (gamepad or keys) open or close the menu and never reach the game
static int combo_held;
@@ -554,8 +619,6 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
// Text entry (swkbd_*.c): while it is open the game reads no input, as behind the keyboard applet; after it closes
// the game reads none until every button and stick direction is released, so the press that closed it stays there.
// g_block: 0 off, 1 text entry open, 2 closed, waiting for the release. The cursor is released while it is open.
-static volatile int32_t g_block;
-static volatile int32_t g_menu; // the in-game menu, the same way (input_menu)
// When g_block and g_menu last went to 2 (plat_now_ns). The wait for the release ends after RELEASE_WAIT_NS even with
// something held: an input that never reads as released (a button another program holds down, a key whose release
// went to another window, a stick that rests outside its dead zone) would otherwise keep the game deaf until restart
diff --git a/runtime/input.h b/runtime/input.h
index 36ca52a0..eea48179 100644
--- a/runtime/input.h
+++ b/runtime/input.h
@@ -6,6 +6,22 @@
// Down=15) and the stick directions (StickLLeft..Down 16-19, StickRLeft..Down 20-23, derived from the sticks).
// Sticks are -32767..32767, up is +y. TOTK_INPUT=none reports no input. Callable from any thread.
void input_poll(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry);
+// Direct mouse input is consumed by the game's camera-input update, after its stick response. While that update
+// accepts camera input, the HID right stick contains only the controller and keyboard. Selectors route mouse input
+// separately: a persistent radial cursor for the ability wheel, discrete steps for horizontal quick lists.
+int input_direct_mouse_active(void);
+int input_direct_mouse_enabled(void);
+// A radial selector uses a persistent virtual cursor while its opening button is held.
+void input_direct_mouse_radial(int32_t dx, int32_t dy, float sensitivity, float cursor[2], int32_t out[2]);
+// Horizontal quick selectors receive short, separated steps instead of a latched radial-cursor direction.
+typedef struct { float distance; uint64_t pulse_until, neutral_until; int direction; } InputMouseRow;
+void input_direct_mouse_row(int32_t dx, float sensitivity, uint64_t now, InputMouseRow *row, int32_t out[2]);
+int input_direct_mouse_selector_active(void);
+// A deliberate mouse shake while manipulating an object feeds a short gesture to the game's native shake detector.
+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);
// 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/mouse_direct.c b/runtime/mouse_direct.c
new file mode 100644
index 00000000..69a6ff1e
--- /dev/null
+++ b/runtime/mouse_direct.c
@@ -0,0 +1,383 @@
+// Direct mouse input for TotK 1.4.3. The camera controller's input update (main+0x874c1c) turns HID stick floats
+// into a radial dead zone, clamps the radius to one and applies StickExponent. The mouse enters after all three,
+// at main+0x874cb4, before the game's X/Y inversion and input-activity flags. It never becomes a HID stick value.
+// Free orbit removes that sample again before the camera's additional vertical square/velocity filters and adds
+// raw-count angular deltas at its spherical-angle output instead. Ultrahand has a separate post-curve hook.
+// The ordinary stick's result stays, so a controller still uses the game's own response.
+//
+// This is deliberately not a render-camera override: the game remains responsible for aiming, camera collision,
+// lock-on and scripted cameras. Which of its other modes consume this camera input needs in-game verification.
+#include
+#include
+#include
+#include
+
+#include "cfg.h"
+#include "host.h"
+#include "input.h"
+#include "input_wsys.h"
+#include "platform/platform.h"
+
+#if __has_include("patches.h")
+#include "patches.h"
+#endif
+
+static int direct_mode(void) {
+ const char *mode = cfg_get("TOTK_MOUSE_MODE");
+ return mode && !strcmp(mode, "direct") && rt_patch_enabled(rt_patch_set("mouse_direct"));
+}
+
+static uint64_t last_update;
+static uint64_t hand_update;
+static uint64_t shake_started, shake_until;
+static int shake_injected;
+static int accepts_mouse;
+
+static int hand_active(uint64_t now) {
+ uint64_t last = __atomic_load_n(&hand_update, __ATOMIC_ACQUIRE);
+ return last && now - last < 150000000ull;
+}
+
+int input_direct_mouse_active(void) {
+ uint64_t last = __atomic_load_n(&last_update, __ATOMIC_ACQUIRE);
+ return direct_mode() && (__atomic_load_n(&accepts_mouse, __ATOMIC_RELAXED) || hand_active(plat_now_ns())) && last &&
+ plat_now_ns() - last < 150000000ull;
+}
+
+int input_direct_mouse_enabled(void) { return direct_mode(); }
+
+void input_direct_mouse_radial(int32_t dx, int32_t dy, float sensitivity, float cursor[2], int32_t out[2]) {
+ if (!(sensitivity > 0)) { cursor[0] = cursor[1] = 0; out[0] = out[1] = 0; return; }
+ cursor[0] += dx * (double)sensitivity / 200;
+ cursor[1] -= dy * (double)sensitivity / 200;
+ double length = hypot(cursor[0], cursor[1]);
+ if (length > 1) { cursor[0] /= length; cursor[1] /= length; }
+ out[0] = (int32_t)lrint(cursor[0] * 32767);
+ out[1] = (int32_t)lrint(cursor[1] * 32767);
+}
+
+void input_direct_mouse_row(int32_t dx, float sensitivity, uint64_t now, InputMouseRow *row, int32_t out[2]) {
+ const float step_counts = 32;
+ out[0] = out[1] = 0;
+ if (!(sensitivity > 0)) { *row = (InputMouseRow){0}; return; }
+ if (dx) {
+ // Reversing direction cancels any remainder from the previous drag. Bound a fast flick to one pending step;
+ // a stopped mouse never starts another pulse, regardless of how often HID is polled.
+ if ((dx < 0 && row->distance > 0) || (dx > 0 && row->distance < 0)) row->distance = 0;
+ row->distance = fmaxf(-2 * step_counts, fminf(2 * step_counts, row->distance + dx * sensitivity));
+ }
+ if (row->direction) {
+ if (now < row->pulse_until && (!dx || (dx > 0 ? 1 : -1) == row->direction)) {
+ out[0] = row->direction * 32767;
+ return;
+ }
+ row->direction = 0;
+ row->neutral_until = now + 32000000ull;
+ return; // publish neutral even when a slow frame skipped the pulse's deadline
+ }
+ if (!dx || now < row->neutral_until || fabsf(row->distance) < step_counts) return;
+ row->direction = row->distance > 0 ? 1 : -1;
+ row->distance = fmodf(row->distance, step_counts);
+ row->pulse_until = now + 32000000ull;
+ out[0] = row->direction * 32767;
+}
+
+int input_direct_mouse_shake(int allow, int32_t out[2]) {
+ uint64_t now = plat_now_ns(), until = __atomic_load_n(&shake_until, __ATOMIC_ACQUIRE);
+ uint64_t start = __atomic_load_n(&shake_started, __ATOMIC_RELAXED);
+ int ready = allow && direct_mode() && hand_active(now) && ikb_captured() && ikb_foreground() && input_cursor_allowed();
+ if (!ready) __atomic_store_n(&shake_until, 0, __ATOMIC_RELEASE);
+ int active = ready && now >= start && now < until;
+ out[0] = active ? ((now - start) / 40000000ull & 1 ? -32767 : 32767) : 0;
+ out[1] = 0;
+ __atomic_store_n(&shake_injected, active, __ATOMIC_RELAXED);
+ return active;
+}
+
+#ifdef RT_PATCH_PAGE
+#define HOST_MOUSE_DIRECT (RT_PATCH_PAGE + 0xfd0)
+#define HOST_MOUSE_ACTIVITY (RT_PATCH_PAGE + 0xfc0)
+#define HOST_MOUSE_ORBIT_BEGIN (RT_PATCH_PAGE + 0xfb0)
+#define HOST_MOUSE_ORBIT_ANGLES (RT_PATCH_PAGE + 0xfa0)
+#define HOST_MOUSE_HAND (RT_PATCH_PAGE + 0xf90)
+static _Thread_local uint64_t mouse_input_object;
+
+typedef struct {
+ uint64_t object, generation, when;
+ float pad[2], motion[2], angle[2]; // post-inversion controller input, mouse input, mouse yaw/pitch in degrees
+ int camera_enabled;
+} MouseSample;
+static MouseSample sample;
+static PlatMutex sample_lock = PLAT_MUTEX_INIT;
+static _Thread_local struct { uint64_t object; MouseSample mouse; } orbit;
+typedef struct {
+ uint64_t object, generation, input;
+ float pitch, original_target;
+ int hold_pitch, target_saved;
+} OrbitTrack;
+static OrbitTrack orbit_seen[16];
+
+// sample_lock held. Camera objects can coexist while the game blends between their outputs.
+static OrbitTrack *orbit_track(uint64_t object) {
+ int oldest = 0;
+ for (int i = 0; i < 16; i++) {
+ if (orbit_seen[i].object == object) return &orbit_seen[i];
+ if (orbit_seen[i].generation < orbit_seen[oldest].generation) oldest = i;
+ }
+ orbit_seen[oldest] = (OrbitTrack){.object = object};
+ return &orbit_seen[oldest];
+}
+
+static int orbit_sample_once(uint64_t object, uint64_t generation) {
+ plat_mutex_lock(&sample_lock);
+ OrbitTrack *track = orbit_track(object);
+ int fresh = track->generation != generation;
+ if (fresh) track->generation = generation;
+ plat_mutex_unlock(&sample_lock);
+ return fresh;
+}
+
+static float guest_float(uint64_t at) { float f; memcpy(&f, (void *)(uintptr_t)at, sizeof f); return f; }
+static void guest_float_store(uint64_t at, float f) { memcpy((void *)(uintptr_t)at, &f, sizeof f); }
+static uint64_t guest_pointer(uint64_t at) { uint64_t p; memcpy(&p, (void *)(uintptr_t)at, sizeof p); return p; }
+static MouseSample sample_read(void) {
+ plat_mutex_lock(&sample_lock);
+ MouseSample m = sample;
+ plat_mutex_unlock(&sample_lock);
+ return m;
+}
+static float wrap_degrees(float a) { return remainderf(a, 360.0f); }
+
+// main+0x191d78 builds the pitch target from +0x15c and +0x11c; the orbit update filters +0x108 toward it and adds
+// (1 - +0x88) * +0x10c at output. Updating only +0x108 makes the camera spring back to the unchanged target.
+static float orbit_pitch_offset(uint64_t object) {
+ return (1 - guest_float(object + 0x88)) * guest_float(object + 0x10c);
+}
+
+static void orbit_pitch_target(uint64_t object, float pitch) {
+ guest_float_store(object + 0x15c, pitch - orbit_pitch_offset(object) - guest_float(object + 0x11c));
+}
+
+static void orbit_pitch_release(OrbitTrack *track) {
+ if (track->target_saved) guest_float_store(track->object + 0x15c, track->original_target);
+ track->hold_pitch = track->target_saved = 0;
+}
+
+static void mouse_shake_feed(int32_t dx, int32_t dy, uint64_t now) {
+ static struct { int sign, confirmed, reversals; double distance; uint64_t last; } stroke[2];
+ static uint64_t cooldown;
+ if (!hand_active(now)) { memset(stroke, 0, sizeof stroke); return; }
+ if (now < cooldown) return;
+ int32_t delta[2] = {dx, dy};
+ for (int i = 0; i < 2; i++) {
+ if (!delta[i]) continue;
+ if (stroke[i].last && now - stroke[i].last > 400000000ull) memset(&stroke[i], 0, sizeof stroke[i]);
+ int sign = delta[i] > 0 ? 1 : -1;
+ if (sign != stroke[i].sign) stroke[i].sign = sign, stroke[i].distance = 0;
+ stroke[i].distance += fabs((double)delta[i]);
+ if (stroke[i].distance < 24) continue;
+ if (stroke[i].confirmed && sign != stroke[i].confirmed) stroke[i].reversals++;
+ stroke[i].confirmed = sign;
+ stroke[i].last = now;
+ stroke[i].distance = 0;
+ if (stroke[i].reversals >= 3) {
+ __atomic_store_n(&shake_started, now, __ATOMIC_RELAXED);
+ __atomic_store_n(&shake_until, now + 320000000ull, __ATOMIC_RELEASE);
+ cooldown = now + 700000000ull;
+ memset(stroke, 0, sizeof stroke);
+ rt_log("mouse: detach shake gesture sent to the game's stick-shake detector");
+ return;
+ }
+ }
+}
+
+// A host call at an instruction site, not an AAPCS64 function replacement. Preserve every guest register except
+// s9/s10, which carry the processed camera input, and perform the displaced store of s9 at [x20 + 0x420].
+static void camera_input(Cpu *c) {
+ mouse_input_object = 0;
+ uint32_t flags = (uint32_t)c->x[21];
+ int accept = !(flags & 1) && !input_direct_mouse_selector_active();
+ __atomic_store_n(&accepts_mouse, accept, __ATOMIC_RELAXED);
+ __atomic_store_n(&last_update, plat_now_ns(), __ATOMIC_RELEASE);
+ if (direct_mode() && __atomic_load_n(&shake_injected, __ATOMIC_RELAXED)) {
+ c->v[9] = c->v[10] = 0; // discrete shake pulses are for the game's gesture detector, not the camera
+ }
+ MouseSample next = {.object = c->x[20], .when = plat_now_ns(), .camera_enabled = accept};
+ memcpy(&next.pad[0], &c->v[9], sizeof(float));
+ memcpy(&next.pad[1], &c->v[10], sizeof(float));
+ if (flags & 2) next.pad[0] = -next.pad[0];
+ if (flags & 4) next.pad[1] = -next.pad[1];
+ if (direct_mode()) {
+ int32_t dx, dy;
+ input_direct_mouse_sample(!input_direct_mouse_selector_active() && (accept || hand_active(plat_now_ns())), &dx, &dy);
+ float sensitivity = (float)cfg_double("TOTK_MOUSE_SENS");
+ sensitivity = fminf(10, fmaxf(0, sensitivity));
+ // The game multiplies camera input by its 30 Hz frame ratio later. Divide by that exact ratio, rather
+ // than wall-clock time: an equal distance of mouse motion then gives equal input at every frame rate.
+ float ratio = 1;
+ if (frame_step_realtime()) memcpy(&ratio, rt_patch_page(), sizeof ratio);
+ else {
+ double hz = frame_step_limit_hz();
+ if (hz > 0) ratio = (float)(30 / hz);
+ }
+ if (isfinite(ratio) && ratio > 0 && sensitivity > 0 && (dx || dy)) {
+ mouse_shake_feed(dx, dy, plat_now_ns());
+ if (accept) mouse_input_object = c->x[20];
+ float x, y;
+ memcpy(&x, &c->v[9], sizeof x);
+ memcpy(&y, &c->v[10], sizeof y);
+ float scale = 0.01f * sensitivity / ratio;
+ next.motion[0] = dx * scale * (flags & 2 ? -1 : 1);
+ next.motion[1] = -dy * scale * (flags & 4 ? -1 : 1);
+ next.angle[0] = -dx * 0.08f * sensitivity * (flags & 2 ? -1 : 1);
+ next.angle[1] = dy * 0.08f * sensitivity * (flags & 4 ? -1 : 1);
+ if (accept) { x += dx * scale; y -= dy * scale; }
+ uint32_t xb, yb;
+ memcpy(&xb, &x, sizeof xb);
+ memcpy(&yb, &y, sizeof yb);
+ c->v[9] = xb;
+ c->v[10] = yb;
+ static int logged;
+ if (!logged) {
+ logged = 1;
+ rt_log("mouse: direct camera input after the stick dead zone, clamp and exponent (experimental)");
+ }
+ }
+ }
+ plat_mutex_lock(&sample_lock);
+ next.generation = sample.generation + 1;
+ sample = next;
+ plat_mutex_unlock(&sample_lock);
+ uint32_t original = (uint32_t)c->v[9];
+ memcpy((void *)(uintptr_t)(c->x[20] + 0x420), &original, sizeof original);
+}
+
+// Free camera has another signed-square curve for its vertical axis, plus mode-dependent velocity filters.
+// Remove only this mouse sample before that controller runs. At its final spherical-angle output, add degrees
+// directly and move its persistent pitch/yaw by the same amounts so the next update starts at the new orientation.
+// The controller still handles its gamepad input, following, collision and focus normally.
+static void orbit_begin(Cpu *c) {
+ orbit.object = 0;
+ uint64_t object = c->x[0];
+ if (direct_mode()) {
+ MouseSample m = sample_read();
+ uint64_t input = guest_pointer(object + 0x20);
+ if (m.object == input && m.camera_enabled) {
+ guest_float_store(input + 0x420, m.pad[0]);
+ guest_float_store(input + 0x424, m.pad[1]);
+ orbit.object = object;
+ orbit.mouse = m;
+ uint64_t held = guest_pointer(input + 0x148);
+ uint8_t override;
+ memcpy(&override, (void *)(uintptr_t)(input + 0x15c), 1);
+ plat_mutex_lock(&sample_lock);
+ OrbitTrack *track = orbit_track(object);
+ if (track->input != input) track->hold_pitch = track->target_saved = 0;
+ if (m.pad[1] != 0 || (held & 0x20) || (override & 0x40)) orbit_pitch_release(track);
+ track->input = input;
+ if (track->hold_pitch) orbit_pitch_target(object, track->pitch);
+ plat_mutex_unlock(&sample_lock);
+ }
+ } else {
+ plat_mutex_lock(&sample_lock);
+ orbit_pitch_release(orbit_track(object));
+ plat_mutex_unlock(&sample_lock);
+ }
+ c->x[8] = guest_pointer(object + 8); // displaced ldr x8, [x0, #8]
+}
+
+static void orbit_angles(Cpu *c) {
+ uint64_t object = c->x[20];
+ if (orbit.object == object) {
+ float yaw_delta = orbit.mouse.angle[0], pitch_delta = orbit.mouse.angle[1];
+ int fresh = plat_now_ns() - orbit.mouse.when < 150000000ull &&
+ orbit_sample_once(object, orbit.mouse.generation);
+ if (!fresh) yaw_delta = pitch_delta = 0;
+ plat_mutex_lock(&sample_lock);
+ OrbitTrack *track = orbit_track(object);
+ if (track->hold_pitch || yaw_delta || pitch_delta) {
+ float pitch = guest_float(c->sp + 0x44), yaw = guest_float(c->sp + 0x48);
+ float low = guest_float(object + 0xf8), high = guest_float(object + 0xfc);
+ if (!isfinite(low) || !isfinite(high) || low >= high) low = -89.9f, high = 89.9f;
+ low = fmaxf(-89.9f, low); high = fminf(89.9f, high);
+ // The game can recalculate its pitch after orbit_begin (following, mode changes and another filter).
+ // Integrate from our last requested angle, then correct the final output even on stationary frames.
+ float base = track->hold_pitch ? track->pitch : pitch;
+ float new_pitch = fminf(high, fmaxf(low, base + pitch_delta));
+ guest_float_store(c->sp + 0x44, new_pitch);
+ guest_float_store(c->sp + 0x48, wrap_degrees(yaw + yaw_delta));
+ guest_float_store(object + 0x108, wrap_degrees(new_pitch - orbit_pitch_offset(object)));
+ guest_float_store(object + 0x120, wrap_degrees(guest_float(object + 0x120) + yaw_delta));
+ if (!track->target_saved) track->original_target = guest_float(object + 0x15c), track->target_saved = 1;
+ track->pitch = new_pitch; track->input = orbit.mouse.object; track->hold_pitch = 1;
+ orbit_pitch_target(object, new_pitch);
+ static int logged;
+ if (!logged) {
+ logged = 1;
+ rt_log("mouse: free-camera angles directly, 0.08 degrees per count at sensitivity 1, equal X/Y");
+ }
+ }
+ plat_mutex_unlock(&sample_lock);
+ orbit.object = 0;
+ }
+ uint32_t bits;
+ memcpy(&bits, (const void *)(uintptr_t)(c->sp + 0x44), sizeof bits);
+ c->v[12] = bits; // displaced ldr s12, [sp, #0x44]
+}
+
+// Ultrahand reads the player's manipulation input rather than the camera's processed stick. Insert the mouse
+// after that controller's per-axis power curves (main+0x1d59460), preserving the keyboard/controller contribution.
+static void hand_input(Cpu *c) {
+ __atomic_store_n(&hand_update, plat_now_ns(), __ATOMIC_RELEASE);
+ static _Thread_local uint64_t object, generation;
+ if (direct_mode()) {
+ MouseSample m = sample_read();
+ if (m.generation != generation || object != c->x[19]) {
+ float x = guest_float(c->sp + 0x60), y;
+ memcpy(&y, &c->v[15], sizeof y);
+ if (__atomic_load_n(&shake_injected, __ATOMIC_RELAXED)) x = y = 0;
+ x += m.motion[0];
+ y += m.motion[1];
+ guest_float_store(c->sp + 0x60, x);
+ uint32_t bits;
+ memcpy(&bits, &y, sizeof bits);
+ c->v[15] = bits;
+ object = c->x[19]; generation = m.generation;
+ static int logged;
+ if (!logged && (m.motion[0] || m.motion[1])) {
+ logged = 1;
+ rt_log("mouse: held-object input after the manipulation controller's power curves (experimental)");
+ }
+ }
+ }
+ uint32_t original = (uint32_t)c->x[8];
+ memcpy((void *)(uintptr_t)(c->sp + 0x48), &original, sizeof original);
+}
+
+// The game also uses a 10% activity threshold after processing the stick. A one-count mouse movement must count
+// as active, even when its displacement is smaller than that threshold. Do this after the game computed the bits,
+// then execute the displaced ldp d9, d8, [sp, #8]. No flag is changed for controller-only input.
+static void camera_activity(Cpu *c) {
+ if (mouse_input_object && mouse_input_object == c->x[20]) {
+ uint32_t flags;
+ void *at = (void *)(uintptr_t)(mouse_input_object + 0x418);
+ memcpy(&flags, at, sizeof flags);
+ flags = (flags | 0x11u) & ~0x28u;
+ memcpy(at, &flags, sizeof flags);
+ mouse_input_object = 0;
+ }
+ uint64_t d9, d8;
+ memcpy(&d9, (const void *)(uintptr_t)(c->sp + 8), sizeof d9);
+ memcpy(&d8, (const void *)(uintptr_t)(c->sp + 16), sizeof d8);
+ c->v[9] = d9;
+ c->v[8] = d8;
+}
+
+__attribute__((constructor)) static void mouse_direct_register(void) {
+ rt_register_host(HOST_MOUSE_DIRECT, camera_input);
+ rt_register_host(HOST_MOUSE_ACTIVITY, camera_activity);
+ rt_register_host(HOST_MOUSE_ORBIT_BEGIN, orbit_begin);
+ rt_register_host(HOST_MOUSE_ORBIT_ANGLES, orbit_angles);
+ rt_register_host(HOST_MOUSE_HAND, hand_input);
+}
+#endif
diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp
index d76bdde5..036db676 100644
--- a/runtime/ui/menu.cpp
+++ b/runtime/ui/menu.cpp
@@ -836,6 +836,8 @@ static void tab_controls(float scale) {
opt_slider("TOTK_RUMBLE_STRENGTH", UI_MENU_RUMBLE_STRENGTH, 0, 300, UI_MENU_RUMBLE_STRENGTH_DESC, 10, "%.0f %%");
opt_slider("TOTK_RUMBLE_CURVE", UI_MENU_RUMBLE_CURVE, 0.2f, 1.5f, UI_MENU_RUMBLE_CURVE_DESC, 0.05f, "%.2f");
opt_toggle("TOTK_RUMBLE_TRIGGERS", UI_MENU_RUMBLE_TRIGGERS, UI_MENU_RUMBLE_TRIGGERS_DESC);
+ static const UiTextId mouse_modes[] = {UI_MENU_MOUSE_STICK, UI_MENU_MOUSE_DIRECT};
+ opt_choice("TOTK_MOUSE_MODE", UI_MENU_MOUSE_MODE, mouse_modes, 2, UI_MENU_MOUSE_MODE_DESC);
opt_slider("TOTK_MOUSE_SENS", UI_MENU_MOUSE_SENS, 0, 10, UI_MENU_MOUSE_SENS_DESC, 0.1f, "%.1f");
row_walk_key();
opt_slider("TOTK_WALK_SCALE", UI_MENU_WALK_SCALE, 0.2f, 0.9f, UI_MENU_WALK_SCALE_DESC, 0.01f, "%.2f");
diff --git a/runtime/ui_text.c b/runtime/ui_text.c
index 28490bdc..52b0bc1d 100644
--- a/runtime/ui_text.c
+++ b/runtime/ui_text.c
@@ -261,6 +261,10 @@ static const char *const text_en[UI_TEXT_COUNT] = {
[UI_MENU_LSTICK_DEADZONE] = "Left stick dead zone", [UI_MENU_RSTICK_DEADZONE] = "Right stick dead zone",
[UI_MENU_STICK_DEADZONE_DESC] = "How far a gamepad stick moves before the game sees it. Raise it if the character or the camera moves on its own: let go of the stick, read \"stick now\" and set the dead zone a few percent above it.",
[UI_MENU_STICK_DEADZONE_NOW] = "stick now: %.0f %%",
+ [UI_MENU_MOUSE_MODE] = "Mouse camera",
+ [UI_MENU_MOUSE_MODE_DESC] = "Direct input bypasses the game's stick dead zone and response curve (experimental, game 1.4.3).",
+ [UI_MENU_MOUSE_STICK] = "Stick emulation",
+ [UI_MENU_MOUSE_DIRECT] = "Direct (experimental)",
[UI_MENU_MOUSE_SENS] = "Mouse camera sensitivity",
[UI_MENU_MOUSE_SENS_DESC] = "How fast the mouse turns the camera.",
[UI_MENU_WALK_KEY] = "Walk key",
@@ -712,6 +716,10 @@ static const char *const text_ru[UI_TEXT_COUNT] = {
[UI_MENU_LSTICK_DEADZONE] = "Мёртвая зона левого стика", [UI_MENU_RSTICK_DEADZONE] = "Мёртвая зона правого стика",
[UI_MENU_STICK_DEADZONE_DESC] = "Насколько нужно отклонить стик геймпада, чтобы игра это заметила. Увеличьте, если персонаж или камера двигаются сами: отпустите стик, посмотрите «стик сейчас» и поставьте мёртвую зону на несколько процентов выше.",
[UI_MENU_STICK_DEADZONE_NOW] = "стик сейчас: %.0f %%",
+ [UI_MENU_MOUSE_MODE] = "Управление мышью",
+ [UI_MENU_MOUSE_MODE_DESC] = "Прямой ввод обходит мёртвую зону и кривую стика (экспериментально, версия игры 1.4.3).",
+ [UI_MENU_MOUSE_STICK] = "Эмуляция стика",
+ [UI_MENU_MOUSE_DIRECT] = "Прямое (экспериментально)",
[UI_MENU_MOUSE_SENS] = "Чувствительность мыши",
[UI_MENU_MOUSE_SENS_DESC] = "Как быстро мышь поворачивает камеру.",
[UI_MENU_WALK_KEY] = "Клавиша шага",
diff --git a/runtime/ui_text.h b/runtime/ui_text.h
index edf669fb..7c9c0f2b 100644
--- a/runtime/ui_text.h
+++ b/runtime/ui_text.h
@@ -107,6 +107,7 @@ typedef enum {
UI_MENU_RESTART, UI_MENU_RESTART_DESC, UI_MENU_RESTART_CONFIRM, UI_MENU_RESTART_YES, UI_MENU_RESTART_SAVING,
UI_MENU_GROUP_STICKS, UI_MENU_LSTICK_DEADZONE, UI_MENU_RSTICK_DEADZONE, UI_MENU_STICK_DEADZONE_DESC,
UI_MENU_STICK_DEADZONE_NOW, UI_MENU_STICK_SENS, UI_MENU_STICK_SENS_DESC,
+ UI_MENU_MOUSE_MODE, UI_MENU_MOUSE_MODE_DESC, UI_MENU_MOUSE_STICK, UI_MENU_MOUSE_DIRECT,
UI_MENU_MOUSE_SENS, UI_MENU_MOUSE_SENS_DESC,
UI_MENU_WALK_KEY, UI_MENU_WALK_KEY_DESC, UI_MENU_WALK_KEY_TAKEN, UI_MENU_WALK_SCALE, UI_MENU_WALK_SCALE_DESC,
diff --git a/tests/mouse_direct_check.py b/tests/mouse_direct_check.py
new file mode 100644
index 00000000..2c080bbd
--- /dev/null
+++ b/tests/mouse_direct_check.py
@@ -0,0 +1,311 @@
+"""Check the direct camera hook's register contract and raw-count routing without game data.
+
+Usage: python tests/mouse_direct_check.py --cc
+"""
+import argparse
+import os
+from pathlib import Path
+import subprocess
+import tempfile
+
+ROOT = Path(__file__).resolve().parents[1]
+HARNESS = r'''
+#include
+#include
+#include "mouse_direct.c"
+static int raw_x, raw_y, takes, captured=1, foreground=1, allowed=1, mode=1, patched=1, selecting;
+static float ratio=0.5f;
+static uint64_t now=1000000000;
+static GuestFn hook, activity_hook, begin_hook, angles_hook, hand_hook;
+const char *cfg_get(const char *key) { (void)key; return mode ? "direct" : "stick"; }
+double cfg_double(const char *key) { (void)key; return 1; }
+int rt_patch_set(const char *s) { (void)s; return 0; }
+int rt_patch_enabled(int s) { (void)s; return patched; }
+int ikb_captured(void) { return captured; }
+int ikb_foreground(void) { return foreground; }
+int input_cursor_allowed(void) { return allowed; }
+int input_direct_mouse_selector_active(void) { return selecting; }
+void ikb_mouse_take(int32_t *x, int32_t *y) { ++takes; *x=raw_x; *y=raw_y; raw_x=raw_y=0; }
+void input_direct_mouse_sample(int enabled, int32_t *x, int32_t *y) {
+ *x=*y=0;
+ if(enabled && captured && foreground && allowed) ikb_mouse_take(x,y);
+}
+uint64_t plat_now_ns(void) { return now; }
+int frame_step_realtime(void) { return 1; }
+double frame_step_limit_hz(void) { return 60; }
+void *rt_patch_page(void) { return ∶ }
+void rt_register_host(uint64_t addr, GuestFn fn) {
+ if(addr==HOST_MOUSE_DIRECT) hook=fn; else if(addr==HOST_MOUSE_ACTIVITY) activity_hook=fn;
+ else if(addr==HOST_MOUSE_ORBIT_BEGIN) begin_hook=fn; else if(addr==HOST_MOUSE_ORBIT_ANGLES) angles_hook=fn;
+ else if(addr==HOST_MOUSE_HAND) hand_hook=fn; else abort();
+}
+void plat_mutex_lock(PlatMutex *m) { (void)m; }
+void plat_mutex_unlock(PlatMutex *m) { (void)m; }
+void rt_log(const char *fmt, ...) { (void)fmt; }
+static float scalar(__uint128_t v) { float f; memcpy(&f,&v,4); return f; }
+int main(void) {
+ assert(hook && activity_hook);
+ Cpu c, expected;
+ unsigned char target[0x500] = {0};
+ memset(&c, 0x5a, sizeof c);
+ c.x[20]=(uintptr_t)target; c.x[21]=0; c.v[9]=c.v[10]=0;
+ expected=c;
+ raw_x=100; raw_y=-50;
+ hook(&c);
+ assert(fabsf(scalar(c.v[9])-2)<1e-6f);
+ assert(fabsf(scalar(c.v[10])-1)<1e-6f);
+ float stored; memcpy(&stored,target+0x420,4); assert(stored==scalar(c.v[9]));
+ expected.v[9]=c.v[9]; expected.v[10]=c.v[10]; assert(!memcmp(&expected,&c,sizeof c));
+ assert(input_direct_mouse_active());
+ // The next read sees no repeated movement. Gamepad/keyboard input is preserved.
+ expected=c; hook(&c); assert(!memcmp(&expected,&c,sizeof c));
+ // Equal mouse distance produces equal integrated input at 30, 60 and 144 Hz; there is no unit-circle clamp.
+ float rates[]={30,60,144};
+ for(int i=0;i<3;i++) {
+ ratio=30/rates[i]; raw_x=10000; raw_y=10000; c.v[9]=c.v[10]=0; hook(&c);
+ assert(fabsf(scalar(c.v[9])*ratio-100)<0.001f);
+ assert(fabsf(scalar(c.v[10])*ratio+100)<0.001f);
+ }
+ // One count is not discarded by a dead zone.
+ ratio=0.5f; raw_x=1; raw_y=0; c.v[9]=c.v[10]=0; hook(&c); assert(scalar(c.v[9])>0);
+ uint64_t stack[4]={0,0x123456789abcdef0ull,0x456789abcdef0123ull,0}; c.sp=(uintptr_t)stack;
+ uint32_t inactive=0x28; memcpy(target+0x418,&inactive,4);
+ activity_hook(&c);
+ uint32_t active; memcpy(&active,target+0x418,4); assert(active==0x11);
+ assert(c.v[9]==stack[1] && c.v[8]==stack[2]);
+ // A controller-only update keeps the game's activity bits.
+ memcpy(target+0x418,&inactive,4); activity_hook(&c);
+ memcpy(&active,target+0x418,4); assert(active==inactive);
+ // Disabled game camera input leaves raw counts for the menu's legacy mouse-to-stick path.
+ c.x[21]=1; raw_x=42; int before=takes; hook(&c);
+ assert(takes==before && raw_x==42 && !input_direct_mouse_active()); c.x[21]=0;
+ // Focus loss and host menus do not move the camera.
+ captured=0; before=takes; hook(&c); assert(takes==before); captured=1;
+ foreground=0; hook(&c); assert(takes==before); foreground=1;
+ allowed=0; hook(&c); assert(takes==before); allowed=1;
+ mode=0; hook(&c); assert(takes==before && !input_direct_mouse_active()); mode=1;
+ patched=0; hook(&c); assert(takes==before && !input_direct_mouse_active()); patched=1;
+ now+=150000001; assert(!input_direct_mouse_active());
+ // Orbit input removes the mouse from the stick path and writes equal degrees per count at the final angles.
+ unsigned char camera[0x300]={0}, frame[0x100]={0}, hand[0x300]={0};
+ uint64_t input_address=(uintptr_t)target; memcpy(camera+0x20,&input_address,8);
+ for(int rate=0;rate<3;rate++) {
+ memset(orbit_seen,0,sizeof orbit_seen);
+ ratio=30/rates[rate]; c.x[20]=(uintptr_t)target; c.x[21]=0; c.v[9]=c.v[10]=0;
+ raw_x=100; raw_y=-50; hook(&c);
+ guest_float_store((uintptr_t)camera+0x108,30); guest_float_store((uintptr_t)camera+0x120,40);
+ guest_float_store((uintptr_t)frame+0x44,30); guest_float_store((uintptr_t)frame+0x48,40);
+ c.x[0]=(uintptr_t)camera; begin_hook(&c);
+ assert(guest_float((uintptr_t)target+0x420)==0 && guest_float((uintptr_t)target+0x424)==0);
+ c.x[20]=(uintptr_t)camera; c.sp=(uintptr_t)frame; angles_hook(&c);
+ assert(fabsf(guest_float((uintptr_t)frame+0x44)-26)<0.0001f);
+ assert(fabsf(guest_float((uintptr_t)frame+0x48)-32)<0.0001f);
+ assert(fabsf(guest_float((uintptr_t)camera+0x15c)-26)<0.0001f);
+ c.x[0]=(uintptr_t)camera; begin_hook(&c); angles_hook(&c);
+ assert(fabsf(guest_float((uintptr_t)frame+0x48)-32)<0.0001f); // one sample cannot repeat next update
+ // The next input frame is stationary and the game's parameter update rewrites the old default target.
+ // Restoring the mouse's desired target makes the camera filter stay at the new pitch, not spring to zero.
+ c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; raw_x=raw_y=0; hook(&c);
+ guest_float_store((uintptr_t)camera+0x15c,0);
+ c.x[0]=(uintptr_t)camera; begin_hook(&c);
+ float current=guest_float((uintptr_t)camera+0x108), goal=guest_float((uintptr_t)camera+0x15c);
+ assert(fabsf(goal-26)<0.0001f && fabsf((current+goal)*0.5f-26)<0.0001f);
+ // A later game filter overwrites the pitch after the begin hook. Neither a stationary frame nor the next
+ // tiny mouse delta may integrate from that recentered result.
+ guest_float_store((uintptr_t)camera+0x108,0);
+ guest_float_store((uintptr_t)camera+0x11c,5);
+ guest_float_store((uintptr_t)camera+0x10c,20);
+ guest_float_store((uintptr_t)camera+0x88,0.3f);
+ guest_float_store((uintptr_t)frame+0x44,0);
+ c.x[20]=(uintptr_t)camera; angles_hook(&c);
+ assert(fabsf(guest_float((uintptr_t)frame+0x44)-26)<0.0001f);
+ assert(fabsf(guest_float((uintptr_t)camera+0x15c)-7)<0.0001f);
+ assert(fabsf(guest_float((uintptr_t)camera+0x108)-12)<0.0001f);
+ for(int i=0;i<100;i++) {
+ guest_float_store((uintptr_t)frame+0x44,0); angles_hook(&c);
+ assert(fabsf(guest_float((uintptr_t)camera+0x108)-12)<0.0001f);
+ }
+ c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; raw_y=1; hook(&c);
+ c.x[0]=(uintptr_t)camera; begin_hook(&c);
+ guest_float_store((uintptr_t)frame+0x44,0);
+ c.x[20]=(uintptr_t)camera; angles_hook(&c);
+ assert(fabsf(guest_float((uintptr_t)frame+0x44)-26.08f)<0.0001f);
+ // An explicit controller recenter still hands pitch back to the game.
+ uint64_t recenter=0x20; memcpy(target+0x148,&recenter,8);
+ c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; hook(&c);
+ c.x[0]=(uintptr_t)camera; begin_hook(&c);
+ guest_float_store((uintptr_t)frame+0x44,5);
+ c.x[20]=(uintptr_t)camera; angles_hook(&c);
+ assert(guest_float((uintptr_t)frame+0x44)==5);
+ memset(target+0x148,0,8);
+ memset(camera+0x88,0,4); memset(camera+0x10c,0,4); memset(camera+0x11c,0,4);
+ }
+ guest_float_store((uintptr_t)camera+0x108,30); guest_float_store((uintptr_t)camera+0x120,40);
+ for(int count=0;count<100;count++) {
+ c.x[20]=(uintptr_t)target; c.x[21]=0; c.v[9]=c.v[10]=0; raw_x=1; raw_y=0; hook(&c);
+ c.x[0]=(uintptr_t)camera; begin_hook(&c); c.x[20]=(uintptr_t)camera;
+ guest_float_store((uintptr_t)frame+0x44,guest_float((uintptr_t)camera+0x108));
+ guest_float_store((uintptr_t)frame+0x48,guest_float((uintptr_t)camera+0x120));
+ angles_hook(&c);
+ }
+ assert(fabsf(guest_float((uintptr_t)camera+0x120)-32)<0.001f);
+ // A held object receives mouse deltas after its own curves, once per sample, including with camera input off.
+ c.x[20]=(uintptr_t)target; c.v[9]=c.v[10]=0; raw_x=100; raw_y=50; ratio=0.5f; hook(&c);
+ c.x[19]=(uintptr_t)hand; c.x[8]=0x12345678; c.v[15]=0;
+ guest_float_store((uintptr_t)frame+0x60,0); hand_hook(&c);
+ assert(fabsf(guest_float((uintptr_t)frame+0x60)-2)<0.00001f);
+ assert(fabsf(scalar(c.v[15])+1)<0.00001f);
+ hand_hook(&c); assert(fabsf(scalar(c.v[15])+1)<0.00001f);
+ c.x[21]=1; raw_x=5; raw_y=6; hook(&c); assert(raw_x==0 && raw_y==0 && input_direct_mouse_active());
+ // A radial selector retains its virtual cursor between mouse packets, including repeated HID polls.
+ float cursor[2]={0}; int32_t selector[2], held_selector[2];
+ input_direct_mouse_radial(100,50,1,cursor,selector);
+ held_selector[0]=selector[0]; held_selector[1]=selector[1];
+ for(int i=0;i<100;i++) {
+ input_direct_mouse_radial(0,0,1,cursor,selector);
+ assert(selector[0]==held_selector[0] && selector[1]==held_selector[1]);
+ }
+ input_direct_mouse_radial(-100,-50,1,cursor,selector); assert(!selector[0] && !selector[1]);
+ input_direct_mouse_radial(100000,100000,1,cursor,selector);
+ assert((int64_t)selector[0]*selector[0]+(int64_t)selector[1]*selector[1]<=32768ll*32768);
+ // Quick lists must not latch a wheel direction or automatically drain a large flick after the mouse stops.
+ InputMouseRow row={0}; uint64_t row_time=0;
+ input_direct_mouse_row(10000,1,row_time,&row,selector); assert(selector[0]==32767 && !selector[1]);
+ input_direct_mouse_row(0,1,row_time+10000000,&row,selector); assert(selector[0]==32767);
+ for(int i=1;i<=100;i++) {
+ input_direct_mouse_row(0,1,row_time+i*100000000ull,&row,selector); assert(!selector[0] && !selector[1]);
+ }
+ // Repeated HID polls do not rearm a pulse; a frame that skips its deadline must still observe neutral first.
+ row=(InputMouseRow){0};
+ for(int i=0;i<31;i++) { input_direct_mouse_row(1,1,0,&row,selector); assert(!selector[0]); }
+ input_direct_mouse_row(1,1,0,&row,selector); assert(selector[0]==32767);
+ input_direct_mouse_row(1000,1,100000000,&row,selector); assert(!selector[0]);
+ input_direct_mouse_row(1000,1,110000000,&row,selector); assert(!selector[0]);
+ input_direct_mouse_row(1000,1,140000000,&row,selector); assert(selector[0]==32767);
+ input_direct_mouse_row(-1000,1,141000000,&row,selector); assert(!selector[0]);
+ input_direct_mouse_row(-1,1,180000000,&row,selector); assert(selector[0]==-32767);
+ selecting=1; c.x[20]=(uintptr_t)target; raw_x=42; raw_y=0; before=takes; hook(&c);
+ assert(takes==before && raw_x==42); selecting=0; raw_x=0;
+ // Deliberate large reversals with an active held object start a bounded shake; monotonic motion does not.
+ now+=800000000ull;
+ for(int i=0;i<4;i++) {
+ hand_hook(&c); mouse_shake_feed(i&1?-40:40,0,now); now+=50000000ull;
+ }
+ int32_t pulse[2]; assert(input_direct_mouse_shake(1,pulse) && abs(pulse[0])==32767 && pulse[1]==0);
+ int first=pulse[0]; now+=40000000ull; hand_hook(&c);
+ assert(input_direct_mouse_shake(1,pulse) && pulse[0]==-first);
+ captured=0; assert(!input_direct_mouse_shake(1,pulse)); captured=1;
+ now+=800000000ull;
+ for(int i=0;i<4;i++) { hand_hook(&c); mouse_shake_feed(40,0,now); now+=50000000ull; }
+ assert(!input_direct_mouse_shake(1,pulse));
+ for(int i=0;i<10;i++) { hand_hook(&c); mouse_shake_feed(i&1?-5:5,0,now); now+=10000000ull; }
+ assert(!input_direct_mouse_shake(1,pulse));
+ puts("direct mouse: register contract, routing, linearity, frame ratio and capture checks passed");
+ puts("direct mouse: final orbit angles, single-count motion, equal axes and held-object routing passed");
+ puts("direct mouse: persistent pitch target, radial cursor hold, deliberate shake and noise rejection passed");
+ puts("direct mouse: final-pitch recenter rejection, explicit handoff and bounded quick-list steps passed");
+}
+'''
+
+INPUT_HARNESS = r'''
+#include
+#include "input.c"
+GfxStats gfx_counters;
+InputMap input_map;
+// Other input entry points are linked by PE unwind metadata; reaching them is outside this script-only test.
+void ikb_cursor_update(void) { abort(); }
+void ikb_menu_closed(void) { abort(); }
+int cfg_bool(const char *key) { (void)key; abort(); }
+int input_menu_gate(int *h,int open,uint64_t *b,int32_t s[4]) { (void)h;(void)open;(void)b;(void)s;abort(); }
+int input_overlay_gate(int *h,uint64_t *b) { (void)h;(void)b;abort(); }
+int menu_is_open(void) { abort(); }
+void menu_toggle(void) { abort(); }
+void menu_perf_cycle(void) { abort(); }
+const char *input_action_name(int action) { (void)action;abort(); }
+void input_map_configure(void) { abort(); }
+int input_direct_mouse_shake(int allow,int32_t out[2]) { (void)allow;(void)out;abort(); }
+int input_direct_mouse_active(void) { return 1; }
+int input_direct_mouse_enabled(void) { return 1; }
+static int row_calls, radial_calls, live_mouse;
+void input_direct_mouse_row(int32_t dx,float sensitivity,uint64_t now,InputMouseRow *row,int32_t out[2]) {
+ (void)dx;(void)sensitivity;(void)now; row_calls++; out[0]=123;out[1]=0;row->direction=1;
+}
+void input_direct_mouse_radial(int32_t dx,int32_t dy,float sensitivity,float cursor[2],int32_t out[2]) {
+ (void)dx;(void)dy;(void)sensitivity;radial_calls++;cursor[0]=1;out[0]=456;out[1]=789;
+}
+static const char *script_path;
+static uint64_t test_tick;
+static int64_t test_world=-1;
+const char *cfg_get(const char *key) {
+ if (!strcmp(key,"TOTK_INPUT_SCRIPT")) return script_path;
+ if (!strcmp(key,"TOTK_INPUT")) return "none";
+ return NULL;
+}
+unsigned cfg_version(void) { return 0; }
+double cfg_double(const char *key) { (void)key; return 1; }
+uint64_t frame_step_ticks(void) { return test_tick; }
+int64_t frame_step_world_us(void) { return test_world; }
+void rt_log(const char *fmt, ...) { (void)fmt; }
+void plat_mutex_lock(PlatMutex *m) { (void)m; }
+void plat_mutex_unlock(PlatMutex *m) { (void)m; }
+int ikb_captured(void) { return live_mouse; }
+int ikb_foreground(void) { return 0; }
+void ikb_mouse_take(int32_t *x,int32_t *y) { *x=*y=0; }
+uint64_t plat_now_ns(void) { return 1000000000ull; }
+int main(int argc,char **argv) {
+ assert(argc==2); script_path=argv[1];
+ uint64_t buttons=0; int32_t left[2]={0},right[2]={0},dx,dy;
+ test_tick=10;
+ for(int i=0;i<3;i++) script_apply(&buttons,left,right);
+ input_direct_mouse_sample(1,&dx,&dy);
+ assert(dx==123 && dy==-45 && !buttons && !right[0] && !right[1]);
+ input_direct_mouse_sample(1,&dx,&dy); assert(!dx && !dy);
+ test_tick=20; script_apply(&buttons,left,right);
+ g_menu=1; input_direct_mouse_sample(1,&dx,&dy); assert(!dx && !dy);
+ g_menu=0; input_direct_mouse_sample(1,&dx,&dy); assert(!dx && !dy);
+ test_world=100000;
+ for(int i=0;i<3;i++) script_apply(&buttons,left,right);
+ rec=tmpfile(); assert(rec); rec_state=1;
+ input_direct_mouse_sample(1,&dx,&dy); assert(dx==-100 && dy==50);
+ rewind(rec); char line[128]; assert(fgets(line,sizeof line,rec));
+ assert(strstr(line,"MOUSE -100 50")); assert(!fgets(line,sizeof line,rec));
+ fclose(rec); rec_state=-1;
+ live_mouse=1;
+ mouse_camera(right,1); assert(radial_calls==1 && !row_calls && right[0]==456 && right[1]==789);
+ assert(input_direct_mouse_selector_active());
+ mouse_camera(right,2); assert(row_calls==1 && radial_calls==1 && right[0]==123 && !right[1]);
+ mouse_camera(right,0); assert(!input_direct_mouse_selector_active() && !right[0] && !right[1]);
+ mouse_camera(right,3); assert(row_calls==2 && right[0]==123 && !right[1]);
+ live_mouse=0; mouse_camera(right,3);
+ assert(!input_direct_mouse_selector_active() && !right[0] && !right[1]);
+ puts("direct mouse: one-shot script replay, host-menu blocking and raw-count recording passed");
+ puts("direct mouse: separate wheel/list routing, selector ownership and release passed");
+}
+'''
+
+def main():
+ ap = argparse.ArgumentParser(description=__doc__)
+ ap.add_argument('--cc', required=True)
+ ap.add_argument('--deps', type=Path, help='dependency include directory (SDL3/Vulkan), to also check input scripts')
+ args = ap.parse_args()
+ with tempfile.TemporaryDirectory(prefix='totk-mouse-') as tmp:
+ folder = Path(tmp)
+ (folder / 'patches.h').write_text('#define RT_PATCH_PAGE 0x7003adf000ull\n')
+ source = folder / 'check.c'
+ source.write_text(HARNESS)
+ exe = folder / ('check.exe' if os.name == 'nt' else 'check')
+ link = [] if os.name == 'nt' else ['-lm']
+ subprocess.run([args.cc, '-O2', '-Wall', '-Wextra', '-I', str(folder), '-I', str(ROOT/'runtime'),
+ str(source), *link, '-o', str(exe)], check=True)
+ subprocess.run([str(exe)], check=True)
+ if args.deps:
+ source.write_text(INPUT_HARNESS)
+ script = folder / 'input.txt'
+ script.write_text('10 MOUSE 123 -45\n20 MOUSE 1000 0\n@100 MOUSE -100 50\n')
+ subprocess.run([args.cc, '-O2', '-flto', '-ffunction-sections', '-fdata-sections', '-Wl,--gc-sections', *(['-Wl,--exclude-all-symbols'] if os.name == 'nt' else []),
+ '-I', str(ROOT/'runtime'), '-I', str(args.deps), '-I', str(args.deps/'vma'),
+ str(source), *link, '-o', str(exe)], check=True)
+ subprocess.run([str(exe), str(script)], check=True)
+
+if __name__ == '__main__':
+ main()
diff --git a/tools/build_game.py b/tools/build_game.py
index 4b9b0419..6de4345e 100644
--- a/tools/build_game.py
+++ b/tools/build_game.py
@@ -156,8 +156,8 @@ LLVM_LINK_LIBS = ['-lpsapi', '-lbcrypt', '-luser32', '-lgdi32', '-ldwmapi', '-li
DEFAULT_DEPS = ROOT / 'toolchain' / 'deps-mingw'
# Runtime sources compiled per install, not in the library: romfs.c includes romfs_config.h, patches.c and ov_zstd.c
# the patches.h of the player's game version (its patch page is the page after that version's main; ov_zstd.c's host
-# functions sit on it).
-INSTALL_SIDE = {'romfs.c', 'patches.c', 'ov_zstd.c'}
+# functions sit on it). mouse_direct.c also registers host callbacks relative to that version's patch page.
+INSTALL_SIDE = {'romfs.c', 'patches.c', 'ov_zstd.c', 'mouse_direct.c'}
LIB_NAME = 'libtotkrt.a'
AR = UCRT / 'llvm-ar.exe' if WIN else on_path('llvm-ar')
WINDRES = UCRT / 'windres.exe' # runtime/totk.rc: the manifest of totk.exe
diff --git a/totk.ini.example b/totk.ini.example
index f5c28b7e..3ada4ddc 100644
--- a/totk.ini.example
+++ b/totk.ini.example
@@ -298,6 +298,10 @@
# tab shows each stick's deflection now, so a drifting stick's resting value can be read off.
# lstick_deadzone = 8
# rstick_deadzone = 8
+# Mouse camera mode: stick (default) or direct (experimental, game 1.4.3 only). Direct free-camera input uses
+# 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
# 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
From 8ff29be86f10c9f6a19adbdb88b9d7d6cf8e6932 Mon Sep 17 00:00:00 2001
From: local
Date: Tue, 3 Nov 2026 19:29:41 +0300
Subject: [PATCH 02/10] 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
From ff49f6b62ff41499f6ac6709b02b8eb9a89e11e5 Mon Sep 17 00:00:00 2001
From: Valiantsin Dzerakh
Date: Wed, 4 Nov 2026 08:10:59 +0300
Subject: [PATCH 03/10] Keep the gamepad unchanged under the direct mouse and
the menu pointer, and fix the mouse activity flags
The camera input's activity hook wrote the flags to +0x418, but the game's next instruction stores w8 over them
(1.4.3 main+0x874d80), so a mouse-only turn still read as idle. The hook now changes w8.
The free-orbit hooks rewrote the camera stick input from the last sample even in steps without mouse motion; they now
take a step only when the mouse moved in it or holds the pitch, so a gamepad-only step reaches the camera as the game
made it.
The mouse shake gesture for detaching parts is gone: three 24-count reversals fire while a held object is turned back
and forth. Arrow keys or the pad's right stick shake as before.
The menu pointer comes only while the keyboard and mouse are the device used last (input_kbm_last): a gamepad player
sees the menus as on the console. Raw mouse travel is counted while the cursor is released too, so moving the mouse
brings the pointer back.
Co-Authored-By: Claude Opus 5.5
---
docs/controls.md | 8 ++--
runtime/input.c | 21 ++++++++---
runtime/input.h | 5 ++-
runtime/input_sdl.c | 1 +
runtime/input_win.c | 13 +++++--
runtime/input_wsys.h | 1 +
runtime/menu_cursor.c | 5 ++-
runtime/mouse_direct.c | 75 ++++++++-----------------------------
tests/mouse_direct_check.py | 43 +++++++++++----------
9 files changed, 77 insertions(+), 95 deletions(-)
diff --git a/docs/controls.md b/docs/controls.md
index c59ebbb1..20b2f8a5 100644
--- a/docs/controls.md
+++ b/docs/controls.md
@@ -108,8 +108,10 @@ with at least 32 ms of neutral between them. At sensitivity 1, 32 horizontal cou
cannot queue continued scrolling after the mouse stops. Vertical movement is ignored in these lists. Switching or
releasing a selector resets its cursor/step state without sending the last movement to the camera. Selector input is
not consumed by the direct camera or Ultrahand hooks.
-With a held object, three substantial reversals on one mouse axis, with at most 400 ms between strokes, begin a
-short stick-shake gesture for the game's own detach detector. Those gesture pulses are excluded from camera and object movement input.
+The mouse never stands in for a stick shake: to detach a fused part, shake the arrow keys (the keyboard's right stick)
+or the gamepad's right stick. A mouse gesture for it would also fire while a held object is turned back and forth.
+A step of the gamepad alone, with no mouse motion and no pitch the mouse holds, reaches the camera exactly as without
+the hooks.
The mapping below is still used when the camera-input hook is unavailable or the game disables camera input.
This hook needs in-game checks of aiming, Ultrahand and menu transitions before it can be considered complete mouse
support. Games 1.1.0 and 1.2.1 have no hook and keep stick emulation.
@@ -124,7 +126,7 @@ The stick direction bits (`StickLLeft` to `StickRDown`) are derived from the sti
## 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.
+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. This happens only while the keyboard and mouse are the device used last: after a gamepad button press or a stick pushed past half way the menus are as on the console, with no pointer, until the mouse moves or a key is pressed (`input_kbm_last` in `runtime/input.c`). 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.
diff --git a/runtime/input.c b/runtime/input.c
index aadd2dfe..ce84f502 100644
--- a/runtime/input.c
+++ b/runtime/input.c
@@ -27,6 +27,12 @@ enum { STICK_MAX = 32767, STICK_DIR = 16384, TRIGGER_ON = 16384, RESCAN_MS = 100
static PlatMutex lock = PLAT_MUTEX_INIT;
static int state; // 0 not started, 1 running, -1 disabled or SDL failed
static volatile int32_t g_block, g_menu; // text entry / host menu: 0 off, 1 open, 2 waiting for release
+// The device the player used last (input_kbm_last): 1 keyboard and mouse, 0 the gamepad. A pad counts once a button is
+// down or a stick is past half way (a drifting stick never switches), the mouse once it moved MOUSE_USED raw counts
+// within one poll (a slow deliberate move does; a desk bump does not), the keyboard with any key or mouse button.
+enum { PAD_USED = 16384, MOUSE_USED = 6 };
+static volatile int32_t g_kbm_last = 1;
+int input_kbm_last(void) { return __atomic_load_n(&g_kbm_last, __ATOMIC_RELAXED); }
static SDL_Gamepad *pad;
static uint64_t next_scan;
static int pad_rumble; // the open gamepad has rumble motors
@@ -563,6 +569,8 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
if (state > 0) {
binds_refresh();
if (state == 1) pad_read(&b, sl, sr);
+ int pad_used = b || abs(sl[0]) > PAD_USED || abs(sl[1]) > PAD_USED || abs(sr[0]) > PAD_USED ||
+ abs(sr[1]) > PAD_USED, kbm_used = 0;
if (ikb_foreground()) {
uint8_t down[KEY_COUNT] = {0};
// Mouse buttons count only while the cursor is captured, and a button already held when the capture began
@@ -572,6 +580,7 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
int cap = ikb_captured();
for (int k = 1; k < KEY_COUNT; k++) {
down[k] = ikb_down(k);
+ kbm_used |= down[k];
if (k < KEY_MOUSE_L || k > KEY_MOUSE_X2) continue;
if (cap && !was_captured) mouse_ignore[k] = down[k];
if (!down[k]) mouse_ignore[k] = 0;
@@ -592,14 +601,16 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
b & (1ull << B_LEFT) ? 3 : b & (1ull << B_RIGHT) ? 4 : 0;
mouse_camera(k, selector);
pick(&sr[0], &sr[1], k[0], k[1]);
+ static uint32_t travel_seen;
+ uint32_t travel = ikb_mouse_travel();
+ if (travel - travel_seen >= MOUSE_USED) kbm_used = 1;
+ travel_seen = travel;
}
+ // A press or a full-way stick on the pad wins over keys held in the same poll: the player picked it up
+ if (pad_used) __atomic_store_n(&g_kbm_last, 0, __ATOMIC_RELAXED);
+ else if (kbm_used) __atomic_store_n(&g_kbm_last, 1, __ATOMIC_RELAXED);
}
script_apply(&b, sl, sr);
- int32_t shake[2];
- int wheel = (b & ((1ull << B_L) | (1ull << B_UP) | (1ull << B_LEFT) | (1ull << B_RIGHT))) != 0;
- if (input_direct_mouse_shake(!wheel && !sr[0] && !sr[1] && !g_menu && !g_block, shake)) {
- sr[0] = shake[0]; sr[1] = shake[1];
- }
rec_apply(b, sl, sr);
// Minus and Plus held together (gamepad or keys) open or close the menu and never reach the game
static int combo_held;
diff --git a/runtime/input.h b/runtime/input.h
index ca4a0def..8de89e77 100644
--- a/runtime/input.h
+++ b/runtime/input.h
@@ -6,6 +6,9 @@
// Down=15) and the stick directions (StickLLeft..Down 16-19, StickRLeft..Down 20-23, derived from the sticks).
// Sticks are -32767..32767, up is +y. TOTK_INPUT=none reports no input. Callable from any thread.
void input_poll(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry);
+// 1 when keyboard and mouse are the device the player used last, 0 when the gamepad is: what is shown for pointing
+// (menu_cursor.c shows the pointer in the game's menus only for the keyboard and mouse)
+int input_kbm_last(void);
// Direct mouse input is consumed by the game's camera-input update, after its stick response. While that update
// accepts camera input, the HID right stick contains only the controller and keyboard. Selectors route mouse input
// separately: a persistent radial cursor for the ability wheel, discrete steps for horizontal quick lists.
@@ -17,8 +20,6 @@ void input_direct_mouse_radial(int32_t dx, int32_t dy, float sensitivity, float
typedef struct { float distance; uint64_t pulse_until, neutral_until; int direction; } InputMouseRow;
void input_direct_mouse_row(int32_t dx, float sensitivity, uint64_t now, InputMouseRow *row, int32_t out[2]);
int input_direct_mouse_selector_active(void);
-// A deliberate mouse shake while manipulating an object feeds a short gesture to the game's native shake detector.
-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);
diff --git a/runtime/input_sdl.c b/runtime/input_sdl.c
index 7d337532..03c86607 100644
--- a/runtime/input_sdl.c
+++ b/runtime/input_sdl.c
@@ -219,6 +219,7 @@ 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; }
+uint32_t ikb_mouse_travel(void) { return 0; } // (the last-device switch: keys and buttons only for now)
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 d0058e8a..a6aa1e8d 100644
--- a/runtime/input_win.c
+++ b/runtime/input_win.c
@@ -83,6 +83,7 @@ uint32_t input_key_char(int key) {
// ---- mouse camera: raw motion accumulated by the window thread, one stick sample per presented frame
static volatile LONG mouse_dx, mouse_dy; // raw counts since the last frame sample
+static volatile LONG mouse_travel; // raw counts moved in total, captured or not (ikb_mouse_travel)
static volatile LONG cur_captured; // cursor hidden and confined to the client area
static volatile LONG cur_active; // WM_ACTIVATEAPP: the window's application is active
// Bits: CUR_REL, Esc or activation by a click on the title bar or border: until a client click; CUR_ESC, set with CUR_REL
@@ -137,10 +138,14 @@ int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) {
case WM_INPUT: {
RAWINPUT ri;
UINT sz = sizeof ri;
- if (cur_captured && GetRawInputData((HRAWINPUT)l, RID_INPUT, &ri, &sz, sizeof(RAWINPUTHEADER)) != (UINT)-1 &&
+ if (GetRawInputData((HRAWINPUT)l, RID_INPUT, &ri, &sz, sizeof(RAWINPUTHEADER)) != (UINT)-1 &&
ri.header.dwType == RIM_TYPEMOUSE && !(ri.data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE)) {
- InterlockedAdd(&mouse_dx, ri.data.mouse.lLastX);
- InterlockedAdd(&mouse_dy, ri.data.mouse.lLastY);
+ LONG x = ri.data.mouse.lLastX, y = ri.data.mouse.lLastY;
+ InterlockedAdd(&mouse_travel, (x < 0 ? -x : x) + (y < 0 ? -y : y));
+ if (cur_captured) {
+ InterlockedAdd(&mouse_dx, x);
+ InterlockedAdd(&mouse_dy, y);
+ }
}
break; // DefWindowProc must see WM_INPUT for cleanup
}
@@ -222,6 +227,8 @@ void ikb_pointer_set(float nx, float ny) {
if (ClientToScreen(h, &p)) SetCursorPos(p.x, p.y);
}
+uint32_t ikb_mouse_travel(void) { return (uint32_t)mouse_travel; }
+
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 08ff1c1a..a734ee2f 100644
--- a/runtime/input_wsys.h
+++ b/runtime/input_wsys.h
@@ -12,6 +12,7 @@ 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
+uint32_t ikb_mouse_travel(void); // raw counts the mouse moved in total (|dx| + |dy|), captured or not
void ikb_cursor_update(void); // what input_cursor_allowed says may have changed
void ikb_menu_closed(void); // the menu closed: a capture that Esc gave up comes back
diff --git a/runtime/menu_cursor.c b/runtime/menu_cursor.c
index d2b0f897..d500f8b5 100644
--- a/runtime/menu_cursor.c
+++ b/runtime/menu_cursor.c
@@ -18,6 +18,7 @@
// 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.
//
+// Only while keyboard and mouse are the device used last (input_kbm_last): a gamepad player sees the menus unchanged.
// TOTK_MENU_CURSOR (live, default 1): 0 leaves the menus to the keys and the pad.
#include
#include
@@ -292,7 +293,9 @@ void menu_cursor_poll(uint64_t *buttons) {
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();
+ // With the gamepad as the last device the menus stay as they are on the console: no pointer, the capture kept.
+ // Moving the mouse (or a key) makes keyboard and mouse the last device and the pointer comes.
+ int menu = f.screen && !gameplay_screen(nm) && ikb_foreground() && input_kbm_last();
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;
diff --git a/runtime/mouse_direct.c b/runtime/mouse_direct.c
index 69a6ff1e..f3516ad3 100644
--- a/runtime/mouse_direct.c
+++ b/runtime/mouse_direct.c
@@ -29,8 +29,6 @@ static int direct_mode(void) {
static uint64_t last_update;
static uint64_t hand_update;
-static uint64_t shake_started, shake_until;
-static int shake_injected;
static int accepts_mouse;
static int hand_active(uint64_t now) {
@@ -82,18 +80,6 @@ void input_direct_mouse_row(int32_t dx, float sensitivity, uint64_t now, InputMo
out[0] = row->direction * 32767;
}
-int input_direct_mouse_shake(int allow, int32_t out[2]) {
- uint64_t now = plat_now_ns(), until = __atomic_load_n(&shake_until, __ATOMIC_ACQUIRE);
- uint64_t start = __atomic_load_n(&shake_started, __ATOMIC_RELAXED);
- int ready = allow && direct_mode() && hand_active(now) && ikb_captured() && ikb_foreground() && input_cursor_allowed();
- if (!ready) __atomic_store_n(&shake_until, 0, __ATOMIC_RELEASE);
- int active = ready && now >= start && now < until;
- out[0] = active ? ((now - start) / 40000000ull & 1 ? -32767 : 32767) : 0;
- out[1] = 0;
- __atomic_store_n(&shake_injected, active, __ATOMIC_RELAXED);
- return active;
-}
-
#ifdef RT_PATCH_PAGE
#define HOST_MOUSE_DIRECT (RT_PATCH_PAGE + 0xfd0)
#define HOST_MOUSE_ACTIVITY (RT_PATCH_PAGE + 0xfc0)
@@ -163,34 +149,6 @@ static void orbit_pitch_release(OrbitTrack *track) {
track->hold_pitch = track->target_saved = 0;
}
-static void mouse_shake_feed(int32_t dx, int32_t dy, uint64_t now) {
- static struct { int sign, confirmed, reversals; double distance; uint64_t last; } stroke[2];
- static uint64_t cooldown;
- if (!hand_active(now)) { memset(stroke, 0, sizeof stroke); return; }
- if (now < cooldown) return;
- int32_t delta[2] = {dx, dy};
- for (int i = 0; i < 2; i++) {
- if (!delta[i]) continue;
- if (stroke[i].last && now - stroke[i].last > 400000000ull) memset(&stroke[i], 0, sizeof stroke[i]);
- int sign = delta[i] > 0 ? 1 : -1;
- if (sign != stroke[i].sign) stroke[i].sign = sign, stroke[i].distance = 0;
- stroke[i].distance += fabs((double)delta[i]);
- if (stroke[i].distance < 24) continue;
- if (stroke[i].confirmed && sign != stroke[i].confirmed) stroke[i].reversals++;
- stroke[i].confirmed = sign;
- stroke[i].last = now;
- stroke[i].distance = 0;
- if (stroke[i].reversals >= 3) {
- __atomic_store_n(&shake_started, now, __ATOMIC_RELAXED);
- __atomic_store_n(&shake_until, now + 320000000ull, __ATOMIC_RELEASE);
- cooldown = now + 700000000ull;
- memset(stroke, 0, sizeof stroke);
- rt_log("mouse: detach shake gesture sent to the game's stick-shake detector");
- return;
- }
- }
-}
-
// A host call at an instruction site, not an AAPCS64 function replacement. Preserve every guest register except
// s9/s10, which carry the processed camera input, and perform the displaced store of s9 at [x20 + 0x420].
static void camera_input(Cpu *c) {
@@ -199,9 +157,6 @@ static void camera_input(Cpu *c) {
int accept = !(flags & 1) && !input_direct_mouse_selector_active();
__atomic_store_n(&accepts_mouse, accept, __ATOMIC_RELAXED);
__atomic_store_n(&last_update, plat_now_ns(), __ATOMIC_RELEASE);
- if (direct_mode() && __atomic_load_n(&shake_injected, __ATOMIC_RELAXED)) {
- c->v[9] = c->v[10] = 0; // discrete shake pulses are for the game's gesture detector, not the camera
- }
MouseSample next = {.object = c->x[20], .when = plat_now_ns(), .camera_enabled = accept};
memcpy(&next.pad[0], &c->v[9], sizeof(float));
memcpy(&next.pad[1], &c->v[10], sizeof(float));
@@ -221,7 +176,6 @@ static void camera_input(Cpu *c) {
if (hz > 0) ratio = (float)(30 / hz);
}
if (isfinite(ratio) && ratio > 0 && sensitivity > 0 && (dx || dy)) {
- mouse_shake_feed(dx, dy, plat_now_ns());
if (accept) mouse_input_object = c->x[20];
float x, y;
memcpy(&x, &c->v[9], sizeof x);
@@ -263,10 +217,9 @@ static void orbit_begin(Cpu *c) {
MouseSample m = sample_read();
uint64_t input = guest_pointer(object + 0x20);
if (m.object == input && m.camera_enabled) {
- guest_float_store(input + 0x420, m.pad[0]);
- guest_float_store(input + 0x424, m.pad[1]);
- orbit.object = object;
- orbit.mouse = m;
+ // The hooks take a step when the mouse moved in it, or while the mouse holds the pitch. A step of the
+ // gamepad alone (no mouse motion, no hold: a player who never moved the mouse) keeps the game's input.
+ int moved = m.motion[0] != 0 || m.motion[1] != 0, take;
uint64_t held = guest_pointer(input + 0x148);
uint8_t override;
memcpy(&override, (void *)(uintptr_t)(input + 0x15c), 1);
@@ -274,9 +227,15 @@ static void orbit_begin(Cpu *c) {
OrbitTrack *track = orbit_track(object);
if (track->input != input) track->hold_pitch = track->target_saved = 0;
if (m.pad[1] != 0 || (held & 0x20) || (override & 0x40)) orbit_pitch_release(track);
- track->input = input;
+ take = moved || track->hold_pitch;
+ if (take) track->input = input;
if (track->hold_pitch) orbit_pitch_target(object, track->pitch);
plat_mutex_unlock(&sample_lock);
+ if (moved) { // the mouse's part of this step's stick goes to the final angles instead
+ guest_float_store(input + 0x420, m.pad[0]);
+ guest_float_store(input + 0x424, m.pad[1]);
+ }
+ if (take) orbit.object = object, orbit.mouse = m;
}
} else {
plat_mutex_lock(&sample_lock);
@@ -335,7 +294,6 @@ static void hand_input(Cpu *c) {
if (m.generation != generation || object != c->x[19]) {
float x = guest_float(c->sp + 0x60), y;
memcpy(&y, &c->v[15], sizeof y);
- if (__atomic_load_n(&shake_injected, __ATOMIC_RELAXED)) x = y = 0;
x += m.motion[0];
y += m.motion[1];
guest_float_store(c->sp + 0x60, x);
@@ -355,15 +313,14 @@ static void hand_input(Cpu *c) {
}
// The game also uses a 10% activity threshold after processing the stick. A one-count mouse movement must count
-// as active, even when its displacement is smaller than that threshold. Do this after the game computed the bits,
-// then execute the displaced ldp d9, d8, [sp, #8]. No flag is changed for controller-only input.
+// as active, even when its displacement is smaller than that threshold. The site is the displaced ldp d9, d8, [sp, #8]
+// just before the update's last `str w8, [x20, #0x418]`: w8 holds the flags then (bit 0 raw input, bit 4 any input,
+// bits 3 and 5 processed input below the threshold), so they are changed in w8; a write to +0x418 would be overwritten
+// by that store. No flag is changed for controller-only input.
static void camera_activity(Cpu *c) {
if (mouse_input_object && mouse_input_object == c->x[20]) {
- uint32_t flags;
- void *at = (void *)(uintptr_t)(mouse_input_object + 0x418);
- memcpy(&flags, at, sizeof flags);
- flags = (flags | 0x11u) & ~0x28u;
- memcpy(at, &flags, sizeof flags);
+ uint32_t flags = (uint32_t)c->x[8];
+ c->x[8] = (flags | 0x11u) & ~0x28u;
mouse_input_object = 0;
}
uint64_t d9, d8;
diff --git a/tests/mouse_direct_check.py b/tests/mouse_direct_check.py
index 2c080bbd..212cd4f7 100644
--- a/tests/mouse_direct_check.py
+++ b/tests/mouse_direct_check.py
@@ -69,13 +69,14 @@ int main(void) {
// One count is not discarded by a dead zone.
ratio=0.5f; raw_x=1; raw_y=0; c.v[9]=c.v[10]=0; hook(&c); assert(scalar(c.v[9])>0);
uint64_t stack[4]={0,0x123456789abcdef0ull,0x456789abcdef0123ull,0}; c.sp=(uintptr_t)stack;
- uint32_t inactive=0x28; memcpy(target+0x418,&inactive,4);
- activity_hook(&c);
- uint32_t active; memcpy(&active,target+0x418,4); assert(active==0x11);
+ // The flags are in w8 at the site: the game's next instruction stores w8 to +0x418, so memory is not touched.
+ uint32_t inactive=0x28, mem=0x77; memcpy(target+0x418,&mem,4);
+ c.x[8]=inactive; activity_hook(&c);
+ assert((uint32_t)c.x[8]==0x11);
+ uint32_t after; memcpy(&after,target+0x418,4); assert(after==mem);
assert(c.v[9]==stack[1] && c.v[8]==stack[2]);
// A controller-only update keeps the game's activity bits.
- memcpy(target+0x418,&inactive,4); activity_hook(&c);
- memcpy(&active,target+0x418,4); assert(active==inactive);
+ c.x[8]=inactive; activity_hook(&c); assert((uint32_t)c.x[8]==inactive);
// Disabled game camera input leaves raw counts for the menu's legacy mouse-to-stick path.
c.x[21]=1; raw_x=42; int before=takes; hook(&c);
assert(takes==before && raw_x==42 && !input_direct_mouse_active()); c.x[21]=0;
@@ -140,6 +141,19 @@ int main(void) {
memset(target+0x148,0,8);
memset(camera+0x88,0,4); memset(camera+0x10c,0,4); memset(camera+0x11c,0,4);
}
+ // A gamepad-only step (no mouse motion, no pitch hold) leaves the camera input and the angles as the game made them.
+ memset(orbit_seen,0,sizeof orbit_seen);
+ c.x[20]=(uintptr_t)target; c.x[21]=0; raw_x=raw_y=0; c.v[9]=c.v[10]=0;
+ { float px=0.25f, py=-0.5f; memcpy(&c.v[9],&px,4); memcpy(&c.v[10],&py,4); }
+ hook(&c);
+ guest_float_store((uintptr_t)target+0x420,0.25f); guest_float_store((uintptr_t)target+0x424,0.75f);
+ guest_float_store((uintptr_t)frame+0x44,11); guest_float_store((uintptr_t)frame+0x48,22);
+ guest_float_store((uintptr_t)camera+0x15c,3);
+ c.x[0]=(uintptr_t)camera; begin_hook(&c);
+ assert(guest_float((uintptr_t)target+0x420)==0.25f && guest_float((uintptr_t)target+0x424)==0.75f);
+ c.x[20]=(uintptr_t)camera; c.sp=(uintptr_t)frame; angles_hook(&c);
+ assert(guest_float((uintptr_t)frame+0x44)==11 && guest_float((uintptr_t)frame+0x48)==22);
+ assert(guest_float((uintptr_t)camera+0x15c)==3);
guest_float_store((uintptr_t)camera+0x108,30); guest_float_store((uintptr_t)camera+0x120,40);
for(int count=0;count<100;count++) {
c.x[20]=(uintptr_t)target; c.x[21]=0; c.v[9]=c.v[10]=0; raw_x=1; raw_y=0; hook(&c);
@@ -186,23 +200,9 @@ int main(void) {
input_direct_mouse_row(-1,1,180000000,&row,selector); assert(selector[0]==-32767);
selecting=1; c.x[20]=(uintptr_t)target; raw_x=42; raw_y=0; before=takes; hook(&c);
assert(takes==before && raw_x==42); selecting=0; raw_x=0;
- // Deliberate large reversals with an active held object start a bounded shake; monotonic motion does not.
- now+=800000000ull;
- for(int i=0;i<4;i++) {
- hand_hook(&c); mouse_shake_feed(i&1?-40:40,0,now); now+=50000000ull;
- }
- int32_t pulse[2]; assert(input_direct_mouse_shake(1,pulse) && abs(pulse[0])==32767 && pulse[1]==0);
- int first=pulse[0]; now+=40000000ull; hand_hook(&c);
- assert(input_direct_mouse_shake(1,pulse) && pulse[0]==-first);
- captured=0; assert(!input_direct_mouse_shake(1,pulse)); captured=1;
- now+=800000000ull;
- for(int i=0;i<4;i++) { hand_hook(&c); mouse_shake_feed(40,0,now); now+=50000000ull; }
- assert(!input_direct_mouse_shake(1,pulse));
- for(int i=0;i<10;i++) { hand_hook(&c); mouse_shake_feed(i&1?-5:5,0,now); now+=10000000ull; }
- assert(!input_direct_mouse_shake(1,pulse));
puts("direct mouse: register contract, routing, linearity, frame ratio and capture checks passed");
puts("direct mouse: final orbit angles, single-count motion, equal axes and held-object routing passed");
- puts("direct mouse: persistent pitch target, radial cursor hold, deliberate shake and noise rejection passed");
+ puts("direct mouse: persistent pitch target, radial cursor hold and gamepad-only steps untouched passed");
puts("direct mouse: final-pitch recenter rejection, explicit handoff and bounded quick-list steps passed");
}
'''
@@ -223,7 +223,6 @@ void menu_toggle(void) { abort(); }
void menu_perf_cycle(void) { abort(); }
const char *input_action_name(int action) { (void)action;abort(); }
void input_map_configure(void) { abort(); }
-int input_direct_mouse_shake(int allow,int32_t out[2]) { (void)allow;(void)out;abort(); }
int input_direct_mouse_active(void) { return 1; }
int input_direct_mouse_enabled(void) { return 1; }
static int row_calls, radial_calls, live_mouse;
@@ -302,7 +301,7 @@ def main():
source.write_text(INPUT_HARNESS)
script = folder / 'input.txt'
script.write_text('10 MOUSE 123 -45\n20 MOUSE 1000 0\n@100 MOUSE -100 50\n')
- subprocess.run([args.cc, '-O2', '-flto', '-ffunction-sections', '-fdata-sections', '-Wl,--gc-sections', *(['-Wl,--exclude-all-symbols'] if os.name == 'nt' else []),
+ subprocess.run([args.cc, '-O2', '-flto', '-ffunction-sections', '-fdata-sections', '-Wl,--gc-sections', *(['-fuse-ld=lld', '-Wl,--exclude-all-symbols'] if os.name == 'nt' else []),
'-I', str(ROOT/'runtime'), '-I', str(args.deps), '-I', str(args.deps/'vma'),
str(source), *link, '-o', str(exe)], check=True)
subprocess.run([str(exe), str(script)], check=True)
From cd35ffaf3633eae5f7d2d6219af36d079e3891b0 Mon Sep 17 00:00:00 2001
From: 0xEmz
Date: Mon, 2 Nov 2026 12:56:38 +0200
Subject: [PATCH 04/10] Show keyboard and mouse icons in the prompts when
prompts = keyboard
Add keyboard and mouse button icons to tools/button_icons.py and
tools/make_romfs_patches.py --prompts keyboard, with a third 'Keyboard
and mouse' choice under Controls > Button prompts (prompts = keyboard).
Keycaps cut their labels out of the game's own text fonts, mouse clicks
and right-stick camera look draw clean mouse silhouettes, and
make_romfs_patches.py --ini reads any bind_ overrides from totk.ini.
---
README.md | 2 +-
docs/building.md | 7 +-
docs/controls.md | 42 +++---
docs/installer.md | 6 +-
installer/steps.py | 18 ++-
installer/text/en.json | 2 +-
installer/text/pt-BR.json | 2 +-
installer/text/ru.json | 2 +-
installer/text/zh-Hans.json | 2 +-
runtime/cfg.c | 2 +-
runtime/cfg_probe.c | 10 ++
runtime/input_map.c | 6 +-
runtime/input_map.h | 8 +-
runtime/romfs.c | 17 ++-
runtime/ui/menu.cpp | 4 +-
runtime/ui_text.c | 15 +-
runtime/ui_text.h | 2 +-
tests/romfs_prompts_test.py | 62 +++++++-
tools/build_game.py | 2 +-
tools/button_icons.py | 293 ++++++++++++++++++++++++++++++++++--
tools/make_romfs_patches.py | 56 ++++---
tools/package_build.py | 2 +-
totk.ini.example | 6 +-
23 files changed, 472 insertions(+), 96 deletions(-)
diff --git a/README.md b/README.md
index 9b0c3f9d..62f7cc45 100644
--- a/README.md
+++ b/README.md
@@ -30,7 +30,7 @@ Report problems as issues, following [CONTRIBUTING.md](CONTRIBUTING.md).
- Setup and in-game menu in English, Russian, Brazilian Portuguese and Simplified Chinese
- Keyboard, mouse and gamepad with remapping, rumble and gyro
- Stereo or 5.1 surround sound output
-- Xbox button prompts: LB, RB, LT, RT, Menu and View, and A, B, X and Y as labelled on the button to press, with either button layout
+- Xbox or keyboard button prompts: Xbox (LB, RB, LT, RT, Menu, View, A/B/X/Y by layout) or keyboard and mouse (keycaps and mouse icons)
- Shaders prepared at install instead of during play
- Mods made for emulators: unpack a mod into the `mods` folder next to `totk.exe`; switch and order them in the menu. RomFS
mods, TKMM's merged output and ExeFS code patches (`.ips`, `.pchtxt`) work; the game rebuilds itself for code patches
diff --git a/docs/building.md b/docs/building.md
index 75585d71..bc0bbc1b 100644
--- a/docs/building.md
+++ b/docs/building.md
@@ -113,9 +113,10 @@ of them.
```
`--patches` builds in the frame-rate, resolution and other patch sets, switchable at run time. The link copies
`SDL3.dll`, `libiconv-2.dll` and `libopus-0.dll` next to the exe.
- For Xbox button prompts, run `make_romfs_patches.py` twice more, with `--prompts xbox` and `--prompts xbox-label`:
- they write `build\\romfs-patches-xbox` and `romfs-patches-xbox-label` ([controls.md](controls.md), "Xbox
- button prompts"), which `build_game.py` copies next to `totk.exe` like `romfs-patches`.
+ For Xbox or keyboard button prompts, run `make_romfs_patches.py` with `--prompts xbox`, `--prompts xbox-label`, or
+ `--prompts keyboard [--ini totk.ini]`: they write `build\\romfs-patches-xbox`, `romfs-patches-xbox-label` and
+ `romfs-patches-keyboard` ([controls.md](controls.md), "Button prompts"), which `build_game.py` copies next to `totk.exe`
+ like `romfs-patches`.
6. Prepare the shaders (once per build folder and translator). It needs `refs\MacNativeGames` (`tools\fetch_refs.sh`)
for the shared shader list:
diff --git a/docs/controls.md b/docs/controls.md
index 20b2f8a5..9e4aee22 100644
--- a/docs/controls.md
+++ b/docs/controls.md
@@ -8,7 +8,7 @@ Input comes from the first SDL3 gamepad, the keyboard and the mouse, merged into
- `xbox`: south A, east B, west X, north Y, as an Xbox pad labels them; on a PlayStation pad cross is A, circle B, square X, triangle Y.
- `auto` (default): `nintendo` for a pad whose east button SDL reports labelled A (Switch Pro Controller, Nintendo-layout pads), `xbox` for the others.
-The log names the layout in effect (`input: gamepad layout ...`); the menu shows it under *Automatic*. Which controller's buttons the game's prompts show is `TOTK_PROMPTS` (see Xbox button prompts below). 0.1.9's `pad_layout = xbox_prompts` is read as `pad_layout = nintendo` with `prompts = xbox`, and the menu writes it that way.
+The log names the layout in effect (`input: gamepad layout ...`); the menu shows it under *Automatic*. Which controller's buttons the game's prompts show is `TOTK_PROMPTS` (see Button prompts below). 0.1.9's `pad_layout = xbox_prompts` is read as `pad_layout = nintendo` with `prompts = xbox`, and the menu writes it that way.
| Switch | Gamepad (by position, Nintendo layout) | Keyboard and mouse | Action in the world |
|---|---|---|---|
@@ -30,31 +30,35 @@ The log names the layout in effect (`input: gamepad layout ...`); the menu shows
Esc is unbound in the game; it opens the settings menu and releases the mouse cursor (see Mouse camera). Z and X duplicate ZL and ZR so menu pages turn with two neighbouring keys; the menu tabs (L, R) are Q and R. The mouse wheel is unbound. The bindings are the tables of `input_map` in `runtime/input_map.c`: the keys (platform-neutral key ids) and one gamepad table per layout (gamepad inputs by position); `runtime/input_win.c` translates Win32 virtual keys and SDL buttons.
-## Xbox button prompts
+## Button prompts
-`TOTK_PROMPTS` (`prompts`, the *Button prompts* row on the Controls tab, read at start) picks the controller whose buttons the game's prompts show: `nintendo` (default) keeps the game's own, `xbox` shows an Xbox controller's.
+`TOTK_PROMPTS` (`prompts`, the *Button prompts* row on the Controls tab, read at start) picks the controller whose buttons the game's prompts show: `nintendo` (default) keeps the game's own, `xbox` shows an Xbox controller's, and `keyboard` shows keyboard keycaps and mouse icons.
-- L, R, ZL, ZR, Plus and Minus become LB, RB, LT, RT, Menu and View, and the d-pad prompts a cross with an arrow for each direction meant. The letters on the keys are cut out in the game's own text font.
-- A, B, X and Y name the label of the button the pad layout presses, so each prompt names the button to press. `pad_layout = nintendo` plays an Xbox pad by position: the right button, labelled B, does Switch A (interact, confirm), and the bottom one, labelled A, does Switch B (run, cancel), so the prompts swap A with B and X with Y. `xbox` and `auto` put Switch A on the button labelled A, and the letters stay.
+- Xbox:
+ - L, R, ZL, ZR, Plus and Minus become LB, RB, LT, RT, Menu and View, and the d-pad prompts a cross with an arrow for each direction meant. The letters on the keys are cut out in the game's own text font.
+ - A, B, X and Y name the label of the button the pad layout presses, so each prompt names the button to press. `pad_layout = nintendo` plays an Xbox pad by position: the right button, labelled B, does Switch A (interact, confirm), and the bottom one, labelled A, does Switch B (run, cancel), so the prompts swap A with B and X with Y. `xbox` and `auto` put Switch A on the button labelled A, and the letters stay.
+- Keyboard:
+ - Each action shows a rounded keycap with its key's label cut out in the game's font (square for 1-2 character labels, wide for 3+ characters like Space, Shift, Ctrl, Tab), arrow polygons for arrow keys, a horizontal bar for unbound keys, and mouse icons for mouse clicks and camera look.
+ - The movement prompts show WASD (or the stick keys' labels) on a wide keycap; camera look prompts show a mouse with look arrows.
+ - By default it reflects the default bindings; `tools/make_romfs_patches.py --prompts keyboard --ini ` reads `totk.ini`'s `bind_` lines to match custom bindings.
-| Switch | `nintendo` layout | `xbox` or `auto` layout |
-|---|---|---|
-| A, B, X, Y | B, A, Y, X | A, B, X, Y |
-| L, R | LB, RB | LB, RB |
-| ZL, ZR | LT, RT | LT, RT |
-| Plus, Minus | Menu, View | Menu, View |
-| D-pad | cross | cross |
+| Switch | `nintendo` layout | `xbox` or `auto` layout | Keyboard (default) |
+|---|---|---|---|
+| A, B, X, Y | B, A, Y, X | A, B, X, Y | E, Shift, Space, Left Mouse |
+| L, R | LB, RB | LB, RB | Q, R |
+| ZL, ZR | LT, RT | LT, RT | Right Mouse, F |
+| Plus, Minus | Menu, View | Menu, View | Tab, M |
+| D-pad | cross | cross | 1, 2, 3, 4 |
+| Left / Right Stick | sticks | sticks | WASD / Mouse |
-The action prompt (`DoCommand_00`) shows its button in a diamond whose shape gives the face buttons' positions as on a Switch, with the other three letters around it. With `nintendo` its letters are swapped with the rest and every one names the button in that place. With `xbox` and `auto` the letters are right but the diamond keeps the Switch positions: A shows on its right, while the button labelled A is at the bottom. The sticks keep the Switch icons, and so do prompts drawn as fixed images (some tutorial animations).
+The action prompt (`DoCommand_00`) shows its button in a diamond whose shape gives the face buttons' positions as on a Switch, with the other three letters around it. With `nintendo` its letters are swapped with the rest and every one names the button in that place. With `xbox` and `auto` the letters are right but the diamond keeps the Switch positions: A shows on its right, while the button labelled A is at the bottom. Keyboard prompts leave the diamond untouched. Fixed tutorial animations keep the Switch images.
-The prompts are part of the game's fonts, textures and messages, so they follow the layout and not the bindings. A button remapped with a `bind_` line or on the Controls tab keeps the prompt the game draws for it, which then names the wrong button. Use the Xbox prompts with the default bindings.
+The prompts are part of the game's fonts and textures. The game reads its UI archive and fonts once at boot, so the prompts change at the next start, also after `pad_layout` changes between `nintendo` and the others (the menu says *prompts change after restart*); the buttons change at once. Under that note the menu offers *Restart now*: after a confirmation it writes `totk.ini`, closes the game as *Quit game* does and starts the game again with the same command line. With `prompts = xbox` the game loads `romfs-patches-xbox` (`pad_layout = nintendo`) or `romfs-patches-xbox-label` (`xbox`, `auto`), and with `prompts = keyboard` it loads `romfs-patches-keyboard` next to `romfs-index.bin` in place of `romfs-patches` (`runtime/romfs.c`, `overlay_init`; `runtime/input_map.c`, `input_prompts_folder`): the same 60 fps patches, with the prompts drawn from the game's own files by `tools/make_romfs_patches.py --prompts xbox`, `--prompts xbox-label` or `--prompts keyboard` (`tools/button_icons.py`), which the setup runs next to the plain one:
-The game reads its UI archive and fonts once at boot, so the prompts change at the next start, also after `pad_layout` changes between `nintendo` and the others (the menu says *prompts change after restart*); the buttons change at once. Under that note the menu offers *Restart now*: after a confirmation it writes `totk.ini`, closes the game as *Quit game* does and starts the game again with the same command line, which waits until the old process is gone before it opens the log and the saves (`runtime/gfx_present.c`, `gfx_window_closed`; `plat_restart` and `plat_restart_wait` in `runtime/platform/win32.c` and `linux.c`). Progress since the last save is lost, as with *Quit game*; while the game writes a save, the confirmation waits. With `prompts = xbox` the game loads `romfs-patches-xbox` (`pad_layout = nintendo`) or `romfs-patches-xbox-label` (`xbox`, `auto`) next to `romfs-index.bin` in place of `romfs-patches` (`runtime/romfs.c`, `overlay_init`; `runtime/input_map.c`, `input_prompts_folder`): the same 60 fps patches, with the prompts drawn from the game's own files by `tools/make_romfs_patches.py --prompts xbox` or `--prompts xbox-label` (`tools/button_icons.py`, after MacNativeGames' `make_button_icons.py`), which the setup runs next to the plain one:
+- every prompt but the button guide in play is text in the button font, `scft/External.bfotf` in each `Font/Font*.Nin_NX_NVN.bfarc.zs`. fontTools writes the font again, so the archives grow and the folder also carries the resource size table (`System/Resource/ResourceSizeTable.Product....rsizetable.zs`) with their entries raised by as much; without that the game stops at start.
+- the button guide in play draws L and ZL from the images `Nt_KeyTexL_00` and `Nt_KeyTexZL_00` of the Common archive's `timg/__Combined.bntx`, painted with the same icons (keyboard prompts also paint `Nt_KeyTexA/B/X/Y_00` and `Nt_KeyTexLSt_00/01`).
-- every prompt but the button guide in play is text in the button font, `scft/External.bfotf` in each `Font/Font*.Nin_NX_NVN.bfarc.zs`: glyphs U+E040 to U+E043 (A B X Y), U+E044 to U+E047 (L R ZL ZR), U+E04B and U+E04C (Plus, Minus), U+E049 and U+E060 to U+E065 (the d-pad). fontTools writes the font again, so the archives grow and the folder also carries the resource size table (`System/Resource/ResourceSizeTable.Product....rsizetable.zs`) with their entries raised by as much; without that the game stops at start.
-- the button guide in play draws L and ZL from the images `Nt_KeyTexL_00` and `Nt_KeyTexZL_00` of the Common archive's `timg/__Combined.bntx`, painted with the same icons. The action prompt draws its button from `Nt_KeyTexA/B/X/Y_00`, whose image data is swapped for `nintendo`, and the diamond's letters are text the game takes from `LayoutMsg/DoCommand_00.msbt` of each language's `Mals` archive (and the layout's own text panes behind it), swapped the same way.
-
-The Switch prompts stay, with a note under *Button prompts* and a `RomFS overlay: ...` line in the log, when the folder is missing (an install made by an older setup, or one where the setup could not prepare it and showed a warning; running the setup again on the install folder writes it), when it fails its check, with a game resolution of another shape than 16:9 (its own UI layout wins; 16:10 and 21:9), and when the `romfs_mods` folder replaces one of its files: `romfs-patches` then loads in its place, where two overlays replacing one file would stop the game. In a source build, `.venv-py314\Scripts\python tools\make_romfs_patches.py --version --prompts xbox` (and `--prompts xbox-label`) writes `build\\romfs-patches-xbox` (`-label`); it needs fontTools from `tools/requirements-py314.txt`. `--xbox-prompts` is the older name of `--prompts xbox`.
+The Switch prompts stay, with a note under *Button prompts* and a `RomFS overlay: ...` line in the log, when the folder is missing (an install made by an older setup, or one where the setup could not prepare it and showed a warning; running the setup again on the install folder writes it), when it fails its check, with a game resolution of another shape than 16:9 (its own UI layout wins; 16:10 and 21:9), and when the `romfs_mods` folder replaces one of its files: `romfs-patches` then loads in its place. In a source build, `.venv-py314\Scripts\python tools\make_romfs_patches.py --version --prompts keyboard` (or `xbox`, `xbox-label`) writes `build\\romfs-patches-