Files
chandra/compositor/host/kiosk.mbt
T
Claude d521d0d840
CI / test (push) Successful in 4m10s
Kiosk mode: --kiosk CMD runs one fullscreen app, locked down, restarted when it exits
No panel, no compositor shortcuts or desktop menus, the focused window
fullscreen (dialogs float), CMD restarted with backoff. Ctrl+Alt+Shift+Escape
or chandra-ctl kiosk lock|unlock|status for admin work. --app-id sets the X11
WM_CLASS / Wayland app id of Chandra's own window. scripts/gamerig-kiosk.sh
runs Chandra as a gamerig rig's library window on its NVIDIA Xorg.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgvjzw8hHirxRsXSsE
2026-11-26 02:22:03 +00:00

252 lines
8.3 KiB
MoonBit

///| Kiosk mode (`--kiosk CMD`): one app, fullscreen, nothing else to click.
///
/// While the kiosk is locked: no panel (and no room kept for it), no compositor shortcuts (the
/// launcher, Super+D, screenshots, recordings, Super+1..9), no desktop or panel right-click
/// menus, and every top-level window without a parent is made fullscreen (dialogs, which name
/// a parent, stay centred floating windows). Alt+Tab still switches between the app's windows.
///
/// CMD (may be empty) is started with WAYLAND_DISPLAY set and started again whenever no
/// window is left: right away once its window has gone, or after `kiosk_start_grace_ms` if it
/// never showed one. An app that keeps dying soon after it starts is started with a growing
/// delay (1 s, 2 s, 4 s ... up to 30 s).
///
/// The admin escape Ctrl+Alt+Shift+Escape unlocks the kiosk (the panel, shortcuts and menus come
/// back, windows can leave fullscreen) and locks it again. `chandra-ctl kiosk lock|unlock|status`
/// does the same from a script.
///| How long a started app may take to show its first window before it is started again.
pub let kiosk_start_grace_ms : UInt64 = 20000UL
///| An app that dies within this long after it started counts as a failed start (backoff).
pub let kiosk_quick_exit_ms : UInt64 = 10000UL
///| The kiosk's state. `cmd` is what it runs; the rest is the respawn bookkeeping.
pub(all) struct Kiosk {
cmd : String
mut locked : Bool
/// When the app was last started (0: never).
mut started_at : UInt64
/// A window was seen since the last start.
mut seen_window : Bool
/// When the last window went away (0: a window is up, or none was seen yet).
mut gone_at : UInt64
/// Failed starts in a row (for the backoff).
mut fails : Int
/// Starts so far.
mut starts : Int
}
///|
pub fn Kiosk::new(cmd : String) -> Kiosk {
{ cmd, locked: true, started_at: 0UL, seen_window: false, gone_at: 0UL, fails: 0, starts: 0 }
}
///| Delay before starting again after `fails` failed starts in a row.
pub fn kiosk_backoff_ms(fails : Int) -> UInt64 {
if fails <= 0 {
return 0UL
}
let mut d = 1000UL
for i = 1; i < fails && d < 30000UL; i = i + 1 {
d = d * 2UL
}
if d > 30000UL {
30000UL
} else {
d
}
}
///| Whether the app should be started now, given whether any window is up. Also keeps the
/// bookkeeping: call it on every step with the current monotonic time.
pub fn Kiosk::should_start(self : Kiosk, now : UInt64, has_window : Bool) -> Bool {
if self.cmd.length() == 0 {
return false
}
if self.started_at == 0UL {
return true // first start
}
if has_window {
if !self.seen_window {
self.seen_window = true
}
self.gone_at = 0UL
return false
}
if self.seen_window {
// Its window went away (the app quit or crashed).
if self.gone_at == 0UL {
self.gone_at = now
if now - self.started_at < kiosk_quick_exit_ms {
self.fails = self.fails + 1
} else {
self.fails = 0
}
}
return now - self.gone_at >= kiosk_backoff_ms(self.fails)
}
// Started, but no window yet: give it time, then try again.
if now - self.started_at >= kiosk_start_grace_ms + kiosk_backoff_ms(self.fails) {
self.fails = self.fails + 1
return true
}
false
}
///| Record a start at `now`.
pub fn Kiosk::started(self : Kiosk, now : UInt64) -> Unit {
self.started_at = now
self.seen_window = false
self.gone_at = 0UL
self.starts = self.starts + 1
}
///| Whether a start is waiting for its time (the idle loop must wake for it).
pub fn Kiosk::waiting(self : Kiosk, has_window : Bool) -> Bool {
self.cmd.length() > 0 && !has_window
}
///| Turn kiosk mode on: lock the shell down and start `cmd` (if any) on the next step.
pub fn ChandraCompositor::start_kiosk(self : ChandraCompositor, cmd : String) -> Unit {
self.kiosk = Some(Kiosk::new(cmd))
self.apply_kiosk_lock()
}
///| Whether the kiosk is on and locked.
pub fn ChandraCompositor::kiosk_locked(self : ChandraCompositor) -> Bool {
match self.kiosk {
Some(k) => k.locked
None => false
}
}
///| Lock (true) or unlock (false) the kiosk. False when there is no kiosk.
pub fn ChandraCompositor::set_kiosk_locked(self : ChandraCompositor, locked : Bool) -> Bool {
match self.kiosk {
Some(k) => {
if k.locked != locked {
k.locked = locked
@sys.log("kiosk: " + (if locked { "locked" } else { "unlocked (admin)" }))
self.apply_kiosk_lock()
}
true
}
None => false
}
}
///| Make the shell and the layout follow the lock: panel shown or not, work area with or
/// without the panel's and title bars' room.
fn ChandraCompositor::apply_kiosk_lock(self : ChandraCompositor) -> Unit {
let locked = self.kiosk_locked()
self.shell.hide_panel = locked
if locked {
self.shell.close_launcher()
if self.switcher_on {
self.switcher_on = false
}
}
self.work_area = work_area_for(self.output_w, self.output_h, self.theme, kiosk=locked)
if !locked {
// Give the windows the kiosk made fullscreen back to the normal layout.
let ids : Array[UInt64] = []
for wid, win in self.windows {
if win.fullscreen {
ids.push(wid)
}
}
for wid in ids {
self.apply_actions(self.wm.handle_request(wid, @wm.WindowRequest::Fullscreen(false), self.work_area))
}
}
self.apply_actions(self.wm.fit_work_area(self.work_area))
self.last_frame = None
self.last_sig = None
self.frame_changed = true
self.damage.mark_full()
}
///| The admin escape: Ctrl+Alt+Shift+Escape toggles the lock. True when the key was taken.
fn ChandraCompositor::kiosk_key(self : ChandraCompositor, code : UInt, pressed : Bool) -> Bool {
if self.kiosk is None {
return false
}
let mods = self.held_mods()
if code == 1U && (mods & (@shell.mod_ctrl | @shell.mod_alt | @shell.mod_shift)) ==
(@shell.mod_ctrl | @shell.mod_alt | @shell.mod_shift) {
if pressed {
ignore(self.set_kiosk_locked(!self.kiosk_locked()))
}
return true
}
false
}
///| Once per step: start (or restart) the kiosk app, and keep its windows fullscreen.
fn ChandraCompositor::kiosk_tick(self : ChandraCompositor) -> Unit {
match self.kiosk {
None => ()
Some(k) => {
let now = @sys.monotonic_ms()
if k.should_start(now, self.windows.size() > 0) {
k.started(now)
@sys.log("kiosk: starting '" + k.cmd + "' (start " + k.starts.to_string() + ")")
if !@sys.spawn(k.cmd, self.shell.display) {
@sys.log("kiosk: could not start '" + k.cmd + "'")
}
}
if k.locked {
self.kiosk_fullscreen_all()
}
}
}
}
///| A locked kiosk's rule: the focused top-level window fills the screen (fullscreen, no frame);
/// the others wait behind it, and a dialog (a window with a parent) floats over it. Switching
/// windows (Alt+Tab, or the app activating another) moves the fullscreen to the new one.
fn ChandraCompositor::kiosk_fullscreen_all(self : ChandraCompositor) -> Unit {
let mut target : UInt64? = None
for wid, win in self.windows {
if win.focused && !self.server.toplevel_has_parent(wid) {
target = Some(wid)
}
}
if target is None {
// Nothing focused (a dialog has the keyboard, say): keep the one already fullscreen, else
// take any window without a parent.
for wid, win in self.windows {
if target is None && !self.server.toplevel_has_parent(wid) {
target = Some(wid)
}
if win.fullscreen && !self.server.toplevel_has_parent(wid) {
target = Some(wid)
}
}
}
let changed : Array[(UInt64, Bool)] = []
for wid, win in self.windows {
let want = target == Some(wid)
if win.fullscreen != want {
changed.push((wid, want))
}
}
// Leave fullscreen first, so the workspace's one fullscreen slot is free for the new window.
changed.sort_by((a, b) => if a.1 == b.1 { 0 } else if a.1 { 1 } else { -1 })
for c in changed {
self.apply_actions(self.wm.handle_request(c.0, @wm.WindowRequest::Fullscreen(c.1), self.work_area))
match self.windows.get(c.0) {
Some(win) => self.configure_window(win)
None => ()
}
}
}
///| Whether the kiosk has a start to make later (keeps an idle main loop waking).
pub fn ChandraCompositor::kiosk_waiting(self : ChandraCompositor) -> Bool {
match self.kiosk {
Some(k) => k.waiting(self.windows.size() > 0)
None => false
}
}