Compile the compositor to a wasm component, run by a small wasmtime host
CI / test (push) Failing after 22s
CI / test (push) Failing after 22s
The whole compositor (Wayland server, window manager, scene, rasteriser, shell) now builds as build/components/compositor.wasm: a component that imports only moonplasma:host/sys (wit/host/sys.wit: sockets + SCM_RIGHTS, memfd, poll, spawn, files, clock) and exports run(). host-wasm/ is the native side: a wasmtime program implementing that interface with libc. - compositor/sys: per-target backends (sys_native.mbt over the C shim, sys_wasm.mbt over the WIT imports) behind one API; wayland/nested/host/shell/scene/renderer/gfx build for wasm unchanged. - compositor/app: the entry point (arg parsing + main loop) as a library for both targets; --serve keeps refreshing a PNG so a live compositor can be inspected without a display. - compositor/font, compositor/wasm: wasm-side placeholders (bitmap font; no panel modules yet, since a component can't embed a second wasm runtime: that becomes a host import). - scripts/gen-native-stubs.py: native stand-ins for wasm-only generated imports. - just compositor-wasm / host-wasm / run-wasm. Verified: foot connects to the component's socket and renders into it; native build and tests unchanged (16/23, the same 7 need unbuilt panel modules). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92d2b29889
commit
6aba9b6a4f
42 files changed
+3163
-10
No files matched your search
@@ -1,142 +0,0 @@
|
||||
///| Real text rendering for the native compositor: Adwaita Mono rasterised with
|
||||
/// stb_truetype, plugged into the renderer's glyph hook. The pure-MoonBit
|
||||
/// renderer (also built for wasm) keeps its bitmap font when no hook is set.
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_font_load(slot : Int, path : Bytes) -> Int = "mp_font_load"
|
||||
|
||||
///|
|
||||
extern "C" fn c_font_metrics(
|
||||
slot : Int,
|
||||
cp : Int,
|
||||
size : Int,
|
||||
info : FixedArray[Int],
|
||||
) = "mp_font_metrics"
|
||||
|
||||
///|
|
||||
#borrow(buf)
|
||||
extern "C" fn c_font_bitmap(
|
||||
slot : Int,
|
||||
cp : Int,
|
||||
size : Int,
|
||||
w : Int,
|
||||
h : Int,
|
||||
buf : Bytes,
|
||||
) = "mp_font_bitmap"
|
||||
|
||||
///|
|
||||
fn utf8(s : String) -> Bytes {
|
||||
let out : Array[Byte] = []
|
||||
for ch in s {
|
||||
let c = ch.to_int()
|
||||
if c < 0x80 {
|
||||
out.push(c.to_byte())
|
||||
} else if c < 0x800 {
|
||||
out.push((0xC0 | (c >> 6)).to_byte())
|
||||
out.push((0x80 | (c & 0x3F)).to_byte())
|
||||
} else if c < 0x10000 {
|
||||
out.push((0xE0 | (c >> 12)).to_byte())
|
||||
out.push((0x80 | ((c >> 6) & 0x3F)).to_byte())
|
||||
out.push((0x80 | (c & 0x3F)).to_byte())
|
||||
} else {
|
||||
out.push((0xF0 | (c >> 18)).to_byte())
|
||||
out.push((0x80 | ((c >> 12) & 0x3F)).to_byte())
|
||||
out.push((0x80 | ((c >> 6) & 0x3F)).to_byte())
|
||||
out.push((0x80 | (c & 0x3F)).to_byte())
|
||||
}
|
||||
}
|
||||
Bytes::from_array(out)
|
||||
}
|
||||
|
||||
///| Where to look for each face: bundled copy first, then the system one.
|
||||
let regular_paths : Array[String] = [
|
||||
"assets/fonts/AdwaitaMono-Regular.ttf",
|
||||
"/usr/share/fonts/Adwaita/AdwaitaMono-Regular.ttf",
|
||||
]
|
||||
|
||||
///|
|
||||
let sans_paths : Array[String] = [
|
||||
"assets/fonts/AlloVariable.ttf",
|
||||
"/usr/local/share/fonts/a/AlloVariable.ttf",
|
||||
"assets/fonts/AdwaitaSans-Regular.ttf",
|
||||
"/usr/share/fonts/Adwaita/AdwaitaSans-Regular.ttf",
|
||||
]
|
||||
|
||||
///|
|
||||
let bold_paths : Array[String] = [
|
||||
"assets/fonts/AdwaitaMono-Bold.ttf",
|
||||
"/usr/share/fonts/Adwaita/AdwaitaMono-Bold.ttf",
|
||||
]
|
||||
|
||||
///|
|
||||
fn load_first(slot : Int, paths : Array[String]) -> Bool {
|
||||
for p in paths {
|
||||
if c_font_load(slot, utf8(p)) == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///|
|
||||
let cache : Map[Int, @renderer.Glyph] = {}
|
||||
|
||||
///|
|
||||
fn rasterise(slot : Int, cp : Int, size : Int, embolden : Bool) -> @renderer.Glyph {
|
||||
let key = cp + 0x110000 * (size + 256 * (slot * 2 + (if embolden { 1 } else { 0 })))
|
||||
match cache.get(key) {
|
||||
Some(g) => g
|
||||
None => {
|
||||
let info : FixedArray[Int] = FixedArray::make(5, 0)
|
||||
c_font_metrics(slot, cp, size, info)
|
||||
let w = info[0]
|
||||
let h = info[1]
|
||||
let buf = if w > 0 && h > 0 {
|
||||
let b = Bytes::make(w * h, b'\x00')
|
||||
c_font_bitmap(slot, cp, size, w, h, b)
|
||||
b
|
||||
} else {
|
||||
Bytes::make(0, b'\x00')
|
||||
}
|
||||
let g : @renderer.Glyph = {
|
||||
w,
|
||||
h,
|
||||
xoff: info[2],
|
||||
yoff: info[3],
|
||||
advance: info[4],
|
||||
coverage: buf,
|
||||
embolden,
|
||||
}
|
||||
cache.set(key, g)
|
||||
g
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Install Allo Variable (UI text, falling back to Adwaita Sans) and Adwaita Mono (code) as the renderer's
|
||||
/// faces. Returns false (keeping the bitmap font) when no face can be found.
|
||||
/// Slots: 0 mono, 1 mono bold, 2 sans. Sans is a variable font whose bold
|
||||
/// instance stb_truetype can't select, so bold sans is emboldened.
|
||||
pub fn install() -> Bool {
|
||||
let has_mono = load_first(0, regular_paths)
|
||||
let has_sans = load_first(2, sans_paths)
|
||||
if !has_mono && !has_sans {
|
||||
return false
|
||||
}
|
||||
let has_bold = has_mono && load_first(1, bold_paths)
|
||||
@renderer.set_glyph_source(fn(cp, size, bold, mono) {
|
||||
if mono && has_mono {
|
||||
if bold && has_bold {
|
||||
rasterise(1, cp, size, false)
|
||||
} else {
|
||||
rasterise(0, cp, size, bold)
|
||||
}
|
||||
} else if has_sans {
|
||||
rasterise(2, cp, size, bold)
|
||||
} else {
|
||||
rasterise(0, cp, size, bold)
|
||||
}
|
||||
})
|
||||
true
|
||||
}
|
||||
Reference in new issue
Block a user