mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
fix: preserve keyboard input on composite devices (#435)
* fix: preserve keyboard input on composite devices * Fix composite input merge noteDevice call --------- Co-authored-by: Jammo <8018115+kaosjammo@users.noreply.github.com> Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
This commit is contained in:
3 files changed
+235
-10
No files matched your search
@@ -1367,7 +1367,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
return super.dispatchKeyEvent(event)
|
||||
}
|
||||
if (pipUi) return true
|
||||
val fromController = PadBridge.isControllerKey(event)
|
||||
val fromController = PadBridge.isControllerKeyEvent(event)
|
||||
if (fromController && event.action == KeyEvent.ACTION_DOWN) {
|
||||
if (drawerOpen && !drawerControllerActive) sessionOverlay.requestFocus()
|
||||
drawerControllerActive = true
|
||||
@@ -1423,8 +1423,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
if (event.keyCode != KeyEvent.KEYCODE_BACK && padBridge?.onKeyEvent(event) == true) return true
|
||||
// A hardware keyboard, forwarded to the compositor's wl_keyboard. Back is left to the
|
||||
// activity, which opens the drawer.
|
||||
val fromPad = fromController
|
||||
if (CompositorHost.isStarted && event.keyCode != KeyEvent.KEYCODE_BACK && !fromPad) {
|
||||
if (CompositorHost.isStarted && event.keyCode != KeyEvent.KEYCODE_BACK && !fromController) {
|
||||
val down = event.action == KeyEvent.ACTION_DOWN
|
||||
if (down || event.action == KeyEvent.ACTION_UP) {
|
||||
var evdev = EvdevKeys.fromKeyCode(event.keyCode)
|
||||
@@ -1462,7 +1461,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
|
||||
override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
|
||||
if (pipUi) return true
|
||||
if (drawerOpen && event.device != null && PadBridge.isFromController(event.device)) {
|
||||
if (drawerOpen && PadBridge.isControllerMotionEvent(event)) {
|
||||
if (!drawerControllerActive) sessionOverlay.requestFocus()
|
||||
drawerControllerActive = true
|
||||
if (event.actionMasked == MotionEvent.ACTION_MOVE &&
|
||||
@@ -1473,7 +1472,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
return true
|
||||
}
|
||||
if (drawerDirectionKey != KeyEvent.KEYCODE_UNKNOWN) releaseDrawerDirection()
|
||||
if (pcKeyboardOpen && event.device != null && PadBridge.isFromController(event.device)) return super.dispatchGenericMotionEvent(event)
|
||||
if (pcKeyboardOpen && PadBridge.isControllerMotionEvent(event)) return super.dispatchGenericMotionEvent(event)
|
||||
if (padBridge?.onMotionEvent(event) == true) return true
|
||||
if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE) && !drawerOpen && onMouse(event)) return true
|
||||
return super.dispatchGenericMotionEvent(event)
|
||||
|
||||
@@ -36,6 +36,18 @@ public final class PadBridge {
|
||||
/** Player slots: the session prepares a ring for each (SessionService). */
|
||||
public static final int SLOTS = 4;
|
||||
private static final float DEAD_ZONE = 0.12f;
|
||||
private static final int[] JOYSTICK_AXES = {
|
||||
MotionEvent.AXIS_X, MotionEvent.AXIS_Y, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ,
|
||||
MotionEvent.AXIS_HAT_X, MotionEvent.AXIS_HAT_Y, MotionEvent.AXIS_LTRIGGER,
|
||||
MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_BRAKE, MotionEvent.AXIS_GAS
|
||||
};
|
||||
private static final int[] CONTROLLER_BUTTONS = {
|
||||
KeyEvent.KEYCODE_BUTTON_A, KeyEvent.KEYCODE_BUTTON_B, KeyEvent.KEYCODE_BUTTON_X,
|
||||
KeyEvent.KEYCODE_BUTTON_Y, KeyEvent.KEYCODE_BUTTON_L1, KeyEvent.KEYCODE_BUTTON_R1,
|
||||
KeyEvent.KEYCODE_BUTTON_SELECT, KeyEvent.KEYCODE_BUTTON_START,
|
||||
KeyEvent.KEYCODE_BUTTON_THUMBL, KeyEvent.KEYCODE_BUTTON_THUMBR,
|
||||
KeyEvent.KEYCODE_BUTTON_MODE, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_BUTTON_R2
|
||||
};
|
||||
// The client takes A as part of the chord only once it has had Guide held for a while, and a
|
||||
// client starved of CPU needs longer. Too short, and it acts on A as well, selecting whatever it
|
||||
// had focused before opening QAM: with 80 ms of lead that was 3 times in 20 at rest, and 250 ms
|
||||
@@ -160,8 +172,58 @@ public final class PadBridge {
|
||||
// counting it would hide the on-screen controls with nothing to replace them.
|
||||
if (device.isVirtual()) return false;
|
||||
int sources = device.getSources();
|
||||
return (sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD
|
||||
|| (sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
|
||||
if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) return true;
|
||||
if ((sources & InputDevice.SOURCE_JOYSTICK) != InputDevice.SOURCE_JOYSTICK) return false;
|
||||
if (device.getKeyboardType() != InputDevice.KEYBOARD_TYPE_ALPHABETIC) return true;
|
||||
// Some composite keyboards advertise joystick input without any sticks or pad buttons.
|
||||
// A keyboard's mouse ranges do not establish a joystick; a real hybrid's joystick ranges
|
||||
// or physical controller buttons do. GAMEPAD above remains authoritative even with keys.
|
||||
for (int axis : JOYSTICK_AXES) {
|
||||
InputDevice.MotionRange range = device.getMotionRange(axis, InputDevice.SOURCE_JOYSTICK);
|
||||
if (range != null && range.getMax() > range.getMin()) return true;
|
||||
}
|
||||
for (boolean present : device.hasKeys(CONTROLLER_BUTTONS)) {
|
||||
if (present) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Whether this key belongs to the pad, rather than a keyboard on the same input device. */
|
||||
public static boolean isControllerKeyEvent(KeyEvent event) {
|
||||
InputDevice device = event.getDevice();
|
||||
if (device == null || device.isVirtual()) return false;
|
||||
// An explicit pad button is unambiguous even when a hybrid omits its capabilities.
|
||||
if (KeyEvent.isGamepadButton(event.getKeyCode())) {
|
||||
return device.supportsSource(InputDevice.SOURCE_GAMEPAD)
|
||||
|| device.supportsSource(InputDevice.SOURCE_JOYSTICK);
|
||||
}
|
||||
switch (event.getKeyCode()) {
|
||||
case KeyEvent.KEYCODE_DPAD_UP:
|
||||
case KeyEvent.KEYCODE_DPAD_RIGHT:
|
||||
case KeyEvent.KEYCODE_DPAD_DOWN:
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT:
|
||||
case KeyEvent.KEYCODE_BACK:
|
||||
case KeyEvent.KEYCODE_MENU:
|
||||
case KeyEvent.KEYCODE_HOME:
|
||||
if (!isFromController(device)) return false;
|
||||
// Without GAMEPAD, an alphabetic hybrid's keyboard arrows remain keyboard keys.
|
||||
// Its ambiguous navigation keys need explicit pad/joystick event provenance;
|
||||
// DPAD alone also describes ordinary keyboard arrows.
|
||||
return device.supportsSource(InputDevice.SOURCE_GAMEPAD)
|
||||
|| device.getKeyboardType() != InputDevice.KEYBOARD_TYPE_ALPHABETIC
|
||||
|| event.isFromSource(InputDevice.SOURCE_GAMEPAD)
|
||||
|| event.isFromSource(InputDevice.SOURCE_JOYSTICK);
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Mouse motion on a composite controller still belongs to the pointer, not its sticks. */
|
||||
public static boolean isControllerMotionEvent(MotionEvent event) {
|
||||
InputDevice device = event.getDevice();
|
||||
// An actual joystick event, like a pad button, survives a hybrid's missing capabilities.
|
||||
return event.isFromSource(InputDevice.SOURCE_JOYSTICK) && device != null && !device.isVirtual()
|
||||
&& (device.supportsSource(InputDevice.SOURCE_GAMEPAD)
|
||||
|| device.supportsSource(InputDevice.SOURCE_JOYSTICK));
|
||||
}
|
||||
|
||||
/** A Bluetooth keyboard can share a device with a joystick. Classify the key, not just
|
||||
@@ -215,10 +277,9 @@ public final class PadBridge {
|
||||
|
||||
/** @return true when the event was a pad button and has been consumed. */
|
||||
public synchronized boolean onKeyEvent(KeyEvent event) {
|
||||
if (!isControllerKey(event)) return false;
|
||||
if (!isControllerKeyEvent(event)) return false;
|
||||
int slot = slotFor(event.getDevice());
|
||||
PadState pad = stateFor(slot);
|
||||
noteDevice(event.getDevice(), slot);
|
||||
boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
|
||||
switch (event.getKeyCode()) {
|
||||
case KeyEvent.KEYCODE_BUTTON_A: pad.press(0, pressed); break;
|
||||
@@ -250,6 +311,7 @@ public final class PadBridge {
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT: pad.left = pressed; break;
|
||||
default: return false;
|
||||
}
|
||||
noteDevice(event.getDevice(), slot);
|
||||
if (pressed) notePlayerInput();
|
||||
statButtons++;
|
||||
scheduleStats();
|
||||
@@ -259,7 +321,7 @@ public final class PadBridge {
|
||||
|
||||
/** @return true when the event was a pad's sticks/triggers and has been consumed. */
|
||||
public synchronized boolean onMotionEvent(MotionEvent event) {
|
||||
if (!isFromController(event.getDevice())) return false;
|
||||
if (!isControllerMotionEvent(event)) return false;
|
||||
if (event.getAction() != MotionEvent.ACTION_MOVE) return false;
|
||||
int slot = slotFor(event.getDevice());
|
||||
PadState pad = stateFor(slot);
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.hardware.input.InputManager
|
||||
import android.view.InputDevice
|
||||
import android.view.InputEvent
|
||||
import android.view.KeyCharacterMap
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.annotation.RealObject
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.InputDeviceBuilder
|
||||
import org.robolectric.shadows.ShadowKeyEvent
|
||||
import org.robolectric.shadows.ShadowMotionEvent
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE, shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class,
|
||||
PadBridgeRoutingTest.RegisteredMotionEvent::class])
|
||||
class PadBridgeRoutingTest {
|
||||
@Test fun aCompositeKeyboardDoesNotCountAsAConnectedPad() {
|
||||
val keyboard = device(COMPOSITE, InputDevice.KEYBOARD_TYPE_ALPHABETIC)
|
||||
assertFalse(PadBridge.isFromController(keyboard))
|
||||
assertFalse(PadBridge.anyControllerConnected())
|
||||
assertFalse(PadBridge.isFromController(device(InputDevice.SOURCE_KEYBOARD, InputDevice.KEYBOARD_TYPE_ALPHABETIC)))
|
||||
}
|
||||
|
||||
@Test fun mouseAxesDoNotMakeACompositeKeyboardAJoystick() {
|
||||
val keyboard = device(COMPOSITE, InputDevice.KEYBOARD_TYPE_ALPHABETIC, rangeSource = InputDevice.SOURCE_MOUSE)
|
||||
assertFalse(PadBridge.isFromController(keyboard))
|
||||
assertFalse(PadBridge.anyControllerConnected())
|
||||
}
|
||||
|
||||
@Test fun gamepadsKeepTheirKeyboardCapabilities() {
|
||||
for (type in listOf(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC, InputDevice.KEYBOARD_TYPE_ALPHABETIC)) {
|
||||
assertTrue(PadBridge.isFromController(device(InputDevice.SOURCE_GAMEPAD or InputDevice.SOURCE_KEYBOARD, type)))
|
||||
}
|
||||
assertTrue(PadBridge.anyControllerConnected())
|
||||
}
|
||||
|
||||
@Test fun aJoystickWithoutKeyboardCapabilitiesRemainsAController() {
|
||||
assertTrue(PadBridge.isFromController(device(InputDevice.SOURCE_JOYSTICK, InputDevice.KEYBOARD_TYPE_NONE)))
|
||||
assertTrue(PadBridge.isFromController(device(COMPOSITE, InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)))
|
||||
}
|
||||
|
||||
@Test fun joystickRangesDistinguishAHybridFromIncidentalSourceBits() {
|
||||
for (axis in listOf(MotionEvent.AXIS_X, MotionEvent.AXIS_HAT_Y, MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)) {
|
||||
val hybrid = device(COMPOSITE, InputDevice.KEYBOARD_TYPE_ALPHABETIC,
|
||||
rangeSource = InputDevice.SOURCE_JOYSTICK, axis = axis)
|
||||
assertTrue(PadBridge.isFromController(hybrid))
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun physicalControllerButtonsDistinguishAHybridWithoutAxes() {
|
||||
val hybrid = device(COMPOSITE, InputDevice.KEYBOARD_TYPE_ALPHABETIC,
|
||||
keys = intArrayOf(KeyEvent.KEYCODE_A, KeyEvent.KEYCODE_BUTTON_A))
|
||||
assertTrue(hybrid.hasKeys(KeyEvent.KEYCODE_BUTTON_A)[0])
|
||||
assertTrue(PadBridge.isFromController(hybrid))
|
||||
}
|
||||
|
||||
@Test fun absentAndVirtualDevicesDoNotCountAsControllers() {
|
||||
assertFalse(PadBridge.isFromController(null))
|
||||
val virtual = device(InputDevice.SOURCE_GAMEPAD or InputDevice.SOURCE_JOYSTICK,
|
||||
InputDevice.KEYBOARD_TYPE_NONE, id = -1)
|
||||
assertTrue(virtual.isVirtual)
|
||||
assertFalse(PadBridge.isFromController(virtual))
|
||||
}
|
||||
|
||||
@Test fun keyAndMotionOwnershipDoesNotFollowEverySourceOnTheDevice() {
|
||||
val keyboard = device(InputDevice.SOURCE_KEYBOARD, InputDevice.KEYBOARD_TYPE_ALPHABETIC)
|
||||
val composite = device(COMPOSITE, InputDevice.KEYBOARD_TYPE_ALPHABETIC)
|
||||
val hybrid = device(COMPOSITE, InputDevice.KEYBOARD_TYPE_ALPHABETIC, rangeSource = InputDevice.SOURCE_JOYSTICK)
|
||||
val gamepad = device(COMPOSITE or InputDevice.SOURCE_GAMEPAD, InputDevice.KEYBOARD_TYPE_ALPHABETIC)
|
||||
val joystick = device(InputDevice.SOURCE_JOYSTICK, InputDevice.KEYBOARD_TYPE_NONE)
|
||||
val virtual = device(InputDevice.SOURCE_GAMEPAD, InputDevice.KEYBOARD_TYPE_NONE, id = -1)
|
||||
for (action in listOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) {
|
||||
for (device in listOf(keyboard, composite, hybrid, gamepad, joystick)) {
|
||||
assertFalse(PadBridge.isControllerKeyEvent(key(device, KeyEvent.KEYCODE_A, action)))
|
||||
assertFalse(PadBridge.isControllerKeyEvent(key(device, KeyEvent.KEYCODE_A, action, InputDevice.SOURCE_GAMEPAD)))
|
||||
assertFalse(PadBridge.isControllerKeyEvent(key(device, KeyEvent.KEYCODE_VOLUME_UP, action)))
|
||||
}
|
||||
for (code in listOf(KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_RIGHT,
|
||||
KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_BACK,
|
||||
KeyEvent.KEYCODE_MENU, KeyEvent.KEYCODE_HOME)) {
|
||||
for (device in listOf(keyboard, composite, hybrid)) {
|
||||
assertFalse(PadBridge.isControllerKeyEvent(key(device, code, action)))
|
||||
assertFalse(PadBridge.isControllerKeyEvent(key(device, code, action, InputDevice.SOURCE_DPAD)))
|
||||
}
|
||||
assertTrue(PadBridge.isControllerKeyEvent(key(gamepad, code, action)))
|
||||
assertTrue(PadBridge.isControllerKeyEvent(key(joystick, code, action)))
|
||||
assertTrue(PadBridge.isControllerKeyEvent(key(hybrid, code, action, InputDevice.SOURCE_JOYSTICK)))
|
||||
}
|
||||
for (code in listOf(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.KEYCODE_BUTTON_B,
|
||||
KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_BUTTON_R2, KeyEvent.KEYCODE_BUTTON_MODE)) {
|
||||
assertFalse(PadBridge.isControllerKeyEvent(key(keyboard, code, action)))
|
||||
assertFalse(PadBridge.isControllerKeyEvent(key(virtual, code, action)))
|
||||
for (device in listOf(composite, hybrid, gamepad, joystick)) {
|
||||
assertTrue(PadBridge.isControllerKeyEvent(key(device, code, action)))
|
||||
}
|
||||
}
|
||||
}
|
||||
assertNull(KeyEvent(0L, 0L, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_A, 0, 0, 999, 0).device)
|
||||
assertFalse(PadBridge.isControllerKeyEvent(KeyEvent(0L, 0L, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_A, 0, 0, 999, 0)))
|
||||
for (device in listOf(keyboard, composite, hybrid, gamepad, joystick, virtual)) {
|
||||
val motion = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_MOVE, 1,
|
||||
arrayOf(MotionEvent.PointerProperties().apply { id = 0 }),
|
||||
arrayOf(MotionEvent.PointerCoords().apply { setAxisValue(MotionEvent.AXIS_X, 0.75f) }),
|
||||
0, 0, 1f, 1f, device.id, 0, InputDevice.SOURCE_JOYSTICK, 0)
|
||||
try {
|
||||
assertSame(device, motion.device)
|
||||
assertEquals(device in listOf(composite, hybrid, gamepad, joystick), PadBridge.isControllerMotionEvent(motion))
|
||||
motion.source = InputDevice.SOURCE_MOUSE
|
||||
assertFalse(PadBridge.isControllerMotionEvent(motion))
|
||||
} finally { motion.recycle() }
|
||||
}
|
||||
}
|
||||
|
||||
private fun key(device: InputDevice, code: Int, action: Int, source: Int = InputDevice.SOURCE_KEYBOARD): KeyEvent {
|
||||
val event = KeyEvent(0L, 0L, action, code, 0, 0, device.id, 0, 0, source)
|
||||
assertSame(device, event.device)
|
||||
return event
|
||||
}
|
||||
|
||||
private var nextId = 10
|
||||
|
||||
private fun device(sources: Int, type: Int, keys: IntArray = intArrayOf(KeyEvent.KEYCODE_A, KeyEvent.KEYCODE_DPAD_UP),
|
||||
rangeSource: Int? = null, axis: Int = MotionEvent.AXIS_X, id: Int = nextId++): InputDevice {
|
||||
val builder = InputDeviceBuilder.newBuilder().setId(id).setName("routing fixture $id")
|
||||
.setDescriptor("routing-$id").setExternal(true).setSources(sources).setKeyboardType(type)
|
||||
.setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD))
|
||||
if (rangeSource != null) builder.addMotionRange(axis, rangeSource, -1f, 1f, 0f, 0f, 0f)
|
||||
val device = builder.build()
|
||||
val manager = RuntimeEnvironment.getApplication().getSystemService(InputManager::class.java)
|
||||
shadowOf(manager).addInputDevice(device)
|
||||
shadowOf(manager).addInputDeviceKeys(id, keys)
|
||||
assertSame(device, InputDevice.getDevice(id))
|
||||
assertEquals(sources, device.sources)
|
||||
assertEquals(type, device.keyboardType)
|
||||
return device
|
||||
}
|
||||
|
||||
private companion object { const val COMPOSITE = 0x1002313 }
|
||||
|
||||
// Robolectric's default InputEvent shadow has its own nullable device field. Use Android's
|
||||
// original lookup so key and motion events resolve the registered device by id.
|
||||
@Implements(KeyEvent::class)
|
||||
class RegisteredKeyEvent : ShadowKeyEvent() {
|
||||
@RealObject private lateinit var event: KeyEvent
|
||||
@Implementation override fun getDevice(): InputDevice? = Shadow.directlyOn(event as InputEvent, InputEvent::class.java, "getDevice")
|
||||
}
|
||||
|
||||
@Implements(MotionEvent::class)
|
||||
class RegisteredMotionEvent : ShadowMotionEvent() {
|
||||
@RealObject private lateinit var event: MotionEvent
|
||||
@Implementation override fun getDevice(): InputDevice? = Shadow.directlyOn(event as InputEvent, InputEvent::class.java, "getDevice")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user