The wasm compositor as a login session: DRM/KMS + libinput through the host
CI / test (push) Failing after 1m13s

New moonplasma:host/display import (open, poll, present, cursor, input events, close). The
wasmtime host implements it by compiling the compositor's own kms shim (libseat + DRM/KMS +
libinput + xkbcommon) via build.rs (feature drm, default on), so the native and wasm backends
share one implementation. compositor/kms gets a wasm backend with the same API, and the shared
app loop gains --drm.

- session: install.py installs the host, compositor.wasm, fonts, config, wit and modules and the
  display-manager entry (--native keeps the old native session); the launcher starts the host
  when installed. just run-drm / install-session use the wasm compositor; *-native keep the old.
- compositor.wasm moves to build/ (out of build/components, where the module catalog scans).

Verified: component builds with the new import, host instantiates it and renders frames, installed
layout (fonts/modules/theme from the share dir) renders correctly in a sandbox, native build and
tests unchanged. NOT exercised on a real login VT.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 02:38:19 -08:00
1 parent 95b7275b3a
commit c03d4c6c17
32 files changed
+862 -124

No files matched your search

+1
View File
@@ -570,6 +570,7 @@ version = "0.1.0"
dependencies = [
"ab_glyph",
"ab_glyph_rasterizer",
"cc",
"libc",
"wasmtime",
]
+9
View File
@@ -4,6 +4,15 @@ version = "0.1.0"
edition = "2021"
description = "Runs the moon-plasma compositor wasm component (build/components/compositor.wasm) and gives it the OS surface it imports (wit/host/sys.wit)."
[features]
# Login-session display backend (DRM/KMS + libinput + libseat + xkbcommon). Needs those libraries'
# development files; build with --no-default-features to leave it out (--drm then reports it).
default = ["drm"]
drm = ["dep:cc"]
[build-dependencies]
cc = { version = "1", optional = true }
[dependencies]
ab_glyph = "0.2"
ab_glyph_rasterizer = "0.1"
+12 -6
View File
@@ -3,7 +3,7 @@
Runs the compositor **as a WebAssembly component**.
```
just compositor-wasm # build/components/compositor.wasm (the compositor, a component)
just compositor-wasm # build/compositor.wasm (the compositor, a component)
just host-wasm # host-wasm/target/release/moon-plasma-host (this crate)
just run-wasm # both, then run in a window on your current Wayland session
```
@@ -17,9 +17,9 @@ file descriptor lives in this host, which implements `sys` with libc (sockets an
`memfd`, `poll`, spawn, files, clock).
```
moon-plasma-host build/components/compositor.wasm --socket PATH [--theme PATH] [--window]
moon-plasma-host build/components/compositor.wasm --socket PATH --gfx-out frame.png # one frame
moon-plasma-host build/components/compositor.wasm --socket PATH --gfx-out frame.png --serve # keep refreshing it
moon-plasma-host build/compositor.wasm --socket PATH [--theme PATH] [--window]
moon-plasma-host build/compositor.wasm --socket PATH --gfx-out frame.png # one frame
moon-plasma-host build/compositor.wasm --socket PATH --gfx-out frame.png --serve # keep refreshing it
```
## Status
@@ -36,8 +36,14 @@ instantiates each module as a real component against one world, `plasmoid-host`
([`wit/worlds.wit`](../wit/worlds.wit)); a module imports the subset of host interfaces it needs and
each is served here. `cargo test --release` loads the built modules (`just modules` first).
Not yet in the component (the native compositor still has these):
- **DRM/KMS and libinput** (`--drm`), the login-session backend.
**Login session** (`--drm`): the host takes the display and input through the seat
(`moonplasma:host/display`, [`wit/host/display.wit`](../wit/host/display.wit)): DRM/KMS output with page
flips and a hardware cursor, libinput keyboard and pointer, an xkb keymap, libseat for device access and
VT switching. It is the compositor's own C shim (`compositor/kms/kms_stub.c`) compiled into this host
by `build.rs` (feature `drm`, on by default; `--no-default-features` leaves it out), so the native and
wasm backends can't drift. Run `just run-drm` from a login VT, or `just install-session` to add Moon
Plasma to your display manager (`session/install.py`; `--native` installs the native compositor instead).
Not yet exercised on a real VT; it is only built and instantiated.
Regenerating the host bindings (into a temp dir, then copy `interface/moonplasma/host`,
`world/compositor` and `components/compositor/gen/{ffi.mbt,world}`; regenerating in place would
+18
View File
@@ -0,0 +1,18 @@
// The login-session display backend (feature `drm`, on by default) compiles the compositor's own
// C shim for DRM/KMS + libinput + libseat + xkbcommon and links the libraries it uses.
fn main() {
#[cfg(feature = "drm")]
{
cc::Build::new()
.file("csrc/kms.c")
.include("csrc/fake")
.include("/usr/include/libdrm")
.warnings(false)
.compile("kms");
for lib in ["drm", "input", "seat", "xkbcommon", "udev"] {
println!("cargo:rustc-link-lib={lib}");
}
println!("cargo:rerun-if-changed=csrc/kms.c");
println!("cargo:rerun-if-changed=../compositor/kms/kms_stub.c");
}
}
+9
View File
@@ -0,0 +1,9 @@
/* Just enough of moonbit.h for compositor/kms/kms_stub.c to build outside the MoonBit runtime.
* The shim uses MoonBit only for the export macro and the length of byte arrays it is handed;
* csrc/kms.c supplies the length through kms_bytes_len. */
#pragma once
#include <stdint.h>
#define MOONBIT_FFI_EXPORT
typedef uint8_t *moonbit_bytes_t;
extern int32_t kms_bytes_len;
#define Moonbit_array_length(p) (kms_bytes_len)
+19
View File
@@ -0,0 +1,19 @@
/* The login-session display backend for the wasm host: the same C file the native compositor uses
* (libseat + DRM/KMS dumb buffers + libinput + xkbcommon), compiled here against a stand-in
* moonbit.h, so the two backends can't drift. The mp_kms_* functions are called from Rust. */
#include <stdint.h>
int32_t kms_bytes_len;
#include "../../compositor/kms/kms_stub.c"
/* The two shim functions that take a MoonBit byte array need its length. */
int32_t hw_kms_present(uint8_t *pixels, int32_t w, int32_t h) {
kms_bytes_len = w * h * 4;
return mp_kms_present(pixels, w, h);
}
int32_t hw_kms_keymap_copy(uint8_t *out, int32_t cap) {
kms_bytes_len = cap;
return mp_kms_keymap_copy(out);
}
+92
View File
@@ -0,0 +1,92 @@
//! The login-session display backend: calls into compositor/kms/kms_stub.c (built by build.rs),
//! the same shim the native compositor uses. One display, one seat, global state.
use crate::moonplasma::host::display::{KeyState, Modifiers, Output, PointerPos, SeatEvent};
extern "C" {
fn mp_kms_open() -> i32;
fn mp_kms_width() -> i32;
fn mp_kms_height() -> i32;
fn mp_kms_quit_requested() -> i32;
fn mp_kms_poll() -> i32;
fn mp_kms_frame_ready() -> i32;
fn mp_kms_set_cursor_shape(shape: i32);
fn mp_kms_next_event() -> i32;
fn mp_kms_arg(i: i32) -> i32;
fn mp_kms_keymap_len() -> i32;
fn mp_kms_close();
fn hw_kms_present(pixels: *mut u8, w: i32, h: i32) -> i32;
fn hw_kms_keymap_copy(out: *mut u8, cap: i32) -> i32;
}
/// Open the seat, the first connected output and libinput. None (the shim has said why on stderr)
/// when this is not an active login VT.
pub fn open() -> Option<Output> {
unsafe {
if mp_kms_open() != 0 {
mp_kms_close();
return None;
}
let n = mp_kms_keymap_len();
let mut keymap = vec![0u8; n.max(0) as usize];
let got = if n > 0 { hw_kms_keymap_copy(keymap.as_mut_ptr(), n) } else { 0 };
keymap.truncate(got.max(0) as usize);
Some(Output { width: mp_kms_width() as u32, height: mp_kms_height() as u32, keymap })
}
}
/// Service the seat, DRM and input fds. False once Ctrl+Alt+Backspace was pressed.
pub fn poll() -> bool {
unsafe {
mp_kms_poll();
mp_kms_quit_requested() == 0
}
}
pub fn frame_ready() -> bool {
unsafe { mp_kms_frame_ready() != 0 }
}
/// Present an RGBA8888 frame; ignored if the buffer is smaller than the stated size.
pub fn present(pixels: &[u8], width: u32, height: u32) {
if pixels.len() < (width as usize) * (height as usize) * 4 {
return;
}
// The shim only reads the pixels; it takes a non-const pointer for the MoonBit byte type.
unsafe { hw_kms_present(pixels.as_ptr() as *mut u8, width as i32, height as i32) };
}
pub fn set_cursor_shape(shape: u32) {
unsafe { mp_kms_set_cursor_shape(shape as i32) };
}
/// Input queued since the last call.
pub fn take_input() -> Vec<SeatEvent> {
let mut out = Vec::new();
unsafe {
loop {
let t = mp_kms_next_event();
if t == 0 {
break;
}
let (a, b) = (mp_kms_arg(1), mp_kms_arg(2));
match t {
1 => out.push(SeatEvent::PointerMove(PointerPos { x: a as f64, y: b as f64 })),
2 => out.push(SeatEvent::PointerButton(KeyState { code: a as u32, pressed: b != 0 })),
3 => out.push(SeatEvent::Key(KeyState { code: a as u32, pressed: b != 0 })),
4 => out.push(SeatEvent::Modifiers(Modifiers {
depressed: a as u32,
latched: b as u32,
locked: mp_kms_arg(3) as u32,
group: mp_kms_arg(4) as u32,
})),
_ => {}
}
}
}
out
}
pub fn close() {
unsafe { mp_kms_close() };
}
+54 -3
View File
@@ -1,12 +1,12 @@
//! Native host for the moon-plasma compositor component.
//!
//! The compositor is a wasm component (build/components/compositor.wasm) that imports exactly one
//! The compositor is a wasm component (build/compositor.wasm) that imports exactly one
//! interface, `moonplasma:host/sys` (wit/host/sys.wit): sockets, memfd, poll, spawn, files, time.
//! This program is the other side of that interface. The component only ever holds integer
//! handles; every real file descriptor lives here.
//!
//! moon-plasma-host [--component PATH] [-- compositor args...]
//! moon-plasma-host build/components/compositor.wasm --socket /run/user/1000/wayland-moon-0 --window
//! moon-plasma-host build/compositor.wasm --socket /run/user/1000/wayland-moon-0 --window
use std::ffi::CString;
use std::io::Read;
@@ -16,6 +16,8 @@ use wasmtime::component::{Component, HasSelf, Linker};
use wasmtime::{Config, Engine, Result, Store};
mod plasmoid;
#[cfg(feature = "drm")]
mod kms;
wasmtime::component::bindgen!({
path: "../wit/host",
@@ -227,6 +229,54 @@ impl moonplasma::host::plasmoids::Host for State {
}
}
impl moonplasma::host::display::Host for State {
fn open(&mut self) -> Option<moonplasma::host::display::Output> {
#[cfg(feature = "drm")]
{
kms::open()
}
#[cfg(not(feature = "drm"))]
{
eprintln!("this host was built without the `drm` feature (no login-session backend)");
None
}
}
fn poll(&mut self) -> bool {
#[cfg(feature = "drm")]
return kms::poll();
#[cfg(not(feature = "drm"))]
false
}
fn frame_ready(&mut self) -> bool {
#[cfg(feature = "drm")]
return kms::frame_ready();
#[cfg(not(feature = "drm"))]
false
}
fn present(&mut self, pixels: Vec<u8>, width: u32, height: u32) {
#[cfg(feature = "drm")]
kms::present(&pixels, width, height);
#[cfg(not(feature = "drm"))]
let _ = (pixels, width, height);
}
fn set_cursor_shape(&mut self, shape: u32) {
#[cfg(feature = "drm")]
kms::set_cursor_shape(shape);
#[cfg(not(feature = "drm"))]
let _ = shape;
}
fn take_input(&mut self) -> Vec<moonplasma::host::display::SeatEvent> {
#[cfg(feature = "drm")]
return kms::take_input();
#[cfg(not(feature = "drm"))]
Vec::new()
}
fn close(&mut self) {
#[cfg(feature = "drm")]
kms::close();
}
}
impl moonplasma::host::font::Host for State {
fn load(&mut self, slot: u32, path: String) -> bool {
if slot as usize >= self.fonts.len() {
@@ -592,7 +642,7 @@ fn main() -> Result<()> {
let component_path = if args.first().map(|a| a.ends_with(".wasm")).unwrap_or(false) {
args.remove(0)
} else {
"build/components/compositor.wasm".to_string()
"build/compositor.wasm".to_string()
};
if args.first().map(|a| a == "--").unwrap_or(false) {
args.remove(0);
@@ -614,6 +664,7 @@ fn main() -> Result<()> {
let mut linker: Linker<State> = Linker::new(&engine);
moonplasma::host::sys::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
moonplasma::host::font::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
moonplasma::host::display::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
moonplasma::host::plasmoids::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
let mut store = Store::new(&engine, State::default());
store.data_mut().pl = Some(plasmoid::Runtime::new(&engine)?);