mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
A pointer for the desktop: an arrow, touch gestures and a real mouse
The pointer moved but nothing drew it — the nested desktop asks its parent compositor for a cursor and ours ignores the request — and touch knew only move and tap. Now the app draws the arrow where it last sent the pointer, touch works like a remote desktop (tap clicks, hold then lift right-clicks, hold then drag drags, two fingers scroll), and a mouse arrives as itself: hover moves, every button presses, the wheel scrolls, with Android's own pointer hidden so there is one arrow.
This commit is contained in:
1 parent
d6789f7b61
commit
f5ca6d0053
3 files changed
+231
-15
No files matched your search
@@ -0,0 +1,121 @@
|
||||
package com.steamdeck.launcher.input
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.MotionEvent
|
||||
import android.view.ViewConfiguration
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Touch as a mouse, the way a remote desktop does it: where you touch is where the pointer goes.
|
||||
*
|
||||
* - tap ............................ left click
|
||||
* - drag ........................... moves the pointer, no button (hover)
|
||||
* - hold, then lift ................ right click
|
||||
* - hold, then drag ................ left-button drag (a window by its title bar, a selection)
|
||||
* - two fingers up / down .......... scroll wheel
|
||||
*
|
||||
* The listener gets pointer positions in the view's pixels; the activity maps them onto the
|
||||
* compositor. Mouse events are not handled here — a mouse has real buttons.
|
||||
*/
|
||||
class PointerGestures(
|
||||
private val slopPx: Float,
|
||||
private val listener: Listener,
|
||||
) {
|
||||
interface Listener {
|
||||
fun onMove(x: Float, y: Float)
|
||||
/** [button] is an evdev code: BTN_LEFT, BTN_RIGHT. */
|
||||
fun onButton(button: Int, pressed: Boolean, x: Float, y: Float)
|
||||
fun onWheel(steps: Int)
|
||||
fun onLongPress()
|
||||
}
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private var downX = 0f
|
||||
private var downY = 0f
|
||||
private var downAt = 0L
|
||||
private var moved = false
|
||||
private var held = false // the long press fired
|
||||
private var dragging = false // left button down after a hold
|
||||
private var scrolling = false
|
||||
private var scrollAnchorY = 0f
|
||||
private val longPress = Runnable {
|
||||
held = true
|
||||
listener.onLongPress()
|
||||
}
|
||||
|
||||
fun onTouch(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
downX = event.x; downY = event.y; downAt = event.eventTime
|
||||
moved = false; held = false; dragging = false; scrolling = false
|
||||
listener.onMove(event.x, event.y)
|
||||
handler.postDelayed(longPress, HOLD_MS)
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_POINTER_DOWN -> {
|
||||
// A second finger: scrolling, and whatever the first one was doing is off.
|
||||
handler.removeCallbacks(longPress)
|
||||
if (dragging) { listener.onButton(BTN_LEFT, false, event.x, event.y); dragging = false }
|
||||
scrolling = true
|
||||
scrollAnchorY = event.getY(0)
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
if (scrolling) {
|
||||
val dy = event.getY(0) - scrollAnchorY
|
||||
val steps = (dy / SCROLL_STEP_PX).toInt()
|
||||
if (steps != 0) {
|
||||
// Finger down = content up = wheel "up", which the compositor takes as negative.
|
||||
listener.onWheel(-steps)
|
||||
scrollAnchorY += steps * SCROLL_STEP_PX
|
||||
}
|
||||
return true
|
||||
}
|
||||
if (!moved && (abs(event.x - downX) > slopPx || abs(event.y - downY) > slopPx)) {
|
||||
moved = true
|
||||
handler.removeCallbacks(longPress)
|
||||
if (held) {
|
||||
// Held, then moved: a drag with the left button, from where the hold was.
|
||||
dragging = true
|
||||
listener.onButton(BTN_LEFT, true, downX, downY)
|
||||
}
|
||||
}
|
||||
if (moved) listener.onMove(event.x, event.y)
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
handler.removeCallbacks(longPress)
|
||||
val cancelled = event.actionMasked == MotionEvent.ACTION_CANCEL
|
||||
when {
|
||||
scrolling -> {}
|
||||
dragging -> listener.onButton(BTN_LEFT, false, event.x, event.y)
|
||||
held && !moved && !cancelled -> {
|
||||
listener.onButton(BTN_RIGHT, true, downX, downY)
|
||||
listener.onButton(BTN_RIGHT, false, downX, downY)
|
||||
}
|
||||
!moved && !cancelled && event.eventTime - downAt < TAP_MS -> {
|
||||
listener.onButton(BTN_LEFT, true, downX, downY)
|
||||
listener.onButton(BTN_LEFT, false, downX, downY)
|
||||
}
|
||||
}
|
||||
scrolling = false; dragging = false; held = false
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_POINTER_UP -> return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val BTN_LEFT = 0x110
|
||||
const val BTN_RIGHT = 0x111
|
||||
const val BTN_MIDDLE = 0x112
|
||||
private const val HOLD_MS = 450L
|
||||
private const val TAP_MS = 350L
|
||||
private const val SCROLL_STEP_PX = 40f
|
||||
|
||||
fun slop(context: android.content.Context) = ViewConfiguration.get(context).scaledTouchSlop.toFloat()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user