sdl12-compat: Use event watch callbacks instead of event filters to scale mouse input

From 52375c0c02b8d97f2833d440a291bc4397a8f984 Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Tue, 25 May 2021 20:22:32 +0800
Subject: [PATCH] Use event watch callbacks instead of event filters to scale
 mouse input

By using SDL20_AddEventWatch() instead of SDL20_SetEventFilter() to process
SDL2 events, we get the versions of the events after processing by other
event watchers, such as the SDL Render system. This means that
SDL_RenderSetLogicalSize()'s scaled mouse coordinates are used.

This fixes the hardware-cursor based mouse in SDL 1.2-based RenPy games.
Note, though, that it won't work with OpenGL-based games, as these don't
use SDL_RenderSetLogicalSize(), so won't get scaled events from SDL2.
---
 src/SDL12_compat.c | 7 ++++++-
 src/SDL20_syms.h   | 2 ++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index dba6c71..0c0a5cd 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -1478,7 +1478,7 @@ Init12Video(void)
     SDL20_memset(EventStates, SDL_ENABLE, sizeof (EventStates)); /* on by default */
     EventStates[SDL12_SYSWMEVENT] = SDL_IGNORE;  /* off by default. */
 
-    SDL20_SetEventFilter(EventFilter20to12, NULL);
+    SDL20_AddEventWatch(EventFilter20to12, NULL);
 
     VideoDisplayIndex = GetVideoDisplay();
     SwapInterval = 0;
@@ -3523,6 +3523,11 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
         }
 
         SDL20_RenderSetLogicalSize(VideoRenderer20, width, height);
+
+        /* we need to make sure we're at the back of the Event Watch queue */	
+        SDL20_DelEventWatch(EventFilter20to12, NULL);
+        SDL20_AddEventWatch(EventFilter20to12, NULL);
+
         SDL20_SetRenderDrawColor(VideoRenderer20, 0, 0, 0, 255);  /* leave this black always, we only use it to clear the framebuffer. */
         SDL20_RenderClear(VideoRenderer20);
         SDL20_RenderPresent(VideoRenderer20);
diff --git a/src/SDL20_syms.h b/src/SDL20_syms.h
index dcddbe2..b3665c8 100644
--- a/src/SDL20_syms.h
+++ b/src/SDL20_syms.h
@@ -65,6 +65,8 @@ SDL20_SYM(SDL_assert_state,ReportAssertion,(SDL_assert_data *a,const char *b,con
 SDL20_SYM(int,PollEvent,(SDL_Event *a),(a),return)
 SDL20_SYM(void,PumpEvents,(void),(),)
 SDL20_SYM(void,SetEventFilter,(SDL_EventFilter a, void *b),(a,b),)
+SDL20_SYM(void,AddEventWatch,(SDL_EventFilter a, void *b),(a,b),)
+SDL20_SYM(void,DelEventWatch,(SDL_EventFilter a, void *b),(a,b),)
 
 SDL20_SYM(int,GetNumDisplayModes,(int a),(a),return)
 SDL20_SYM(int,GetDisplayMode,(int a, int b, SDL_DisplayMode *c),(a,b,c),return)