From e1598554503991d483113b1c41f9baf6fcf2f0d2 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sun, 22 Nov 2026 19:06:47 -0700 Subject: [PATCH] ESP32: keys work at the menus after JK is quit and relaunched; console commands too tab5-emu runs the engine several times in one process, and desktop's per-launch OpenJKDF2_Globals_Reset doesn't run there, so state that assumes a fresh process carried over: - The message handler table. jk_esp_engine_startup reset only the count. stdControl's handler is never removed, and on ESP32 neither is Windows_GdiHandler, so their re-adds were refused as duplicates. Dispatch visits g_handler_count slots and stopped before the GUI's handler: in the second session keys and taps were ignored at every menu (the first tally waited forever for Enter) and Esc no longer opened the in-game menu. Only the on-screen pad still worked, because jkGuiRend_UpdateController calls the GUI's handler directly. Window_ESP32_ResetMsgHandlers clears the table and the count at session start. - sithConsole_numRegisteredCmds and jkDev_numCheats outlived their tables. Each session registered on top of the last one's count, and from the third session every console command and cheat was refused ("not recognized"). Both are reset when their tables are made. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- src/Devices/sithConsole.c | 4 ++++ src/Main/jkDev.c | 1 + src/Platform/ESP32/Window_ESP32.c | 2 +- src/Win95/Window.c | 19 +++++++++++++++++++ src/Win95/Window.h | 3 +++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Devices/sithConsole.c b/src/Devices/sithConsole.c index 566c73cfd..ccfa20b92 100644 --- a/src/Devices/sithConsole.c +++ b/src/Devices/sithConsole.c @@ -23,6 +23,10 @@ int sithConsole_Startup(int maxCmds) { sithConsole_maxCmds = maxCmds; _memset(sithConsole_aCmds, 0, sizeof(stdDebugConsoleCmd) * maxCmds); + // Added: the count outlived the array; a second engine session in one + // process registered on top of the first one's count, and from the + // third on every command (cheats included) was refused as "full" + sithConsole_numRegisteredCmds = 0; DebugGui_fnPrint = 0; DebugGui_fnPrintUniStr = 0; sithCommand_Startup(); diff --git a/src/Main/jkDev.c b/src/Main/jkDev.c index 644c810b9..a5f056a75 100644 --- a/src/Main/jkDev.c +++ b/src/Main/jkDev.c @@ -51,6 +51,7 @@ void jkDev_Startup() jkDev_cheatHashtable = stdHashtbl_New(JKDEV_NUM_CHEATS*2); _memset(jkDev_aCheatCmds, 0, sizeof(stdDebugConsoleCmd) * JKDEV_NUM_CHEATS); + jkDev_numCheats = 0; // Added: like sithConsole's count, it outlived the table sithConsole_RegisterCommand(jkDev_CmdVersion, "version", 0); sithConsole_RegisterCommand(jkDev_CmdTeam, "team", 0); diff --git a/src/Platform/ESP32/Window_ESP32.c b/src/Platform/ESP32/Window_ESP32.c index 71a3e88fb..d58e5ded5 100644 --- a/src/Platform/ESP32/Window_ESP32.c +++ b/src/Platform/ESP32/Window_ESP32.c @@ -99,7 +99,7 @@ int jk_esp_engine_startup(const char* game_dir) strncpy(cmdLine, jk_esp_engine_args(), sizeof(cmdLine) - 1); cmdLine[sizeof(cmdLine) - 1] = 0; - g_handler_count = 0; + Window_ESP32_ResetMsgHandlers(); // the table too, not only the count (see Window.c) g_thing_two_some_dialog_count = 0; g_should_exit = 0; g_window_not_destroyed = 0; diff --git a/src/Win95/Window.c b/src/Win95/Window.c index ee015955b..41cb86c3f 100644 --- a/src/Win95/Window.c +++ b/src/Win95/Window.c @@ -151,6 +151,25 @@ int Window_RemoveMsgHandler(WindowHandler_t a1) return 1; } +#ifdef TARGET_ESP32 +// Added: tab5-emu runs the engine more than once in a process. The handler +// table outlived a session (stdControl's handler is never removed, and +// jkGuiRend_Shutdown forgets the GUI's), but only the count was reset. The next +// session's adds were refused as duplicates, so dispatch, which visits +// g_handler_count slots, stopped before the GUI's handler. Keys and taps were +// ignored at the menus (only the on-screen pad still worked: jkGuiRend_UpdateController +// calls the GUI's handler directly), and Esc no longer opened the in-game menu. +void Window_ESP32_ResetMsgHandlers(void) +{ + for (int i = 0; i < 16; i++) + { + Window_ext_handlers[i].handler = 0; + Window_ext_handlers[i].exists = 0; + } + g_handler_count = 0; +} +#endif + int Window_AddDialogHwnd(HWND a1) { int v1; // eax diff --git a/src/Win95/Window.h b/src/Win95/Window.h index cd2fec387..ce62bf9c1 100644 --- a/src/Win95/Window.h +++ b/src/Win95/Window.h @@ -23,6 +23,9 @@ extern int Window_ySize; int Window_AddMsgHandler(WindowHandler_t a1); int Window_RemoveMsgHandler(WindowHandler_t a1); +#ifdef TARGET_ESP32 +void Window_ESP32_ResetMsgHandlers(void); +#endif int Window_AddDialogHwnd(HWND a1); int Window_msg_main_handler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); int Window_Main(HINSTANCE hInstance, int a2, char *lpCmdLine, int nShowCmd, LPCSTR lpWindowName);