sdl2-compat: Re-add the event filter in case it was removed by SDL_QuitEvents()

From e7395abb22215c69045cd91e86cc9d24030e62f3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 9 Jan 2024 08:46:11 -0800
Subject: [PATCH] Re-add the event filter in case it was removed by
 SDL_QuitEvents()

Fixes video_getSetWindowGrab in testautomation
---
 src/sdl2_compat.c | 30 ++++++++++++++++++++++++------
 src/sdl3_syms.h   |  1 +
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index b81a7f6..b452728 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -1196,8 +1196,6 @@ static int NumTouchDevices = 0;
 
 /* Functions! */
 
-static int SDLCALL EventFilter3to2(void *userdata, SDL_Event *event3);
-
 /* this stuff _might_ move to SDL_Init later */
 static int
 SDL2Compat_InitOnStartup(void)
@@ -1222,8 +1220,6 @@ SDL2Compat_InitOnStartup(void)
         goto fail;
     }
 
-    SDL3_SetEventFilter(EventFilter3to2, NULL);
-
     SDL3_SetHint("SDL_WINDOWS_DPI_SCALING", 0);
     SDL3_SetHint("SDL_WINDOWS_DPI_AWARENESS", "unaware");
     SDL3_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0");
@@ -1734,10 +1730,20 @@ EventFilter3to2(void *userdata, SDL_Event *event3)
     return 1;
 }
 
+static void CheckEventFilter(void)
+{
+    SDL_EventFilter filter = NULL;
+
+    if (!SDL3_GetEventFilter(&filter, NULL) || filter != EventFilter3to2) {
+        SDL3_SetEventFilter(EventFilter3to2, NULL);
+    }
+}
 
 DECLSPEC void SDLCALL
 SDL_SetEventFilter(SDL2_EventFilter filter2, void *userdata)
 {
+    CheckEventFilter();
+
     EventFilter2 = filter2;
     EventFilterUserData2 = userdata;
 }
@@ -1811,8 +1817,12 @@ SDL_WaitEvent(SDL2_Event *event2)
 DECLSPEC void SDLCALL
 SDL_AddEventWatch(SDL2_EventFilter filter2, void *userdata)
 {
+    EventFilterWrapperData *wrapperdata;
+
+    CheckEventFilter();
+
     /* we set up an SDL3 event filter to manage things already; we will also use it to call all added SDL2 event watchers. Put this new one in that list. */
-    EventFilterWrapperData *wrapperdata = (EventFilterWrapperData *) SDL3_malloc(sizeof (EventFilterWrapperData));
+    wrapperdata = (EventFilterWrapperData *) SDL3_malloc(sizeof (EventFilterWrapperData));
     if (!wrapperdata) {
         return;  /* oh well. */
     }
@@ -3495,6 +3505,8 @@ SDL_CreateShapedWindow(const char *title, unsigned int x, unsigned int y, unsign
     SDL_Window *window;
     int hidden = flags & SDL_WINDOW_HIDDEN;
 
+    CheckEventFilter();
+
     if (g_shaped_window != NULL) {
         SDL3_SetError("only 1 shaped window");
         return NULL;
@@ -5964,6 +5976,8 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
     SDL_Window *window = NULL;
     const Uint32 is_popup = flags & (SDL_WINDOW_POPUP_MENU | SDL_WINDOW_TOOLTIP);
 
+    CheckEventFilter();
+
     if (flags & SDL2_WINDOW_FULLSCREEN_DESKTOP) {
         flags &= ~SDL2_WINDOW_FULLSCREEN_DESKTOP;
         flags |= SDL_WINDOW_FULLSCREEN; /* This is fullscreen desktop for new windows */
@@ -6007,7 +6021,11 @@ SDL_CreateWindowFrom(const void *data)
 {
     SDL_Window *window;
     const char *hint;
-    SDL_PropertiesID props = SDL3_CreateProperties();
+    SDL_PropertiesID props;
+
+    CheckEventFilter();
+
+    props = SDL3_CreateProperties();
     if (!props) {
         return NULL;
     }
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index 0dd4895..6bdbad0 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -156,6 +156,7 @@ SDL3_SYM_PASSTHROUGH(void,FlushEvent,(Uint32 a),(a),)
 SDL3_SYM_PASSTHROUGH(void,FlushEvents,(Uint32 a, Uint32 b),(a,b),)
 SDL3_SYM(SDL_bool,WaitEventTimeout,(SDL_Event *a, Sint32 b),(a,b),return)
 SDL3_SYM(int,PushEvent,(SDL_Event *a),(a),return)
+SDL3_SYM(SDL_bool,GetEventFilter,(SDL_EventFilter *a, void **b),(a,b),)
 SDL3_SYM(void,SetEventFilter,(SDL_EventFilter a, void *b),(a,b),)
 SDL3_SYM(void,FilterEvents,(SDL_EventFilter a, void *b),(a,b),)
 SDL3_SYM(void *,AllocateEventMemory,(size_t a),(a),return)