sdl12-compat: Clamp mouse coordinates to the backbuffer size for non-OpenGL

From cc23a07dc4c7a720d192285cc38d1bd90508f72d Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Sun, 25 Sep 2022 18:05:34 +0800
Subject: [PATCH] Clamp mouse coordinates to the backbuffer size for non-OpenGL

When we're logically scaling, and preserving the aspect ratio (so are
letterboxing or pillarboxing), the scaled mouse coordinates may be
outside the application window (backbuffer). These can even be negative,
which get underflowed to very large positive values, as SDL 1.2 uses
unsigned coordinates here.

We were already clamping these values for OpenGL logical scaling, which
we do ourselves, but the event filter used with SDL_Renderer doesn't, so
we'll manually clamp these all the time.
---
 src/SDL12_compat.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 76b6f987..5ee543bd 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -4512,6 +4512,9 @@ EventFilter20to12(void *data, SDL_Event *event20)
             event12.motion.which = (Uint8) event20->motion.which;
             event12.motion.state = event20->motion.state;
             AdjustOpenGLLogicalScalingPoint(&event20->motion.x, &event20->motion.y);
+            /* Clamp the absolute position to the window dimensions. */
+            event20->motion.x = SDL_max(SDL_min(event20->motion.x, VideoSurface12->w), 0);
+            event20->motion.y = SDL_max(SDL_min(event20->motion.y, VideoSurface12->h), 0);
             event12.motion.x = (Uint16) event20->motion.x;
             event12.motion.y = (Uint16) event20->motion.y;
             if (UseMouseRelativeScaling) {