From 58079666cef542c16f606937673a14da2f579712 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Wed, 23 Apr 2025 19:13:22 -0400
Subject: [PATCH] Fix "ghost" instance of SyncTERM on exit in SDL mode on Win32

Since nothing will default to this mode anymore, we can just hack
this fix into SyncTERM. This ensures that if SyncTERM is terminated
by calling return from main() on Windows that the quit wrapper does
nothing.

This is a fairly ugly hack, but it works for SyncTERM.

Fixes ticket 203
---
 src/conio/sdl_con.c     | 3 +++
 src/syncterm/syncterm.c | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/src/conio/sdl_con.c b/src/conio/sdl_con.c
index 0690c917e2..06b18d17b4 100644
--- a/src/conio/sdl_con.c
+++ b/src/conio/sdl_con.c
@@ -82,6 +82,7 @@ struct sdl_keyvals {
 static pthread_mutex_t sdl_headlock;
 static struct rectlist *update_list = NULL;
 static struct rectlist *update_list_tail = NULL;
+extern int sdl_video_initialized;
 
 #ifdef __DARWIN__
 static int mac_width;
@@ -1150,6 +1151,7 @@ void sdl_video_event_thread(void *data)
 					sdl_add_key(CIO_KEY_QUIT, &cvstat);
 				else {
 					sdl.QuitSubSystem(SDL_INIT_VIDEO);
+					sdl_video_initialized = FALSE;
 					return;
 				}
 				break;
@@ -1188,6 +1190,7 @@ void sdl_video_event_thread(void *data)
 					case SDL_USEREVENT_QUIT:
 						sdl_ufunc_retval=0;
 						sdl.QuitSubSystem(SDL_INIT_VIDEO);
+						sdl_video_initialized = FALSE;
 						sem_post(&sdl_ufunc_ret);
 						return;
 					case SDL_USEREVENT_FLUSH:
diff --git a/src/syncterm/syncterm.c b/src/syncterm/syncterm.c
index 6e535dccb9..160ee148ad 100644
--- a/src/syncterm/syncterm.c
+++ b/src/syncterm/syncterm.c
@@ -2581,6 +2581,9 @@ ciolib_to_screen(int ciolib)
 }
 
 #if defined(_WIN32) && defined(DLLIFY)
+#ifdef WITH_SDL
+extern int sdl_video_initialized;
+#endif
 __declspec(dllexport) int __cdecl stub_main(int argc, char *argv[], char **env)
 {
 	int n;
@@ -2611,6 +2614,10 @@ __declspec(dllexport) int __cdecl stub_main(int argc, char *argv[], char **env)
 	/* Run the application main() code */
 	n=CIOLIB_main(argc, argv);
 
+#ifdef WITH_SDL
+	// Defeat the QuitWrap call to uninitialize SDL.
+	sdl_video_initialized = FALSE;
+#endif
 	/* Exit cleanly, calling atexit() functions */
 	exit(n);
 
-- 
GitLab