feat(gfx): integrate wasi-gfx for loading and presenting GUI
CI / test (push) Successful in 24s

This commit is contained in:
Antigravity committed 2026-11-25 02:54:03 +00:00
1 parent ccb809bdc3
commit 7caadb3f7f
59 files changed
+7495 -16

No files matched your search

+22 -2
View File
@@ -1,10 +1,11 @@
///| Standalone native MoonBit Wayland compositor entrypoint.
///| Standalone native MoonBit Wayland compositor entrypoint with wasi-gfx presentation.
///|
fn main {
let args = @env.args()
let mut socket_path = "/tmp/wayland-moon-0"
let mut theme_path = "config/themes/moon-night.json"
let mut gfx_out = ""
let width = 1280
let height = 720
@@ -17,6 +18,9 @@ fn main {
} else if a == "--theme" && i + 1 < args.length() {
theme_path = args[i + 1]
i = i + 2
} else if a == "--gfx-out" && i + 1 < args.length() {
gfx_out = args[i + 1]
i = i + 2
} else {
i = i + 1
}
@@ -33,11 +37,27 @@ fn main {
@scene.ThemeTokens::moon_night()
}
let gfx_surface = @gfx.Surface::new({
width: Some(width),
height: Some(height),
})
let gfx_ctx = @gfx.SurfaceFrameBufferContext::new(gfx_surface)
match @host.MoonPlasmaCompositor::new(socket_path, theme, width, height) {
Some(comp) => {
println("moon-plasma compositor listening on " + socket_path)
if gfx_out.length() > 0 {
let pixmap = comp.present_to_gfx(gfx_ctx)
let png = @renderer.encode_png(pixmap)
ignore(@sys.write_file_bytes(gfx_out, png))
comp.close()
println("wasi-gfx frame written to " + gfx_out)
return
}
println("moon-plasma compositor listening on " + socket_path + " (wasi-gfx surface active)")
while true {
comp.step()
comp.handle_gfx_events(gfx_surface)
ignore(comp.present_to_gfx(gfx_ctx))
@sys.sleep_ms(16)
}
}
+2
View File
@@ -3,6 +3,8 @@ import {
"moonbitlang/core/env",
"moonplasma/desktop/compositor/sys" @sys,
"moonplasma/desktop/compositor/scene" @scene,
"moonplasma/desktop/compositor/renderer" @renderer,
"moonplasma/desktop/compositor/gfx" @gfx,
"moonplasma/desktop/compositor/host" @host,
}
+286
View File
@@ -0,0 +1,286 @@
///| Pure-MoonBit implementation of wasi-gfx:surface@0.2.0,
/// wasi-gfx:surface/surface-frame-buffer@0.2.0, and
/// wasi-gfx:frame-buffer/frame-buffer@0.2.0 for loading & presenting GUIs.
///|
pub(all) struct CreateDesc {
width : Int?
height : Int?
}
///|
pub(all) struct ResizeEvent {
width : Int
height : Int
}
///|
pub(all) struct FrameEvent {
nothing : Bool
}
///|
pub(all) struct PointerEvent {
x : Double
y : Double
}
///|
pub(all) struct KeyEvent {
key : String?
text : String?
alt_key : Bool
ctrl_key : Bool
meta_key : Bool
shift_key : Bool
}
///|
/// Equivalent to `wasi-gfx:frame-buffer/frame-buffer.buffer`.
pub(all) struct FrameBuffer {
mut width : Int
mut height : Int
mut bytes : FixedArray[Byte]
}
///|
pub fn FrameBuffer::new(width : Int, height : Int) -> FrameBuffer {
let w = if width < 1 { 1 } else { width }
let h = if height < 1 { 1 } else { height }
{ width: w, height: h, bytes: FixedArray::make(w * h * 4, b'\x00') }
}
///|
pub fn FrameBuffer::get_with_copy(self : FrameBuffer) -> FixedArray[Byte] {
let out = FixedArray::make(self.bytes.length(), b'\x00')
for i = 0; i < self.bytes.length(); i = i + 1 {
out[i] = self.bytes[i]
}
out
}
///|
pub fn FrameBuffer::set_with_copy(
self : FrameBuffer,
val : FixedArray[Byte],
) -> Unit {
let n = val.length()
let next = FixedArray::make(n, b'\x00')
for i = 0; i < n; i = i + 1 {
next[i] = val[i]
}
self.bytes = next
}
///|
/// Equivalent to `wasi-gfx:surface/surface.surface`.
pub(all) struct Surface {
mut width : Int
mut height : Int
resize_events : Array[ResizeEvent]
frame_events : Array[FrameEvent]
pointer_down_events : Array[PointerEvent]
pointer_up_events : Array[PointerEvent]
pointer_move_events : Array[PointerEvent]
key_down_events : Array[KeyEvent]
key_up_events : Array[KeyEvent]
mut presented_frames : Int
mut last_presented : FixedArray[Byte]?
}
///|
pub fn Surface::new(desc : CreateDesc) -> Surface {
let w = match desc.width {
Some(v) => if v < 1 { 1 } else { v }
None => 1280
}
let h = match desc.height {
Some(v) => if v < 1 { 1 } else { v }
None => 720
}
{
width: w,
height: h,
resize_events: [],
frame_events: [],
pointer_down_events: [],
pointer_up_events: [],
pointer_move_events: [],
key_down_events: [],
key_up_events: [],
presented_frames: 0,
last_presented: None,
}
}
///|
pub fn Surface::width(self : Surface) -> Int {
self.width
}
///|
pub fn Surface::height(self : Surface) -> Int {
self.height
}
///|
pub fn Surface::request_set_size(
self : Surface,
height : Int?,
width : Int?,
) -> Unit {
let nw = match width {
Some(v) => if v < 1 { 1 } else { v }
None => self.width
}
let nh = match height {
Some(v) => if v < 1 { 1 } else { v }
None => self.height
}
if nw != self.width || nh != self.height {
self.width = nw
self.height = nh
self.resize_events.push({ width: nw, height: nh })
}
}
///|
pub fn Surface::push_frame(self : Surface) -> Unit {
self.frame_events.push({ nothing: false })
}
///|
pub fn Surface::push_pointer_down(
self : Surface,
x : Double,
y : Double,
) -> Unit {
self.pointer_down_events.push({ x, y })
}
///|
pub fn Surface::push_pointer_up(
self : Surface,
x : Double,
y : Double,
) -> Unit {
self.pointer_up_events.push({ x, y })
}
///|
pub fn Surface::push_pointer_move(
self : Surface,
x : Double,
y : Double,
) -> Unit {
self.pointer_move_events.push({ x, y })
}
///|
pub fn Surface::push_key_down(self : Surface, ev : KeyEvent) -> Unit {
self.key_down_events.push(ev)
}
///|
pub fn Surface::push_key_up(self : Surface, ev : KeyEvent) -> Unit {
self.key_up_events.push(ev)
}
///|
pub fn Surface::drain_resize(self : Surface) -> Array[ResizeEvent] {
let out = self.resize_events.copy()
self.resize_events.clear()
out
}
///|
pub fn Surface::drain_pointer_down(self : Surface) -> Array[PointerEvent] {
let out = self.pointer_down_events.copy()
self.pointer_down_events.clear()
out
}
///|
pub fn Surface::drain_pointer_up(self : Surface) -> Array[PointerEvent] {
let out = self.pointer_up_events.copy()
self.pointer_up_events.clear()
out
}
///|
pub fn Surface::drain_pointer_move(self : Surface) -> Array[PointerEvent] {
let out = self.pointer_move_events.copy()
self.pointer_move_events.clear()
out
}
///|
pub fn Surface::drain_key_down(self : Surface) -> Array[KeyEvent] {
let out = self.key_down_events.copy()
self.key_down_events.clear()
out
}
///|
/// Equivalent to `wasi-gfx:surface/surface-frame-buffer.context`.
pub(all) struct SurfaceFrameBufferContext {
surface : Surface
back_buffer : FrameBuffer
}
///|
pub fn SurfaceFrameBufferContext::new(
surface : Surface,
) -> SurfaceFrameBufferContext {
{
surface,
back_buffer: FrameBuffer::new(surface.width, surface.height),
}
}
///|
pub fn SurfaceFrameBufferContext::get_current_buffer(
self : SurfaceFrameBufferContext,
) -> FrameBuffer {
if self.back_buffer.width != self.surface.width ||
self.back_buffer.height != self.surface.height {
self.back_buffer.width = self.surface.width
self.back_buffer.height = self.surface.height
self.back_buffer.bytes = FixedArray::make(
self.surface.width * self.surface.height * 4,
b'\x00',
)
}
self.back_buffer
}
///|
pub fn SurfaceFrameBufferContext::present(
self : SurfaceFrameBufferContext,
) -> Unit {
let copy = self.back_buffer.get_with_copy()
self.surface.last_presented = Some(copy)
self.surface.presented_frames = self.surface.presented_frames + 1
}
///|
/// Present a rendered `Pixmap` through a `wasi-gfx` `SurfaceFrameBufferContext`.
pub fn present_pixmap(
ctx : SurfaceFrameBufferContext,
pixmap : @renderer.Pixmap,
) -> Unit {
let buf = ctx.get_current_buffer()
buf.set_with_copy(pixmap.data)
ctx.present()
}
///|
/// Render a `SceneGraph` and present it onto a `wasi-gfx` `SurfaceFrameBufferContext`.
pub fn present_scene(
ctx : SurfaceFrameBufferContext,
sg : @scene.SceneGraph,
) -> @renderer.Pixmap {
let pixmap = @renderer.render_scene(sg)
present_pixmap(ctx, pixmap)
pixmap
}
+56
View File
@@ -0,0 +1,56 @@
///| Unit tests for pure-MoonBit wasi-gfx surface & frame-buffer presentation.
test "wasi-gfx surface, frame-buffer, and present_scene" {
let surf = @gfx.Surface::new({ width: Some(160), height: Some(90) })
assert_eq(surf.width(), 160)
assert_eq(surf.height(), 90)
let ctx = @gfx.SurfaceFrameBufferContext::new(surf)
let theme = @scene.ThemeTokens::moon_night()
let sg = @scene.SceneGraph::new(160, 90)
let (bg, panel) = @scene.build_shell_ops(160, 90, theme, "Tiling", [
"Terminal",
])
sg.background_ops = bg
sg.panel_ops = panel
let pixmap = @gfx.present_scene(ctx, sg)
assert_eq(surf.presented_frames, 1)
match surf.last_presented {
Some(bytes) => {
assert_eq(bytes.length(), 160 * 90 * 4)
assert_eq(bytes[0], pixmap.data[0])
assert_eq(bytes[3], b'\xff')
}
None => abort("expected presented frame bytes")
}
}
test "wasi-gfx surface resize and pointer/key event queues" {
let surf = @gfx.Surface::new({ width: Some(320), height: Some(200) })
surf.request_set_size(Some(240), Some(400))
assert_eq(surf.width(), 400)
assert_eq(surf.height(), 240)
let resizes = surf.drain_resize()
assert_eq(resizes.length(), 1)
assert_eq(resizes[0].width, 400)
assert_eq(resizes[0].height, 240)
surf.push_pointer_down(12.5, 34.5)
let ptrs = surf.drain_pointer_down()
assert_eq(ptrs.length(), 1)
assert_eq(ptrs[0].x, 12.5)
assert_eq(ptrs[0].y, 34.5)
surf.push_key_down({
key: Some("Escape"),
text: Some("Escape"),
alt_key: false,
ctrl_key: false,
meta_key: false,
shift_key: false,
})
let keys = surf.drain_key_down()
assert_eq(keys.length(), 1)
assert_eq(keys[0].text, Some("Escape"))
}
+5
View File
@@ -0,0 +1,5 @@
import {
"moonbitlang/core/builtin",
"moonplasma/desktop/compositor/scene" @scene,
"moonplasma/desktop/compositor/renderer" @renderer,
}
+93
View File
@@ -0,0 +1,93 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonplasma/desktop/compositor/gfx"
import {
"moonplasma/desktop/compositor/renderer",
"moonplasma/desktop/compositor/scene",
}
// Values
pub fn present_pixmap(SurfaceFrameBufferContext, @renderer.Pixmap) -> Unit
pub fn present_scene(SurfaceFrameBufferContext, @scene.SceneGraph) -> @renderer.Pixmap
// Errors
// Types and methods
pub(all) struct CreateDesc {
width : Int32?
height : Int32?
}
pub(all) struct FrameBuffer {
mut width : Int32
mut height : Int32
mut bytes : FixedArray[Byte]
}
pub fn FrameBuffer::get_with_copy(Self) -> FixedArray[Byte]
pub fn FrameBuffer::new(Int32, Int32) -> Self
pub fn FrameBuffer::set_with_copy(Self, FixedArray[Byte]) -> Unit
pub(all) struct FrameEvent {
nothing : Bool
}
pub(all) struct KeyEvent {
key : String?
text : String?
alt_key : Bool
ctrl_key : Bool
meta_key : Bool
shift_key : Bool
}
pub(all) struct PointerEvent {
x : Double
y : Double
}
pub(all) struct ResizeEvent {
width : Int32
height : Int32
}
pub(all) struct Surface {
mut width : Int32
mut height : Int32
resize_events : Array[ResizeEvent]
frame_events : Array[FrameEvent]
pointer_down_events : Array[PointerEvent]
pointer_up_events : Array[PointerEvent]
pointer_move_events : Array[PointerEvent]
key_down_events : Array[KeyEvent]
key_up_events : Array[KeyEvent]
mut presented_frames : Int32
mut last_presented : FixedArray[Byte]?
}
pub fn Surface::drain_key_down(Self) -> Array[KeyEvent]
pub fn Surface::drain_pointer_down(Self) -> Array[PointerEvent]
pub fn Surface::drain_pointer_move(Self) -> Array[PointerEvent]
pub fn Surface::drain_pointer_up(Self) -> Array[PointerEvent]
pub fn Surface::drain_resize(Self) -> Array[ResizeEvent]
pub fn Surface::height(Self) -> Int32
pub fn Surface::new(CreateDesc) -> Self
pub fn Surface::push_frame(Self) -> Unit
pub fn Surface::push_key_down(Self, KeyEvent) -> Unit
pub fn Surface::push_key_up(Self, KeyEvent) -> Unit
pub fn Surface::push_pointer_down(Self, Double, Double) -> Unit
pub fn Surface::push_pointer_move(Self, Double, Double) -> Unit
pub fn Surface::push_pointer_up(Self, Double, Double) -> Unit
pub fn Surface::request_set_size(Self, Int32?, Int32?) -> Unit
pub fn Surface::width(Self) -> Int32
pub(all) struct SurfaceFrameBufferContext {
surface : Surface
back_buffer : FrameBuffer
}
pub fn SurfaceFrameBufferContext::get_current_buffer(Self) -> FrameBuffer
pub fn SurfaceFrameBufferContext::new(Surface) -> Self
pub fn SurfaceFrameBufferContext::present(Self) -> Unit
// Type aliases
// Traits
+47
View File
@@ -274,3 +274,50 @@ pub fn MoonPlasmaCompositor::compose_frame(
ignore(self.damage.take_damage(self.output_w, self.output_h))
@renderer.render_scene(scene)
}
///|
/// Compose the current desktop GUI frame and present it onto a wasi-gfx
/// `SurfaceFrameBufferContext`.
pub fn MoonPlasmaCompositor::present_to_gfx(
self : MoonPlasmaCompositor,
ctx : @gfx.SurfaceFrameBufferContext,
) -> @renderer.Pixmap {
let pixmap = self.compose_frame()
@gfx.present_pixmap(ctx, pixmap)
pixmap
}
///|
/// Process pending wasi-gfx surface input events (pointer clicks for focus).
pub fn MoonPlasmaCompositor::handle_gfx_events(
self : MoonPlasmaCompositor,
surface : @gfx.Surface,
) -> Unit {
let downs = surface.drain_pointer_down()
for ev in downs {
let px = ev.x.to_int()
let py = ev.y.to_int()
let order = self.wm.stacking_order()
// Top-most window hit-test (iterate reverse Z-order).
let mut i = order.length() - 1
while i >= 0 {
let wid = order[i]
match self.windows.get(wid) {
Some(win) =>
if px >= win.geometry.x &&
px < win.geometry.x + win.geometry.w &&
py >= win.geometry.y - 32 &&
py < win.geometry.y + win.geometry.h {
self.apply_actions([
@wm.WmAction::Focus(wid),
@wm.WmAction::Raise(wid),
])
break
}
None => ()
}
i = i - 1
}
}
}
+1
View File
@@ -6,6 +6,7 @@ import {
"moonplasma/desktop/compositor/shell" @shell,
"moonplasma/desktop/compositor/renderer" @renderer,
"moonplasma/desktop/compositor/wayland" @wayland,
"moonplasma/desktop/compositor/gfx" @gfx,
}
supported_targets = "-all+native"