From f2e870c251f92d4220bd2141eb69da3beced5592 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 18:50:40 +0000 Subject: [PATCH] Task manager: draw each app's real icon from its .desktop file Each pill now shows the icon the app's .desktop file names (Icon=), looked up in the freedesktop icon themes, instead of the letter tile. The tile stays as the fallback for windows with no app id, no desktop file or no icon the host can read. - WIT: a new `icons` capability (`app-icon(app-id) -> u32`, a handle, never a path or pixels) and an `image(image-op)` draw op (appended, so existing modules keep their ABI). taskbar-widget and plasmoid-host import `icons`. Host raster gains `load-image(path)` and a `draw-image` op; the plasmoids host gets an icons table (6: app id, handle). - Compositor (shell/icons.mbt): window app id -> desktop file (resolve_app_id, so StartupWMClass and last-dotted-part matches work) -> Icon= -> theme lookup (XDG data dirs, ~/.icons, flatpak exports; hicolor, Adwaita, breeze; 48/64/32 px PNG first, then scalable SVG when the host can draw it, then other sizes, then /usr/share/pixmaps). Absolute Icon= paths are used as they are. Results are cached per app id; the table is published only when a module imports `icons`. - wasm host (host-wasm): PNG via the png crate, SVG via resvg (rendered sharp at each size), PNGs area-averaged to the drawn size; loads cached by path. - Native build: a small PNG decoder (compositor/renderer/png.mbt: inflate + filters, 1-16 bit, palettes with tRNS) and the Image op in the software renderer. SVG-only icons fall back to the tile there. The wasmer stub serves `icons.app-icon` from the same table. - Tests: PNG decoder fixtures (all filters, fixed/dynamic/stored blocks, palette), image scaling and drawing, icon lookup order, Icon= parsing, task module icon-or-tile (native and wasmtime), host raster PNG+SVG loading and drawing. Checked end to end with the wasm host: foot's hicolor icon shows on its pill. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hhmtDt52EjaW8jevGU --- .../chandra/desktop/plasmoid/ffi.mbt | 56 ++- .../chandra/desktop/plasmoid/ffi.mbt | 56 ++- .../interface/chandra/wm/decoration/ffi.mbt | 75 +-- .../chandra/desktop/plasmoid/ffi.mbt | 60 ++- .../chandra/desktop/plasmoid/ffi.mbt | 54 +- .../chandra/desktop/plasmoid/ffi.mbt | 52 +- .../chandra/desktop/plasmoid/ffi.mbt | 68 ++- .../chandra/desktop/plasmoid/ffi.mbt | 60 ++- .../chandra/desktop/plasmoid/ffi.mbt | 54 +- .../chandra/desktop/desktop-surface/ffi.mbt | 33 +- .../chandra/desktop/panel-layout/ffi.mbt | 43 +- .../chandra/desktop/plasmoid/ffi.mbt | 58 ++- .../chandra/desktop/plasmoid/moon.pkg | 1 + .../chandra/desktop/plasmoid/stub.mbt | 21 +- compositor/raster/moon.pkg | 1 + compositor/raster/raster_native.mbt | 23 + compositor/raster/raster_wasm.mbt | 16 + compositor/renderer/image.mbt | 88 ++++ compositor/renderer/pkg.generated.mbti | 6 + compositor/renderer/png.mbt | 434 ++++++++++++++++ compositor/renderer/png_wbtest.mbt | 71 +++ compositor/renderer/renderer.mbt | 2 + compositor/scene/pkg.generated.mbti | 2 + compositor/scene/scene.mbt | 2 + compositor/shell/apps.mbt | 11 +- compositor/shell/icons.mbt | 145 ++++++ compositor/shell/moon.pkg | 1 + compositor/shell/shell.mbt | 14 + compositor/wasm/wasm_host.mbt | 3 + compositor/wasm/wasm_native.mbt | 4 + compositor/wasm/wasm_stub.c | 33 +- compositor/wasm/wasm_test.mbt | 38 ++ docs/ARCHITECTURE.md | 2 +- host-wasm/Cargo.lock | 301 ++++++++++- host-wasm/Cargo.toml | 2 + host-wasm/src/main.rs | 4 + host-wasm/src/plasmoid.rs | 49 +- host-wasm/src/raster.rs | 206 +++++++- interface/chandra/desktop/icons/README.md | 3 + interface/chandra/desktop/icons/ffi.mbt | 14 + interface/chandra/desktop/icons/moon.pkg | 1 + interface/chandra/desktop/icons/top.mbt | 14 + .../chandra/desktop/types/pkg.generated.mbti | 6 + interface/chandra/desktop/types/top.mbt | 7 + interface/chandra/host/plasmoids/top.mbt | 11 +- interface/chandra/host/raster/ffi.mbt | 29 +- interface/chandra/host/raster/native_stub.mbt | 27 +- interface/chandra/host/raster/top.mbt | 472 ++++++++++-------- wit/capabilities.wit | 9 + wit/host/plasmoids.wit | 3 +- wit/host/raster.wit | 6 + wit/types.wit | 4 + wit/worlds.wit | 2 + 53 files changed, 2284 insertions(+), 473 deletions(-) create mode 100644 compositor/renderer/image.mbt create mode 100644 compositor/renderer/png.mbt create mode 100644 compositor/renderer/png_wbtest.mbt create mode 100644 compositor/shell/icons.mbt create mode 100644 interface/chandra/desktop/icons/README.md create mode 100644 interface/chandra/desktop/icons/ffi.mbt create mode 100644 interface/chandra/desktop/icons/moon.pkg create mode 100644 interface/chandra/desktop/icons/top.mbt diff --git a/components/clock-binary/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/clock-binary/gen/interface/chandra/desktop/plasmoid/ffi.mbt index 006f31f..98442e7 100644 --- a/components/clock-binary/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/clock-binary/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -369,17 +391,24 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } ///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) + +///| +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) + +///| +extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = +#|(func (param i32) (param i32) (result i32) +#| local.get 0 +#| local.get 1 call $moonbit.init_array16 +#| local.get 0) ///| extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) -///| -extern "wasm" fn mbt_ffi_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) - ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #|(func (param i32) (result i32) (local i32) @@ -392,18 +421,11 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = #|(func (param i32) (result i32) local.get 0) +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) + ///| extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) -///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) - -///| -extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = -#|(func (param i32) (param i32) (result i32) -#| local.get 0 -#| local.get 1 call $moonbit.init_array16 -#| local.get 0) - diff --git a/components/clock-digital/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/clock-digital/gen/interface/chandra/desktop/plasmoid/ffi.mbt index b229d5d..b8f0a3f 100644 --- a/components/clock-digital/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/clock-digital/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -368,10 +390,22 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } +///| +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) + ///| extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = #|(func (param i32) (result i32) local.get 0 i32.load) +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) + +///| +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) + ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #|(func (param i32) (result i32) (local i32) @@ -379,27 +413,10 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #| local.tee 1 i32.const 0 call $moonbit.init_array8 #| local.get 1) -///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) - -///| -extern "wasm" fn mbt_ffi_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) - ///| extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) -///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) - -///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) - ///| extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #|(func (param i32) (param i32) (result i32) @@ -407,3 +424,8 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| local.get 1 call $moonbit.init_array16 #| local.get 0) +///| +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) + diff --git a/components/decoration-breeze/gen/interface/chandra/wm/decoration/ffi.mbt b/components/decoration-breeze/gen/interface/chandra/wm/decoration/ffi.mbt index 0683b05..bbd6957 100644 --- a/components/decoration-breeze/gen/interface/chandra/wm/decoration/ffi.mbt +++ b/components/decoration-breeze/gen/interface/chandra/wm/decoration/ffi.mbt @@ -117,6 +117,16 @@ pub fn wasmExportRender(p0 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -125,10 +135,10 @@ pub fn wasmExportRender(p0 : Int) -> Int { mbt_ffi_store32((return_area) + 20, ((result1).ops).length()) mbt_ffi_store32((return_area) + 16, address) - let address8 = mbt_ffi_malloc(((result1).hit_regions).length() * 20); - for index9 = 0; index9 < ((result1).hit_regions).length(); index9 = index9 + 1 { - let iter_elem : HitRegion = ((result1).hit_regions)[(index9)] - let iter_base = address8 + (index9 * 20); + let address9 = mbt_ffi_malloc(((result1).hit_regions).length() * 20); + for index10 = 0; index10 < ((result1).hit_regions).length(); index10 = index10 + 1 { + let iter_elem : HitRegion = ((result1).hit_regions)[(index10)] + let iter_base = address9 + (index10 * 20); mbt_ffi_store32((iter_base) + 0, ((iter_elem).rect).x) mbt_ffi_store32((iter_base) + 4, ((iter_elem).rect).y) mbt_ffi_store32((iter_base) + 8, (((iter_elem).rect).w).reinterpret_as_int()) @@ -137,7 +147,7 @@ pub fn wasmExportRender(p0 : Int) -> Int { } mbt_ffi_store32((return_area) + 28, ((result1).hit_regions).length()) - mbt_ffi_store32((return_area) + 24, address8) + mbt_ffi_store32((return_area) + 24, address9) let ret = return_area return ret @@ -161,6 +171,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -170,6 +181,33 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } +///| +extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = +#|(func (param i32) (param i32) (result i32) +#| local.get 0 +#| local.get 1 call $moonbit.init_array16 +#| local.get 0) + +///| +extern "wasm" fn mbt_ffi_load64(offset : Int) -> Int64 = +#|(func (param i32) (result i64) local.get 0 i64.load) + +///| +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) + +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) + +///| +extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) + +///| +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) + ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #|(func (param i32) (result i32) (local i32) @@ -181,33 +219,6 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) -///| -extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = -#|(func (param i32) (param i32) (result i32) -#| local.get 0 -#| local.get 1 call $moonbit.init_array16 -#| local.get 0) - -///| -extern "wasm" fn mbt_ffi_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) - -///| -extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) - -///| -extern "wasm" fn mbt_ffi_load64(offset : Int) -> Int64 = -#|(func (param i32) (result i64) local.get 0 i64.load) - -///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) - -///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) - ///| #owned(str) extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = diff --git a/components/dock/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/dock/gen/interface/chandra/desktop/plasmoid/ffi.mbt index ec871b3..bea5099 100644 --- a/components/dock/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/dock/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -369,33 +391,22 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } ///| -extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = -#|(func (param i32) (param i32) (result i32) -#| local.get 0 -#| local.get 1 call $moonbit.init_array16 -#| local.get 0) +extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) + +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) ///| #owned(str) extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = #|(func (param i32) (result i32) local.get 0) -///| -extern "wasm" fn mbt_ffi_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) - ///| extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = #|(func (param i32) (result i32) local.get 0 i32.load) -///| -extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) - -///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) - ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #|(func (param i32) (result i32) (local i32) @@ -404,6 +415,17 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #| local.get 1) ///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) + +///| +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) + +///| +extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = +#|(func (param i32) (param i32) (result i32) +#| local.get 0 +#| local.get 1 call $moonbit.init_array16 +#| local.get 0) diff --git a/components/inspector/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/inspector/gen/interface/chandra/desktop/plasmoid/ffi.mbt index 5bf62d9..5d6c903 100644 --- a/components/inspector/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/inspector/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -368,24 +390,14 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } -///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) - ///| extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = #|(func (param i32) (result i32) local.get 0 i32.load) ///| -extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = -#|(func (param i32) (param i32) (result i32) -#| local.get 0 -#| local.get 1 call $moonbit.init_array16 -#| local.get 0) - -///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = @@ -399,11 +411,21 @@ extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) ///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) ///| extern "wasm" fn mbt_ffi_free(position : Int) = #|(func (param i32) local.get 0 call $moonbit.decref) +///| +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) + +///| +extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = +#|(func (param i32) (param i32) (result i32) +#| local.get 0 +#| local.get 1 call $moonbit.init_array16 +#| local.get 0) + diff --git a/components/launcher/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/launcher/gen/interface/chandra/desktop/plasmoid/ffi.mbt index ec871b3..9cf01db 100644 --- a/components/launcher/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/launcher/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -368,6 +390,10 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } +///| +extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) + ///| extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #|(func (param i32) (param i32) (result i32) @@ -376,26 +402,26 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| local.get 0) ///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) ///| extern "wasm" fn mbt_ffi_free(position : Int) = #|(func (param i32) local.get 0 call $moonbit.decref) -///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) - -///| -extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) - ///| extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) +///| +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) + +///| +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) + ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #|(func (param i32) (result i32) (local i32) @@ -403,7 +429,3 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #| local.tee 1 i32.const 0 call $moonbit.init_array8 #| local.get 1) -///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) - diff --git a/components/layout/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/layout/gen/interface/chandra/desktop/plasmoid/ffi.mbt index 3bd0ddc..feb60d5 100644 --- a/components/layout/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/layout/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -368,27 +390,6 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } -///| -extern "wasm" fn mbt_ffi_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) - -///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) - -///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) - -///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) - -///| -extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) - ///| extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #|(func (param i32) (param i32) (result i32) @@ -397,8 +398,17 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| local.get 0) ///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) + +///| +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) + +///| +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = @@ -407,3 +417,15 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #| local.tee 1 i32.const 0 call $moonbit.init_array8 #| local.get 1) +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) + +///| +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) + +///| +extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) + diff --git a/components/logout/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/logout/gen/interface/chandra/desktop/plasmoid/ffi.mbt index 08b246c..8f96308 100644 --- a/components/logout/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/logout/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -373,13 +395,28 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = #|(func (param i32) (result i32) local.get 0) +///| +extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = +#|(func (param i32) (result i32) (local i32) +#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc +#| local.tee 1 i32.const 0 call $moonbit.init_array8 +#| local.get 1) + +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) + ///| extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) ///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) + +///| +extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) ///| extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = @@ -389,21 +426,6 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| local.get 0) ///| -extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = -#|(func (param i32) (result i32) (local i32) -#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc -#| local.tee 1 i32.const 0 call $moonbit.init_array8 -#| local.get 1) - -///| -extern "wasm" fn mbt_ffi_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) - -///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) - -///| -extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) diff --git a/components/session-settings/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/session-settings/gen/interface/chandra/desktop/plasmoid/ffi.mbt index dd4f26a..e084bab 100644 --- a/components/session-settings/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/session-settings/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -369,32 +391,29 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } ///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) ///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) ///| extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) + ///| extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = #|(func (param i32) (result i32) local.get 0 i32.load) ///| -extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = -#|(func (param i32) (result i32) (local i32) -#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc -#| local.tee 1 i32.const 0 call $moonbit.init_array8 -#| local.get 1) - -///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) ///| extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = @@ -404,6 +423,9 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| local.get 0) ///| -extern "wasm" fn mbt_ffi_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) +extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = +#|(func (param i32) (result i32) (local i32) +#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc +#| local.tee 1 i32.const 0 call $moonbit.init_array8 +#| local.get 1) diff --git a/components/shell/gen/interface/chandra/desktop/desktop-surface/ffi.mbt b/components/shell/gen/interface/chandra/desktop/desktop-surface/ffi.mbt index 7ad118c..f1cbd00 100644 --- a/components/shell/gen/interface/chandra/desktop/desktop-surface/ffi.mbt +++ b/components/shell/gen/interface/chandra/desktop/desktop-surface/ffi.mbt @@ -99,6 +99,16 @@ pub fn wasmExportRenderBackground(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -129,6 +139,7 @@ pub fn wasmExportRenderBackgroundPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -138,8 +149,9 @@ pub fn wasmExportRenderBackgroundPostReturn(p0 : Int) -> Unit { } ///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) ///| extern "wasm" fn mbt_ffi_free(position : Int) = @@ -150,12 +162,8 @@ extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) ///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) - -///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = @@ -165,7 +173,10 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #| local.get 1) ///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) + +///| +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) diff --git a/components/shell/gen/interface/chandra/desktop/panel-layout/ffi.mbt b/components/shell/gen/interface/chandra/desktop/panel-layout/ffi.mbt index ed9209f..20179c2 100644 --- a/components/shell/gen/interface/chandra/desktop/panel-layout/ffi.mbt +++ b/components/shell/gen/interface/chandra/desktop/panel-layout/ffi.mbt @@ -154,6 +154,16 @@ pub fn wasmExportRenderPanel(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, p PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -184,6 +194,7 @@ pub fn wasmExportRenderPanelPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -211,13 +222,6 @@ pub fn wasmExportPopupPosition(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) -///| -extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = -#|(func (param i32) (result i32) (local i32) -#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc -#| local.tee 1 i32.const 0 call $moonbit.init_array8 -#| local.get 1) - ///| extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #|(func (param i32) (param i32) (result i32) @@ -225,24 +229,31 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| local.get 1 call $moonbit.init_array16 #| local.get 0) -///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) - -///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) - ///| extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = #|(func (param i32) (result i32) local.get 0 i32.load8_u) +///| +extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = +#|(func (param i32) (result i32) (local i32) +#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc +#| local.tee 1 i32.const 0 call $moonbit.init_array8 +#| local.get 1) + ///| extern "wasm" fn mbt_ffi_free(position : Int) = #|(func (param i32) local.get 0 call $moonbit.decref) +///| +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) + ///| extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = #|(func (param i32) (result i32) local.get 0 i32.load) +///| +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) + diff --git a/components/tasks/gen/interface/chandra/desktop/plasmoid/ffi.mbt b/components/tasks/gen/interface/chandra/desktop/plasmoid/ffi.mbt index 2d7f52f..70808bd 100644 --- a/components/tasks/gen/interface/chandra/desktop/plasmoid/ffi.mbt +++ b/components/tasks/gen/interface/chandra/desktop/plasmoid/ffi.mbt @@ -130,6 +130,16 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload6) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int()) + () } } @@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -278,6 +289,16 @@ pub fn wasmExportPopup() -> Int { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + Image(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { } 5 => () 6 => () + 7 => () _ => panic() } @@ -372,18 +394,28 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) +///| +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) + +///| +extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = +#|(func (param i32) (param i32) (result i32) +#| local.get 0 +#| local.get 1 call $moonbit.init_array16 +#| local.get 0) + ///| extern "wasm" fn mbt_ffi_free(position : Int) = #|(func (param i32) local.get 0 call $moonbit.decref) ///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) +extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) ///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) +extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load8_u) ///| extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = @@ -393,17 +425,7 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #| local.get 1) ///| -extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load8_u) - -///| -extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) - -///| -extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = -#|(func (param i32) (param i32) (result i32) -#| local.get 0 -#| local.get 1 call $moonbit.init_array16 -#| local.get 0) +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) diff --git a/components/tasks/gen/interface/chandra/desktop/plasmoid/moon.pkg b/components/tasks/gen/interface/chandra/desktop/plasmoid/moon.pkg index 02c2bfd..90d417f 100644 --- a/components/tasks/gen/interface/chandra/desktop/plasmoid/moon.pkg +++ b/components/tasks/gen/interface/chandra/desktop/plasmoid/moon.pkg @@ -2,6 +2,7 @@ import { "chandra/desktop/interface/chandra/desktop/types" @types, "chandra/desktop/interface/chandra/desktop/theme" @theme, "chandra/desktop/interface/chandra/desktop/windows" @windows, + "chandra/desktop/interface/chandra/desktop/icons" @icons, "chandra/desktop/lib/ui" @ui, "chandra/desktop/lib/dock" @dock, } diff --git a/components/tasks/gen/interface/chandra/desktop/plasmoid/stub.mbt b/components/tasks/gen/interface/chandra/desktop/plasmoid/stub.mbt index 3fbfff1..a0822a8 100644 --- a/components/tasks/gen/interface/chandra/desktop/plasmoid/stub.mbt +++ b/components/tasks/gen/interface/chandra/desktop/plasmoid/stub.mbt @@ -1,6 +1,7 @@ ///| Implementation of `chandra:desktop/plasmoid` for the task manager: one pill per open /// window, in the order the windows were opened (so the bar doesn't reshuffle when focus changes). -/// Each pill starts with the app's icon (the same coloured tile the dock shows for that app id). +/// Each pill starts with the app's icon: the one its .desktop file names, from the icon theme (the +/// `icons` capability), else the coloured letter tile the dock shows for that app id. /// The focused window's pill is lit, a minimized window's is dimmed; a click focuses the window and /// restores it if it was minimized. When the bar is too narrow the pills shrink to fit, and below /// `icon_only_w` they show just the icon. @@ -33,6 +34,17 @@ fn app_ids() -> Map[UInt, String] { m } +///| The app's icon `s` pixels square centred on (cx, cy): its theme icon when the host has one, +/// else the letter tile (keyed by the app id, or the title for windows without one). +fn pill_icon(cx : Int, cy : Int, s : Int, app : String, title : String) -> Array[@types.DrawOp] { + let handle = if app.length() > 0 { @icons.app_icon(app) } else { 0U } + if handle != 0U { + return [Image({ rect: @ui.rect(cx - s / 2, cy - s / 2, s, s), image: handle })] + } + let key = if app.length() > 0 { app } else { title } + @dock.app_icon(cx, cy, s, key, @dock.app_glyph(app, title)) +} + ///| The width the module was last drawn at, and the pill the pointer is over (-1: none). let last_w : Ref[Int] = { val: 300 } @@ -147,14 +159,11 @@ pub fn render(size : @types.Size) -> Array[@types.DrawOp] { } let title = if win.title.length() > 0 { win.title } else { "(untitled)" } let app = ids.get(win.id).unwrap_or("") - let glyph = @dock.app_glyph(app, title) - // Windows without an app id still get a stable colour, keyed by their title. - let key = if app.length() > 0 { app } else { title } let isz = icon_size(h) if pw < icon_only_w { - ops.append(@dock.app_icon(x + pw / 2, h / 2, isz, key, glyph)) + ops.append(pill_icon(x + pw / 2, h / 2, isz, app, title)) } else { - ops.append(@dock.app_icon(x + 6 + isz / 2, h / 2, isz, key, glyph)) + ops.append(pill_icon(x + 6 + isz / 2, h / 2, isz, app, title)) let tx = x + 6 + isz + 6 ops.push(@ui.text(tx, h / 2, fit_text(title, fs, x + pw - 8 - tx), fs, fg, bold=focused)) } diff --git a/compositor/raster/moon.pkg b/compositor/raster/moon.pkg index f0857db..fcb2293 100644 --- a/compositor/raster/moon.pkg +++ b/compositor/raster/moon.pkg @@ -2,6 +2,7 @@ import { "moonbitlang/core/builtin", "chandra/desktop/compositor/scene" @scene, "chandra/desktop/compositor/renderer" @renderer, + "chandra/desktop/compositor/sys" @sys, "chandra/desktop/interface/chandra/host/raster" @hostraster, } diff --git a/compositor/raster/raster_native.mbt b/compositor/raster/raster_native.mbt index 715a35f..5b0df7a 100644 --- a/compositor/raster/raster_native.mbt +++ b/compositor/raster/raster_native.mbt @@ -10,3 +10,26 @@ pub fn submit(_scene : @scene.SceneGraph) -> Unit { pub fn read_frame(_width : Int, _height : Int) -> @renderer.Pixmap { abort("the host rasteriser is only available to the wasm compositor") } + +///| Images already loaded, by path (0: it failed, don't try again). +let loaded_images : Map[String, UInt] = {} + +///| Load the image file at `path` for `Image` draw ops: a handle, or 0 when it can't be decoded. +/// The software renderer only reads PNG (an SVG icon gets 0, and the caller's fallback). +pub fn load_image(path : String) -> UInt { + match loaded_images.get(path) { + Some(h) => h + None => { + let h = if path.has_suffix(".png") { + match @renderer.decode_png(@sys.read_file_bytes(path)) { + Some(p) => @renderer.add_image(p) + None => 0U + } + } else { + 0U + } + loaded_images.set(path, h) + h + } + } +} diff --git a/compositor/raster/raster_wasm.mbt b/compositor/raster/raster_wasm.mbt index 89a919f..299ead8 100644 --- a/compositor/raster/raster_wasm.mbt +++ b/compositor/raster/raster_wasm.mbt @@ -36,6 +36,7 @@ fn conv_op(op : @scene.DrawOp) -> @hostraster.Op { DrawText({ x, y, text, size, color: conv_color(c), align: conv_align(align), bold, mono: true }) PushClip(r) => PushClip(conv_rect(r)) PopClip => PopClip + Image(r, image) => DrawImage({ area: conv_rect(r), image }) } } @@ -95,3 +96,18 @@ pub fn read_frame(width : Int, height : Int) -> @renderer.Pixmap { let data = @hostraster.read_frame() { width, height, data } } + +///| Images already loaded, by path (0: it failed, don't ask the host again). +let loaded_images : Map[String, UInt] = {} + +///| Have the host load the PNG or SVG file at `path` for `Image` draw ops: a handle, or 0. +pub fn load_image(path : String) -> UInt { + match loaded_images.get(path) { + Some(h) => h + None => { + let h = @hostraster.load_image(path) + loaded_images.set(path, h) + h + } + } +} diff --git a/compositor/renderer/image.mbt b/compositor/renderer/image.mbt new file mode 100644 index 0000000..1bb0ea5 --- /dev/null +++ b/compositor/renderer/image.mbt @@ -0,0 +1,88 @@ +///| Images the compositor loaded (app icons) and the `Image` draw op that shows them. + +///| Loaded images; handle `i + 1` is `images[i]` (0 means "no image"). +let images : Array[Pixmap] = [] + +///| Each image scaled to the sizes it has been drawn at, by (handle, w, h). +let scaled : Map[(UInt, Int, Int), Pixmap] = {} + +///| Keep `p` for `Image` ops and return its handle (never 0). +pub fn add_image(p : Pixmap) -> UInt { + images.push(p) + images.length().reinterpret_as_uint() +} + +///| Scale `src` to `w`x`h`: each target pixel averages the source pixels it covers (alpha +/// weighted, so transparent edges don't darken), or takes the nearest one when enlarging. +pub fn scale_pixmap(src : Pixmap, w : Int, h : Int) -> Pixmap { + let out = Pixmap::new(w, h) + for y = 0; y < h; y = y + 1 { + let y0 = y * src.height / h + let y1 = { let e = (y + 1) * src.height / h; if e > y0 { e } else { y0 + 1 } } + for x = 0; x < w; x = x + 1 { + let x0 = x * src.width / w + let x1 = { let e = (x + 1) * src.width / w; if e > x0 { e } else { x0 + 1 } } + let mut r = 0 + let mut g = 0 + let mut b = 0 + let mut a = 0 + let mut n = 0 + for sy = y0; sy < y1; sy = sy + 1 { + for sx = x0; sx < x1; sx = sx + 1 { + let o = (sy * src.width + sx) * 4 + let pa = src.data[o + 3].to_int() + r = r + src.data[o].to_int() * pa + g = g + src.data[o + 1].to_int() * pa + b = b + src.data[o + 2].to_int() * pa + a = a + pa + n = n + 1 + } + } + let o = (y * w + x) * 4 + if a > 0 { + out.data[o] = (r / a).to_byte() + out.data[o + 1] = (g / a).to_byte() + out.data[o + 2] = (b / a).to_byte() + out.data[o + 3] = (a / n).to_byte() + } + } + } + out +} + +///| Draw image `handle` scaled into `r` (unknown handles draw nothing). +fn draw_image(dst : Pixmap, handle : UInt, r : @scene.Rect, clip : @scene.Rect?) -> Unit { + let i = handle.reinterpret_as_int() - 1 + if i < 0 || i >= images.length() || r.w <= 0 || r.h <= 0 || r.w > 1024 || r.h > 1024 { + return + } + let key = (handle, r.w, r.h) + let img = match scaled.get(key) { + Some(p) => p + None => { + let src = images[i] + let p = if src.width == r.w && src.height == r.h { + src + } else { + scale_pixmap(src, r.w, r.h) + } + scaled.set(key, p) + p + } + } + for y = 0; y < r.h; y = y + 1 { + for x = 0; x < r.w; x = x + 1 { + let o = (y * r.w + x) * 4 + blend_pixel( + dst, + r.x + x, + r.y + y, + img.data[o].to_int(), + img.data[o + 1].to_int(), + img.data[o + 2].to_int(), + img.data[o + 3].to_int(), + clip, + ) + } + } +} diff --git a/compositor/renderer/pkg.generated.mbti b/compositor/renderer/pkg.generated.mbti index 827ef08..bd9eef0 100644 --- a/compositor/renderer/pkg.generated.mbti +++ b/compositor/renderer/pkg.generated.mbti @@ -6,8 +6,12 @@ import { } // Values +pub fn add_image(Pixmap) -> UInt32 + pub fn blit_shm_surface(Pixmap, @scene.ShmBufferSnapshot, @scene.Rect) -> Unit +pub fn decode_png(Bytes) -> Pixmap? + pub fn draw_ops(Pixmap, Array[@scene.DrawOp], Int32, Int32) -> Unit pub fn encode_png(Pixmap) -> Bytes @@ -16,6 +20,8 @@ pub fn encode_ppm(Pixmap) -> Bytes pub fn render_scene(@scene.SceneGraph) -> Pixmap +pub fn scale_pixmap(Pixmap, Int32, Int32) -> Pixmap + pub fn set_glyph_source((Int32, Int32, Bool, Bool) -> Glyph) -> Unit pub fn text_width(String, Int32, Bool, Bool) -> Int32 diff --git a/compositor/renderer/png.mbt b/compositor/renderer/png.mbt new file mode 100644 index 0000000..d25c12f --- /dev/null +++ b/compositor/renderer/png.mbt @@ -0,0 +1,434 @@ +///| A small PNG decoder for app icons (the native build has no image library). Handles what icon +/// themes ship: 8-bit grey, grey+alpha, RGB, RGBA (16-bit read as 8), palettes of 1-8 bits with +/// tRNS alpha. Interlaced images and anything over 1024 px a side are refused. + +///| +priv suberror PngError + +///| Inflate (RFC 1951) state over a zlib stream's deflate data. +priv struct Inflate { + src : Bytes + mut pos : Int + mut bitbuf : Int + mut bitcnt : Int + out : Array[Byte] +} + +///| +fn Inflate::bits(self : Inflate, need : Int) -> Int raise PngError { + let mut val = self.bitbuf + while self.bitcnt < need { + if self.pos >= self.src.length() { + raise PngError + } + val = val | (self.src[self.pos].to_int() << self.bitcnt) + self.pos = self.pos + 1 + self.bitcnt = self.bitcnt + 8 + } + self.bitbuf = val >> need + self.bitcnt = self.bitcnt - need + val & ((1 << need) - 1) +} + +///| A canonical Huffman code: how many codes of each length, and the symbols in code order. +priv struct Huffman { + count : FixedArray[Int] + symbol : FixedArray[Int] +} + +///| +fn Huffman::build(lengths : FixedArray[Int], start : Int, n : Int) -> Huffman raise PngError { + let count = FixedArray::make(16, 0) + let symbol = FixedArray::make(n, 0) + for i = 0; i < n; i = i + 1 { + count[lengths[start + i]] = count[lengths[start + i]] + 1 + } + let mut left = 1 + for len = 1; len < 16; len = len + 1 { + left = (left << 1) - count[len] + if left < 0 { + raise PngError + } + } + let offs = FixedArray::make(16, 0) + for len = 1; len < 15; len = len + 1 { + offs[len + 1] = offs[len] + count[len] + } + for i = 0; i < n; i = i + 1 { + let l = lengths[start + i] + if l != 0 { + symbol[offs[l]] = i + offs[l] = offs[l] + 1 + } + } + { count, symbol } +} + +///| +fn Inflate::decode(self : Inflate, h : Huffman) -> Int raise PngError { + let mut code = 0 + let mut first = 0 + let mut index = 0 + for len = 1; len < 16; len = len + 1 { + code = code | self.bits(1) + let c = h.count[len] + if code - c < first { + return h.symbol[index + (code - first)] + } + index = index + c + first = (first + c) << 1 + code = code << 1 + } + raise PngError +} + +///| +let len_base : FixedArray[Int] = [ + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, + 99, 115, 131, 163, 195, 227, 258, +] + +///| +let len_extra : FixedArray[Int] = [ + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, + 5, 0, +] + +///| +let dist_base : FixedArray[Int] = [ + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, + 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, +] + +///| +let dist_extra : FixedArray[Int] = [ + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, + 12, 12, 13, 13, +] + +///| Decode one compressed block's symbols until end-of-block; `limit` caps the output size. +fn Inflate::codes( + self : Inflate, + lencode : Huffman, + distcode : Huffman, + limit : Int, +) -> Unit raise PngError { + while true { + let sym = self.decode(lencode) + if sym < 256 { + self.out.push(sym.to_byte()) + } else if sym == 256 { + return + } else { + let s = sym - 257 + if s >= 29 { + raise PngError + } + let len = len_base[s] + self.bits(len_extra[s]) + let ds = self.decode(distcode) + if ds >= 30 { + raise PngError + } + let dist = dist_base[ds] + self.bits(dist_extra[ds]) + let n = self.out.length() + if dist > n || n + len > limit { + raise PngError + } + for k = 0; k < len; k = k + 1 { + self.out.push(self.out[n - dist + k]) + } + } + if self.out.length() > limit { + raise PngError + } + } +} + +///| +fn Inflate::stored(self : Inflate) -> Unit raise PngError { + self.bitbuf = 0 + self.bitcnt = 0 + if self.pos + 4 > self.src.length() { + raise PngError + } + let len = self.src[self.pos].to_int() | (self.src[self.pos + 1].to_int() << 8) + let nlen = self.src[self.pos + 2].to_int() | (self.src[self.pos + 3].to_int() << 8) + self.pos = self.pos + 4 + if len != (nlen ^ 0xFFFF) || self.pos + len > self.src.length() { + raise PngError + } + for k = 0; k < len; k = k + 1 { + self.out.push(self.src[self.pos + k]) + } + self.pos = self.pos + len +} + +///| +fn Inflate::fixed(self : Inflate, limit : Int) -> Unit raise PngError { + let l = FixedArray::make(288 + 30, 5) + for i = 0; i < 288; i = i + 1 { + l[i] = if i < 144 { 8 } else if i < 256 { 9 } else if i < 280 { 7 } else { 8 } + } + self.codes(Huffman::build(l, 0, 288), Huffman::build(l, 288, 30), limit) +} + +///| +let code_order : FixedArray[Int] = [ + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, +] + +///| +fn Inflate::dynamic(self : Inflate, limit : Int) -> Unit raise PngError { + let nlen = self.bits(5) + 257 + let ndist = self.bits(5) + 1 + let ncode = self.bits(4) + 4 + if nlen > 286 || ndist > 30 { + raise PngError + } + let lengths = FixedArray::make(320, 0) + for i = 0; i < ncode; i = i + 1 { + lengths[code_order[i]] = self.bits(3) + } + let lencode = Huffman::build(lengths, 0, 19) + let mut idx = 0 + while idx < nlen + ndist { + let sym = self.decode(lencode) + if sym < 16 { + lengths[idx] = sym + idx = idx + 1 + } else { + let mut len = 0 + let rep = if sym == 16 { + if idx == 0 { + raise PngError + } + len = lengths[idx - 1] + 3 + self.bits(2) + } else if sym == 17 { + 3 + self.bits(3) + } else { + 11 + self.bits(7) + } + if idx + rep > nlen + ndist { + raise PngError + } + for k = 0; k < rep; k = k + 1 { + lengths[idx] = len + idx = idx + 1 + } + } + } + if lengths[256] == 0 { + raise PngError + } + let lc = Huffman::build(lengths, 0, nlen) + let dc = Huffman::build(lengths, nlen, ndist) + self.codes(lc, dc, limit) +} + +///| Inflate a zlib stream (header checked, checksum ignored), refusing output beyond `limit`. +fn zlib_inflate(src : Bytes, limit : Int) -> Array[Byte] raise PngError { + if src.length() < 2 || (src[0].to_int() & 0x0F) != 8 || + ((src[0].to_int() << 8) | src[1].to_int()) % 31 != 0 { + raise PngError + } + let s : Inflate = { src, pos: 2, bitbuf: 0, bitcnt: 0, out: [] } + while true { + let last = s.bits(1) + match s.bits(2) { + 0 => s.stored() + 1 => s.fixed(limit) + 2 => s.dynamic(limit) + _ => raise PngError + } + if s.out.length() > limit { + raise PngError + } + if last == 1 { + break + } + } + s.out +} + +///| +fn be32(b : Bytes, o : Int) -> Int { + (b[o].to_int() << 24) | (b[o + 1].to_int() << 16) | (b[o + 2].to_int() << 8) | + b[o + 3].to_int() +} + +///| +fn paeth(a : Int, b : Int, c : Int) -> Int { + let p = a + b - c + let pa = (p - a).abs() + let pb = (p - b).abs() + let pc = (p - c).abs() + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } +} + +///| Decode a PNG file into an RGBA pixmap; None when it is not a PNG this decoder handles. +pub fn decode_png(data : Bytes) -> Pixmap? { + decode_png_inner(data) catch { + _ => None + } +} + +///| +fn decode_png_inner(data : Bytes) -> Pixmap? raise PngError { + let sig : FixedArray[Int] = [137, 80, 78, 71, 13, 10, 26, 10] + if data.length() < 33 { + return None + } + for i = 0; i < 8; i = i + 1 { + if data[i].to_int() != sig[i] { + return None + } + } + let mut w = 0 + let mut h = 0 + let mut depth = 0 + let mut ctype = 0 + let mut interlace = 0 + let mut palette : Array[Byte] = [] + let mut trns : Array[Byte] = [] + let idat : Array[Byte] = [] + let mut pos = 8 + while pos + 8 <= data.length() { + let len = be32(data, pos) + if len < 0 || pos + 12 + len > data.length() { + return None + } + let kind = data[pos + 4:pos + 8].to_bytes() + let body = pos + 8 + if kind == b"IHDR" && len >= 13 { + w = be32(data, body) + h = be32(data, body + 4) + depth = data[body + 8].to_int() + ctype = data[body + 9].to_int() + interlace = data[body + 12].to_int() + } else if kind == b"PLTE" { + palette = data[body:body + len].to_bytes().to_array() + } else if kind == b"tRNS" { + trns = data[body:body + len].to_bytes().to_array() + } else if kind == b"IDAT" { + for k = 0; k < len; k = k + 1 { + idat.push(data[body + k]) + } + } else if kind == b"IEND" { + break + } + pos = body + len + 4 + } + if w <= 0 || h <= 0 || w > 1024 || h > 1024 || interlace != 0 { + return None + } + let channels = match ctype { + 0 => 1 + 2 => 3 + 3 => 1 + 4 => 2 + 6 => 4 + _ => return None + } + let ok_depth = match ctype { + 0 | 3 => depth == 1 || depth == 2 || depth == 4 || depth == 8 || (ctype == 0 && depth == 16) + _ => depth == 8 || depth == 16 + } + if !ok_depth { + return None + } + let bits_pp = channels * depth + let bpp = if bits_pp < 8 { 1 } else { bits_pp / 8 } + let stride = (w * bits_pp + 7) / 8 + let raw = zlib_inflate(Bytes::from_array(idat), h * (stride + 1)) + if raw.length() < h * (stride + 1) { + return None + } + // Undo the per-row filters in place, into `cur` rows of `stride` bytes. + let px = FixedArray::make(h * stride, b'\x00') + for y = 0; y < h; y = y + 1 { + let f = raw[y * (stride + 1)].to_int() + let src = y * (stride + 1) + 1 + let row = y * stride + for x = 0; x < stride; x = x + 1 { + let a = if x >= bpp { px[row + x - bpp].to_int() } else { 0 } + let b = if y > 0 { px[row - stride + x].to_int() } else { 0 } + let c = if x >= bpp && y > 0 { px[row - stride + x - bpp].to_int() } else { 0 } + let v = raw[src + x].to_int() + let out = match f { + 0 => v + 1 => v + a + 2 => v + b + 3 => v + (a + b) / 2 + 4 => v + paeth(a, b, c) + _ => raise PngError + } + px[row + x] = (out & 0xFF).to_byte() + } + } + let img = Pixmap::new(w, h) + // Sample `i` (0-based within the row) of `n` bits, for 1/2/4/8-bit rows. + let sample = fn(row : Int, i : Int) -> Int { + if depth == 8 { + px[row + i].to_int() + } else if depth == 16 { + px[row + i * 2].to_int() + } else { + let bit = i * depth + let byte = px[row + bit / 8].to_int() + (byte >> (8 - depth - bit % 8)) & ((1 << depth) - 1) + } + } + let scale = fn(v : Int) -> Int { + match depth { + 1 => v * 255 + 2 => v * 85 + 4 => v * 17 + _ => v + } + } + for y = 0; y < h; y = y + 1 { + let row = y * stride + for x = 0; x < w; x = x + 1 { + let (r, g, b, a) = match ctype { + 0 => { + let v = scale(sample(row, x)) + (v, v, v, 255) + } + 2 => (sample(row, x * 3), sample(row, x * 3 + 1), sample(row, x * 3 + 2), 255) + 3 => { + let i = sample(row, x) + if i * 3 + 2 >= palette.length() { + (0, 0, 0, 0) + } else { + let a = if i < trns.length() { trns[i].to_int() } else { 255 } + (palette[i * 3].to_int(), palette[i * 3 + 1].to_int(), palette[i * 3 + 2].to_int(), a) + } + } + 4 => { + let v = sample(row, x * 2) + (v, v, v, sample(row, x * 2 + 1)) + } + _ => + ( + sample(row, x * 4), + sample(row, x * 4 + 1), + sample(row, x * 4 + 2), + sample(row, x * 4 + 3), + ) + } + let o = (y * w + x) * 4 + img.data[o] = r.to_byte() + img.data[o + 1] = g.to_byte() + img.data[o + 2] = b.to_byte() + img.data[o + 3] = a.to_byte() + } + } + Some(img) +} diff --git a/compositor/renderer/png_wbtest.mbt b/compositor/renderer/png_wbtest.mbt new file mode 100644 index 0000000..55ddcc1 --- /dev/null +++ b/compositor/renderer/png_wbtest.mbt @@ -0,0 +1,71 @@ +///| +fn px(p : Pixmap, x : Int, y : Int) -> (Int, Int, Int, Int) { + let o = (y * p.width + x) * 4 + (p.data[o].to_int(), p.data[o + 1].to_int(), p.data[o + 2].to_int(), p.data[o + 3].to_int()) +} + +///| +test "decode_png: RGBA rows using all five filters (fixed Huffman)" { + let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x03\x00\x00\x00\x05\x08\x06\x00\x00\x00\x80\x71\x56\xa2\x00\x00\x00\x3e\x49\x44\x41\x54\x78\xda\x63\x60\x60\x60\xf8\xaf\xc1\xc0\x70\x38\x80\x81\xa1\x9d\x91\xdd\x08\xc4\x91\x3b\x02\xc2\x4c\x40\x0e\x03\xbb\x91\x1c\x10\xdb\x30\x30\xf3\xa5\x30\x34\x48\x48\xda\x3c\x92\x90\xf4\x7e\xc4\x02\x96\x61\x00\xca\x30\xd8\x30\x00\x00\x9a\x17\x0b\x5d\x72\xb0\x16\x16\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() + assert_eq((p.width, p.height), (3, 5)) + for y = 0; y < 5; y = y + 1 { + for x = 0; x < 3; x = x + 1 { + assert_eq(px(p, x, y), ((x * 40 + y * 7) & 255, (y * 50) & 255, (x * y * 30) & 255, 255 - x * 60)) + } + } +} + +///| +test "decode_png: RGB with dynamic Huffman blocks" { + let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x28\x00\x00\x00\x28\x08\x02\x00\x00\x00\x03\x9c\x2f\x3a\x00\x00\x00\x33\x49\x44\x41\x54\x78\xda\xed\xcd\xc1\x09\x00\x20\x0c\x00\xb1\x03\xa5\xfb\x8f\xec\x16\x0a\x12\xc8\x3f\xbb\x9a\xd6\x7d\xbb\x59\xf5\x80\x58\x2c\x16\x8b\xc5\x62\xb1\x58\x2c\x16\x8b\xc5\x62\xb1\xf8\xc7\xf8\x00\xd3\x7b\x15\x32\x6b\xd7\xe3\x22\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() + assert_eq((p.width, p.height), (40, 40)) + for y = 0; y < 40; y = y + 1 { + for x = 0; x < 40; x = x + 1 { + assert_eq(px(p, x, y), (x * 6, y * 6, (x + y) * 3, 255)) + } + } +} + +///| +test "decode_png: 2-bit palette with tRNS, and stored grey+alpha" { + let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x03\x00\x00\x00\x02\x02\x03\x00\x00\x00\xe0\x1a\x8e\x89\x00\x00\x00\x0c\x50\x4c\x54\x45\xff\x00\x00\x00\xff\x00\x00\x00\xff\x09\x09\x09\x5c\x71\x7e\x86\x00\x00\x00\x03\x74\x52\x4e\x53\xff\x80\x00\x7f\x6d\x68\x78\x00\x00\x00\x0c\x49\x44\x41\x54\x78\xda\x63\x90\x60\x7c\x02\x00\x01\x32\x00\xfe\x06\xf2\x6e\x18\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() + assert_eq(px(p, 0, 0), (255, 0, 0, 255)) + assert_eq(px(p, 1, 0), (0, 255, 0, 128)) + assert_eq(px(p, 2, 0), (0, 0, 255, 0)) + assert_eq(px(p, 0, 1), (9, 9, 9, 255)) + assert_eq(px(p, 2, 1), (0, 255, 0, 128)) + let g = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x02\x00\x00\x00\x01\x08\x04\x00\x00\x00\x5e\x2b\xb7\x01\x00\x00\x00\x10\x49\x44\x41\x54\x78\x01\x01\x05\x00\xfa\xff\x00\x0a\x14\x1e\x28\x00\xcd\x00\x65\x7b\x27\x54\xba\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap() + assert_eq(px(g, 0, 0), (10, 10, 10, 20)) + assert_eq(px(g, 1, 0), (30, 30, 30, 40)) +} + +///| +test "decode_png refuses what isn't a PNG it can read" { + assert_true(decode_png(b"not a png at all, not a png at all, nope").is_empty()) + assert_true(decode_png(b"").is_empty()) + // a truncated file + let cut = b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x28\x00\x00\x00\x28\x08\x02\x00\x00\x00\x03\x9c\x2f\x3a\x00\x00\x00\x33\x49\x44\x41\x54\x78\xda\xed\xcd\xc1\x09\x00\x20\x0c\x00\xb1\x03\xa5\xfb\x8f\xec\x16\x0a\x12\xc8\x3f\xbb\x9a\xd6\x7d\xbb\x59\xf5\x80\x58\x2c\x16\x8b\xc5\x62\xb1\x58\x2c\x16" + assert_true(decode_png(cut).is_empty()) +} + +///| +test "scale_pixmap averages down and draw_image blends the icon into the frame" { + let src = Pixmap::new(4, 4) + for i = 0; i < 16; i = i + 1 { + src.data[i * 4] = b'\xff' + src.data[i * 4 + 3] = if i % 2 == 0 { b'\xff' } else { b'\x00' } + } + let s = scale_pixmap(src, 2, 2) + // half the source pixels are opaque red: the result is red at half alpha + assert_eq(px(s, 0, 0), (255, 0, 0, 127)) + let h = add_image(src) + assert_true(h != 0U) + let dst = Pixmap::new(10, 10) + draw_ops(dst, [Image({ x: 2, y: 2, w: 4, h: 4 }, h)], 0, 0) + assert_eq(px(dst, 2, 2), (255, 0, 0, 255)) + assert_eq(px(dst, 3, 2).3, 0) + assert_eq(px(dst, 0, 0), (0, 0, 0, 0)) + // unknown handles draw nothing + draw_ops(dst, [Image({ x: 0, y: 0, w: 2, h: 2 }, 9999U)], 0, 0) + assert_eq(px(dst, 0, 0), (0, 0, 0, 0)) +} diff --git a/compositor/renderer/renderer.mbt b/compositor/renderer/renderer.mbt index 7001784..49d0060 100644 --- a/compositor/renderer/renderer.mbt +++ b/compositor/renderer/renderer.mbt @@ -668,6 +668,8 @@ pub fn draw_ops( PushClip(r) => clip_stack.push(@scene.Rect::new(r.x + ox, r.y + oy, r.w, r.h)) PopClip => if clip_stack.length() > 0 { ignore(clip_stack.pop()) } + Image(r, handle) => + draw_image(dst, handle, @scene.Rect::new(r.x + ox, r.y + oy, r.w, r.h), cur_clip) } } } diff --git a/compositor/scene/pkg.generated.mbti b/compositor/scene/pkg.generated.mbti index d312351..56b3d25 100644 --- a/compositor/scene/pkg.generated.mbti +++ b/compositor/scene/pkg.generated.mbti @@ -32,6 +32,7 @@ pub(all) enum DrawOp { MonoText(Int32, Int32, String, Int32, Rgba, TextAlign, Bool) PushClip(Rect) PopClip + Image(Rect, UInt32) } pub(all) struct PanelWidgetNode { @@ -113,6 +114,7 @@ pub(all) struct ThemeTokens { panel_height : Int32 } pub fn ThemeTokens::from_json_string(String) -> Self +pub fn ThemeTokens::moon_day() -> Self pub fn ThemeTokens::moon_night() -> Self pub(all) struct WindowSceneNode { diff --git a/compositor/scene/scene.mbt b/compositor/scene/scene.mbt index 8107359..a3a92e9 100644 --- a/compositor/scene/scene.mbt +++ b/compositor/scene/scene.mbt @@ -253,6 +253,8 @@ pub(all) enum DrawOp { MonoText(Int, Int, String, Int, Rgba, TextAlign, Bool) PushClip(Rect) PopClip + /// A loaded image (`@renderer.add_image` / `raster.load-image` handle) scaled into the rect. + Image(Rect, UInt) } ///| diff --git a/compositor/shell/apps.mbt b/compositor/shell/apps.mbt index f2a73c1..8bf4da7 100644 --- a/compositor/shell/apps.mbt +++ b/compositor/shell/apps.mbt @@ -10,6 +10,8 @@ priv struct DesktopApp { command : String /// StartupWMClass: the app id its windows carry when that is not the desktop file's name. wm_class : String + /// Icon=: an icon theme name ("firefox") or an absolute path. + icon : String } ///| Directories scanned, lowest priority first (later ones override by file name). @@ -66,6 +68,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp let mut terminal = false let mut kind = "Application" let mut wm_class = "" + let mut icon = "" for raw in text.split("\n") { let line = raw.trim(chars=" \r\t").to_string() if line.length() == 0 || line.has_prefix("#") { @@ -93,6 +96,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp "NoDisplay" | "Hidden" => if val == "true" { hidden = true } "Terminal" => terminal = val == "true" "StartupWMClass" => wm_class = val + "Icon" => icon = val _ => () } } @@ -113,7 +117,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp _ => "Other" } let comment = if comment.length() > 0 { comment } else { generic } - Some({ id, name, comment, category, command, wm_class }) + Some({ id, name, comment, category, command, wm_class, icon }) } ///| Scan every applications dir; later dirs override earlier ones, result sorted by name. @@ -169,6 +173,11 @@ test "parse_desktop keeps StartupWMClass" { Some(a) => assert_eq(a.wm_class, "Code") None => fail("expected entry") } + let t2 = "[Desktop Entry]\nType=Application\nName=Files\nExec=nautilus\nIcon=org.gnome.Nautilus\n[Desktop Action w]\nIcon=other\n" + match parse_desktop("org.gnome.Nautilus", t2, true) { + Some(a) => assert_eq(a.icon, "org.gnome.Nautilus") + None => fail("expected entry") + } } ///| diff --git a/compositor/shell/icons.mbt b/compositor/shell/icons.mbt new file mode 100644 index 0000000..33b4246 --- /dev/null +++ b/compositor/shell/icons.mbt @@ -0,0 +1,145 @@ +///| App icons for panel modules (the `icons` capability): a window's app id is matched to its +/// .desktop file (`resolve_app_id`), whose Icon= key is looked up in the freedesktop icon themes +/// (a simplified icon theme spec lookup), and the file is loaded for `Image` draw ops. + +///| Base directories icon themes live in, highest priority first. +fn icon_base_dirs() -> Array[String] { + let home = @sys.getenv("HOME") + let data_home = { + let x = @sys.getenv("XDG_DATA_HOME") + if x.length() > 0 { x } else { home + "/.local/share" } + } + let dirs = [data_home + "/icons", home + "/.icons"] + let xdg = @sys.getenv("XDG_DATA_DIRS") + let list = if xdg.length() > 0 { xdg } else { "/usr/local/share:/usr/share" } + for p in list.split(":") { + if p.length() > 0 { + dirs.push(p.to_string() + "/icons") + } + } + dirs.push(home + "/.local/share/flatpak/exports/share/icons") + dirs.push("/var/lib/flatpak/exports/share/icons") + dirs +} + +///| Themes searched: hicolor is where apps install their icons; the others cover apps whose icon +/// only ships with a desktop theme. +let icon_themes : Array[String] = ["hicolor", "Adwaita", "breeze"] + +///| Fixed-size directories, best first for a ~22-48 px icon (scaled down well); the scalable SVG +/// goes after the first three when the host can draw SVG. +let icon_sizes : Array[String] = [ + "48x48", "64x64", "32x32", "96x96", "128x128", "256x256", "24x24", "22x22", "16x16", + "512x512", +] + +///| Candidate files for icon `name` (an Icon= value), in lookup order. `svg_ok`: whether the +/// image loader can read SVG (the host does; the native software renderer only reads PNG). +fn icon_candidates( + name : String, + svg_ok : Bool, + bases : Array[String], + pixmaps : Array[String], +) -> Array[String] { + if name.length() == 0 { + return [] + } + if name.has_prefix("/") { + return if name.has_suffix(".png") || (svg_ok && name.has_suffix(".svg")) { + [name] + } else { + [] + } + } + // Icon=foo.png is not allowed by the spec but common: look it up as "foo". + let stem = if name.has_suffix(".png") || name.has_suffix(".svg") || name.has_suffix(".xpm") { + name[:name.length() - 4].to_string() + } else { + name + } + let out : Array[String] = [] + for theme in icon_themes { + for base in bases { + let dir = base + "/" + theme + for i = 0; i < icon_sizes.length(); i = i + 1 { + if i == 3 && svg_ok { + out.push(dir + "/scalable/apps/" + stem + ".svg") + } + out.push(dir + "/" + icon_sizes[i] + "/apps/" + stem + ".png") + } + } + } + for p in pixmaps { + out.push(p + "/" + stem + ".png") + if svg_ok { + out.push(p + "/" + stem + ".svg") + } + } + out +} + +///| The icon file for Icon= value `name`, or None when no theme has it. +fn find_icon_file(name : String, svg_ok : Bool) -> String? { + let pixmaps = ["/usr/share/pixmaps"] + for path in icon_candidates(name, svg_ok, icon_base_dirs(), pixmaps) { + if @sys.read_file_bytes(path).length() > 0 { + return Some(path) + } + } + None +} + +///| The image handle of the icon for windows carrying `app_id` (0: none). Cached per app id, +/// so each app's themes are searched once. +fn DesktopShell::app_icon(self : DesktopShell, app_id : String) -> UInt { + match self.icon_handles.get(app_id) { + Some(h) => return h + None => () + } + let icon = match resolve_app_id(app_id, self.exec, self.wm_classes) { + Some(id) => self.icon_names.get(id).unwrap_or("") + None => "" + } + // No desktop file: many apps' icons are named after their app id anyway. + let name = if icon.length() > 0 { icon } else { app_id } + let h = match find_icon_file(name, @sys.host_raster()) { + Some(path) => @raster.load_image(path) + None => 0U + } + self.icon_handles.set(app_id, h) + h +} + +///| Publish the icons table (app id -> image handle) for the windows' apps. +fn DesktopShell::publish_icons(self : DesktopShell) -> Unit { + let t = @wasm.Table::new() + let seen : Map[String, Bool] = {} + for w in self.win_list { + if w.app_id.length() > 0 && !seen.contains(w.app_id) { + seen.set(w.app_id, true) + let h = self.app_icon(w.app_id) + if h != 0U { + t.str(w.app_id) + t.int(h.reinterpret_as_int()) + t.end_row() + } + } + } + @wasm.publish(Icons, t) +} + +///| +test "icon lookup tries the themes' sized PNGs, then scalable SVG when it can, then pixmaps" { + let c = icon_candidates("firefox", true, ["/u/icons"], ["/p"]) + assert_eq(c[0], "/u/icons/hicolor/48x48/apps/firefox.png") + assert_eq(c[3], "/u/icons/hicolor/scalable/apps/firefox.svg") + assert_eq(c[c.length() - 2], "/p/firefox.png") + assert_true(c.contains("/u/icons/breeze/16x16/apps/firefox.png")) + // The software renderer reads PNG only. + assert_false(icon_candidates("firefox", false, ["/u/icons"], ["/p"]).iter().any(p => p.has_suffix(".svg"))) + // Icon=foo.png means foo; absolute paths are taken as they are. + assert_eq(icon_candidates("foo.png", false, ["/b"], [])[0], "/b/hicolor/48x48/apps/foo.png") + assert_eq(icon_candidates("/opt/app/icon.png", false, ["/b"], []), ["/opt/app/icon.png"]) + assert_eq(icon_candidates("/opt/app/icon.svg", false, ["/b"], []), []) + assert_eq(icon_candidates("", true, ["/b"], []), []) +} diff --git a/compositor/shell/moon.pkg b/compositor/shell/moon.pkg index ac584e8..2bad7d8 100644 --- a/compositor/shell/moon.pkg +++ b/compositor/shell/moon.pkg @@ -14,6 +14,7 @@ import { "jqlssg/moonedn" @edn, "chandra/desktop/compositor/sys" @sys, "chandra/desktop/compositor/renderer" @renderer, + "chandra/desktop/compositor/raster" @raster, "chandra/desktop/compositor/wasm" @wasm, } diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index e0ee320..3107c0f 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -108,6 +108,7 @@ fn translate( } PushClip(r) => out.push(@scene.DrawOp::PushClip(rect(r))) PopClip => out.push(@scene.DrawOp::PopClip) + Image(i) => out.push(@scene.DrawOp::Image(rect(i.rect), i.image)) } } } @@ -184,6 +185,10 @@ pub(all) struct DesktopShell { exec : Map[String, String] /// Lower-cased StartupWMClass -> desktop file id, for apps whose windows' app id differs. wm_classes : Map[String, String] + /// Desktop file id -> its Icon= value, and the image handle found for each window app id + /// (0: no icon), for the `icons` capability. + icon_names : Map[String, String] + icon_handles : Map[String, UInt] /// Wayland socket handed to launched apps as WAYLAND_DISPLAY. display : String /// Panel modules in panel order; a module with `stretch` splits the panel into a left and a right group. @@ -266,7 +271,11 @@ pub fn DesktopShell::new(display : String) -> DesktopShell { let apps : Array[@types.AppEntry] = [] let exec : Map[String, String] = {} let wm_classes : Map[String, String] = {} + let icon_names : Map[String, String] = {} for a in scan_desktop_apps() { + if a.icon.length() > 0 { + icon_names.set(a.id, a.icon) + } if a.wm_class.length() > 0 { wm_classes.set(a.wm_class.to_lower(), a.id) } @@ -282,6 +291,8 @@ pub fn DesktopShell::new(display : String) -> DesktopShell { apps, exec, wm_classes, + icon_names, + icon_handles: {}, display, widgets: [], next_slot: 1, @@ -785,6 +796,9 @@ pub fn DesktopShell::render( self.sampler.refresh() @wasm.set_windows(self.win_list) } + if self.widgets.iter().any(w => w.has_cap("icons")) { + self.publish_icons() + } let bg : Array[@scene.DrawOp] = [] translate(@libshell.render_wallpaper(@ui.size(w, h), tokens, self.wallpaper_sel), 0, 0, bg) self.wallpaper_len = bg.length() diff --git a/compositor/wasm/wasm_host.mbt b/compositor/wasm/wasm_host.mbt index 63d173d..d89ef34 100644 --- a/compositor/wasm/wasm_host.mbt +++ b/compositor/wasm/wasm_host.mbt @@ -233,6 +233,8 @@ pub(all) enum HostTable { Instances Wallpapers SessionSettings + /// App id -> image handle (`raster.load-image`), served by `icons.app-icon`. + Icons } ///| @@ -254,6 +256,7 @@ pub fn publish(which : HostTable, t : Table) -> Unit { Instances => 4U Wallpapers => 5U SessionSettings => 6U + Icons => 7U } @hostpl.publish_table(id, out) } diff --git a/compositor/wasm/wasm_native.mbt b/compositor/wasm/wasm_native.mbt index afd2447..b7092a7 100644 --- a/compositor/wasm/wasm_native.mbt +++ b/compositor/wasm/wasm_native.mbt @@ -435,6 +435,7 @@ fn Plasmoid::decode_ops(self : Plasmoid, base : Int, n : Int, out : Array[@types }), ) 5 => out.push(PushClip(rect(o + 4))) + 7 => out.push(Image({ rect: rect(o + 4), image: rd32(b, o + 20).reinterpret_as_uint() })) _ => out.push(PopClip) } } @@ -609,6 +610,8 @@ pub(all) enum HostTable { Instances Wallpapers SessionSettings + /// App id -> image handle (`raster.load-image`), served by `icons.app-icon`. + Icons } ///| Publish `t` as the data behind `which` for every module's next call. @@ -621,6 +624,7 @@ pub fn publish(which : HostTable, t : Table) -> Unit { Instances => 4 Wallpapers => 5 SessionSettings => 6 + Icons => 7 } let out : Array[Byte] = [] for k = 0; k < 4; k = k + 1 { diff --git a/compositor/wasm/wasm_stub.c b/compositor/wasm/wasm_stub.c index 76b08f9..57888ac 100644 --- a/compositor/wasm/wasm_stub.c +++ b/compositor/wasm/wasm_stub.c @@ -48,13 +48,14 @@ enum { K_OTHER, K_TOKENS, K_CLOCK, K_APPS, K_LAUNCH, K_WINDOWS, K_FOCUS, K_LAYOUTS, K_LAYOUT_CUR, K_SET_LAYOUT, K_INSPECT_IS_ON, K_INSPECT_SET, K_MOD_AVAILABLE, K_MOD_LOADED, K_MOD_LOAD, K_MOD_UNLOAD, K_MOD_BROWSE, K_SYSTEM_READ, K_WIN_ALL, K_WIN_FOCUS_ID, K_LOGOUT, K_WALLPAPERS, K_WALLPAPER_CUR, K_SET_WALLPAPER, - K_SESSION_SETTINGS, K_SESSION_IS_ON, K_SESSION_SET + K_SESSION_SETTINGS, K_SESSION_IS_ON, K_SESSION_SET, + K_APP_ICON }; typedef struct Env { Inst *inst; int kind; } Env; /* ---- host data the shell publishes before calling a guest ---- * A table is [u32 rows] then rows of fields, each field a LE i32 ('i') or a u32 byte length + UTF-8 ('s'). */ -enum { T_APPS, T_WINDOWS, T_LAYOUTS, T_MODULES, T_INSTANCES, T_WALLPAPERS, T_SESSION_SETTINGS, T_N }; +enum { T_APPS, T_WINDOWS, T_LAYOUTS, T_MODULES, T_INSTANCES, T_WALLPAPERS, T_SESSION_SETTINGS, T_ICONS, T_N }; typedef struct { uint8_t *data; size_t len; } Blob; static Blob tables[T_N]; static int32_t host_ints[4]; /* 0 layout mode, 1 inspector on, 2 current wallpaper, 3 session settings on (bit i: row i) */ @@ -226,6 +227,25 @@ static int table_has_id(int t, const char *spec, const char *id) { return 0; } +/* The image handle table T_ICONS ("si": app id, handle) holds for `id`, or 0. */ +static int32_t icon_for(const char *id) { + Blob *b = &tables[T_ICONS]; + uint32_t n = b->len >= 4 ? rd_u32(b->data) : 0; + size_t pos = 4, idl = strlen(id); + for (uint32_t r = 0; r < n; r++) { + if (pos + 4 > b->len) return 0; + uint32_t sl = rd_u32(b->data + pos); + pos += 4; + if (pos + sl + 4 > b->len) return 0; + int hit = sl == idl && memcmp(b->data + pos, id, sl) == 0; + pos += sl; + int32_t h = (int32_t)rd_u32(b->data + pos); + pos += 4; + if (hit) return h; + } + return 0; +} + static wasm_trap_t *host_import(void *env, const wasm_val_vec_t *args, wasm_val_vec_t *results) { Env *e = (Env *)env; Inst *in = e->inst; @@ -305,6 +325,14 @@ static wasm_trap_t *host_import(void *env, const wasm_val_vec_t *args, wasm_val_ free(id); break; } + case K_APP_ICON: { + /* app-icon(app-id) -> u32; (ptr, len) */ + char *id = guest_str(in, a0, a1); + int32_t h = id ? icon_for(id) : 0; + free(id); + if (results->size >= 1) results->data[0] = (wasm_val_t)WASM_I32_VAL(h); + break; + } case K_SYSTEM_READ: if (args->size >= 1) mem_write(in, a0, g_system, 64); break; @@ -414,6 +442,7 @@ static int classify_import(const wasm_name_t *m, const wasm_name_t *f) { if (!strcmp(iface, "windows") && !strcmp(fn, "all")) return K_WIN_ALL; if (!strcmp(iface, "windows") && !strcmp(fn, "focus")) return K_WIN_FOCUS_ID; if (!strcmp(iface, "system") && !strcmp(fn, "read")) return K_SYSTEM_READ; + if (!strcmp(iface, "icons") && !strcmp(fn, "app-icon")) return K_APP_ICON; if (!strcmp(iface, "session") && !strcmp(fn, "logout")) return K_LOGOUT; if (!strcmp(iface, "wallpaper") && !strcmp(fn, "wallpapers")) return K_WALLPAPERS; if (!strcmp(iface, "wallpaper") && !strcmp(fn, "current-wallpaper")) return K_WALLPAPER_CUR; diff --git a/compositor/wasm/wasm_test.mbt b/compositor/wasm/wasm_test.mbt index 5ba396a..1fef2f3 100644 --- a/compositor/wasm/wasm_test.mbt +++ b/compositor/wasm/wasm_test.mbt @@ -121,6 +121,44 @@ test "task list module draws the host's windows and asks to focus one" { p.unload() } +///| +test "task list draws the host's icon for an app it has one for, the letter tile otherwise" { + let p = load_module("tasks") + assert_true(p.imports().contains("icons")) + let t = Table::new() + for w in [(3, "Firefox"), (4, "foot")] { + t.int(w.0) + t.str(w.1) + t.int(0) + t.int(0) + t.end_row() + } + publish(Windows, t) + set_windows([ + { id: 3UL, title: "Firefox", app_id: "firefox", focused: false }, + { id: 4UL, title: "foot", app_id: "foot", focused: false }, + ]) + let icons = Table::new() + icons.str("firefox") + icons.int(42) + icons.end_row() + publish(Icons, icons) + let ops = p.render(400, 36, test_tokens()) + let images = ops.filter_map(op => match op { + Image(i) => Some(i) + _ => None + }) + // Firefox: the icon (handle 42, a 22px square centred in the bar), no glyph; foot: the tile. + assert_eq(images.length(), 1) + assert_eq(images[0].image, 42U) + assert_eq((images[0].rect.w, images[0].rect.h), (22U, 22U)) + assert_eq(images[0].rect.y, 7) + assert_eq(texts_of(ops), ["Firefox", "F", "foot"]) + publish(Icons, Table::new()) + set_windows([]) + p.unload() +} + ///| test "layout module is a tile/float toggle" { let p = load_module("layout") diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 244885f..6a64636 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -16,7 +16,7 @@ versioned, permission-checked, hot-swappable interface. |---|---|---|---| | `plasmashell` (containments, panels) | Hosts the desktop & panels, loads plasmoids | `shell` component exporting `desktop-surface` + `panel-layout` **[M0]** | component | | Plasmoids / widgets (QML + C++/JS) | Clock, task manager, kickoff… | `widget` world exporting `plasmoid` **[M0]**: `digital-clock`, `binary-clock`, `launcher` | component, one instance per widget | -| Plasma Look-and-Feel, colour schemes, Breeze, Kirigami theme | Design tokens, icons, cursors, fonts | `theme` interface (`tokens`) **[M0]**; icon/cursor/font interfaces **[designed]**; a "look-and-feel" bundle = theme tokens + default layout + wallpaper | tokens served by a `theme-provider` component (M0: static JSON in host) | +| Plasma Look-and-Feel, colour schemes, Breeze, Kirigami theme | Design tokens, icons, cursors, fonts | `theme` interface (`tokens`) **[M0]**; app icons through the `icons` interface + an `image` draw op (the host resolves .desktop Icon= in the freedesktop icon themes and decodes PNG/SVG) **[M1]**; cursor/font interfaces **[designed]**; a "look-and-feel" bundle = theme tokens + default layout + wallpaper | tokens served by a `theme-provider` component (M0: static JSON in host) | | KWin (WM policy: stacking, focus, tiling, rules, virtual desktops) | Decides where windows go | `window-manager` world: exports `wm-policy` (place/resize/focus decisions), imports `window-registry` **[designed]** | component | | KWin (compositing: scene graph, damage, scanout) | Draws windows to screen | Rasteriser/scanout of the **render-list** | **host** (only because it touches GPU/DRM) | | KWin decorations (Aurorae etc.) | Titlebar/borders | `decoration` interface: returns draw-ops + hit regions for a window state **[designed]** | component | diff --git a/host-wasm/Cargo.lock b/host-wasm/Cargo.lock index cefbac3..9882c98 100644 --- a/host-wasm/Cargo.lock +++ b/host-wasm/Cargo.lock @@ -27,6 +27,12 @@ dependencies = [ "gimli", ] +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "allocator-api2" version = "0.2.21" @@ -45,6 +51,18 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" +[[package]] +name = "arrayref" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" + +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + [[package]] name = "async-trait" version = "0.1.92" @@ -56,6 +74,18 @@ dependencies = [ "syn", ] +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.13.2" @@ -80,6 +110,12 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" + [[package]] name = "cc" version = "1.11.3" @@ -104,6 +140,8 @@ dependencies = [ "ab_glyph_rasterizer", "cc", "libc", + "png 0.17.16", + "resvg", "wasmtime", ] @@ -306,6 +344,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "data-url" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" + [[package]] name = "digest" version = "0.10.7" @@ -364,12 +408,47 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "euclid" +version = "0.22.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1a05365e3b1c6d1650318537c7460c6923f1abdd272ad6842baa2b509957a06" +dependencies = [ + "num-traits", +] + +[[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + [[package]] name = "find-msvc-tools" version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" +[[package]] +name = "flate2" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626d4928326b646e70f65ed36fc7cf9f206bdb68a0019ee7c6b4709da4e084c4" +dependencies = [ + "crc32fast", + "miniz_oxide 0.9.1", + "zlib-rs", +] + +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" + [[package]] name = "fnv" version = "1.0.7" @@ -497,6 +576,12 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" +[[package]] +name = "imagesize" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" + [[package]] name = "indexmap" version = "2.14.2" @@ -524,6 +609,18 @@ version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" +[[package]] +name = "kurbo" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b60dfc32f652b926df6192e55525b16d186c69d47876c3ead4da5cc9f8450e2" +dependencies = [ + "arrayvec", + "euclid", + "polycool", + "smallvec", +] + [[package]] name = "leb128fmt" version = "0.1.0" @@ -575,12 +672,41 @@ dependencies = [ "rustix", ] +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "multiversion_no_op" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.40.0" @@ -608,12 +734,53 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" + [[package]] name = "pin-project-lite" version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752" +[[package]] +name = "png" +version = "0.17.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526" +dependencies = [ + "bitflags 1.3.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.8.9", +] + +[[package]] +name = "png" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61" +dependencies = [ + "bitflags 2.13.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.8.9", +] + +[[package]] +name = "polycool" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50596ddc09eb5ad5f75cacd40209568e66df71baf86e1499a0e99c4cff12a5a6" +dependencies = [ + "arrayvec", +] + [[package]] name = "postcard" version = "1.2.0" @@ -682,6 +849,39 @@ dependencies = [ "smallvec", ] +[[package]] +name = "resvg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77bb4b284cf8a5c8ae721594af53387fb755d2196fddb32b2acfb4fa9251fa0c" +dependencies = [ + "bytemuck", + "log", + "pico-args", + "rgb", + "svgtypes", + "tiny-skia", + "usvg", +] + +[[package]] +name = "rgb" +version = "0.8.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b34b781b31e5d73e9fbc8689c70551fd1ade9a19e3e28cfec8580a79290cc4" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "roxmltree" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907" +dependencies = [ + "memchr", +] + [[package]] name = "rustc-demangle" version = "0.1.28" @@ -700,7 +900,7 @@ version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ - "bitflags", + "bitflags 2.13.2", "errno", "libc", "linux-raw-sys", @@ -789,12 +989,33 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + [[package]] name = "simdutf8" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" +[[package]] +name = "simplecss" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a9c6883ca9c3c7c90e888de77b7a5c849c779d25d74a1269b0218b14e8b136c" +dependencies = [ + "log", +] + +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + [[package]] name = "slab" version = "0.4.12" @@ -816,6 +1037,25 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "strict-num" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +dependencies = [ + "float-cmp", +] + +[[package]] +name = "svgtypes" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f12b12995e3638c70618c1facb1ce65302f6a6f706b675f07c58a5d7391120e" +dependencies = [ + "kurbo", + "siphasher", +] + [[package]] name = "syn" version = "3.0.10" @@ -862,6 +1102,32 @@ dependencies = [ "syn", ] +[[package]] +name = "tiny-skia" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47ffee5eaaf5527f630fb0e356b90ebdec84d5d18d937c5e440350f88c5a91ea" +dependencies = [ + "arrayref", + "arrayvec", + "bytemuck", + "cfg-if", + "log", + "png 0.18.1", + "tiny-skia-path", +] + +[[package]] +name = "tiny-skia-path" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edca365c3faccca67d06593c5980fa6c57687de727a03131735bb85f01fdeeb9" +dependencies = [ + "arrayref", + "bytemuck", + "strict-num", +] + [[package]] name = "ttf-parser" version = "0.25.1" @@ -880,6 +1146,25 @@ version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" +[[package]] +name = "usvg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdb896f04730b97805bbc3191fc57e4bf03face0f14577dd42e783c4d8c98802" +dependencies = [ + "data-url", + "imagesize", + "kurbo", + "log", + "pico-args", + "roxmltree", + "simplecss", + "siphasher", + "strict-num", + "svgtypes", + "tiny-skia-path", +] + [[package]] name = "version_check" version = "0.9.5" @@ -914,7 +1199,7 @@ version = "0.261.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f20f20e44f7e8aeb6744823ea9d869ede51e51be4fdaedede2852282e54d2d8" dependencies = [ - "bitflags", + "bitflags 2.13.2", "hashbrown 0.17.2", "indexmap", "semver", @@ -940,7 +1225,7 @@ checksum = "3231d67bedf0f4849558b01ee5accd0283469cff7ee6746ce6a9742ddc178312" dependencies = [ "addr2line", "async-trait", - "bitflags", + "bitflags 2.13.2", "bumpalo", "cc", "encoding_rs", @@ -1126,7 +1411,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "735a94d76f9b7f34b3fdb3181ac1f4234b5b0c9916b1b54a840c399e209cccda" dependencies = [ "anyhow", - "bitflags", + "bitflags 2.13.2", "heck", "indexmap", "wit-component", @@ -1164,7 +1449,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2818766842bd2321074c348fb5c15d8fdb105a9eb78374e6d77c7d4127e1f2f9" dependencies = [ "anyhow", - "bitflags", + "bitflags 2.13.2", "indexmap", "log", "serde", @@ -1195,6 +1480,12 @@ dependencies = [ "wasmparser", ] +[[package]] +name = "zlib-rs" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa76918995d776d9dd34d371030be228d021137449771493d56e29f77e4148ac" + [[package]] name = "zmij" version = "1.0.23" diff --git a/host-wasm/Cargo.toml b/host-wasm/Cargo.toml index 4eda446..3d776f1 100644 --- a/host-wasm/Cargo.toml +++ b/host-wasm/Cargo.toml @@ -17,6 +17,8 @@ cc = { version = "1", optional = true } ab_glyph = "0.2" ab_glyph_rasterizer = "0.1" libc = "0.2" +png = "0.17" +resvg = { version = "0.50.0", default-features = false } wasmtime = { version = "50", default-features = false, features = ["component-model", "cranelift", "runtime", "std"] } [profile.release] diff --git a/host-wasm/src/main.rs b/host-wasm/src/main.rs index 2365e47..9a3bc4e 100644 --- a/host-wasm/src/main.rs +++ b/host-wasm/src/main.rs @@ -350,6 +350,10 @@ impl chandra::host::raster::Host for State { self.raster.release(id); } + fn load_image(&mut self, path: String) -> u32 { + self.raster.images.load(&path) + } + fn submit(&mut self, f: chandra::host::raster::Frame) { self.raster.submit(&f, &mut self.fonts); } diff --git a/host-wasm/src/plasmoid.rs b/host-wasm/src/plasmoid.rs index f771018..b356b67 100644 --- a/host-wasm/src/plasmoid.rs +++ b/host-wasm/src/plasmoid.rs @@ -46,11 +46,13 @@ pub const T_MODULES: usize = 3; pub const T_INSTANCES: usize = 4; pub const T_WALLPAPERS: usize = 5; pub const T_SESSION_SETTINGS: usize = 6; +/// App id -> raster image handle (`raster.load-image`), behind `icons.app-icon`. +pub const T_ICONS: usize = 7; /// Host data every module reads from, shared by all loaded modules. #[derive(Default)] pub struct Shared { - pub tables: [Vec; 7], + pub tables: [Vec; 8], pub layout_mode: i32, pub wallpaper: i32, /// Which session settings are on: bit i for row i of the session settings table. @@ -304,6 +306,18 @@ impl d::session_settings::Host for PState { } } +impl d::icons::Host for PState { + /// The handle the compositor published for this app id: it found the app's .desktop file and + /// its themed icon, and had the rasteriser load it. Modules never see the path. + fn app_icon(&mut self, app_id: String) -> u32 { + self.rows(T_ICONS, "si") + .iter() + .find(|r| s(&r[0]) == app_id) + .map(|r| i(&r[1]) as u32) + .unwrap_or(0) + } +} + impl d::system::Host for PState { fn read(&mut self) -> d::system::Snapshot { self.shared.borrow().system.clone().unwrap_or(d::system::Snapshot { @@ -646,4 +660,37 @@ mod tests { assert!(rt.shared.borrow().requests.is_empty()); rt.unload(h); } + + #[test] + fn task_list_draws_the_published_icon_handle_else_the_letter_tile() { + let mut rt = runtime(); + let l = rt.load(&module("tasks")).expect("tasks loads"); + assert!(l.imports.contains(&"icons".to_string())); + // windows table (isii: id title focused minimized) and the richer windows.all view + let mut t = 2u32.to_le_bytes().to_vec(); + for (id, title) in [(3i32, "Firefox"), (4, "foot")] { + t.extend(id.to_le_bytes()); + t.extend((title.len() as u32).to_le_bytes()); + t.extend(title.as_bytes()); + t.extend(0i32.to_le_bytes()); + t.extend(0i32.to_le_bytes()); + } + // icons table (si: app id, image handle): only Firefox has one + let mut ic = 1u32.to_le_bytes().to_vec(); + ic.extend(7u32.to_le_bytes()); + ic.extend(b"firefox"); + ic.extend(42i32.to_le_bytes()); + { + let mut sh = rt.shared.borrow_mut(); + sh.tables[T_WINDOWS] = t; + sh.tables[T_ICONS] = ic; + sh.windows = vec![(3, "Firefox".into(), "firefox".into(), false), (4, "foot".into(), "foot".into(), false)]; + } + let ops = rt.render(l.handle, Size { w: 400, h: 36 }); + let images: Vec = + ops.iter().filter_map(|o| if let d::types::DrawOp::Image(i) = o { Some(i.image) } else { None }).collect(); + assert_eq!(images, [42]); + assert_eq!(texts(&ops), ["Firefox", "F", "foot"]); + rt.unload(l.handle); + } } diff --git a/host-wasm/src/raster.rs b/host-wasm/src/raster.rs index 2c947a2..7a63387 100644 --- a/host-wasm/src/raster.rs +++ b/host-wasm/src/raster.rs @@ -8,6 +8,7 @@ use crate::chandra::host::raster as wt; use ab_glyph::{Font, FontVec, OutlineCurve}; use ab_glyph_rasterizer::{point, Rasterizer}; +use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; @@ -416,8 +417,170 @@ fn draw_text(c: &mut Canvas, fonts: &mut Fonts, t: &wt::Label, ox: i32, oy: i32, } } +// ---------------------------------------------------------------- images + +/// An image loaded with `load-image` (an app icon). An SVG keeps its tree, so it is rendered sharp at +/// each size it is drawn at; a PNG is kept as RGBA8888 and scaled. +enum ImageSrc { + Pixels { w: i32, h: i32, rgba: Vec }, + Svg(resvg::usvg::Tree), +} + +/// Loaded images by handle (`index + 1`), the handle of each path, and each image at the sizes it +/// has been drawn at (RGBA8888, not premultiplied). +#[derive(Default)] +pub struct Images { + srcs: Vec, + by_path: HashMap, + sized: RefCell>>>, +} + +/// Biggest image side accepted from a file, and drawn. +const MAX_IMAGE: i32 = 1024; + +fn decode_png(bytes: &[u8]) -> Option { + let mut dec = png::Decoder::new(std::io::Cursor::new(bytes)); + // Palette, grey and 16-bit images come out as 8-bit grey/GA/RGB/RGBA. + dec.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16); + let mut reader = dec.read_info().ok()?; + let (w, h) = (reader.info().width as i32, reader.info().height as i32); + if w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE { + return None; + } + let mut buf = vec![0u8; reader.output_buffer_size()]; + let info = reader.next_frame(&mut buf).ok()?; + let n = (w * h) as usize; + let mut rgba = Vec::with_capacity(n * 4); + let px = &buf[..info.buffer_size()]; + match info.color_type { + png::ColorType::Rgba => rgba.extend_from_slice(&px[..n * 4]), + png::ColorType::Rgb => px.chunks_exact(3).take(n).for_each(|p| rgba.extend_from_slice(&[p[0], p[1], p[2], 255])), + png::ColorType::GrayscaleAlpha => px.chunks_exact(2).take(n).for_each(|p| rgba.extend_from_slice(&[p[0], p[0], p[0], p[1]])), + png::ColorType::Grayscale => px.iter().take(n).for_each(|&v| rgba.extend_from_slice(&[v, v, v, 255])), + png::ColorType::Indexed => return None, + } + (rgba.len() == n * 4).then_some(ImageSrc::Pixels { w, h, rgba }) +} + +fn decode_svg(bytes: &[u8]) -> Option { + let tree = resvg::usvg::Tree::from_data(bytes, &resvg::usvg::Options::default()).ok()?; + let s = tree.size(); + (s.width() > 0.0 && s.height() > 0.0).then_some(ImageSrc::Svg(tree)) +} + +/// Scale RGBA `src` (`sw` x `sh`) to `w` x `h`: each target pixel averages the source pixels it +/// covers (alpha weighted), or takes the nearest one when enlarging. Same as the software renderer. +fn scale_rgba(src: &[u8], sw: i32, sh: i32, w: i32, h: i32) -> Vec { + let mut out = vec![0u8; (w * h * 4) as usize]; + for y in 0..h { + let y0 = y * sh / h; + let y1 = ((y + 1) * sh / h).max(y0 + 1); + for x in 0..w { + let x0 = x * sw / w; + let x1 = ((x + 1) * sw / w).max(x0 + 1); + let (mut r, mut g, mut b, mut a, mut n) = (0i64, 0i64, 0i64, 0i64, 0i64); + for sy in y0..y1 { + for sx in x0..x1 { + let o = ((sy * sw + sx) * 4) as usize; + let pa = src[o + 3] as i64; + r += src[o] as i64 * pa; + g += src[o + 1] as i64 * pa; + b += src[o + 2] as i64 * pa; + a += pa; + n += 1; + } + } + if a > 0 { + let o = ((y * w + x) * 4) as usize; + out[o] = (r / a) as u8; + out[o + 1] = (g / a) as u8; + out[o + 2] = (b / a) as u8; + out[o + 3] = (a / n) as u8; + } + } + } + out +} + +/// Render an SVG tree into a `w` x `h` RGBA8888 (not premultiplied) buffer, aspect kept and centred. +fn render_svg(tree: &resvg::usvg::Tree, w: i32, h: i32) -> Option> { + let mut pm = resvg::tiny_skia::Pixmap::new(w as u32, h as u32)?; + let s = tree.size(); + let k = (w as f32 / s.width()).min(h as f32 / s.height()); + let (dx, dy) = ((w as f32 - s.width() * k) / 2.0, (h as f32 - s.height() * k) / 2.0); + let t = resvg::tiny_skia::Transform::from_row(k, 0.0, 0.0, k, dx, dy); + resvg::render(tree, t, &mut pm.as_mut()); + let mut out = pm.take(); + for p in out.chunks_exact_mut(4) { + let a = p[3] as u32; + if a > 0 && a < 255 { + for c in &mut p[..3] { + *c = ((*c as u32 * 255 + a / 2) / a).min(255) as u8; + } + } + } + Some(out) +} + +impl Images { + /// Load the PNG or SVG at `path`; the same path gives the same handle. 0 on failure. + pub fn load(&mut self, path: &str) -> u32 { + if let Some(h) = self.by_path.get(path) { + return *h; + } + let src = std::fs::read(path).ok().and_then(|b| { + if b.starts_with(b"\x89PNG") { + decode_png(&b) + } else if path.ends_with(".svg") || path.ends_with(".svgz") { + decode_svg(&b) + } else { + None + } + }); + let h = match src { + Some(s) => { + self.srcs.push(s); + self.srcs.len() as u32 + } + None => 0, + }; + self.by_path.insert(path.to_string(), h); + h + } + + /// Image `handle` at `w` x `h` (cached), or None for an unknown handle or a silly size. + fn sized(&self, handle: u32, w: i32, h: i32) -> Option>> { + if w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE { + return None; + } + let src = self.srcs.get((handle as usize).checked_sub(1)?)?; + if let Some(px) = self.sized.borrow().get(&(handle, w, h)) { + return Some(px.clone()); + } + let px = Rc::new(match src { + ImageSrc::Pixels { w: sw, h: sh, rgba } if *sw == w && *sh == h => rgba.clone(), + ImageSrc::Pixels { w: sw, h: sh, rgba } => scale_rgba(rgba, *sw, *sh, w, h), + ImageSrc::Svg(tree) => render_svg(tree, w, h)?, + }); + self.sized.borrow_mut().insert((handle, w, h), px.clone()); + Some(px) + } +} + +fn draw_image(c: &mut Canvas, imgs: &Images, p: &wt::Picture, ox: i32, oy: i32, clip: Clip) { + let (w, h) = (p.area.w, p.area.h); + let Some(px) = imgs.sized(p.image, w, h) else { return }; + let (x0, y0) = (p.area.x + ox, p.area.y + oy); + for y in 0..h { + for x in 0..w { + let o = ((y * w + x) * 4) as usize; + blend_pixel(c, x0 + x, y0 + y, px[o] as i32, px[o + 1] as i32, px[o + 2] as i32, px[o + 3] as i32, clip); + } + } +} + /// Draw `ops` offset by (ox, oy), with the clip stack they push and pop. -fn draw_ops(c: &mut Canvas, fonts: &mut Fonts, ops: &[wt::Op], ox: i32, oy: i32) { +fn draw_ops(c: &mut Canvas, fonts: &mut Fonts, imgs: &Images, ops: &[wt::Op], ox: i32, oy: i32) { let mut stack: Vec = Vec::new(); for op in ops { let clip = stack.last().copied(); @@ -437,6 +600,7 @@ fn draw_ops(c: &mut Canvas, fonts: &mut Fonts, ops: &[wt::Op], ox: i32, oy: i32) wt::Op::PopClip => { stack.pop(); } + wt::Op::DrawImage(p) => draw_image(c, imgs, p, ox, oy, clip), } } } @@ -531,6 +695,8 @@ pub struct Raster { wallpaper: Option<(Vec, i32, i32, Vec)>, surfaces: HashMap, next_id: u32, + /// Images for `draw-image` ops (app icons). + pub images: Images, } impl Raster { @@ -569,6 +735,7 @@ impl Raster { self.fb.fill(0); } let surfaces = &self.surfaces; + let imgs = &self.images; let mut c = Canvas { w, h, data: &mut self.fb }; // 1. Wallpaper: drawn once, copied back while unchanged. @@ -578,7 +745,7 @@ impl Raster { if cached { c.data.copy_from_slice(&self.wallpaper.as_ref().unwrap().3); } else { - draw_ops(&mut c, fonts, ops, 0, 0); + draw_ops(&mut c, fonts, imgs, ops, 0, 0); self.wallpaper = Some((ops.clone(), w, h, c.data.to_vec())); } } @@ -590,7 +757,7 @@ impl Raster { } } } - draw_ops(&mut c, fonts, &f.background, 0, 0); + draw_ops(&mut c, fonts, imgs, &f.background, 0, 0); // 2. Windows, bottom to top. for win in &f.windows { @@ -611,7 +778,7 @@ impl Raster { for s in strips { fill_rounded_rect(&mut c, shadow, col, 8, Some(s)); } - draw_ops(&mut c, fonts, &win.deco, outer_x, outer_y); + draw_ops(&mut c, fonts, imgs, &win.deco, outer_x, outer_y); } match surfaces.get(&win.surface) { Some(s) => blit_surface(&mut c, s, g), @@ -620,7 +787,7 @@ impl Raster { } // 3. Panel (and its popups, menus and notices), then 4. client popups above everything. - draw_ops(&mut c, fonts, &f.panel, 0, 0); + draw_ops(&mut c, fonts, imgs, &f.panel, 0, 0); for p in &f.popups { if let Some(s) = surfaces.get(&p.surface) { blit_surface(&mut c, s, R::from(p.area)); @@ -784,4 +951,33 @@ mod tests { r.submit(&f, &mut Fonts::default()); assert_eq!(&r.to_argb8888()[..4], &[3, 2, 1, 255]); } + + #[test] + fn images_load_once_by_path_and_draw_scaled_png_and_svg() { + let dir = std::env::temp_dir().join(format!("chandra-img-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let png_path = dir.join("half.png"); + std::fs::write(&png_path, b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x04\x00\x00\x00\x04\x08\x06\x00\x00\x00\xa9\xf1\x9e\x7e\x00\x00\x00\x13\x49\x44\x41\x54\x78\x9c\x63\xf8\xcf\xc0\xf0\x1f\x84\x19\x60\x80\x74\x01\x00\x5e\x02\x0f\xf1\x64\xf6\x7e\x7c\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap(); + let svg_path = dir.join("blue.svg"); + std::fs::write(&svg_path, br##""##).unwrap(); + let mut r = Raster::default(); + let p = r.images.load(png_path.to_str().unwrap()); + assert!(p != 0); + assert_eq!(r.images.load(png_path.to_str().unwrap()), p); + let s = r.images.load(svg_path.to_str().unwrap()); + assert!(s != 0 && s != p); + assert_eq!(r.images.load("/nonexistent/icon.png"), 0); + let pic = |x: i32, w: i32, image: u32| wt::Op::DrawImage(wt::Picture { area: rect(x, 0, w, w), image }); + let mut f = frame(40, 20); + f.panel = vec![pic(0, 8, p), pic(10, 20, s), pic(30, 4, 999)]; + r.submit(&f, &mut Fonts::default()); + // The PNG doubled: left half red, right half untouched. + assert_eq!(px(&r, 1, 1), [255, 0, 0, 255]); + assert_eq!(px(&r, 6, 1), [0, 0, 0, 0]); + // The SVG rendered at 20px fills its square. + assert_eq!(px(&r, 20, 10), [0, 0, 255, 255]); + // An unknown handle draws nothing. + assert_eq!(px(&r, 31, 1), [0, 0, 0, 0]); + let _ = std::fs::remove_dir_all(&dir); + } } diff --git a/interface/chandra/desktop/icons/README.md b/interface/chandra/desktop/icons/README.md new file mode 100644 index 0000000..332f616 --- /dev/null +++ b/interface/chandra/desktop/icons/README.md @@ -0,0 +1,3 @@ +App icons. The host finds the app's .desktop file (by desktop id or StartupWMClass), reads its +Icon= key and looks it up in the freedesktop icon themes; the widget only gets a handle to draw +with a `draw-op.image`, never a path or pixels. \ No newline at end of file diff --git a/interface/chandra/desktop/icons/ffi.mbt b/interface/chandra/desktop/icons/ffi.mbt new file mode 100644 index 0000000..addbcb7 --- /dev/null +++ b/interface/chandra/desktop/icons/ffi.mbt @@ -0,0 +1,14 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! + +///| +fn wasmImportAppIcon(p0 : Int, p1 : Int) -> Int = "chandra:desktop/icons@0.1.0" "app-icon" + +///| +extern "wasm" fn mbt_ffi_free(position : Int) = +#|(func (param i32) local.get 0 call $moonbit.decref) + +///| +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) + diff --git a/interface/chandra/desktop/icons/moon.pkg b/interface/chandra/desktop/icons/moon.pkg new file mode 100644 index 0000000..e2a6033 --- /dev/null +++ b/interface/chandra/desktop/icons/moon.pkg @@ -0,0 +1 @@ +warnings = "-44" diff --git a/interface/chandra/desktop/icons/top.mbt b/interface/chandra/desktop/icons/top.mbt new file mode 100644 index 0000000..bcbbedc --- /dev/null +++ b/interface/chandra/desktop/icons/top.mbt @@ -0,0 +1,14 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! +///| +/// The icon of the app whose windows carry `app-id`, or 0 when the host has none (draw your +/// own stand-in then). +pub fn app_icon(app_id : String) -> UInt { + + let ptr = mbt_ffi_str2ptr(app_id) + let result : Int = wasmImportAppIcon(ptr, app_id.length()); + let ret = (result).reinterpret_as_uint() + mbt_ffi_free(ptr) + return ret + +} + diff --git a/interface/chandra/desktop/types/pkg.generated.mbti b/interface/chandra/desktop/types/pkg.generated.mbti index d6b3e6e..f22d80d 100644 --- a/interface/chandra/desktop/types/pkg.generated.mbti +++ b/interface/chandra/desktop/types/pkg.generated.mbti @@ -46,6 +46,7 @@ pub(all) enum DrawOp { Text(TextOp) PushClip(Rect) PopClip + Image(ImageOp) } pub(all) struct FillRectOp { @@ -54,6 +55,11 @@ pub(all) struct FillRectOp { radius : UInt32 } +pub(all) struct ImageOp { + rect : Rect + image : UInt32 +} + pub(all) enum InputEvent { PointerMove(Point) PointerDown(Point) diff --git a/interface/chandra/desktop/types/top.mbt b/interface/chandra/desktop/types/top.mbt index 3961624..c76a303 100644 --- a/interface/chandra/desktop/types/top.mbt +++ b/interface/chandra/desktop/types/top.mbt @@ -66,6 +66,12 @@ pub(all) struct TextOp { origin : Point; text : String; style : TextStyle } ///| +/// An image the host holds, scaled to fill `rect`. `image` is a handle a capability handed +/// out (`icons.app-icon`); 0 or an unknown handle draws nothing. +pub(all) struct ImageOp { + rect : Rect; image : UInt +} +///| /// The render-list vocabulary. Components never touch pixels: they return /// a list of these in their own local coordinate space (origin top-left), /// and the host/compositor translates, clips and rasterises them. @@ -77,6 +83,7 @@ pub(all) enum DrawOp { Text(TextOp) PushClip(Rect) PopClip + Image(ImageOp) } ///| pub(all) enum InputEvent { diff --git a/interface/chandra/host/plasmoids/top.mbt b/interface/chandra/host/plasmoids/top.mbt index 4b3849d..9ab476e 100644 --- a/interface/chandra/host/plasmoids/top.mbt +++ b/interface/chandra/host/plasmoids/top.mbt @@ -189,6 +189,10 @@ pub fn render(handle : UInt, box : @types.Size) -> Array[@types.DrawOp] { @types.DrawOp::PopClip } + 7 => { + + @types.DrawOp::Image(@types.ImageOp::{rect : @types.Rect::{x : mbt_ffi_load32((iter_base) + 4), y : mbt_ffi_load32((iter_base) + 8), w : (mbt_ffi_load32((iter_base) + 12)).reinterpret_as_uint(), h : (mbt_ffi_load32((iter_base) + 16)).reinterpret_as_uint()}, image : (mbt_ffi_load32((iter_base) + 20)).reinterpret_as_uint()}) + } _ => panic() } @@ -245,6 +249,10 @@ pub fn popup(handle : UInt) -> PopupSurface? { @types.DrawOp::PopClip } + 7 => { + + @types.DrawOp::Image(@types.ImageOp::{rect : @types.Rect::{x : mbt_ffi_load32((iter_base) + 4), y : mbt_ffi_load32((iter_base) + 8), w : (mbt_ffi_load32((iter_base) + 12)).reinterpret_as_uint(), h : (mbt_ffi_load32((iter_base) + 16)).reinterpret_as_uint()}, image : (mbt_ffi_load32((iter_base) + 20)).reinterpret_as_uint()}) + } _ => panic() } @@ -307,7 +315,8 @@ pub fn on_event(handle : UInt, on_popup : Bool, event : @types.InputEvent) -> Bo /// Row tables, as built by compositor/wasm: a LE u32 row count, then per row each field as a LE /// i32 ('i') or a LE u32 byte length + UTF-8 ('s'). `which`: 0 apps (sssss: id name comment /// category glyph), 1 windows (isii: id title focused minimized), 2 layouts (s), 3 modules -/// (sssi: id name path loaded), 4 instances (sss: slot module name), 5 wallpapers (s), 6 session settings (s). +/// (sssi: id name path loaded), 4 instances (sss: slot module name), 5 wallpapers (s), 6 session +/// settings (s), 7 icons (si: app id, raster image handle from `raster.load-image`) served by `icons.app-icon`. pub fn publish_table(which : UInt, rows : FixedArray[Byte]) -> Unit { let ptr = mbt_ffi_bytes2ptr(rows) diff --git a/interface/chandra/host/raster/ffi.mbt b/interface/chandra/host/raster/ffi.mbt index a79cb97..debbb20 100644 --- a/interface/chandra/host/raster/ffi.mbt +++ b/interface/chandra/host/raster/ffi.mbt @@ -6,6 +6,9 @@ fn wasmImportCapture(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, p5 : Int) ///| fn wasmImportRelease(p0 : Int) = "chandra:host/raster@0.1.0" "release" +///| +fn wasmImportLoadImage(p0 : Int, p1 : Int) -> Int = "chandra:host/raster@0.1.0" "load-image" + ///| fn wasmImportSubmit(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, p5 : Int, p6 : Int, p7 : Int, p8 : Int, p9 : Int, p10 : Int, p11 : Int, p12 : Int) = "chandra:host/raster@0.1.0" "submit" @@ -15,10 +18,6 @@ fn wasmImportReadFrame(p0 : Int) = "chandra:host/raster@0.1.0" "read-frame" ///| fn wasmImportExportMemfd() -> Int = "chandra:host/raster@0.1.0" "export-memfd" -///| -extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = -#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) - ///| extern "wasm" fn mbt_ffi_free(position : Int) = #|(func (param i32) local.get 0 call $moonbit.decref) @@ -30,15 +29,6 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = #| local.tee 1 i32.const 0 call $moonbit.init_array8 #| local.get 1) -///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = -#|(func (param i32) (result i32) local.get 0) - -///| -extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = -#|(func (param i32) (result i32) local.get 0 i32.load) - ///| extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] = #|(func (param i32) (param i32) (result i32) @@ -50,3 +40,16 @@ extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] = extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) +///| +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) local.get 0) + +///| +extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = +#|(func (param i32) (result i32) local.get 0 i32.load) + +///| +extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = +#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) + diff --git a/interface/chandra/host/raster/native_stub.mbt b/interface/chandra/host/raster/native_stub.mbt index 2642b09..9596290 100644 --- a/interface/chandra/host/raster/native_stub.mbt +++ b/interface/chandra/host/raster/native_stub.mbt @@ -10,6 +10,11 @@ fn wasmImportRelease(p0 : Int) -> Unit { abort("wasm-only host import") } +///| +fn wasmImportLoadImage(p0 : Int, p1 : Int) -> Int { + abort("wasm-only host import") +} + ///| fn wasmImportSubmit(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, p5 : Int, p6 : Int, p7 : Int, p8 : Int, p9 : Int, p10 : Int, p11 : Int, p12 : Int) -> Unit { abort("wasm-only host import") @@ -25,11 +30,6 @@ fn wasmImportExportMemfd() -> Int { abort("wasm-only host import") } -///| -fn mbt_ffi_store8(offset : Int, value : Int) -> Unit { - abort("wasm-only") -} - ///| fn mbt_ffi_free(position : Int) -> Unit { abort("wasm-only") @@ -40,6 +40,16 @@ fn mbt_ffi_malloc(size : Int) -> Int { abort("wasm-only") } +///| +fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] { + abort("wasm-only") +} + +///| +fn mbt_ffi_store32(offset : Int, value : Int) -> Unit { + abort("wasm-only") +} + ///| fn mbt_ffi_str2ptr(str : String) -> Int { abort("wasm-only") @@ -51,11 +61,6 @@ fn mbt_ffi_load32(offset : Int) -> Int { } ///| -fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] { - abort("wasm-only") -} - -///| -fn mbt_ffi_store32(offset : Int, value : Int) -> Unit { +fn mbt_ffi_store8(offset : Int, value : Int) -> Unit { abort("wasm-only") } diff --git a/interface/chandra/host/raster/top.mbt b/interface/chandra/host/raster/top.mbt index 16d9a31..260f2b0 100644 --- a/interface/chandra/host/raster/top.mbt +++ b/interface/chandra/host/raster/top.mbt @@ -29,6 +29,11 @@ pub(all) struct Label { x : Int; y : Int; text : String; size : Int; color : Color; align : Byte; bold : Bool; mono : Bool } ///| +/// An image from `load-image`, scaled to fill `area`. +pub(all) struct Picture { + area : Rect; image : UInt +} +///| pub(all) enum Op { FillRect(Fill) StrokeRect(Stroke) @@ -37,6 +42,7 @@ pub(all) enum Op { DrawText(Label) PushClip(Rect) PopClip + DrawImage(Picture) } ///| /// A toplevel: its decoration (drawn at `geometry` minus the insets), then its client surface. @@ -66,13 +72,25 @@ pub fn release(id : UInt) -> Unit { wasmImportRelease((id).reinterpret_as_int()); +} +///| +/// Decode the PNG or SVG file at `path` into a host image (an app icon), for `draw-image`. +/// Loading the same path again returns the same handle. 0 when it can't be read or decoded. +pub fn load_image(path : String) -> UInt { + + let ptr = mbt_ffi_str2ptr(path) + let result : Int = wasmImportLoadImage(ptr, path.length()); + let ret = (result).reinterpret_as_uint() + mbt_ffi_free(ptr) + return ret + } ///| /// Draw a frame into the host framebuffer. pub fn submit(f : Frame) -> Unit { let cleanup_list : Array[Int] = [] - let (lowered, lowered8, lowered9) = match ((f).wallpaper) { + let (lowered, lowered9, lowered10) = match ((f).wallpaper) { None => { ((0), 0, 0) @@ -173,6 +191,16 @@ pub fn submit(f : Frame) -> Unit { PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + DrawImage(payload8) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload8).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload8).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload8).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload8).area).h) + mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int()) + () } } @@ -184,28 +212,14 @@ pub fn submit(f : Frame) -> Unit { } } - let address18 = mbt_ffi_malloc(((f).background).length() * 32); - for index19 = 0; index19 < ((f).background).length(); index19 = index19 + 1 { - let iter_elem : Op = ((f).background)[(index19)] - let iter_base = address18 + (index19 * 32); + let address20 = mbt_ffi_malloc(((f).background).length() * 32); + for index21 = 0; index21 < ((f).background).length(); index21 = index21 + 1 { + let iter_elem : Op = ((f).background)[(index21)] + let iter_base = address20 + (index21 * 32); match iter_elem { - FillRect(payload10) => { + FillRect(payload11) => { mbt_ffi_store8((iter_base) + 0, (0)) - mbt_ffi_store32((iter_base) + 4, ((payload10).area).x) - mbt_ffi_store32((iter_base) + 8, ((payload10).area).y) - mbt_ffi_store32((iter_base) + 12, ((payload10).area).w) - mbt_ffi_store32((iter_base) + 16, ((payload10).area).h) - mbt_ffi_store8((iter_base) + 20, (((payload10).color).r).to_int()) - mbt_ffi_store8((iter_base) + 21, (((payload10).color).g).to_int()) - mbt_ffi_store8((iter_base) + 22, (((payload10).color).b).to_int()) - mbt_ffi_store8((iter_base) + 23, (((payload10).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload10).radius) - - () - } - StrokeRect(payload11) => { - mbt_ffi_store8((iter_base) + 0, (1)) mbt_ffi_store32((iter_base) + 4, ((payload11).area).x) mbt_ffi_store32((iter_base) + 8, ((payload11).area).y) mbt_ffi_store32((iter_base) + 12, ((payload11).area).w) @@ -214,80 +228,104 @@ pub fn submit(f : Frame) -> Unit { mbt_ffi_store8((iter_base) + 21, (((payload11).color).g).to_int()) mbt_ffi_store8((iter_base) + 22, (((payload11).color).b).to_int()) mbt_ffi_store8((iter_base) + 23, (((payload11).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload11).width) - mbt_ffi_store32((iter_base) + 28, (payload11).radius) + mbt_ffi_store32((iter_base) + 24, (payload11).radius) () } - DrawLine(payload12) => { - mbt_ffi_store8((iter_base) + 0, (2)) - mbt_ffi_store32((iter_base) + 4, (payload12).x0) - mbt_ffi_store32((iter_base) + 8, (payload12).y0) - mbt_ffi_store32((iter_base) + 12, (payload12).x1) - mbt_ffi_store32((iter_base) + 16, (payload12).y1) + StrokeRect(payload12) => { + mbt_ffi_store8((iter_base) + 0, (1)) + mbt_ffi_store32((iter_base) + 4, ((payload12).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload12).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload12).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload12).area).h) mbt_ffi_store8((iter_base) + 20, (((payload12).color).r).to_int()) mbt_ffi_store8((iter_base) + 21, (((payload12).color).g).to_int()) mbt_ffi_store8((iter_base) + 22, (((payload12).color).b).to_int()) mbt_ffi_store8((iter_base) + 23, (((payload12).color).a).to_int()) mbt_ffi_store32((iter_base) + 24, (payload12).width) + mbt_ffi_store32((iter_base) + 28, (payload12).radius) () } - DrawCircle(payload13) => { + DrawLine(payload13) => { + mbt_ffi_store8((iter_base) + 0, (2)) + mbt_ffi_store32((iter_base) + 4, (payload13).x0) + mbt_ffi_store32((iter_base) + 8, (payload13).y0) + mbt_ffi_store32((iter_base) + 12, (payload13).x1) + mbt_ffi_store32((iter_base) + 16, (payload13).y1) + mbt_ffi_store8((iter_base) + 20, (((payload13).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload13).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload13).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload13).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, (payload13).width) + + () + } + DrawCircle(payload14) => { mbt_ffi_store8((iter_base) + 0, (3)) - mbt_ffi_store32((iter_base) + 4, (payload13).cx) - mbt_ffi_store32((iter_base) + 8, (payload13).cy) - mbt_ffi_store32((iter_base) + 12, (payload13).radius) - mbt_ffi_store8((iter_base) + 16, (((payload13).color).r).to_int()) - mbt_ffi_store8((iter_base) + 17, (((payload13).color).g).to_int()) - mbt_ffi_store8((iter_base) + 18, (((payload13).color).b).to_int()) - mbt_ffi_store8((iter_base) + 19, (((payload13).color).a).to_int()) - mbt_ffi_store8((iter_base) + 20, (if (payload13).filled { 1 } else { 0 })) + mbt_ffi_store32((iter_base) + 4, (payload14).cx) + mbt_ffi_store32((iter_base) + 8, (payload14).cy) + mbt_ffi_store32((iter_base) + 12, (payload14).radius) + mbt_ffi_store8((iter_base) + 16, (((payload14).color).r).to_int()) + mbt_ffi_store8((iter_base) + 17, (((payload14).color).g).to_int()) + mbt_ffi_store8((iter_base) + 18, (((payload14).color).b).to_int()) + mbt_ffi_store8((iter_base) + 19, (((payload14).color).a).to_int()) + mbt_ffi_store8((iter_base) + 20, (if (payload14).filled { 1 } else { 0 })) () } - DrawText(payload14) => { + DrawText(payload15) => { mbt_ffi_store8((iter_base) + 0, (4)) - mbt_ffi_store32((iter_base) + 4, (payload14).x) - mbt_ffi_store32((iter_base) + 8, (payload14).y) + mbt_ffi_store32((iter_base) + 4, (payload15).x) + mbt_ffi_store32((iter_base) + 8, (payload15).y) - let ptr15 = mbt_ffi_str2ptr((payload14).text) - mbt_ffi_store32((iter_base) + 16, (payload14).text.length()) - mbt_ffi_store32((iter_base) + 12, ptr15) - mbt_ffi_store32((iter_base) + 20, (payload14).size) - mbt_ffi_store8((iter_base) + 24, (((payload14).color).r).to_int()) - mbt_ffi_store8((iter_base) + 25, (((payload14).color).g).to_int()) - mbt_ffi_store8((iter_base) + 26, (((payload14).color).b).to_int()) - mbt_ffi_store8((iter_base) + 27, (((payload14).color).a).to_int()) - mbt_ffi_store8((iter_base) + 28, ((payload14).align).to_int()) - mbt_ffi_store8((iter_base) + 29, (if (payload14).bold { 1 } else { 0 })) - mbt_ffi_store8((iter_base) + 30, (if (payload14).mono { 1 } else { 0 })) - cleanup_list.push(ptr15) + let ptr16 = mbt_ffi_str2ptr((payload15).text) + mbt_ffi_store32((iter_base) + 16, (payload15).text.length()) + mbt_ffi_store32((iter_base) + 12, ptr16) + mbt_ffi_store32((iter_base) + 20, (payload15).size) + mbt_ffi_store8((iter_base) + 24, (((payload15).color).r).to_int()) + mbt_ffi_store8((iter_base) + 25, (((payload15).color).g).to_int()) + mbt_ffi_store8((iter_base) + 26, (((payload15).color).b).to_int()) + mbt_ffi_store8((iter_base) + 27, (((payload15).color).a).to_int()) + mbt_ffi_store8((iter_base) + 28, ((payload15).align).to_int()) + mbt_ffi_store8((iter_base) + 29, (if (payload15).bold { 1 } else { 0 })) + mbt_ffi_store8((iter_base) + 30, (if (payload15).mono { 1 } else { 0 })) + cleanup_list.push(ptr16) () } - PushClip(payload16) => { + PushClip(payload17) => { mbt_ffi_store8((iter_base) + 0, (5)) - mbt_ffi_store32((iter_base) + 4, (payload16).x) - mbt_ffi_store32((iter_base) + 8, (payload16).y) - mbt_ffi_store32((iter_base) + 12, (payload16).w) - mbt_ffi_store32((iter_base) + 16, (payload16).h) + mbt_ffi_store32((iter_base) + 4, (payload17).x) + mbt_ffi_store32((iter_base) + 8, (payload17).y) + mbt_ffi_store32((iter_base) + 12, (payload17).w) + mbt_ffi_store32((iter_base) + 16, (payload17).h) () } PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + DrawImage(payload19) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload19).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload19).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload19).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload19).area).h) + mbt_ffi_store32((iter_base) + 20, ((payload19).image).reinterpret_as_int()) + () } } } - let address30 = mbt_ffi_malloc(((f).windows).length() * 48); - for index31 = 0; index31 < ((f).windows).length(); index31 = index31 + 1 { - let iter_elem : Window = ((f).windows)[(index31)] - let iter_base = address30 + (index31 * 48); + let address33 = mbt_ffi_malloc(((f).windows).length() * 48); + for index34 = 0; index34 < ((f).windows).length(); index34 = index34 + 1 { + let iter_elem : Window = ((f).windows)[(index34)] + let iter_base = address33 + (index34 * 48); mbt_ffi_store32((iter_base) + 0, ((iter_elem).geometry).x) mbt_ffi_store32((iter_base) + 4, ((iter_elem).geometry).y) mbt_ffi_store32((iter_base) + 8, ((iter_elem).geometry).w) @@ -297,217 +335,237 @@ pub fn submit(f : Frame) -> Unit { mbt_ffi_store32((iter_base) + 24, (iter_elem).inset_bottom) mbt_ffi_store32((iter_base) + 28, (iter_elem).inset_left) - let address28 = mbt_ffi_malloc(((iter_elem).deco).length() * 32); - for index29 = 0; index29 < ((iter_elem).deco).length(); index29 = index29 + 1 { - let iter_elem : Op = ((iter_elem).deco)[(index29)] - let iter_base = address28 + (index29 * 32); + let address31 = mbt_ffi_malloc(((iter_elem).deco).length() * 32); + for index32 = 0; index32 < ((iter_elem).deco).length(); index32 = index32 + 1 { + let iter_elem : Op = ((iter_elem).deco)[(index32)] + let iter_base = address31 + (index32 * 32); match iter_elem { - FillRect(payload20) => { + FillRect(payload22) => { mbt_ffi_store8((iter_base) + 0, (0)) - mbt_ffi_store32((iter_base) + 4, ((payload20).area).x) - mbt_ffi_store32((iter_base) + 8, ((payload20).area).y) - mbt_ffi_store32((iter_base) + 12, ((payload20).area).w) - mbt_ffi_store32((iter_base) + 16, ((payload20).area).h) - mbt_ffi_store8((iter_base) + 20, (((payload20).color).r).to_int()) - mbt_ffi_store8((iter_base) + 21, (((payload20).color).g).to_int()) - mbt_ffi_store8((iter_base) + 22, (((payload20).color).b).to_int()) - mbt_ffi_store8((iter_base) + 23, (((payload20).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload20).radius) - - () - } - StrokeRect(payload21) => { - mbt_ffi_store8((iter_base) + 0, (1)) - mbt_ffi_store32((iter_base) + 4, ((payload21).area).x) - mbt_ffi_store32((iter_base) + 8, ((payload21).area).y) - mbt_ffi_store32((iter_base) + 12, ((payload21).area).w) - mbt_ffi_store32((iter_base) + 16, ((payload21).area).h) - mbt_ffi_store8((iter_base) + 20, (((payload21).color).r).to_int()) - mbt_ffi_store8((iter_base) + 21, (((payload21).color).g).to_int()) - mbt_ffi_store8((iter_base) + 22, (((payload21).color).b).to_int()) - mbt_ffi_store8((iter_base) + 23, (((payload21).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload21).width) - mbt_ffi_store32((iter_base) + 28, (payload21).radius) - - () - } - DrawLine(payload22) => { - mbt_ffi_store8((iter_base) + 0, (2)) - mbt_ffi_store32((iter_base) + 4, (payload22).x0) - mbt_ffi_store32((iter_base) + 8, (payload22).y0) - mbt_ffi_store32((iter_base) + 12, (payload22).x1) - mbt_ffi_store32((iter_base) + 16, (payload22).y1) + mbt_ffi_store32((iter_base) + 4, ((payload22).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload22).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload22).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload22).area).h) mbt_ffi_store8((iter_base) + 20, (((payload22).color).r).to_int()) mbt_ffi_store8((iter_base) + 21, (((payload22).color).g).to_int()) mbt_ffi_store8((iter_base) + 22, (((payload22).color).b).to_int()) mbt_ffi_store8((iter_base) + 23, (((payload22).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload22).width) + mbt_ffi_store32((iter_base) + 24, (payload22).radius) () } - DrawCircle(payload23) => { + StrokeRect(payload23) => { + mbt_ffi_store8((iter_base) + 0, (1)) + mbt_ffi_store32((iter_base) + 4, ((payload23).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload23).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload23).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload23).area).h) + mbt_ffi_store8((iter_base) + 20, (((payload23).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload23).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload23).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload23).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, (payload23).width) + mbt_ffi_store32((iter_base) + 28, (payload23).radius) + + () + } + DrawLine(payload24) => { + mbt_ffi_store8((iter_base) + 0, (2)) + mbt_ffi_store32((iter_base) + 4, (payload24).x0) + mbt_ffi_store32((iter_base) + 8, (payload24).y0) + mbt_ffi_store32((iter_base) + 12, (payload24).x1) + mbt_ffi_store32((iter_base) + 16, (payload24).y1) + mbt_ffi_store8((iter_base) + 20, (((payload24).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload24).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload24).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload24).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, (payload24).width) + + () + } + DrawCircle(payload25) => { mbt_ffi_store8((iter_base) + 0, (3)) - mbt_ffi_store32((iter_base) + 4, (payload23).cx) - mbt_ffi_store32((iter_base) + 8, (payload23).cy) - mbt_ffi_store32((iter_base) + 12, (payload23).radius) - mbt_ffi_store8((iter_base) + 16, (((payload23).color).r).to_int()) - mbt_ffi_store8((iter_base) + 17, (((payload23).color).g).to_int()) - mbt_ffi_store8((iter_base) + 18, (((payload23).color).b).to_int()) - mbt_ffi_store8((iter_base) + 19, (((payload23).color).a).to_int()) - mbt_ffi_store8((iter_base) + 20, (if (payload23).filled { 1 } else { 0 })) + mbt_ffi_store32((iter_base) + 4, (payload25).cx) + mbt_ffi_store32((iter_base) + 8, (payload25).cy) + mbt_ffi_store32((iter_base) + 12, (payload25).radius) + mbt_ffi_store8((iter_base) + 16, (((payload25).color).r).to_int()) + mbt_ffi_store8((iter_base) + 17, (((payload25).color).g).to_int()) + mbt_ffi_store8((iter_base) + 18, (((payload25).color).b).to_int()) + mbt_ffi_store8((iter_base) + 19, (((payload25).color).a).to_int()) + mbt_ffi_store8((iter_base) + 20, (if (payload25).filled { 1 } else { 0 })) () } - DrawText(payload24) => { + DrawText(payload26) => { mbt_ffi_store8((iter_base) + 0, (4)) - mbt_ffi_store32((iter_base) + 4, (payload24).x) - mbt_ffi_store32((iter_base) + 8, (payload24).y) - - let ptr25 = mbt_ffi_str2ptr((payload24).text) - mbt_ffi_store32((iter_base) + 16, (payload24).text.length()) - mbt_ffi_store32((iter_base) + 12, ptr25) - mbt_ffi_store32((iter_base) + 20, (payload24).size) - mbt_ffi_store8((iter_base) + 24, (((payload24).color).r).to_int()) - mbt_ffi_store8((iter_base) + 25, (((payload24).color).g).to_int()) - mbt_ffi_store8((iter_base) + 26, (((payload24).color).b).to_int()) - mbt_ffi_store8((iter_base) + 27, (((payload24).color).a).to_int()) - mbt_ffi_store8((iter_base) + 28, ((payload24).align).to_int()) - mbt_ffi_store8((iter_base) + 29, (if (payload24).bold { 1 } else { 0 })) - mbt_ffi_store8((iter_base) + 30, (if (payload24).mono { 1 } else { 0 })) - cleanup_list.push(ptr25) - - () - } - PushClip(payload26) => { - mbt_ffi_store8((iter_base) + 0, (5)) mbt_ffi_store32((iter_base) + 4, (payload26).x) mbt_ffi_store32((iter_base) + 8, (payload26).y) - mbt_ffi_store32((iter_base) + 12, (payload26).w) - mbt_ffi_store32((iter_base) + 16, (payload26).h) + + let ptr27 = mbt_ffi_str2ptr((payload26).text) + mbt_ffi_store32((iter_base) + 16, (payload26).text.length()) + mbt_ffi_store32((iter_base) + 12, ptr27) + mbt_ffi_store32((iter_base) + 20, (payload26).size) + mbt_ffi_store8((iter_base) + 24, (((payload26).color).r).to_int()) + mbt_ffi_store8((iter_base) + 25, (((payload26).color).g).to_int()) + mbt_ffi_store8((iter_base) + 26, (((payload26).color).b).to_int()) + mbt_ffi_store8((iter_base) + 27, (((payload26).color).a).to_int()) + mbt_ffi_store8((iter_base) + 28, ((payload26).align).to_int()) + mbt_ffi_store8((iter_base) + 29, (if (payload26).bold { 1 } else { 0 })) + mbt_ffi_store8((iter_base) + 30, (if (payload26).mono { 1 } else { 0 })) + cleanup_list.push(ptr27) + + () + } + PushClip(payload28) => { + mbt_ffi_store8((iter_base) + 0, (5)) + mbt_ffi_store32((iter_base) + 4, (payload28).x) + mbt_ffi_store32((iter_base) + 8, (payload28).y) + mbt_ffi_store32((iter_base) + 12, (payload28).w) + mbt_ffi_store32((iter_base) + 16, (payload28).h) () } PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + DrawImage(payload30) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload30).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload30).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload30).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload30).area).h) + mbt_ffi_store32((iter_base) + 20, ((payload30).image).reinterpret_as_int()) + () } } } mbt_ffi_store32((iter_base) + 36, ((iter_elem).deco).length()) - mbt_ffi_store32((iter_base) + 32, address28) + mbt_ffi_store32((iter_base) + 32, address31) mbt_ffi_store8((iter_base) + 40, (if (iter_elem).fullscreen { 1 } else { 0 })) mbt_ffi_store32((iter_base) + 44, ((iter_elem).surface).reinterpret_as_int()) - cleanup_list.push(address28) + cleanup_list.push(address31) } - let address40 = mbt_ffi_malloc(((f).panel).length() * 32); - for index41 = 0; index41 < ((f).panel).length(); index41 = index41 + 1 { - let iter_elem : Op = ((f).panel)[(index41)] - let iter_base = address40 + (index41 * 32); + let address44 = mbt_ffi_malloc(((f).panel).length() * 32); + for index45 = 0; index45 < ((f).panel).length(); index45 = index45 + 1 { + let iter_elem : Op = ((f).panel)[(index45)] + let iter_base = address44 + (index45 * 32); match iter_elem { - FillRect(payload32) => { + FillRect(payload35) => { mbt_ffi_store8((iter_base) + 0, (0)) - mbt_ffi_store32((iter_base) + 4, ((payload32).area).x) - mbt_ffi_store32((iter_base) + 8, ((payload32).area).y) - mbt_ffi_store32((iter_base) + 12, ((payload32).area).w) - mbt_ffi_store32((iter_base) + 16, ((payload32).area).h) - mbt_ffi_store8((iter_base) + 20, (((payload32).color).r).to_int()) - mbt_ffi_store8((iter_base) + 21, (((payload32).color).g).to_int()) - mbt_ffi_store8((iter_base) + 22, (((payload32).color).b).to_int()) - mbt_ffi_store8((iter_base) + 23, (((payload32).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload32).radius) + mbt_ffi_store32((iter_base) + 4, ((payload35).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload35).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload35).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload35).area).h) + mbt_ffi_store8((iter_base) + 20, (((payload35).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload35).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload35).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload35).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, (payload35).radius) () } - StrokeRect(payload33) => { + StrokeRect(payload36) => { mbt_ffi_store8((iter_base) + 0, (1)) - mbt_ffi_store32((iter_base) + 4, ((payload33).area).x) - mbt_ffi_store32((iter_base) + 8, ((payload33).area).y) - mbt_ffi_store32((iter_base) + 12, ((payload33).area).w) - mbt_ffi_store32((iter_base) + 16, ((payload33).area).h) - mbt_ffi_store8((iter_base) + 20, (((payload33).color).r).to_int()) - mbt_ffi_store8((iter_base) + 21, (((payload33).color).g).to_int()) - mbt_ffi_store8((iter_base) + 22, (((payload33).color).b).to_int()) - mbt_ffi_store8((iter_base) + 23, (((payload33).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload33).width) - mbt_ffi_store32((iter_base) + 28, (payload33).radius) + mbt_ffi_store32((iter_base) + 4, ((payload36).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload36).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload36).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload36).area).h) + mbt_ffi_store8((iter_base) + 20, (((payload36).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload36).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload36).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload36).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, (payload36).width) + mbt_ffi_store32((iter_base) + 28, (payload36).radius) () } - DrawLine(payload34) => { + DrawLine(payload37) => { mbt_ffi_store8((iter_base) + 0, (2)) - mbt_ffi_store32((iter_base) + 4, (payload34).x0) - mbt_ffi_store32((iter_base) + 8, (payload34).y0) - mbt_ffi_store32((iter_base) + 12, (payload34).x1) - mbt_ffi_store32((iter_base) + 16, (payload34).y1) - mbt_ffi_store8((iter_base) + 20, (((payload34).color).r).to_int()) - mbt_ffi_store8((iter_base) + 21, (((payload34).color).g).to_int()) - mbt_ffi_store8((iter_base) + 22, (((payload34).color).b).to_int()) - mbt_ffi_store8((iter_base) + 23, (((payload34).color).a).to_int()) - mbt_ffi_store32((iter_base) + 24, (payload34).width) + mbt_ffi_store32((iter_base) + 4, (payload37).x0) + mbt_ffi_store32((iter_base) + 8, (payload37).y0) + mbt_ffi_store32((iter_base) + 12, (payload37).x1) + mbt_ffi_store32((iter_base) + 16, (payload37).y1) + mbt_ffi_store8((iter_base) + 20, (((payload37).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload37).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload37).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload37).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, (payload37).width) () } - DrawCircle(payload35) => { + DrawCircle(payload38) => { mbt_ffi_store8((iter_base) + 0, (3)) - mbt_ffi_store32((iter_base) + 4, (payload35).cx) - mbt_ffi_store32((iter_base) + 8, (payload35).cy) - mbt_ffi_store32((iter_base) + 12, (payload35).radius) - mbt_ffi_store8((iter_base) + 16, (((payload35).color).r).to_int()) - mbt_ffi_store8((iter_base) + 17, (((payload35).color).g).to_int()) - mbt_ffi_store8((iter_base) + 18, (((payload35).color).b).to_int()) - mbt_ffi_store8((iter_base) + 19, (((payload35).color).a).to_int()) - mbt_ffi_store8((iter_base) + 20, (if (payload35).filled { 1 } else { 0 })) + mbt_ffi_store32((iter_base) + 4, (payload38).cx) + mbt_ffi_store32((iter_base) + 8, (payload38).cy) + mbt_ffi_store32((iter_base) + 12, (payload38).radius) + mbt_ffi_store8((iter_base) + 16, (((payload38).color).r).to_int()) + mbt_ffi_store8((iter_base) + 17, (((payload38).color).g).to_int()) + mbt_ffi_store8((iter_base) + 18, (((payload38).color).b).to_int()) + mbt_ffi_store8((iter_base) + 19, (((payload38).color).a).to_int()) + mbt_ffi_store8((iter_base) + 20, (if (payload38).filled { 1 } else { 0 })) () } - DrawText(payload36) => { + DrawText(payload39) => { mbt_ffi_store8((iter_base) + 0, (4)) - mbt_ffi_store32((iter_base) + 4, (payload36).x) - mbt_ffi_store32((iter_base) + 8, (payload36).y) + mbt_ffi_store32((iter_base) + 4, (payload39).x) + mbt_ffi_store32((iter_base) + 8, (payload39).y) - let ptr37 = mbt_ffi_str2ptr((payload36).text) - mbt_ffi_store32((iter_base) + 16, (payload36).text.length()) - mbt_ffi_store32((iter_base) + 12, ptr37) - mbt_ffi_store32((iter_base) + 20, (payload36).size) - mbt_ffi_store8((iter_base) + 24, (((payload36).color).r).to_int()) - mbt_ffi_store8((iter_base) + 25, (((payload36).color).g).to_int()) - mbt_ffi_store8((iter_base) + 26, (((payload36).color).b).to_int()) - mbt_ffi_store8((iter_base) + 27, (((payload36).color).a).to_int()) - mbt_ffi_store8((iter_base) + 28, ((payload36).align).to_int()) - mbt_ffi_store8((iter_base) + 29, (if (payload36).bold { 1 } else { 0 })) - mbt_ffi_store8((iter_base) + 30, (if (payload36).mono { 1 } else { 0 })) - cleanup_list.push(ptr37) + let ptr40 = mbt_ffi_str2ptr((payload39).text) + mbt_ffi_store32((iter_base) + 16, (payload39).text.length()) + mbt_ffi_store32((iter_base) + 12, ptr40) + mbt_ffi_store32((iter_base) + 20, (payload39).size) + mbt_ffi_store8((iter_base) + 24, (((payload39).color).r).to_int()) + mbt_ffi_store8((iter_base) + 25, (((payload39).color).g).to_int()) + mbt_ffi_store8((iter_base) + 26, (((payload39).color).b).to_int()) + mbt_ffi_store8((iter_base) + 27, (((payload39).color).a).to_int()) + mbt_ffi_store8((iter_base) + 28, ((payload39).align).to_int()) + mbt_ffi_store8((iter_base) + 29, (if (payload39).bold { 1 } else { 0 })) + mbt_ffi_store8((iter_base) + 30, (if (payload39).mono { 1 } else { 0 })) + cleanup_list.push(ptr40) () } - PushClip(payload38) => { + PushClip(payload41) => { mbt_ffi_store8((iter_base) + 0, (5)) - mbt_ffi_store32((iter_base) + 4, (payload38).x) - mbt_ffi_store32((iter_base) + 8, (payload38).y) - mbt_ffi_store32((iter_base) + 12, (payload38).w) - mbt_ffi_store32((iter_base) + 16, (payload38).h) + mbt_ffi_store32((iter_base) + 4, (payload41).x) + mbt_ffi_store32((iter_base) + 8, (payload41).y) + mbt_ffi_store32((iter_base) + 12, (payload41).w) + mbt_ffi_store32((iter_base) + 16, (payload41).h) () } PopClip => { mbt_ffi_store8((iter_base) + 0, (6)) + () + } + DrawImage(payload43) => { + mbt_ffi_store8((iter_base) + 0, (7)) + mbt_ffi_store32((iter_base) + 4, ((payload43).area).x) + mbt_ffi_store32((iter_base) + 8, ((payload43).area).y) + mbt_ffi_store32((iter_base) + 12, ((payload43).area).w) + mbt_ffi_store32((iter_base) + 16, ((payload43).area).h) + mbt_ffi_store32((iter_base) + 20, ((payload43).image).reinterpret_as_int()) + () } } } - let address42 = mbt_ffi_malloc(((f).popups).length() * 20); - for index43 = 0; index43 < ((f).popups).length(); index43 = index43 + 1 { - let iter_elem : Popup = ((f).popups)[(index43)] - let iter_base = address42 + (index43 * 20); + let address46 = mbt_ffi_malloc(((f).popups).length() * 20); + for index47 = 0; index47 < ((f).popups).length(); index47 = index47 + 1 { + let iter_elem : Popup = ((f).popups)[(index47)] + let iter_base = address46 + (index47 * 20); mbt_ffi_store32((iter_base) + 0, ((iter_elem).area).x) mbt_ffi_store32((iter_base) + 4, ((iter_elem).area).y) mbt_ffi_store32((iter_base) + 8, ((iter_elem).area).w) @@ -515,11 +573,11 @@ pub fn submit(f : Frame) -> Unit { mbt_ffi_store32((iter_base) + 16, ((iter_elem).surface).reinterpret_as_int()) } - wasmImportSubmit((f).width, (f).height, lowered, lowered8, lowered9, address18, ((f).background).length(), address30, ((f).windows).length(), address40, ((f).panel).length(), address42, ((f).popups).length()); - mbt_ffi_free(address18) - mbt_ffi_free(address30) - mbt_ffi_free(address40) - mbt_ffi_free(address42) + wasmImportSubmit((f).width, (f).height, lowered, lowered9, lowered10, address20, ((f).background).length(), address33, ((f).windows).length(), address44, ((f).panel).length(), address46, ((f).popups).length()); + mbt_ffi_free(address20) + mbt_ffi_free(address33) + mbt_ffi_free(address44) + mbt_ffi_free(address46) cleanup_list.each(mbt_ffi_free) diff --git a/wit/capabilities.wit b/wit/capabilities.wit index 836fb09..27e07be 100644 --- a/wit/capabilities.wit +++ b/wit/capabilities.wit @@ -69,6 +69,15 @@ interface windows { } +/// App icons. The host finds the app's .desktop file (by desktop id or StartupWMClass), reads its +/// Icon= key and looks it up in the freedesktop icon themes; the widget only gets a handle to draw +/// with a `draw-op.image`, never a path or pixels. +interface icons { + /// The icon of the app whose windows carry `app-id`, or 0 when the host has none (draw your + /// own stand-in then). + app-icon: func(app-id: string) -> u32; +} + /// The window manager's layout mode (tiling / monocle / floating). interface layout-control { /// Display names, indexed by mode. diff --git a/wit/host/plasmoids.wit b/wit/host/plasmoids.wit index 699e1a2..96151d7 100644 --- a/wit/host/plasmoids.wit +++ b/wit/host/plasmoids.wit @@ -40,7 +40,8 @@ interface plasmoids { /// Row tables, as built by compositor/wasm: a LE u32 row count, then per row each field as a LE /// i32 ('i') or a LE u32 byte length + UTF-8 ('s'). `which`: 0 apps (sssss: id name comment /// category glyph), 1 windows (isii: id title focused minimized), 2 layouts (s), 3 modules - /// (sssi: id name path loaded), 4 instances (sss: slot module name), 5 wallpapers (s), 6 session settings (s). + /// (sssi: id name path loaded), 4 instances (sss: slot module name), 5 wallpapers (s), 6 session + /// settings (s), 7 icons (si: app id, raster image handle from `raster.load-image`) served by `icons.app-icon`. publish-table: func(which: u32, rows: list); /// Layout mode (0 tiling, 1 monocle, 2 floating) and whether the Interface Explorer is on. publish-state: func(layout-mode: s32, inspector-on: bool); diff --git a/wit/host/raster.wit b/wit/host/raster.wit index fbd27b3..b55e561 100644 --- a/wit/host/raster.wit +++ b/wit/host/raster.wit @@ -14,6 +14,8 @@ interface raster { record disc { cx: s32, cy: s32, radius: s32, color: color, filled: bool } /// `align`: 0 left, 1 centre, 2 right of `x`; `y` is the text's vertical centre. record label { x: s32, y: s32, text: string, size: s32, color: color, align: u8, bold: bool, mono: bool } + /// An image from `load-image`, scaled to fill `area`. + record picture { area: rect, image: u32 } variant op { fill-rect(fill), stroke-rect(stroke), @@ -22,6 +24,7 @@ interface raster { draw-text(label), push-clip(rect), pop-clip, + draw-image(picture), } /// A toplevel: its decoration (drawn at `geometry` minus the insets), then its client surface. @@ -52,6 +55,9 @@ interface raster { /// host surface. `format` is the wl_shm format (0 ARGB8888, 1 XRGB8888). 0 on failure. capture: func(fd: s32, offset: s32, stride: s32, width: s32, height: s32, format: s32) -> u32; release: func(id: u32); + /// Decode the PNG or SVG file at `path` into a host image (an app icon), for `draw-image`. + /// Loading the same path again returns the same handle. 0 when it can't be read or decoded. + load-image: func(path: string) -> u32; /// Draw a frame into the host framebuffer. submit: func(f: frame); /// The framebuffer as RGBA8888 (for screenshots and tests; the display path never needs it). diff --git a/wit/types.wit b/wit/types.wit index 82ce45d..f0f9f79 100644 --- a/wit/types.wit +++ b/wit/types.wit @@ -26,6 +26,9 @@ interface types { /// `origin` is the left edge (align=left), centre (center) or right edge /// (right) of the text, at the vertical *middle* of the line box. record text-op { origin: point, text: string, style: text-style } + /// An image the host holds, scaled to fill `rect`. `image` is a handle a capability handed + /// out (`icons.app-icon`); 0 or an unknown handle draws nothing. + record image-op { rect: rect, image: u32 } /// The render-list vocabulary. Components never touch pixels: they return /// a list of these in their own local coordinate space (origin top-left), @@ -38,6 +41,7 @@ interface types { text(text-op), push-clip(rect), pop-clip, + image(image-op), } variant input-event { diff --git a/wit/worlds.wit b/wit/worlds.wit index 1e9c0fd..08cdf1c 100644 --- a/wit/worlds.wit +++ b/wit/worlds.wit @@ -40,6 +40,7 @@ world pulse-widget { world taskbar-widget { include widget; import windows; + import icons; } /// Layout switcher. @@ -70,6 +71,7 @@ world plasmoid-host { import session; import wallpaper; import session-settings; + import icons; export plasmoid; }