aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Hid
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2018-06-10 21:46:42 -0300
committergdkchan <gab.dark.100@gmail.com>2018-06-10 21:46:42 -0300
commit76f3b1b3a4637ec72abfbb8cbc0679f2e0ca838f (patch)
tree0411b709de31c1c0517763512df8eeb9f7491bc9 /Ryujinx.HLE/Hid
parent518fe799da6dd4f12c58c9e6e174767effb0b868 (diff)
Rename Ryujinx.Core to Ryujinx.HLE and add a separate project for a future LLE implementation
Diffstat (limited to 'Ryujinx.HLE/Hid')
-rw-r--r--Ryujinx.HLE/Hid/Hid.cs279
-rw-r--r--Ryujinx.HLE/Hid/HidControllerButtons.cs35
-rw-r--r--Ryujinx.HLE/Hid/HidControllerColorDesc.cs10
-rw-r--r--Ryujinx.HLE/Hid/HidControllerConnState.cs11
-rw-r--r--Ryujinx.HLE/Hid/HidControllerId.cs16
-rw-r--r--Ryujinx.HLE/Hid/HidControllerLayouts.cs13
-rw-r--r--Ryujinx.HLE/Hid/HidControllerType.cs14
-rw-r--r--Ryujinx.HLE/Hid/HidJoystickPosition.cs8
-rw-r--r--Ryujinx.HLE/Hid/HidTouchPoint.cs11
-rw-r--r--Ryujinx.HLE/Hid/JoyCon.cs45
-rw-r--r--Ryujinx.HLE/Hid/JoyConColor.cs23
11 files changed, 465 insertions, 0 deletions
diff --git a/Ryujinx.HLE/Hid/Hid.cs b/Ryujinx.HLE/Hid/Hid.cs
new file mode 100644
index 00000000..054c4fac
--- /dev/null
+++ b/Ryujinx.HLE/Hid/Hid.cs
@@ -0,0 +1,279 @@
+using ChocolArm64.Memory;
+using Ryujinx.HLE.Logging;
+using Ryujinx.HLE.OsHle;
+using Ryujinx.HLE.OsHle.Handles;
+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 Logger Log;
+
+ private object ShMemLock;
+
+ private (AMemory, long)[] ShMemPositions;
+
+ public Hid(Logger Log)
+ {
+ this.Log = Log;
+
+ ShMemLock = new object();
+
+ ShMemPositions = new (AMemory, long)[0];
+ }
+
+ internal void ShMemMap(object sender, EventArgs e)
+ {
+ HSharedMem SharedMem = (HSharedMem)sender;
+
+ lock (ShMemLock)
+ {
+ ShMemPositions = SharedMem.GetVirtualPositions();
+
+ (AMemory Memory, long Position) = ShMemPositions[ShMemPositions.Length - 1];
+
+ for (long Offset = 0; Offset < Horizon.HidSize; Offset += 8)
+ {
+ Memory.WriteInt64Unchecked(Position + Offset, 0);
+ }
+
+ Log.PrintInfo(LogClass.Hid, $"HID shared memory successfully mapped to 0x{Position:x16}!");
+
+ Init(Memory, Position);
+ }
+ }
+
+ internal void ShMemUnmap(object sender, EventArgs e)
+ {
+ HSharedMem SharedMem = (HSharedMem)sender;
+
+ lock (ShMemLock)
+ {
+ ShMemPositions = SharedMem.GetVirtualPositions();
+ }
+ }
+
+ private void Init(AMemory Memory, long Position)
+ {
+ InitializeJoyconPair(
+ Memory,
+ Position,
+ JoyConColor.Body_Neon_Red,
+ JoyConColor.Buttons_Neon_Red,
+ JoyConColor.Body_Neon_Blue,
+ JoyConColor.Buttons_Neon_Blue);
+ }
+
+ private void InitializeJoyconPair(
+ AMemory Memory,
+ long Position,
+ JoyConColor LeftColorBody,
+ JoyConColor LeftColorButtons,
+ JoyConColor RightColorBody,
+ JoyConColor RightColorButtons)
+ {
+ long BaseControllerOffset = Position + HidControllersOffset + 8 * HidControllerSize;
+
+ 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;
+
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x0, (int)Type);
+
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x4, IsHalf ? 1 : 0);
+
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x8, (int)SingleColorDesc);
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0xc, (int)SingleColorBody);
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x10, (int)SingleColorButtons);
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x14, (int)SplitColorDesc);
+
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x18, (int)LeftColorBody);
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
+
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x20, (int)RightColorBody);
+ Memory.WriteInt32Unchecked(BaseControllerOffset + 0x24, (int)RightColorButtons);
+ }
+
+ public void SetJoyconButton(
+ HidControllerId ControllerId,
+ HidControllerLayouts ControllerLayout,
+ HidControllerButtons Buttons,
+ HidJoystickPosition LeftStick,
+ HidJoystickPosition RightStick)
+ {
+ lock (ShMemLock)
+ {
+ foreach ((AMemory Memory, long Position) in ShMemPositions)
+ {
+ long ControllerOffset = Position + HidControllersOffset;
+
+ ControllerOffset += (int)ControllerId * HidControllerSize;
+
+ ControllerOffset += HidControllerHeaderSize;
+
+ ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
+
+ long LastEntry = Memory.ReadInt64Unchecked(ControllerOffset + 0x10);
+
+ long CurrEntry = (LastEntry + 1) % HidEntryCount;
+
+ long Timestamp = GetTimestamp();
+
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x0, Timestamp);
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x8, HidEntryCount);
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x10, CurrEntry);
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x18, HidEntryCount - 1);
+
+ ControllerOffset += HidControllersLayoutHeaderSize;
+
+ long LastEntryOffset = ControllerOffset + LastEntry * HidControllersInputEntrySize;
+
+ ControllerOffset += CurrEntry * HidControllersInputEntrySize;
+
+ long SampleCounter = Memory.ReadInt64Unchecked(LastEntryOffset) + 1;
+
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x0, SampleCounter);
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x8, SampleCounter);
+
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x10, (uint)Buttons);
+
+ Memory.WriteInt32Unchecked(ControllerOffset + 0x18, LeftStick.DX);
+ Memory.WriteInt32Unchecked(ControllerOffset + 0x1c, LeftStick.DY);
+
+ Memory.WriteInt32Unchecked(ControllerOffset + 0x20, RightStick.DX);
+ Memory.WriteInt32Unchecked(ControllerOffset + 0x24, RightStick.DY);
+
+ Memory.WriteInt64Unchecked(ControllerOffset + 0x28,
+ (uint)HidControllerConnState.Controller_State_Connected |
+ (uint)HidControllerConnState.Controller_State_Wired);
+ }
+ }
+ }
+
+ public void SetTouchPoints(params HidTouchPoint[] Points)
+ {
+ lock (ShMemLock)
+ {
+ foreach ((AMemory Memory, long Position) in ShMemPositions)
+ {
+ long TouchScreenOffset = Position + HidTouchScreenOffset;
+
+ long LastEntry = Memory.ReadInt64Unchecked(TouchScreenOffset + 0x10);
+
+ long CurrEntry = (LastEntry + 1) % HidEntryCount;
+
+ long Timestamp = GetTimestamp();
+
+ Memory.WriteInt64Unchecked(TouchScreenOffset + 0x0, Timestamp);
+ Memory.WriteInt64Unchecked(TouchScreenOffset + 0x8, HidEntryCount);
+ Memory.WriteInt64Unchecked(TouchScreenOffset + 0x10, CurrEntry);
+ Memory.WriteInt64Unchecked(TouchScreenOffset + 0x18, HidEntryCount - 1);
+ Memory.WriteInt64Unchecked(TouchScreenOffset + 0x20, Timestamp);
+
+ long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
+
+ long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
+
+ long SampleCounter = Memory.ReadInt64Unchecked(LastEntryOffset) + 1;
+
+ TouchEntryOffset += CurrEntry * HidTouchEntrySize;
+
+ Memory.WriteInt64Unchecked(TouchEntryOffset + 0x0, SampleCounter);
+ Memory.WriteInt64Unchecked(TouchEntryOffset + 0x8, Points.Length);
+
+ TouchEntryOffset += HidTouchEntryHeaderSize;
+
+ 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;
+ }
+ }
+ }
+ }
+
+ private static long GetTimestamp()
+ {
+ return (long)((ulong)Environment.TickCount * 19_200);
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidControllerButtons.cs b/Ryujinx.HLE/Hid/HidControllerButtons.cs
new file mode 100644
index 00000000..f41d17e1
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidControllerButtons.cs
@@ -0,0 +1,35 @@
+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)
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidControllerColorDesc.cs b/Ryujinx.HLE/Hid/HidControllerColorDesc.cs
new file mode 100644
index 00000000..b8cf2a5e
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidControllerColorDesc.cs
@@ -0,0 +1,10 @@
+using System;
+
+namespace Ryujinx.HLE.Input
+{
+ [Flags]
+ public enum HidControllerColorDesc
+ {
+ ColorDesc_ColorsNonexistent = (1 << 1)
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidControllerConnState.cs b/Ryujinx.HLE/Hid/HidControllerConnState.cs
new file mode 100644
index 00000000..1fc9482a
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidControllerConnState.cs
@@ -0,0 +1,11 @@
+using System;
+
+namespace Ryujinx.HLE.Input
+{
+ [Flags]
+ public enum HidControllerConnState
+ {
+ Controller_State_Connected = (1 << 0),
+ Controller_State_Wired = (1 << 1)
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidControllerId.cs b/Ryujinx.HLE/Hid/HidControllerId.cs
new file mode 100644
index 00000000..e4a0e26c
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidControllerId.cs
@@ -0,0 +1,16 @@
+namespace Ryujinx.HLE.Input
+{
+ public enum HidControllerId
+ {
+ CONTROLLER_PLAYER_1 = 0,
+ CONTROLLER_PLAYER_2 = 1,
+ CONTROLLER_PLAYER_3 = 2,
+ CONTROLLER_PLAYER_4 = 3,
+ CONTROLLER_PLAYER_5 = 4,
+ CONTROLLER_PLAYER_6 = 5,
+ CONTROLLER_PLAYER_7 = 6,
+ CONTROLLER_PLAYER_8 = 7,
+ CONTROLLER_HANDHELD = 8,
+ CONTROLLER_UNKNOWN = 9
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidControllerLayouts.cs b/Ryujinx.HLE/Hid/HidControllerLayouts.cs
new file mode 100644
index 00000000..39fdd3fe
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidControllerLayouts.cs
@@ -0,0 +1,13 @@
+namespace Ryujinx.HLE.Input
+{
+ public enum HidControllerLayouts
+ {
+ Pro_Controller = 0,
+ Handheld_Joined = 1,
+ Joined = 2,
+ Left = 3,
+ Right = 4,
+ Main_No_Analog = 5,
+ Main = 6
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidControllerType.cs b/Ryujinx.HLE/Hid/HidControllerType.cs
new file mode 100644
index 00000000..ea8ddfd4
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidControllerType.cs
@@ -0,0 +1,14 @@
+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)
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidJoystickPosition.cs b/Ryujinx.HLE/Hid/HidJoystickPosition.cs
new file mode 100644
index 00000000..a06ef7b2
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidJoystickPosition.cs
@@ -0,0 +1,8 @@
+namespace Ryujinx.HLE.Input
+{
+ public struct HidJoystickPosition
+ {
+ public int DX;
+ public int DY;
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/HidTouchPoint.cs b/Ryujinx.HLE/Hid/HidTouchPoint.cs
new file mode 100644
index 00000000..25412456
--- /dev/null
+++ b/Ryujinx.HLE/Hid/HidTouchPoint.cs
@@ -0,0 +1,11 @@
+namespace Ryujinx.HLE.Input
+{
+ public struct HidTouchPoint
+ {
+ public int X;
+ public int Y;
+ public int DiameterX;
+ public int DiameterY;
+ public int Angle;
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/Hid/JoyCon.cs b/Ryujinx.HLE/Hid/JoyCon.cs
new file mode 100644
index 00000000..e45e1a47
--- /dev/null
+++ b/Ryujinx.HLE/Hid/JoyCon.cs
@@ -0,0 +1,45 @@
+//TODO: This is only used by Config, it doesn't belong to Core.
+namespace Ryujinx.HLE.Input
+{
+ public struct JoyConLeft
+ {
+ public int StickUp;
+ public int StickDown;
+ public int StickLeft;
+ public int StickRight;
+ public int StickButton;
+ public int DPadUp;
+ public int DPadDown;
+ public int DPadLeft;
+ public int DPadRight;
+ public int ButtonMinus;
+ public int ButtonL;
+ public int ButtonZL;
+ public int ButtonSL;
+ public int ButtonSR;
+ }
+
+ public struct JoyConRight
+ {
+ public int StickUp;
+ public int StickDown;
+ public int StickLeft;
+ public int StickRight;
+ public int StickButton;
+ public int ButtonA;
+ public int ButtonB;
+ public int ButtonX;
+ public int ButtonY;
+ public int ButtonPlus;
+ public int ButtonR;
+ public int ButtonZR;
+ public int ButtonSL;
+ public int ButtonSR;
+ }
+
+ public struct JoyCon
+ {
+ public JoyConLeft Left;
+ public JoyConRight Right;
+ }
+}
diff --git a/Ryujinx.HLE/Hid/JoyConColor.cs b/Ryujinx.HLE/Hid/JoyConColor.cs
new file mode 100644
index 00000000..514ec21b
--- /dev/null
+++ b/Ryujinx.HLE/Hid/JoyConColor.cs
@@ -0,0 +1,23 @@
+namespace Ryujinx.HLE.Input
+{
+ public enum JoyConColor //Thanks to CTCaer
+ {
+ Black = 0,
+
+ Body_Grey = 0x828282,
+ Body_Neon_Blue = 0x0AB9E6,
+ Body_Neon_Red = 0xFF3C28,
+ Body_Neon_Yellow = 0xE6FF00,
+ Body_Neon_Pink = 0xFF3278,
+ Body_Neon_Green = 0x1EDC00,
+ Body_Red = 0xE10F00,
+
+ Buttons_Grey = 0x0F0F0F,
+ Buttons_Neon_Blue = 0x001E1E,
+ Buttons_Neon_Red = 0x1E0A0A,
+ Buttons_Neon_Yellow = 0x142800,
+ Buttons_Neon_Pink = 0x28001E,
+ Buttons_Neon_Green = 0x002800,
+ Buttons_Red = 0x280A0A
+ }
+} \ No newline at end of file