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

@@ -535,6 +535,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
private var upscaleSharpness by mutableStateOf(75)
private var hdrReason by mutableStateOf<String?>(null)
private var touchMode by mutableStateOf(SessionPrefs.TOUCH_AUTO)
private var pen by mutableStateOf<SessionPrefs.Pen?>(null)
private var suspendPolicy by mutableStateOf(SessionPrefs.SUSPEND_MANUAL)
private var steamDownloadsInBackground by mutableStateOf(false)
private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO)
@@ -2471,6 +2472,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
upscaler = upscaler, upscaleSharpness = upscaleSharpness,
gpuDrivers = drivers.summary(),
touchMode = touchMode,
pen = pen,
suspendPolicy = suspendPolicy,
steamDownloadsInBackground = steamDownloadsInBackground,
pipSupported = com.droiddeck.launcher.session.SessionPipController.supported(this),
@@ -2523,6 +2525,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
onUpscaler = { m -> SessionPrefs.setUpscaler(this, m); upscaler = m },
onUpscaleSharpness = { pct -> SessionPrefs.setUpscaleSharpness(this, pct); upscaleSharpness = pct },
onTouch = { t -> SessionPrefs.setTouchMode(this, t); touchMode = t },
onPen = { key, value -> SessionPrefs.setPenSetting(this, key, value); pen = SessionPrefs.pen(this) },
onSuspendPolicy = { policy -> SessionPrefs.setSuspendPolicy(this, mode, policy); suspendPolicy = policy },
onSteamDownloadsInBackground = { enabled ->
SessionPrefs.setSteamDownloadsInBackground(this, enabled)
@@ -2869,6 +2872,8 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
upscaleSharpness = SessionPrefs.upscaleSharpness(this)
hdrReason = com.droiddeck.launcher.wayland.HdrSupport.probe(this).reason
touchMode = SessionPrefs.touchMode(this)
if (!SessionPrefs.penSeen(this) && com.droiddeck.launcher.input.PenStream.penConnected()) SessionPrefs.setPenSeen(this)
pen = SessionPrefs.pen(this).takeIf { SessionPrefs.penSeen(this) }
suspendPolicy = SessionPrefs.suspendPolicy(this, mode)
steamDownloadsInBackground = SessionPrefs.steamDownloadsInBackground(this)
oscMode = SessionPrefs.oscMode(this)
@@ -97,8 +97,11 @@ import com.droiddeck.launcher.input.PadState
import com.droiddeck.launcher.input.SteamControllerBle
import com.droiddeck.launcher.input.DirectTouchHold
import com.droiddeck.launcher.input.GestureTouchStreamGate
import com.droiddeck.launcher.input.PenRouter
import com.droiddeck.launcher.input.PenStream
import com.droiddeck.launcher.input.PointerGestures
import com.droiddeck.launcher.input.PointerSpace
import com.droiddeck.launcher.input.pointerSubset
import com.droiddeck.launcher.input.SecondScreenDisplay
import com.droiddeck.launcher.input.SecondScreenDisplays
import com.droiddeck.launcher.input.SecondScreenMode
@@ -266,6 +269,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private var steamTouchOptions: com.droiddeck.launcher.input.SteamTouchConfig.Options? = null
private var routedTouchMode = SessionPrefs.TOUCH_AUTO
private var touchRoute: com.droiddeck.launcher.input.TouchRouter.Route? = null
private var penSettings by mutableStateOf(SessionPrefs.Pen())
private var penSeen by mutableStateOf(false)
private var penRoute: PenRouter.Route? = null
private var penDown = false
private val penStream = PenStream(WaylandCompositor::nativeSendPen)
private var keyboard: KeyboardHost? = null
private var desktopKeyboardButton: com.droiddeck.launcher.input.DesktopKeyboardButton? = null
private var lastUserPressMs = 0L
@@ -667,6 +675,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private val deviceListener = object : InputManager.InputDeviceListener {
override fun onInputDeviceAdded(deviceId: Int) {
logInputDevice("connected", deviceId)
if (!penSeen && InputDevice.getDevice(deviceId)?.let { PenStream.isPenDevice(it.sources) } == true) notePenSeen()
joyConVr?.refresh()
padBridge?.syncConnectedControllers()
refreshControls()
@@ -677,6 +686,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
override fun onInputDeviceRemoved(deviceId: Int) {
physicalShortcuts.onDeviceRemoved(deviceId)
penStream.deviceRemoved(deviceId)
Log.i(TAG, "input device $deviceId disconnected")
dropMouseCursor()
backQamInput.onDeviceRemoved(deviceId)
@@ -771,6 +781,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
uiHandler.postDelayed(::updatePointerCapture, 250)
}
}
override fun onResolvePointerIcon(event: MotionEvent, pointerIndex: Int): android.view.PointerIcon? =
if (platformPointerShown == false && PenStream.isPen(event, pointerIndex)) pointerIcon
else super.onResolvePointerIcon(event, pointerIndex)
}
sessionRoot = root
surfaceView = SurfaceView(this)
@@ -852,7 +865,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
refreshGestureConfig()
root.isMotionEventSplittingEnabled = true
surfaceView.setOnTouchListener { _, event ->
if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) {
if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN) && PenStream.penIndex(event) < 0) {
false
} else if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SURFACE, event)) {
false
@@ -1374,6 +1387,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
micEnabled = SessionState.micEnabled,
onMicEnabled = { SessionService.setMicEnabled(this@SessionActivity, it) },
touchAuto = getString(if (usingTouchpad()) R.string.session_touch_auto_touchpad else R.string.session_touch_auto_direct),
pen = penSettings.takeIf { penSeen },
onPen = { key, value -> SessionPrefs.setPenSetting(this@SessionActivity, key, value); readPrefs() },
fexPreset = fexPreset,
secondScreenMode = secondScreenMode,
secondScreenNone = !bottomOutputActive && !dockDrawer(),
@@ -2365,6 +2380,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
textureLodBias = SessionPrefs.textureLodBias(this)
val nextTouchMode = SessionPrefs.touchMode(this)
if (touchMode != nextTouchMode) {
cancelPen()
if (::touchpad.isInitialized) touchpad.cancel()
if (::directTouchHold.isInitialized) directTouchHold.cancel()
if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel()
@@ -2378,6 +2394,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (nextTouchMode == SessionPrefs.TOUCH_OFF || !usingTouchpad()) cursorVisible = false
}
touchMode = nextTouchMode
val nextPen = SessionPrefs.pen(this)
if (penSettings != nextPen) cancelPen()
penSettings = nextPen
penStream.sideButton = nextPen.button
if (!penSeen && (SessionPrefs.penSeen(this) || PenStream.penConnected())) notePenSeen()
val nextRoutedTouchMode = effectiveTouchMode()
if (routedTouchMode != nextRoutedTouchMode) gestureTouchStreamGate.reset()
routedTouchMode = nextRoutedTouchMode
@@ -2753,9 +2774,20 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (::directTouchHold.isInitialized) directTouchHold.cancel()
if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel()
if (::scrollTouch.isInitialized) scrollTouch.cancel()
if (sessionTouchDown || penDown) dismissTouch = true
cancelPen()
scrollTouchGesture = false
gesturePointerReady = false
if (sessionTouchDown) dismissTouch = true
}
private fun cancelPen(releaseKey: Boolean = false) {
penStream.cancel(releaseKey)
penDown = false
}
private fun notePenSeen() {
penSeen = true
SessionPrefs.setPenSeen(this)
}
private fun panelBounds(): android.graphics.Rect = if (Build.VERSION.SDK_INT >= 30) {
@@ -3219,6 +3251,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
return super.dispatchKeyEvent(event)
}
}
if (penStream.holdsKey(event) || (penSettings.mode != SessionPrefs.PEN_TOUCH && PenStream.isPenKey(event))) return penStream.onKey(event)
if (appTextFocus) return super.dispatchKeyEvent(event)
if (!pipUi) {
if (physicalInputBindings?.onKey(event, resumed && !drawerOpen && !pcKeyboardOpen && !achievementsOpen &&
@@ -3404,6 +3437,19 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (joyConVr?.onMotionEvent(event) == true) return true
if (PadBridge.isControllerMotionEvent(event) && padMouseMode() && padMouse.onMotion(event)) return true
if (padBridge?.onMotionEvent(event) == true) return true
val pen = PenStream.penIndex(event)
if (pen >= 0) when (routePen(event, pen)) {
PenRouter.Route.TOUCH -> {}
PenRouter.Route.PEN -> {
if (!appUiOwnsPointer()) return penSample(event, pen) || super.dispatchGenericMotionEvent(event)
if (super.dispatchGenericMotionEvent(event)) {
penStream.cancel()
return true
}
return !touchEditorOpen && penSample(event, pen)
}
else -> return super.dispatchGenericMotionEvent(event)
}
if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE) && !drawerOpen) {
if (appUiOwnsPointer()) {
if (super.dispatchGenericMotionEvent(event)) return true
@@ -3438,7 +3484,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (pipUi) return true
notePress(event)
if (event.actionMasked == MotionEvent.ACTION_DOWN && event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) {
if (event.actionMasked == MotionEvent.ACTION_DOWN &&
(event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN) || PenStream.penIndex(event) >= 0)) {
drawerControllerActive = false
gestureTouchStreamGate.reset()
}
@@ -4440,6 +4487,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
if (next == routedTouchMode) return
if (::touchscreenGestures.isInitialized) touchscreenGestures.cancel()
cancelPen()
gesturePointerReady = false
if (::touchpad.isInitialized) touchpad.cancel()
if (::directTouchHold.isInitialized) directTouchHold.cancel()
@@ -4608,7 +4656,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private var sessionTouchDown = false
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event)
val pen = PenStream.penIndex(event)
val penRouted = if (pen >= 0) routePen(event, pen).takeIf { it != PenRouter.Route.TOUCH } else null
if (penRouted == null && event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event)
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
sessionTouchDown = true
@@ -4627,19 +4677,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) dismissTouch = false
return true
}
val uiBlocking = drawerOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || SessionState.suspended
if (penRouted != null) return onPenTouch(event, pen, penRouted)
val uiBlocking = uiBlocksTouch()
if (event.actionMasked == MotionEvent.ACTION_DOWN && !uiBlocking && routedTouchMode != SessionPrefs.TOUCH_GESTURES) {
scrollTouchGesture = scrollTouchMode()
scrollRestX = 0f
scrollRestY = 0f
}
val snapshot = com.droiddeck.launcher.input.TouchRouter.Snapshot(
uiBlocking = uiBlocking,
mode = routedTouchMode,
scrollLayer = scrollTouchGesture,
steamSession = SessionState.mode == SessionService.MODE_STEAM,
steamDesktopUi = SessionState.steamUi == "desktop",
)
val snapshot = fingerTouchSnapshot(uiBlocking)
val route = com.droiddeck.launcher.input.TouchRouter.decide(snapshot)
if (route != touchRoute) {
Log.i(TAG, "touch route $touchRoute -> $route (mode=$routedTouchMode steamUi=${SessionState.steamUi} scrollLayer=$scrollTouchGesture)")
@@ -4667,17 +4712,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (threeFingerTap.active || wasActive) return true
}
if (route == com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD) {
val rect = (if (pointerLocked) drawnRect() else pointerSurfaceRect()) ?: return false
if (touchpad.bounds != rect) {
val fresh = touchpad.bounds.width() <= 1f
touchpad.bounds = rect
if (fresh) touchpad.place(rect.centerX(), rect.centerY())
}
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
touchpad.desktop = SessionState.mode == SessionService.MODE_DESKTOP
placeTouchpadAtGuest(rect)
}
touchpad.onTouch(event)
touchpadTouch(event)
} else {
dispatchDirectTouch(event)
}
@@ -4685,6 +4720,92 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
}
private fun uiBlocksTouch(): Boolean =
drawerOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || SessionState.suspended
private fun fingerTouchSnapshot(uiBlocking: Boolean) = com.droiddeck.launcher.input.TouchRouter.Snapshot(
uiBlocking = uiBlocking,
mode = routedTouchMode,
scrollLayer = scrollTouchGesture,
steamSession = SessionState.mode == SessionService.MODE_STEAM,
steamDesktopUi = SessionState.steamUi == "desktop",
)
private fun touchpadTouch(event: MotionEvent): Boolean {
val rect = (if (pointerLocked) drawnRect() else pointerSurfaceRect()) ?: return false
if (touchpad.bounds != rect) {
val fresh = touchpad.bounds.width() <= 1f
touchpad.bounds = rect
if (fresh) touchpad.place(rect.centerX(), rect.centerY())
}
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
touchpad.desktop = SessionState.mode == SessionService.MODE_DESKTOP
placeTouchpadAtGuest(rect)
}
return touchpad.onTouch(event)
}
private fun routePen(event: MotionEvent, pen: Int): PenRouter.Route {
if (!penSeen) notePenSeen()
val action = event.actionMasked
if (action == MotionEvent.ACTION_HOVER_ENTER || action == MotionEvent.ACTION_HOVER_MOVE || action == MotionEvent.ACTION_HOVER_EXIT) penDown = false
val own = PenStream.isPenAction(event, pen)
val starts = own && (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN)
val route = penRoute.takeIf { penDown && !starts } ?: decidePen()
if (own) penDown = starts
return route
}
private fun decidePen(): PenRouter.Route {
val mode = penSettings.mode
val route = PenRouter.decide(uiBlocksTouch(), mode, glassesOutputActive, programLocksPointer())
if (route != penRoute) {
if (penRoute == PenRouter.Route.PEN) penStream.cancel()
Log.i(TAG, "pen route $penRoute -> $route (mode=$mode)")
penRoute = route
}
return route
}
private fun onPenTouch(event: MotionEvent, pen: Int, route: PenRouter.Route): Boolean {
if (route == PenRouter.Route.RELATIVE && event.pointerCount > 1 &&
com.droiddeck.launcher.input.TouchRouter.decide(fingerTouchSnapshot(uiBlocksTouch())) ==
com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD) {
touchpadTouch(event)
return true
}
if (event.pointerCount > 1) {
var fingers = 0
for (index in 0 until event.pointerCount) if (!PenStream.isPen(event, index)) fingers = fingers or (1 shl event.getPointerId(index))
event.pointerSubset(fingers)?.let { split -> try { onTouchEvent(split) } finally { split.recycle() } }
}
when (route) {
PenRouter.Route.PEN -> penSample(event, pen)
PenRouter.Route.RELATIVE -> {
val split = if (event.pointerCount == 1) MotionEvent.obtain(event) else event.pointerSubset(1 shl event.getPointerId(pen))
split?.let {
try {
if (it.actionMasked == MotionEvent.ACTION_UP && it.flags and MotionEvent.FLAG_CANCELED != 0) it.action = MotionEvent.ACTION_CANCEL
touchpadTouch(it)
} finally { it.recycle() }
}
}
else -> {}
}
return true
}
private fun penSample(event: MotionEvent, pen: Int): Boolean {
val area = pointerSurfaceRect() ?: return false
val view = outputView()
if (!penStream.onMotion(event, pen, area, view.width, view.height)) return false
if (penStream.inProximity) {
if (pointerLocked) releaseDesktopMouseGrab()
showCursor(event.getX(pen).coerceIn(area.left, area.right), event.getY(pen).coerceIn(area.top, area.bottom), fromMouse = true)
}
return true
}
private val directTouchSink = object : DirectTouchHold.Touch {
override fun down(pointerId: Int, x: Float, y: Float) = sendDirectTouch(0, pointerId, x, y)
override fun move(pointerId: Int, x: Float, y: Float) = sendDirectTouch(1, pointerId, x, y)
@@ -4770,7 +4891,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
private fun isHardwareMouse(d: InputDevice?): Boolean =
d != null && !d.isVirtual && (d.supportsSource(InputDevice.SOURCE_MOUSE) || d.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE))
d != null && !d.isVirtual && !d.supportsSource(InputDevice.SOURCE_STYLUS) &&
(d.supportsSource(InputDevice.SOURCE_MOUSE) || d.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE))
/** Android also sends the Back and Forward buttons as key events, so they are handled as keys. */
private fun guestMouseButton(actionButton: Int): Int? = when (actionButton) {
@@ -4809,7 +4931,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
pointerLocked && SessionState.mode != SessionService.MODE_DESKTOP
private fun desktopMouseGrabAllowed(): Boolean =
SessionState.mode == SessionService.MODE_DESKTOP && relativeMouseAttached() && !bottomOutputActive
SessionState.mode == SessionService.MODE_DESKTOP && !penStream.inProximity && relativeMouseAttached() && !bottomOutputActive
private fun tapDesktopGrabKey() {
val now = android.os.SystemClock.uptimeMillis()
@@ -5104,6 +5226,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private fun releaseHeldGuestInput() {
mouseBackInput.cancel()
cancelPen(releaseKey = true)
heldButtons.release(1 or 2 or 8 or 16)
guestKeysDown.toList().forEach { sendGuestKey(it, false) }
heldKeys.release(8 or 16 or 32)
@@ -5195,6 +5318,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
scrollTouch.cancel()
directTouchHold.cancel()
twoFingerGestures.cancel()
cancelPen()
guestKeysDown.toList().forEach { sendGuestKey(it, false) }
heldKeys.clear()
heldButtons.clear()
@@ -5391,6 +5515,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (::touchpad.isInitialized) touchpad.cancel()
if (::directTouchHold.isInitialized) directTouchHold.cancel()
if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel()
cancelPen(releaseKey = true)
uiHandler.removeCallbacks(clearRecognizedGesture)
uiHandler.removeCallbacks(cursorResync)
val screenOff = screenIsOff()
@@ -5697,7 +5822,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
if (shown != null) {
closeSecondScreen(reset = false)
val screen = com.droiddeck.launcher.ui.BottomGameScreen(this, shown, this, ::onBottomGameTouch)
val screen = com.droiddeck.launcher.ui.BottomGameScreen(this, shown, this, ::onBottomGameTouch, ::onBottomGameHover)
bottomGame = screen
surfaceW = 0
surfaceH = 0
@@ -5749,9 +5874,23 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (surfaceView.holder.surface?.isValid == true) surfaceCreated(surfaceView.holder)
}
private fun onBottomGameHover(event: MotionEvent): Boolean {
val pen = PenStream.penIndex(event)
if (pipUi || pen < 0) return false
return when (routePen(event, pen)) {
PenRouter.Route.TOUCH -> false
PenRouter.Route.PEN -> {
notePress(event)
penSample(event, pen)
true
}
else -> true
}
}
private fun onBottomGameTouch(event: MotionEvent): Boolean {
if (pipUi) return true
if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) return false
if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN) && PenStream.penIndex(event) < 0) return false
notePress(event)
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
drawerControllerActive = false
@@ -6052,6 +6191,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
padBridge?.stop()
DeckControls.clearRouter(inputRouter)
inputRouter = null
cancelPen()
heldKeys.clear()
heldButtons.clear()
steamController?.stop()
@@ -296,6 +296,8 @@ object DeviceReport {
k("Microphone", SessionPrefs.micEnabled(context))
k("On-screen controls", SessionPrefs.oscMode(context))
k("Touch mode", SessionPrefs.touchMode(context))
k("Pen mode", SessionPrefs.penMode(context))
k("Pen side button", SessionPrefs.penButton(context))
k("Performance HUD", SessionPrefs.hudEnabled(context))
k("Game storage", when (val g = SessionPrefs.gameStorage(context)) {
"" -> "automatic (" + (com.droiddeck.launcher.session.GameStorage.effective(context)?.path ?: "no card present") + ")"
@@ -3,11 +3,16 @@ package com.droiddeck.launcher.input
object InputSpace {
const val WIDTH = 1920
const val HEIGHT = 1080
const val FIXED_ONE = 256
fun x(position: Float, viewWidth: Int): Int = scale(position, viewWidth, WIDTH)
fun y(position: Float, viewHeight: Int): Int = scale(position, viewHeight, HEIGHT)
fun fixedX(position: Float, viewWidth: Int): Int = scale(position, viewWidth, WIDTH * FIXED_ONE)
fun fixedY(position: Float, viewHeight: Int): Int = scale(position, viewHeight, HEIGHT * FIXED_ONE)
private fun scale(position: Float, extent: Int, size: Int): Int =
if (extent <= 0) 0 else (position / extent * size).toInt().coerceIn(0, size - 1)
}
@@ -0,0 +1,14 @@
package com.droiddeck.launcher.input
import com.droiddeck.launcher.session.SessionPrefs
object PenRouter {
enum class Route { BLOCKED, PEN, RELATIVE, TOUCH }
fun decide(uiBlocking: Boolean, mode: String, glasses: Boolean, programLock: Boolean): Route = when {
uiBlocking -> Route.BLOCKED
mode == SessionPrefs.PEN_TOUCH -> Route.TOUCH
glasses || programLock -> Route.RELATIVE
else -> Route.PEN
}
}
@@ -0,0 +1,247 @@
package com.droiddeck.launcher.input
import android.graphics.RectF
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import com.droiddeck.launcher.session.SessionPrefs
class PenStream(private val sink: Sink) {
fun interface Sink {
fun sample(flags: Int, x: Int, y: Int)
}
private val handler = Handler(Looper.getMainLooper())
private val exitLater = Runnable { if (exitPending) leave() }
private val hoverWatchdog = Runnable { checkHover() }
private var watching = false
private var contact = false
private var barrel = 0
private var keyDown = false
private var keyDevice = Int.MIN_VALUE
private var keyCode = 0
private var exitPending = false
private var exitTime = 0L
private var deviceId = Int.MIN_VALUE
private var x = 0
private var y = 0
private var sampleTime = 0L
var sideButton = SessionPrefs.PEN_BUTTON_RIGHT
var inProximity = false
private set
fun onMotion(event: MotionEvent, pointer: Int, area: RectF, width: Int, height: Int): Boolean {
val action = event.actionMasked
val time = event.eventTime
val own = isPenAction(event, pointer)
expire(time)
when (action) {
MotionEvent.ACTION_HOVER_ENTER, MotionEvent.ACTION_HOVER_MOVE -> {
continueAt(time)
contact = false
enter(event)
samples(event, pointer, area, width, height)
}
MotionEvent.ACTION_HOVER_EXIT -> if (inProximity && !exitPending && !contact) pendExit(time)
MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> {
continueAt(time)
if (!inProximity) return true
barrel = event.buttonState
samples(event, pointer, area, width, height)
}
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> if (own) {
continueAt(time)
enter(event)
contact = true
samples(event, pointer, area, width, height)
} else if (contact) samples(event, pointer, area, width, height)
MotionEvent.ACTION_MOVE -> if (contact) {
barrel = event.buttonState
samples(event, pointer, area, width, height)
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> if (!own) {
if (contact) samples(event, pointer, area, width, height)
} else if (contact) {
if (event.flags and MotionEvent.FLAG_CANCELED != 0) cancel()
else {
barrel = event.buttonState
samples(event, pointer, area, width, height, lift = true)
pendExit(time)
}
}
MotionEvent.ACTION_CANCEL -> cancel()
else -> return false
}
return true
}
fun onKey(event: KeyEvent): Boolean {
expire(event.eventTime)
val down = event.action == KeyEvent.ACTION_DOWN
val repeat = event.repeatCount > 0
if (exitPending && !repeat) leave()
if (!inProximity || exitPending) {
if (!repeat && event.deviceId == keyDevice && event.keyCode == keyCode) keyDown = down
return true
}
if (down == keyDown) return true
val before = state()
keyDown = down
if (down) {
keyDevice = event.deviceId
keyCode = event.keyCode
}
if (state() == before) return true
emit(state())
return true
}
fun cancel(releaseKey: Boolean = false) {
if (releaseKey) keyDown = false
leave()
}
fun holdsKey(event: KeyEvent): Boolean = keyDown && event.deviceId == keyDevice && event.keyCode == keyCode
fun deviceRemoved(deviceId: Int) {
if (deviceId == this.deviceId) cancel()
if (!keyDown || deviceId != keyDevice) return
val before = state()
keyDown = false
if (inProximity && state() != before) emit(state())
}
private fun enter(event: MotionEvent) {
barrel = event.buttonState
if (inProximity) return
inProximity = true
deviceId = event.deviceId
}
private fun continueAt(time: Long) {
if (!exitPending) return
if (time - exitTime > EXIT_GRACE_MS) { leave(); return }
exitPending = false
handler.removeCallbacks(exitLater)
}
private fun pendExit(time: Long) {
exitPending = true
exitTime = time
handler.removeCallbacks(exitLater)
handler.postDelayed(exitLater, EXIT_GRACE_MS)
}
private fun expire(time: Long) {
if (inProximity && !contact && time - sampleTime >= HOVER_TIMEOUT_MS) leave()
}
private fun watchHover(delay: Long = HOVER_TIMEOUT_MS) {
if (watching || contact) return
watching = true
handler.postDelayed(hoverWatchdog, delay)
}
private fun checkHover() {
watching = false
if (!inProximity || contact || exitPending) return
val silent = SystemClock.uptimeMillis() - sampleTime
if (silent >= HOVER_TIMEOUT_MS) leave() else watchHover(HOVER_TIMEOUT_MS - silent)
}
private fun leave() {
if (!inProximity) return
emit(0)
handler.removeCallbacks(exitLater)
handler.removeCallbacks(hoverWatchdog)
watching = false
inProximity = false
contact = false
exitPending = false
barrel = 0
}
private fun samples(event: MotionEvent, pointer: Int, area: RectF, width: Int, height: Int, lift: Boolean = false) {
for (h in 0 until event.historySize) sample(event, pointer, h, area, width, height)
if (lift) contact = false
sample(event, pointer, -1, area, width, height)
}
private fun sample(event: MotionEvent, pointer: Int, h: Int, area: RectF, width: Int, height: Int) {
x = InputSpace.fixedX(event.axis(MotionEvent.AXIS_X, pointer, h).coerceIn(area.left, area.right), width)
y = InputSpace.fixedY(event.axis(MotionEvent.AXIS_Y, pointer, h).coerceIn(area.top, area.bottom), height)
sampleTime = if (h < 0) event.eventTime else event.getHistoricalEventTime(h)
emit(state())
watchHover()
}
private fun emit(flags: Int) = sink.sample(flags, x, y)
private fun state(): Int =
PEN_IN or (if (contact) PEN_TIP else 0) or buttonFlags(keyDown || barrel and MotionEvent.BUTTON_STYLUS_PRIMARY != 0,
barrel and MotionEvent.BUTTON_STYLUS_SECONDARY != 0, sideButton)
private fun MotionEvent.axis(axis: Int, pointer: Int, h: Int): Float =
if (h < 0) getAxisValue(axis, pointer) else getHistoricalAxisValue(axis, pointer, h)
companion object {
const val PEN_IN = 1 shl 0
const val PEN_TIP = 1 shl 1
const val PEN_RIGHT = 1 shl 2
const val PEN_MIDDLE = 1 shl 3
const val HOVER_TIMEOUT_MS = 2_000L
const val EXIT_GRACE_MS = 50L
private val PEN_NAME = Regex("\\b(pen|stylus)", RegexOption.IGNORE_CASE)
fun isPen(event: MotionEvent, index: Int): Boolean = when (event.getToolType(index)) {
MotionEvent.TOOL_TYPE_STYLUS, MotionEvent.TOOL_TYPE_ERASER -> true
MotionEvent.TOOL_TYPE_UNKNOWN -> event.isFromSource(InputDevice.SOURCE_STYLUS)
else -> false
}
fun penConnected(): Boolean =
InputDevice.getDeviceIds().any { id -> InputDevice.getDevice(id)?.let { isPenDevice(it.sources) } == true }
// Before Android 14 a touchscreen that declares ABS_MT_TOOL_TYPE reports SOURCE_STYLUS without any pen.
fun isPenDevice(sources: Int, sdk: Int = Build.VERSION.SDK_INT): Boolean =
sources and InputDevice.SOURCE_STYLUS == InputDevice.SOURCE_STYLUS &&
(sources and InputDevice.SOURCE_TOUCHSCREEN != InputDevice.SOURCE_TOUCHSCREEN || sdk >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
fun penIndex(event: MotionEvent): Int {
for (index in 0 until event.pointerCount) if (isPen(event, index)) return index
return -1
}
fun isPenAction(event: MotionEvent, index: Int): Boolean = when (event.actionMasked) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> true
MotionEvent.ACTION_POINTER_DOWN, MotionEvent.ACTION_POINTER_UP -> event.actionIndex == index
else -> false
}
fun isPenKey(event: KeyEvent): Boolean {
val device = event.device ?: return false
return isPenKeyboard(device.name, device.sources, Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || device.isExternal,
device.keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC)
}
fun isPenKeyboard(name: String?, sources: Int, external: Boolean, alphabetic: Boolean): Boolean =
external && !alphabetic && sources == InputDevice.SOURCE_KEYBOARD && name != null && PEN_NAME.containsMatchIn(name)
fun buttonFlags(primary: Boolean, secondary: Boolean, sideButton: String): Int {
val first = when (sideButton) {
SessionPrefs.PEN_BUTTON_RIGHT -> PEN_RIGHT
SessionPrefs.PEN_BUTTON_MIDDLE -> PEN_MIDDLE
else -> 0
}
val second = if (sideButton == SessionPrefs.PEN_BUTTON_RIGHT) PEN_MIDDLE else PEN_RIGHT
return (if (primary) first else 0) or (if (secondary) second else 0)
}
}
}
@@ -23,13 +23,20 @@ internal fun MotionEvent.pointerSubset(pointerIdBits: Int): MotionEvent? {
}
val properties = Array(indices.size) { MotionEvent.PointerProperties() }
val coordinates = Array(indices.size) { MotionEvent.PointerCoords() }
indices.forEachIndexed { target, index ->
getPointerProperties(index, properties[target])
getPointerCoords(index, coordinates[target])
indices.forEachIndexed { target, index -> getPointerProperties(index, properties[target]) }
fun sample(position: Int): Array<MotionEvent.PointerCoords> {
indices.forEachIndexed { target, index ->
if (position < historySize) getHistoricalPointerCoords(index, position, coordinates[target])
else getPointerCoords(index, coordinates[target])
}
return coordinates
}
return MotionEvent.obtain(
downTime, eventTime, splitAction, indices.size, properties, coordinates,
fun time(position: Int) = if (position < historySize) getHistoricalEventTime(position) else eventTime
val split = MotionEvent.obtain(
downTime, time(0), splitAction, indices.size, properties, sample(0),
metaState, buttonState, xPrecision, yPrecision,
deviceId, edgeFlags, source, flags,
)
for (position in 1..historySize) split.addBatch(time(position), sample(position), metaState)
return split
}
@@ -516,6 +516,38 @@ object SessionPrefs {
prefs(context).edit().putString("touch", mode).apply()
}
const val PEN_MOUSE = "mouse"
const val PEN_TOUCH = "touch"
const val PEN_BUTTON_RIGHT = "right"
const val PEN_BUTTON_MIDDLE = "middle"
const val PEN_BUTTON_OFF = "off"
data class Pen(val mode: String = PEN_MOUSE, val button: String = PEN_BUTTON_RIGHT)
private val PEN_SETTINGS = mapOf(
"pen" to setOf(PEN_MOUSE, PEN_TOUCH),
"penButton" to setOf(PEN_BUTTON_RIGHT, PEN_BUTTON_MIDDLE, PEN_BUTTON_OFF),
)
private fun penSetting(context: Context, key: String, default: String): String =
prefs(context).getString(key, default)?.takeIf { it in PEN_SETTINGS.getValue(key) } ?: default
fun setPenSetting(context: Context, key: String, value: String) {
require(value in PEN_SETTINGS.getValue(key))
prefs(context).edit().putString(key, value).apply()
}
fun penMode(context: Context): String = penSetting(context, "pen", PEN_MOUSE)
fun penButton(context: Context): String = penSetting(context, "penButton", PEN_BUTTON_RIGHT)
fun pen(context: Context): Pen = Pen(penMode(context), penButton(context))
fun penSeen(context: Context): Boolean = prefs(context).getBoolean("penSeen", false)
fun setPenSeen(context: Context) {
if (!penSeen(context)) prefs(context).edit().putBoolean("penSeen", true).apply()
}
const val SHAPE_AUTO = "auto"
const val SHAPE_WIDE = "16:9"
const val SHAPE_EXACT = "exact"
@@ -16,7 +16,7 @@ object SettingsReset {
"dualScreenMode", "dualScreenSessionMode", "dualScreenLauncher", "dualScreenSessionControls", "androidTextFieldKeyboard",
"gameStoresEnabled", "soundtracksEnabled", "soundtracksIngame", "gameStoresSpeedTier", "gameStoresParallel", "storesOpenTab", "storesShowMature",
"storesInstallLanguage",
"hud", "hudDeck", "hudFps", "hudBattery", "hudGraph", "hudColor", "hudOpacity", "__KEEP_droidDeckHud__", "__KEEP_droidDeckHudPos__", "backHold", "backActionsInverted", "backDoubleTapMs", "touch", "osc", "shape", "steamController",
"hud", "hudDeck", "hudFps", "hudBattery", "hudGraph", "hudColor", "hudOpacity", "__KEEP_droidDeckHud__", "__KEEP_droidDeckHudPos__", "backHold", "backActionsInverted", "backDoubleTapMs", "touch", "pen", "penButton", "osc", "shape", "steamController",
"gamepadLeft", "gamepadRight", "pipAutoEnter", "unfoldedControls", "unfoldedControlsWithController",
"stretch16x9", "picturePosition", "railLabels", "agentTab", "navTabOrder", "navTabShelved", "navTabHidden", "pcKeyboardSize", "pcKeyboardOpacity", "pcKeyboardHiddenKeys",
"gpuDriverMode", "androidDriver", "linuxDriver", "linuxDriver.steam", "linuxDriver.desktop",
@@ -19,6 +19,7 @@ class BottomGameScreen(
display: Display,
private val callback: SurfaceHolder.Callback,
private val onTouch: (MotionEvent) -> Boolean,
private val onHover: (MotionEvent) -> Boolean,
) : Presentation(activity, display) {
val surfaceView = SurfaceView(getContext())
val holder: SurfaceHolder get() = surfaceView.holder
@@ -51,7 +52,7 @@ class BottomGameScreen(
activity.dispatchKeyEvent(event) || super.dispatchKeyEvent(event)
override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean =
activity.dispatchGenericMotionEvent(event) || super.dispatchGenericMotionEvent(event)
onHover(event) || activity.dispatchGenericMotionEvent(event) || super.dispatchGenericMotionEvent(event)
}
object GameScreenSwap {
@@ -63,6 +63,7 @@ class ModeSettings(
val upscaler: Int = 0,
val upscaleSharpness: Int = 75,
val touchMode: String,
val pen: SessionPrefs.Pen? = null,
val suspendPolicy: String,
val steamDownloadsInBackground: Boolean = false,
val pipSupported: Boolean = false,
@@ -116,6 +117,7 @@ class ModeSettingsActions(
val onUpscaler: (Int) -> Unit = {},
val onUpscaleSharpness: (Int) -> Unit = {},
val onTouch: (String) -> Unit,
val onPen: (key: String, value: String) -> Unit = { _, _ -> },
val onSuspendPolicy: (String) -> Unit,
val onSteamDownloadsInBackground: (Boolean) -> Unit = {},
val onPipAutoEnter: (Boolean) -> Unit = {},
@@ -321,6 +323,7 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras:
}
PhysicalShortcutRows()
}
s.pen?.let { PenSettingsGroup(host, it, onPick = a.onPen) }
}
if (tab == SheetTab.AUDIO && steam) {
if (s.directAudio != null && s.mic != null) SettingsGroup(stringResource(R.string.mode_audio)) {
@@ -0,0 +1,25 @@
package com.droiddeck.launcher.ui
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.droiddeck.launcher.R
import com.droiddeck.launcher.session.SessionPrefs
@Composable
internal fun PenSettingsGroup(
host: MenuHost, pen: SessionPrefs.Pen, track: (String) -> Modifier = { Modifier },
onPick: (key: String, value: String) -> Unit,
) {
SettingsGroup(stringResource(R.string.mode_pen)) {
ChoiceRow(host, "pen", stringResource(R.string.mode_pen), null,
listOf(SessionPrefs.PEN_MOUSE to stringResource(R.string.mode_pen_mouse),
SessionPrefs.PEN_TOUCH to stringResource(R.string.mode_pen_touch)),
pen.mode, chipModifier = track("pen")) { onPick("pen", it) }
if (pen.mode != SessionPrefs.PEN_TOUCH) ChoiceRow(host, "pen-button", stringResource(R.string.mode_pen_button), null,
listOf(SessionPrefs.PEN_BUTTON_RIGHT to stringResource(R.string.gesture_right_click),
SessionPrefs.PEN_BUTTON_MIDDLE to stringResource(R.string.gesture_middle_click),
SessionPrefs.PEN_BUTTON_OFF to stringResource(R.string.widgets_off)),
pen.button, chipModifier = track("pen-button")) { onPick("penButton", it) }
}
}
@@ -239,6 +239,8 @@ class DrawerActions(
val onPadType: (String) -> Unit = {},
val touchMode: String,
val touchAuto: String,
val pen: SessionPrefs.Pen? = null,
val onPen: (key: String, value: String) -> Unit = { _, _ -> },
val fexPreset: String,
val upscaler: Int = 0,
val upscaleSharpness: Int = 75,
@@ -672,6 +674,7 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
onPick = a.onDirectHoldBehavior)
}
}
a.pen?.let { PenSettingsGroup(host, it, track = { key -> focus.track(page, key) }, onPick = a.onPen) }
SettingsGroup(stringResource(R.string.input_screen_visibility)) {
ChoiceRow(host, "osc", stringResource(R.string.mode_osc), null,
if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never))
@@ -162,6 +162,8 @@ public final class WaylandCompositor {
public static native void nativeSendTouch(int action, int pointerId, int x, int y);
public static native void nativeSendPen(int flags, int x, int y);
public static native int nativeCursorSnapshot(int[] out);
public static native int nativeCursorSerial();