From c99e29b74fab56c8a921c6447587b4982ca27d31 Mon Sep 17 00:00:00 2001
From: Alam Ed Arias <alam@srb2.org>
Date: Sat, 29 Mar 2025 14:00:20 -0400
Subject: [PATCH] Emscripten: use EMSCRIPTEN_KEEALIVE

---
 src/command.c     | 10 +++++++++-
 src/d_main.c      |  6 ++----
 src/sdl/i_video.c | 42 ++++++++++++++++++++++++++++++++++--------
 3 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/src/command.c b/src/command.c
index ab6cfc08af..be892e57ae 100644
--- a/src/command.c
+++ b/src/command.c
@@ -16,6 +16,10 @@
 ///
 ///        code shamelessly inspired by the QuakeC sources, thanks Id :)
 
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#endif
+
 #include "doomdef.h"
 #include "doomstat.h"
 #include "command.h"
@@ -277,7 +281,11 @@ void COM_BufExecute(void)
 
 /** Executes a string immediately.  Used for skirting around WAIT commands.
   */
-void COM_ImmedExecute(const char *ptext)
+void
+#ifdef __EMSCRIPTEN__
+EMSCRIPTEN_KEEPALIVE
+#endif
+COM_ImmedExecute(const char *ptext)
 {
 	size_t i = 0, j = 0;
 	char line[1024] = "";
diff --git a/src/d_main.c b/src/d_main.c
index d5787fc652..5d8211393e 100644
--- a/src/d_main.c
+++ b/src/d_main.c
@@ -774,18 +774,16 @@ void D_SRB2Loop(void)
 static boolean D_LockFrame = false;
 
 #ifdef __EMSCRIPTEN__
-int pause_loop(void)
+int EMSCRIPTEN_KEEPALIVE pause_loop(void)
 {
 	D_LockFrame = true;
-	//emscripten_sleep(1000);
 	emscripten_pause_main_loop();
 	return 0;
 }
 
-int resume_loop(void)
+int EMSCRIPTEN_KEEPALIVE resume_loop(void)
 {
 	D_LockFrame = false;
-	//emscripten_sleep(1000);
 	emscripten_resume_main_loop();
 	return 0;
 }
diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c
index 28a628176f..7a5ddaa74a 100644
--- a/src/sdl/i_video.c
+++ b/src/sdl/i_video.c
@@ -1979,14 +1979,40 @@ UINT32 I_GetRefreshRate(void)
 }
 
 #ifdef __EMSCRIPTEN__
-int change_resolution(int x, int y)
+int EMSCRIPTEN_KEEPALIVE change_resolution(int x, int y)
 {
-	vid.width = x;
-	vid.height = y;
-	return VID_CheckRenderer();
+	int newmode = -1;
+
+	if ( x < BASEVIDWIDTH*1 && y < BASEVIDHEIGHT*1)
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*1, BASEVIDHEIGHT*1);
+	else if (x < BASEVIDWIDTH*2 && y < BASEVIDHEIGHT*2)
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*1, BASEVIDHEIGHT*1);
+	else if (x < BASEVIDWIDTH*3 && y < BASEVIDHEIGHT*3)
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*2, BASEVIDHEIGHT*2);
+#if 0
+	else if (x < BASEVIDWIDTH*4 && y < BASEVIDHEIGHT*4)
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*3, BASEVIDHEIGHT*3);
+	else if (x < BASEVIDWIDTH*5 && y < BASEVIDHEIGHT*5)
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*4, BASEVIDHEIGHT*4);
+	else if (x < BASEVIDWIDTH*6 && y < BASEVIDHEIGHT*6)
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*5, BASEVIDHEIGHT*5);
+	else
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*6, BASEVIDHEIGHT*6);
+#else
+	else
+		newmode = VID_GetModeForSize(BASEVIDWIDTH*2, BASEVIDHEIGHT*2);
+#endif
+
+	if (newmode != -1)
+		setmodeneeded = newmode;
+
+	if (setmodeneeded)
+		return 1;
+
+	return 0;
 }
 
-void inject_text(const char *text)
+void EMSCRIPTEN_KEEPALIVE inject_text(const char *text)
 {
 	event_t event;
 	size_t len = 0;
@@ -1998,7 +2024,7 @@ void inject_text(const char *text)
 	} while (text[len] != 0x00);
 }
 
-void inject_keycode(int key, int type)
+void EMSCRIPTEN_KEEPALIVE inject_keycode(int key, int type)
 {
 	event_t event;
 	if (type == true)
@@ -2017,12 +2043,12 @@ void inject_keycode(int key, int type)
 	if (event.key) D_PostEvent(&event);
 }
 
-void unlock_mouse(void)
+void EMSCRIPTEN_KEEPALIVE unlock_mouse(void)
 {
 	SDLforceUngrabMouse();
 }
 
-void lock_mouse(void)
+void EMSCRIPTEN_KEEPALIVE lock_mouse(void)
 {
 	SDLdoGrabMouse();
 }
-- 
GitLab