A new controller no longer inherits the previous one's gyro bias, and a calibration where the pad moved is rejected

The bias reset in sensors_ready only ran when sensor_pad was nonzero, but scan_pads zeroes it on every disconnect,
so a pad connected after another one kept the old pad's bias, and an attempt still collecting finished on the new
pad under the old pad's name. The bias was also loaded only once, at the first StartSixAxisSensor.

- input.c pad_bias_select runs for every pad the sensor code sees: it drops the old bias and attempt, then loads the
  saved bias when gyro_bias_pad names this pad. scan_pads drops them on disconnect. The bias loads with
  gyro_calibration off too, so turning the switch on mid-run applies it.
- The pad identity is the name plus an FNV-1a hash of SDL's serial when the pad reports one, so two pads of one
  model no longer share a bias; the hash keeps the Bluetooth address out of totk.ini and the log.
- An attempt skips 0.5 s of samples (the button press shakes the pad), then averages 1 s. It is rejected when the
  largest per-axis standard deviation exceeds 0.04 rad/s; a steady bias of any size passes. The row then says the
  controller moved (new UI_MENU_GYRO_CALIBRATE_MOVED in all four catalogs) and the bias in effect stays.
- The timeout counts 5 s without a sample, not 5 s from the start.
- Calibrate gyro starts the sensor thread when the game has not yet (title screen) instead of doing nothing.
- Row states are an enum (MOTION_CALIB_*); motion_calibrate_start takes the clock, and only the gamepad's fusion
  instance writes totk.ini, so --probe-services now checks a measured bias, a rejected attempt and the timeout.
- The duplicate copy_name in motion_fusion.c is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-15 09:32:05 +03:00
1 parent 0a792f22c1
commit 0efa904d90
9 files changed
+267 -182

No files matched your search

