Added functions to watch events as they go through the event queue.
authorSam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 19:15:42 -0800
changeset 51463052772b59db
parent 5145 2f44e6969a59
child 5147 ad50b3db78bd
Added functions to watch events as they go through the event queue.
include/SDL_events.h
src/events/SDL_events.c
     1.1 --- a/include/SDL_events.h	Tue Feb 01 15:02:21 2011 -0800
     1.2 +++ b/include/SDL_events.h	Tue Feb 01 19:15:42 2011 -0800
     1.3 @@ -581,6 +581,18 @@
     1.4                                                      void **userdata);
     1.5  
     1.6  /**
     1.7 + *  Add a function which is called when an event is added to the queue.
     1.8 + */
     1.9 +extern DECLSPEC void SDLCALL SDL_AddEventWatch(SDL_EventFilter filter,
    1.10 +                                               void *userdata);
    1.11 +
    1.12 +/**
    1.13 + *  Remove an event watch function added with SDL_AddEventWatch()
    1.14 + */
    1.15 +extern DECLSPEC void SDLCALL SDL_DelEventWatch(SDL_EventFilter filter,
    1.16 +                                               void *userdata);
    1.17 +
    1.18 +/**
    1.19   *  Run the filter function on the current event queue, removing any
    1.20   *  events for which the filter returns 0.
    1.21   */
     2.1 --- a/src/events/SDL_events.c	Tue Feb 01 15:02:21 2011 -0800
     2.2 +++ b/src/events/SDL_events.c	Tue Feb 01 19:15:42 2011 -0800
     2.3 @@ -38,6 +38,14 @@
     2.4  SDL_EventFilter SDL_EventOK = NULL;
     2.5  void *SDL_EventOKParam;
     2.6  
     2.7 +typedef struct SDL_EventWatcher {
     2.8 +    SDL_EventFilter callback;
     2.9 +    void *userdata;
    2.10 +    struct SDL_EventWatcher *next;
    2.11 +} SDL_EventWatcher;
    2.12 +
    2.13 +static SDL_EventWatcher *SDL_event_watchers = NULL;
    2.14 +
    2.15  typedef struct {
    2.16      Uint32 bits[8];
    2.17  } SDL_DisabledEventBlock;
    2.18 @@ -96,6 +104,12 @@
    2.19              SDL_disabled_events[i] = NULL;
    2.20          }
    2.21      }
    2.22 +
    2.23 +    while (SDL_event_watchers) {
    2.24 +        SDL_EventWatcher *tmp = SDL_event_watchers;
    2.25 +        SDL_event_watchers = tmp->next;
    2.26 +        SDL_free(tmp);
    2.27 +    }
    2.28  }
    2.29  
    2.30  /* This function (and associated calls) may be called more than once */
    2.31 @@ -340,9 +354,16 @@
    2.32  int
    2.33  SDL_PushEvent(SDL_Event * event)
    2.34  {
    2.35 +    SDL_EventWatcher *curr;
    2.36 +
    2.37      if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
    2.38          return 0;
    2.39      }
    2.40 +
    2.41 +    for (curr = SDL_event_watchers; curr; curr = curr->next) {
    2.42 +        curr->callback(curr->userdata, event);
    2.43 +    }
    2.44 +
    2.45      if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
    2.46          return -1;
    2.47      }
    2.48 @@ -376,6 +397,43 @@
    2.49      return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
    2.50  }
    2.51  
    2.52 +/* FIXME: This is not thread-safe yet */
    2.53 +void
    2.54 +SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
    2.55 +{
    2.56 +    SDL_EventWatcher *watcher;
    2.57 +
    2.58 +    watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher));
    2.59 +    if (!watcher) {
    2.60 +        /* Uh oh... */
    2.61 +        return;
    2.62 +    }
    2.63 +    watcher->callback = filter;
    2.64 +    watcher->userdata = userdata;
    2.65 +    watcher->next = SDL_event_watchers;
    2.66 +    SDL_event_watchers = watcher;
    2.67 +}
    2.68 +
    2.69 +/* FIXME: This is not thread-safe yet */
    2.70 +void
    2.71 +SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
    2.72 +{
    2.73 +    SDL_EventWatcher *prev = NULL;
    2.74 +    SDL_EventWatcher *curr;
    2.75 +
    2.76 +    for (curr = SDL_event_watchers; curr; prev = curr, curr = curr->next) {
    2.77 +        if (curr->callback == filter && curr->userdata == userdata) {
    2.78 +            if (prev) {
    2.79 +                prev->next = curr->next;
    2.80 +            } else {
    2.81 +                SDL_event_watchers = curr->next;
    2.82 +            }
    2.83 +            SDL_free(curr);
    2.84 +            break;
    2.85 +        }
    2.86 +    }
    2.87 +}
    2.88 +
    2.89  void
    2.90  SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
    2.91  {