From f72d114335ae05de28a4fd4751dd74ab595dda87 Mon Sep 17 00:00:00 2001
From: NarcolepticK <NarcolepticKrias@gmail.com>
Date: Fri, 29 Jun 2018 10:31:56 -0400
Subject: [PATCH] service/pm: Migrate to ServiceFramework

---
 src/core/CMakeLists.txt                |  8 ++++--
 src/core/hle/service/pm/pm.cpp         | 18 ++++++++++++++
 src/core/hle/service/pm/pm.h           | 16 ++++++++++++
 src/core/hle/service/pm/pm_app.cpp     | 34 ++++++++++++++++++++++++++
 src/core/hle/service/{ => pm}/pm_app.h |  7 ++----
 src/core/hle/service/pm/pm_dbg.cpp     | 24 ++++++++++++++++++
 src/core/hle/service/pm/pm_dbg.h       | 19 ++++++++++++++
 src/core/hle/service/pm_app.cpp        | 33 -------------------------
 src/core/hle/service/service.cpp       |  4 +--
 9 files changed, 121 insertions(+), 42 deletions(-)
 create mode 100644 src/core/hle/service/pm/pm.cpp
 create mode 100644 src/core/hle/service/pm/pm.h
 create mode 100644 src/core/hle/service/pm/pm_app.cpp
 rename src/core/hle/service/{ => pm}/pm_app.h (70%)
 create mode 100644 src/core/hle/service/pm/pm_dbg.cpp
 create mode 100644 src/core/hle/service/pm/pm_dbg.h
 delete mode 100644 src/core/hle/service/pm_app.cpp

diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 2da700455..fd7634ce8 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -322,8 +322,12 @@ add_library(core STATIC
     hle/service/nwm/uds_connection.h
     hle/service/nwm/uds_data.cpp
     hle/service/nwm/uds_data.h
-    hle/service/pm_app.cpp
-    hle/service/pm_app.h
+    hle/service/pm/pm.cpp
+    hle/service/pm/pm.h
+    hle/service/pm/pm_app.cpp
+    hle/service/pm/pm_app.h
+    hle/service/pm/pm_dbg.cpp
+    hle/service/pm/pm_dbg.h
     hle/service/ptm/ptm.cpp
     hle/service/ptm/ptm.h
     hle/service/ptm/ptm_gets.cpp
diff --git a/src/core/hle/service/pm/pm.cpp b/src/core/hle/service/pm/pm.cpp
new file mode 100644
index 000000000..c79541921
--- /dev/null
+++ b/src/core/hle/service/pm/pm.cpp
@@ -0,0 +1,18 @@
+// Copyright 2018 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "core/hle/service/pm/pm.h"
+#include "core/hle/service/pm/pm_app.h"
+#include "core/hle/service/pm/pm_dbg.h"
+
+namespace Service {
+namespace PM {
+
+void InstallInterfaces(SM::ServiceManager& service_manager) {
+    std::make_shared<PM_APP>()->InstallAsService(service_manager);
+    std::make_shared<PM_DBG>()->InstallAsService(service_manager);
+}
+
+} // namespace PM
+} // namespace Service
diff --git a/src/core/hle/service/pm/pm.h b/src/core/hle/service/pm/pm.h
new file mode 100644
index 000000000..1d3474b0a
--- /dev/null
+++ b/src/core/hle/service/pm/pm.h
@@ -0,0 +1,16 @@
+// Copyright 2018 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include "core/hle/service/service.h"
+
+namespace Service {
+namespace PM {
+
+/// Initializes the PM services.
+void InstallInterfaces(SM::ServiceManager& service_manager);
+
+} // namespace PM
+} // namespace Service
diff --git a/src/core/hle/service/pm/pm_app.cpp b/src/core/hle/service/pm/pm_app.cpp
new file mode 100644
index 000000000..764d4c075
--- /dev/null
+++ b/src/core/hle/service/pm/pm_app.cpp
@@ -0,0 +1,34 @@
+// Copyright 2014 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "core/hle/ipc_helpers.h"
+#include "core/hle/service/pm/pm_app.h"
+
+namespace Service {
+namespace PM {
+
+PM_APP::PM_APP() : ServiceFramework("pm:app", 3) {
+    static const FunctionInfo functions[] = {
+        // clang-format off
+        {0x00010140, nullptr, "LaunchTitle"},
+        {0x00020082, nullptr, "LaunchFIRM"},
+        {0x00030080, nullptr, "TerminateApplication"},
+        {0x00040100, nullptr, "TerminateTitle"},
+        {0x000500C0, nullptr, "TerminateProcess"},
+        {0x00060082, nullptr, "PrepareForReboot"},
+        {0x00070042, nullptr, "GetFIRMLaunchParams"},
+        {0x00080100, nullptr, "GetTitleExheaderFlags"},
+        {0x00090042, nullptr, "SetFIRMLaunchParams"},
+        {0x000A0140, nullptr, "SetAppResourceLimit"},
+        {0x000B0140, nullptr, "GetAppResourceLimit"},
+        {0x000C0080, nullptr, "UnregisterProcess"},
+        {0x000D0240, nullptr, "LaunchTitleUpdate"},
+        // clang-format on
+    };
+
+    RegisterHandlers(functions);
+}
+
+} // namespace PM
+} // namespace Service
diff --git a/src/core/hle/service/pm_app.h b/src/core/hle/service/pm/pm_app.h
similarity index 70%
rename from src/core/hle/service/pm_app.h
rename to src/core/hle/service/pm/pm_app.h
index 151c69f3d..4bd778166 100644
--- a/src/core/hle/service/pm_app.h
+++ b/src/core/hle/service/pm/pm_app.h
@@ -9,13 +9,10 @@
 namespace Service {
 namespace PM {
 
-class PM_APP final : public Interface {
+class PM_APP final : public ServiceFramework<PM_APP> {
 public:
     PM_APP();
-
-    std::string GetPortName() const override {
-        return "pm:app";
-    }
+    ~PM_APP() = default;
 };
 
 } // namespace PM
diff --git a/src/core/hle/service/pm/pm_dbg.cpp b/src/core/hle/service/pm/pm_dbg.cpp
new file mode 100644
index 000000000..b4889509d
--- /dev/null
+++ b/src/core/hle/service/pm/pm_dbg.cpp
@@ -0,0 +1,24 @@
+// Copyright 2018 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "core/hle/ipc_helpers.h"
+#include "core/hle/service/pm/pm_dbg.h"
+
+namespace Service {
+namespace PM {
+
+PM_DBG::PM_DBG() : ServiceFramework("pm:dbg", 3) {
+    static const FunctionInfo functions[] = {
+        // clang-format off
+        {0x00010140, nullptr, "LaunchAppDebug"},
+        {0x00020140, nullptr, "LaunchApp"},
+        {0x00030000, nullptr, "RunQueuedProcess"},
+        // clang-format on
+    };
+
+    RegisterHandlers(functions);
+}
+
+} // namespace PM
+} // namespace Service
diff --git a/src/core/hle/service/pm/pm_dbg.h b/src/core/hle/service/pm/pm_dbg.h
new file mode 100644
index 000000000..6fc118769
--- /dev/null
+++ b/src/core/hle/service/pm/pm_dbg.h
@@ -0,0 +1,19 @@
+// Copyright 2018 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include "core/hle/service/service.h"
+
+namespace Service {
+namespace PM {
+
+class PM_DBG final : public ServiceFramework<PM_DBG> {
+public:
+    PM_DBG();
+    ~PM_DBG() = default;
+};
+
+} // namespace PM
+} // namespace Service
diff --git a/src/core/hle/service/pm_app.cpp b/src/core/hle/service/pm_app.cpp
deleted file mode 100644
index caa16f952..000000000
--- a/src/core/hle/service/pm_app.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright 2014 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#include "core/hle/service/pm_app.h"
-
-namespace Service {
-namespace PM {
-
-const Interface::FunctionInfo FunctionTable[] = {
-    // clang-format off
-    {0x00010140, nullptr, "LaunchTitle"},
-    {0x00020082, nullptr, "LaunchFIRM"},
-    {0x00030080, nullptr, "TerminateApplication"},
-    {0x00040100, nullptr, "TerminateTitle"},
-    {0x000500C0, nullptr, "TerminateProcess"},
-    {0x00060082, nullptr, "PrepareForReboot"},
-    {0x00070042, nullptr, "GetFIRMLaunchParams"},
-    {0x00080100, nullptr, "GetTitleExheaderFlags"},
-    {0x00090042, nullptr, "SetFIRMLaunchParams"},
-    {0x000A0140, nullptr, "SetAppResourceLimit"},
-    {0x000B0140, nullptr, "GetAppResourceLimit"},
-    {0x000C0080, nullptr, "UnregisterProcess"},
-    {0x000D0240, nullptr, "LaunchTitleUpdate"},
-    // clang-format on
-};
-
-PM_APP::PM_APP() {
-    Register(FunctionTable);
-}
-
-} // namespace PM
-} // namespace Service
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index 443f32ee9..98254156d 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -43,7 +43,7 @@
 #include "core/hle/service/nim/nim.h"
 #include "core/hle/service/ns/ns.h"
 #include "core/hle/service/nwm/nwm.h"
-#include "core/hle/service/pm_app.h"
+#include "core/hle/service/pm/pm.h"
 #include "core/hle/service/ptm/ptm.h"
 #include "core/hle/service/pxi/pxi.h"
 #include "core/hle/service/qtm/qtm.h"
@@ -259,7 +259,7 @@ void Init(std::shared_ptr<SM::ServiceManager>& sm) {
     AddService(new CSND::CSND_SND);
     AddService(new DSP_DSP::Interface);
     AddService(new HTTP::HTTP_C);
-    AddService(new PM::PM_APP);
+    PM::InstallInterfaces(*sm);
     AddService(new SOC::SOC_U);
     SSL::InstallInterfaces(*sm);
     Y2R::InstallInterfaces(*sm);