Little rewrite of HID input (#723)
* change hid sharedmem writing to use structures
This commit is contained in:
parent
1f3a34dd7a
commit
d254548548
42 changed files with 682 additions and 409 deletions
142
Ryujinx.HLE/Input/Controller/BaseController.cs
Normal file
142
Ryujinx.HLE/Input/Controller/BaseController.cs
Normal file
|
@ -0,0 +1,142 @@
|
|||
using static Ryujinx.HLE.Input.Hid;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public abstract class BaseController : IHidDevice
|
||||
{
|
||||
protected ControllerStatus HidControllerType;
|
||||
protected ControllerId ControllerId;
|
||||
|
||||
private long _currentLayoutOffset;
|
||||
private long _mainLayoutOffset;
|
||||
|
||||
protected long DeviceStateOffset => Offset + 0x4188;
|
||||
|
||||
protected Switch Device { get; }
|
||||
|
||||
public long Offset { get; private set; }
|
||||
public bool Connected { get; protected set; }
|
||||
|
||||
public ControllerHeader Header { get; private set; }
|
||||
public ControllerStateHeader CurrentStateHeader { get; private set; }
|
||||
public ControllerDeviceState DeviceState { get; private set; }
|
||||
public ControllerLayouts CurrentLayout { get; private set; }
|
||||
public ControllerState LastInputState { get; set; }
|
||||
public ControllerConnectionState ConnectionState { get; protected set; }
|
||||
|
||||
public BaseController(Switch device, ControllerStatus controllerType)
|
||||
{
|
||||
Device = device;
|
||||
HidControllerType = controllerType;
|
||||
}
|
||||
|
||||
protected void Initialize(
|
||||
bool isHalf,
|
||||
(NpadColor left, NpadColor right) bodyColors,
|
||||
(NpadColor left, NpadColor right) buttonColors,
|
||||
ControllerColorDescription singleColorDesc = 0,
|
||||
ControllerColorDescription splitColorDesc = 0,
|
||||
NpadColor singleBodyColor = 0,
|
||||
NpadColor singleButtonColor = 0
|
||||
)
|
||||
{
|
||||
Header = new ControllerHeader()
|
||||
{
|
||||
IsJoyConHalf = isHalf ? 1 : 0,
|
||||
LeftBodyColor = bodyColors.left,
|
||||
LeftButtonColor = buttonColors.left,
|
||||
RightBodyColor = bodyColors.right,
|
||||
RightButtonColor = buttonColors.right,
|
||||
Status = HidControllerType,
|
||||
SingleBodyColor = singleBodyColor,
|
||||
SingleButtonColor = singleButtonColor,
|
||||
SplitColorDescription = splitColorDesc,
|
||||
SingleColorDescription = singleColorDesc,
|
||||
};
|
||||
|
||||
CurrentStateHeader = new ControllerStateHeader
|
||||
{
|
||||
EntryCount = HidEntryCount,
|
||||
MaxEntryCount = HidEntryCount - 1,
|
||||
CurrentEntryIndex = -1
|
||||
};
|
||||
|
||||
DeviceState = new ControllerDeviceState()
|
||||
{
|
||||
PowerInfo0BatteryState = BatteryState.Percent100,
|
||||
PowerInfo1BatteryState = BatteryState.Percent100,
|
||||
PowerInfo2BatteryState = BatteryState.Percent100,
|
||||
DeviceType = ControllerDeviceType.NPadLeftController | ControllerDeviceType.NPadRightController,
|
||||
DeviceFlags = DeviceFlags.PowerInfo0Connected
|
||||
| DeviceFlags.PowerInfo1Connected
|
||||
| DeviceFlags.PowerInfo2Connected
|
||||
};
|
||||
|
||||
LastInputState = new ControllerState()
|
||||
{
|
||||
SamplesTimestamp = -1,
|
||||
SamplesTimestamp2 = -1
|
||||
};
|
||||
}
|
||||
|
||||
public virtual void Connect(ControllerId controllerId)
|
||||
{
|
||||
ControllerId = controllerId;
|
||||
|
||||
Offset = Device.Hid.HidPosition + HidControllersOffset + (int)controllerId * HidControllerSize;
|
||||
|
||||
_mainLayoutOffset = Offset + HidControllerHeaderSize
|
||||
+ ((int)ControllerLayouts.Main * HidControllerLayoutsSize);
|
||||
|
||||
Device.Memory.FillWithZeros(Offset, 0x5000);
|
||||
Device.Memory.WriteStruct(Offset, Header);
|
||||
Device.Memory.WriteStruct(DeviceStateOffset, DeviceState);
|
||||
|
||||
Connected = true;
|
||||
}
|
||||
|
||||
public void SetLayout(ControllerLayouts controllerLayout)
|
||||
{
|
||||
CurrentLayout = controllerLayout;
|
||||
|
||||
_currentLayoutOffset = Offset + HidControllerHeaderSize
|
||||
+ ((int)controllerLayout * HidControllerLayoutsSize);
|
||||
}
|
||||
|
||||
public void SendInput(
|
||||
ControllerButtons buttons,
|
||||
JoystickPosition leftStick,
|
||||
JoystickPosition rightStick)
|
||||
{
|
||||
ControllerState currentInput = new ControllerState()
|
||||
{
|
||||
SamplesTimestamp = (long)LastInputState.SamplesTimestamp + 1,
|
||||
SamplesTimestamp2 = (long)LastInputState.SamplesTimestamp + 1,
|
||||
ButtonState = buttons,
|
||||
ConnectionState = ConnectionState,
|
||||
LeftStick = leftStick,
|
||||
RightStick = rightStick
|
||||
};
|
||||
|
||||
ControllerStateHeader newInputStateHeader = new ControllerStateHeader
|
||||
{
|
||||
EntryCount = HidEntryCount,
|
||||
MaxEntryCount = HidEntryCount - 1,
|
||||
CurrentEntryIndex = (CurrentStateHeader.CurrentEntryIndex + 1) % HidEntryCount,
|
||||
Timestamp = GetTimestamp(),
|
||||
};
|
||||
|
||||
Device.Memory.WriteStruct(_currentLayoutOffset, newInputStateHeader);
|
||||
Device.Memory.WriteStruct(_mainLayoutOffset, newInputStateHeader);
|
||||
|
||||
long currentInputStateOffset = HidControllersLayoutHeaderSize
|
||||
+ newInputStateHeader.CurrentEntryIndex * HidControllersInputEntrySize;
|
||||
|
||||
Device.Memory.WriteStruct(_currentLayoutOffset + currentInputStateOffset, currentInput);
|
||||
Device.Memory.WriteStruct(_mainLayoutOffset + currentInputStateOffset, currentInput);
|
||||
|
||||
LastInputState = currentInput;
|
||||
CurrentStateHeader = newInputStateHeader;
|
||||
}
|
||||
}
|
||||
}
|
68
Ryujinx.HLE/Input/Controller/NpadController.cs
Normal file
68
Ryujinx.HLE/Input/Controller/NpadController.cs
Normal file
|
@ -0,0 +1,68 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public class NpadController : BaseController
|
||||
{
|
||||
private (NpadColor Left, NpadColor Right) _npadBodyColors;
|
||||
private (NpadColor Left, NpadColor Right) _npadButtonColors;
|
||||
|
||||
private bool _isHalf;
|
||||
|
||||
public NpadController(
|
||||
ControllerStatus controllerStatus,
|
||||
Switch device,
|
||||
(NpadColor, NpadColor) npadBodyColors,
|
||||
(NpadColor, NpadColor) npadButtonColors) : base(device, controllerStatus)
|
||||
{
|
||||
_npadBodyColors = npadBodyColors;
|
||||
_npadButtonColors = npadButtonColors;
|
||||
}
|
||||
|
||||
public override void Connect(ControllerId controllerId)
|
||||
{
|
||||
if (HidControllerType != ControllerStatus.NpadLeft && HidControllerType != ControllerStatus.NpadRight)
|
||||
{
|
||||
_isHalf = false;
|
||||
}
|
||||
|
||||
ConnectionState = ControllerConnectionState.ControllerStateConnected;
|
||||
|
||||
if (controllerId == ControllerId.ControllerHandheld)
|
||||
ConnectionState |= ControllerConnectionState.ControllerStateWired;
|
||||
|
||||
ControllerColorDescription singleColorDesc =
|
||||
ControllerColorDescription.ColorDescriptionColorsNonexistent;
|
||||
|
||||
ControllerColorDescription splitColorDesc = 0;
|
||||
|
||||
NpadColor singleBodyColor = NpadColor.Black;
|
||||
NpadColor singleButtonColor = NpadColor.Black;
|
||||
|
||||
Initialize(_isHalf,
|
||||
(_npadBodyColors.Left, _npadBodyColors.Right),
|
||||
(_npadButtonColors.Left, _npadButtonColors.Right),
|
||||
singleColorDesc,
|
||||
splitColorDesc,
|
||||
singleBodyColor,
|
||||
singleButtonColor );
|
||||
|
||||
base.Connect(controllerId);
|
||||
|
||||
var _currentLayout = ControllerLayouts.HandheldJoined;
|
||||
|
||||
switch (HidControllerType)
|
||||
{
|
||||
case ControllerStatus.NpadLeft:
|
||||
_currentLayout = ControllerLayouts.Left;
|
||||
break;
|
||||
case ControllerStatus.NpadRight:
|
||||
_currentLayout = ControllerLayouts.Right;
|
||||
break;
|
||||
case ControllerStatus.NpadPair:
|
||||
_currentLayout = ControllerLayouts.Joined;
|
||||
break;
|
||||
}
|
||||
|
||||
SetLayout(_currentLayout);
|
||||
}
|
||||
}
|
||||
}
|
42
Ryujinx.HLE/Input/Controller/ProController.cs
Normal file
42
Ryujinx.HLE/Input/Controller/ProController.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public class ProController : BaseController
|
||||
{
|
||||
private bool _wired = false;
|
||||
|
||||
private NpadColor _bodyColor;
|
||||
private NpadColor _buttonColor;
|
||||
|
||||
public ProController(Switch device,
|
||||
NpadColor bodyColor,
|
||||
NpadColor buttonColor) : base(device, ControllerStatus.ProController)
|
||||
{
|
||||
_wired = true;
|
||||
|
||||
_bodyColor = bodyColor;
|
||||
_buttonColor = buttonColor;
|
||||
}
|
||||
|
||||
public override void Connect(ControllerId controllerId)
|
||||
{
|
||||
ControllerColorDescription singleColorDesc =
|
||||
ControllerColorDescription.ColorDescriptionColorsNonexistent;
|
||||
|
||||
ControllerColorDescription splitColorDesc = 0;
|
||||
|
||||
ConnectionState = ControllerConnectionState.ControllerStateConnected | ControllerConnectionState.ControllerStateWired;
|
||||
|
||||
Initialize(false,
|
||||
(0, 0),
|
||||
(0, 0),
|
||||
singleColorDesc,
|
||||
splitColorDesc,
|
||||
_bodyColor,
|
||||
_buttonColor);
|
||||
|
||||
base.Connect(controllerId);
|
||||
|
||||
SetLayout(ControllerLayouts.ProController);
|
||||
}
|
||||
}
|
||||
}
|
12
Ryujinx.HLE/Input/Controller/Types/BatteryState.cs
Normal file
12
Ryujinx.HLE/Input/Controller/Types/BatteryState.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public enum BatteryState : int
|
||||
{
|
||||
// TODO : Check if these are the correct states
|
||||
Percent0 = 0,
|
||||
Percent25 = 1,
|
||||
Percent50 = 2,
|
||||
Percent75 = 3,
|
||||
Percent100 = 4
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@ using System;
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum HidControllerButtons
|
||||
public enum ControllerButtons : long
|
||||
{
|
||||
A = 1 << 0,
|
||||
B = 1 << 1,
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum ControllerColorDescription : int
|
||||
{
|
||||
ColorDescriptionColorsNonexistent = (1 << 1)
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@ using System;
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum HidControllerConnState
|
||||
public enum ControllerConnectionState : long
|
||||
{
|
||||
ControllerStateConnected = (1 << 0),
|
||||
ControllerStateWired = (1 << 1)
|
18
Ryujinx.HLE/Input/Controller/Types/ControllerDeviceState.cs
Normal file
18
Ryujinx.HLE/Input/Controller/Types/ControllerDeviceState.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public unsafe struct ControllerDeviceState
|
||||
{
|
||||
public ControllerDeviceType DeviceType;
|
||||
public int Padding;
|
||||
public DeviceFlags DeviceFlags;
|
||||
public int UnintendedHomeButtonInputProtectionEnabled;
|
||||
public BatteryState PowerInfo0BatteryState;
|
||||
public BatteryState PowerInfo1BatteryState;
|
||||
public BatteryState PowerInfo2BatteryState;
|
||||
public fixed byte ControllerMac[16];
|
||||
public fixed byte ControllerMac2[16];
|
||||
}
|
||||
}
|
12
Ryujinx.HLE/Input/Controller/Types/ControllerDeviceType.cs
Normal file
12
Ryujinx.HLE/Input/Controller/Types/ControllerDeviceType.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum ControllerDeviceType : int
|
||||
{
|
||||
ProController = 1 << 0,
|
||||
NPadLeftController = 1 << 4,
|
||||
NPadRightController = 1 << 5,
|
||||
}
|
||||
}
|
19
Ryujinx.HLE/Input/Controller/Types/ControllerHeader.cs
Normal file
19
Ryujinx.HLE/Input/Controller/Types/ControllerHeader.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct ControllerHeader
|
||||
{
|
||||
public ControllerStatus Status;
|
||||
public int IsJoyConHalf;
|
||||
public ControllerColorDescription SingleColorDescription;
|
||||
public NpadColor SingleBodyColor;
|
||||
public NpadColor SingleButtonColor;
|
||||
public ControllerColorDescription SplitColorDescription;
|
||||
public NpadColor RightBodyColor;
|
||||
public NpadColor RightButtonColor;
|
||||
public NpadColor LeftBodyColor;
|
||||
public NpadColor LeftButtonColor;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public enum HidControllerId
|
||||
public enum ControllerId
|
||||
{
|
||||
ControllerPlayer1 = 0,
|
||||
ControllerPlayer2 = 1,
|
|
@ -1,6 +1,6 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public enum HidControllerLayouts
|
||||
public enum ControllerLayouts
|
||||
{
|
||||
ProController = 0,
|
||||
HandheldJoined = 1,
|
15
Ryujinx.HLE/Input/Controller/Types/ControllerState.cs
Normal file
15
Ryujinx.HLE/Input/Controller/Types/ControllerState.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct ControllerState
|
||||
{
|
||||
public long SamplesTimestamp;
|
||||
public long SamplesTimestamp2;
|
||||
public ControllerButtons ButtonState;
|
||||
public JoystickPosition LeftStick;
|
||||
public JoystickPosition RightStick;
|
||||
public ControllerConnectionState ConnectionState;
|
||||
}
|
||||
}
|
13
Ryujinx.HLE/Input/Controller/Types/ControllerStateHeader.cs
Normal file
13
Ryujinx.HLE/Input/Controller/Types/ControllerStateHeader.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct ControllerStateHeader
|
||||
{
|
||||
public long Timestamp;
|
||||
public long EntryCount;
|
||||
public long CurrentEntryIndex;
|
||||
public long MaxEntryCount;
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@ using System;
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum HidControllerType
|
||||
public enum ControllerStatus : int
|
||||
{
|
||||
ProController = 1 << 0,
|
||||
Handheld = 1 << 1,
|
22
Ryujinx.HLE/Input/Controller/Types/DeviceFlags.cs
Normal file
22
Ryujinx.HLE/Input/Controller/Types/DeviceFlags.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum DeviceFlags : long
|
||||
{
|
||||
PowerInfo0Charging = 1 << 0,
|
||||
PowerInfo1Charging = 1 << 1,
|
||||
PowerInfo2Charging = 1 << 2,
|
||||
PowerInfo0Connected = 1 << 3,
|
||||
PowerInfo1Connected = 1 << 4,
|
||||
PowerInfo2Connected = 1 << 5,
|
||||
UnsupportedButtonPressedNpadSystem = 1 << 9,
|
||||
UnsupportedButtonPressedNpadSystemExt = 1 << 10,
|
||||
AbxyButtonOriented = 1 << 11,
|
||||
SlSrButtonOriented = 1 << 12,
|
||||
PlusButtonCapability = 1 << 13,
|
||||
MinusButtonCapability = 1 << 14,
|
||||
DirectionalButtonsSupported = 1 << 15
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum HidHotkeyButtons
|
||||
public enum HotkeyButtons
|
||||
{
|
||||
ToggleVSync = 1 << 0,
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public struct HidJoystickPosition
|
||||
public struct JoystickPosition
|
||||
{
|
||||
public int Dx;
|
||||
public int Dy;
|
|
@ -1,6 +1,6 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public enum NpadColor //Thanks to CTCaer
|
||||
public enum NpadColor : int //Thanks to CTCaer
|
||||
{
|
||||
Black = 0,
|
||||
|
|
@ -7,7 +7,15 @@ namespace Ryujinx.HLE.Input
|
|||
{
|
||||
private Switch _device;
|
||||
|
||||
public HidControllerBase PrimaryController { get; private set; }
|
||||
private long _touchScreenOffset;
|
||||
private long _touchEntriesOffset;
|
||||
private long _keyboardOffset;
|
||||
|
||||
private TouchHeader _currentTouchHeader;
|
||||
private KeyboardHeader _currentKeyboardHeader;
|
||||
private KeyboardEntry _currentKeyboardEntry;
|
||||
|
||||
public BaseController PrimaryController { get; private set; }
|
||||
|
||||
internal long HidPosition;
|
||||
|
||||
|
@ -17,22 +25,42 @@ namespace Ryujinx.HLE.Input
|
|||
HidPosition = hidPosition;
|
||||
|
||||
device.Memory.FillWithZeros(hidPosition, Horizon.HidSize);
|
||||
|
||||
_currentTouchHeader = new TouchHeader()
|
||||
{
|
||||
CurrentEntryIndex = -1,
|
||||
};
|
||||
|
||||
_currentKeyboardHeader = new KeyboardHeader()
|
||||
{
|
||||
CurrentEntryIndex = -1,
|
||||
};
|
||||
|
||||
_currentKeyboardEntry = new KeyboardEntry()
|
||||
{
|
||||
SamplesTimestamp = -1,
|
||||
SamplesTimestamp2 = -1
|
||||
};
|
||||
|
||||
_touchScreenOffset = HidPosition + HidTouchScreenOffset;
|
||||
_touchEntriesOffset = _touchScreenOffset + HidTouchHeaderSize;
|
||||
_keyboardOffset = HidPosition + HidKeyboardOffset;
|
||||
}
|
||||
|
||||
public void InitializePrimaryController(HidControllerType controllerType)
|
||||
public void InitializePrimaryController(ControllerStatus controllerType)
|
||||
{
|
||||
HidControllerId controllerId = controllerType == HidControllerType.Handheld ?
|
||||
HidControllerId.ControllerHandheld : HidControllerId.ControllerPlayer1;
|
||||
ControllerId controllerId = controllerType == ControllerStatus.Handheld ?
|
||||
ControllerId.ControllerHandheld : ControllerId.ControllerPlayer1;
|
||||
|
||||
if (controllerType == HidControllerType.ProController)
|
||||
if (controllerType == ControllerStatus.ProController)
|
||||
{
|
||||
PrimaryController = new HidProController(_device);
|
||||
PrimaryController = new ProController(_device, NpadColor.Black, NpadColor.Black);
|
||||
}
|
||||
else
|
||||
{
|
||||
PrimaryController = new HidNpadController(controllerType,
|
||||
PrimaryController = new NpadController(controllerType,
|
||||
_device,
|
||||
(NpadColor.BodyNeonRed, NpadColor.BodyNeonRed),
|
||||
(NpadColor.BodyNeonRed, NpadColor.BodyNeonRed),
|
||||
(NpadColor.ButtonsNeonBlue, NpadColor.ButtonsNeonBlue));
|
||||
}
|
||||
|
||||
|
@ -44,124 +72,132 @@ namespace Ryujinx.HLE.Input
|
|||
_device.Memory.FillWithZeros(HidPosition + HidKeyboardOffset, HidKeyboardSize);
|
||||
}
|
||||
|
||||
public HidControllerButtons UpdateStickButtons(
|
||||
HidJoystickPosition leftStick,
|
||||
HidJoystickPosition rightStick)
|
||||
public ControllerButtons UpdateStickButtons(
|
||||
JoystickPosition leftStick,
|
||||
JoystickPosition rightStick)
|
||||
{
|
||||
HidControllerButtons result = 0;
|
||||
ControllerButtons result = 0;
|
||||
|
||||
if (rightStick.Dx < 0)
|
||||
{
|
||||
result |= HidControllerButtons.RStickLeft;
|
||||
result |= ControllerButtons.RStickLeft;
|
||||
}
|
||||
|
||||
if (rightStick.Dx > 0)
|
||||
{
|
||||
result |= HidControllerButtons.RStickRight;
|
||||
result |= ControllerButtons.RStickRight;
|
||||
}
|
||||
|
||||
if (rightStick.Dy < 0)
|
||||
{
|
||||
result |= HidControllerButtons.RStickDown;
|
||||
result |= ControllerButtons.RStickDown;
|
||||
}
|
||||
|
||||
if (rightStick.Dy > 0)
|
||||
{
|
||||
result |= HidControllerButtons.RStickUp;
|
||||
result |= ControllerButtons.RStickUp;
|
||||
}
|
||||
|
||||
if (leftStick.Dx < 0)
|
||||
{
|
||||
result |= HidControllerButtons.LStickLeft;
|
||||
result |= ControllerButtons.LStickLeft;
|
||||
}
|
||||
|
||||
if (leftStick.Dx > 0)
|
||||
{
|
||||
result |= HidControllerButtons.LStickRight;
|
||||
result |= ControllerButtons.LStickRight;
|
||||
}
|
||||
|
||||
if (leftStick.Dy < 0)
|
||||
{
|
||||
result |= HidControllerButtons.LStickDown;
|
||||
result |= ControllerButtons.LStickDown;
|
||||
}
|
||||
|
||||
if (leftStick.Dy > 0)
|
||||
{
|
||||
result |= HidControllerButtons.LStickUp;
|
||||
result |= ControllerButtons.LStickUp;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void SetTouchPoints(params HidTouchPoint[] points)
|
||||
public void SetTouchPoints(params TouchPoint[] points)
|
||||
{
|
||||
long touchScreenOffset = HidPosition + HidTouchScreenOffset;
|
||||
long lastEntry = _device.Memory.ReadInt64(touchScreenOffset + 0x10);
|
||||
long currEntry = (lastEntry + 1) % HidEntryCount;
|
||||
long timestamp = GetTimestamp();
|
||||
long timestamp = GetTimestamp();
|
||||
long sampleCounter = _currentTouchHeader.SamplesTimestamp + 1;
|
||||
|
||||
_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)
|
||||
var newTouchHeader = new TouchHeader
|
||||
{
|
||||
_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);
|
||||
CurrentEntryIndex = (_currentTouchHeader.CurrentEntryIndex + 1) % HidEntryCount,
|
||||
EntryCount = HidEntryCount,
|
||||
MaxEntries = HidEntryCount - 1,
|
||||
SamplesTimestamp = sampleCounter,
|
||||
Timestamp = timestamp,
|
||||
};
|
||||
|
||||
touchEntryOffset += HidTouchEntryTouchSize;
|
||||
long currentTouchEntryOffset = _touchEntriesOffset + newTouchHeader.CurrentEntryIndex * HidTouchEntrySize;
|
||||
|
||||
TouchEntry touchEntry = new TouchEntry()
|
||||
{
|
||||
SamplesTimestamp = sampleCounter,
|
||||
TouchCount = points.Length
|
||||
};
|
||||
|
||||
_device.Memory.WriteStruct(currentTouchEntryOffset, touchEntry);
|
||||
|
||||
currentTouchEntryOffset += HidTouchEntryHeaderSize;
|
||||
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
TouchData touch = new TouchData()
|
||||
{
|
||||
Angle = points[i].Angle,
|
||||
DiameterX = points[i].DiameterX,
|
||||
DiameterY = points[i].DiameterY,
|
||||
Index = i,
|
||||
SampleTimestamp = sampleCounter,
|
||||
X = points[i].X,
|
||||
Y = points[i].Y
|
||||
};
|
||||
|
||||
_device.Memory.WriteStruct(currentTouchEntryOffset, touch);
|
||||
|
||||
currentTouchEntryOffset += HidTouchEntryTouchSize;
|
||||
}
|
||||
|
||||
_device.Memory.WriteStruct(_touchScreenOffset, newTouchHeader);
|
||||
|
||||
_currentTouchHeader = newTouchHeader;
|
||||
}
|
||||
|
||||
public void WriteKeyboard(HidKeyboard keyboard)
|
||||
public unsafe void WriteKeyboard(Keyboard keyboard)
|
||||
{
|
||||
long keyboardOffset = HidPosition + HidKeyboardOffset;
|
||||
long lastEntry = _device.Memory.ReadInt64(keyboardOffset + 0x10);
|
||||
long currEntry = (lastEntry + 1) % HidEntryCount;
|
||||
long timestamp = GetTimestamp();
|
||||
long timestamp = GetTimestamp();
|
||||
|
||||
_device.Memory.WriteInt64(keyboardOffset + 0x00, timestamp);
|
||||
_device.Memory.WriteInt64(keyboardOffset + 0x08, HidEntryCount);
|
||||
_device.Memory.WriteInt64(keyboardOffset + 0x10, currEntry);
|
||||
_device.Memory.WriteInt64(keyboardOffset + 0x18, HidEntryCount - 1);
|
||||
|
||||
long keyboardEntryOffset = keyboardOffset + HidKeyboardHeaderSize;
|
||||
long lastEntryOffset = keyboardEntryOffset + lastEntry * HidKeyboardEntrySize;
|
||||
long sampleCounter = _device.Memory.ReadInt64(lastEntryOffset);
|
||||
|
||||
keyboardEntryOffset += currEntry * HidKeyboardEntrySize;
|
||||
_device.Memory.WriteInt64(keyboardEntryOffset + 0x00, sampleCounter + 1);
|
||||
_device.Memory.WriteInt64(keyboardEntryOffset + 0x08, sampleCounter);
|
||||
_device.Memory.WriteInt64(keyboardEntryOffset + 0x10, keyboard.Modifier);
|
||||
|
||||
for (int i = 0; i < keyboard.Keys.Length; i++)
|
||||
var newKeyboardHeader = new KeyboardHeader()
|
||||
{
|
||||
_device.Memory.WriteInt32(keyboardEntryOffset + 0x18 + (i * 4), keyboard.Keys[i]);
|
||||
}
|
||||
CurrentEntryIndex = (_currentKeyboardHeader.CurrentEntryIndex + 1) % HidEntryCount,
|
||||
EntryCount = HidEntryCount,
|
||||
MaxEntries = HidEntryCount - 1,
|
||||
Timestamp = timestamp,
|
||||
};
|
||||
|
||||
_device.Memory.WriteStruct(_keyboardOffset, newKeyboardHeader);
|
||||
|
||||
long keyboardEntryOffset = _keyboardOffset + HidKeyboardHeaderSize;
|
||||
keyboardEntryOffset += newKeyboardHeader.CurrentEntryIndex * HidKeyboardEntrySize;
|
||||
|
||||
var newkeyboardEntry = new KeyboardEntry()
|
||||
{
|
||||
SamplesTimestamp = _currentKeyboardEntry.SamplesTimestamp + 1,
|
||||
SamplesTimestamp2 = _currentKeyboardEntry.SamplesTimestamp2 + 1,
|
||||
Keys = keyboard.Keys,
|
||||
Modifier = keyboard.Modifier,
|
||||
};
|
||||
|
||||
_device.Memory.WriteStruct(keyboardEntryOffset, newkeyboardEntry);
|
||||
|
||||
_currentKeyboardEntry = newkeyboardEntry;
|
||||
_currentKeyboardHeader = newKeyboardHeader;
|
||||
}
|
||||
|
||||
internal static long GetTimestamp()
|
||||
|
|
|
@ -1,76 +0,0 @@
|
|||
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)
|
||||
{
|
||||
Device = device;
|
||||
|
||||
HidControllerType = controllerType;
|
||||
}
|
||||
|
||||
public virtual void Connect(HidControllerId controllerId)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum HidControllerColorDesc
|
||||
{
|
||||
ColorDescColorsNonexistent = (1 << 1)
|
||||
}
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
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)
|
||||
{
|
||||
_npadBodyColors = npadBodyColors;
|
||||
_npadButtonColors = npadButtonColors;
|
||||
|
||||
_currentLayout = HidControllerLayouts.HandheldJoined;
|
||||
|
||||
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.HandheldJoined ? HidControllerId.ControllerHandheld : controllerId);
|
||||
|
||||
HidControllerColorDesc singleColorDesc =
|
||||
HidControllerColorDesc.ColorDescColorsNonexistent;
|
||||
|
||||
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.ControllerStateConnected : 0) |
|
||||
(_currentLayout == HidControllerLayouts.HandheldJoined ? (uint)HidControllerConnState.ControllerStateWired : 0));
|
||||
|
||||
controllerOffset = WriteInput(buttons, leftStick, rightStick, HidControllerLayouts.Main);
|
||||
|
||||
Device.Memory.WriteInt64(controllerOffset + 0x28,
|
||||
(Connected ? (uint)HidControllerConnState.ControllerStateWired : 0) |
|
||||
(uint)HidControllerConnState.ControllerStateWired);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
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.ColorDescColorsNonexistent;
|
||||
|
||||
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.ProController);
|
||||
|
||||
Device.Memory.WriteInt64(controllerOffset + 0x28,
|
||||
(Connected ? (uint)HidControllerConnState.ControllerStateConnected : 0) |
|
||||
(_wired ? (uint)HidControllerConnState.ControllerStateWired : 0));
|
||||
|
||||
controllerOffset = WriteInput(buttons, leftStick, rightStick, HidControllerLayouts.Main);
|
||||
|
||||
Device.Memory.WriteInt64(controllerOffset + 0x28,
|
||||
(Connected ? (uint)HidControllerConnState.ControllerStateWired : 0) |
|
||||
(uint)HidControllerConnState.ControllerStateWired);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public struct HidKeyboard
|
||||
public struct Keyboard
|
||||
{
|
||||
public int Modifier;
|
||||
public int[] Keys;
|
15
Ryujinx.HLE/Input/Keyboard/KeyboardEntry.cs
Normal file
15
Ryujinx.HLE/Input/Keyboard/KeyboardEntry.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct KeyboardEntry
|
||||
{
|
||||
public long SamplesTimestamp;
|
||||
public long SamplesTimestamp2;
|
||||
public long Modifier;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValArray , SizeConst = 0x8)]
|
||||
public int[] Keys;
|
||||
}
|
||||
}
|
13
Ryujinx.HLE/Input/Keyboard/KeyboardHeader.cs
Normal file
13
Ryujinx.HLE/Input/Keyboard/KeyboardHeader.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct KeyboardHeader
|
||||
{
|
||||
public long Timestamp;
|
||||
public long EntryCount;
|
||||
public long CurrentEntryIndex;
|
||||
public long MaxEntries;
|
||||
}
|
||||
}
|
18
Ryujinx.HLE/Input/Touch/TouchData.cs
Normal file
18
Ryujinx.HLE/Input/Touch/TouchData.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct TouchData
|
||||
{
|
||||
public long SampleTimestamp;
|
||||
public int Padding;
|
||||
public int Index;
|
||||
public int X;
|
||||
public int Y;
|
||||
public int DiameterX;
|
||||
public int DiameterY;
|
||||
public int Angle;
|
||||
public int Padding2;
|
||||
}
|
||||
}
|
11
Ryujinx.HLE/Input/Touch/TouchEntry.cs
Normal file
11
Ryujinx.HLE/Input/Touch/TouchEntry.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public unsafe struct TouchEntry
|
||||
{
|
||||
public long SamplesTimestamp;
|
||||
public long TouchCount;
|
||||
}
|
||||
}
|
14
Ryujinx.HLE/Input/Touch/TouchHeader.cs
Normal file
14
Ryujinx.HLE/Input/Touch/TouchHeader.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct TouchHeader
|
||||
{
|
||||
public long Timestamp;
|
||||
public long EntryCount;
|
||||
public long CurrentEntryIndex;
|
||||
public long MaxEntries;
|
||||
public long SamplesTimestamp;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
namespace Ryujinx.HLE.Input
|
||||
{
|
||||
public struct HidTouchPoint
|
||||
public struct TouchPoint
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
Loading…
Add table
Add a link
Reference in a new issue