Improved SDL_PollEvent usage (#4794)

* Avoid unnecessary SDL_PumpEvents calls in SDL_WaitEventTimeout

* Add a sentinel event to avoid infinite poll loops

* Move SDL_POLLSENTINEL to new internal event category

* Tweak documentation to indicate SDL_PumpEvents isn't always called

* Avoid shadowing event variable

* Ignore poll sentinel if more (user) events have been added after

Co-authored-by: Sam Lantinga <slouken@libsdl.org>
This commit is contained in:
Brick
2021-10-15 06:26:10 +01:00
committed by GitHub
parent 43da35b567
commit 8bf32e12d8
3 changed files with 52 additions and 3 deletions

View File

@@ -26,6 +26,7 @@
#include "SDL_events.h"
#include "SDL_thread.h"
#include "SDL_events_c.h"
#include "../SDL_hints_c.h"
#include "../timer/SDL_timer_c.h"
#if !SDL_JOYSTICK_DISABLED
#include "../joystick/SDL_joystick_c.h"
@@ -139,6 +140,11 @@ SDL_AutoUpdateSensorsChanged(void *userdata, const char *name, const char *oldVa
#endif /* !SDL_SENSOR_DISABLED */
static void SDLCALL
SDL_PollSentinelChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_EventState(SDL_POLLSENTINEL, SDL_GetStringBoolean(hint, SDL_TRUE) ? SDL_ENABLE : SDL_DISABLE);
}
/* 0 (default) means no logging, 1 means logging, 2 means logging with mouse and finger motion */
static int SDL_DoEventLogging = 0;
@@ -886,6 +892,21 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
Uint32 start = 0;
Uint32 expiration = 0;
/* First check for existing events */
switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT)) {
case -1:
return 0;
case 0:
break;
default:
/* Check whether we have reached the end of the poll cycle, and no more events are left */
if (timeout == 0 && event && event->type == SDL_POLLSENTINEL) {
return SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1;
}
/* Has existing events */
return 1;
}
if (timeout > 0) {
start = SDL_GetTicks();
expiration = start + timeout;
@@ -922,6 +943,13 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
SDL_Delay(1);
break;
default:
if (timeout == 0 && SDL_GetEventState(SDL_POLLSENTINEL) == SDL_ENABLE) {
/* We are at the start of a poll cycle with at least one new event.
Add a sentinel event to mark the end of the cycle. */
SDL_Event sentinel;
sentinel.type = SDL_POLLSENTINEL;
SDL_PushEvent(&sentinel);
}
/* Has events */
return 1;
}
@@ -1216,6 +1244,7 @@ SDL_EventsInit(void)
SDL_AddHintCallback(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_AutoUpdateSensorsChanged, NULL);
#endif
SDL_AddHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
SDL_AddHintCallback(SDL_HINT_POLL_SENTINEL, SDL_PollSentinelChanged, NULL);
if (SDL_StartEventLoop() < 0) {
SDL_DelHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
return -1;
@@ -1231,6 +1260,7 @@ SDL_EventsQuit(void)
{
SDL_QuitQuit();
SDL_StopEventLoop();
SDL_DelHintCallback(SDL_HINT_POLL_SENTINEL, SDL_PollSentinelChanged, NULL);
SDL_DelHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
#if !SDL_JOYSTICK_DISABLED
SDL_DelHintCallback(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_AutoUpdateJoysticksChanged, NULL);