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
This commit is contained in:
Hunter Kvalevog 2024-05-29 19:18:00 -05:00 committed by Sam Lantinga
parent 4954690828
commit 0136bf2f8f
1 changed files with 11 additions and 1 deletions

View File

@ -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);
}