From b9c7e5c2c89a6db7f7f160c6186da246a270623f Mon Sep 17 00:00:00 2001
From: Narr the Reg <juangerman-13@hotmail.com>
Date: Thu, 16 Nov 2023 11:18:11 -0600
Subject: [PATCH] service: hid: Implement xpad calls

---
 src/core/hle/service/hid/hid_server.cpp | 169 ++++++++++++++++++------
 src/core/hle/service/hid/hid_server.h   |   8 ++
 2 files changed, 140 insertions(+), 37 deletions(-)

diff --git a/src/core/hle/service/hid/hid_server.cpp b/src/core/hle/service/hid/hid_server.cpp
index 348dc98d95..2fb31cf477 100644
--- a/src/core/hle/service/hid/hid_server.cpp
+++ b/src/core/hle/service/hid/hid_server.cpp
@@ -75,19 +75,19 @@ IHidServer::IHidServer(Core::System& system_, std::shared_ptr<ResourceManager> r
         {26, nullptr, "ActivateDebugMouse"},
         {31, &IHidServer::ActivateKeyboard, "ActivateKeyboard"},
         {32, &IHidServer::SendKeyboardLockKeyEvent, "SendKeyboardLockKeyEvent"},
-        {40, nullptr, "AcquireXpadIdEventHandle"},
-        {41, nullptr, "ReleaseXpadIdEventHandle"},
+        {40, &IHidServer::AcquireXpadIdEventHandle, "AcquireXpadIdEventHandle"},
+        {41, &IHidServer::ReleaseXpadIdEventHandle, "ReleaseXpadIdEventHandle"},
         {51, &IHidServer::ActivateXpad, "ActivateXpad"},
         {55, &IHidServer::GetXpadIds, "GetXpadIds"},
-        {56, nullptr, "ActivateJoyXpad"},
-        {58, nullptr, "GetJoyXpadLifoHandle"},
-        {59, nullptr, "GetJoyXpadIds"},
+        {56, &IHidServer::ActivateJoyXpad, "ActivateJoyXpad"},
+        {58, &IHidServer::GetJoyXpadLifoHandle, "GetJoyXpadLifoHandle"},
+        {59, &IHidServer::GetJoyXpadIds, "GetJoyXpadIds"},
         {60, &IHidServer::ActivateSixAxisSensor, "ActivateSixAxisSensor"},
         {61, &IHidServer::DeactivateSixAxisSensor, "DeactivateSixAxisSensor"},
-        {62, nullptr, "GetSixAxisSensorLifoHandle"},
-        {63, nullptr, "ActivateJoySixAxisSensor"},
-        {64, nullptr, "DeactivateJoySixAxisSensor"},
-        {65, nullptr, "GetJoySixAxisSensorLifoHandle"},
+        {62, &IHidServer::GetSixAxisSensorLifoHandle, "GetSixAxisSensorLifoHandle"},
+        {63, &IHidServer::ActivateJoySixAxisSensor, "ActivateJoySixAxisSensor"},
+        {64, &IHidServer::DeactivateJoySixAxisSensor, "DeactivateJoySixAxisSensor"},
+        {65, &IHidServer::GetJoySixAxisSensorLifoHandle, "GetJoySixAxisSensorLifoHandle"},
         {66, &IHidServer::StartSixAxisSensor, "StartSixAxisSensor"},
         {67, &IHidServer::StopSixAxisSensor, "StopSixAxisSensor"},
         {68, &IHidServer::IsSixAxisSensorFusionEnabled, "IsSixAxisSensorFusionEnabled"},
@@ -284,6 +284,31 @@ void IHidServer::SendKeyboardLockKeyEvent(HLERequestContext& ctx) {
     rb.Push(ResultSuccess);
 }
 
+void IHidServer::AcquireXpadIdEventHandle(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto applet_resource_user_id{rp.Pop<u64>()};
+
+    LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
+
+    // This function has been stubbed since 10.0.0+
+
+    IPC::ResponseBuilder rb{ctx, 2, 1};
+    rb.Push(ResultSuccess);
+    // Handle returned is null here
+}
+
+void IHidServer::ReleaseXpadIdEventHandle(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto applet_resource_user_id{rp.Pop<u64>()};
+
+    LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
+
+    // This function has been stubbed since 10.0.0+
+
+    IPC::ResponseBuilder rb{ctx, 2};
+    rb.Push(ResultSuccess);
+}
+
 void IHidServer::ActivateXpad(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     struct Parameters {
@@ -295,41 +320,70 @@ void IHidServer::ActivateXpad(HLERequestContext& ctx) {
 
     const auto parameters{rp.PopRaw<Parameters>()};
 
-    GetResourceManager()->ActivateController(HidController::XPad);
-
     LOG_DEBUG(Service_HID, "called, basic_xpad_id={}, applet_resource_user_id={}",
               parameters.basic_xpad_id, parameters.applet_resource_user_id);
 
+    // This function has been stubbed since 10.0.0+
+
     IPC::ResponseBuilder rb{ctx, 2};
     rb.Push(ResultSuccess);
 }
 
 void IHidServer::GetXpadIds(HLERequestContext& ctx) {
-    IPC::RequestParser rp{ctx};
-    const auto applet_resource_user_id{rp.Pop<u64>()};
+    LOG_DEBUG(Service_HID, "called");
 
-    LOG_DEBUG(Service_HID, "(STUBBED) called, applet_resource_user_id={}", applet_resource_user_id);
+    // This function has been hardcoded since 10.0.0+
+    const std::array<u32, 4> basic_xpad_id{0, 1, 2, 3};
+    ctx.WriteBuffer(basic_xpad_id);
 
-    IPC::ResponseBuilder rb{ctx, 3};
+    IPC::ResponseBuilder rb{ctx, 4};
     rb.Push(ResultSuccess);
-    rb.Push(0);
+    rb.Push<s64>(basic_xpad_id.size());
+}
+
+void IHidServer::ActivateJoyXpad(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto joy_xpad_id{rp.Pop<u32>()};
+
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
+
+    // This function has been stubbed since 10.0.0+
+
+    IPC::ResponseBuilder rb{ctx, 2};
+    rb.Push(ResultSuccess);
+}
+
+void IHidServer::GetJoyXpadLifoHandle(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto joy_xpad_id{rp.Pop<u32>()};
+
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
+
+    // This function has been stubbed since 10.0.0+
+
+    IPC::ResponseBuilder rb{ctx, 2, 1};
+    rb.Push(ResultSuccess);
+    // Handle returned is null here
+}
+
+void IHidServer::GetJoyXpadIds(HLERequestContext& ctx) {
+    LOG_DEBUG(Service_HID, "called");
+
+    // This function has been hardcoded since 10.0.0+
+    const s64 basic_xpad_id_count{};
+
+    IPC::ResponseBuilder rb{ctx, 4};
+    rb.Push(ResultSuccess);
+    rb.Push(basic_xpad_id_count);
 }
 
 void IHidServer::ActivateSixAxisSensor(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
-    struct Parameters {
-        u32 basic_xpad_id;
-        INSERT_PADDING_WORDS_NOINIT(1);
-        u64 applet_resource_user_id;
-    };
-    static_assert(sizeof(Parameters) == 0x10, "Parameters has incorrect size.");
+    const auto joy_xpad_id{rp.Pop<u32>()};
 
-    const auto parameters{rp.PopRaw<Parameters>()};
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
 
-    // This function does nothing on 10.0.0+
-
-    LOG_WARNING(Service_HID, "(STUBBED) called, basic_xpad_id={}, applet_resource_user_id={}",
-                parameters.basic_xpad_id, parameters.applet_resource_user_id);
+    // This function has been stubbed since 10.0.0+
 
     IPC::ResponseBuilder rb{ctx, 2};
     rb.Push(ResultSuccess);
@@ -337,24 +391,65 @@ void IHidServer::ActivateSixAxisSensor(HLERequestContext& ctx) {
 
 void IHidServer::DeactivateSixAxisSensor(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
-    struct Parameters {
-        u32 basic_xpad_id;
-        INSERT_PADDING_WORDS_NOINIT(1);
-        u64 applet_resource_user_id;
-    };
-    static_assert(sizeof(Parameters) == 0x10, "Parameters has incorrect size.");
+    const auto joy_xpad_id{rp.Pop<u32>()};
 
-    const auto parameters{rp.PopRaw<Parameters>()};
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
 
-    // This function does nothing on 10.0.0+
+    // This function has been stubbed since 10.0.0+
 
-    LOG_WARNING(Service_HID, "(STUBBED) called, basic_xpad_id={}, applet_resource_user_id={}",
-                parameters.basic_xpad_id, parameters.applet_resource_user_id);
+    IPC::ResponseBuilder rb{ctx, 2, 1};
+    rb.Push(ResultSuccess);
+}
+
+void IHidServer::GetSixAxisSensorLifoHandle(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto joy_xpad_id{rp.Pop<u32>()};
+
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
+
+    // This function has been stubbed since 10.0.0+
 
     IPC::ResponseBuilder rb{ctx, 2};
     rb.Push(ResultSuccess);
 }
 
+void IHidServer::ActivateJoySixAxisSensor(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto joy_xpad_id{rp.Pop<u32>()};
+
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
+
+    // This function has been stubbed since 10.0.0+
+
+    IPC::ResponseBuilder rb{ctx, 2};
+    rb.Push(ResultSuccess);
+}
+
+void IHidServer::DeactivateJoySixAxisSensor(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto joy_xpad_id{rp.Pop<u32>()};
+
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
+
+    // This function has been stubbed since 10.0.0+
+
+    IPC::ResponseBuilder rb{ctx, 2};
+    rb.Push(ResultSuccess);
+}
+
+void IHidServer::GetJoySixAxisSensorLifoHandle(HLERequestContext& ctx) {
+    IPC::RequestParser rp{ctx};
+    const auto joy_xpad_id{rp.Pop<u32>()};
+
+    LOG_DEBUG(Service_HID, "called, joy_xpad_id={}", joy_xpad_id);
+
+    // This function has been stubbed since 10.0.0+
+
+    IPC::ResponseBuilder rb{ctx, 2, 1};
+    rb.Push(ResultSuccess);
+    // Handle returned is null here
+}
+
 void IHidServer::StartSixAxisSensor(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     struct Parameters {
diff --git a/src/core/hle/service/hid/hid_server.h b/src/core/hle/service/hid/hid_server.h
index 3d25ea1bc2..98353b0dbb 100644
--- a/src/core/hle/service/hid/hid_server.h
+++ b/src/core/hle/service/hid/hid_server.h
@@ -26,11 +26,19 @@ private:
     void ActivateMouse(HLERequestContext& ctx);
     void ActivateKeyboard(HLERequestContext& ctx);
     void SendKeyboardLockKeyEvent(HLERequestContext& ctx);
+    void AcquireXpadIdEventHandle(HLERequestContext& ctx);
+    void ReleaseXpadIdEventHandle(HLERequestContext& ctx);
     void ActivateXpad(HLERequestContext& ctx);
     void GetXpadIds(HLERequestContext& ctx);
+    void ActivateJoyXpad(HLERequestContext& ctx);
+    void GetJoyXpadLifoHandle(HLERequestContext& ctx);
     void GetJoyXpadIds(HLERequestContext& ctx);
     void ActivateSixAxisSensor(HLERequestContext& ctx);
     void DeactivateSixAxisSensor(HLERequestContext& ctx);
+    void GetSixAxisSensorLifoHandle(HLERequestContext& ctx);
+    void ActivateJoySixAxisSensor(HLERequestContext& ctx);
+    void DeactivateJoySixAxisSensor(HLERequestContext& ctx);
+    void GetJoySixAxisSensorLifoHandle(HLERequestContext& ctx);
     void StartSixAxisSensor(HLERequestContext& ctx);
     void StopSixAxisSensor(HLERequestContext& ctx);
     void IsSixAxisSensorFusionEnabled(HLERequestContext& ctx);