///| Selecting text with the mouse (Kwi, Konsole, any terminal or editor): the implicit pointer /// grab that keeps a drag on the window it started in, and the primary selection a middle /// click pastes. ///| Every message the compositor sent `c`: (object, opcode, argument words). fn drain_args(c : TestWaylandClient) -> Array[(UInt, UInt, Array[UInt])] { 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[UInt])] = [] 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 } let args : Array[UInt] = [] for a = off + 8; a + 4 <= off + size; a = a + 4 { args.push(@wayland.read_u32_le(raw, a)) } out.push((obj, w2 & 0xFFFFU, args)) off = off + size } out } ///| Bind the seat and get its pointer; returns the wl_pointer id. fn pointer_for(comp : @host.ChandraCompositor, c : TestWaylandClient) -> UInt { let seat = bind_global(c, 2U, "wl_seat", 7U) let ptr = c.alloc_id() let w = @wayland.WireWriter::new() w.u32(ptr) c.send(w.finish_msg(seat, 0U)) for _i = 0; _i < 5; _i = _i + 1 { comp.step() @sys.sleep_ms(2) } ptr } ///| fn count_msgs(msgs : Array[(UInt, UInt, Array[UInt])], obj : UInt, op : UInt) -> Int { msgs.iter().filter(m => m.0 == obj && m.1 == op).count() } ///| test "a drag that starts in a window stays with it until the button is released" { let socket_path = "/tmp/wayland-moon-selgrab-" + @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, "Kwi", "org.kde.kwi") commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL) let (b, shm_b, surf_b, xdg_b, _, _) = open_and_configure_client(comp, socket_path, "Other", "other") commit_shm_frame(comp, b, shm_b, surf_b, xdg_b, 2UL) let ptr_a = pointer_for(comp, a) let ptr_b = pointer_for(comp, b) ignore(comp.compose_frame()) ignore(drain_args(a)) ignore(drain_args(b)) let g = comp.windows.get(1UL).unwrap().geometry let gb = comp.windows.get(2UL).unwrap().geometry let sx = g.x + 40 let sy = g.y + 40 // Press in window 1 (the terminal) and start selecting. comp.handle_input(@nested.InputEvent::PointerMove(sx.to_double(), sy.to_double()), 100U) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 101U) comp.handle_input(@nested.InputEvent::PointerMove((sx + 30).to_double(), sy.to_double()), 102U) // Drag over the title bar, over the panel at the top of the screen, past the divider and onto // the other window, then far below the bottom of the screen area of the window. comp.handle_input(@nested.InputEvent::PointerMove((sx + 30).to_double(), (g.y - 10).to_double()), 103U) comp.handle_input(@nested.InputEvent::PointerMove((sx + 30).to_double(), 2.0), 104U) comp.handle_input(@nested.InputEvent::PointerMove((g.x + g.w).to_double(), (g.y + 100).to_double()), 105U) comp.handle_input(@nested.InputEvent::PointerMove((gb.x + 50).to_double(), (gb.y + 50).to_double()), 106U) comp.handle_input(@nested.InputEvent::PointerMove((sx + 60).to_double(), (g.y + g.h + 30).to_double()), 107U) // The wheel during the drag scrolls the terminal, not the window under the pointer. comp.handle_input(@nested.InputEvent::Scroll(0U, 15.0, 1), 108U) let during = drain_args(a) assert_eq(count_msgs(during, ptr_a, 1U), 0) // no wl_pointer.leave while the button is held assert_eq(count_msgs(during, ptr_a, 2U), 7) // every motion reached the terminal (the first before the press) assert_eq(count_msgs(during, ptr_a, 4U), 1) // and the wheel // The last motion is in the terminal's coordinates, below its bottom edge. let last = during.iter().filter(m => m.0 == ptr_a && m.1 == 2U).last().unwrap() let (ox, oy) = comp.windows.get(1UL).unwrap().surface_origin() assert_eq(last.2[0], 107U) assert_eq(last.2[1].reinterpret_as_int(), (sx + 60 - ox) * 256) assert_eq(last.2[2].reinterpret_as_int(), (g.y + g.h + 30 - oy) * 256) assert_eq(drain_args(b).length(), 0) // the other window saw nothing // Release outside: the terminal gets the release, then leave; the other window gets // nothing (the pointer isn't over it). comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 109U) let after = drain_args(a) let rel = after.iter().find_first(m => m.0 == ptr_a && m.1 == 3U).unwrap() assert_eq(rel.2[3], 0U) // released assert_eq(count_msgs(after, ptr_a, 1U), 1) // then leave // Back to normal: motion over the other window reaches it. comp.handle_input(@nested.InputEvent::PointerMove((gb.x + 60).to_double(), (gb.y + 60).to_double()), 110U) let bm = drain_args(b) assert_true(count_msgs(bm, ptr_b, 0U) == 1) // enter assert_true(count_msgs(bm, ptr_b, 2U) == 1) // motion comp.close() } ///| test "releasing a drag over another window hands the pointer to it" { let socket_path = "/tmp/wayland-moon-selgrab2-" + @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, "Kwi", "org.kde.kwi") commit_shm_frame(comp, a, shm_a, surf_a, xdg_a, 1UL) let (b, shm_b, surf_b, xdg_b, _, _) = open_and_configure_client(comp, socket_path, "Other", "other") commit_shm_frame(comp, b, shm_b, surf_b, xdg_b, 2UL) let ptr_a = pointer_for(comp, a) let ptr_b = pointer_for(comp, b) let g = comp.windows.get(1UL).unwrap().geometry let gb = comp.windows.get(2UL).unwrap().geometry comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y + 40).to_double()), 1U) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U) comp.handle_input(@nested.InputEvent::PointerMove((gb.x + 40).to_double(), (gb.y + 40).to_double()), 3U) ignore(drain_args(a)) assert_eq(count_msgs(drain_args(b), ptr_b, 0U), 0) // no enter while the drag is held comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U) let ma = drain_args(a) assert_eq(count_msgs(ma, ptr_a, 3U), 1) // the release went to the terminal assert_eq(count_msgs(ma, ptr_a, 1U), 1) // and it lost the pointer let mb = drain_args(b) assert_eq(count_msgs(mb, ptr_b, 0U), 1) // the window under the pointer has it now assert_eq(count_msgs(mb, ptr_b, 3U), 0) // without a stray release comp.close() } ///| Bind zwp_primary_selection_device_manager_v1 and get a device: (manager, device). fn primary_device_for(c : TestWaylandClient) -> (UInt, UInt) { let mgr = bind_global(c, 2U, "zwp_primary_selection_device_manager_v1", 1U) let seat = bind_global(c, 2U, "wl_seat", 7U) let dev = c.alloc_id() let w = @wayland.WireWriter::new() w.u32(dev) w.u32(seat) c.send(w.finish_msg(mgr, 1U)) (mgr, dev) } ///| test "text selected in one app is pasted by a middle click in another (primary selection)" { let socket_path = "/tmp/wayland-moon-primary-" + @sys.getpid().to_string() + ".sock" let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() let (a, _, _, _, _, _) = open_and_configure_client(comp, socket_path, "Kwi", "org.kde.kwi") let (b, _, _, _, _, _) = open_and_configure_client(comp, socket_path, "B", "b") assert_true(a.globals.contains("zwp_primary_selection_device_manager_v1")) let (mgr_a, dev_a) = primary_device_for(a) let (_, dev_b) = primary_device_for(b) // A selects text: create_source, offer, set_selection. let src = a.alloc_id() let w1 = @wayland.WireWriter::new() w1.u32(src) a.send(w1.finish_msg(mgr_a, 0U)) let w2 = @wayland.WireWriter::new() w2.str("text/plain;charset=utf-8") a.send(w2.finish_msg(src, 0U)) let w3 = @wayland.WireWriter::new() w3.u32(src) w3.u32(1U) a.send(w3.finish_msg(dev_a, 0U)) for _i = 0; _i < 5; _i = _i + 1 { comp.step() @sys.sleep_ms(2) } // B gets the keyboard (a click) and with it the primary selection offer. let bg = comp.windows.get(2UL).unwrap().geometry comp.handle_input( @nested.InputEvent::PointerMove((bg.x + bg.w / 2).to_double(), (bg.y + bg.h / 2).to_double()), 1U, ) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 3U) for _i = 0; _i < 5; _i = _i + 1 { comp.step() @sys.sleep_ms(2) } let mb = drain_args(b) let offer = mb.iter().filter(m => m.0 == dev_b && m.1 == 0U).last().unwrap().2[0] assert_true(has_msg(mb.map(m => (m.0, m.1)), offer, 0U)) // offer(mime) let sel = mb.iter().filter(m => m.0 == dev_b && m.1 == 1U).last().unwrap() assert_eq(sel.2[0], offer) // selection(offer) // Middle click in B: receive(mime, fd) reaches A as source.send(mime, fd). ignore(drain_args(a)) let fd = @sys.memfd_create("primary-paste", 16) let w4 = @wayland.WireWriter::new() w4.str("text/plain;charset=utf-8") b.send(w4.finish_msg(offer, 0U), send_fd=fd) for _i = 0; _i < 5; _i = _i + 1 { comp.step() @sys.sleep_ms(2) } assert_true(has_msg(drain_args(a).map(m => (m.0, m.1)), src, 0U)) @sys.close_fd(fd) // A new selection in B cancels A's source. let mgr_b = bind_global(b, 2U, "zwp_primary_selection_device_manager_v1", 1U) let src_b = b.alloc_id() let w5 = @wayland.WireWriter::new() w5.u32(src_b) b.send(w5.finish_msg(mgr_b, 0U)) let w6 = @wayland.WireWriter::new() w6.u32(src_b) w6.u32(2U) b.send(w6.finish_msg(dev_b, 0U)) for _i = 0; _i < 5; _i = _i + 1 { comp.step() @sys.sleep_ms(2) } assert_true(has_msg(drain_args(a).map(m => (m.0, m.1)), src, 1U)) // cancelled comp.close() }