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

@@ -37,7 +37,7 @@ class App : Application() {
return
}
if (process == "$packageName:vr_recovery" || process == "$packageName:rgb_diagnostic" ||
process == "$packageName:shortcuts_sync") return
process == "$packageName:shortcuts_sync" || process == "$packageName:bottom_desktop") return
CrashHandler.install(this)
com.droiddeck.launcher.core.SteamIntegration.syncShortcutsListing(this)
com.droiddeck.launcher.session.QuestExperimental.initialize(this)
@@ -67,7 +67,6 @@ import com.droiddeck.launcher.gpu.ScreenEffectLooks
import com.droiddeck.launcher.gpu.ScreenEffects
import com.droiddeck.launcher.gpu.Lossless
import com.droiddeck.launcher.gpu.TurnipDriver
import com.droiddeck.launcher.input.EvdevKeys
import com.droiddeck.launcher.input.HomeGuideInput
import com.droiddeck.launcher.input.InputSpace
import com.droiddeck.launcher.input.KeyboardHost
@@ -1198,6 +1197,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
fexPreset = fexPreset,
secondScreenMode = secondScreenMode,
dashboardAvailable = bottomPages().isNotEmpty(),
desktopAvailable = com.droiddeck.launcher.session.BottomDesktop.available(),
secondScreenDisplays = secondScreenDisplays,
selectedSecondScreenDisplay = selectedSecondScreenDisplay,
onHud = { on -> SessionPrefs.setHudEnabled(this@SessionActivity, on); hudOn = on; hud.refresh() },
@@ -2613,27 +2613,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
if (CompositorHost.isStarted && event.keyCode != KeyEvent.KEYCODE_BACK && !fromController) {
val down = event.action == KeyEvent.ACTION_DOWN
if (down && event.repeatCount > 0) return true
if (down || event.action == KeyEvent.ACTION_UP) {
var evdev = EvdevKeys.fromKeyCode(event.keyCode)
val ch = event.unicodeChar
val plain = if (ch > 0) EvdevKeys.unshiftedChar(ch) else 0
if (evdev <= 0 && ch > 0) {
val code = EvdevKeys.fromKeyCode(EvdevKeys.keycodeForChar(if (plain != 0) plain else ch))
if (code > 0) evdev = code
}
val shiftEvdev = if (evdev > 0 && plain != 0 && event.deviceId <= 0) 42 else 0
if (evdev <= 0 && event.scanCode > 0) evdev = event.scanCode
if (evdev > 0) {
if (down) {
if (shiftEvdev != 0) sendGuestKey(shiftEvdev, true)
sendGuestKey(evdev, true)
} else {
sendGuestKey(evdev, false)
if (shiftEvdev != 0) sendGuestKey(shiftEvdev, false)
}
return true
}
}
if (com.droiddeck.launcher.input.GuestKeys.send(event, ::sendGuestKey)) return true
}
return super.dispatchKeyEvent(event)
}
@@ -3083,7 +3063,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
if (secondScreenDisplays.none { it.id == target.displayId }) return
SessionState.secondScreenAutoOpened = true
selectedSecondScreenDisplay = target.displayId
selectSecondScreenMode(SecondScreenMode.opening(SessionPrefs.secondScreenPanel(this), SessionState.deckPad, bottomPages().isNotEmpty()))
selectSecondScreenMode(SecondScreenMode.opening(SessionPrefs.secondScreenPanel(this), SessionState.deckPad, bottomPages().isNotEmpty(),
com.droiddeck.launcher.session.BottomDesktop.available()))
dualControlsProvisional = ::loading.isInitialized && loading.visible
}
@@ -3091,7 +3072,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
if (!dualControlsProvisional || pipUi || !started || (::loading.isInitialized && loading.visible)) return
dualControlsProvisional = false
if (secondScreenMode == SecondScreenMode.NONE) return
val wanted = SecondScreenMode.opening(SessionPrefs.secondScreenPanel(this), SessionState.deckPad, bottomPages().isNotEmpty())
val wanted = SecondScreenMode.opening(SessionPrefs.secondScreenPanel(this), SessionState.deckPad, bottomPages().isNotEmpty(),
com.droiddeck.launcher.session.BottomDesktop.available())
if (wanted != secondScreenMode) selectSecondScreenMode(wanted) else secondScreenPresentation?.retryTerminal()
}
@@ -0,0 +1,31 @@
package com.droiddeck.launcher.input
import android.view.KeyEvent
object GuestKeys {
/** Sends [event] as evdev presses, with Shift around characters typed by a keyboard that has no Shift key; false when it has no evdev code. */
fun send(event: KeyEvent, sendKey: (code: Int, down: Boolean) -> Unit): Boolean {
val down = event.action == KeyEvent.ACTION_DOWN
if (!down && event.action != KeyEvent.ACTION_UP) return false
var evdev = EvdevKeys.fromKeyCode(event.keyCode)
val ch = event.unicodeChar
val plain = if (ch > 0) EvdevKeys.unshiftedChar(ch) else 0
if (evdev <= 0 && ch > 0) {
val code = EvdevKeys.fromKeyCode(EvdevKeys.keycodeForChar(if (plain != 0) plain else ch))
if (code > 0) evdev = code
}
val shiftEvdev = if (evdev > 0 && plain != 0 && event.deviceId <= 0) KEY_LEFTSHIFT else 0
if (evdev <= 0 && event.scanCode > 0) evdev = event.scanCode
if (evdev <= 0) return false
if (down) {
if (shiftEvdev != 0) sendKey(shiftEvdev, true)
sendKey(evdev, true)
} else {
sendKey(evdev, false)
if (shiftEvdev != 0) sendKey(shiftEvdev, false)
}
return true
}
private const val KEY_LEFTSHIFT = 42
}
@@ -10,12 +10,14 @@ enum class SecondScreenMode(val id: String, @StringRes val label: Int) {
KEYBOARD_TRACKPAD("keyboard-trackpad", R.string.second_mode_keyboard_trackpad),
TERMINAL("terminal", R.string.second_mode_terminal),
DECK_CONTROLS("deck-controls", R.string.second_mode_deck_controls),
DASHBOARD("dashboard", R.string.second_mode_dashboard);
DASHBOARD("dashboard", R.string.second_mode_dashboard),
DESKTOP("desktop", R.string.second_mode_desktop);
companion object {
fun opening(remembered: String?, deckPad: Boolean, dashboard: Boolean = false): SecondScreenMode {
fun opening(remembered: String?, deckPad: Boolean, dashboard: Boolean = false, desktop: Boolean = false): SecondScreenMode {
entries.firstOrNull {
it != NONE && it.id == remembered && (it != DECK_CONTROLS || deckPad) && (it != DASHBOARD || dashboard)
it != NONE && it.id == remembered && (it != DECK_CONTROLS || deckPad) && (it != DASHBOARD || dashboard) &&
(it != DESKTOP || desktop)
}?.let { return it }
// No pick yet: the dashboard. A pick that can't open right now keeps the old fallback.
val picked = entries.any { it != NONE && it.id == remembered }
@@ -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)
@@ -0,0 +1,345 @@
package com.droiddeck.launcher.ui
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.os.SystemClock
import android.graphics.Typeface
import android.view.Gravity
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.view.ViewConfiguration
import android.graphics.RectF
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.ComposeView
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.ImageView
import android.widget.TextView
import com.droiddeck.launcher.R
import com.droiddeck.launcher.input.GuestKeys
import com.droiddeck.launcher.input.InputSpace
import com.droiddeck.launcher.input.KeyboardHost
import com.droiddeck.launcher.input.PadBridge
import com.droiddeck.launcher.input.PointerGestures
import com.droiddeck.launcher.input.TouchpadGestures
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.BottomDesktop
@SuppressLint("ViewConstructor")
class BottomDesktopPanel(
context: Context,
onClose: () -> Unit,
onMenu: (() -> Unit)?,
steamButtons: Pair<() -> Unit, () -> Unit>?,
) : LinearLayout(context), SurfaceHolder.Callback {
private lateinit var surfaceView: SurfaceView
private val area = FrameLayout(context).apply { clipChildren = true }
private val state = mutableStateOf(BottomDesktop.state)
private val status = ComposeView(context).apply {
setContent { DroidDeckTheme { BottomDesktopStatus(state.value, ::startAgain) } }
}
private val keyboardHost = KeyboardHost(context) { event -> sendKey(event) }
private var directTouch = SessionPrefs.bottomDesktopDirectTouch(context)
private lateinit var touchButton: ImageView
private lateinit var title: TextView
private val trackpad = TouchpadGestures(ViewConfiguration.get(context).scaledTouchSlop.toFloat(), TrackpadInput()).apply {
desktop = true
}
private var attached = false
/** The surface was given to a compositor process; a new process cannot connect to it again. */
private var surfaceUsed = false
private var fullWidth = 0
private var fullHeight = 0
private var lastTouchY = 0f
private var lastPressMs = 0L
private var scrollRestX = 0f
private var scrollRestY = 0f
private val stateListener: () -> Unit = { showState() }
init {
orientation = VERTICAL
setBackgroundColor(Color.BLACK)
isFocusable = true
isFocusableInTouchMode = true
addView(bar(onClose, onMenu, steamButtons), LayoutParams(-1, -2))
area.addView(keyboardHost)
addSurface()
area.addView(status, FrameLayout.LayoutParams(-1, -1))
addView(area, LayoutParams(-1, 0, 1f))
keyboardHost.onImeChanged = { pan() }
showTouchMode()
}
private fun addSurface() {
surfaceView = SurfaceView(context)
surfaceView.holder.addCallback(this)
@Suppress("ClickableViewAccessibility")
surfaceView.setOnTouchListener { _, event -> onSurfaceTouch(event) }
area.addView(surfaceView, 1, FrameLayout.LayoutParams(fullWidth.takeIf { it > 0 } ?: -1, fullHeight.takeIf { it > 0 } ?: -1))
}
private fun bar(onClose: () -> Unit, onMenu: (() -> Unit)?, steamButtons: Pair<() -> Unit, () -> Unit>?): View {
val bar = LinearLayout(context).apply {
orientation = HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
setPadding(dp(8), dp(6), dp(8), dp(6))
setBackgroundColor(Color.BLACK)
}
fun gap() = View(context).apply { layoutParams = LayoutParams(dp(6), 1) }
bar.addView(DeckControlsPanel.roundButton(context, "×", context.getString(R.string.deck_close), onClose))
title = TextView(context).apply {
textSize = 14f
typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
setTextColor(Color.rgb(0x7A, 0x80, 0x8A))
setPadding(dp(12), 0, dp(6), 0)
isSingleLine = true
ellipsize = android.text.TextUtils.TruncateAt.END
}
bar.addView(title, LayoutParams(0, -2, 1f))
touchButton = DeckControlsPanel.iconButton(context, R.drawable.ic_bottom_mouse, "") { setDirectTouch(!directTouch) }
bar.addView(touchButton)
bar.addView(gap())
bar.addView(DeckControlsPanel.iconButton(context, R.drawable.ic_bottom_keyboard,
context.getString(R.string.second_show_keyboard)) { keyboardHost.toggle() })
onMenu?.let {
bar.addView(gap())
bar.addView(DeckControlsPanel.menuButton(context, it))
}
steamButtons?.let { (steam, qam) ->
bar.addView(gap())
bar.addView(DeckControlsPanel.roundButton(context, "STEAM", context.getString(R.string.deck_open_steam_menu), steam, wide = true))
bar.addView(gap())
bar.addView(DeckControlsPanel.roundButton(context, "•••", context.getString(R.string.drawer_open_qam), qam, wide = true))
}
return bar
}
private fun setDirectTouch(on: Boolean) {
trackpad.cancel()
BottomDesktop.sendTouch(3, -1, 0, 0)
directTouch = on
SessionPrefs.setBottomDesktopDirectTouch(context, on)
showTouchMode()
}
private fun showTouchMode() {
touchButton.setImageResource(if (directTouch) R.drawable.ic_bottom_touch else R.drawable.ic_bottom_mouse)
touchButton.contentDescription = context.getString(
if (directTouch) R.string.bottom_desktop_direct_touch else R.string.bottom_desktop_trackpad)
title.text = context.getString(R.string.bottom_desktop_title,
context.getString(if (directTouch) R.string.bottom_desktop_mode_touch else R.string.bottom_desktop_mode_trackpad))
BottomDesktop.showCursor(!directTouch)
if (!directTouch && surfaceView.width > 0) {
trackpad.bounds = RectF(0f, 0f, surfaceView.width.toFloat(), surfaceView.height.toFloat())
trackpad.place(surfaceView.width / 2f, surfaceView.height / 2f)
movePointer(trackpad.x, trackpad.y)
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
BottomDesktop.addListener(stateListener)
BottomDesktop.setTextInputListener { focused ->
val tapped = SystemClock.uptimeMillis() - lastPressMs < TEXT_FIELD_TAP_MS
val hardKeyboard = resources.configuration.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO
if (focused && tapped && !hardKeyboard && !keyboardHost.shown) keyboardHost.show()
else if (!focused && keyboardHost.shown) keyboardHost.hide()
}
showState()
post { if (!keyboardHost.shown) requestFocus() }
}
override fun onDetachedFromWindow() {
BottomDesktop.removeListener(stateListener)
BottomDesktop.setTextInputListener(null)
keyboardHost.takeIf { it.shown }?.hide()
trackpad.cancel()
status.disposeComposition()
super.onDetachedFromWindow()
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean =
if (BottomDesktop.state == BottomDesktop.State.RUNNING && !PadBridge.isControllerKeyEvent(event) &&
event.keyCode != KeyEvent.KEYCODE_BACK && sendKey(event)) true
else super.dispatchKeyEvent(event)
private fun sendKey(event: KeyEvent): Boolean {
if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount > 0) return true
if (GuestKeys.send(event, BottomDesktop::sendKey)) return true
val ch = event.unicodeChar
if (event.action == KeyEvent.ACTION_DOWN && ch > 0) {
BottomDesktop.sendText(String(Character.toChars(ch)))
return true
}
return false
}
private fun startAgain() {
if (surfaceUsed && !BottomDesktop.connected) {
attached = false
surfaceUsed = false
area.removeView(surfaceView)
addSurface()
return
}
if (attached) attach(surfaceView.holder)
}
private fun showState() {
state.value = BottomDesktop.state
status.visibility = if (state.value == BottomDesktop.State.RUNNING) View.GONE else View.VISIBLE
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
post { holdSurfaceSize() }
}
// The keyboard shrinks the window; the desktop keeps its size and slides up under it instead of being squeezed.
private fun holdSurfaceSize() {
val w = area.width
val h = area.height
if (w <= 0 || h <= 0) return
if (w != fullWidth || h > fullHeight || !keyboardHost.shown) {
fullWidth = w
fullHeight = h
surfaceView.layoutParams = FrameLayout.LayoutParams(w, h)
}
pan()
}
private fun pan() {
val visible = area.height
val hidden = (fullHeight - visible).coerceAtLeast(0)
val anchor = if (directTouch) lastTouchY else trackpad.y
surfaceView.translationY = if (hidden == 0 || !keyboardHost.shown) 0f
else -(anchor - visible * 0.4f).coerceIn(0f, hidden.toFloat())
}
override fun surfaceCreated(holder: SurfaceHolder) = Unit
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
if (!attached) attach(holder)
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
if (holder !== surfaceView.holder) return
attached = false
surfaceUsed = false
BottomDesktop.detach()
}
private fun attach(holder: SurfaceHolder) {
val frame = holder.surfaceFrame
val (width, height) = BottomDesktop.outputSizeFor(fullWidth.takeIf { it > 0 } ?: frame.width(),
fullHeight.takeIf { it > 0 } ?: frame.height())
if (width <= 0 || height <= 0) return
if (frame.width() != width || frame.height() != height) {
holder.setFixedSize(width, height)
return
}
attached = true
surfaceUsed = true
BottomDesktop.attach(context, holder.surface, width, height, display?.refreshRate ?: 60f)
showTouchMode()
}
private fun onSurfaceTouch(event: MotionEvent): Boolean {
val w = surfaceView.width
val h = surfaceView.height
if (w <= 0 || h <= 0 || BottomDesktop.state != BottomDesktop.State.RUNNING) return true
if (event.actionMasked == MotionEvent.ACTION_DOWN || event.actionMasked == MotionEvent.ACTION_UP)
lastPressMs = SystemClock.uptimeMillis()
if (directTouch) return forwardTouch(event, w, h)
val bounds = RectF(0f, 0f, w.toFloat(), h.toFloat())
if (trackpad.bounds != bounds) {
trackpad.bounds = bounds
trackpad.place(trackpad.x, trackpad.y)
}
return trackpad.onTouch(event)
}
private fun forwardTouch(event: MotionEvent, w: Int, h: Int): Boolean {
val action = when (event.actionMasked) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> 0
MotionEvent.ACTION_MOVE -> 1
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> 2
MotionEvent.ACTION_CANCEL -> { BottomDesktop.sendTouch(3, -1, 0, 0); return true }
else -> return true
}
if (event.actionMasked == MotionEvent.ACTION_DOWN) lastTouchY = event.y
if (action == 1) {
for (i in 0 until event.pointerCount) send(1, event.getPointerId(i), event.getX(i), event.getY(i), w, h)
} else {
val i = event.actionIndex
send(action, event.getPointerId(i), event.getX(i), event.getY(i), w, h)
}
return true
}
private fun send(action: Int, id: Int, x: Float, y: Float, w: Int, h: Int) =
BottomDesktop.sendTouch(action, id, InputSpace.x(x, w), InputSpace.y(y, h))
private fun movePointer(x: Float, y: Float) =
BottomDesktop.sendPointer(InputSpace.x(x, surfaceView.width), InputSpace.y(y, surfaceView.height))
private inner class TrackpadInput : PointerGestures.Listener {
override fun onMove(x: Float, y: Float) = movePointer(x, y)
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) {
movePointer(x, y)
BottomDesktop.sendScene(SCENE_BUTTON, button, if (pressed) 1 else 0)
}
override fun onWheel(steps: Int) = BottomDesktop.sendScene(SCENE_WHEEL, steps)
override fun onScroll(dx: Float, dy: Float) {
val (outW, outH) = BottomDesktop.outputSizeFor(surfaceView.width, surfaceView.height)
scrollRestX += dx * outW / surfaceView.width.coerceAtLeast(1) * AXIS_UNITS_PER_PIXEL
scrollRestY += dy * outH / surfaceView.height.coerceAtLeast(1) * AXIS_UNITS_PER_PIXEL
val h = scrollRestX.toInt()
val v = scrollRestY.toInt()
if (h == 0 && v == 0) return
scrollRestX -= h
scrollRestY -= v
BottomDesktop.sendScene(SCENE_AXIS, v, h)
}
override fun onScrollEnd() {
scrollRestX = 0f
scrollRestY = 0f
BottomDesktop.sendScene(SCENE_AXIS, 0, 0)
}
override fun onZoom(steps: Int) {
BottomDesktop.sendKey(KEY_LEFTCTRL, true)
BottomDesktop.sendScene(SCENE_WHEEL, steps)
BottomDesktop.sendKey(KEY_LEFTCTRL, false)
}
override fun onThreeFingerTap(): Boolean {
post { keyboardHost.toggle() }
return true
}
override fun onLongPress() {
performHapticFeedback(android.view.HapticFeedbackConstants.LONG_PRESS)
}
}
private fun dp(value: Int): Int = (value * resources.displayMetrics.density + 0.5f).toInt()
private companion object {
const val SCENE_BUTTON = 3
const val SCENE_WHEEL = 4
const val SCENE_AXIS = 12
const val KEY_LEFTCTRL = 29
const val AXIS_UNITS_PER_PIXEL = 3f
const val TEXT_FIELD_TAP_MS = 1_000L
}
}
@@ -0,0 +1,55 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.droiddeck.launcher.R
import com.droiddeck.launcher.session.BottomDesktop
/** What the bottom screen shows while the desktop is not drawing: the session loading screen's throbber and a line of text. */
@Composable
internal fun BottomDesktopStatus(state: BottomDesktop.State, onStart: () -> Unit) {
val colors = MaterialTheme.colorScheme
val starting = state == BottomDesktop.State.STARTING
Box(Modifier.fillMaxSize().background(colors.background).blockTouches(), contentAlignment = Alignment.Center) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.padding(horizontal = 32.dp),
) {
LogoThrobber(Modifier.width(if (starting) 96.dp else 76.dp), running = starting)
Spacer(Modifier.height(24.dp))
Text(
stringResource(when (state) {
BottomDesktop.State.ENDED -> R.string.bottom_desktop_ended
BottomDesktop.State.FAILED -> R.string.bottom_desktop_failed
BottomDesktop.State.OFF -> R.string.bottom_desktop_off
else -> R.string.bottom_desktop_starting
}),
fontSize = 18.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground,
textAlign = TextAlign.Center, modifier = Modifier.widthIn(max = 420.dp),
)
if (!starting) {
Spacer(Modifier.height(20.dp))
PrimaryButton(stringResource(R.string.bottom_desktop_start), main = true, onClick = onStart)
}
}
}
}
@@ -715,10 +715,13 @@ class DeckControlsPanel(
}
}
internal fun menuButton(context: Context, onClick: () -> Unit) = android.widget.ImageView(context).apply {
setImageResource(R.drawable.ic_stat_session)
internal fun menuButton(context: Context, onClick: () -> Unit) =
iconButton(context, R.drawable.ic_stat_session, context.getString(R.string.touch_droiddeck_menu), onClick)
internal fun iconButton(context: Context, icon: Int, description: String, onClick: () -> Unit) = android.widget.ImageView(context).apply {
setImageResource(icon)
setColorFilter(TEXT)
contentDescription = context.getString(R.string.touch_droiddeck_menu)
contentDescription = description
scaleType = android.widget.ImageView.ScaleType.CENTER_INSIDE
val pad = dp(context, 11)
setPadding(pad, pad, pad, pad)
@@ -34,6 +34,7 @@ import com.droiddeck.launcher.input.PointerGestures
import com.droiddeck.launcher.input.SecondScreenMode
import com.droiddeck.launcher.input.TouchpadGestures
import com.droiddeck.launcher.SessionActivity
import com.droiddeck.launcher.session.BottomDesktop
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.session.SessionTerminal
@@ -199,7 +200,8 @@ class SecondScreenPresentation(
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
val ownKeys = currentMode == SecondScreenMode.TERMINAL || currentMode == SecondScreenMode.DASHBOARD
val ownKeys = currentMode == SecondScreenMode.TERMINAL || currentMode == SecondScreenMode.DASHBOARD ||
currentMode == SecondScreenMode.DESKTOP
val forward = menuShown || !ownKeys || PadBridge.isControllerKeyEvent(event)
if (forward && sessionActivity.dispatchKeyEvent(event)) return true
return super.dispatchKeyEvent(event)
@@ -231,6 +233,7 @@ class SecondScreenPresentation(
val mode = when {
currentMode == SecondScreenMode.DECK_CONTROLS && !SessionState.deckPad -> SecondScreenMode.NONE
currentMode == SecondScreenMode.DASHBOARD && dashboard == null -> SecondScreenMode.NONE
currentMode == SecondScreenMode.DESKTOP && !BottomDesktop.available() -> SecondScreenMode.NONE
else -> currentMode
}
if (mode == SecondScreenMode.NONE) return
@@ -240,6 +243,11 @@ class SecondScreenPresentation(
SecondScreenMode.TERMINAL -> renderTerminal()
SecondScreenMode.DECK_CONTROLS -> root.addView(DeckControlsPanel(context, onClose, onSteamMenu, onQam, pad, onMenu = onMenu), weightParams())
SecondScreenMode.DASHBOARD -> renderDashboard()
SecondScreenMode.DESKTOP -> {
ownComposeViews()
root.addView(BottomDesktopPanel(context, onClose, onMenu,
(onSteamMenu to onQam).takeIf { SessionState.mode == SessionService.MODE_STEAM }), weightParams())
}
SecondScreenMode.NONE -> Unit
}
}
@@ -32,6 +32,7 @@ import androidx.compose.foundation.layout.heightIn
import androidx.compose.material.icons.outlined.Close
import androidx.compose.material.icons.outlined.Terminal
import androidx.compose.material.icons.outlined.Dashboard
import androidx.compose.material.icons.outlined.DesktopWindows
import androidx.compose.material.icons.outlined.Keyboard
import androidx.compose.material.icons.outlined.AutoFixHigh
import androidx.compose.material.icons.outlined.ExpandMore
@@ -244,6 +245,7 @@ class DrawerActions(
val textureLodBias: String? = TextureFiltering.LOD_BIAS_OFF,
val secondScreenMode: SecondScreenMode,
val dashboardAvailable: Boolean = false,
val desktopAvailable: Boolean = false,
val secondScreenDisplays: List<SecondScreenDisplay>,
val selectedSecondScreenDisplay: Int,
val onHud: (Boolean) -> Unit,
@@ -901,6 +903,7 @@ private fun BottomScreenModes(a: DrawerActions, modifier: Modifier, onPick: () -
add(SecondScreenMode.KEYBOARD_TRACKPAD to Icons.Outlined.Keyboard)
add(SecondScreenMode.TERMINAL to Icons.Outlined.Terminal)
if (a.dashboardAvailable) add(SecondScreenMode.DASHBOARD to Icons.Outlined.Dashboard)
if (a.desktopAvailable) add(SecondScreenMode.DESKTOP to Icons.Outlined.DesktopWindows)
}
Row(
verticalAlignment = Alignment.CenterVertically,
@@ -1199,7 +1202,8 @@ private fun SecondScreenGroup(host: MenuHost, a: DrawerActions, track: (String)
val pal = LocalPalette.current
val modes = listOf(SecondScreenMode.NONE, SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL) +
(if (com.droiddeck.launcher.session.SessionState.deckPad) listOf(SecondScreenMode.DECK_CONTROLS) else emptyList()) +
(if (a.dashboardAvailable) listOf(SecondScreenMode.DASHBOARD) else emptyList())
(if (a.dashboardAvailable) listOf(SecondScreenMode.DASHBOARD) else emptyList()) +
(if (a.desktopAvailable) listOf(SecondScreenMode.DESKTOP) else emptyList())
SettingsGroup(stringResource(R.string.drawer_second_screen_mode)) {
Column(Modifier.fillMaxWidth().selectableGroup()) {
modes.forEach { mode ->
@@ -0,0 +1,117 @@
package com.droiddeck.launcher.wayland
import android.app.Service
import android.content.Intent
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.FileUtils
import java.io.File
/**
* The bottom-screen desktop's compositor. The compositor's native state is one per process, so the
* desktop gets a process of its own while the session's compositor keeps the top screen.
*/
class BottomDesktopCompositorService : Service() {
private var client: Messenger? = null
/** Echoed in every reply, so the session process can tell replies to an earlier attach apart. */
@Volatile private var attachTag = 0
private val handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
when (msg.what) {
MSG_ATTACH -> attach(msg)
MSG_DETACH -> CompositorHost.detach(null)
MSG_TOUCH -> WaylandCompositor.nativeSendTouch(msg.arg1 and 0xff, msg.arg1 shr 8,
msg.arg2 ushr 16, msg.arg2 and 0xffff)
MSG_KEY -> WaylandCompositor.nativeSendKey(msg.arg1, msg.arg2)
MSG_POINTER -> WaylandCompositor.nativeSendPointer(1, msg.arg1, msg.arg2)
MSG_SCENE -> WaylandCompositor.nativeSendSceneInput(msg.arg1, msg.arg2, msg.peekData()?.getInt(KEY_B) ?: 0)
MSG_CURSOR -> if (CompositorHost.isStarted) WaylandCompositor.nativeSetCursorComposite(msg.arg1 != 0)
MSG_NEW_GUEST -> WaylandCompositor.nativeResetFirstFrame()
MSG_TEXT -> msg.data?.getString(KEY_TEXT)?.let { WaylandCompositor.textInputCommit(it) }
MSG_STOP -> {
Log.i(TAG, "bottom-screen desktop closed; ending its compositor process")
// A producer that dies still connected keeps the panel's surface from taking another one.
CompositorHost.detach(null)
postDelayed({ android.os.Process.killProcess(android.os.Process.myPid()) }, 300)
}
else -> super.handleMessage(msg)
}
}
}
private val messenger = Messenger(handler)
override fun onBind(intent: Intent?): IBinder = messenger.binder
override fun onUnbind(intent: Intent?): Boolean {
// The session process went away without saying so; nothing is left to draw for.
android.os.Process.killProcess(android.os.Process.myPid())
return false
}
private fun attach(msg: Message) {
val surface = msg.obj as? Surface ?: return
val data = msg.data ?: return
client = msg.replyTo
attachTag = msg.arg1
val runtimeDir = File(data.getString(KEY_RUNTIME_DIR) ?: return).apply { mkdirs() }
val width = data.getInt(KEY_WIDTH)
val height = data.getInt(KEY_HEIGHT)
if (!CompositorHost.isStarted) {
FileUtils.copyAsset(this, "wayland/keymap.xkb", File(runtimeDir, "keymap.xkb"))
data.getString(KEY_LOG)?.let { path ->
WaylandCompositor.setSessionLogFile(File(path))
runCatching { android.system.Os.setenv("BL_WAYLAND_LOG", path, true) }
.onFailure { Log.w(TAG, "could not point the compositor's log at $path", it) }
}
WaylandCompositor.setFirstFrameListener { reply(MSG_FIRST_FRAME) }
WaylandCompositor.nativeSetScaleMode(SCALE_FIT, ALIGN_CENTER)
}
val turnip = com.droiddeck.launcher.gpu.TurnipDriver(this)
val driverId = if (CompositorHost.isStarted) null else turnip.install()
val started = CompositorHost.startOrAttach(
this, surface, runtimeDir.path, driverId?.let { turnip.driverPath(it) }, driverId?.let { turnip.libraryName(it) },
applicationInfo.nativeLibraryDir,
width, height, data.getFloat(KEY_REFRESH, 60f), 0,
)
Log.i(TAG, (if (started) "compositor started" else "compositor attached") + " at ${width}x$height in ${runtimeDir.path}")
reply(MSG_ATTACHED)
}
private fun reply(what: Int) {
val target = client ?: return
runCatching { target.send(Message.obtain(null, what, attachTag, 0)) }
.onFailure { Log.w(TAG, "the session process did not take message $what", it) }
}
companion object {
private const val TAG = "BottomDesktopComp"
const val MSG_ATTACH = 1
const val MSG_DETACH = 2
const val MSG_TOUCH = 3
const val MSG_KEY = 4
const val MSG_TEXT = 5
const val MSG_STOP = 6
const val MSG_NEW_GUEST = 7
const val MSG_POINTER = 8
const val MSG_SCENE = 9
const val MSG_CURSOR = 10
const val MSG_ATTACHED = 101
const val MSG_FIRST_FRAME = 102
const val KEY_RUNTIME_DIR = "runtimeDir"
const val KEY_WIDTH = "width"
const val KEY_HEIGHT = "height"
const val KEY_REFRESH = "refresh"
const val KEY_LOG = "log"
const val KEY_TEXT = "text"
const val KEY_B = "b"
private const val SCALE_FIT = 1
private const val ALIGN_CENTER = 0
}
}