From 7477e211fee2448f256cb20aa4ce85f3491a0367 Mon Sep 17 00:00:00 2001
From: darktux
 <darktux@y2nlvhmmk5jnsvechppxnbyzmmv3vbl7dvzn6ltwcdbpgxixp3clkgqd.onion>
Date: Fri, 5 Apr 2024 01:58:29 +0200
Subject: [PATCH] Implement option to start QLaunch from Tools menu

---
 .../am/service/global_state_controller.cpp    | 16 +++++++++++--
 .../am/service/global_state_controller.h      |  2 ++
 src/yuzu/main.cpp                             | 24 +++++++++++++++++++
 src/yuzu/main.h                               |  1 +
 src/yuzu/main.ui                              |  6 +++++
 5 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/core/hle/service/am/service/global_state_controller.cpp b/src/core/hle/service/am/service/global_state_controller.cpp
index dba5d36130..3e3b61b0a0 100644
--- a/src/core/hle/service/am/service/global_state_controller.cpp
+++ b/src/core/hle/service/am/service/global_state_controller.cpp
@@ -15,8 +15,8 @@ IGlobalStateController::IGlobalStateController(Core::System& system_)
         {0, nullptr, "RequestToEnterSleep"},
         {1, nullptr, "EnterSleep"},
         {2, nullptr, "StartSleepSequence"},
-        {3, nullptr, "StartShutdownSequence"},
-        {4, nullptr, "StartRebootSequence"},
+        {3, D<&IGlobalStateController::StartShutdownSequence>, "StartShutdownSequence"},
+        {4, D<&IGlobalStateController::StartRebootSequence>, "StartRebootSequence"},
         {9, nullptr, "IsAutoPowerDownRequested"},
         {10, D<&IGlobalStateController::LoadAndApplyIdlePolicySettings>, "LoadAndApplyIdlePolicySettings"},
         {11, nullptr, "NotifyCecSettingsChanged"},
@@ -31,6 +31,18 @@ IGlobalStateController::IGlobalStateController(Core::System& system_)
     RegisterHandlers(functions);
 }
 
+Result IGlobalStateController::StartShutdownSequence() {
+    LOG_INFO(Service_AM, "called");
+    system.Exit();
+    R_SUCCEED();
+}
+
+Result IGlobalStateController::StartRebootSequence() {
+    LOG_INFO(Service_AM, "called");
+    system.Exit();
+    R_SUCCEED();
+}
+
 IGlobalStateController::~IGlobalStateController() = default;
 
 Result IGlobalStateController::LoadAndApplyIdlePolicySettings() {
diff --git a/src/core/hle/service/am/service/global_state_controller.h b/src/core/hle/service/am/service/global_state_controller.h
index 67c753513d..83efb57dfc 100644
--- a/src/core/hle/service/am/service/global_state_controller.h
+++ b/src/core/hle/service/am/service/global_state_controller.h
@@ -18,6 +18,8 @@ public:
     ~IGlobalStateController() override;
 
 private:
+    Result StartShutdownSequence();
+    Result StartRebootSequence();
     Result LoadAndApplyIdlePolicySettings();
     Result ShouldSleepOnBoot(Out<bool> out_should_sleep_on_boot);
     Result GetHdcpAuthenticationFailedEvent(OutCopyHandle<Kernel::KReadableEvent> out_event);
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index 56d8803bb8..7e1ec122a7 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -1591,6 +1591,7 @@ void GMainWindow::ConnectMenuEvents() {
                  [this]() { OnCabinet(Service::NFP::CabinetMode::StartFormatter); });
     connect_menu(ui->action_Load_Mii_Edit, &GMainWindow::OnMiiEdit);
     connect_menu(ui->action_Open_Controller_Menu, &GMainWindow::OnOpenControllerMenu);
+    connect_menu(ui->action_Load_Home_Menu, &GMainWindow::OnHomeMenu);
     connect_menu(ui->action_Capture_Screenshot, &GMainWindow::OnCaptureScreenshot);
 
     // TAS
@@ -4480,6 +4481,29 @@ void GMainWindow::OnOpenControllerMenu() {
              LibraryAppletParameters(ControllerAppletId, Service::AM::AppletId::Controller));
 }
 
+void GMainWindow::OnHomeMenu() {
+    constexpr u64 QLaunchId = static_cast<u64>(Service::AM::AppletProgramId::QLaunch);
+    auto bis_system = system->GetFileSystemController().GetSystemNANDContents();
+    if (!bis_system) {
+        QMessageBox::warning(this, tr("No firmware available"),
+                             tr("Please install the firmware to use the Home Menu."));
+        return;
+    }
+
+    auto qlaunch_applet_nca = bis_system->GetEntry(QLaunchId, FileSys::ContentRecordType::Program);
+    if (!qlaunch_applet_nca) {
+        QMessageBox::warning(this, tr("Home Menu Applet"),
+                             tr("Home Menu is not available. Please reinstall firmware."));
+        return;
+    }
+
+    system->GetFrontendAppletHolder().SetCurrentAppletId(Service::AM::AppletId::QLaunch);
+
+    const auto filename = QString::fromStdString((qlaunch_applet_nca->GetFullPath()));
+    UISettings::values.roms_path = QFileInfo(filename).path().toStdString();
+    BootGame(filename, LibraryAppletParameters(QLaunchId, Service::AM::AppletId::QLaunch));
+}
+
 void GMainWindow::OnCaptureScreenshot() {
     if (emu_thread == nullptr || !emu_thread->IsRunning()) {
         return;
diff --git a/src/yuzu/main.h b/src/yuzu/main.h
index cb68f5a751..aaade93a82 100644
--- a/src/yuzu/main.h
+++ b/src/yuzu/main.h
@@ -399,6 +399,7 @@ private slots:
     void OnCabinet(Service::NFP::CabinetMode mode);
     void OnMiiEdit();
     void OnOpenControllerMenu();
+    void OnHomeMenu();
     void OnCaptureScreenshot();
     void OnCheckFirmwareDecryption();
     void OnLanguageChanged(const QString& locale);
diff --git a/src/yuzu/main.ui b/src/yuzu/main.ui
index 3d02a09127..754ef650af 100644
--- a/src/yuzu/main.ui
+++ b/src/yuzu/main.ui
@@ -173,6 +173,7 @@
     <addaction name="action_Load_Album"/>
     <addaction name="action_Load_Mii_Edit"/>
     <addaction name="action_Open_Controller_Menu"/>
+    <addaction name="action_Load_Home_Menu"/>
     <addaction name="separator"/>
     <addaction name="action_Capture_Screenshot"/>
     <addaction name="menuTAS"/>
@@ -475,6 +476,11 @@
     <string>Install Decryption Keys</string>
    </property>
   </action>
+  <action name="action_Load_Home_Menu">
+   <property name="text">
+    <string>Open Home Menu</string>
+   </property>
+  </action>
  </widget>
  <resources>
   <include location="yuzu.qrc"/>