sdl12-compat: Always let SDL2 events propagate through the SDL2 event queue.

From 28d4b38488dd1626252a2e253a89ae900aefcf77 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 26 Feb 2021 15:44:38 -0500
Subject: [PATCH] Always let SDL2 events propagate through the SDL2 event
 queue.

Previously, our event filter would take what it wanted for the 1.2 app,
and throw _everything_ away. But there are internal event watchers in SDL2
that need to be able to see some events, and the filter prevents that.

In this case: the render API needed to see window resize events to know to
adjust logical scaling, or SDL_WM_ToggleFullscreen() would seriously fail.

Now the events go all the way through the SDL2 queue--never filtered--even
though we still build the 1.2 queue in an event filter. When the user calls
SDL_PumpEvents or SDL_PollEvents, we drain the now-seen-by-everyone-that-cares
events from the SDL2 queue without further processing.

Fixes #25.
---
 src/SDL12_compat.c | 24 ++++++++++++++++--------
 src/SDL20_syms.h   |  1 +
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index c1addba..7605f76 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -2181,16 +2181,16 @@ EventFilter20to12(void *data, SDL_Event *event20)
             break;
 
         // !!! FIXME: this is sort of a mess to convert.
-        case SDL_SYSWMEVENT: FIXME("write me"); return 0;
+        case SDL_SYSWMEVENT: FIXME("write me"); return 1;
 
         case SDL_KEYUP:
         case SDL_KEYDOWN:
             if (event20->key.repeat) {
-                return 0;  /* ignore 2.0-style key repeat events */
+                return 1;  /* ignore 2.0-style key repeat events */
             }
             event12.key.keysym.sym = Keysym20to12(event20->key.keysym.sym);
             if (event12.key.keysym.sym == SDLK12_UNKNOWN) {
-                return 0;  /* drop it if we can't map it */
+                return 1;  /* drop it if we can't map it */
             }
 
             KeyState[event12.key.keysym.sym] = event20->key.state;
@@ -2207,8 +2207,8 @@ EventFilter20to12(void *data, SDL_Event *event20)
             event12.key.keysym.unicode = (EnabledUnicode)? event12.key.keysym.sym : 0;
             break;
 
-        case SDL_TEXTEDITING: FIXME("write me"); return 0;
-        case SDL_TEXTINPUT: FIXME("write me"); return 0;
+        case SDL_TEXTEDITING: FIXME("write me"); return 1;
+        case SDL_TEXTINPUT: FIXME("write me"); return 1;
 
         case SDL_MOUSEMOTION:
             event12.type = SDL12_MOUSEMOTION;
@@ -2328,12 +2328,18 @@ EventFilter20to12(void *data, SDL_Event *event20)
         //case SDL_DROPFILE:
 
         default:
-            return 0;  /* drop everything else. */
+            return 1;  /* drop everything else. */
     }
 
     PushEventIfNotFiltered(&event12);
 
-    return 0;  /* always drop it from the 2.0 event queue. */
+    /* always pass it to the 2.0 event queue, as internal watchers (like the render API)
+       might need to see these events to deal with logical scaling, etc. We've already
+       copied it to the separate 1.2 event queue, and run the app's 1.2 event filter.
+       Next time we call 1.2's SDL_PollEvent or SDL_PumpEvents(), we'll throw away the
+       entire SDL2 event queue, as everything that cares about those will have then
+       had a chance to examine it. */
+    return 1;
 }
 
 DECLSPEC void SDLCALL
@@ -3516,6 +3522,8 @@ SDL_Flip(SDL12_Surface *surface12)
 DECLSPEC void SDLCALL
 SDL_PumpEvents(void)
 {
+    SDL_Event e;
+
     // If the app is doing dirty rectangles, we set a flag and present the
     //  screen surface when they pump for new events if we're close to 60Hz,
     //  which we consider a sign that they are done rendering for the current
@@ -3523,7 +3531,7 @@ SDL_PumpEvents(void)
     if (VideoSurfacePresentTicks && SDL_TICKS_PASSED(SDL20_GetTicks(), VideoSurfacePresentTicks)) {
         PresentScreen();
     }
-    SDL20_PumpEvents();
+    while (SDL20_PollEvent(&e)) {  /* spin to drain the SDL2 event queue. */ }
 }
 
 DECLSPEC void SDLCALL
diff --git a/src/SDL20_syms.h b/src/SDL20_syms.h
index 2b4382f..92aec50 100644
--- a/src/SDL20_syms.h
+++ b/src/SDL20_syms.h
@@ -54,6 +54,7 @@ SDL20_SYM(const char *,GetCurrentVideoDriver,(void),(),return)
 
 SDL20_SYM(SDL_assert_state,ReportAssertion,(SDL_assert_data *a,const char *b,const char *c, int d),(a,b,c,d),return)
 
+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),)