equal
deleted
inserted
replaced
308 |
308 |
309 Terminal::Terminal() |
309 Terminal::Terminal() |
310 : _screen(), _attr(7), _cursor_x(0), _cursor_y(0), _cursor_visible(false), |
310 : _screen(), _attr(7), _cursor_x(0), _cursor_y(0), _cursor_visible(false), |
311 _mousemove_last_x(-1), _mousemove_last_y(-1) |
311 _mousemove_last_x(-1), _mousemove_last_y(-1) |
312 { |
312 { |
313 if (SDL_Init(SDL_INIT_VIDEO) == -1) |
313 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) |
314 { |
314 { |
315 fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); |
315 fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); |
316 throw std::exception(); |
316 throw std::exception(); |
317 } |
317 } |
318 SDL_EnableUNICODE(1); |
318 SDL_EnableUNICODE(1); |
339 { |
339 { |
340 _screen.commit(); |
340 _screen.commit(); |
341 } |
341 } |
342 } |
342 } |
343 |
343 |
344 void Terminal::get_next_event(Event &event) |
344 |
|
345 bool Terminal::wait_event(Event &event, Uint32 timeout) |
345 { |
346 { |
346 static SDL_Event sdl_event; |
347 static SDL_Event sdl_event; |
347 |
348 |
|
349 // use timer to simulate SDL_WaitEventTimeout, which is not available in SDL 1.2 |
|
350 SDL_TimerID timer_id = NULL; |
|
351 if (timeout) |
|
352 { |
|
353 timer_id = SDL_AddTimer(timeout, _wait_event_callback, NULL); |
|
354 } |
|
355 |
348 while (SDL_WaitEvent(&sdl_event)) |
356 while (SDL_WaitEvent(&sdl_event)) |
349 { |
357 { |
|
358 if (timer_id) |
|
359 { |
|
360 if (sdl_event.type == SDL_USEREVENT && sdl_event.user.code == 1) |
|
361 { |
|
362 // timeout |
|
363 return false; |
|
364 } |
|
365 // some event came before timeout... |
|
366 SDL_RemoveTimer(timer_id); |
|
367 timer_id = NULL; |
|
368 } |
350 switch (sdl_event.type) |
369 switch (sdl_event.type) |
351 { |
370 { |
352 case SDL_QUIT: |
371 case SDL_QUIT: |
353 event.type = Event::QUIT; |
372 event.type = Event::QUIT; |
354 return; |
373 return true; |
355 |
374 |
356 case SDL_VIDEORESIZE: |
375 case SDL_VIDEORESIZE: |
357 event.type = Event::RESIZE; |
376 event.type = Event::RESIZE; |
358 _screen.resize(sdl_event.resize.w, sdl_event.resize.h); |
377 _screen.resize(sdl_event.resize.w, sdl_event.resize.h); |
359 return; |
378 return true; |
360 |
379 |
361 case SDL_VIDEOEXPOSE: |
380 case SDL_VIDEOEXPOSE: |
362 _screen.redraw(); |
381 _screen.redraw(); |
363 break; |
382 break; |
364 |
383 |
378 event.key.keyname[0] = 0; |
397 event.key.keyname[0] = 0; |
379 event.key.unicode = sdl_event.key.keysym.unicode; |
398 event.key.unicode = sdl_event.key.keysym.unicode; |
380 if (!event.key.unicode) |
399 if (!event.key.unicode) |
381 break; // continue loop (unknown key) |
400 break; // continue loop (unknown key) |
382 } |
401 } |
383 return; |
402 return true; |
384 } |
403 } |
385 |
404 |
386 case SDL_MOUSEBUTTONDOWN: |
405 case SDL_MOUSEBUTTONDOWN: |
387 case SDL_MOUSEBUTTONUP: |
406 case SDL_MOUSEBUTTONUP: |
388 event.type = (sdl_event.type == SDL_MOUSEBUTTONDOWN) ? Event::MOUSEDOWN : Event::MOUSEUP; |
407 event.type = (sdl_event.type == SDL_MOUSEBUTTONDOWN) ? Event::MOUSEDOWN : Event::MOUSEUP; |
394 if (sdl_event.type == SDL_MOUSEBUTTONUP) |
413 if (sdl_event.type == SDL_MOUSEBUTTONUP) |
395 break; // do not report button-up events for mouse wheel |
414 break; // do not report button-up events for mouse wheel |
396 event.type = Event::MOUSEWHEEL; |
415 event.type = Event::MOUSEWHEEL; |
397 } |
416 } |
398 _mousemove_last_x = -1; |
417 _mousemove_last_x = -1; |
399 return; |
418 return true; |
400 |
419 |
401 case SDL_MOUSEMOTION: |
420 case SDL_MOUSEMOTION: |
402 if (sdl_event.motion.state == 0) |
421 if (sdl_event.motion.state == 0) |
403 break; // continue loop, do not report move events when no button is pressed |
422 break; // continue loop, do not report move events when no button is pressed |
404 event.type = Event::MOUSEMOVE; |
423 event.type = Event::MOUSEMOVE; |
407 if (_mousemove_last_x != event.mouse.x || |
426 if (_mousemove_last_x != event.mouse.x || |
408 _mousemove_last_y != event.mouse.y) |
427 _mousemove_last_y != event.mouse.y) |
409 { |
428 { |
410 _mousemove_last_x = event.mouse.x; |
429 _mousemove_last_x = event.mouse.x; |
411 _mousemove_last_y = event.mouse.y; |
430 _mousemove_last_y = event.mouse.y; |
412 return; |
431 return true; |
413 } |
432 } |
414 break; // continue loop when mouse position did not change |
433 break; // continue loop when mouse position did not change |
415 |
434 |
416 default: |
435 default: |
417 break; // continue loop |
436 break; // continue loop |
418 } |
437 } |
419 } |
438 } |
|
439 fprintf(stderr, "SDL_WaitEvent error: %s\n", SDL_GetError()); |
|
440 throw std::exception(); |
420 } |
441 } |
421 |
442 |
422 |
443 |
423 const char *Terminal::_translate_keyname(SDLKey sym) |
444 const char *Terminal::_translate_keyname(SDLKey sym) |
424 { |
445 { |
456 case SDLK_PAUSE: return "pause"; |
477 case SDLK_PAUSE: return "pause"; |
457 default: return NULL; |
478 default: return NULL; |
458 } |
479 } |
459 } |
480 } |
460 |
481 |
|
482 |
|
483 Uint32 Terminal::_wait_event_callback(Uint32 interval, void *param) |
|
484 { |
|
485 SDL_Event event; |
|
486 event.type = SDL_USEREVENT; |
|
487 event.user.code = 1; |
|
488 SDL_PushEvent(&event); |
|
489 return 0; |
|
490 } |
|
491 |