2018-11-20 00:01:36 +00:00
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using Ryujinx.HLE.HOS;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Input
|
|
|
|
|
{
|
|
|
|
|
public partial class Hid
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private Switch _device;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
|
|
|
|
public HidControllerBase PrimaryController { get; private set; }
|
|
|
|
|
|
|
|
|
|
internal long HidPosition;
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
public Hid(Switch device, long hidPosition)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_device = device;
|
|
|
|
|
HidPosition = hidPosition;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
device.Memory.FillWithZeros(hidPosition, Horizon.HidSize);
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
public void InitilizePrimaryController(HidControllerType controllerType)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
HidControllerId controllerId = controllerType == HidControllerType.Handheld ?
|
|
|
|
|
HidControllerId.ControllerHandheld : HidControllerId.ControllerPlayer1;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (controllerType == HidControllerType.ProController)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
PrimaryController = new HidProController(_device);
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
PrimaryController = new HidNpadController(controllerType,
|
|
|
|
|
_device,
|
|
|
|
|
(NpadColor.BodyNeonRed, NpadColor.BodyNeonRed),
|
|
|
|
|
(NpadColor.ButtonsNeonBlue, NpadColor.ButtonsNeonBlue));
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
PrimaryController.Connect(controllerId);
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 23:29:01 +00:00
|
|
|
|
public void InitilizeKeyboard()
|
|
|
|
|
{
|
|
|
|
|
_device.Memory.FillWithZeros(HidPosition + HidKeyboardOffset, HidKeyboardSize);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 20:59:38 +00:00
|
|
|
|
public HidControllerButtons UpdateStickButtons(
|
2018-12-06 11:16:24 +00:00
|
|
|
|
HidJoystickPosition leftStick,
|
|
|
|
|
HidJoystickPosition rightStick)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
HidControllerButtons result = 0;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (rightStick.Dx < 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.RStickLeft;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (rightStick.Dx > 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.RStickRight;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (rightStick.Dy < 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.RStickDown;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (rightStick.Dy > 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.RStickUp;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (leftStick.Dx < 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.LStickLeft;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (leftStick.Dx > 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.LStickRight;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (leftStick.Dy < 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.LStickDown;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
if (leftStick.Dy > 0)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
result |= HidControllerButtons.LStickUp;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
return result;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
public void SetTouchPoints(params HidTouchPoint[] points)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
long touchScreenOffset = HidPosition + HidTouchScreenOffset;
|
|
|
|
|
long lastEntry = _device.Memory.ReadInt64(touchScreenOffset + 0x10);
|
|
|
|
|
long currEntry = (lastEntry + 1) % HidEntryCount;
|
|
|
|
|
long timestamp = GetTimestamp();
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_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);
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
long touchEntryOffset = touchScreenOffset + HidTouchHeaderSize;
|
|
|
|
|
long lastEntryOffset = touchEntryOffset + lastEntry * HidTouchEntrySize;
|
|
|
|
|
long sampleCounter = _device.Memory.ReadInt64(lastEntryOffset) + 1;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
touchEntryOffset += currEntry * HidTouchEntrySize;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_device.Memory.WriteInt64(touchEntryOffset + 0x00, sampleCounter);
|
|
|
|
|
_device.Memory.WriteInt64(touchEntryOffset + 0x08, points.Length);
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
touchEntryOffset += HidTouchEntryHeaderSize;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
const int padding = 0;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
int index = 0;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
foreach (HidTouchPoint point in points)
|
2018-11-20 00:01:36 +00:00
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_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;
|
2018-11-20 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 23:29:01 +00:00
|
|
|
|
public void WriteKeyboard(HidKeyboard keyboard)
|
|
|
|
|
{
|
|
|
|
|
long keyboardOffset = HidPosition + HidKeyboardOffset;
|
|
|
|
|
long lastEntry = _device.Memory.ReadInt64(keyboardOffset + 0x10);
|
|
|
|
|
long currEntry = (lastEntry + 1) % HidEntryCount;
|
|
|
|
|
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++)
|
|
|
|
|
{
|
|
|
|
|
_device.Memory.WriteInt32(keyboardEntryOffset + 0x18 + (i * 4), keyboard.Keys[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 00:01:36 +00:00
|
|
|
|
internal static long GetTimestamp()
|
|
|
|
|
{
|
|
|
|
|
return PerformanceCounter.ElapsedMilliseconds * 19200;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|