Commit cf63af8ae265c99e80ffc93e9930336c939524bc

Ryan C. Gordon 2022-07-31T22:10:45

events: Don't lose window RESIZED events during SIZED_CHANGED processing. Previously, calling SDL_SendWindowEvent for a SIZED_CHANGED event would filter the queue to remove RESIZED and SIZED_CHANGED events, so you don't overflow the queue with obsolete data, but any RESIZED events would be lost in this process. Now we note if there was a RESIZED pending and replace it with a new event using the same dimensions as the new SIZED_CHANGED event. This fixes cases where an app is only listening for RESIZED events and thus might lose important information in some cases. Fixes #5925.

diff --git a/src/events/SDL_windowevents.c b/src/events/SDL_windowevents.c
index cc8b891..209295a 100644
--- a/src/events/SDL_windowevents.c
+++ b/src/events/SDL_windowevents.c
@@ -27,15 +27,27 @@
 #include "SDL_mouse_c.h"
 #include "SDL_hints.h"
 
+typedef struct RemovePendingSizeChangedAndResizedEvents_Data
+{
+    const SDL_Event *new_event;
+    SDL_bool saw_resized;
+} RemovePendingSizeChangedAndResizedEvents_Data;
+
 static int SDLCALL
-RemovePendingSizeChangedAndResizedEvents(void * userdata, SDL_Event *event)
+RemovePendingSizeChangedAndResizedEvents(void *_userdata, SDL_Event *event)
 {
-    SDL_Event *new_event = (SDL_Event *)userdata;
+    RemovePendingSizeChangedAndResizedEvents_Data *userdata = (RemovePendingSizeChangedAndResizedEvents_Data *) _userdata;
+    const SDL_Event *new_event = userdata->new_event;
 
     if (event->type == SDL_WINDOWEVENT &&
         (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
          event->window.event == SDL_WINDOWEVENT_RESIZED) &&
         event->window.windowID == new_event->window.windowID) {
+
+        if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
+            userdata->saw_resized = SDL_TRUE;
+        }
+
         /* We're about to post a new size event, drop the old one */
         return 0;
     }
@@ -188,7 +200,18 @@ SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
 
         /* Fixes queue overflow with resize events that aren't processed */
         if (windowevent == SDL_WINDOWEVENT_SIZE_CHANGED) {
-            SDL_FilterEvents(RemovePendingSizeChangedAndResizedEvents, &event);
+            /* !!! FIXME: in SDL3, let's make RESIZED/SIZE_CHANGED into one event with a flag to distinguish between them, and remove all this tapdancing. */
+            RemovePendingSizeChangedAndResizedEvents_Data userdata;
+            userdata.new_event = &event;
+            userdata.saw_resized = SDL_FALSE;
+            SDL_FilterEvents(RemovePendingSizeChangedAndResizedEvents, &userdata);
+            if (userdata.saw_resized) {  /* if there was a pending resize, make sure one at the new dimensions remains. */
+                event.window.event = SDL_WINDOWEVENT_RESIZED;
+                if (SDL_PushEvent(&event) <= 0) {
+                    return 0;  /* oh well. */
+                }
+                event.window.event = SDL_WINDOWEVENT_SIZE_CHANGED;  /* then push the actual event next. */
+            }
         }
         if (windowevent == SDL_WINDOWEVENT_MOVED) {
             SDL_FilterEvents(RemovePendingMoveEvents, &event);
@@ -207,7 +230,7 @@ SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
         }
     }
 
-    return (posted);
+    return posted;
 }
 
 /* vi: set ts=4 sw=4 expandtab: */