370 |
370 |
371 |
371 |
372 bool Terminal::wait_event(Event &event, Uint32 timeout) |
372 bool Terminal::wait_event(Event &event, Uint32 timeout) |
373 { |
373 { |
374 static SDL_Event sdl_event; |
374 static SDL_Event sdl_event; |
|
375 bool event_ready = false; |
375 |
376 |
376 // use timer to simulate SDL_WaitEventTimeout, which is not available in SDL 1.2 |
377 // use timer to simulate SDL_WaitEventTimeout, which is not available in SDL 1.2 |
377 SDL_TimerID timer_id = NULL; |
378 SDL_TimerID timer_id = NULL; |
378 if (timeout) |
379 if (timeout) |
379 { |
380 { |
380 timer_id = SDL_AddTimer(timeout, _wait_event_callback, NULL); |
381 timer_id = SDL_AddTimer(timeout, _wait_event_callback, NULL); |
381 } |
382 } |
382 |
383 |
383 while (SDL_WaitEvent(&sdl_event)) |
384 while (SDL_WaitEvent(&sdl_event) && !event_ready) |
384 { |
385 { |
385 if (timer_id) |
|
386 { |
|
387 if (sdl_event.type == SDL_USEREVENT && sdl_event.user.code == 1) |
|
388 { |
|
389 // timeout |
|
390 return false; |
|
391 } |
|
392 // some event came before timeout... |
|
393 SDL_RemoveTimer(timer_id); |
|
394 timer_id = NULL; |
|
395 } |
|
396 switch (sdl_event.type) |
386 switch (sdl_event.type) |
397 { |
387 { |
398 case SDL_USEREVENT: |
388 case SDL_USEREVENT: |
|
389 // timeout |
|
390 if (sdl_event.user.code == 1) |
|
391 { |
|
392 SDL_RemoveTimer(timer_id); |
|
393 return false; |
|
394 } |
399 // toggle blink |
395 // toggle blink |
400 if (sdl_event.user.code == 2) |
396 if (sdl_event.user.code == 2) |
401 { |
397 { |
402 _screen.toggle_blink(); |
398 _screen.toggle_blink(); |
403 } |
399 } |
404 break; // continue loop |
400 break; // continue loop |
|
401 |
405 case SDL_QUIT: |
402 case SDL_QUIT: |
406 event.type = Event::QUIT; |
403 event.type = Event::QUIT; |
407 return true; |
404 event_ready = true; |
|
405 break; |
408 |
406 |
409 case SDL_VIDEORESIZE: |
407 case SDL_VIDEORESIZE: |
410 event.type = Event::RESIZE; |
408 event.type = Event::RESIZE; |
411 _screen.resize(sdl_event.resize.w, sdl_event.resize.h); |
409 _screen.resize(sdl_event.resize.w, sdl_event.resize.h); |
412 return true; |
410 event_ready = true; |
|
411 break; |
413 |
412 |
414 case SDL_VIDEOEXPOSE: |
413 case SDL_VIDEOEXPOSE: |
415 _screen.redraw(); |
414 _screen.redraw(); |
416 break; |
415 break; |
417 |
416 |
431 event.key.keyname[0] = 0; |
430 event.key.keyname[0] = 0; |
432 event.key.unicode = sdl_event.key.keysym.unicode; |
431 event.key.unicode = sdl_event.key.keysym.unicode; |
433 if (!event.key.unicode) |
432 if (!event.key.unicode) |
434 break; // continue loop (unknown key) |
433 break; // continue loop (unknown key) |
435 } |
434 } |
436 return true; |
435 event_ready = true; |
|
436 break; |
437 } |
437 } |
438 |
438 |
439 case SDL_MOUSEBUTTONDOWN: |
439 case SDL_MOUSEBUTTONDOWN: |
440 case SDL_MOUSEBUTTONUP: |
440 case SDL_MOUSEBUTTONUP: |
441 event.type = (sdl_event.type == SDL_MOUSEBUTTONDOWN) ? Event::MOUSEDOWN : Event::MOUSEUP; |
441 event.type = (sdl_event.type == SDL_MOUSEBUTTONDOWN) ? Event::MOUSEDOWN : Event::MOUSEUP; |
447 if (sdl_event.type == SDL_MOUSEBUTTONUP) |
447 if (sdl_event.type == SDL_MOUSEBUTTONUP) |
448 break; // do not report button-up events for mouse wheel |
448 break; // do not report button-up events for mouse wheel |
449 event.type = Event::MOUSEWHEEL; |
449 event.type = Event::MOUSEWHEEL; |
450 } |
450 } |
451 _mousemove_last_x = -1; |
451 _mousemove_last_x = -1; |
452 return true; |
452 event_ready = true; |
|
453 break; |
453 |
454 |
454 case SDL_MOUSEMOTION: |
455 case SDL_MOUSEMOTION: |
455 if (sdl_event.motion.state == 0) |
456 if (sdl_event.motion.state == 0) |
456 break; // continue loop, do not report move events when no button is pressed |
457 break; // continue loop, do not report move events when no button is pressed |
457 event.type = Event::MOUSEMOVE; |
458 event.type = Event::MOUSEMOVE; |
460 if (_mousemove_last_x != event.mouse.x || |
461 if (_mousemove_last_x != event.mouse.x || |
461 _mousemove_last_y != event.mouse.y) |
462 _mousemove_last_y != event.mouse.y) |
462 { |
463 { |
463 _mousemove_last_x = event.mouse.x; |
464 _mousemove_last_x = event.mouse.x; |
464 _mousemove_last_y = event.mouse.y; |
465 _mousemove_last_y = event.mouse.y; |
465 return true; |
466 event_ready = true; |
|
467 break; |
466 } |
468 } |
467 break; // continue loop when mouse position did not change |
469 break; // continue loop when mouse position did not change |
468 |
470 |
469 default: |
471 default: |
470 break; // continue loop |
472 break; // continue loop |
471 } |
473 } |
472 } |
474 } |
473 throw SDLTermError(std::string("SDL_WaitEvent: ") + SDL_GetError()); |
475 |
|
476 // remove timer when other event came before timeout |
|
477 if (timeout) |
|
478 { |
|
479 SDL_RemoveTimer(timer_id); |
|
480 } |
|
481 |
|
482 // ok or error? |
|
483 if (event_ready) |
|
484 { |
|
485 return true; |
|
486 } |
|
487 else |
|
488 { |
|
489 throw SDLTermError(std::string("SDL_WaitEvent: ") + SDL_GetError()); |
|
490 } |
474 } |
491 } |
475 |
492 |
476 |
493 |
477 const char *Terminal::_translate_keyname(SDLKey sym) |
494 const char *Terminal::_translate_keyname(SDLKey sym) |
478 { |
495 { |