From c7e97b22fb6fd14800ad374a13ad21d6a4c829f1 Mon Sep 17 00:00:00 2001
From: Liam <byteslice@airmail.cc>
Date: Sun, 11 Feb 2024 18:31:29 -0500
Subject: [PATCH] am: rewrite IApplicationProxy

---
 src/core/CMakeLists.txt                       |   4 +-
 src/core/hle/service/am/application_proxy.cpp | 115 ------------------
 src/core/hle/service/am/application_proxy.h   |  33 -----
 .../service/am/service/application_proxy.cpp  | 106 ++++++++++++++++
 .../service/am/service/application_proxy.h    |  48 ++++++++
 .../am/service/application_proxy_service.cpp  |   5 +-
 6 files changed, 159 insertions(+), 152 deletions(-)
 delete mode 100644 src/core/hle/service/am/application_proxy.cpp
 delete mode 100644 src/core/hle/service/am/application_proxy.h
 create mode 100644 src/core/hle/service/am/service/application_proxy.cpp
 create mode 100644 src/core/hle/service/am/service/application_proxy.h

diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 5ae31932c1..e2486f2cdd 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -429,8 +429,6 @@ add_library(core STATIC
     hle/service/am/application_creator.h
     hle/service/am/application_functions.cpp
     hle/service/am/application_functions.h
-    hle/service/am/application_proxy.cpp
-    hle/service/am/application_proxy.h
     hle/service/am/audio_controller.cpp
     hle/service/am/audio_controller.h
     hle/service/am/common_state_getter.cpp
@@ -473,6 +471,8 @@ add_library(core STATIC
     hle/service/am/service/all_system_applet_proxies_service.h
     hle/service/am/service/application_proxy_service.cpp
     hle/service/am/service/application_proxy_service.h
+    hle/service/am/service/application_proxy.cpp
+    hle/service/am/service/application_proxy.h
     hle/service/am/system_applet_proxy.cpp
     hle/service/am/system_applet_proxy.h
     hle/service/am/system_buffer_manager.cpp
diff --git a/src/core/hle/service/am/application_proxy.cpp b/src/core/hle/service/am/application_proxy.cpp
deleted file mode 100644
index a6fd6d37f9..0000000000
--- a/src/core/hle/service/am/application_proxy.cpp
+++ /dev/null
@@ -1,115 +0,0 @@
-// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-#include "core/hle/service/am/applet_common_functions.h"
-#include "core/hle/service/am/application_functions.h"
-#include "core/hle/service/am/application_proxy.h"
-#include "core/hle/service/am/audio_controller.h"
-#include "core/hle/service/am/common_state_getter.h"
-#include "core/hle/service/am/debug_functions.h"
-#include "core/hle/service/am/display_controller.h"
-#include "core/hle/service/am/library_applet_creator.h"
-#include "core/hle/service/am/library_applet_self_accessor.h"
-#include "core/hle/service/am/process_winding_controller.h"
-#include "core/hle/service/am/self_controller.h"
-#include "core/hle/service/am/window_controller.h"
-#include "core/hle/service/ipc_helpers.h"
-
-namespace Service::AM {
-
-IApplicationProxy::IApplicationProxy(Nvnflinger::Nvnflinger& nvnflinger_,
-                                     std::shared_ptr<Applet> applet_, Core::System& system_)
-    : ServiceFramework{system_, "IApplicationProxy"}, nvnflinger{nvnflinger_}, applet{std::move(
-                                                                                   applet_)} {
-    // clang-format off
-    static const FunctionInfo functions[] = {
-        {0, &IApplicationProxy::GetCommonStateGetter, "GetCommonStateGetter"},
-        {1, &IApplicationProxy::GetSelfController, "GetSelfController"},
-        {2, &IApplicationProxy::GetWindowController, "GetWindowController"},
-        {3, &IApplicationProxy::GetAudioController, "GetAudioController"},
-        {4, &IApplicationProxy::GetDisplayController, "GetDisplayController"},
-        {10, &IApplicationProxy::GetProcessWindingController, "GetProcessWindingController"},
-        {11, &IApplicationProxy::GetLibraryAppletCreator, "GetLibraryAppletCreator"},
-        {20, &IApplicationProxy::GetApplicationFunctions, "GetApplicationFunctions"},
-        {1000, &IApplicationProxy::GetDebugFunctions, "GetDebugFunctions"},
-    };
-    // clang-format on
-
-    RegisterHandlers(functions);
-}
-
-IApplicationProxy::~IApplicationProxy() = default;
-
-void IApplicationProxy::GetAudioController(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<IAudioController>(system);
-}
-
-void IApplicationProxy::GetDisplayController(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<IDisplayController>(system, applet);
-}
-
-void IApplicationProxy::GetProcessWindingController(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<IProcessWindingController>(system, applet);
-}
-
-void IApplicationProxy::GetDebugFunctions(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<IDebugFunctions>(system);
-}
-
-void IApplicationProxy::GetWindowController(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<IWindowController>(system, applet);
-}
-
-void IApplicationProxy::GetSelfController(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<ISelfController>(system, applet, nvnflinger);
-}
-
-void IApplicationProxy::GetCommonStateGetter(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<ICommonStateGetter>(system, applet);
-}
-
-void IApplicationProxy::GetLibraryAppletCreator(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<ILibraryAppletCreator>(system, applet);
-}
-
-void IApplicationProxy::GetApplicationFunctions(HLERequestContext& ctx) {
-    LOG_DEBUG(Service_AM, "called");
-
-    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
-    rb.Push(ResultSuccess);
-    rb.PushIpcInterface<IApplicationFunctions>(system, applet);
-}
-
-} // namespace Service::AM
diff --git a/src/core/hle/service/am/application_proxy.h b/src/core/hle/service/am/application_proxy.h
deleted file mode 100644
index eb98b095cb..0000000000
--- a/src/core/hle/service/am/application_proxy.h
+++ /dev/null
@@ -1,33 +0,0 @@
-// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-#pragma once
-
-#include "core/hle/service/service.h"
-
-namespace Service::AM {
-
-struct Applet;
-
-class IApplicationProxy final : public ServiceFramework<IApplicationProxy> {
-public:
-    explicit IApplicationProxy(Nvnflinger::Nvnflinger& nvnflinger_,
-                               std::shared_ptr<Applet> msg_queue_, Core::System& system_);
-    ~IApplicationProxy();
-
-private:
-    void GetAudioController(HLERequestContext& ctx);
-    void GetDisplayController(HLERequestContext& ctx);
-    void GetProcessWindingController(HLERequestContext& ctx);
-    void GetDebugFunctions(HLERequestContext& ctx);
-    void GetWindowController(HLERequestContext& ctx);
-    void GetSelfController(HLERequestContext& ctx);
-    void GetCommonStateGetter(HLERequestContext& ctx);
-    void GetLibraryAppletCreator(HLERequestContext& ctx);
-    void GetApplicationFunctions(HLERequestContext& ctx);
-
-    Nvnflinger::Nvnflinger& nvnflinger;
-    std::shared_ptr<Applet> applet;
-};
-
-} // namespace Service::AM
diff --git a/src/core/hle/service/am/service/application_proxy.cpp b/src/core/hle/service/am/service/application_proxy.cpp
new file mode 100644
index 0000000000..d1f87709ea
--- /dev/null
+++ b/src/core/hle/service/am/service/application_proxy.cpp
@@ -0,0 +1,106 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/service/am/applet_common_functions.h"
+#include "core/hle/service/am/application_functions.h"
+#include "core/hle/service/am/audio_controller.h"
+#include "core/hle/service/am/common_state_getter.h"
+#include "core/hle/service/am/debug_functions.h"
+#include "core/hle/service/am/display_controller.h"
+#include "core/hle/service/am/library_applet_creator.h"
+#include "core/hle/service/am/library_applet_self_accessor.h"
+#include "core/hle/service/am/process_winding_controller.h"
+#include "core/hle/service/am/self_controller.h"
+#include "core/hle/service/am/service/application_proxy.h"
+#include "core/hle/service/am/window_controller.h"
+#include "core/hle/service/cmif_serialization.h"
+
+namespace Service::AM {
+
+IApplicationProxy::IApplicationProxy(Core::System& system_, std::shared_ptr<Applet> applet,
+                                     Kernel::KProcess* process, Nvnflinger::Nvnflinger& nvnflinger)
+    : ServiceFramework{system_, "IApplicationProxy"},
+      m_nvnflinger{nvnflinger}, m_process{process}, m_applet{std::move(applet)} {
+    // clang-format off
+    static const FunctionInfo functions[] = {
+        {0, D<&IApplicationProxy::GetCommonStateGetter>, "GetCommonStateGetter"},
+        {1, D<&IApplicationProxy::GetSelfController>, "GetSelfController"},
+        {2, D<&IApplicationProxy::GetWindowController>, "GetWindowController"},
+        {3, D<&IApplicationProxy::GetAudioController>, "GetAudioController"},
+        {4, D<&IApplicationProxy::GetDisplayController>, "GetDisplayController"},
+        {10, D<&IApplicationProxy::GetProcessWindingController>, "GetProcessWindingController"},
+        {11, D<&IApplicationProxy::GetLibraryAppletCreator>, "GetLibraryAppletCreator"},
+        {20, D<&IApplicationProxy::GetApplicationFunctions>, "GetApplicationFunctions"},
+        {1000, D<&IApplicationProxy::GetDebugFunctions>, "GetDebugFunctions"},
+    };
+    // clang-format on
+
+    RegisterHandlers(functions);
+}
+
+IApplicationProxy::~IApplicationProxy() = default;
+
+Result IApplicationProxy::GetAudioController(
+    Out<SharedPointer<IAudioController>> out_audio_controller) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_audio_controller = std::make_shared<IAudioController>(system);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetDisplayController(
+    Out<SharedPointer<IDisplayController>> out_display_controller) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_display_controller = std::make_shared<IDisplayController>(system, m_applet);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetProcessWindingController(
+    Out<SharedPointer<IProcessWindingController>> out_process_winding_controller) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_process_winding_controller = std::make_shared<IProcessWindingController>(system, m_applet);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetDebugFunctions(
+    Out<SharedPointer<IDebugFunctions>> out_debug_functions) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_debug_functions = std::make_shared<IDebugFunctions>(system);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetWindowController(
+    Out<SharedPointer<IWindowController>> out_window_controller) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_window_controller = std::make_shared<IWindowController>(system, m_applet);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetSelfController(
+    Out<SharedPointer<ISelfController>> out_self_controller) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_self_controller = std::make_shared<ISelfController>(system, m_applet, m_nvnflinger);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetCommonStateGetter(
+    Out<SharedPointer<ICommonStateGetter>> out_common_state_getter) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_common_state_getter = std::make_shared<ICommonStateGetter>(system, m_applet);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetLibraryAppletCreator(
+    Out<SharedPointer<ILibraryAppletCreator>> out_library_applet_creator) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_library_applet_creator = std::make_shared<ILibraryAppletCreator>(system, m_applet);
+    R_SUCCEED();
+}
+
+Result IApplicationProxy::GetApplicationFunctions(
+    Out<SharedPointer<IApplicationFunctions>> out_application_functions) {
+    LOG_DEBUG(Service_AM, "called");
+    *out_application_functions = std::make_shared<IApplicationFunctions>(system, m_applet);
+    R_SUCCEED();
+}
+
+} // namespace Service::AM
diff --git a/src/core/hle/service/am/service/application_proxy.h b/src/core/hle/service/am/service/application_proxy.h
new file mode 100644
index 0000000000..1ebc593ba8
--- /dev/null
+++ b/src/core/hle/service/am/service/application_proxy.h
@@ -0,0 +1,48 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/cmif_types.h"
+#include "core/hle/service/service.h"
+
+namespace Service::AM {
+
+struct Applet;
+class IAudioController;
+class IApplicationFunctions;
+class ICommonStateGetter;
+class IDebugFunctions;
+class IDisplayController;
+class ILibraryAppletCreator;
+class IProcessWindingController;
+class ISelfController;
+class IWindowController;
+
+class IApplicationProxy final : public ServiceFramework<IApplicationProxy> {
+public:
+    explicit IApplicationProxy(Core::System& system_, std::shared_ptr<Applet> applet,
+                               Kernel::KProcess* process, Nvnflinger::Nvnflinger& nvnflinger);
+    ~IApplicationProxy();
+
+private:
+    Result GetAudioController(Out<SharedPointer<IAudioController>> out_audio_controller);
+    Result GetDisplayController(Out<SharedPointer<IDisplayController>> out_display_controller);
+    Result GetProcessWindingController(
+        Out<SharedPointer<IProcessWindingController>> out_process_winding_controller);
+    Result GetDebugFunctions(Out<SharedPointer<IDebugFunctions>> out_debug_functions);
+    Result GetWindowController(Out<SharedPointer<IWindowController>> out_window_controller);
+    Result GetSelfController(Out<SharedPointer<ISelfController>> out_self_controller);
+    Result GetCommonStateGetter(Out<SharedPointer<ICommonStateGetter>> out_common_state_getter);
+    Result GetLibraryAppletCreator(
+        Out<SharedPointer<ILibraryAppletCreator>> out_library_applet_creator);
+    Result GetApplicationFunctions(
+        Out<SharedPointer<IApplicationFunctions>> out_application_functions);
+
+private:
+    Nvnflinger::Nvnflinger& m_nvnflinger;
+    Kernel::KProcess* const m_process;
+    const std::shared_ptr<Applet> m_applet;
+};
+
+} // namespace Service::AM
diff --git a/src/core/hle/service/am/service/application_proxy_service.cpp b/src/core/hle/service/am/service/application_proxy_service.cpp
index b8532d0476..36d4478df7 100644
--- a/src/core/hle/service/am/service/application_proxy_service.cpp
+++ b/src/core/hle/service/am/service/application_proxy_service.cpp
@@ -4,7 +4,7 @@
 #include "core/core.h"
 #include "core/hle/service/am/am.h"
 #include "core/hle/service/am/applet_manager.h"
-#include "core/hle/service/am/application_proxy.h"
+#include "core/hle/service/am/service/application_proxy.h"
 #include "core/hle/service/am/service/application_proxy_service.h"
 #include "core/hle/service/cmif_serialization.h"
 
@@ -27,7 +27,8 @@ Result IApplicationProxyService::OpenApplicationProxy(
     LOG_DEBUG(Service_AM, "called");
 
     if (const auto applet = this->GetAppletFromProcessId(pid)) {
-        *out_application_proxy = std::make_shared<IApplicationProxy>(m_nvnflinger, applet, system);
+        *out_application_proxy =
+            std::make_shared<IApplicationProxy>(system, applet, process_handle.Get(), m_nvnflinger);
         R_SUCCEED();
     } else {
         UNIMPLEMENTED();