Files
chandra/compositor/sys/sys_native.mbt
T
Claude 8f88d93d9f
CI / test (push) Failing after 16s
Apps drawing with Vulkan on wl_shm (Delta, Zed) show every frame
Delta is a GPUI app: it draws with Vulkan, and without linux-dmabuf Mesa's WSI
presents through wl_shm, making one wl_shm_pool per swapchain image. libwayland
flushes those create_pool requests with all their fds in one sendmsg (vkcube on
lavapipe sends 4), but both hosts kept only the first SCM_RIGHTS fd and closed
the rest. Every pool but the first had no memory, so only one swapchain image
ever reached the screen: stale, frozen or flickering windows.

- recvmsg keeps every fd (up to 32), in order: natively through recvmsg_fds,
  and in the wasm host through a new chandra:host/sys take-fd import.
- A commit consumes the attached buffer. A bare commit (GPUI commits after
  ack_configure) used to re-show and re-release the old buffer, which the
  client may already be drawing its next frame into.
- ARGB8888 client buffers are blended as premultiplied alpha, as wl_shm
  defines, in both rasterisers (translucent edges were darkened).
- wl_seat.name and wl_output.scale/done are only sent to clients that bound
  version 2 or later (vkcube binds v1 and aborted on them).

Regression test: a client flushes three pools with their fds in one message,
then commits a bare frame; it fails on the old code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hm8wb1UKxQxf8o6gpz
2026-11-25 19:09:18 +00:00

328 lines
8.2 KiB
MoonBit

