Support other switch controller types (#487)

* Make controllers modular, support changing controller type

* return readable events

* signal hid events

* fix style
This commit is contained in:
emmauss 2018-11-20 02:01:36 +02:00 committed by Ac_K
parent 0c36835f6d
commit dc02ac08ca
24 changed files with 574 additions and 409 deletions

View file

@ -23,7 +23,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
private bool UsbFullKeyControllerEnabled;
private HidNpadJoyHoldType NpadJoyHoldType;
private HidNpadStyle NpadStyleTag;
private HidNpadStyle NpadStyleSet;
private HidNpadJoyAssignmentMode NpadJoyAssignmentMode;
private HidNpadHandheldActivationMode NpadHandheldActivationMode;
private HidGyroscopeZeroDriftMode GyroscopeZeroDriftMode;
@ -153,7 +153,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
PalmaOperationCompleteEvent = new KEvent(System);
NpadJoyHoldType = HidNpadJoyHoldType.Vertical;
NpadStyleTag = HidNpadStyle.FullKey | HidNpadStyle.Dual | HidNpadStyle.Left | HidNpadStyle.Right;
NpadStyleSet = HidNpadStyle.FullKey | HidNpadStyle.Dual | HidNpadStyle.Left | HidNpadStyle.Right | HidNpadStyle.Handheld;
NpadJoyAssignmentMode = HidNpadJoyAssignmentMode.Dual;
NpadHandheldActivationMode = HidNpadHandheldActivationMode.Dual;
GyroscopeZeroDriftMode = HidGyroscopeZeroDriftMode.Standard;
@ -161,6 +161,9 @@ namespace Ryujinx.HLE.HOS.Services.Hid
SensorFusionParams = new HidSensorFusionParameters();
AccelerometerParams = new HidAccelerometerParameters();
VibrationValue = new HidVibrationValue();
// TODO: signal event at right place
XpadIdEvent.ReadableEvent.Signal();
}
// CreateAppletResource(nn::applet::AppletResourceUserId) -> object<nn::hid::IAppletResource>
@ -218,7 +221,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
{
long XpadId = Context.RequestData.ReadInt64();
if (Context.Process.HandleTable.GenerateHandle(XpadIdEvent, out XpadIdEventHandle) != KernelResult.Success)
if (Context.Process.HandleTable.GenerateHandle(XpadIdEvent.ReadableEvent, out XpadIdEventHandle) != KernelResult.Success)
{
throw new InvalidOperationException("Out of handles!");
}
@ -652,12 +655,14 @@ namespace Ryujinx.HLE.HOS.Services.Hid
// SetSupportedNpadStyleSet(nn::applet::AppletResourceUserId, nn::hid::NpadStyleTag)
public long SetSupportedNpadStyleSet(ServiceCtx Context)
{
NpadStyleTag = (HidNpadStyle)Context.RequestData.ReadInt32();
NpadStyleSet = (HidNpadStyle)Context.RequestData.ReadInt32();
long AppletResourceUserId = Context.RequestData.ReadInt64();
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. AppletResourceUserId: {AppletResourceUserId} - " +
$"NpadStyleTag: {NpadStyleTag}");
$"NpadStyleSet: {NpadStyleSet}");
NpadStyleSetUpdateEvent.ReadableEvent.Signal();
return 0;
}
@ -667,10 +672,10 @@ namespace Ryujinx.HLE.HOS.Services.Hid
{
long AppletResourceUserId = Context.RequestData.ReadInt64();
Context.ResponseData.Write((int)NpadStyleTag);
Context.ResponseData.Write((int)NpadStyleSet);
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. AppletResourceUserId: {AppletResourceUserId} - " +
$"NpadStyleTag: {NpadStyleTag}");
$"NpadStyleSet: {NpadStyleSet}");
return 0;
}
@ -714,7 +719,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
int NpadId = Context.RequestData.ReadInt32();
long NpadStyleSet = Context.RequestData.ReadInt64();
if (Context.Process.HandleTable.GenerateHandle(NpadStyleSetUpdateEvent, out int Handle) != KernelResult.Success)
if (Context.Process.HandleTable.GenerateHandle(NpadStyleSetUpdateEvent.ReadableEvent, out int Handle) != KernelResult.Success)
{
throw new InvalidOperationException("Out of handles!");
}
@ -1348,6 +1353,8 @@ namespace Ryujinx.HLE.HOS.Services.Hid
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. PalmaConnectionHandle: {PalmaConnectionHandle}");
PalmaOperationCompleteEvent.ReadableEvent.Signal();
return 0;
}
@ -1356,7 +1363,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
{
int PalmaConnectionHandle = Context.RequestData.ReadInt32();
if (Context.Process.HandleTable.GenerateHandle(PalmaOperationCompleteEvent, out int Handle) != KernelResult.Success)
if (Context.Process.HandleTable.GenerateHandle(PalmaOperationCompleteEvent.ReadableEvent, out int Handle) != KernelResult.Success)
{
throw new InvalidOperationException("Out of handles!");
}
@ -1392,6 +1399,8 @@ namespace Ryujinx.HLE.HOS.Services.Hid
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. PalmaConnectionHandle: {PalmaConnectionHandle} - " +
$"Unknown0: {Unknown0}");
PalmaOperationCompleteEvent.ReadableEvent.Signal();
return 0;
}
@ -1404,6 +1413,8 @@ namespace Ryujinx.HLE.HOS.Services.Hid
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. PalmaConnectionHandle: {PalmaConnectionHandle} - " +
$"FrModeType: {FrModeType}");
PalmaOperationCompleteEvent.ReadableEvent.Signal();
return 0;
}
@ -1426,6 +1437,8 @@ namespace Ryujinx.HLE.HOS.Services.Hid
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. PalmaConnectionHandle: {PalmaConnectionHandle} - " +
$"EnabledPalmaStep: {EnabledPalmaStep}");
PalmaOperationCompleteEvent.ReadableEvent.Signal();
return 0;
}
@ -1436,6 +1449,8 @@ namespace Ryujinx.HLE.HOS.Services.Hid
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. PalmaConnectionHandle: {PalmaConnectionHandle}");
PalmaOperationCompleteEvent.ReadableEvent.Signal();
return 0;
}
@ -1446,6 +1461,8 @@ namespace Ryujinx.HLE.HOS.Services.Hid
Logger.PrintStub(LogClass.ServiceHid, $"Stubbed. PalmaConnectionHandle: {PalmaConnectionHandle}");
PalmaOperationCompleteEvent.ReadableEvent.Signal();
return 0;
}
@ -1475,6 +1492,8 @@ namespace Ryujinx.HLE.HOS.Services.Hid
$"Unknown0: {Unknown0} - " +
$"Unknown1: {Unknown1}");
PalmaOperationCompleteEvent.ReadableEvent.Signal();
return 0;
}

