SDL: SDL_SetEventFilter: Don't flush when removing filter

From 8604847214bce82512732b0cbadf9509649a4432 Mon Sep 17 00:00:00 2001
From: Hunter Kvalevog <[EMAIL REDACTED]>
Date: Wed, 29 May 2024 20:49:00 -0500
Subject: [PATCH] SDL_SetEventFilter: Don't flush when removing filter

Don't flush the event queue if the filter paramter is NULL.
---
 src/events/SDL_events.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index f033abf98e81a..61a4da61dbe33 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -1310,22 +1310,25 @@ int SDL_PushEvent(SDL_Event *event)
 
 void SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
 {
-    SDL_EventEntry *event;
+    SDL_EventEntry *event, *next;
     SDL_LockMutex(SDL_event_watchers_lock);
     {
         /* Set filter and discard pending events */
         SDL_EventOK.callback = filter;
         SDL_EventOK.userdata = userdata;
-        /* 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);
+        if (filter) {
+            /* Cut all events not accepted by the filter */
+            SDL_LockMutex(SDL_EventQ.lock);
+            {
+                for (event = SDL_EventQ.head; event; event = next) {
+                    next = event->next;
+                    if (!filter(userdata, &event->event)) {
+                        SDL_CutEvent(event);
+                    }
                 }
             }
+            SDL_UnlockMutex(SDL_EventQ.lock);
         }
-        SDL_UnlockMutex(SDL_EventQ.lock);
     }
     SDL_UnlockMutex(SDL_event_watchers_lock);
 }