feat(input): a pen moves the pointer while hovering, clicks with its tip and right-clicks with its side button (#2625)

* feat(compositor): pen samples drive the pointer like a mouse

Input record 13 carries the pen's state (in proximity, tip, right and
middle button) and its position in input space as 24.8 fixed point.
Hover moves the pointer, the tip holds the left button, and a record
without the in-proximity bit releases every button. Under a pointer
lock only the buttons pass. A button pressed or released mid-stroke
keeps the implicit grab on the window the stroke started in, unless
that window closed.

* feat(input): a pen moves the pointer while hovering, clicks with its tip and right-clicks with its side button

In Mouse mode (the default) a pen's hover moves the pointer, the tip
holds the left button and the side button right-clicks; Touch keeps it a
finger. The side button can send a middle click or nothing instead, and
a Bluetooth pen key counts as the side button while the pen is near.
With glasses or a program's pointer lock the pen drives the touchpad.
A hover exit followed within 50 ms by more hover or a touch keeps the
pen near, so a pen at the edge of its hover range does not flicker out,
and a hover that sends nothing for 2 s leaves proximity. On the desktop
a pen that comes near releases KWin's grab of a hardware mouse, so the
pen moves KWin's pointer. Whatever cancels touches also ends the pen,
and stylus devices no longer count as hardware mice.

* feat(input): pen mode and side button settings in the Controls tab and the session drawer

A Pen group follows the touch settings once the phone has a stylus
device or a pen event was seen, which is remembered as penSeen and kept
by a settings reset. It holds Pen (Mouse, Touch) and Side button (Right
click, Middle click, Off); Side button is hidden in Touch. Before
Android 14 a touchscreen reports the stylus source without a pen, so
there it counts only with its first pen event. The strings are
translated into all 32 languages.

* docs: describe pen input and its limits

---------

Co-authored-by: alien2003 <11610652+alien2003@users.noreply.github.com>
This commit is contained in:
alien2003andalien2003 authored and GitHub committed 2026-11-15 09:38:01 -05:00
1 parent de70e37f30
commit decab6ea90
63 files changed
+2352 -38

No files matched your search

