From e800aa141b58a0026ea8b752db9c6d7e334a092d Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 8 Jul 2006 20:07:08 +0000 Subject: [PATCH] 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 | 9 ++++--- src/SDL_compat.c | 47 ++++++++++++++++++++++++----------- src/events/SDL_events.c | 23 +++++++++-------- src/events/SDL_keyboard.c | 16 +++--------- src/events/SDL_mouse.c | 18 +++----------- src/events/SDL_quit.c | 6 +---- src/events/SDL_windowevents.c | 6 +---- test/testwm.c | 8 ++++++ 8 files changed, 66 insertions(+), 67 deletions(-) diff --git a/include/SDL_events.h b/include/SDL_events.h index 97d16e384..bc2df3465 100644 --- a/include/SDL_events.h +++ b/include/SDL_events.h @@ -389,8 +389,8 @@ extern DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event * event); 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 @@ extern DECLSPEC void SDLCALL SDL_SetEventFilter(SDL_EventFilter filter, /* 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 diff --git a/src/SDL_compat.c b/src/SDL_compat.c index 6ed5e61b2..b5d213a91 100644 --- a/src/SDL_compat.c +++ b/src/SDL_compat.c @@ -153,9 +153,6 @@ SDL_ListModes(SDL_PixelFormat * format, Uint32 flags) return modes; } -static SDL_EventFilter orig_eventfilter; -static void *orig_eventfilterparam; - static int SDL_CompatEventFilter(void *userdata, SDL_Event * event) { @@ -269,11 +266,7 @@ SDL_CompatEventFilter(void *userdata, SDL_Event * event) } } - if (orig_eventfilter) { - return orig_eventfilter(orig_eventfilterparam, event); - } else { - return 1; - } + return 1; } static int @@ -291,6 +284,26 @@ SDL_VideoPaletteChanged(void *userdata, SDL_Palette * palette) 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 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) 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_SetVideoMode(int width, int height, int bpp, Uint32 flags) 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 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) 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; diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c index c55df5eea..5b706fa41 100644 --- a/src/events/SDL_events.c +++ b/src/events/SDL_events.c @@ -435,9 +435,13 @@ SDL_WaitEvent(SDL_Event * event) 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 @@ SDL_SetEventFilter(SDL_EventFilter filter, void *userdata) 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_SendSysWMEvent(SDL_SysWMmsg * message) 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); diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 533e7b5e1..6adcedb12 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -664,10 +664,7 @@ SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key) 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 @@ SDL_SendKeyboardText(int index, const char *text) 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 @@ SDL_CheckKeyRepeat(void) } 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); } } } diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c index 87a323baa..0f8517d05 100644 --- a/src/events/SDL_mouse.c +++ b/src/events/SDL_mouse.c @@ -371,11 +371,7 @@ SDL_SendMouseMotion(int index, int relative, int x, int y) 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 @@ SDL_SendMouseButton(int index, Uint8 state, Uint8 button) 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 @@ SDL_SendMouseWheel(int index, int motion) 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; } diff --git a/src/events/SDL_quit.c b/src/events/SDL_quit.c index 5fca3d367..4d7567dfa 100644 --- a/src/events/SDL_quit.c +++ b/src/events/SDL_quit.c @@ -88,11 +88,7 @@ SDL_SendQuit(void) 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); } diff --git a/src/events/SDL_windowevents.c b/src/events/SDL_windowevents.c index 35f12a33a..08fa3d4dc 100644 --- a/src/events/SDL_windowevents.c +++ b/src/events/SDL_windowevents.c @@ -114,11 +114,7 @@ SDL_SendWindowEvent(SDL_WindowID windowID, Uint8 windowevent, int data1, 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); } diff --git a/test/testwm.c b/test/testwm.c index 299cb93bc..116840944 100644 --- a/test/testwm.c +++ b/test/testwm.c @@ -175,11 +175,18 @@ HotKey_Quit(void) 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 @@ main(int argc, char *argv[]) } /* 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 */