Skip to content

Commit 345535f

Browse files
authored
FIX: Don't pass events for null devices (for devices which have not b… (#805)
1 parent 8b0278c commit 345535f

File tree

6 files changed

+28
-35
lines changed

6 files changed

+28
-35
lines changed

Assets/Tests/InputSystem/CoreTests_Events.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,26 +543,23 @@ public void Events_AreProcessedInOrderTheyAreQueuedIn()
543543

544544
[Test]
545545
[Category("Events")]
546-
public void Events_CanQueueAndReceiveEventsAgainstNonExistingDevices()
546+
public void Events_WillNotReceiveEventsAgainstNonExistingDevices()
547547
{
548548
// Device IDs are looked up only *after* the system shows the event to us.
549549

550550
var receivedCalls = 0;
551-
var receivedDeviceId = InputDevice.InvalidDeviceId;
552551
InputSystem.onEvent +=
553552
(eventPtr, _) =>
554553
{
555554
++receivedCalls;
556-
receivedDeviceId = eventPtr.deviceId;
557555
};
558556

559557
var inputEvent = DeviceConfigurationEvent.Create(4, 1.0);
560558
InputSystem.QueueEvent(ref inputEvent);
561559

562560
InputSystem.Update();
563561

564-
Assert.That(receivedCalls, Is.EqualTo(1));
565-
Assert.That(receivedDeviceId, Is.EqualTo(4));
562+
Assert.That(receivedCalls, Is.EqualTo(0));
566563
}
567564

568565
[Test]
@@ -579,7 +576,9 @@ public void Events_HandledFlagIsResetWhenEventIsQueued()
579576
wasHandled = eventPtr.handled;
580577
};
581578

582-
var inputEvent = DeviceConfigurationEvent.Create(4, 1.0);
579+
var device = InputSystem.AddDevice<Gamepad>();
580+
581+
var inputEvent = DeviceConfigurationEvent.Create(device.id, 1.0);
583582

584583
// This should go back to false when we inputEvent goes on the queue.
585584
// The way the behavior is implemented is a side-effect of how we store

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ however, it has to be formatted properly to pass verification tests.
1111

1212
### Fixed
1313

14+
- Don't pass events for null devices (for devices which have not been created) to `InputSystem.onEvent` callbacks.
1415
- Will close debugger input state windows, when the state is no longer valid instead of throwing exceptions.
1516

1617
#### Actions

Packages/com.unity.inputsystem/Documentation~/Events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ For example, the following code demonstrates how to read a value for [`Gamepad.l
8080

8181
```
8282
InputSystem.onEvent +=
83-
eventPtr =>
83+
(eventPtr, device) =>
8484
{
8585
// Ignore anything that isn't a state event.
8686
if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>())
8787
return;
8888
89-
var gamepad = InputSystem.GetDeviceById(eventPtr.deviceId) as Gamepad;
89+
var gamepad = device as Gamepad;
9090
if (gamepad == null)
9191
{
9292
// Event isn't for a gamepad or device ID is no longer valid.

Packages/com.unity.inputsystem/Documentation~/HowDoI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ And then make sure you put extra StateEvents for your HMD on the queue right in
555555
```C#
556556

557557
InputSystem.onEvent +=
558-
eventPtr =>
558+
(eventPtr, device) =>
559559
{
560560
// Can handle events yourself, for example, and then stop them
561561
// from further processing by marking them as handled.

Packages/com.unity.inputsystem/InputSystem/InputManager.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,25 +2367,6 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
23672367
if (currentEventReadPtr->internalTime <= currentTime)
23682368
totalEventLag += currentTime - currentEventReadPtr->internalTime;
23692369

2370-
// Give listeners a shot at the event.
2371-
var listenerCount = m_EventListeners.length;
2372-
if (listenerCount > 0)
2373-
{
2374-
if (device == null)
2375-
device = TryGetDeviceById(currentEventReadPtr->deviceId);
2376-
2377-
for (var i = 0; i < listenerCount; ++i)
2378-
m_EventListeners[i](new InputEventPtr(currentEventReadPtr), device);
2379-
2380-
// If a listener marks the event as handled, we don't process it further.
2381-
if (currentEventReadPtr->handled)
2382-
{
2383-
eventBuffer.AdvanceToNextEvent(ref currentEventReadPtr, ref currentEventWritePtr,
2384-
ref numEventsRetainedInBuffer, ref remainingEventCount, leaveEventInBuffer: false);
2385-
continue;
2386-
}
2387-
}
2388-
23892370
// Grab device for event. In before-render updates, we already had to
23902371
// check the device.
23912372
if (device == null)
@@ -2404,6 +2385,22 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
24042385
continue;
24052386
}
24062387

2388+
// Give listeners a shot at the event.
2389+
var listenerCount = m_EventListeners.length;
2390+
if (listenerCount > 0)
2391+
{
2392+
for (var i = 0; i < listenerCount; ++i)
2393+
m_EventListeners[i](new InputEventPtr(currentEventReadPtr), device);
2394+
2395+
// If a listener marks the event as handled, we don't process it further.
2396+
if (currentEventReadPtr->handled)
2397+
{
2398+
eventBuffer.AdvanceToNextEvent(ref currentEventReadPtr, ref currentEventWritePtr,
2399+
ref numEventsRetainedInBuffer, ref remainingEventCount, leaveEventInBuffer: false);
2400+
continue;
2401+
}
2402+
}
2403+
24072404
// Process.
24082405
var currentEventType = currentEventReadPtr->type;
24092406
var currentEventTimeInternal = currentEventReadPtr->internalTime;

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,18 +1463,14 @@ public static int FindControls<TControl>(string path, ref InputControlList<TCont
14631463
/// may be postponed to a subsequent frame).
14641464
///
14651465
/// As the input system reads events from the buffer one by one, it will trigger this
1466-
/// callback for each event before then proceeding to process the event. However, if
1467-
/// any of the callbacks sets <see cref="InputEvent.handled"/> to true, the event will
1468-
/// be skipped and ignored.
1466+
/// callback for each event which originates from a recognized device, before then proceeding
1467+
/// to process the event. However, if any of the callbacks sets <see cref="InputEvent.handled"/>
1468+
/// to true, the event will be skipped and ignored.
14691469
///
14701470
/// Note that the input system does NOT sort events by timestamps (<see cref="InputEvent.time"/>).
14711471
/// Instead, they are consumed in the order they are produced. This means that they
14721472
/// will also surface on this callback in that order.
14731473
///
1474-
/// Note that the callback will be called for any event picked up from the internal
1475-
/// event buffer. This may include events for devices that have not been created and thus
1476-
/// may mean that the <see cref="InputDevice"/> parameter to the callback is null.
1477-
///
14781474
/// <example>
14791475
/// <code>
14801476
/// // Treat left+right mouse button as middle mouse button.

0 commit comments

Comments
 (0)