@@ -40,4 +40,14 @@ class InputSpaceTest {
assertEquals(InputSpace.HEIGHT - 1, InputSpace.y(5000f, 1504))
assertEquals(0, InputSpace.y(10f, 0))
}
@Test fun fixedPositionsKeepTheFractionOfAnInputUnit() {
assertEquals(960 * 256, InputSpace.fixedX(1200f, 2400))
assertEquals(1700, InputSpace.fixedX(5.534f, 1600))
assertEquals(InputSpace.x(5.534f, 1600), InputSpace.fixedX(5.534f, 1600) / InputSpace.FIXED_ONE)
assertEquals(InputSpace.WIDTH * InputSpace.FIXED_ONE - 1, InputSpace.fixedX(2400f, 2400))
assertEquals(InputSpace.HEIGHT * InputSpace.FIXED_ONE - 1, InputSpace.fixedY(5000f, 1504))
assertEquals(0, InputSpace.fixedY(-1f, 1504))
assertEquals(0, InputSpace.fixedY(10f, 0))
}
}
@@ -0,0 +1,27 @@
package com.droiddeck.launcher.input
import com.droiddeck.launcher.session.SessionPrefs
import org.junit.Assert.assertEquals
import org.junit.Test
class PenRouterTest {
@Test fun appUiBlocksThePenInEveryModeAndScreen() {
for (mode in listOf(SessionPrefs.PEN_MOUSE, SessionPrefs.PEN_TOUCH))
for (glasses in listOf(false, true)) for (lock in listOf(false, true))
assertEquals("$mode $glasses $lock", PenRouter.Route.BLOCKED, PenRouter.decide(true, mode, glasses, lock))
}
@Test fun touchModeKeepsThePenAFingerEverywhere() {
for (glasses in listOf(false, true)) for (lock in listOf(false, true))
assertEquals("$glasses $lock", PenRouter.Route.TOUCH, PenRouter.decide(false, SessionPrefs.PEN_TOUCH, glasses, lock))
}
@Test fun glassesAndProgramLocksMakeTheMouseRelative() {
for ((glasses, lock) in listOf(true to false, false to true, true to true))
assertEquals("$glasses $lock", PenRouter.Route.RELATIVE, PenRouter.decide(false, SessionPrefs.PEN_MOUSE, glasses, lock))
}
@Test fun mouseDrivesThePenRouteOnThePhone() {
assertEquals(PenRouter.Route.PEN, PenRouter.decide(false, SessionPrefs.PEN_MOUSE, false, false))
}
}
@@ -0,0 +1,384 @@
package com.droiddeck.launcher.input
import android.graphics.RectF
import android.os.Looper
import android.os.SystemClock
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import com.droiddeck.launcher.input.PenStream.Companion.PEN_IN
import com.droiddeck.launcher.input.PenStream.Companion.PEN_MIDDLE
import com.droiddeck.launcher.input.PenStream.Companion.PEN_RIGHT
import com.droiddeck.launcher.input.PenStream.Companion.PEN_TIP
import com.droiddeck.launcher.session.SessionPrefs
import java.time.Duration
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class PenStreamTest {
data class Record(val flags: Int, val x: Int, val y: Int)
data class Sample(val time: Long, val x: Float, val y: Float = 300f)
private val records = mutableListOf<Record>()
private val stream = PenStream { flags, x, y -> records += Record(flags, x, y) }
private val area = RectF(0f, 0f, 1000f, 600f)
@Test fun hoverEntersMovesAndLeavesProximity() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 200f))
send(MotionEvent.ACTION_HOVER_EXIT, Sample(30, 250f))
assertEquals(listOf(PEN_IN, PEN_IN), flags())
idle()
assertEquals(listOf(PEN_IN, PEN_IN), flags())
grace()
assertEquals(listOf(PEN_IN, PEN_IN, 0), flags())
assertEquals(listOf(100f, 200f).map(::x), records.take(2).map { it.x })
assertFalse(stream.inProximity)
}
@Test fun hoverExitThenDownWithTheSameTimeStaysInProximity() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f))
send(MotionEvent.ACTION_DOWN, Sample(20, 100f))
grace()
assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP), flags())
}
@Test fun hoverExitThenADownAfterTheGraceLeavesProximityFirst() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f))
send(MotionEvent.ACTION_DOWN, Sample(21 + PenStream.EXIT_GRACE_MS, 100f))
assertEquals(listOf(PEN_IN, 0, PEN_IN or PEN_TIP), flags())
}
@Test fun aPenFlickeringAtTheEdgeOfHoverRangeStaysInProximity() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
for (i in 0 until 5) {
send(MotionEvent.ACTION_HOVER_EXIT, Sample(20L + i * 22, 100f))
send(MotionEvent.ACTION_HOVER_ENTER, Sample(34L + i * 22, 101f))
}
send(MotionEvent.ACTION_HOVER_EXIT, Sample(130, 101f))
send(MotionEvent.ACTION_DOWN, Sample(130 + PenStream.EXIT_GRACE_MS, 101f))
grace()
assertEquals(List(6) { PEN_IN } + (PEN_IN or PEN_TIP), flags())
assertTrue(stream.inProximity)
}
@Test fun liftingIntoHoverKeepsProximityAndLiftingAwayLeavesIt() {
send(MotionEvent.ACTION_DOWN, Sample(10, 100f))
send(MotionEvent.ACTION_UP, Sample(20, 110f))
send(MotionEvent.ACTION_HOVER_ENTER, Sample(20, 110f))
grace()
assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN, PEN_IN), flags())
records.clear()
send(MotionEvent.ACTION_DOWN, Sample(30, 100f))
send(MotionEvent.ACTION_UP, Sample(40, 100f))
grace()
assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN, 0), flags())
}
@Test fun aPenKeyDuringTheExitGraceEndsProximityFirst() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f))
assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 25)))
grace()
assertEquals(listOf(PEN_IN, 0), flags())
}
@Test fun historicalSamplesGoOutOldestFirst() {
send(MotionEvent.ACTION_DOWN, Sample(100, 10f))
send(MotionEvent.ACTION_MOVE, Sample(103, 20f), Sample(106, 30f), Sample(109, 40f))
assertEquals(listOf(10f, 20f, 30f, 40f).map(::x), records.map { it.x })
assertTrue(records.all { it.flags == PEN_IN or PEN_TIP })
assertEquals(y(300f), records.last().y)
}
@Test fun cancelEndsTheStrokeAndIgnoresTheRest() {
send(MotionEvent.ACTION_DOWN, Sample(10, 100f))
send(MotionEvent.ACTION_MOVE, Sample(20, 150f))
send(MotionEvent.ACTION_CANCEL, Sample(30, 160f))
send(MotionEvent.ACTION_MOVE, Sample(40, 170f))
send(MotionEvent.ACTION_UP, Sample(50, 170f))
assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, 0), flags())
assertFalse(stream.inProximity)
}
@Test fun anUpThatAndroidCanceledIsACancelNotALift() {
send(MotionEvent.ACTION_DOWN, Sample(10, 100f))
send(MotionEvent.ACTION_UP, Sample(20, 100f), flags = MotionEvent.FLAG_CANCELED)
assertEquals(listOf(PEN_IN or PEN_TIP, 0), flags())
}
@Test fun theSideButtonSettingMapsThePrimaryButtonAndTheSecondaryGetsTheOtherClick() {
val cases = mapOf(SessionPrefs.PEN_BUTTON_RIGHT to (PEN_RIGHT to PEN_MIDDLE),
SessionPrefs.PEN_BUTTON_MIDDLE to (PEN_MIDDLE to PEN_RIGHT), SessionPrefs.PEN_BUTTON_OFF to (0 to PEN_RIGHT))
for ((setting, expected) in cases) {
assertEquals(setting, 0, PenStream.buttonFlags(primary = false, secondary = false, sideButton = setting))
assertEquals(setting, expected.first, PenStream.buttonFlags(primary = true, secondary = false, sideButton = setting))
assertEquals(setting, expected.second, PenStream.buttonFlags(primary = false, secondary = true, sideButton = setting))
assertEquals(setting, expected.first or expected.second, PenStream.buttonFlags(primary = true, secondary = true, sideButton = setting))
}
stream.sideButton = SessionPrefs.PEN_BUTTON_MIDDLE
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY)
send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 100f), buttons = MotionEvent.BUTTON_STYLUS_SECONDARY)
assertEquals(listOf(PEN_IN or PEN_MIDDLE, PEN_IN or PEN_RIGHT), flags())
}
@Test fun barrelButtonsFollowTheButtonState() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY)
send(MotionEvent.ACTION_BUTTON_PRESS, Sample(21, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY or MotionEvent.BUTTON_STYLUS_SECONDARY)
send(MotionEvent.ACTION_DOWN, Sample(30, 100f), buttons = MotionEvent.BUTTON_STYLUS_SECONDARY)
send(MotionEvent.ACTION_MOVE, Sample(40, 100f))
assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, PEN_IN or PEN_RIGHT or PEN_MIDDLE, PEN_IN or PEN_TIP or PEN_MIDDLE,
PEN_IN or PEN_TIP), flags())
}
@Test fun aBluetoothPenKeyIsTheSideButtonWhileThePenIsNear() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 15)))
assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 16, repeat = 1)))
send(MotionEvent.ACTION_HOVER_MOVE, Sample(20, 130f))
assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 25)))
stream.cancel()
stream.sideButton = SessionPrefs.PEN_BUTTON_MIDDLE
send(MotionEvent.ACTION_HOVER_ENTER, Sample(30, 100f))
stream.onKey(key(KeyEvent.ACTION_DOWN, 35))
stream.onKey(key(KeyEvent.ACTION_UP, 36))
stream.cancel()
assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, PEN_IN or PEN_RIGHT, PEN_IN, 0,
PEN_IN, PEN_IN or PEN_MIDDLE, PEN_IN, 0), flags())
assertEquals(x(130f), records[2].x)
records.clear()
stream.sideButton = SessionPrefs.PEN_BUTTON_OFF
send(MotionEvent.ACTION_HOVER_ENTER, Sample(40, 100f))
assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 45)))
assertTrue(stream.holdsKey(key(KeyEvent.ACTION_UP, 46)))
assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 46)))
assertEquals(listOf(PEN_IN), flags())
}
@Test fun aBluetoothPenKeyIsConsumedWhenThePenIsAway() {
assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 10)))
send(MotionEvent.ACTION_HOVER_ENTER, Sample(20, 100f))
assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 30)))
assertEquals(listOf(PEN_IN), flags())
}
@Test fun aKeyHeldWhileThePenIsAwayIsReleasedAndPressedAgainOnReturn() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
stream.onKey(key(KeyEvent.ACTION_DOWN, 15))
send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f))
grace()
stream.onKey(key(KeyEvent.ACTION_DOWN, 500, repeat = 1))
stream.onKey(key(KeyEvent.ACTION_DOWN, 550, repeat = 2))
send(MotionEvent.ACTION_HOVER_ENTER, Sample(600, 100f))
send(MotionEvent.ACTION_DOWN, Sample(610, 100f))
send(MotionEvent.ACTION_UP, Sample(620, 100f))
send(MotionEvent.ACTION_HOVER_ENTER, Sample(620, 100f))
stream.onKey(key(KeyEvent.ACTION_UP, 630))
val right = PEN_IN or PEN_RIGHT
assertEquals(listOf(PEN_IN, right, 0, right, right or PEN_TIP, right, right, PEN_IN), flags())
}
@Test fun aKeyReleasedWhileThePenIsAwayStaysReleased() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
stream.onKey(key(KeyEvent.ACTION_DOWN, 15))
send(MotionEvent.ACTION_HOVER_EXIT, Sample(20, 100f))
grace()
assertTrue(stream.holdsKey(key(KeyEvent.ACTION_UP, 40)))
assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 40)))
assertFalse(stream.holdsKey(key(KeyEvent.ACTION_UP, 41)))
send(MotionEvent.ACTION_HOVER_ENTER, Sample(50, 100f))
assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, 0, PEN_IN), flags())
}
@Test fun onlyALifecycleCancelForgetsAHeldKeyAndARepeatBringsItBack() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
stream.onKey(key(KeyEvent.ACTION_DOWN, 15))
stream.cancel()
send(MotionEvent.ACTION_HOVER_ENTER, Sample(20, 100f))
stream.cancel(releaseKey = true)
send(MotionEvent.ACTION_HOVER_ENTER, Sample(30, 100f))
assertFalse(stream.holdsKey(key(KeyEvent.ACTION_UP, 35)))
stream.onKey(key(KeyEvent.ACTION_DOWN, 40, repeat = 1))
stream.onKey(key(KeyEvent.ACTION_UP, 50))
val right = PEN_IN or PEN_RIGHT
assertEquals(listOf(PEN_IN, right, 0, right, 0, PEN_IN, right, PEN_IN), flags())
}
@Test fun removingTheBluetoothPenReleasesItsKeyAndForgetsIt() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f))
stream.onKey(key(KeyEvent.ACTION_DOWN, 15))
stream.deviceRemoved(10)
assertFalse(stream.holdsKey(key(KeyEvent.ACTION_UP, 18)))
assertTrue(stream.inProximity)
stream.onKey(key(KeyEvent.ACTION_DOWN, 20))
send(MotionEvent.ACTION_HOVER_EXIT, Sample(25, 100f))
grace()
stream.deviceRemoved(10)
send(MotionEvent.ACTION_HOVER_ENTER, Sample(30, 100f))
assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, PEN_IN, PEN_IN or PEN_RIGHT, 0, PEN_IN), flags())
}
@Test fun aHoverThatStopsReportingCountsAsGone() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(1_000, 100f))
send(MotionEvent.ACTION_HOVER_MOVE, Sample(2_000, 100f))
send(MotionEvent.ACTION_HOVER_MOVE, Sample(4_500, 110f))
assertEquals(listOf(PEN_IN, PEN_IN, 0, PEN_IN), flags())
records.clear()
assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 7_000)))
assertEquals(listOf(0), flags())
}
@Test fun aPenKeyDoesNotKeepASilentHoverNear() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(1_000, 100f))
assertTrue(stream.onKey(key(KeyEvent.ACTION_DOWN, 2_500)))
assertTrue(stream.onKey(key(KeyEvent.ACTION_UP, 3_000)))
assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, 0), flags())
}
@Test fun aSilentHoverIsSentOutByTheWatchdogWithoutAnotherEvent() {
val start = SystemClock.uptimeMillis()
send(MotionEvent.ACTION_HOVER_ENTER, Sample(start, 100f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1_500))
send(MotionEvent.ACTION_HOVER_MOVE, Sample(start + 1_500, 110f), buttons = MotionEvent.BUTTON_STYLUS_PRIMARY)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.HOVER_TIMEOUT_MS - 1))
assertTrue(stream.inProximity)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1))
assertFalse(stream.inProximity)
assertEquals(listOf(PEN_IN or PEN_RIGHT, PEN_IN or PEN_RIGHT, 0), flags())
}
@Test fun theWatchdogNeverEndsAContact() {
val start = SystemClock.uptimeMillis()
send(MotionEvent.ACTION_HOVER_ENTER, Sample(start, 100f))
send(MotionEvent.ACTION_DOWN, Sample(start + 10, 100f))
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(5))
assertTrue(stream.inProximity)
assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP), flags())
}
@Test fun aStrokeIsNeverTimedOut() {
send(MotionEvent.ACTION_DOWN, Sample(1_000, 100f))
send(MotionEvent.ACTION_MOVE, Sample(9_000, 110f))
assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP), flags())
}
@Test fun lifecycleCancelEndsEverythingOnce() {
send(MotionEvent.ACTION_DOWN, Sample(10, 100f))
stream.cancel()
stream.cancel()
assertEquals(listOf(PEN_IN or PEN_TIP, 0), flags())
}
@Test fun removingThePenDeviceEndsItsStream() {
send(MotionEvent.ACTION_HOVER_ENTER, Sample(10, 100f), device = 6)
stream.deviceRemoved(8)
assertTrue(stream.inProximity)
stream.deviceRemoved(6)
assertEquals(listOf(PEN_IN, 0), flags())
}
@Test fun aPenNextToAFingerKeepsItsOwnStroke() {
val finger = MotionEvent.TOOL_TYPE_FINGER
val stylus = MotionEvent.TOOL_TYPE_STYLUS
mixed(MotionEvent.ACTION_DOWN, 10, finger to 50f)
assertTrue(records.isEmpty())
mixed(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 20, finger to 50f, stylus to 400f)
mixed(MotionEvent.ACTION_MOVE, 30, finger to 60f, stylus to 410f)
mixed(MotionEvent.ACTION_POINTER_UP, 40, finger to 60f, stylus to 420f)
mixed(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 50, finger to 60f, stylus to 430f)
assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, PEN_IN), flags())
assertEquals(listOf(400f, 410f, 420f, 430f).map(::x), records.map { it.x })
}
@Test fun positionsAreClampedToThePicture() {
val event = event(MotionEvent.ACTION_HOVER_ENTER, listOf(Sample(10, 40f, y = 580f)), MotionEvent.TOOL_TYPE_STYLUS)
try { stream.onMotion(event, 0, RectF(100f, 50f, 900f, 550f), 1000, 600) } finally { event.recycle() }
assertEquals(Record(PEN_IN, x(100f), y(550f)), records.single())
}
@Test fun pensAreFoundByToolTypeFirstAndSourceSecond() {
fun index(tool: Int, source: Int): Int {
val event = event(MotionEvent.ACTION_DOWN, listOf(Sample(0, 1f)), tool, source = source)
return try { PenStream.penIndex(event) } finally { event.recycle() }
}
assertEquals(0, index(MotionEvent.TOOL_TYPE_STYLUS, InputDevice.SOURCE_MOUSE))
assertEquals(0, index(MotionEvent.TOOL_TYPE_ERASER, InputDevice.SOURCE_TOUCHSCREEN))
assertEquals(0, index(MotionEvent.TOOL_TYPE_UNKNOWN, InputDevice.SOURCE_STYLUS))
assertEquals(-1, index(MotionEvent.TOOL_TYPE_FINGER, InputDevice.SOURCE_STYLUS))
assertEquals(-1, index(MotionEvent.TOOL_TYPE_MOUSE, InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS))
assertEquals(-1, index(MotionEvent.TOOL_TYPE_UNKNOWN, InputDevice.SOURCE_TOUCHSCREEN))
}
@Test fun penButtonKeyboardsAreRecognisedByNameAndShape() {
val keyboard = InputDevice.SOURCE_KEYBOARD
assertTrue(PenStream.isPenKeyboard("OPPO AI Pen", keyboard, external = true, alphabetic = false))
assertTrue(PenStream.isPenKeyboard("Xiaomi Smart STYLUS", keyboard, external = true, alphabetic = false))
assertTrue(PenStream.isPenKeyboard("HUAWEI M-Pencil", keyboard, external = true, alphabetic = false))
assertFalse(PenStream.isPenKeyboard("OPPO AI Pen", keyboard, external = false, alphabetic = false))
assertFalse(PenStream.isPenKeyboard("OPPO AI Pen", keyboard or InputDevice.SOURCE_MOUSE, external = true, alphabetic = false))
assertFalse(PenStream.isPenKeyboard("Pen Keyboard K380", keyboard, external = true, alphabetic = true))
assertFalse(PenStream.isPenKeyboard("Open Remote", keyboard, external = true, alphabetic = false))
assertFalse(PenStream.isPenKeyboard("8BitDo Remote", keyboard, external = true, alphabetic = false))
assertFalse(PenStream.isPenKeyboard(null, keyboard, external = true, alphabetic = false))
}
@Test fun aTouchscreenCountsAsAPenDeviceOnlyWhereAndroidSetsTheStylusBitForPens() {
val screen = InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS
assertFalse(PenStream.isPenDevice(screen, sdk = 33))
assertTrue(PenStream.isPenDevice(screen, sdk = 34))
for (sdk in listOf(26, 33, 34)) {
assertTrue(PenStream.isPenDevice(InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS, sdk))
assertTrue(PenStream.isPenDevice(InputDevice.SOURCE_BLUETOOTH_STYLUS, sdk))
assertTrue(PenStream.isPenDevice(InputDevice.SOURCE_STYLUS, sdk))
assertFalse(PenStream.isPenDevice(InputDevice.SOURCE_TOUCHSCREEN, sdk))
assertFalse(PenStream.isPenDevice(InputDevice.SOURCE_MOUSE, sdk))
}
}
private fun flags() = records.map { it.flags }
private fun x(position: Float) = InputSpace.fixedX(position, 1000)
private fun y(position: Float) = InputSpace.fixedY(position, 600)
private fun idle() = shadowOf(Looper.getMainLooper()).idle()
private fun grace() = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.EXIT_GRACE_MS))
private fun key(action: Int, time: Long, repeat: Int = 0) =
KeyEvent(time, time, action, KeyEvent.KEYCODE_UNKNOWN, repeat, 0, 10, 191, 0, InputDevice.SOURCE_KEYBOARD)
private fun send(action: Int, vararg samples: Sample, buttons: Int = 0, flags: Int = 0, device: Int = 6) {
val event = event(action, samples.toList(), MotionEvent.TOOL_TYPE_STYLUS, buttons, flags, device = device)
try { stream.onMotion(event, 0, area, 1000, 600) } finally { event.recycle() }
}
private fun mixed(action: Int, time: Long, vararg pointers: Pair<Int, Float>) {
val properties = Array(pointers.size) { MotionEvent.PointerProperties().apply { id = it; toolType = pointers[it].first } }
val coords = Array(pointers.size) { MotionEvent.PointerCoords().apply { x = pointers[it].second; y = 300f; pressure = 0.5f } }
val event = MotionEvent.obtain(0, time, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 6, 0,
InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS, 0)
try {
val pen = PenStream.penIndex(event)
if (pen >= 0) stream.onMotion(event, pen, area, 1000, 600)
} finally { event.recycle() }
}
private fun event(action: Int, samples: List<Sample>, tool: Int, buttons: Int = 0, flags: Int = 0,
source: Int = InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS, device: Int = 6): MotionEvent {
val properties = arrayOf(MotionEvent.PointerProperties().apply { id = 0; toolType = tool })
fun coords(sample: Sample) = arrayOf(MotionEvent.PointerCoords().apply {
x = sample.x
y = sample.y
})
val first = samples.first()
val event = MotionEvent.obtain(0, first.time, action, 1, properties, coords(first), 0, buttons, 1f, 1f, device, 0, source, flags)
for (sample in samples.drop(1)) event.addBatch(sample.time, coords(sample), 0)
return event
}
}
@@ -0,0 +1,57 @@
package com.droiddeck.launcher.input
import android.view.InputDevice
import android.view.MotionEvent
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class PointerSubsetTest {
@Test fun aForwardedPointerKeepsItsHistoryAndTimes() {
val properties = Array(2) { MotionEvent.PointerProperties().apply { id = it; toolType = if (it == 0) MotionEvent.TOOL_TYPE_FINGER else MotionEvent.TOOL_TYPE_STYLUS } }
fun coords(step: Int) = Array(2) { MotionEvent.PointerCoords().apply { x = 100f * it + step; y = 50f; pressure = 0.1f * step } }
val event = MotionEvent.obtain(5, 10, MotionEvent.ACTION_MOVE, 2, properties, coords(1), 0, 0, 1f, 1f, 3, 0,
InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS, MotionEvent.FLAG_CANCELED)
event.addBatch(14, coords(2), 0)
event.addBatch(18, coords(3), 0)
val split = event.pointerSubset(1 shl 1)!!
try {
assertEquals(1, split.pointerCount)
assertEquals(MotionEvent.TOOL_TYPE_STYLUS, split.getToolType(0))
assertEquals(2, split.historySize)
assertEquals(listOf(10L, 14L), (0 until split.historySize).map { split.getHistoricalEventTime(it) })
assertEquals(listOf(101f, 102f), (0 until split.historySize).map { split.getHistoricalX(0, it) })
assertEquals(0.2f, split.getHistoricalPressure(0, 1), 1e-6f)
assertEquals(18L, split.eventTime)
assertEquals(103f, split.x, 0f)
assertEquals(5L, split.downTime)
assertEquals(MotionEvent.ACTION_MOVE, split.actionMasked)
assertEquals(event.source, split.source)
assertEquals(MotionEvent.FLAG_CANCELED, split.flags and MotionEvent.FLAG_CANCELED)
} finally {
split.recycle()
event.recycle()
}
}
@Test fun anEventWithoutHistoryStaysASingleSample() {
val properties = arrayOf(MotionEvent.PointerProperties().apply { id = 2; toolType = MotionEvent.TOOL_TYPE_FINGER })
val coords = arrayOf(MotionEvent.PointerCoords().apply { x = 7f; y = 8f })
val event = MotionEvent.obtain(0, 20, MotionEvent.ACTION_DOWN, 1, properties, coords, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0)
val split = event.pointerSubset(1 shl 2)!!
try {
assertEquals(0, split.historySize)
assertEquals(20L, split.eventTime)
assertEquals(MotionEvent.ACTION_DOWN, split.actionMasked)
assertEquals(7f, split.x, 0f)
assertNull(event.pointerSubset(1 shl 0))
} finally {
split.recycle()
event.recycle()
}
}
}
@@ -0,0 +1,426 @@
package com.droiddeck.launcher.input
import android.hardware.input.InputManager
import android.os.Looper
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.SurfaceView
import androidx.compose.runtime.MutableState
import androidx.compose.ui.geometry.Offset
import com.droiddeck.launcher.SessionActivity
import com.droiddeck.launcher.input.PenStream.Companion.PEN_IN
import com.droiddeck.launcher.input.PenStream.Companion.PEN_MIDDLE
import com.droiddeck.launcher.input.PenStream.Companion.PEN_RIGHT
import com.droiddeck.launcher.input.PenStream.Companion.PEN_TIP
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.wayland.WaylandCompositor
import java.time.Duration
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.Implementation
import org.robolectric.annotation.Implements
import org.robolectric.shadows.InputDeviceBuilder
import org.robolectric.shadows.ShadowInputDevice
import org.robolectric.util.ReflectionHelpers
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE, shadows = [SessionPenRoutingTest.CompositorShadow::class])
class SessionPenRoutingTest {
private lateinit var activity: SessionActivity
private val previousSize = SessionState.outputSize
private val previousMode = SessionState.mode
private val previousSteamUi = SessionState.steamUi
private val pen = MotionEvent.TOOL_TYPE_STYLUS
private val finger = MotionEvent.TOOL_TYPE_FINGER
private val stylusScreen = InputDevice.SOURCE_TOUCHSCREEN or InputDevice.SOURCE_STYLUS
@Before fun setUp() {
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(2))
activity = Robolectric.buildActivity(SessionActivity::class.java).get()
activity.window.decorView
activity.getSharedPreferences("session", 0).edit().clear().commit()
field("surfaceView", SurfaceView(activity).apply { layout(0, 0, 1000, 600) })
SessionState.outputSize = 1000 to 600
SessionState.mode = SessionService.MODE_DESKTOP
SessionState.steamUi = null
CompositorShadow.clear()
}
@After fun tearDown() {
shadowOf(Looper.getMainLooper()).idle()
SessionState.outputSize = previousSize
SessionState.mode = previousMode
SessionState.steamUi = previousSteamUi
}
@Test fun penHoverMovesThePointerAndTheCursorOnTheDesktopAndInSteam() {
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
assertEquals(listOf(Pen(PEN_IN, InputSpace.fixedX(500f, 1000), InputSpace.fixedY(300f, 600))), CompositorShadow.pens)
assertTrue(state<Boolean>("cursorVisible").value)
assertEquals(Offset(500f, 300f), state<Offset>("cursorPos").value)
steamDirect(SessionPrefs.PEN_MOUSE)
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 600f)))
assertEquals(Pen(PEN_IN, InputSpace.fixedX(600f, 1000), InputSpace.fixedY(300f, 600)), CompositorShadow.pens.last())
assertEquals(Offset(600f, 300f), state<Offset>("cursorPos").value)
assertTrue(CompositorShadow.pointers.isEmpty())
assertTrue(CompositorShadow.touches.isEmpty())
}
@Test fun theSideButtonSettingDecidesWhatThePrimaryButtonSends() {
val cases = listOf(SessionPrefs.PEN_BUTTON_RIGHT to PEN_RIGHT, SessionPrefs.PEN_BUTTON_MIDDLE to PEN_MIDDLE,
SessionPrefs.PEN_BUTTON_OFF to 0)
for ((step, case) in cases.withIndex()) {
SessionPrefs.setPenSetting(activity, "penButton", case.first)
invoke("readPrefs")
CompositorShadow.pens.clear()
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10L + 10 * step, pen to 500f,
buttons = MotionEvent.BUTTON_STYLUS_PRIMARY)))
assertEquals(case.first, listOf(PEN_IN or case.second), CompositorShadow.pens.map { it.flags })
}
}
@Test fun penContactInMouseModeHoldsTheTipUntilItLifts() {
steamDirect(SessionPrefs.PEN_MOUSE)
assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f)))
assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_MOVE, 20, pen to 520f)))
assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_UP, 30, pen to 520f)))
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.EXIT_GRACE_MS))
assertEquals(listOf(PEN_IN or PEN_TIP, PEN_IN or PEN_TIP, PEN_IN, 0), CompositorShadow.pens.map { it.flags })
assertTrue(CompositorShadow.touches.isEmpty())
}
@Test fun anExternalTabletTipReachesThePenInsteadOfTheMouse() {
steamDirect(SessionPrefs.PEN_MOUSE)
val source = InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS
assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f, source = source)))
assertEquals(listOf(PEN_IN or PEN_TIP), CompositorShadow.pens.map { it.flags })
assertTrue(CompositorShadow.pointers.isEmpty())
}
@Test fun penContactInTouchModeTakesTheFingerRouteAsBefore() {
steamDirect(SessionPrefs.PEN_TOUCH)
assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f)))
assertEquals(listOf(Touch(0, 0, InputSpace.x(500f, 1000), InputSpace.y(300f, 600))), CompositorShadow.touches)
activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 600f))
assertTrue(CompositorShadow.pens.isEmpty())
assertTrue(CompositorShadow.pointers.isEmpty())
}
@Test fun anExternalTabletStaysOnTheMousePathInTouchMode() {
steamDirect(SessionPrefs.PEN_TOUCH)
val source = InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS
assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, pen to 500f, source = source)))
assertTrue(CompositorShadow.touches.isEmpty())
assertTrue(CompositorShadow.pens.isEmpty())
}
@Test fun fingersKeepTheirRouteWhateverThePenMode() {
for (mode in listOf(SessionPrefs.PEN_MOUSE, SessionPrefs.PEN_TOUCH)) {
steamDirect(mode)
CompositorShadow.clear()
assertTrue(activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, finger to 500f, source = InputDevice.SOURCE_TOUCHSCREEN)))
assertEquals(listOf(Touch(0, 0, InputSpace.x(500f, 1000), InputSpace.y(300f, 600))), CompositorShadow.touches)
assertTrue(CompositorShadow.pens.isEmpty())
activity.onTouchEvent(event(MotionEvent.ACTION_CANCEL, 20, finger to 500f, source = InputDevice.SOURCE_TOUCHSCREEN))
}
}
@Test fun aFingerAndAPenInOneEventAreSplitByToolType() {
steamDirect(SessionPrefs.PEN_MOUSE)
activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10, finger to 100f))
activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 20, finger to 110f, pen to 500f))
assertEquals(listOf(PEN_IN or PEN_TIP), CompositorShadow.pens.map { it.flags })
assertEquals(InputSpace.fixedX(500f, 1000), CompositorShadow.pens.last().x)
assertTrue(CompositorShadow.touches.isNotEmpty())
assertTrue(CompositorShadow.touches.all { it.id == 0 && it.x < InputSpace.x(200f, 1000) })
}
@Test fun theLowerScreenGameTakesPenHoverAndContact() {
steamDirect(SessionPrefs.PEN_MOUSE)
fun hover(event: MotionEvent) = ReflectionHelpers.callInstanceMethod<Boolean>(activity, "onBottomGameHover",
ReflectionHelpers.ClassParameter.from(MotionEvent::class.java, event))
fun touch(event: MotionEvent) = ReflectionHelpers.callInstanceMethod<Boolean>(activity, "onBottomGameTouch",
ReflectionHelpers.ClassParameter.from(MotionEvent::class.java, event))
assertTrue(hover(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
assertTrue(touch(event(MotionEvent.ACTION_DOWN, 20, pen to 500f, source = InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS)))
assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP), CompositorShadow.pens.map { it.flags })
SessionPrefs.setPenSetting(activity, "pen", SessionPrefs.PEN_TOUCH)
invoke("readPrefs")
assertFalse(hover(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f)))
assertFalse(hover(event(MotionEvent.ACTION_HOVER_MOVE, 40, finger to 500f, source = InputDevice.SOURCE_MOUSE)))
assertEquals(listOf(PEN_IN, PEN_IN or PEN_TIP, 0), CompositorShadow.pens.map { it.flags })
}
@Test fun glassesAProgramPointerLockAndAppUiDropPenHover() {
state<Boolean>("glassesOutputActive").value = true
activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))
state<Boolean>("glassesOutputActive").value = false
SessionState.mode = SessionService.MODE_STEAM
field("pointerLocked", true)
activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 500f))
field("pointerLocked", false)
state<Boolean>("achievementsOpen").value = true
activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f))
assertTrue(CompositorShadow.pens.isEmpty())
assertTrue(CompositorShadow.pointers.isEmpty())
}
@Config(sdk = [34])
@Test fun aPenNearTheDesktopReleasesTheMouseGrabUntilItLeaves() {
shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice(
InputDeviceBuilder.newBuilder().setId(11).setName("mouse").setSources(InputDevice.SOURCE_MOUSE).build())
field("desktopMousePlaced", true)
field("pointerLocked", true)
val start = android.os.SystemClock.uptimeMillis()
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, start, pen to 500f)))
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(1))
assertEquals(listOf(RIGHT_CTRL to 1, RIGHT_CTRL to 0), CompositorShadow.keys)
field("pointerLocked", false)
invoke("grabDesktopMouse")
assertEquals(2, CompositorShadow.keys.size)
activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_EXIT, start + 1_000, pen to 500f))
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(PenStream.EXIT_GRACE_MS))
invoke("grabDesktopMouse")
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(1))
assertEquals(listOf(RIGHT_CTRL to 1, RIGHT_CTRL to 0, RIGHT_CTRL to 1, RIGHT_CTRL to 0), CompositorShadow.keys)
}
@Test fun openingAppUiEndsAHoveringPen() {
activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f))
state<Boolean>("achievementsOpen").value = true
activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 510f))
assertEquals(listOf(PEN_IN, 0), CompositorShadow.pens.map { it.flags })
}
@Test fun touchModeAndRouteChangesDeviceRemovalAndPauseEndThePen() {
invoke("readPrefs")
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
SessionPrefs.setTouchMode(activity, SessionPrefs.TOUCH_DIRECT)
invoke("readPrefs")
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 500f)))
field("routedTouchMode", SessionPrefs.TOUCH_PAD)
invoke("applyTouchRouting")
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f)))
ReflectionHelpers.getField<InputManager.InputDeviceListener>(activity, "deviceListener").onInputDeviceRemoved(6)
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 40, pen to 500f)))
invoke("onPause")
assertEquals(listOf(PEN_IN, 0, PEN_IN, 0, PEN_IN, 0, PEN_IN, 0), CompositorShadow.pens.map { it.flags })
}
@Config(sdk = [34], shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class])
@Test fun aHeldPenKeyOutlivesATouchCancelButNotAFocusLoss() {
penKeyboard()
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
assertTrue(penKey(KeyEvent.ACTION_DOWN, 15))
invoke("cancelTouchGestures")
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20, pen to 500f)))
invoke("releaseHeldGuestInput")
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 30, pen to 500f)))
val right = PEN_IN or PEN_RIGHT
assertEquals(listOf(PEN_IN, right, 0, right, 0, PEN_IN), CompositorShadow.pens.map { it.flags })
}
@Config(sdk = [34], shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class])
@Test fun aPenKeyIsAnOrdinaryKeyInTouchMode() {
penKeyboard()
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
assertTrue(penKey(KeyEvent.ACTION_DOWN, 15))
SessionPrefs.setPenSetting(activity, "pen", SessionPrefs.PEN_TOUCH)
invoke("readPrefs")
assertTrue(penKey(KeyEvent.ACTION_UP, 20))
assertFalse(penKey(KeyEvent.ACTION_DOWN, 25))
assertFalse(penKey(KeyEvent.ACTION_UP, 30))
assertEquals(listOf(PEN_IN, PEN_IN or PEN_RIGHT, 0), CompositorShadow.pens.map { it.flags })
}
@Test fun aFingerAndARelativePenOnTheTouchpadStayOneTwoPointerStream() {
state<Boolean>("glassesOutputActive").value = true
field("padMouseCheckedAt", android.os.SystemClock.uptimeMillis())
for (penFirst in listOf(false, true)) {
val pad = touchpad()
val first = if (penFirst) pen to 500f else finger to 100f
val second = if (penFirst) finger to 100f else pen to 500f
fun both(step: Int) = arrayOf(first.first to first.second + 10f * step, second.first to second.second + 10f * step)
activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10_000, first))
activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_010, *both(0)))
for (step in 1..4) activity.onTouchEvent(event(MotionEvent.ACTION_MOVE, 10_010L + 10 * step, *both(step)))
activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_060, *both(4)))
activity.onTouchEvent(event(MotionEvent.ACTION_UP, 10_070, first.first to first.second + 40f))
assertTrue("penFirst=$penFirst", pad.scrolls.isNotEmpty())
assertTrue("penFirst=$penFirst ${pad.moves}", pad.moves.all { it in 450f..550f })
assertTrue(CompositorShadow.pens.isEmpty())
}
}
@Test fun aRelativePenStrokeThatAndroidCanceledDoesNotClick() {
steamDirect(SessionPrefs.PEN_MOUSE)
state<Boolean>("glassesOutputActive").value = true
val pad = touchpad()
activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10_000, finger to 100f))
activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_010, finger to 100f, pen to 500f))
activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_050, finger to 100f, pen to 500f,
flags = MotionEvent.FLAG_CANCELED))
activity.onTouchEvent(event(MotionEvent.ACTION_UP, 10_060, finger to 100f))
activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 20_000, pen to 500f))
activity.onTouchEvent(event(MotionEvent.ACTION_UP, 20_050, pen to 500f, flags = MotionEvent.FLAG_CANCELED))
assertTrue(pad.presses.isEmpty())
assertTrue(CompositorShadow.touches.isNotEmpty())
activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 30_000, pen to 500f))
activity.onTouchEvent(event(MotionEvent.ACTION_UP, 30_050, pen to 500f))
assertEquals(listOf(PointerGestures.BTN_LEFT), pad.presses)
assertTrue(CompositorShadow.pens.isEmpty())
}
@Test fun aTouchCancelAfterAFingerLiftsEndsTheRelativePenStroke() {
steamDirect(SessionPrefs.PEN_MOUSE)
state<Boolean>("glassesOutputActive").value = true
val pad = touchpad()
activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 10_000, pen to 500f))
activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_010, pen to 500f, finger to 100f))
activity.onTouchEvent(event(MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 10_020, pen to 500f, finger to 100f))
invoke("cancelTouchGestures")
activity.onTouchEvent(event(MotionEvent.ACTION_UP, 10_050, pen to 500f))
assertTrue(pad.presses.isEmpty())
activity.onTouchEvent(event(MotionEvent.ACTION_DOWN, 20_000, pen to 500f))
activity.onTouchEvent(event(MotionEvent.ACTION_UP, 20_050, pen to 500f))
assertEquals(listOf(PointerGestures.BTN_LEFT), pad.presses)
}
@Test fun changingAPenSettingEndsThePenAndRereadingTheSameSettingsDoesNot() {
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
invoke("readPrefs")
assertEquals(listOf(PEN_IN), CompositorShadow.pens.map { it.flags })
for ((step, setting) in listOf("penButton" to SessionPrefs.PEN_BUTTON_MIDDLE, "pen" to SessionPrefs.PEN_TOUCH).withIndex()) {
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 20L + 10 * step, pen to 500f)))
SessionPrefs.setPenSetting(activity, setting.first, setting.second)
invoke("readPrefs")
assertEquals(setting.first, 0, CompositorShadow.pens.last().flags)
}
}
@Test fun theFirstPenEventRemembersThatThePhoneHasAPen() {
invoke("readPrefs")
assertFalse(state<Boolean>("penSeen").value)
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
assertTrue(state<Boolean>("penSeen").value)
assertTrue(SessionPrefs.penSeen(activity))
}
@Config(sdk = [34])
@Test fun aConnectedStylusDeviceCountsAsSeenBeforeAnyPenEvent() {
invoke("readPrefs")
assertFalse(state<Boolean>("penSeen").value)
shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice(
InputDeviceBuilder.newBuilder().setId(42).setName("digitizer").setSources(stylusScreen).build())
invoke("readPrefs")
assertTrue(state<Boolean>("penSeen").value)
assertTrue(SessionPrefs.penSeen(activity))
}
@Config(sdk = [33])
@Test fun beforeAndroid14ATouchscreenWithTheStylusBitWaitsForAPenEvent() {
shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice(
ShadowInputDevice.makeInputDeviceNamed("touchscreen").also {
ReflectionHelpers.setField(it, "mId", 42)
ReflectionHelpers.setField(it, "mSources", stylusScreen)
})
assertEquals(listOf(42), InputDevice.getDeviceIds().toList())
invoke("readPrefs")
assertFalse(state<Boolean>("penSeen").value)
assertFalse(SessionPrefs.penSeen(activity))
assertTrue(activity.dispatchGenericMotionEvent(event(MotionEvent.ACTION_HOVER_MOVE, 10, pen to 500f)))
assertTrue(state<Boolean>("penSeen").value)
}
@Config(sdk = [34])
@Test fun aStylusDeviceIsNotAHardwareMouse() {
fun device(sources: Int) = InputDeviceBuilder.newBuilder().setId(7).setName("device").setSources(sources).build()
fun mouse(sources: Int) = ReflectionHelpers.callInstanceMethod<Boolean>(activity, "isHardwareMouse",
ReflectionHelpers.ClassParameter.from(InputDevice::class.java, device(sources)))
assertTrue(mouse(InputDevice.SOURCE_MOUSE))
assertFalse(mouse(InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS))
assertFalse(mouse(InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_STYLUS or InputDevice.SOURCE_KEYBOARD))
assertFalse(mouse(stylusScreen))
}
private fun steamDirect(mode: String) {
SessionPrefs.setPenSetting(activity, "pen", mode)
invoke("readPrefs")
SessionState.mode = SessionService.MODE_STEAM
field("routedTouchMode", SessionPrefs.TOUCH_DIRECT)
field("padMouseCheckedAt", android.os.SystemClock.uptimeMillis())
field("threeFingerTap", ThreeFingerTap(8f))
field("directTouchHold", DirectTouchHold(8f, object : PointerGestures.Listener {
override fun onMove(x: Float, y: Float) {}
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) {}
override fun onWheel(steps: Int) {}
override fun onLongPress() {}
}, ReflectionHelpers.getField(activity, "directTouchSink")).also {
it.configure(TouchGestureConfig(directHoldBehavior = TouchGestureConfig.MOUSE_BEHAVIOR_NONE))
})
}
private fun penKeyboard() = shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice(
InputDeviceBuilder.newBuilder().setId(10).setName("OPPO AI Pen").setSources(InputDevice.SOURCE_KEYBOARD).setExternal(true)
.setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC).build())
private fun penKey(action: Int, time: Long) =
activity.dispatchKeyEvent(KeyEvent(time, time, action, KeyEvent.KEYCODE_UNKNOWN, 0, 0, 10, 191, 0, InputDevice.SOURCE_KEYBOARD))
private fun event(action: Int, time: Long, vararg pointers: Pair<Int, Float>, source: Int = stylusScreen, flags: Int = 0,
buttons: Int = 0): MotionEvent {
val properties = Array(pointers.size) { MotionEvent.PointerProperties().apply { id = it; toolType = pointers[it].first } }
val coords = Array(pointers.size) { MotionEvent.PointerCoords().apply { x = pointers[it].second; y = 300f; pressure = 0.5f } }
return MotionEvent.obtain(0, time, action, pointers.size, properties, coords, 0, buttons, 1f, 1f, 6, 0, source, flags)
}
private class Pad : PointerGestures.Listener {
val moves = mutableListOf<Float>()
val scrolls = mutableListOf<Float>()
val presses = mutableListOf<Int>()
override fun onMove(x: Float, y: Float) { moves += x }
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { if (pressed) presses += button }
override fun onWheel(steps: Int) {}
override fun onLongPress() {}
override fun onScroll(dx: Float, dy: Float) { scrolls += dx }
}
private fun touchpad() = Pad().also { field("touchpad", TouchpadGestures(8f, it)) }
private fun field(name: String, value: Any) = ReflectionHelpers.setField(activity, name, value)
private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod<Any?>(activity, name)
private fun <T> state(name: String): MutableState<T> = ReflectionHelpers.getField(activity, "$name\$delegate")
private companion object { const val RIGHT_CTRL = 97 }
data class Pen(val flags: Int, val x: Int, val y: Int)
data class Touch(val action: Int, val id: Int, val x: Int, val y: Int)
@Implements(value = WaylandCompositor::class, isInAndroidSdk = false)
class CompositorShadow {
companion object {
val pens = mutableListOf<Pen>()
val touches = mutableListOf<Touch>()
val pointers = mutableListOf<Triple<Int, Int, Int>>()
val keys = mutableListOf<Pair<Int, Int>>()
fun clear() { pens.clear(); touches.clear(); pointers.clear(); keys.clear() }
@JvmStatic @Implementation fun __staticInitializer__() {}
@JvmStatic @Implementation fun nativeSendPen(flags: Int, x: Int, y: Int) { pens += Pen(flags, x, y) }
@JvmStatic @Implementation fun nativeSendTouch(action: Int, id: Int, x: Int, y: Int) { touches += Touch(action, id, x, y) }
@JvmStatic @Implementation fun nativeSendPointer(action: Int, x: Int, y: Int) { pointers += Triple(action, x, y) }
@JvmStatic @Implementation fun nativeSendSceneInput(type: Int, code: Int, value: Int) {}
@JvmStatic @Implementation fun nativeSendKey(code: Int, value: Int) { keys += code to value }
@JvmStatic @Implementation fun nativeFocusTakesTouch(): Boolean = false
}
}
}
@@ -34,6 +34,9 @@ class SettingsResetTest {
@Test fun settingsGoBackToTheirDefaults() {
SessionPrefs.setTheme(app, "ocean")
SessionPrefs.setBackDoubleTapMs(app, 100)
SessionPrefs.setPenSetting(app, "pen", SessionPrefs.PEN_TOUCH)
SessionPrefs.setPenSetting(app, "penButton", SessionPrefs.PEN_BUTTON_OFF)
SessionPrefs.setPenSeen(app)
SessionPrefs.setUnfoldedControlsWithController(app, true)
SessionPrefs.setNoXalia(app, false)
session.edit().putInt("fpsLimit.steam", 30).commit()
@@ -58,6 +61,8 @@ class SettingsResetTest {
assertEquals("graphite", SessionPrefs.theme(app))
assertEquals(500, SessionPrefs.backDoubleTapMs(app))
assertEquals(SessionPrefs.Pen(), SessionPrefs.pen(app))
assertTrue(SessionPrefs.penSeen(app))
assertFalse(SessionPrefs.unfoldedControlsWithController(app))
assertTrue(SessionPrefs.noXalia(app))
assertFalse(session.contains("fpsLimit.steam"))
@@ -82,6 +87,26 @@ class SettingsResetTest {
assertFalse(prefs("steam_touch").contains("gyro"))
}
@Test fun penSettingsKeepOnlyKnownValues() {
val settings = mapOf(
"pen" to listOf(SessionPrefs.PEN_MOUSE, SessionPrefs.PEN_TOUCH),
"penButton" to listOf(SessionPrefs.PEN_BUTTON_RIGHT, SessionPrefs.PEN_BUTTON_MIDDLE, SessionPrefs.PEN_BUTTON_OFF),
)
fun read(key: String) = if (key == "pen") SessionPrefs.penMode(app) else SessionPrefs.penButton(app)
for ((key, values) in settings) {
assertTrue(key, SettingsReset.isSessionSetting(key))
assertEquals(key, values.first(), read(key))
for (value in values.drop(1)) {
SessionPrefs.setPenSetting(app, key, value)
assertEquals(key, value, read(key))
}
session.edit().putString(key, "left").commit()
assertEquals(key, values.first(), read(key))
assertThrows(IllegalArgumentException::class.java) { SessionPrefs.setPenSetting(app, key, "left") }
}
assertFalse(SettingsReset.isSessionSetting("penSeen"))
}
@Test fun aFailedDebugOptOutDoesNotHideTheEnabledPreference() {
assertTrue(SessionPrefs.setGameDebugLogsEnabled(app, true))
val directory = java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(app),
@@ -98,6 +98,24 @@ class InputSurfaceTouchTest {
assertFalse(state().isDown(PadState.A))
}
@Test fun aPenPressesButtonsLikeAFingerAndItsMissesKeepTheirHistory() {
touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_STYLUS)
assertTrue(state().isDown(PadState.A))
touch(MotionEvent.ACTION_UP, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_STYLUS)
assertFalse(state().isDown(PadState.A))
val missed = mutableListOf<List<Long>>()
view.onTouchMiss = { event -> missed += (0 until event.historySize).map { event.getHistoricalEventTime(it) } + event.eventTime }
touch(MotionEvent.ACTION_DOWN, 1 to (500f to 100f), tool = MotionEvent.TOOL_TYPE_STYLUS)
val properties = arrayOf(MotionEvent.PointerProperties().apply { id = 1; toolType = MotionEvent.TOOL_TYPE_STYLUS })
fun coords(x: Float) = arrayOf(MotionEvent.PointerCoords().apply { this.x = x; y = 100f; pressure = 0.5f })
val move = MotionEvent.obtain(1000L, 2000L, MotionEvent.ACTION_MOVE, 1, properties, coords(510f), 0, 0, 1f, 1f, 0, 0, 0, 0)
move.addBatch(2003L, coords(520f), 0)
move.addBatch(2006L, coords(530f), 0)
try { view.dispatchTouchEvent(move) } finally { move.recycle() }
assertEquals(listOf(listOf(time - 1), listOf(2000L, 2003L, 2006L)), missed)
assertFalse(state().isDown(PadState.A))
}
@Test fun keyboardShortcutOpensOnRelease() {
touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f))
assertTrue(events.isEmpty())
@@ -0,0 +1,99 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.*
import com.droiddeck.launcher.R
import com.droiddeck.launcher.gpu.FrameGen
import com.droiddeck.launcher.gpu.Lossless
import com.droiddeck.launcher.input.SecondScreenMode
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SessionService
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], qualifiers = "w800dp-h600dp")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class PenSettingsGroupTest {
@get:Rule val compose = createRobolectricComposeRule()
private val context get() = RuntimeEnvironment.getApplication()
@Test fun theDrawerHasNoPenGroupUntilAPenIsKnown() {
compose.setContent { DroidDeckTheme { SessionDrawer(true, SessionDrawerPage.CONTROLLER, false, {}, actions(pen = null)) } }
compose.onNodeWithTag("setting-touch").assertExists()
compose.onNodeWithTag("setting-pen").assertDoesNotExist()
compose.onNodeWithTag("setting-pen-button").assertDoesNotExist()
}
@Test fun theDrawerSavesPenPicks() {
val picks = mutableListOf<Pair<String, String>>()
compose.setContent {
DroidDeckTheme {
SessionDrawer(true, SessionDrawerPage.CONTROLLER, false, {}, actions(SessionPrefs.Pen()) { k, v -> picks += k to v })
}
}
compose.onNodeWithTag("setting-pen-button").performScrollTo().performClick()
compose.onNodeWithText(context.getString(R.string.gesture_middle_click)).performScrollTo().performClick()
compose.onNodeWithTag("setting-pen").performScrollTo().performClick()
compose.onNodeWithText(context.getString(R.string.mode_pen_touch)).performScrollTo().performClick()
compose.runOnIdle {
assertEquals(listOf("penButton" to SessionPrefs.PEN_BUTTON_MIDDLE, "pen" to SessionPrefs.PEN_TOUCH), picks)
}
}
@Test fun theSideButtonRowIsHiddenInTouchMode() {
val pen = mutableStateOf(SessionPrefs.Pen())
compose.setContent { DroidDeckTheme { Column { PenSettingsGroup(rememberMenuHost(), pen.value) { _, _ -> } } } }
compose.onNodeWithTag("setting-pen-button").assertExists()
compose.runOnIdle { pen.value = SessionPrefs.Pen(mode = SessionPrefs.PEN_TOUCH) }
compose.onNodeWithTag("setting-pen").assertExists()
compose.onNodeWithTag("setting-pen-button").assertDoesNotExist()
}
@Test fun theControlsTabShowsThePenGroupAfterTheTouchRowsOnceAPenIsKnown() {
val pen = mutableStateOf<SessionPrefs.Pen?>(null)
val picks = mutableListOf<Pair<String, String>>()
compose.setContent {
DroidDeckTheme {
Column(Modifier.verticalScroll(rememberScrollState())) {
ModeSheetRows(ModeSettings(mode = SessionService.MODE_DESKTOP, resolution = "auto", hdr = false, hdrReason = null,
touchMode = "auto", pen = pen.value, suspendPolicy = "auto", oscMode = "auto", directAudio = null, mic = null),
ModeSettingsActions(onResolution = {}, onHdr = {}, onTouch = {}, onPen = { k, v -> picks += k to v },
onSuspendPolicy = {}, onOsc = {}, onInputSettings = {}, onDirectAudio = {}, onMic = {}, onDismiss = {}),
SheetTab.CONTROLS)
}
}
}
compose.onNodeWithTag("setting-touch").assertExists()
compose.onNodeWithTag("setting-pen").assertDoesNotExist()
compose.runOnIdle { pen.value = SessionPrefs.Pen() }
compose.onNodeWithTag("setting-pen-button").assertExists()
assertTrue(compose.onNodeWithTag("setting-touch").fetchSemanticsNode().positionInRoot.y <
compose.onNodeWithTag("setting-pen").fetchSemanticsNode().positionInRoot.y)
compose.onNodeWithTag("setting-pen-button").performScrollTo().performClick()
compose.onNodeWithText(context.getString(R.string.gesture_middle_click)).performScrollTo().performClick()
compose.runOnIdle { assertEquals(listOf("penButton" to SessionPrefs.PEN_BUTTON_MIDDLE), picks) }
}
private fun actions(pen: SessionPrefs.Pen?, onPen: (String, String) -> Unit = { _, _ -> }) =
DrawerActions(steam = false, desktop = true, isHomeApp = false, androidApps = emptyList(), hudOn = false,
frameGen = FrameGen.Mode.OFF, lossless = Lossless.State.NONE, oscMode = "never",
suspendPolicy = "auto", backActionsInverted = false,
touchMode = "auto", touchAuto = "touchpad", pen = pen, onPen = onPen, fexPreset = "default",
secondScreenMode = SecondScreenMode.NONE, secondScreenDisplays = emptyList(), selectedSecondScreenDisplay = 1,
onHud = {}, onFrameGenPick = {}, onImportLossless = {}, onKeyboard = {}, onHardwareKeyboard = {},
onSteamMenu = null, onQam = null, onOsc = {}, onSuspendPolicy = {}, onBackActionsInverted = {},
onTouch = {}, onFexPreset = {}, onSecondScreenMode = {}, onSecondScreenDisplay = {},
onLaunchAndroidApp = { _, _ -> }, onBackground = {}, onShareLogs = {}, onStop = {}, onClose = {})
}