xdg_popup support: right-click menus in client apps (Konsole etc.)
CI / test (push) Failing after 37s

- 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 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 00:03:49 -08:00
1 parent 3b2e19ace5
commit bc1e3dfdbd
5 files changed
+491 -5

No files matched your search

+170 -5
View File
@@ -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 => ()
}
}