aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Input/HidBaseController.cs
blob: 9b9bb7ea98940bd34e56d5819c2c86284cc1dc14 (plain)
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
using static Ryujinx.HLE.Input.Hid;

namespace Ryujinx.HLE.Input
{
    public abstract class HidControllerBase : IHidDevice
    {
        protected HidControllerType HidControllerType;
        protected Switch            Device;
        protected HidControllerId   ControllerId;

        public long Offset    { get; private set; }
        public bool Connected { get; protected set; }

        public HidControllerBase(HidControllerType ControllerType, Switch Device)
        {
            this.Device = Device;

            HidControllerType = ControllerType;
        }

        public virtual void Connect(HidControllerId ControllerId)
        {
            this.ControllerId = ControllerId;

            Offset = Device.Hid.HidPosition + HidControllersOffset + (int)ControllerId * HidControllerSize;

            Device.Memory.FillWithZeros(Offset, 0x5000);

            Device.Memory.WriteInt32(Offset + 0x00, (int)HidControllerType);
        }

        public abstract void SendInput(
            HidControllerButtons Buttons,
            HidJoystickPosition  LeftStick,
            HidJoystickPosition  RightStick);

        protected long WriteInput(
            HidControllerButtons Buttons,
            HidJoystickPosition  LeftStick,
            HidJoystickPosition  RightStick, 
            HidControllerLayouts ControllerLayout)
        {
            long ControllerOffset = Offset + 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);

            return ControllerOffset;
        }
    }
}