aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Hid/Hid.cs
blob: 21580223e739340632ddf88714ccb9d4f76bd997 (plain) (blame)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using Ryujinx.Common;
using Ryujinx.HLE.HOS;
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 Switch Device;

        private long HidPosition;

        public Hid(Switch Device, long HidPosition)
        {
            this.Device      = Device;
            this.HidPosition = HidPosition;

            Device.Memory.FillWithZeros(HidPosition, Horizon.HidSize);

            InitializeJoyconPair(
                JoyConColor.Body_Neon_Red,
                JoyConColor.Buttons_Neon_Red,
                JoyConColor.Body_Neon_Blue,
                JoyConColor.Buttons_Neon_Blue);
        }

        private void InitializeJoyconPair(
            JoyConColor LeftColorBody,
            JoyConColor LeftColorButtons,
            JoyConColor RightColorBody,
            JoyConColor RightColorButtons)
        {
            long BaseControllerOffset = HidPosition + HidControllersOffset + 8 * HidControllerSize;

            HidControllerType Type = HidControllerType.ControllerType_Handheld;

            bool IsHalf = false;

            HidControllerColorDesc SingleColorDesc =
                HidControllerColorDesc.ColorDesc_ColorsNonexistent;

            JoyConColor SingleColorBody    = JoyConColor.Black;
            JoyConColor SingleColorButtons = JoyConColor.Black;

            HidControllerColorDesc SplitColorDesc = 0;

            Device.Memory.WriteInt32(BaseControllerOffset + 0x00, (int)Type);

            Device.Memory.WriteInt32(BaseControllerOffset + 0x04, IsHalf ? 1 : 0);

            Device.Memory.WriteInt32(BaseControllerOffset + 0x08, (int)SingleColorDesc);
            Device.Memory.WriteInt32(BaseControllerOffset + 0x0c, (int)SingleColorBody);
            Device.Memory.WriteInt32(BaseControllerOffset + 0x10, (int)SingleColorButtons);
            Device.Memory.WriteInt32(BaseControllerOffset + 0x14, (int)SplitColorDesc);

            Device.Memory.WriteInt32(BaseControllerOffset + 0x18, (int)LeftColorBody);
            Device.Memory.WriteInt32(BaseControllerOffset + 0x1c, (int)LeftColorButtons);

            Device.Memory.WriteInt32(BaseControllerOffset + 0x20, (int)RightColorBody);
            Device.Memory.WriteInt32(BaseControllerOffset + 0x24, (int)RightColorButtons);
        }

        private HidControllerButtons UpdateStickButtons(
            HidJoystickPosition LeftStick,
            HidJoystickPosition RightStick)
        {
            HidControllerButtons Result = 0;

            if (RightStick.DX < 0)
            {
                Result |= HidControllerButtons.KEY_RSTICK_LEFT;
            }

            if (RightStick.DX > 0)
            {
                Result |= HidControllerButtons.KEY_RSTICK_RIGHT;
            }

            if (RightStick.DY < 0)
            {
                Result |= HidControllerButtons.KEY_RSTICK_DOWN;
            }

            if (RightStick.DY > 0)
            {
                Result |= HidControllerButtons.KEY_RSTICK_UP;
            }

            if (LeftStick.DX < 0)
            {
                Result |= HidControllerButtons.KEY_LSTICK_LEFT;
            }

            if (LeftStick.DX > 0)
            {
                Result |= HidControllerButtons.KEY_LSTICK_RIGHT;
            }

            if (LeftStick.DY < 0)
            {
                Result |= HidControllerButtons.KEY_LSTICK_DOWN;
            }

            if (LeftStick.DY > 0)
            {
                Result |= HidControllerButtons.KEY_LSTICK_UP;
            }

            return Result;
        }

        public void SetJoyconButton(
            HidControllerId      ControllerId,
            HidControllerLayouts ControllerLayout,
            HidControllerButtons Buttons,
            HidJoystickPosition  LeftStick,
            HidJoystickPosition  RightStick)
        {
            Buttons |= UpdateStickButtons(LeftStick, RightStick);

            long ControllerOffset = HidPosition + HidControllersOffset;

            ControllerOffset += (int)ControllerId * HidControllerSize;

            ControllerOffset += 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);

            Device.Memory.WriteInt64(ControllerOffset + 0x28,
                (uint)HidControllerConnState.Controller_State_Connected |
                (uint)HidControllerConnState.Controller_State_Wired);
        }

        public void SetTouchPoints(params HidTouchPoint[] Points)
        {
            long TouchScreenOffset = HidPosition + HidTouchScreenOffset;

            long LastEntry = Device.Memory.ReadInt64(TouchScreenOffset + 0x10);

            long CurrEntry = (LastEntry + 1) % HidEntryCount;

            long Timestamp = GetTimestamp();

            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)
            {
                Device.Memory.WriteInt64(TouchEntryOffset + 0x00, Timestamp);
                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;
            }
        }

        private static long GetTimestamp()
        {
            return PerformanceCounter.ElapsedMilliseconds * 19200;
        }
    }
}