View file

@ -1,273 +0,0 @@
using Ryujinx.Common;
using Ryujinx.HLE.HOS;
using System;
namespace Ryujinx.HLE.Input
{
public class Hid
{
/*
* Reference:
* https://github.com/reswitched/libtransistor/blob/development/lib/hid.c
* https://github.com/reswitched/libtransistor/blob/development/include/libtransistor/hid.h
* https://github.com/switchbrew/libnx/blob/master/nx/source/services/hid.c
* https://github.com/switchbrew/libnx/blob/master/nx/include/switch/services/hid.h
*/
private const int HidHeaderSize = 0x400;
private const int HidTouchScreenSize = 0x3000;
private const int HidMouseSize = 0x400;
private const int HidKeyboardSize = 0x400;
private const int HidUnkSection1Size = 0x400;
private const int HidUnkSection2Size = 0x400;
private const int HidUnkSection3Size = 0x400;
private const int HidUnkSection4Size = 0x400;
private const int HidUnkSection5Size = 0x200;
private const int HidUnkSection6Size = 0x200;
private const int HidUnkSection7Size = 0x200;
private const int HidUnkSection8Size = 0x800;
private const int HidControllerSerialsSize = 0x4000;
private const int HidControllersSize = 0x32000;
private const int HidUnkSection9Size = 0x800;
private const int HidTouchHeaderSize = 0x28;
private const int HidTouchEntrySize = 0x298;
private const int HidTouchEntryHeaderSize = 0x10;
private const int HidTouchEntryTouchSize = 0x28;
private const int HidControllerSize = 0x5000;
private const int HidControllerHeaderSize = 0x28;
private const int HidControllerLayoutsSize = 0x350;
private const int HidControllersLayoutHeaderSize = 0x20;
private const int HidControllersInputEntrySize = 0x30;
private const int HidHeaderOffset = 0;
private const int HidTouchScreenOffset = HidHeaderOffset + HidHeaderSize;
private const int HidMouseOffset = HidTouchScreenOffset + HidTouchScreenSize;
private const int HidKeyboardOffset = HidMouseOffset + HidMouseSize;
private const int HidUnkSection1Offset = HidKeyboardOffset + HidKeyboardSize;
private const int HidUnkSection2Offset = HidUnkSection1Offset + HidUnkSection1Size;
private const int HidUnkSection3Offset = HidUnkSection2Offset + HidUnkSection2Size;
private const int HidUnkSection4Offset = HidUnkSection3Offset + HidUnkSection3Size;
private const int HidUnkSection5Offset = HidUnkSection4Offset + HidUnkSection4Size;
private const int HidUnkSection6Offset = HidUnkSection5Offset + HidUnkSection5Size;
private const int HidUnkSection7Offset = HidUnkSection6Offset + HidUnkSection6Size;
private const int HidUnkSection8Offset = HidUnkSection7Offset + HidUnkSection7Size;
private const int HidControllerSerialsOffset = HidUnkSection8Offset + HidUnkSection8Size;
private const int HidControllersOffset = HidControllerSerialsOffset + HidControllerSerialsSize;
private const int HidUnkSection9Offset = HidControllersOffset + HidControllersSize;
private const int HidEntryCount = 17;
private Switch Device;
private long HidPosition;
public Hid(Switch Device, long HidPosition)
{
this.Device = Device;
this.HidPosition = HidPosition;
Device.Memory.FillWithZeros(HidPosition, Horizon.HidSize);
InitializeJoyconPair(
JoyConColor.Body_Neon_Red,
JoyConColor.Buttons_Neon_Red,
JoyConColor.Body_Neon_Blue,
JoyConColor.Buttons_Neon_Blue);
}
private void InitializeJoyconPair(
JoyConColor LeftColorBody,
JoyConColor LeftColorButtons,
JoyConColor RightColorBody,
JoyConColor RightColorButtons)
{
long BaseControllerOffset = HidPosition + HidControllersOffset + 8 * HidControllerSize;
HidControllerType Type = HidControllerType.ControllerType_Handheld;
bool IsHalf = false;
HidControllerColorDesc SingleColorDesc =
HidControllerColorDesc.ColorDesc_ColorsNonexistent;
JoyConColor SingleColorBody = JoyConColor.Black;
JoyConColor SingleColorButtons = JoyConColor.Black;
HidControllerColorDesc SplitColorDesc = 0;
Device.Memory.WriteInt32(BaseControllerOffset + 0x00, (int)Type);
Device.Memory.WriteInt32(BaseControllerOffset + 0x04, IsHalf ? 1 : 0);
Device.Memory.WriteInt32(BaseControllerOffset + 0x08, (int)SingleColorDesc);
Device.Memory.WriteInt32(BaseControllerOffset + 0x0c, (int)SingleColorBody);
Device.Memory.WriteInt32(BaseControllerOffset + 0x10, (int)SingleColorButtons);
Device.Memory.WriteInt32(BaseControllerOffset + 0x14, (int)SplitColorDesc);
Device.Memory.WriteInt32(BaseControllerOffset + 0x18, (int)LeftColorBody);
Device.Memory.WriteInt32(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
Device.Memory.WriteInt32(BaseControllerOffset + 0x20, (int)RightColorBody);
Device.Memory.WriteInt32(BaseControllerOffset + 0x24, (int)RightColorButtons);
}
private HidControllerButtons UpdateStickButtons(
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
HidControllerButtons Result = 0;
if (RightStick.DX < 0)
{
Result |= HidControllerButtons.KEY_RSTICK_LEFT;
}
if (RightStick.DX > 0)
{
Result |= HidControllerButtons.KEY_RSTICK_RIGHT;
}
if (RightStick.DY < 0)
{
Result |= HidControllerButtons.KEY_RSTICK_DOWN;
}
if (RightStick.DY > 0)
{
Result |= HidControllerButtons.KEY_RSTICK_UP;
}
if (LeftStick.DX < 0)
{
Result |= HidControllerButtons.KEY_LSTICK_LEFT;
}
if (LeftStick.DX > 0)
{
Result |= HidControllerButtons.KEY_LSTICK_RIGHT;
}
if (LeftStick.DY < 0)
{
Result |= HidControllerButtons.KEY_LSTICK_DOWN;
}
if (LeftStick.DY > 0)
{
Result |= HidControllerButtons.KEY_LSTICK_UP;
}
return Result;
}
public void SetJoyconButton(
HidControllerId ControllerId,
HidControllerLayouts ControllerLayout,
HidControllerButtons Buttons,
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
Buttons |= UpdateStickButtons(LeftStick, RightStick);
long ControllerOffset = HidPosition + HidControllersOffset;
ControllerOffset += (int)ControllerId * HidControllerSize;
ControllerOffset += HidControllerHeaderSize;
ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
long LastEntry = Device.Memory.ReadInt64(ControllerOffset + 0x10);
long CurrEntry = (LastEntry + 1) % HidEntryCount;
long Timestamp = GetTimestamp();
Device.Memory.WriteInt64(ControllerOffset + 0x00, Timestamp);
Device.Memory.WriteInt64(ControllerOffset + 0x08, HidEntryCount);
Device.Memory.WriteInt64(ControllerOffset + 0x10, CurrEntry);
Device.Memory.WriteInt64(ControllerOffset + 0x18, HidEntryCount - 1);
ControllerOffset += HidControllersLayoutHeaderSize;
long LastEntryOffset = ControllerOffset + LastEntry * HidControllersInputEntrySize;
ControllerOffset += CurrEntry * HidControllersInputEntrySize;
long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
Device.Memory.WriteInt64(ControllerOffset + 0x00, SampleCounter);
Device.Memory.WriteInt64(ControllerOffset + 0x08, SampleCounter);
Device.Memory.WriteInt64(ControllerOffset + 0x10, (uint)Buttons);
Device.Memory.WriteInt32(ControllerOffset + 0x18, LeftStick.DX);
Device.Memory.WriteInt32(ControllerOffset + 0x1c, LeftStick.DY);
Device.Memory.WriteInt32(ControllerOffset + 0x20, RightStick.DX);
Device.Memory.WriteInt32(ControllerOffset + 0x24, RightStick.DY);
Device.Memory.WriteInt64(ControllerOffset + 0x28,
(uint)HidControllerConnState.Controller_State_Connected |
(uint)HidControllerConnState.Controller_State_Wired);
}
public void SetTouchPoints(params HidTouchPoint[] Points)
{
long TouchScreenOffset = HidPosition + HidTouchScreenOffset;
long LastEntry = Device.Memory.ReadInt64(TouchScreenOffset + 0x10);
long CurrEntry = (LastEntry + 1) % HidEntryCount;
long Timestamp = GetTimestamp();
Device.Memory.WriteInt64(TouchScreenOffset + 0x00, Timestamp);
Device.Memory.WriteInt64(TouchScreenOffset + 0x08, HidEntryCount);
Device.Memory.WriteInt64(TouchScreenOffset + 0x10, CurrEntry);
Device.Memory.WriteInt64(TouchScreenOffset + 0x18, HidEntryCount - 1);
Device.Memory.WriteInt64(TouchScreenOffset + 0x20, Timestamp);
long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
TouchEntryOffset += CurrEntry * HidTouchEntrySize;
Device.Memory.WriteInt64(TouchEntryOffset + 0x00, SampleCounter);
Device.Memory.WriteInt64(TouchEntryOffset + 0x08, Points.Length);
TouchEntryOffset += HidTouchEntryHeaderSize;
const int Padding = 0;
int Index = 0;
foreach (HidTouchPoint Point in Points)
{
Device.Memory.WriteInt64(TouchEntryOffset + 0x00, Timestamp);
Device.Memory.WriteInt32(TouchEntryOffset + 0x08, Padding);
Device.Memory.WriteInt32(TouchEntryOffset + 0x0c, Index++);
Device.Memory.WriteInt32(TouchEntryOffset + 0x10, Point.X);
Device.Memory.WriteInt32(TouchEntryOffset + 0x14, Point.Y);
Device.Memory.WriteInt32(TouchEntryOffset + 0x18, Point.DiameterX);
Device.Memory.WriteInt32(TouchEntryOffset + 0x1c, Point.DiameterY);
Device.Memory.WriteInt32(TouchEntryOffset + 0x20, Point.Angle);
Device.Memory.WriteInt32(TouchEntryOffset + 0x24, Padding);
TouchEntryOffset += HidTouchEntryTouchSize;
}
}
private static long GetTimestamp()
{
return PerformanceCounter.ElapsedMilliseconds * 19200;
}
}
}

View file

@ -1,35 +0,0 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerButtons
{
KEY_A = (1 << 0),
KEY_B = (1 << 1),
KEY_X = (1 << 2),
KEY_Y = (1 << 3),
KEY_LSTICK = (1 << 4),
KEY_RSTICK = (1 << 5),
KEY_L = (1 << 6),
KEY_R = (1 << 7),
KEY_ZL = (1 << 8),
KEY_ZR = (1 << 9),
KEY_PLUS = (1 << 10),
KEY_MINUS = (1 << 11),
KEY_DLEFT = (1 << 12),
KEY_DUP = (1 << 13),
KEY_DRIGHT = (1 << 14),
KEY_DDOWN = (1 << 15),
KEY_LSTICK_LEFT = (1 << 16),
KEY_LSTICK_UP = (1 << 17),
KEY_LSTICK_RIGHT = (1 << 18),
KEY_LSTICK_DOWN = (1 << 19),
KEY_RSTICK_LEFT = (1 << 20),
KEY_RSTICK_UP = (1 << 21),
KEY_RSTICK_RIGHT = (1 << 22),
KEY_RSTICK_DOWN = (1 << 23),
KEY_SL = (1 << 24),
KEY_SR = (1 << 25)
}
}

