SDL_PushEvent() calls the event filter code, and has a return value to tell SDL-1.3
authorSam Lantinga
Sat, 08 Jul 2006 20:07:08 +0000
branchSDL-1.3
changeset 1731875c3cf1a12c
parent 1730 e70477157db9
child 1732 fd65f12b6de6
SDL_PushEvent() calls the event filter code, and has a return value to tell
whether or not the event was actually pushed.
SDL_GetEventFilter() now returns an SDL_bool instead of the filter function.
include/SDL_events.h
src/SDL_compat.c
src/events/SDL_events.c
src/events/SDL_keyboard.c
src/events/SDL_mouse.c
src/events/SDL_quit.c
src/events/SDL_windowevents.c
test/testwm.c
     1.1 --- a/include/SDL_events.h	Sat Jul 08 18:06:02 2006 +0000
     1.2 +++ b/include/SDL_events.h	Sat Jul 08 20:07:08 2006 +0000
     1.3 @@ -389,8 +389,8 @@
     1.4  extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event * event);
     1.5  
     1.6  /* Add an event to the event queue.
     1.7 -   This function returns 0 on success, or -1 if the event queue was full
     1.8 -   or there was some other error.
     1.9 +   This function returns 1 on success, 0 if the event was filtered,
    1.10 +   or -1 if the event queue was full or there was some other error.
    1.11   */
    1.12  extern DECLSPEC int SDLCALL SDL_PushEvent(SDL_Event * event);
    1.13  
    1.14 @@ -422,9 +422,10 @@
    1.15  
    1.16  /*
    1.17    Return the current event filter - can be used to "chain" filters.
    1.18 -  If there is no event filter set, this function returns NULL.
    1.19 +  If there is no event filter set, this function returns SDL_FALSE.
    1.20  */
    1.21 -extern DECLSPEC SDL_EventFilter SDLCALL SDL_GetEventFilter(void **userdata);
    1.22 +extern DECLSPEC SDL_bool SDLCALL SDL_GetEventFilter(SDL_EventFilter * filter,
    1.23 +                                                    void **userdata);
    1.24  
    1.25  /*
    1.26    Run the filter function on the current event queue, removing any
     2.1 --- a/src/SDL_compat.c	Sat Jul 08 18:06:02 2006 +0000
     2.2 +++ b/src/SDL_compat.c	Sat Jul 08 20:07:08 2006 +0000
     2.3 @@ -153,9 +153,6 @@
     2.4      return modes;
     2.5  }
     2.6  
     2.7 -static SDL_EventFilter orig_eventfilter;
     2.8 -static void *orig_eventfilterparam;
     2.9 -
    2.10  static int
    2.11  SDL_CompatEventFilter(void *userdata, SDL_Event * event)
    2.12  {
    2.13 @@ -269,11 +266,7 @@
    2.14          }
    2.15  
    2.16      }
    2.17 -    if (orig_eventfilter) {
    2.18 -        return orig_eventfilter(orig_eventfilterparam, event);
    2.19 -    } else {
    2.20 -        return 1;
    2.21 -    }
    2.22 +    return 1;
    2.23  }
    2.24  
    2.25  static int
    2.26 @@ -291,6 +284,26 @@
    2.27      return 0;
    2.28  }
    2.29  
    2.30 +static void
    2.31 +GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
    2.32 +{
    2.33 +    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
    2.34 +    const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
    2.35 +    if (window) {
    2.36 +        if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
    2.37 +            return;
    2.38 +        }
    2.39 +        if (SDL_strcmp(window, "center") == 0) {
    2.40 +            center = window;
    2.41 +        }
    2.42 +    }
    2.43 +    if (center) {
    2.44 +        const SDL_DisplayMode *current = SDL_GetDesktopDisplayMode();
    2.45 +        *x = (current->w - w) / 2;
    2.46 +        *y = (current->h - h) / 2;
    2.47 +    }
    2.48 +}
    2.49 +
    2.50  SDL_Surface *
    2.51  SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
    2.52  {
    2.53 @@ -298,6 +311,8 @@
    2.54      void *filterparam;
    2.55      const SDL_DisplayMode *desktop_mode;
    2.56      SDL_DisplayMode mode;
    2.57 +    int window_x = SDL_WINDOWPOS_UNDEFINED;
    2.58 +    int window_y = SDL_WINDOWPOS_UNDEFINED;
    2.59      Uint32 window_flags;
    2.60      Uint32 desktop_format;
    2.61      Uint32 desired_format;
    2.62 @@ -321,15 +336,15 @@
    2.63          SDL_FreeSurface(SDL_VideoSurface);
    2.64          SDL_VideoSurface = NULL;
    2.65      }
    2.66 +    if (SDL_VideoWindow) {
    2.67 +        SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
    2.68 +    }
    2.69      SDL_DestroyWindow(SDL_VideoWindow);
    2.70  
    2.71      /* Set up the event filter */
    2.72 -    filter = SDL_GetEventFilter(&filterparam);
    2.73 -    if (filter != SDL_CompatEventFilter) {
    2.74 -        orig_eventfilter = filter;
    2.75 -        orig_eventfilterparam = filterparam;
    2.76 +    if (!SDL_GetEventFilter(NULL, NULL)) {
    2.77 +        SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
    2.78      }
    2.79 -    SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
    2.80  
    2.81      /* Create a new window */
    2.82      window_flags = SDL_WINDOW_SHOWN;
    2.83 @@ -345,9 +360,11 @@
    2.84      if (flags & SDL_NOFRAME) {
    2.85          window_flags |= SDL_WINDOW_BORDERLESS;
    2.86      }
    2.87 +    if (SDL_getenv("SDL_WINDOW_POS")) {
    2.88 +    }
    2.89 +    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
    2.90      SDL_VideoWindow =
    2.91 -        SDL_CreateWindow(wm_title, SDL_WINDOWPOS_UNDEFINED,
    2.92 -                         SDL_WINDOWPOS_UNDEFINED, width, height,
    2.93 +        SDL_CreateWindow(wm_title, window_x, window_y, width, height,
    2.94                           window_flags);
    2.95      if (!SDL_VideoWindow) {
    2.96          return NULL;
     3.1 --- a/src/events/SDL_events.c	Sat Jul 08 18:06:02 2006 +0000
     3.2 +++ b/src/events/SDL_events.c	Sat Jul 08 20:07:08 2006 +0000
     3.3 @@ -435,9 +435,13 @@
     3.4  int
     3.5  SDL_PushEvent(SDL_Event * event)
     3.6  {
     3.7 -    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0)
     3.8 +    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
     3.9 +        return 0;
    3.10 +    }
    3.11 +    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0) {
    3.12          return -1;
    3.13 -    return 0;
    3.14 +    }
    3.15 +    return 1;
    3.16  }
    3.17  
    3.18  void
    3.19 @@ -451,13 +455,16 @@
    3.20      while (SDL_PollEvent(&bitbucket) > 0);
    3.21  }
    3.22  
    3.23 -SDL_EventFilter
    3.24 -SDL_GetEventFilter(void **userdata)
    3.25 +SDL_bool
    3.26 +SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
    3.27  {
    3.28 +    if (filter) {
    3.29 +        *filter = SDL_EventOK;
    3.30 +    }
    3.31      if (userdata) {
    3.32          *userdata = SDL_EventOKParam;
    3.33      }
    3.34 -    return (SDL_EventOK);
    3.35 +    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
    3.36  }
    3.37  
    3.38  void
    3.39 @@ -536,11 +543,7 @@
    3.40          SDL_memset(&event, 0, sizeof(event));
    3.41          event.type = SDL_SYSWMEVENT;
    3.42          event.syswm.msg = message;
    3.43 -        if ((SDL_EventOK == NULL)
    3.44 -            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
    3.45 -            posted = 1;
    3.46 -            SDL_PushEvent(&event);
    3.47 -        }
    3.48 +        posted = (SDL_PushEvent(&event) > 0);
    3.49      }
    3.50      /* Update internal event state */
    3.51      return (posted);
     4.1 --- a/src/events/SDL_keyboard.c	Sat Jul 08 18:06:02 2006 +0000
     4.2 +++ b/src/events/SDL_keyboard.c	Sat Jul 08 20:07:08 2006 +0000
     4.3 @@ -664,10 +664,7 @@
     4.4              keyboard->repeat.firsttime = 1;
     4.5              keyboard->repeat.timestamp = 1;
     4.6          }
     4.7 -        if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) {
     4.8 -            posted = 1;
     4.9 -            SDL_PushEvent(&event);
    4.10 -        }
    4.11 +        posted = (SDL_PushEvent(&event) > 0);
    4.12      }
    4.13      return (posted);
    4.14  }
    4.15 @@ -690,10 +687,7 @@
    4.16          event.text.which = (Uint8) index;
    4.17          SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
    4.18          event.key.windowID = keyboard->focus;
    4.19 -        if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) {
    4.20 -            posted = 1;
    4.21 -            SDL_PushEvent(&event);
    4.22 -        }
    4.23 +        posted = (SDL_PushEvent(&event) > 0);
    4.24      }
    4.25      return (posted);
    4.26  }
    4.27 @@ -726,11 +720,7 @@
    4.28              } else {
    4.29                  if (interval > (Uint32) keyboard->repeat.interval) {
    4.30                      keyboard->repeat.timestamp = now;
    4.31 -                    if ((SDL_EventOK == NULL)
    4.32 -                        || SDL_EventOK(SDL_EventOKParam,
    4.33 -                                       &keyboard->repeat.evt)) {
    4.34 -                        SDL_PushEvent(&keyboard->repeat.evt);
    4.35 -                    }
    4.36 +                    SDL_PushEvent(&keyboard->repeat.evt);
    4.37                  }
    4.38              }
    4.39          }
     5.1 --- a/src/events/SDL_mouse.c	Sat Jul 08 18:06:02 2006 +0000
     5.2 +++ b/src/events/SDL_mouse.c	Sat Jul 08 20:07:08 2006 +0000
     5.3 @@ -371,11 +371,7 @@
     5.4          event.motion.xrel = xrel;
     5.5          event.motion.yrel = yrel;
     5.6          event.motion.windowID = mouse->focus;
     5.7 -        if ((SDL_EventOK == NULL)
     5.8 -            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
     5.9 -            posted = 1;
    5.10 -            SDL_PushEvent(&event);
    5.11 -        }
    5.12 +        posted = (SDL_PushEvent(&event) > 0);
    5.13      }
    5.14      return posted;
    5.15  }
    5.16 @@ -425,11 +421,7 @@
    5.17          event.button.x = mouse->x;
    5.18          event.button.y = mouse->y;
    5.19          event.button.windowID = mouse->focus;
    5.20 -        if ((SDL_EventOK == NULL)
    5.21 -            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
    5.22 -            posted = 1;
    5.23 -            SDL_PushEvent(&event);
    5.24 -        }
    5.25 +        posted = (SDL_PushEvent(&event) > 0);
    5.26      }
    5.27      return posted;
    5.28  }
    5.29 @@ -452,11 +444,7 @@
    5.30          event.wheel.which = (Uint8) index;
    5.31          event.wheel.motion = motion;
    5.32          event.wheel.windowID = mouse->focus;
    5.33 -        if ((SDL_EventOK == NULL)
    5.34 -            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
    5.35 -            posted = 1;
    5.36 -            SDL_PushEvent(&event);
    5.37 -        }
    5.38 +        posted = (SDL_PushEvent(&event) > 0);
    5.39      }
    5.40      return posted;
    5.41  }
     6.1 --- a/src/events/SDL_quit.c	Sat Jul 08 18:06:02 2006 +0000
     6.2 +++ b/src/events/SDL_quit.c	Sat Jul 08 20:07:08 2006 +0000
     6.3 @@ -88,11 +88,7 @@
     6.4      if (SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE) {
     6.5          SDL_Event event;
     6.6          event.type = SDL_QUIT;
     6.7 -        if ((SDL_EventOK == NULL)
     6.8 -            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
     6.9 -            posted = 1;
    6.10 -            SDL_PushEvent(&event);
    6.11 -        }
    6.12 +        posted = (SDL_PushEvent(&event) > 0);
    6.13      }
    6.14      return (posted);
    6.15  }
     7.1 --- a/src/events/SDL_windowevents.c	Sat Jul 08 18:06:02 2006 +0000
     7.2 +++ b/src/events/SDL_windowevents.c	Sat Jul 08 20:07:08 2006 +0000
     7.3 @@ -114,11 +114,7 @@
     7.4          event.window.data1 = data1;
     7.5          event.window.data2 = data2;
     7.6          event.window.windowID = windowID;
     7.7 -        if ((SDL_EventOK == NULL)
     7.8 -            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
     7.9 -            posted = 1;
    7.10 -            SDL_PushEvent(&event);
    7.11 -        }
    7.12 +        posted = (SDL_PushEvent(&event) > 0);
    7.13      }
    7.14      return (posted);
    7.15  }
     8.1 --- a/test/testwm.c	Sat Jul 08 18:06:02 2006 +0000
     8.2 +++ b/test/testwm.c	Sat Jul 08 20:07:08 2006 +0000
     8.3 @@ -175,11 +175,18 @@
     8.4      SDL_PushEvent(&event);
     8.5  }
     8.6  
     8.7 +static int SDLCALL(*old_filterfunc) (void *, SDL_Event *);
     8.8 +static void *old_filterdata;
     8.9 +
    8.10  int SDLCALL
    8.11  FilterEvents(void *userdata, SDL_Event * event)
    8.12  {
    8.13      static int reallyquit = 0;
    8.14  
    8.15 +    if (old_filterfunc) {
    8.16 +        old_filterfunc(old_filterdata, event);
    8.17 +    }
    8.18 +
    8.19      switch (event->type) {
    8.20  
    8.21      case SDL_ACTIVEEVENT:
    8.22 @@ -344,6 +351,7 @@
    8.23      }
    8.24  
    8.25      /* Set an event filter that discards everything but QUIT */
    8.26 +    SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
    8.27      SDL_SetEventFilter(FilterEvents, NULL);
    8.28  
    8.29      /* Ignore key up events, they don't even get filtered */