1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
using Ryujinx.Core.OsHle;
using System;
using System.Runtime.InteropServices;
namespace Ryujinx.Core
{
public class Hid
{
/*
Thanks to:
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
struct HidSharedMemory
{
header[0x400];
touchscreen[0x3000];
mouse[0x400];
keyboard[0x400];
unkSection1[0x400];
unkSection2[0x400];
unkSection3[0x400];
unkSection4[0x400];
unkSection5[0x200];
unkSection6[0x200];
unkSection7[0x200];
unkSection8[0x800];
controllerSerials[0x4000];
controllers[0x5000 * 10];
unkSection9[0x4600];
}
*/
private const int Hid_Num_Entries = 16;
private Switch Ns;
private long SharedMemOffset;
public Hid(Switch Ns)
{
this.Ns = Ns;
}
public void Init(long HidOffset)
{
unsafe
{
if (HidOffset == 0 || HidOffset + Horizon.HidSize > uint.MaxValue)
{
return;
}
SharedMemOffset = HidOffset;
uint InnerOffset = (uint)Marshal.SizeOf(typeof(HidSharedMemHeader));
IntPtr HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);
HidTouchScreen TouchScreen = new HidTouchScreen();
TouchScreen.Header.TimestampTicks = (ulong)Environment.TickCount;
TouchScreen.Header.NumEntries = (ulong)Hid_Num_Entries;
TouchScreen.Header.LatestEntry = 0;
TouchScreen.Header.MaxEntryIndex = (ulong)Hid_Num_Entries - 1;
TouchScreen.Header.Timestamp = (ulong)Environment.TickCount;
//TODO: Write this structure when the input is implemented
//Marshal.StructureToPtr(TouchScreen, HidPtr, false);
InnerOffset += (uint)Marshal.SizeOf(typeof(HidTouchScreen));
HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);
HidMouse Mouse = new HidMouse();
Mouse.Header.TimestampTicks = (ulong)Environment.TickCount;
Mouse.Header.NumEntries = (ulong)Hid_Num_Entries;
Mouse.Header.LatestEntry = 0;
Mouse.Header.MaxEntryIndex = (ulong)Hid_Num_Entries - 1;
//TODO: Write this structure when the input is implemented
//Marshal.StructureToPtr(Mouse, HidPtr, false);
InnerOffset += (uint)Marshal.SizeOf(typeof(HidMouse));
HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);
HidKeyboard Keyboard = new HidKeyboard();
Keyboard.Header.TimestampTicks = (ulong)Environment.TickCount;
Keyboard.Header.NumEntries = (ulong)Hid_Num_Entries;
Keyboard.Header.LatestEntry = 0;
Keyboard.Header.MaxEntryIndex = (ulong)Hid_Num_Entries - 1;
//TODO: Write this structure when the input is implemented
//Marshal.StructureToPtr(Keyboard, HidPtr, false);
InnerOffset += (uint)Marshal.SizeOf(typeof(HidKeyboard)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection1)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection2)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection3)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection4)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection5)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection6)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection7)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection8)) +
(uint)Marshal.SizeOf(typeof(HidControllerSerials));
//Increase the loop to initialize more controller.
for (int i = 8; i < Enum.GetNames(typeof(HidControllerID)).Length - 1; i++)
{
HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset + (uint)(Marshal.SizeOf(typeof(HidController)) * i));
HidController Controller = new HidController();
Controller.Header.Type = (uint)(HidControllerType.ControllerType_Handheld | HidControllerType.ControllerType_JoyconPair);
Controller.Header.IsHalf = 0;
Controller.Header.SingleColorsDescriptor = (uint)(HidControllerColorDescription.ColorDesc_ColorsNonexistent);
Controller.Header.SingleColorBody = 0;
Controller.Header.SingleColorButtons = 0;
Controller.Header.SplitColorsDescriptor = 0;
Controller.Header.LeftColorBody = (uint)JoyConColor.Body_Neon_Red;
Controller.Header.LeftColorButtons = (uint)JoyConColor.Buttons_Neon_Red;
Controller.Header.RightColorBody = (uint)JoyConColor.Body_Neon_Blue;
Controller.Header.RightColorButtons = (uint)JoyConColor.Buttons_Neon_Blue;
Controller.Layouts = new HidControllerLayout[Enum.GetNames(typeof(HidControllerLayouts)).Length];
Controller.Layouts[(int)HidControllerLayouts.Main] = new HidControllerLayout();
Controller.Layouts[(int)HidControllerLayouts.Main].Header.LatestEntry = (ulong)Hid_Num_Entries;
Marshal.StructureToPtr(Controller, HidPtr, false);
}
Logging.Info("HID Initialized!");
}
}
public void SendControllerButtons(HidControllerID ControllerId,
HidControllerLayouts Layout,
HidControllerKeys Buttons,
JoystickPosition LeftJoystick,
JoystickPosition RightJoystick)
{
uint InnerOffset = (uint)Marshal.SizeOf(typeof(HidSharedMemHeader)) +
(uint)Marshal.SizeOf(typeof(HidTouchScreen)) +
(uint)Marshal.SizeOf(typeof(HidMouse)) +
(uint)Marshal.SizeOf(typeof(HidKeyboard)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection1)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection2)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection3)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection4)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection5)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection6)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection7)) +
(uint)Marshal.SizeOf(typeof(HidUnknownSection8)) +
(uint)Marshal.SizeOf(typeof(HidControllerSerials)) +
((uint)(Marshal.SizeOf(typeof(HidController)) * (int)ControllerId)) +
(uint)Marshal.SizeOf(typeof(HidControllerHeader)) +
(uint)Layout * (uint)Marshal.SizeOf(typeof(HidControllerLayout));
IntPtr HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);
HidControllerLayoutHeader OldControllerHeaderLayout = (HidControllerLayoutHeader)Marshal.PtrToStructure(HidPtr, typeof(HidControllerLayoutHeader));
HidControllerLayoutHeader ControllerLayoutHeader = new HidControllerLayoutHeader
{
TimestampTicks = (ulong)Environment.TickCount,
NumEntries = (ulong)Hid_Num_Entries,
MaxEntryIndex = (ulong)Hid_Num_Entries - 1,
LatestEntry = (OldControllerHeaderLayout.LatestEntry < (ulong)Hid_Num_Entries ? OldControllerHeaderLayout.LatestEntry + 1 : 0)
};
Marshal.StructureToPtr(ControllerLayoutHeader, HidPtr, false);
InnerOffset += (uint)Marshal.SizeOf(typeof(HidControllerLayoutHeader)) + (uint)((uint)(ControllerLayoutHeader.LatestEntry) * Marshal.SizeOf(typeof(HidControllerInputEntry)));
HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);
HidControllerInputEntry ControllerInputEntry = new HidControllerInputEntry();
ControllerInputEntry.Timestamp = (ulong)Environment.TickCount;
ControllerInputEntry.Timestamp_2 = (ulong)Environment.TickCount;
ControllerInputEntry.Buttons = (ulong)Buttons;
ControllerInputEntry.Joysticks = new JoystickPosition[(int)HidControllerJoystick.Joystick_Num_Sticks];
ControllerInputEntry.Joysticks[(int)HidControllerJoystick.Joystick_Left] = LeftJoystick;
ControllerInputEntry.Joysticks[(int)HidControllerJoystick.Joystick_Right] = RightJoystick;
ControllerInputEntry.ConnectionState = (ulong)(HidControllerConnectionState.Controller_State_Connected | HidControllerConnectionState.Controller_State_Wired);
Marshal.StructureToPtr(ControllerInputEntry, HidPtr, false);
}
}
}
|