From a8965aad970549b3fca87520616674aa3600a9dd Mon Sep 17 00:00:00 2001 From: Ac_K Date: Thu, 27 Jun 2019 18:02:41 +0200 Subject: [PATCH] irs: Little service cleanup (#712) * irs: Little service cleanup * more cleanup * Fix nit * Fix condition --- Ryujinx.HLE/HOS/Services/Hid/HidUtils.cs | 27 +++++++ .../Services/{ => Hid}/Irs/IIrSensorServer.cs | 75 +++++++++---------- Ryujinx.HLE/HOS/Services/Hid/Irs/IrsError.cs | 8 ++ Ryujinx.HLE/HOS/Services/Hid/NpadIdType.cs | 16 ++++ Ryujinx.HLE/HOS/Services/ServiceFactory.cs | 4 +- 5 files changed, 87 insertions(+), 43 deletions(-) create mode 100644 Ryujinx.HLE/HOS/Services/Hid/HidUtils.cs rename Ryujinx.HLE/HOS/Services/{ => Hid}/Irs/IIrSensorServer.cs (52%) create mode 100644 Ryujinx.HLE/HOS/Services/Hid/Irs/IrsError.cs create mode 100644 Ryujinx.HLE/HOS/Services/Hid/NpadIdType.cs diff --git a/Ryujinx.HLE/HOS/Services/Hid/HidUtils.cs b/Ryujinx.HLE/HOS/Services/Hid/HidUtils.cs new file mode 100644 index 0000000000..64252ce80e --- /dev/null +++ b/Ryujinx.HLE/HOS/Services/Hid/HidUtils.cs @@ -0,0 +1,27 @@ +using Ryujinx.HLE.Input; +using System; + +namespace Ryujinx.HLE.HOS.Services.Hid +{ + static class HidUtils + { + public static HidControllerId GetIndexFromNpadIdType(NpadIdType npadIdType) + { + switch (npadIdType) + { + case NpadIdType.Player1: return HidControllerId.ControllerPlayer1; + case NpadIdType.Player2: return HidControllerId.ControllerPlayer2; + case NpadIdType.Player3: return HidControllerId.ControllerPlayer3; + case NpadIdType.Player4: return HidControllerId.ControllerPlayer4; + case NpadIdType.Player5: return HidControllerId.ControllerPlayer5; + case NpadIdType.Player6: return HidControllerId.ControllerPlayer6; + case NpadIdType.Player7: return HidControllerId.ControllerPlayer7; + case NpadIdType.Player8: return HidControllerId.ControllerPlayer8; + case NpadIdType.Handheld: return HidControllerId.ControllerHandheld; + case NpadIdType.Unknown: return HidControllerId.ControllerUnknown; + + default: throw new ArgumentOutOfRangeException(nameof(npadIdType)); + } + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/HOS/Services/Irs/IIrSensorServer.cs b/Ryujinx.HLE/HOS/Services/Hid/Irs/IIrSensorServer.cs similarity index 52% rename from Ryujinx.HLE/HOS/Services/Irs/IIrSensorServer.cs rename to Ryujinx.HLE/HOS/Services/Hid/Irs/IIrSensorServer.cs index f0864c58dd..da1fbc1891 100644 --- a/Ryujinx.HLE/HOS/Services/Irs/IIrSensorServer.cs +++ b/Ryujinx.HLE/HOS/Services/Hid/Irs/IIrSensorServer.cs @@ -1,33 +1,43 @@ using Ryujinx.Common.Logging; -using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Kernel.Common; -using Ryujinx.HLE.HOS.Kernel.Memory; +using Ryujinx.HLE.Input; using System; using System.Collections.Generic; -namespace Ryujinx.HLE.HOS.Services.Irs +namespace Ryujinx.HLE.HOS.Services.Hid.Irs { class IIrSensorServer : IpcService { + private int _irsensorSharedMemoryHandle = 0; + private Dictionary _commands; public override IReadOnlyDictionary Commands => _commands; - private KSharedMemory _irsSharedMem; - - public IIrSensorServer(KSharedMemory irsSharedMem) + public IIrSensorServer() { _commands = new Dictionary { { 302, ActivateIrsensor }, { 303, DeactivateIrsensor }, { 304, GetIrsensorSharedMemoryHandle }, + //{ 305, StopImageProcessor }, + //{ 306, RunMomentProcessor }, + //{ 307, RunClusteringProcessor }, + //{ 308, RunImageTransferProcessor }, + //{ 309, GetImageTransferProcessorState }, + //{ 310, RunTeraPluginProcessor }, { 311, GetNpadIrCameraHandle }, - { 319, ActivateIrsensorWithFunctionLevel } + //{ 312, RunPointingProcessor }, + //{ 313, SuspendImageProcessor }, + //{ 314, CheckFirmwareVersion }, // 3.0.0+ + //{ 315, SetFunctionLevel }, // 4.0.0+ + //{ 316, RunImageTransferExProcessor }, // 4.0.0+ + //{ 317, RunIrLedProcessor }, // 4.0.0+ + //{ 318, StopImageProcessorAsync }, // 4.0.0+ + { 319, ActivateIrsensorWithFunctionLevel }, // 4.0.0+ }; - - _irsSharedMem = irsSharedMem; } // ActivateIrsensor(nn::applet::AppletResourceUserId, pid) @@ -53,14 +63,15 @@ namespace Ryujinx.HLE.HOS.Services.Irs // GetIrsensorSharedMemoryHandle(nn::applet::AppletResourceUserId, pid) -> handle public long GetIrsensorSharedMemoryHandle(ServiceCtx context) { - var handleTable = context.Process.HandleTable; - - if (handleTable.GenerateHandle(_irsSharedMem, out int handle) != KernelResult.Success) + if (_irsensorSharedMemoryHandle == 0) { - throw new InvalidOperationException("Out of handles!"); + if (context.Process.HandleTable.GenerateHandle(context.Device.System.IirsSharedMem, out _irsensorSharedMemoryHandle) != KernelResult.Success) + { + throw new InvalidOperationException("Out of handles!"); + } } - context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle); + context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_irsensorSharedMemoryHandle); return 0; } @@ -68,43 +79,25 @@ namespace Ryujinx.HLE.HOS.Services.Irs // GetNpadIrCameraHandle(u32) -> nn::irsensor::IrCameraHandle public long GetNpadIrCameraHandle(ServiceCtx context) { - uint npadId = context.RequestData.ReadUInt32(); + NpadIdType npadIdType = (NpadIdType)context.RequestData.ReadUInt32(); - if (npadId >= 8 && npadId != 16 && npadId != 32) + if (npadIdType > NpadIdType.Player8 && + npadIdType != NpadIdType.Unknown && + npadIdType != NpadIdType.Handheld) { - return ErrorCode.MakeError(ErrorModule.Hid, 0x2c5); + return ErrorCode.MakeError(ErrorModule.Irsensor, IrsError.NpadIdOutOfRange); } - if (((1 << (int)npadId) & 0x1000100FF) == 0) - { - return ErrorCode.MakeError(ErrorModule.Hid, 0x2c5); - } + HidControllerId irCameraHandle = HidUtils.GetIndexFromNpadIdType(npadIdType); - int npadTypeId = GetNpadTypeId(npadId); + context.ResponseData.Write((int)irCameraHandle); - context.ResponseData.Write(npadTypeId); + // NOTE: If the irCameraHandle pointer is null this error is returned, Doesn't occur in our case. + // return ErrorCode.MakeError(ErrorModule.Irsensor, IrsError.HandlePointerIsNull); return 0; } - private int GetNpadTypeId(uint npadId) - { - switch(npadId) - { - case 0: return 0; - case 1: return 1; - case 2: return 2; - case 3: return 3; - case 4: return 4; - case 5: return 5; - case 6: return 6; - case 7: return 7; - case 32: return 8; - case 16: return 9; - default: throw new ArgumentOutOfRangeException(nameof(npadId)); - } - } - // ActivateIrsensorWithFunctionLevel(nn::applet::AppletResourceUserId, nn::irsensor::PackedFunctionLevel, pid) public long ActivateIrsensorWithFunctionLevel(ServiceCtx context) { diff --git a/Ryujinx.HLE/HOS/Services/Hid/Irs/IrsError.cs b/Ryujinx.HLE/HOS/Services/Hid/Irs/IrsError.cs new file mode 100644 index 0000000000..fca3850738 --- /dev/null +++ b/Ryujinx.HLE/HOS/Services/Hid/Irs/IrsError.cs @@ -0,0 +1,8 @@ +namespace Ryujinx.HLE.HOS.Services.Hid.Irs +{ + static class IrsError + { + public const int HandlePointerIsNull = 212; + public const int NpadIdOutOfRange = 709; + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/HOS/Services/Hid/NpadIdType.cs b/Ryujinx.HLE/HOS/Services/Hid/NpadIdType.cs new file mode 100644 index 0000000000..5f6a68cb05 --- /dev/null +++ b/Ryujinx.HLE/HOS/Services/Hid/NpadIdType.cs @@ -0,0 +1,16 @@ +namespace Ryujinx.HLE.HOS.Services.Hid +{ + public enum NpadIdType + { + Player1 = 0, + Player2 = 1, + Player3 = 2, + Player4 = 3, + Player5 = 4, + Player6 = 5, + Player7 = 6, + Player8 = 7, + Unknown = 16, + Handheld = 32 + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/HOS/Services/ServiceFactory.cs b/Ryujinx.HLE/HOS/Services/ServiceFactory.cs index 3bde7b8779..63488d2178 100644 --- a/Ryujinx.HLE/HOS/Services/ServiceFactory.cs +++ b/Ryujinx.HLE/HOS/Services/ServiceFactory.cs @@ -8,7 +8,7 @@ using Ryujinx.HLE.HOS.Services.Caps; using Ryujinx.HLE.HOS.Services.Es; using Ryujinx.HLE.HOS.Services.FspSrv; using Ryujinx.HLE.HOS.Services.Hid; -using Ryujinx.HLE.HOS.Services.Irs; +using Ryujinx.HLE.HOS.Services.Hid.Irs; using Ryujinx.HLE.HOS.Services.Ldr; using Ryujinx.HLE.HOS.Services.Lm; using Ryujinx.HLE.HOS.Services.Mm; @@ -112,7 +112,7 @@ namespace Ryujinx.HLE.HOS.Services return new IHidServer(system); case "irs": - return new IIrSensorServer(system.IirsSharedMem); + return new IIrSensorServer(); case "ldr:ro": return new IRoInterface();