From aa92aaac6f4283a8a91225ecd3bb644fcc6ab977 Mon Sep 17 00:00:00 2001 From: nandithebull Date: Sat, 28 Nov 2026 23:33:17 -0800 Subject: [PATCH] Text input: don't send leave for destroyed client surfaces - Drop text-input and keyboard focus references when a client destroys a wl_surface, without sending leave for it - Skip wl_keyboard.leave and zwp_text_input_v3.leave/enter for surfaces the client no longer has - Skip pointer enter/leave for missing subsurfaces or toplevels - Add a regression test: closing a second toplevel (e.g. OAuth popup) must return focus to the main window without leaving stale events --- compositor/host/text_input_destroy_test.mbt | 72 +++++++++++++++++++++ compositor/wayland/server.mbt | 40 ++++++++---- compositor/wayland/text_input.mbt | 32 +++++++-- 3 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 compositor/host/text_input_destroy_test.mbt diff --git a/compositor/host/text_input_destroy_test.mbt b/compositor/host/text_input_destroy_test.mbt new file mode 100644 index 0000000..0bfde2f --- /dev/null +++ b/compositor/host/text_input_destroy_test.mbt @@ -0,0 +1,72 @@ +///| Destroying a client's second wl_surface (e.g. an OAuth popup closing) must not send +///| zwp_text_input_v3.leave or wl_keyboard.leave referencing the destroyed surface when focus +///| returns to the client's remaining window. + +///| +test "destroying a second toplevel surface on the same client never sends leave for the gone surface" { + let socket_path = "/tmp/wayland-moon-ti-destroy-" + @sys.getpid().to_string() + ".sock" + let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + let (a, shm_a, surf_a, xdg_a, _, _) = open_and_configure_client(comp, socket_path, "Main", "main") + commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL) + assert_eq(comp.kbd_win, Some(1UL)) + let (ti, kbd) = text_input_for(comp, a) + ignore(drain_args(a)) + + // Open a second toplevel on the same client (simulating an OAuth popup). + let comp_b = bind_global(a, 2U, "wl_compositor", 5U) + let xdg_wm_b = bind_global(a, 2U, "xdg_wm_base", 5U) + let surf_b = a.alloc_id() + let w_cs = @wayland.WireWriter::new() + w_cs.u32(surf_b) + a.send(w_cs.finish_msg(comp_b, 0U)) + let xdg_b = a.alloc_id() + let w_xs = @wayland.WireWriter::new() + w_xs.u32(xdg_b) + w_xs.u32(surf_b) + a.send(w_xs.finish_msg(xdg_wm_b, 2U)) + let tl_b = a.alloc_id() + let w_tl = @wayland.WireWriter::new() + w_tl.u32(tl_b) + a.send(w_tl.finish_msg(xdg_b, 1U)) + let w_t = @wayland.WireWriter::new() + w_t.str("Popup") + a.send(w_t.finish_msg(tl_b, 2U)) + let w_a = @wayland.WireWriter::new() + w_a.str("popup") + a.send(w_a.finish_msg(tl_b, 3U)) + a.configure_serial = None + a.configured_w = 0 + a.configured_h = 0 + a.send(@wayland.WireWriter::new().finish_msg(surf_b, 6U)) + for _i = 0; _i < 20; _i = _i + 1 { + comp.step() + a.read_events(xdg_b, tl_b, 0U) + if a.configure_serial is Some(_) && a.configured_w > 0 { + break + } + @sys.sleep_ms(2) + } + commit_shm_frame(comp, a, shm_a, surf_b, xdg_b, 2UL) + assert_eq(comp.kbd_win, Some(2UL)) + text_input_state(comp, a, ti, true, 10, 10, 16) + ignore(drain_args(a)) + + // Destroy the popup toplevel + xdg_surface + wl_surface together, as Electron/Ozone does on close. + a.send(@wayland.WireWriter::new().finish_msg(tl_b, 0U)) + a.send(@wayland.WireWriter::new().finish_msg(xdg_b, 0U)) + a.send(@wayland.WireWriter::new().finish_msg(surf_b, 0U)) + for _i = 0; _i < 5; _i = _i + 1 { + comp.step() + @sys.sleep_ms(2) + } + + assert_eq(comp.kbd_win, Some(1UL)) + let msgs = drain_args(a) + let ti_leaves_gone = msgs.filter(m => m.0 == ti && m.1 == 1U && m.2.length() > 0 && m.2[0] == surf_b) + let kbd_leaves_gone = msgs.filter(m => m.0 == kbd && m.1 == 2U && m.2.length() > 1 && m.2[1] == surf_b) + let ti_enters_main = msgs.filter(m => m.0 == ti && m.1 == 0U && m.2.length() > 0 && m.2[0] == surf_a) + assert_eq(ti_leaves_gone.length(), 0) + assert_eq(kbd_leaves_gone.length(), 0) + assert_true(ti_enters_main.length() >= 1) + comp.close() +} diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index bca3bb5..3235895 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -737,6 +737,10 @@ fn WaylandServer::dispatch_msg( } } self.dnd_icon_gone(c, sid, events) + text_inputs_surface_gone(c, sid) + if self.kbd_surface == Some((c.client_id, sid)) { + self.kbd_surface = None + } c.surfaces.remove(sid) c.objects.remove(sid) send_delete_id(c, sid) @@ -1974,16 +1978,21 @@ fn fixed(v : Double) -> Int { pub fn WaylandServer::pointer_enter(self : WaylandServer, win_id : UInt64, x : Double, y : Double, sub? : UInt = 0U) -> Unit { match self.win_client(win_id) { None => () - Some((c, tl)) => + Some((c, tl)) => { + let target = if sub != 0U && c.surfaces.contains(sub) { sub } else { tl.wl_surface_id } + if !c.surfaces.contains(target) { + return + } for pid in c.pointer_ids { let w = WireWriter::new() w.u32(self.next_serial()) - w.u32(if sub != 0U && c.surfaces.contains(sub) { sub } else { tl.wl_surface_id }) + w.u32(target) w.i32(fixed(x)) w.i32(fixed(y)) c.send_msg(w.finish_msg(pid, 0U)) self.pointer_frame(c, pid) } + } } } @@ -1991,17 +2000,19 @@ pub fn WaylandServer::pointer_enter(self : WaylandServer, win_id : UInt64, x : D pub fn WaylandServer::pointer_leave(self : WaylandServer, win_id : UInt64, sub? : UInt = 0U) -> Unit { match self.win_client(win_id) { None => () - Some((c, tl)) => + Some((c, tl)) => { + let target = if sub != 0U { sub } else { tl.wl_surface_id } + if !c.surfaces.contains(target) { + return + } for pid in c.pointer_ids { - if sub != 0U && !c.surfaces.contains(sub) { - continue // the subsurface is gone: nothing to leave - } let w = WireWriter::new() w.u32(self.next_serial()) - w.u32(if sub != 0U { sub } else { tl.wl_surface_id }) + w.u32(target) c.send_msg(w.finish_msg(pid, 1U)) self.pointer_frame(c, pid) } + } } } @@ -2089,6 +2100,9 @@ pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> U match self.win_client(win_id) { None => () Some((c, tl)) => { + if !c.surfaces.contains(tl.wl_surface_id) { + return + } self.kbd_client = Some(c.client_id) self.kbd_surface = Some((c.client_id, tl.wl_surface_id)) self.send_selection(c) @@ -2110,11 +2124,13 @@ pub fn WaylandServer::keyboard_leave(self : WaylandServer, win_id : UInt64) -> U match self.win_client(win_id) { None => () Some((c, tl)) => { - for kid in c.keyboard_ids { - let w = WireWriter::new() - w.u32(self.next_serial()) - w.u32(tl.wl_surface_id) - c.send_msg(w.finish_msg(kid, 2U)) + if c.surfaces.contains(tl.wl_surface_id) { + for kid in c.keyboard_ids { + let w = WireWriter::new() + w.u32(self.next_serial()) + w.u32(tl.wl_surface_id) + c.send_msg(w.finish_msg(kid, 2U)) + } } text_inputs_leave(c, tl.wl_surface_id) if self.kbd_surface == Some((c.client_id, tl.wl_surface_id)) { diff --git a/compositor/wayland/text_input.mbt b/compositor/wayland/text_input.mbt index a9d8e0e..2f86c2e 100644 --- a/compositor/wayland/text_input.mbt +++ b/compositor/wayland/text_input.mbt @@ -113,11 +113,21 @@ fn send_text_input_surface(c : ClientConn, id : UInt, opcode : UInt, surface : U fn text_inputs_enter(c : ClientConn, surface : UInt) -> Unit { for id, ti in c.text_inputs { if ti.surface != 0U && ti.surface != surface { - send_text_input_surface(c, id, 1U, ti.surface) + if c.surfaces.contains(ti.surface) { + send_text_input_surface(c, id, 1U, ti.surface) + } + ti.enabled = false + ti.cursor = None } if ti.surface != surface { - ti.surface = surface - send_text_input_surface(c, id, 0U, surface) + if c.surfaces.contains(surface) { + ti.surface = surface + send_text_input_surface(c, id, 0U, surface) + } else { + ti.surface = 0U + ti.enabled = false + ti.cursor = None + } } } } @@ -129,7 +139,21 @@ fn text_inputs_leave(c : ClientConn, surface : UInt) -> Unit { ti.surface = 0U // The client disables it in answer; until it commits that, it can't take text anyway. ti.enabled = false - send_text_input_surface(c, id, 1U, surface) + ti.cursor = None + if c.surfaces.contains(surface) { + send_text_input_surface(c, id, 1U, surface) + } + } + } +} + +///| A wl_surface was destroyed by the client: drop text-input references without sending `leave`. +fn text_inputs_surface_gone(c : ClientConn, surface : UInt) -> Unit { + for _, ti in c.text_inputs { + if ti.surface == surface { + ti.surface = 0U + ti.enabled = false + ti.cursor = None } } }