diff -r 282a07d20680 -r 911927edbdde sdlterm/src/sdlterm.cc --- a/sdlterm/src/sdlterm.cc Mon Jan 07 23:15:17 2013 +0100 +++ b/sdlterm/src/sdlterm.cc Tue Jan 08 01:12:07 2013 +0100 @@ -310,7 +310,7 @@ : _screen(), _attr(7), _cursor_x(0), _cursor_y(0), _cursor_visible(false), _mousemove_last_x(-1), _mousemove_last_y(-1) { - if (SDL_Init(SDL_INIT_VIDEO) == -1) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); throw std::exception(); @@ -341,22 +341,41 @@ } } -void Terminal::get_next_event(Event &event) + +bool Terminal::wait_event(Event &event, Uint32 timeout) { static SDL_Event sdl_event; + // use timer to simulate SDL_WaitEventTimeout, which is not available in SDL 1.2 + SDL_TimerID timer_id = NULL; + if (timeout) + { + timer_id = SDL_AddTimer(timeout, _wait_event_callback, NULL); + } + while (SDL_WaitEvent(&sdl_event)) { + if (timer_id) + { + if (sdl_event.type == SDL_USEREVENT && sdl_event.user.code == 1) + { + // timeout + return false; + } + // some event came before timeout... + SDL_RemoveTimer(timer_id); + timer_id = NULL; + } switch (sdl_event.type) { case SDL_QUIT: event.type = Event::QUIT; - return; + return true; case SDL_VIDEORESIZE: event.type = Event::RESIZE; _screen.resize(sdl_event.resize.w, sdl_event.resize.h); - return; + return true; case SDL_VIDEOEXPOSE: _screen.redraw(); @@ -380,7 +399,7 @@ if (!event.key.unicode) break; // continue loop (unknown key) } - return; + return true; } case SDL_MOUSEBUTTONDOWN: @@ -396,7 +415,7 @@ event.type = Event::MOUSEWHEEL; } _mousemove_last_x = -1; - return; + return true; case SDL_MOUSEMOTION: if (sdl_event.motion.state == 0) @@ -409,7 +428,7 @@ { _mousemove_last_x = event.mouse.x; _mousemove_last_y = event.mouse.y; - return; + return true; } break; // continue loop when mouse position did not change @@ -417,6 +436,8 @@ break; // continue loop } } + fprintf(stderr, "SDL_WaitEvent error: %s\n", SDL_GetError()); + throw std::exception(); } @@ -458,3 +479,13 @@ } } + +Uint32 Terminal::_wait_event_callback(Uint32 interval, void *param) +{ + SDL_Event event; + event.type = SDL_USEREVENT; + event.user.code = 1; + SDL_PushEvent(&event); + return 0; +} +