mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
On-screen controls for a device with no pad, gone the moment one is attached
A phone had no way to answer Big Picture: touch moved the pointer and nothing pressed a button. The on-screen pad writes into the same rings the physical one does, so the client sees a single Xbox pad whichever is in use and both work at once. An InputManager listener takes the controls away when a controller appears and brings them back when it goes; steamdeck-osc in Downloads pins it either way. Virtual input devices no longer count as controllers — the system's own synthetic devices would otherwise hide the controls with nothing to replace them.
This commit is contained in:
1 parent
cc868496d1
commit
432e197454
4 files changed
+281
-1
No files matched your search
@@ -4,6 +4,8 @@ import android.app.Activity
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.hardware.input.InputManager
|
||||
import android.os.Environment
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.view.KeyEvent
|
||||
@@ -17,6 +19,7 @@ import android.widget.TextView
|
||||
import com.steamdeck.launcher.core.FileUtils
|
||||
import com.steamdeck.launcher.gpu.TurnipDriver
|
||||
import com.steamdeck.launcher.input.EvdevKeys
|
||||
import com.steamdeck.launcher.input.OnScreenControls
|
||||
import com.steamdeck.launcher.input.PadBridge
|
||||
import com.steamdeck.launcher.runtime.LinuxRuntime
|
||||
import com.steamdeck.launcher.session.SessionService
|
||||
@@ -39,8 +42,20 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
|
||||
private lateinit var surfaceView: SurfaceView
|
||||
private lateinit var statusView: TextView
|
||||
private var padBridge: PadBridge? = null
|
||||
private var onScreenControls: OnScreenControls? = null
|
||||
private var watching = true
|
||||
|
||||
/**
|
||||
* Shows the on-screen pad when nothing is plugged in and takes it away the moment something
|
||||
* is — a user with a controller in their hands should not be looking at buttons they cannot
|
||||
* press, and a user without one must not be left with no way to answer Big Picture.
|
||||
*/
|
||||
private val deviceListener = object : InputManager.InputDeviceListener {
|
||||
override fun onInputDeviceAdded(deviceId: Int) = updateOnScreenControls()
|
||||
override fun onInputDeviceRemoved(deviceId: Int) = updateOnScreenControls()
|
||||
override fun onInputDeviceChanged(deviceId: Int) = updateOnScreenControls()
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
@@ -69,7 +84,10 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
|
||||
root.addView(statusView)
|
||||
setContentView(root)
|
||||
|
||||
padBridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input"))
|
||||
val bridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input"))
|
||||
padBridge = bridge
|
||||
onScreenControls = OnScreenControls(this, bridge).also { root.addView(it, 1) }
|
||||
updateOnScreenControls()
|
||||
WaylandCompositor.setFirstFrameListener { runOnUiThread { statusView.visibility = View.GONE } }
|
||||
SessionState.endListener = { status -> onSessionEnded(status) }
|
||||
watchSessionLog()
|
||||
@@ -242,6 +260,40 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
|
||||
moveTaskToBack(true)
|
||||
}
|
||||
|
||||
/**
|
||||
* `steamdeck-osc` in Downloads forces the matter either way ("always" / "never"); with no such
|
||||
* file the controls follow what is attached.
|
||||
*/
|
||||
private fun updateOnScreenControls() {
|
||||
val forced = File(Environment.getExternalStorageDirectory(), "Download/steamdeck-osc")
|
||||
.takeIf { it.isFile }
|
||||
?.let { FileUtils.readString(it)?.trim()?.lowercase() }
|
||||
val show = when {
|
||||
forced == "always" -> true
|
||||
forced == "never" -> false
|
||||
else -> !PadBridge.anyControllerConnected()
|
||||
}
|
||||
val controls = onScreenControls ?: return
|
||||
if (show == (controls.visibility == View.VISIBLE)) return
|
||||
if (!show) controls.releaseAll()
|
||||
controls.visibility = if (show) View.VISIBLE else View.GONE
|
||||
Log.i(TAG, "on-screen controls " + (if (show) "shown" else "hidden"))
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
(getSystemService(INPUT_SERVICE) as? InputManager)
|
||||
?.registerInputDeviceListener(deviceListener, Handler(Looper.getMainLooper()))
|
||||
updateOnScreenControls()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
(getSystemService(INPUT_SERVICE) as? InputManager)?.unregisterInputDeviceListener(deviceListener)
|
||||
// A button held when the app goes away would stay held in the ring for the whole session.
|
||||
onScreenControls?.releaseAll()
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
super.onWindowFocusChanged(hasFocus)
|
||||
if (hasFocus) goFullscreen()
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
package com.steamdeck.launcher.input
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RectF
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
|
||||
/**
|
||||
* A pad drawn on the screen for devices that have no controller attached.
|
||||
*
|
||||
* It writes into the same [PadBridge] a physical pad does, so the Steam client sees one Xbox pad
|
||||
* whichever is being used, and the two can be used at once. The layout is fixed and deliberately
|
||||
* small: Big Picture needs a d-pad, A and B, a menu button and the Steam button, and anything
|
||||
* more would be a control editor rather than a way to sign in and start a game.
|
||||
*
|
||||
* Only the controls themselves take touches; everything else falls through to the activity, where
|
||||
* it moves the mouse pointer.
|
||||
*/
|
||||
@SuppressLint("ViewConstructor")
|
||||
class OnScreenControls(context: Context, private val pad: PadBridge) : View(context) {
|
||||
|
||||
private class Control(
|
||||
val id: String,
|
||||
val label: String,
|
||||
/** Bit index in GamepadState, or -1 for a d-pad direction. */
|
||||
val button: Int,
|
||||
/** Index into GamepadState.dpad, or -1. */
|
||||
val dpad: Int,
|
||||
val radius: Float,
|
||||
) {
|
||||
var cx = 0f
|
||||
var cy = 0f
|
||||
var pressedBy = -1 // the pointer id holding it, or -1
|
||||
val bounds = RectF()
|
||||
fun contains(x: Float, y: Float): Boolean {
|
||||
val dx = x - cx
|
||||
val dy = y - cy
|
||||
// A generous hit area: a finger on glass is not a mouse, and a miss in Big Picture
|
||||
// means the user thinks the pad does not work.
|
||||
val r = radius * 1.25f
|
||||
return dx * dx + dy * dy <= r * r
|
||||
}
|
||||
}
|
||||
|
||||
private val density = resources.displayMetrics.density
|
||||
private fun dp(value: Float) = value * density
|
||||
|
||||
private val controls = listOf(
|
||||
Control("up", "▲", -1, 0, dp(26f)),
|
||||
Control("right", "▶", -1, 1, dp(26f)),
|
||||
Control("down", "▼", -1, 2, dp(26f)),
|
||||
Control("left", "◀", -1, 3, dp(26f)),
|
||||
Control("a", "A", 0, -1, dp(30f)),
|
||||
Control("b", "B", 1, -1, dp(30f)),
|
||||
Control("x", "X", 2, -1, dp(30f)),
|
||||
Control("y", "Y", 3, -1, dp(30f)),
|
||||
Control("lb", "LB", 4, -1, dp(24f)),
|
||||
Control("rb", "RB", 5, -1, dp(24f)),
|
||||
Control("select", "⧉", 6, -1, dp(20f)),
|
||||
Control("start", "☰", 7, -1, dp(20f)),
|
||||
// The client's own in-game menu; the interposer publishes it as BTN_MODE.
|
||||
Control("guide", "◉", GamepadState.IDX_BUTTON_MODE.toInt(), -1, dp(22f)),
|
||||
)
|
||||
|
||||
private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL }
|
||||
private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
style = Paint.Style.STROKE
|
||||
strokeWidth = dp(1.5f)
|
||||
}
|
||||
private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
textAlign = Paint.Align.CENTER
|
||||
typeface = android.graphics.Typeface.DEFAULT_BOLD
|
||||
}
|
||||
|
||||
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
||||
super.onSizeChanged(w, h, oldw, oldh)
|
||||
layoutControls(w.toFloat(), h.toFloat())
|
||||
}
|
||||
|
||||
/** Thumbs reach the bottom corners; nothing is placed where a game's own HUD usually is. */
|
||||
private fun layoutControls(w: Float, h: Float) {
|
||||
val margin = dp(34f)
|
||||
val dpadX = margin + dp(74f)
|
||||
val dpadY = h - margin - dp(74f)
|
||||
val step = dp(52f)
|
||||
place("up", dpadX, dpadY - step)
|
||||
place("down", dpadX, dpadY + step)
|
||||
place("left", dpadX - step, dpadY)
|
||||
place("right", dpadX + step, dpadY)
|
||||
|
||||
val faceX = w - margin - dp(74f)
|
||||
val faceY = h - margin - dp(74f)
|
||||
val faceStep = dp(50f)
|
||||
place("a", faceX, faceY + faceStep)
|
||||
place("b", faceX + faceStep, faceY)
|
||||
place("x", faceX - faceStep, faceY)
|
||||
place("y", faceX, faceY - faceStep)
|
||||
|
||||
place("lb", margin + dp(46f), h - margin - dp(196f))
|
||||
place("rb", w - margin - dp(46f), h - margin - dp(196f))
|
||||
|
||||
val centre = w / 2f
|
||||
place("select", centre - dp(78f), h - margin - dp(26f))
|
||||
place("guide", centre, h - margin - dp(26f))
|
||||
place("start", centre + dp(78f), h - margin - dp(26f))
|
||||
}
|
||||
|
||||
private fun place(id: String, x: Float, y: Float) {
|
||||
val control = controls.firstOrNull { it.id == id } ?: return
|
||||
control.cx = x
|
||||
control.cy = y
|
||||
control.bounds.set(x - control.radius, y - control.radius, x + control.radius, y + control.radius)
|
||||
}
|
||||
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
for (control in controls) {
|
||||
val held = control.pressedBy != -1
|
||||
fill.color = if (held) Color.argb(150, 199, 125, 255) else Color.argb(70, 20, 12, 30)
|
||||
canvas.drawCircle(control.cx, control.cy, control.radius, fill)
|
||||
stroke.color = if (held) Color.argb(220, 235, 212, 255) else Color.argb(110, 201, 160, 255)
|
||||
canvas.drawCircle(control.cx, control.cy, control.radius, stroke)
|
||||
text.color = if (held) Color.WHITE else Color.argb(190, 225, 210, 245)
|
||||
text.textSize = control.radius * 0.85f
|
||||
canvas.drawText(control.label, control.cx, control.cy + text.textSize * 0.35f, text)
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
|
||||
val index = event.actionIndex
|
||||
val control = controlAt(event.getX(index), event.getY(index)) ?: return false
|
||||
control.pressedBy = event.getPointerId(index)
|
||||
apply()
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
// A finger that slides off a button releases it, and one that slides onto another
|
||||
// presses that — which is how a d-pad is used in practice.
|
||||
var changed = false
|
||||
for (index in 0 until event.pointerCount) {
|
||||
val pointer = event.getPointerId(index)
|
||||
val over = controlAt(event.getX(index), event.getY(index))
|
||||
for (control in controls) {
|
||||
if (control.pressedBy == pointer && control !== over) {
|
||||
control.pressedBy = -1
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if (over != null && over.pressedBy == -1) {
|
||||
over.pressedBy = pointer
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if (changed) apply()
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
val pointer = event.getPointerId(event.actionIndex)
|
||||
var changed = false
|
||||
for (control in controls) {
|
||||
if (control.pressedBy == pointer || event.actionMasked == MotionEvent.ACTION_CANCEL) {
|
||||
if (control.pressedBy != -1) changed = true
|
||||
control.pressedBy = -1
|
||||
}
|
||||
}
|
||||
if (changed) apply()
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun controlAt(x: Float, y: Float): Control? = controls.firstOrNull { it.contains(x, y) }
|
||||
|
||||
private fun apply() {
|
||||
pad.applyTouch { state ->
|
||||
for (control in controls) {
|
||||
val held = control.pressedBy != -1
|
||||
if (control.dpad >= 0) state.dpad[control.dpad] = held
|
||||
else if (control.button >= 0) state.setPressed(control.button, held)
|
||||
}
|
||||
}
|
||||
invalidate()
|
||||
}
|
||||
|
||||
/** Everything up, for when the controls are hidden mid-press. */
|
||||
fun releaseAll() {
|
||||
if (controls.none { it.pressedBy != -1 }) return
|
||||
controls.forEach { it.pressedBy = -1 }
|
||||
apply()
|
||||
}
|
||||
}
|
||||
@@ -54,11 +54,22 @@ public final class PadBridge {
|
||||
/** True when the device this event came from is a gamepad or joystick, not the touchscreen. */
|
||||
public static boolean isFromController(InputDevice device) {
|
||||
if (device == null) return false;
|
||||
// A virtual device is the system's own synthetic input, not a pad somebody is holding —
|
||||
// counting it would hide the on-screen controls with nothing to replace them.
|
||||
if (device.isVirtual()) return false;
|
||||
int sources = device.getSources();
|
||||
return (sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD
|
||||
|| (sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
|
||||
}
|
||||
|
||||
/** Whether any real controller is attached right now. */
|
||||
public static boolean anyControllerConnected() {
|
||||
for (int id : InputDevice.getDeviceIds()) {
|
||||
if (isFromController(InputDevice.getDevice(id))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @return true when the event was a pad button and has been consumed. */
|
||||
public synchronized boolean onKeyEvent(KeyEvent event) {
|
||||
if (!isFromController(event.getDevice())) return false;
|
||||
@@ -119,6 +130,15 @@ public final class PadBridge {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The on-screen controls' way in: they mutate the same state a physical pad writes, so the
|
||||
* client only ever sees one device and a user can use both at once without them fighting.
|
||||
*/
|
||||
public synchronized void applyTouch(java.util.function.Consumer<GamepadState> mutation) {
|
||||
mutation.accept(state);
|
||||
publish();
|
||||
}
|
||||
|
||||
private void publish() {
|
||||
if (!open && !start()) return;
|
||||
writer.writeGamepadState(state);
|
||||
|
||||
Reference in new issue
Block a user