Fixed bug #684
authorSam Lantinga <slouken@libsdl.org>
Tue, 17 Feb 2009 05:59:40 +0000
changeset 30729da8f57ab92c
parent 3071 6f3308d4b6cd
child 3073 c5280d33c32a
Fixed bug #684

Reworked Pierre's patch a little bit, which added SDL_WaitEventTimeout()
include/SDL_events.h
src/events/SDL_events.c
     1.1 --- a/include/SDL_events.h	Tue Feb 17 05:57:54 2009 +0000
     1.2 +++ b/include/SDL_events.h	Tue Feb 17 05:59:40 2009 +0000
     1.3 @@ -412,6 +412,14 @@
     1.4   */
     1.5  extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event * event);
     1.6  
     1.7 +/* Waits until the specified timeout (in milliseconds) for the next available
     1.8 +   event, returning 1, or 0 if there was an error while waiting for events. If
     1.9 +   'event' is not NULL, the next event is removed from the queue and stored in
    1.10 +   that area.
    1.11 + */
    1.12 +extern DECLSPEC int SDLCALL SDL_WaitEventTimeout(SDL_Event * event,
    1.13 +                                                 int timeout);
    1.14 +
    1.15  /* Add an event to the event queue.
    1.16     This function returns 1 on success, 0 if the event was filtered,
    1.17     or -1 if the event queue was full or there was some other error.
     2.1 --- a/src/events/SDL_events.c	Tue Feb 17 05:57:54 2009 +0000
     2.2 +++ b/src/events/SDL_events.c	Tue Feb 17 05:59:40 2009 +0000
     2.3 @@ -399,18 +399,24 @@
     2.4  int
     2.5  SDL_PollEvent(SDL_Event * event)
     2.6  {
     2.7 -    SDL_PumpEvents();
     2.8 -
     2.9 -    /* We can't return -1, just return 0 (no event) on error */
    2.10 -    if (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0)
    2.11 -        return 0;
    2.12 -    return 1;
    2.13 +    return SDL_WaitEventTimeout(event, 0);
    2.14  }
    2.15  
    2.16  int
    2.17  SDL_WaitEvent(SDL_Event * event)
    2.18  {
    2.19 -    while (1) {
    2.20 +    return SDL_WaitEventTimeout(event, -1);
    2.21 +}
    2.22 +
    2.23 +int
    2.24 +SDL_WaitEventTimeout(SDL_Event * event, int timeout)
    2.25 +{
    2.26 +    Uint32 expiration = 0;
    2.27 +
    2.28 +    if (timeout > 0)
    2.29 +        expiration = SDL_GetTicks() + timeout;
    2.30 +
    2.31 +    for (;;) {
    2.32          SDL_PumpEvents();
    2.33          switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
    2.34          case -1:
    2.35 @@ -418,7 +424,16 @@
    2.36          case 1:
    2.37              return 1;
    2.38          case 0:
    2.39 +            if (timeout == 0) {
    2.40 +                /* Polling and no events, just return */
    2.41 +                return 0;
    2.42 +            }
    2.43 +            if (timeout > 0 && ((int) (SDL_GetTicks() - expiration) >= 0)) {
    2.44 +                /* Timeout expired and no events */
    2.45 +                return 0;
    2.46 +            }
    2.47              SDL_Delay(10);
    2.48 +            break;
    2.49          }
    2.50      }
    2.51  }