///| Minimal POSIX OS primitives for the pure-MoonBit Wayland compositor.
///|
#borrow(path)
extern "C" fn c_unix_listen(path : Bytes) -> Int = "mp_unix_listen"
///|
extern "C" fn c_unix_accept(listen_fd : Int) -> Int = "mp_unix_accept"
///|
#borrow(path)
extern "C" fn c_unix_connect(path : Bytes) -> Int = "mp_unix_connect"
///|
extern "C" fn c_close_fd(fd : Int) -> Int = "mp_close_fd"
///|
#borrow(path)
extern "C" fn c_unlink(path : Bytes) -> Int = "mp_unlink"
///|
extern "C" fn c_poll_read(fd : Int, timeout_ms : Int) -> Int = "mp_poll_read"
///|
#borrow(buf, out_fd)
extern "C" fn c_recvmsg_fd(
fd : Int,
buf : FixedArray[Byte],
max_len : Int,
out_fd : FixedArray[Int],
) -> Int = "mp_recvmsg_fd"
///|
#borrow(buf, fds)
extern "C" fn c_sendmsg_fds(
fd : Int,
buf : Bytes,
len : Int,
fds : FixedArray[Int],
n_fds : Int,
) -> Int = "mp_sendmsg_fds"
///|
#borrow(buf)
extern "C" fn c_sendmsg_fd(
fd : Int,
buf : Bytes,
len : Int,
send_fd : Int,
) -> Int = "mp_sendmsg_fd"
///|
#borrow(name)
extern "C" fn c_memfd_create(name : Bytes, size : Int) -> Int = "mp_memfd_create"
///|
#borrow(buf)
extern "C" fn c_pwrite_bytes(
fd : Int,
buf : Bytes,
len : Int,
offset : Int,
) -> Int = "mp_pwrite_bytes"
///|
extern "C" fn c_mmap_read(fd : Int, size : Int) -> Bytes = "mp_mmap_read"
///|
#borrow(path)
extern "C" fn c_read_file(path : Bytes) -> Bytes = "mp_read_file"
///|
#borrow(path, buf)
extern "C" fn c_write_file(path : Bytes, buf : Bytes, len : Int) -> Int = "mp_write_file"
///|
extern "C" fn c_sleep_ms(ms : Int) -> Unit = "mp_sleep_ms"
///|
extern "C" fn c_getpid() -> Int = "mp_getpid"
///| Encode ASCII/UTF-8 String into Bytes for C paths/names.
pub fn str_to_utf8_bytes(s : String) -> Bytes {
let buf : Array[Byte] = []
for ch in s {
let code = ch.to_int()
if code < 0x80 {
buf.push(code.to_byte())
} else if code < 0x800 {
buf.push((0xC0 | (code >> 6)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
} else if code < 0x10000 {
buf.push((0xE0 | (code >> 12)).to_byte())
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
} else {
buf.push((0xF0 | (code >> 18)).to_byte())
buf.push((0x80 | ((code >> 12) & 0x3F)).to_byte())
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
buf.push((0x80 | (code & 0x3F)).to_byte())
}
}
Bytes::from_array(buf)
}
///|
pub fn unix_listen(path : String) -> Int {
c_unix_listen(str_to_utf8_bytes(path))
}
///|
pub fn unix_accept(listen_fd : Int) -> Int {
c_unix_accept(listen_fd)
}
///|
pub fn unix_connect(path : String) -> Int {
c_unix_connect(str_to_utf8_bytes(path))
}
///|
pub fn close_fd(fd : Int) -> Unit {
ignore(c_close_fd(fd))
}
///|
pub fn unlink(path : String) -> Unit {
ignore(c_unlink(str_to_utf8_bytes(path)))
}
///|
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
c_poll_read(fd, timeout_ms)
}
///| Receive bytes and every SCM_RIGHTS fd that came with them, in order. Returns
/// (bytes_read, fds); bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
pub fn recvmsg_fds(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Array[Int]) {
let out_fd : FixedArray[Int] = FixedArray::make(33, -1)
let n = c_recvmsg_fd(fd, buf, max_len, out_fd)
let fds : Array[Int] = []
for i = 0; i < out_fd[0]; i = i + 1 {
fds.push(out_fd[1 + i])
}
(n, fds)
}
///| Receive bytes and an optional SCM_RIGHTS fd. Returns (bytes_read, received_fd); any further
/// fds are closed. bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF.
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
let (n, fds) = recvmsg_fds(fd, buf, max_len)
for i = 1; i < fds.length(); i = i + 1 {
close_fd(fds[i])
}
(n, if fds.length() > 0 { fds[0] } else { -1 })
}
///|
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
c_sendmsg_fd(fd, buf, buf.length(), send_fd)
}
///| Send `buf` with several fds in one SCM_RIGHTS message, the way libwayland flushes (tests).
pub fn sendmsg_fds(fd : Int, buf : Bytes, fds : Array[Int]) -> Int {
c_sendmsg_fds(fd, buf, buf.length(), FixedArray::from_array(fds), fds.length())
}
///|
pub fn memfd_create(name : String, size : Int) -> Int {
c_memfd_create(str_to_utf8_bytes(name), size)
}
///| Write as much of `buf` as a pipe takes without blocking (the fd is made non-blocking):
/// the byte count, 0 when the pipe is full, -1 on an error (the reader went away).
pub fn write_nonblock(fd : Int, buf : Bytes) -> Int {
c_pwrite_bytes(fd, buf, buf.length(), -1)
}
///|
pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
c_pwrite_bytes(fd, buf, buf.length(), offset)
}
///|
pub fn mmap_read_at(fd : Int, offset : Int, len : Int) -> Bytes {
let raw = c_mmap_read(fd, offset + len)
if raw.length() < offset + len {
return Bytes::make(0, b'\x00')
}
if offset == 0 && raw.length() == len {
raw
} else {
raw[offset:offset + len].to_bytes()
}
}
///|
pub fn mmap_read(fd : Int, size : Int) -> Bytes {
c_mmap_read(fd, size)
}
///|
pub fn read_file_bytes(path : String) -> Bytes {
c_read_file(str_to_utf8_bytes(path))
}
///|
pub fn write_file_bytes(path : String, data : Bytes) -> Int {
c_write_file(str_to_utf8_bytes(path), data, data.length())
}
///|
pub fn sleep_ms(ms : Int) -> Unit {
c_sleep_ms(ms)
}
///|
pub fn getpid() -> Int {
c_getpid()
}
///|
extern "C" fn c_wall_time_field(which : Int) -> Int = "mp_wall_time_field"
///| Local wall clock: 0 year, 1 month (1-12), 2 day, 3 hour, 4 minute, 5 second, 6 weekday (0 = Sunday).
pub fn wall_time_field(which : Int) -> Int {
c_wall_time_field(which)
}
///|
extern "C" fn c_has_exe(name : Bytes) -> Int = "mp_has_exe"
///|
extern "C" fn c_spawn(cmd : Bytes, display : Bytes) -> Int = "mp_spawn"
///| Whether `name` is an executable on PATH.
pub fn has_exe(name : String) -> Bool {
c_has_exe(str_to_utf8_bytes(name)) != 0
}
///| Launch `cmd` detached with WAYLAND_DISPLAY set to `display`.
pub fn spawn(cmd : String, display : String) -> Bool {
c_spawn(str_to_utf8_bytes(cmd), str_to_utf8_bytes(display)) == 0
}
///|
extern "C" fn c_list_desktop(dir : Bytes) -> Bytes = "mp_list_desktop"
///|
extern "C" fn c_getenv(name : Bytes) -> Bytes = "mp_getenv"
///| Newline-separated names of the `*.desktop` files directly in `dir`.
pub fn list_desktop_files(dir : String) -> Array[String] {
let raw = c_list_desktop(str_to_utf8_bytes(dir))
let out : Array[String] = []
let sb = StringBuilder::new()
let mut i = 0
while i < raw.length() {
if raw[i] == b'\n' {
out.push(sb.to_string())
sb.reset()
} else {
sb.write_char(Int::unsafe_to_char(raw[i].to_int()))
}
i = i + 1
}
out
}
///| Environment variable, or "" when unset.
pub fn getenv(name : String) -> String {
let raw = c_getenv(str_to_utf8_bytes(name))
let sb = StringBuilder::new()
for i = 0; i < raw.length(); i = i + 1 {
sb.write_char(Int::unsafe_to_char(raw[i].to_int()))
}
sb.to_string()
}
///|
extern "C" fn c_mkdir_p(path : Bytes) -> Int = "mp_mkdir_p"
///| Create `path` and any missing parents; true when it exists afterwards.
pub fn mkdir_p(path : String) -> Bool {
c_mkdir_p(str_to_utf8_bytes(path)) == 0
}
///|
#borrow(path)
extern "C" fn c_read_proc(path : Bytes) -> Bytes = "mp_read_proc"
///| Contents of a small /proc or /sys file ("" when unreadable). Unlike `read_file_bytes`
/// this works on files that report size 0.
pub fn read_proc_bytes(path : String) -> Bytes {
c_read_proc(str_to_utf8_bytes(path))
}
///| Milliseconds on a monotonic clock.
pub fn monotonic_ms() -> UInt64 {
@env.now()
}
///| Print a line to stderr (stdout on native).
pub fn log(message : String) -> Unit {
println(message)
}
///| Whether the host draws the screen (the wasm compositor): then client buffers are captured in
/// the host and the pixels never enter this program. Never on the native build.
pub fn host_raster() -> Bool {
false
}
///| Copy a client buffer into a host surface; 0 (none) on the native build.
pub fn surface_capture(_fd : Int, _offset : Int, _stride : Int, _width : Int, _height : Int, _format : Int) -> UInt {
0U
}
///|
pub fn surface_release(_id : UInt) -> Unit {
()
}
///| A memfd with the host framebuffer as ARGB8888; -1 on the native build.
pub fn frame_memfd() -> Int {
-1
}