Text selection in terminals: drags stay with their window, middle-click paste works
CI / test (push) Canceled after 4m14s
CI / test (push) Canceled after 4m14s
V: "kwi is having problems selecting text". Two compositor gaps: - No implicit pointer grab. A drag that started in a window lost it as soon as the pointer crossed its edge (onto the frame, the panel, a divider or another window): the client got wl_pointer.leave mid-drag (Qt then forgets the held button), and the release went to the other window or nowhere, so the terminal's selection never ended and kept following the pointer. Selecting past the top/bottom edge to scroll, which Kwi and Konsole autoscroll on, could not work. Now a press forwarded to a client grabs the pointer: motion (in that surface's coordinates, also outside it), the wheel and every button go to it until the last button is up; then the pointer goes to whatever is under it. Anything that takes the pointer (a window move or resize, the overview, a screenshot) ends the grab as before. - No primary selection. zwp_primary_selection_device_manager_v1 was not offered, so Qt's QClipboard::supportsSelection() was false: selected text was never put on the primary selection and a middle click pasted nothing. Now offered (v1), with the offer sent to the focused client like the clipboard. Checked headless with Konsole (Qt 5 Wayland): a selection dragged out of the window keeps extending, is set as the primary selection on release, and a middle click in another Konsole window pastes it. Tests in selection_test.mbt (both grab tests fail without the fix). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5himKXPLcfCZN6BXByd
This commit is contained in:
3 files changed
+466
No files matched your search
@@ -53,6 +53,19 @@ fn HostWindow::surface_at(self : HostWindow, x : Double, y : Double) -> (UInt, D
|
||||
(0U, x - ox.to_double(), y - oy.to_double())
|
||||
}
|
||||
|
||||
///| Screen point (x, y) in the coordinates of window `self`'s surface `sub` (a subsurface's
|
||||
/// wl_surface id, 0 for the window itself), wherever the point is: the pointer can be held
|
||||
/// outside the surface during a drag.
|
||||
fn HostWindow::surface_local(self : HostWindow, sub : UInt, x : Double, y : Double) -> (Double, Double) {
|
||||
let (ox, oy) = self.surface_origin()
|
||||
for s in self.subs {
|
||||
if s.sid == sub {
|
||||
return (x - (ox + s.x).to_double(), y - (oy + s.y).to_double())
|
||||
}
|
||||
}
|
||||
(x - ox.to_double(), y - oy.to_double())
|
||||
}
|
||||
|
||||
///| The window style in use (Settings > Session > Flat window frames). Global because the frame's
|
||||
/// size is part of every window's on-screen box (`client_rect`); the compositor sets it from the
|
||||
/// shell's setting (`ChandraCompositor::apply_window_style`).
|
||||
@@ -156,6 +169,13 @@ pub(all) struct ChandraCompositor {
|
||||
mut title_press : TitlePress?
|
||||
/// True while a button press was forwarded to a client (so its release is too).
|
||||
mut press_fwd : Bool
|
||||
/// Implicit pointer grab: a button was pressed on `ptr_win` (surface `ptr_sub`) and is still
|
||||
/// held, so motion, the wheel and the release all go to that surface, even outside it (text
|
||||
/// selection dragged past the window's edge, over the frame or the panel). `grab_buttons`
|
||||
/// counts the buttons held. Ends with the last release or when the compositor takes the
|
||||
/// pointer (`leave_pointer`: a window move, the overview, a screenshot).
|
||||
mut ptr_grab : Bool
|
||||
mut grab_buttons : Int
|
||||
mut kbd_win : UInt64?
|
||||
mut mods : UInt
|
||||
/// A Super key is down and nothing else was pressed since: releasing it is a Super tap (the
|
||||
@@ -328,6 +348,8 @@ pub fn ChandraCompositor::new(
|
||||
split_drag: None,
|
||||
title_press: None,
|
||||
press_fwd: false,
|
||||
ptr_grab: false,
|
||||
grab_buttons: 0,
|
||||
kbd_win: None,
|
||||
mods: 0U,
|
||||
super_solo: false,
|
||||
@@ -1751,6 +1773,8 @@ fn ChandraCompositor::handle_at(
|
||||
|
||||
///|
|
||||
fn ChandraCompositor::leave_pointer(self : ChandraCompositor) -> Unit {
|
||||
self.ptr_grab = false
|
||||
self.grab_buttons = 0
|
||||
match self.ptr_win {
|
||||
Some(w) => {
|
||||
self.server.pointer_leave(w, sub=self.ptr_sub)
|
||||
@@ -1761,6 +1785,34 @@ fn ChandraCompositor::leave_pointer(self : ChandraCompositor) -> Unit {
|
||||
}
|
||||
}
|
||||
|
||||
///| The window holding the implicit pointer grab, if one is held and the window still exists.
|
||||
fn ChandraCompositor::grab_window(self : ChandraCompositor) -> UInt64? {
|
||||
if !self.ptr_grab {
|
||||
return None
|
||||
}
|
||||
match self.ptr_win {
|
||||
Some(w) if self.windows.contains(w) => Some(w)
|
||||
_ => {
|
||||
self.ptr_grab = false
|
||||
self.grab_buttons = 0
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Pointer motion during an implicit grab: it goes to the grabbing surface, in its
|
||||
/// coordinates, wherever the pointer is. False when no grab is held.
|
||||
fn ChandraCompositor::grab_motion(self : ChandraCompositor, x : Double, y : Double, time_ms : UInt) -> Bool {
|
||||
match self.grab_window() {
|
||||
Some(wid) => {
|
||||
let (lx, ly) = self.windows.get(wid).unwrap().surface_local(self.ptr_sub, x, y)
|
||||
self.server.pointer_motion(wid, time_ms, lx, ly)
|
||||
true
|
||||
}
|
||||
None => false
|
||||
}
|
||||
}
|
||||
|
||||
///| evdev keycode -> the key name lib/launcher understands (US layout).
|
||||
fn key_name(code : UInt, shift : Bool) -> String {
|
||||
let rows = [
|
||||
@@ -1850,6 +1902,9 @@ pub fn ChandraCompositor::handle_input(
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
if self.grab_motion(x, y, time_ms) {
|
||||
return
|
||||
}
|
||||
if self.shell.inspecting() {
|
||||
self.leave_pointer()
|
||||
let id = match self.shell.component_at(xi, yi) {
|
||||
@@ -1944,6 +1999,13 @@ pub fn ChandraCompositor::handle_input(
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
match self.grab_window() {
|
||||
Some(w) => {
|
||||
self.server.pointer_axis(w, time_ms, axis, v, discrete)
|
||||
return
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
if self.overview is Some(_) {
|
||||
return // nothing scrolls under the overview
|
||||
}
|
||||
@@ -2000,6 +2062,26 @@ pub fn ChandraCompositor::handle_input(
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
match self.grab_window() {
|
||||
Some(w) => {
|
||||
self.server.pointer_button(w, time_ms, button, pressed)
|
||||
if pressed {
|
||||
self.grab_buttons = self.grab_buttons + 1
|
||||
} else {
|
||||
self.grab_buttons = self.grab_buttons - 1
|
||||
if self.grab_buttons <= 0 {
|
||||
// The last button is up: the grab ends, and the pointer goes to whatever is
|
||||
// under it now (the client sees it leave if that is elsewhere).
|
||||
self.ptr_grab = false
|
||||
self.grab_buttons = 0
|
||||
self.press_fwd = false
|
||||
self.handle_input(PointerMove(self.ptr_x, self.ptr_y), time_ms)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
if pressed && self.popups.length() > 0 {
|
||||
// A click outside the open popups dismisses them (the click is swallowed).
|
||||
let mut i = self.popups.length() - 1
|
||||
@@ -2118,6 +2200,8 @@ pub fn ChandraCompositor::handle_input(
|
||||
Some(w) =>
|
||||
if pressed {
|
||||
self.press_fwd = true
|
||||
self.ptr_grab = true
|
||||
self.grab_buttons = 1
|
||||
self.server.pointer_button(w, time_ms, button, pressed)
|
||||
} else if self.press_fwd {
|
||||
self.press_fwd = false
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
///| 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()
|
||||
}
|
||||
@@ -84,6 +84,12 @@ enum ObjKind {
|
||||
ObjDataDevice
|
||||
/// Selection offer: (owner client id, owner's wl_data_source id).
|
||||
ObjDataOffer(UInt, UInt)
|
||||
/// zwp_primary_selection_v1 (the selected text that a middle click pastes): manager,
|
||||
/// source, device, and an offer of (owner client id, owner's source id).
|
||||
ObjPrimaryManager
|
||||
ObjPrimarySource
|
||||
ObjPrimaryDevice
|
||||
ObjPrimaryOffer(UInt, UInt)
|
||||
ObjCursorShapeMgr
|
||||
ObjCursorShapeDev
|
||||
}
|
||||
@@ -208,6 +214,10 @@ struct ClientConn {
|
||||
data_devices : Array[UInt]
|
||||
/// wl_data_source id -> mime types it offers.
|
||||
data_sources : Map[UInt, Array[String]]
|
||||
/// zwp_primary_selection_device_v1 ids this client created.
|
||||
prim_devices : Array[UInt]
|
||||
/// zwp_primary_selection_source_v1 id -> mime types it offers.
|
||||
prim_sources : Map[UInt, Array[String]]
|
||||
/// Next server-allocated id for a wl_data_offer.
|
||||
mut next_offer : UInt
|
||||
/// wp_cursor_shape_v1 shape the client last asked for (0 = never set).
|
||||
@@ -237,6 +247,8 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn {
|
||||
seat_ver: 1U,
|
||||
data_devices: [],
|
||||
data_sources: {},
|
||||
prim_devices: [],
|
||||
prim_sources: {},
|
||||
next_offer: 0xff000000U,
|
||||
cursor_shape: 0U,
|
||||
alive: true,
|
||||
@@ -308,6 +320,8 @@ pub(all) struct WaylandServer {
|
||||
mut keymap : Bytes
|
||||
/// Clipboard owner: (client id, wl_data_source id).
|
||||
mut selection : (UInt, UInt)?
|
||||
/// Primary selection owner (the last text selected in any app): (client id, source id).
|
||||
mut primary : (UInt, UInt)?
|
||||
/// Client that currently has keyboard focus (receives selection events).
|
||||
mut kbd_client : UInt?
|
||||
/// Area a popup must stay inside, as (x, y, w, h) relative to its parent's content origin
|
||||
@@ -346,6 +360,7 @@ pub fn WaylandServer::listen(
|
||||
keymap: Bytes::from_array([]),
|
||||
selection: None,
|
||||
kbd_client: None,
|
||||
primary: None,
|
||||
popup_bounds: fn(_) { (-1000000, -1000000, 2000000, 2000000) },
|
||||
own_mimes: [],
|
||||
own_data: Bytes::from_array([]),
|
||||
@@ -388,6 +403,7 @@ fn emit_registry_globals(c : ClientConn, reg_id : UInt) -> Unit {
|
||||
(8U, "wl_data_device_manager", 3U),
|
||||
(9U, "wp_cursor_shape_manager_v1", 1U),
|
||||
(10U, "org_kde_kwin_server_decoration_manager", 1U),
|
||||
(11U, "zwp_primary_selection_device_manager_v1", 1U),
|
||||
]
|
||||
for g in globals {
|
||||
let w = WireWriter::new()
|
||||
@@ -605,6 +621,8 @@ fn WaylandServer::dispatch_msg(
|
||||
c.objects.set(new_id, ObjCursorShapeMgr)
|
||||
} else if iface == "wl_data_device_manager" {
|
||||
c.objects.set(new_id, ObjDataManager)
|
||||
} else if iface == "zwp_primary_selection_device_manager_v1" {
|
||||
c.objects.set(new_id, ObjPrimaryManager)
|
||||
} else if iface == "wl_subcompositor" {
|
||||
c.objects.set(new_id, ObjSubcompositor)
|
||||
} else if iface == "wl_output" {
|
||||
@@ -868,6 +886,88 @@ fn WaylandServer::dispatch_msg(
|
||||
c.objects.remove(obj_id)
|
||||
send_delete_id(c, obj_id)
|
||||
}
|
||||
ObjPrimaryManager =>
|
||||
if opcode == 0U {
|
||||
// create_source(id)
|
||||
let id = reader.u32()
|
||||
c.objects.set(id, ObjPrimarySource)
|
||||
c.prim_sources.set(id, [])
|
||||
} else if opcode == 1U {
|
||||
// get_device(id, seat)
|
||||
let id = reader.u32()
|
||||
c.objects.set(id, ObjPrimaryDevice)
|
||||
c.prim_devices.push(id)
|
||||
if self.kbd_client == Some(c.client_id) {
|
||||
self.send_primary(c)
|
||||
}
|
||||
} else if opcode == 2U {
|
||||
// destroy
|
||||
c.objects.remove(obj_id)
|
||||
send_delete_id(c, obj_id)
|
||||
}
|
||||
ObjPrimarySource =>
|
||||
if opcode == 0U {
|
||||
// offer(mime_type)
|
||||
let mime = reader.str()
|
||||
match c.prim_sources.get(obj_id) {
|
||||
Some(m) => m.push(mime)
|
||||
None => ()
|
||||
}
|
||||
} else if opcode == 1U {
|
||||
// destroy
|
||||
c.prim_sources.remove(obj_id)
|
||||
c.objects.remove(obj_id)
|
||||
send_delete_id(c, obj_id)
|
||||
if self.primary == Some((c.client_id, obj_id)) {
|
||||
self.primary = None
|
||||
self.notify_primary()
|
||||
}
|
||||
}
|
||||
ObjPrimaryDevice =>
|
||||
if opcode == 0U {
|
||||
// set_selection(source or null, serial)
|
||||
let src = reader.u32()
|
||||
let old = self.primary
|
||||
self.primary = if src == 0U { None } else { Some((c.client_id, src)) }
|
||||
match old {
|
||||
Some((ocid, osid)) =>
|
||||
if old != self.primary {
|
||||
match self.find_client(ocid) {
|
||||
Some(oc) => oc.send_msg(WireWriter::new().finish_msg(osid, 1U)) // cancelled
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
self.notify_primary()
|
||||
} else if opcode == 1U {
|
||||
// destroy
|
||||
c.objects.remove(obj_id)
|
||||
c.prim_devices.retain(d => d != obj_id)
|
||||
send_delete_id(c, obj_id)
|
||||
}
|
||||
ObjPrimaryOffer(ocid, osid) =>
|
||||
if opcode == 0U {
|
||||
// receive(mime_type, fd): the owner writes the text into the fd (source.send)
|
||||
let mime = reader.str()
|
||||
let fd = c.take_fd()
|
||||
if fd >= 0 {
|
||||
match self.find_client(ocid) {
|
||||
Some(oc) =>
|
||||
if oc.prim_sources.contains(osid) && oc.alive {
|
||||
let w = WireWriter::new()
|
||||
w.str(mime)
|
||||
ignore(@sys.sendmsg_fd(oc.fd, w.finish_msg(osid, 0U), fd))
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
@sys.close_fd(fd)
|
||||
}
|
||||
} else if opcode == 1U {
|
||||
// destroy
|
||||
c.objects.remove(obj_id)
|
||||
send_delete_id(c, obj_id)
|
||||
}
|
||||
ObjCursorShapeMgr =>
|
||||
if opcode == 1U {
|
||||
// get_pointer(cursor_shape_device: new_id, pointer)
|
||||
@@ -1474,6 +1574,14 @@ pub fn WaylandServer::poll_events(
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
match self.primary {
|
||||
Some((ocid, _)) =>
|
||||
if ocid == c.client_id {
|
||||
self.primary = None
|
||||
self.notify_primary()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
j = j - 1
|
||||
}
|
||||
|
||||
@@ -1856,6 +1964,7 @@ pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> U
|
||||
Some((c, tl)) => {
|
||||
self.kbd_client = Some(c.client_id)
|
||||
self.send_selection(c)
|
||||
self.send_primary(c)
|
||||
for kid in c.keyboard_ids {
|
||||
let w = WireWriter::new()
|
||||
w.u32(self.next_serial())
|
||||
@@ -2109,6 +2218,55 @@ fn WaylandServer::send_selection(self : WaylandServer, c : ClientConn) -> Unit {
|
||||
}
|
||||
}
|
||||
|
||||
///| Offer the primary selection to `c`'s primary selection devices (or clear it).
|
||||
fn WaylandServer::send_primary(self : WaylandServer, c : ClientConn) -> Unit {
|
||||
let mimes : Array[String] = match self.primary {
|
||||
Some((ocid, osid)) =>
|
||||
match self.find_client(ocid) {
|
||||
Some(oc) => oc.prim_sources.get(osid).unwrap_or([])
|
||||
None => []
|
||||
}
|
||||
None => []
|
||||
}
|
||||
for dev in c.prim_devices {
|
||||
match self.primary {
|
||||
Some((ocid, osid)) => {
|
||||
let oid = c.next_offer
|
||||
c.next_offer = c.next_offer + 1U
|
||||
c.objects.set(oid, ObjPrimaryOffer(ocid, osid))
|
||||
let w = WireWriter::new()
|
||||
w.u32(oid)
|
||||
c.send_msg(w.finish_msg(dev, 0U)) // data_offer
|
||||
for m in mimes {
|
||||
let ow = WireWriter::new()
|
||||
ow.str(m)
|
||||
c.send_msg(ow.finish_msg(oid, 0U)) // offer
|
||||
}
|
||||
let sw = WireWriter::new()
|
||||
sw.u32(oid)
|
||||
c.send_msg(sw.finish_msg(dev, 1U)) // selection
|
||||
}
|
||||
None => {
|
||||
let sw = WireWriter::new()
|
||||
sw.u32(0U)
|
||||
c.send_msg(sw.finish_msg(dev, 1U))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Push the primary selection to whichever client has keyboard focus.
|
||||
fn WaylandServer::notify_primary(self : WaylandServer) -> Unit {
|
||||
match self.kbd_client {
|
||||
Some(cid) =>
|
||||
match self.find_client(cid) {
|
||||
Some(c) => self.send_primary(c)
|
||||
None => ()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| Push the clipboard to whichever client has keyboard focus.
|
||||
fn WaylandServer::notify_selection(self : WaylandServer) -> Unit {
|
||||
match self.kbd_client {
|
||||
|
||||
Reference in new issue
Block a user