Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
SDL_PushEvent() calls the event filter code, and has a return value t…
Browse files Browse the repository at this point in the history
…o tell

whether or not the event was actually pushed.
SDL_GetEventFilter() now returns an SDL_bool instead of the filter function.
  • Loading branch information
slouken committed Jul 8, 2006
1 parent c16ff97 commit e800aa1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 67 deletions.
9 changes: 5 additions & 4 deletions include/SDL_events.h
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
47 changes: 32 additions & 15 deletions src/SDL_compat.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -291,13 +284,35 @@ 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)
{
SDL_EventFilter filter;
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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
23 changes: 13 additions & 10 deletions src/events/SDL_events.c
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 3 additions & 13 deletions src/events/SDL_keyboard.c
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
18 changes: 3 additions & 15 deletions src/events/SDL_mouse.c
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions src/events/SDL_quit.c
Expand Up @@ -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);
}
Expand Down
6 changes: 1 addition & 5 deletions src/events/SDL_windowevents.c
Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions test/testwm.c
Expand Up @@ -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:
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit e800aa1

Please sign in to comment.