Ryujinx/Ryujinx.HLE/Input/HidBaseController.cs

77 lines
2.8 KiB
C#
Raw Normal View History

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; }
2018-12-01 21:01:59 +01:00
public HidControllerBase(HidControllerType controllerType, Switch device)
{
2018-12-01 21:24:37 +01:00
Device = device;
2018-12-01 21:01:59 +01:00
HidControllerType = controllerType;
}
2018-12-01 21:01:59 +01:00
public virtual void Connect(HidControllerId controllerId)
{
2018-12-01 21:24:37 +01:00
ControllerId = controllerId;
2018-12-01 21:01:59 +01:00
Offset = Device.Hid.HidPosition + HidControllersOffset + (int)controllerId * HidControllerSize;
Device.Memory.FillWithZeros(Offset, 0x5000);
Device.Memory.WriteInt32(Offset + 0x00, (int)HidControllerType);
}
public abstract void SendInput(
2018-12-01 21:01:59 +01:00
HidControllerButtons buttons,
HidJoystickPosition leftStick,
HidJoystickPosition rightStick);
protected long WriteInput(
2018-12-01 21:01:59 +01:00
HidControllerButtons buttons,
HidJoystickPosition leftStick,
HidJoystickPosition rightStick,
HidControllerLayouts controllerLayout)
{
2018-12-01 21:01:59 +01:00
long controllerOffset = Offset + HidControllerHeaderSize;
2018-12-01 21:01:59 +01:00
controllerOffset += (int)controllerLayout * HidControllerLayoutsSize;
2018-12-01 21:01:59 +01:00
long lastEntry = Device.Memory.ReadInt64(controllerOffset + 0x10);
long currEntry = (lastEntry + 1) % HidEntryCount;
long timestamp = GetTimestamp();
2018-12-01 21:01:59 +01:00
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);
2018-12-01 21:01:59 +01:00
controllerOffset += HidControllersLayoutHeaderSize;
2018-12-01 21:01:59 +01:00
long lastEntryOffset = controllerOffset + lastEntry * HidControllersInputEntrySize;
2018-12-01 21:01:59 +01:00
controllerOffset += currEntry * HidControllersInputEntrySize;
2018-12-01 21:01:59 +01:00
long sampleCounter = Device.Memory.ReadInt64(lastEntryOffset) + 1;
2018-12-01 21:01:59 +01:00
Device.Memory.WriteInt64(controllerOffset + 0x00, sampleCounter);
Device.Memory.WriteInt64(controllerOffset + 0x08, sampleCounter);
Device.Memory.WriteInt64(controllerOffset + 0x10, (uint)buttons);
2018-12-01 21:01:59 +01:00
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);
2018-12-01 21:01:59 +01:00
return controllerOffset;
}
}
}