dxx-rebirth/similar/arch/sdl/event.cpp

196 lines
4.3 KiB
C++
Raw Normal View History

2014-06-01 17:55:23 +00:00
/*
* This file is part of the DXX-Rebirth project <http://www.dxx-rebirth.com/>.
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
/*
*
* SDL Event related stuff
*
*
*/
2006-03-20 16:43:15 +00:00
#include <stdio.h>
#include <stdlib.h>
2009-05-21 12:16:39 +00:00
#include "event.h"
#include "key.h"
#include "mouse.h"
2009-05-21 12:16:39 +00:00
#include "window.h"
#include "timer.h"
#include "config.h"
2006-03-20 16:43:15 +00:00
#include "joy.h"
#include "args.h"
2006-03-20 16:43:15 +00:00
void event_poll()
{
SDL_Event event;
int clean_uniframe=1;
window *wind = window_get_front();
int idle = 1;
// If the front window changes, exit this loop, otherwise unintended behavior can occur
// like pressing 'Return' really fast at 'Difficulty Level' causing multiple games to be started
2014-07-04 03:48:47 +00:00
while ((wind == window_get_front()) && (event = {}, SDL_PollEvent(&event)))
{
switch(event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
if (clean_uniframe)
2014-07-14 03:18:40 +00:00
unicode_frame_buffer = {};
clean_uniframe=0;
2015-02-08 17:43:29 +00:00
key_handler(&event.key);
idle = 0;
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
2015-02-08 17:43:29 +00:00
mouse_button_handler(&event.button);
idle = 0;
break;
case SDL_MOUSEMOTION:
2015-02-08 17:43:29 +00:00
mouse_motion_handler(&event.motion);
idle = 0;
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
2015-02-08 17:43:29 +00:00
joy_button_handler(&event.jbutton);
idle = 0;
break;
case SDL_JOYAXISMOTION:
2015-02-08 17:43:29 +00:00
if (joy_axis_handler(&event.jaxis))
idle = 0;
break;
case SDL_JOYHATMOTION:
2015-02-08 17:43:29 +00:00
joy_hat_handler(&event.jhat);
idle = 0;
break;
case SDL_JOYBALLMOTION:
break;
case SDL_QUIT: {
d_event qevent = { EVENT_QUIT };
2014-10-04 21:47:13 +00:00
call_default_handler(qevent);
idle = 0;
} break;
}
}
// Send the idle event if there were no other events
if (idle)
{
d_event ievent;
ievent.type = EVENT_IDLE;
2014-10-04 21:47:13 +00:00
event_send(ievent);
}
else
event_reset_idle_seconds();
mouse_cursor_autohide();
}
2006-03-20 16:43:15 +00:00
void event_flush()
{
SDL_Event event;
while (SDL_PollEvent(&event));
}
2006-03-20 16:43:15 +00:00
int event_init()
{
// We should now be active and responding to events.
return 0;
}
2009-05-21 12:16:39 +00:00
2014-10-04 21:47:13 +00:00
int call_default_handler(const d_event &event)
{
return standard_handler(event);
}
2014-10-04 21:47:13 +00:00
void event_send(const d_event &event)
{
window *wind;
2014-08-06 02:10:49 +00:00
window_event_result handled = window_event_result::ignored;
2015-01-17 18:31:41 +00:00
for (wind = window_get_front(); wind && handled == window_event_result::ignored; wind = window_get_prev(*wind))
if (window_is_visible(wind))
{
2015-01-17 18:31:40 +00:00
handled = window_send_event(*wind, event);
if (!window_exists(wind)) // break away if necessary: window_send_event() could have closed wind by now
break;
2015-01-17 18:31:40 +00:00
if (window_is_modal(*wind))
break;
}
2014-08-06 02:10:49 +00:00
if (handled == window_event_result::ignored)
call_default_handler(event);
}
2009-05-21 12:16:39 +00:00
// Process the first event in queue, sending to the appropriate handler
// This is the new object-oriented system
// Uses the old system for now, but this may change
2009-05-21 12:16:39 +00:00
void event_process(void)
{
d_event event;
window *wind = window_get_front();
2009-05-21 12:16:39 +00:00
timer_update();
event_poll(); // send input events first
// Doing this prevents problems when a draw event can create a newmenu,
// such as some network menus when they report a problem
if (window_get_front() != wind)
return;
event.type = EVENT_WINDOW_DRAW; // then draw all visible windows
wind = window_get_first();
while (wind != NULL)
{
2015-01-17 18:31:41 +00:00
window *prev = window_get_prev(*wind);
2009-05-21 12:16:39 +00:00
if (window_is_visible(wind))
2015-01-17 18:31:40 +00:00
window_send_event(*wind, event);
if (!window_exists(wind))
{
if (!prev) // well there isn't a previous window ...
break; // ... just bail out - we've done everything for this frame we can.
2015-01-17 18:31:41 +00:00
wind = window_get_next(*prev); // the current window seemed to be closed. so take the next one from the previous which should be able to point to the one after the current closed
}
else
2015-01-17 18:31:41 +00:00
wind = window_get_next(*wind);
}
gr_flip();
2009-05-21 12:16:39 +00:00
}
2015-05-09 17:39:03 +00:00
template <bool activate_focus>
static void event_change_focus()
{
2015-05-14 02:23:13 +00:00
SDL_WM_GrabInput(activate_focus && GameCfg.Grabinput && likely(!GameArg.DbgForbidConsoleGrab) ? SDL_GRAB_ON : SDL_GRAB_OFF);
mouse_toggle_cursor(activate_focus);
}
2015-05-09 17:39:03 +00:00
void event_enable_focus()
{
event_change_focus<true>();
}
void event_disable_focus()
{
event_change_focus<false>();
}
static fix64 last_event = 0;
void event_reset_idle_seconds()
{
last_event = timer_query();
}
fix event_get_idle_seconds()
{
return (timer_query() - last_event)/F1_0;
}