View file

@ -1,14 +0,0 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerType
{
ControllerType_ProController = (1 << 0),
ControllerType_Handheld = (1 << 1),
ControllerType_JoyconPair = (1 << 2),
ControllerType_JoyconLeft = (1 << 3),
ControllerType_JoyconRight = (1 << 4)
}
}

140
Ryujinx.HLE/Input/Hid.cs Normal file
View file

@ -0,0 +1,140 @@
using Ryujinx.Common;
using Ryujinx.HLE.HOS;
namespace Ryujinx.HLE.Input
{
public partial class Hid
{
private Switch Device;
public HidControllerBase PrimaryController { get; private set; }
internal long HidPosition;
public Hid(Switch Device, long HidPosition)
{
this.Device = Device;
this.HidPosition = HidPosition;
Device.Memory.FillWithZeros(HidPosition, Horizon.HidSize);
}
public void InitilizePrimaryController(HidControllerType ControllerType)
{
HidControllerId ControllerId = ControllerType == HidControllerType.Handheld ?
HidControllerId.CONTROLLER_HANDHELD : HidControllerId.CONTROLLER_PLAYER_1;
if (ControllerType == HidControllerType.ProController)
{
PrimaryController = new HidProController(Device);
}
else
{
PrimaryController = new HidNpadController(ControllerType,
Device,
(NpadColor.Body_Neon_Red, NpadColor.Body_Neon_Red),
(NpadColor.Buttons_Neon_Blue, NpadColor.Buttons_Neon_Blue));
}
PrimaryController.Connect(ControllerId);
}
private HidControllerButtons UpdateStickButtons(
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
HidControllerButtons Result = 0;
if (RightStick.DX < 0)
{
Result |= HidControllerButtons.RStickLeft;
}
if (RightStick.DX > 0)
{
Result |= HidControllerButtons.RStickRight;
}
if (RightStick.DY < 0)
{
Result |= HidControllerButtons.RStickDown;
}
if (RightStick.DY > 0)
{
Result |= HidControllerButtons.RStickUp;
}
if (LeftStick.DX < 0)
{
Result |= HidControllerButtons.LStickLeft;
}
if (LeftStick.DX > 0)
{
Result |= HidControllerButtons.LStickRight;
}
if (LeftStick.DY < 0)
{
Result |= HidControllerButtons.LStickDown;
}
if (LeftStick.DY > 0)
{
Result |= HidControllerButtons.LStickUp;
}
return Result;
}
public void SetTouchPoints(params HidTouchPoint[] Points)
{
long TouchScreenOffset = HidPosition + HidTouchScreenOffset;
long LastEntry = Device.Memory.ReadInt64(TouchScreenOffset + 0x10);
long CurrEntry = (LastEntry + 1) % HidEntryCount;
long Timestamp = GetTimestamp();
Device.Memory.WriteInt64(TouchScreenOffset + 0x00, Timestamp);
Device.Memory.WriteInt64(TouchScreenOffset + 0x08, HidEntryCount);
Device.Memory.WriteInt64(TouchScreenOffset + 0x10, CurrEntry);
Device.Memory.WriteInt64(TouchScreenOffset + 0x18, HidEntryCount - 1);
Device.Memory.WriteInt64(TouchScreenOffset + 0x20, Timestamp);
long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
TouchEntryOffset += CurrEntry * HidTouchEntrySize;
Device.Memory.WriteInt64(TouchEntryOffset + 0x00, SampleCounter);
Device.Memory.WriteInt64(TouchEntryOffset + 0x08, Points.Length);
TouchEntryOffset += HidTouchEntryHeaderSize;
const int Padding = 0;
int Index = 0;
foreach (HidTouchPoint Point in Points)
{
Device.Memory.WriteInt64(TouchEntryOffset + 0x00, SampleCounter);
Device.Memory.WriteInt32(TouchEntryOffset + 0x08, Padding);
Device.Memory.WriteInt32(TouchEntryOffset + 0x0c, Index++);
Device.Memory.WriteInt32(TouchEntryOffset + 0x10, Point.X);
Device.Memory.WriteInt32(TouchEntryOffset + 0x14, Point.Y);
Device.Memory.WriteInt32(TouchEntryOffset + 0x18, Point.DiameterX);
Device.Memory.WriteInt32(TouchEntryOffset + 0x1c, Point.DiameterY);
Device.Memory.WriteInt32(TouchEntryOffset + 0x20, Point.Angle);
Device.Memory.WriteInt32(TouchEntryOffset + 0x24, Padding);
TouchEntryOffset += HidTouchEntryTouchSize;
}
}
internal static long GetTimestamp()
{
return PerformanceCounter.ElapsedMilliseconds * 19200;
}
}
}

