From 18b6aae38129077d34419aec1bc5e332bdcf01ec Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 12 Jul 2023 01:07:14 -0500 Subject: [PATCH] Disable gamepad sensors while in PiP mode --- app/src/main/java/com/limelight/Game.java | 6 ++ .../binding/input/ControllerHandler.java | 55 +++++++++++++++++++ .../jni/moonlight-core/moonlight-common-c | 2 +- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/limelight/Game.java b/app/src/main/java/com/limelight/Game.java index 33a26088..2aefb986 100644 --- a/app/src/main/java/com/limelight/Game.java +++ b/app/src/main/java/com/limelight/Game.java @@ -639,6 +639,9 @@ public class Game extends Activity implements SurfaceHolder.Callback, performanceOverlayView.setVisibility(View.GONE); notificationOverlayView.setVisibility(View.GONE); + // Disable sensors while in PiP mode + controllerHandler.disableSensors(); + // Update GameManager state to indicate we're in PiP (still gaming, but interruptible) UiHelper.notifyStreamEnteringPiP(this); } @@ -657,6 +660,9 @@ public class Game extends Activity implements SurfaceHolder.Callback, notificationOverlayView.setVisibility(requestedNotificationOverlayVisibility); + // Enable sensors again after exiting PiP + controllerHandler.enableSensors(); + // Update GameManager state to indicate we're out of PiP (gaming, non-interruptible) UiHelper.notifyStreamExitingPiP(this); } diff --git a/app/src/main/java/com/limelight/binding/input/ControllerHandler.java b/app/src/main/java/com/limelight/binding/input/ControllerHandler.java index ec314953..604369b9 100644 --- a/app/src/main/java/com/limelight/binding/input/ControllerHandler.java +++ b/app/src/main/java/com/limelight/binding/input/ControllerHandler.java @@ -251,6 +251,20 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD deviceVibrator.cancel(); } + public void disableSensors() { + for (int i = 0; i < inputDeviceContexts.size(); i++) { + InputDeviceContext deviceContext = inputDeviceContexts.valueAt(i); + deviceContext.disableSensors(); + } + } + + public void enableSensors() { + for (int i = 0; i < inputDeviceContexts.size(); i++) { + InputDeviceContext deviceContext = inputDeviceContexts.valueAt(i); + deviceContext.enableSensors(); + } + } + private static boolean hasJoystickAxes(InputDevice device) { return (device.getSources() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK && getMotionRangeForJoystickAxis(device, MotionEvent.AXIS_X) != null && @@ -1928,6 +1942,10 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD if (reportRateHz != 0 && accelSensor != null) { sm.registerListener(newSensorListener, accelSensor, 1000000 / reportRateHz); deviceContext.accelListener = newSensorListener; + deviceContext.accelReportRateHz = reportRateHz; + } + else { + deviceContext.accelReportRateHz = 0; } break; case MoonBridge.LI_MOTION_TYPE_GYRO: @@ -1941,6 +1959,10 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD if (reportRateHz != 0 && gyroSensor != null) { sm.registerListener(newSensorListener, gyroSensor,1000000 / reportRateHz); deviceContext.gyroListener = newSensorListener; + deviceContext.gyroReportRateHz = reportRateHz; + } + else { + deviceContext.gyroReportRateHz = 0; } break; } @@ -2561,7 +2583,9 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD public short leftTriggerMotor, rightTriggerMotor; public SensorEventListener gyroListener; + public short gyroReportRateHz; public SensorEventListener accelListener; + public short accelReportRateHz; public InputDevice inputDevice; @@ -2769,6 +2793,8 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD public void migrateContext(InputDeviceContext oldContext) { // Take ownership of the sensor and light sessions if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + this.gyroReportRateHz = oldContext.gyroReportRateHz; + this.accelReportRateHz = oldContext.accelReportRateHz; this.lightsSession = oldContext.lightsSession; this.gyroListener = oldContext.gyroListener; this.accelListener = oldContext.accelListener; @@ -2791,6 +2817,35 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD sendControllerBatteryPacket(this); handler.postDelayed(batteryStateUpdateRunnable, BATTERY_RECHECK_INTERVAL_MS); } + + public void disableSensors() { + // Unregister all sensor listeners + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (gyroListener != null) { + inputDevice.getSensorManager().unregisterListener(gyroListener); + gyroListener = null; + + // Send a gyro event to ensure the virtual controller is stationary + conn.sendControllerMotionEvent((byte) controllerNumber, MoonBridge.LI_MOTION_TYPE_GYRO, 0.f, 0.f, 0.f); + } + if (accelListener != null) { + inputDevice.getSensorManager().unregisterListener(accelListener); + accelListener = null; + + // We leave the acceleration as-is to preserve the attitude of the controller + } + } + } + + public void enableSensors() { + // Turn back on any sensors that should be reporting but are currently unregistered + if (accelReportRateHz != 0 && accelListener == null) { + handleSetMotionEventState(controllerNumber, MoonBridge.LI_MOTION_TYPE_ACCEL, accelReportRateHz); + } + if (gyroReportRateHz != 0 && gyroListener == null) { + handleSetMotionEventState(controllerNumber, MoonBridge.LI_MOTION_TYPE_GYRO, gyroReportRateHz); + } + } } class UsbDeviceContext extends GenericControllerContext { diff --git a/app/src/main/jni/moonlight-core/moonlight-common-c b/app/src/main/jni/moonlight-core/moonlight-common-c index a3b28eb4..2d0badde 160000 --- a/app/src/main/jni/moonlight-core/moonlight-common-c +++ b/app/src/main/jni/moonlight-core/moonlight-common-c @@ -1 +1 @@ -Subproject commit a3b28eb4d7433d4f492df117a75a5edd8855eb80 +Subproject commit 2d0badde9aa9a48480e24a289569de1046c6acba