sdl12-compat: Make fullscreen vidmodes always grab the mouse.

From 702b730de95e320843b6c683d147c9d27633f6d0 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sat, 15 May 2021 00:28:03 -0400
Subject: [PATCH] Make fullscreen vidmodes always grab the mouse.

This solves the problem in UT2004 where mouse input was not able to spin all
the way around in fullscreen mode (even though it worked in windowed mode!).
UT2004 explicitly grabbed the mouse for windowed mode, but didn't touch it
for fullscreen, so now we grab by default in fullscreen modes, and don't let
the grab turn off during fullscreen modes in SDL_WM_GrabInput().

Fixes #50.
---
 src/SDL12_compat.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index c9b64b8..e97ada5 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -717,6 +717,13 @@ typedef struct
     SDL_Cursor *wm_cursor;  /* the real SDL 1.2 has an opaque pointer to a platform-specific cursor here. */
 } SDL12_Cursor;
 
+typedef enum
+{
+    SDL12_GRAB_QUERY = -1,
+    SDL12_GRAB_OFF = 0,
+    SDL12_GRAB_ON = 1
+} SDL12_GrabMode;
+
 typedef enum
 {
     SDL12_GL_RED_SIZE,
@@ -3272,6 +3279,7 @@ InitializeOpenGLScaling(const int w, const int h)
 }
 
 
+static void HandleInputGrab(SDL12_GrabMode mode);
 
 DECLSPEC SDL12_Surface * SDLCALL
 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
@@ -3536,6 +3544,11 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
 
     SDL20_RaiseWindow(VideoWindow20);
 
+    /* SDL 1.2 always grabbed input if the video mode was fullscreen. */
+    if (VideoSurface12->flags & SDL12_FULLSCREEN) {
+        HandleInputGrab(SDL12_GRAB_ON);
+    }
+
     FIXME("setup screen saver");
 
     VideoSurfacePresentTicks = 0;
@@ -4042,13 +4055,6 @@ SDL_WM_ToggleFullScreen(SDL12_Surface *surface)
     return retval;
 }
 
-typedef enum
-{
-    SDL12_GRAB_QUERY = -1,
-    SDL12_GRAB_OFF = 0,
-    SDL12_GRAB_ON = 1
-} SDL12_GrabMode;
-
 static void
 UpdateRelativeMouseMode(void)
 {
@@ -4083,17 +4089,23 @@ SDL_ShowCursor(int toggle)
     return retval;
 }
 
+static void
+HandleInputGrab(SDL12_GrabMode mode)
+{
+    /* SDL 1.2 always grabbed input if the video mode was fullscreen. */
+    const SDL_bool wantgrab = ((VideoSurface12->flags & SDL12_FULLSCREEN) || (mode == SDL12_GRAB_ON)) ? SDL_TRUE : SDL_FALSE;
+    if (VideoWindowGrabbed != wantgrab) {
+        SDL20_SetWindowGrab(VideoWindow20, wantgrab);
+        VideoWindowGrabbed = wantgrab;
+        UpdateRelativeMouseMode();
+    }
+}
 
 DECLSPEC SDL12_GrabMode SDLCALL
 SDL_WM_GrabInput(SDL12_GrabMode mode)
 {
     if (mode != SDL12_GRAB_QUERY) {
-        const SDL_bool wantgrab = (mode == SDL12_GRAB_ON) ? SDL_TRUE : SDL_FALSE;
-        if (VideoWindowGrabbed != wantgrab) {
-            SDL20_SetWindowGrab(VideoWindow20, wantgrab);
-            VideoWindowGrabbed = wantgrab;
-            UpdateRelativeMouseMode();
-        }
+        HandleInputGrab(mode);
     }
     return VideoWindowGrabbed ? SDL12_GRAB_ON : SDL12_GRAB_OFF;
 }