From c634c263db5b1f7e5cffb7d1beb3ba07960b621b Mon Sep 17 00:00:00 2001
From: Ian Chamberlain <ian.h.chamberlain@gmail.com>
Date: Sat, 20 Aug 2022 20:49:59 -0400
Subject: [PATCH] citra-sdl: use the result of system.RunLoop()

If a shutdown was requested by the application, close the SDL window,
initiating the normal shutdown procedure. This causes a graceful exit
process instead of hanging.
---
 src/citra/citra.cpp                      | 14 +++++++++++++-
 src/citra/emu_window/emu_window_sdl2.cpp |  6 +++++-
 src/citra/emu_window/emu_window_sdl2.h   |  3 +++
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index a9b0b3eca..9e46fa9d9 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -391,6 +391,8 @@ int main(int argc, char** argv) {
         return -1;
     case Core::System::ResultStatus::Success:
         break; // Expected case
+    default:
+        LOG_ERROR(Frontend, "Error while loading ROM: {}", system.GetStatusDetails());
     }
 
     system.TelemetrySession().AddField(Common::Telemetry::FieldType::App, "Frontend", "SDL");
@@ -437,7 +439,17 @@ int main(int argc, char** argv) {
         });
 
     while (emu_window->IsOpen()) {
-        system.RunLoop();
+        const auto result = system.RunLoop();
+
+        switch (result) {
+        case Core::System::ResultStatus::ShutdownRequested:
+            emu_window->Close();
+            break;
+        case Core::System::ResultStatus::Success:
+            break;
+        default:
+            LOG_ERROR(Frontend, "Error in main run loop: {}", result, system.GetStatusDetails());
+        }
     }
     render_thread.join();
 
diff --git a/src/citra/emu_window/emu_window_sdl2.cpp b/src/citra/emu_window/emu_window_sdl2.cpp
index e5a0594c4..30206486b 100644
--- a/src/citra/emu_window/emu_window_sdl2.cpp
+++ b/src/citra/emu_window/emu_window_sdl2.cpp
@@ -104,6 +104,10 @@ bool EmuWindow_SDL2::IsOpen() const {
     return is_open;
 }
 
+void EmuWindow_SDL2::Close() {
+    is_open = false;
+}
+
 void EmuWindow_SDL2::OnResize() {
     int width, height;
     SDL_GetWindowSize(render_window, &width, &height);
@@ -134,7 +138,7 @@ void EmuWindow_SDL2::Fullscreen() {
 EmuWindow_SDL2::EmuWindow_SDL2(bool fullscreen) {
     // Initialize the window
     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0) {
-        LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
+        LOG_CRITICAL(Frontend, "Failed to initialize SDL2: {}! Exiting...", SDL_GetError());
         exit(1);
     }
 
diff --git a/src/citra/emu_window/emu_window_sdl2.h b/src/citra/emu_window/emu_window_sdl2.h
index 64dec5381..831837ea6 100644
--- a/src/citra/emu_window/emu_window_sdl2.h
+++ b/src/citra/emu_window/emu_window_sdl2.h
@@ -46,6 +46,9 @@ public:
     /// Whether the window is still open, and a close request hasn't yet been sent
     bool IsOpen() const;
 
+    /// Close the window.
+    void Close();
+
     /// Creates a new context that is shared with the current context
     std::unique_ptr<GraphicsContext> CreateSharedContext() const override;