Files
chandra/compositor/host/compositor.mbt
T
nandiandClaude Sonnet 5.5 59ea224d38
CI / test (push) Successful in 45s
12-hour clock and hover-dependent cursor
- timefmt.hhmm12 + render_digital(hour12=); compositor panel shows '7:51 PM'
  (wasm clock component keeps 24h by default)
- wp_cursor_shape_v1: server lets clients pick their cursor; nested window
  forwards the shape to the host. Launcher button/entries show a hand, the
  search field a text cursor, client windows what the client asked for.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 19:51:16 -08:00

518 lines
14 KiB
MoonBit

///| Pure-MoonBit Wayland compositor host wiring WaylandServer, lib/wm, Breeze SSD, SceneGraph, and SoftwareRenderer.
///|
pub(all) struct HostWindow {
id : UInt64
mut title : String
mut app_id : String
mut geometry : @scene.Rect
mut focused : Bool
mut maximized : Bool
mut fullscreen : Bool
mut surface : @scene.ShmBufferSnapshot?
}
///|
pub(all) struct MoonPlasmaCompositor {
server : @wayland.WaylandServer
wm : @wm.WmState
theme : @scene.ThemeTokens
output_w : Int
output_h : Int
work_area : @wm.Rect
windows : Map[UInt64, HostWindow]
damage : @scene.DamageTracker
shell : @shell.DesktopShell
mut ptr_x : Double
mut ptr_y : Double
mut ptr_win : UInt64?
mut kbd_win : UInt64?
mut mods : UInt
mut layout_mode : @wm.LayoutMode
}
///|
pub fn MoonPlasmaCompositor::new(
socket_path : String,
theme : @scene.ThemeTokens,
output_w : Int,
output_h : Int,
) -> MoonPlasmaCompositor? {
let server = match @wayland.WaylandServer::listen(socket_path, output_w, output_h) {
Some(s) => s
None => return None
}
let wm = @wm.WmState::new(
inner_gap=8,
outer_gap=8,
titlebar_h=32,
border_w=2,
)
let out_rect : @wm.Rect = { x: 0, y: 0, w: output_w, h: output_h }
let work_area = @wm.compute_work_area(out_rect, excl_bottom=theme.panel_height)
Some({
server,
wm,
theme,
output_w,
output_h,
work_area,
windows: {},
damage: @scene.DamageTracker::new(),
shell: @shell.DesktopShell::new(socket_path),
ptr_x: 0.0,
ptr_y: 0.0,
ptr_win: None,
kbd_win: None,
mods: 0U,
layout_mode: @wm.LayoutMode::Tiling,
})
}
///|
pub fn MoonPlasmaCompositor::close(self : MoonPlasmaCompositor) -> Unit {
self.server.close()
}
///|
fn MoonPlasmaCompositor::apply_actions(
self : MoonPlasmaCompositor,
actions : Array[@wm.WmAction],
) -> Unit {
for act in actions {
match act {
@wm.WmAction::SetGeometry(wid, r) =>
match self.windows.get(wid) {
Some(win) => {
win.geometry = @scene.Rect::new(r.x, r.y, r.w, r.h)
self.server.send_toplevel_configure(
wid,
r.w,
r.h,
win.focused,
win.maximized,
win.fullscreen,
)
self.damage.mark_full()
}
None => ()
}
@wm.WmAction::Focus(wid) => {
for id, w in self.windows {
w.focused = id == wid
}
self.set_kbd_focus(Some(wid))
self.damage.mark_full()
}
@wm.WmAction::Raise(_) => self.damage.mark_full()
@wm.WmAction::Close(wid) => self.server.send_toplevel_close(wid)
@wm.WmAction::SetMaximized(wid, m) =>
match self.windows.get(wid) {
Some(win) => win.maximized = m
None => ()
}
@wm.WmAction::SetFullscreen(wid, f) =>
match self.windows.get(wid) {
Some(win) => win.fullscreen = f
None => ()
}
}
}
}
///|
pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit {
let events = self.server.poll_events()
for ev in events {
match ev {
@wayland.CompositorEvent::ToplevelOpened(wid, app_id, title, min_w, min_h) => {
let win : HostWindow = {
id: wid,
title,
app_id,
geometry: @scene.Rect::new(100, 100, 640, 480),
focused: true,
maximized: false,
fullscreen: false,
surface: None,
}
self.windows.set(wid, win)
let rec : @wm.WindowRecord = {
id: wid,
app_id,
title,
geometry: { x: 100, y: 100, w: 640, h: 480 },
floating_geometry: { x: 100, y: 100, w: 640, h: 480 },
min_size: { w: min_w, h: min_h },
max_size: None,
focused: true,
maximized: false,
fullscreen: false,
minimized: false,
is_floating: false,
workspace: 0U,
can_move: true,
can_resize: true,
can_maximize: true,
can_minimize: true,
can_fullscreen: true,
}
let actions = self.wm.open_window(rec, self.work_area)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelUpdated(wid, app_id, title) =>
match self.windows.get(wid) {
Some(win) => {
if app_id.length() > 0 {
win.app_id = app_id
}
if title.length() > 0 {
win.title = title
}
self.damage.mark_full()
}
None => ()
}
@wayland.CompositorEvent::ToplevelCommitted(wid, snap) =>
match self.windows.get(wid) {
Some(win) => {
match snap {
Some(s) => {
win.surface = Some(s)
self.damage.mark_rect(win.geometry)
}
None => ()
}
}
None => ()
}
@wayland.CompositorEvent::ToplevelClosed(wid) => {
ignore(self.windows.remove(wid))
if self.ptr_win == Some(wid) {
self.ptr_win = None
}
if self.kbd_win == Some(wid) {
self.kbd_win = None
}
let actions = self.wm.close_window(wid, self.work_area)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMaximizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Maximize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelFullscreenRequest(wid, f) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Fullscreen(f),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMinimizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Minimize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMoveRequest(_) => ()
}
}
}
///|
pub fn MoonPlasmaCompositor::set_layout_mode(
self : MoonPlasmaCompositor,
mode : @wm.LayoutMode,
) -> Unit {
self.layout_mode = mode
let actions = self.wm.set_layout_mode(mode, self.work_area)
self.apply_actions(actions)
}
///|
pub fn MoonPlasmaCompositor::compose_frame(
self : MoonPlasmaCompositor,
) -> @renderer.Pixmap {
let scene = @scene.SceneGraph::new(self.output_w, self.output_h)
let order = self.wm.stacking_order()
let titles : Array[String] = []
for wid in order {
match self.windows.get(wid) {
Some(win) => {
let label = if win.title.length() > 0 { win.title } else { win.app_id }
titles.push(label)
let (insets, deco_ops) = @scene.build_breeze_decoration(
win.title,
win.app_id,
win.focused,
win.fullscreen,
win.geometry.w + 4,
win.geometry.h + 34,
self.theme,
)
scene.windows.push({
id: win.id,
title: win.title,
app_id: win.app_id,
focused: win.focused,
fullscreen: win.fullscreen,
geometry: win.geometry,
deco_insets: insets,
deco_ops,
surface: win.surface,
})
}
None => ()
}
}
let layout_label = match self.layout_mode {
@wm.LayoutMode::Tiling => "BSP Tiling"
@wm.LayoutMode::Floating => "Floating"
@wm.LayoutMode::Monocle => "Monocle"
}
let (bg_ops, panel_ops) = self.shell.render(
self.output_w,
self.output_h,
self.theme,
layout_label,
titles,
)
scene.background_ops = bg_ops
scene.panel_ops = panel_ops
ignore(self.damage.take_damage(self.output_w, self.output_h))
@renderer.render_scene(scene)
}
///|
/// Compose the current desktop GUI frame and present it onto a wasi-gfx
/// `SurfaceFrameBufferContext`.
pub fn MoonPlasmaCompositor::present_to_gfx(
self : MoonPlasmaCompositor,
ctx : @gfx.SurfaceFrameBufferContext,
) -> @renderer.Pixmap {
let pixmap = self.compose_frame()
@gfx.present_pixmap(ctx, pixmap)
pixmap
}
///|
/// Process pending wasi-gfx surface input events (pointer clicks for focus).
pub fn MoonPlasmaCompositor::handle_gfx_events(
self : MoonPlasmaCompositor,
surface : @gfx.Surface,
) -> Unit {
let downs = surface.drain_pointer_down()
for ev in downs {
let px = ev.x.to_int()
let py = ev.y.to_int()
let order = self.wm.stacking_order()
// Top-most window hit-test (iterate reverse Z-order).
let mut i = order.length() - 1
while i >= 0 {
let wid = order[i]
match self.windows.get(wid) {
Some(win) =>
if px >= win.geometry.x &&
px < win.geometry.x + win.geometry.w &&
py >= win.geometry.y - 32 &&
py < win.geometry.y + win.geometry.h {
self.apply_actions([
@wm.WmAction::Focus(wid),
@wm.WmAction::Raise(wid),
])
break
}
None => ()
}
i = i - 1
}
}
}
///| Move keyboard focus (wl_keyboard leave/enter + current modifiers).
fn MoonPlasmaCompositor::set_kbd_focus(self : MoonPlasmaCompositor, wid : UInt64?) -> Unit {
if self.kbd_win == wid {
return
}
match self.kbd_win {
Some(old) => self.server.keyboard_leave(old)
None => ()
}
self.kbd_win = wid
match wid {
Some(w) => {
self.server.keyboard_enter(w)
self.server.keyboard_modifiers(w, self.mods, 0U, 0U, 0U)
}
None => ()
}
}
///| Top-most window under (x, y): (id, in client area). The titlebar counts as the window but not the client.
fn MoonPlasmaCompositor::window_at(self : MoonPlasmaCompositor, x : Int, y : Int) -> (UInt64, Bool)? {
let order = self.wm.stacking_order()
let mut i = order.length() - 1
while i >= 0 {
match self.windows.get(order[i]) {
Some(win) => {
let g = win.geometry
if x >= g.x && x < g.x + g.w && y >= g.y && y < g.y + g.h {
return Some((win.id, true))
}
if x >= g.x - 2 && x < g.x + g.w + 2 && y >= g.y - 32 && y < g.y {
return Some((win.id, false))
}
}
None => ()
}
i = i - 1
}
None
}
///|
fn MoonPlasmaCompositor::leave_pointer(self : MoonPlasmaCompositor) -> Unit {
match self.ptr_win {
Some(w) => {
self.server.pointer_leave(w)
self.ptr_win = None
}
None => ()
}
}
///| evdev keycode -> the key name lib/launcher understands (US layout).
fn key_name(code : UInt, shift : Bool) -> String {
let rows = [
(16U, "qwertyuiop"),
(30U, "asdfghjkl"),
(44U, "zxcvbnm"),
(2U, "1234567890"),
]
for r in rows {
let (first, chars) = r
if code >= first && code < first + chars.length().reinterpret_as_uint() {
let c = chars[(code - first).reinterpret_as_int()]
return if shift && c >= 'a' && c <= 'z' {
Int::unsafe_to_char(c.to_int() - 32).to_string()
} else {
c.to_string()
}
}
}
match code {
1U => "Escape"
14U => "Backspace"
28U => "Enter"
57U => " "
12U => "-"
52U => "."
53U => "/"
_ => ""
}
}
///| Route host input: shell (launcher) first, then the window under the pointer
/// / the keyboard-focused window.
pub fn MoonPlasmaCompositor::handle_input(
self : MoonPlasmaCompositor,
ev : @nested.InputEvent,
time_ms : UInt,
) -> Unit {
match ev {
PointerMove(x, y) => {
self.ptr_x = x
self.ptr_y = y
let xi = x.to_int()
let yi = y.to_int()
if self.shell.pointer_move(xi, yi) || self.shell.hit(xi, yi) {
self.leave_pointer()
return
}
match self.window_at(xi, yi) {
Some((wid, true)) =>
match self.windows.get(wid) {
Some(win) => {
let lx = x - win.geometry.x.to_double()
let ly = y - win.geometry.y.to_double()
if self.ptr_win != Some(wid) {
self.leave_pointer()
self.ptr_win = Some(wid)
self.server.pointer_enter(wid, lx, ly)
}
self.server.pointer_motion(wid, time_ms, lx, ly)
}
None => ()
}
_ => self.leave_pointer()
}
}
PointerButton(button, pressed) => {
let xi = self.ptr_x.to_int()
let yi = self.ptr_y.to_int()
if pressed {
if self.shell.pointer_down(xi, yi) {
return
}
match self.window_at(xi, yi) {
Some((wid, _)) => {
self.apply_actions([
@wm.WmAction::Focus(wid),
@wm.WmAction::Raise(wid),
])
}
None => ()
}
}
match self.ptr_win {
Some(w) => self.server.pointer_button(w, time_ms, button, pressed)
None => ()
}
}
Key(code, pressed) =>
if self.shell.popup_open() {
if pressed {
let name = key_name(code, (self.mods & 1U) != 0U)
if name.length() > 0 {
self.shell.key_down(name)
}
}
} else {
match self.kbd_win {
Some(w) => self.server.keyboard_key(w, time_ms, code, pressed)
None => ()
}
}
Modifiers(dep, lat, lock, group) => {
self.mods = dep
match self.kbd_win {
Some(w) => self.server.keyboard_modifiers(w, dep, lat, lock, group)
None => ()
}
}
}
}
///| Cursor shape (wp_cursor_shape_v1 value) for the current pointer position: shell UI
/// decides over the launcher; a client window decides inside its client area; otherwise default.
pub fn MoonPlasmaCompositor::desired_cursor(self : MoonPlasmaCompositor) -> UInt {
let shell_shape = self.shell.cursor_at(self.ptr_x.to_int(), self.ptr_y.to_int())
if shell_shape != 0U {
return shell_shape
}
match self.ptr_win {
Some(w) => self.server.client_cursor_shape(w)
None => 1U
}
}