From bbc143718835f62878b408700380b7f56741f259 Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sat, 6 Jul 2019 13:08:33 -0400
Subject: [PATCH 1/6] core: Track system exit lock status Used to determine if
 yuzu should confirm before pausing or stopping a game.

---
 src/core/core.cpp | 11 +++++++++++
 src/core/core.h   |  4 ++++
 2 files changed, 15 insertions(+)

diff --git a/src/core/core.cpp b/src/core/core.cpp
index f22244cf7..c1bc92782 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -163,6 +163,7 @@ struct System::Impl {
         gpu_core = VideoCore::CreateGPU(system);
 
         is_powered_on = true;
+        exit_lock = false;
 
         LOG_DEBUG(Core, "Initialized OK");
 
@@ -244,6 +245,7 @@ struct System::Impl {
                                     perf_stats->GetMeanFrametime());
 
         is_powered_on = false;
+        exit_lock = false;
 
         // Shutdown emulation session
         renderer.reset();
@@ -328,6 +330,7 @@ struct System::Impl {
     std::unique_ptr<Core::Hardware::InterruptManager> interrupt_manager;
     CpuCoreManager cpu_core_manager;
     bool is_powered_on = false;
+    bool exit_lock = false;
 
     std::unique_ptr<FileSys::CheatEngine> cheat_engine;
     std::unique_ptr<Tools::Freezer> memory_freezer;
@@ -624,6 +627,14 @@ const Service::APM::Controller& System::GetAPMController() const {
     return impl->apm_controller;
 }
 
+void System::SetExitLock(bool locked) {
+    impl->exit_lock = locked;
+}
+
+bool System::GetExitLock() const {
+    return impl->exit_lock;
+}
+
 System::ResultStatus System::Init(Frontend::EmuWindow& emu_window) {
     return impl->Init(*this, emu_window);
 }
diff --git a/src/core/core.h b/src/core/core.h
index bb2962fdd..9874ee487 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -322,6 +322,10 @@ public:
 
     const Service::APM::Controller& GetAPMController() const;
 
+    void SetExitLock(bool locked);
+
+    bool GetExitLock() const;
+
 private:
     System();
 

From 4c1c8801a51335aa4a74e7db0868b861803d0c61 Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sat, 6 Jul 2019 13:09:27 -0400
Subject: [PATCH 2/6] am: Add RequestExit event to AppletMessageQueue Tested
 against libnx, signals to games to begin cleanup.

---
 src/core/hle/service/am/am.cpp | 4 ++++
 src/core/hle/service/am/am.h   | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index 6c594dcaf..c98fefdeb 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -550,6 +550,10 @@ void AppletMessageQueue::OperationModeChanged() {
     on_operation_mode_changed.writable->Signal();
 }
 
+void AppletMessageQueue::RequestExit() {
+    PushMessage(AppletMessage::ExitRequested);
+}
+
 ICommonStateGetter::ICommonStateGetter(Core::System& system,
                                        std::shared_ptr<AppletMessageQueue> msg_queue)
     : ServiceFramework("ICommonStateGetter"), system(system), msg_queue(std::move(msg_queue)) {
diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h
index 28f870302..9d2c8b2ca 100644
--- a/src/core/hle/service/am/am.h
+++ b/src/core/hle/service/am/am.h
@@ -45,6 +45,7 @@ class AppletMessageQueue {
 public:
     enum class AppletMessage : u32 {
         NoMessage = 0,
+        ExitRequested = 4,
         FocusStateChanged = 15,
         OperationModeChanged = 30,
         PerformanceModeChanged = 31,
@@ -59,6 +60,7 @@ public:
     AppletMessage PopMessage();
     std::size_t GetMessageCount() const;
     void OperationModeChanged();
+    void RequestExit();
 
 private:
     std::queue<AppletMessage> messages;

From a7fda849023664212f152adbb0ceed17b246acb0 Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sat, 6 Jul 2019 13:41:38 -0400
Subject: [PATCH 3/6] am: Implement ISelfController Exit Closes the current
 application.

---
 src/core/hle/service/am/am.cpp      | 17 +++++++++++++----
 src/core/hle/service/am/am.h        |  3 +++
 src/core/hle/service/am/applet_ae.h |  2 ++
 src/core/hle/service/am/applet_oe.h |  2 ++
 4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index c98fefdeb..7d8649642 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -232,12 +232,12 @@ IDebugFunctions::IDebugFunctions() : ServiceFramework{"IDebugFunctions"} {
 
 IDebugFunctions::~IDebugFunctions() = default;
 
-ISelfController::ISelfController(Core::System& system_,
-                                 std::shared_ptr<NVFlinger::NVFlinger> nvflinger_)
-    : ServiceFramework("ISelfController"), nvflinger(std::move(nvflinger_)) {
+ISelfController::ISelfController(Core::System& system,
+                                 std::shared_ptr<NVFlinger::NVFlinger> nvflinger)
+    : ServiceFramework("ISelfController"), system(system), nvflinger(std::move(nvflinger)) {
     // clang-format off
     static const FunctionInfo functions[] = {
-        {0, nullptr, "Exit"},
+        {0, &ISelfController::Exit, "Exit"},
         {1, &ISelfController::LockExit, "LockExit"},
         {2, &ISelfController::UnlockExit, "UnlockExit"},
         {3, &ISelfController::EnterFatalSection, "EnterFatalSection"},
@@ -298,6 +298,15 @@ ISelfController::ISelfController(Core::System& system_,
 
 ISelfController::~ISelfController() = default;
 
+void ISelfController::Exit(Kernel::HLERequestContext& ctx) {
+    LOG_DEBUG(Service_AM, "called");
+
+    system.Shutdown();
+
+    IPC::ResponseBuilder rb{ctx, 2};
+    rb.Push(RESULT_SUCCESS);
+}
+
 void ISelfController::LockExit(Kernel::HLERequestContext& ctx) {
     LOG_WARNING(Service_AM, "(STUBBED) called");
 
diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h
index 9d2c8b2ca..a3baeb673 100644
--- a/src/core/hle/service/am/am.h
+++ b/src/core/hle/service/am/am.h
@@ -125,6 +125,7 @@ public:
     ~ISelfController() override;
 
 private:
+    void Exit(Kernel::HLERequestContext& ctx);
     void LockExit(Kernel::HLERequestContext& ctx);
     void UnlockExit(Kernel::HLERequestContext& ctx);
     void EnterFatalSection(Kernel::HLERequestContext& ctx);
@@ -153,6 +154,8 @@ private:
     u32 idle_time_detection_extension = 0;
     u64 num_fatal_sections_entered = 0;
     bool is_auto_sleep_disabled = false;
+
+    Core::System& system;
 };
 
 class ICommonStateGetter final : public ServiceFramework<ICommonStateGetter> {
diff --git a/src/core/hle/service/am/applet_ae.h b/src/core/hle/service/am/applet_ae.h
index 0e0d10858..2e3e45915 100644
--- a/src/core/hle/service/am/applet_ae.h
+++ b/src/core/hle/service/am/applet_ae.h
@@ -19,6 +19,8 @@ class NVFlinger;
 
 namespace AM {
 
+class AppletMessageQueue;
+
 class AppletAE final : public ServiceFramework<AppletAE> {
 public:
     explicit AppletAE(std::shared_ptr<NVFlinger::NVFlinger> nvflinger,
diff --git a/src/core/hle/service/am/applet_oe.h b/src/core/hle/service/am/applet_oe.h
index 99a65e7b5..758da792d 100644
--- a/src/core/hle/service/am/applet_oe.h
+++ b/src/core/hle/service/am/applet_oe.h
@@ -19,6 +19,8 @@ class NVFlinger;
 
 namespace AM {
 
+class AppletMessageQueue;
+
 class AppletOE final : public ServiceFramework<AppletOE> {
 public:
     explicit AppletOE(std::shared_ptr<NVFlinger::NVFlinger> nvflinger,

From e58e3719d89bd8ce2c919ab154ec93a657807b3a Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sat, 6 Jul 2019 13:42:06 -0400
Subject: [PATCH 4/6] am: Implement ISelfController ExitLock commands

---
 src/core/hle/service/am/am.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index 7d8649642..a64e9c430 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -308,14 +308,18 @@ void ISelfController::Exit(Kernel::HLERequestContext& ctx) {
 }
 
 void ISelfController::LockExit(Kernel::HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
+    LOG_DEBUG(Service_AM, "called");
+
+    system.SetExitLock(true);
 
     IPC::ResponseBuilder rb{ctx, 2};
     rb.Push(RESULT_SUCCESS);
 }
 
 void ISelfController::UnlockExit(Kernel::HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
+    LOG_DEBUG(Service_AM, "called");
+
+    system.SetExitLock(false);
 
     IPC::ResponseBuilder rb{ctx, 2};
     rb.Push(RESULT_SUCCESS);

From 60c2e9e675ce3618d1eedd9a73479c48eb9ba1dc Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sat, 21 Sep 2019 22:46:53 -0400
Subject: [PATCH 5/6] qt: Prompt user for confirmation if exit lock is active

---
 src/core/hle/service/am/am.cpp |  2 +-
 src/yuzu/main.cpp              | 41 ++++++++++++++++++++++++++++++++++
 src/yuzu/main.h                |  2 ++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp
index a64e9c430..c4ddc7c69 100644
--- a/src/core/hle/service/am/am.cpp
+++ b/src/core/hle/service/am/am.cpp
@@ -282,7 +282,7 @@ ISelfController::ISelfController(Core::System& system,
 
     RegisterHandlers(functions);
 
-    auto& kernel = system_.Kernel();
+    auto& kernel = system.Kernel();
     launchable_event = Kernel::WritableEvent::CreateEventPair(kernel, Kernel::ResetType::Manual,
                                                               "ISelfController:LaunchableEvent");
 
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index f147044d9..328f599f1 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -22,6 +22,8 @@
 #include "core/frontend/applets/general_frontend.h"
 #include "core/frontend/scope_acquire_window_context.h"
 #include "core/hle/service/acc/profile_manager.h"
+#include "core/hle/service/am/applet_ae.h"
+#include "core/hle/service/am/applet_oe.h"
 #include "core/hle/service/am/applets/applets.h"
 #include "core/hle/service/hid/controllers/npad.h"
 #include "core/hle/service/hid/hid.h"
@@ -83,6 +85,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual
 #include "core/file_sys/submission_package.h"
 #include "core/frontend/applets/software_keyboard.h"
 #include "core/hle/kernel/process.h"
+#include "core/hle/service/am/am.h"
 #include "core/hle/service/filesystem/filesystem.h"
 #include "core/hle/service/nfp/nfp.h"
 #include "core/hle/service/sm/sm.h"
@@ -1674,6 +1677,11 @@ void GMainWindow::OnStartGame() {
 }
 
 void GMainWindow::OnPauseGame() {
+    Core::System& system{Core::System::GetInstance()};
+    if (system.GetExitLock() && !ConfirmForceLockedExit()) {
+        return;
+    }
+
     emu_thread->SetRunning(false);
 
     ui.action_Start->setEnabled(true);
@@ -1685,6 +1693,11 @@ void GMainWindow::OnPauseGame() {
 }
 
 void GMainWindow::OnStopGame() {
+    Core::System& system{Core::System::GetInstance()};
+    if (system.GetExitLock() && !ConfirmForceLockedExit()) {
+        return;
+    }
+
     ShutdownGame();
 }
 
@@ -2189,6 +2202,34 @@ bool GMainWindow::ConfirmChangeGame() {
     return answer != QMessageBox::No;
 }
 
+bool GMainWindow::ConfirmForceLockedExit() {
+    if (emu_thread == nullptr)
+        return true;
+
+    auto answer =
+        QMessageBox::question(this, tr("yuzu"),
+                              tr("The currently running application has requested yuzu to not "
+                                 "exit.\n\nWould you like to bypass this and exit anyway?"),
+                              QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
+    return answer != QMessageBox::No;
+}
+
+void GMainWindow::RequestGameExit() {
+    auto& sm{Core::System::GetInstance().ServiceManager()};
+    auto applet_oe = sm.GetService<Service::AM::AppletOE>("appletOE");
+    auto applet_ae = sm.GetService<Service::AM::AppletAE>("appletAE");
+    bool has_signalled = false;
+
+    if (applet_oe != nullptr) {
+        applet_oe->GetMessageQueue()->RequestExit();
+        has_signalled = true;
+    }
+
+    if (applet_ae != nullptr && !has_signalled) {
+        applet_ae->GetMessageQueue()->RequestExit();
+    }
+}
+
 void GMainWindow::filterBarSetChecked(bool state) {
     ui.action_Show_Filter_Bar->setChecked(state);
     emit(OnToggleFilterBar());
diff --git a/src/yuzu/main.h b/src/yuzu/main.h
index 7d16188cb..e942d1248 100644
--- a/src/yuzu/main.h
+++ b/src/yuzu/main.h
@@ -172,6 +172,8 @@ private:
      */
     bool ConfirmClose();
     bool ConfirmChangeGame();
+    bool ConfirmForceLockedExit();
+    void RequestGameExit();
     void closeEvent(QCloseEvent* event) override;
 
 private slots:

From 9f3bf6d157826fff311950972e91ec86b98598ae Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sun, 22 Sep 2019 10:02:07 -0400
Subject: [PATCH 6/6] main: Use const on all variable initializations

---
 src/yuzu/main.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index 328f599f1..2d82df739 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -2195,7 +2195,7 @@ bool GMainWindow::ConfirmChangeGame() {
     if (emu_thread == nullptr)
         return true;
 
-    auto answer = QMessageBox::question(
+    const auto answer = QMessageBox::question(
         this, tr("yuzu"),
         tr("Are you sure you want to stop the emulation? Any unsaved progress will be lost."),
         QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
@@ -2206,7 +2206,7 @@ bool GMainWindow::ConfirmForceLockedExit() {
     if (emu_thread == nullptr)
         return true;
 
-    auto answer =
+    const auto answer =
         QMessageBox::question(this, tr("yuzu"),
                               tr("The currently running application has requested yuzu to not "
                                  "exit.\n\nWould you like to bypass this and exit anyway?"),