View file

@ -0,0 +1,76 @@
using static Ryujinx.HLE.Input.Hid;
namespace Ryujinx.HLE.Input
{
public abstract class HidControllerBase : IHidDevice
{
protected HidControllerType HidControllerType;
protected Switch Device;
protected HidControllerId ControllerId;
public long Offset { get; private set; }
public bool Connected { get; protected set; }
public HidControllerBase(HidControllerType ControllerType, Switch Device)
{
this.Device = Device;
HidControllerType = ControllerType;
}
public virtual void Connect(HidControllerId ControllerId)
{
this.ControllerId = ControllerId;
Offset = Device.Hid.HidPosition + HidControllersOffset + (int)ControllerId * HidControllerSize;
Device.Memory.FillWithZeros(Offset, 0x5000);
Device.Memory.WriteInt32(Offset + 0x00, (int)HidControllerType);
}
public abstract void SendInput(
HidControllerButtons Buttons,
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick);
protected long WriteInput(
HidControllerButtons Buttons,
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick,
HidControllerLayouts ControllerLayout)
{
long ControllerOffset = Offset + HidControllerHeaderSize;
ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
long LastEntry = Device.Memory.ReadInt64(ControllerOffset + 0x10);
long CurrEntry = (LastEntry + 1) % HidEntryCount;
long Timestamp = GetTimestamp();
Device.Memory.WriteInt64(ControllerOffset + 0x00, Timestamp);
Device.Memory.WriteInt64(ControllerOffset + 0x08, HidEntryCount);
Device.Memory.WriteInt64(ControllerOffset + 0x10, CurrEntry);
Device.Memory.WriteInt64(ControllerOffset + 0x18, HidEntryCount - 1);
ControllerOffset += HidControllersLayoutHeaderSize;
long LastEntryOffset = ControllerOffset + LastEntry * HidControllersInputEntrySize;
ControllerOffset += CurrEntry * HidControllersInputEntrySize;
long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
Device.Memory.WriteInt64(ControllerOffset + 0x00, SampleCounter);
Device.Memory.WriteInt64(ControllerOffset + 0x08, SampleCounter);
Device.Memory.WriteInt64(ControllerOffset + 0x10, (uint)Buttons);
Device.Memory.WriteInt32(ControllerOffset + 0x18, LeftStick.DX);
Device.Memory.WriteInt32(ControllerOffset + 0x1c, LeftStick.DY);
Device.Memory.WriteInt32(ControllerOffset + 0x20, RightStick.DX);
Device.Memory.WriteInt32(ControllerOffset + 0x24, RightStick.DY);
return ControllerOffset;
}
}
}

