CI / test (push) Failing after 17s
GPUI apps (Delta, Zed) answer a configure that takes the maximized state away by going back to their old floating size, whatever size it asks for. Unmaximized into a tiled slot (or a layout change while maximized), Delta kept drawing 640x480 and Chandra padded it out with the bottom right colour to the whole frame, so only the sidebar and toolbar showed. The server now tracks each toplevel's last configure and the serial the client acked. When a client commits a buffer of another size after acking the latest configure, the same configure is sent once more (GPUI, no longer maximized then, takes it). A client that still keeps its own size gets a ToplevelSizeChosen: a floating window's frame follows it, a tiled, maximized or fullscreen window keeps the compositor's size. Sizes take set_window_geometry and set_buffer_scale into account. CHANDRA_DEBUG_CONFIGURE=1 logs configures, acks and committed buffer sizes to stderr. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hiKUmCPZNFNiGwd8EW
498 lines
14 KiB
MoonBit
498 lines
14 KiB
MoonBit
///| End-to-end native Wayland compositor test in pure MoonBit.
|
|
///| Connects real Wayland wire-protocol clients with SCM_RIGHTS wl_shm memfds,
|
|
///| verifies ServerSide decoration negotiation, lib/wm BSP tiling & Floating switch,
|
|
///| and composited pixel + PNG output.
|
|
|
|
///|
|
|
struct TestWaylandClient {
|
|
fd : Int
|
|
mut next_id : UInt
|
|
rx_buf : Array[Byte]
|
|
globals : Map[String, (UInt, UInt)]
|
|
mut deco_mode : UInt?
|
|
mut configured_w : Int
|
|
mut configured_h : Int
|
|
mut configure_serial : UInt?
|
|
/// The last xdg_toplevel.configure had the maximized state.
|
|
mut configured_max : Bool
|
|
/// Every configure received, in order, as (width, height, maximized, serial).
|
|
configures : Array[(Int, Int, Bool, UInt)]
|
|
}
|
|
|
|
///|
|
|
fn TestWaylandClient::connect(socket_path : String) -> TestWaylandClient? {
|
|
let fd = @sys.unix_connect(socket_path)
|
|
if fd < 0 {
|
|
return None
|
|
}
|
|
Some({
|
|
fd,
|
|
next_id: 2U,
|
|
rx_buf: [],
|
|
globals: {},
|
|
deco_mode: None,
|
|
configured_w: 0,
|
|
configured_h: 0,
|
|
configure_serial: None,
|
|
configured_max: false,
|
|
configures: [],
|
|
})
|
|
}
|
|
|
|
///|
|
|
fn TestWaylandClient::alloc_id(self : TestWaylandClient) -> UInt {
|
|
let id = self.next_id
|
|
self.next_id = self.next_id + 1U
|
|
id
|
|
}
|
|
|
|
///|
|
|
fn TestWaylandClient::send(
|
|
self : TestWaylandClient,
|
|
msg : Bytes,
|
|
send_fd? : Int = -1,
|
|
) -> Unit {
|
|
ignore(@sys.sendmsg_fd(self.fd, msg, send_fd))
|
|
}
|
|
|
|
///|
|
|
fn TestWaylandClient::read_events(
|
|
self : TestWaylandClient,
|
|
xdg_surf_id : UInt,
|
|
toplevel_id : UInt,
|
|
deco_id : UInt,
|
|
) -> Unit {
|
|
let scratch : FixedArray[Byte] = FixedArray::make(8192, b'\x00')
|
|
while @sys.poll_read(self.fd, 5) > 0 {
|
|
let (n, rfd) = @sys.recvmsg_fd(self.fd, scratch, 8192)
|
|
if rfd >= 0 {
|
|
@sys.close_fd(rfd)
|
|
}
|
|
if n <= 0 {
|
|
break
|
|
}
|
|
for i = 0; i < n; i = i + 1 {
|
|
self.rx_buf.push(scratch[i])
|
|
}
|
|
}
|
|
let mut off = 0
|
|
while off + 8 <= self.rx_buf.length() {
|
|
let obj_id = @wayland.read_u32_le(self.rx_buf, off)
|
|
let word2 = @wayland.read_u32_le(self.rx_buf, off + 4)
|
|
let size = ((word2 >> 16) & 0xFFFFU).reinterpret_as_int()
|
|
let opcode = word2 & 0xFFFFU
|
|
if size < 8 || off + size > self.rx_buf.length() {
|
|
break
|
|
}
|
|
let reader = @wayland.WireReader::from_array(self.rx_buf, off + 8)
|
|
if obj_id == 2U && opcode == 0U {
|
|
// wl_registry.global(name, interface, version)
|
|
let name = reader.u32()
|
|
let iface = reader.str()
|
|
let ver = reader.u32()
|
|
self.globals.set(iface, (name, ver))
|
|
} else if obj_id == deco_id && opcode == 0U {
|
|
// zxdg_toplevel_decoration_v1.configure(mode)
|
|
self.deco_mode = Some(reader.u32())
|
|
} else if obj_id == toplevel_id && opcode == 0U {
|
|
// xdg_toplevel.configure(width, height, states)
|
|
let w = reader.i32()
|
|
let h = reader.i32()
|
|
let states = reader.array()
|
|
self.configured_max = false
|
|
for i = 0; i + 3 < states.length(); i = i + 4 {
|
|
if states[i] == b'\x01' {
|
|
self.configured_max = true
|
|
}
|
|
}
|
|
if w > 0 && h > 0 {
|
|
self.configured_w = w
|
|
self.configured_h = h
|
|
}
|
|
} else if obj_id == xdg_surf_id && opcode == 0U {
|
|
// xdg_surface.configure(serial)
|
|
let serial = reader.u32()
|
|
self.configure_serial = Some(serial)
|
|
self.configures.push((self.configured_w, self.configured_h, self.configured_max, serial))
|
|
}
|
|
off = off + size
|
|
}
|
|
if off > 0 {
|
|
let rem : Array[Byte] = []
|
|
for i = off; i < self.rx_buf.length(); i = i + 1 {
|
|
rem.push(self.rx_buf[i])
|
|
}
|
|
self.rx_buf.clear()
|
|
for b in rem {
|
|
self.rx_buf.push(b)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn bind_global(
|
|
c : TestWaylandClient,
|
|
reg_id : UInt,
|
|
iface : String,
|
|
ver : UInt,
|
|
) -> UInt {
|
|
let (name, adv_ver) = match c.globals.get(iface) {
|
|
Some(v) => v
|
|
None => (0U, ver)
|
|
}
|
|
let use_ver = if ver < adv_ver { ver } else { adv_ver }
|
|
let id = c.alloc_id()
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(name)
|
|
w.str(iface)
|
|
w.u32(use_ver)
|
|
w.u32(id)
|
|
c.send(w.finish_msg(reg_id, 0U))
|
|
id
|
|
}
|
|
|
|
///|
|
|
fn open_and_configure_client(
|
|
comp : @host.ChandraCompositor,
|
|
socket_path : String,
|
|
title : String,
|
|
app_id : String,
|
|
) -> (TestWaylandClient, UInt, UInt, UInt, UInt, UInt) {
|
|
let c = TestWaylandClient::connect(socket_path).unwrap()
|
|
// 1. wl_display.get_registry(id=2)
|
|
let reg_id = c.alloc_id() // 2
|
|
let w_reg = @wayland.WireWriter::new()
|
|
w_reg.u32(reg_id)
|
|
c.send(w_reg.finish_msg(1U, 1U))
|
|
|
|
for _i = 0; _i < 10; _i = _i + 1 {
|
|
comp.step()
|
|
c.read_events(0U, 0U, 0U)
|
|
if c.globals.contains("zxdg_decoration_manager_v1") {
|
|
break
|
|
}
|
|
@sys.sleep_ms(2)
|
|
}
|
|
|
|
let comp_id = bind_global(c, reg_id, "wl_compositor", 5U)
|
|
let shm_id = bind_global(c, reg_id, "wl_shm", 1U)
|
|
let xdg_wm_id = bind_global(c, reg_id, "xdg_wm_base", 5U)
|
|
let deco_mgr_id = bind_global(c, reg_id, "zxdg_decoration_manager_v1", 1U)
|
|
|
|
// create_surface
|
|
let surf_id = c.alloc_id()
|
|
let w_cs = @wayland.WireWriter::new()
|
|
w_cs.u32(surf_id)
|
|
c.send(w_cs.finish_msg(comp_id, 0U))
|
|
|
|
// get_xdg_surface
|
|
let xdg_surf_id = c.alloc_id()
|
|
let w_xs = @wayland.WireWriter::new()
|
|
w_xs.u32(xdg_surf_id)
|
|
w_xs.u32(surf_id)
|
|
c.send(w_xs.finish_msg(xdg_wm_id, 2U))
|
|
|
|
// get_toplevel
|
|
let toplevel_id = c.alloc_id()
|
|
let w_tl = @wayland.WireWriter::new()
|
|
w_tl.u32(toplevel_id)
|
|
c.send(w_tl.finish_msg(xdg_surf_id, 1U))
|
|
|
|
// set_title & set_app_id
|
|
let w_t = @wayland.WireWriter::new()
|
|
w_t.str(title)
|
|
c.send(w_t.finish_msg(toplevel_id, 2U))
|
|
let w_a = @wayland.WireWriter::new()
|
|
w_a.str(app_id)
|
|
c.send(w_a.finish_msg(toplevel_id, 3U))
|
|
|
|
// get_toplevel_decoration
|
|
let deco_id = c.alloc_id()
|
|
let w_dc = @wayland.WireWriter::new()
|
|
w_dc.u32(deco_id)
|
|
w_dc.u32(toplevel_id)
|
|
c.send(w_dc.finish_msg(deco_mgr_id, 1U))
|
|
|
|
// initial commit
|
|
let w_cm = @wayland.WireWriter::new()
|
|
c.send(w_cm.finish_msg(surf_id, 6U))
|
|
|
|
for _i = 0; _i < 20; _i = _i + 1 {
|
|
comp.step()
|
|
c.read_events(xdg_surf_id, toplevel_id, deco_id)
|
|
if c.configure_serial is Some(_) && c.configured_w > 0 {
|
|
break
|
|
}
|
|
@sys.sleep_ms(2)
|
|
}
|
|
|
|
(c, shm_id, surf_id, xdg_surf_id, toplevel_id, deco_id)
|
|
}
|
|
|
|
///|
|
|
fn commit_shm_frame(
|
|
comp : @host.ChandraCompositor,
|
|
c : TestWaylandClient,
|
|
shm_id : UInt,
|
|
surf_id : UInt,
|
|
xdg_surf_id : UInt,
|
|
win_id : UInt64,
|
|
destroy_pool? : Bool = false,
|
|
) -> Unit raise {
|
|
match c.configure_serial {
|
|
Some(ser) => {
|
|
let w_ack = @wayland.WireWriter::new()
|
|
w_ack.u32(ser)
|
|
c.send(w_ack.finish_msg(xdg_surf_id, 4U))
|
|
}
|
|
None => ()
|
|
}
|
|
let w = c.configured_w
|
|
let h = c.configured_h
|
|
let stride = w * 4
|
|
let byte_len = stride * h
|
|
|
|
let pixels : Array[Byte] = Array(capacity=byte_len)
|
|
for y = 0; y < h; y = y + 1 {
|
|
for x = 0; x < w; x = x + 1 {
|
|
let in_center = (x - w / 2).abs() < 40 && (y - h / 2).abs() < 40
|
|
// Little-endian ARGB8888: [B, G, R, A]
|
|
if in_center {
|
|
pixels.push(b'\xf8')
|
|
pixels.push(b'\xbd')
|
|
pixels.push(b'\x38')
|
|
pixels.push(b'\xff')
|
|
} else {
|
|
pixels.push(b'\x3b')
|
|
pixels.push(b'\x29')
|
|
pixels.push(b'\x1e')
|
|
pixels.push(b'\xff')
|
|
}
|
|
}
|
|
}
|
|
let memfd = @sys.memfd_create("wl-shm-test", byte_len)
|
|
assert_true(memfd >= 0)
|
|
ignore(@sys.pwrite_bytes(memfd, Bytes::from_array(pixels), 0))
|
|
|
|
// wl_shm.create_pool(id, size) with SCM_RIGHTS memfd
|
|
let pool_id = c.alloc_id()
|
|
let w_pool = @wayland.WireWriter::new()
|
|
w_pool.u32(pool_id)
|
|
w_pool.i32(byte_len)
|
|
c.send(w_pool.finish_msg(shm_id, 0U), send_fd=memfd)
|
|
@sys.close_fd(memfd)
|
|
|
|
// wl_shm_pool.create_buffer(id, offset, w, h, stride, format=0 ARGB8888)
|
|
let buf_id = c.alloc_id()
|
|
let w_buf = @wayland.WireWriter::new()
|
|
w_buf.u32(buf_id)
|
|
w_buf.i32(0)
|
|
w_buf.i32(w)
|
|
w_buf.i32(h)
|
|
w_buf.i32(stride)
|
|
w_buf.u32(0U)
|
|
c.send(w_buf.finish_msg(pool_id, 0U))
|
|
if destroy_pool {
|
|
// Mesa's software EGL path (Qt Quick apps like kwi) destroys the pool right away;
|
|
// the buffer must stay valid.
|
|
c.send(@wayland.WireWriter::new().finish_msg(pool_id, 1U))
|
|
}
|
|
|
|
// wl_surface.attach(buf, 0, 0) + damage_buffer + commit
|
|
let w_att = @wayland.WireWriter::new()
|
|
w_att.u32(buf_id)
|
|
w_att.i32(0)
|
|
w_att.i32(0)
|
|
c.send(w_att.finish_msg(surf_id, 1U))
|
|
|
|
let w_dmg = @wayland.WireWriter::new()
|
|
w_dmg.i32(0)
|
|
w_dmg.i32(0)
|
|
w_dmg.i32(w)
|
|
w_dmg.i32(h)
|
|
c.send(w_dmg.finish_msg(surf_id, 9U))
|
|
|
|
let w_cmt = @wayland.WireWriter::new()
|
|
c.send(w_cmt.finish_msg(surf_id, 6U))
|
|
|
|
for _i = 0; _i < 20; _i = _i + 1 {
|
|
comp.step()
|
|
match comp.windows.get(win_id) {
|
|
Some(win) => if win.surface is Some(_) { break }
|
|
None => ()
|
|
}
|
|
@sys.sleep_ms(2)
|
|
}
|
|
}
|
|
|
|
///|
|
|
test "e2e pure-moonbit wayland compositor with lib/wm, ssd decorations, and shm blitting" {
|
|
let socket_path = "/tmp/wayland-moon-e2e-" +
|
|
@sys.getpid().to_string() +
|
|
".sock"
|
|
let theme = @scene.ThemeTokens::moon_night()
|
|
let comp = @host.ChandraCompositor::new(socket_path, theme, 1280, 720).unwrap()
|
|
|
|
// Client 1: MoonTerm
|
|
let (c1, shm1, surf1, xdg1, _tl1, _deco1) = open_and_configure_client(
|
|
comp, socket_path, "MoonTerm", "org.chandra.term",
|
|
)
|
|
assert_eq(c1.deco_mode, Some(2U)) // ServerSide
|
|
assert_true(c1.configured_w > 1200 && c1.configured_h > 600)
|
|
|
|
commit_shm_frame(comp, c1, shm1, surf1, xdg1, 1UL)
|
|
let pixmap = comp.compose_frame()
|
|
|
|
// Save PNG and verify PNG signature
|
|
let png_bytes = @renderer.encode_png(pixmap)
|
|
assert_true(png_bytes.length() > 100)
|
|
assert_eq(png_bytes[0], b'\x89')
|
|
assert_eq(png_bytes[1], b'\x50')
|
|
assert_eq(png_bytes[2], b'\x4e')
|
|
assert_eq(png_bytes[3], b'\x47')
|
|
assert_eq(@sys.write_file_bytes("_build/e2e_frame.png", png_bytes), 0)
|
|
|
|
// Verify cyan (#38bdf8) center pixel landed inside WM-assigned geometry!
|
|
let win1 = comp.windows.get(1UL).unwrap()
|
|
let cx = win1.geometry.x + win1.geometry.w / 2
|
|
let cy = win1.geometry.y + win1.geometry.h / 2
|
|
let px = pixmap.pixel(cx, cy).unwrap()
|
|
assert_eq(px, @scene.Rgba::new(0x38, 0xbd, 0xf8, 0xff))
|
|
|
|
// Client 2: Files -> verify BSP tiling splits the screen!
|
|
let (c2, shm2, surf2, xdg2, _tl2, _deco2) = open_and_configure_client(
|
|
comp, socket_path, "Files", "org.chandra.files",
|
|
)
|
|
assert_true(c2.configured_w > 0 && c2.configured_w < 700)
|
|
commit_shm_frame(comp, c2, shm2, surf2, xdg2, 2UL)
|
|
|
|
// Switch to Floating layout mode and compose
|
|
comp.set_layout_mode(@wm.LayoutMode::Floating)
|
|
let float_pixmap = comp.compose_frame()
|
|
assert_eq(float_pixmap.width, 1280)
|
|
assert_eq(float_pixmap.height, 720)
|
|
|
|
@sys.close_fd(c1.fd)
|
|
@sys.close_fd(c2.fd)
|
|
comp.close()
|
|
}
|
|
|
|
///|
|
|
test "a buffer stays valid after its wl_shm_pool is destroyed" {
|
|
let socket_path = "/tmp/wayland-moon-pooldestroy-" +
|
|
@sys.getpid().to_string() +
|
|
".sock"
|
|
let theme = @scene.ThemeTokens::moon_night()
|
|
let comp = @host.ChandraCompositor::new(socket_path, theme, 1280, 720).unwrap()
|
|
let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(
|
|
comp, socket_path, "Kwi", "org.kde.kwi",
|
|
)
|
|
commit_shm_frame(comp, c, shm, surf, xdg, 1UL, destroy_pool=true)
|
|
assert_true(comp.windows.get(1UL).unwrap().surface is Some(_))
|
|
}
|
|
|
|
///| A solid ARGB8888 frame of `w` x `h` in a new memfd.
|
|
fn solid_memfd(w : Int, h : Int, b : Byte, g : Byte, r : Byte) -> Int {
|
|
let px : Array[Byte] = Array(capacity=w * h * 4)
|
|
for _i = 0; _i < w * h; _i = _i + 1 {
|
|
px.push(b)
|
|
px.push(g)
|
|
px.push(r)
|
|
px.push(b'\xff')
|
|
}
|
|
let fd = @sys.memfd_create("wl-shm-swapchain", w * h * 4)
|
|
ignore(@sys.pwrite_bytes(fd, Bytes::from_array(px), 0))
|
|
fd
|
|
}
|
|
|
|
///|
|
|
fn attach_commit(comp : @host.ChandraCompositor, c : TestWaylandClient, surf : UInt, buf : UInt?) -> Unit {
|
|
match buf {
|
|
Some(b) => {
|
|
let a = @wayland.WireWriter::new()
|
|
a.u32(b)
|
|
a.i32(0)
|
|
a.i32(0)
|
|
c.send(a.finish_msg(surf, 1U))
|
|
}
|
|
None => ()
|
|
}
|
|
c.send(@wayland.WireWriter::new().finish_msg(surf, 6U))
|
|
for _i = 0; _i < 5; _i = _i + 1 {
|
|
comp.step()
|
|
@sys.sleep_ms(2)
|
|
}
|
|
}
|
|
|
|
///|
|
|
test "swapchain pools sent in one message all keep their fds, and a bare commit keeps the frame" {
|
|
// Mesa's Vulkan WSI on wl_shm (what GPUI apps like Zed and Delta fall back to) makes one
|
|
// wl_shm_pool per swapchain image and libwayland flushes them with all their fds in one
|
|
// sendmsg. Every fd but the first used to be dropped, so those images never showed.
|
|
let socket_path = "/tmp/wayland-moon-swapchain-" + @sys.getpid().to_string() + ".sock"
|
|
let theme = @scene.ThemeTokens::moon_night()
|
|
let comp = @host.ChandraCompositor::new(socket_path, theme, 1280, 720).unwrap()
|
|
let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(
|
|
comp, socket_path, "Delta", "dev.delta.Delta",
|
|
)
|
|
match c.configure_serial {
|
|
Some(ser) => {
|
|
let ack = @wayland.WireWriter::new()
|
|
ack.u32(ser)
|
|
c.send(ack.finish_msg(xdg, 4U))
|
|
}
|
|
None => ()
|
|
}
|
|
let w = c.configured_w
|
|
let h = c.configured_h
|
|
let fds = [
|
|
solid_memfd(w, h, b'\x00', b'\x00', b'\xff'),
|
|
solid_memfd(w, h, b'\x00', b'\xff', b'\x00'),
|
|
solid_memfd(w, h, b'\xff', b'\x00', b'\x00'),
|
|
]
|
|
// Three create_pool + create_buffer requests, flushed together with their three fds.
|
|
let msg : Array[Byte] = []
|
|
let bufs : Array[UInt] = []
|
|
for _i = 0; _i < 3; _i = _i + 1 {
|
|
let pool = c.alloc_id()
|
|
let p = @wayland.WireWriter::new()
|
|
p.u32(pool)
|
|
p.i32(w * h * 4)
|
|
for b in p.finish_msg(shm, 0U) {
|
|
msg.push(b)
|
|
}
|
|
let buf = c.alloc_id()
|
|
let cb = @wayland.WireWriter::new()
|
|
cb.u32(buf)
|
|
cb.i32(0)
|
|
cb.i32(w)
|
|
cb.i32(h)
|
|
cb.i32(w * 4)
|
|
cb.u32(0U)
|
|
for b in cb.finish_msg(pool, 0U) {
|
|
msg.push(b)
|
|
}
|
|
bufs.push(buf)
|
|
}
|
|
assert_true(@sys.sendmsg_fds(c.fd, Bytes::from_array(msg), fds) > 0)
|
|
let win = comp.windows.get(1UL).unwrap()
|
|
let cx = win.geometry.x + win.geometry.w / 2
|
|
let cy = win.geometry.y + win.geometry.h / 2
|
|
|
|
attach_commit(comp, c, surf, Some(bufs[2]))
|
|
assert_eq(comp.compose_frame().pixel(cx, cy), Some(@scene.Rgba::new(0, 0, 0xff, 0xff)))
|
|
attach_commit(comp, c, surf, Some(bufs[1]))
|
|
assert_eq(comp.compose_frame().pixel(cx, cy), Some(@scene.Rgba::new(0, 0xff, 0, 0xff)))
|
|
|
|
// The buffer was released, so the client draws its next frame into it; a commit without a
|
|
// new attach (GPUI commits after ack_configure) must not show that half-drawn frame.
|
|
ignore(@sys.pwrite_bytes(fds[1], Bytes::from_array([b'\x00', b'\x00', b'\xff', b'\xff']), (h / 2 * w + w / 2) * 4))
|
|
attach_commit(comp, c, surf, None)
|
|
assert_eq(comp.compose_frame().pixel(cx, cy), Some(@scene.Rgba::new(0, 0xff, 0, 0xff)))
|
|
for fd in fds {
|
|
@sys.close_fd(fd)
|
|
}
|
|
@sys.close_fd(c.fd)
|
|
comp.close()
|
|
}
|