Offer Restart now under the button prompts while they change after restart

The row opens a confirmation; Restart writes totk.ini, closes the game as Quit game does and starts totk.exe again
with the same command line and environment (plat_restart). The new process waits until the old one is gone before it
moves the log and opens the saves (plat_restart_wait), and the confirmation waits while the game writes a save.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Deon FoucheandClaude Opus 5.5 committed 2026-10-31 20:50:17 +10:00
1 parent 19fb3a75c9
commit 703e721bf8
10 files changed
+138 -11

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@ static const char *const known[] = {
"PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES",
"PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC",
"RENDER_SCALE", "REPLAY_ASYNC",
"REPLAY_CHECK", "RESOLVE_CHECK", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE",
"SHADER_MEMO", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "SWKBD_ANSWER",
"SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64",
+19 -2
View File
@@ -28,6 +28,7 @@
#include "gfx.h"
#include "input.h"
#include "nvn_api.h"
#include "platform/platform.h"
#include "ui/menu.h"
#include "vk.h"
@@ -47,6 +48,7 @@ static HWND g_hwnd;
static HANDLE g_wnd_ready;
static VkSurfaceKHR g_surface;
static volatile LONG g_dirty; // WM_SIZE seen; swapchain stale
static volatile LONG g_restart; // gfx_window_request_restart: WM_CLOSE starts totk.exe again
static volatile LONG g_focused = 1;
static volatile LONG g_focus_msgs;
static CRITICAL_SECTION g_start_lock; // gfx_window_start / vsync start
@@ -318,6 +320,19 @@ void gfx_window_request_close(void) {
if (g_hwnd) PostMessageW(g_hwnd, WM_CLOSE, 0, 0);
}
void gfx_window_request_restart(void) {
InterlockedExchange(&g_restart, 1);
gfx_window_request_close();
}
// The restart, once WM_CLOSE has written the saves and the pipeline cache: totk.exe again with this process's command
// line and environment (plat_restart), and the new one waits until this one is gone (main.c) before it moves the log
// and opens the RomFS and the saves.
static void relaunch(void) {
SetEnvironmentVariableA("TOTK_REEXEC", NULL); // the new process counts its own address-space retries (memory.c)
if (!plat_restart()) rt_log("gfx: the restart could not start totk.exe again (error %lu)", GetLastError());
}
// ---- refresh rate of the window's monitor (TOTK_FPS_LIMIT=display). The window thread measures it when the window
// opens, lands on another monitor or the display mode changes; readers take the stored value. QueryDisplayConfig gives
// the exact rational rate (59.94, 143.998); EnumDisplaySettings' whole hertz is the fallback.
@@ -447,11 +462,13 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) {
return 0;
}
case WM_CLOSE:
// The process ends with the window, as in MNG, once a save Commit still running in the background is done.
rt_log("gfx: window closed; exiting");
// The process ends with the window, as in MNG, once a save Commit still running in the background is done;
// after a restart request a new totk.exe takes over (relaunch).
rt_log("gfx: window closed; %s", g_restart ? "restarting" : "exiting");
fs_save_flush();
vk_pipeline_save();
prof_write(); // ExitProcess runs no atexit handlers
if (g_restart) relaunch();
ExitProcess(0);
case WM_ERASEBKGND:
return 1;
+3
View File
@@ -512,6 +512,9 @@ static void portable_paths(RtConfig *c) {
int main(int argc, char **argv) {
mem_init(); // first: claim the guest address space before anything else can land there; reads totk.ini
// A restart from the settings menu (gfx_window.c relaunch) starts this process while the one it replaces is still
// exiting: its log, RomFS and saves are free once it is gone.
if (plat_restart_wait(30000) < 0) rt_log("restart: the previous process still runs after 30 s; starting anyway");
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
AddVectoredExceptionHandler(0, crash_handler);
+8
View File
@@ -129,3 +129,11 @@ PlatChild *plat_child_start(const char *exe, const char *arg, uint32_t *err);
void plat_child_stop(PlatChild *c); // kills the child and frees c
int plat_child_write(PlatChild *c, const void *data, size_t n); // 1 when all n bytes were written
long plat_child_read(PlatChild *c, void *buf, size_t cap); // bytes read (at least 1); 0 or negative: pipe broke
// ---- restart: this program again, in a new process that starts once this one is gone
// Starts this executable again with the same command line, environment and working folder, and no handle of this
// process. 1 when it started; the caller then exits.
int plat_restart(void);
// In a process plat_restart started: waits, at most timeout_ms, until the process that started it has exited. 1 after
// the wait, -1 on timeout, 0 when there is nothing to wait for (not started by plat_restart, or that process is gone).
int plat_restart_wait(uint32_t timeout_ms);
+42
View File
@@ -706,3 +706,45 @@ long plat_child_read(PlatChild *c, void *buf, size_t cap) {
if (!ReadFile(c->out, buf, (DWORD)cap, &got, NULL)) return -1;
return (long)got;
}
// ---- restart
// TOTK_RESTART_WAIT names the process that called plat_restart, as its id and creation time: a process that has only
// reused the id is not waited for. Waiting keeps the new process off the old one's files (the log it moves at start,
// the saves) until the old one is gone; no handle is inherited, so none of them stays open in the new one either.
#define RESTART_WAIT "TOTK_RESTART_WAIT"
static unsigned long long process_created(HANDLE h) {
FILETIME c, e, k, u;
return GetProcessTimes(h, &c, &e, &k, &u) ? (unsigned long long)c.dwHighDateTime << 32 | c.dwLowDateTime : 0;
}
int plat_restart(void) {
char wait[48];
snprintf(wait, sizeof wait, "%lu.%llu", GetCurrentProcessId(), process_created(GetCurrentProcess()));
SetEnvironmentVariableA(RESTART_WAIT, wait);
STARTUPINFOW si = {.cb = sizeof si};
PROCESS_INFORMATION pi;
wchar_t *cmd = _wcsdup(GetCommandLineW()); // CreateProcessW may write its command line
BOOL ok = cmd && CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
free(cmd);
SetEnvironmentVariableA(RESTART_WAIT, NULL);
if (!ok) return 0;
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 1;
}
int plat_restart_wait(uint32_t timeout_ms) {
char v[48];
DWORD n = GetEnvironmentVariableA(RESTART_WAIT, v, sizeof v);
if (!n) return 0;
SetEnvironmentVariableA(RESTART_WAIT, NULL); // the processes this one starts do not wait again
unsigned long pid = 0;
unsigned long long created = 0;
if (n >= sizeof v || sscanf(v, "%lu.%llu", &pid, &created) != 2) return 0;
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (!h) return 0; // already gone
int r = process_created(h) != created ? 0 : WaitForSingleObject(h, timeout_ms) == WAIT_OBJECT_0 ? 1 : -1;
CloseHandle(h);
return r;
}
+49 -7
View File
@@ -561,6 +561,9 @@ static void tab_sound(float scale) {
opt_end();
}
// A confirmation is open (Controls: restart, Advanced: reset): B and Esc close it, not the menu; L / R stay on the page.
static bool g_modal;
// The note under the layout: Automatic with a gamepad connected, the layout it picked ("in use: Xbox"). NULL otherwise.
static const char *pad_layout_note(void) {
int got = input_pad_layout();
@@ -575,9 +578,10 @@ static const char *pad_layout_note(void) {
// layout, as a layout that puts A on the right button needs the face buttons named the other way round), but the game
// reads its fonts and UI archive once at start (romfs.c overlay_init). While the folder in use differs: "files
// missing" when the setup did not write the one asked for, why it was left out at start (damaged, a wide game
// resolution, a RomFS mod with the same files, TOTK_ROMFS_PATCHES), else "prompts change after restart". NULL when
// they match.
static const char *prompts_note(void) {
// resolution, a RomFS mod with the same files, TOTK_ROMFS_PATCHES), else "prompts change after restart", and only then
// *restart is set: a restart would bring the prompts asked for. NULL when they match.
static const char *prompts_note(bool *restart) {
*restart = false;
const char *layout = cfg_value("TOTK_PAD_LAYOUT"), *used = romfs_prompts();
const char *want = input_prompts_folder(input_prompts_pick(cfg_get("TOTK_PROMPTS"), layout), layout);
if (want == used || (want && used && !strcmp(want, used))) return nullptr;
@@ -587,16 +591,53 @@ static const char *prompts_note(void) {
case ROMFS_PROMPTS_WIDE: return menu_text(UI_MENU_PROMPTS_OFF_WIDE);
case ROMFS_PROMPTS_MODS: return menu_text(UI_MENU_PROMPTS_OFF_MODS);
case ROMFS_PROMPTS_PATCHES: return menu_text(UI_MENU_PROMPTS_OFF_PATCHES);
default: return menu_text(UI_MENU_PROMPTS_RESTART);
default: *restart = true; return menu_text(UI_MENU_PROMPTS_RESTART);
}
}
// The confirmation of "Restart now", over the menu; Cancel has the cursor first. Restart writes totk.ini, then closes
// the game as Quit does and starts it again (gfx_window_request_restart). It waits while the game writes a save, and
// the game stays when totk.ini cannot be written, as the new prompts would not come back.
static void restart_confirm(void) {
g_modal = false;
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f));
if (!ImGui::BeginPopupModal("##restart_confirm", nullptr,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize))
return;
g_modal = true;
ImGui::TextUnformatted(menu_text(UI_MENU_RESTART_CONFIRM));
bool saving = fs_save_busy() != 0;
if (saving || !cfg_last_save_ok())
ImGui::TextColored(ImVec4(0.95f, 0.45f, 0.35f, 1), "%s",
menu_text(saving ? UI_MENU_RESTART_SAVING : UI_MENU_SAVE_FAILED));
ImGui::Spacing();
const char *yes = menu_text(UI_MENU_RESTART_YES), *no = menu_text(UI_MENU_RESET_NO);
const ImGuiStyle &st = ImGui::GetStyle();
float bw = fmaxf(ImGui::CalcTextSize(yes).x, ImGui::CalcTextSize(no).x) + st.FramePadding.x * 4;
bool close = ImGui::Button(no, ImVec2(bw, 0));
if (ImGui::IsWindowAppearing()) ImGui::SetItemDefaultFocus();
ImGui::SameLine();
ImGui::BeginDisabled(saving);
if (ImGui::Button(yes, ImVec2(bw, 0)) && cfg_save()) gfx_window_request_restart();
ImGui::EndDisabled();
if (ImGui::IsKeyPressed(ImGuiKey_Escape, false) || ImGui::IsKeyPressed(ImGuiKey_GamepadFaceRight, false))
close = true;
if (close) {
ImGui::CloseCurrentPopup();
opt_focus_key("##restart"); // back on the row it came from
}
ImGui::EndPopup();
}
static void tab_controls(float scale) {
opt_begin("##controls", scale);
static const UiTextId layout[] = {UI_MENU_PAD_LAYOUT_AUTO, UI_MENU_PAD_LAYOUT_NINTENDO, UI_MENU_PAD_LAYOUT_XBOX};
opt_choice("TOTK_PAD_LAYOUT", UI_MENU_PAD_LAYOUT, layout, 3, UI_MENU_PAD_LAYOUT_DESC, pad_layout_note());
static const UiTextId prompts[] = {UI_MENU_PROMPTS_NINTENDO, UI_MENU_PROMPTS_XBOX};
opt_choice("TOTK_PROMPTS", UI_MENU_PROMPTS, prompts, 2, UI_MENU_PROMPTS_DESC, prompts_note());
bool restart;
const char *note = prompts_note(&restart);
opt_choice("TOTK_PROMPTS", UI_MENU_PROMPTS, prompts, 2, UI_MENU_PROMPTS_DESC, note);
bool ask = restart && opt_button("##restart", UI_MENU_RESTART, UI_MENU_RESTART_DESC);
opt_toggle("TOTK_MOTION", UI_MENU_MOTION, UI_MENU_MOTION_DESC);
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");
@@ -606,6 +647,8 @@ static void tab_controls(float scale) {
binds_rows(scale);
opt_end();
binds_after_page();
if (ask) ImGui::OpenPopup("##restart_confirm"); // at the level BeginPopupModal looks it up
restart_confirm();
}
static void tab_menu(float scale) {
@@ -616,7 +659,6 @@ static void tab_menu(float scale) {
}
// ---- Advanced: the testing and troubleshooting keys, the bug report helpers and the reset of every setting
static bool g_modal; // the reset confirmation is open: B and Esc close it, not the menu; L / R stay on the page
// A thread count with no fixed default: Automatic (unset), then the common counts.
static void row_threads(const char *key, UiTextId label, UiTextId desc) {
@@ -849,7 +891,7 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) {
ImGui::SameLine();
if (ImGui::Button(quit, ImVec2(bw, 0))) gfx_window_request_close();
bool was_modal = g_modal || binds_modal();
if (g_tab != TAB_ADVANCED) g_modal = false;
if (g_tab != TAB_ADVANCED && g_tab != TAB_CONTROLS) g_modal = false;
if (!was_modal && (ImGui::IsKeyPressed(ImGuiKey_Escape, false) || ImGui::IsKeyPressed(ImGuiKey_GamepadFaceRight, false)))
set_open(0);
ImGui::End();
+1
View File
@@ -57,6 +57,7 @@ void menu_vk_release(void); // before the device goes away (gfx_wi
// ---- what the menu asks of the platform side (gfx_window.c)
void gfx_window_apply_settings(void); // TOTK_FULLSCREEN or TOTK_WINDOW_SIZE changed: the window follows
void gfx_window_request_close(void); // as the window's close button: the process ends
void gfx_window_request_restart(void); // the same, then totk.exe starts again with the same command line
int gfx_window_monitor_size(int *w, int *h); // pixels of the monitor the window is on; 0 when unknown
double gfx_window_refresh_hz(void); // its refresh rate (TOTK_FPS_LIMIT=display); 0 when unknown
unsigned gfx_window_dpi(void); // the window's DPI (96 at 100% display scale); any thread
+12
View File
@@ -122,6 +122,12 @@ static const char *const text_en[UI_TEXT_COUNT] = {
[UI_MENU_PROMPTS_OFF_WIDE] = "off with a wide game resolution",
[UI_MENU_PROMPTS_OFF_MODS] = "off: a RomFS mod replaces their files",
[UI_MENU_PROMPTS_OFF_PATCHES] = "off while TOTK_ROMFS_PATCHES is set",
[UI_MENU_RESTART] = "Restart now",
[UI_MENU_RESTART_DESC] = "Closes the game and starts it again with the new prompts. Progress since your last save "
"is lost.",
[UI_MENU_RESTART_CONFIRM] = "Restart the game now? Progress since your last save is lost.",
[UI_MENU_RESTART_YES] = "Restart",
[UI_MENU_RESTART_SAVING] = "The game is saving: Restart is possible in a moment.",
[UI_MENU_STICK_DEADZONE] = "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.",
[UI_MENU_MOUSE_SENS] = "Mouse camera sensitivity",
@@ -373,6 +379,12 @@ static const char *const text_ru[UI_TEXT_COUNT] = {
[UI_MENU_PROMPTS_OFF_WIDE] = "выкл. при широком разрешении игры",
[UI_MENU_PROMPTS_OFF_MODS] = "выкл.: мод RomFS заменяет их файлы",
[UI_MENU_PROMPTS_OFF_PATCHES] = "выкл., пока задан TOTK_ROMFS_PATCHES",
[UI_MENU_RESTART] = "Перезапустить сейчас",
[UI_MENU_RESTART_DESC] = "Закрывает игру и запускает её снова с новыми подсказками. Прогресс после последнего "
"сохранения будет потерян.",
[UI_MENU_RESTART_CONFIRM] = "Перезапустить игру сейчас? Прогресс после последнего сохранения будет потерян.",
[UI_MENU_RESTART_YES] = "Перезапустить",
[UI_MENU_RESTART_SAVING] = "Игра сохраняется: перезапуск будет доступен через мгновение.",
[UI_MENU_STICK_DEADZONE] = "Мёртвая зона стиков",
[UI_MENU_STICK_DEADZONE_DESC] = "Насколько нужно отклонить стик геймпада, чтобы игра это заметила. Увеличьте, если персонаж или камера двигаются сами.",
[UI_MENU_MOUSE_SENS] = "Чувствительность мыши",
+2
View File
@@ -63,6 +63,8 @@ typedef enum {
// the notes under the prompts: their files are missing or damaged, they change after restart, or why they are off
UI_MENU_PROMPTS_MISSING, UI_MENU_PROMPTS_FAILED, UI_MENU_PROMPTS_RESTART, UI_MENU_PROMPTS_OFF_WIDE,
UI_MENU_PROMPTS_OFF_MODS, UI_MENU_PROMPTS_OFF_PATCHES,
// "Restart now" under the prompts while they change after restart, and its confirmation (Cancel: UI_MENU_RESET_NO)
UI_MENU_RESTART, UI_MENU_RESTART_DESC, UI_MENU_RESTART_CONFIRM, UI_MENU_RESTART_YES, UI_MENU_RESTART_SAVING,
UI_MENU_STICK_DEADZONE, UI_MENU_STICK_DEADZONE_DESC,
UI_MENU_MOUSE_SENS, UI_MENU_MOUSE_SENS_DESC,