From c66de56cce9143ef0d671ada68d12a74e5d12db4 Mon Sep 17 00:00:00 2001
From: Exverge <exverge@exverge.xyz>
Date: Sun, 24 Mar 2024 19:51:18 -0400
Subject: [PATCH] fix: retrack application when guest applet is closed

---
 src/core/hle/service/am/applet_manager.cpp     |  9 +++++++++
 src/core/hle/service/am/applet_manager.h       |  5 +++++
 .../am/service/library_applet_accessor.cpp     | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+)

diff --git a/src/core/hle/service/am/applet_manager.cpp b/src/core/hle/service/am/applet_manager.cpp
index c6b7ec8bb5..211c5d9ca8 100644
--- a/src/core/hle/service/am/applet_manager.cpp
+++ b/src/core/hle/service/am/applet_manager.cpp
@@ -1,4 +1,5 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 #include "common/settings.h"
@@ -335,4 +336,12 @@ void AppletManager::SetWindowSystem(WindowSystem* window_system) {
     applet->process->Run();
 }
 
+void AppletManager::RequestApplicationToForeground() {
+    m_window_system->RequestApplicationToGetForeground();
+}
+
+void AppletManager::TrackApplet(std::shared_ptr<Applet> applet, bool is_application) {
+    m_window_system->TrackApplet(applet, is_application);
+}
+
 } // namespace Service::AM
diff --git a/src/core/hle/service/am/applet_manager.h b/src/core/hle/service/am/applet_manager.h
index fbdc771400..f88a695822 100644
--- a/src/core/hle/service/am/applet_manager.h
+++ b/src/core/hle/service/am/applet_manager.h
@@ -1,4 +1,5 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 #pragma once
@@ -44,8 +45,11 @@ public:
     void RequestExit();
     void OperationModeChanged();
 
+    void TrackApplet(std::shared_ptr<Applet> applet, bool is_application);
+
 public:
     void SetWindowSystem(WindowSystem* window_system);
+    void RequestApplicationToForeground();
 
 private:
     Core::System& m_system;
@@ -57,6 +61,7 @@ private:
 
     FrontendAppletParameters m_pending_parameters{};
     std::unique_ptr<Process> m_pending_process{};
+
 };
 
 } // namespace Service::AM
diff --git a/src/core/hle/service/am/service/library_applet_accessor.cpp b/src/core/hle/service/am/service/library_applet_accessor.cpp
index cda8c3eb87..26d716fc8c 100644
--- a/src/core/hle/service/am/service/library_applet_accessor.cpp
+++ b/src/core/hle/service/am/service/library_applet_accessor.cpp
@@ -1,4 +1,5 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 #include "core/hle/service/am/applet_data_broker.h"
@@ -101,6 +102,23 @@ Result ILibraryAppletAccessor::PushInData(SharedPointer<IStorage> storage) {
 
 Result ILibraryAppletAccessor::PopOutData(Out<SharedPointer<IStorage>> out_storage) {
     LOG_DEBUG(Service_AM, "called");
+    if(auto caller = m_applet->caller_applet.lock(); caller != nullptr) {
+        LOG_DEBUG(Service_AM, "resuming caller");
+        LOG_DEBUG(Service_AM, "{}", caller->applet_id);
+        LOG_DEBUG(Service_AM, "{}", caller->program_id);
+        system.GetAppletManager().RequestApplicationToForeground();
+        // todo: is_appliaction is false because TrackApplet will not accept replacing the currently tracked applet
+        system.GetAppletManager().TrackApplet(caller, false);
+        caller->lifecycle_manager.SetResumeNotificationEnabled(true);
+        caller->lifecycle_manager.SetFocusState(FocusState::InFocus);
+        caller->lifecycle_manager.RequestResumeNotification();
+        caller->lifecycle_manager.RequestResumeNotification();
+        bool result = caller->lifecycle_manager.UpdateRequestedFocusState();
+        LOG_DEBUG(Service_AM, "result: {}, exit: {}", result, caller->lifecycle_manager.GetExitRequested());
+    } else {
+        LOG_CRITICAL(Service_AM, "Caller applet pointer is invalid.");
+        LOG_CRITICAL(Service_AM, "The emulator will freeze!");
+    }
     R_RETURN(m_broker->GetOutData().Pop(out_storage.Get()));
 }