fix(input): let on-screen controls work while the touchpad finger is down

The game surface only took touches in gestures mode, so in touchpad and
direct mode a finger on the game left the window with no touch target and
Android sent every later finger to the touchpad, even over a stick or
button. The surface now owns those fingers in every mode, and the
on-screen and Steam touch controls hand fingers that miss a control back
to the touchpad instead of dropping them.
This commit is contained in:
maxjivi05 authored and MaxsTechReview committed 2026-10-26 15:52:02 -04:00
1 parent 2d8957a401
commit 46076e165b
7 files changed
+155 -36

No files matched your search

@@ -557,12 +557,18 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
uiHandler.removeCallbacks(cursorHide)
cursorVisible = false
}
onScreenControls = OnScreenControls(this, bridge, onKeyboard = ::togglePcKeyboard).also { root.addView(it) }
onScreenControls = OnScreenControls(this, bridge, onKeyboard = ::togglePcKeyboard).also { controls ->
controls.onTouchMiss = miss@{ event ->
if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.ON_SCREEN_CONTROLS, event)) return@miss
event.offsetLocation(controls.left - surfaceView.left.toFloat(), controls.top - surfaceView.top.toFloat())
onTouchEvent(event)
}
root.addView(controls)
}
screenKeysShown = com.droiddeck.launcher.input.ControllerPrefs.screenKeysShown(this)
screenKeys = ScreenKeys(this) { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down) }.also { keys ->
keys.onTouchMiss = miss@{ event ->
if (routedTouchMode == SessionPrefs.TOUCH_GESTURES &&
!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, event)) return@miss
if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, event)) return@miss
event.offsetLocation(keys.left - surfaceView.left.toFloat(), keys.top - surfaceView.top.toFloat())
onTouchEvent(event)
}
@@ -587,8 +593,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
// game surface and on-screen buttons, in either finger-down order.
root.isMotionEventSplittingEnabled = true
surfaceView.setOnTouchListener { _, event ->
if (routedTouchMode != SessionPrefs.TOUCH_GESTURES) {
gestureTouchStreamGate.reset()
if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) {
false
} else if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SURFACE, event)) {
false
@@ -2337,6 +2342,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
if (pipUi) return true
if (event.actionMasked == MotionEvent.ACTION_DOWN && event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) {
drawerControllerActive = false
gestureTouchStreamGate.reset()
}
return super.dispatchTouchEvent(event)
}
@@ -2987,6 +2993,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
uiHandler.postDelayed({ updateOnScreenControls() }, 700)
}
view.onStyle = { screenKeys?.refreshAppearance() }
view.onTouchMiss = miss@{ event ->
if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.ON_SCREEN_CONTROLS, event)) return@miss
event.offsetLocation(view.left - surfaceView.left.toFloat(), view.top - surfaceView.top.toFloat())
onTouchEvent(event)
}
root.addView(view, root.indexOfChild(onScreenControls) + 1)
steamTouchControls = view
steamTouchAvailable = true
@@ -4,7 +4,7 @@ import android.view.MotionEvent
/** Keeps split Android touch targets from restarting one shared gesture recognizer. */
internal class GestureTouchStreamGate {
enum class Source { SURFACE, SCREEN_KEYS }
enum class Source { SURFACE, SCREEN_KEYS, ON_SCREEN_CONTROLS }
private var owner: Source? = null
@@ -96,6 +96,8 @@ class OnScreenControls(
private var quickHidden = false
private var quickPressedBy = -1
private var keyboardPressedBy = -1
private var forwardedPointerBits = 0
var onTouchMiss: ((MotionEvent) -> Unit)? = null
private var settings = ControllerPrefs.read(context)
private var snap = settings.snap
private var safe = Rect()
@@ -515,6 +517,9 @@ class OnScreenControls(
val index = event.actionIndex
val x = event.getX(index)
val y = event.getY(index)
if (event.actionMasked == MotionEvent.ACTION_DOWN) forwardedPointerBits = 0
if (event.actionMasked == MotionEvent.ACTION_POINTER_DOWN && forwardMiss(event, x, y)) return true
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
if (event.actionMasked == MotionEvent.ACTION_DOWN && keyboardContains(x, y)) {
keyboardPressedBy = event.getPointerId(index)
invalidate()
@@ -542,11 +547,13 @@ class OnScreenControls(
return true
}
MotionEvent.ACTION_MOVE -> {
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
if (keyboardPressedBy != -1) return true
if (quickPressedBy != -1) return true
var changed = false
for (index in 0 until event.pointerCount) {
val pointer = event.getPointerId(index)
if (forwardedPointerBits and (1 shl pointer) != 0) continue
val x = event.getX(index)
val y = event.getY(index)
val stick = controls.firstOrNull { it.stick >= 0 && it.pressedBy == pointer }
@@ -568,10 +575,16 @@ class OnScreenControls(
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> {
val pointer = event.getPointerId(event.actionIndex)
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
if (event.actionMasked == MotionEvent.ACTION_CANCEL) {
forwardedPointerBits = 0
releaseAll()
return true
}
if (forwardedPointerBits and (1 shl pointer) != 0) {
forwardedPointerBits = forwardedPointerBits and (1 shl pointer).inv()
return true
}
if (keyboardPressedBy == pointer) {
val tapped = event.actionMasked == MotionEvent.ACTION_UP && keyboardContains(event.x, event.y)
keyboardPressedBy = -1
@@ -604,6 +617,23 @@ class OnScreenControls(
return false
}
private fun forwardMiss(event: MotionEvent, x: Float, y: Float): Boolean {
if (onTouchMiss == null || keyboardPressedBy != -1 || quickPressedBy != -1) return false
if (!quickHidden && (controlAt(x, y) ?: adaptiveStickAt(x, y)) != null) return false
forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(event.actionIndex))
forwardPointers(event, forwardedPointerBits)
return true
}
private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) {
val split = event.pointerSubset(pointerIdBits) ?: return
try {
onTouchMiss?.invoke(split)
} finally {
split.recycle()
}
}
private fun onEditTouch(event: MotionEvent): Boolean {
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
@@ -0,0 +1,35 @@
package com.droiddeck.launcher.input
import android.view.MotionEvent
internal fun MotionEvent.pointerSubset(pointerIdBits: Int): MotionEvent? {
val indices = (0 until pointerCount).filter { index ->
pointerIdBits and (1 shl getPointerId(index)) != 0
}
if (indices.isEmpty()) return null
val splitIndex = indices.indexOf(actionIndex)
val splitAction = when (actionMasked) {
MotionEvent.ACTION_DOWN -> if (splitIndex >= 0) MotionEvent.ACTION_DOWN else MotionEvent.ACTION_MOVE
MotionEvent.ACTION_POINTER_DOWN -> if (splitIndex >= 0) {
if (indices.size == 1) MotionEvent.ACTION_DOWN
else MotionEvent.ACTION_POINTER_DOWN or (splitIndex shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
} else MotionEvent.ACTION_MOVE
MotionEvent.ACTION_UP -> if (splitIndex >= 0) MotionEvent.ACTION_UP else MotionEvent.ACTION_MOVE
MotionEvent.ACTION_POINTER_UP -> if (splitIndex >= 0) {
if (indices.size == 1) MotionEvent.ACTION_UP
else MotionEvent.ACTION_POINTER_UP or (splitIndex shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
} else MotionEvent.ACTION_MOVE
else -> action
}
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])
}
return MotionEvent.obtain(
downTime, eventTime, splitAction, indices.size, properties, coordinates,
metaState, buttonState, xPrecision, yPrecision,
deviceId, edgeFlags, source, flags,
)
}
@@ -926,35 +926,7 @@ class ScreenKeys(context: Context, private val sendKey: (Int, Boolean) -> Unit)
}
private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) {
val indices = (0 until event.pointerCount).filter { index ->
pointerIdBits and (1 shl event.getPointerId(index)) != 0
}
if (indices.isEmpty()) return
val actionIndex = indices.indexOf(event.actionIndex)
val action = when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> if (actionIndex >= 0) MotionEvent.ACTION_DOWN else MotionEvent.ACTION_MOVE
MotionEvent.ACTION_POINTER_DOWN -> if (actionIndex >= 0) {
if (indices.size == 1) MotionEvent.ACTION_DOWN
else MotionEvent.ACTION_POINTER_DOWN or (actionIndex shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
} else MotionEvent.ACTION_MOVE
MotionEvent.ACTION_UP -> if (actionIndex >= 0) MotionEvent.ACTION_UP else MotionEvent.ACTION_MOVE
MotionEvent.ACTION_POINTER_UP -> if (actionIndex >= 0) {
if (indices.size == 1) MotionEvent.ACTION_UP
else MotionEvent.ACTION_POINTER_UP or (actionIndex shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
} else MotionEvent.ACTION_MOVE
else -> event.action
}
val properties = Array(indices.size) { MotionEvent.PointerProperties() }
val coordinates = Array(indices.size) { MotionEvent.PointerCoords() }
indices.forEachIndexed { target, source ->
event.getPointerProperties(source, properties[target])
event.getPointerCoords(source, coordinates[target])
}
val split = MotionEvent.obtain(
event.downTime, event.eventTime, action, indices.size, properties, coordinates,
event.metaState, event.buttonState, event.xPrecision, event.yPrecision,
event.deviceId, event.edgeFlags, event.source, event.flags,
)
val split = event.pointerSubset(pointerIdBits) ?: return
try {
onTouchMiss?.invoke(split)
} finally {
@@ -103,6 +103,8 @@ class SteamTouchControls(
private class Finger(val element: Element?, var x: Float, var y: Float, val toolbar: Int = -1)
private val fingers = HashMap<Int, Finger>()
private var forwardedPointerBits = 0
var onTouchMiss: ((MotionEvent) -> Unit)? = null
private var editing = false
private var editElements: MutableList<Element> = mutableListOf()
@@ -629,7 +631,17 @@ class SteamTouchControls(
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
val x = event.getX(index)
val y = event.getY(index)
val e = hit(live(), x, y) ?: return event.actionMasked == MotionEvent.ACTION_POINTER_DOWN
if (event.actionMasked == MotionEvent.ACTION_DOWN) forwardedPointerBits = 0
val e = hit(live(), x, y)
if (e == null) {
if (event.actionMasked == MotionEvent.ACTION_DOWN) return false
if (onTouchMiss != null) {
forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(index))
forwardPointers(event, forwardedPointerBits)
}
return true
}
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
fingers[event.getPointerId(index)] = Finger(e, x, y)
if (feedback) performHapticFeedback(android.view.HapticFeedbackConstants.VIRTUAL_KEY)
requestUnbufferedDispatch(event)
@@ -637,6 +649,7 @@ class SteamTouchControls(
return true
}
MotionEvent.ACTION_MOVE -> {
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
for (i in 0 until event.pointerCount) {
val f = fingers[event.getPointerId(i)] ?: continue
f.x = event.getX(i)
@@ -655,6 +668,8 @@ class SteamTouchControls(
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> {
val id = event.getPointerId(index)
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
forwardedPointerBits = if (event.actionMasked == MotionEvent.ACTION_CANCEL) 0 else forwardedPointerBits and (1 shl id).inv()
val f = fingers.remove(id)
if (event.actionMasked == MotionEvent.ACTION_CANCEL) fingers.clear()
val e = f?.element
@@ -673,6 +688,15 @@ class SteamTouchControls(
}
private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) {
val split = event.pointerSubset(pointerIdBits) ?: return
try {
onTouchMiss?.invoke(split)
} finally {
split.recycle()
}
}
val isEditing get() = editing
fun startEditing() {