MoonBit host for the compositor component on prebuilt libwasmtime (sys, font, raster)
CI / test (push) Failing after 17s
CI / test (push) Failing after 17s
wasmhost/ runs build/compositor.wasm with wasmtime linked as the upstream C API library (fetched by setup-toolchain.sh) instead of compiling it: a cold build is about 4 s against about 2 min for host-wasm. sys and font reuse the native compositor's packages; the rasteriser is a port of host-wasm/src/raster.rs with the pixel work in C, SVG through nanosvg. Panel modules, --drm and --window come next; host-wasm stays the working host until then. wit: sys.append-file. The C API moves list<u8> as one dynamic value per byte and wasmtime caps what one call may copy, so the component writes files over 1 MiB as write-file plus appends (host-wasm implements it too). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB
This commit is contained in:
26 files changed
+7592
-2
No files matched your search
@@ -0,0 +1,37 @@
|
||||
/* Small pieces of the MoonBit host that need C. */
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "moonbit.h"
|
||||
|
||||
/* A line on stderr (the compositor's log). */
|
||||
void wh_eprint(moonbit_bytes_t s) {
|
||||
fwrite(s, 1, (size_t)Moonbit_array_length(s), stderr);
|
||||
fputc('\n', stderr);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
/* No zombies from launched apps. */
|
||||
void wh_ignore_sigchld(void) { signal(SIGCHLD, SIG_IGN); }
|
||||
|
||||
void wh_exit(int32_t code) {
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
exit(code);
|
||||
}
|
||||
|
||||
/* Append `data` to the file at `path` (created if missing): 0, or -1 on error. */
|
||||
int32_t wh_append_file(moonbit_bytes_t path, moonbit_bytes_t data) {
|
||||
char p[4096];
|
||||
size_t n = (size_t)Moonbit_array_length(path);
|
||||
if (n >= sizeof p) return -1;
|
||||
memcpy(p, path, n);
|
||||
p[n] = 0;
|
||||
FILE *f = fopen(p, "ab");
|
||||
if (!f) return -1;
|
||||
size_t len = (size_t)Moonbit_array_length(data);
|
||||
int ok = fwrite(data, 1, len, f) == len;
|
||||
return fclose(f) == 0 && ok ? 0 : -1;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
///| The MoonBit host for the compositor component (build/compositor.wasm).
|
||||
///
|
||||
/// It runs the component with wasmtime, linked as the prebuilt C API library, and implements the
|
||||
/// interfaces the component imports (wit/host): sys, font, raster, display and plasmoids. Most of
|
||||
/// it is the native compositor's own packages (compositor/sys, font, kms, wasm, renderer), so the
|
||||
/// native and wasm builds share one implementation of each.
|
||||
///
|
||||
/// wasmhost build/compositor.wasm --socket PATH [--window | --drm | --gfx-out PNG [--serve]]
|
||||
|
||||
///|
|
||||
#borrow(s)
|
||||
extern "C" fn c_eprint(s : Bytes) = "wh_eprint"
|
||||
|
||||
///|
|
||||
extern "C" fn c_ignore_sigchld() = "wh_ignore_sigchld"
|
||||
|
||||
///|
|
||||
extern "C" fn c_exit(code : Int) = "wh_exit"
|
||||
|
||||
///|
|
||||
fn exit(code : Int) -> Unit {
|
||||
c_exit(code)
|
||||
}
|
||||
|
||||
///|
|
||||
fn eprint(s : String) -> Unit {
|
||||
c_eprint(@utf8.encode(s))
|
||||
}
|
||||
|
||||
///| Every host function, by interface, in registration order (its id is its index).
|
||||
let handlers : Array[(Array[@wt.Val]) -> Array[@wt.Val]] = []
|
||||
|
||||
///| "interface.function" of each handler, for WASMHOST_TRACE.
|
||||
let handler_names : Array[String] = []
|
||||
|
||||
///|
|
||||
fn register(
|
||||
iface : String,
|
||||
funcs : Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])],
|
||||
) -> Unit {
|
||||
let first = handlers.length()
|
||||
for f in funcs {
|
||||
handlers.push(f.1)
|
||||
handler_names.push(iface + "." + f.0)
|
||||
}
|
||||
match @wt.define_funcs(iface, funcs.map(f => f.0), first) {
|
||||
Ok(_) => ()
|
||||
Err(e) => {
|
||||
eprint("error: " + e)
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn main {
|
||||
let argv = @env.args()
|
||||
let args : Array[String] = if argv.length() > 1 {
|
||||
argv[1:].to_array()
|
||||
} else {
|
||||
[]
|
||||
}
|
||||
// First argument may be the component path; otherwise use the default build location.
|
||||
let path = if args.length() > 0 && args[0].has_suffix(".wasm") {
|
||||
args.remove(0)
|
||||
} else {
|
||||
"build/compositor.wasm"
|
||||
}
|
||||
if args.length() > 0 && args[0] == "--" {
|
||||
args.remove(0) |> ignore
|
||||
}
|
||||
c_ignore_sigchld()
|
||||
match @wt.load(path) {
|
||||
Ok(_) => ()
|
||||
Err(e) => {
|
||||
eprint(
|
||||
"error: cannot load \{path}: \{e}\n build it with `just compositor-wasm`",
|
||||
)
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
if @sys.getenv("WASMHOST_TRACE") != "" {
|
||||
@wt.set_handler(fn(id, a) {
|
||||
eprint("> " + handler_names[id] + " " + a.length().to_string())
|
||||
handlers[id](a)
|
||||
})
|
||||
} else {
|
||||
@wt.set_handler(fn(id, a) { handlers[id](a) })
|
||||
}
|
||||
register("chandra:host/sys@0.1.0", sys_funcs())
|
||||
register("chandra:host/font@0.1.0", font_funcs())
|
||||
register("chandra:host/raster@0.1.0", raster_funcs())
|
||||
register("chandra:host/display@0.1.0", display_funcs())
|
||||
register("chandra:host/plasmoids@0.1.0", plasmoid_funcs())
|
||||
match @wt.instantiate() {
|
||||
Ok(_) => ()
|
||||
Err(e) => {
|
||||
eprint("error: " + e)
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
match @wt.call("", "run", [@wt.Val::strings(args)]) {
|
||||
Ok(r) => exit(if r.length() > 0 { r[0].to_int() } else { 0 })
|
||||
Err(e) => {
|
||||
eprint("error: " + e)
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import {
|
||||
"moonbitlang/core/builtin",
|
||||
"moonbitlang/core/env" @env,
|
||||
"moonbitlang/core/encoding/utf8" @utf8,
|
||||
"moonbitlang/core/cmp" @cmp,
|
||||
"chandra/desktop/compositor/sys" @sys,
|
||||
"chandra/desktop/compositor/font" @font,
|
||||
"chandra/desktop/compositor/renderer" @renderer,
|
||||
"chandra/desktop/wasmhost/wt" @wt,
|
||||
}
|
||||
|
||||
supported_targets = "-all+native"
|
||||
|
||||
pkgtype(kind: "executable")
|
||||
|
||||
// libwasmtime comes prebuilt (scripts/setup-toolchain.sh); scripts/build-wasmhost.sh puts it on
|
||||
// the compiler's and linker's search paths. Linked statically: the binary needs nothing at runtime.
|
||||
options(
|
||||
"native-stub": [ "host_stub.c", "raster_stub.c" ],
|
||||
link: {
|
||||
"native": {
|
||||
"cc-link-flags": "-l:libwasmtime.a -lpthread -ldl -lm",
|
||||
},
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,774 @@
|
||||
///| `chandra:host/raster` and `chandra:host/font`: the compositor sends draw operations and window
|
||||
/// surface ids; the pixels live here (raster_stub.c) and never cross the component boundary.
|
||||
/// What to draw where (layers, the wallpaper cache, damage) is decided here, as in
|
||||
/// host-wasm/src/raster.rs.
|
||||
|
||||
///|
|
||||
extern "C" fn rs_resize(w : Int, h : Int) -> Int = "rs_resize"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_clear() = "rs_clear"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_set_clip(on : Int, x : Int, y : Int, w : Int, h : Int) = "rs_set_clip"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_fill_rect(
|
||||
x : Int,
|
||||
y : Int,
|
||||
w : Int,
|
||||
h : Int,
|
||||
r : Int,
|
||||
g : Int,
|
||||
b : Int,
|
||||
a : Int,
|
||||
radius : Int,
|
||||
) = "rs_fill_rect"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_stroke_rect(
|
||||
x : Int,
|
||||
y : Int,
|
||||
w : Int,
|
||||
h : Int,
|
||||
r : Int,
|
||||
g : Int,
|
||||
b : Int,
|
||||
a : Int,
|
||||
width : Int,
|
||||
radius : Int,
|
||||
) = "rs_stroke_rect"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_line(
|
||||
x0 : Int,
|
||||
y0 : Int,
|
||||
x1 : Int,
|
||||
y1 : Int,
|
||||
r : Int,
|
||||
g : Int,
|
||||
b : Int,
|
||||
a : Int,
|
||||
width : Int,
|
||||
) = "rs_line"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_circle(
|
||||
cx : Int,
|
||||
cy : Int,
|
||||
radius : Int,
|
||||
r : Int,
|
||||
g : Int,
|
||||
b : Int,
|
||||
a : Int,
|
||||
filled : Int,
|
||||
) = "rs_circle"
|
||||
|
||||
///| compositor/font's shim (stb_truetype); 0 on success.
|
||||
#borrow(path)
|
||||
extern "C" fn mp_font_load(slot : Int, path : Bytes) -> Int = "mp_font_load"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_font_loaded(slot : Int) = "rs_font_loaded"
|
||||
|
||||
///| Load the TTF file at `path` into `slot` (0..3).
|
||||
fn font_load(slot : Int, path : String) -> Bool {
|
||||
if slot < 0 || slot >= 4 {
|
||||
return false
|
||||
}
|
||||
if mp_font_load(slot, @utf8.encode(path)) != 0 {
|
||||
return false
|
||||
}
|
||||
rs_font_loaded(slot)
|
||||
true
|
||||
}
|
||||
|
||||
///|
|
||||
extern "C" fn rs_font_has(slot : Int) -> Int = "rs_font_has"
|
||||
|
||||
///|
|
||||
#borrow(info)
|
||||
extern "C" fn rs_glyph_info(
|
||||
slot : Int,
|
||||
cp : Int,
|
||||
size : Int,
|
||||
info : FixedArray[Int],
|
||||
) = "rs_glyph_info"
|
||||
|
||||
///|
|
||||
#borrow(out)
|
||||
extern "C" fn rs_glyph_coverage(
|
||||
slot : Int,
|
||||
cp : Int,
|
||||
size : Int,
|
||||
out : FixedArray[Byte],
|
||||
) = "rs_glyph_coverage"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_draw_glyph(
|
||||
slot : Int,
|
||||
cp : Int,
|
||||
size : Int,
|
||||
pen : Int,
|
||||
baseline : Int,
|
||||
r : Int,
|
||||
g : Int,
|
||||
b : Int,
|
||||
a : Int,
|
||||
embolden : Int,
|
||||
) = "rs_draw_glyph"
|
||||
|
||||
///|
|
||||
#borrow(rgba)
|
||||
extern "C" fn rs_image_put(
|
||||
reuse : Int,
|
||||
w : Int,
|
||||
h : Int,
|
||||
rgba : FixedArray[Byte],
|
||||
) -> Int = "rs_image_put"
|
||||
|
||||
///|
|
||||
#borrow(data)
|
||||
extern "C" fn rs_image_svg(data : Bytes) -> Int = "rs_image_svg"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_draw_image(handle : Int, x : Int, y : Int, w : Int, h : Int) = "rs_draw_image"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_capture(
|
||||
fd : Int,
|
||||
offset : Int,
|
||||
stride : Int,
|
||||
w : Int,
|
||||
h : Int,
|
||||
format : Int,
|
||||
) -> UInt = "rs_capture"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_release(id : UInt) = "rs_release"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_thumbnail(id : UInt, w : Int, h : Int, reuse : Int) -> Int = "rs_thumbnail"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_blit_surface(id : UInt, x : Int, y : Int, w : Int, h : Int) -> Int = "rs_blit_surface"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_save_backdrop() = "rs_save_backdrop"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_has_backdrop() -> Int = "rs_has_backdrop"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_copy_backdrop() = "rs_copy_backdrop"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_width() -> Int = "rs_width"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_height() -> Int = "rs_height"
|
||||
|
||||
///|
|
||||
#borrow(out)
|
||||
extern "C" fn rs_read_frame(out : FixedArray[Byte]) = "rs_read_frame"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_export_memfd() -> Int = "rs_export_memfd"
|
||||
|
||||
// ---------------------------------------------------------------- geometry
|
||||
|
||||
///|
|
||||
struct R {
|
||||
x : Int
|
||||
y : Int
|
||||
w : Int
|
||||
h : Int
|
||||
} derive(Eq, Show)
|
||||
|
||||
///|
|
||||
fn R::empty(self : R) -> Bool {
|
||||
self.w <= 0 || self.h <= 0
|
||||
}
|
||||
|
||||
///|
|
||||
fn rect_of(v : @wt.Val) -> R {
|
||||
{
|
||||
x: v.field("x").to_int(),
|
||||
y: v.field("y").to_int(),
|
||||
w: v.field("w").to_int(),
|
||||
h: v.field("h").to_int(),
|
||||
}
|
||||
}
|
||||
|
||||
///| The overlap of two clips (None: no clip).
|
||||
fn intersect(a : R?, b : R?) -> R? {
|
||||
match (a, b) {
|
||||
(None, k) | (k, None) => k
|
||||
(Some(a), Some(b)) => {
|
||||
let x0 = @cmp.maximum(a.x, b.x)
|
||||
let y0 = @cmp.maximum(a.y, b.y)
|
||||
let x1 = @cmp.minimum(a.x + a.w, b.x + b.w)
|
||||
let y1 = @cmp.minimum(a.y + a.h, b.y + b.h)
|
||||
Some({
|
||||
x: x0,
|
||||
y: y0,
|
||||
w: @cmp.maximum(x1 - x0, 0),
|
||||
h: @cmp.maximum(y1 - y0, 0),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| The bounding box of two rectangles (an empty one counts for nothing).
|
||||
fn union(a : R, b : R) -> R {
|
||||
if a.empty() {
|
||||
return b
|
||||
}
|
||||
if b.empty() {
|
||||
return a
|
||||
}
|
||||
let x0 = @cmp.minimum(a.x, b.x)
|
||||
let y0 = @cmp.minimum(a.y, b.y)
|
||||
let x1 = @cmp.maximum(a.x + a.w, b.x + b.w)
|
||||
let y1 = @cmp.maximum(a.y + a.h, b.y + b.h)
|
||||
{ x: x0, y: y0, w: x1 - x0, h: y1 - y0 }
|
||||
}
|
||||
|
||||
///|
|
||||
fn set_clip(k : R?) -> Unit {
|
||||
match k {
|
||||
Some(r) => rs_set_clip(1, r.x, r.y, r.w, r.h)
|
||||
None => rs_set_clip(0, 0, 0, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- text
|
||||
|
||||
///| The font slot for (bold, mono), and whether its glyphs must be emboldened (a face with no
|
||||
/// bold instance gets drawn a pixel wider). Slots: 0 mono, 1 mono bold, 2 sans. None when no
|
||||
/// face is loaded at all.
|
||||
fn pick_face(bold : Bool, mono : Bool) -> (Int, Bool)? {
|
||||
let has_mono = rs_font_has(0) != 0
|
||||
let has_sans = rs_font_has(2) != 0
|
||||
if !has_mono && !has_sans {
|
||||
return None
|
||||
}
|
||||
let has_bold = has_mono && rs_font_has(1) != 0
|
||||
Some(
|
||||
if mono && has_mono {
|
||||
if bold && has_bold {
|
||||
(1, false)
|
||||
} else {
|
||||
(0, bold)
|
||||
}
|
||||
} else if has_sans {
|
||||
(2, bold)
|
||||
} else {
|
||||
(0, bold)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
let glyph_info : FixedArray[Int] = FixedArray::make(5, 0)
|
||||
|
||||
///| Advance of `cp` in `slot`.
|
||||
fn advance(slot : Int, cp : Int, size : Int) -> Int {
|
||||
rs_glyph_info(slot, cp, size, glyph_info)
|
||||
glyph_info[4]
|
||||
}
|
||||
|
||||
///|
|
||||
fn draw_text(l : @wt.Val, ox : Int, oy : Int) -> Unit {
|
||||
let text = l.field("text").to_string()
|
||||
let col = l.field("color")
|
||||
let ca = col.field("a").to_int()
|
||||
if text == "" || ca == 0 {
|
||||
return
|
||||
}
|
||||
let size = @cmp.maximum(l.field("size").to_int(), 0)
|
||||
let bold = l.field("bold").to_bool()
|
||||
let mono = l.field("mono").to_bool()
|
||||
let (slot, embolden) = match pick_face(bold, mono) {
|
||||
Some(f) => f
|
||||
None => return // no face loaded: nothing to draw with
|
||||
}
|
||||
let extra = if embolden { 1 } else { 0 }
|
||||
let cps : Array[Int] = []
|
||||
let mut total_w = 0
|
||||
for ch in text {
|
||||
let cp = ch.to_int()
|
||||
cps.push(cp)
|
||||
total_w += advance(slot, cp, size) + extra
|
||||
}
|
||||
let x = l.field("x").to_int() + ox
|
||||
let start_x = match l.field("align").to_int() {
|
||||
1 => x - total_w / 2
|
||||
2 => x - total_w
|
||||
_ => x
|
||||
}
|
||||
// `y` is the vertical centre of the text; centre the capital height ('H') on it.
|
||||
let (cap_slot, _) = match pick_face(false, mono) {
|
||||
Some(f) => f
|
||||
None => return
|
||||
}
|
||||
rs_glyph_info(cap_slot, 72, size, glyph_info)
|
||||
let baseline = l.field("y").to_int() + oy + -glyph_info[3] / 2
|
||||
let r = col.field("r").to_int()
|
||||
let g = col.field("g").to_int()
|
||||
let b = col.field("b").to_int()
|
||||
let mut pen = start_x
|
||||
for cp in cps {
|
||||
rs_draw_glyph(slot, cp, size, pen, baseline, r, g, b, ca, extra)
|
||||
pen += advance(slot, cp, size) + extra
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- ops
|
||||
|
||||
///|
|
||||
fn rgba(c : @wt.Val) -> (Int, Int, Int, Int) {
|
||||
(
|
||||
c.field("r").to_int(),
|
||||
c.field("g").to_int(),
|
||||
c.field("b").to_int(),
|
||||
c.field("a").to_int(),
|
||||
)
|
||||
}
|
||||
|
||||
///| Draw `ops` offset by (ox, oy). `base` limits everything (the frame's damaged area); a
|
||||
/// push-clip op replaces the clip within it.
|
||||
fn draw_ops(ops : Array[@wt.Val], ox : Int, oy : Int, base : R?) -> Unit {
|
||||
let stack : Array[R] = []
|
||||
set_clip(base)
|
||||
for op in ops {
|
||||
guard op is Variant(name, payload)
|
||||
match (name, payload) {
|
||||
("fill-rect", Some(f)) => {
|
||||
let a = rect_of(f.field("area"))
|
||||
let (r, g, b, al) = rgba(f.field("color"))
|
||||
rs_fill_rect(
|
||||
a.x + ox,
|
||||
a.y + oy,
|
||||
a.w,
|
||||
a.h,
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
al,
|
||||
f.field("radius").to_int(),
|
||||
)
|
||||
}
|
||||
("stroke-rect", Some(s)) => {
|
||||
let a = rect_of(s.field("area"))
|
||||
let (r, g, b, al) = rgba(s.field("color"))
|
||||
rs_stroke_rect(
|
||||
a.x + ox,
|
||||
a.y + oy,
|
||||
a.w,
|
||||
a.h,
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
al,
|
||||
s.field("width").to_int(),
|
||||
s.field("radius").to_int(),
|
||||
)
|
||||
}
|
||||
("draw-line", Some(s)) => {
|
||||
let (r, g, b, al) = rgba(s.field("color"))
|
||||
rs_line(
|
||||
s.field("x0").to_int() + ox,
|
||||
s.field("y0").to_int() + oy,
|
||||
s.field("x1").to_int() + ox,
|
||||
s.field("y1").to_int() + oy,
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
al,
|
||||
s.field("width").to_int(),
|
||||
)
|
||||
}
|
||||
("draw-circle", Some(d)) => {
|
||||
let (r, g, b, al) = rgba(d.field("color"))
|
||||
rs_circle(
|
||||
d.field("cx").to_int() + ox,
|
||||
d.field("cy").to_int() + oy,
|
||||
d.field("radius").to_int(),
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
al,
|
||||
if d.field("filled").to_bool() {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
},
|
||||
)
|
||||
}
|
||||
("draw-text", Some(l)) => draw_text(l, ox, oy)
|
||||
("push-clip", Some(k)) => {
|
||||
let k = rect_of(k)
|
||||
stack.push({ x: k.x + ox, y: k.y + oy, w: k.w, h: k.h })
|
||||
set_clip(intersect(Some(stack[stack.length() - 1]), base))
|
||||
}
|
||||
("pop-clip", _) => {
|
||||
stack.pop() |> ignore
|
||||
set_clip(
|
||||
if stack.length() > 0 {
|
||||
intersect(Some(stack[stack.length() - 1]), base)
|
||||
} else {
|
||||
base
|
||||
},
|
||||
)
|
||||
}
|
||||
("draw-image", Some(p)) => {
|
||||
let a = rect_of(p.field("area"))
|
||||
rs_draw_image(p.field("image").to_int(), a.x + ox, a.y + oy, a.w, a.h)
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- frames
|
||||
|
||||
///| A frame as the compositor sent it (the parts compared to find what changed).
|
||||
struct Frame {
|
||||
width : Int
|
||||
height : Int
|
||||
wallpaper : @wt.Val? // Some(ops) when sent with this frame
|
||||
background : @wt.Val
|
||||
windows : Array[@wt.Val]
|
||||
panel : @wt.Val
|
||||
popups : @wt.Val
|
||||
}
|
||||
|
||||
///| The last frame drawn, and the wallpaper ops the cached backdrop was drawn from.
|
||||
let prev_frame : Ref[Frame?] = Ref(None)
|
||||
|
||||
///|
|
||||
let backdrop_ops : Ref[@wt.Val?] = Ref(None)
|
||||
|
||||
///| Room around a window for its drop shadow (4 px right, 6 px down) and a little slack.
|
||||
const SHADOW_PAD : Int = 8
|
||||
|
||||
///| What a window covers on screen: its decoration and drop shadow, or just itself when fullscreen.
|
||||
fn window_footprint(win : @wt.Val) -> R {
|
||||
let g = rect_of(win.field("geometry"))
|
||||
if win.field("fullscreen").to_bool() {
|
||||
return g
|
||||
}
|
||||
let it = win.field("inset-top").to_int()
|
||||
let ir = win.field("inset-right").to_int()
|
||||
let ib = win.field("inset-bottom").to_int()
|
||||
let il = win.field("inset-left").to_int()
|
||||
{
|
||||
x: g.x - il - SHADOW_PAD,
|
||||
y: g.y - it - SHADOW_PAD,
|
||||
w: g.w + il + ir + 2 * SHADOW_PAD,
|
||||
h: g.h + it + ib + 2 * SHADOW_PAD,
|
||||
}
|
||||
}
|
||||
|
||||
///| The part of the screen `f` changes over `prev`, as one rectangle; None when all of it must
|
||||
/// be redrawn. Only window changes are narrowed down: any change to the wallpaper, desktop card,
|
||||
/// panel or the set of windows redraws everything.
|
||||
fn damage(prev : Frame, f : Frame) -> R? {
|
||||
if prev.width != f.width ||
|
||||
prev.height != f.height ||
|
||||
prev.windows.length() != f.windows.length() {
|
||||
return None
|
||||
}
|
||||
if f.background != prev.background || f.panel != prev.panel {
|
||||
return None
|
||||
}
|
||||
if f.wallpaper is Some(ops) && prev.wallpaper != Some(ops) {
|
||||
return None
|
||||
}
|
||||
let mut d : R = { x: 0, y: 0, w: 0, h: 0 }
|
||||
for i, a in prev.windows {
|
||||
let b = f.windows[i]
|
||||
if a == b {
|
||||
continue
|
||||
}
|
||||
d = union(d, union(window_footprint(a), window_footprint(b)))
|
||||
}
|
||||
if prev.popups != f.popups {
|
||||
for p in prev.popups.to_list() {
|
||||
d = union(d, rect_of(p.field("area")))
|
||||
}
|
||||
for p in f.popups.to_list() {
|
||||
d = union(d, rect_of(p.field("area")))
|
||||
}
|
||||
}
|
||||
Some(d)
|
||||
}
|
||||
|
||||
///| Remember `f` as the last frame. Its wallpaper ops are kept from the frame that sent them, so
|
||||
/// a later frame that resends the same wallpaper still compares equal.
|
||||
fn keep_prev(f : Frame) -> Unit {
|
||||
let wallpaper = match f.wallpaper {
|
||||
Some(_) => f.wallpaper
|
||||
None =>
|
||||
match prev_frame.val {
|
||||
Some(p) => p.wallpaper
|
||||
None => None
|
||||
}
|
||||
}
|
||||
prev_frame.val = Some({ ..f, wallpaper, })
|
||||
}
|
||||
|
||||
///| Pixels redrawn by the last submit (for --profile).
|
||||
let last_drawn : Ref[Int] = Ref(0)
|
||||
|
||||
///| Draw a frame into the framebuffer: wallpaper (cached), background, windows bottom to top,
|
||||
/// panel, then client popups. The same layering as the software renderer's `render_scene`. Only
|
||||
/// the part that differs from the last frame is redrawn.
|
||||
fn submit(v : @wt.Val) -> Unit {
|
||||
let f : Frame = {
|
||||
width: @cmp.maximum(v.field("width").to_int(), 1),
|
||||
height: @cmp.maximum(v.field("height").to_int(), 1),
|
||||
wallpaper: v.field("wallpaper").to_option(),
|
||||
background: v.field("background"),
|
||||
windows: v.field("windows").to_list(),
|
||||
panel: v.field("panel"),
|
||||
popups: v.field("popups"),
|
||||
}
|
||||
let w = f.width
|
||||
let h = f.height
|
||||
let resized = rs_resize(w, h) != 0
|
||||
// The frame starts from the cached backdrop (which covers every pixel), so the buffer is only
|
||||
// cleared when there is none.
|
||||
let backdrop_ready = rs_has_backdrop() != 0 &&
|
||||
(match (f.wallpaper, backdrop_ops.val) {
|
||||
(None, Some(_)) => true
|
||||
(Some(ops), Some(o)) => o == ops
|
||||
_ => false
|
||||
})
|
||||
let clip : R? = match prev_frame.val {
|
||||
Some(prev) if !resized && backdrop_ready =>
|
||||
match damage(prev, f) {
|
||||
Some(d) =>
|
||||
match intersect(Some(d), Some({ x: 0, y: 0, w, h })) {
|
||||
Some(k) if !k.empty() => Some(k)
|
||||
_ => {
|
||||
// Nothing on screen changed (a window off screen, say).
|
||||
last_drawn.val = 0
|
||||
keep_prev(f)
|
||||
return
|
||||
}
|
||||
}
|
||||
None => None
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
if !resized && !backdrop_ready {
|
||||
rs_clear()
|
||||
}
|
||||
last_drawn.val = match clip {
|
||||
Some(k) => k.w * k.h
|
||||
None => w * h
|
||||
}
|
||||
|
||||
// 1. Wallpaper: drawn once, copied back while unchanged (only the damaged part).
|
||||
match f.wallpaper {
|
||||
Some(ops) =>
|
||||
if backdrop_ready {
|
||||
set_clip(clip)
|
||||
rs_copy_backdrop()
|
||||
} else {
|
||||
draw_ops(ops.to_list(), 0, 0, None)
|
||||
rs_save_backdrop()
|
||||
backdrop_ops.val = Some(ops)
|
||||
}
|
||||
None =>
|
||||
if rs_has_backdrop() != 0 {
|
||||
set_clip(clip)
|
||||
rs_copy_backdrop()
|
||||
}
|
||||
}
|
||||
draw_ops(f.background.to_list(), 0, 0, clip)
|
||||
|
||||
// 2. Windows, bottom to top.
|
||||
for win in f.windows {
|
||||
let g = rect_of(win.field("geometry"))
|
||||
if clip is Some(_) {
|
||||
match intersect(Some(window_footprint(win)), clip) {
|
||||
Some(r) if !r.empty() => ()
|
||||
_ => continue
|
||||
}
|
||||
}
|
||||
if !win.field("fullscreen").to_bool() {
|
||||
let it = win.field("inset-top").to_int()
|
||||
let ir = win.field("inset-right").to_int()
|
||||
let ib = win.field("inset-bottom").to_int()
|
||||
let il = win.field("inset-left").to_int()
|
||||
let outer_x = g.x - il
|
||||
let outer_y = g.y - it
|
||||
let outer_w = g.w + il + ir
|
||||
let outer_h = g.h + it + ib
|
||||
// Subtle drop shadow: only the strips that stick out from under the window (and the part
|
||||
// under the translucent title bar) are blended.
|
||||
let strips : Array[R] = [
|
||||
{ x: outer_x + outer_w, y: outer_y, w: 4, h: outer_h + 6 },
|
||||
{ x: outer_x, y: outer_y + outer_h, w: outer_w + 4, h: 6 },
|
||||
{ x: outer_x, y: outer_y, w: outer_w, h: it + 2 },
|
||||
]
|
||||
for s in strips {
|
||||
match intersect(Some(s), clip) {
|
||||
Some(k) if !k.empty() => {
|
||||
set_clip(Some(k))
|
||||
rs_fill_rect(outer_x + 4, outer_y + 6, outer_w, outer_h, 0, 0, 0, 80, 8)
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
draw_ops(win.field("deco").to_list(), outer_x, outer_y, clip)
|
||||
}
|
||||
set_clip(clip)
|
||||
if rs_blit_surface(win.field("surface").to_uint(), g.x, g.y, g.w, g.h) == 0 {
|
||||
rs_fill_rect(g.x, g.y, g.w, g.h, 0x1e, 0x29, 0x3b, 0xff, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Panel (and its popups, menus and notices), then 4. client popups above everything.
|
||||
draw_ops(f.panel.to_list(), 0, 0, clip)
|
||||
set_clip(clip)
|
||||
for p in f.popups.to_list() {
|
||||
let a = rect_of(p.field("area"))
|
||||
rs_blit_surface(p.field("surface").to_uint(), a.x, a.y, a.w, a.h) |> ignore
|
||||
}
|
||||
keep_prev(f)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- images
|
||||
|
||||
///| Image handles by path (0: it failed, don't try again).
|
||||
let image_paths : Map[String, Int] = {}
|
||||
|
||||
///| Biggest image side accepted from a file.
|
||||
const MAX_IMAGE : Int = 1024
|
||||
|
||||
///| Load the PNG or SVG at `path` (an app icon); the same path gives the same handle. 0 on failure.
|
||||
fn load_image(path : String) -> Int {
|
||||
match image_paths.get(path) {
|
||||
Some(h) => return h
|
||||
None => ()
|
||||
}
|
||||
let b = @sys.read_file_bytes(path)
|
||||
let h = if b.length() > 8 && b[0] == b'\x89' && b[1] == b'P' && b[2] == b'N' {
|
||||
match @renderer.decode_png(b) {
|
||||
Some(p) if p.width <= MAX_IMAGE && p.height <= MAX_IMAGE =>
|
||||
rs_image_put(0, p.width, p.height, p.data)
|
||||
_ => 0
|
||||
}
|
||||
} else if path.has_suffix(".svg") && b.length() > 0 {
|
||||
rs_image_svg(b)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
image_paths.set(path, h)
|
||||
h
|
||||
}
|
||||
|
||||
///|
|
||||
fn is_path_image(h : Int) -> Bool {
|
||||
for _, v in image_paths {
|
||||
if v == h {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- the interfaces
|
||||
|
||||
///|
|
||||
fn font_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
||||
[
|
||||
(
|
||||
"load",
|
||||
a => boolean(font_load(a[0].to_int(), a[1].to_string())),
|
||||
),
|
||||
(
|
||||
"rasterise",
|
||||
a => {
|
||||
let (slot, cp, size) = (a[0].to_int(), a[1].to_int(), a[2].to_int())
|
||||
let info = FixedArray::make(5, 0)
|
||||
rs_glyph_info(slot, cp, size, info)
|
||||
let cov = FixedArray::make(info[0] * info[1], b'\x00')
|
||||
if cov.length() > 0 {
|
||||
rs_glyph_coverage(slot, cp, size, cov)
|
||||
}
|
||||
[
|
||||
Record([
|
||||
("width", S32(info[0])),
|
||||
("height", S32(info[1])),
|
||||
("xoff", S32(info[2])),
|
||||
("yoff", S32(info[3])),
|
||||
("advance", S32(info[4])),
|
||||
("coverage", @wt.Val::bytes(Bytes::from_fixedarray(cov))),
|
||||
]),
|
||||
]
|
||||
},
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
///|
|
||||
fn raster_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
||||
[
|
||||
(
|
||||
"capture",
|
||||
a => [
|
||||
U32(
|
||||
rs_capture(
|
||||
a[0].to_int(),
|
||||
a[1].to_int(),
|
||||
a[2].to_int(),
|
||||
a[3].to_int(),
|
||||
a[4].to_int(),
|
||||
a[5].to_int(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
(
|
||||
"release",
|
||||
a => {
|
||||
rs_release(a[0].to_uint())
|
||||
none
|
||||
},
|
||||
),
|
||||
("load-image", a => [U32(load_image(a[0].to_string()).reinterpret_as_uint())]),
|
||||
(
|
||||
"thumbnail",
|
||||
a => {
|
||||
let reuse = a[3].to_int()
|
||||
let reuse = if is_path_image(reuse) { 0 } else { reuse }
|
||||
[
|
||||
U32(
|
||||
rs_thumbnail(a[0].to_uint(), a[1].to_int(), a[2].to_int(), reuse).reinterpret_as_uint(),
|
||||
),
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
"submit",
|
||||
a => {
|
||||
submit(a[0])
|
||||
none
|
||||
},
|
||||
),
|
||||
(
|
||||
"read-frame",
|
||||
_ => {
|
||||
let out = FixedArray::make(rs_width() * rs_height() * 4, b'\x00')
|
||||
rs_read_frame(out)
|
||||
[@wt.Val::bytes(Bytes::from_fixedarray(out))]
|
||||
},
|
||||
),
|
||||
("export-memfd", _ => s32(rs_export_memfd())),
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,600 @@
|
||||
/* The host's rasteriser (chandra:host/raster), pixel work in C.
|
||||
*
|
||||
* A port of host-wasm/src/raster.rs, itself a port of compositor/renderer, so a frame comes out
|
||||
* pixel for pixel the same. raster.mbt decides what to draw (layers, damage, the wallpaper cache);
|
||||
* this file owns the framebuffer, the captured client surfaces, the images and the glyph cache,
|
||||
* and draws one primitive per call, inside the current clip.
|
||||
*
|
||||
* Glyphs come from stb_truetype through compositor/font's shim (mp_font_*); SVG icons from
|
||||
* nanosvg (third_party/nanosvg). */
|
||||
#define _GNU_SOURCE
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "moonbit.h"
|
||||
|
||||
#define NANOSVG_IMPLEMENTATION
|
||||
#define NANOSVGRAST_IMPLEMENTATION
|
||||
#include "../../third_party/nanosvg/nanosvg.h"
|
||||
#include "../../third_party/nanosvg/nanosvgrast.h"
|
||||
|
||||
/* compositor/font/font_stub.c */
|
||||
int32_t mp_font_load(int32_t slot, moonbit_bytes_t path_bytes);
|
||||
void mp_font_metrics(int32_t slot, int32_t cp, int32_t size, int32_t *info);
|
||||
void mp_font_bitmap(int32_t slot, int32_t cp, int32_t size, int32_t w, int32_t h, moonbit_bytes_t buf);
|
||||
|
||||
/* ------------------------------------------------------------------ canvas */
|
||||
|
||||
static int32_t fb_w, fb_h;
|
||||
static uint8_t *fb; /* RGBA8888 */
|
||||
|
||||
static int clip_on;
|
||||
static int32_t clip_x, clip_y, clip_w, clip_h;
|
||||
|
||||
static inline int clamp_u8(int v) { return v < 0 ? 0 : v > 255 ? 255 : v; }
|
||||
static inline int imin(int a, int b) { return a < b ? a : b; }
|
||||
static inline int imax(int a, int b) { return a > b ? a : b; }
|
||||
|
||||
/* Resize the framebuffer (zeroed). 1 when the size changed. */
|
||||
int32_t rs_resize(int32_t w, int32_t h) {
|
||||
if (w < 1) w = 1;
|
||||
if (h < 1) h = 1;
|
||||
if (fb && w == fb_w && h == fb_h) return 0;
|
||||
free(fb);
|
||||
fb = calloc((size_t)w * h, 4);
|
||||
fb_w = w;
|
||||
fb_h = h;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void rs_clear(void) { if (fb) memset(fb, 0, (size_t)fb_w * fb_h * 4); }
|
||||
|
||||
void rs_set_clip(int32_t on, int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
clip_on = on;
|
||||
clip_x = x; clip_y = y; clip_w = w; clip_h = h;
|
||||
}
|
||||
|
||||
static inline void blend_pixel(int x, int y, int r, int g, int b, int a) {
|
||||
if (x < 0 || y < 0 || x >= fb_w || y >= fb_h || a <= 0) return;
|
||||
if (clip_on && (x < clip_x || y < clip_y || x >= clip_x + clip_w || y >= clip_y + clip_h)) return;
|
||||
uint8_t *d = fb + ((size_t)y * fb_w + x) * 4;
|
||||
if (a >= 255) {
|
||||
d[0] = (uint8_t)r; d[1] = (uint8_t)g; d[2] = (uint8_t)b; d[3] = 0xff;
|
||||
return;
|
||||
}
|
||||
int inv = 255 - a;
|
||||
d[0] = (uint8_t)((r * a + d[0] * inv + 127) / 255);
|
||||
d[1] = (uint8_t)((g * a + d[1] * inv + 127) / 255);
|
||||
d[2] = (uint8_t)((b * a + d[2] * inv + 127) / 255);
|
||||
d[3] = (uint8_t)clamp_u8(a + (d[3] * inv + 127) / 255);
|
||||
}
|
||||
|
||||
/* Blend a colour over pixels [xa, xb) of row y; the span is already clipped. */
|
||||
static void blend_span(int y, int xa, int xb, int r, int g, int b, int a) {
|
||||
if (xa >= xb || a <= 0) return;
|
||||
uint8_t *p = fb + ((size_t)y * fb_w + xa) * 4, *end = fb + ((size_t)y * fb_w + xb) * 4;
|
||||
if (a >= 255) {
|
||||
for (; p < end; p += 4) { p[0] = (uint8_t)r; p[1] = (uint8_t)g; p[2] = (uint8_t)b; p[3] = 0xff; }
|
||||
return;
|
||||
}
|
||||
int inv = 255 - a, ra = r * a + 127, ga = g * a + 127, ba = b * a + 127;
|
||||
for (; p < end; p += 4) {
|
||||
p[0] = (uint8_t)((ra + p[0] * inv) / 255);
|
||||
p[1] = (uint8_t)((ga + p[1] * inv) / 255);
|
||||
p[2] = (uint8_t)((ba + p[2] * inv) / 255);
|
||||
p[3] = (uint8_t)clamp_u8(a + (p[3] * inv + 127) / 255);
|
||||
}
|
||||
}
|
||||
|
||||
static int in_rounded_rect(int px, int py, int rx, int ry, int rw, int rh, int rad) {
|
||||
if (px < rx || py < ry || px >= rx + rw || py >= ry + rh) return 0;
|
||||
if (rad <= 0) return 1;
|
||||
int r = imin(rad, imin(rw, rh) / 2);
|
||||
if (r <= 0) return 1;
|
||||
int x_left = rx + r, x_right = rx + rw - r - 1, y_top = ry + r, y_bot = ry + rh - r - 1;
|
||||
#define CORNER(dx, dy) ((dx) * (dx) + (dy) * (dy) <= r * r)
|
||||
if (px < x_left && py < y_top) return CORNER(x_left - px, y_top - py);
|
||||
if (px > x_right && py < y_top) return CORNER(px - x_right, y_top - py);
|
||||
if (px < x_left && py > y_bot) return CORNER(x_left - px, py - y_bot);
|
||||
if (px > x_right && py > y_bot) return CORNER(px - x_right, py - y_bot);
|
||||
#undef CORNER
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The part of the rect on the canvas and inside the clip. 0 when empty. */
|
||||
static int visible(int x, int y, int w, int h, int *x0, int *y0, int *x1, int *y1) {
|
||||
*x0 = imax(x, 0); *y0 = imax(y, 0);
|
||||
*x1 = imin(x + w, fb_w); *y1 = imin(y + h, fb_h);
|
||||
if (clip_on) {
|
||||
*x0 = imax(*x0, clip_x); *y0 = imax(*y0, clip_y);
|
||||
*x1 = imin(*x1, clip_x + clip_w); *y1 = imin(*y1, clip_y + clip_h);
|
||||
}
|
||||
return *x0 < *x1 && *y0 < *y1;
|
||||
}
|
||||
|
||||
void rs_fill_rect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
|
||||
int32_t radius) {
|
||||
if (!fb || w <= 0 || h <= 0 || ca == 0) return;
|
||||
int x0, y0, x1, y1;
|
||||
if (!visible(x, y, w, h, &x0, &y0, &x1, &y1)) return;
|
||||
if (radius <= 0) {
|
||||
for (int yy = y0; yy < y1; yy++) blend_span(yy, x0, x1, cr, cg, cb, ca);
|
||||
return;
|
||||
}
|
||||
int r = imin(radius, imin(w, h) / 2);
|
||||
int x_left = x + r, x_right = x + w - r - 1, y_top = y + r, y_bot = y + h - r - 1;
|
||||
for (int yy = y0; yy < y1; yy++) {
|
||||
if (yy >= y_top && yy <= y_bot) { blend_span(yy, x0, x1, cr, cg, cb, ca); continue; }
|
||||
int xx = x0;
|
||||
while (xx < x1 && xx < x_left) {
|
||||
if (in_rounded_rect(xx, yy, x, y, w, h, radius)) blend_span(yy, xx, xx + 1, cr, cg, cb, ca);
|
||||
xx++;
|
||||
}
|
||||
int mid_a = imax(x_left, x0), mid_b = imin(x_right + 1, x1);
|
||||
blend_span(yy, mid_a, mid_b, cr, cg, cb, ca);
|
||||
xx = imax(imax(x_right + 1, x0), mid_a);
|
||||
while (xx < x1) {
|
||||
if (in_rounded_rect(xx, yy, x, y, w, h, radius)) blend_span(yy, xx, xx + 1, cr, cg, cb, ca);
|
||||
xx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rs_stroke_rect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
|
||||
int32_t width, int32_t radius) {
|
||||
if (!fb || w <= 0 || h <= 0 || width <= 0 || ca == 0) return;
|
||||
int x0 = imax(x, 0), y0 = imax(y, 0), x1 = imin(x + w, fb_w), y1 = imin(y + h, fb_h);
|
||||
int inner_x = x + width, inner_y = y + width, inner_w = w - width * 2, inner_h = h - width * 2;
|
||||
int inner_r = radius > width ? radius - width : 0;
|
||||
int cx0 = clip_on ? clip_x : 0, cx1 = clip_on ? clip_x + clip_w : fb_w;
|
||||
int cy0 = clip_on ? clip_y : 0, cy1 = clip_on ? clip_y + clip_h : fb_h;
|
||||
int mid_a = inner_y + inner_r, mid_b = inner_y + inner_h - inner_r;
|
||||
for (int yy = y0; yy < y1; yy++) {
|
||||
if (inner_w > 0 && inner_h > 0 && yy >= mid_a && yy < mid_b) {
|
||||
if (yy >= cy0 && yy < cy1) {
|
||||
int l0 = imax(x0, cx0), l1 = imin(imin(inner_x, x1), cx1);
|
||||
blend_span(yy, l0, l1, cr, cg, cb, ca);
|
||||
int r0 = imax(imax(inner_x + inner_w, x0), cx0);
|
||||
blend_span(yy, r0, imin(x1, cx1), cr, cg, cb, ca);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (int xx = x0; xx < x1; xx++) {
|
||||
int in_outer = in_rounded_rect(xx, yy, x, y, w, h, radius);
|
||||
int in_inner = inner_w > 0 && inner_h > 0 && in_rounded_rect(xx, yy, inner_x, inner_y, inner_w, inner_h, inner_r);
|
||||
if (in_outer && !in_inner) blend_pixel(xx, yy, cr, cg, cb, ca);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rs_line(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
|
||||
int32_t width) {
|
||||
if (!fb) return;
|
||||
int half = width <= 1 ? 0 : width / 2;
|
||||
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
||||
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
||||
int err = dx + dy;
|
||||
for (;;) {
|
||||
for (int oy = -half; oy <= half; oy++)
|
||||
for (int ox = -half; ox <= half; ox++) blend_pixel(x0 + ox, y0 + oy, cr, cg, cb, ca);
|
||||
if (x0 == x1 && y0 == y1) break;
|
||||
int e2 = 2 * err;
|
||||
if (e2 >= dy) { err += dy; x0 += sx; }
|
||||
if (e2 <= dx) { err += dx; y0 += sy; }
|
||||
}
|
||||
}
|
||||
|
||||
void rs_circle(int32_t cx, int32_t cy, int32_t radius, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
|
||||
int32_t filled) {
|
||||
if (!fb || radius <= 0) return;
|
||||
double rad = radius;
|
||||
for (int dy = -radius - 1; dy <= radius + 1; dy++) {
|
||||
for (int dx = -radius - 1; dx <= radius + 1; dx++) {
|
||||
double dist = sqrt((double)(dx * dx + dy * dy));
|
||||
double outer = rad + 0.5 - dist;
|
||||
double cov = outer > 1.0 ? 1.0 : outer;
|
||||
if (!filled) {
|
||||
double inner = dist - (rad - 1.0 - 0.5);
|
||||
double ic = inner > 1.0 ? 1.0 : inner;
|
||||
if (ic < cov) cov = ic;
|
||||
}
|
||||
if (cov > 0.02) blend_pixel(cx + dx, cy + dy, cr, cg, cb, (int)round(ca * cov));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ glyphs */
|
||||
|
||||
typedef struct Glyph {
|
||||
int32_t slot, cp, size;
|
||||
int32_t w, h, xoff, yoff, advance;
|
||||
uint8_t *coverage;
|
||||
struct Glyph *next;
|
||||
} Glyph;
|
||||
|
||||
#define GLYPH_BUCKETS 4096
|
||||
static Glyph *glyphs[GLYPH_BUCKETS];
|
||||
static int font_loaded[4];
|
||||
|
||||
static Glyph *glyph(int32_t slot, int32_t cp, int32_t size) {
|
||||
uint32_t hsh = ((uint32_t)slot * 2654435761u) ^ ((uint32_t)cp * 40503u) ^ ((uint32_t)size * 977u);
|
||||
Glyph **b = &glyphs[hsh % GLYPH_BUCKETS];
|
||||
for (Glyph *g = *b; g; g = g->next)
|
||||
if (g->slot == slot && g->cp == cp && g->size == size) return g;
|
||||
Glyph *g = calloc(1, sizeof *g);
|
||||
g->slot = slot; g->cp = cp; g->size = size;
|
||||
int32_t info[5] = {0};
|
||||
if (slot >= 0 && slot < 4 && font_loaded[slot]) mp_font_metrics(slot, cp, size, info);
|
||||
g->w = info[0]; g->h = info[1]; g->xoff = info[2]; g->yoff = info[3]; g->advance = info[4];
|
||||
if (g->w > 0 && g->h > 0) {
|
||||
/* The shim checks the buffer's MoonBit length: hand it a real MoonBit byte string. */
|
||||
moonbit_bytes_t buf = moonbit_make_bytes(g->w * g->h, 0);
|
||||
mp_font_bitmap(slot, cp, size, g->w, g->h, buf);
|
||||
g->coverage = malloc((size_t)g->w * g->h);
|
||||
memcpy(g->coverage, buf, (size_t)g->w * g->h);
|
||||
moonbit_decref(buf);
|
||||
} else {
|
||||
g->w = g->h = 0;
|
||||
}
|
||||
g->next = *b;
|
||||
*b = g;
|
||||
return g;
|
||||
}
|
||||
|
||||
static void drop_glyphs(int32_t slot) {
|
||||
for (int i = 0; i < GLYPH_BUCKETS; i++) {
|
||||
Glyph **p = &glyphs[i];
|
||||
while (*p) {
|
||||
Glyph *g = *p;
|
||||
if (g->slot == slot) { *p = g->next; free(g->coverage); free(g); }
|
||||
else p = &g->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Slot `slot` now holds a newly loaded face (raster.mbt loads it with mp_font_load, which also
|
||||
* links compositor/font's shim in). */
|
||||
void rs_font_loaded(int32_t slot) {
|
||||
if (slot < 0 || slot >= 4) return;
|
||||
font_loaded[slot] = 1;
|
||||
drop_glyphs(slot);
|
||||
}
|
||||
|
||||
int32_t rs_font_has(int32_t slot) { return slot >= 0 && slot < 4 && font_loaded[slot]; }
|
||||
|
||||
/* Metrics into info: width, height, xoff, yoff, advance. */
|
||||
void rs_glyph_info(int32_t slot, int32_t cp, int32_t size, int32_t *info) {
|
||||
Glyph *g = glyph(slot, cp, size);
|
||||
info[0] = g->w; info[1] = g->h; info[2] = g->xoff; info[3] = g->yoff; info[4] = g->advance;
|
||||
}
|
||||
|
||||
/* The coverage bitmap (w * h bytes) into `out`. */
|
||||
void rs_glyph_coverage(int32_t slot, int32_t cp, int32_t size, moonbit_bytes_t out) {
|
||||
Glyph *g = glyph(slot, cp, size);
|
||||
size_t n = (size_t)g->w * g->h, cap = (size_t)Moonbit_array_length(out);
|
||||
if (g->coverage && n <= cap) memcpy(out, g->coverage, n);
|
||||
}
|
||||
|
||||
void rs_draw_glyph(int32_t slot, int32_t cp, int32_t size, int32_t pen, int32_t baseline, int32_t cr, int32_t cg,
|
||||
int32_t cb, int32_t ca, int32_t embolden) {
|
||||
if (!fb) return;
|
||||
Glyph *g = glyph(slot, cp, size);
|
||||
for (int row = 0; row < g->h; row++) {
|
||||
for (int col = 0; col < g->w; col++) {
|
||||
int cov = g->coverage[row * g->w + col];
|
||||
if (cov > 0) {
|
||||
int px = pen + g->xoff + col, py = baseline + g->yoff + row;
|
||||
blend_pixel(px, py, cr, cg, cb, ca * cov / 255);
|
||||
if (embolden) blend_pixel(px + 1, py, cr, cg, cb, ca * cov / 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ images */
|
||||
|
||||
#define MAX_IMAGE 1024
|
||||
|
||||
typedef struct Sized { int32_t w, h; uint8_t *rgba; struct Sized *next; } Sized;
|
||||
typedef struct {
|
||||
int used;
|
||||
int32_t w, h;
|
||||
uint8_t *rgba; /* pixels, or NULL for an SVG */
|
||||
NSVGimage *svg;
|
||||
Sized *sized; /* the image at the sizes it was drawn at */
|
||||
} Image;
|
||||
|
||||
static Image *images;
|
||||
static int32_t n_images, cap_images;
|
||||
|
||||
/* Scale RGBA src (sw x sh) to w x h: average of the covered source pixels (alpha weighted), the
|
||||
* nearest one when enlarging. Same as the software renderer. */
|
||||
static uint8_t *scale_rgba(const uint8_t *src, int sw, int sh, int w, int h) {
|
||||
uint8_t *out = calloc((size_t)w * h, 4);
|
||||
for (int y = 0; y < h; y++) {
|
||||
int y0 = y * sh / h, y1 = imax((y + 1) * sh / h, y0 + 1);
|
||||
for (int x = 0; x < w; x++) {
|
||||
int x0 = x * sw / w, x1 = imax((x + 1) * sw / w, x0 + 1);
|
||||
int64_t r = 0, g = 0, b = 0, a = 0, n = 0;
|
||||
for (int sy = y0; sy < y1; sy++) {
|
||||
for (int sx = x0; sx < x1; sx++) {
|
||||
const uint8_t *p = src + ((size_t)sy * sw + sx) * 4;
|
||||
int64_t pa = p[3];
|
||||
r += p[0] * pa; g += p[1] * pa; b += p[2] * pa; a += pa; n++;
|
||||
}
|
||||
}
|
||||
if (a > 0) {
|
||||
uint8_t *o = out + ((size_t)y * w + x) * 4;
|
||||
o[0] = (uint8_t)(r / a); o[1] = (uint8_t)(g / a); o[2] = (uint8_t)(b / a); o[3] = (uint8_t)(a / n);
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static void image_reset(Image *im) {
|
||||
free(im->rgba);
|
||||
if (im->svg) nsvgDelete(im->svg);
|
||||
for (Sized *s = im->sized; s;) { Sized *n = s->next; free(s->rgba); free(s); s = n; }
|
||||
memset(im, 0, sizeof *im);
|
||||
}
|
||||
|
||||
static int32_t image_slot(int32_t reuse) {
|
||||
if (reuse > 0 && reuse <= n_images) {
|
||||
image_reset(&images[reuse - 1]);
|
||||
images[reuse - 1].used = 1;
|
||||
return reuse;
|
||||
}
|
||||
if (n_images == cap_images) {
|
||||
cap_images = cap_images ? cap_images * 2 : 64;
|
||||
images = realloc(images, sizeof(Image) * (size_t)cap_images);
|
||||
}
|
||||
memset(&images[n_images], 0, sizeof(Image));
|
||||
images[n_images].used = 1;
|
||||
return ++n_images;
|
||||
}
|
||||
|
||||
/* Store w x h RGBA pixels as an image: overwrite `reuse` when it is an earlier handle, else add one. */
|
||||
int32_t rs_image_put(int32_t reuse, int32_t w, int32_t h, moonbit_bytes_t rgba) {
|
||||
if (w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE) return 0;
|
||||
if ((size_t)Moonbit_array_length(rgba) < (size_t)w * h * 4) return 0;
|
||||
int32_t id = image_slot(reuse);
|
||||
Image *im = &images[id - 1];
|
||||
im->w = w; im->h = h;
|
||||
im->rgba = malloc((size_t)w * h * 4);
|
||||
memcpy(im->rgba, rgba, (size_t)w * h * 4);
|
||||
return id;
|
||||
}
|
||||
|
||||
/* An SVG document (its bytes) as an image, rendered sharp at each size it is drawn at. 0 when it
|
||||
* doesn't parse. */
|
||||
int32_t rs_image_svg(moonbit_bytes_t data) {
|
||||
size_t n = (size_t)Moonbit_array_length(data);
|
||||
char *text = malloc(n + 1);
|
||||
memcpy(text, data, n);
|
||||
text[n] = 0;
|
||||
NSVGimage *svg = nsvgParse(text, "px", 96.0f);
|
||||
free(text);
|
||||
if (!svg) return 0;
|
||||
if (svg->width <= 0 || svg->height <= 0 || !svg->shapes) { nsvgDelete(svg); return 0; }
|
||||
int32_t id = image_slot(0);
|
||||
images[id - 1].svg = svg;
|
||||
return id;
|
||||
}
|
||||
|
||||
static uint8_t *render_svg(NSVGimage *svg, int w, int h) {
|
||||
static NSVGrasterizer *rast;
|
||||
if (!rast) rast = nsvgCreateRasterizer();
|
||||
uint8_t *out = calloc((size_t)w * h, 4);
|
||||
float k = fminf((float)w / svg->width, (float)h / svg->height);
|
||||
float dx = ((float)w - svg->width * k) / 2.0f, dy = ((float)h - svg->height * k) / 2.0f;
|
||||
nsvgRasterize(rast, svg, dx, dy, k, out, w, h, w * 4); /* straight (not premultiplied) RGBA */
|
||||
return out;
|
||||
}
|
||||
|
||||
static const uint8_t *image_sized(int32_t handle, int w, int h) {
|
||||
if (w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE) return NULL;
|
||||
if (handle <= 0 || handle > n_images || !images[handle - 1].used) return NULL;
|
||||
Image *im = &images[handle - 1];
|
||||
if (im->rgba && im->w == w && im->h == h) return im->rgba;
|
||||
for (Sized *s = im->sized; s; s = s->next)
|
||||
if (s->w == w && s->h == h) return s->rgba;
|
||||
uint8_t *px = im->svg ? render_svg(im->svg, w, h) : im->rgba ? scale_rgba(im->rgba, im->w, im->h, w, h) : NULL;
|
||||
if (!px) return NULL;
|
||||
Sized *s = malloc(sizeof *s);
|
||||
s->w = w; s->h = h; s->rgba = px; s->next = im->sized;
|
||||
im->sized = s;
|
||||
return px;
|
||||
}
|
||||
|
||||
void rs_draw_image(int32_t handle, int32_t x0, int32_t y0, int32_t w, int32_t h) {
|
||||
if (!fb) return;
|
||||
const uint8_t *px = image_sized(handle, w, h);
|
||||
if (!px) return;
|
||||
for (int y = 0; y < h; y++)
|
||||
for (int x = 0; x < w; x++) {
|
||||
const uint8_t *p = px + ((size_t)y * w + x) * 4;
|
||||
blend_pixel(x0 + x, y0 + y, p[0], p[1], p[2], p[3]);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ surfaces */
|
||||
|
||||
typedef struct Surface {
|
||||
uint32_t id;
|
||||
int32_t w, h;
|
||||
uint8_t *rgba;
|
||||
int opaque; /* every pixel has alpha 255: blits copy rows */
|
||||
struct Surface *next;
|
||||
} Surface;
|
||||
|
||||
#define SURF_BUCKETS 256
|
||||
static Surface *surfaces[SURF_BUCKETS];
|
||||
static uint32_t next_surface;
|
||||
|
||||
static Surface *surface(uint32_t id) {
|
||||
for (Surface *s = surfaces[id % SURF_BUCKETS]; s; s = s->next)
|
||||
if (s->id == id) return s;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Copy a client buffer (stride * height bytes at `offset` of the file behind fd, little-endian
|
||||
* B, G, R, A/X) into a surface. 0 on failure. */
|
||||
uint32_t rs_capture(int32_t fd, int32_t offset, int32_t stride, int32_t w, int32_t h, int32_t format) {
|
||||
if (fd < 0 || offset < 0 || stride <= 0 || w <= 0 || h <= 0 || stride < w * 4) return 0;
|
||||
int64_t len = (int64_t)stride * h, end = offset + len;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0 || st.st_size < end) return 0;
|
||||
/* Mapping a large pool only reserves address space; just the pages copied are touched. */
|
||||
uint8_t *p = mmap(NULL, (size_t)end, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED) return 0;
|
||||
const uint8_t *src = p + offset;
|
||||
uint8_t *rgba = malloc((size_t)w * h * 4);
|
||||
int opaque = 1, xrgb = format == 1;
|
||||
for (int y = 0; y < h; y++) {
|
||||
const uint8_t *s = src + (size_t)y * stride;
|
||||
uint8_t *d = rgba + (size_t)y * w * 4;
|
||||
for (int x = 0; x < w; x++, s += 4, d += 4) {
|
||||
d[0] = s[2]; d[1] = s[1]; d[2] = s[0];
|
||||
d[3] = xrgb ? 0xff : s[3];
|
||||
opaque &= d[3] == 0xff;
|
||||
}
|
||||
}
|
||||
munmap(p, (size_t)end);
|
||||
do { next_surface++; } while (next_surface == 0 || surface(next_surface));
|
||||
Surface *sf = malloc(sizeof *sf);
|
||||
sf->id = next_surface; sf->w = w; sf->h = h; sf->rgba = rgba; sf->opaque = opaque;
|
||||
sf->next = surfaces[sf->id % SURF_BUCKETS];
|
||||
surfaces[sf->id % SURF_BUCKETS] = sf;
|
||||
return sf->id;
|
||||
}
|
||||
|
||||
void rs_release(uint32_t id) {
|
||||
Surface **p = &surfaces[id % SURF_BUCKETS];
|
||||
while (*p) {
|
||||
if ((*p)->id == id) { Surface *s = *p; *p = s->next; free(s->rgba); free(s); return; }
|
||||
p = &(*p)->next;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t rs_surface_count(void) {
|
||||
int n = 0;
|
||||
for (int i = 0; i < SURF_BUCKETS; i++) for (Surface *s = surfaces[i]; s; s = s->next) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
/* A w x h scaled copy of surface `id` as an image (see rs_image_put for `reuse`). */
|
||||
int32_t rs_thumbnail(uint32_t id, int32_t w, int32_t h, int32_t reuse) {
|
||||
if (w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE) return 0;
|
||||
Surface *s = surface(id);
|
||||
if (!s) return 0;
|
||||
uint8_t *px = scale_rgba(s->rgba, s->w, s->h, w, h);
|
||||
int32_t img = image_slot(reuse);
|
||||
images[img - 1].w = w; images[img - 1].h = h; images[img - 1].rgba = px;
|
||||
return img;
|
||||
}
|
||||
|
||||
/* Draw surface `id` into (x, y, w, h). While a client catches up with a resize its buffer is
|
||||
* smaller than the window: the buffer's bottom-right colour is extended over the rest.
|
||||
* 0 when there is no such surface. */
|
||||
int32_t rs_blit_surface(uint32_t id, int32_t dx, int32_t dy, int32_t dw, int32_t dh) {
|
||||
Surface *s = surface(id);
|
||||
if (!s) return 0;
|
||||
if (!fb || s->w <= 0 || s->h <= 0 || dw <= 0 || dh <= 0) return 1;
|
||||
if (s->w < dw || s->h < dh) {
|
||||
const uint8_t *o = s->rgba + ((size_t)(s->h - 1) * s->w + (s->w - 1)) * 4;
|
||||
rs_fill_rect(dx, dy, dw, dh, o[0], o[1], o[2], o[3], 0);
|
||||
}
|
||||
int copy_w = imin(s->w, dw), copy_h = imin(s->h, dh);
|
||||
int c0 = imax(-dx, 0), c1 = imin(copy_w, fb_w - dx);
|
||||
int r0 = imax(-dy, 0), r1 = imin(copy_h, fb_h - dy);
|
||||
if (clip_on) {
|
||||
c0 = imax(c0, clip_x - dx); c1 = imin(c1, clip_x + clip_w - dx);
|
||||
r0 = imax(r0, clip_y - dy); r1 = imin(r1, clip_y + clip_h - dy);
|
||||
}
|
||||
if (c0 >= c1 || r0 >= r1) return 1;
|
||||
for (int row = r0; row < r1; row++) {
|
||||
const uint8_t *src = s->rgba + ((size_t)row * s->w + c0) * 4;
|
||||
uint8_t *dst = fb + ((size_t)(dy + row) * fb_w + dx + c0) * 4;
|
||||
size_t n = (size_t)(c1 - c0) * 4;
|
||||
if (s->opaque) { memcpy(dst, src, n); continue; }
|
||||
for (size_t i = 0; i < n; i += 4) {
|
||||
int a = src[i + 3];
|
||||
if (a >= 255) memcpy(dst + i, src + i, 4);
|
||||
else if (a > 0) {
|
||||
/* wl_shm ARGB8888 is premultiplied: the colour already carries its alpha. */
|
||||
int inv = 255 - a;
|
||||
dst[i] = (uint8_t)clamp_u8(src[i] + (dst[i] * inv + 127) / 255);
|
||||
dst[i + 1] = (uint8_t)clamp_u8(src[i + 1] + (dst[i + 1] * inv + 127) / 255);
|
||||
dst[i + 2] = (uint8_t)clamp_u8(src[i + 2] + (dst[i + 2] * inv + 127) / 255);
|
||||
dst[i + 3] = (uint8_t)clamp_u8(a + (dst[i + 3] * inv + 127) / 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ backdrop and output */
|
||||
|
||||
static uint8_t *backdrop;
|
||||
static int32_t backdrop_w, backdrop_h;
|
||||
|
||||
/* Keep the framebuffer as the backdrop (wallpaper and desktop card). */
|
||||
void rs_save_backdrop(void) {
|
||||
free(backdrop);
|
||||
backdrop = malloc((size_t)fb_w * fb_h * 4);
|
||||
memcpy(backdrop, fb, (size_t)fb_w * fb_h * 4);
|
||||
backdrop_w = fb_w;
|
||||
backdrop_h = fb_h;
|
||||
}
|
||||
|
||||
/* 1 when a backdrop of the framebuffer's size is kept. */
|
||||
int32_t rs_has_backdrop(void) { return backdrop && backdrop_w == fb_w && backdrop_h == fb_h; }
|
||||
|
||||
/* Copy the backdrop back into the clip (or everywhere). */
|
||||
void rs_copy_backdrop(void) {
|
||||
if (!rs_has_backdrop()) return;
|
||||
if (!clip_on) { memcpy(fb, backdrop, (size_t)fb_w * fb_h * 4); return; }
|
||||
int x0 = imax(clip_x, 0), x1 = imin(clip_x + clip_w, fb_w);
|
||||
int y0 = imax(clip_y, 0), y1 = imin(clip_y + clip_h, fb_h);
|
||||
if (x0 >= x1) return;
|
||||
for (int y = y0; y < y1; y++) {
|
||||
size_t a = ((size_t)y * fb_w + x0) * 4;
|
||||
memcpy(fb + a, backdrop + a, (size_t)(x1 - x0) * 4);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t rs_width(void) { return fb_w; }
|
||||
int32_t rs_height(void) { return fb_h; }
|
||||
uint8_t *rs_pixels(void) { return fb; }
|
||||
|
||||
/* The framebuffer as RGBA8888 into `out` (w * h * 4 bytes). */
|
||||
void rs_read_frame(moonbit_bytes_t out) {
|
||||
size_t n = (size_t)fb_w * fb_h * 4;
|
||||
if (fb && (size_t)Moonbit_array_length(out) >= n) memcpy(out, fb, n);
|
||||
}
|
||||
|
||||
/* A new memfd holding the framebuffer as little-endian ARGB8888 (B, G, R, 255). -1 on failure. */
|
||||
int32_t rs_export_memfd(void) {
|
||||
if (!fb) return -1;
|
||||
size_t n = (size_t)fb_w * fb_h * 4;
|
||||
uint8_t *data = malloc(n);
|
||||
for (size_t i = 0; i < n; i += 4) {
|
||||
data[i] = fb[i + 2]; data[i + 1] = fb[i + 1]; data[i + 2] = fb[i]; data[i + 3] = 0xff;
|
||||
}
|
||||
int fd = memfd_create("chandra-frame", MFD_CLOEXEC);
|
||||
if (fd < 0) { free(data); return -1; }
|
||||
size_t done = 0;
|
||||
while (done < n) {
|
||||
ssize_t w = write(fd, data + done, n - done);
|
||||
if (w <= 0) { close(fd); free(data); return -1; }
|
||||
done += (size_t)w;
|
||||
}
|
||||
free(data);
|
||||
return fd;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
///| Interfaces not served yet (filled in stage by stage).
|
||||
|
||||
///|
|
||||
fn display_wait_fds() -> Array[Int] {
|
||||
[]
|
||||
}
|
||||
|
||||
|
||||
|
||||
///|
|
||||
fn display_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
||||
[
|
||||
("open", _ => [Option(None)]),
|
||||
("poll", _ => [Bool(false)]),
|
||||
("frame-ready", _ => [Bool(false)]),
|
||||
("present", _ => none),
|
||||
("present-frame", _ => none),
|
||||
("set-cursor-shape", _ => none),
|
||||
("take-input", _ => [List([])]),
|
||||
("close", _ => none),
|
||||
]
|
||||
}
|
||||
|
||||
///|
|
||||
fn plasmoid_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
||||
[
|
||||
("load", _ => [Result(false, Some(Str("not yet")))]),
|
||||
("unload", _ => none),
|
||||
("set-tokens", _ => none),
|
||||
("render", _ => [List([])]),
|
||||
("popup", _ => [Option(None)]),
|
||||
("on-event", _ => [Bool(false)]),
|
||||
("config-items", _ => [List([])]),
|
||||
("set-config", _ => [Bool(false)]),
|
||||
("publish-table", _ => none),
|
||||
("publish-state", _ => none),
|
||||
("publish-wallpaper", _ => none),
|
||||
("publish-session-settings", _ => none),
|
||||
("publish-screenshot", _ => none),
|
||||
("publish-system", _ => none),
|
||||
("publish-windows", _ => none),
|
||||
("next-request", _ => [Option(None)]),
|
||||
("take-focus-request", _ => [Option(None)]),
|
||||
("list-wasm-files", _ => [List([])]),
|
||||
("picker-start", _ => [Bool(false)]),
|
||||
("picker-poll", _ => [Variant("cancelled", None)]),
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
///| `chandra:host/sys`: the OS surface, over compositor/sys's native implementation (the same
|
||||
/// C shim the native compositor uses).
|
||||
|
||||
///|
|
||||
#borrow(path, data)
|
||||
extern "C" fn c_append_file(path : Bytes, data : Bytes) -> Int = "wh_append_file"
|
||||
|
||||
///| Fds beyond the first that arrived with the last `recvmsg` on a socket, for `take-fd`.
|
||||
let extra_fds : Map[Int, Array[Int]] = {}
|
||||
|
||||
///|
|
||||
fn drop_extra_fds(sock : Int) -> Unit {
|
||||
match extra_fds.get(sock) {
|
||||
Some(left) => {
|
||||
for f in left {
|
||||
@sys.close_fd(f)
|
||||
}
|
||||
extra_fds.remove(sock)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn s32(v : Int) -> Array[@wt.Val] {
|
||||
[S32(v)]
|
||||
}
|
||||
|
||||
///|
|
||||
fn boolean(b : Bool) -> Array[@wt.Val] {
|
||||
[Bool(b)]
|
||||
}
|
||||
|
||||
///|
|
||||
let none : Array[@wt.Val] = []
|
||||
|
||||
///|
|
||||
fn sys_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
||||
[
|
||||
("unix-listen", a => s32(@sys.unix_listen(a[0].to_string()))),
|
||||
("unix-accept", a => s32(@sys.unix_accept(a[0].to_int()))),
|
||||
("unix-connect", a => s32(@sys.unix_connect(a[0].to_string()))),
|
||||
(
|
||||
"close-fd",
|
||||
a => {
|
||||
let fd = a[0].to_int()
|
||||
if fd >= 0 {
|
||||
// A closed client socket's number is reused by the next one: drop what it left.
|
||||
drop_extra_fds(fd)
|
||||
@sys.close_fd(fd)
|
||||
}
|
||||
none
|
||||
},
|
||||
),
|
||||
(
|
||||
"unlink",
|
||||
a => {
|
||||
@sys.unlink(a[0].to_string())
|
||||
none
|
||||
},
|
||||
),
|
||||
("poll-read", a => s32(@sys.poll_read(a[0].to_int(), a[1].to_int()))),
|
||||
(
|
||||
"wait-readable",
|
||||
a => {
|
||||
let fds = a[0].to_list().map(v => v.to_int())
|
||||
for f in display_wait_fds() {
|
||||
fds.push(f)
|
||||
}
|
||||
s32(@sys.wait_readable(fds, a[1].to_int()))
|
||||
},
|
||||
),
|
||||
(
|
||||
"recvmsg",
|
||||
a => {
|
||||
let fd = a[0].to_int()
|
||||
// Fds from the previous read nobody took would leak: close them.
|
||||
drop_extra_fds(fd)
|
||||
let max_len = a[1].to_int()
|
||||
let received = fn(count : Int, got : Int, data : Bytes) -> Array[@wt.Val] {
|
||||
[
|
||||
Record([
|
||||
("count", S32(count)),
|
||||
("fd", S32(got)),
|
||||
("data", @wt.Val::bytes(data)),
|
||||
]),
|
||||
]
|
||||
}
|
||||
if max_len <= 0 {
|
||||
return received(0, -1, b"")
|
||||
}
|
||||
let buf = FixedArray::make(max_len, b'\x00')
|
||||
let (n, fds) = @sys.recvmsg_fds(fd, buf, max_len)
|
||||
if n < 0 {
|
||||
for f in fds {
|
||||
@sys.close_fd(f)
|
||||
}
|
||||
return received(n, -1, b"")
|
||||
}
|
||||
let got = if fds.length() > 0 { fds[0] } else { -1 }
|
||||
if fds.length() > 1 {
|
||||
extra_fds.set(fd, fds[1:].to_array())
|
||||
}
|
||||
received(n, got, Bytes::from_fixedarray(buf, len=n))
|
||||
},
|
||||
),
|
||||
(
|
||||
"take-fd",
|
||||
a => {
|
||||
let sock = a[0].to_int()
|
||||
match extra_fds.get(sock) {
|
||||
Some(q) if q.length() > 0 => {
|
||||
let f = q.remove(0)
|
||||
if q.length() == 0 {
|
||||
extra_fds.remove(sock)
|
||||
}
|
||||
s32(f)
|
||||
}
|
||||
_ => s32(-1)
|
||||
}
|
||||
},
|
||||
),
|
||||
(
|
||||
"sendmsg",
|
||||
a => {
|
||||
let data = a[1].to_bytes()
|
||||
if data.length() == 0 {
|
||||
return s32(0)
|
||||
}
|
||||
s32(@sys.sendmsg_fd(a[0].to_int(), data, a[2].to_int()))
|
||||
},
|
||||
),
|
||||
("memfd-create", a => s32(@sys.memfd_create(a[0].to_string(), a[1].to_int()))),
|
||||
(
|
||||
"pwrite",
|
||||
a => {
|
||||
let fd = a[0].to_int()
|
||||
let data = a[1].to_bytes()
|
||||
let off = a[2].to_int()
|
||||
s32(
|
||||
if off < 0 {
|
||||
@sys.write_nonblock(fd, data)
|
||||
} else {
|
||||
@sys.pwrite_bytes(fd, data, off)
|
||||
},
|
||||
)
|
||||
},
|
||||
),
|
||||
(
|
||||
"mmap-read",
|
||||
a => [@wt.Val::bytes(@sys.mmap_read(a[0].to_int(), a[1].to_int()))],
|
||||
),
|
||||
(
|
||||
"mmap-read-at",
|
||||
a => {
|
||||
let (fd, off, len) = (a[0].to_int(), a[1].to_int(), a[2].to_int())
|
||||
if fd < 0 || off < 0 || len <= 0 {
|
||||
return [@wt.Val::bytes(b"")]
|
||||
}
|
||||
[@wt.Val::bytes(@sys.mmap_read_at(fd, off, len))]
|
||||
},
|
||||
),
|
||||
(
|
||||
"read-file",
|
||||
a => [@wt.Val::bytes(@sys.read_file_bytes(a[0].to_string()))],
|
||||
),
|
||||
(
|
||||
"write-file",
|
||||
a => s32(@sys.write_file_bytes(a[0].to_string(), a[1].to_bytes())),
|
||||
),
|
||||
(
|
||||
"append-file",
|
||||
a => s32(c_append_file(@utf8.encode(a[0].to_string()), a[1].to_bytes())),
|
||||
),
|
||||
(
|
||||
"read-proc",
|
||||
a => [@wt.Val::bytes(@sys.read_proc_bytes(a[0].to_string()))],
|
||||
),
|
||||
("mkdir-p", a => boolean(@sys.mkdir_p(a[0].to_string()))),
|
||||
(
|
||||
"list-desktop-files",
|
||||
a => [@wt.Val::strings(@sys.list_desktop_files(a[0].to_string()))],
|
||||
),
|
||||
(
|
||||
"sleep-ms",
|
||||
a => {
|
||||
let ms = a[0].to_int()
|
||||
if ms > 0 {
|
||||
@sys.sleep_ms(ms)
|
||||
}
|
||||
none
|
||||
},
|
||||
),
|
||||
("monotonic-ms", _ => [U64(@sys.monotonic_ms())]),
|
||||
(
|
||||
"log",
|
||||
a => {
|
||||
eprint(a[0].to_string())
|
||||
none
|
||||
},
|
||||
),
|
||||
("getpid", _ => s32(@sys.getpid())),
|
||||
("wall-time-field", a => s32(@sys.wall_time_field(a[0].to_int()))),
|
||||
("getenv", a => [Str(@sys.getenv(a[0].to_string()))]),
|
||||
("has-exe", a => boolean(@sys.has_exe(a[0].to_string()))),
|
||||
(
|
||||
"spawn",
|
||||
a => boolean(@sys.spawn(a[0].to_string(), a[1].to_string())),
|
||||
),
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user