///| `chandra:host/plasmoids`: the panel modules. They run in this host, through compositor/wasm /// (the native compositor's module runtime, on the wasm.h API that libwasmtime implements too), /// so both builds serve modules the same capabilities from the same code. ///| compositor/wasm's shim: the rows behind table `which` (a LE u32 row count, then the rows). #borrow(data) extern "C" fn mp_host_set_table(which : Int, data : Bytes) = "mp_host_set_table" ///| Loaded modules by handle. let modules : Map[Int, @wasm.Plasmoid] = {} ///| The theme the modules see (`set-tokens`). let tokens : Ref[@theme.Tokens?] = Ref(None) ///| fn default_tokens() -> @theme.Tokens { let c : @types.Color = { r: b'\x00', g: b'\x00', b: b'\x00', a: b'\xff' } { name: "", palette: { desktop_top: c, desktop_bottom: c, surface: c, surface_hover: c, surface_popup: c, foreground: c, foreground_muted: c, accent: c, accent_foreground: c, border: c, }, font_size: 13, radius: 6, spacing: 8, panel_height: 44, } } ///| fn current_tokens() -> @theme.Tokens { match tokens.val { Some(t) => t None => default_tokens() } } // ---- values to and from the module types ---- ///| fn color_of(v : @wt.Val) -> @types.Color { { r: v.field("r").to_int().to_byte(), g: v.field("g").to_int().to_byte(), b: v.field("b").to_int().to_byte(), a: v.field("a").to_int().to_byte(), } } ///| fn tokens_of(v : @wt.Val) -> @theme.Tokens { let p = v.field("palette") { name: v.field("name").to_string(), palette: { desktop_top: color_of(p.field("desktop-top")), desktop_bottom: color_of(p.field("desktop-bottom")), surface: color_of(p.field("surface")), surface_hover: color_of(p.field("surface-hover")), surface_popup: color_of(p.field("surface-popup")), foreground: color_of(p.field("foreground")), foreground_muted: color_of(p.field("foreground-muted")), accent: color_of(p.field("accent")), accent_foreground: color_of(p.field("accent-foreground")), border: color_of(p.field("border")), }, font_size: v.field("font-size").to_uint(), radius: v.field("radius").to_uint(), spacing: v.field("spacing").to_uint(), panel_height: v.field("panel-height").to_uint(), } } ///| fn color_val(c : @types.Color) -> @wt.Val { Record([ ("r", U8(c.r.to_int())), ("g", U8(c.g.to_int())), ("b", U8(c.b.to_int())), ("a", U8(c.a.to_int())), ]) } ///| fn point_val(p : @types.Point) -> @wt.Val { Record([("x", S32(p.x)), ("y", S32(p.y))]) } ///| fn rect_val(r : @types.Rect) -> @wt.Val { Record([("x", S32(r.x)), ("y", S32(r.y)), ("w", U32(r.w)), ("h", U32(r.h))]) } ///| fn size_val(w : Int, h : Int) -> @wt.Val { Record([ ("w", U32(w.reinterpret_as_uint())), ("h", U32(h.reinterpret_as_uint())), ]) } ///| fn op_val(op : @types.DrawOp) -> @wt.Val { match op { FillRect(f) => Variant( "fill-rect", Some( Record([ ("rect", rect_val(f.rect)), ("color", color_val(f.color)), ("radius", U32(f.radius)), ]), ), ) StrokeRect(s) => Variant( "stroke-rect", Some( Record([ ("rect", rect_val(s.rect)), ("color", color_val(s.color)), ("width", U32(s.width)), ("radius", U32(s.radius)), ]), ), ) Line(l) => Variant( "line", Some( Record([ ("start", point_val(l.start)), ("end", point_val(l.end)), ("color", color_val(l.color)), ("width", U32(l.width)), ]), ), ) Circle(c) => Variant( "circle", Some( Record([ ("center", point_val(c.center)), ("radius", U32(c.radius)), ("color", color_val(c.color)), ("filled", Bool(c.filled)), ]), ), ) Text(t) => { let align = match t.style.align { LEFT => "left" CENTER => "center" RIGHT => "right" } Variant( "text", Some( Record([ ("origin", point_val(t.origin)), ("text", Str(t.text)), ( "style", Record([ ("size", U32(t.style.size)), ("color", color_val(t.style.color)), ("align", Enum(align)), ("bold", Bool(t.style.bold)), ]), ), ]), ), ) } PushClip(r) => Variant("push-clip", Some(rect_val(r))) PopClip => Variant("pop-clip", None) Image(i) => Variant( "image", Some(Record([("rect", rect_val(i.rect)), ("image", U32(i.image))])), ) } } ///| fn ops_val(ops : Array[@types.DrawOp]) -> @wt.Val { List(ops.map(op_val)) } ///| fn event_of(v : @wt.Val) -> @wasm.PlasmoidEvent? { guard v is Variant(name, payload) else { None } let xy = fn() -> (Int, Int) { match payload { Some(p) => (p.field("x").to_int(), p.field("y").to_int()) None => (0, 0) } } match name { "pointer-move" => { let (x, y) = xy() Some(PointerMove(x, y)) } "pointer-down" => { let (x, y) = xy() Some(PointerDown(x, y)) } "pointer-up" => { let (x, y) = xy() Some(PointerUp(x, y)) } "pointer-leave" => Some(PointerLeave) "focus-lost" => Some(FocusLost) "key-down" => match payload { Some(k) => Some(KeyDown(k.to_string())) None => None } _ => None } } ///| fn request_val(r : @wasm.HostRequest) -> @wt.Val { match r { Launch(s) => Variant("launch", Some(Str(s))) FocusWindow(n) => Variant("focus-window", Some(S32(n))) SetLayout(n) => Variant("set-layout", Some(S32(n))) SetInspector(b) => Variant("set-inspector", Some(Bool(b))) LoadModule(s) => Variant("load-module", Some(Str(s))) UnloadInstance(s) => Variant("unload-instance", Some(Str(s))) MoveInstance(slot, delta) => Variant("move-instance", Some(Record([("slot", Str(slot)), ("delta", S32(delta))]))) Browse => Variant("browse", None) Logout => Variant("logout", None) SetWallpaper(n) => Variant("set-wallpaper", Some(S32(n))) SetSessionSetting(index, on) => Variant( "set-session-setting", Some(Record([("index", S32(index)), ("on", Bool(on))])), ) Screenshot(n) => Variant("screenshot", Some(S32(n))) SetScreenOff(n) => Variant("set-screen-off", Some(S32(n))) SetSuspendAfter(n) => Variant("set-suspend-after", Some(S32(n))) PowerAction(n) => Variant("power-action", Some(S32(n))) Network(action, ssid, password) => Variant( "network", Some(Record([("action", S32(action)), ("ssid", Str(ssid)), ("password", Str(password))])), ) Shortcut(n) => Variant("shortcut", Some(S32(n))) } } ///| The file chooser "Add widget" opened, while it is open. let picker : Ref[@wasm.Picker?] = Ref(None) ///| fn with_module(a : Array[@wt.Val], f : (@wasm.Plasmoid) -> Array[@wt.Val], or : Array[@wt.Val]) -> Array[@wt.Val] { match modules.get(a[0].to_int()) { Some(m) => f(m) None => or } } ///| fn plasmoid_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] { [ ( "load", a => match @wasm.load(a[0].to_string(), current_tokens()) { Ok(p) => { modules.set(p.handle, p) [ Result( true, Some( Record([ ("handle", U32(p.handle.reinterpret_as_uint())), ("id", Str(p.id)), ("name", Str(p.name)), ("preferred", size_val(p.pref_w, p.pref_h)), ("min-width", U32(p.min_w.reinterpret_as_uint())), ("stretch", Bool(p.stretch)), ("imports", @wt.Val::strings(p.imports)), ]), ), ), ] } Err(e) => [Result(false, Some(Str(e)))] }, ), ( "unload", a => { let h = a[0].to_int() match modules.get(h) { Some(m) => { m.unload() modules.remove(h) } None => () } none }, ), ( "set-tokens", a => { tokens.val = Some(tokens_of(a[0])) none }, ), ( "render", a => with_module( a, m => { let b = a[1] [ops_val(m.render(b.field("w").to_int(), b.field("h").to_int(), current_tokens()))] }, [List([])], ), ), ( "popup", a => with_module( a, m => match m.popup() { Some(p) => [ Option( Some(Record([("box", size_val(p.w, p.h)), ("ops", ops_val(p.ops))])), ), ] None => [Option(None)] }, [Option(None)], ), ), ( "on-event", a => with_module( a, m => match event_of(a[2]) { Some(ev) => boolean(m.on_event(a[1].to_bool(), ev)) None => boolean(false) }, boolean(false), ), ), ( "config-items", a => with_module( a, m => [ List( m .config_items() .map(c => Record([ ("key", Str(c.key)), ("label", Str(c.label)), ("choices", @wt.Val::strings(c.choices)), ("value", Str(c.value)), ])), ), ], [List([])], ), ), ( "set-config", a => with_module( a, m => boolean(m.set_config(a[1].to_string(), a[2].to_string())), boolean(false), ), ), ( "publish-table", a => { mp_host_set_table(a[0].to_int(), a[1].to_bytes()) none }, ), ( "publish-state", a => { @wasm.publish_state(a[0].to_int(), a[1].to_bool()) none }, ), ( "publish-wallpaper", a => { @wasm.publish_wallpaper(a[0].to_int()) none }, ), ( "publish-session-settings", a => { @wasm.publish_session_settings(a[0].to_int()) none }, ), ( "publish-power", a => { @wasm.publish_power(a[0].to_int(), a[1].to_int()) none }, ), ( "publish-battery", a => { @wasm.publish_battery(a[0].to_int(), a[1].to_int(), a[2].to_int()) none }, ), ( "publish-screenshot", a => { @wasm.publish_screenshot(a[0].to_int()) none }, ), ( "publish-system", a => { let s = a[0] let i64 = fn(name : String) -> Int64 { match s.field(name) { U64(v) => v.reinterpret_as_int64() v => v.to_int().to_int64() } } @wasm.set_system({ cpu_permille: s.field("cpu-permille").to_int(), mem_used_kib: i64("mem-used-kib"), mem_total_kib: i64("mem-total-kib"), battery_percent: s.field("battery-percent").to_int(), charging: s.field("charging").to_bool(), uptime_secs: i64("uptime-secs"), rx_bps: i64("net-rx-bytes-per-sec"), tx_bps: i64("net-tx-bytes-per-sec"), load_centi: s.field("load-centi").to_int(), }) none }, ), ( "publish-windows", a => { @wasm.set_windows( a[0] .to_list() .map(w => { id: match w.field("id") { U64(v) => v v => v.to_int().to_uint64() }, title: w.field("title").to_string(), app_id: w.field("app-id").to_string(), focused: w.field("focused").to_bool(), }), ) none }, ), ( "next-request", _ => [ Option( match @wasm.next_request() { Some(r) => Some(request_val(r)) None => None }, ), ], ), ( "take-focus-request", _ => [ Option( match @wasm.take_focus_request() { Some(id) => Some(U64(id)) None => None }, ), ], ), ( "list-wasm-files", a => [@wt.Val::strings(@wasm.list_wasm_files(a[0].to_string()))], ), ( "picker-start", _ => { picker.val = @wasm.Picker::start() boolean(picker.val is Some(_)) }, ), ( "picker-poll", _ => match picker.val { None => [Variant("cancelled", None)] Some(p) => match p.poll() { None => [Variant("open", None)] Some(None) => { picker.val = None [Variant("cancelled", None)] } Some(Some(path)) => { picker.val = None [Variant("chosen", Some(Str(path)))] } } }, ), ] }