Only react to input events we care about

It seems that at least under X11, SDL2 sends events to the window constantly even when no user input has occurred.
This resulted in 100% cpu usage when having the GUI open, since for every event detected we were redrawing the screen for no reason.
With this change, we monitor only the kind of events we want to react upon, and perform the screen update when one of those is triggered.
This commit is contained in:
Dimitris Panokostas 2020-02-05 23:33:39 +01:00
parent 79b4c8dfe9
commit bfe8a34b20
6 changed files with 85 additions and 11 deletions

View file

@ -212,10 +212,10 @@ static void CreateFilesysHardfileLoop()
SDL_Event touch_event;
while (SDL_PollEvent(&event))
{
gotEvent = 1;
switch (event.type)
{
case SDL_KEYDOWN:
gotEvent = 1;
switch (event.key.keysym.sym)
{
case VK_ESCAPE:
@ -259,6 +259,7 @@ static void CreateFilesysHardfileLoop()
case SDL_JOYAXISMOTION:
if (gui_joystick)
{
gotEvent = 1;
const int hat = SDL_JoystickGetHat(gui_joystick, 0);
if (SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].dpad_up) || (hat & SDL_HAT_UP) ||
@ -309,6 +310,7 @@ static void CreateFilesysHardfileLoop()
break;
case SDL_FINGERDOWN:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONDOWN;
touch_event.button.which = 0;
@ -320,6 +322,7 @@ static void CreateFilesysHardfileLoop()
break;
case SDL_FINGERUP:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONUP;
touch_event.button.which = 0;
@ -331,6 +334,7 @@ static void CreateFilesysHardfileLoop()
break;
case SDL_FINGERMOTION:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEMOTION;
touch_event.motion.which = 0;
@ -340,6 +344,13 @@ static void CreateFilesysHardfileLoop()
gui_input->pushInput(touch_event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
gotEvent = 1;
break;
default:
break;

View file

@ -384,10 +384,10 @@ static void EditFilesysHardfileLoop()
SDL_Event touch_event;
while (SDL_PollEvent(&event))
{
gotEvent = 1;
switch (event.type)
{
case SDL_KEYDOWN:
gotEvent = 1;
switch (event.key.keysym.sym)
{
case VK_ESCAPE:
@ -430,6 +430,7 @@ static void EditFilesysHardfileLoop()
case SDL_JOYAXISMOTION:
if (gui_joystick)
{
gotEvent = 1;
const int hat = SDL_JoystickGetHat(gui_joystick, 0);
if (SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].dpad_up) || (hat & SDL_HAT_UP) || SDL_JoystickGetAxis(gui_joystick, host_input_buttons[0].lstick_axis_y) == -32768) // dpad
@ -475,6 +476,7 @@ static void EditFilesysHardfileLoop()
break;
case SDL_FINGERDOWN:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONDOWN;
touch_event.button.which = 0;
@ -486,6 +488,7 @@ static void EditFilesysHardfileLoop()
break;
case SDL_FINGERUP:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONUP;
touch_event.button.which = 0;
@ -497,6 +500,7 @@ static void EditFilesysHardfileLoop()
break;
case SDL_FINGERMOTION:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEMOTION;
touch_event.motion.which = 0;
@ -506,6 +510,13 @@ static void EditFilesysHardfileLoop()
gui_input->pushInput(touch_event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
gotEvent = 1;
break;
default:
break;
}

View file

@ -222,10 +222,10 @@ static void EditFilesysVirtualLoop()
SDL_Event touch_event;
while (SDL_PollEvent(&event))
{
gotEvent = 1;
switch (event.type)
{
case SDL_KEYDOWN:
gotEvent = 1;
switch (event.key.keysym.sym)
{
case VK_ESCAPE:
@ -268,6 +268,7 @@ static void EditFilesysVirtualLoop()
case SDL_JOYAXISMOTION:
if (gui_joystick)
{
gotEvent = 1;
const int hat = SDL_JoystickGetHat(gui_joystick, 0);
if (SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].dpad_up) || (hat & SDL_HAT_UP) || SDL_JoystickGetAxis(gui_joystick, host_input_buttons[0].lstick_axis_y) == -32768) // dpad
@ -313,6 +314,7 @@ static void EditFilesysVirtualLoop()
break;
case SDL_FINGERDOWN:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONDOWN;
touch_event.button.which = 0;
@ -324,6 +326,7 @@ static void EditFilesysVirtualLoop()
break;
case SDL_FINGERUP:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONUP;
touch_event.button.which = 0;
@ -335,6 +338,7 @@ static void EditFilesysVirtualLoop()
break;
case SDL_FINGERMOTION:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEMOTION;
touch_event.motion.which = 0;
@ -344,6 +348,13 @@ static void EditFilesysVirtualLoop()
gui_input->pushInput(touch_event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
gotEvent = 1;
break;
default:
break;
}

View file

@ -140,10 +140,10 @@ void message_checkInput()
SDL_Event touch_event;
while (SDL_PollEvent(&msg_event))
{
gotEvent = 1;
switch(msg_event.type)
{
case SDL_KEYDOWN:
gotEvent = 1;
switch (msg_event.key.keysym.sym)
{
case VK_ESCAPE:
@ -166,6 +166,7 @@ void message_checkInput()
case SDL_JOYBUTTONDOWN:
if (gui_joystick)
{
gotEvent = 1;
if (SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].east_button) ||
SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].start_button) ||
SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].east_button))
@ -175,6 +176,7 @@ void message_checkInput()
break;
case SDL_FINGERDOWN:
gotEvent = 1;
memcpy(&touch_event, &msg_event, sizeof msg_event);
touch_event.type = SDL_MOUSEBUTTONDOWN;
touch_event.button.which = 0;
@ -186,6 +188,7 @@ void message_checkInput()
break;
case SDL_FINGERUP:
gotEvent = 1;
memcpy(&touch_event, &msg_event, sizeof msg_event);
touch_event.type = SDL_MOUSEBUTTONUP;
touch_event.button.which = 0;
@ -197,6 +200,7 @@ void message_checkInput()
break;
case SDL_FINGERMOTION:
gotEvent = 1;
memcpy(&touch_event, &msg_event, sizeof msg_event);
touch_event.type = SDL_MOUSEMOTION;
touch_event.motion.which = 0;
@ -205,6 +209,13 @@ void message_checkInput()
touch_event.motion.y = msg_graphics->getTarget()->h * msg_event.tfinger.y;
gui_input->pushInput(touch_event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
gotEvent = 1;
break;
default:
break;

View file

@ -111,9 +111,9 @@ static void ShowMessageWaitInputLoop()
SDL_Event event;
while (SDL_PollEvent(&event))
{
gotEvent = 1;
if (event.type == SDL_KEYDOWN)
{
gotEvent = 1;
switch (event.key.keysym.sym)
{
case VK_ESCAPE:
@ -129,6 +129,7 @@ static void ShowMessageWaitInputLoop()
if (event.type == SDL_CONTROLLERBUTTONDOWN)
{
gotEvent = 1;
dialogControlPressed = SDL_GameControllerGetStringForButton(
SDL_GameControllerButton(event.cbutton.button));
dialogFinished = true;
@ -179,10 +180,10 @@ static void ShowMessageLoop()
SDL_Event touch_event;
while (SDL_PollEvent(&event))
{
gotEvent = 1;
switch (event.type)
{
case SDL_KEYDOWN:
gotEvent = 1;
switch (event.key.keysym.sym)
{
case VK_ESCAPE:
@ -210,6 +211,7 @@ static void ShowMessageLoop()
case SDL_JOYHATMOTION:
if (gui_joystick)
{
gotEvent = 1;
const int hat = SDL_JoystickGetHat(gui_joystick, 0);
if (SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].south_button))
@ -236,6 +238,7 @@ static void ShowMessageLoop()
break;
case SDL_FINGERDOWN:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONDOWN;
touch_event.button.which = 0;
@ -247,6 +250,7 @@ static void ShowMessageLoop()
break;
case SDL_FINGERUP:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEBUTTONUP;
touch_event.button.which = 0;
@ -258,6 +262,7 @@ static void ShowMessageLoop()
break;
case SDL_FINGERMOTION:
gotEvent = 1;
memcpy(&touch_event, &event, sizeof event);
touch_event.type = SDL_MOUSEMOTION;
touch_event.motion.which = 0;
@ -267,6 +272,13 @@ static void ShowMessageLoop()
gui_input->pushInput(touch_event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
gotEvent = 1;
break;
default:
break;
}

View file

@ -539,13 +539,13 @@ void checkInput()
int gotEvent = 0;
while (SDL_PollEvent(&gui_event))
{
gotEvent = 1;
switch (gui_event.type)
{
case SDL_QUIT:
//-------------------------------------------------
// Quit entire program via SQL-Quit
//-------------------------------------------------
gotEvent = 1;
uae_quit();
gui_running = false;
break;
@ -554,6 +554,7 @@ void checkInput()
case SDL_JOYBUTTONDOWN:
if (gui_joystick)
{
gotEvent = 1;
const int hat = SDL_JoystickGetHat(gui_joystick, 0);
if (SDL_JoystickGetButton(gui_joystick, host_input_buttons[0].dpad_up) || hat & SDL_HAT_UP) // dpad
@ -630,6 +631,7 @@ void checkInput()
case SDL_JOYAXISMOTION:
if (gui_joystick)
{
gotEvent = 1;
// Deadzone
if (std::abs(gui_event.jaxis.value) >= 10000 || std::abs(gui_event.jaxis.value) <= 5000)
{
@ -681,6 +683,7 @@ void checkInput()
break;
case SDL_KEYDOWN:
gotEvent = 1;
if (gui_event.key.keysym.sym == key_for_gui)
{
if (emulating && cmdStart->isEnabled())
@ -763,7 +766,8 @@ void checkInput()
}
break;
case SDL_FINGERDOWN:
case SDL_FINGERDOWN:
gotEvent = 1;
memcpy(&touch_event, &gui_event, sizeof gui_event);
touch_event.type = SDL_MOUSEBUTTONDOWN;
touch_event.button.which = 0;
@ -774,7 +778,8 @@ void checkInput()
gui_input->pushInput(touch_event);
break;
case SDL_FINGERUP:
case SDL_FINGERUP:
gotEvent = 1;
memcpy(&touch_event, &gui_event, sizeof gui_event);
touch_event.type = SDL_MOUSEBUTTONUP;
touch_event.button.which = 0;
@ -785,7 +790,8 @@ void checkInput()
gui_input->pushInput(touch_event);
break;
case SDL_FINGERMOTION:
case SDL_FINGERMOTION:
gotEvent = 1;
memcpy(&touch_event, &gui_event, sizeof gui_event);
touch_event.type = SDL_MOUSEMOTION;
touch_event.motion.which = 0;
@ -795,6 +801,14 @@ void checkInput()
gui_input->pushInput(touch_event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
case SDL_MOUSEWHEEL:
gotEvent = 1;
break;
default:
break;
}
@ -847,7 +861,11 @@ void amiberry_gui_run()
// Prepare the screen once
uae_gui->logic();
uae_gui->draw();
#ifdef USE_DISPMANX
#else
SDL_UpdateTexture(gui_texture, nullptr, gui_screen->pixels, gui_screen->pitch);
#endif
//-------------------------------------------------
// The main loop
//-------------------------------------------------