From 54af70005d4f38dc1ac3715dd9fa67eb94cf89a7 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 16 Jul 2021 19:51:14 -0500 Subject: [PATCH] Fix spurious gamepad removal when entering PiP with PS4 controller on Android 12 The relative mouse axes AXIS_RELATIVE_X/Y are added/removed when gaining/losing input focus --- .../binding/input/ControllerHandler.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) 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 d8c6599e..70fcf88d 100644 --- a/app/src/main/java/com/limelight/binding/input/ControllerHandler.java +++ b/app/src/main/java/com/limelight/binding/input/ControllerHandler.java @@ -158,11 +158,36 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD } } + // This can happen when gaining/losing input focus with some devices. + // Input devices that have a trackpad may gain/lose AXIS_RELATIVE_X/Y. @Override public void onInputDeviceChanged(int deviceId) { - // Remove and re-add - onInputDeviceRemoved(deviceId); - onInputDeviceAdded(deviceId); + InputDevice device = InputDevice.getDevice(deviceId); + if (device == null) { + return; + } + + // If we don't have a context for this device, we don't need to update anything + InputDeviceContext existingContext = inputDeviceContexts.get(deviceId); + if (existingContext == null) { + return; + } + + LimeLog.info("Device changed: "+existingContext.name+" ("+deviceId+")"); + + // Don't release the controller number, because we will carry it over if it is present. + // We also want to make sure the change is invisible to the host PC to avoid an add/remove + // cycle for the gamepad which may break some games. + existingContext.destroy(); + + InputDeviceContext newContext = createInputDeviceContextForDevice(device); + + // Copy over existing controller number state + newContext.assignedControllerNumber = existingContext.assignedControllerNumber; + newContext.reservedControllerNumber = existingContext.reservedControllerNumber; + newContext.controllerNumber = existingContext.controllerNumber; + + inputDeviceContexts.put(deviceId, newContext); } public void stop() {