Nested window backend: run the compositor in a window on the host Wayland session
CI / test (push) Successful in 26s

The compositor opens an xdg_toplevel on $WAYLAND_DISPLAY, presents frames via
wl_shm with frame-callback pacing and forwards host pointer input to the gfx
surface. just run now uses it (release build); run-headless keeps the old mode.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-24 19:06:51 -08:00
1 parent a265eebf02
commit a1b85474f8
5 files changed
+455 -6

No files matched your search

+396
View File
@@ -0,0 +1,396 @@
///| Nested backend: shows the compositor's frames in a window of the host
/// Wayland session. The compositor is a plain Wayland *client* here (wl_shm
/// buffers over SCM_RIGHTS + xdg_toplevel), reusing the same wire code and C
/// shim as the server side, and forwards host pointer input to the gfx surface.
///|
pub(all) struct NestedWindow {
fd : Int
rx : Array[Byte]
globals : Map[String, (UInt, UInt)]
live_buffers : Map[UInt, Bool]
mut next_id : UInt
mut comp_id : UInt
mut shm_id : UInt
mut wm_id : UInt
mut seat_id : UInt
mut surf_id : UInt
mut xdg_surf_id : UInt
mut toplevel_id : UInt
mut pointer_id : UInt
mut sync_cb : UInt
mut sync_done : Bool
mut frame_cb : UInt
mut frame_pending : Bool
mut configured : Bool
mut closed : Bool
mut px : Double
mut py : Double
width : Int
height : Int
}
///|
fn NestedWindow::alloc(self : NestedWindow) -> UInt {
let id = self.next_id
self.next_id = id + 1U
id
}
///|
fn NestedWindow::send(self : NestedWindow, msg : Bytes, send_fd? : Int = -1) -> Unit {
ignore(@sys.sendmsg_fd(self.fd, msg, send_fd))
}
///|
fn NestedWindow::bind(self : NestedWindow, iface : String, ver : UInt) -> UInt {
match self.globals.get(iface) {
None => 0U
Some((name, adv)) => {
let id = self.alloc()
let w = @wayland.WireWriter::new()
w.u32(name)
w.str(iface)
w.u32(if ver < adv { ver } else { adv })
w.u32(id)
self.send(w.finish_msg(2U, 0U))
id
}
}
}
///| Read and dispatch everything pending on the host socket (non-blocking).
fn NestedWindow::pump_events(self : NestedWindow, surface : @gfx.Surface?) -> Unit {
let scratch : FixedArray[Byte] = FixedArray::make(16384, b'\x00')
while @sys.poll_read(self.fd, 0) > 0 {
let (n, rfd) = @sys.recvmsg_fd(self.fd, scratch, 16384)
if rfd >= 0 {
@sys.close_fd(rfd)
}
if n <= 0 {
self.closed = true
return
}
for i = 0; i < n; i = i + 1 {
self.rx.push(scratch[i])
}
}
let mut off = 0
while off + 8 <= self.rx.length() {
let obj = @wayland.read_u32_le(self.rx, off)
let word2 = @wayland.read_u32_le(self.rx, off + 4)
let size = ((word2 >> 16) & 0xFFFFU).reinterpret_as_int()
let op = word2 & 0xFFFFU
if size < 8 || off + size > self.rx.length() {
break
}
let r = @wayland.WireReader::from_array(self.rx, off + 8)
self.dispatch(obj, op, r, surface)
off = off + size
}
if off > 0 {
let rest : Array[Byte] = []
for i = off; i < self.rx.length(); i = i + 1 {
rest.push(self.rx[i])
}
self.rx.clear()
for b in rest {
self.rx.push(b)
}
}
}
///|
fn NestedWindow::dispatch(
self : NestedWindow,
obj : UInt,
op : UInt,
r : @wayland.WireReader,
surface : @gfx.Surface?,
) -> Unit {
if obj == 1U {
if op == 0U {
let _id = r.u32()
let _code = r.u32()
println("nested: host wl_display error: " + r.str())
self.closed = true
}
} else if obj == 2U {
if op == 0U {
let name = r.u32()
let iface = r.str()
let ver = r.u32()
self.globals.set(iface, (name, ver))
}
} else if self.sync_cb != 0U && obj == self.sync_cb {
self.sync_done = true
} else if self.frame_cb != 0U && obj == self.frame_cb {
self.frame_pending = false
self.frame_cb = 0U
} else if obj == self.wm_id {
if op == 0U {
// xdg_wm_base.ping(serial) -> pong
let serial = r.u32()
let w = @wayland.WireWriter::new()
w.u32(serial)
self.send(w.finish_msg(self.wm_id, 3U))
}
} else if obj == self.xdg_surf_id {
if op == 0U {
// configure(serial) -> ack_configure
let serial = r.u32()
let w = @wayland.WireWriter::new()
w.u32(serial)
self.send(w.finish_msg(self.xdg_surf_id, 4U))
self.configured = true
}
} else if obj == self.toplevel_id {
if op == 1U {
self.closed = true
}
} else if obj == self.seat_id {
if op == 0U && self.pointer_id == 0U {
let caps = r.u32()
if (caps & 1U) != 0U {
let id = self.alloc()
let w = @wayland.WireWriter::new()
w.u32(id)
self.send(w.finish_msg(self.seat_id, 0U))
self.pointer_id = id
}
}
} else if self.pointer_id != 0U && obj == self.pointer_id {
match op {
0U => {
let _serial = r.u32()
let _surf = r.u32()
self.px = r.i32().to_double() / 256.0
self.py = r.i32().to_double() / 256.0
}
2U => {
let _time = r.u32()
self.px = r.i32().to_double() / 256.0
self.py = r.i32().to_double() / 256.0
match surface {
Some(s) => s.push_pointer_move(self.px, self.py)
None => ()
}
}
3U => {
let _serial = r.u32()
let _time = r.u32()
let _button = r.u32()
let state = r.u32()
match surface {
Some(s) =>
if state == 1U {
s.push_pointer_down(self.px, self.py)
} else {
s.push_pointer_up(self.px, self.py)
}
None => ()
}
}
_ => ()
}
} else if self.live_buffers.contains(obj) {
if op == 0U {
// wl_buffer.release -> destroy it
self.live_buffers.remove(obj)
let w = @wayland.WireWriter::new()
self.send(w.finish_msg(obj, 0U))
}
}
}
///| wl_display.sync and wait for the callback.
fn NestedWindow::roundtrip(self : NestedWindow) -> Unit {
let cb = self.alloc()
self.sync_cb = cb
self.sync_done = false
let w = @wayland.WireWriter::new()
w.u32(cb)
self.send(w.finish_msg(1U, 0U))
let mut tries = 0
while !self.sync_done && !self.closed && tries < 500 {
ignore(@sys.poll_read(self.fd, 10))
self.pump_events(None)
tries = tries + 1
}
self.sync_cb = 0U
}
///| Connect to the host compositor and open a window of `width` x `height`.
/// `display` is a socket name (relative to `runtime_dir`) or an absolute path.
pub fn NestedWindow::open(
display : String,
runtime_dir : String,
title : String,
app_id : String,
width : Int,
height : Int,
) -> NestedWindow? {
let path = if display.length() > 0 && display[0] == '/' {
display
} else {
runtime_dir + "/" + display
}
let fd = @sys.unix_connect(path)
if fd < 0 {
return None
}
let self : NestedWindow = {
fd,
rx: [],
globals: {},
live_buffers: {},
next_id: 3U,
comp_id: 0U,
shm_id: 0U,
wm_id: 0U,
seat_id: 0U,
surf_id: 0U,
xdg_surf_id: 0U,
toplevel_id: 0U,
pointer_id: 0U,
sync_cb: 0U,
sync_done: false,
frame_cb: 0U,
frame_pending: false,
configured: false,
closed: false,
px: 0.0,
py: 0.0,
width,
height,
}
let w = @wayland.WireWriter::new()
w.u32(2U)
self.send(w.finish_msg(1U, 1U)) // get_registry
self.roundtrip()
self.comp_id = self.bind("wl_compositor", 4U)
self.shm_id = self.bind("wl_shm", 1U)
self.wm_id = self.bind("xdg_wm_base", 2U)
self.seat_id = self.bind("wl_seat", 5U)
if self.comp_id == 0U || self.shm_id == 0U || self.wm_id == 0U {
println("nested: host compositor lacks wl_compositor/wl_shm/xdg_wm_base")
return None
}
self.surf_id = self.alloc()
let w1 = @wayland.WireWriter::new()
w1.u32(self.surf_id)
self.send(w1.finish_msg(self.comp_id, 0U))
self.xdg_surf_id = self.alloc()
let w2 = @wayland.WireWriter::new()
w2.u32(self.xdg_surf_id)
w2.u32(self.surf_id)
self.send(w2.finish_msg(self.wm_id, 2U))
self.toplevel_id = self.alloc()
let w3 = @wayland.WireWriter::new()
w3.u32(self.toplevel_id)
self.send(w3.finish_msg(self.xdg_surf_id, 1U))
let w4 = @wayland.WireWriter::new()
w4.str(title)
self.send(w4.finish_msg(self.toplevel_id, 2U))
let w5 = @wayland.WireWriter::new()
w5.str(app_id)
self.send(w5.finish_msg(self.toplevel_id, 3U))
// Fixed size: min == max == output size.
let w6 = @wayland.WireWriter::new()
w6.i32(width)
w6.i32(height)
self.send(w6.finish_msg(self.toplevel_id, 7U)) // set_max_size
let w7 = @wayland.WireWriter::new()
w7.i32(width)
w7.i32(height)
self.send(w7.finish_msg(self.toplevel_id, 8U)) // set_min_size
self.send(@wayland.WireWriter::new().finish_msg(self.surf_id, 6U)) // commit
let mut tries = 0
while !self.configured && !self.closed && tries < 500 {
ignore(@sys.poll_read(self.fd, 10))
self.pump_events(None)
tries = tries + 1
}
if !self.configured {
println("nested: host never sent xdg_surface.configure")
return None
}
Some(self)
}
///| Pump host events (forwarding pointer input to `surface`). False once the
/// window was closed or the host went away.
pub fn NestedWindow::poll(self : NestedWindow, surface : @gfx.Surface) -> Bool {
self.pump_events(Some(surface))
!self.closed
}
///| Show `pixmap` (RGBA8888) in the window. Skipped while the host has not yet
/// consumed the previous frame, so a slow host never builds a backlog.
pub fn NestedWindow::present(self : NestedWindow, pixmap : @renderer.Pixmap) -> Unit {
if self.frame_pending || self.closed {
return
}
let w = pixmap.width
let h = pixmap.height
let stride = w * 4
let len = stride * h
// RGBA -> little-endian ARGB8888 (B, G, R, A).
let out : FixedArray[Byte] = FixedArray::make(len, b'\x00')
for i = 0; i < w * h; i = i + 1 {
let o = i * 4
out[o] = pixmap.data[o + 2]
out[o + 1] = pixmap.data[o + 1]
out[o + 2] = pixmap.data[o]
out[o + 3] = b'\xff'
}
let memfd = @sys.memfd_create("moon-plasma-frame", len)
if memfd < 0 {
return
}
ignore(@sys.pwrite_bytes(memfd, Bytes::from_fixedarray(out), 0))
let pool = self.alloc()
let wp = @wayland.WireWriter::new()
wp.u32(pool)
wp.i32(len)
self.send(wp.finish_msg(self.shm_id, 0U), send_fd=memfd)
@sys.close_fd(memfd)
let buf = self.alloc()
let wb = @wayland.WireWriter::new()
wb.u32(buf)
wb.i32(0)
wb.i32(w)
wb.i32(h)
wb.i32(stride)
wb.u32(0U)
self.send(wb.finish_msg(pool, 0U))
self.send(@wayland.WireWriter::new().finish_msg(pool, 1U)) // pool.destroy
self.live_buffers.set(buf, true)
let wa = @wayland.WireWriter::new()
wa.u32(buf)
wa.i32(0)
wa.i32(0)
self.send(wa.finish_msg(self.surf_id, 1U))
let wd = @wayland.WireWriter::new()
wd.i32(0)
wd.i32(0)
wd.i32(w)
wd.i32(h)
self.send(wd.finish_msg(self.surf_id, 9U))
self.frame_cb = self.alloc()
let wf = @wayland.WireWriter::new()
wf.u32(self.frame_cb)
self.send(wf.finish_msg(self.surf_id, 3U))
self.frame_pending = true
self.send(@wayland.WireWriter::new().finish_msg(self.surf_id, 6U))
}
///|
pub fn NestedWindow::close(self : NestedWindow) -> Unit {
@sys.close_fd(self.fd)
}
///| True when the host is ready for a new frame (avoids rendering frames that would be dropped).
pub fn NestedWindow::frame_ready(self : NestedWindow) -> Bool {
!self.frame_pending && !self.closed
}