Files
chandra/compositor/kms/kms_native.mbt
T
nandiandClaude Sonnet 5.5 c03d4c6c17
CI / test (push) Failing after 1m13s
The wasm compositor as a login session: DRM/KMS + libinput through the host
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>
2026-11-25 02:38:19 -08:00

121 lines
3.3 KiB
MoonBit

///| Native display backend for running moon-plasma as a login session: DRM/KMS
/// output (dumb buffers, page flips, hardware cursor), libinput keyboard and
/// pointer, xkbcommon keymap, libseat for device access and VT switching.
/// All state lives in the C shim (kms_stub.c); this wrapper exposes it with the
/// same shape as `@nested.NestedWindow` so the main loop can drive either.
///|
extern "C" fn c_open() -> Int = "mp_kms_open"
///|
extern "C" fn c_width() -> Int = "mp_kms_width"
///|
extern "C" fn c_height() -> Int = "mp_kms_height"
///|
extern "C" fn c_quit_requested() -> Int = "mp_kms_quit_requested"
///|
extern "C" fn c_poll() -> Int = "mp_kms_poll"
///|
extern "C" fn c_frame_ready() -> Int = "mp_kms_frame_ready"
///|
extern "C" fn c_present(pixels : FixedArray[Byte], w : Int, h : Int) -> Int = "mp_kms_present"
///|
extern "C" fn c_set_cursor_shape(shape : Int) = "mp_kms_set_cursor_shape"
///|
extern "C" fn c_next_event() -> Int = "mp_kms_next_event"
///|
extern "C" fn c_arg(i : Int) -> Int = "mp_kms_arg"
///|
extern "C" fn c_keymap_len() -> Int = "mp_kms_keymap_len"
///|
extern "C" fn c_keymap_copy(out : FixedArray[Byte]) -> Int = "mp_kms_keymap_copy"
///|
extern "C" fn c_close() = "mp_kms_close"
///|
pub(all) struct Kms {
width : Int
height : Int
keymap : Bytes
}
///| Open the seat, the first connected DRM output and libinput. Returns None
/// (after printing why to stderr) when not running on an active login VT.
pub fn Kms::open() -> Kms? {
if c_open() != 0 {
c_close()
return None
}
let n = c_keymap_len()
let buf : FixedArray[Byte] = FixedArray::make(n, b'\x00')
let got = if n > 0 { c_keymap_copy(buf) } else { 0 }
let keymap = Bytes::from_fixedarray(buf, len=got)
Some({ width: c_width(), height: c_height(), keymap })
}
///| Service the seat, DRM and input fds. False once Ctrl+Alt+Backspace was pressed.
pub fn Kms::poll(_self : Kms) -> Bool {
ignore(c_poll())
c_quit_requested() == 0
}
///| True when a new frame can be presented (seat active, no page flip in flight).
pub fn Kms::frame_ready(_self : Kms) -> Bool {
c_frame_ready() != 0
}
///| Present a composed frame (its size must match the output mode).
pub fn Kms::present(_self : Kms, pixmap : @renderer.Pixmap) -> Unit {
ignore(c_present(pixmap.data, pixmap.width, pixmap.height))
}
///| Show the cursor for a wp_cursor_shape_v1 value.
pub fn Kms::set_cursor_shape(_self : Kms, shape : UInt) -> Unit {
c_set_cursor_shape(shape.reinterpret_as_int())
}
///| Input queued since the last call, in the compositor's event type.
pub fn Kms::take_input(_self : Kms) -> Array[@nested.InputEvent] {
let out : Array[@nested.InputEvent] = []
while true {
let t = c_next_event()
if t == 0 {
break
}
let a = c_arg(1)
let b = c_arg(2)
match t {
1 => out.push(PointerMove(a.to_double(), b.to_double()))
2 => out.push(PointerButton(a.reinterpret_as_uint(), b != 0))
3 => out.push(Key(a.reinterpret_as_uint(), b != 0))
4 =>
out.push(
Modifiers(
a.reinterpret_as_uint(),
b.reinterpret_as_uint(),
c_arg(3).reinterpret_as_uint(),
c_arg(4).reinterpret_as_uint(),
),
)
_ => ()
}
}
out
}
///| Restore the previous video mode and release the seat.
pub fn Kms::close(_self : Kms) -> Unit {
c_close()
}