sdl12-compat: Use scaled mouse coordinates from events in SDL_GetMouseState()

From a915ff116e094ce4bf5143b863a79a47ec89217a Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Tue, 25 May 2021 19:59:56 +0800
Subject: [PATCH] Use scaled mouse coordinates from events in
 SDL_GetMouseState()

Currently, we pass through SDL_GetMouseState() calls to SDL2 (when not
in relative mouse mode), but SDL20_GetMouseState() returns the actual
coordinates, not the scaled coordinates after
SDL_RenderSetLogicalSize()'s mouse scaling event watch callback is
called.

Keep a track of the mouse coordinates from the events we get (like we do
with relative mouse mode), and return that in SDL_GetMouseState().

(At the moment, we still get the button state from
SDL20_GetMouseState(), which is inconsistent, but not actively a
problem.)
---
 src/SDL12_compat.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 0c0a5cd..652736e 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -813,7 +813,7 @@ static int SwapInterval = 0;
 static JoystickOpenedItem JoystickOpenList[16];
 static Uint8 KeyState[SDLK12_LAST];
 static SDL_bool MouseInputIsRelative = SDL_FALSE;
-static SDL_Point MousePositionWhenRelative = { 0, 0 };
+static SDL_Point MousePosition = { 0, 0 };
 static OpenGLEntryPoints OpenGLFuncs;
 static int OpenGLLogicalScalingWidth = 0;
 static int OpenGLLogicalScalingHeight = 0;
@@ -1882,10 +1882,8 @@ DECLSPEC Uint8 SDLCALL
 SDL_GetMouseState(int *x, int *y)
 {
     const Uint8 buttons = MouseButtonState20to12(SDL20_GetMouseState(x, y));
-    if (MouseInputIsRelative) {
-        if (x) { *x = MousePositionWhenRelative.x; }
-        if (y) { *y = MousePositionWhenRelative.y; }
-    }
+    if (x) { *x = MousePosition.x; }
+    if (y) { *y = MousePosition.y; }
     return buttons;
 }
 
@@ -2481,16 +2479,19 @@ EventFilter20to12(void *data, SDL_Event *event20)
             if (MouseInputIsRelative) {
                 /* in relative mode, clamp fake absolute position to the window dimensions. */
                 #define ADJUST_RELATIVE(axis, rel, dim) { \
-                    MousePositionWhenRelative.axis += event20->motion.rel; \
-                    if (MousePositionWhenRelative.axis <= 0) { \
-                        MousePositionWhenRelative.axis = 0; \
-                    } else if (MousePositionWhenRelative.axis >= VideoSurface12->dim) { \
-                        MousePositionWhenRelative.axis = (VideoSurface12->dim - 1); \
+                    MousePosition.axis += event20->motion.rel; \
+                    if (MousePosition.axis <= 0) { \
+                        MousePosition.axis = 0; \
+                    } else if (MousePosition.axis >= VideoSurface12->dim) { \
+                        MousePosition.axis = (VideoSurface12->dim - 1); \
                     } \
                 }
                 ADJUST_RELATIVE(x, xrel, w);
                 ADJUST_RELATIVE(y, yrel, h);
                 #undef ADJUST_RELATIVE
+            } else {
+                MousePosition.x = event12.motion.x;
+                MousePosition.y = event12.motion.y;
             }
             break;
 
@@ -3195,8 +3196,8 @@ EndVidModeCreate(void)
     OpenGLLogicalScalingDepth = 0;
 
     MouseInputIsRelative = SDL_FALSE;
-    MousePositionWhenRelative.x = 0;
-    MousePositionWhenRelative.y = 0;
+    MousePosition.x = 0;
+    MousePosition.y = 0;
 
     return NULL;
 }
@@ -4160,7 +4161,7 @@ UpdateRelativeMouseMode(void)
             if (MouseInputIsRelative) {
                 /* reset position, we'll have to track it ourselves in SDL_MOUSEMOTION events, since 1.2
                  *  would give you window coordinates, even in relative mode. */
-                SDL_GetMouseState(&MousePositionWhenRelative.x, &MousePositionWhenRelative.y);
+                SDL_GetMouseState(&MousePosition.x, &MousePosition.y);
             }
             SDL20_SetRelativeMouseMode(MouseInputIsRelative);
         }
@@ -4209,8 +4210,8 @@ DECLSPEC void SDLCALL
 SDL_WarpMouse(Uint16 x, Uint16 y)
 {
     if (MouseInputIsRelative) {  /* we have to track this ourselves, in case app calls SDL_GetMouseState(). */
-        MousePositionWhenRelative.x = (int) x;
-        MousePositionWhenRelative.y = (int) y;
+        MousePosition.x = (int) x;
+        MousePosition.y = (int) y;
     } else {
         SDL20_WarpMouseInWindow(VideoWindow20, x, y);
     }