Files
chandra/compositor/sys/sys_native.mbt
T
Claude ba82d3c59b
CI / test (push) Failing after 15s
Idle desktop sleeps instead of redrawing; only changed parts of the screen are redrawn
Measured headless and nested at 1080p (host-wasm), against main:
- Idle: ~9.4% CPU per compositor -> ~0.35%. Idle frames cost ~1.3 ms, and the loop
  woke 60-125 times a second to find nothing had changed.
- One 800x600 floating client animating at 60 fps: 33% -> 16% CPU; raster per frame
  ~4.5 ms -> ~0.9 ms. A full-screen tiled client: 65% -> 54% (its cost is now mostly
  copying the 8 MB buffer out of the client's shared memory).

Changes:
- shell: the wallpaper and desktop card ops (~1900, the moon disc is drawn per pixel)
  are kept and reused until what they are drawn from changes (size, theme, wallpaper,
  card position, date, hour:minute). This was ~75% of an idle frame.
- FrameSig compares the background by identity first instead of op by op.
- Host tables (windows, layouts, ...) are only sent to the host when their bytes change.
- Main loop: a pass with no input, no changed frame and nothing waiting to be shown
  blocks in the new sys.wait-readable on the Wayland socket and clients (plus the
  nested host connection; the login session's seat, DRM and libinput fds are added by
  the host) until just before the wall clock's next second, then polls every 5 ms
  until it turns, so clocks still tick on time. Recording, pending screenshots and
  toasts keep the frame pace (ChandraCompositor::has_timed_work).
- Host raster: works out what changed from the last frame. When only windows changed
  (a client drew, a window moved, focus moved) it redraws just their footprint
  (decoration and shadow included, old and new place), copying back only that part
  of the cached backdrop; anything else still redraws everything. A test checks the
  partial redraw is pixel-identical to a full one for a client redraw, a move and a
  focus change with stacking swapped.

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

344 lines
8.9 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)
}
///|
#borrow(fds)
extern "C" fn c_wait_readable(fds : FixedArray[Int], n : Int, timeout_ms : Int) -> Int = "mp_wait_readable"
///| Block until one of `fds` is readable or `timeout_ms` passes (how an idle loop sleeps). The
/// number of ready fds, 0 on timeout.
pub fn wait_readable(fds : Array[Int], timeout_ms : Int) -> Int {
c_wait_readable(FixedArray::from_array(fds), fds.length(), 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 scaled copy of a host surface as an image handle; 0 (none) on the native build, which
/// scales the snapshot's own pixels instead (`@renderer.shm_thumbnail`).
pub fn surface_thumbnail(_id : UInt, _w : Int, _h : Int, _reuse : UInt) -> UInt {
0U
}
///| A memfd with the host framebuffer as ARGB8888; -1 on the native build.
pub fn frame_memfd() -> Int {
-1
}