diff options
author | Vincent Sanders <vince@netsurf-browser.org> | 2010-04-28 23:26:13 +0000 |
---|---|---|
committer | Vincent Sanders <vince@netsurf-browser.org> | 2010-04-28 23:26:13 +0000 |
commit | ca955b4c6dab2ac4c279714ed51fe00cdff7b32c (patch) | |
tree | cc1c3fdb9c7929fd903e1cba9b7e8eff69241583 | |
parent | 50f6ee7bd5cecb308ccf6284b0a44595adcf3903 (diff) | |
download | libnsfb-ca955b4c6dab2ac4c279714ed51fe00cdff7b32c.tar.gz |
Make the SDL surface event handler cope with timeout correctly
svn path=/trunk/libnsfb/; revision=10514
-rw-r--r-- | src/surface/sdl.c | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/src/surface/sdl.c b/src/surface/sdl.c index cc83a4e..f0f48af 100644 --- a/src/surface/sdl.c +++ b/src/surface/sdl.c @@ -445,7 +445,7 @@ static int sdl_initialise(nsfb_t *nsfb) return -1; /* initialise SDL library */ - if (SDL_Init(SDL_INIT_VIDEO) < 0 ) { + if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); return -1; } @@ -481,6 +481,22 @@ static int sdl_finalise(nsfb_t *nsfb) return 0; } +static uint32_t wakeeventtimer(uint32_t ival, void *param) +{ + SDL_Event event; + ival = ival; + param = param; + + event.type = SDL_USEREVENT; + event.user.code = 0; + event.user.data1 = 0; + event.user.data2 = 0; + + SDL_PushEvent(&event); + + return 0; +} + static bool sdl_input(nsfb_t *nsfb, nsfb_event_t *event, int timeout) { int got_event; @@ -488,10 +504,23 @@ static bool sdl_input(nsfb_t *nsfb, nsfb_event_t *event, int timeout) nsfb = nsfb; /* unused */ - if (timeout < 0) - got_event = SDL_WaitEvent(&sdlevent); - else + if (timeout == 0) { got_event = SDL_PollEvent(&sdlevent); + } else { + if (timeout > 0) { + /* setup wake timer to ensure the wait event below exits no later + * than when the timeout has occoured. + */ + SDL_TimerID tid; + tid = SDL_AddTimer(timeout, wakeeventtimer, NULL); + got_event = SDL_WaitEvent(&sdlevent); + if ((got_event == 0) || (sdlevent.type != SDL_USEREVENT)) { + SDL_RemoveTimer(tid); + } + } else { + got_event = SDL_WaitEvent(&sdlevent); + } + } /* Do nothing if there was no event */ if (got_event == 0) |