fix(steam): preserve Back taps and recover QAM controls

This commit is contained in:
Kurt Himebauch committed 2026-10-26 13:13:08 -04:00
1 parent dde1a55d00
commit fa5506df35
13 files changed
+395 -56

No files matched your search

@@ -257,7 +257,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private val cursorResync = Runnable { if (syncClientCursor() && clientHidesCursor) cursorVisible = false }
private val uiHandler = Handler(Looper.getMainLooper())
private var pendingBackAction: Runnable? = null
private val backQamInput = com.droiddeck.launcher.input.BackQamInput { padBridge?.setBackQamPressed(it) }
private val backQamInput = com.droiddeck.launcher.input.BackQamInput(
setPressed = { padBridge?.setBackQamPressed(it) },
onTap = { performBackAction(double = it) },
)
private var drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN
private var drawerDirectionDownTime = 0L
private var drawerDirectionDeviceId = -1
@@ -1,10 +1,21 @@
package com.droiddeck.launcher.input
import android.view.KeyEvent
import android.os.Handler
import android.os.Looper
class BackQamInput(private val setPressed: (Boolean) -> Unit) {
private val held = mutableSetOf<Int>()
class BackQamInput(private val setPressed: (Boolean) -> Unit, private val onTap: (Boolean) -> Unit) {
private class Press(val hold: Runnable, var held: Boolean = false, var used: Boolean = false)
private val handler = Handler(Looper.getMainLooper())
private val presses = mutableMapOf<Int, Press>()
private val cancelled = mutableSetOf<Int>()
private var tapPending = false
private val singleTap = Runnable { tapPending = false; onTap(false) }
private fun cancelTap() {
handler.removeCallbacks(singleTap)
tapPending = false
}
fun onKeyEvent(event: KeyEvent, enabled: Boolean): Boolean {
if (!enabled) cancel()
@@ -12,31 +23,54 @@ class BackQamInput(private val setPressed: (Boolean) -> Unit) {
val device = event.deviceId
return when (event.action) {
KeyEvent.ACTION_DOWN -> {
if (event.repeatCount > 0) return enabled || device in held || device in cancelled
if (event.repeatCount > 0) return enabled || device in presses || device in cancelled
cancelled.remove(device)
if (!enabled || event.isCanceled) return false
if (held.add(device) && held.size == 1) setPressed(true)
if (device in presses) return true
handler.removeCallbacks(singleTap)
val hold = Runnable {
val press = presses[device] ?: return@Runnable
val alreadyHeld = presses.values.any { it.held }
press.held = true
presses.values.forEach { it.used = true }
cancelTap()
if (!alreadyHeld) setPressed(true)
}
presses[device] = Press(hold, used = presses.values.any { it.held })
handler.postDelayed(hold, 500)
true
}
KeyEvent.ACTION_UP -> {
val released = held.remove(device)
val press = presses.remove(device)
val wasCancelled = cancelled.remove(device)
if (released && held.isEmpty()) setPressed(false)
released || wasCancelled || enabled
if (press != null) {
handler.removeCallbacks(press.hold)
if (press.held && presses.values.none { it.held }) setPressed(false)
if (!press.used && !event.isCanceled) {
if (tapPending) { cancelTap(); onTap(true) }
else { tapPending = true; handler.postDelayed(singleTap, 500) }
} else if (event.isCanceled) cancelTap()
}
press != null || wasCancelled || enabled
}
else -> device in held || device in cancelled
else -> device in presses || device in cancelled
}
}
fun cancel() {
if (held.isEmpty()) return
cancelled.addAll(held)
held.clear()
setPressed(false)
cancelTap()
val wasHeld = presses.values.any { it.held }
presses.values.forEach { handler.removeCallbacks(it.hold) }
cancelled.addAll(presses.keys)
presses.clear()
if (wasHeld) setPressed(false)
}
fun onDeviceRemoved(device: Int) {
cancelled.remove(device)
if (held.remove(device) && held.isEmpty()) setPressed(false)
val press = presses.remove(device) ?: return
handler.removeCallbacks(press.hold)
cancelTap()
if (press.held && presses.values.none { it.held }) setPressed(false)
}
}
@@ -2,6 +2,10 @@ package com.droiddeck.launcher.runtime
import android.content.Context
import android.content.Intent
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.database.ContentObserver
import android.media.AudioManager
import android.net.Uri
@@ -12,19 +16,20 @@ import android.os.SystemClock
import android.provider.Settings
import android.util.Log
import com.droiddeck.launcher.core.SessionPart
import com.droiddeck.launcher.R
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import org.json.JSONObject
import kotlin.math.roundToInt
class DeviceControlsComponent(context: Context, root: File) : SessionPart() {
class DeviceControlsComponent(context: Context, root: File, private val activityVisible: () -> Boolean = { false }) : SessionPart() {
private val context = context.applicationContext
private val audio = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
private val directory = directory(root)
private val handler = Handler(Looper.getMainLooper())
private var running = false
private var pendingBrightness: Pair<Float, Long>? = null
private var pendingBrightness: Float? = null
private var permissionRequestAt = -5_000L
private var previous = ""
private var revision = 0L
@@ -41,10 +46,10 @@ class DeviceControlsComponent(context: Context, root: File) : SessionPart() {
override fun run() {
if (!running) return
consume()
pendingBrightness?.let { (value, time) ->
if (SystemClock.elapsedRealtime() - time > 120_000) pendingBrightness = null
else if (Settings.System.canWrite(context)) {
pendingBrightness?.let { value ->
if (Settings.System.canWrite(context)) {
pendingBrightness = null
context.getSystemService(NotificationManager::class.java).cancel(PERMISSION_NOTIFICATION)
brightness(value)
}
}
@@ -94,6 +99,7 @@ class DeviceControlsComponent(context: Context, root: File) : SessionPart() {
requests.stopWatching()
context.contentResolver.unregisterContentObserver(observer)
pendingBrightness = null
context.getSystemService(NotificationManager::class.java).cancel(PERMISSION_NOTIFICATION)
}
}
@@ -128,13 +134,23 @@ class DeviceControlsComponent(context: Context, root: File) : SessionPart() {
private fun brightness(value: Float) {
if (!Settings.System.canWrite(context)) {
pendingBrightness = value to SystemClock.elapsedRealtime()
pendingBrightness = value
val now = SystemClock.elapsedRealtime()
if (now - permissionRequestAt >= 5_000) {
permissionRequestAt = now
try {
context.startActivity(Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:${context.packageName}")).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:${context.packageName}")).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val manager = context.getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(NotificationChannel(PERMISSION_CHANNEL,
context.getString(R.string.brightness_permission_title), NotificationManager.IMPORTANCE_DEFAULT))
val action = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
manager.notify(PERMISSION_NOTIFICATION, Notification.Builder(context, PERMISSION_CHANNEL)
.setSmallIcon(R.drawable.ic_stat_session)
.setContentTitle(context.getString(R.string.brightness_permission_title))
.setContentText(context.getString(R.string.brightness_permission_text))
.setContentIntent(action).setOnlyAlertOnce(true).setAutoCancel(true).build())
if (activityVisible()) context.startActivity(intent)
} catch (e: Exception) {
Log.w(TAG, "Could not open brightness permission", e)
}
@@ -161,6 +177,8 @@ class DeviceControlsComponent(context: Context, root: File) : SessionPart() {
companion object {
private const val TAG = "DeviceControls"
private const val PERMISSION_CHANNEL = "brightness_permission"
private const val PERMISSION_NOTIFICATION = 0x4442
private val REQUESTS = setOf("brightness.request", "volume.request", "mute.request")
@JvmStatic fun directory(root: File) = File(root, "etc/droiddeck-device-controls")
@JvmStatic fun prepareBacklight(context: Context, root: File): File {
@@ -630,7 +630,7 @@ class SessionService : Service() {
networkLink.attach(this)
networkLink.publish()
components.add(networkLink)
val deviceControls = com.droiddeck.launcher.runtime.DeviceControlsComponent(this, root)
val deviceControls = com.droiddeck.launcher.runtime.DeviceControlsComponent(this, root) { activityVisible && screenOn }
deviceControls.attach(this)
deviceControls.publish()
components.add(deviceControls)
@@ -306,15 +306,16 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras:
stringResource(R.string.unfolded_controls_with_controller_hint),
s.unfoldedControlsWithController, onChange = a.onUnfoldedControlsWithController,
)
if (steam && s.steamController in listOf(SessionPrefs.CONTROLLER_DECK, SessionPrefs.CONTROLLER_STEAM)) {
Note(stringResource(R.string.back_native_qam_hint))
} else if (steam) ChoiceRow(
if (steam) ChoiceRow(
host, "back-actions", stringResource(R.string.mode_back), stringResource(SessionPrefs.backActionsOrder(s.backActionsInverted)),
listOf(
false to stringResource(SessionPrefs.BACK_MENU_THEN_QAM),
true to stringResource(SessionPrefs.BACK_QAM_THEN_MENU),
), s.backActionsInverted, onPick = a.onBackActionsInverted,
)
if (steam && s.steamController in listOf(SessionPrefs.CONTROLLER_DECK, SessionPrefs.CONTROLLER_STEAM)) {
Note(stringResource(R.string.back_native_qam_hint))
}
}
}
if (tab == SheetTab.AUDIO && steam) {
@@ -604,11 +604,12 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
chipModifier = focus.track(page, "keyboard-button"), onChange = a.onKeyboardButton)
}
}
if (a.steam && a.padType in listOf(SessionPrefs.CONTROLLER_DECK, SessionPrefs.CONTROLLER_STEAM)) {
Note(stringResource(R.string.back_native_qam_hint))
} else if (a.steam) ChoiceRow(host, "back-actions", stringResource(R.string.mode_back), null,
if (a.steam) ChoiceRow(host, "back-actions", stringResource(R.string.mode_back), null,
listOf(false to stringResource(SessionPrefs.BACK_MENU_THEN_QAM), true to stringResource(SessionPrefs.BACK_QAM_THEN_MENU)),
a.backActionsInverted, chipModifier = focus.track(page, "back-actions"), onPick = a.onBackActionsInverted)
if (a.steam && a.padType in listOf(SessionPrefs.CONTROLLER_DECK, SessionPrefs.CONTROLLER_STEAM)) {
Note(stringResource(R.string.back_native_qam_hint))
}
if (a.steam && a.unfoldedControls != null) ToggleRow(host, "unfolded-controls", stringResource(R.string.unfolded_controls),
stringResource(R.string.unfolded_controls_hint), a.unfoldedControls,
chipModifier = focus.track(page, "unfolded-controls"), onChange = a.onUnfoldedControls)