Support other switch controller types (#487)

* Make controllers modular, support changing controller type

* return readable events

* signal hid events

* fix style
This commit is contained in:
emmauss 2018-11-20 02:01:36 +02:00 committed by Ac_K
parent 0c36835f6d
commit dc02ac08ca
24 changed files with 574 additions and 409 deletions

140
Ryujinx.HLE/Input/Hid.cs Normal file
View file

@ -0,0 +1,140 @@
using Ryujinx.Common;
using Ryujinx.HLE.HOS;
namespace Ryujinx.HLE.Input
{
public partial class Hid
{
private Switch Device;
public HidControllerBase PrimaryController { get; private set; }
internal long HidPosition;
public Hid(Switch Device, long HidPosition)
{
this.Device = Device;
this.HidPosition = HidPosition;
Device.Memory.FillWithZeros(HidPosition, Horizon.HidSize);
}
public void InitilizePrimaryController(HidControllerType ControllerType)
{
HidControllerId ControllerId = ControllerType == HidControllerType.Handheld ?
HidControllerId.CONTROLLER_HANDHELD : HidControllerId.CONTROLLER_PLAYER_1;
if (ControllerType == HidControllerType.ProController)
{
PrimaryController = new HidProController(Device);
}
else
{
PrimaryController = new HidNpadController(ControllerType,
Device,
(NpadColor.Body_Neon_Red, NpadColor.Body_Neon_Red),
(NpadColor.Buttons_Neon_Blue, NpadColor.Buttons_Neon_Blue));
}
PrimaryController.Connect(ControllerId);
}
private HidControllerButtons UpdateStickButtons(
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
HidControllerButtons Result = 0;
if (RightStick.DX < 0)
{
Result |= HidControllerButtons.RStickLeft;
}
if (RightStick.DX > 0)
{
Result |= HidControllerButtons.RStickRight;
}
if (RightStick.DY < 0)
{
Result |= HidControllerButtons.RStickDown;
}
if (RightStick.DY > 0)
{
Result |= HidControllerButtons.RStickUp;
}
if (LeftStick.DX < 0)
{
Result |= HidControllerButtons.LStickLeft;
}
if (LeftStick.DX > 0)
{
Result |= HidControllerButtons.LStickRight;
}
if (LeftStick.DY < 0)
{
Result |= HidControllerButtons.LStickDown;
}
if (LeftStick.DY > 0)
{
Result |= HidControllerButtons.LStickUp;
}
return Result;
}
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, SampleCounter);
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;
}
}
internal static long GetTimestamp()
{
return PerformanceCounter.ElapsedMilliseconds * 19200;
}
}
}

View file

@ -0,0 +1,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;
}
}
}

View file

@ -0,0 +1,35 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerButtons
{
A = 1 << 0,
B = 1 << 1,
X = 1 << 2,
Y = 1 << 3,
StickLeft = 1 << 4,
StickRight = 1 << 5,
L = 1 << 6,
R = 1 << 7,
Zl = 1 << 8,
Zr = 1 << 9,
Plus = 1 << 10,
Minus = 1 << 11,
DpadLeft = 1 << 12,
DpadUp = 1 << 13,
DPadRight = 1 << 14,
DpadDown = 1 << 15,
LStickLeft = 1 << 16,
LStickUp = 1 << 17,
LStickRight = 1 << 18,
LStickDown = 1 << 19,
RStickLeft = 1 << 20,
RStickUp = 1 << 21,
RStickRight = 1 << 22,
RStickDown = 1 << 23,
Sl = 1 << 24,
Sr = 1 << 25
}
}

View file

@ -0,0 +1,10 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerColorDesc
{
ColorDesc_ColorsNonexistent = (1 << 1)
}
}

View file

