///| Client right-click menus (xdg_popup): keyboard focus, on-screen placement, reposition, ///| and client-drawn shadows (window geometry). ///| struct PopupRig { comp : @host.ChandraCompositor c : TestWaylandClient shm : UInt xdg : UInt comp_id : UInt wm_id : UInt ptr_id : UInt kbd_id : UInt win : @scene.Rect } ///| A compositor with one mapped window whose client has a pointer and a keyboard. fn popup_rig(tag : String) -> PopupRig raise { let socket_path = "/tmp/wayland-moon-cpopup-" + tag + "-" + @sys.getpid().to_string() + ".sock" let theme = @scene.ThemeTokens::moon_night() let comp = @host.ChandraCompositor::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 seat_id = bind_global(c, 2U, "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 kbd_id = c.alloc_id() let w2 = @wayland.WireWriter::new() w2.u32(kbd_id) c.send(w2.finish_msg(seat_id, 1U)) let comp_id = bind_global(c, 2U, "wl_compositor", 5U) let wm_id = bind_global(c, 2U, "xdg_wm_base", 5U) pump(comp, 5) let win = comp.windows.get(1UL).unwrap().geometry { comp, c, shm, xdg, comp_id, wm_id, ptr_id, kbd_id, win } } ///| fn pump(comp : @host.ChandraCompositor, n : Int) -> Unit { for _i = 0; _i < n; _i = _i + 1 { comp.step() @sys.sleep_ms(2) } } ///| A positioner: `w`x`h` popup, 1x1 anchor rect at (ax, ay) in the parent, anchor/gravity /// enums and constraint_adjustment bits. fn PopupRig::positioner( self : PopupRig, w : Int, h : Int, ax : Int, ay : Int, anchor : UInt, gravity : UInt, adjust : UInt, ) -> UInt { let c = self.c let pos_id = c.alloc_id() let a = @wayland.WireWriter::new() a.u32(pos_id) c.send(a.finish_msg(self.wm_id, 1U)) let b = @wayland.WireWriter::new() b.i32(w) b.i32(h) c.send(b.finish_msg(pos_id, 1U)) let d = @wayland.WireWriter::new() d.i32(ax) d.i32(ay) d.i32(1) d.i32(1) c.send(d.finish_msg(pos_id, 2U)) let e = @wayland.WireWriter::new() e.u32(anchor) c.send(e.finish_msg(pos_id, 3U)) let f = @wayland.WireWriter::new() f.u32(gravity) c.send(f.finish_msg(pos_id, 4U)) let g = @wayland.WireWriter::new() g.u32(adjust) c.send(g.finish_msg(pos_id, 5U)) pos_id } ///| Open a popup on the window from a positioner; returns (wl_surface, xdg_surface, xdg_popup). fn PopupRig::open_popup(self : PopupRig, pos_id : UInt, geometry? : (Int, Int, Int, Int)? = None) -> (UInt, UInt, UInt) { let c = self.c let psurf = c.alloc_id() let g = @wayland.WireWriter::new() g.u32(psurf) c.send(g.finish_msg(self.comp_id, 0U)) let pxdg = c.alloc_id() let h = @wayland.WireWriter::new() h.u32(pxdg) h.u32(psurf) c.send(h.finish_msg(self.wm_id, 2U)) let popup_id = c.alloc_id() let i = @wayland.WireWriter::new() i.u32(popup_id) i.u32(self.xdg) i.u32(pos_id) c.send(i.finish_msg(pxdg, 2U)) match geometry { Some((gx, gy, gw, gh)) => { let w = @wayland.WireWriter::new() w.i32(gx) w.i32(gy) w.i32(gw) w.i32(gh) c.send(w.finish_msg(pxdg, 3U)) } None => () } c.send(@wayland.WireWriter::new().finish_msg(psurf, 6U)) pump(self.comp, 5) (psurf, pxdg, popup_id) } ///| xdg_popup.grab(seat, serial). fn PopupRig::grab(self : PopupRig, popup_id : UInt) -> Unit { let w = @wayland.WireWriter::new() w.u32(0U) w.u32(0U) self.c.send(w.finish_msg(popup_id, 1U)) pump(self.comp, 3) } ///| Attach a w x h shm buffer to the popup surface and commit it. fn PopupRig::map_popup(self : PopupRig, psurf : UInt, w : Int, h : Int) -> Unit { let c = self.c let bytes = w * h * 4 let memfd = @sys.memfd_create("cpopup-shm", bytes) ignore(@sys.pwrite_bytes(memfd, Bytes::from_array(Array::make(bytes, b'\xff')), 0)) let pool = c.alloc_id() let j = @wayland.WireWriter::new() j.u32(pool) j.i32(bytes) c.send(j.finish_msg(self.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(w) k.i32(h) k.i32(w * 4) 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)) pump(self.comp, 10) } ///| Everything the compositor sent, with payloads: (object, opcode, payload bytes). fn drain_full(c : TestWaylandClient) -> Array[(UInt, UInt, Array[Byte])] { let scratch : FixedArray[Byte] = FixedArray::make(65536, b'\x00') let raw : Array[Byte] = [] while @sys.poll_read(c.fd, 5) > 0 { let (n, rfd) = @sys.recvmsg_fd(c.fd, scratch, 65536) if rfd >= 0 { @sys.close_fd(rfd) } if n <= 0 { break } for i = 0; i < n; i = i + 1 { raw.push(scratch[i]) } } let out : Array[(UInt, UInt, Array[Byte])] = [] let mut off = 0 while off + 8 <= raw.length() { let obj = @wayland.read_u32_le(raw, off) let w2 = @wayland.read_u32_le(raw, off + 4) let size = ((w2 >> 16) & 0xFFFFU).reinterpret_as_int() if size < 8 || off + size > raw.length() { break } out.push((obj, w2 & 0xFFFFU, raw[off + 8:off + size].to_array())) off = off + size } out } ///| Signed 32-bit argument `i` of a payload. fn arg_i32(p : Array[Byte], i : Int) -> Int { @wayland.read_u32_le(p, i * 4).reinterpret_as_int() } ///| The last xdg_popup.configure (x, y, w, h) in `msgs`. fn last_configure(msgs : Array[(UInt, UInt, Array[Byte])], popup_id : UInt) -> (Int, Int, Int, Int)? { let mut found = None for m in msgs { if m.0 == popup_id && m.1 == 0U { found = Some((arg_i32(m.2, 0), arg_i32(m.2, 1), arg_i32(m.2, 2), arg_i32(m.2, 3))) } } found } ///| test "a right-click menu without a grab leaves keyboard focus on its window (Chrome)" { let r = popup_rig("nograb") assert_eq(r.comp.kbd_win, Some(1UL)) drain_full(r.c) |> ignore let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U) let (psurf, _, _) = r.open_popup(pos) r.map_popup(psurf, 200, 150) assert_eq(r.comp.popups.length(), 1) // No wl_keyboard.leave: Chrome reads that as "dismiss the menu" and destroys it at once. let msgs = drain_full(r.c) assert_false(msgs.iter().any(m => m.0 == r.kbd_id && m.1 == 2U)) assert_eq(r.comp.kbd_win, Some(1UL)) } ///| test "a grabbing right-click menu takes keyboard focus once it is drawn (Qt)" { let r = popup_rig("grab") let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U) let (psurf, _, popup_id) = r.open_popup(pos) r.grab(popup_id) // Not mapped yet: the window keeps the keyboard. assert_eq(r.comp.kbd_win, Some(1UL)) r.map_popup(psurf, 200, 150) let pwid = r.comp.popups[0].id assert_eq(r.comp.kbd_win, Some(pwid)) // Dismissed from outside and destroyed: the keyboard goes back to the window. click_at(r.comp, r.win.x + r.win.w - 20, r.win.y + r.win.h - 20, 50U) assert_true(has_msg(drain_msgs(r.c), popup_id, 1U)) // popup_done r.c.send(@wayland.WireWriter::new().finish_msg(popup_id, 0U)) pump(r.comp, 5) assert_eq(r.comp.popups.length(), 0) assert_eq(r.comp.kbd_win, Some(1UL)) } ///| test "a menu opened near the bottom flips above the pointer when the client allows it" { let r = popup_rig("flip") drain_full(r.c) |> ignore // Window content is r.win.h tall; ask for a 300px menu growing down from 40px above its // bottom edge, with flip_y (8) allowed. let ay = r.win.h - 40 let pos = r.positioner(200, 300, 100, ay, 6U, 8U, 8U) let (_, _, popup_id) = r.open_popup(pos) let (x, y, w, h) = last_configure(drain_full(r.c), popup_id).unwrap() // anchor bottom_left -> top_left, gravity bottom_right -> top_right: ends at the anchor. assert_eq((x, y, w, h), (100, ay - 300, 200, 300)) assert_eq(r.comp.popups[0].ry, ay - 300) } ///| test "a menu that cannot flip slides or shrinks to stay on screen, as the client allows" { let r = popup_rig("slide") drain_full(r.c) |> ignore // Screen bottom relative to the window's content origin. let bottom = 720 - r.win.y let ay = r.win.h - 40 // Too tall to fit above either, slide_y (2) only: pushed up to end at the screen bottom. let p1 = r.positioner(200, 700, 100, ay, 6U, 8U, 2U) let (_, _, id1) = r.open_popup(p1) let (_, y1, _, h1) = last_configure(drain_full(r.c), id1).unwrap() assert_eq((y1, h1), (bottom - 700, 700)) // resize_y (32) only: keeps its place and is cut to the space left below it. let p2 = r.positioner(200, 300, 100, ay, 6U, 8U, 32U) let (_, _, id2) = r.open_popup(p2) let (_, y2, _, h2) = last_configure(drain_full(r.c), id2).unwrap() assert_eq((y2, h2), (ay + 1, bottom - ay - 1)) // Off the right edge with slide_x (1): pushed left to end at the screen's right edge. let right = 1280 - r.win.x let p3 = r.positioner(300, 100, r.win.w - 10, 100, 6U, 8U, 1U) let (_, _, id3) = r.open_popup(p3) let (x3, _, w3, _) = last_configure(drain_full(r.c), id3).unwrap() assert_eq((x3, w3), (right - 300, 300)) // No adjustment allowed: placed exactly where asked. let p4 = r.positioner(200, 300, 100, ay, 6U, 8U, 0U) let (_, _, id4) = r.open_popup(p4) let (_, y4, _, _) = last_configure(drain_full(r.c), id4).unwrap() assert_eq(y4, ay + 1) } ///| test "xdg_popup.reposition answers with repositioned and a new configure" { let r = popup_rig("repos") let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U) let (psurf, _, popup_id) = r.open_popup(pos) r.map_popup(psurf, 200, 150) drain_full(r.c) |> ignore let pos2 = r.positioner(250, 180, 300, 200, 6U, 8U, 0U) let w = @wayland.WireWriter::new() w.u32(pos2) w.u32(77U) r.c.send(w.finish_msg(popup_id, 2U)) pump(r.comp, 5) let msgs = drain_full(r.c) let token = msgs.iter().find_first(m => m.0 == popup_id && m.1 == 2U).unwrap() assert_eq(arg_i32(token.2, 0), 77) assert_eq(last_configure(msgs, popup_id), Some((300, 201, 250, 180))) assert_eq((r.comp.popups[0].rx, r.comp.popups[0].ry), (300, 201)) } ///| test "a menu with a client-drawn shadow is placed by its window geometry, not its buffer" { let r = popup_rig("shadow") // Chrome: 200x150 menu inside a 240x182 buffer, the menu starting at (20, 12). let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U) let (psurf, _, _) = r.open_popup(pos, geometry=Some((20, 12, 200, 150))) r.map_popup(psurf, 240, 182) let p = r.comp.popups[0] assert_eq((p.geo_x, p.geo_y), (20, 12)) // The visible menu's top-left is at the configured spot; a point 5px inside it is 25, 17 // into the buffer. let vx = r.win.x + 100 let vy = r.win.y + 101 drain_full(r.c) |> ignore r.comp.handle_input(@nested.InputEvent::PointerMove((vx + 5).to_double(), (vy + 5).to_double()), 10U) let msgs = drain_full(r.c) let enter = msgs.iter().find_first(m => m.0 == r.ptr_id && m.1 == 0U).unwrap() // enter(serial, surface, x: fixed, y: fixed) assert_eq(arg_i32(enter.2, 2) / 256, 25) assert_eq(arg_i32(enter.2, 3) / 256, 17) }