CI / test (push) Failing after 46s
A component can't embed a second wasm runtime, so the compositor drives panel modules through a host import and the wasmtime host instantiates them: load, render, popup, on-event, plus the data they read (apps, windows, layouts, modules, system) and the requests they make (launch, focus window, set layout, load/unload, browse), with a zenity file chooser for 'Add widget'. - wit/worlds.wit: world plasmoid-host, every capability a module can import + the plasmoid export. The host instantiates any module against it; a module imports the subset it uses. - wit/host/plasmoids.wit + generated MoonBit bindings; compositor/wasm gets a wasm backend (wasm_host.mbt) with the same public API as the native wasmer one. - host-wasm/src/plasmoid.rs: typed component bindings, one Store per module, shared host data. Tests load the built launcher/layout modules and drive them (typed filter, Enter, launch request). - scripts/gen-native-stubs.py keeps a package's imports when it rewrites moon.pkg. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
315 lines
7.5 KiB
MoonBit
315 lines
7.5 KiB
MoonBit
///| Plasmoid host for the compositor-as-a-wasm-component build.
|
|
///
|
|
/// The native build runs panel modules itself through wasmer. A component can't embed a second
|
|
/// wasm runtime, so here the same API is a thin client of the `moonplasma:host/plasmoids` import:
|
|
/// the host (wasmtime) instantiates the modules, serves their capability imports from the data
|
|
/// published below, and hands back draw ops and requests.
|
|
|
|
///|
|
|
fn utf8(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)
|
|
}
|
|
|
|
///| The native build hands tokens to modules as packed bytes; here they cross as a typed record.
|
|
pub fn pack_tokens(_t : @theme.Tokens) -> Bytes {
|
|
Bytes::make(0, b'\x00')
|
|
}
|
|
|
|
///| A loaded plasmoid instance.
|
|
pub(all) struct Plasmoid {
|
|
handle : Int
|
|
id : String
|
|
name : String
|
|
pref_w : Int
|
|
pref_h : Int
|
|
min_w : Int
|
|
stretch : Bool
|
|
imports : Array[String]
|
|
}
|
|
|
|
///| Load a plasmoid component. Err carries a short reason.
|
|
pub fn load(path : String, tokens : @theme.Tokens) -> Result[Plasmoid, String] {
|
|
@hostpl.set_tokens(tokens)
|
|
match @hostpl.load(path) {
|
|
Err(e) => Err(e)
|
|
Ok(l) => {
|
|
let imports : Array[String] = []
|
|
for i in l.imports {
|
|
if i != "theme" {
|
|
imports.push(i)
|
|
}
|
|
}
|
|
Ok({
|
|
handle: l.handle.reinterpret_as_int(),
|
|
id: l.id,
|
|
name: l.name,
|
|
pref_w: l.preferred.w.reinterpret_as_int(),
|
|
pref_h: l.preferred.h.reinterpret_as_int(),
|
|
min_w: l.min_width.reinterpret_as_int(),
|
|
stretch: l.stretch,
|
|
imports,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub(all) struct SystemSnapshot {
|
|
cpu_permille : Int
|
|
mem_used_kib : Int64
|
|
mem_total_kib : Int64
|
|
battery_percent : Int
|
|
charging : Bool
|
|
uptime_secs : Int64
|
|
rx_bps : Int64
|
|
tx_bps : Int64
|
|
load_centi : Int
|
|
}
|
|
|
|
///|
|
|
pub(all) struct WindowEntry {
|
|
id : UInt64
|
|
title : String
|
|
app_id : String
|
|
focused : Bool
|
|
}
|
|
|
|
///|
|
|
pub fn set_system(s : SystemSnapshot) -> Unit {
|
|
@hostpl.publish_system({
|
|
cpu_permille: s.cpu_permille.reinterpret_as_uint(),
|
|
mem_used_kib: s.mem_used_kib.reinterpret_as_uint64(),
|
|
mem_total_kib: s.mem_total_kib.reinterpret_as_uint64(),
|
|
battery_percent: s.battery_percent,
|
|
charging: s.charging,
|
|
uptime_secs: s.uptime_secs.reinterpret_as_uint64(),
|
|
net_rx_bytes_per_sec: s.rx_bps.reinterpret_as_uint64(),
|
|
net_tx_bytes_per_sec: s.tx_bps.reinterpret_as_uint64(),
|
|
load_centi: s.load_centi.reinterpret_as_uint(),
|
|
})
|
|
}
|
|
|
|
///|
|
|
pub fn set_windows(wins : Array[WindowEntry]) -> Unit {
|
|
let out : Array[@hostpl.WindowEntry] = []
|
|
for w in wins {
|
|
out.push({ id: w.id, title: w.title, app_id: w.app_id, focused: w.focused })
|
|
}
|
|
@hostpl.publish_windows(out)
|
|
}
|
|
|
|
///|
|
|
pub fn take_focus_request() -> UInt64? {
|
|
@hostpl.take_focus_request()
|
|
}
|
|
|
|
///|
|
|
pub fn Plasmoid::pointer_down(self : Plasmoid, x : Int, y : Int) -> Bool {
|
|
self.on_event(false, PointerDown(x, y))
|
|
}
|
|
|
|
///|
|
|
pub fn Plasmoid::unload(self : Plasmoid) -> Unit {
|
|
@hostpl.unload(self.handle.reinterpret_as_uint())
|
|
}
|
|
|
|
///|
|
|
pub fn Plasmoid::render(
|
|
self : Plasmoid,
|
|
w : Int,
|
|
h : Int,
|
|
tokens : @theme.Tokens,
|
|
) -> Array[@types.DrawOp] {
|
|
@hostpl.set_tokens(tokens)
|
|
@hostpl.render(self.handle.reinterpret_as_uint(), { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() })
|
|
}
|
|
|
|
///| A popup surface a plasmoid wants shown next to its panel widget.
|
|
pub(all) struct PopupSurface {
|
|
w : Int
|
|
h : Int
|
|
ops : Array[@types.DrawOp]
|
|
}
|
|
|
|
///|
|
|
pub fn Plasmoid::popup(self : Plasmoid) -> PopupSurface? {
|
|
match @hostpl.popup(self.handle.reinterpret_as_uint()) {
|
|
None => None
|
|
Some(p) =>
|
|
Some({ w: p.box.w.reinterpret_as_int(), h: p.box.h.reinterpret_as_int(), ops: p.ops })
|
|
}
|
|
}
|
|
|
|
///| An input event for a plasmoid (mirrors the WIT `input-event`).
|
|
pub(all) enum PlasmoidEvent {
|
|
PointerMove(Int, Int)
|
|
PointerDown(Int, Int)
|
|
PointerUp(Int, Int)
|
|
PointerLeave
|
|
FocusLost
|
|
KeyDown(String)
|
|
}
|
|
|
|
///|
|
|
pub fn Plasmoid::on_event(self : Plasmoid, popup : Bool, ev : PlasmoidEvent) -> Bool {
|
|
let e : @types.InputEvent = match ev {
|
|
PointerMove(x, y) => PointerMove({ x, y })
|
|
PointerDown(x, y) => PointerDown({ x, y })
|
|
PointerUp(x, y) => PointerUp({ x, y })
|
|
PointerLeave => PointerLeave
|
|
FocusLost => FocusLost
|
|
KeyDown(k) => KeyDown(k)
|
|
}
|
|
@hostpl.on_event(self.handle.reinterpret_as_uint(), popup, e)
|
|
}
|
|
|
|
///|
|
|
pub fn Plasmoid::imports(self : Plasmoid) -> Array[String] {
|
|
self.imports
|
|
}
|
|
|
|
///| Rows of host data a module can read through its capability imports.
|
|
pub(all) struct Table {
|
|
rows : Array[Byte]
|
|
mut n : Int
|
|
}
|
|
|
|
///|
|
|
pub fn Table::new() -> Table {
|
|
{ rows: [], n: 0 }
|
|
}
|
|
|
|
///|
|
|
fn Table::push32(self : Table, v : Int) -> Unit {
|
|
for k = 0; k < 4; k = k + 1 {
|
|
self.rows.push(((v >> (8 * k)) & 0xFF).to_byte())
|
|
}
|
|
}
|
|
|
|
///| An i32 field.
|
|
pub fn Table::int(self : Table, v : Int) -> Unit {
|
|
self.push32(v)
|
|
}
|
|
|
|
///| A string field.
|
|
pub fn Table::str(self : Table, s : String) -> Unit {
|
|
let b = utf8(s)
|
|
self.push32(b.length())
|
|
for i = 0; i < b.length(); i = i + 1 {
|
|
self.rows.push(b[i])
|
|
}
|
|
}
|
|
|
|
///| Finish one row.
|
|
pub fn Table::end_row(self : Table) -> Unit {
|
|
self.n = self.n + 1
|
|
}
|
|
|
|
///|
|
|
pub(all) enum HostTable {
|
|
Apps
|
|
Windows
|
|
Layouts
|
|
Modules
|
|
Instances
|
|
}
|
|
|
|
///|
|
|
pub fn publish(which : HostTable, t : Table) -> Unit {
|
|
// [u32 row count][rows], the encoding the host parses.
|
|
let n = t.n
|
|
let out : FixedArray[Byte] = FixedArray::make(4 + t.rows.length(), b'\x00')
|
|
for k = 0; k < 4; k = k + 1 {
|
|
out[k] = ((n >> (8 * k)) & 0xFF).to_byte()
|
|
}
|
|
for i = 0; i < t.rows.length(); i = i + 1 {
|
|
out[4 + i] = t.rows[i]
|
|
}
|
|
let id = match which {
|
|
Apps => 0U
|
|
Windows => 1U
|
|
Layouts => 2U
|
|
Modules => 3U
|
|
Instances => 4U
|
|
}
|
|
@hostpl.publish_table(id, out)
|
|
}
|
|
|
|
///|
|
|
pub fn publish_state(layout_mode : Int, inspector_on : Bool) -> Unit {
|
|
@hostpl.publish_state(layout_mode, inspector_on)
|
|
}
|
|
|
|
///| Something a module asked the host to do during a call.
|
|
pub(all) enum HostRequest {
|
|
Launch(String)
|
|
FocusWindow(Int)
|
|
SetLayout(Int)
|
|
SetInspector(Bool)
|
|
LoadModule(String)
|
|
UnloadInstance(String)
|
|
Browse
|
|
} derive(Eq, Debug)
|
|
|
|
///|
|
|
pub fn next_request() -> HostRequest? {
|
|
match @hostpl.next_request() {
|
|
None => None
|
|
Some(Launch(id)) => Some(Launch(id))
|
|
Some(FocusWindow(n)) => Some(FocusWindow(n))
|
|
Some(SetLayout(m)) => Some(SetLayout(m))
|
|
Some(SetInspector(on)) => Some(SetInspector(on))
|
|
Some(LoadModule(p)) => Some(LoadModule(p))
|
|
Some(UnloadInstance(s)) => Some(UnloadInstance(s))
|
|
Some(Browse) => Some(Browse)
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn list_wasm_files(dir : String) -> Array[String] {
|
|
@hostpl.list_wasm_files(dir)
|
|
}
|
|
|
|
///| An in-flight native file-chooser dialog.
|
|
pub(all) struct Picker {
|
|
fd : Int
|
|
buf : Array[Byte]
|
|
}
|
|
|
|
///|
|
|
pub fn Picker::start() -> Picker? {
|
|
if @hostpl.picker_start() {
|
|
Some({ fd: 0, buf: [] })
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn Picker::poll(_self : Picker) -> String?? {
|
|
match @hostpl.picker_poll() {
|
|
Open => None
|
|
Cancelled => Some(None)
|
|
Chosen(path) => Some(Some(path))
|
|
}
|
|
}
|