sdl12-compat: Improve support for SDL_VIDEO_FULLSCREEN_DISPLAY, remove FIXME

From d41f3cc6fe6b7e605316ffe7afb6a3abe71f5ec5 Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Mon, 22 Nov 2021 11:42:33 +0800
Subject: [PATCH] Improve support for SDL_VIDEO_FULLSCREEN_DISPLAY, remove
 FIXME

SDL_VIDEO_FULLSCREEN_DISPLAY worked in some cases (where the window was
being centered, and for some -- but not all -- lookups of available
video modes), but had several flaws.

Fix it by:
- Using VideoDisplayIndex in all places where the current display is
  read.
- Setting x and y to SDL_WINDOWPOS_UNDEFINED_DISPLAY(display) as a
  fallback in GetEnvironmentWindowPosition()
- Handling the case where SDL_VIDEO_FULLSCREEN_DISPLAY is out-of-bounds
  by falling back to zero.
---
 src/SDL12_compat.c | 12 ++++++++++--
 src/SDL20_syms.h   |  1 +
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 7e622ff..06032f5 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -1569,6 +1569,12 @@ GetVideoDisplay(void)
         variable = SDL20_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
     }
     if (variable) {
+        int preferred_display = SDL20_atoi(variable);
+        
+        if (preferred_display < 0 || preferred_display >= SDL20_GetNumVideoDisplays()) {
+            return 0;
+        }
+
         return SDL20_atoi(variable);
     } else {
         return 0;
@@ -4579,6 +4585,9 @@ GetEnvironmentWindowPosition(int *x, int *y)
     if (center) {
         *x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
         *y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
+    } else {
+        *x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
+        *y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
     }
 }
 
@@ -4969,8 +4978,7 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
         return NULL;
     }
 
-    FIXME("There's an environment variable to choose a display");
-    if (SDL20_GetCurrentDisplayMode(0, &dmode) < 0) {
+    if (SDL20_GetCurrentDisplayMode(VideoDisplayIndex, &dmode) < 0) {
         return NULL;
     }
 
diff --git a/src/SDL20_syms.h b/src/SDL20_syms.h
index 417e325..c30114f 100644
--- a/src/SDL20_syms.h
+++ b/src/SDL20_syms.h
@@ -74,6 +74,7 @@ SDL20_SYM(Uint8,EventState,(Uint32 a, int b),(a,b),return)
 
 SDL20_SYM(SDL_bool,GetWindowWMInfo,(SDL_Window *a, SDL_SysWMinfo *b),(a,b),)
 
+SDL20_SYM(int,GetNumVideoDisplays,(void),(),return)
 SDL20_SYM(int,GetNumDisplayModes,(int a),(a),return)
 SDL20_SYM(int,GetDisplayMode,(int a, int b, SDL_DisplayMode *c),(a,b,c),return)
 SDL20_SYM(int,GetDesktopDisplayMode,(int a, SDL_DisplayMode *b),(a,b),return)