From 4e097e05d2eb4bde2844f4eb026a66946a08fe1e Mon Sep 17 00:00:00 2001 From: Alexandre LittleWhite Laurent Date: Tue, 16 Aug 2016 19:00:28 +0200 Subject: [PATCH] Implement pause on focus lost for SDL frontend --- src/citra/citra.cpp | 4 ++++ src/citra/emu_window/emu_window_sdl2.cpp | 28 ++++++++++++++++++++++++ src/citra/emu_window/emu_window_sdl2.h | 9 ++++++++ 3 files changed, 41 insertions(+) diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp index 128b9a16d..cb49373ca 100644 --- a/src/citra/citra.cpp +++ b/src/citra/citra.cpp @@ -150,6 +150,10 @@ int main(int argc, char **argv) { while (emu_window->IsOpen()) { Core::RunLoop(); + + // Pause on focus lost + if (!emu_window->HasFocus()) + emu_window->WaitForFocus(); } return 0; diff --git a/src/citra/emu_window/emu_window_sdl2.cpp b/src/citra/emu_window/emu_window_sdl2.cpp index 591f68aa4..9e24bc457 100644 --- a/src/citra/emu_window/emu_window_sdl2.cpp +++ b/src/citra/emu_window/emu_window_sdl2.cpp @@ -50,6 +50,10 @@ bool EmuWindow_SDL2::IsOpen() const { return is_open; } +bool EmuWindow_SDL2::HasFocus() const { + return has_focus; +} + void EmuWindow_SDL2::OnResize() { int width, height; @@ -58,6 +62,24 @@ void EmuWindow_SDL2::OnResize() { NotifyFramebufferLayoutChanged(EmuWindow::FramebufferLayout::DefaultScreenLayout(width, height)); } +void EmuWindow_SDL2::WaitForFocus() { + while(!has_focus) { + SDL_Event event; + SDL_WaitEvent(&event); + if(event.type == SDL_WINDOWEVENT) { + switch (event.window.event) { + case SDL_WINDOWEVENT_FOCUS_GAINED: + has_focus=true; + break; + case SDL_WINDOWEVENT_CLOSE: + has_focus=true; + is_open = false; + break; + } + } + } +} + EmuWindow_SDL2::EmuWindow_SDL2() { keyboard_id = KeyMap::NewDeviceId(); @@ -136,6 +158,12 @@ void EmuWindow_SDL2::PollEvents() { case SDL_WINDOWEVENT_MINIMIZED: OnResize(); break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + has_focus = true; + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + has_focus = false; + break; case SDL_WINDOWEVENT_CLOSE: is_open = false; break; diff --git a/src/citra/emu_window/emu_window_sdl2.h b/src/citra/emu_window/emu_window_sdl2.h index 77279f022..854db17ec 100644 --- a/src/citra/emu_window/emu_window_sdl2.h +++ b/src/citra/emu_window/emu_window_sdl2.h @@ -30,6 +30,12 @@ public: /// Whether the window is still open, and a close request hasn't yet been sent bool IsOpen() const; + /// Whether the window has focus + bool HasFocus() const; + + /// Wait for the window to gain focus + void WaitForFocus(); + /// Load keymap from configuration void ReloadSetKeymaps() override; @@ -52,6 +58,9 @@ private: /// Is the window still open? bool is_open = true; + /// Is the window has focus + bool has_focus = true; + /// Internal SDL2 render window SDL_Window* render_window;