Here’s the plan that I have in mind. Some of those steps are
refactorings of the “remove this function, then fix what broke”. The
API to SDL itself should be backward compatible after this, but the
API of video drivers would change.
The first step is one of the hardest. We’d start by replacing the
PumpEvents method of SDL_VideoDevice with one along the lines of this:
/* A negative timeout means to wait indefinitely, returns 1 if an
event was returned, 0 otherwise (or maybe -1 for an error?). /
int GetEvent(_THIS, SDL_Event event, int timeout);
Yes, yes, a timeout, the crowds will go wild, I’m sure…
An SDL_WaitEventTimeout with a similar signature would be added, which
would check the SDL event queue and return an event from there if
there is one, or otherwise call GetEvent. SDL_PollEvent and
SDL_WaitEvent would be simply wrappers for this single call, with the
appropriate timeout.
At this point, there is no systematic superfluous queueing or
SDL_Delay in SDL_WaitEvent. Unless the video driver is silly, as we’ll
see next…
There is a quick video driver migration path, which would be to
implement their GetEvent in a way similar to how SDL_WaitEvent is
currently implemented, only needing to add the timeout feature. This
could even be provided in a general way by the core SDL, but you
should have the goal of eliminating any need for it, of course,
because it’s silly.
That leaves just my third point from my previous email (not being
possible to wait for events at the same time as something else).
Adding timer events would be trivial, done in SDL_WaitEventTimeout,
checking whether a timer has expired before calling GetEvent, skipping
it if there was, if there wasn’t, tweaking the timeout so that it
returns before the next one, and finally return the next timer event
if GetEvent timed out (or wasn’t called). These timers would not have
the low latency of the existing timers, but would remove the threading
requirement, would never tell you that a platform doesn’t support
multiple timers, and most importantly, you would not have the “you
can’t call any functions except SDL_PushEvent” restriction. For a
caller that just calls SDL_PushEvent, this would probably be more
efficient, actually (no context switching or mutex/queue
manipulation). The existing timer feature can remain as is, anyway.
Support for other, more interesting things, like file descriptors,
though, would be a bit more work, and every type would require
modifying all the video drivers that support it, since they would be
responsible for dealing with it. Realistically, we should probably
pull a fast one and just add support for “sockets”, which would really
support all matters of stuff based on file descriptors on Unix
platforms, but would portably just be sockets (Win32 is funky that
way, for example).
There’s a few questions, still…
Should SDL_PeepEvents still just “check the event queue”, or should it
actually wrap GetEvent as well? If the latter, then peeking be a
little bit icky, if the queue is empty, it would GetEvent, push it on
the queue, then do the peek as it is now (for those who are paying
attention, yes, this is a bit of a race, although it shouldn’t change
outcomes significantly).
SDL_HasEvent could behave a bit strangely. If you checked whether it
has an event and then do SDL_PollEvent right away, you could very well
be told that a certain type of event is not available, and get exactly
that right away. Does anyone really use that? Not very popular, it
seems: http://google.com/codesearch?q=SDL_HasEvent&hl=en&btnG=Search+Code
Oh, it’s new to 1.3 and only used internally… To work around the
event queue.
Is the event thread actually used much? It’s not supported on a major
top-tier platform (Windows), that makes it’s general usefulness rather
questionable, IMHO, and it’s the kind of thing that would tend to muck
up things (I think it would work with my changes, it would just be
rather wacky, and I wouldn’t really like to be you if you hit a bug!).
I’m sure there will be a zillion other niggly details, but that’s
basically the plan I have. What do you think?–
http://pphaneuf.livejournal.com/