2018-03-10 18:39:16 -05:00
|
|
|
|
using ChocolArm64.Memory;
|
2018-04-24 14:57:39 -04:00
|
|
|
|
using Ryujinx.Core.Logging;
|
2018-03-10 18:39:16 -05:00
|
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
2018-03-05 00:09:52 -05:00
|
|
|
|
using System;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Core.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;
|
|
|
|
|
|
2018-04-24 14:57:39 -04:00
|
|
|
|
private Logger Log;
|
|
|
|
|
|
2018-03-05 00:09:52 -05:00
|
|
|
|
private object ShMemLock;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
|
private (AMemory, long)[] ShMemPositions;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-04-24 14:57:39 -04:00
|
|
|
|
public Hid(Logger Log)
|
2018-03-02 20:49:17 -05:00
|
|
|
|
{
|
2018-04-24 14:57:39 -04:00
|
|
|
|
this.Log = Log;
|
|
|
|
|
|
2018-03-05 00:09:52 -05:00
|
|
|
|
ShMemLock = new object();
|
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
|
ShMemPositions = new (AMemory, long)[0];
|
2018-03-02 20:49:17 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 00:09:52 -05:00
|
|
|
|
internal void ShMemMap(object sender, EventArgs e)
|
2018-03-02 20:49:17 -05:00
|
|
|
|
{
|
2018-03-05 00:09:52 -05:00
|
|
|
|
HSharedMem SharedMem = (HSharedMem)sender;
|
|
|
|
|
|
|
|
|
|
lock (ShMemLock)
|
|
|
|
|
{
|
|
|
|
|
ShMemPositions = SharedMem.GetVirtualPositions();
|
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
|
(AMemory Memory, long Position) ShMem = ShMemPositions[ShMemPositions.Length - 1];
|
2018-03-05 00:09:52 -05:00
|
|
|
|
|
2018-04-24 14:57:39 -04:00
|
|
|
|
Log.PrintInfo(LogClass.Hid, $"HID shared memory successfully mapped to 0x{ShMem.Position:x16}!");
|
2018-03-05 00:09:52 -05:00
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
|
Init(ShMem.Memory, ShMem.Position);
|
2018-03-05 00:09:52 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void ShMemUnmap(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
HSharedMem SharedMem = (HSharedMem)sender;
|
|
|
|
|
|
|
|
|
|
lock (ShMemLock)
|
|
|
|
|
{
|
|
|
|
|
ShMemPositions = SharedMem.GetVirtualPositions();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-12 00:04:52 -04:00
|
|
|
|
private void Init(AMemory Memory, long Position)
|
2018-03-05 00:09:52 -05:00
|
|
|
|
{
|
2018-03-02 20:49:17 -05:00
|
|
|
|
InitializeJoyconPair(
|
2018-03-12 00:04:52 -04:00
|
|
|
|
Memory,
|
|
|
|
|
Position,
|
2018-03-02 20:49:17 -05:00
|
|
|
|
JoyConColor.Body_Neon_Red,
|
|
|
|
|
JoyConColor.Buttons_Neon_Red,
|
|
|
|
|
JoyConColor.Body_Neon_Blue,
|
|
|
|
|
JoyConColor.Buttons_Neon_Blue);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 00:09:52 -05:00
|
|
|
|
private void InitializeJoyconPair(
|
2018-03-12 00:04:52 -04:00
|
|
|
|
AMemory Memory,
|
|
|
|
|
long Position,
|
2018-03-02 20:49:17 -05:00
|
|
|
|
JoyConColor LeftColorBody,
|
|
|
|
|
JoyConColor LeftColorButtons,
|
|
|
|
|
JoyConColor RightColorBody,
|
|
|
|
|
JoyConColor RightColorButtons)
|
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
|
long BaseControllerOffset = Position + HidControllersOffset + 8 * HidControllerSize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
|
|
|
|
HidControllerType Type =
|
|
|
|
|
HidControllerType.ControllerType_Handheld |
|
|
|
|
|
HidControllerType.ControllerType_JoyconPair;
|
|
|
|
|
|
|
|
|
|
bool IsHalf = false;
|
|
|
|
|
|
|
|
|
|
HidControllerColorDesc SingleColorDesc =
|
|
|
|
|
HidControllerColorDesc.ColorDesc_ColorsNonexistent;
|
|
|
|
|
|
|
|
|
|
JoyConColor SingleColorBody = JoyConColor.Black;
|
|
|
|
|
JoyConColor SingleColorButtons = JoyConColor.Black;
|
|
|
|
|
|
|
|
|
|
HidControllerColorDesc SplitColorDesc = 0;
|
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x0, (int)Type);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x4, IsHalf ? 1 : 0);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x8, (int)SingleColorDesc);
|
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0xc, (int)SingleColorBody);
|
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x10, (int)SingleColorButtons);
|
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x14, (int)SplitColorDesc);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x18, (int)LeftColorBody);
|
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x20, (int)RightColorBody);
|
|
|
|
|
Memory.WriteInt32Unchecked(BaseControllerOffset + 0x24, (int)RightColorButtons);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetJoyconButton(
|
|
|
|
|
HidControllerId ControllerId,
|
|
|
|
|
HidControllerLayouts ControllerLayout,
|
|
|
|
|
HidControllerButtons Buttons,
|
|
|
|
|
HidJoystickPosition LeftStick,
|
|
|
|
|
HidJoystickPosition RightStick)
|
|
|
|
|
{
|
2018-03-05 00:09:52 -05:00
|
|
|
|
lock (ShMemLock)
|
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
|
foreach ((AMemory Memory, long Position) in ShMemPositions)
|
2018-03-05 00:09:52 -05:00
|
|
|
|
{
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long ControllerOffset = Position + HidControllersOffset;
|
2018-03-05 00:09:52 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
ControllerOffset += (int)ControllerId * HidControllerSize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
ControllerOffset += HidControllerHeaderSize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long LastEntry = Memory.ReadInt64Unchecked(ControllerOffset + 0x10);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long CurrEntry = (LastEntry + 1) % HidEntryCount;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long Timestamp = GetTimestamp();
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x0, Timestamp);
|
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x8, HidEntryCount);
|
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x10, CurrEntry);
|
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x18, HidEntryCount - 1);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
ControllerOffset += HidControllersLayoutHeaderSize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
ControllerOffset += CurrEntry * HidControllersInputEntrySize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x0, Timestamp);
|
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x8, Timestamp);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x10, (uint)Buttons);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt32Unchecked(ControllerOffset + 0x18, LeftStick.DX);
|
|
|
|
|
Memory.WriteInt32Unchecked(ControllerOffset + 0x1c, LeftStick.DY);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x20, RightStick.DX);
|
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x24, RightStick.DY);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt64Unchecked(ControllerOffset + 0x28,
|
|
|
|
|
(uint)HidControllerConnState.Controller_State_Connected |
|
|
|
|
|
(uint)HidControllerConnState.Controller_State_Wired);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-02 20:49:17 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetTouchPoints(params HidTouchPoint[] Points)
|
|
|
|
|
{
|
2018-03-05 00:09:52 -05:00
|
|
|
|
lock (ShMemLock)
|
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
|
foreach ((AMemory Memory, long Position) in ShMemPositions)
|
2018-03-05 00:09:52 -05:00
|
|
|
|
{
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long TouchScreenOffset = Position + HidTouchScreenOffset;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long LastEntry = Memory.ReadInt64Unchecked(TouchScreenOffset + 0x10);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long CurrEntry = (LastEntry + 1) % HidEntryCount;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long Timestamp = GetTimestamp();
|
2018-03-03 22:06:44 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt64Unchecked(TouchScreenOffset + 0x0, Timestamp);
|
|
|
|
|
Memory.WriteInt64Unchecked(TouchScreenOffset + 0x8, HidEntryCount);
|
|
|
|
|
Memory.WriteInt64Unchecked(TouchScreenOffset + 0x10, CurrEntry);
|
|
|
|
|
Memory.WriteInt64Unchecked(TouchScreenOffset + 0x18, HidEntryCount - 1);
|
2018-04-13 16:39:45 -04:00
|
|
|
|
Memory.WriteInt64Unchecked(TouchScreenOffset + 0x20, Timestamp);
|
2018-03-03 22:06:44 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
long SampleCounter = Memory.ReadInt64Unchecked(LastEntryOffset) + 1;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
TouchEntryOffset += CurrEntry * HidTouchEntrySize;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
Memory.WriteInt64Unchecked(TouchEntryOffset + 0x0, SampleCounter);
|
|
|
|
|
Memory.WriteInt64Unchecked(TouchEntryOffset + 0x8, Points.Length);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
TouchEntryOffset += HidTouchEntryHeaderSize;
|
2018-03-05 00:09:52 -05:00
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
|
const int Padding = 0;
|
|
|
|
|
|
|
|
|
|
int Index = 0;
|
|
|
|
|
|
|
|
|
|
foreach (HidTouchPoint Point in Points)
|
|
|
|
|
{
|
|
|
|
|
Memory.WriteInt64Unchecked(TouchEntryOffset + 0x0, Timestamp);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0x8, Padding);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0xc, Index++);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0x10, Point.X);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0x14, Point.Y);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0x18, Point.DiameterX);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0x1c, Point.DiameterY);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0x20, Point.Angle);
|
|
|
|
|
Memory.WriteInt32Unchecked(TouchEntryOffset + 0x24, Padding);
|
|
|
|
|
|
|
|
|
|
TouchEntryOffset += HidTouchEntryTouchSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-02 20:49:17 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-10 18:51:55 -05:00
|
|
|
|
private static long GetTimestamp()
|
2018-03-02 20:49:17 -05:00
|
|
|
|
{
|
2018-03-10 18:51:55 -05:00
|
|
|
|
return (long)((ulong)Environment.TickCount * 19_200);
|
2018-03-02 20:49:17 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|