Files
Claude 90fdda2455
CI / test (push) Successful in 2m57s
Client-drawn window frames: GTK apps (GNOME Files) get no second title bar
GTK never asks for server-side decoration (it has no xdg-decoration support), so it draws its
own header bar plus a transparent shadow margin around it, and says where the real window is
with xdg_surface.set_window_geometry. Chandra drew its Breeze frame around every window anyway
and blitted the whole buffer from the frame's corner: a second title bar on top, and the
content shifted down/right by the shadow (a see-through gap), with the right/bottom cut off.

- The server reports per window who draws the frame (ToplevelFrame: client-side when the
  client never made a zxdg_toplevel_decoration_v1, plus the window-geometry offset).
- A client-decorated window gets no Chandra frame and fills the frame's whole box
  (HostWindow::client_rect): it is configured for that size, drawn with its buffer shifted by
  the geometry offset (its own shadow drawn around it), and pointer coordinates and popup
  placement use the same origin. Tiling, title-bar hit areas and resize zones are unchanged.
- xdg_toplevel.move / resize (dragging a header bar, a CSD border) start Chandra's move and
  resize drags; they were ignored before.
- Tiled windows get the tiled_left/right/top/bottom states, so GTK drops its shadow and
  rounded corners there.

Checked with real Nautilus 46 and gtk4-demo on the headless native compositor. Tests:
compositor/host/csd_test.mbt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hkfW5ttNd831XmdsC5
2026-11-26 01:35:31 +00:00

501 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,
server_deco? : Bool = true,
) -> (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 (GTK never does: it draws its own frame)
let deco_id = if server_deco { c.alloc_id() } else { 0U }
if server_deco {
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()
}