From eb17b48c17a5b39ca39c2ebae9d69eaedfc2b1d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Mon, 23 Sep 2024 01:44:34 -0400
Subject: [PATCH] Use SDL_GetWindowSizeInPixels, and don't resize when
 maximized

Fixes sourceforge issue 126 (finally!)

Apparently, macOS "maximized" is the same as "fullscreen" in SDL.
---
 src/conio/sdl_con.c  | 15 ++++++++++-----
 src/conio/sdlfuncs.c |  4 ++++
 src/conio/sdlfuncs.h |  1 +
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/conio/sdl_con.c b/src/conio/sdl_con.c
index 47602f6006..6af6844c8c 100644
--- a/src/conio/sdl_con.c
+++ b/src/conio/sdl_con.c
@@ -458,7 +458,7 @@ static void internal_setwinsize(struct video_stats *vs, bool force)
 	}
 	else {
 		pthread_mutex_lock(&win_mutex);
-		sdl.GetWindowSize(win, &w, &h);
+		sdl.GetWindowSizeInPixels(win, &w, &h);
 		pthread_mutex_unlock(&win_mutex);
 		if (w != vs->winwidth || h != vs->winheight)
 			changed = true;
@@ -633,7 +633,7 @@ static void setup_surfaces(struct video_stats *vs)
 		// SDL2: This is slow sometimes... not sure why.
 		new_win = true;
 		if (sdl.CreateWindowAndRenderer(vs->winwidth, vs->winheight, flags, &win, &renderer) == 0) {
-			sdl.GetWindowSize(win, &idealw, &idealh);
+			sdl.GetWindowSizeInPixels(win, &idealw, &idealh);
 			vs->winwidth = idealw;
 			vs->winheight = idealh;
 			sdl.RenderClear(renderer);
@@ -655,8 +655,11 @@ static void setup_surfaces(struct video_stats *vs)
 	}
 	else {
 		sdl_bughack_minsize(idealmw, idealmh, false);
-		sdl.SetWindowSize(win, idealw, idealh);
-		sdl.GetWindowSize(win, &idealw, &idealh);
+		// Don't change window size when maximized...
+		if ((sdl.GetWindowFlags(win) & SDL_WINDOW_MAXIMIZED) == 0) {
+			sdl.SetWindowSize(win, idealw, idealh);
+		}
+		sdl.GetWindowSizeInPixels(win, &idealw, &idealh);
 		vs->winwidth = idealw;
 		vs->winheight = idealh;
 		if (internal_scaling) {
@@ -704,7 +707,7 @@ static void sdl_add_key(unsigned int keyval, struct video_stats *vs)
 			int w, h;
 
 			// Get current window size
-			sdl.GetWindowSize(win, &w, &h);
+			sdl.GetWindowSizeInPixels(win, &w, &h);
 			// Limit to max window size if available
 			SDL_Rect r;
 			if (sdl.GetDisplayUsableBounds(0, &r) == 0) {
@@ -1038,6 +1041,8 @@ void sdl_video_event_thread(void *data)
 				switch(ev.window.event) {
 					case SDL_WINDOWEVENT_SIZE_CHANGED:
 						// SDL2: User resized window
+					case SDL_WINDOWEVENT_MAXIMIZED:
+					case SDL_WINDOWEVENT_RESTORED:
 					case SDL_WINDOWEVENT_RESIZED:
 						pthread_mutex_lock(&sdl_mode_mutex);
 						if (sdl_mode) {
diff --git a/src/conio/sdlfuncs.c b/src/conio/sdlfuncs.c
index 889212eef9..eb2fcf61ea 100644
--- a/src/conio/sdlfuncs.c
+++ b/src/conio/sdlfuncs.c
@@ -78,6 +78,10 @@ int load_sdl_funcs(struct sdlfuncs *sdlf)
 		xp_dlclose(sdl_dll);
 		return(-1);
 	}
+	if((sdlf->GetWindowSizeInPixels=xp_dlsym(sdl_dll, SDL_GetWindowSizeInPixels))==NULL) {
+		xp_dlclose(sdl_dll);
+		return(-1);
+	}
 	if((sdlf->SetWindowIcon=xp_dlsym(sdl_dll, SDL_SetWindowIcon))==NULL) {
 		xp_dlclose(sdl_dll);
 		return(-1);
diff --git a/src/conio/sdlfuncs.h b/src/conio/sdlfuncs.h
index bf52c2b7f7..8ad0b3b92a 100644
--- a/src/conio/sdlfuncs.h
+++ b/src/conio/sdlfuncs.h
@@ -25,6 +25,7 @@ struct sdlfuncs {
 	void	(HACK_HACK_HACK *FreeSurface)	(SDL_Surface *surface);
 	void	(HACK_HACK_HACK *SetWindowTitle)	(SDL_Window *window, const char *title);
 	void	(HACK_HACK_HACK *GetWindowSize)	(SDL_Window *window, int *w, int *h);
+	void	(HACK_HACK_HACK *GetWindowSizeInPixels)	(SDL_Window *window, int *w, int *h);
 	void	(HACK_HACK_HACK *SetWindowIcon)	(SDL_Window *win, SDL_Surface *icon);
 	int	(HACK_HACK_HACK *ShowCursor)	(int toggle);
 	Uint32	(HACK_HACK_HACK *WasInit)	(Uint32 flags);
-- 
GitLab