SDL_PushEvent() calls the event filter code, and has a return value to tell SDL-1.3
authorSam Lantinga <slouken@libsdl.org>
Sat, 08 Jul 2006 20:07:08 +0000
branchSDL-1.3
changeset 1731 875c3cf1a12c
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
--- a/include/SDL_events.h	Sat Jul 08 18:06:02 2006 +0000
+++ b/include/SDL_events.h	Sat Jul 08 20:07:08 2006 +0000
@@ -389,8 +389,8 @@
 extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event * event);
 
 /* Add an event to the event queue.
-   This function returns 0 on success, or -1 if the event queue was full
-   or there was some other error.
+   This function returns 1 on success, 0 if the event was filtered,
+   or -1 if the event queue was full or there was some other error.
  */
 extern DECLSPEC int SDLCALL SDL_PushEvent(SDL_Event * event);
 
@@ -422,9 +422,10 @@
 
 /*
   Return the current event filter - can be used to "chain" filters.
-  If there is no event filter set, this function returns NULL.
+  If there is no event filter set, this function returns SDL_FALSE.
 */
-extern DECLSPEC SDL_EventFilter SDLCALL SDL_GetEventFilter(void **userdata);
+extern DECLSPEC SDL_bool SDLCALL SDL_GetEventFilter(SDL_EventFilter * filter,
+                                                    void **userdata);
 
 /*
   Run the filter function on the current event queue, removing any
--- a/src/SDL_compat.c	Sat Jul 08 18:06:02 2006 +0000
+++ b/src/SDL_compat.c	Sat Jul 08 20:07:08 2006 +0000
@@ -153,9 +153,6 @@
     return modes;
 }
 
-static SDL_EventFilter orig_eventfilter;
-static void *orig_eventfilterparam;
-
 static int
 SDL_CompatEventFilter(void *userdata, SDL_Event * event)
 {
@@ -269,11 +266,7 @@
         }
 
     }
-    if (orig_eventfilter) {
-        return orig_eventfilter(orig_eventfilterparam, event);
-    } else {
-        return 1;
-    }
+    return 1;
 }
 
 static int
@@ -291,6 +284,26 @@
     return 0;
 }
 
+static void
+GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
+{
+    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
+    const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
+    if (window) {
+        if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
+            return;
+        }
+        if (SDL_strcmp(window, "center") == 0) {
+            center = window;
+        }
+    }
+    if (center) {
+        const SDL_DisplayMode *current = SDL_GetDesktopDisplayMode();
+        *x = (current->w - w) / 2;
+        *y = (current->h - h) / 2;
+    }
+}
+
 SDL_Surface *
 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
 {
@@ -298,6 +311,8 @@
     void *filterparam;
     const SDL_DisplayMode *desktop_mode;
     SDL_DisplayMode mode;
+    int window_x = SDL_WINDOWPOS_UNDEFINED;
+    int window_y = SDL_WINDOWPOS_UNDEFINED;
     Uint32 window_flags;
     Uint32 desktop_format;
     Uint32 desired_format;
@@ -321,15 +336,15 @@
         SDL_FreeSurface(SDL_VideoSurface);
         SDL_VideoSurface = NULL;
     }
+    if (SDL_VideoWindow) {
+        SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
+    }
     SDL_DestroyWindow(SDL_VideoWindow);
 
     /* Set up the event filter */
-    filter = SDL_GetEventFilter(&filterparam);
-    if (filter != SDL_CompatEventFilter) {
-        orig_eventfilter = filter;
-        orig_eventfilterparam = filterparam;
+    if (!SDL_GetEventFilter(NULL, NULL)) {
+        SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
     }
-    SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
 
     /* Create a new window */
     window_flags = SDL_WINDOW_SHOWN;
@@ -345,9 +360,11 @@
     if (flags & SDL_NOFRAME) {
         window_flags |= SDL_WINDOW_BORDERLESS;
     }
+    if (SDL_getenv("SDL_WINDOW_POS")) {
+    }
+    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
     SDL_VideoWindow =
-        SDL_CreateWindow(wm_title, SDL_WINDOWPOS_UNDEFINED,
-                         SDL_WINDOWPOS_UNDEFINED, width, height,
+        SDL_CreateWindow(wm_title, window_x, window_y, width, height,
                          window_flags);
     if (!SDL_VideoWindow) {
         return NULL;
--- a/src/events/SDL_events.c	Sat Jul 08 18:06:02 2006 +0000
+++ b/src/events/SDL_events.c	Sat Jul 08 20:07:08 2006 +0000
@@ -435,9 +435,13 @@
 int
 SDL_PushEvent(SDL_Event * event)
 {
-    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0)
+    if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
+        return 0;
+    }
+    if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0) {
         return -1;
-    return 0;
+    }
+    return 1;
 }
 
 void
