-
Notifications
You must be signed in to change notification settings - Fork 329
FIX: Incorrect HID Stick values for LogicalMinimum with negative values #2246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
7cb59ce
9a5f397
bd804af
8ddd35e
637e5eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,27 +218,9 @@ public void Devices_CanCreateGenericHID_FromDeviceWithBinaryReportDescriptor() | |
|
||
// The HID report descriptor is fetched from the device via an IOCTL. | ||
var deviceId = runtime.AllocateDeviceId(); | ||
unsafe | ||
{ | ||
runtime.SetDeviceCommandCallback(deviceId, | ||
(id, commandPtr) => | ||
{ | ||
if (commandPtr->type == HID.QueryHIDReportDescriptorSizeDeviceCommandType) | ||
return reportDescriptor.Length; | ||
|
||
if (commandPtr->type == HID.QueryHIDReportDescriptorDeviceCommandType | ||
&& commandPtr->payloadSizeInBytes >= reportDescriptor.Length) | ||
{ | ||
fixed(byte* ptr = reportDescriptor) | ||
{ | ||
UnsafeUtility.MemCpy(commandPtr->payloadPtr, ptr, reportDescriptor.Length); | ||
return reportDescriptor.Length; | ||
} | ||
} | ||
SetDeviceCommandCallbackToReturnReportDescriptor(deviceId, reportDescriptor); | ||
|
||
return InputDeviceCommand.GenericFailure; | ||
}); | ||
} | ||
// Report device. | ||
runtime.ReportNewInputDevice( | ||
new InputDeviceDescription | ||
|
@@ -309,6 +291,111 @@ public void Devices_CanCreateGenericHID_FromDeviceWithBinaryReportDescriptor() | |
////TODO: check hat switch | ||
} | ||
|
||
// This is used to mock out the IOCTL the HID device driver would use to return | ||
// the report descriptor and its size. | ||
unsafe void SetDeviceCommandCallbackToReturnReportDescriptor(int deviceId, byte[] reportDescriptor) | ||
{ | ||
runtime.SetDeviceCommandCallback(deviceId, | ||
(id, commandPtr) => | ||
{ | ||
if (commandPtr->type == HID.QueryHIDReportDescriptorSizeDeviceCommandType) | ||
return reportDescriptor.Length; | ||
|
||
if (commandPtr->type == HID.QueryHIDReportDescriptorDeviceCommandType | ||
&& commandPtr->payloadSizeInBytes >= reportDescriptor.Length) | ||
{ | ||
fixed(byte* ptr = reportDescriptor) | ||
{ | ||
UnsafeUtility.MemCpy(commandPtr->payloadPtr, ptr, reportDescriptor.Length); | ||
return reportDescriptor.Length; | ||
} | ||
} | ||
|
||
return InputDeviceCommand.GenericFailure; | ||
}); | ||
} | ||
|
||
[Test] | ||
[Category("HID Devices")] | ||
public void Devices_CanCreateGenericHID_WithSignedLogicalMinAndMaxSticks() | ||
{ | ||
// This is a HID report descriptor for a simple device with two analog sticks; | ||
// Similar to a user that reported an issue in Discussions: | ||
// https://discussions.unity.com/t/input-system-reading-invalid-values-from-hall-effect-keyboards/1684840/3 | ||
var reportDescriptor = new byte[] | ||
{ | ||
0x05, 0x01, // Usage Page (Generic Desktop Ctrls) | ||
0x09, 0x05, // Usage (Game Pad) | ||
0xA1, 0x01, // Collection (Application) | ||
0x85, 0x01, // Report ID (1) | ||
0x05, 0x01, // Usage Page (Generic Desktop Ctrls) | ||
0x09, 0x30, // Usage (X) | ||
0x09, 0x31, // Usage (Y) | ||
0x15, 0x81, // Logical Minimum (-127) | ||
0x25, 0x7F, // Logical Maximum (127) | ||
0x75, 0x08, // Report Size (8) | ||
0x95, 0x02, // Report Count (2) | ||
0x81, 0x02, // Input (Data,Var,Abs) | ||
0xC0, // End Collection | ||
}; | ||
|
||
// The HID report descriptor is fetched from the device via an IOCTL. | ||
var deviceId = runtime.AllocateDeviceId(); | ||
|
||
// Callback to return the desired report descriptor. | ||
SetDeviceCommandCallbackToReturnReportDescriptor(deviceId, reportDescriptor); | ||
|
||
// Report device. | ||
runtime.ReportNewInputDevice( | ||
new InputDeviceDescription | ||
{ | ||
interfaceName = HID.kHIDInterface, | ||
manufacturer = "TestVendor", | ||
product = "TestHID", | ||
capabilities = new HID.HIDDeviceDescriptor | ||
{ | ||
vendorId = 0x321, | ||
productId = 0x432 | ||
}.ToJson() | ||
}.ToJson(), deviceId); | ||
|
||
InputSystem.Update(); | ||
|
||
var device = (Joystick)InputSystem.GetDeviceById(deviceId); | ||
Assert.That(device, Is.Not.Null); | ||
Assert.That(device, Is.TypeOf<Joystick>()); | ||
|
||
// Stick vector 2 should be centered at (0,0). | ||
Assert.That(device.stick.ReadValue(), Is.EqualTo(new Vector2(0f, 0f)).Using(Vector2EqualityComparer.Instance)); | ||
|
||
// Queue event with stick pushed to bottom. We assume Y axis is inverted by default in HID devices. | ||
// See HID.HIDElementDescriptor.DetermineParameters() | ||
InputSystem.QueueStateEvent(device, new SimpleJoystickLayoutWithStickByte { reportId = 1, x = 0, y = 127 }); | ||
InputSystem.Update(); | ||
|
||
Assert.That(device.stick.ReadValue() , Is.EqualTo(new Vector2(0f, -1f)).Using(Vector2EqualityComparer.Instance)); | ||
|
||
InputSystem.QueueStateEvent(device, new SimpleJoystickLayoutWithStickByte { reportId = 1, x = 0, y = -127 }); | ||
InputSystem.Update(); | ||
|
||
Assert.That(device.stick.ReadValue(), Is.EqualTo(new Vector2(0f, 1f)).Using(Vector2EqualityComparer.Instance)); | ||
|
||
InputSystem.QueueStateEvent(device, new SimpleJoystickLayoutWithStickByte { reportId = 1, x = 127, y = 0 }); | ||
InputSystem.Update(); | ||
|
||
Assert.That(device.stick.ReadValue() , Is.EqualTo(new Vector2(1f, 0f)).Using(Vector2EqualityComparer.Instance)); | ||
|
||
InputSystem.QueueStateEvent(device, new SimpleJoystickLayoutWithStickByte { reportId = 1, x = -127, y = 0 }); | ||
InputSystem.Update(); | ||
|
||
Assert.That(device.stick.ReadValue(), Is.EqualTo(new Vector2(-1f, 0f)).Using(Vector2EqualityComparer.Instance)); | ||
|
||
InputSystem.QueueStateEvent(device, new SimpleJoystickLayoutWithStickByte { reportId = 1, x = 127, y = 127 }); | ||
InputSystem.Update(); | ||
|
||
Assert.That(device.stick.ReadValue(), Is.EqualTo(new Vector2(0.7071f, -0.7071f)).Using(Vector2EqualityComparer.Instance)); | ||
} | ||
|
||
[Test] | ||
[Category("Devices")] | ||
public void Devices_CanCreateGenericHID_FromDeviceWithParsedReportDescriptor() | ||
|
@@ -1026,7 +1113,7 @@ public void Devices_GenericHIDConvertsXAndYUsagesToStickControl() | |
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
struct SimpleJoystickLayout : IInputStateTypeInfo | ||
struct SimpleJoystickLayoutWithStickUshort : IInputStateTypeInfo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to create a test with a report descriptor that fits this struct as well. |
||
{ | ||
[FieldOffset(0)] public byte reportId; | ||
[FieldOffset(1)] public ushort x; | ||
|
@@ -1035,6 +1122,16 @@ struct SimpleJoystickLayout : IInputStateTypeInfo | |
public FourCC format => new FourCC('H', 'I', 'D'); | ||
} | ||
|
||
[StructLayout(LayoutKind.Explicit)] | ||
struct SimpleJoystickLayoutWithStickByte : IInputStateTypeInfo | ||
{ | ||
[FieldOffset(0)] public byte reportId; | ||
[FieldOffset(1)] public sbyte x; | ||
[FieldOffset(2)] public sbyte y; | ||
|
||
public FourCC format => new FourCC('H', 'I', 'D'); | ||
} | ||
|
||
[Test] | ||
[Category("Devices")] | ||
public void Devices_GenericHIDXAndYDrivesStickControl() | ||
|
@@ -1069,7 +1166,7 @@ public void Devices_GenericHIDXAndYDrivesStickControl() | |
Assert.That(device, Is.TypeOf<Joystick>()); | ||
Assert.That(device["Stick"], Is.TypeOf<StickControl>()); | ||
|
||
InputSystem.QueueStateEvent(device, new SimpleJoystickLayout { reportId = 1, x = ushort.MaxValue, y = ushort.MinValue }); | ||
InputSystem.QueueStateEvent(device, new SimpleJoystickLayoutWithStickUshort { reportId = 1, x = ushort.MaxValue, y = ushort.MinValue }); | ||
InputSystem.Update(); | ||
|
||
Assert.That(device["stick"].ReadValueAsObject(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,7 +385,7 @@ public InputControlLayout Build() | |
var yElementParameters = yElement.DetermineParameters(); | ||
|
||
builder.AddControl(stickName + "/x") | ||
.WithFormat(xElement.isSigned ? InputStateBlock.FormatSBit : InputStateBlock.FormatBit) | ||
.WithFormat(xElement.DetermineFormat()) | ||
.WithByteOffset((uint)(xElement.reportOffsetInBits / 8 - byteOffset)) | ||
.WithBitOffset((uint)(xElement.reportOffsetInBits % 8)) | ||
.WithSizeInBits((uint)xElement.reportSizeInBits) | ||
|
@@ -394,7 +394,7 @@ public InputControlLayout Build() | |
.WithProcessors(xElement.DetermineProcessors()); | ||
|
||
builder.AddControl(stickName + "/y") | ||
.WithFormat(yElement.isSigned ? InputStateBlock.FormatSBit : InputStateBlock.FormatBit) | ||
.WithFormat(yElement.DetermineFormat()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change was included from #2245 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it required to always determine format for any control? Also buttons support this (but I cannot recall ever seeing it in practise), but definitely e.g. triggers go into same territory There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, and we also did it before. Not sure why assigned it the SBit/Bit format before but we do read multiple "bits" if we had them (e.g. in stateBlock.ReadFloat()). So it might have been to deal with cases where we could use like "10 bits" to define an axis. I'll have a look and test this as well to see if there are any other changes needed. Thanks. |
||
.WithByteOffset((uint)(yElement.reportOffsetInBits / 8 - byteOffset)) | ||
.WithBitOffset((uint)(yElement.reportOffsetInBits % 8)) | ||
.WithSizeInBits((uint)yElement.reportSizeInBits) | ||
|
Uh oh!
There was an error while loading. Please reload this page.