Clipboard: implement wl_data_device selection (copy/paste between clients)

The data-device manager was accepted but ignored. Now set_selection is tracked,
the focused client is offered the selection (data_offer/offer/selection) on focus
change and on update, receive() forwards the fd to the owner as source.send, and
the selection is cleared when its owner goes away. Drag-and-drop is still unsupported.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 00:41:44 -08:00
1 parent 82cac8a915
commit 306a7c121e
2 files changed
+235 -6

No files matched your search

+169 -6
View File
@@ -41,7 +41,10 @@ enum ObjKind {
ObjSubcompositor
ObjSubsurface
ObjDataManager
ObjDataObj
ObjDataSource
ObjDataDevice
/// Selection offer: (owner client id, owner's wl_data_source id).
ObjDataOffer(UInt, UInt)
ObjCursorShapeMgr
ObjCursorShapeDev
}
@@ -125,6 +128,12 @@ struct ClientConn {
pointer_ids : Array[UInt]
keyboard_ids : Array[UInt]
mut seat_ver : UInt
/// wl_data_device ids this client created (they receive selection events).
data_devices : Array[UInt]
/// wl_data_source id -> mime types it offers.
data_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).
mut cursor_shape : UInt
mut alive : Bool
@@ -149,6 +158,9 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn {
pointer_ids: [],
keyboard_ids: [],
seat_ver: 1U,
data_devices: [],
data_sources: {},
next_offer: 0xff000000U,
cursor_shape: 0U,
alive: true,
}
@@ -217,6 +229,10 @@ pub(all) struct WaylandServer {
mut next_win_id : UInt64
mut next_serial : UInt
mut keymap : Bytes
/// Clipboard owner: (client id, wl_data_source id).
mut selection : (UInt, UInt)?
/// Client that currently has keyboard focus (receives selection events).
mut kbd_client : UInt?
}
///|
@@ -240,6 +256,8 @@ pub fn WaylandServer::listen(
next_win_id: 1UL,
next_serial: 100U,
keymap: Bytes::from_array([]),
selection: None,
kbd_client: None,
})
}
@@ -552,12 +570,90 @@ fn WaylandServer::dispatch_msg(
c.objects.set(sub_id, ObjSubsurface)
}
ObjDataManager =>
if opcode == 0U || opcode == 1U {
// create_data_source / get_data_device: accepted, no selection support yet
if opcode == 0U {
// create_data_source(id)
let id = reader.u32()
c.objects.set(id, ObjDataObj)
c.objects.set(id, ObjDataSource)
c.data_sources.set(id, [])
} else if opcode == 1U {
// get_data_device(id, seat)
let id = reader.u32()
c.objects.set(id, ObjDataDevice)
c.data_devices.push(id)
// A client created after it got focus still needs the current clipboard.
if self.kbd_client == Some(c.client_id) {
self.send_selection(c)
}
}
ObjDataSource =>
if opcode == 0U {
// offer(mime_type)
let mime = reader.str()
match c.data_sources.get(obj_id) {
Some(m) => m.push(mime)
None => ()
}
} else if opcode == 1U {
// destroy
c.data_sources.remove(obj_id)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
if self.selection == Some((c.client_id, obj_id)) {
self.selection = None
self.notify_selection()
}
}
ObjDataDevice =>
if opcode == 1U {
// set_selection(source or null, serial)
let src = reader.u32()
let old = self.selection
if src == 0U {
self.selection = None
} else {
self.selection = Some((c.client_id, src))
}
match old {
Some((ocid, osid)) =>
if old != self.selection {
match self.find_client(ocid) {
Some(oc) => oc.send_msg(WireWriter::new().finish_msg(osid, 2U)) // cancelled
None => ()
}
}
None => ()
}
self.notify_selection()
} else if opcode == 2U {
// release
c.objects.remove(obj_id)
c.data_devices.retain(d => d != obj_id)
send_delete_id(c, obj_id)
}
ObjDataOffer(ocid, osid) =>
if opcode == 1U {
// receive(mime_type, fd): ask the owner to write the data into the fd
let mime = reader.str()
let fd = c.take_fd()
if fd >= 0 {
match self.find_client(ocid) {
Some(oc) =>
if oc.data_sources.contains(osid) {
let w = WireWriter::new()
w.str(mime)
if oc.alive {
ignore(@sys.sendmsg_fd(oc.fd, w.finish_msg(osid, 1U), fd))
}
}
None => ()
}
@sys.close_fd(fd)
}
} else if opcode == 2U {
// destroy
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
}
ObjDataObj => ()
ObjCursorShapeMgr =>
if opcode == 1U {
// get_pointer(cursor_shape_device: new_id, pointer)
@@ -978,6 +1074,17 @@ pub fn WaylandServer::poll_events(
events.push(ToplevelClosed(wid))
}
c.close_all()
if self.kbd_client == Some(c.client_id) {
self.kbd_client = None
}
match self.selection {
Some((ocid, _)) =>
if ocid == c.client_id {
self.selection = None
self.notify_selection()
}
None => ()
}
j = j - 1
}
@@ -1195,7 +1302,9 @@ fn WaylandServer::pointer_frame(_self : WaylandServer, c : ClientConn, pid : UIn
pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, tl)) =>
Some((c, tl)) => {
self.kbd_client = Some(c.client_id)
self.send_selection(c)
for kid in c.keyboard_ids {
let w = WireWriter::new()
w.u32(self.next_serial())
@@ -1203,6 +1312,7 @@ pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> U
w.array([])
c.send_msg(w.finish_msg(kid, 1U))
}
}
}
}
@@ -1308,3 +1418,56 @@ pub fn WaylandServer::popup_done(self : WaylandServer, win_id : UInt64) -> Unit
None => ()
}
}
///| Offer the current clipboard to `c`'s data devices (or clear it if there is none).
fn WaylandServer::send_selection(self : WaylandServer, c : ClientConn) -> Unit {
let mimes : Array[String] = match self.selection {
Some((ocid, osid)) =>
match self.find_client(ocid) {
Some(oc) =>
match oc.data_sources.get(osid) {
Some(m) => m
None => []
}
None => []
}
None => []
}
for dev in c.data_devices {
match self.selection {
Some((ocid, osid)) => {
let oid = c.next_offer
c.next_offer = c.next_offer + 1U
c.objects.set(oid, ObjDataOffer(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, 5U)) // selection
}
None => {
let sw = WireWriter::new()
sw.u32(0U)
c.send_msg(sw.finish_msg(dev, 5U))
}
}
}
}
///| Push the clipboard to whichever client has keyboard focus.
fn WaylandServer::notify_selection(self : WaylandServer) -> Unit {
match self.kbd_client {
Some(cid) =>
match self.find_client(cid) {
Some(c) => self.send_selection(c)
None => ()
}
None => ()
}
}