From 624c90a439c6a7cca9276aec8ccfa6d15ccb8d35 Mon Sep 17 00:00:00 2001
From: Narr the Reg <juangerman-13@hotmail.com>
Date: Fri, 23 Feb 2024 12:32:58 -0600
Subject: [PATCH] service: npns: Add ListenTo and GetReceiveEvent for QLaunch

---
 src/core/hle/service/npns/npns.cpp | 42 ++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/core/hle/service/npns/npns.cpp b/src/core/hle/service/npns/npns.cpp
index a162e5c54..e54827efe 100644
--- a/src/core/hle/service/npns/npns.cpp
+++ b/src/core/hle/service/npns/npns.cpp
@@ -3,22 +3,26 @@
 
 #include <memory>
 
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/service/cmif_serialization.h"
+#include "core/hle/service/kernel_helpers.h"
 #include "core/hle/service/npns/npns.h"
 #include "core/hle/service/server_manager.h"
 #include "core/hle/service/service.h"
 
 namespace Service::NPNS {
 
-class NPNS_S final : public ServiceFramework<NPNS_S> {
+class INpnsSystem final : public ServiceFramework<INpnsSystem> {
 public:
-    explicit NPNS_S(Core::System& system_) : ServiceFramework{system_, "npns:s"} {
+    explicit INpnsSystem(Core::System& system_)
+        : ServiceFramework{system_, "npns:s"}, service_context{system, "npns:s"} {
         // clang-format off
         static const FunctionInfo functions[] = {
             {1, nullptr, "ListenAll"},
-            {2, nullptr, "ListenTo"},
+            {2, C<&INpnsSystem::ListenTo>, "ListenTo"},
             {3, nullptr, "Receive"},
             {4, nullptr, "ReceiveRaw"},
-            {5, nullptr, "GetReceiveEvent"},
+            {5, C<&INpnsSystem::GetReceiveEvent>, "GetReceiveEvent"},
             {6, nullptr, "ListenUndelivered"},
             {7, nullptr, "GetStateChangeEVent"},
             {11, nullptr, "SubscribeTopic"},
@@ -59,12 +63,34 @@ public:
         // clang-format on
 
         RegisterHandlers(functions);
+
+        get_receive_event = service_context.CreateEvent("npns:s:GetReceiveEvent");
     }
+
+    ~INpnsSystem() override {
+        service_context.CloseEvent(get_receive_event);
+    }
+
+private:
+    Result ListenTo(u32 program_id) {
+        LOG_WARNING(Service_AM, "(STUBBED) called, program_id={}", program_id);
+        R_SUCCEED();
+    }
+
+    Result GetReceiveEvent(OutCopyHandle<Kernel::KReadableEvent> out_event) {
+        LOG_WARNING(Service_AM, "(STUBBED) called");
+
+        *out_event = &get_receive_event->GetReadableEvent();
+        R_SUCCEED();
+    }
+
+    KernelHelpers::ServiceContext service_context;
+    Kernel::KEvent* get_receive_event;
 };
 
-class NPNS_U final : public ServiceFramework<NPNS_U> {
+class INpnsUser final : public ServiceFramework<INpnsUser> {
 public:
-    explicit NPNS_U(Core::System& system_) : ServiceFramework{system_, "npns:u"} {
+    explicit INpnsUser(Core::System& system_) : ServiceFramework{system_, "npns:u"} {
         // clang-format off
         static const FunctionInfo functions[] = {
             {1, nullptr, "ListenAll"},
@@ -97,8 +123,8 @@ public:
 void LoopProcess(Core::System& system) {
     auto server_manager = std::make_unique<ServerManager>(system);
 
-    server_manager->RegisterNamedService("npns:s", std::make_shared<NPNS_S>(system));
-    server_manager->RegisterNamedService("npns:u", std::make_shared<NPNS_U>(system));
+    server_manager->RegisterNamedService("npns:s", std::make_shared<INpnsSystem>(system));
+    server_manager->RegisterNamedService("npns:u", std::make_shared<INpnsUser>(system));
     ServerManager::RunServer(std::move(server_manager));
 }