Files
chandra/compositor/sys/sys_wasm.mbt
T
Claude 85f6ff42f4
CI / test (push) Failing after 16s
Screenshot module after Spectacle (Super+Shift+S), with a screen recorder
- Super+Shift+S (or Super+Shift+Print) freezes the screen under a selection overlay, like
  Spectacle's Rectangular Region: drag a rectangle (size readout under it, magnifier while
  drawing), drag inside to move it, drag its handles to resize, or click a window to select it.
  Enter or a double-click takes it, Escape or right-click cancels; F full screen, W active window,
  arrows nudge (Shift+arrows resize). Print takes the full screen, Super+Print the active window,
  Super+Ctrl+Print the window under the pointer.
- Saved as ~/Pictures/Screenshots/Screenshot_YYYYMMDD_HHMMSS.png (XDG_PICTURES_DIR honoured),
  put on the clipboard as image/png (compositor-owned wl_data selection, written to the paste
  pipe without blocking), and confirmed by a notification with a thumbnail.
- Screen recording: Super+Shift+R picks a region on the same overlay, Super+Alt+R the screen,
  Super+Ctrl+R a window; the same shortcut or the REC pill (top centre) stops. Raw frames
  (<= 30 fps, wall-clock stamped) stream into ffmpeg over a unix socket; it writes H.264 MP4 to
  ~/Videos/Screencasts/Screencast_*.mp4. The pointer is drawn in unless switched off.
- renderer: encode_png_compressed (per-row filters + LZ77/fixed-Huffman deflate): a 1280x720
  desktop is ~90 KB instead of 3.7 MB.
- New 'screenshot' capability and 'screenshot' panel module (in the default panel): Spectacle's
  modes and the recording modes with their shortcuts, and a pointer switch; the button becomes
  Stop while recording. Served by both hosts (wasmer stub request 12 / host int 4; host-wasm
  publish-screenshot and a screenshot request). Panel requests wait for a frame without the
  module's popup.
- sys: pwrite with a negative offset is a non-blocking write (pipes, sockets), SIGPIPE ignored.
- Tests: native overlay, shortcuts, window pick, module, recording (with ffmpeg); host-wasm
  module test; renderer compression test.

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

203 lines
5.5 KiB
MoonBit

///| The compositor as a wasm component: the same API as sys_native.mbt, with every operation
/// forwarded to the host through the `chandra:host/sys` imports. The component holds only
/// integer handles (fds) and bytes; the host owns the real descriptors.
///| Encode a String into UTF-8 Bytes.
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)
}
///|
fn to_fixed(b : Bytes) -> FixedArray[Byte] {
let out : FixedArray[Byte] = FixedArray::make(b.length(), b'\x00')
for i = 0; i < b.length(); i = i + 1 {
out[i] = b[i]
}
out
}
///|
fn from_fixed(a : FixedArray[Byte]) -> Bytes {
Bytes::from_fixedarray(a)
}
///|
pub fn unix_listen(path : String) -> Int {
@hostsys.unix_listen(path)
}
///|
pub fn unix_accept(listen_fd : Int) -> Int {
@hostsys.unix_accept(listen_fd)
}
///|
pub fn unix_connect(path : String) -> Int {
@hostsys.unix_connect(path)
}
///|
pub fn close_fd(fd : Int) -> Unit {
@hostsys.close_fd(fd)
}
///|
pub fn unlink(path : String) -> Unit {
@hostsys.unlink(path)
}
///|
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
@hostsys.poll_read(fd, timeout_ms)
}
///| Receive bytes and an optional SCM_RIGHTS fd into `buf`. Returns (bytes_read, received_fd);
/// bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
let r = @hostsys.recvmsg(fd, max_len.reinterpret_as_uint())
if r.count > 0 {
let n = if r.count < r.data.length() { r.count } else { r.data.length() }
for i = 0; i < n && i < buf.length(); i = i + 1 {
buf[i] = r.data[i]
}
}
(r.count, r.fd)
}
///|
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
@hostsys.sendmsg(fd, to_fixed(buf), send_fd)
}
///|
pub fn memfd_create(name : String, size : Int) -> Int {
@hostsys.memfd_create(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 {
@hostsys.pwrite(fd, to_fixed(buf), -1)
}
///|
pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
@hostsys.pwrite(fd, to_fixed(buf), offset)
}
///|
pub fn mmap_read(fd : Int, size : Int) -> Bytes {
from_fixed(@hostsys.mmap_read(fd, size))
}
///| `len` bytes of the file behind `fd` starting at `offset` (a client buffer within its pool).
pub fn mmap_read_at(fd : Int, offset : Int, len : Int) -> Bytes {
from_fixed(@hostsys.mmap_read_at(fd, offset, len))
}
///|
pub fn read_file_bytes(path : String) -> Bytes {
from_fixed(@hostsys.read_file(path))
}
///|
pub fn write_file_bytes(path : String, data : Bytes) -> Int {
@hostsys.write_file(path, to_fixed(data))
}
///|
pub fn sleep_ms(ms : Int) -> Unit {
@hostsys.sleep_ms(ms)
}
///|
pub fn getpid() -> Int {
@hostsys.getpid()
}
///| 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 {
@hostsys.wall_time_field(which)
}
///| Whether `name` is an executable on PATH.
pub fn has_exe(name : String) -> Bool {
@hostsys.has_exe(name)
}
///| Launch `cmd` detached with WAYLAND_DISPLAY set to `display`.
pub fn spawn(cmd : String, display : String) -> Bool {
@hostsys.spawn(cmd, display)
}
///| Names of the `*.desktop` files directly in `dir`.
pub fn list_desktop_files(dir : String) -> Array[String] {
@hostsys.list_desktop_files(dir)
}
///| Environment variable, or "" when unset.
pub fn getenv(name : String) -> String {
@hostsys.getenv(name)
}
///| Create `path` and any missing parents; true when it exists afterwards.
pub fn mkdir_p(path : String) -> Bool {
@hostsys.mkdir_p(path)
}
///| Contents of a small /proc or /sys file ("" when unreadable).
pub fn read_proc_bytes(path : String) -> Bytes {
from_fixed(@hostsys.read_proc(path))
}
///| Milliseconds on the host's monotonic clock.
pub fn monotonic_ms() -> UInt64 {
@hostsys.monotonic_ms()
}
///| Print a line to the host's stderr.
pub fn log(message : String) -> Unit {
@hostsys.log(message)
}
///| The host draws the screen: client buffers are captured in the host (see `surface_capture`).
pub fn host_raster() -> Bool {
true
}
///| Copy `stride * height` bytes at `offset` of the file behind `fd` into a host surface (a native
/// copy; the pixels never enter this program). `format`: 0 ARGB8888, 1 XRGB8888. 0 on failure.
pub fn surface_capture(fd : Int, offset : Int, stride : Int, width : Int, height : Int, format : Int) -> UInt {
@hostraster.capture(fd, offset, stride, width, height, format)
}
///| Forget a captured surface.
pub fn surface_release(id : UInt) -> Unit {
@hostraster.release(id)
}
///| A new memfd holding the host framebuffer as little-endian ARGB8888, for a wl_shm pool. The
/// caller closes it. -1 on failure.
pub fn frame_memfd() -> Int {
@hostraster.export_memfd()
}