From ca955b4c6dab2ac4c279714ed51fe00cdff7b32c Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 28 Apr 2010 23:26:13 +0000 Subject: Make the SDL surface event handler cope with timeout correctly svn path=/trunk/libnsfb/; revision=10514 --- src/surface/sdl.c | 37 +++++++++++++++++++++++++++++++++---- 1 file 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) -- cgit v1.2.3