Replace Rust host-native with 100% pure-MoonBit Wayland compositor
CI / test (push) Successful in 36s
CI / test (push) Successful in 36s
This commit is contained in:
1 parent
17a8a46cfd
commit
ae4f3fa93c
31 files changed
+3647
-4962
No files matched your search
@@ -0,0 +1,358 @@
|
||||
///| 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?
|
||||
}
|
||||
|
||||
///|
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
///|
|
||||
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()
|
||||
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)
|
||||
self.configure_serial = Some(reader.u32())
|
||||
}
|
||||
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.MoonPlasmaCompositor,
|
||||
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.MoonPlasmaCompositor,
|
||||
c : TestWaylandClient,
|
||||
shm_id : UInt,
|
||||
surf_id : UInt,
|
||||
xdg_surf_id : UInt,
|
||||
win_id : UInt64,
|
||||
) -> 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))
|
||||
|
||||
// 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.MoonPlasmaCompositor::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.moonplasma.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.moonplasma.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()
|
||||
}
|
||||
Reference in new issue
Block a user