///| Drag and drop (wl_data_device.start_drag): a file dragged out of Dolphin or GNOME Files, a /// tab, a selection of text. The app that starts the drag offers a wl_data_source; while the /// button is held the host moves the drag focus (`drag_focus`) to the surface under the pointer, /// whose client gets enter / motion / leave with a wl_data_offer for the source's mime types, /// and the release drops it (`drag_drop`). Copy / move / ask is agreed between the source's /// actions and the target's (`set_actions`), as wl_data_device_manager version 3 describes. ///| wl_data_device_manager.dnd_action bits. const DND_COPY : UInt = 1U ///| const DND_MOVE : UInt = 2U ///| const DND_ASK : UInt = 4U ///| The drag in progress. struct DragState { /// The client that started it, its wl_data_source (0: a drag inside that client only, which /// never leaves its own surfaces and has no offer), and the surface it started on. client_id : UInt source : UInt origin : UInt /// The drag icon (a wl_surface of the dragging client; 0 for none) and its hotspot offset: /// where its top-left goes relative to the pointer. mut icon : UInt mut icon_x : Int mut icon_y : Int /// The drag focus: (client id, wl_surface id), and the offers made to that client (one per /// wl_data_device it has). mut focus : (UInt, UInt)? offers : Array[UInt] /// The last mime type the target accepted ("" = none) and the actions it takes, with its /// preferred one (dnd_action bits; set by wl_data_offer.set_actions). mut accepted : String mut target_actions : UInt mut preferred : UInt /// The action agreed with the target (0 = none), last sent to both sides. mut action : UInt } ///| A drop the target is still reading: its offer finishes it (wl_data_offer.finish -> /// dnd_finished), or destroying the offer without finishing cancels it. struct DropState { target : UInt offers : Array[UInt] source_client : UInt source : UInt action : UInt } ///| fn dnd_pick_action(source : UInt, target : UInt, preferred : UInt) -> UInt { let common = source & target if common == 0U { 0U } else if (preferred & common) != 0U && (preferred == DND_COPY || preferred == DND_MOVE || preferred == DND_ASK) { preferred } else if (common & DND_COPY) != 0U { DND_COPY } else if (common & DND_MOVE) != 0U { DND_MOVE } else { DND_ASK } } ///| The actions a source offers. One from a client older than version 3 never says: copy. fn WaylandServer::source_actions(self : WaylandServer, cid : UInt, sid : UInt) -> UInt { match self.find_client(cid) { Some(c) => match c.source_actions.get(sid) { Some(a) => a None => DND_COPY } None => 0U } } ///| Send an event to a live wl_data_source of client `cid`. fn WaylandServer::to_source(self : WaylandServer, cid : UInt, sid : UInt, w : WireWriter, opcode : UInt) -> Unit { match self.find_client(cid) { Some(c) => if c.data_sources.contains(sid) { c.send_msg(w.finish_msg(sid, opcode)) } None => () } } ///| Whether a drag is in progress. pub fn WaylandServer::drag_active(self : WaylandServer) -> Bool { self.drag is Some(_) } ///| The drag was started by a client-internal drag (no data source): it may only enter that /// client's own surfaces. pub fn WaylandServer::drag_client(self : WaylandServer) -> UInt { match self.drag { Some(d) => d.client_id None => 0U } } ///| wl_data_device.start_drag(source, origin, icon, serial). fn WaylandServer::start_drag( self : WaylandServer, c : ClientConn, source : UInt, origin : UInt, icon : UInt, events : Array[CompositorEvent], ) -> Unit { // A new drag replaces one still going (a client that lost track of its own). ignore(self.drag_end(DragCancel, events)) if source != 0U && !c.data_sources.contains(source) { return } // The window the drag started on: the host checks it holds the pointer (a button down). let win = match c.surfaces.get(origin) { Some({ window_id: Some(w), .. }) => w _ => match sub_root(c, origin) { Some((w, _, _)) => w None => 0UL } } self.drag = Some({ client_id: c.client_id, source, origin, icon: if c.surfaces.contains(icon) { icon } else { 0U }, icon_x: 0, icon_y: 0, focus: None, offers: [], accepted: "", target_actions: 0U, preferred: 0U, action: 0U, }) events.push(DragStarted(win)) } ///| Move the drag focus to surface `sub` of window `win_id` (0 for the window's own surface) at /// surface-local (x, y), or off every client (None: the panel, the desktop, a frame). pub fn WaylandServer::drag_focus( self : WaylandServer, win_id : UInt64?, sub : UInt, x : Double, y : Double, ) -> Unit { guard self.drag is Some(d) else { return } let target : (ClientConn, UInt)? = match win_id { Some(w) => match self.win_client(w) { Some((c, tl)) => { let sid = if sub != 0U && c.surfaces.contains(sub) { sub } else { tl.wl_surface_id } // A drag with no source stays inside its own client. if d.source == 0U && c.client_id != d.client_id { None } else { Some((c, sid)) } } None => None } None => None } let same = match (target, d.focus) { (Some((c, sid)), Some((fc, fs))) => c.client_id == fc && sid == fs (None, None) => true _ => false } if same { return } self.drag_leave_focus(d) match target { None => () Some((c, sid)) => { d.focus = Some((c.client_id, sid)) d.accepted = "" d.target_actions = 0U d.preferred = 0U d.action = 0U let mimes : Array[String] = if d.source == 0U { [] } else { match self.find_client(d.client_id) { Some(oc) => oc.data_sources.get(d.source).unwrap_or([]) None => [] } } let src_actions = self.source_actions(d.client_id, d.source) let serial = self.next_serial() for dev in c.data_devices { let mut oid = 0U if d.source != 0U { oid = c.next_offer c.next_offer = c.next_offer + 1U c.objects.set(oid, ObjDataOffer(d.client_id, d.source)) d.offers.push(oid) 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 } if c.data_ver >= 3U { let aw = WireWriter::new() aw.u32(src_actions) c.send_msg(aw.finish_msg(oid, 1U)) // source_actions } } let ew = WireWriter::new() ew.u32(serial) ew.u32(sid) ew.i32(fixed(x)) ew.i32(fixed(y)) ew.u32(oid) c.send_msg(ew.finish_msg(dev, 1U)) // enter } // A target older than version 3 never says which actions it takes: copy. if c.data_ver < 3U { d.target_actions = DND_COPY d.preferred = DND_COPY self.drag_update_action(d) } } } } ///| The drag leaves its focus: leave to the target, and its offers stop meaning anything. fn WaylandServer::drag_leave_focus(self : WaylandServer, d : DragState) -> Unit { match d.focus { Some((cid, _)) => match self.find_client(cid) { Some(c) => for dev in c.data_devices { c.send_msg(WireWriter::new().finish_msg(dev, 2U)) // leave } None => () } None => () } d.focus = None d.offers.clear() if d.accepted != "" && d.source != 0U { // The source shows "no target" again (wl_data_source.target(null)). let w = WireWriter::new() w.u32(0U) self.to_source(d.client_id, d.source, w, 0U) } d.accepted = "" if d.action != 0U { d.action = 0U if d.source != 0U && self.client_data_ver(d.client_id) >= 3U { let w = WireWriter::new() w.u32(0U) self.to_source(d.client_id, d.source, w, 5U) // action(none) } } } ///| fn WaylandServer::client_data_ver(self : WaylandServer, cid : UInt) -> UInt { match self.find_client(cid) { Some(c) => c.data_ver None => 0U } } ///| Pointer motion inside the drag focus, surface-local. pub fn WaylandServer::drag_motion(self : WaylandServer, time_ms : UInt, x : Double, y : Double) -> Unit { guard self.drag is Some({ focus: Some((cid, _)), .. }) else { return } match self.find_client(cid) { Some(c) => for dev in c.data_devices { let w = WireWriter::new() w.u32(time_ms) w.i32(fixed(x)) w.i32(fixed(y)) c.send_msg(w.finish_msg(dev, 3U)) // motion } None => () } } ///| Work out the action again and tell both sides if it changed. fn WaylandServer::drag_update_action(self : WaylandServer, d : DragState) -> Unit { guard d.focus is Some((cid, _)) else { return } if d.source == 0U { return } let a = dnd_pick_action(self.source_actions(d.client_id, d.source), d.target_actions, d.preferred) if a == d.action { return } d.action = a match self.find_client(cid) { Some(c) => if c.data_ver >= 3U { for oid in d.offers { let w = WireWriter::new() w.u32(a) c.send_msg(w.finish_msg(oid, 2U)) // offer.action } } None => () } if self.client_data_ver(d.client_id) >= 3U { let w = WireWriter::new() w.u32(a) self.to_source(d.client_id, d.source, w, 5U) // source.action } } ///| How a drag ends. enum DragEnd { /// The button was released: drop if the target accepted, else cancel. DragRelease /// Escape, the source going away, the window closing: always cancel. DragCancel } ///| Release: drop on the target if it accepted something it can take, otherwise cancel. /// Returns whether it dropped. pub fn WaylandServer::drag_drop(self : WaylandServer) -> Bool { let events : Array[CompositorEvent] = [] self.drag_end(DragRelease, events) } ///| Cancel the drag (Escape, or the button went up before the client asked to drag). pub fn WaylandServer::drag_cancel(self : WaylandServer) -> Unit { let events : Array[CompositorEvent] = [] ignore(self.drag_end(DragCancel, events)) } ///| fn WaylandServer::drag_end(self : WaylandServer, how : DragEnd, events : Array[CompositorEvent]) -> Bool { guard self.drag is Some(d) else { return false } self.drag = None events.push(DragEnded) let src_v3 = self.client_data_ver(d.client_id) >= 3U let ok = match (how, d.focus) { (DragRelease, Some((cid, _))) => if d.source == 0U { cid == d.client_id } else { d.accepted != "" && (d.action != 0U || !src_v3) } _ => false } if ok { let (cid, _) = d.focus.unwrap() match self.find_client(cid) { Some(c) => for dev in c.data_devices { c.send_msg(WireWriter::new().finish_msg(dev, 4U)) // drop } None => () } if d.source != 0U { if src_v3 { self.to_source(d.client_id, d.source, WireWriter::new(), 3U) // dnd_drop_performed } self.drop_pending = Some({ target: cid, offers: d.offers.copy(), source_client: d.client_id, source: d.source, action: d.action, }) } true } else { self.drag_leave_focus(d) if d.source != 0U { self.to_source(d.client_id, d.source, WireWriter::new(), 2U) // cancelled } false } } ///| wl_data_offer.accept(serial, mime or null) from the drag target. fn WaylandServer::dnd_accept(self : WaylandServer, c : ClientConn, oid : UInt, mime : String) -> Unit { guard self.drag is Some(d) else { return } guard d.focus is Some((cid, _)) && cid == c.client_id && d.offers.contains(oid) else { return } if d.accepted == mime { return } d.accepted = mime let w = WireWriter::new() if mime == "" { w.u32(0U) } else { w.str(mime) } self.to_source(d.client_id, d.source, w, 0U) // target(mime) } ///| wl_data_offer.set_actions(dnd_actions, preferred_action) from the drag target. fn WaylandServer::dnd_set_actions( self : WaylandServer, c : ClientConn, oid : UInt, actions : UInt, preferred : UInt, ) -> Unit { guard self.drag is Some(d) else { return } guard d.focus is Some((cid, _)) && cid == c.client_id && d.offers.contains(oid) else { return } d.target_actions = actions & (DND_COPY | DND_MOVE | DND_ASK) d.preferred = preferred self.drag_update_action(d) } ///| wl_data_offer.finish: the target took the dropped data. fn WaylandServer::dnd_finish(self : WaylandServer, c : ClientConn, oid : UInt) -> Unit { guard self.drop_pending is Some(p) else { return } guard p.target == c.client_id && p.offers.contains(oid) else { return } self.drop_pending = None if self.client_data_ver(p.source_client) >= 3U { self.to_source(p.source_client, p.source, WireWriter::new(), 4U) // dnd_finished } } ///| A dnd offer was destroyed: a dropped one that never finished cancels the drop. fn WaylandServer::dnd_offer_gone(self : WaylandServer, c : ClientConn, oid : UInt) -> Unit { match self.drag { Some(d) => if d.focus is Some((cid, _)) && cid == c.client_id { d.offers.retain(o => o != oid) } None => () } guard self.drop_pending is Some(p) else { return } guard p.target == c.client_id && p.offers.contains(oid) else { return } p.offers.retain(o => o != oid) if p.offers.length() == 0 { self.drop_pending = None if self.client_data_ver(p.source_client) >= 3U { self.to_source(p.source_client, p.source, WireWriter::new(), 2U) // cancelled } } } ///| A wl_data_source was destroyed: a drag it fed ends. fn WaylandServer::dnd_source_gone(self : WaylandServer, c : ClientConn, sid : UInt, events : Array[CompositorEvent]) -> Unit { match self.drag { Some(d) => if d.client_id == c.client_id && d.source == sid { self.drag_leave_focus(d) self.drag = None events.push(DragEnded) } None => () } match self.drop_pending { Some(p) => if p.source_client == c.client_id && p.source == sid { self.drop_pending = None } None => () } } ///| A client went away: its drag ends, and a drag over it loses its target. fn WaylandServer::dnd_client_gone(self : WaylandServer, cid : UInt, events : Array[CompositorEvent]) -> Unit { match self.drag { Some(d) => if d.client_id == cid { // Its own target (the client itself) is gone with it; tell another target it left. match d.focus { Some((fc, _)) if fc != cid => self.drag_leave_focus(d) _ => () } self.drag = None events.push(DragEnded) } else if d.focus is Some((fc, _)) && fc == cid { d.focus = None d.offers.clear() d.accepted = "" d.action = 0U } None => () } match self.drop_pending { Some(p) => if p.source_client == cid { self.drop_pending = None } else if p.target == cid { self.drop_pending = None if self.client_data_ver(p.source_client) >= 3U { self.to_source(p.source_client, p.source, WireWriter::new(), 2U) // cancelled } } None => () } } ///| A surface of the dragging client committed: if it is the drag icon, the host draws it under /// the pointer, `dx`/`dy` being the hotspot moves (attach offset, wl_surface.offset) it carries. fn WaylandServer::dnd_icon_commit( self : WaylandServer, c : ClientConn, sid : UInt, surf : SurfaceState, events : Array[CompositorEvent], ) -> Unit { guard self.drag is Some(d) && d.client_id == c.client_id && d.icon == sid && sid != 0U else { return } d.icon_x = d.icon_x + surf.pend_dx d.icon_y = d.icon_y + surf.pend_dy if surf.attached_null { events.push(DragIcon(None, d.icon_x, d.icon_y)) return } match surf.attached_buffer { Some(bid) => match snapshot_shm_buffer(c, bid) { Some(snap) => events.push(DragIcon(Some(snap), d.icon_x, d.icon_y)) None => () } None => if surf.pend_dx != 0 || surf.pend_dy != 0 { events.push(DragIconMoved(d.icon_x, d.icon_y)) } } } ///| The drag icon surface was destroyed. fn WaylandServer::dnd_icon_gone(self : WaylandServer, c : ClientConn, sid : UInt, events : Array[CompositorEvent]) -> Unit { match self.drag { Some(d) => if d.client_id == c.client_id && d.icon == sid { d.icon = 0U events.push(DragIcon(None, 0, 0)) } None => () } } ///| Whether two windows belong to the same client. pub fn WaylandServer::same_client(self : WaylandServer, a : UInt64, b : UInt64) -> Bool { match (self.win_to_client.get(a), self.win_to_client.get(b)) { (Some(x), Some(y)) => x == y _ => false } }