314 lines
12 KiB
Rust
314 lines
12 KiB
Rust
use std::fs::File;
|
|
use std::os::fd::AsFd;
|
|
use std::os::unix::net::UnixStream;
|
|
use std::path::PathBuf;
|
|
use std::sync::{Arc, Mutex};
|
|
use std::time::{Duration, Instant};
|
|
|
|
use moon_plasma_host::compositor::MoonPlasmaCompositor;
|
|
use moon_plasma_host::renderer::SoftwareRenderer;
|
|
use moon_plasma_host::theme::ThemeTokens;
|
|
use moon_plasma_host::wasm::wm_wit::exports::moonplasma::wm::policy::LayoutMode;
|
|
|
|
use wayland_client::protocol::{
|
|
wl_buffer::WlBuffer,
|
|
wl_compositor::WlCompositor,
|
|
wl_registry::{self, WlRegistry},
|
|
wl_shm::{self, WlShm},
|
|
wl_shm_pool::WlShmPool,
|
|
wl_surface::WlSurface,
|
|
};
|
|
use wayland_client::{Connection, Dispatch, QueueHandle, WEnum};
|
|
use wayland_protocols::xdg::decoration::zv1::client::{
|
|
zxdg_decoration_manager_v1::ZxdgDecorationManagerV1,
|
|
zxdg_toplevel_decoration_v1::{self, Mode as DecoMode, ZxdgToplevelDecorationV1},
|
|
};
|
|
use wayland_protocols::xdg::shell::client::{
|
|
xdg_surface::{self, XdgSurface},
|
|
xdg_toplevel::{self, XdgToplevel},
|
|
xdg_wm_base::{self, XdgWmBase},
|
|
};
|
|
|
|
struct ClientData {
|
|
compositor: Option<WlCompositor>,
|
|
shm: Option<WlShm>,
|
|
xdg_wm_base: Option<XdgWmBase>,
|
|
deco_mgr: Option<ZxdgDecorationManagerV1>,
|
|
configured_size: Arc<Mutex<Option<(i32, i32)>>>,
|
|
deco_mode: Arc<Mutex<Option<DecoMode>>>,
|
|
acked_serial: Option<u32>,
|
|
}
|
|
|
|
impl Dispatch<WlRegistry, ()> for ClientData {
|
|
fn event(
|
|
state: &mut Self,
|
|
registry: &WlRegistry,
|
|
event: wl_registry::Event,
|
|
_: &(),
|
|
_: &Connection,
|
|
qh: &QueueHandle<Self>,
|
|
) {
|
|
if let wl_registry::Event::Global { name, interface, version } = event {
|
|
match interface.as_str() {
|
|
"wl_compositor" => {
|
|
state.compositor = Some(registry.bind::<WlCompositor, _, _>(name, version.min(5), qh, ()));
|
|
}
|
|
"wl_shm" => {
|
|
state.shm = Some(registry.bind::<WlShm, _, _>(name, 1, qh, ()));
|
|
}
|
|
"xdg_wm_base" => {
|
|
state.xdg_wm_base = Some(registry.bind::<XdgWmBase, _, _>(name, version.min(5), qh, ()));
|
|
}
|
|
"zxdg_decoration_manager_v1" => {
|
|
state.deco_mgr = Some(registry.bind::<ZxdgDecorationManagerV1, _, _>(name, 1, qh, ()));
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Dispatch<WlCompositor, ()> for ClientData {
|
|
fn event(_: &mut Self, _: &WlCompositor, _: wayland_client::protocol::wl_compositor::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {}
|
|
}
|
|
impl Dispatch<WlSurface, ()> for ClientData {
|
|
fn event(_: &mut Self, _: &WlSurface, _: wayland_client::protocol::wl_surface::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {}
|
|
}
|
|
impl Dispatch<WlShm, ()> for ClientData {
|
|
fn event(_: &mut Self, _: &WlShm, _: wl_shm::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {}
|
|
}
|
|
impl Dispatch<WlShmPool, ()> for ClientData {
|
|
fn event(_: &mut Self, _: &WlShmPool, _: wayland_client::protocol::wl_shm_pool::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {}
|
|
}
|
|
impl Dispatch<WlBuffer, ()> for ClientData {
|
|
fn event(_: &mut Self, _: &WlBuffer, _: wayland_client::protocol::wl_buffer::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {}
|
|
}
|
|
impl Dispatch<XdgWmBase, ()> for ClientData {
|
|
fn event(_: &mut Self, wm: &XdgWmBase, event: xdg_wm_base::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {
|
|
if let xdg_wm_base::Event::Ping { serial } = event {
|
|
wm.pong(serial);
|
|
}
|
|
}
|
|
}
|
|
impl Dispatch<XdgSurface, ()> for ClientData {
|
|
fn event(state: &mut Self, surf: &XdgSurface, event: xdg_surface::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {
|
|
if let xdg_surface::Event::Configure { serial } = event {
|
|
surf.ack_configure(serial);
|
|
state.acked_serial = Some(serial);
|
|
}
|
|
}
|
|
}
|
|
impl Dispatch<XdgToplevel, ()> for ClientData {
|
|
fn event(state: &mut Self, _: &XdgToplevel, event: xdg_toplevel::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {
|
|
if let xdg_toplevel::Event::Configure { width, height, .. } = event {
|
|
if width > 0 && height > 0 {
|
|
*state.configured_size.lock().unwrap() = Some((width, height));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
impl Dispatch<ZxdgDecorationManagerV1, ()> for ClientData {
|
|
fn event(_: &mut Self, _: &ZxdgDecorationManagerV1, _: wayland_protocols::xdg::decoration::zv1::client::zxdg_decoration_manager_v1::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {}
|
|
}
|
|
impl Dispatch<ZxdgToplevelDecorationV1, ()> for ClientData {
|
|
fn event(state: &mut Self, _: &ZxdgToplevelDecorationV1, event: zxdg_toplevel_decoration_v1::Event, _: &(), _: &Connection, _: &QueueHandle<Self>) {
|
|
if let zxdg_toplevel_decoration_v1::Event::Configure { mode } = event {
|
|
if let WEnum::Value(m) = mode {
|
|
*state.deco_mode.lock().unwrap() = Some(m);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn spawn_wayland_test_client(
|
|
socket_path: PathBuf,
|
|
title: &'static str,
|
|
app_id: &'static str,
|
|
shm_dir: PathBuf,
|
|
idx: usize,
|
|
) -> (
|
|
std::thread::JoinHandle<()>,
|
|
Arc<Mutex<Option<(i32, i32)>>>,
|
|
Arc<Mutex<Option<DecoMode>>>,
|
|
) {
|
|
let configured_size = Arc::new(Mutex::new(None));
|
|
let deco_mode = Arc::new(Mutex::new(None));
|
|
let cfg_clone = configured_size.clone();
|
|
let deco_clone = deco_mode.clone();
|
|
|
|
let handle = std::thread::spawn(move || {
|
|
let stream = UnixStream::connect(&socket_path).expect("connect to wayland socket");
|
|
let conn = Connection::from_socket(stream).expect("wayland Connection::from_socket");
|
|
let mut queue = conn.new_event_queue();
|
|
let qh = queue.handle();
|
|
let display = conn.display();
|
|
let mut data = ClientData {
|
|
compositor: None,
|
|
shm: None,
|
|
xdg_wm_base: None,
|
|
deco_mgr: None,
|
|
configured_size: cfg_clone.clone(),
|
|
deco_mode: deco_clone,
|
|
acked_serial: None,
|
|
};
|
|
let _reg = display.get_registry(&qh, ());
|
|
queue.roundtrip(&mut data).expect("roundtrip 1");
|
|
|
|
let comp = data.compositor.as_ref().expect("wl_compositor").clone();
|
|
let shm = data.shm.as_ref().expect("wl_shm").clone();
|
|
let wm_base = data.xdg_wm_base.as_ref().expect("xdg_wm_base").clone();
|
|
let deco_mgr = data.deco_mgr.as_ref().expect("zxdg_decoration_manager_v1").clone();
|
|
|
|
let surface = comp.create_surface(&qh, ());
|
|
let xdg_surf = wm_base.get_xdg_surface(&surface, &qh, ());
|
|
let toplevel = xdg_surf.get_toplevel(&qh, ());
|
|
toplevel.set_title(title.into());
|
|
toplevel.set_app_id(app_id.into());
|
|
let _deco = deco_mgr.get_toplevel_decoration(&toplevel, &qh, ());
|
|
|
|
// Initial commit triggers ToplevelOpened -> WM configure
|
|
surface.commit();
|
|
conn.flush().unwrap();
|
|
|
|
let deadline = Instant::now() + Duration::from_secs(3);
|
|
while cfg_clone.lock().unwrap().is_none() && Instant::now() < deadline {
|
|
let _ = queue.dispatch_pending(&mut data);
|
|
if let Some(guard) = conn.prepare_read() {
|
|
let _ = guard.read();
|
|
}
|
|
let _ = queue.dispatch_pending(&mut data);
|
|
std::thread::sleep(Duration::from_millis(5));
|
|
}
|
|
|
|
let (w, h) = cfg_clone.lock().unwrap().expect("received WM configure size");
|
|
|
|
// Create backing SHM file and fill with slate #1e293b + cyan #38bdf8 center square
|
|
let stride = w * 4;
|
|
let byte_len = (stride * h) as usize;
|
|
let shm_file_path = shm_dir.join(format!("client-{idx}.shm"));
|
|
let file = File::options()
|
|
.read(true)
|
|
.write(true)
|
|
.create(true)
|
|
.truncate(true)
|
|
.open(&shm_file_path)
|
|
.unwrap();
|
|
file.set_len(byte_len as u64).unwrap();
|
|
let mut pixels = vec![0u8; byte_len];
|
|
for y in 0..h {
|
|
for x in 0..w {
|
|
let off = ((y * w + x) * 4) as usize;
|
|
let in_center = (x - w / 2).abs() < 40 && (y - h / 2).abs() < 40;
|
|
// Little-endian ARGB8888: [B, G, R, A]
|
|
if in_center {
|
|
pixels[off] = 0xf8;
|
|
pixels[off + 1] = 0xbd;
|
|
pixels[off + 2] = 0x38;
|
|
pixels[off + 3] = 0xff;
|
|
} else {
|
|
pixels[off] = 0x3b;
|
|
pixels[off + 1] = 0x29;
|
|
pixels[off + 2] = 0x1e;
|
|
pixels[off + 3] = 0xff;
|
|
}
|
|
}
|
|
}
|
|
use std::io::Write;
|
|
(&file).write_all(&pixels).unwrap();
|
|
|
|
let pool = shm.create_pool(file.as_fd(), byte_len as i32, &qh, ());
|
|
let buffer = pool.create_buffer(0, w, h, stride, wl_shm::Format::Argb8888, &qh, ());
|
|
surface.attach(Some(&buffer), 0, 0);
|
|
surface.damage_buffer(0, 0, w, h);
|
|
surface.commit();
|
|
conn.flush().unwrap();
|
|
|
|
std::thread::sleep(Duration::from_millis(150));
|
|
});
|
|
|
|
(handle, configured_size, deco_mode)
|
|
}
|
|
|
|
#[test]
|
|
fn e2e_wayland_client_with_moonbit_wm_and_ssd_decorations() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let socket_path = tmp.path().join("wayland-moon-0");
|
|
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).parent().unwrap().to_path_buf();
|
|
let components_dir = root.join("build/components");
|
|
let theme = ThemeTokens::load_file(&root.join("config/themes/moon-night.json")).unwrap();
|
|
|
|
let mut comp = MoonPlasmaCompositor::new(&socket_path, &components_dir, theme, 1280, 720)
|
|
.expect("boot compositor");
|
|
|
|
let (c1, cfg1, deco1) = spawn_wayland_test_client(
|
|
socket_path.clone(),
|
|
"MoonTerm",
|
|
"org.moonplasma.term",
|
|
tmp.path().to_path_buf(),
|
|
1,
|
|
);
|
|
|
|
let deadline = Instant::now() + Duration::from_secs(3);
|
|
while Instant::now() < deadline {
|
|
comp.step().unwrap();
|
|
if comp.windows.get(&1).and_then(|w| w.surface.as_ref()).is_some() {
|
|
break;
|
|
}
|
|
std::thread::sleep(Duration::from_millis(5));
|
|
}
|
|
|
|
assert_eq!(*deco1.lock().unwrap(), Some(DecoMode::ServerSide));
|
|
let (cw, ch) = cfg1.lock().unwrap().expect("client 1 configured");
|
|
assert!(cw > 1200 && ch > 600, "unexpected tiled size: {cw}x{ch}");
|
|
|
|
let pixmap = comp.compose_frame().expect("compose frame");
|
|
let out_png = root.join("host-native/target/e2e_frame.png");
|
|
SoftwareRenderer::save_png(&pixmap, &out_png).unwrap();
|
|
|
|
// Verify client surface center pixel (#38bdf8) landed inside the WM-assigned geometry!
|
|
let win = comp.windows.get(&1).unwrap();
|
|
let cx = win.geometry.x + (win.geometry.w as i32) / 2;
|
|
let cy = win.geometry.y + (win.geometry.h as i32) / 2;
|
|
let px = pixmap.pixel(cx as u32, cy as u32).unwrap();
|
|
assert_eq!(
|
|
(px.red(), px.green(), px.blue(), px.alpha()),
|
|
(0x38, 0xbd, 0xf8, 0xff),
|
|
"expected cyan client pixel at ({cx}, {cy})"
|
|
);
|
|
|
|
// Verify 0 component faults
|
|
assert!(
|
|
comp.wasm.shared.faults.lock().unwrap().is_empty(),
|
|
"unexpected faults: {:?}",
|
|
comp.wasm.shared.faults.lock().unwrap()
|
|
);
|
|
|
|
// Now open a 2nd Wayland client and verify BSP tiling splits the screen!
|
|
let (c2, cfg2, _) = spawn_wayland_test_client(
|
|
socket_path.clone(),
|
|
"Files",
|
|
"org.moonplasma.files",
|
|
tmp.path().to_path_buf(),
|
|
2,
|
|
);
|
|
|
|
let deadline2 = Instant::now() + Duration::from_secs(3);
|
|
while Instant::now() < deadline2 {
|
|
comp.step().unwrap();
|
|
if comp.windows.get(&2).and_then(|w| w.surface.as_ref()).is_some() {
|
|
break;
|
|
}
|
|
std::thread::sleep(Duration::from_millis(5));
|
|
}
|
|
|
|
let (cw2, _) = cfg2.lock().unwrap().expect("client 2 configured");
|
|
assert!(cw2 < 700, "expected BSP split width < 700, got {cw2}");
|
|
|
|
// Test layout mode switch to Floating
|
|
comp.set_layout_mode(LayoutMode::Floating);
|
|
let _ = comp.compose_frame().unwrap();
|
|
|
|
let _ = c1.join();
|
|
let _ = c2.join();
|
|
}
|