src/events/SDL_events.c
changeset 5146 3052772b59db
parent 5123 dc0dfdd58f27
child 5154 fb424691cfc7
     1.1 --- a/src/events/SDL_events.c	Tue Feb 01 15:02:21 2011 -0800
     1.2 +++ b/src/events/SDL_events.c	Tue Feb 01 19:15:42 2011 -0800
     1.3 @@ -38,6 +38,14 @@
     1.4  SDL_EventFilter SDL_EventOK = NULL;
     1.5  void *SDL_EventOKParam;
     1.6  
     1.7 +typedef struct SDL_EventWatcher {
     1.8 +    SDL_EventFilter callback;
     1.9 +    void *userdata;
    1.10 +    struct SDL_EventWatcher *next;
    1.11 +} SDL_EventWatcher;
    1.12 +
    1.13 +static SDL_EventWatcher *SDL_event_watchers = NULL;
    1.14 +
    1.15  typedef struct {
    1.16      Uint32 bits[8];
    1.17  } SDL_DisabledEventBlock;
    1.18 @@ -96,6 +104,12 @@
    1.19              SDL_disabled_events[i] = NULL;
    1.20          }
    1.21      }
    1.22 +
    1.23 +    while (SDL_event_watchers) {
    1.24 +        SDL_EventWatcher *tmp = SDL_event_watchers;
    1.25 +        SDL_event_watchers = tmp->next;
    1.26 +        SDL_free(tmp);
    1.27 +    }
    1.28  }
    1.29  
    1.30  /* This function (and associated calls) may be called more than once */
    1.31 @@ -340,9 +354,16 @@
    1.32  int
    1.33  SDL_PushEvent(SDL_Event * event)
    1.34  {
    1.35 +    SDL_EventWatcher *curr;
    1.36 +
    1.37      if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
    1.38          return 0;
    1.39      }
    1.40 +
    1.41 +    for (curr = SDL_event_watchers; curr; curr = curr->next) {
    1.42 +        curr->callback(curr->userdata, event);
    1.43 +    }
    1.44 +
    1.45      if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
    1.46          return -1;
    1.47      }
    1.48 @@ -376,6 +397,43 @@
    1.49      return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
    1.50  }
    1.51  
    1.52 +/* FIXME: This is not thread-safe yet */
    1.53 +void
    1.54 +SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
    1.55 +{
    1.56 +    SDL_EventWatcher *watcher;
    1.57 +
    1.58 +    watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher));
    1.59 +    if (!watcher) {
    1.60 +        /* Uh oh... */
    1.61 +        return;
    1.62 +    }
    1.63 +    watcher->callback = filter;
    1.64 +    watcher->userdata = userdata;
    1.65 +    watcher->next = SDL_event_watchers;
    1.66 +    SDL_event_watchers = watcher;
    1.67 +}
    1.68 +
    1.69 +/* FIXME: This is not thread-safe yet */
    1.70 +void
    1.71 +SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
    1.72 +{
    1.73 +    SDL_EventWatcher *prev = NULL;
    1.74 +    SDL_EventWatcher *curr;
    1.75 +
    1.76 +    for (curr = SDL_event_watchers; curr; prev = curr, curr = curr->next) {
    1.77 +        if (curr->callback == filter && curr->userdata == userdata) {
    1.78 +            if (prev) {
    1.79 +                prev->next = curr->next;
    1.80 +            } else {
    1.81 +                SDL_event_watchers = curr->next;
    1.82 +            }
    1.83 +            SDL_free(curr);
    1.84 +            break;
    1.85 +        }
    1.86 +    }
    1.87 +}
    1.88 +
    1.89  void
    1.90  SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
    1.91  {