diff --git a/README.md b/README.md index afb1336..bf5ff08 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,8 @@ just client # start foot inside it (or: just client konsol session), `--theme PATH`, `--no-restore`, `--profile`, `--size WxH` and `--gfx-out frame.png` (render to a PNG, headless; add `--serve` to keep refreshing it). `WASMHOST_PROFILE=1` prints the time spent in each host function every 5 seconds; `WASMHOST_TRACE=1` logs every host call. +`CHANDRA_DEBUG_CONFIGURE=1` (any host) logs each window's configures, acks and committed buffer +sizes, for an app that draws at the wrong size. The installed session logs to `~/.local/state/chandra/session.log`. Start it as yourself, never as root: `chandra-session` refuses to run as root (`CHANDRA_ALLOW_ROOT=1` overrides). Installing diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 528b8ea..83ae310 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -472,6 +472,17 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit { } None => () } + @wayland.CompositorEvent::ToplevelSizeChosen(wid, w, h) => + // The client keeps its own size after being asked twice. A floating window is the + // client's to size: the frame follows it instead of padding the buffer out. A tiled, + // maximized or fullscreen window keeps the compositor's size. + match self.wm.windows.get(wid) { + Some(r) => + if r.is_floating && !r.maximized && !r.fullscreen && self.wm.drag is None && self.split_drag is None { + self.apply_actions(self.wm.handle_request(wid, @wm.WindowRequest::ResizeTo(w, h), self.work_area)) + } + None => () + } @wayland.CompositorEvent::PopupOpened(wid, parent, rx, ry, w, h) => { // Keyboard focus stays on the parent until the popup grabs and maps: moving it away // early makes clients (Chrome, Qt) take the keyboard leave as "dismiss the menu". diff --git a/compositor/host/configure_test.mbt b/compositor/host/configure_test.mbt new file mode 100644 index 0000000..5d5d90d --- /dev/null +++ b/compositor/host/configure_test.mbt @@ -0,0 +1,167 @@ +///| Clients that answer a configure with another size than the one asked for. + +///| What a GPUI app (Zed, Delta) does with a configure (gpui's wayland window.rs): leaving the +/// maximized state, it goes back to the size it had before, whatever size the configure asks +/// for; otherwise it takes the configure's size and remembers it as its floating size. +struct GpuiModel { + mut maximized : Bool + mut bounds_w : Int + mut bounds_h : Int +} + +///| +fn GpuiModel::respond(self : GpuiModel, w : Int, h : Int, max : Bool) -> (Int, Int) { + let got_unmaximized = self.maximized && !max + self.maximized = max + if max { + (w, h) + } else if got_unmaximized { + (self.bounds_w, self.bounds_h) + } else { + self.bounds_w = w + self.bounds_h = h + (w, h) + } +} + +///| +fn pump_client(comp : @host.ChandraCompositor, c : TestWaylandClient, xdg : UInt, tl : UInt) -> Unit { + for _i = 0; _i < 6; _i = _i + 1 { + comp.step() + c.read_events(xdg, tl, 0U) + @sys.sleep_ms(2) + } +} + +///| Ack every configure received since the last call, the way `size_for` decides, then draw +/// one solid frame of the last size. Returns that size (or None when nothing was configured). +fn answer_configures( + comp : @host.ChandraCompositor, + c : TestWaylandClient, + shm : UInt, + surf : UInt, + xdg : UInt, + tl : UInt, + size_for : (Int, Int, Bool) -> (Int, Int), +) -> (Int, Int)? { + pump_client(comp, c, xdg, tl) + if c.configures.length() == 0 { + return None + } + let mut size = (0, 0) + for cfg in c.configures { + let (w, h, max, serial) = cfg + size = size_for(w, h, max) + let ack = @wayland.WireWriter::new() + ack.u32(serial) + c.send(ack.finish_msg(xdg, 4U)) + } + c.configures.clear() + let (w, h) = size + let fd = solid_memfd(w, h, b'\x80', b'\x80', b'\x80') + 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)) + attach_commit(comp, c, surf, Some(buf)) + Some(size) +} + +///| +fn toplevel_request(comp : @host.ChandraCompositor, c : TestWaylandClient, tl : UInt, opcode : UInt) -> Unit { + c.send(@wayland.WireWriter::new().finish_msg(tl, opcode)) + for _i = 0; _i < 3; _i = _i + 1 { + comp.step() + @sys.sleep_ms(2) + } +} + +///| +fn drawn_size(comp : @host.ChandraCompositor, wid : UInt64) -> (Int, Int) { + match comp.windows.get(wid) { + Some({ surface: Some(s), .. }) => (s.width, s.height) + _ => (0, 0) + } +} + +///| +fn frame_size(comp : @host.ChandraCompositor, wid : UInt64) -> (Int, Int) { + let g = comp.windows.get(wid).unwrap().geometry + (g.w, g.h) +} + +///| +test "a GPUI app leaving the maximized state for a tiled slot draws the whole window" { + // Delta showed its sidebar and toolbar in a 640x480 corner of a full-screen frame: unmaximized + // into a tiled slot, GPUI went back to its old floating size, and nothing asked it again. + let socket_path = "/tmp/wayland-moon-gpui-unmax-" + @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, "Delta", "dev.delta.Delta") + let gpui : GpuiModel = { maximized: false, bounds_w: 0, bounds_h: 0 } + let size_for = fn(w, h, max) { gpui.respond(w, h, max) } + ignore(answer_configures(comp, c, shm, surf, xdg, tl, size_for)) + assert_eq(drawn_size(comp, 1UL), (640, 480)) + assert_eq(frame_size(comp, 1UL), (640, 480)) + + // Maximize, tile the desktop, then leave the maximized state: the window keeps its tiled slot. + toplevel_request(comp, c, tl, 9U) // set_maximized + ignore(answer_configures(comp, c, shm, surf, xdg, tl, size_for)) + comp.set_layout_mode(@wm.LayoutMode::Tiling) + ignore(answer_configures(comp, c, shm, surf, xdg, tl, size_for)) + toplevel_request(comp, c, tl, 10U) // unset_maximized + assert_eq(answer_configures(comp, c, shm, surf, xdg, tl, size_for), Some((640, 480))) + // Chandra asks again with the same configure; out of the maximized state, GPUI takes it. + let again = answer_configures(comp, c, shm, surf, xdg, tl, size_for) + let slot = frame_size(comp, 1UL) + assert_true(slot.0 > 1000 && slot.1 > 500) + assert_eq(again, Some(slot)) + assert_eq(drawn_size(comp, 1UL), slot) + // ...and then leaves it alone: no configure loop. + assert_eq(answer_configures(comp, c, shm, surf, xdg, tl, size_for), None) + @sys.close_fd(c.fd) + comp.close() +} + +///| +test "a floating window that keeps its own size gets a frame of that size" { + let socket_path = "/tmp/wayland-moon-own-size-" + @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, "Fixed", "org.example.fixed") + let fixed = fn(_w, _h, _max) { (500, 400) } + ignore(answer_configures(comp, c, shm, surf, xdg, tl, fixed)) // asked 640x480, drew 500x400 + ignore(answer_configures(comp, c, shm, surf, xdg, tl, fixed)) // asked again, same answer + ignore(answer_configures(comp, c, shm, surf, xdg, tl, fixed)) // the frame follows: 500x400 + assert_eq(frame_size(comp, 1UL), (500, 400)) + assert_eq(drawn_size(comp, 1UL), (500, 400)) + assert_eq(answer_configures(comp, c, shm, surf, xdg, tl, fixed), None) + @sys.close_fd(c.fd) + comp.close() +} + +///| +test "a tiled window that keeps its own size is asked once more, then left alone" { + let socket_path = "/tmp/wayland-moon-tiled-own-" + @sys.getpid().to_string() + ".sock" + let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + let (c, shm, surf, xdg, tl, _deco) = open_and_configure_client(comp, socket_path, "Fixed", "org.example.fixed") + let slot = frame_size(comp, 1UL) + let fixed = fn(_w, _h, _max) { (500, 400) } + ignore(answer_configures(comp, c, shm, surf, xdg, tl, fixed)) + ignore(answer_configures(comp, c, shm, surf, xdg, tl, fixed)) + assert_eq(answer_configures(comp, c, shm, surf, xdg, tl, fixed), None) + assert_eq(frame_size(comp, 1UL), slot) + @sys.close_fd(c.fd) + comp.close() +} diff --git a/compositor/host/e2e_test.mbt b/compositor/host/e2e_test.mbt index 070d304..000f61b 100644 --- a/compositor/host/e2e_test.mbt +++ b/compositor/host/e2e_test.mbt @@ -13,6 +13,10 @@ struct TestWaylandClient { mut configured_w : Int mut configured_h : Int mut configure_serial : UInt? + /// The last xdg_toplevel.configure had the maximized state. + mut configured_max : Bool + /// Every configure received, in order, as (width, height, maximized, serial). + configures : Array[(Int, Int, Bool, UInt)] } ///| @@ -30,6 +34,8 @@ fn TestWaylandClient::connect(socket_path : String) -> TestWaylandClient? { configured_w: 0, configured_h: 0, configure_serial: None, + configured_max: false, + configures: [], }) } @@ -92,14 +98,22 @@ fn TestWaylandClient::read_events( // xdg_toplevel.configure(width, height, states) let w = reader.i32() let h = reader.i32() - let _states = reader.array() + let states = reader.array() + self.configured_max = false + for i = 0; i + 3 < states.length(); i = i + 4 { + if states[i] == b'\x01' { + self.configured_max = true + } + } if w > 0 && h > 0 { self.configured_w = w self.configured_h = h } } else if obj_id == xdg_surf_id && opcode == 0U { // xdg_surface.configure(serial) - self.configure_serial = Some(reader.u32()) + let serial = reader.u32() + self.configure_serial = Some(serial) + self.configures.push((self.configured_w, self.configured_h, self.configured_max, serial)) } off = off + size } diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index 8bbc030..59e0523 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -5,6 +5,10 @@ pub(all) enum CompositorEvent { ToplevelOpened(UInt64, String, String, Int, Int) ToplevelUpdated(UInt64, String, String) ToplevelCommitted(UInt64, @scene.ShmBufferSnapshot?) + /// The client acked the compositor's last configure, was asked again, and still drew a + /// different size: (id, w, h) is the size it chose. A floating window takes it (xdg-shell lets + /// a client pick its own size when it is not maximized, fullscreen or tiled). + ToplevelSizeChosen(UInt64, Int, Int) ToplevelClosed(UInt64) ToplevelMaximizeRequest(UInt64, Bool) ToplevelFullscreenRequest(UInt64, Bool) @@ -85,6 +89,8 @@ struct SurfaceState { mut pending_frame_cb : UInt? mut xdg_surface_id : UInt? mut window_id : UInt64? + /// wl_surface.set_buffer_scale: the buffer is this many times the window's size. + mut buffer_scale : Int } ///| @@ -94,6 +100,9 @@ struct XdgSurfaceState { /// set_window_geometry origin: where the visible window starts inside the buffer (shadows). mut geo_x : Int mut geo_y : Int + /// set_window_geometry size (0 until the client sets one: the window is the whole buffer). + mut geo_w : Int + mut geo_h : Int } ///| @@ -112,6 +121,16 @@ struct ToplevelState { mut has_parent : Bool /// Set for xdg_popup surfaces (they live in `toplevels` so input routing is shared). popup_parent : UInt64? + /// The last xdg_toplevel.configure sent: size, states and its xdg_surface serial (0 = none). + mut cfg_w : Int + mut cfg_h : Int + mut cfg_states : Array[Byte] + mut cfg_serial : UInt + /// The serial of the last configure the client acked. + mut acked_serial : UInt + /// How far the last configure got with a client drawing another size after acking it: + /// 0 not yet, 1 sent again, 2 reported as ToplevelSizeChosen. + mut cfg_retry : Int } ///| xdg_positioner state. @@ -261,6 +280,8 @@ pub(all) struct WaylandServer { mut own_data : Bytes /// Clipboard pastes of `own_data` still being written: (pipe fd, bytes written, give up at ms). transfers : Array[(Int, Int, UInt64)] + /// CHANDRA_DEBUG_CONFIGURE=1: log each configure, ack and committed buffer size to stderr. + mut debug_configure : Bool } ///| @@ -290,6 +311,7 @@ pub fn WaylandServer::listen( own_mimes: [], own_data: Bytes::from_array([]), transfers: [], + debug_configure: @sys.getenv("CHANDRA_DEBUG_CONFIGURE").length() > 0, }) } @@ -555,6 +577,7 @@ fn WaylandServer::dispatch_msg( pending_frame_cb: None, xdg_surface_id: None, window_id: None, + buffer_scale: 1, }) } else if opcode == 1U { let rid = reader.u32() @@ -583,6 +606,10 @@ fn WaylandServer::dispatch_msg( // frame(callback) let cb_id = reader.u32() surf.pending_frame_cb = Some(cb_id) + } else if opcode == 8U { + // set_buffer_scale(scale) + let sc = reader.i32() + surf.buffer_scale = if sc > 0 { sc } else { 1 } } else if opcode == 6U { // commit match surf.window_id { @@ -612,6 +639,10 @@ fn WaylandServer::dispatch_msg( None => None } events.push(ToplevelCommitted(wid, snap)) + match snap { + Some(sn) => self.check_committed_size(c, tl, surf, sn, events) + None => () + } } None => () } @@ -854,7 +885,7 @@ fn WaylandServer::dispatch_msg( let xsid = reader.u32() let wl_sid = reader.u32() c.objects.set(xsid, ObjXdgSurface(xsid)) - c.xdg_surfaces.set(xsid, { wl_surface_id: wl_sid, window_id: None, geo_x: 0, geo_y: 0 }) + c.xdg_surfaces.set(xsid, { wl_surface_id: wl_sid, window_id: None, geo_x: 0, geo_y: 0, geo_w: 0, geo_h: 0 }) match c.surfaces.get(wl_sid) { Some(s) => s.xdg_surface_id = Some(xsid) None => () @@ -865,14 +896,34 @@ fn WaylandServer::dispatch_msg( c.xdg_surfaces.remove(xsid) c.objects.remove(xsid) send_delete_id(c, xsid) + } else if opcode == 4U { + // ack_configure(serial) + let serial = reader.u32() + match c.xdg_surfaces.get(xsid) { + Some({ window_id: Some(wid), .. }) => + match c.toplevels.get(wid) { + Some(tl) => { + tl.acked_serial = serial + if self.debug_configure { + @sys.log("chandra-wl: window \{wid} ack_configure \{serial}") + } + } + None => () + } + _ => () + } } else if opcode == 3U { // set_window_geometry(x, y, w, h) let gx = reader.i32() let gy = reader.i32() + let gw = reader.i32() + let gh = reader.i32() match c.xdg_surfaces.get(xsid) { Some(xs) => { xs.geo_x = gx xs.geo_y = gy + xs.geo_w = gw + xs.geo_h = gh match xs.window_id { Some(wid) => match c.toplevels.get(wid) { @@ -914,6 +965,12 @@ fn WaylandServer::dispatch_msg( opened_sent: false, has_parent: false, popup_parent: None, + cfg_w: 0, + cfg_h: 0, + cfg_states: [], + cfg_serial: 0U, + acked_serial: 0U, + cfg_retry: 0, }) self.win_to_client.set(wid, c.client_id) } else if opcode == 2U { @@ -960,6 +1017,12 @@ fn WaylandServer::dispatch_msg( opened_sent: true, has_parent: true, popup_parent: Some(parent_wid), + cfg_w: 0, + cfg_h: 0, + cfg_states: [], + cfg_serial: 0U, + acked_serial: 0U, + cfg_retry: 0, }) self.win_to_client.set(wid, c.client_id) // xdg_popup.configure(x, y, w, h) then xdg_surface.configure(serial) @@ -1312,16 +1375,111 @@ pub fn WaylandServer::send_toplevel_configure( if activated { push_state(4U) } + tl.cfg_w = width + tl.cfg_h = height + tl.cfg_states = states + tl.cfg_retry = 0 + self.write_configure(c, tl) +} +///| Write the toplevel's current configure (cfg_w, cfg_h, cfg_states) with a new serial. +fn WaylandServer::write_configure( + self : WaylandServer, + c : ClientConn, + tl : ToplevelState, +) -> Unit { let w1 = WireWriter::new() - w1.i32(width) - w1.i32(height) - w1.array(states) + w1.i32(tl.cfg_w) + w1.i32(tl.cfg_h) + w1.array(tl.cfg_states) c.send_msg(w1.finish_msg(tl.wl_toplevel_id, 0U)) + let serial = self.next_serial() + tl.cfg_serial = serial let w2 = WireWriter::new() - w2.u32(self.next_serial()) + w2.u32(serial) c.send_msg(w2.finish_msg(tl.xdg_surface_id, 0U)) + if self.debug_configure { + let names : Array[String] = [] + for i = 0; i + 3 < tl.cfg_states.length(); i = i + 4 { + names.push( + match tl.cfg_states[i].to_int() { + 1 => "maximized" + 2 => "fullscreen" + 3 => "resizing" + 4 => "activated" + n => n.to_string() + }, + ) + } + @sys.log( + "chandra-wl: window \{tl.win_id} configure \{tl.cfg_w}x\{tl.cfg_h} [\{names.join(",")}] serial \{serial}" + + (if tl.cfg_retry > 0 { " (sent again)" } else { "" }), + ) + } +} + +///| After a commit with a buffer: compare the window's size with the configure the client acked. +/// +/// A client may answer a configure with another size. GPUI apps (Zed, Delta) do on purpose when +/// a configure takes the maximized state away: they go back to the size they had before being +/// maximized, even when the compositor asked for a tiled window's full size. The window then +/// showed the app in a small corner of a large frame. Asked once more with the same configure (the +/// client is no longer in the maximized state then), they take the size. A client that still +/// picks its own size gets a ToplevelSizeChosen, and the host keeps it for a floating window. +fn WaylandServer::check_committed_size( + self : WaylandServer, + c : ClientConn, + tl : ToplevelState, + surf : SurfaceState, + snap : @scene.ShmBufferSnapshot, + events : Array[CompositorEvent], +) -> Unit { + let (w, h) = committed_size(c, surf, snap) + if self.debug_configure { + @sys.log( + "chandra-wl: window \{tl.win_id} commit buffer \{snap.width}x\{snap.height} scale \{surf.buffer_scale} -> window \{w}x\{h} (configured \{tl.cfg_w}x\{tl.cfg_h}, acked \{tl.acked_serial}/\{tl.cfg_serial})", + ) + } + // Only once the client has acked the latest configure: before that, an old-sized frame is + // just the client catching up. + if tl.cfg_serial == 0U || tl.acked_serial != tl.cfg_serial || tl.cfg_w <= 0 || tl.cfg_h <= 0 { + return + } + if w == tl.cfg_w && h == tl.cfg_h { + return + } + if tl.cfg_retry == 0 { + tl.cfg_retry = 1 + self.write_configure(c, tl) + } else if tl.cfg_retry == 1 { + tl.cfg_retry = 2 + events.push(ToplevelSizeChosen(tl.win_id, w, h)) + } +} + +///| The window size a committed buffer stands for: the window geometry when the client set one +/// (a client drawing its own shadows), else the buffer divided by its scale. +fn committed_size( + c : ClientConn, + surf : SurfaceState, + snap : @scene.ShmBufferSnapshot, +) -> (Int, Int) { + let geo = match surf.xdg_surface_id { + Some(xsid) => + match c.xdg_surfaces.get(xsid) { + Some(xs) if xs.geo_w > 0 && xs.geo_h > 0 => Some((xs.geo_w, xs.geo_h)) + _ => None + } + None => None + } + match geo { + Some(g) => g + None => { + let sc = if surf.buffer_scale > 0 { surf.buffer_scale } else { 1 } + (snap.width / sc, snap.height / sc) + } + } } ///|