Skip to content

Commit 632b112

Browse files
committed
Replace shouldCreateHID with supportedHIDUsages
1 parent b03546a commit 632b112

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

Assets/Tests/InputSystem/Plugins/HIDTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void Devices_CanCreateGenericHID()
7979

8080
[Test]
8181
[Category("Devices")]
82-
public void Devices_DevicesNotAllowedByShouldCreateHIDAreSkipped()
82+
public void Devices_DevicesNotAllowedBySupportedHIDUsagesAreSkipped()
8383
{
8484
var hidDescriptor = new HID.HIDDeviceDescriptor
8585
{
@@ -107,10 +107,9 @@ public void Devices_DevicesNotAllowedByShouldCreateHIDAreSkipped()
107107
Assert.That(InputSystem.devices, Has.Count.EqualTo(0));
108108
Assert.That(InputSystem.GetDeviceById(deviceId), Is.Null);
109109

110-
HIDSupport.shouldCreateHID += descriptor =>
111-
descriptor.usagePage == (HID.UsagePage) 5678 && descriptor.usage == 1234
112-
? true
113-
: (bool?)null;
110+
HIDSupport.supportedHIDUsages = new ReadOnlyArray<HIDSupport.HIDPageUsage>(
111+
new[] {new HIDSupport.HIDPageUsage((HID.UsagePage) 5678, 1234)}
112+
);
114113

115114
runtime.ReportNewInputDevice(descriptionJson, deviceId);
116115
InputSystem.Update();

Packages/com.unity.inputsystem/InputSystem/Plugins/HID/HID.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ internal static string OnFindLayoutForDevice(int deviceId, ref InputDeviceDescri
9191
// Read HID descriptor.
9292
var hidDeviceDescriptor = ReadHIDDeviceDescriptor(deviceId, ref description, runtime);
9393

94-
// Check callbacks to see whether we should actually create a device for this specific HID.
95-
// If no callback says yes or one says no, we ignore the device.
96-
if (HIDSupport.s_ShouldCreateHID.All(f => f(hidDeviceDescriptor) != true) ||
97-
HIDSupport.s_ShouldCreateHID.Any(f => f(hidDeviceDescriptor) == false))
94+
if (!HIDSupport.supportedHIDUsages.Contains(new HIDSupport.HIDPageUsage(hidDeviceDescriptor.usagePage, hidDeviceDescriptor.usage)))
9895
return null;
9996

10097
// Determine if there's any usable elements on the device.

Packages/com.unity.inputsystem/InputSystem/Plugins/HID/HIDSupport.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,27 @@ namespace UnityEngine.InputSystem.HID
2828
/// </remarks>
2929
public static class HIDSupport
3030
{
31-
public static event ShouldCreateHIDCallback shouldCreateHID
31+
public struct HIDPageUsage
3232
{
33-
add => s_ShouldCreateHID.Append(value);
34-
remove => s_ShouldCreateHID.Remove(value);
35-
}
33+
public HID.UsagePage page;
34+
public int usage;
3635

37-
internal static InlinedArray<ShouldCreateHIDCallback> s_ShouldCreateHID;
36+
public HIDPageUsage(HID.UsagePage page, int usage)
37+
{
38+
this.page = page;
39+
this.usage = usage;
40+
}
3841

39-
private static bool? DefaultShouldCreateHIDCallback(HID.HIDDeviceDescriptor descriptor)
40-
{
41-
if (descriptor.usagePage == HID.UsagePage.GenericDesktop)
42+
public HIDPageUsage(HID.GenericDesktop usage)
4243
{
43-
switch (descriptor.usage)
44-
{
45-
case (int)HID.GenericDesktop.Joystick:
46-
case (int)HID.GenericDesktop.Gamepad:
47-
case (int)HID.GenericDesktop.MultiAxisController:
48-
return true;
49-
}
44+
this.page = HID.UsagePage.GenericDesktop;
45+
this.usage = (int)usage;
5046
}
51-
return null;
5247
}
5348

49+
50+
public static ReadOnlyArray<HIDPageUsage> supportedHIDUsages { get; set; }
51+
5452
/// <summary>
5553
/// Add support for generic HIDs to InputSystem.
5654
/// </summary>
@@ -61,7 +59,12 @@ public static event ShouldCreateHIDCallback shouldCreateHID
6159
#endif
6260
static void Initialize()
6361
{
64-
s_ShouldCreateHID.Append(DefaultShouldCreateHIDCallback);
62+
supportedHIDUsages = new ReadOnlyArray<HIDPageUsage>(new[]
63+
{
64+
new HIDPageUsage(HID.GenericDesktop.Joystick),
65+
new HIDPageUsage(HID.GenericDesktop.Gamepad),
66+
new HIDPageUsage(HID.GenericDesktop.MultiAxisController),
67+
});
6568

6669
InputSystem.RegisterLayout<HID>();
6770
InputSystem.onFindLayoutForDevice += HID.OnFindLayoutForDevice;

0 commit comments

Comments
 (0)