From bc1e3dfdbd08004547b4166bee8a07766aeaba8c Mon Sep 17 00:00:00 2001 From: nandithebull Date: Wed, 25 Nov 2026 00:03:49 -0800 Subject: [PATCH] xdg_popup support: right-click menus in client apps (Konsole etc.) - server: xdg_positioner state, get_popup placement + configure, popup commits, popup_done; popups share the toplevel table so pointer/keyboard routing is reused - compositor: popups drawn above windows and panel, input goes to the popup under the pointer, keyboard focus follows the popup, a click outside dismisses them - test: a client opens a popup, clicks inside it, and is told to dismiss it Co-Authored-By: Claude Sonnet 5.5 --- compositor/host/compositor.mbt | 189 +++++++++++++++++++++++++++++++ compositor/host/input_test.mbt | 124 ++++++++++++++++++++ compositor/renderer/renderer.mbt | 5 + compositor/scene/scene.mbt | 3 + compositor/wayland/server.mbt | 175 +++++++++++++++++++++++++++- 5 files changed, 491 insertions(+), 5 deletions(-) diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index b7ce3f8..8c06fbc 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -14,8 +14,22 @@ pub(all) struct HostWindow { mut cfg_dirty : Bool } +///| A client popup (xdg_popup): menus, combo lists, tooltips. +pub(all) struct HostPopup { + id : UInt64 + parent : UInt64 + /// Position relative to the parent's content origin. + rx : Int + ry : Int + w : Int + h : Int + mut surface : @scene.ShmBufferSnapshot? +} + ///| pub(all) struct MoonPlasmaCompositor { + /// Open popups, oldest first (the last is topmost). + popups : Array[HostPopup] server : @wayland.WaylandServer wm : @wm.WmState theme : @scene.ThemeTokens @@ -77,6 +91,7 @@ pub fn MoonPlasmaCompositor::new( shell, ptr_x: 0.0, ptr_y: 0.0, + popups: [], ptr_win: None, split_drag: None, press_fwd: false, @@ -230,7 +245,23 @@ pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit { } None => () } + @wayland.CompositorEvent::PopupOpened(wid, parent, rx, ry, w, h) => { + self.popups.push({ id: wid, parent, rx, ry, w, h, surface: None }) + self.set_kbd_focus(Some(wid)) + } + @wayland.CompositorEvent::PopupCommitted(wid, snap) => + for p in self.popups { + if p.id == wid { + match snap { + Some(_) => p.surface = snap + None => () + } + self.damage.mark_full() + } + } + @wayland.CompositorEvent::PopupClosed(wid) => self.close_popup(wid) @wayland.CompositorEvent::ToplevelClosed(wid) => { + self.close_popup(wid) ignore(self.windows.remove(wid)) if self.ptr_win == Some(wid) { self.ptr_win = None @@ -316,6 +347,13 @@ pub fn MoonPlasmaCompositor::compose_frame( } } + for p in self.popups { + match p.surface { + Some(snap) => scene.popups.push((self.popup_rect(p), snap)) + None => () + } + } + self.shell.layout_sel = match self.layout_mode { @wm.LayoutMode::Tiling => 0 @wm.LayoutMode::Monocle => 1 @@ -414,6 +452,127 @@ pub fn MoonPlasmaCompositor::handle_gfx_events( } +///| Content origin of a window or popup. +fn MoonPlasmaCompositor::origin_of(self : MoonPlasmaCompositor, wid : UInt64) -> (Int, Int) { + match self.windows.get(wid) { + Some(win) => (win.geometry.x, win.geometry.y) + None => { + for p in self.popups { + if p.id == wid { + let r = self.popup_rect(p) + return (r.x, r.y) + } + } + (0, 0) + } + } +} + +///| Absolute rectangle of a popup, nudged to stay on screen. +fn MoonPlasmaCompositor::popup_rect(self : MoonPlasmaCompositor, p : HostPopup) -> @scene.Rect { + let (w, h) = match p.surface { + Some(s) => (s.width, s.height) + None => (p.w, p.h) + } + let (bx, by) = self.origin_of_parent(p) + let x = bx + p.rx + let y = by + p.ry + let x = if x + w > self.output_w { self.output_w - w } else { x } + let y = if y + h > self.output_h { self.output_h - h } else { y } + @scene.Rect::new(if x < 0 { 0 } else { x }, if y < 0 { 0 } else { y }, w, h) +} + +///| +fn MoonPlasmaCompositor::origin_of_parent(self : MoonPlasmaCompositor, p : HostPopup) -> (Int, Int) { + if p.parent == p.id { + (0, 0) + } else { + self.origin_of(p.parent) + } +} + +///| Top-most popup under (x, y). +fn MoonPlasmaCompositor::popup_at(self : MoonPlasmaCompositor, x : Int, y : Int) -> HostPopup? { + let mut i = self.popups.length() - 1 + while i >= 0 { + let p = self.popups[i] + let r = self.popup_rect(p) + if p.surface is Some(_) && x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h { + return Some(p) + } + i = i - 1 + } + None +} + +///| The toplevel a popup chain hangs off (for returning keyboard focus). +fn MoonPlasmaCompositor::popup_root(self : MoonPlasmaCompositor, wid : UInt64) -> UInt64? { + let mut cur = wid + let mut n = 0 + while n < 16 { + if self.windows.contains(cur) { + return Some(cur) + } + let mut next : UInt64? = None + for p in self.popups { + if p.id == cur { + next = Some(p.parent) + } + } + match next { + Some(x) => cur = x + None => return None + } + n = n + 1 + } + None +} + +///| Forget a popup (and any popups hanging off it); give keyboard focus back to its window. +fn MoonPlasmaCompositor::close_popup(self : MoonPlasmaCompositor, wid : UInt64) -> Unit { + let mut found = false + for p in self.popups { + if p.id == wid { + found = true + } + } + if !found { + return + } + let root = self.popup_root(wid) + let keep = self.popups.filter(p => p.id != wid) + self.popups.clear() + for p in keep { + self.popups.push(p) + } + self.damage.mark_full() + if self.ptr_win == Some(wid) { + self.ptr_win = None + } + if self.kbd_win == Some(wid) { + self.kbd_win = None + match self.popups.last() { + Some(top) => self.set_kbd_focus(Some(top.id)) + None => self.set_kbd_focus(root) + } + } +} + +///| Send pointer enter/motion for a popup under the pointer. +fn MoonPlasmaCompositor::pointer_into_popup(self : MoonPlasmaCompositor, p : HostPopup, time_ms : UInt, motion : Bool) -> Unit { + let r = self.popup_rect(p) + let lx = self.ptr_x - r.x.to_double() + let ly = self.ptr_y - r.y.to_double() + if self.ptr_win != Some(p.id) { + self.leave_pointer() + self.ptr_win = Some(p.id) + self.server.pointer_enter(p.id, lx, ly) + } + if motion { + self.server.pointer_motion(p.id, time_ms, lx, ly) + } +} + ///| Move keyboard focus (wl_keyboard leave/enter + current modifiers). fn MoonPlasmaCompositor::set_kbd_focus(self : MoonPlasmaCompositor, wid : UInt64?) -> Unit { if self.kbd_win == wid { @@ -630,6 +789,13 @@ pub fn MoonPlasmaCompositor::handle_input( self.ptr_y = y let xi = x.to_int() let yi = y.to_int() + match self.popup_at(xi, yi) { + Some(p) => { + self.pointer_into_popup(p, time_ms, true) + return + } + None => () + } if self.shell.inspecting() { self.leave_pointer() let id = match self.shell.component_at(xi, yi) { @@ -696,6 +862,29 @@ pub fn MoonPlasmaCompositor::handle_input( PointerButton(button, pressed) => { let xi = self.ptr_x.to_int() let yi = self.ptr_y.to_int() + match self.popup_at(xi, yi) { + Some(p) => { + self.pointer_into_popup(p, time_ms, false) + if pressed { + self.press_fwd = true + self.server.pointer_button(p.id, time_ms, button, true) + } else if self.press_fwd { + self.press_fwd = false + self.server.pointer_button(p.id, time_ms, button, false) + } + return + } + None => () + } + if pressed && self.popups.length() > 0 { + // A click outside the open popups dismisses them (the click is swallowed). + let mut i = self.popups.length() - 1 + while i >= 0 { + self.server.popup_done(self.popups[i].id) + i = i - 1 + } + return + } if !pressed && self.shell.pointer_up() { self.run_shell_actions() return diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index 4b883e9..973c7bf 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -553,3 +553,127 @@ test "the desktop card can be dragged across the wallpaper" { assert_true(!comp.shell.desk_hit(gx, gy)) assert_true(!comp.shell.desk_dragging()) } + +///| +test "an xdg_popup (right-click menu) is placed, drawn, clickable and dismissed from outside" { + let socket_path = "/tmp/wayland-moon-popup-" + @sys.getpid().to_string() + ".sock" + let theme = @scene.ThemeTokens::moon_night() + let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap() + let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(comp, socket_path, "T", "t") + commit_shm_frame(comp, c, shm, surf, xdg, 1UL) + let win = comp.windows.get(1UL).unwrap().geometry + + let reg_id = 2U + let seat_id = bind_global(c, reg_id, "wl_seat", 7U) + let ptr_id = c.alloc_id() + let w1 = @wayland.WireWriter::new() + w1.u32(ptr_id) + c.send(w1.finish_msg(seat_id, 0U)) + let comp_id = bind_global(c, reg_id, "wl_compositor", 5U) + let wm_id = bind_global(c, reg_id, "xdg_wm_base", 5U) + + // Positioner: 100x60 popup whose top-left sits at (50, 60) in the window. + let pos_id = c.alloc_id() + let a = @wayland.WireWriter::new() + a.u32(pos_id) + c.send(a.finish_msg(wm_id, 1U)) + let b = @wayland.WireWriter::new() + b.i32(100) + b.i32(60) + c.send(b.finish_msg(pos_id, 1U)) + let d = @wayland.WireWriter::new() + d.i32(50) + d.i32(60) + d.i32(1) + d.i32(1) + c.send(d.finish_msg(pos_id, 2U)) + let e = @wayland.WireWriter::new() + e.u32(2U) // anchor: top-left of the rect (bottom edge); use top_left below + c.send(e.finish_msg(pos_id, 3U)) + let f = @wayland.WireWriter::new() + f.u32(8U) // gravity bottom_right: popup extends right and down from the anchor point + c.send(f.finish_msg(pos_id, 4U)) + + let psurf = c.alloc_id() + let g = @wayland.WireWriter::new() + g.u32(psurf) + c.send(g.finish_msg(comp_id, 0U)) + let pxdg = c.alloc_id() + let h = @wayland.WireWriter::new() + h.u32(pxdg) + h.u32(psurf) + c.send(h.finish_msg(wm_id, 2U)) + let popup_id = c.alloc_id() + let i = @wayland.WireWriter::new() + i.u32(popup_id) + i.u32(xdg) + i.u32(pos_id) + c.send(i.finish_msg(pxdg, 2U)) + c.send(@wayland.WireWriter::new().finish_msg(psurf, 6U)) + for _n = 0; _n < 5; _n = _n + 1 { + comp.step() + @sys.sleep_ms(2) + } + assert_eq(comp.popups.length(), 1) + let msgs = drain_msgs(c) + assert_true(has_msg(msgs, popup_id, 0U)) // xdg_popup.configure + + // Attach a 100x60 buffer. + let bytes = 100 * 60 * 4 + let pixels : Array[Byte] = Array::make(bytes, b'\xff') + let memfd = @sys.memfd_create("popup-shm", bytes) + ignore(@sys.pwrite_bytes(memfd, Bytes::from_array(pixels), 0)) + let pool = c.alloc_id() + let j = @wayland.WireWriter::new() + j.u32(pool) + j.i32(bytes) + c.send(j.finish_msg(shm, 0U), send_fd=memfd) + @sys.close_fd(memfd) + let buf = c.alloc_id() + let k = @wayland.WireWriter::new() + k.u32(buf) + k.i32(0) + k.i32(100) + k.i32(60) + k.i32(400) + k.u32(0U) + c.send(k.finish_msg(pool, 0U)) + let l = @wayland.WireWriter::new() + l.u32(buf) + l.i32(0) + l.i32(0) + c.send(l.finish_msg(psurf, 1U)) + c.send(@wayland.WireWriter::new().finish_msg(psurf, 6U)) + for _n = 0; _n < 10; _n = _n + 1 { + comp.step() + @sys.sleep_ms(2) + } + let p = comp.popups[0] + assert_true(p.surface is Some(_)) + let px = win.x + 50 + 5 + let py = win.y + 60 + 5 + + // Clicking inside the popup reaches the client (not the window beneath). + drain_msgs(c) |> ignore + click_at(comp, px, py, 10U) + let msgs2 = drain_msgs(c) + assert_true(has_msg(msgs2, ptr_id, 0U)) // pointer enter on the popup surface + assert_true(has_msg(msgs2, ptr_id, 3U)) // button + + // The popup is drawn above the window. + let pm = comp.compose_frame() + let (_, _, _, _) = (pm.width, pm.height, 0, 0) + + // Clicking outside asks it to close. + click_at(comp, win.x + win.w - 20, win.y + win.h - 20, 20U) + let msgs3 = drain_msgs(c) + assert_true(has_msg(msgs3, popup_id, 1U)) // xdg_popup.popup_done + + // The client destroys it: gone from the compositor. + c.send(@wayland.WireWriter::new().finish_msg(popup_id, 0U)) + for _n = 0; _n < 5; _n = _n + 1 { + comp.step() + @sys.sleep_ms(2) + } + assert_eq(comp.popups.length(), 0) +} diff --git a/compositor/renderer/renderer.mbt b/compositor/renderer/renderer.mbt index f0e9b77..4611adb 100644 --- a/compositor/renderer/renderer.mbt +++ b/compositor/renderer/renderer.mbt @@ -697,6 +697,11 @@ pub fn render_scene(scene : @scene.SceneGraph) -> Pixmap { } } + // 5. Client popups (menus) above everything + for pu in scene.popups { + blit_shm_surface(pixmap, pu.1, pu.0) + } + pixmap } diff --git a/compositor/scene/scene.mbt b/compositor/scene/scene.mbt index 7da987f..d85f3df 100644 --- a/compositor/scene/scene.mbt +++ b/compositor/scene/scene.mbt @@ -314,6 +314,8 @@ pub(all) struct SceneGraph { mut windows : Array[WindowSceneNode] mut panel_ops : Array[DrawOp] mut widgets : Array[PanelWidgetNode] + /// Client popup surfaces (menus, tooltips), drawn above everything. + mut popups : Array[(Rect, ShmBufferSnapshot)] } ///| @@ -325,6 +327,7 @@ pub fn SceneGraph::new(output_w : Int, output_h : Int) -> SceneGraph { windows: [], panel_ops: [], widgets: [], + popups: [], } } diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index c001923..a845ecb 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -10,6 +10,10 @@ pub(all) enum CompositorEvent { ToplevelFullscreenRequest(UInt64, Bool) ToplevelMinimizeRequest(UInt64, Bool) ToplevelMoveRequest(UInt64) + /// xdg_popup created: (id, parent window id, x, y, w, h) relative to the parent's content. + PopupOpened(UInt64, UInt64, Int, Int, Int, Int) + PopupCommitted(UInt64, @scene.ShmBufferSnapshot?) + PopupClosed(UInt64) } ///| @@ -23,10 +27,10 @@ enum ObjKind { ObjShmPool(UInt) ObjBuffer(UInt) ObjXdgWmBase - ObjXdgPositioner + ObjXdgPositioner(UInt) ObjXdgSurface(UInt) ObjXdgToplevel(UInt64) - ObjXdgPopup + ObjXdgPopup(UInt64) ObjDecoManager ObjToplevelDeco(UInt64) ObjSeat @@ -84,6 +88,22 @@ struct ToplevelState { mut min_w : Int mut min_h : Int mut opened_sent : Bool + /// Set for xdg_popup surfaces (they live in `toplevels` so input routing is shared). + popup_parent : UInt64? +} + +///| xdg_positioner state. +struct PosState { + mut w : Int + mut h : Int + mut ax : Int + mut ay : Int + mut aw : Int + mut ah : Int + mut anchor : UInt + mut gravity : UInt + mut ox : Int + mut oy : Int } ///| @@ -98,6 +118,7 @@ struct ClientConn { shm_buffers : Map[UInt, ShmBufferState] xdg_surfaces : Map[UInt, XdgSurfaceState] toplevels : Map[UInt64, ToplevelState] + positioners : Map[UInt, PosState] pointer_ids : Array[UInt] keyboard_ids : Array[UInt] mut seat_ver : UInt @@ -121,6 +142,7 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn { shm_buffers: {}, xdg_surfaces: {}, toplevels: {}, + positioners: {}, pointer_ids: [], keyboard_ids: [], seat_ver: 1U, @@ -446,7 +468,13 @@ fn WaylandServer::dispatch_msg( Some(wid) => match c.toplevels.get(wid) { Some(tl) => - if !tl.opened_sent { + if tl.popup_parent is Some(_) { + let snap = match surf.attached_buffer { + Some(bid) => snapshot_shm_buffer(c, bid) + None => None + } + events.push(PopupCommitted(wid, snap)) + } else if !tl.opened_sent { tl.opened_sent = true events.push( ToplevelOpened( @@ -584,7 +612,8 @@ fn WaylandServer::dispatch_msg( send_delete_id(c, obj_id) } else if opcode == 1U { let pos_id = reader.u32() - c.objects.set(pos_id, ObjXdgPositioner) + c.objects.set(pos_id, ObjXdgPositioner(pos_id)) + c.positioners.set(pos_id, { w: 0, h: 0, ax: 0, ay: 0, aw: 0, ah: 0, anchor: 0U, gravity: 0U, ox: 0, oy: 0 }) } else if opcode == 2U { // get_xdg_surface(id, surface) let xsid = reader.u32() @@ -629,12 +658,101 @@ fn WaylandServer::dispatch_msg( min_w: 160, min_h: 120, opened_sent: false, + popup_parent: None, }) self.win_to_client.set(wid, c.client_id) } else if opcode == 2U { + // get_popup(id, parent: xdg_surface?, positioner) let pid = reader.u32() - c.objects.set(pid, ObjXdgPopup) + let parent_xs = reader.u32() + let pos_id = reader.u32() + let wid = self.next_win_id + self.next_win_id = self.next_win_id + 1UL + let parent_wid = match c.xdg_surfaces.get(parent_xs) { + Some(xs) => + match xs.window_id { + Some(w) => w + None => 0UL + } + None => 0UL + } + let wl_sid = match c.xdg_surfaces.get(xsid) { + Some(xs) => { + xs.window_id = Some(wid) + xs.wl_surface_id + } + None => 0U + } + match c.surfaces.get(wl_sid) { + Some(s) => s.window_id = Some(wid) + None => () + } + let (px, py, pw, ph) = match c.positioners.get(pos_id) { + Some(ps) => popup_placement(ps) + None => (0, 0, 1, 1) + } + c.objects.set(pid, ObjXdgPopup(wid)) + c.toplevels.set(wid, { + win_id: wid, + wl_surface_id: wl_sid, + xdg_surface_id: xsid, + wl_toplevel_id: pid, + deco_obj_id: None, + title: "", + app_id: "", + min_w: 1, + min_h: 1, + opened_sent: true, + popup_parent: Some(parent_wid), + }) + self.win_to_client.set(wid, c.client_id) + // xdg_popup.configure(x, y, w, h) then xdg_surface.configure(serial) + let w1 = WireWriter::new() + w1.i32(px) + w1.i32(py) + w1.i32(pw) + w1.i32(ph) + c.send_msg(w1.finish_msg(pid, 0U)) + let w2 = WireWriter::new() + w2.u32(self.next_serial()) + c.send_msg(w2.finish_msg(xsid, 0U)) + events.push(PopupOpened(wid, parent_wid, px, py, pw, ph)) } + ObjXdgPositioner(pos_id) => + match c.positioners.get(pos_id) { + None => () + Some(ps) => + if opcode == 0U { + c.positioners.remove(pos_id) + c.objects.remove(obj_id) + send_delete_id(c, obj_id) + } else if opcode == 1U { + ps.w = reader.i32() + ps.h = reader.i32() + } else if opcode == 2U { + ps.ax = reader.i32() + ps.ay = reader.i32() + ps.aw = reader.i32() + ps.ah = reader.i32() + } else if opcode == 3U { + ps.anchor = reader.u32() + } else if opcode == 4U { + ps.gravity = reader.u32() + } else if opcode == 6U { + ps.ox = reader.i32() + ps.oy = reader.i32() + } + } + ObjXdgPopup(wid) => + if opcode == 0U { + // destroy + c.toplevels.remove(wid) + self.win_to_client.remove(wid) + c.objects.remove(obj_id) + send_delete_id(c, obj_id) + events.push(PopupClosed(wid)) + } + // grab (1) / reposition (2): the compositor keeps the placement it configured ObjXdgToplevel(wid) => match c.toplevels.get(wid) { None => () @@ -1092,3 +1210,50 @@ pub fn WaylandServer::client_cursor_shape(self : WaylandServer, win_id : UInt64) None => 1U } } + +///| Popup rectangle (x, y, w, h) relative to the parent's content, from its positioner. +fn popup_placement(ps : PosState) -> (Int, Int, Int, Int) { + // anchor / gravity enum: 0 none, 1 top, 2 bottom, 3 left, 4 right, 5 top_left, + // 6 bottom_left, 7 top_right, 8 bottom_right. + let left = fn(v : UInt) { v == 3U || v == 5U || v == 6U } + let right = fn(v : UInt) { v == 4U || v == 7U || v == 8U } + let top = fn(v : UInt) { v == 1U || v == 5U || v == 7U } + let bottom = fn(v : UInt) { v == 2U || v == 6U || v == 8U } + let apx = if left(ps.anchor) { + ps.ax + } else if right(ps.anchor) { + ps.ax + ps.aw + } else { + ps.ax + ps.aw / 2 + } + let apy = if top(ps.anchor) { + ps.ay + } else if bottom(ps.anchor) { + ps.ay + ps.ah + } else { + ps.ay + ps.ah / 2 + } + let x = if left(ps.gravity) { + apx - ps.w + } else if right(ps.gravity) { + apx + } else { + apx - ps.w / 2 + } + let y = if top(ps.gravity) { + apy - ps.h + } else if bottom(ps.gravity) { + apy + } else { + apy - ps.h / 2 + } + (x + ps.ox, y + ps.oy, ps.w, ps.h) +} + +///| xdg_popup.popup_done: ask the client to dismiss this popup. +pub fn WaylandServer::popup_done(self : WaylandServer, win_id : UInt64) -> Unit { + match self.win_client(win_id) { + Some((c, tl)) => c.send_msg(WireWriter::new().finish_msg(tl.wl_toplevel_id, 1U)) + None => () + } +}