@ -0,0 +1,11 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerConnState
{
Controller_State_Connected = (1 << 0),
Controller_State_Wired = (1 << 1)
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -0,0 +1,14 @@
using System;
namespace Ryujinx.HLE.Input
{
[Flags]
public enum HidControllerType
{
ProController = 1 << 0,
Handheld = 1 << 1,
NpadPair = 1 << 2,
NpadLeft = 1 << 3,
NpadRight = 1 << 4
}
}

View file

@ -0,0 +1,8 @@
namespace Ryujinx.HLE.Input
{
public struct HidJoystickPosition
{
public int DX;
public int DY;
}
}

View file

@ -0,0 +1,86 @@
namespace Ryujinx.HLE.Input
{
public class HidNpadController : HidControllerBase
{
private (NpadColor Left, NpadColor Right) NpadBodyColors;
private (NpadColor Left, NpadColor Right) NpadButtonColors;
private HidControllerLayouts CurrentLayout;
private bool IsHalf;
public HidNpadController(
HidControllerType ControllerType,
Switch Device,
(NpadColor, NpadColor) NpadBodyColors,
(NpadColor, NpadColor) NpadButtonColors) : base(ControllerType, Device)
{
this.NpadBodyColors = NpadBodyColors;
this.NpadButtonColors = NpadButtonColors;
CurrentLayout = HidControllerLayouts.Handheld_Joined;
switch (ControllerType)
{
case HidControllerType.NpadLeft:
CurrentLayout = HidControllerLayouts.Left;
break;
case HidControllerType.NpadRight:
CurrentLayout = HidControllerLayouts.Right;
break;
case HidControllerType.NpadPair:
CurrentLayout = HidControllerLayouts.Joined;
break;
}
}
public override void Connect(HidControllerId ControllerId)
{
if(HidControllerType != HidControllerType.NpadLeft && HidControllerType != HidControllerType.NpadRight)
{
IsHalf = false;
}
base.Connect(CurrentLayout == HidControllerLayouts.Handheld_Joined ? HidControllerId.CONTROLLER_HANDHELD : ControllerId);
HidControllerColorDesc SingleColorDesc =
HidControllerColorDesc.ColorDesc_ColorsNonexistent;
HidControllerColorDesc SplitColorDesc = 0;
NpadColor SingleColorBody = NpadColor.Black;
NpadColor SingleColorButtons = NpadColor.Black;
Device.Memory.WriteInt32(Offset + 0x04, IsHalf ? 1 : 0);
if (IsHalf)
{
Device.Memory.WriteInt32(Offset + 0x08, (int)SingleColorDesc);
Device.Memory.WriteInt32(Offset + 0x0c, (int)SingleColorBody);
Device.Memory.WriteInt32(Offset + 0x10, (int)SingleColorButtons);
Device.Memory.WriteInt32(Offset + 0x14, (int)SplitColorDesc);
}
else
{
Device.Memory.WriteInt32(Offset + 0x18, (int)NpadBodyColors.Left);
Device.Memory.WriteInt32(Offset + 0x1c, (int)NpadButtonColors.Left);
Device.Memory.WriteInt32(Offset + 0x20, (int)NpadBodyColors.Right);
Device.Memory.WriteInt32(Offset + 0x24, (int)NpadButtonColors.Right);
}
Connected = true;
}
public override void SendInput
(HidControllerButtons Buttons,
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
long ControllerOffset = WriteInput(Buttons, LeftStick, RightStick, CurrentLayout);
Device.Memory.WriteInt64(ControllerOffset + 0x28,
(Connected ? (uint)HidControllerConnState.Controller_State_Connected : 0) |
(CurrentLayout == HidControllerLayouts.Handheld_Joined ? (uint)HidControllerConnState.Controller_State_Wired : 0));
}
}
}

View file

@ -0,0 +1,44 @@
namespace Ryujinx.HLE.Input
{
public class HidProController : HidControllerBase
{
bool Wired = false;
public HidProController(Switch Device) : base(HidControllerType.ProController, Device)
{
Wired = true;
}
public override void Connect(HidControllerId ControllerId)
{
base.Connect(ControllerId);
HidControllerColorDesc SingleColorDesc =
HidControllerColorDesc.ColorDesc_ColorsNonexistent;
HidControllerColorDesc SplitColorDesc = 0;
NpadColor SingleColorBody = NpadColor.Black;
NpadColor SingleColorButtons = NpadColor.Black;
Device.Memory.WriteInt32(Offset + 0x08, (int)SingleColorDesc);
Device.Memory.WriteInt32(Offset + 0x0c, (int)SingleColorBody);
Device.Memory.WriteInt32(Offset + 0x10, (int)SingleColorButtons);
Device.Memory.WriteInt32(Offset + 0x14, (int)SplitColorDesc);
Connected = true;
}
public override void SendInput(
HidControllerButtons Buttons,
HidJoystickPosition LeftStick,
HidJoystickPosition RightStick)
{
long ControllerOffset = WriteInput(Buttons, LeftStick, RightStick, HidControllerLayouts.Pro_Controller);
Device.Memory.WriteInt64(ControllerOffset + 0x28,
(Connected ? (uint)HidControllerConnState.Controller_State_Connected : 0) |
(Wired ? (uint)HidControllerConnState.Controller_State_Wired : 0));
}
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.HLE.Input
{
public partial 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
*/
internal const int HidHeaderSize = 0x400;
internal const int HidTouchScreenSize = 0x3000;
internal const int HidMouseSize = 0x400;
internal const int HidKeyboardSize = 0x400;
internal const int HidUnkSection1Size = 0x400;
internal const int HidUnkSection2Size = 0x400;
internal const int HidUnkSection3Size = 0x400;
internal const int HidUnkSection4Size = 0x400;
internal const int HidUnkSection5Size = 0x200;
internal const int HidUnkSection6Size = 0x200;
internal const int HidUnkSection7Size = 0x200;
internal const int HidUnkSection8Size = 0x800;
internal const int HidControllerSerialsSize = 0x4000;
internal const int HidControllersSize = 0x32000;
internal const int HidUnkSection9Size = 0x800;
internal const int HidTouchHeaderSize = 0x28;
internal const int HidTouchEntrySize = 0x298;
internal const int HidTouchEntryHeaderSize = 0x10;
internal const int HidTouchEntryTouchSize = 0x28;
internal const int HidControllerSize = 0x5000;
internal const int HidControllerHeaderSize = 0x28;
internal const int HidControllerLayoutsSize = 0x350;
internal const int HidControllersLayoutHeaderSize = 0x20;
internal const int HidControllersInputEntrySize = 0x30;
internal const int HidHeaderOffset = 0;
internal const int HidTouchScreenOffset = HidHeaderOffset + HidHeaderSize;
internal const int HidMouseOffset = HidTouchScreenOffset + HidTouchScreenSize;
internal const int HidKeyboardOffset = HidMouseOffset + HidMouseSize;
internal const int HidUnkSection1Offset = HidKeyboardOffset + HidKeyboardSize;
internal const int HidUnkSection2Offset = HidUnkSection1Offset + HidUnkSection1Size;
internal const int HidUnkSection3Offset = HidUnkSection2Offset + HidUnkSection2Size;
internal const int HidUnkSection4Offset = HidUnkSection3Offset + HidUnkSection3Size;
internal const int HidUnkSection5Offset = HidUnkSection4Offset + HidUnkSection4Size;
internal const int HidUnkSection6Offset = HidUnkSection5Offset + HidUnkSection5Size;
internal const int HidUnkSection7Offset = HidUnkSection6Offset + HidUnkSection6Size;
internal const int HidUnkSection8Offset = HidUnkSection7Offset + HidUnkSection7Size;
internal const int HidControllerSerialsOffset = HidUnkSection8Offset + HidUnkSection8Size;
internal const int HidControllersOffset = HidControllerSerialsOffset + HidControllerSerialsSize;
internal const int HidUnkSection9Offset = HidControllersOffset + HidControllersSize;
internal const int HidEntryCount = 17;
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.HLE.Input
{
interface IHidDevice
{
long Offset { get; }
bool Connected { get; }
}
}

View file

@ -0,0 +1,23 @@
namespace Ryujinx.HLE.Input
{
public enum NpadColor //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
}
}