From dda22f6e0110a9c89d0b51cbf5d2dcb70f858bdb Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Fri, 23 Oct 2026 23:12:15 +0300 Subject: [PATCH] Add rumble strength, curve and trigger settings for Xbox pads Co-Authored-By: Claude Opus 5.5 --- docs/controls.md | 2 +- runtime/cfg.c | 3 ++- runtime/cfg_probe.c | 6 ++++++ runtime/input.h | 4 ++++ runtime/input_win.c | 18 +++++++++++++++++- runtime/ov_input.c | 28 +++++++++++++++++++++++++--- totk.ini.example | 5 +++++ 7 files changed, 60 insertions(+), 6 deletions(-) diff --git a/docs/controls.md b/docs/controls.md index 3d458f0e..504a7e56 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -1,6 +1,6 @@ # Controls -Input comes from the first SDL3 gamepad and the keyboard, merged into one Switch controller (`runtime/input_win.c`). Buttons are ORed; each stick takes the source with the larger deflection. Keys count only while the game window is in the foreground. `TOTK_INPUT=none` disables both (automated runs). `TOTK_*` settings can also be set in `totk.ini` next to `totk.exe` (`input = none`, `motion = 0`, `rumble = 0`); the environment overrides the file. +Input comes from the first SDL3 gamepad and the keyboard, merged into one Switch controller (`runtime/input_win.c`). Buttons are ORed; each stick takes the source with the larger deflection. Keys count only while the game window is in the foreground. `TOTK_INPUT=none` disables both (automated runs). `TOTK_*` settings can also be set in `totk.ini` next to `totk.exe` (`input = none`, `motion = 0`, `rumble = 0`); the environment overrides the file. `TOTK_RUMBLE_STRENGTH` is the rumble strength in percent (0 to 300, default 130; 0 is the same as `rumble = 0`). `TOTK_RUMBLE_CURVE` is the exponent applied to the amplitude before the strength (0.2 to 1.5, default 0.6; lower makes weak pulses stronger, 1 is linear). `TOTK_RUMBLE_TRIGGERS=1` also drives the impulse triggers of an Xbox pad. | Switch | Gamepad (by position) | Keyboard | |---|---|---| diff --git a/runtime/cfg.c b/runtime/cfg.c index a5f0cd1a..da3d62c5 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -32,7 +32,8 @@ static const char *const known[] = { "MOVIE_PACE", "OPERATION_MODE", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_PREBUILD", "PIPE_SYNC", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_RENDER", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REEXEC", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", - "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RUMBLE", "SHADER_CACHE", "SHARED_FONTS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", + "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", + "SHADER_CACHE", "SHARED_FONTS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPLIT_DRAWS", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TIMER_1MS", "TRACE", "VK_NO_FLOAT64", "VK_PRESENT", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VS_OUTS", "XLATE", "XLATE_ID", "XLATE_PROCS", diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c index c478755f..df6c5fa7 100644 --- a/runtime/cfg_probe.c +++ b/runtime/cfg_probe.c @@ -5,6 +5,7 @@ #include "cfg.h" #include "host.h" +#include "input.h" #include "platform/platform.h" static int fails; @@ -52,6 +53,11 @@ int probe_cfg(void) { plat_file_delete(path); CHECK(cfg_load(path) == 0, "missing file is not an error"); CHECK(cfg_get("TOTK_FPS60") == NULL, "missing file leaves no keys"); + CHECK(rumble_motor(0, 0.6f, 1.3f) == 0 && rumble_motor(0.5f, 0.6f, 0) == 0, "rumble: zero amplitude or strength is silent"); + CHECK(rumble_motor(1, 0.6f, 3) == 65535 && rumble_motor(5, 1, 1) == 65535, "rumble: saturates at 65535"); + CHECK(rumble_motor(0.25f, 1, 1) > 16000 && rumble_motor(0.25f, 1, 1) < 16800, "rumble: curve 1, strength 100% is linear"); + CHECK(rumble_motor(0.1f, 0.6f, 1.3f) > rumble_motor(0.1f, 1, 1.3f), "rumble: curve lifts weak values"); + CHECK(rumble_motor(0.3f, 0.6f, 1.3f) > rumble_motor(0.3f, 0.6f, 1), "rumble: strength scales"); printf("probe-cfg: %s\n", fails ? "FAIL" : "ok"); return fails ? 1 : 0; } diff --git a/runtime/input.h b/runtime/input.h index ba13660b..c86222ee 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -14,6 +14,10 @@ void input_block(int on); // Rumble: SDL_RumbleGamepad on the open gamepad, low (heavy, left) and high (light, right) motor 0..65535 for ms // milliseconds. 1 if a gamepad with rumble took it. Callable from any thread. int input_rumble(uint16_t low, uint16_t high, uint32_t ms); +// Motor value for a normalised amplitude: pow(a, curve) * strength, saturated to 0..65535. Pure. +uint16_t rumble_motor(float a, float curve, float strength); +// TOTK_RUMBLE_TRIGGERS=1: input_rumble also drives the impulse triggers (Xbox) with the high motor's value. +int input_rumble_triggers_enabled(void); // Motion (TOTK_MOTION=0 turns it off). input_motion_start starts the sensor thread once: every 4 ms it reads the // gamepad's gyro and accelerometer (when it has them) and feeds motion_global(). Callable from any thread. diff --git a/runtime/input_win.c b/runtime/input_win.c index 2f06aa41..f35b2d9b 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -279,10 +279,26 @@ void input_poll_ui(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int poll_merged(buttons, lx, ly, rx, ry); } +uint16_t rumble_motor(float a, float curve, float strength) { + if (!(a > 0) || !(strength > 0)) return 0; + float v = powf(a > 1 ? 1 : a, curve) * strength * 65535.0f + 0.5f; + return v >= 65535.0f ? 65535 : (uint16_t)v; +} + +int input_rumble_triggers_enabled(void) { + const char *e = cfg_get("TOTK_RUMBLE_TRIGGERS"); + return e && !strcmp(e, "1"); +} + int input_rumble(uint16_t low, uint16_t high, uint32_t ms) { int ok = 0; + static int triggers = -1; + if (triggers < 0) triggers = input_rumble_triggers_enabled(); AcquireSRWLockExclusive(&lock); - if (state == 1 && pad && pad_rumble) ok = SDL_RumbleGamepad(pad, low, high, ms); + if (state == 1 && pad && pad_rumble) { + ok = SDL_RumbleGamepad(pad, low, high, ms); + if (triggers) SDL_RumbleGamepadTriggers(pad, high, high, ms); // unsupported pads return false: ignored + } ReleaseSRWLockExclusive(&lock); return ok; } diff --git a/runtime/ov_input.c b/runtime/ov_input.c index a4f26688..aa6ee6ea 100644 --- a/runtime/ov_input.c +++ b/runtime/ov_input.c @@ -186,6 +186,28 @@ static void once(volatile long *flag, const char *fmt, uint32_t v) { if (!InterlockedExchange(flag, 1)) rt_log(fmt, v); } +// TOTK_RUMBLE_STRENGTH (percent, 0..300, default 130; 0 = off) and TOTK_RUMBLE_CURVE (exponent on the amplitude, 0.2..1.5, +// default 0.6). HD rumble is strong at low amplitude, an ERM motor hardly turns there, so weak values are lifted by the +// curve (0.1 -> 0.25) and the strength scales the result (saturating at 65535; 0.66 and up reach full drive at 130 %). +static float rumble_strength = 1.3f, rumble_curve = 0.6f; +static volatile long rumble_cfg_done; +static float cfg_float(const char *name, float def, float lo, float hi) { + const char *e = cfg_get(name); + char *end; + double v = e ? strtod(e, &end) : 0; + if (!e || end == e || *end || v != v) return def; + return (float)(v < lo ? lo : v > hi ? hi : v); +} +static void rumble_cfg(void) { + if (rumble_cfg_done) return; + rumble_strength = cfg_float("TOTK_RUMBLE_STRENGTH", 130, 0, 300) / 100; + rumble_curve = cfg_float("TOTK_RUMBLE_CURVE", 0.6f, 0.2f, 1.5f); + int trig = input_rumble_triggers_enabled(); + if (!InterlockedExchange(&rumble_cfg_done, 1)) + rt_log("hid: rumble strength %.0f%%, curve %.2f, triggers %s", rumble_strength * 100, rumble_curve, trig ? "on" : "off"); +} +static uint16_t motor(float a) { return rumble_motor(a, rumble_curve, rumble_strength); } + static uint32_t pad_style_index(void) { return ov_handheld() ? STYLE_INDEX_HANDHELD : STYLE_INDEX_FULL_KEY; } // The SDK's id check (utility functions): players 1-8, other (0x10) and handheld (0x20); the public functions abort @@ -205,7 +227,8 @@ static void ov_vibration_handles(Cpu *c) { if (!out || max <= 0 || __builtin_popcount(style) != 1) rt_fatal("nn::hid::GetVibrationDeviceHandles: invalid arguments (count %d, styles %#x)", max, style); static volatile long asked; - if (env_zero("TOTK_RUMBLE")) { once(&asked, "hid: no vibration devices (TOTK_RUMBLE=%u)", 0); ret_x(c, 0); return; } + rumble_cfg(); + if (env_zero("TOTK_RUMBLE") || rumble_strength == 0) { once(&asked, "hid: no vibration devices (TOTK_RUMBLE=%u)", 0); ret_x(c, 0); return; } static const struct { uint8_t bit, n; uint32_t base; } t[] = { {0, 2, 0x3}, {1, 2, 0x4}, {2, 2, 0x5}, {3, 1, 0x6}, {4, 1, 0x10007}, {5, 1, 0x8}, {10, 1, 0xd}, {8, 2, 0x4}, {30, 2, 0x21}, {29, 2, 0x20}}; @@ -269,8 +292,6 @@ static float clamp01(float v) { return v > 0 ? (v < 1 ? v : 1) : 0; } // NaN - // Share of a band at f Hz that goes to the light (high-frequency, right) motor: none at 160 Hz and below (the low band's // neutral frequency), all at 320 Hz and above (the high band's), log scale between. static float light_share(float f) { return f > 160 ? (f < 320 ? log2f(f / 160) : 1) : 0; } -// An ERM motor hardly turns at low drive: lift weak values ((a + sqrt a) / 2: 0.1 -> 0.21, 0.5 -> 0.6, 1 -> 1). -static uint16_t motor(float a) { return (uint16_t)((a + sqrtf(a)) * 0.5f * 65535.0f + 0.5f); } // Switch HD rumble -> SDL_RumbleGamepad. Each side's two bands split by frequency between the heavy (low) and the light // (high) motor (light_share) and add up as energy per motor (root of the summed squares); the stronger side drives each @@ -278,6 +299,7 @@ static uint16_t motor(float a) { return (uint16_t)((a + sqrtf(a)) * 0.5f * 65535 // the game streams, so the pad stops by itself when the game stops sending (pause, freeze). Under vib_lock. static void rumble_update(uint64_t now) { float heavy = 0, light = 0; + rumble_cfg(); for (int s = 0; s < 2 && vib_allowed; s++) { if (!vib_at[s] || now - vib_at[s] > RUMBLE_STALE_NS) continue; const VibrationValue *v = &vib_last[s]; diff --git a/totk.ini.example b/totk.ini.example index 123b5593..a106a0d8 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -36,6 +36,11 @@ # Gamepad motion controls (0 = off) and vibration (0 = off). # motion = 1 # rumble = 1 +# Rumble strength in percent (0 to 300; 0 = off) and the exponent on the amplitude (0.2 to 1.5; lower = weak pulses +# stronger, 1 = linear). rumble_triggers = 1 also drives the impulse triggers of an Xbox pad. +# rumble_strength = 130 +# rumble_curve = 0.6 +# rumble_triggers = 0 [files] # Folder with RomFS mod files that replace or add game files. Default: none.