SDL: SDL_SetEventFilter: Don't flush good events

From 0136bf2f8f61aa2dbe554a3fda1c2c7a7009c60f Mon Sep 17 00:00:00 2001
From: Hunter Kvalevog <[EMAIL REDACTED]>
Date: Wed, 29 May 2024 19:18:00 -0500
Subject: [PATCH] SDL_SetEventFilter: Don't flush good events

Only cut events that don't pass the user filter instead of flushing the
entire list.

Fixes #9592
---
 src/events/SDL_events.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index b17487a5b2d6d..f033abf98e81a 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -1310,12 +1310,22 @@ int SDL_PushEvent(SDL_Event *event)
 
 void SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
 {
+    SDL_EventEntry *event;
     SDL_LockMutex(SDL_event_watchers_lock);
     {
         /* Set filter and discard pending events */
         SDL_EventOK.callback = filter;
         SDL_EventOK.userdata = userdata;
-        SDL_FlushEvents(SDL_EVENT_FIRST, SDL_EVENT_LAST);
+        /* Flush all events not accpeted by the filter */
+        SDL_LockMutex(SDL_EventQ.lock);
+        {
+            for (event = SDL_EventQ.head; event; event = event->next) {
+                if (!filter(userdata, &event->event)) {
+                    SDL_CutEvent(event);
+                }
+            }
+        }
+        SDL_UnlockMutex(SDL_EventQ.lock);
     }
     SDL_UnlockMutex(SDL_event_watchers_lock);
 }