+76 -55
View File
@@ -425,6 +425,9 @@ static void scan_pads(void) {
if (pad && SDL_GamepadConnected(pad)) return;
if (pad) {
SDL_CloseGamepad(pad); pad = NULL; sensor_pad = 0; sensor_ok = 0;
// Its bias and an attempt collecting from it go with it; the next pad gets its own in pad_bias_select.
motion_bias_reset(motion_global());
calib_pad_name[0] = 0;
plat_xchg32(&pad_layout_shown, -1);
for (int i = 0; i < 4; i++) plat_xchg32(&pad_labels[i], PAD_LABEL_NONE);
rt_log("input: gamepad disconnected");
@@ -957,17 +960,56 @@ void input_motion_axes(const float a[3], const float g[3], double acc[3], double
gyro[2] = g[1];
}
// Copy a controller name into cap bytes without splitting a UTF-8 sequence: a name cut mid-character would show as
// garbage in the menu and be written to totk.ini the same way.
static void copy_name(char *dst, size_t cap, const char *src) {
if (!src) src = "";
size_t n = strlen(src);
if (n >= cap) {
n = cap - 1;
while (n > 0 && ((unsigned char)src[n] & 0xC0) == 0x80) n--; // back off to the start of the character
}
memcpy(dst, src, n);
dst[n] = 0;
}
// Under lock: the open gamepad's identity for the saved bias. Its name, plus a hash of SDL's serial when the pad
// reports one (Switch and PlayStation pads report their Bluetooth address), so two controllers of one model keep their
// own bias; the hash keeps the address out of totk.ini and the log, which players post with bug reports.
static void pad_identity(char *dst, size_t cap) {
const char *name = SDL_GetGamepadName(pad), *serial = SDL_GetGamepadSerial(pad);
char id[160];
if (serial && *serial) {
uint32_t h = 2166136261u; // FNV-1a
for (const char *c = serial; *c; c++) h = (h ^ (uint8_t)*c) * 16777619u;
snprintf(id, sizeof id, "%s #%08x", name ? name : "", h);
} else snprintf(id, sizeof id, "%s", name ? name : "");
copy_name(dst, cap, id);
}
// Under lock, for each gamepad the sensor code sees (a first one, or one connected after another went away): the
// previous controller's bias and any attempt still collecting from it are dropped, then the bias saved in totk.ini is
// taken when gyro_bias_pad names this controller. It is taken with gyro_calibration off too: the switch only decides
// whether motion_feed subtracts it, so turning the switch on mid-run applies the saved bias.
static void pad_bias_select(void) {
char id[128];
pad_identity(id, sizeof id);
motion_bias_reset(motion_global());
calib_pad_name[0] = 0;
const char *saved = cfg_get("TOTK_GYRO_BIAS_PAD");
if (saved && !strcmp(saved, id)) {
motion_bias_load(motion_global());
copy_name(calib_pad_name, sizeof calib_pad_name, SDL_GetGamepadName(pad));
rt_log("input: gyro bias loaded from totk.ini for %s", calib_pad_name);
}
}
// Under lock: (re)enable the sensors when the open gamepad changed; 1 if it has both and they are on.
static int sensors_ready(void) {
SDL_JoystickID id = SDL_GetGamepadID(pad);
if (id == sensor_pad) return sensor_ok;
// A different controller: the old one's bias estimate does not apply. Only on a change, so the first call does
// not drop a bias that input_motion_start loaded from totk.ini for this controller.
if (sensor_pad) {
motion_bias_reset(motion_global());
calib_pad_name[0] = 0;
}
sensor_pad = id;
pad_bias_select();
// Nothing drains the SDL event queue: no event per sample (the data still updates, SDL_SendJoystickSensor)
SDL_SetEventEnabled(SDL_EVENT_GAMEPAD_SENSOR_UPDATE, false);
sensor_ok =SDL_GamepadHasSensor(pad, SDL_SENSOR_GYRO) && SDL_GamepadHasSensor(pad, SDL_SENSOR_ACCEL) &&
@@ -1015,58 +1057,43 @@ static void motion_main(void *arg) {
}
}
// Copy a controller name into cap bytes without splitting a UTF-8 sequence: a name cut mid-character would show as
// garbage in the menu and be written to totk.ini the same way.
static void copy_name(char *dst, size_t cap, const char *src) {
if (!src) src = "";
size_t n = strlen(src);
if (n >= cap) {
n = cap - 1;
while (n > 0 && ((unsigned char)src[n] & 0xC0) == 0x80) n--; // back off to the start of the character
// Under lock: the sensor thread, once; -1 when SDL is not running.
static void motion_thread_start(void) {
if (!state) start();
if (motion_thread) return;
if (!input_motion_enabled()) rt_log("input: motion off (TOTK_MOTION=0); it can be turned on while the game runs");
if (state != 1) motion_thread = -1;
else {
if (!plat_thread_spawn(motion_main, NULL, "motion", 0)) rt_fatal("input: cannot start the motion thread");
motion_thread = 1;
}
memcpy(dst, src, n);
dst[n] = 0;
}
void input_motion_start(void) {
plat_mutex_lock(&lock);
if (!state) start();
if (!motion_thread) {
// Load the calibrated bias from totk.ini when it was measured for the controller that is connected now and
// the switch is on; the keys stay in the file either way, so turning the switch back on restores the bias.
const char *name = pad && SDL_GamepadConnected(pad) ? SDL_GetGamepadName(pad) : NULL;
const char *calib_pad = cfg_get("TOTK_GYRO_BIAS_PAD");
if (name && calib_pad && strcmp(calib_pad, name) == 0 && motion_calibration_enabled()) {
motion_bias_load(motion_global());
copy_name(calib_pad_name, sizeof calib_pad_name, name);
rt_log("input: gyro bias loaded from totk.ini for %s", name);
} else {
// No usable bias for this controller: drop whatever an earlier start left behind, so the row does not
// report another controller's calibration.
calib_pad_name[0] = 0;
motion_bias_reset(motion_global());
}
if (!input_motion_enabled()) rt_log("input: motion off (TOTK_MOTION=0); it can be turned on while the game runs");
if (state != 1) motion_thread = -1;
else {
if (!plat_thread_spawn(motion_main, NULL, "motion", 0)) rt_fatal("input: cannot start the motion thread");
motion_thread = 1;
}
}
motion_thread_start();
plat_mutex_unlock(&lock);
}
void input_motion_calibrate(void) {
plat_mutex_lock(&lock);
if (!input_gyro_calibration_enabled()) {
rt_log("input: calibrate gyro ignored: gyro_calibration is off");
} else if (motion_thread != 1 || !pad) {
// Nothing would collect the samples; say so rather than leaving the row looking pressed.
rt_log("input: calibrate gyro ignored: %s", motion_thread != 1 ? "motion is off (TOTK_MOTION=0)" : "no gamepad");
} else {
const char *name = SDL_GetGamepadName(pad);
copy_name(calib_pad_name, sizeof calib_pad_name, name);
motion_calibrate_start(motion_global(), calib_pad_name);
// The menu can run before the game starts the six-axis sensor (the title screen): the thread starts here then.
if (input_gyro_calibration_enabled() && input_motion_enabled()) motion_thread_start();
const char *why = NULL;
if (!input_gyro_calibration_enabled()) why = "gyro_calibration is off";
else if (!input_motion_enabled()) why = "motion is off (TOTK_MOTION=0)";
else if (motion_thread != 1) why = "no SDL gamepad support";
else if (!pad || !SDL_GamepadConnected(pad)) why = "no gamepad";
// sensors_ready here, not at the sensor thread's next look at a new pad: that look selects the pad's bias and
// would cancel the attempt that starts below.
else if (!sensors_ready()) why = "the gamepad has no gyro and accelerometer";
if (why) rt_log("input: calibrate gyro ignored: %s", why);
else {
char id[128];
pad_identity(id, sizeof id);
copy_name(calib_pad_name, sizeof calib_pad_name, SDL_GetGamepadName(pad));
motion_calibrate_start(motion_global(), id, kern_now_ns());
rt_log("input: gyro calibration started on %s", calib_pad_name);
}
plat_mutex_unlock(&lock);
}
@@ -1075,13 +1102,7 @@ int input_gyro_calibration_enabled(void) { return motion_calibration_enabled();
int input_motion_calib_state(char *pad, size_t cap) {
plat_mutex_lock(&lock);
int st = 0;
if (motion_thread == 1) {
st = motion_calib_poll(motion_global(), kern_now_ns()); // also ends an attempt whose thread stalled
// Loaded from totk.ini at start, no attempt in this run; with the switch off the bias does not apply, so
// the row reads as before any calibration.
if (st == 0 && calib_pad_name[0] && input_gyro_calibration_enabled()) st = 2;
}
int st = motion_thread == 1 ? motion_calib_poll(motion_global(), kern_now_ns()) : MOTION_CALIB_NONE;
if (pad && cap) snprintf(pad, cap, "%s", calib_pad_name);
plat_mutex_unlock(&lock);
return st;