Files
Claude ff6f733810
CI / test (push) Canceled after 7m7s
chandra-ctl windows: how Chandra sees each window, and what the pointer is over
For "this window won't resize" reports (V's Gajim still not resizing on real hardware,
while a libadwaita window built like Gajim's resizes headless on both hosts): one line per
window with its frame kind (client-frame / chandra-frame), layout state (floating, tiled,
maximized, fullscreen), box, buffer, the client's shadow per side, min/max size and title,
then the pointer position and what it's over (a resize grip and which edge, a window, a
title bar, a tiling divider, the panel, the desktop).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiu4r7mEXBdKY6cgv6
2026-11-26 04:39:45 +00:00

414 lines
17 KiB
MoonBit

///| Client-side decorated windows: GTK apps (GNOME Files, most GNOME apps) never ask for
/// server-side decoration, draw their own title bar, and put a shadow around it in the buffer.
///| The shadow a GTK window draws around itself, in buffer pixels.
let gtk_shadow : Int = 24
///| Answer the last configure the way GTK does: a buffer `w` x `h` plus the shadow on every
/// side, the visible window (solid red, its own title bar included) announced with
/// set_window_geometry, the shadow transparent.
fn gtk_frame(
comp : @host.ChandraCompositor,
c : TestWaylandClient,
shm : UInt,
surf : UInt,
xdg : UInt,
tl : UInt,
) -> (Int, Int) {
for _i = 0; _i < 6; _i = _i + 1 {
comp.step()
c.read_events(xdg, tl, 0U)
@sys.sleep_ms(2)
}
for cfg in c.configures {
let ack = @wayland.WireWriter::new()
ack.u32(cfg.3)
c.send(ack.finish_msg(xdg, 4U))
}
c.configures.clear()
let w = c.configured_w
let h = c.configured_h
let s = gtk_shadow
let bw = w + 2 * s
let bh = h + 2 * s
let px : Array[Byte] = Array(capacity=bw * bh * 4)
for y = 0; y < bh; y = y + 1 {
for x = 0; x < bw; x = x + 1 {
if x >= s && x < s + w && y >= s && y < s + h {
px.push(b'\x00')
px.push(b'\x00')
px.push(b'\xff')
px.push(b'\xff')
} else {
for _k = 0; _k < 4; _k = _k + 1 {
px.push(b'\x00')
}
}
}
}
let fd = @sys.memfd_create("gtk-frame", bw * bh * 4)
ignore(@sys.pwrite_bytes(fd, Bytes::from_array(px), 0))
let pool = c.alloc_id()
let p = @wayland.WireWriter::new()
p.u32(pool)
p.i32(bw * bh * 4)
c.send(p.finish_msg(shm, 0U), send_fd=fd)
@sys.close_fd(fd)
let buf = c.alloc_id()
let cb = @wayland.WireWriter::new()
cb.u32(buf)
cb.i32(0)
cb.i32(bw)
cb.i32(bh)
cb.i32(bw * 4)
cb.u32(0U)
c.send(cb.finish_msg(pool, 0U))
// xdg_surface.set_window_geometry(x, y, w, h)
let g = @wayland.WireWriter::new()
g.i32(s)
g.i32(s)
g.i32(w)
g.i32(h)
c.send(g.finish_msg(xdg, 3U))
attach_commit(comp, c, surf, Some(buf))
(w, h)
}
///|
fn csd_setup(name : String) -> (@host.ChandraCompositor, TestWaylandClient, UInt, UInt, UInt, UInt) {
let socket_path = "/tmp/wayland-moon-csd-" + name + "-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, _) = open_and_configure_client(
comp,
socket_path,
"Home",
"org.gnome.Nautilus",
server_deco=false,
)
(comp, c, shm, surf, xdg, tl)
}
///|
test "a GTK window draws its own frame: no second title bar, and its shadow is not the window" {
// GNOME Files showed its own header bar inside a Chandra-titled frame, with its transparent
// shadow margin drawn as part of the window (content shifted down/right and cut off).
let (comp, c, shm, surf, xdg, tl) = csd_setup("frame")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let win = comp.windows.get(1UL).unwrap()
assert_true(win.csd)
assert_eq((win.geo_x, win.geo_y), (gtk_shadow, gtk_shadow))
// The client is asked to fill the frame's whole box: where Chandra's title bar would be is its own.
let r = win.client_rect()
assert_eq((r.w, r.h), (win.geometry.w + 2, win.geometry.h + 39))
let (w, h) = gtk_frame(comp, c, shm, surf, xdg, tl)
assert_eq((w, h), (r.w, r.h))
let red = Some(@scene.Rgba::new(0xff, 0, 0, 0xff))
let frame = comp.compose_frame()
// The client's pixels start right at the box's top-left (no Chandra title bar over them)
// and fill it to the bottom-right corner.
assert_eq(frame.pixel(r.x + 3, r.y + 3), red)
assert_eq(frame.pixel(r.x + r.w - 3, r.y + r.h - 3), red)
// The transparent shadow is not drawn as part of the window.
assert_true(frame.pixel(r.x + r.w + 10, r.y + 10) != red)
assert_true(frame.pixel(r.x + 10, r.y - 10) != red)
}
///|
test "a server-decorated client keeps Chandra's frame" {
let socket_path = "/tmp/wayland-moon-csd-ssd-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, _) = open_and_configure_client(comp, socket_path, "Dolphin", "org.kde.dolphin")
let size_for = fn(w, h, _max) { (w, h) }
ignore(answer_configures(comp, c, shm, surf, xdg, tl, size_for))
let win = comp.windows.get(1UL).unwrap()
assert_false(win.csd)
let r = win.client_rect()
assert_eq((r.x, r.y, r.w, r.h), (win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h))
}
///|
test "dragging a GTK header bar moves the window, and its border resizes it" {
let (comp, c, shm, surf, xdg, tl) = csd_setup("move")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let before = comp.windows.get(1UL).unwrap().geometry
let r = comp.windows.get(1UL).unwrap().client_rect()
// Press on the header bar (the client's own), which answers with xdg_toplevel.move.
let (px, py) = (r.x + r.w / 2, r.y + 15)
comp.handle_input(@nested.InputEvent::PointerMove(px.to_double(), py.to_double()), 10U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 11U)
assert_true(comp.wm.drag is None) // the press went to the client, not to a Chandra title bar
let mv = @wayland.WireWriter::new()
mv.u32(0U) // seat
mv.u32(1U) // serial
c.send(mv.finish_msg(tl, 5U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_true(comp.wm.drag is Some(_))
comp.handle_input(@nested.InputEvent::PointerMove((px + 60).to_double(), (py + 40).to_double()), 12U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 13U)
let after = comp.windows.get(1UL).unwrap().geometry
assert_eq((after.x - before.x, after.y - before.y), (60, 40))
assert_eq((after.w, after.h), (before.w, before.h))
// xdg_toplevel.resize from the bottom-right corner (edges 10).
let r = comp.windows.get(1UL).unwrap().client_rect()
let (px, py) = (r.x + r.w - 2, r.y + r.h - 2)
comp.handle_input(@nested.InputEvent::PointerMove(px.to_double(), py.to_double()), 20U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 21U)
let rs = @wayland.WireWriter::new()
rs.u32(0U)
rs.u32(2U)
rs.u32(10U)
c.send(rs.finish_msg(tl, 6U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
if comp.wm.drag is Some(_) {
comp.handle_input(@nested.InputEvent::PointerMove((px + 30).to_double(), (py + 20).to_double()), 22U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 23U)
let resized = comp.windows.get(1UL).unwrap().geometry
assert_eq((resized.w - after.w, resized.h - after.h), (30, 20))
} else {
// The press landed in one of Chandra's own resize zones instead: also a resize.
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 23U)
}
}
///|
test "Chrome asking to draw its own frame gets it, instead of a second one around it" {
// Chrome with "Use system title bar and borders" off (and its installed web apps) asks for
// client-side decoration and keeps drawing its own title bar and shadow even when told
// server-side: Chandra's title bar showed above Chrome's, with Chrome's shadow as a gap.
let socket_path = "/tmp/wayland-moon-csd-chrome-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, deco) = open_and_configure_client(comp, socket_path, "YouTube", "chrome-youtube")
assert_eq(c.deco_mode, Some(2U))
// zxdg_toplevel_decoration_v1.set_mode(client_side)
let m = @wayland.WireWriter::new()
m.u32(1U)
c.send(m.finish_msg(deco, 1U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
c.read_events(xdg, tl, deco)
@sys.sleep_ms(2)
}
assert_eq(c.deco_mode, Some(1U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let win = comp.windows.get(1UL).unwrap()
assert_true(win.csd)
let r = win.client_rect()
assert_eq((r.w, r.h), (win.geometry.w + 2, win.geometry.h + 39))
// unset_mode: back to Chandra's frame.
c.send(@wayland.WireWriter::new().finish_msg(deco, 2U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
c.read_events(xdg, tl, deco)
@sys.sleep_ms(2)
}
assert_eq(c.deco_mode, Some(2U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
assert_false(comp.windows.get(1UL).unwrap().csd)
}
///|
test "a GTK 3 window asking through org_kde_kwin_server_decoration gets Chandra's frame" {
// GTK 3 has no xdg-decoration support: without this protocol every GTK 3 window (Webby) drew
// its own fallback title bar, with only a close button, inside a black border.
let socket_path = "/tmp/wayland-moon-csd-kde-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, _) = open_and_configure_client(
comp,
socket_path,
"Webby",
"webby",
server_deco=false,
)
assert_true(c.globals.contains("org_kde_kwin_server_decoration_manager"))
let mgr = bind_global(c, 2U, "org_kde_kwin_server_decoration_manager", 1U)
// create(id, surface), then request_mode(Server) as gdk_wayland_window_announce_ssd does.
let deco = c.alloc_id()
let cr = @wayland.WireWriter::new()
cr.u32(deco)
cr.u32(surf)
c.send(cr.finish_msg(mgr, 0U))
let rm = @wayland.WireWriter::new()
rm.u32(2U)
c.send(rm.finish_msg(deco, 1U))
let size_for = fn(w, h, _max) { (w, h) }
ignore(answer_configures(comp, c, shm, surf, xdg, tl, size_for))
let win = comp.windows.get(1UL).unwrap()
assert_false(win.csd)
let r = win.client_rect()
assert_eq((r.x, r.y, r.w, r.h), (win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h))
// request_mode(Client): a GTK 3 app with its own header bar keeps drawing its frame.
let rc = @wayland.WireWriter::new()
rc.u32(1U)
c.send(rc.finish_msg(deco, 1U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
assert_true(comp.windows.get(1UL).unwrap().csd)
}
///|
test "a GTK window resizes from its shadow and from every edge, top included" {
// Gajim (GTK 4) couldn't be resized: GTK only resizes from a press in its shadow, which
// Chandra keeps, and Chandra's own zones were a few pixels wide with none on the top edge.
let (comp, c, shm, surf, xdg, tl) = csd_setup("grips")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let drag = fn(px : Int, py : Int, dx : Int, dy : Int) -> @scene.Rect raise {
comp.handle_input(@nested.InputEvent::PointerMove(px.to_double(), py.to_double()), 30U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 31U)
assert_true(comp.wm.drag is Some(_))
comp.handle_input(@nested.InputEvent::PointerMove((px + dx).to_double(), (py + dy).to_double()), 32U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 33U)
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
comp.windows.get(1UL).unwrap().client_rect()
}
// 10px out into the shadow on the right: wider.
let r0 = comp.windows.get(1UL).unwrap().client_rect()
let r1 = drag(r0.x + r0.w + 10, r0.y + r0.h / 2, 30, 0)
assert_eq((r1.x, r1.y, r1.w, r1.h), (r0.x, r0.y, r0.w + 30, r0.h))
// The top edge (the header bar's top 2px): taller, growing upwards.
let r2 = drag(r1.x + r1.w / 2, r1.y + 1, 0, -20)
assert_eq((r2.x, r2.y, r2.w, r2.h), (r1.x, r1.y - 20, r1.w, r1.h + 20))
// 8px out past the bottom-left corner: both at once.
let r3 = drag(r2.x - 8, r2.y + r2.h + 8, -10, 10)
assert_eq((r3.x, r3.y, r3.w, r3.h), (r2.x - 10, r2.y, r2.w + 10, r2.h + 10))
// Further out than GTK's 12px handles is the desktop again, and the header bar itself
// (below its top edge) still goes to the client.
comp.handle_input(@nested.InputEvent::PointerMove((r3.x + r3.w + 20).to_double(), (r3.y + 100).to_double()), 40U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 41U)
assert_true(comp.wm.drag is None)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 42U)
comp.handle_input(@nested.InputEvent::PointerMove((r3.x + r3.w / 2).to_double(), (r3.y + 15).to_double()), 43U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 44U)
assert_true(comp.wm.drag is None)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 45U)
}
///|
test "a maximized GTK window has no resize zones, and restores to its size" {
let (comp, c, shm, surf, xdg, tl) = csd_setup("max")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let before = comp.windows.get(1UL).unwrap().client_rect()
// The header bar's maximize button: xdg_toplevel.set_maximized.
c.send(@wayland.WireWriter::new().finish_msg(tl, 9U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let win = comp.windows.get(1UL).unwrap()
assert_true(win.maximized)
let m = win.client_rect()
assert_true(m.w > before.w && m.h > before.h)
comp.handle_input(@nested.InputEvent::PointerMove((m.x + m.w - 1).to_double(), (m.y + m.h / 2).to_double()), 50U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 51U)
assert_true(comp.wm.drag is None)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 52U)
// ... and unset_maximized puts it back.
c.send(@wayland.WireWriter::new().finish_msg(tl, 10U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let after = comp.windows.get(1UL).unwrap().client_rect()
assert_false(comp.windows.get(1UL).unwrap().maximized)
assert_eq((after.w, after.h), (before.w, before.h))
}
///| A solid 0xBBGGRR buffer of `w` x `h` in its own pool.
fn solid_buffer(c : TestWaylandClient, shm : UInt, w : Int, h : Int, b : Byte, g : Byte, r : Byte) -> UInt {
let fd = solid_memfd(w, h, b, g, r)
let pool = c.alloc_id()
let p = @wayland.WireWriter::new()
p.u32(pool)
p.i32(w * h * 4)
c.send(p.finish_msg(shm, 0U), send_fd=fd)
@sys.close_fd(fd)
let buf = c.alloc_id()
let cb = @wayland.WireWriter::new()
cb.u32(buf)
cb.i32(0)
cb.i32(w)
cb.i32(h)
cb.i32(w * 4)
cb.u32(0U)
c.send(cb.finish_msg(pool, 0U))
buf
}
///|
test "a subsurface (Chrome's toolbar bubbles) is drawn over its window and gets the pointer" {
// Chrome draws the bookmark-star, downloads and extensions bubbles as wl_subsurfaces of the
// window. Chandra accepted them but never drew them or sent them input: the buttons seemed
// to do nothing.
let socket_path = "/tmp/wayland-moon-csd-sub-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, _) = open_and_configure_client(comp, socket_path, "Chrome", "chromium")
ignore(answer_configures(comp, c, shm, surf, xdg, tl, fn(w, h, _m) { (w, h) }))
// wl_registry is object 2; wl_compositor and wl_subcompositor by name.
let comp_id = bind_global(c, 2U, "wl_compositor", 5U)
let subc = bind_global(c, 2U, "wl_subcompositor", 1U)
let child = c.alloc_id()
let cs = @wayland.WireWriter::new()
cs.u32(child)
c.send(cs.finish_msg(comp_id, 0U))
let sub = c.alloc_id()
let gs = @wayland.WireWriter::new()
gs.u32(sub)
gs.u32(child)
gs.u32(surf)
c.send(gs.finish_msg(subc, 1U))
let sp = @wayland.WireWriter::new()
sp.i32(100)
sp.i32(50)
c.send(sp.finish_msg(sub, 1U)) // set_position
let buf = solid_buffer(c, shm, 40, 30, b'\xff', b'\x00', b'\x00')
attach_commit(comp, c, child, Some(buf))
let win = comp.windows.get(1UL).unwrap()
assert_eq(win.subs.length(), 1)
let (ox, oy) = win.surface_origin()
let blue = Some(@scene.Rgba::new(0, 0, 0xff, 0xff))
assert_eq(comp.compose_frame().pixel(ox + 110, oy + 60), blue)
assert_true(comp.compose_frame().pixel(ox + 90, oy + 60) != blue)
// The pointer over it enters the subsurface, at its own coordinates.
comp.handle_input(@nested.InputEvent::PointerMove((ox + 110).to_double(), (oy + 60).to_double()), 10U)
assert_eq(comp.ptr_win, Some(1UL))
assert_eq(comp.ptr_sub, child)
comp.handle_input(@nested.InputEvent::PointerMove((ox + 90).to_double(), (oy + 60).to_double()), 11U)
assert_eq(comp.ptr_sub, 0U)
// A null buffer unmaps it.
let a = @wayland.WireWriter::new()
a.u32(0U)
a.i32(0)
a.i32(0)
c.send(a.finish_msg(child, 1U))
attach_commit(comp, c, child, None)
assert_eq(comp.windows.get(1UL).unwrap().subs.length(), 0)
assert_true(comp.compose_frame().pixel(ox + 110, oy + 60) != blue)
}
///|
test "chandra-ctl windows says how a GTK window is framed and what the pointer is over" {
let (comp, c, shm, surf, xdg, tl) = csd_setup("report")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let r = comp.windows.get(1UL).unwrap().client_rect()
comp.handle_input(@nested.InputEvent::PointerMove((r.x + r.w + 5).to_double(), (r.y + r.h / 2).to_double()), 60U)
let (lines, ok) = comp.control_command("windows")
assert_true(ok)
assert_true(lines[0].has_prefix("1 org.gnome.Nautilus client-frame floating "))
assert_true(lines[0].contains(" shadow=24,24,24,24 "))
assert_true(lines.contains("pointer " + (r.x + r.w + 5).to_string() + "," + (r.y + r.h / 2).to_string() + " over resize grip right of 1"))
assert_eq(lines[lines.length() - 1], "ok 1 windows")
}