feat: KDE desktop on the bottom screen beside the session (#1966)

* feat: KDE desktop as a bottom-screen mode beside the top-screen session

* test: bottom-desktop launch test sets DISPLAY instead of inheriting it

* fix: drop the bottom desktop's launch when the session stops while preparing it

* fix: bottom desktop restarts on a fresh surface, trackpad/touch toggle, loading throbber, DroidDeck's display driver

* fix: bottom desktop pauses with the session and opens the keyboard for a tapped text field

* fix: no bottom desktop guest starts or keeps running unfrozen while the session is paused

* fix: bottom desktop treats a freeze in progress as paused

* fix: only the session that began a freeze clears its pausing flag

* fix: bottom desktop launch is stored and cleared only for its own session generation

* fix: a stopped session tears down the bottom desktop it started even after a newer launch is prepared

* fix: a new session's bottom desktop replaces one an earlier session left running

* fix: release the bottom desktop's keyboard watcher when its compositor dies

* fix: bottom desktop starts its guest only for the launch its compositor was attached for

* fix: bottom desktop ignores compositor replies to an earlier session's attach
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-01 16:13:52 -05:00
1 parent 8cb5c2168e
commit bf9435cd36
26 files changed
+1083 -36

No files matched your search

@@ -0,0 +1,353 @@
package com.droiddeck.launcher.session
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.os.Messenger
import android.util.Log
import android.view.Surface
import com.droiddeck.launcher.core.HostProcess
import com.droiddeck.launcher.runtime.DesktopCatalog
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.droiddeck.launcher.wayland.BottomDesktopCompositorService as Comp
import java.io.File
/**
* KDE Plasma on the bottom screen while the session keeps the top one: its own compositor process
* ([Comp]) and a second guest started with the session's environment, like the bottom-screen terminal.
*/
object BottomDesktop {
private const val TAG = "BottomDesktop"
const val SCRIPT = "/usr/local/bin/droiddeck-bottom-desktop"
private const val TARGET_SHORT_SIDE = 720f
enum class State { OFF, STARTING, RUNNING, ENDED, FAILED }
private class Launch(
val guest: List<String>,
val binds: List<String>,
val sessionRoot: File,
val hostEnvironment: Array<String>,
val root: File,
val sessionDir: File,
val generation: Int,
val installed: Boolean,
)
@Volatile private var launch: Launch? = null
private val launchLock = Any()
private val main = Handler(Looper.getMainLooper())
private val listeners = LinkedHashSet<() -> Unit>()
@Volatile var state = State.OFF
private set
private var onTextInput: ((Boolean) -> Unit)? = null
private var imeWatcher: android.os.FileObserver? = null
private var appContext: Context? = null
private var service: Messenger? = null
private var binding: ServiceConnection? = null
private var pendingAttach: Message? = null
/** The desktop guest's proot: -1 when there is none, 0 while it is being started. */
@Volatile private var guestPid = -1
/** The proot to freeze with the session when it pauses. */
val suspendPid: Int get() = guestPid
private var guestGeneration = 0
private var outputSize = 0 to 0
private var cursorShown = false
/** The session generation whose launch the running compositor and guest belong to; -1 when none. */
private var owner = -1
private val replies = Messenger(object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
if (msg.arg1 != owner) return
when (msg.what) {
Comp.MSG_ATTACHED -> {
service?.let { send(it, Message.obtain(null, Comp.MSG_CURSOR, if (cursorShown) 1 else 0, 0)) }
if (guestPid < 0 && state == State.STARTING) startGuest()
}
Comp.MSG_FIRST_FRAME -> if (state == State.STARTING) setState(State.RUNNING)
}
}
})
fun prepare(
guest: List<String>,
binds: List<String>,
sessionRoot: File,
hostEnvironment: Array<String>,
root: File,
sessionDir: File,
generation: Int,
stillCurrent: () -> Boolean,
) {
val prepared = Launch(ArrayList(guest), ArrayList(binds), sessionRoot, hostEnvironment.copyOf(), root, sessionDir, generation,
DesktopCatalog.desktopInstalled(root))
synchronized(launchLock) { if (stillCurrent()) launch = prepared }
}
fun available(): Boolean =
launch?.installed == true && SessionState.running && SessionState.mode != SessionService.MODE_DESKTOP
/**
* The desktop renders below the panel's resolution: about 720 pixels on the short side keeps
* Plasma's text big enough to touch and its software rendering cheap. A running desktop keeps its size.
*/
fun outputSizeFor(width: Int, height: Int): Pair<Int, Int> {
if ((state == State.STARTING || state == State.RUNNING) && outputSize.first > 0) return outputSize
if (width <= 0 || height <= 0) return 0 to 0
val scale = maxOf(1f, Math.round(minOf(width, height) / TARGET_SHORT_SIDE * 2f) / 2f)
return ((width / scale).toInt() and 1.inv()) to ((height / scale).toInt() and 1.inv())
}
fun addListener(listener: () -> Unit) { listeners.add(listener) }
fun removeListener(listener: () -> Unit) { listeners.remove(listener) }
/** Called on the main thread when a text field in the desktop gains or loses focus (droiddeck-ime's report). */
fun setTextInputListener(listener: ((Boolean) -> Unit)?) { onTextInput = listener }
private fun watchIme(dir: File) {
imeWatcher?.stopWatching()
dir.deleteRecursively()
dir.mkdirs()
@Suppress("DEPRECATION")
imeWatcher = object : android.os.FileObserver(dir.path, MOVED_TO or CLOSE_WRITE) {
override fun onEvent(event: Int, path: String?) {
if (path != "ime.json") return
val focused = runCatching { org.json.JSONObject(File(dir, path).readText()).optBoolean("textField") }
.getOrNull() ?: return
main.post { if (imeWatcher === this) onTextInput?.invoke(focused) }
}
}.also { it.startWatching() }
}
private fun setState(next: State) {
if (Looper.myLooper() != Looper.getMainLooper()) { main.post { setState(next) }; return }
if (state == next) return
Log.i(TAG, "state $state -> $next")
state = next
listeners.toList().forEach { it() }
}
fun attach(context: Context, surface: Surface, width: Int, height: Int, refreshHz: Float) {
val current = launch
if (current == null || !SessionState.running) {
setState(State.FAILED)
return
}
if (owner != -1 && owner != current.generation) stop(context, owner)
appContext = context.applicationContext
owner = current.generation
outputSize = width to height
val dir = File(current.sessionDir, "bottom-desktop").apply { mkdirs() }
val msg = Message.obtain(null, Comp.MSG_ATTACH).apply {
arg1 = owner
obj = surface
replyTo = replies
data = Bundle().apply {
putString(Comp.KEY_RUNTIME_DIR, runtimeDir(context).path)
putInt(Comp.KEY_WIDTH, width)
putInt(Comp.KEY_HEIGHT, height)
putFloat(Comp.KEY_REFRESH, refreshHz)
putString(Comp.KEY_LOG, File(dir, "wayland.log").path)
}
}
if (state == State.OFF || state == State.ENDED || state == State.FAILED) setState(State.STARTING)
val target = service
if (target != null) send(target, msg) else {
pendingAttach = msg
bind(context.applicationContext)
}
}
fun detach() {
pendingAttach = null
service?.let { send(it, Message.obtain(null, Comp.MSG_DETACH)) }
}
fun sendTouch(action: Int, pointerId: Int, x: Int, y: Int) {
val target = service ?: return
send(target, Message.obtain(null, Comp.MSG_TOUCH, (pointerId shl 8) or (action and 0xff),
(x.coerceIn(0, 0xffff) shl 16) or y.coerceIn(0, 0xffff)))
}
fun sendPointer(x: Int, y: Int) {
val target = service ?: return
send(target, Message.obtain(null, Comp.MSG_POINTER, x, y))
}
/** Button (3), wheel (4) and smooth-scroll (12) input, as `WaylandCompositor.nativeSendSceneInput` takes it. */
fun sendScene(type: Int, a: Int, b: Int = 0) {
val target = service ?: return
send(target, Message.obtain(null, Comp.MSG_SCENE, type, a).apply {
if (b != 0) data = Bundle().apply { putInt(Comp.KEY_B, b) }
})
}
fun showCursor(on: Boolean) {
cursorShown = on
service?.let { send(it, Message.obtain(null, Comp.MSG_CURSOR, if (on) 1 else 0, 0)) }
}
/** False when the next attach starts a new compositor process, which needs a surface no earlier one used. */
val connected: Boolean get() = service != null
fun sendKey(evdev: Int, down: Boolean) {
val target = service ?: return
send(target, Message.obtain(null, Comp.MSG_KEY, evdev, if (down) 1 else 0))
}
fun sendText(text: String) {
val target = service ?: return
send(target, Message.obtain(null, Comp.MSG_TEXT).apply { data = Bundle().apply { putString(Comp.KEY_TEXT, text) } })
}
fun stop(context: Context, generation: Int? = null) {
if (Looper.myLooper() != Looper.getMainLooper()) { main.post { stop(context, generation) }; return }
synchronized(launchLock) {
if (generation != null && launch?.generation == generation) launch = null
}
// A newer session may already have prepared its launch; only the desktop the stopped session started goes.
if (generation != null && owner != generation) return
owner = -1
val pid = guestPid
guestPid = -1
guestGeneration++
if (pid > 1) SessionService.stopTerminalProcess(context, pid)
service?.let { send(it, Message.obtain(null, Comp.MSG_STOP)) }
binding?.let { runCatching { context.applicationContext.unbindService(it) } }
binding = null
service = null
pendingAttach = null
imeWatcher?.stopWatching()
imeWatcher = null
setState(State.OFF)
}
private fun bind(context: Context) {
if (binding != null) return
val connection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
if (binding !== this || binder == null) return
val messenger = Messenger(binder)
service = messenger
pendingAttach?.let { send(messenger, it) }
pendingAttach = null
}
override fun onServiceDisconnected(name: ComponentName?) {
if (binding !== this) return
Log.w(TAG, "the desktop's compositor process died")
service = null
binding = null
runCatching { context.unbindService(this) }
val pid = guestPid
guestPid = -1
guestGeneration++
if (pid > 1) SessionService.stopTerminalProcess(context, pid)
imeWatcher?.stopWatching()
imeWatcher = null
owner = -1
setState(State.FAILED)
}
}
binding = connection
val bound = runCatching {
context.bindService(Intent(context, Comp::class.java), connection,
Context.BIND_AUTO_CREATE or Context.BIND_IMPORTANT)
}.getOrDefault(false)
if (!bound) {
Log.e(TAG, "could not bind the desktop's compositor")
binding = null
setState(State.FAILED)
}
}
private fun send(target: Messenger, msg: Message) {
runCatching { target.send(msg) }.onFailure { Log.w(TAG, "compositor message ${msg.what} not delivered", it) }
}
private fun runtimeDir(context: Context) = File(context.filesDir, ".wayland-rt-bottom")
private fun startGuest() {
val context = appContext ?: return
val current = launch
if (current == null || !SessionState.running || current.generation != owner) {
setState(State.FAILED)
return
}
// A freeze collects its helpers once, when it begins; a guest started after that would run on unfrozen.
if (SessionState.suspended || SessionState.suspending) {
setState(State.OFF)
return
}
val generation = ++guestGeneration
guestPid = 0
val (width, height) = outputSize
service?.let { send(it, Message.obtain(null, Comp.MSG_NEW_GUEST)) }
Thread({
if (!DesktopCatalog.desktopInstalled(current.root)) {
Log.e(TAG, "the desktop package is not installed")
main.post { if (generation == guestGeneration) { guestPid = -1; setState(State.FAILED) } }
return@Thread
}
val dir = File(current.sessionDir, "bottom-desktop").apply { mkdirs() }
val runtime = runtimeDir(context).apply { mkdirs() }
val imeDir = File(current.sessionRoot, "bottom-desktop-ime")
main.post { if (generation == guestGeneration) watchIme(imeDir) }
val binds = LinuxRuntime.binds(context, current.sessionRoot, runtime, Environment.getExternalStorageDirectory(), current.binds)
val guest = ArrayList(current.guest).apply {
add("BL_WIDTH=$width")
add("BL_HEIGHT=$height")
add("BL_DEBUG_DIR=" + dir.path)
add("BL_TOP_SESSION=" + SessionState.mode)
add("BL_IME_DIR=" + imeDir.path)
add(SCRIPT)
}
val command = LinuxRuntime.commandWithBinds(context, current.root, binds, guest)
val line = command.joinToString(" ") { it.replace("\\", "\\\\").replace(" ", "\\ ") }
val pid = HostProcess.start(line, current.hostEnvironment, current.root, { status ->
main.post {
if (generation != guestGeneration) return@post
Log.i(TAG, "the desktop ended ($status)")
SessionService.terminalProcessExited(context, guestPid)
guestPid = -1
guestGeneration++
setState(if (state == State.RUNNING || status == 0) State.ENDED else State.FAILED)
}
}, null, File(dir, "proot.log"))
main.post {
if (generation != guestGeneration || launch !== current) {
if (pid > 1) SessionService.stopTerminalProcess(context, pid)
if (generation == guestGeneration) guestPid = -1
return@post
}
if (pid <= 1) {
guestPid = -1
Log.e(TAG, "the desktop's guest did not start")
setState(State.FAILED)
return@post
}
if (SessionState.suspended || SessionState.suspending) {
SessionService.stopTerminalProcess(context, pid)
guestPid = -1
guestGeneration++
setState(State.OFF)
return@post
}
guestPid = pid
SessionService.registerTerminalProcess(context, pid)
Log.i(TAG, "desktop guest pid $pid at ${width}x$height")
}
}, "bottom-desktop-start").start()
}
}
@@ -22,6 +22,7 @@ object SessionFiles {
internal val DESKTOP_FILES = arrayOf(
"usr/local/bin/droiddeck-desktop" to "usr/local/bin/droiddeck-desktop",
"usr/local/bin/droiddeck-bottom-desktop" to "usr/local/bin/droiddeck-bottom-desktop",
"usr/local/bin/droiddeck-gpu" to "usr/local/bin/droiddeck-gpu",
"usr/local/bin/droiddeck-desktop-gpu" to "usr/local/bin/droiddeck-desktop-gpu",
"usr/local/bin/droiddeck-desktop-window-rules" to "usr/local/bin/droiddeck-desktop-window-rules",
@@ -100,6 +100,13 @@ object SessionPrefs {
prefs(context).edit().putString("secondScreenPanel", mode).apply()
}
/** The bottom-screen desktop takes touch as a trackpad unless the user switched it to direct touch. */
fun bottomDesktopDirectTouch(context: Context): Boolean = prefs(context).getBoolean("bottomDesktopDirectTouch", false)
fun setBottomDesktopDirectTouch(context: Context, on: Boolean) {
prefs(context).edit().putBoolean("bottomDesktopDirectTouch", on).apply()
}
fun statsStrip(context: Context): String? = prefs(context).getString("statsStrip", null)
fun setStatsStrip(context: Context, id: String) {
@@ -577,6 +577,7 @@ class SessionService : Service() {
add("-c")
add("cd \"\$HOME\" && exec /bin/bash -i")
}
val desktopGuest = ArrayList(guest)
guest.add(LinuxRuntime.SESSION_SCRIPT)
guest.add(SessionState.mode)
if (SessionState.mode == MODE_STEAM) SessionState.steamUrl?.takeIf { it.startsWith("steam://") }?.let {
@@ -643,6 +644,10 @@ class SessionService : Service() {
}
if (gen == sessionGen && SessionState.running) {
SessionTerminal.prepare(terminalCommand, terminalHostEnvironment.map { (key, value) -> "$key=$value" }.toTypedArray(), root, gen)
val (fastPathName, _) = ProotFastPath.hostEnv("")
BottomDesktop.prepare(desktopGuest, binds, sessionRoot,
hostEnv.asArray().filterNot { it.startsWith("$fastPathName=") }.toTypedArray(), root, sessionDir, gen,
) { gen == sessionGen && SessionState.running }
}
if (SessionState.mode == MODE_STEAM || SessionState.mode == MODE_DESKTOP) OfflineMode.apply(this, root)
@@ -704,7 +709,10 @@ class SessionService : Service() {
if (gen == sessionGen && SessionState.running) {
suspendController = SessionSuspendController(
sessionRoot = { sessionPid },
helperRoots = { components.mapNotNull { it.suspendPid().takeIf { pid -> pid > 1 } } },
helperRoots = {
components.mapNotNull { it.suspendPid().takeIf { pid -> pid > 1 } } +
listOfNotNull(BottomDesktop.suspendPid.takeIf { it > 1 })
},
suspendAudio = {
if (!pulse.setSinkSuspended(true)) Log.w(TAG, "could not suspend audio sink")
if (!pulse.setMicSuspended(true)) Log.w(TAG, "could not suspend microphone source")
@@ -1610,6 +1618,7 @@ class SessionService : Service() {
))
if (shouldSuspend) {
freezePending = true
SessionState.suspending = true
freezeCancelled = false
val freezeSteamToken = steamSleepToken
val freezeNativeToken = nativeSleepToken
@@ -1618,6 +1627,7 @@ class SessionService : Service() {
) { success ->
mainHandler.post {
if (gen != sessionGen || !SessionState.running) return@post
SessionState.suspending = false
suspendOperationPending = false
freezePending = false
if (success) {
@@ -1830,6 +1840,7 @@ class SessionService : Service() {
GameThreadHints.stop()
suspendOperationPending = false
freezePending = false
SessionState.suspending = false
launchWatcher?.stopWatching()
launchWatcher = null
syncWatcher?.stopWatching()
@@ -1840,6 +1851,7 @@ class SessionService : Service() {
auxiliaryProcesses.toMap().also { auxiliaryProcesses.clear() }
}
SessionTerminal.clear(sessionGen)
BottomDesktop.stop(this, stoppedGen)
val controller = suspendController
suspendController = null
if (ended != null) {
@@ -54,6 +54,8 @@ object SessionState {
var suspended by mutableStateOf(false)
var resumeRequested by mutableStateOf(false)
var resuming by mutableStateOf(false)
/** A freeze has begun and has not finished; it already chose which processes it stops. */
@Volatile var suspending = false
var resumeFailures by mutableIntStateOf(0)
var resumeFailed by mutableStateOf(false)
var manualPause by mutableStateOf(false)