CI / test (push) Failing after 19s
Measured on the MoonBit host, 1080p, cloud container (two runs each, before -> after): - full-screen (tiled) client redrawing at 60 fps: 43/39% -> 31/35% - 800x600 floating client at 60 fps: 15.5/13.2% -> 10.4/11.9% - client inside the nested window: nested 18% -> 12%, outer 17% -> 11.6% - Alt+Tab open over two animating clients: 36.6% -> 25.4% - idle (headless / nested): 0.2-0.6% -> 0.1-0.3% wasmhost raster: - capture keeps client pools mapped (keyed by dev/inode/size, dropped after 1 s unused) and reuses released surface buffers, swizzling a word at a time: 3.2 -> 2.1 ms per 1080p commit. - a window whose only change is a new buffer damages just its client area, and the topmost opaque buffer that covers the damage is drawn first (no backdrop copy, desktop card, lower windows or its own decoration under it): submit 3.2 -> 0.8 ms per full-screen frame. - the framebuffer tracks what changed since each output took it. The nested window's frames are a ring of three mapped memfds, each updated only where the screen changed, instead of a new memfd with a full 8 MB conversion and write every frame. - KMS: present copies only the rows the back buffer is missing (this frame's change and the last one's), and kms_stub.c is compiled with -O2 (its stub-cc-flags dropped moon's default, so the per-frame copy ran unoptimised). - images re-filled under the same handle (Alt+Tab previews) damage where the panel draws them: before, a live preview only repainted where it overlapped the window that redrew. compositor: - Alt+Tab previews of a window that keeps redrawing are remade at most every 100 ms (scaling a 1080p buffer takes ~2 ms); the loop keeps its pace while one is pending. - set-tokens is sent to the host only when the theme changes (it was 9 calls a frame). - /proc is sampled only when a module imports `system`; a machine without a battery looks for one every 30 s instead of probing eight sysfs paths every second. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hkh3qygSXtftCTVhSZ
378 lines
14 KiB
MoonBit
378 lines
14 KiB
MoonBit
///| Alt+Tab window switcher (the combo is "Switch windows" in the shortcut table).
|
|
///
|
|
/// Hold Alt and press Tab: a list of the open windows (minimized ones too), most recently used
|
|
/// first, with the previous window selected. Each further Tab moves down the list (Shift+Tab up,
|
|
/// the arrow keys too); releasing Alt switches to the selected window (restoring it if it was
|
|
/// minimized) and Escape cancels. The Tab presses are not forwarded to clients, and the client
|
|
/// that had focus is told Alt was let go so it doesn't think it is still held.
|
|
|
|
///| evdev codes.
|
|
let key_esc : UInt = 1
|
|
|
|
///|
|
|
let key_lalt : UInt = 56
|
|
|
|
///|
|
|
let key_ralt : UInt = 100
|
|
|
|
///|
|
|
let key_up : UInt = 103
|
|
|
|
///|
|
|
let key_down : UInt = 108
|
|
|
|
///|
|
|
let key_left : UInt = 105
|
|
|
|
///|
|
|
let key_right : UInt = 106
|
|
|
|
///| Windows of the active workspace, most recently used first (minimized ones included).
|
|
fn ChandraCompositor::switcher_candidates(self : ChandraCompositor) -> Array[UInt64] {
|
|
let out : Array[UInt64] = []
|
|
for ws in self.wm.workspaces {
|
|
if ws.id == self.wm.active_workspace {
|
|
for id in ws.mru {
|
|
if self.windows.contains(id) && !out.contains(id) {
|
|
out.push(id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Anything the MRU list doesn't know about yet goes last.
|
|
for id, _ in self.windows {
|
|
if !out.contains(id) {
|
|
out.push(id)
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
///| True while the switcher is on screen.
|
|
pub fn ChandraCompositor::switcher_open(self : ChandraCompositor) -> Bool {
|
|
self.switcher_on
|
|
}
|
|
|
|
///| Close the switcher; with `commit`, switch to the selected window.
|
|
fn ChandraCompositor::switcher_close(self : ChandraCompositor, commit : Bool) -> Unit {
|
|
if !self.switcher_on {
|
|
return
|
|
}
|
|
self.switcher_on = false
|
|
if commit && self.switcher_sel >= 0 && self.switcher_sel < self.switcher.length() {
|
|
self.activate_task(self.switcher[self.switcher_sel])
|
|
}
|
|
self.switcher.clear()
|
|
self.damage.mark_full()
|
|
}
|
|
|
|
///| Handle a key for the switcher. Returns true when the key is consumed (not for clients).
|
|
/// Its combo comes from the shortcut table ("switch windows", Alt+Tab by default): holding the
|
|
/// combo's modifiers, its key opens the switcher and steps down (Shift: up), and letting go of one
|
|
/// of those modifiers switches.
|
|
fn ChandraCompositor::switcher_key(self : ChandraCompositor, code : UInt, pressed : Bool, time_ms : UInt) -> Bool {
|
|
let combo = self.shell.shortcuts.combo_for(SwitchWindows)
|
|
let hold = match combo {
|
|
Some(c) => c.mods & @shell.combo_mods
|
|
None => 0U
|
|
}
|
|
if @shell.is_modifier_key(code) {
|
|
if !pressed && self.switcher_on && (@shell.modifier_of_key(code) & hold) != 0U {
|
|
self.switcher_close(true)
|
|
}
|
|
return false // clients still see the modifiers themselves
|
|
}
|
|
let key = match combo {
|
|
Some(c) => c.key
|
|
None => 0U
|
|
}
|
|
if !pressed {
|
|
// Releases of keys we swallowed must not reach a client either.
|
|
return self.switcher_on && (code == key || code == key_esc || code == key_up || code == key_down || code == key_left || code == key_right)
|
|
}
|
|
// The combo's modifiers held (Shift may be added, to go backwards).
|
|
let held = self.held_mods()
|
|
let shift = (held & @shell.mod_shift) != 0U && (hold & @shell.mod_shift) == 0U
|
|
let combo_held = hold != 0U && (held & (@shell.combo_mods ^ @shell.mod_shift)) == (hold & (@shell.combo_mods ^ @shell.mod_shift)) &&
|
|
((hold & @shell.mod_shift) == 0U || (held & @shell.mod_shift) != 0U)
|
|
if code == key && combo_held && !self.shell.inspecting() {
|
|
if !self.switcher_on {
|
|
let c = self.switcher_candidates()
|
|
if c.length() < 2 {
|
|
return true // nothing to switch to; still not a client's key
|
|
}
|
|
// Let the client that had focus see the held modifiers released: it won't get the real
|
|
// releases.
|
|
match self.kbd_win {
|
|
Some(w) => {
|
|
for m in [@shell.mod_alt, @shell.mod_ctrl, @shell.mod_logo, @shell.mod_shift] {
|
|
if (hold & m) != 0U {
|
|
self.server.keyboard_key(w, time_ms, @shell.key_of_modifier(m), false)
|
|
}
|
|
}
|
|
self.server.keyboard_modifiers(w, 0U, 0U, 0U, 0U)
|
|
}
|
|
None => ()
|
|
}
|
|
self.switcher = c
|
|
self.switcher_on = true
|
|
self.switcher_sel = if shift { c.length() - 1 } else { 1 }
|
|
} else {
|
|
let n = self.switcher.length()
|
|
self.switcher_sel = if shift { (self.switcher_sel + n - 1) % n } else { (self.switcher_sel + 1) % n }
|
|
}
|
|
self.damage.mark_full()
|
|
return true
|
|
}
|
|
if self.switcher_on {
|
|
let n = self.switcher.length()
|
|
if code == key_esc {
|
|
self.switcher_close(false)
|
|
} else if code == key_down || code == key_right {
|
|
self.switcher_sel = (self.switcher_sel + 1) % n
|
|
self.damage.mark_full()
|
|
} else if code == key_up || code == key_left {
|
|
self.switcher_sel = (self.switcher_sel + n - 1) % n
|
|
self.damage.mark_full()
|
|
}
|
|
return true // while the switcher is up, keys belong to it
|
|
}
|
|
false
|
|
}
|
|
|
|
///| A window's preview in the switcher: an image handle (renderer or host image) of its last
|
|
/// buffer scaled to `w`x`h`, made again only when the window committed a new buffer (`stale`) or
|
|
/// the size it is shown at changed, and only while the switcher is up.
|
|
pub(all) struct SwitcherThumb {
|
|
mut handle : UInt
|
|
mut w : Int
|
|
mut h : Int
|
|
mut stale : Bool
|
|
/// When it was last made (monotonic ms).
|
|
mut made_ms : UInt64
|
|
}
|
|
|
|
///| How often a preview of a window that keeps redrawing (a video) is made again: scaling a
|
|
/// full-screen buffer down takes a couple of ms, too much for every frame of every window.
|
|
const THUMB_REFRESH_MS : UInt64 = 100UL
|
|
|
|
///| True while the switcher is up and a preview waits for its next refresh (the main loop
|
|
/// keeps its frame pace for it).
|
|
fn ChandraCompositor::thumbs_pending(self : ChandraCompositor) -> Bool {
|
|
self.switcher_on && self.thumbs.values().any(t => t.stale)
|
|
}
|
|
|
|
///| The window committed a new buffer: its preview is made again the next time it is shown.
|
|
fn ChandraCompositor::thumb_stale(self : ChandraCompositor, id : UInt64) -> Unit {
|
|
match self.thumbs.get(id) {
|
|
Some(t) => t.stale = true
|
|
None => ()
|
|
}
|
|
}
|
|
|
|
///| The window is gone: keep its preview's image handle for the next window's.
|
|
fn ChandraCompositor::thumb_forget(self : ChandraCompositor, id : UInt64) -> Unit {
|
|
match self.thumbs.get(id) {
|
|
Some(t) => {
|
|
if t.handle != 0U {
|
|
self.thumb_free.push(t.handle)
|
|
}
|
|
ignore(self.thumbs.remove(id))
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
|
|
///| `bw`x`bh` fitted inside `box_w`x`box_h` with its aspect ratio kept, never enlarged.
|
|
pub fn thumb_fit(bw : Int, bh : Int, box_w : Int, box_h : Int) -> (Int, Int) {
|
|
if bw <= 0 || bh <= 0 || box_w <= 0 || box_h <= 0 {
|
|
return (0, 0)
|
|
}
|
|
if bw <= box_w && bh <= box_h {
|
|
return (bw, bh)
|
|
}
|
|
if bw * box_h >= bh * box_w {
|
|
let h = bh * box_w / bw
|
|
(box_w, if h < 1 { 1 } else { h })
|
|
} else {
|
|
let w = bw * box_h / bh
|
|
(if w < 1 { 1 } else { w }, box_h)
|
|
}
|
|
}
|
|
|
|
///| The preview of window `id` fitted in `box_w`x`box_h`: (image handle, w, h), handle 0 when the
|
|
/// window has no buffer yet. A minimized window keeps the last buffer it showed.
|
|
fn ChandraCompositor::switcher_thumb(self : ChandraCompositor, id : UInt64, box_w : Int, box_h : Int) -> (UInt, Int, Int) {
|
|
let snap = match self.windows.get(id) {
|
|
Some(win) => win.surface
|
|
None => None
|
|
}
|
|
let old = self.thumbs.get(id)
|
|
match snap {
|
|
None =>
|
|
match old {
|
|
Some(t) => (t.handle, t.w, t.h)
|
|
None => (0U, 0, 0)
|
|
}
|
|
Some(buf) => {
|
|
let (tw, th) = thumb_fit(buf.width, buf.height, box_w, box_h)
|
|
let now = @sys.monotonic_ms()
|
|
match old {
|
|
Some(t) if t.w == tw && t.h == th && t.handle != 0U &&
|
|
(!t.stale || now - t.made_ms < THUMB_REFRESH_MS) => return (t.handle, tw, th)
|
|
_ => ()
|
|
}
|
|
let reuse = match old {
|
|
Some(t) => t.handle
|
|
None => self.thumb_free.pop().unwrap_or(0U)
|
|
}
|
|
let h = if tw <= 0 {
|
|
0U
|
|
} else if buf.host_id != 0U {
|
|
@sys.surface_thumbnail(buf.host_id, tw, th, reuse)
|
|
} else {
|
|
match @renderer.shm_thumbnail(buf, tw, th) {
|
|
Some(px) => @renderer.set_image(reuse, px)
|
|
None => 0U
|
|
}
|
|
}
|
|
if h == 0U {
|
|
// Couldn't make one: show the previous preview if there is one, and try again next time.
|
|
match old {
|
|
Some(t) => (t.handle, t.w, t.h)
|
|
None => {
|
|
if reuse != 0U {
|
|
self.thumb_free.push(reuse)
|
|
}
|
|
(0U, 0, 0)
|
|
}
|
|
}
|
|
} else {
|
|
match old {
|
|
Some(t) => {
|
|
t.handle = h
|
|
t.w = tw
|
|
t.h = th
|
|
t.stale = false
|
|
t.made_ms = now
|
|
// New pixels under the same handle: the frame must be drawn even when nothing else
|
|
// changed (the host finds where the preview is).
|
|
self.damage.mark_rect(@scene.Rect::new(0, 0, tw, th))
|
|
}
|
|
None => self.thumbs.set(id, { handle: h, w: tw, h: th, stale: false, made_ms: now })
|
|
}
|
|
(h, tw, th)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///| Where the switcher's cards go: (columns, preview box width, preview box height). Cards wrap
|
|
/// into rows, and shrink when the screen is too small for them.
|
|
fn switcher_grid(n : Int, out_w : Int, out_h : Int) -> (Int, Int, Int) {
|
|
let mut bw = 220
|
|
let mut bh = 138
|
|
while true {
|
|
let card_w = bw + 16
|
|
let card_h = bh + 46
|
|
let fit = (out_w - 60 + 8) / (card_w + 8)
|
|
let cols = if fit < 1 { 1 } else if fit > n { n } else { fit }
|
|
let rows = (n + cols - 1) / cols
|
|
if (rows * (card_h + 8) + 40 <= out_h && cols * (card_w + 8) + 52 <= out_w) || bw <= 64 {
|
|
return (cols, bw, bh)
|
|
}
|
|
bw = bw * 4 / 5
|
|
bh = bh * 4 / 5
|
|
}
|
|
(1, bw, bh)
|
|
}
|
|
|
|
///| The switcher's draw ops: a centred grid of cards, each a preview of the window (its last
|
|
/// frame, also for minimized windows) over its app icon and title; the selected card lit.
|
|
/// Empty when closed.
|
|
fn ChandraCompositor::switcher_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] {
|
|
let ops : Array[@scene.DrawOp] = []
|
|
if !self.switcher_on {
|
|
return ops
|
|
}
|
|
let p = self.theme.palette
|
|
let n = self.switcher.length()
|
|
let (cols, bw, bh) = switcher_grid(n, self.output_w, self.output_h)
|
|
let rows = (n + cols - 1) / cols
|
|
let cp = 8 // card padding
|
|
let gap = 8
|
|
let pad = 12
|
|
let label_h = 30
|
|
let card_w = bw + cp * 2
|
|
let card_h = cp + bh + label_h + cp
|
|
let w = cols * card_w + (cols - 1) * gap + pad * 2
|
|
let h = rows * card_h + (rows - 1) * gap + pad * 2
|
|
let x = (self.output_w - w) / 2
|
|
let y = (self.output_h - h) / 2
|
|
let r = self.theme.radius + 4
|
|
let fs = self.theme.font_size
|
|
// Dim everything behind it a little, then the panel.
|
|
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, self.output_w, self.output_h), @scene.Rgba::new(0, 0, 0, 90), 0))
|
|
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x, y, w, h), p.surface_popup, r))
|
|
ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(x, y, w, h), p.border, 1, r))
|
|
for i = 0; i < n; i = i + 1 {
|
|
let id = self.switcher[i]
|
|
let cx = x + pad + i % cols * (card_w + gap)
|
|
let cy = y + pad + i / cols * (card_h + gap)
|
|
let sel = i == self.switcher_sel
|
|
let (title, app, minimized) = match self.windows.get(id) {
|
|
Some(win) => {
|
|
let m = match self.wm.windows.get(id) {
|
|
Some(rec) => rec.minimized
|
|
None => false
|
|
}
|
|
(if win.title.length() > 0 { win.title } else { win.app_id }, win.app_id, m)
|
|
}
|
|
None => ("", "", false)
|
|
}
|
|
if sel {
|
|
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(cx, cy, card_w, card_h), p.accent, self.theme.radius + 2))
|
|
}
|
|
let icon = self.shell.window_icon(app)
|
|
// The preview, centred in its box; without a buffer yet, the app icon on a plain tile.
|
|
let (thumb, tw, th) = self.switcher_thumb(id, bw, bh)
|
|
let bx = cx + cp
|
|
let by = cy + cp
|
|
if thumb != 0U {
|
|
let tr = @scene.Rect::new(bx + (bw - tw) / 2, by + (bh - th) / 2, tw, th)
|
|
ops.push(@scene.DrawOp::Image(tr, thumb))
|
|
ops.push(@scene.DrawOp::StrokeRect(tr, p.border, 1, 0))
|
|
if minimized {
|
|
ops.push(@scene.DrawOp::FillRect(tr, @scene.Rgba::new(0, 0, 0, 120), 0))
|
|
}
|
|
} else {
|
|
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(bx, by, bw, bh), p.surface, self.theme.radius))
|
|
ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(bx, by, bw, bh), p.border, 1, self.theme.radius))
|
|
let big = if bh / 2 < 48 { bh / 2 } else { 48 }
|
|
if icon != 0U {
|
|
ops.push(@scene.DrawOp::Image(@scene.Rect::new(bx + (bw - big) / 2, by + (bh - big) / 2, big, big), icon))
|
|
} else {
|
|
let letter = if app.length() > 0 { app[:1].to_string().to_upper() } else if title.length() > 0 { title[:1].to_string().to_upper() } else { "?" }
|
|
ops.push(@scene.DrawOp::Text(bx + bw / 2, by + bh / 2, letter, big * 2 / 3, p.foreground_muted, @scene.TextAlign::Center, true))
|
|
}
|
|
}
|
|
if minimized {
|
|
ops.push(@scene.DrawOp::Text(bx + bw / 2, by + bh / 2, "minimized", fs - 1, @scene.Rgba::new(255, 255, 255, 230), @scene.TextAlign::Center, true))
|
|
}
|
|
// The label: app icon, then the title cut to fit.
|
|
let ly = by + bh + label_h / 2
|
|
let isz = 18
|
|
let mut tx = bx + 2
|
|
if icon != 0U {
|
|
ops.push(@scene.DrawOp::Image(@scene.Rect::new(tx, ly - isz / 2, isz, isz), icon))
|
|
tx = tx + isz + 6
|
|
}
|
|
let fg = if sel { p.accent_foreground } else if minimized { p.foreground_muted } else { p.foreground }
|
|
let cw = (fs * 11 + 19) / 20
|
|
let max_chars = (bx + bw - tx) / cw
|
|
let shown = if title.length() > max_chars && max_chars > 1 { title[:max_chars - 1].to_string() + "…" } else { title }
|
|
ops.push(@scene.DrawOp::Text(tx, ly, shown, fs, fg, @scene.TextAlign::Left, sel))
|
|
}
|
|
ops
|
|
}
|