Skip to content

Commit a1c3f74

Browse files
authored
FIX: Fixing Android sensors not working because MakeCurrent was never called (#1434)
1 parent 472aaae commit a1c3f74

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Assets/Tests/InputSystem/CoreTests_Devices.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,8 @@ public void Devices_WhenCreationFails_SystemRecoversGracefully()
15671567
public unsafe void Devices_CanBeDisabledAndReEnabled()
15681568
{
15691569
var device = InputSystem.AddDevice<Mouse>();
1570+
var nativeBackendDisableWasCalled = false;
1571+
var nativeBackendEnableWasCalled = false;
15701572

15711573
bool? disabled = null;
15721574
runtime.SetDeviceCommandCallback(device.deviceId,
@@ -1576,13 +1578,15 @@ public unsafe void Devices_CanBeDisabledAndReEnabled()
15761578
{
15771579
Assert.That(disabled, Is.Null);
15781580
disabled = true;
1581+
nativeBackendDisableWasCalled = true;
15791582
return InputDeviceCommand.GenericSuccess;
15801583
}
15811584

15821585
if (commandPtr->type == EnableDeviceCommand.Type)
15831586
{
15841587
Assert.That(disabled, Is.Null);
15851588
disabled = false;
1589+
nativeBackendEnableWasCalled = true;
15861590
return InputDeviceCommand.GenericSuccess;
15871591
}
15881592

@@ -1597,6 +1601,7 @@ public unsafe void Devices_CanBeDisabledAndReEnabled()
15971601
Assert.That(device.enabled, Is.False);
15981602
Assert.That(disabled.HasValue, Is.True);
15991603
Assert.That(disabled.Value, Is.True);
1604+
Assert.That(nativeBackendDisableWasCalled, Is.True);
16001605

16011606
// Make sure that state sent against the device is ignored.
16021607
InputSystem.QueueStateEvent(device, new MouseState { buttons = 0xffff });
@@ -1611,6 +1616,7 @@ public unsafe void Devices_CanBeDisabledAndReEnabled()
16111616
Assert.That(device.enabled, Is.True);
16121617
Assert.That(disabled.HasValue, Is.True);
16131618
Assert.That(disabled.Value, Is.False);
1619+
Assert.That(nativeBackendEnableWasCalled, Is.True);
16141620
}
16151621

16161622
[Test]
@@ -3502,6 +3508,26 @@ public void TODO_Devices_CannotChangeStateLayoutOfTouchscreen()
35023508
Assert.Fail();
35033509
}
35043510

3511+
[Test]
3512+
[Category("Devices")]
3513+
public void Devices_AddingDisabledSensorMakesItCurrent()
3514+
{
3515+
var deviceId = runtime.ReportNewInputDevice<Accelerometer>();
3516+
runtime.SetDeviceCommandCallback(deviceId,
3517+
new QueryEnabledStateCommand
3518+
{
3519+
baseCommand = new InputDeviceCommand(QueryEnabledStateCommand.Type, QueryEnabledStateCommand.kSize),
3520+
isEnabled = false
3521+
});
3522+
3523+
InputSystem.Update();
3524+
3525+
Assert.That(Accelerometer.current, Is.Not.Null);
3526+
Assert.That(Accelerometer.current.enabled, Is.False);
3527+
3528+
InputSystem.EnableDevice(Accelerometer.current);
3529+
}
3530+
35053531
[Test]
35063532
[Category("Devices")]
35073533
public void Devices_CanGetSensorSamplingFrequency()

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ however, it has to be formatted properly to pass verification tests.
2828
- Fixed "Default constructor not found for type UnityEngine.InputSystem.iOS.LowLevel.iOSStepCounter" any other potential exceptions due to classes, methods, fields and properties being stripped when managed stripping setting set to medium or high ([case 1368761](https://issuetracker.unity3d.com/issues/ios-new-input-system-iosstepcounter-crash-on-launch-with-managed-stripping)).
2929
- Fixed an issue where InvalidOperationExceptions are thrown if an input for an action with multiple interactions is held while disconnecting the device([case 1354098](https://issuetracker.unity3d.com/issues/input-system-errors-are-thrown-when-disconnecting-controller-while-holding-a-button-with-press-and-release-set-up-separately)).
3030
- Fixed `action.ReadValue` and others returning invalid data when used from `FixedUpdate` or early update when running in play mode in the editor ([case 1368559](https://issuetracker.unity3d.com/issues/enter-key-is-not-registered-when-using-waspressedthisframe-with-input-system-1-dot-1-1) [case 1367556](https://issuetracker.unity3d.com/issues/input-action-readvalue-always-returns-zero-when-called-from-fixedupdate) [case 1372830](https://issuetracker.unity3d.com/issues/querying-inputs-before-preupdate-dot-newinputupdate-returns-invalid-data-when-running-in-play-mode-in-editor)).
31+
- Fixed current being `null` for sensors (`Accelerometer.current`, others) ([case 1371204](https://issuetracker.unity3d.com/issues/accelerometer-not-working-when-using-input-system-1-dot-1-1)).
3132

3233
### Added
3334

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,8 +1162,9 @@ public void AddDevice(InputDevice device)
11621162
////REVIEW: is this really a good thing to do? just plugging in a device shouldn't make
11631163
//// it current, no?
11641164
// Make the device current.
1165-
if (device.enabled)
1166-
device.MakeCurrent();
1165+
// BEWARE: if this will not happen for whatever reason, you will break Android sensors,
1166+
// as they rely on .current for enabling native backend, see https://fogbugz.unity3d.com/f/cases/1371204/
1167+
device.MakeCurrent();
11671168

11681169
// Notify listeners.
11691170
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.Added, "InputSystem.onDeviceChange");

0 commit comments

Comments
 (0)