From 9431254de029aa5a9ad18d5a1c8c5b100d2b0439 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 19:48:38 +0000 Subject: [PATCH] wasmhost: login-session display (DRM/KMS) through compositor/kms Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB --- scripts/build-wasmhost.sh | 6 ++ wasmhost/bin/display.mbt | 204 +++++++++++++++++++++++++++++++++++++ wasmhost/bin/moon.pkg | 8 +- wasmhost/bin/raster_stub.c | 7 ++ wasmhost/bin/stubs.mbt | 23 ----- 5 files changed, 223 insertions(+), 25 deletions(-) create mode 100644 wasmhost/bin/display.mbt delete mode 100644 wasmhost/bin/stubs.mbt diff --git a/scripts/build-wasmhost.sh b/scripts/build-wasmhost.sh index 92c374c..6d4a826 100755 --- a/scripts/build-wasmhost.sh +++ b/scripts/build-wasmhost.sh @@ -12,6 +12,12 @@ CAPI="$ROOT/.tools/wasmtime-c-api" command -v moon >/dev/null || { echo "error: moon not found" >&2; exit 1; } export C_INCLUDE_PATH="$CAPI/include${C_INCLUDE_PATH:+:$C_INCLUDE_PATH}" export LIBRARY_PATH="$CAPI/lib${LIBRARY_PATH:+:$LIBRARY_PATH}" +# libseat (and its headers) from the pixi env when there is one (see pixi.toml). +PIXI="$ROOT/.pixi/envs/default" +if [ -d "$PIXI/lib" ]; then + export C_INCLUDE_PATH="$C_INCLUDE_PATH:$PIXI/include" + export LIBRARY_PATH="$LIBRARY_PATH:$PIXI/lib" +fi moon build --target native --release --warn-list -a wasmhost/bin mkdir -p build cp _build/native/release/build/wasmhost/bin/bin.exe build/wasmhost diff --git a/wasmhost/bin/display.mbt b/wasmhost/bin/display.mbt new file mode 100644 index 0000000..5d6209d --- /dev/null +++ b/wasmhost/bin/display.mbt @@ -0,0 +1,204 @@ +///| `chandra:host/display`: the login session (DRM/KMS output, libinput, xkbcommon, libseat), +/// through compositor/kms's shim (kms_stub.c), the one the native compositor uses. + +///| +extern "C" fn mp_kms_open() -> Int = "mp_kms_open" + +///| +extern "C" fn mp_kms_width() -> Int = "mp_kms_width" + +///| +extern "C" fn mp_kms_height() -> Int = "mp_kms_height" + +///| +extern "C" fn mp_kms_quit_requested() -> Int = "mp_kms_quit_requested" + +///| +extern "C" fn mp_kms_poll() -> Int = "mp_kms_poll" + +///| +extern "C" fn mp_kms_frame_ready() -> Int = "mp_kms_frame_ready" + +///| +#borrow(pixels) +extern "C" fn mp_kms_present(pixels : Bytes, w : Int, h : Int) -> Int = "mp_kms_present" + +///| +extern "C" fn mp_kms_set_cursor_shape(shape : Int) = "mp_kms_set_cursor_shape" + +///| +extern "C" fn mp_kms_set_power(on : Int) = "mp_kms_set_power" + +///| +extern "C" fn mp_kms_next_event() -> Int = "mp_kms_next_event" + +///| +extern "C" fn mp_kms_arg(i : Int) -> Int = "mp_kms_arg" + +///| +extern "C" fn mp_kms_keymap_len() -> Int = "mp_kms_keymap_len" + +///| +#borrow(out) +extern "C" fn mp_kms_keymap_copy(out : Bytes) -> Int = "mp_kms_keymap_copy" + +///| +extern "C" fn mp_kms_close() = "mp_kms_close" + +///| +extern "C" fn mp_kms_fd(i : Int) -> Int = "mp_kms_fd" + +///| Show the rasteriser's framebuffer (raster_stub.c). +extern "C" fn rs_present_kms() = "rs_present_kms" + +///| The seat, DRM and input fds while the display is open (none otherwise), for an idle wait. +fn display_wait_fds() -> Array[Int] { + let out : Array[Int] = [] + for i in 0..<3 { + let fd = mp_kms_fd(i) + if fd >= 0 { + out.push(fd) + } + } + out +} + +///| Input queued since the last call, as seat events. +fn take_input() -> Array[@wt.Val] { + let out : Array[@wt.Val] = [] + while true { + let t = mp_kms_next_event() + if t == 0 { + break + } + let a = mp_kms_arg(1) + let b = mp_kms_arg(2) + let key_state = fn() -> @wt.Val { + Record([("code", U32(a.reinterpret_as_uint())), ("pressed", Bool(b != 0))]) + } + match t { + 1 => + out.push( + Variant( + "pointer-move", + Some(Record([("x", F64(a.to_double())), ("y", F64(b.to_double()))])), + ), + ) + 2 => out.push(Variant("pointer-button", Some(key_state()))) + 3 => out.push(Variant("key", Some(key_state()))) + 4 => + out.push( + Variant( + "modifiers", + Some( + Record([ + ("depressed", U32(a.reinterpret_as_uint())), + ("latched", U32(b.reinterpret_as_uint())), + ("locked", U32(mp_kms_arg(3).reinterpret_as_uint())), + ("group", U32(mp_kms_arg(4).reinterpret_as_uint())), + ]), + ), + ), + ) + 5 => + out.push( + Variant( + "scroll", + Some( + Record([ + ("axis", U32(a.reinterpret_as_uint())), + ("value", F64(b.to_double() / 256.0)), + ("discrete", S32(mp_kms_arg(3))), + ]), + ), + ), + ) + _ => () + } + } + out +} + +///| +fn display_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] { + [ + ( + "open", + _ => { + if mp_kms_open() != 0 { + // Not an active login VT; the shim has said why on stderr. + mp_kms_close() + return [Option(None)] + } + let n = mp_kms_keymap_len() + let keymap = if n > 0 { + let buf = Bytes::make(n, b'\x00') + let got = mp_kms_keymap_copy(buf) + buf[0:@cmp.maximum(got, 0)].to_owned() + } else { + b"" + } + [ + Option( + Some( + Record([ + ("width", U32(mp_kms_width().reinterpret_as_uint())), + ("height", U32(mp_kms_height().reinterpret_as_uint())), + ("keymap", @wt.Val::bytes(keymap)), + ]), + ), + ), + ] + }, + ), + ( + "poll", + _ => { + mp_kms_poll() |> ignore + boolean(mp_kms_quit_requested() == 0) + }, + ), + ("frame-ready", _ => boolean(mp_kms_frame_ready() != 0)), + ( + "present", + a => { + let px = a[0].to_bytes() + let w = a[1].to_int() + let h = a[2].to_int() + if px.length() >= w * h * 4 { + mp_kms_present(px, w, h) |> ignore + } + none + }, + ), + ( + "present-frame", + _ => { + rs_present_kms() + none + }, + ), + ( + "set-cursor-shape", + a => { + mp_kms_set_cursor_shape(a[0].to_int()) + none + }, + ), + ( + "set-power", + a => { + mp_kms_set_power(if a[0].to_bool() { 1 } else { 0 }) + none + }, + ), + ("take-input", _ => [List(take_input())]), + ( + "close", + _ => { + mp_kms_close() + none + }, + ), + ] +} diff --git a/wasmhost/bin/moon.pkg b/wasmhost/bin/moon.pkg index ca80412..df0780e 100644 --- a/wasmhost/bin/moon.pkg +++ b/wasmhost/bin/moon.pkg @@ -7,6 +7,7 @@ import { "chandra/desktop/compositor/font" @font, "chandra/desktop/compositor/renderer" @renderer, "chandra/desktop/compositor/wasm" @wasm, + "chandra/desktop/compositor/kms" @kms, "chandra/desktop/interface/chandra/desktop/types" @types, "chandra/desktop/interface/chandra/desktop/theme" @theme, "chandra/desktop/wasmhost/wt" @wt, @@ -17,12 +18,15 @@ supported_targets = "-all+native" pkgtype(kind: "executable") // libwasmtime comes prebuilt (scripts/setup-toolchain.sh); scripts/build-wasmhost.sh puts it on -// the compiler's and linker's search paths. Linked statically: the binary needs nothing at runtime. +// the compiler's and linker's search paths, and links it statically. The DRM/KMS backend's system +// libraries are linked as usual; libseat may come from the pixi env (pixi.toml), so build/wasmhost +// looks for it there too (the rpath is relative to build/), not through LD_LIBRARY_PATH, which +// apps started by the compositor would inherit. options( "native-stub": [ "host_stub.c", "raster_stub.c" ], link: { "native": { - "cc-link-flags": "-l:libwasmtime.a -lpthread -ldl -lm", + "cc-link-flags": "-l:libwasmtime.a -lpthread -ldl -lm -ldrm -linput -lseat -lxkbcommon -ludev -Wl,-rpath,$ORIGIN/../.pixi/envs/default/lib", }, }, ) diff --git a/wasmhost/bin/raster_stub.c b/wasmhost/bin/raster_stub.c index e4df32d..ee6adec 100644 --- a/wasmhost/bin/raster_stub.c +++ b/wasmhost/bin/raster_stub.c @@ -598,3 +598,10 @@ int32_t rs_export_memfd(void) { free(data); return fd; } + +/* compositor/kms/kms_stub.c: show an RGBA8888 frame on the login session's output. */ +int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h); + +void rs_present_kms(void) { + if (fb) mp_kms_present((moonbit_bytes_t)fb, fb_w, fb_h); +} diff --git a/wasmhost/bin/stubs.mbt b/wasmhost/bin/stubs.mbt deleted file mode 100644 index 8e0c221..0000000 --- a/wasmhost/bin/stubs.mbt +++ /dev/null @@ -1,23 +0,0 @@ -///| Interfaces not served yet (filled in stage by stage). - -///| -fn display_wait_fds() -> Array[Int] { - [] -} - - - -///| -fn display_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] { - [ - ("open", _ => [Option(None)]), - ("poll", _ => [Bool(false)]), - ("frame-ready", _ => [Bool(false)]), - ("present", _ => none), - ("present-frame", _ => none), - ("set-cursor-shape", _ => none), - ("take-input", _ => [List([])]), - ("close", _ => none), - ] -} -