From 915a0f7173299c0b8a2c285f4e90b34cdae80811 Mon Sep 17 00:00:00 2001
From: Steveice10 <1269164+Steveice10@users.noreply.github.com>
Date: Mon, 12 Jun 2023 08:33:13 -0700
Subject: [PATCH] hle: Stub IHidbusServer.GetBusHandle (#5284)

---
 .../HOS/Services/Hid/IHidbusServer.cs         | 23 ++++++++++++++++++-
 .../HOS/Services/Hid/Types/Npad/BusHandle.cs  | 14 +++++++++++
 .../HOS/Services/Hid/Types/Npad/BusType.cs    |  9 ++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusHandle.cs
 create mode 100644 src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusType.cs

diff --git a/src/Ryujinx.HLE/HOS/Services/Hid/IHidbusServer.cs b/src/Ryujinx.HLE/HOS/Services/Hid/IHidbusServer.cs
index bfd1d4dcdf..eec5292f5d 100644
--- a/src/Ryujinx.HLE/HOS/Services/Hid/IHidbusServer.cs
+++ b/src/Ryujinx.HLE/HOS/Services/Hid/IHidbusServer.cs
@@ -1,8 +1,29 @@
-namespace Ryujinx.HLE.HOS.Services.Hid
+using Ryujinx.Common;
+using Ryujinx.Common.Logging;
+
+namespace Ryujinx.HLE.HOS.Services.Hid
 {
     [Service("hidbus")]
     class IHidbusServer : IpcService
     {
         public IHidbusServer(ServiceCtx context) { }
+
+        [CommandCmif(1)]
+        // GetBusHandle(nn::hid::NpadIdType, nn::hidbus::BusType, nn::applet::AppletResourceUserId) -> (bool HasHandle, nn::hidbus::BusHandle)
+        public ResultCode GetBusHandle(ServiceCtx context)
+        {
+            NpadIdType npadIdType           = (NpadIdType)context.RequestData.ReadInt32();
+            context.RequestData.BaseStream.Position += 4; // Padding
+            BusType    busType              = (BusType)context.RequestData.ReadInt64();
+            long       appletResourceUserId = context.RequestData.ReadInt64();
+
+            context.ResponseData.Write(false);
+            context.ResponseData.BaseStream.Position += 7; // Padding
+            context.ResponseData.WriteStruct(new BusHandle());
+
+            Logger.Stub?.PrintStub(LogClass.ServiceHid, new { npadIdType, busType, appletResourceUserId });
+
+            return ResultCode.Success;
+        }
     }
 }
\ No newline at end of file
diff --git a/src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusHandle.cs b/src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusHandle.cs
new file mode 100644
index 0000000000..936ee68c4a
--- /dev/null
+++ b/src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusHandle.cs
@@ -0,0 +1,14 @@
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.HLE.HOS.Services.Hid
+{
+    [StructLayout(LayoutKind.Sequential)]
+    struct BusHandle
+    {
+        public int  AbstractedPadId;
+        public byte InternalIndex;
+        public byte PlayerNumber;
+        public byte BusTypeId;
+        public byte IsValid;
+    }
+}
diff --git a/src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusType.cs b/src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusType.cs
new file mode 100644
index 0000000000..41852365bc
--- /dev/null
+++ b/src/Ryujinx.HLE/HOS/Services/Hid/Types/Npad/BusType.cs
@@ -0,0 +1,9 @@
+namespace Ryujinx.HLE.HOS.Services.Hid
+{
+    public enum BusType : long
+    {
+        LeftJoyRail  = 0,
+        RightJoyRail = 1,
+        InternalBus  = 2
+    }
+}