@@ -451,13 +455,16 @@
     while (SDL_PollEvent(&bitbucket) > 0);
 }
 
-SDL_EventFilter
-SDL_GetEventFilter(void **userdata)
+SDL_bool
+SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
 {
+    if (filter) {
+        *filter = SDL_EventOK;
+    }
     if (userdata) {
         *userdata = SDL_EventOKParam;
     }
-    return (SDL_EventOK);
+    return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
 }
 
 void
@@ -536,11 +543,7 @@
         SDL_memset(&event, 0, sizeof(event));
         event.type = SDL_SYSWMEVENT;
         event.syswm.msg = message;
-        if ((SDL_EventOK == NULL)
-            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     /* Update internal event state */
     return (posted);
--- a/src/events/SDL_keyboard.c	Sat Jul 08 18:06:02 2006 +0000
+++ b/src/events/SDL_keyboard.c	Sat Jul 08 20:07:08 2006 +0000
@@ -664,10 +664,7 @@
             keyboard->repeat.firsttime = 1;
             keyboard->repeat.timestamp = 1;
         }
-        if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     return (posted);
 }
@@ -690,10 +687,7 @@
         event.text.which = (Uint8) index;
         SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
         event.key.windowID = keyboard->focus;
-        if ((SDL_EventOK == NULL) || SDL_EventOK(SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     return (posted);
 }
@@ -726,11 +720,7 @@
             } else {
                 if (interval > (Uint32) keyboard->repeat.interval) {
                     keyboard->repeat.timestamp = now;
-                    if ((SDL_EventOK == NULL)
-                        || SDL_EventOK(SDL_EventOKParam,
-                                       &keyboard->repeat.evt)) {
-                        SDL_PushEvent(&keyboard->repeat.evt);
-                    }
+                    SDL_PushEvent(&keyboard->repeat.evt);
                 }
             }
         }
--- a/src/events/SDL_mouse.c	Sat Jul 08 18:06:02 2006 +0000
+++ b/src/events/SDL_mouse.c	Sat Jul 08 20:07:08 2006 +0000
@@ -371,11 +371,7 @@
         event.motion.xrel = xrel;
         event.motion.yrel = yrel;
         event.motion.windowID = mouse->focus;
-        if ((SDL_EventOK == NULL)
-            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     return posted;
 }
@@ -425,11 +421,7 @@
         event.button.x = mouse->x;
         event.button.y = mouse->y;
         event.button.windowID = mouse->focus;
-        if ((SDL_EventOK == NULL)
-            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     return posted;
 }
@@ -452,11 +444,7 @@
         event.wheel.which = (Uint8) index;
         event.wheel.motion = motion;
         event.wheel.windowID = mouse->focus;
-        if ((SDL_EventOK == NULL)
-            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     return posted;
 }
--- a/src/events/SDL_quit.c	Sat Jul 08 18:06:02 2006 +0000
+++ b/src/events/SDL_quit.c	Sat Jul 08 20:07:08 2006 +0000
@@ -88,11 +88,7 @@
     if (SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE) {
         SDL_Event event;
         event.type = SDL_QUIT;
-        if ((SDL_EventOK == NULL)
-            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     return (posted);
 }
--- a/src/events/SDL_windowevents.c	Sat Jul 08 18:06:02 2006 +0000
+++ b/src/events/SDL_windowevents.c	Sat Jul 08 20:07:08 2006 +0000
@@ -114,11 +114,7 @@
         event.window.data1 = data1;
         event.window.data2 = data2;
         event.window.windowID = windowID;
-        if ((SDL_EventOK == NULL)
-            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
-            posted = 1;
-            SDL_PushEvent(&event);
-        }
+        posted = (SDL_PushEvent(&event) > 0);
     }
     return (posted);
 }
--- a/test/testwm.c	Sat Jul 08 18:06:02 2006 +0000
+++ b/test/testwm.c	Sat Jul 08 20:07:08 2006 +0000
@@ -175,11 +175,18 @@
     SDL_PushEvent(&event);
 }
 
+static int SDLCALL(*old_filterfunc) (void *, SDL_Event *);
+static void *old_filterdata;
+
 int SDLCALL
 FilterEvents(void *userdata, SDL_Event * event)
 {
     static int reallyquit = 0;
 
+    if (old_filterfunc) {
+        old_filterfunc(old_filterdata, event);
+    }
+
     switch (event->type) {
 
     case SDL_ACTIVEEVENT:
@@ -344,6 +351,7 @@
     }
 
     /* Set an event filter that discards everything but QUIT */
+    SDL_GetEventFilter(&old_filterfunc, &old_filterdata);
     SDL_SetEventFilter(FilterEvents, NULL);
 
     /* Ignore key up events, they don't even get filtered */