The wasm compositor as a login session: DRM/KMS + libinput through the host
CI / test (push) Failing after 1m13s
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:
1 parent
95b7275b3a
commit
c03d4c6c17
32 files changed
+862
-124
No files matched your search
@@ -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
@@ -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)?);
|
||||
|
||||
Reference in new issue
Block a user