Replace Rust host-native with 100% pure-MoonBit Wayland compositor
CI / test (push) Successful in 36s

This commit is contained in:
Antigravity committed 2026-11-25 01:18:46 +00:00
1 parent 17a8a46cfd
commit ae4f3fa93c
31 files changed
+3647 -4962

No files matched your search

+274
View File
@@ -0,0 +1,274 @@
///| Pure-MoonBit Wayland compositor host wiring WaylandServer, lib/wm, Breeze SSD, SceneGraph, and SoftwareRenderer.
///|
pub(all) struct HostWindow {
id : UInt64
mut title : String
mut app_id : String
mut geometry : @scene.Rect
mut focused : Bool
mut maximized : Bool
mut fullscreen : Bool
mut surface : @scene.ShmBufferSnapshot?
}
///|
pub(all) struct MoonPlasmaCompositor {
server : @wayland.WaylandServer
wm : @wm.WmState
theme : @scene.ThemeTokens
output_w : Int
output_h : Int
work_area : @wm.Rect
windows : Map[UInt64, HostWindow]
damage : @scene.DamageTracker
mut layout_mode : @wm.LayoutMode
}
///|
pub fn MoonPlasmaCompositor::new(
socket_path : String,
theme : @scene.ThemeTokens,
output_w : Int,
output_h : Int,
) -> MoonPlasmaCompositor? {
let server = match @wayland.WaylandServer::listen(socket_path, output_w, output_h) {
Some(s) => s
None => return None
}
let wm = @wm.WmState::new(
inner_gap=8,
outer_gap=8,
titlebar_h=32,
border_w=2,
)
let out_rect : @wm.Rect = { x: 0, y: 0, w: output_w, h: output_h }
let work_area = @wm.compute_work_area(out_rect, excl_bottom=theme.panel_height)
Some({
server,
wm,
theme,
output_w,
output_h,
work_area,
windows: {},
damage: @scene.DamageTracker::new(),
layout_mode: @wm.LayoutMode::Tiling,
})
}
///|
pub fn MoonPlasmaCompositor::close(self : MoonPlasmaCompositor) -> Unit {
self.server.close()
}
///|
fn MoonPlasmaCompositor::apply_actions(
self : MoonPlasmaCompositor,
actions : Array[@wm.WmAction],
) -> Unit {
for act in actions {
match act {
@wm.WmAction::SetGeometry(wid, r) =>
match self.windows.get(wid) {
Some(win) => {
win.geometry = @scene.Rect::new(r.x, r.y, r.w, r.h)
self.server.send_toplevel_configure(
wid,
r.w,
r.h,
win.focused,
win.maximized,
win.fullscreen,
)
self.damage.mark_full()
}
None => ()
}
@wm.WmAction::Focus(wid) => {
for id, w in self.windows {
w.focused = id == wid
}
self.damage.mark_full()
}
@wm.WmAction::Raise(_) => self.damage.mark_full()
@wm.WmAction::Close(wid) => self.server.send_toplevel_close(wid)
@wm.WmAction::SetMaximized(wid, m) =>
match self.windows.get(wid) {
Some(win) => win.maximized = m
None => ()
}
@wm.WmAction::SetFullscreen(wid, f) =>
match self.windows.get(wid) {
Some(win) => win.fullscreen = f
None => ()
}
}
}
}
///|
pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit {
let events = self.server.poll_events()
for ev in events {
match ev {
@wayland.CompositorEvent::ToplevelOpened(wid, app_id, title, min_w, min_h) => {
let win : HostWindow = {
id: wid,
title,
app_id,
geometry: @scene.Rect::new(100, 100, 640, 480),
focused: true,
maximized: false,
fullscreen: false,
surface: None,
}
self.windows.set(wid, win)
let rec : @wm.WindowRecord = {
id: wid,
app_id,
title,
geometry: { x: 100, y: 100, w: 640, h: 480 },
floating_geometry: { x: 100, y: 100, w: 640, h: 480 },
min_size: { w: min_w, h: min_h },
max_size: None,
focused: true,
maximized: false,
fullscreen: false,
minimized: false,
is_floating: false,
workspace: 0U,
can_move: true,
can_resize: true,
can_maximize: true,
can_minimize: true,
can_fullscreen: true,
}
let actions = self.wm.open_window(rec, self.work_area)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelUpdated(wid, app_id, title) =>
match self.windows.get(wid) {
Some(win) => {
if app_id.length() > 0 {
win.app_id = app_id
}
if title.length() > 0 {
win.title = title
}
self.damage.mark_full()
}
None => ()
}
@wayland.CompositorEvent::ToplevelCommitted(wid, snap) =>
match self.windows.get(wid) {
Some(win) => {
match snap {
Some(s) => {
win.surface = Some(s)
self.damage.mark_rect(win.geometry)
}
None => ()
}
}
None => ()
}
@wayland.CompositorEvent::ToplevelClosed(wid) => {
ignore(self.windows.remove(wid))
let actions = self.wm.close_window(wid, self.work_area)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMaximizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Maximize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelFullscreenRequest(wid, f) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Fullscreen(f),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMinimizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Minimize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMoveRequest(_) => ()
}
}
}
///|
pub fn MoonPlasmaCompositor::set_layout_mode(
self : MoonPlasmaCompositor,
mode : @wm.LayoutMode,
) -> Unit {
self.layout_mode = mode
let actions = self.wm.set_layout_mode(mode, self.work_area)
self.apply_actions(actions)
}
///|
pub fn MoonPlasmaCompositor::compose_frame(
self : MoonPlasmaCompositor,
) -> @renderer.Pixmap {
let scene = @scene.SceneGraph::new(self.output_w, self.output_h)
let order = self.wm.stacking_order()
let titles : Array[String] = []
for wid in order {
match self.windows.get(wid) {
Some(win) => {
let label = if win.title.length() > 0 { win.title } else { win.app_id }
titles.push(label)
let (insets, deco_ops) = @scene.build_breeze_decoration(
win.title,
win.app_id,
win.focused,
win.fullscreen,
win.geometry.w + 4,
win.geometry.h + 34,
self.theme,
)
scene.windows.push({
id: win.id,
title: win.title,
app_id: win.app_id,
focused: win.focused,
fullscreen: win.fullscreen,
geometry: win.geometry,
deco_insets: insets,
deco_ops,
surface: win.surface,
})
}
None => ()
}
}
let layout_label = match self.layout_mode {
@wm.LayoutMode::Tiling => "BSP Tiling"
@wm.LayoutMode::Floating => "Floating"
@wm.LayoutMode::Monocle => "Monocle"
}
let (bg_ops, panel_ops) = @scene.build_shell_ops(
self.output_w,
self.output_h,
self.theme,
layout_label,
titles,
)
scene.background_ops = bg_ops
scene.panel_ops = panel_ops
ignore(self.damage.take_damage(self.output_w, self.output_h))
@renderer.render_scene(scene)
}
+358
View File
@@ -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()
}
+10
View File
@@ -0,0 +1,10 @@
import {
"moonbitlang/core/builtin",
"moonplasma/desktop/lib/wm" @wm,
"moonplasma/desktop/compositor/sys" @sys,
"moonplasma/desktop/compositor/scene" @scene,
"moonplasma/desktop/compositor/renderer" @renderer,
"moonplasma/desktop/compositor/wayland" @wayland,
}
supported_targets = "-all+native"