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

+66
View File
@@ -697,3 +697,69 @@ test "the dock module magnifies the tile under the pointer and shows nothing whe
comp.handle_input(@nested.InputEvent::PointerMove(640.0, 300.0), 2U) comp.handle_input(@nested.InputEvent::PointerMove(640.0, 300.0), 2U)
assert_eq(comp.shell.hover_slot, "") assert_eq(comp.shell.hover_slot, "")
} }
///| Bind wl_data_device_manager and get a data device; returns (manager, device).
fn data_device_for(c : TestWaylandClient) -> (UInt, UInt) {
let mgr = bind_global(c, 2U, "wl_data_device_manager", 3U)
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 "copy in one client, paste in another" {
let socket_path = "/tmp/wayland-moon-clip-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let (a, _, _, _, _, _) = open_and_configure_client(comp, socket_path, "A", "a")
let (b, _, _, _, _, _) = open_and_configure_client(comp, socket_path, "B", "b")
let (mgr_a, dev_a) = data_device_for(a)
let (_, dev_b) = data_device_for(b)
// A creates a source offering text and makes it the 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, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
// Focus B's window by clicking it; B is then offered the clipboard.
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 msgs_b = drain_msgs(b)
assert_true(has_msg(msgs_b, dev_b, 0U)) // data_offer
assert_true(has_msg(msgs_b, 0xff000000U, 0U)) // offer(mime) on the new wl_data_offer
assert_true(has_msg(msgs_b, dev_b, 5U)) // selection
// B pastes: receive(mime, fd) on the offer reaches A as source.send.
let fd = @sys.memfd_create("paste", 16)
let w4 = @wayland.WireWriter::new()
w4.str("text/plain;charset=utf-8")
b.send(w4.finish_msg(0xff000000U, 1U), send_fd=fd)
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_true(has_msg(drain_msgs(a), src, 1U))
@sys.close_fd(fd)
}
+169 -6
View File
@@ -41,7 +41,10 @@ enum ObjKind {
ObjSubcompositor ObjSubcompositor
ObjSubsurface ObjSubsurface
ObjDataManager ObjDataManager
ObjDataObj ObjDataSource
ObjDataDevice
/// Selection offer: (owner client id, owner's wl_data_source id).
ObjDataOffer(UInt, UInt)
ObjCursorShapeMgr ObjCursorShapeMgr
ObjCursorShapeDev ObjCursorShapeDev
} }
@@ -125,6 +128,12 @@ struct ClientConn {
pointer_ids : Array[UInt] pointer_ids : Array[UInt]
keyboard_ids : Array[UInt] keyboard_ids : Array[UInt]
mut seat_ver : 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). /// wp_cursor_shape_v1 shape the client last asked for (0 = never set).
mut cursor_shape : UInt mut cursor_shape : UInt
mut alive : Bool mut alive : Bool
@@ -149,6 +158,9 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn {
pointer_ids: [], pointer_ids: [],
keyboard_ids: [], keyboard_ids: [],
seat_ver: 1U, seat_ver: 1U,
data_devices: [],
data_sources: {},
next_offer: 0xff000000U,
cursor_shape: 0U, cursor_shape: 0U,
alive: true, alive: true,
} }
@@ -217,6 +229,10 @@ pub(all) struct WaylandServer {
mut next_win_id : UInt64 mut next_win_id : UInt64
mut next_serial : UInt mut next_serial : UInt
mut keymap : Bytes 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_win_id: 1UL,
next_serial: 100U, next_serial: 100U,
keymap: Bytes::from_array([]), keymap: Bytes::from_array([]),
selection: None,
kbd_client: None,
}) })
} }
@@ -552,12 +570,90 @@ fn WaylandServer::dispatch_msg(
c.objects.set(sub_id, ObjSubsurface) c.objects.set(sub_id, ObjSubsurface)
} }
ObjDataManager => ObjDataManager =>
if opcode == 0U || opcode == 1U { if opcode == 0U {
// create_data_source / get_data_device: accepted, no selection support yet // create_data_source(id)
let id = reader.u32() 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 => ObjCursorShapeMgr =>
if opcode == 1U { if opcode == 1U {
// get_pointer(cursor_shape_device: new_id, pointer) // get_pointer(cursor_shape_device: new_id, pointer)
@@ -978,6 +1074,17 @@ pub fn WaylandServer::poll_events(
events.push(ToplevelClosed(wid)) events.push(ToplevelClosed(wid))
} }
c.close_all() 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 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 { pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> Unit {
match self.win_client(win_id) { match self.win_client(win_id) {
None => () None => ()
Some((c, tl)) => Some((c, tl)) => {
self.kbd_client = Some(c.client_id)
self.send_selection(c)
for kid in c.keyboard_ids { for kid in c.keyboard_ids {
let w = WireWriter::new() let w = WireWriter::new()
w.u32(self.next_serial()) w.u32(self.next_serial())
@@ -1203,6 +1312,7 @@ pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> U
w.array([]) w.array([])
c.send_msg(w.finish_msg(kid, 1U)) c.send_msg(w.finish_msg(kid, 1U))
} }
}
} }
} }
@@ -1308,3 +1418,56 @@ pub fn WaylandServer::popup_done(self : WaylandServer, win_id : UInt64) -> Unit
None => () 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 => ()
}
}