Skip to content

Commit

Permalink
PS5 DualSense input support
Browse files Browse the repository at this point in the history
  • Loading branch information
MollyJameson committed May 21, 2021
1 parent 38e5ebd commit 493e394
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Expand Up @@ -74,6 +74,67 @@ internal struct DualShock4HIDInputReport : IInputStateTypeInfo
public FourCC format => new FourCC('H', 'I', 'D');
}

[StructLayout(LayoutKind.Explicit, Size = 32)]
internal struct DualSense5HIDInputReport : IInputStateTypeInfo
{
[FieldOffset(0)] public byte reportId;

[InputControl(name = "leftStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "leftStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "leftStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "leftStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "leftStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(1)] public byte leftStickX;
[FieldOffset(2)] public byte leftStickY;

[InputControl(name = "rightStick", layout = "Stick", format = "VC2B")]
[InputControl(name = "rightStick/x", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/left", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/right", offset = 0, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1")]
[InputControl(name = "rightStick/y", offset = 1, format = "BYTE", parameters = "invert,normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5")]
[InputControl(name = "rightStick/up", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0,clampMax=0.5,invert")]
[InputControl(name = "rightStick/down", offset = 1, format = "BYTE", parameters = "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5,clamp=1,clampMin=0.5,clampMax=1,invert=false")]
[FieldOffset(3)] public byte rightStickX;
[FieldOffset(4)] public byte rightStickY;


[InputControl(name = "leftTrigger", format = "BYTE")]
[FieldOffset(5)] public byte leftTrigger;
[InputControl(name = "rightTrigger", format = "BYTE")]
[FieldOffset(6)] public byte rightTrigger;

//TODO: something really noisy. Not sure what.
[FieldOffset(7)] public byte placeholder;

[InputControl(name = "dpad", format = "BIT", layout = "Dpad", sizeInBits = 4, defaultState = 8)]
[InputControl(name = "dpad/up", format = "BIT", layout = "DiscreteButton", parameters = "minValue=7,maxValue=1,nullValue=8,wrapAtValue=7", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/right", format = "BIT", layout = "DiscreteButton", parameters = "minValue=1,maxValue=3", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/down", format = "BIT", layout = "DiscreteButton", parameters = "minValue=3,maxValue=5", bit = 0, sizeInBits = 4)]
[InputControl(name = "dpad/left", format = "BIT", layout = "DiscreteButton", parameters = "minValue=5, maxValue=7", bit = 0, sizeInBits = 4)]
[InputControl(name = "buttonWest", displayName = "Square", bit = 4)]
[InputControl(name = "buttonSouth", displayName = "Cross", bit = 5)]
[InputControl(name = "buttonEast", displayName = "Circle", bit = 6)]
[InputControl(name = "buttonNorth", displayName = "Triangle", bit = 7)]
[FieldOffset(8)] public byte buttons1;
[InputControl(name = "leftShoulder", bit = 0)]
[InputControl(name = "rightShoulder", bit = 1)]
[InputControl(name = "leftTriggerButton", layout = "Button", bit = 2)]
[InputControl(name = "rightTriggerButton", layout = "Button", bit = 3)]
[InputControl(name = "select", displayName = "Share", bit = 4)]
[InputControl(name = "start", displayName = "Options", bit = 5)]
[InputControl(name = "leftStickPress", bit = 6)]
[InputControl(name = "rightStickPress", bit = 7)]
[FieldOffset(9)] public byte buttons2;
[InputControl(name = "systemButton", layout = "Button", displayName = "System", bit = 0)]
[InputControl(name = "touchpadButton", layout = "Button", displayName = "Touchpad Press", bit = 1)]
[FieldOffset(10)] public byte buttons3;

public FourCC format => new FourCC('H', 'I', 'D');
}


/// <summary>
/// Structure of HID input reports for PS3 DualShock 3 controllers.
/// </summary>
Expand Down Expand Up @@ -203,6 +264,31 @@ public static DualShockHIDOutputReport Create()

namespace UnityEngine.InputSystem.DualShock
{
[InputControlLayout(displayName = "PS5 Controller", stateType = typeof(DualSense5HIDInputReport), hideInUI = true)]
[Scripting.Preserve]
public class DuelSense5GamepadHID : DualShockGamepad
{
public ButtonControl leftTriggerButton { get; private set; }
public ButtonControl rightTriggerButton { get; private set; }
public ButtonControl playStationButton { get; private set; }

protected override void FinishSetup()
{
leftTriggerButton = GetChildControl<ButtonControl>("leftTriggerButton");
rightTriggerButton = GetChildControl<ButtonControl>("rightTriggerButton");
playStationButton = GetChildControl<ButtonControl>("systemButton");

base.FinishSetup();
}

// TODO: implement output
public override void PauseHaptics(){ }
public override void ResetHaptics() {}
public override void ResumeHaptics(){}
public override void SetLightBarColor(Color color){}
public override void SetMotorSpeeds(float lowFrequency, float highFrequency){}

}
/// <summary>
/// PS4 DualShock controller that is interfaced to a HID backend.
/// </summary>
Expand Down
Expand Up @@ -39,6 +39,13 @@ public static void Initialize()
.WithCapability("productId", 0x5C4)); // Wireless controller.
InputSystem.RegisterPrecompiledLayout<FastDualShock4GamepadHID>(FastDualShock4GamepadHID.metadata);

// DuelSense PS5
InputSystem.RegisterLayout<DuelSense5GamepadHID>(
matches: new InputDeviceMatcher()
.WithInterface("HID")
.WithCapability("vendorId", 0x54C) // Sony Entertainment.
.WithCapability("productId", 0xCE6)); // Wireless controller.

// Just to make sure, also set up a matcher that goes by strings so that we cover
// all bases.
InputSystem.RegisterLayoutMatcher<DualShock4GamepadHID>(
Expand Down

0 comments on commit 493e394

Please sign in to comment.