Nested window backend: run the compositor in a window on the host Wayland session
CI / test (push) Successful in 26s
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:
1 parent
a265eebf02
commit
a1b85474f8
5 files changed
+453
-4
No files matched your search
@@ -6,6 +6,7 @@ fn main {
|
||||
let mut socket_path = "/tmp/wayland-moon-0"
|
||||
let mut theme_path = "config/themes/moon-night.json"
|
||||
let mut gfx_out = ""
|
||||
let mut windowed = false
|
||||
let width = 1280
|
||||
let height = 720
|
||||
|
||||
@@ -18,6 +19,9 @@ fn main {
|
||||
} else if a == "--theme" && i + 1 < args.length() {
|
||||
theme_path = args[i + 1]
|
||||
i = i + 2
|
||||
} else if a == "--window" {
|
||||
windowed = true
|
||||
i = i + 1
|
||||
} else if a == "--gfx-out" && i + 1 < args.length() {
|
||||
gfx_out = args[i + 1]
|
||||
i = i + 2
|
||||
@@ -53,11 +57,44 @@ fn main {
|
||||
println("wasi-gfx frame written to " + gfx_out)
|
||||
return
|
||||
}
|
||||
let nested = if windowed {
|
||||
let display = match @env.get_env_var("WAYLAND_DISPLAY") {
|
||||
Some(d) => d
|
||||
None => "wayland-0"
|
||||
}
|
||||
let rt = match @env.get_env_var("XDG_RUNTIME_DIR") {
|
||||
Some(d) => d
|
||||
None => "/tmp"
|
||||
}
|
||||
match @nested.NestedWindow::open(display, rt, "moon-plasma", "moon-plasma", width, height) {
|
||||
Some(n) => Some(n)
|
||||
None => {
|
||||
println("failed to open a window on the host Wayland display")
|
||||
comp.close()
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
println("moon-plasma compositor listening on " + socket_path + " (wasi-gfx surface active)")
|
||||
while true {
|
||||
comp.step()
|
||||
match nested {
|
||||
Some(n) => {
|
||||
if !n.poll(gfx_surface) {
|
||||
break
|
||||
}
|
||||
comp.handle_gfx_events(gfx_surface)
|
||||
if n.frame_ready() {
|
||||
n.present(comp.present_to_gfx(gfx_ctx))
|
||||
}
|
||||
}
|
||||
None => {
|
||||
comp.handle_gfx_events(gfx_surface)
|
||||
ignore(comp.present_to_gfx(gfx_ctx))
|
||||
}
|
||||
}
|
||||
@sys.sleep_ms(16)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
"moonplasma/desktop/compositor/renderer" @renderer,
|
||||
"moonplasma/desktop/compositor/gfx" @gfx,
|
||||
"moonplasma/desktop/compositor/host" @host,
|
||||
"moonplasma/desktop/compositor/nested" @nested,
|
||||
}
|
||||
|
||||
supported_targets = "-all+native"
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import {
|
||||
"moonbitlang/core/builtin",
|
||||
"moonplasma/desktop/compositor/sys" @sys,
|
||||
"moonplasma/desktop/compositor/renderer" @renderer,
|
||||
"moonplasma/desktop/compositor/wayland" @wayland,
|
||||
"moonplasma/desktop/compositor/gfx" @gfx,
|
||||
}
|
||||
|
||||
supported_targets = "-all+native"
|
||||
@@ -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
|
||||
}
|
||||
@@ -5,17 +5,23 @@ export PATH := env_var('HOME') + "/.moon/bin:" + env_var('PATH')
|
||||
|
||||
socket := env_var_or_default('XDG_RUNTIME_DIR', '/tmp') + "/wayland-moon-0"
|
||||
theme := "config/themes/moon-night.json"
|
||||
exe := "_build/native/debug/build/compositor/bin/bin.exe"
|
||||
exe := "_build/native/release/build/compositor/bin/bin.exe"
|
||||
debug_exe := "_build/native/debug/build/compositor/bin/bin.exe"
|
||||
|
||||
default:
|
||||
@just --list
|
||||
|
||||
# Build the native compositor.
|
||||
# Build the native compositor (release: the software rasteriser is slow in debug).
|
||||
build:
|
||||
moon build --target native --warn-list -a compositor/bin
|
||||
moon build --target native --release --warn-list -a compositor/bin
|
||||
|
||||
# Run the compositor (Ctrl-C to stop). Connect clients via `just client`.
|
||||
# Run the compositor in a window on your current Wayland session (close the window or Ctrl-C to stop). Connect clients via `just client`.
|
||||
run: build
|
||||
rm -f {{socket}}
|
||||
{{exe}} --window --socket {{socket}} --theme {{theme}}
|
||||
|
||||
# Run the compositor with no window (clients only; no output).
|
||||
run-headless: build
|
||||
rm -f {{socket}}
|
||||
{{exe}} --socket {{socket}} --theme {{theme}}
|
||||
|
||||
|
||||
Reference in new issue
Block a user