View file

@ -0,0 +1,35 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerButtons
{
A = 1 << 0,
B = 1 << 1,
X = 1 << 2,
Y = 1 << 3,
StickLeft = 1 << 4,
StickRight = 1 << 5,
L = 1 << 6,
R = 1 << 7,
Zl = 1 << 8,
Zr = 1 << 9,
Plus = 1 << 10,
Minus = 1 << 11,
DpadLeft = 1 << 12,
DpadUp = 1 << 13,
DPadRight = 1 << 14,
DpadDown = 1 << 15,
LStickLeft = 1 << 16,
LStickUp = 1 << 17,
LStickRight = 1 << 18,
LStickDown = 1 << 19,
RStickLeft = 1 << 20,
RStickUp = 1 << 21,
RStickRight = 1 << 22,
RStickDown = 1 << 23,
Sl = 1 << 24,
Sr = 1 << 25
}
}

View file

@ -0,0 +1,14 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerType
{
ProController = 1 << 0,
Handheld = 1 << 1,
NpadPair = 1 << 2,
NpadLeft = 1 << 3,
NpadRight = 1 << 4
}
}

View file

@ -0,0 +1,86 @@
namespace Ryujinx.HLE.Input
{
public class HidNpadController : HidControllerBase
{
private (NpadColor Left, NpadColor Right) NpadBodyColors;
private (NpadColor Left, NpadColor Right) NpadButtonColors;
private HidControllerLayouts CurrentLayout;
private bool IsHalf;
public HidNpadController(
HidControllerType ControllerType,
Switch Device,
(NpadColor, NpadColor) NpadBodyColors,
(NpadColor, NpadColor) NpadButtonColors) : base(ControllerType, Device)
{
this.NpadBodyColors = NpadBodyColors;
this.NpadButtonColors = NpadButtonColors;
CurrentLayout = HidControllerLayouts.Handheld_Joined;
switch (ControllerType)
{
case HidControllerType.NpadLeft:
CurrentLayout = HidControllerLayouts.Left;
break;
case HidControllerType.NpadRight:
CurrentLayout = HidControllerLayouts.Right;
break;
case HidControllerType.NpadPair:
CurrentLayout = HidControllerLayouts.Joined;
break;
}
}
public override void Connect(HidControllerId ControllerId)
{
if(HidControllerType != HidControllerType.NpadLeft && HidControllerType != HidControllerType.NpadRight)
{
IsHalf = false;
}
base.Connect(CurrentLayout == HidControllerLayouts.Handheld_Joined ? HidControllerId.CONTROLLER_HANDHELD : ControllerId);
HidControllerColorDesc SingleColorDesc =
HidControllerColorDesc.ColorDesc_ColorsNonexistent;
HidControllerColorDesc SplitColorDesc = 0;
NpadColor SingleColorBody = NpadColor.Black;
NpadColor SingleColorButtons = NpadColor.Black;
Device.Memory.WriteInt32(Offset + 0x04, IsHalf ? 1 : 0);
if (IsHalf)
{
Device.Memory.WriteInt32(Offset + 0x08, (int)SingleColorDesc);
Device.Memory.WriteInt32(Offset + 0x0c, (int)SingleColorBody);
Device.Memory.WriteInt32(Offset + 0x10, (int)SingleColorButtons);
Device.Memory.WriteInt32(Offset + 0x14, (int)SplitColorDesc);
}
else
{
Device.Memory.WriteInt32(Offset + 0x18, (int)NpadBodyColors.Left);
Device.Memory.WriteInt32(Offset + 0x1c, (int)NpadButtonColors.Left);
Device.Memory.WriteInt32(Offset + 0x20, (int)NpadBodyColors.Right);
Device.Memory.WriteInt32(Offset + 0x24, (int)NpadButtonColors.Right);
}
Connected = true;
}
public override void SendInput
(HidControllerButtons Buttons,
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
long ControllerOffset = WriteInput(Buttons, LeftStick, RightStick, CurrentLayout);
Device.Memory.WriteInt64(ControllerOffset + 0x28,
(Connected ? (uint)HidControllerConnState.Controller_State_Connected : 0) |
(CurrentLayout == HidControllerLayouts.Handheld_Joined ? (uint)HidControllerConnState.Controller_State_Wired : 0));
}
}
}

