Browse Source

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
Hunter Kvalevog 10 months ago
parent
commit
0136bf2f8f
1 changed files with 11 additions and 1 deletions
  1. 11 1
      src/events/SDL_events.c

+ 11 - 1
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);
 }