From 5dfe34027b7b8206b4f8d52c84ad5e4fdb77e7d6 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 16:38:46 +0000 Subject: [PATCH] Wallpaper chooser panel module, and four new wallpapers - New 'wallpaper' capability (wit/capabilities.wit: wallpapers, current-wallpaper, set-wallpaper) and 'wallpaper' panel module: a button whose popup lists the wallpapers with the current one ticked. Clicking a row switches the desktop at once (the list stays open to compare); Enter picks the highlighted row, Escape or a click elsewhere closes. In the default panel, before Log out. - lib/shell: render_wallpaper(screen, tokens, index) with Plasma glow (the old background), Aurora, Starfield, Dusk and Solid, all draw ops; the themed ones follow a theme swap. - The shell draws the chosen one (the renderer's wallpaper cache redraws it on change) and saves it in state.edn as :wallpaper (:aurora, ...); unknown names fall back to the default. - Served by both hosts: wasmer stub (native) and host-wasm (publish-wallpaper, wallpapers table 5, set-wallpaper request). - Tests: lib/shell wallpaper test, host-wasm module test, native end-to-end test (open, pick, Escape) and a state.edn round trip. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hhM8CTYpe2XVYTFQGn --- components/wallpaper/gen/ffi.mbt | 82 ++++ .../moonplasma/desktop/plasmoid/README.md | 1 + .../moonplasma/desktop/plasmoid/ffi.mbt | 409 ++++++++++++++++++ .../moonplasma/desktop/plasmoid/moon.pkg | 8 + .../moonplasma/desktop/plasmoid/stub.mbt | 175 ++++++++ .../moonplasma/desktop/plasmoid/top.mbt | 42 ++ components/wallpaper/gen/moon.pkg | 26 ++ compositor/host/input_test.mbt | 63 +++ compositor/scene/pkg.generated.mbti | 3 + compositor/shell/shell.mbt | 5 +- compositor/shell/state.mbt | 33 +- compositor/shell/widgets.mbt | 22 +- compositor/wasm/wasm_host.mbt | 9 + compositor/wasm/wasm_native.mbt | 9 + compositor/wasm/wasm_stub.c | 14 +- host-wasm/src/main.rs | 5 + host-wasm/src/plasmoid.rs | 51 ++- .../moonplasma/desktop/wallpaper/README.md | 2 + .../moonplasma/desktop/wallpaper/ffi.mbt | 33 ++ .../moonplasma/desktop/wallpaper/moon.pkg | 1 + .../moonplasma/desktop/wallpaper/top.mbt | 38 ++ .../host/display/pkg.generated.mbti | 9 + interface/moonplasma/host/plasmoids/ffi.mbt | 61 +-- .../moonplasma/host/plasmoids/native_stub.mbt | 53 ++- .../host/plasmoids/pkg.generated.mbti | 4 + interface/moonplasma/host/plasmoids/top.mbt | 14 +- .../moonplasma/host/sys/pkg.generated.mbti | 6 + lib/shell/pkg.generated.mbti | 4 + lib/shell/shell.mbt | 180 +++++++- scripts/components.txt | 1 + wit/capabilities.wit | 9 + wit/host/plasmoids.wit | 5 +- wit/worlds.wit | 6 + world/wallpaper-widget/README.md | 1 + world/wallpaper-widget/ffi_import.mbt | 2 + world/wallpaper-widget/import.mbt | 2 + world/wallpaper-widget/moon.pkg | 1 + 37 files changed, 1311 insertions(+), 78 deletions(-) create mode 100644 components/wallpaper/gen/ffi.mbt create mode 100644 components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/README.md create mode 100644 components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/ffi.mbt create mode 100644 components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/moon.pkg create mode 100644 components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/stub.mbt create mode 100644 components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/top.mbt create mode 100644 components/wallpaper/gen/moon.pkg create mode 100644 interface/moonplasma/desktop/wallpaper/README.md create mode 100644 interface/moonplasma/desktop/wallpaper/ffi.mbt create mode 100644 interface/moonplasma/desktop/wallpaper/moon.pkg create mode 100644 interface/moonplasma/desktop/wallpaper/top.mbt create mode 100644 world/wallpaper-widget/README.md create mode 100644 world/wallpaper-widget/ffi_import.mbt create mode 100644 world/wallpaper-widget/import.mbt create mode 100644 world/wallpaper-widget/moon.pkg diff --git a/components/wallpaper/gen/ffi.mbt b/components/wallpaper/gen/ffi.mbt new file mode 100644 index 0000000..b6c5012 --- /dev/null +++ b/components/wallpaper/gen/ffi.mbt @@ -0,0 +1,82 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! + +///| +pub fn mbt_ffi_cabi_realloc( +src_offset : Int, +src_size : Int, +_dst_alignment : Int, +dst_size : Int, +) -> Int { + // malloc + if src_offset == 0 && src_size == 0 { + return mbt_ffi_malloc(dst_size) + } + // free + if dst_size == 0 { + mbt_ffi_free(src_offset) + return 0 + } + // realloc + let dst = mbt_ffi_malloc(dst_size) + mbt_ffi_copy(dst, src_offset, if src_size < dst_size { src_size } else { dst_size }) + mbt_ffi_free(src_offset) + dst +} + +///| +extern "wasm" fn mbt_ffi_copy(dest : Int, src : Int, len : Int) = +#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy) + +///| +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) + +///| +#doc(hidden) +pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { + @plasmoid.wasmExportRender(p0, p1) +} + +///| +#doc(hidden) +pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { + @plasmoid.wasmExportRenderPostReturn(p0) +} + +///| +#doc(hidden) +pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { + @plasmoid.wasmExportOnEvent(p0, p1, p2, p3) +} + +///| +#doc(hidden) +pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit { + @plasmoid.wasmExportInfoPostReturn(p0) +} + +///| +#doc(hidden) +pub fn wasmExportInfo() -> Int { + @plasmoid.wasmExportInfo() +} + +///| +#doc(hidden) +pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { + @plasmoid.wasmExportPopupPostReturn(p0) +} + +///| +#doc(hidden) +pub fn wasmExportPopup() -> Int { + @plasmoid.wasmExportPopup() +} + diff --git a/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/README.md b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/README.md new file mode 100644 index 0000000..53ecf69 --- /dev/null +++ b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/README.md @@ -0,0 +1 @@ +The widget (plasmoid) contract. \ No newline at end of file diff --git a/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/ffi.mbt b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/ffi.mbt new file mode 100644 index 0000000..8c50e6a --- /dev/null +++ b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/ffi.mbt @@ -0,0 +1,409 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! + +///| +#doc(hidden) +pub fn wasmExportInfo() -> Int { + + let (result) : (PlasmoidInfo) = info(); + let return_area = mbt_ffi_malloc(32) + + let ptr = mbt_ffi_str2ptr((result).id) + mbt_ffi_store32((return_area) + 4, (result).id.length()) + mbt_ffi_store32((return_area) + 0, ptr) + + let ptr0 = mbt_ffi_str2ptr((result).name) + mbt_ffi_store32((return_area) + 12, (result).name.length()) + mbt_ffi_store32((return_area) + 8, ptr0) + mbt_ffi_store32((return_area) + 16, (((result).preferred).w).reinterpret_as_int()) + mbt_ffi_store32((return_area) + 20, (((result).preferred).h).reinterpret_as_int()) + mbt_ffi_store32((return_area) + 24, ((result).min_width).reinterpret_as_int()) + mbt_ffi_store8((return_area) + 28, (if (result).stretch { 1 } else { 0 })) + let ret = return_area + return ret + +} + +///| +#doc(hidden) +pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit { + mbt_ffi_free(mbt_ffi_load32((p0) + 0)) + mbt_ffi_free(mbt_ffi_load32((p0) + 8)) + +} + +///| +#doc(hidden) +pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int { + + let (result) : (Array[@types.DrawOp]) = render(@types.Size::{w : (p0).reinterpret_as_uint(), h : (p1).reinterpret_as_uint()}); + let return_area = mbt_ffi_malloc(8) + + let address = mbt_ffi_malloc((result).length() * 32); + for index = 0; index < (result).length(); index = index + 1 { + let iter_elem : @types.DrawOp = (result)[(index)] + let iter_base = address + (index * 32); + + match iter_elem { + FillRect(payload) => { + mbt_ffi_store8((iter_base) + 0, (0)) + mbt_ffi_store32((iter_base) + 4, ((payload).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload).rect).h).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 20, (((payload).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, ((payload).radius).reinterpret_as_int()) + + () + } + StrokeRect(payload0) => { + mbt_ffi_store8((iter_base) + 0, (1)) + mbt_ffi_store32((iter_base) + 4, ((payload0).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload0).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload0).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload0).rect).h).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 20, (((payload0).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload0).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload0).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload0).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, ((payload0).width).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 28, ((payload0).radius).reinterpret_as_int()) + + () + } + Line(payload1) => { + mbt_ffi_store8((iter_base) + 0, (2)) + mbt_ffi_store32((iter_base) + 4, ((payload1).start).x) + mbt_ffi_store32((iter_base) + 8, ((payload1).start).y) + mbt_ffi_store32((iter_base) + 12, ((payload1).end).x) + mbt_ffi_store32((iter_base) + 16, ((payload1).end).y) + mbt_ffi_store8((iter_base) + 20, (((payload1).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload1).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload1).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload1).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, ((payload1).width).reinterpret_as_int()) + + () + } + Circle(payload2) => { + mbt_ffi_store8((iter_base) + 0, (3)) + mbt_ffi_store32((iter_base) + 4, ((payload2).center).x) + mbt_ffi_store32((iter_base) + 8, ((payload2).center).y) + mbt_ffi_store32((iter_base) + 12, ((payload2).radius).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 16, (((payload2).color).r).to_int()) + mbt_ffi_store8((iter_base) + 17, (((payload2).color).g).to_int()) + mbt_ffi_store8((iter_base) + 18, (((payload2).color).b).to_int()) + mbt_ffi_store8((iter_base) + 19, (((payload2).color).a).to_int()) + mbt_ffi_store8((iter_base) + 20, (if (payload2).filled { 1 } else { 0 })) + + () + } + Text(payload3) => { + mbt_ffi_store8((iter_base) + 0, (4)) + mbt_ffi_store32((iter_base) + 4, ((payload3).origin).x) + mbt_ffi_store32((iter_base) + 8, ((payload3).origin).y) + + let ptr = mbt_ffi_str2ptr((payload3).text) + mbt_ffi_store32((iter_base) + 16, (payload3).text.length()) + mbt_ffi_store32((iter_base) + 12, ptr) + mbt_ffi_store32((iter_base) + 20, (((payload3).style).size).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 24, ((((payload3).style).color).r).to_int()) + mbt_ffi_store8((iter_base) + 25, ((((payload3).style).color).g).to_int()) + mbt_ffi_store8((iter_base) + 26, ((((payload3).style).color).b).to_int()) + mbt_ffi_store8((iter_base) + 27, ((((payload3).style).color).a).to_int()) + mbt_ffi_store8((iter_base) + 28, ((payload3).style).align.ordinal()) + mbt_ffi_store8((iter_base) + 29, (if ((payload3).style).bold { 1 } else { 0 })) + + () + } + PushClip(payload4) => { + mbt_ffi_store8((iter_base) + 0, (5)) + mbt_ffi_store32((iter_base) + 4, (payload4).x) + mbt_ffi_store32((iter_base) + 8, (payload4).y) + mbt_ffi_store32((iter_base) + 12, ((payload4).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, ((payload4).h).reinterpret_as_int()) + + () + } + PopClip => { + mbt_ffi_store8((iter_base) + 0, (6)) + + () + } + } + + } + mbt_ffi_store32((return_area) + 4, (result).length()) + mbt_ffi_store32((return_area) + 0, address) + let ret = return_area + return ret + +} + +///| +#doc(hidden) +pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit { + + for index = 0; index < (mbt_ffi_load32((p0) + 4)); index = index + 1 { + let iter_base = (mbt_ffi_load32((p0) + 0)) + (index * 32) + + match (mbt_ffi_load8_u((iter_base) + 0)) { + 0 => () + 1 => () + 2 => () + 3 => () + 4 => { + mbt_ffi_free(mbt_ffi_load32((iter_base) + 12)) + + } + 5 => () + 6 => () + _ => panic() + } + + } + mbt_ffi_free(mbt_ffi_load32((p0) + 0)) + +} + +///| +#doc(hidden) +pub fn wasmExportPopup() -> Int { + + let (result) : (PopupSurface?) = popup(); + let return_area = mbt_ffi_malloc(20) + + match (result) { + None => { + mbt_ffi_store8((return_area) + 0, (0)) + + () + } + Some(payload0) => { + mbt_ffi_store8((return_area) + 0, (1)) + mbt_ffi_store32((return_area) + 4, (((payload0).size).w).reinterpret_as_int()) + mbt_ffi_store32((return_area) + 8, (((payload0).size).h).reinterpret_as_int()) + + let address = mbt_ffi_malloc(((payload0).ops).length() * 32); + for index = 0; index < ((payload0).ops).length(); index = index + 1 { + let iter_elem : @types.DrawOp = ((payload0).ops)[(index)] + let iter_base = address + (index * 32); + + match iter_elem { + FillRect(payload1) => { + mbt_ffi_store8((iter_base) + 0, (0)) + mbt_ffi_store32((iter_base) + 4, ((payload1).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload1).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload1).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload1).rect).h).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 20, (((payload1).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload1).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload1).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload1).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, ((payload1).radius).reinterpret_as_int()) + + () + } + StrokeRect(payload2) => { + mbt_ffi_store8((iter_base) + 0, (1)) + mbt_ffi_store32((iter_base) + 4, ((payload2).rect).x) + mbt_ffi_store32((iter_base) + 8, ((payload2).rect).y) + mbt_ffi_store32((iter_base) + 12, (((payload2).rect).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, (((payload2).rect).h).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 20, (((payload2).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload2).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload2).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload2).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, ((payload2).width).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 28, ((payload2).radius).reinterpret_as_int()) + + () + } + Line(payload3) => { + mbt_ffi_store8((iter_base) + 0, (2)) + mbt_ffi_store32((iter_base) + 4, ((payload3).start).x) + mbt_ffi_store32((iter_base) + 8, ((payload3).start).y) + mbt_ffi_store32((iter_base) + 12, ((payload3).end).x) + mbt_ffi_store32((iter_base) + 16, ((payload3).end).y) + mbt_ffi_store8((iter_base) + 20, (((payload3).color).r).to_int()) + mbt_ffi_store8((iter_base) + 21, (((payload3).color).g).to_int()) + mbt_ffi_store8((iter_base) + 22, (((payload3).color).b).to_int()) + mbt_ffi_store8((iter_base) + 23, (((payload3).color).a).to_int()) + mbt_ffi_store32((iter_base) + 24, ((payload3).width).reinterpret_as_int()) + + () + } + Circle(payload4) => { + mbt_ffi_store8((iter_base) + 0, (3)) + mbt_ffi_store32((iter_base) + 4, ((payload4).center).x) + mbt_ffi_store32((iter_base) + 8, ((payload4).center).y) + mbt_ffi_store32((iter_base) + 12, ((payload4).radius).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 16, (((payload4).color).r).to_int()) + mbt_ffi_store8((iter_base) + 17, (((payload4).color).g).to_int()) + mbt_ffi_store8((iter_base) + 18, (((payload4).color).b).to_int()) + mbt_ffi_store8((iter_base) + 19, (((payload4).color).a).to_int()) + mbt_ffi_store8((iter_base) + 20, (if (payload4).filled { 1 } else { 0 })) + + () + } + Text(payload5) => { + mbt_ffi_store8((iter_base) + 0, (4)) + mbt_ffi_store32((iter_base) + 4, ((payload5).origin).x) + mbt_ffi_store32((iter_base) + 8, ((payload5).origin).y) + + let ptr = mbt_ffi_str2ptr((payload5).text) + mbt_ffi_store32((iter_base) + 16, (payload5).text.length()) + mbt_ffi_store32((iter_base) + 12, ptr) + mbt_ffi_store32((iter_base) + 20, (((payload5).style).size).reinterpret_as_int()) + mbt_ffi_store8((iter_base) + 24, ((((payload5).style).color).r).to_int()) + mbt_ffi_store8((iter_base) + 25, ((((payload5).style).color).g).to_int()) + mbt_ffi_store8((iter_base) + 26, ((((payload5).style).color).b).to_int()) + mbt_ffi_store8((iter_base) + 27, ((((payload5).style).color).a).to_int()) + mbt_ffi_store8((iter_base) + 28, ((payload5).style).align.ordinal()) + mbt_ffi_store8((iter_base) + 29, (if ((payload5).style).bold { 1 } else { 0 })) + + () + } + PushClip(payload6) => { + mbt_ffi_store8((iter_base) + 0, (5)) + mbt_ffi_store32((iter_base) + 4, (payload6).x) + mbt_ffi_store32((iter_base) + 8, (payload6).y) + mbt_ffi_store32((iter_base) + 12, ((payload6).w).reinterpret_as_int()) + mbt_ffi_store32((iter_base) + 16, ((payload6).h).reinterpret_as_int()) + + () + } + PopClip => { + mbt_ffi_store8((iter_base) + 0, (6)) + + () + } + } + + } + mbt_ffi_store32((return_area) + 16, ((payload0).ops).length()) + mbt_ffi_store32((return_area) + 12, address) + + () + } + } + let ret = return_area + return ret + +} + +///| +#doc(hidden) +pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit { + + match (mbt_ffi_load8_u((p0) + 0)) { + 0 => () + 1 => { + + for index = 0; index < (mbt_ffi_load32((p0) + 16)); index = index + 1 { + let iter_base = (mbt_ffi_load32((p0) + 12)) + (index * 32) + + match (mbt_ffi_load8_u((iter_base) + 0)) { + 0 => () + 1 => () + 2 => () + 3 => () + 4 => { + mbt_ffi_free(mbt_ffi_load32((iter_base) + 12)) + + } + 5 => () + 6 => () + _ => panic() + } + + } + mbt_ffi_free(mbt_ffi_load32((p0) + 12)) + + } + _ => panic() + } + +} + +///| +#doc(hidden) +pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { + + let lifted = match (p1) { + 0 => { + + @types.InputEvent::PointerMove(@types.Point::{x : p2, y : p3}) + } + 1 => { + + @types.InputEvent::PointerDown(@types.Point::{x : p2, y : p3}) + } + 2 => { + + @types.InputEvent::PointerUp(@types.Point::{x : p2, y : p3}) + } + 3 => { + + @types.InputEvent::PointerLeave + } + 4 => { + + @types.InputEvent::FocusLost + } + 5 => { + + let result = mbt_ffi_ptr2str(p2, p3) + + @types.InputEvent::KeyDown(result) + } + _ => panic() + } + + let (result0) : (Bool) = on_event(Surface::from(p0), lifted); + let ret = (if result0 { 1 } else { 0 }) + return ret + +} + +///| +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_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) + +///| +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) +#| 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/wallpaper/gen/interface/moonplasma/desktop/plasmoid/moon.pkg b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/moon.pkg new file mode 100644 index 0000000..42a8b82 --- /dev/null +++ b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/moon.pkg @@ -0,0 +1,8 @@ +import { + "moonplasma/desktop/interface/moonplasma/desktop/types" @types, + "moonplasma/desktop/interface/moonplasma/desktop/theme" @theme, + "moonplasma/desktop/interface/moonplasma/desktop/wallpaper" @wallpaper, + "moonplasma/desktop/lib/ui" @ui, +} + +warnings = "-44" diff --git a/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/stub.mbt b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/stub.mbt new file mode 100644 index 0000000..51eb6dd --- /dev/null +++ b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/stub.mbt @@ -0,0 +1,175 @@ +///| Implementation of `moonplasma:desktop/plasmoid` for the wallpaper chooser. The panel button +/// opens a popup listing the wallpapers the shell can draw (`wallpaper.wallpapers`), the current +/// one ticked. Clicking a row switches the desktop to it at once (the list stays open to compare); Enter +/// picks the highlighted row and closes, Escape or a click elsewhere closes. The shell remembers the choice across restarts. + +///| Popup geometry: a column of rows with a margin around them. +let popup_w : Int = 180 + +///| +let row_h : Int = 30 + +///| +let margin : Int = 6 + +///| Whether the popup is open, and which row is highlighted (-1: none). +let open_ : Ref[Bool] = { val: false } + +///| +let hover : Ref[Int] = { val: -1 } + +///| The names, read when the popup opens (the list is fixed while it is open). +let names : Ref[Array[String]] = { val: [] } + +///| +fn popup_h() -> Int { + margin * 2 + names.val.length() * row_h +} + +///| Row under a popup-local point, or -1. +fn row_at(x : Int, y : Int) -> Int { + if x < margin || x >= popup_w - margin || y < margin { + return -1 + } + let i = (y - margin) / row_h + if i < names.val.length() { i } else { -1 } +} + +///| +fn open() -> Unit { + names.val = @wallpaper.wallpapers() + open_.val = true + hover.val = @wallpaper.current_wallpaper().reinterpret_as_int() +} + +///| +fn close() -> Unit { + open_.val = false + hover.val = -1 +} + +///| +fn choose(i : Int) -> Unit { + if i >= 0 && i < names.val.length() { + @wallpaper.set_wallpaper(i.reinterpret_as_uint()) + } +} + +///| +pub fn info() -> PlasmoidInfo { + { + id: "org.moonplasma.wallpaper", + name: "Wallpaper", + preferred: { w: 96, h: 34 }, + min_width: 70, + stretch: false, + } +} + +///| +pub fn render(size : @types.Size) -> Array[@types.DrawOp] { + let t = @theme.get_tokens() + let w = size.w.reinterpret_as_int() + let h = size.h.reinterpret_as_int() + let r = t.radius.reinterpret_as_int() + let fs = t.font_size.reinterpret_as_int() + let bg = if open_.val { t.palette.accent } else { t.palette.surface_hover } + let fg = if open_.val { t.palette.accent_foreground } else { t.palette.foreground } + // A tiny landscape swatch, then the label. + let sw = 18 + let sx = 8 + let sy = (h - 14) / 2 + [ + @ui.fill_rect(@ui.rect(0, 0, w, h), bg, radius=r), + @ui.fill_rect(@ui.rect(sx, sy, sw, 14), t.palette.desktop_top, radius=2), + @ui.circle(sx + sw - 5, sy + 4, 2, t.palette.accent_foreground), + @ui.fill_rect(@ui.rect(sx, sy + 9, sw, 5), t.palette.desktop_bottom, radius=2), + @ui.stroke_rect(@ui.rect(sx, sy, sw, 14), fg, width=1, radius=2), + @ui.text(sx + sw + 6, h / 2, "Wallpaper", fs - 2, fg), + ] +} + +///| +pub fn popup() -> PopupSurface? { + if !open_.val { + return None + } + let t = @theme.get_tokens() + let w = popup_w + let h = popup_h() + let r = t.radius.reinterpret_as_int() + let fs = t.font_size.reinterpret_as_int() + let cur = @wallpaper.current_wallpaper().reinterpret_as_int() + let ops : Array[@types.DrawOp] = [ + @ui.fill_rect(@ui.rect(0, 0, w, h), t.palette.surface_popup, radius=r), + @ui.stroke_rect(@ui.rect(0, 0, w, h), t.palette.border, width=1, radius=r), + ] + for i = 0; i < names.val.length(); i = i + 1 { + let y = margin + i * row_h + let hot = i == hover.val + if hot { + ops.push(@ui.fill_rect(@ui.rect(margin, y, w - 2 * margin, row_h - 2), t.palette.accent, radius=r)) + } + let fg = if hot { t.palette.accent_foreground } else { t.palette.foreground } + let mid = y + (row_h - 2) / 2 + if i == cur { + // A tick: two strokes. + ops.push(@ui.line(margin + 8, mid, margin + 11, mid + 4, fg, width=2)) + ops.push(@ui.line(margin + 11, mid + 4, margin + 18, mid - 4, fg, width=2)) + } + ops.push(@ui.text(margin + 26, mid, names.val[i], fs - 1, fg, bold=i == cur)) + } + Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops }) +} + +///| +pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool { + match (target, event) { + (WIDGET, PointerDown(_)) => { + if open_.val { + close() + } else { + open() + } + true + } + (POPUP, PointerMove(p)) => { + let i = row_at(p.x, p.y) + if i != hover.val && i >= 0 { + hover.val = i + true + } else { + false + } + } + (POPUP, PointerDown(p)) => { + let i = row_at(p.x, p.y) + if i >= 0 { + choose(i) + hover.val = i + } + true + } + (_, FocusLost) => + if open_.val { + close() + true + } else { + false + } + (_, KeyDown(k)) => + if !open_.val { + false + } else if k == "Escape" { + close() + true + } else if k == "Enter" { + choose(hover.val) + close() + true + } else { + false + } + _ => false + } +} diff --git a/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/top.mbt b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/top.mbt new file mode 100644 index 0000000..24d7d0e --- /dev/null +++ b/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid/top.mbt @@ -0,0 +1,42 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! +///| +pub(all) struct PlasmoidInfo { + id : String; name : String; preferred : @types.Size; min_width : UInt; stretch : Bool +} +///| +pub(all) struct PopupSurface { + size : @types.Size; ops : Array[@types.DrawOp] +} +///| +pub(all) enum Surface { + WIDGET; POPUP +} derive() + +///| +pub fn Surface::ordinal(self : Surface) -> Int { + match self { + WIDGET => 0 + POPUP => 1 + } +} + +///| +pub fn Surface::from(self : Int) -> Surface { + match self { + 0 => WIDGET + 1 => POPUP + _ => panic() + } +} +///| +declare pub fn info() -> PlasmoidInfo +///| +/// Draw the widget into a `size` box (local coordinates). +declare pub fn render(size : @types.Size) -> Array[@types.DrawOp] +///| +/// Present when the widget wants a popup surface (e.g. a launcher menu). +declare pub fn popup() -> PopupSurface? +///| +/// Returns true when the widget's visuals changed and a redraw is needed. +declare pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool + diff --git a/components/wallpaper/gen/moon.pkg b/components/wallpaper/gen/moon.pkg new file mode 100644 index 0000000..8855def --- /dev/null +++ b/components/wallpaper/gen/moon.pkg @@ -0,0 +1,26 @@ +import { + "moonplasma/desktop/components/wallpaper/gen/interface/moonplasma/desktop/plasmoid" @plasmoid, +} + +warnings = "-44" + +pkgtype(kind: "foreign_library") + +options( + link: { + "wasm": { + "export-memory-name": "memory", + "heap-start-address": 16, + "exports": [ + "mbt_ffi_cabi_realloc:cabi_realloc", + "wasmExportInfo:moonplasma:desktop/plasmoid@0.1.0#info", + "wasmExportInfoPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#info", + "wasmExportOnEvent:moonplasma:desktop/plasmoid@0.1.0#on-event", + "wasmExportPopup:moonplasma:desktop/plasmoid@0.1.0#popup", + "wasmExportPopupPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#popup", + "wasmExportRender:moonplasma:desktop/plasmoid@0.1.0#render", + "wasmExportRenderPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#render" + ], + }, + }, +) diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index b911d02..50aab9f 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -1121,3 +1121,66 @@ test "super+d minimizes every window to show the desktop, and again brings them key(32U, false, 21U) assert_false(comp.wm.windows.get(2UL).unwrap().minimized) } + +///| Click (x, y) without drawing, then return the next scene's wallpaper op count (0: no new scene). +fn click_wallpaper_ops(comp : @host.MoonPlasmaCompositor, x : Int, y : Int, t : UInt) -> Int { + comp.handle_input(@nested.InputEvent::PointerMove(x.to_double(), y.to_double()), t) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 2U) + match comp.compose_scene() { + Some(sg) => sg.wallpaper_ops.length() + None => 0 + } +} + +///| +test "the wallpaper module lists the wallpapers and switches the desktop to the one clicked" { + let comp = new_comp("wallpaper") + let slot = comp.shell.slot_of("wallpaper") + assert_true(comp.shell.slot_rect(slot).w > 0) // it is in the default panel + assert_eq(comp.shell.wallpaper_sel, 0) + // A click opens the list; nothing changes yet. + click_module(comp, "wallpaper", 1U) + let pr = comp.shell.slot_popup_rect(slot) + assert_true(pr.h >= 6 * 2 + 5 * 30) // one row per wallpaper + assert_eq(comp.shell.wallpaper_sel, 0) + // Third row: Starfield. The list stays open and the backdrop is drawn anew. + let star = click_wallpaper_ops(comp, pr.x + 40, pr.y + 6 + 2 * 30 + 5, 5U) + assert_eq(comp.shell.wallpaper_sel, 2) + assert_true(star > 0) + assert_true(comp.shell.slot_popup_rect(slot).w > 0) + // Last row: Solid, a single fill. Escape then closes the list. + assert_eq(click_wallpaper_ops(comp, pr.x + 40, pr.y + 6 + 4 * 30 + 5, 10U), 1) + assert_eq(comp.shell.wallpaper_sel, 4) + comp.shell.key_down("Escape") + ignore(comp.compose_frame()) + assert_eq(comp.shell.slot_popup_rect(slot).w, 0) + // Out-of-range choices are ignored. + comp.shell.set_wallpaper(99) + assert_eq(comp.shell.wallpaper_sel, 4) +} + +///| +test "the chosen wallpaper is saved to state.edn and restored" { + if @sys.getenv("MOON_PLASMA_TEST_CONFIG") == "1" { + write_state_edn("{:note \"keep\"}\n") + let a = new_comp("wall-a") + a.shell.persist = true + a.restore_state() + assert_eq(a.shell.wallpaper_sel, 0) // nothing saved: the default + a.shell.set_wallpaper(1) + let text = read_state_edn() + assert_true(text.contains(":wallpaper :aurora")) + assert_true(text.contains(":note \"keep\"")) + let b = new_comp("wall-b") + b.shell.persist = true + b.restore_state() + assert_eq(b.shell.wallpaper_sel, 1) + // An unknown name falls back to the default. + write_state_edn("{:wallpaper :lava-lamp}\n") + let c = new_comp("wall-c") + c.shell.persist = true + c.restore_state() + assert_eq(c.shell.wallpaper_sel, 0) + } +} diff --git a/compositor/scene/pkg.generated.mbti b/compositor/scene/pkg.generated.mbti index 5627159..7a5cf4a 100644 --- a/compositor/scene/pkg.generated.mbti +++ b/compositor/scene/pkg.generated.mbti @@ -22,6 +22,7 @@ pub fn DamageTracker::mark_rect(Self, Rect) -> Unit pub fn DamageTracker::new() -> Self pub fn DamageTracker::take_damage(Self, Int32, Int32) -> Array[Rect] +#derive(Eq) pub(all) enum DrawOp { FillRect(Rect, Rgba, Int32) StrokeRect(Rect, Rgba, Int32, Int32) @@ -65,6 +66,7 @@ pub fn Rgba::with_alpha(Self, Int32) -> Self pub(all) struct SceneGraph { output_w : Int32 output_h : Int32 + mut wallpaper_ops : Array[DrawOp] mut background_ops : Array[DrawOp] mut windows : Array[WindowSceneNode] mut panel_ops : Array[DrawOp] @@ -79,6 +81,7 @@ pub(all) struct ShmBufferSnapshot { stride : Int32 format : Int32 bytes : Bytes + host_id : UInt32 } #derive(Eq, @debug.Debug) diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index 077b527..7a49062 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -198,6 +198,8 @@ pub(all) struct DesktopShell { /// Desktop card position (once moved) and drag state. /// How many of the background ops are the static wallpaper (the rest is the desktop card). mut wallpaper_len : Int + /// Which wallpaper is drawn (an index into `@libshell.wallpaper_names`); the wallpaper module sets it. + mut wallpaper_sel : Int mut desk_set : Bool mut desk_x : Int mut desk_y : Int @@ -276,6 +278,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell { tokens: conv_theme(@scene.ThemeTokens::moon_night()), screen_w: 0, wallpaper_len: 0, + wallpaper_sel: 0, desk_set: false, desk_x: 0, desk_y: 0, @@ -765,7 +768,7 @@ pub fn DesktopShell::render( @wasm.set_windows(self.win_list) } let bg : Array[@scene.DrawOp] = [] - translate(@libshell.render_background(@ui.size(w, h), tokens), 0, 0, bg) + translate(@libshell.render_wallpaper(@ui.size(w, h), tokens, self.wallpaper_sel), 0, 0, bg) self.wallpaper_len = bg.length() let (dx, dy) = self.desk_pos() render_desktop_widget(dx, dy, tokens, now(), bg) diff --git a/compositor/shell/state.mbt b/compositor/shell/state.mbt index 55080cb..2af7ab8 100644 --- a/compositor/shell/state.mbt +++ b/compositor/shell/state.mbt @@ -4,7 +4,8 @@ /// {:layout :tiling ; the Tile | Float switcher: :tiling, :monocle or :floating /// :recent ["foot" "dolphin"] ; the launcher's recently started apps, newest first /// :windows {"foot" {:x 80 :y 60 :w 640 :h 480}} ; floating windows' last place, by app id -/// :desktop-card {:x 900 :y 24}} ; where the desktop card was dragged to (absent: default spot) +/// :desktop-card {:x 900 :y 24} ; where the desktop card was dragged to (absent: default spot) +/// :wallpaper :aurora} ; the wallpaper module's choice (absent: :plasma-glow) /// /// Saving merges into what is already in the file, so keys this build doesn't know (a future /// widget's state, hand-written notes) survive. Everything is a no-op unless `persist` is on @@ -19,6 +20,8 @@ pub(all) struct SavedState { /// Top-left of the desktop card, when it was moved. desk : (Int, Int)? windows : Map[String, (Int, Int, Int, Int)] + /// Index into `@libshell.wallpaper_names`. + wallpaper : Int? } ///| @@ -30,7 +33,10 @@ fn state_path() -> String { let layout_names : Array[String] = ["tiling", "monocle", "floating"] ///| The keys this build owns; everything else in the file is kept as it is. -let owned_keys : Array[String] = ["layout", "recent", "desktop-card", "windows"] +let owned_keys : Array[String] = ["layout", "recent", "desktop-card", "windows", "wallpaper"] + +///| How state.edn names each wallpaper, in `@libshell.wallpaper_names` order. +let wallpaper_keys : Array[String] = ["plasma-glow", "aurora", "starfield", "dusk", "solid"] ///| fn read_state_file() -> Array[(@edn.Value, @edn.Value)] { @@ -62,7 +68,7 @@ fn lookup_key( ///| Read state.edn. Without `persist` nothing is read. pub fn DesktopShell::load_state(self : DesktopShell) -> SavedState { if !self.persist { - return { layout: None, recent: None, desk: None, windows: {} } + return { layout: None, recent: None, desk: None, windows: {}, wallpaper: None } } let entries = read_state_file() let layout = match lookup_key(entries, "layout") { @@ -135,7 +141,19 @@ pub fn DesktopShell::load_state(self : DesktopShell) -> SavedState { } _ => () } - { layout, recent, desk, windows } + let wallpaper = match lookup_key(entries, "wallpaper") { + Some(Keyword(k)) => { + let mut found : Int? = None + for i = 0; i < wallpaper_keys.length(); i = i + 1 { + if wallpaper_keys[i] == k { + found = Some(i) + } + } + found + } + _ => None + } + { layout, recent, desk, windows, wallpaper } } ///| Take the saved recents (the layout is the compositor's to apply). @@ -143,6 +161,10 @@ pub fn DesktopShell::apply_state(self : DesktopShell, st : SavedState) -> Unit { for app, g in st.windows { self.win_geoms.set(app, g) } + match st.wallpaper { + Some(i) => self.wallpaper_sel = i + None => () + } match st.desk { Some((x, y)) => { self.desk_set = true @@ -211,6 +233,9 @@ pub fn DesktopShell::save_state(self : DesktopShell) -> Unit { (@edn.Value::Keyword("y"), @edn.Value::Integer(y.to_string())), ]))) } + if self.wallpaper_sel > 0 && self.wallpaper_sel < wallpaper_keys.length() { + entries.insert(2, (@edn.Value::Keyword("wallpaper"), @edn.Value::Keyword(wallpaper_keys[self.wallpaper_sel]))) + } let body : String? = try Some(@edn.pretty(@edn.Value::Mapping(entries), layout={ width: 100, indent: 1 })) catch { _ => None } match body { Some(b) => { diff --git a/compositor/shell/widgets.mbt b/compositor/shell/widgets.mbt index ee3b23a..eaa5e9a 100644 --- a/compositor/shell/widgets.mbt +++ b/compositor/shell/widgets.mbt @@ -11,7 +11,7 @@ ///| Modules loaded into a fresh panel, in order. `tasks` stretches, so what follows it packs right. let default_panel : Array[String] = [ - "launcher", "inspector", "layout", "tasks", "moon-disc", "clock-digital12", "logout", + "launcher", "inspector", "layout", "tasks", "moon-disc", "clock-digital12", "wallpaper", "logout", ] ///| The module whose popup is the wasm picker. @@ -382,6 +382,15 @@ fn DesktopShell::publish_host_data(self : DesktopShell) -> Unit { } @wasm.publish(Layouts, lt) @wasm.publish_state(self.layout_sel, self.inspect_on) + if self.widgets.iter().any(w => w.has_cap("wallpaper")) { + let pt = @wasm.Table::new() + for n in @libshell.wallpaper_names { + pt.str(n) + pt.end_row() + } + @wasm.publish(Wallpapers, pt) + @wasm.publish_wallpaper(self.wallpaper_sel) + } let it = @wasm.Table::new() for w in self.widgets { it.str(w.slot) @@ -416,6 +425,16 @@ fn DesktopShell::publish_host_data(self : DesktopShell) -> Unit { } } +///| Show wallpaper `i` (ignored when out of range) and remember it in state.edn. +pub fn DesktopShell::set_wallpaper(self : DesktopShell, i : Int) -> Unit { + if i < 0 || i >= @libshell.wallpaper_names.length() || i == self.wallpaper_sel { + return + } + self.wallpaper_sel = i + @wasm.publish_wallpaper(i) + self.save_state() +} + ///| Carry out what modules asked of the host during their last calls. fn DesktopShell::process_requests(self : DesktopShell) -> Unit { while true { @@ -436,6 +455,7 @@ fn DesktopShell::process_requests(self : DesktopShell) -> Unit { Some(FocusWindow(n)) => self.actions.push(FocusWindow(n.to_int64().to_uint64())) Some(SetLayout(m)) => self.actions.push(SetLayout(m)) Some(Logout) => self.actions.push(Logout) + Some(SetWallpaper(i)) => self.set_wallpaper(i) Some(SetInspector(on)) => self.set_inspecting(on) Some(LoadModule(path)) => { match self.load_path(path) { diff --git a/compositor/wasm/wasm_host.mbt b/compositor/wasm/wasm_host.mbt index 85b100f..96af14c 100644 --- a/compositor/wasm/wasm_host.mbt +++ b/compositor/wasm/wasm_host.mbt @@ -231,6 +231,7 @@ pub(all) enum HostTable { Layouts Modules Instances + Wallpapers } ///| @@ -250,6 +251,7 @@ pub fn publish(which : HostTable, t : Table) -> Unit { Layouts => 2U Modules => 3U Instances => 4U + Wallpapers => 5U } @hostpl.publish_table(id, out) } @@ -259,6 +261,11 @@ pub fn publish_state(layout_mode : Int, inspector_on : Bool) -> Unit { @hostpl.publish_state(layout_mode, inspector_on) } +///| +pub fn publish_wallpaper(current : Int) -> Unit { + @hostpl.publish_wallpaper(current) +} + ///| Something a module asked the host to do during a call. pub(all) enum HostRequest { Launch(String) @@ -269,6 +276,7 @@ pub(all) enum HostRequest { UnloadInstance(String) Browse Logout + SetWallpaper(Int) } derive(Eq, Debug) ///| @@ -283,6 +291,7 @@ pub fn next_request() -> HostRequest? { Some(UnloadInstance(s)) => Some(UnloadInstance(s)) Some(Browse) => Some(Browse) Some(Logout) => Some(Logout) + Some(SetWallpaper(i)) => Some(SetWallpaper(i)) } } diff --git a/compositor/wasm/wasm_native.mbt b/compositor/wasm/wasm_native.mbt index cb010f0..c327562 100644 --- a/compositor/wasm/wasm_native.mbt +++ b/compositor/wasm/wasm_native.mbt @@ -607,6 +607,7 @@ pub(all) enum HostTable { Layouts Modules Instances + Wallpapers } ///| Publish `t` as the data behind `which` for every module's next call. @@ -617,6 +618,7 @@ pub fn publish(which : HostTable, t : Table) -> Unit { Layouts => 2 Modules => 3 Instances => 4 + Wallpapers => 5 } let out : Array[Byte] = [] for k = 0; k < 4; k = k + 1 { @@ -634,6 +636,11 @@ pub fn publish_state(layout_mode : Int, inspector_on : Bool) -> Unit { c_host_set_int(1, if inspector_on { 1 } else { 0 }) } +///| Publish which wallpaper (an index into the Wallpapers table) is on the desktop. +pub fn publish_wallpaper(current : Int) -> Unit { + c_host_set_int(2, current) +} + ///| Something a module asked the host to do during a call. pub(all) enum HostRequest { Launch(String) @@ -644,6 +651,7 @@ pub(all) enum HostRequest { UnloadInstance(String) Browse Logout + SetWallpaper(Int) } derive(Eq, Debug) ///| @@ -664,6 +672,7 @@ pub fn next_request() -> HostRequest? { 6 => Some(UnloadInstance(req_str())) 7 => Some(Browse) 8 => Some(Logout) + 9 => Some(SetWallpaper(c_host_req_int())) _ => None } } diff --git a/compositor/wasm/wasm_stub.c b/compositor/wasm/wasm_stub.c index d0d94ff..6c8a732 100644 --- a/compositor/wasm/wasm_stub.c +++ b/compositor/wasm/wasm_stub.c @@ -47,16 +47,16 @@ static Inst insts[MAX_INST]; 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_SYSTEM_READ, K_WIN_ALL, K_WIN_FOCUS_ID, K_LOGOUT, K_WALLPAPERS, K_WALLPAPER_CUR, K_SET_WALLPAPER }; 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_N }; +enum { T_APPS, T_WINDOWS, T_LAYOUTS, T_MODULES, T_INSTANCES, T_WALLPAPERS, 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 */ +static int32_t host_ints[4]; /* 0 layout mode, 1 inspector on, 2 current wallpaper */ /* ---- requests a guest made of the host (launch, focus, ...) ; drained by the shell after each call ---- */ typedef struct { int kind; int32_t n; char *s; } Req; @@ -264,17 +264,22 @@ static wasm_trap_t *host_import(void *env, const wasm_val_vec_t *args, wasm_val_ case K_APPS: build_list(in, T_APPS, "sssss", a0); break; /* id name comment category glyph */ case K_WINDOWS: build_list(in, T_WINDOWS, "isii", a0); break; case K_LAYOUTS: build_list(in, T_LAYOUTS, "s", a0); break; + case K_WALLPAPERS: build_list(in, T_WALLPAPERS, "s", a0); break; case K_MOD_AVAILABLE: build_list(in, T_MODULES, "sssi", a0); break; case K_MOD_LOADED: build_list(in, T_INSTANCES, "sss", a0); break; case K_LAYOUT_CUR: if (results->size >= 1) results->data[0] = (wasm_val_t)WASM_I32_VAL(host_ints[0]); break; + case K_WALLPAPER_CUR: + if (results->size >= 1) results->data[0] = (wasm_val_t)WASM_I32_VAL(host_ints[2]); + break; case K_INSPECT_IS_ON: if (results->size >= 1) results->data[0] = (wasm_val_t)WASM_I32_VAL(host_ints[1]); break; case K_SET_LAYOUT: push_req(3, a0, NULL); break; case K_FOCUS: push_req(2, a0, NULL); break; case K_LOGOUT: push_req(8, 0, NULL); break; + case K_SET_WALLPAPER: push_req(9, a0, NULL); break; case K_INSPECT_SET: push_req(4, a0, NULL); break; case K_MOD_BROWSE: push_req(7, 0, NULL); break; case K_MOD_LOAD: { char *s = guest_str(in, a0, a1); push_req(5, 0, s); free(s); break; } @@ -403,6 +408,9 @@ static int classify_import(const wasm_name_t *m, const wasm_name_t *f) { 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, "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; + if (!strcmp(iface, "wallpaper") && !strcmp(fn, "set-wallpaper")) return K_SET_WALLPAPER; if (!strcmp(iface, "layout-control") && !strcmp(fn, "layout-modes")) return K_LAYOUTS; if (!strcmp(iface, "layout-control") && !strcmp(fn, "current-layout")) return K_LAYOUT_CUR; if (!strcmp(iface, "layout-control") && !strcmp(fn, "set-layout")) return K_SET_LAYOUT; diff --git a/host-wasm/src/main.rs b/host-wasm/src/main.rs index bfb60dd..0e38867 100644 --- a/host-wasm/src/main.rs +++ b/host-wasm/src/main.rs @@ -179,6 +179,10 @@ impl moonplasma::host::plasmoids::Host for State { sh.inspector_on = inspector_on; } + fn publish_wallpaper(&mut self, current: i32) { + self.pl().shared.borrow_mut().wallpaper = current; + } + fn publish_system(&mut self, s: moonplasma::host::plasmoids::SystemSnapshot) { use plasmoid::ph::moonplasma::desktop::system::Snapshot; self.pl().shared.borrow_mut().system = Some(Snapshot { @@ -212,6 +216,7 @@ impl moonplasma::host::plasmoids::Host for State { Req::UnloadInstance(s) => R::UnloadInstance(s), Req::Browse => R::Browse, Req::Logout => R::Logout, + Req::SetWallpaper(n) => R::SetWallpaper(n), }) } diff --git a/host-wasm/src/plasmoid.rs b/host-wasm/src/plasmoid.rs index fa61657..e55bdd3 100644 --- a/host-wasm/src/plasmoid.rs +++ b/host-wasm/src/plasmoid.rs @@ -35,6 +35,7 @@ pub enum Req { UnloadInstance(String), Browse, Logout, + SetWallpaper(i32), } pub const T_APPS: usize = 0; @@ -42,12 +43,14 @@ pub const T_WINDOWS: usize = 1; pub const T_LAYOUTS: usize = 2; pub const T_MODULES: usize = 3; pub const T_INSTANCES: usize = 4; +pub const T_WALLPAPERS: usize = 5; /// Host data every module reads from, shared by all loaded modules. #[derive(Default)] pub struct Shared { - pub tables: [Vec; 5], + pub tables: [Vec; 6], pub layout_mode: i32, + pub wallpaper: i32, pub inspector_on: bool, pub system: Option, /// (id, title, app id, focused) @@ -273,6 +276,18 @@ impl d::session::Host for PState { } } +impl d::wallpaper::Host for PState { + fn wallpapers(&mut self) -> Vec { + self.rows(T_WALLPAPERS, "s").iter().map(|r| s(&r[0])).collect() + } + fn current_wallpaper(&mut self) -> u32 { + self.shared.borrow().wallpaper.max(0) as u32 + } + fn set_wallpaper(&mut self, index: u32) { + self.shared.borrow_mut().requests.push_back(Req::SetWallpaper(index as i32)); + } +} + impl d::system::Host for PState { fn read(&mut self) -> d::system::Snapshot { self.shared.borrow().system.clone().unwrap_or(d::system::Snapshot { @@ -562,4 +577,38 @@ mod tests { assert!(matches!(rt.shared.borrow_mut().requests.pop_front(), Some(Req::Logout))); rt.unload(h); } + + #[test] + fn wallpaper_module_lists_the_wallpapers_and_requests_a_switch() { + let mut rt = runtime(); + let l = rt.load(&module("wallpaper")).expect("wallpaper loads"); + assert!(l.imports.contains(&"wallpaper".to_string())); + let h = l.handle; + let names = ["Plasma glow", "Aurora", "Starfield"]; + let mut t = (names.len() as u32).to_le_bytes().to_vec(); + for n in names { + t.extend((n.len() as u32).to_le_bytes()); + t.extend(n.as_bytes()); + } + rt.shared.borrow_mut().tables[T_WALLPAPERS] = t; + rt.shared.borrow_mut().wallpaper = 1; + assert!(texts(&rt.render(h, Size { w: 96, h: 34 })).contains(&"Wallpaper".to_string())); + // A click opens the list of every wallpaper; nothing is requested yet. + assert!(rt.popup(h).is_none()); + assert!(rt.on_event(h, false, InputEvent::PointerDown(Point { x: 5, y: 5 }))); + let p = rt.popup(h).expect("popup open"); + for n in names { + assert!(texts(&p.ops).contains(&n.to_string()), "{n} listed"); + } + assert!(rt.shared.borrow().requests.is_empty()); + // Third row: Starfield. + assert!(rt.on_event(h, true, InputEvent::PointerDown(Point { x: 20, y: 6 + 2 * 30 + 5 }))); + assert!(matches!(rt.shared.borrow_mut().requests.pop_front(), Some(Req::SetWallpaper(2)))); + // The list stays open; Escape closes it without another request. + assert!(rt.popup(h).is_some()); + assert!(rt.on_event(h, true, InputEvent::KeyDown("Escape".to_string()))); + assert!(rt.popup(h).is_none()); + assert!(rt.shared.borrow().requests.is_empty()); + rt.unload(h); + } } diff --git a/interface/moonplasma/desktop/wallpaper/README.md b/interface/moonplasma/desktop/wallpaper/README.md new file mode 100644 index 0000000..b71c545 --- /dev/null +++ b/interface/moonplasma/desktop/wallpaper/README.md @@ -0,0 +1,2 @@ +The desktop wallpaper. The shell draws a fixed set of wallpapers (from draw ops, coloured by the +theme); a module with this capability lists them and picks which one is on the desktop. \ No newline at end of file diff --git a/interface/moonplasma/desktop/wallpaper/ffi.mbt b/interface/moonplasma/desktop/wallpaper/ffi.mbt new file mode 100644 index 0000000..a31f1f3 --- /dev/null +++ b/interface/moonplasma/desktop/wallpaper/ffi.mbt @@ -0,0 +1,33 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! + +///| +fn wasmImportWallpapers(p0 : Int) = "moonplasma:desktop/wallpaper@0.1.0" "wallpapers" + +///| +fn wasmImportCurrentWallpaper() -> Int = "moonplasma:desktop/wallpaper@0.1.0" "current-wallpaper" + +///| +fn wasmImportSetWallpaper(p0 : Int) = "moonplasma:desktop/wallpaper@0.1.0" "set-wallpaper" + +///| +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_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) + diff --git a/interface/moonplasma/desktop/wallpaper/moon.pkg b/interface/moonplasma/desktop/wallpaper/moon.pkg new file mode 100644 index 0000000..e2a6033 --- /dev/null +++ b/interface/moonplasma/desktop/wallpaper/moon.pkg @@ -0,0 +1 @@ +warnings = "-44" diff --git a/interface/moonplasma/desktop/wallpaper/top.mbt b/interface/moonplasma/desktop/wallpaper/top.mbt new file mode 100644 index 0000000..c85ad9a --- /dev/null +++ b/interface/moonplasma/desktop/wallpaper/top.mbt @@ -0,0 +1,38 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! +///| +/// Display names, indexed by choice. +pub fn wallpapers() -> Array[String] { + + let return_area = mbt_ffi_malloc(8) + wasmImportWallpapers(return_area); + + let array : Array[String] = []; + for index = 0; index < (mbt_ffi_load32((return_area) + 4)); index = index + 1 { + let iter_base = (mbt_ffi_load32((return_area) + 0)) + (index * 8) + + let result = mbt_ffi_ptr2str(mbt_ffi_load32((iter_base) + 0), mbt_ffi_load32((iter_base) + 4)) + + array.push(result) + } + mbt_ffi_free(mbt_ffi_load32((return_area) + 0)) + let ret = array + mbt_ffi_free(return_area) + return ret + +} +///| +pub fn current_wallpaper() -> UInt { + + let result : Int = wasmImportCurrentWallpaper(); + let ret = (result).reinterpret_as_uint() + return ret + +} +///| +/// Show wallpaper `index` (out-of-range indices are ignored). The choice is remembered. +pub fn set_wallpaper(index : UInt) -> Unit { + + wasmImportSetWallpaper((index).reinterpret_as_int()); + +} + diff --git a/interface/moonplasma/host/display/pkg.generated.mbti b/interface/moonplasma/host/display/pkg.generated.mbti index 0740df7..971527f 100644 --- a/interface/moonplasma/host/display/pkg.generated.mbti +++ b/interface/moonplasma/host/display/pkg.generated.mbti @@ -12,6 +12,8 @@ pub fn poll() -> Bool pub fn present(FixedArray[Byte], UInt32, UInt32) -> Unit +pub fn present_frame() -> Unit + pub fn set_cursor_shape(UInt32) -> Unit pub fn take_input() -> Array[SeatEvent] @@ -42,11 +44,18 @@ pub(all) struct PointerPos { y : Double } +pub(all) struct ScrollDelta { + axis : UInt32 + value : Double + discrete : Int32 +} + pub(all) enum SeatEvent { PointerMove(PointerPos) PointerButton(KeyState) Key(KeyState) Modifiers(Modifiers) + Scroll(ScrollDelta) } // Type aliases diff --git a/interface/moonplasma/host/plasmoids/ffi.mbt b/interface/moonplasma/host/plasmoids/ffi.mbt index be739a9..a9cdc13 100644 --- a/interface/moonplasma/host/plasmoids/ffi.mbt +++ b/interface/moonplasma/host/plasmoids/ffi.mbt @@ -24,6 +24,9 @@ fn wasmImportPublishTable(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/plas ///| fn wasmImportPublishState(p0 : Int, p1 : Int) = "moonplasma:host/plasmoids@0.1.0" "publish-state" +///| +fn wasmImportPublishWallpaper(p0 : Int) = "moonplasma:host/plasmoids@0.1.0" "publish-wallpaper" + ///| fn wasmImportPublishSystem(p0 : Int, p1 : Int64, p2 : Int64, p3 : Int, p4 : Int, p5 : Int64, p6 : Int64, p7 : Int64, p8 : Int) = "moonplasma:host/plasmoids@0.1.0" "publish-system" @@ -46,8 +49,29 @@ fn wasmImportPickerStart() -> Int = "moonplasma:host/plasmoids@0.1.0" "picker-st fn wasmImportPickerPoll(p0 : Int) = "moonplasma:host/plasmoids@0.1.0" "picker-poll" ///| -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) + +///| +#owned(str) +extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#|(func (param i32) (result i32) 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) + +///| +#owned(bytes) +extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int = +#|(func (param i32) (result i32) local.get 0) ///| extern "wasm" fn mbt_ffi_load64(offset : Int) -> Int64 = @@ -58,20 +82,12 @@ 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_store64(offset : Int, value : Int64) = -#|(func (param i32) (param i64) local.get 0 local.get 1 i64.store) - -///| -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_ptr2str(ptr : Int, len : Int) -> String = @@ -81,19 +97,6 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| 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) - -///| -#owned(bytes) -extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> 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_store64(offset : Int, value : Int64) = +#|(func (param i32) (param i64) local.get 0 local.get 1 i64.store) diff --git a/interface/moonplasma/host/plasmoids/native_stub.mbt b/interface/moonplasma/host/plasmoids/native_stub.mbt index 077a47d..7d79274 100644 --- a/interface/moonplasma/host/plasmoids/native_stub.mbt +++ b/interface/moonplasma/host/plasmoids/native_stub.mbt @@ -40,6 +40,11 @@ fn wasmImportPublishState(p0 : Int, p1 : Int) -> Unit { abort("wasm-only host import") } +///| +fn wasmImportPublishWallpaper(p0 : Int) -> Unit { + abort("wasm-only host import") +} + ///| fn wasmImportPublishSystem(p0 : Int, p1 : Int64, p2 : Int64, p3 : Int, p4 : Int, p5 : Int64, p6 : Int64, p7 : Int64, p8 : Int) -> Unit { abort("wasm-only host import") @@ -76,7 +81,27 @@ fn wasmImportPickerPoll(p0 : Int) -> Unit { } ///| -fn mbt_ffi_load8_u(offset : Int) -> Int { +fn mbt_ffi_malloc(size : Int) -> Int { + abort("wasm-only") +} + +///| +fn mbt_ffi_free(position : Int) -> Unit { + abort("wasm-only") +} + +///| +fn mbt_ffi_str2ptr(str : String) -> Int { + abort("wasm-only") +} + +///| +fn mbt_ffi_store8(offset : Int, value : Int) -> Unit { + abort("wasm-only") +} + +///| +fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int { abort("wasm-only") } @@ -91,17 +116,12 @@ fn mbt_ffi_store32(offset : Int, value : Int) -> Unit { } ///| -fn mbt_ffi_str2ptr(str : String) -> Int { +fn mbt_ffi_load32(offset : Int) -> Int { abort("wasm-only") } ///| -fn mbt_ffi_store64(offset : Int, value : Int64) -> Unit { - abort("wasm-only") -} - -///| -fn mbt_ffi_malloc(size : Int) -> Int { +fn mbt_ffi_load8_u(offset : Int) -> Int { abort("wasm-only") } @@ -111,21 +131,6 @@ fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String { } ///| -fn mbt_ffi_store8(offset : Int, value : Int) -> Unit { - abort("wasm-only") -} - -///| -fn mbt_ffi_free(position : Int) -> Unit { - abort("wasm-only") -} - -///| -fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int { - abort("wasm-only") -} - -///| -fn mbt_ffi_load32(offset : Int) -> Int { +fn mbt_ffi_store64(offset : Int, value : Int64) -> Unit { abort("wasm-only") } diff --git a/interface/moonplasma/host/plasmoids/pkg.generated.mbti b/interface/moonplasma/host/plasmoids/pkg.generated.mbti index 512399d..d54c53e 100644 --- a/interface/moonplasma/host/plasmoids/pkg.generated.mbti +++ b/interface/moonplasma/host/plasmoids/pkg.generated.mbti @@ -27,6 +27,8 @@ pub fn publish_system(SystemSnapshot) -> Unit pub fn publish_table(UInt32, FixedArray[Byte]) -> Unit +pub fn publish_wallpaper(Int32) -> Unit + pub fn publish_windows(Array[WindowEntry]) -> Unit pub fn render(UInt32, @types.Size) -> Array[@types.DrawOp] @@ -69,6 +71,8 @@ pub(all) enum Request { LoadModule(String) UnloadInstance(String) Browse + Logout + SetWallpaper(Int32) } pub(all) struct SystemSnapshot { diff --git a/interface/moonplasma/host/plasmoids/top.mbt b/interface/moonplasma/host/plasmoids/top.mbt index 7cd7b5a..f79e804 100644 --- a/interface/moonplasma/host/plasmoids/top.mbt +++ b/interface/moonplasma/host/plasmoids/top.mbt @@ -28,6 +28,7 @@ pub(all) enum Request { UnloadInstance(String) Browse Logout + SetWallpaper(Int) } ///| /// A native file-chooser dialog (zenity on the host session) for "Add widget". @@ -300,7 +301,7 @@ 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). +/// (sssi: id name path loaded), 4 instances (sss: slot module name), 5 wallpapers (s). pub fn publish_table(which : UInt, rows : FixedArray[Byte]) -> Unit { let ptr = mbt_ffi_bytes2ptr(rows) @@ -315,6 +316,13 @@ pub fn publish_state(layout_mode : Int, inspector_on : Bool) -> Unit { wasmImportPublishState(layout_mode, (if inspector_on { 1 } else { 0 })); +} +///| +/// The wallpaper on the desktop now (an index into the wallpapers table). +pub fn publish_wallpaper(current : Int) -> Unit { + + wasmImportPublishWallpaper(current); + } ///| pub fn publish_system(s : SystemSnapshot) -> Unit { @@ -400,6 +408,10 @@ pub fn next_request() -> Request? { Request::Logout } + 8 => { + + Request::SetWallpaper(mbt_ffi_load32((return_area) + 8)) + } _ => panic() } diff --git a/interface/moonplasma/host/sys/pkg.generated.mbti b/interface/moonplasma/host/sys/pkg.generated.mbti index c2dcebc..ae35bbe 100644 --- a/interface/moonplasma/host/sys/pkg.generated.mbti +++ b/interface/moonplasma/host/sys/pkg.generated.mbti @@ -12,12 +12,18 @@ pub fn has_exe(String) -> Bool pub fn list_desktop_files(String) -> Array[String] +pub fn log(String) -> Unit + pub fn memfd_create(String, Int32) -> Int32 pub fn mkdir_p(String) -> Bool pub fn mmap_read(Int32, Int32) -> FixedArray[Byte] +pub fn mmap_read_at(Int32, Int32, Int32) -> FixedArray[Byte] + +pub fn monotonic_ms() -> UInt64 + pub fn poll_read(Int32, Int32) -> Int32 pub fn pwrite(Int32, FixedArray[Byte], Int32) -> Int32 diff --git a/lib/shell/pkg.generated.mbti b/lib/shell/pkg.generated.mbti index 1d875b4..a32a973 100644 --- a/lib/shell/pkg.generated.mbti +++ b/lib/shell/pkg.generated.mbti @@ -11,6 +11,10 @@ pub fn render_background(@types.Size, @theme.Tokens) -> Array[@types.DrawOp] pub fn render_panel(@types.Rect, @theme.Tokens) -> Array[@types.DrawOp] +pub fn render_wallpaper(@types.Size, @theme.Tokens, Int32) -> Array[@types.DrawOp] + +pub let wallpaper_names : Array[String] + // Errors // Types and methods diff --git a/lib/shell/shell.mbt b/lib/shell/shell.mbt index d1c2867..197dac9 100644 --- a/lib/shell/shell.mbt +++ b/lib/shell/shell.mbt @@ -1,26 +1,49 @@ ///| Desktop background and panel chrome (pure render logic). -///| Vertical gradient built from horizontal bands, a soft "plasma" glow and a -/// wordmark. Everything derives from theme tokens. +///| The wallpapers the shell can draw, in choice order (what `wallpaper.wallpapers` lists). +pub let wallpaper_names : Array[String] = ["Plasma glow", "Aurora", "Starfield", "Dusk", "Solid"] + +///| The default desktop background: wallpaper 0, "Plasma glow". pub fn render_background( screen : @types.Size, t : @theme.Tokens, ) -> Array[@types.DrawOp] { - let w = screen.w.reinterpret_as_int() - let h = screen.h.reinterpret_as_int() - let p = t.palette - let bands = 48 - let ops = [] + render_wallpaper(screen, t, 0) +} + +///| Draw wallpaper `index` (see `wallpaper_names`) to fill `screen`; an unknown index draws wallpaper 0. +/// Everything is draw ops, coloured from the theme (Dusk keeps its own warm colours), so a +/// theme swap recolours the wallpaper too. +pub fn render_wallpaper( + screen : @types.Size, + t : @theme.Tokens, + index : Int, +) -> Array[@types.DrawOp] { + match index { + 1 => aurora(screen, t) + 2 => starfield(screen, t) + 3 => dusk(screen) + 4 => [@ui.fill_rect(@ui.rect(0, 0, screen.w.reinterpret_as_int(), screen.h.reinterpret_as_int()), t.palette.desktop_bottom)] + _ => plasma_glow(screen, t) + } +} + +///| `bands` horizontal strips blending `top` into `bottom`. +fn gradient(w : Int, h : Int, top : @types.Color, bottom : @types.Color, bands : Int, ops : Array[@types.DrawOp]) -> Unit { for i = 0; i < bands; i = i + 1 { let y0 = h * i / bands let y1 = h * (i + 1) / bands - ops.push( - @ui.fill_rect( - @ui.rect(0, y0, w, y1 - y0 + 1), - @ui.lerp_color(p.desktop_top, p.desktop_bottom, i, bands - 1), - ), - ) + ops.push(@ui.fill_rect(@ui.rect(0, y0, w, y1 - y0 + 1), @ui.lerp_color(top, bottom, i, bands - 1))) } +} + +///| +fn plasma_glow(screen : @types.Size, t : @theme.Tokens) -> Array[@types.DrawOp] { + let w = screen.w.reinterpret_as_int() + let h = screen.h.reinterpret_as_int() + let p = t.palette + let ops = [] + gradient(w, h, p.desktop_top, p.desktop_bottom, 48, ops) // Plasma glow: concentric translucent discs. let gx = w * 3 / 4 let gy = h * 3 / 10 @@ -47,6 +70,109 @@ pub fn render_background( ops } +///| sin(2*pi*num/den) * 1000, by Bhaskara I's approximation (good to about 0.2%). +fn sin_milli(num : Int, den : Int) -> Int { + let d = if den > 0 { den } else { 1 } + let mut t = num % d + if t < 0 { + t = t + d + } + // Angle in degrees, 0..359, then fold the second half onto the first with a sign flip. + let deg = t * 360 / d + let (x, sign) = if deg < 180 { (deg, 1) } else { (deg - 180, -1) } + sign * 4000 * x * (180 - x) / (40500 - x * (180 - x)) +} + +///| Curtains of light across the upper sky, like northern lights: each ribbon is a wave of thin +/// vertical strokes whose colour fades out above and below the crest. +fn aurora(screen : @types.Size, t : @theme.Tokens) -> Array[@types.DrawOp] { + let w = screen.w.reinterpret_as_int() + let h = screen.h.reinterpret_as_int() + let p = t.palette + let ops = [] + gradient(w, h, p.desktop_top, p.desktop_bottom, 48, ops) + let col = 4 + let layers = 5 + let ribbons = 3 + for k = 0; k < ribbons; k = k + 1 { + let base = h * (22 + k * 11) / 100 + let amp = h * (5 + k * 2) / 100 + let period = w * (3 + k) / 4 + 1 + let thick = h * (14 - k * 3) / 100 + let tint = @ui.lerp_color(p.accent, p.foreground, k, ribbons) + let mut x = 0 + while x < w { + let crest = base + sin_milli(x + k * period / 3, period) * amp / 1000 + // The curtain hangs from the crest; its height breathes along the ribbon. + let len = thick * (1000 + sin_milli(x * 2 + k * 97, period) / 2) / 1000 + for j = 0; j < layers; j = j + 1 { + let y0 = crest + len * j / layers + let y1 = crest + len * (j + 1) / layers + let alpha = (56 - k * 12) * (layers - j) / layers + ops.push(@ui.fill_rect(@ui.rect(x, y0, col, y1 - y0), @ui.with_alpha(tint, alpha))) + } + x = x + col + } + } + ops +} + +///| A small deterministic pseudo-random generator (the same stars every frame). +fn next_rand(seed : Int) -> Int { + (seed * 1103515245 + 12345) & 0x7fffffff +} + +///| A night sky: scattered stars and a crescent moon. +fn starfield(screen : @types.Size, t : @theme.Tokens) -> Array[@types.DrawOp] { + let w = screen.w.reinterpret_as_int() + let h = screen.h.reinterpret_as_int() + let p = t.palette + let ops = [] + let night = @ui.lerp_color(p.desktop_top, @ui.color(0, 0, 0, 255), 1, 2) + gradient(w, h, night, p.desktop_top, 32, ops) + let n = (w / 16) * (h / 16) / 12 + 20 + let mut seed = 20261125 + for i = 0; i < n; i = i + 1 { + seed = next_rand(seed) + let x = seed % (if w > 0 { w } else { 1 }) + seed = next_rand(seed) + let y = seed % (if h > 0 { h } else { 1 }) + seed = next_rand(seed) + let bright = 90 + seed % 166 + let r = if i % 17 == 0 { 2 } else { 1 } + ops.push(@ui.circle(x, y, r, @ui.with_alpha(p.foreground, bright))) + } + // Crescent: a bright disc with a sky-coloured disc over it. + let mx = w / 5 + let my = h / 4 + ops.push(@ui.circle(mx, my, 64, @ui.with_alpha(p.foreground, 18))) + ops.push(@ui.circle(mx, my, 40, @ui.with_alpha(p.foreground, 230))) + ops.push(@ui.circle(mx + 16, my - 10, 36, @ui.lerp_color(night, p.desktop_top, my, h))) + ops +} + +///| A warm sunset: violet sky over orange, a low sun and hills, independent of the theme. +fn dusk(screen : @types.Size) -> Array[@types.DrawOp] { + let w = screen.w.reinterpret_as_int() + let h = screen.h.reinterpret_as_int() + let ops = [] + let sky = @ui.color(46, 32, 84, 255) + let glow = @ui.color(240, 128, 84, 255) + gradient(w, h * 7 / 10, sky, glow, 40, ops) + // The sun, half set, with a halo. + let sx = w * 2 / 3 + let sy = h * 7 / 10 + for i = 0; i < 5; i = i + 1 { + ops.push(@ui.circle(sx, sy, 70 + (5 - i) * 24, @ui.color(255, 196, 120, 12 + i * 5))) + } + ops.push(@ui.circle(sx, sy, 64, @ui.color(255, 214, 140, 255))) + // Ground, then two hills on top of it. + ops.push(@ui.fill_rect(@ui.rect(0, h * 7 / 10, w, h - h * 7 / 10), @ui.color(44, 24, 52, 255))) + ops.push(@ui.circle(w / 4, h * 7 / 10 + h / 3, h / 2, @ui.color(60, 32, 68, 255))) + ops.push(@ui.circle(w * 9 / 10, h * 7 / 10 + h / 2, h * 3 / 5, @ui.color(36, 20, 44, 255))) + ops +} + ///| pub fn render_panel( panel : @types.Rect, @@ -102,3 +228,31 @@ test "background gradient covers the screen top to bottom" { _ => fail("expected fill") } } + +///| +test "every wallpaper fills the screen and an unknown index falls back to the first" { + let t = test_tokens() + let screen = @ui.size(640, 400) + for i = 0; i < wallpaper_names.length(); i = i + 1 { + let ops = render_wallpaper(screen, t, i) + assert_true(ops.length() > 0) + // The first op paints from the top-left corner across the full width. + match ops[0] { + FillRect(f) => { + assert_eq(f.rect.x, 0) + assert_eq(f.rect.y, 0) + assert_eq(f.rect.w.reinterpret_as_int(), 640) + } + _ => fail("wallpaper \{i} should start with a fill") + } + } + // (DrawOp has no Eq: compare op counts.) + let glow = render_wallpaper(screen, t, 0).length() + assert_eq(render_wallpaper(screen, t, 99).length(), glow) + assert_eq(render_wallpaper(screen, t, -1).length(), glow) + assert_eq(render_background(screen, t).length(), glow) + // Different choices draw different things. + assert_true(render_wallpaper(screen, t, 1).length() != glow) + assert_true(render_wallpaper(screen, t, 2).length() != glow) + assert_eq(render_wallpaper(screen, t, 4).length(), 1) +} diff --git a/scripts/components.txt b/scripts/components.txt index b86e90f..d566ccb 100644 --- a/scripts/components.txt +++ b/scripts/components.txt @@ -13,3 +13,4 @@ layout layout-widget inspector inspector-widget module-manager module-manager-widget logout logout-widget +wallpaper wallpaper-widget diff --git a/wit/capabilities.wit b/wit/capabilities.wit index 68891a3..b361a1b 100644 --- a/wit/capabilities.wit +++ b/wit/capabilities.wit @@ -117,3 +117,12 @@ interface session { logout: func(); } +/// The desktop wallpaper. The shell draws a fixed set of wallpapers (from draw ops, coloured by the +/// theme); a module with this capability lists them and picks which one is on the desktop. +interface wallpaper { + /// Display names, indexed by choice. + wallpapers: func() -> list; + current-wallpaper: func() -> u32; + /// Show wallpaper `index` (out-of-range indices are ignored). The choice is remembered. + set-wallpaper: func(index: u32); +} diff --git a/wit/host/plasmoids.wit b/wit/host/plasmoids.wit index 1aeb7c0..5b36b9f 100644 --- a/wit/host/plasmoids.wit +++ b/wit/host/plasmoids.wit @@ -40,10 +40,12 @@ 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). + /// (sssi: id name path loaded), 4 instances (sss: slot module name), 5 wallpapers (s). 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); + /// The wallpaper on the desktop now (an index into the wallpapers table). + publish-wallpaper: func(current: s32); record system-snapshot { cpu-permille: u32, @@ -77,6 +79,7 @@ interface plasmoids { unload-instance(string), browse, logout, + set-wallpaper(s32), } /// The next pending request, oldest first. next-request: func() -> option; diff --git a/wit/worlds.wit b/wit/worlds.wit index e1d9e3b..47d1a1d 100644 --- a/wit/worlds.wit +++ b/wit/worlds.wit @@ -74,6 +74,7 @@ world plasmoid-host { import modules; import system; import session; + import wallpaper; export plasmoid; } @@ -83,3 +84,8 @@ world logout-widget { import session; } +/// Wallpaper chooser. +world wallpaper-widget { + include widget; + import wallpaper; +} diff --git a/world/wallpaper-widget/README.md b/world/wallpaper-widget/README.md new file mode 100644 index 0000000..669e8ef --- /dev/null +++ b/world/wallpaper-widget/README.md @@ -0,0 +1 @@ +Wallpaper chooser. \ No newline at end of file diff --git a/world/wallpaper-widget/ffi_import.mbt b/world/wallpaper-widget/ffi_import.mbt new file mode 100644 index 0000000..cdedbce --- /dev/null +++ b/world/wallpaper-widget/ffi_import.mbt @@ -0,0 +1,2 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! + diff --git a/world/wallpaper-widget/import.mbt b/world/wallpaper-widget/import.mbt new file mode 100644 index 0000000..cdedbce --- /dev/null +++ b/world/wallpaper-widget/import.mbt @@ -0,0 +1,2 @@ +// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT! + diff --git a/world/wallpaper-widget/moon.pkg b/world/wallpaper-widget/moon.pkg new file mode 100644 index 0000000..e2a6033 --- /dev/null +++ b/world/wallpaper-widget/moon.pkg @@ -0,0 +1 @@ +warnings = "-44"