Text selection in terminals: drags stay with their window, middle-click paste works
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:
Claude committed 2026-11-26 05:07:25 +00:00
1 parent 516180196f
commit 2c33c4a847
3 files changed
+466

No files matched your search

+158
View File
@@ -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 {