Add the settings defaults table, cfg_reset and a save status to cfg, and replace the unread fps_counter key with perf_overlay

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-26 08:59:48 +03:00
1 parent c6a7a28ab4
commit 2dd2a177a4
3 files changed
+213 -16

No files matched your search

+120 -16
View File
@@ -19,12 +19,13 @@
#define PREFIX_LEN 5
#define CFG_MAX_KEYS 512
typedef struct { char *key, *value; int dirty; } CfgEntry; // key upper-case, without the prefix
typedef struct { char *key, *value; int dirty, removed; } CfgEntry; // key upper-case, without the prefix; value NULL after cfg_reset
static CfgEntry entries[CFG_MAX_KEYS];
static int n_entries;
static unsigned version;
static PlatMutex set_lock = PLAT_MUTEX_INIT; // cfg_set, cfg_save
static PlatMutex set_lock = PLAT_MUTEX_INIT; // cfg_set, cfg_reset, cfg_save
static int save_ok = 1; // the last cfg_save_to wrote (or had nothing to write)
// Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is
// logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line.
@@ -34,11 +35,11 @@ static const char *const known[] = {
"BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y",
"BIND_ZL", "BIND_ZR", "CLOTH_REALTIME", "CORE_DIAG", "DESC_TEMPLATE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG",
"EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS",
"FLIGHT_THUMBS", "FPS60", "FPS_COUNTER", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "GPL", "GPL_DRAW_BUILD", "GPU",
"FLIGHT_THUMBS", "FPS60", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "GPL", "GPL_DRAW_BUILD", "GPU",
"GPU_INDEX", "GPU_NAME", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD",
"LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "MEMORY", "MENU_LANG", "MENU_TEST",
"MENU_TEST_DIR", "MINIMAL_LAYOUT", "MIRROR_GAP", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE",
"OPERATION_MODE", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT",
"OPERATION_MODE", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT",
"PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES",
"PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REEXEC", "REPLAY_ASYNC",
"REPLAY_CHECK", "RESOLVE_CHECK", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
@@ -52,11 +53,63 @@ static const char *const known[] = {
// The totk.ini section a key cfg_set may write goes to when the file lacks it (totk.ini.example's sections).
static const struct { const char *key, *section; } sections[] = {
{"FPS60", "frame rate"},
{"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"FPS_COUNTER", "display"}, {"VK_PRESENT", "display"},
{"MENU_LANG", "language"},
{"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"},
{"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"PERF_OVERLAY", "display"}, {"VK_PRESENT", "display"},
{"GPU_INDEX", "display"},
{"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"},
{"VOLUME", "audio"},
{"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"},
{"MOUSE_SENS", "input"},
{"MOUSE_SENS", "input"}, {"INPUT_RECORD", "input"},
{"ASYNC_PREEMPT", "performance"}, {"REPLAY_ASYNC", "performance"}, {"FREE_CORES", "performance"},
{"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"},
{"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"},
{"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"},
{"TEX_ALIAS", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"},
{"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"},
{"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"VK_VALIDATION", "performance"},
{"VK_DYN_VERTEX", "performance"}, {"VK_SUBGROUP_PIN", "performance"},
{"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"},
};
// The settings the menu shows: the value each consumer uses when the key is unset, and what the key accepts. Defaults
// are copied from the consumers (grep cfg_get("TOTK_<KEY>")); the consumers keep parsing as they do.
#define LIVE 1
#define RESTART 0
static const CfgMeta metas[] = {
// Screen
{"FULLSCREEN", "0", "0|1", LIVE}, {"WINDOW_SIZE", "1920x1080", NULL, LIVE},
{"VK_PRESENT", "mailbox", "mailbox|fifo|immediate", LIVE}, {"GPU_INDEX", "", NULL, RESTART},
{"PERF_OVERLAY", "off", "off|fps|full", LIVE},
// Game
{"FPS60", "1", "0|1", LIVE}, {"REALTIME", "1", "0|1", RESTART}, {"CLOTH_REALTIME", "1", "0|1", RESTART},
{"HIGHFPS_FIX", "1", "0|1", RESTART},
{"LANG", "", "ja|en-US|fr|de|it|es|zh-CN|ko|nl|pt|ru|zh-TW|en-GB|fr-CA|es-419|zh-Hans|zh-Hant|pt-BR", RESTART},
{"OPERATION_MODE", "docked", "docked|handheld", RESTART},
// Sound, Controls, Menu
{"VOLUME", "100", "0..100", LIVE},
{"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE},
{"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"MOUSE_SENS", "1", "0..10", LIVE},
{"MENU_LANG", "", "en|ru", LIVE},
// Advanced: scheduling
{"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART},
{"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART},
// shaders and pipelines
{"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "any", "strict|any|fullscreen", RESTART},
{"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART},
{"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART},
// textures and memory
{"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART},
{"TEX_ALIAS", "1", "0|1", RESTART},
// render fixes
{"LOD_GUARD", "1", "0|1", RESTART}, {"FIX_C0", "1", "0|1", RESTART}, {"FIX_ZERO", "1", "0|1", RESTART},
{"BIND_SKIP", "1", "0|1", RESTART}, {"SHADER_MEMO", "1", "0|1", RESTART}, {"DESC_TEMPLATE", "1", "0|1", RESTART},
{"PUSH_SKIP", "1", "0|1", RESTART}, {"SPLIT_DRAWS", "64", "0..1000", RESTART},
// Vulkan
{"VK_VALIDATION", "0", "0|1", RESTART}, {"VK_DYN_VERTEX", "", "0|1", RESTART},
{"VK_SUBGROUP_PIN", "1", "0|1", RESTART},
// diagnostics
{"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART},
{"INPUT_RECORD", "", NULL, RESTART},
};
static int is_known(const char *key) {
@@ -97,7 +150,7 @@ static void add(char *key, char *value, int line) {
return;
}
if (n_entries == CFG_MAX_KEYS) return;
entries[n_entries++] = (CfgEntry){_strdup(key), _strdup(value), 0};
entries[n_entries++] = (CfgEntry){_strdup(key), _strdup(value), 0, 0};
}
int cfg_load(const char *path) {
@@ -148,18 +201,54 @@ void cfg_set(const char *name, const char *value) {
int i = 0, n = n_entries;
while (i < n && strcmp(entries[i].key, key)) i++;
if (i < n) {
if (strcmp(entries[i].value, value)) {
if (!entries[i].value || strcmp(entries[i].value, value)) {
__atomic_store_n(&entries[i].value, _strdup(value), __ATOMIC_RELEASE); // the old string stays for readers
entries[i].dirty = 1;
}
entries[i].removed = 0; // a value set after cfg_reset replaces the line instead of deleting it
} else if (n < CFG_MAX_KEYS) {
entries[n] = (CfgEntry){_strdup(key), _strdup(value), 1};
entries[n] = (CfgEntry){_strdup(key), _strdup(value), 1, 0};
__atomic_store_n(&n_entries, n + 1, __ATOMIC_RELEASE);
}
__atomic_add_fetch(&version, 1, __ATOMIC_RELEASE);
plat_mutex_unlock(&set_lock);
}
int cfg_reset(const char *const *keys, int n) {
int done = 0;
plat_mutex_lock(&set_lock);
for (int k = 0; k < n; k++) {
char key[64], var[80];
snprintf(key, sizeof key, "%s", keys[k]);
const char *name = norm_key(key);
snprintf(var, sizeof var, PREFIX "%s", name);
if (getenv(var)) continue; // the environment wins; the file line stays
for (int i = 0; i < n_entries; i++)
if (!strcmp(entries[i].key, name)) {
if (!entries[i].value) break;
__atomic_store_n(&entries[i].value, NULL, __ATOMIC_RELEASE); // the old string stays for readers
entries[i].dirty = 0;
entries[i].removed = 1; // cfg_save_to deletes the key's lines
done++;
break;
}
}
if (done) __atomic_add_fetch(&version, 1, __ATOMIC_RELEASE);
plat_mutex_unlock(&set_lock);
return done;
}
int cfg_last_save_ok(void) { return __atomic_load_n(&save_ok, __ATOMIC_ACQUIRE); }
const CfgMeta *cfg_meta(const char *key) {
char k[64];
snprintf(k, sizeof k, "%s", key);
const char *name = norm_key(k);
for (size_t i = 0; i < sizeof metas / sizeof *metas; i++)
if (!strcmp(metas[i].key, name)) return &metas[i];
return NULL;
}
// ---- cfg_save: the file as lines, each with its line ending
typedef struct { const char *s; size_t n; } Line; // n includes the '\n' (and '\r') when present
@@ -252,8 +341,9 @@ static void put_entry(Out *o, const CfgEntry *e, const char *eol) {
int cfg_save_to(const char *path) {
plat_mutex_lock(&set_lock);
int n = n_entries, any = 0;
for (int i = 0; i < n; i++) any |= entries[i].dirty;
for (int i = 0; i < n; i++) any |= entries[i].dirty | entries[i].removed;
if (!any) {
__atomic_store_n(&save_ok, 1, __ATOMIC_RELEASE);
plat_mutex_unlock(&set_lock);
return 1;
}
@@ -264,7 +354,8 @@ int cfg_save_to(const char *path) {
Line *lines = calloc((size_t)nl + 1, sizeof *lines);
// after[i]: entries to write after line i (-1 = none); replace[i]: the entry whose value line i now carries
int *replace = malloc(sizeof(int) * ((size_t)nl + 1)), *after_key = malloc(sizeof(int) * (size_t)(n + 1));
if (!lines || !replace || !after_key) rt_fatal("cfg: out of memory writing totk.ini");
char *drop = calloc((size_t)nl + 1, 1); // lines of a reset key
if (!lines || !replace || !after_key || !drop) rt_fatal("cfg: out of memory writing totk.ini");
for (int i = 0, at = 0; i < nl; i++) {
size_t start = (size_t)at;
while ((uint64_t)at < size && text[at] != '\n') at++;
@@ -277,6 +368,11 @@ int cfg_save_to(const char *path) {
char t[1024], k[128], sec[128];
for (int e = 0; e < n; e++) {
after_key[e] = -2; // -2 clean, -1 append in a new section at the end, >= 0 insert after that line
if (entries[e].removed)
for (int i = 0; i < nl; i++) {
line_text(&lines[i], i == 0, t, sizeof t);
if (line_key(t, 0, k, sizeof k) && !strcmp(k, entries[e].key)) drop[i] = 1;
}
if (!entries[e].dirty) continue;
int last = -1;
for (int i = 0; i < nl; i++) {
@@ -304,7 +400,9 @@ int cfg_save_to(const char *path) {
}
Out o = {0};
for (int i = 0; i < nl; i++) {
if (replace[i] >= 0) {
if (drop[i]) {
// the line leaves the file; keys inserted after it still need a line ending before them
} else if (replace[i] >= 0) {
// keep the line's indentation and key spelling, put the new value after " = "
const char *s = lines[i].s;
size_t len = lines[i].n;
@@ -322,7 +420,7 @@ int cfg_save_to(const char *path) {
}
for (int e = 0; e < n; e++)
if (after_key[e] == i) {
if (!lines[i].n || lines[i].s[lines[i].n - 1] != '\n') puts_(&o, eol);
if (o.n && o.buf[o.n - 1] != '\n') puts_(&o, eol);
put_entry(&o, &entries[e], eol);
}
}
@@ -348,10 +446,12 @@ int cfg_save_to(const char *path) {
}
int ok = plat_file_write_atomic(path, o.buf ? o.buf : "", o.n);
if (ok)
for (int e = 0; e < n; e++) entries[e].dirty = 0;
for (int e = 0; e < n; e++) entries[e].dirty = entries[e].removed = 0;
else
rt_log("cfg: cannot write %s", path);
__atomic_store_n(&save_ok, ok ? 1 : 0, __ATOMIC_RELEASE);
free(o.buf);
free(drop);
free(after_key);
free(replace);
free(lines);
@@ -374,7 +474,11 @@ static int find_ini(void) {
return 1;
}
int cfg_save(void) { return find_ini() && cfg_save_to(ini_path); }
int cfg_save(void) {
if (find_ini()) return cfg_save_to(ini_path);
__atomic_store_n(&save_ok, 0, __ATOMIC_RELEASE);
return 0;
}
void cfg_init(void) {
if (!find_ini()) return;
+14
View File
@@ -27,3 +27,17 @@ int cfg_from_env(const char *name);
// when missing), comments and every other line stay byte for byte. 1 on success or with nothing to write.
int cfg_save(void);
int cfg_save_to(const char *path);
// Reset (the menu's "Reset all settings to defaults"). Removes the active line of each key (names without TOTK_, any
// case) from totk.ini at the next cfg_save and its value from memory, so the consumer's own default applies again; the
// commented example lines stay. Keys the environment sets are skipped. Bumps cfg_version. Returns how many it reset.
int cfg_reset(const char *const *keys, int n);
// 0 when the last cfg_save / cfg_save_to failed to write (the values stay set in memory, the next save tries again).
int cfg_last_save_ok(void);
// What the menu knows about a setting. key: without TOTK_, upper case. def: what the consumer uses when the key is
// unset ("" = no fixed default, the consumer picks per machine). values: "0|1", "mailbox|fifo|immediate", a range
// "0..300", or NULL for free text. live: applies at once; the others are read at start.
typedef struct { const char *key, *def, *values; int live; } CfgMeta;
// The row of key (prefix and case ignored); NULL for a key the menu does not show.
const CfgMeta *cfg_meta(const char *key);
+79
View File
@@ -9,6 +9,9 @@
#include "input.h"
#include "input_map.h"
#include "platform/platform.h"
#ifdef _WIN32
#include <windows.h>
#endif
static int fails;
#define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL cfg: %s\n", what); } } while (0)
@@ -128,6 +131,82 @@ int probe_cfg(void) {
got = plat_file_read(path, &got_n);
CHECK(got && !strcmp((const char *)got, "[audio]\nvolume = 90\n"), "new file: the section and the key");
free(got);
// Defaults table: the menu's "default" marks and Reset
const CfgMeta *m = cfg_meta("FPS60");
CHECK(m && EQ(m->def, "1") && m->live, "meta: fps60 defaults to 1 and is live");
m = cfg_meta("ASYNC_PREEMPT");
CHECK(m && EQ(m->values, "0|1|2") && !m->live, "meta: async_preempt takes 0|1|2 and needs a restart");
m = cfg_meta("totk_vk_present");
CHECK(m && EQ(m->key, "VK_PRESENT") && EQ(m->values, "mailbox|fifo|immediate") && m->live,
"meta: prefix and case are accepted");
m = cfg_meta("RUMBLE_STRENGTH");
CHECK(m && EQ(m->def, "130") && EQ(m->values, "0..300"), "meta: rumble strength 130 in 0..300");
m = cfg_meta("PERF_OVERLAY");
CHECK(m && EQ(m->def, "off") && EQ(m->values, "off|fps|full") && m->live, "meta: perf_overlay is live, off by default");
CHECK(cfg_meta("BIND_A") == NULL && cfg_meta("FPS_COUNTER") == NULL && cfg_meta("NO_SUCH") == NULL,
"meta: keys the menu does not show are NULL");
// cfg_reset: the key's active lines leave totk.ini, the commented example stays
static const char rbase[] = "[audio]\n# volume = 100\n[input]\nmotion = 0\n";
static const char *const vol[] = {"VOLUME"};
_putenv_s("TOTK_VOLUME", "");
CHECK(plat_file_write(path, rbase, sizeof rbase - 1), "write the reset ini");
CHECK(cfg_load(path) == 1, "load the reset ini");
cfg_set("TOTK_VOLUME", "40");
CHECK(cfg_save_to(path) == 1 && cfg_last_save_ok(), "reset: volume saved");
got = plat_file_read(path, &got_n);
CHECK(got && strstr((const char *)got, "\nvolume = 40\n"), "reset: the active line is in the file");
free(got);
unsigned v1 = cfg_version();
CHECK(cfg_reset(vol, 1) == 1, "reset: one key reset");
CHECK(cfg_version() > v1 && cfg_get("TOTK_VOLUME") == NULL, "reset: the value is gone in memory, the version moved");
CHECK(cfg_reset(vol, 1) == 0, "reset: a key already reset counts 0");
CHECK(cfg_save_to(path) == 1, "reset: save");
got = plat_file_read(path, &got_n);
CHECK(got && got_n == sizeof rbase - 1 && !memcmp(got, rbase, got_n) && strstr((const char *)got, "# volume = 100"),
"reset: no active volume line, the commented example and the other keys stay");
if (got && (got_n != sizeof rbase - 1 || memcmp(got, rbase, got_n))) fprintf(stderr, "---- saved:\n%s\n----\n", got);
free(got);
cfg_set("TOTK_VOLUME", "55");
CHECK(cfg_reset(vol, 1) == 1 && cfg_get("TOTK_VOLUME") == NULL, "reset: a value set after a save resets too");
cfg_set("TOTK_VOLUME", "60");
CHECK(cfg_save_to(path) == 1 && EQ(cfg_get("TOTK_VOLUME"), "60"), "reset then set before the save: the set wins");
got = plat_file_read(path, &got_n);
CHECK(got && strstr((const char *)got, "\nvolume = 60\n"), "reset then set: the new line is written");
free(got);
// a key the environment sets is skipped and keeps its file line
CHECK(cfg_load(path) == 1 && EQ(cfg_get("TOTK_VOLUME"), "60"), "reset: reload");
_putenv_s("TOTK_VOLUME", "70");
CHECK(cfg_reset(vol, 1) == 0, "reset: an environment-set key is skipped");
_putenv_s("TOTK_VOLUME", "");
CHECK(EQ(cfg_get("TOTK_VOLUME"), "60"), "reset: the skipped key keeps its value");
CHECK(cfg_save_to(path) == 1, "reset: save after a skip");
got = plat_file_read(path, &got_n);
CHECK(got && strstr((const char *)got, "\nvolume = 60\n"), "reset: the skipped key keeps its file line");
free(got);
// a failed save: the call fails, cfg_last_save_ok says so, the value stays set and a later save works
CHECK(cfg_last_save_ok() == 1, "save status: ok after a good save");
cfg_set("TOTK_VOLUME", "20");
char bad[1100];
snprintf(bad, sizeof bad, "%s", path);
strcpy(strrchr(bad, '\\') + 1, "no-such-dir\\cfg-probe.ini");
CHECK(cfg_save_to(bad) == 0 && cfg_last_save_ok() == 0 && EQ(cfg_get("TOTK_VOLUME"), "20"),
"save status: an unwritable path fails, the value stays");
cfg_set("TOTK_VOLUME", "21");
#ifdef _WIN32
CHECK(plat_file_write(path, rbase, sizeof rbase - 1), "write the read-only ini");
SetFileAttributesA(path, FILE_ATTRIBUTE_READONLY);
CHECK(cfg_save_to(path) == 0 && cfg_last_save_ok() == 0 && EQ(cfg_get("TOTK_VOLUME"), "21"),
"save status: a read-only file fails, cfg_get returns the new value");
SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL);
#endif
CHECK(cfg_save_to(path) == 1 && cfg_last_save_ok() == 1, "save status: the retry after the failure works");
got = plat_file_read(path, &got_n);
CHECK(got && strstr((const char *)got, "\nvolume = 21\n"), "save status: the retried save wrote the value");
free(got);
plat_file_delete(path);
cfg_load(path);
CHECK(rumble_motor(0, 0.6f, 1.3f) == 0 && rumble_motor(0.5f, 0.6f, 0) == 0, "rumble: zero amplitude or strength is silent");