View file

@ -0,0 +1,44 @@
namespace Ryujinx.HLE.Input
{
public class HidProController : HidControllerBase
{
bool Wired = false;
public HidProController(Switch Device) : base(HidControllerType.ProController, Device)
{
Wired = true;
}
public override void Connect(HidControllerId ControllerId)
{
base.Connect(ControllerId);
HidControllerColorDesc SingleColorDesc =
HidControllerColorDesc.ColorDesc_ColorsNonexistent;
HidControllerColorDesc SplitColorDesc = 0;
NpadColor SingleColorBody = NpadColor.Black;
NpadColor SingleColorButtons = NpadColor.Black;
Device.Memory.WriteInt32(Offset + 0x08, (int)SingleColorDesc);
Device.Memory.WriteInt32(Offset + 0x0c, (int)SingleColorBody);
Device.Memory.WriteInt32(Offset + 0x10, (int)SingleColorButtons);
Device.Memory.WriteInt32(Offset + 0x14, (int)SplitColorDesc);
Connected = true;
}
public override void SendInput(
HidControllerButtons Buttons,
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
long ControllerOffset = WriteInput(Buttons, LeftStick, RightStick, HidControllerLayouts.Pro_Controller);
Device.Memory.WriteInt64(ControllerOffset + 0x28,
(Connected ? (uint)HidControllerConnState.Controller_State_Connected : 0) |
(Wired ? (uint)HidControllerConnState.Controller_State_Wired : 0));
}
}
}

View file

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.HLE.Input
{
public partial class Hid
{
/*
* Reference:
* https://github.com/reswitched/libtransistor/blob/development/lib/hid.c
* https://github.com/reswitched/libtransistor/blob/development/include/libtransistor/hid.h
* https://github.com/switchbrew/libnx/blob/master/nx/source/services/hid.c
* https://github.com/switchbrew/libnx/blob/master/nx/include/switch/services/hid.h
*/
internal const int HidHeaderSize = 0x400;
internal const int HidTouchScreenSize = 0x3000;
internal const int HidMouseSize = 0x400;
internal const int HidKeyboardSize = 0x400;
internal const int HidUnkSection1Size = 0x400;
internal const int HidUnkSection2Size = 0x400;
internal const int HidUnkSection3Size = 0x400;
internal const int HidUnkSection4Size = 0x400;
internal const int HidUnkSection5Size = 0x200;
internal const int HidUnkSection6Size = 0x200;
internal const int HidUnkSection7Size = 0x200;
internal const int HidUnkSection8Size = 0x800;
internal const int HidControllerSerialsSize = 0x4000;
internal const int HidControllersSize = 0x32000;
internal const int HidUnkSection9Size = 0x800;
internal const int HidTouchHeaderSize = 0x28;
internal const int HidTouchEntrySize = 0x298;
internal const int HidTouchEntryHeaderSize = 0x10;
internal const int HidTouchEntryTouchSize = 0x28;
internal const int HidControllerSize = 0x5000;
internal const int HidControllerHeaderSize = 0x28;
internal const int HidControllerLayoutsSize = 0x350;
internal const int HidControllersLayoutHeaderSize = 0x20;
internal const int HidControllersInputEntrySize = 0x30;
internal const int HidHeaderOffset = 0;
internal const int HidTouchScreenOffset = HidHeaderOffset + HidHeaderSize;
internal const int HidMouseOffset = HidTouchScreenOffset + HidTouchScreenSize;
internal const int HidKeyboardOffset = HidMouseOffset + HidMouseSize;
internal const int HidUnkSection1Offset = HidKeyboardOffset + HidKeyboardSize;
internal const int HidUnkSection2Offset = HidUnkSection1Offset + HidUnkSection1Size;
internal const int HidUnkSection3Offset = HidUnkSection2Offset + HidUnkSection2Size;
internal const int HidUnkSection4Offset = HidUnkSection3Offset + HidUnkSection3Size;
internal const int HidUnkSection5Offset = HidUnkSection4Offset + HidUnkSection4Size;
internal const int HidUnkSection6Offset = HidUnkSection5Offset + HidUnkSection5Size;
internal const int HidUnkSection7Offset = HidUnkSection6Offset + HidUnkSection6Size;
internal const int HidUnkSection8Offset = HidUnkSection7Offset + HidUnkSection7Size;
internal const int HidControllerSerialsOffset = HidUnkSection8Offset + HidUnkSection8Size;
internal const int HidControllersOffset = HidControllerSerialsOffset + HidControllerSerialsSize;
internal const int HidUnkSection9Offset = HidControllersOffset + HidControllersSize;
internal const int HidEntryCount = 17;
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.HLE.Input
{
interface IHidDevice
{
long Offset { get; }
bool Connected { get; }
}
}

View file

@ -1,6 +1,6 @@
namespace Ryujinx.HLE.Input
{
public enum JoyConColor //Thanks to CTCaer
public enum NpadColor //Thanks to CTCaer
{
Black = 0,