From b29089ebdcc24317c6a4c62e6ff04aed133f4337 Mon Sep 17 00:00:00 2001 From: Funami580 Date: Mon, 20 Jun 2022 13:41:25 +0200 Subject: [PATCH] support drag and drop files for wayland --- Cargo.lock | 1 + window/Cargo.toml | 1 + window/src/os/wayland/copy_and_paste.rs | 7 +- window/src/os/wayland/drag_and_drop.rs | 119 ++++++++++++++++++++++++ window/src/os/wayland/mod.rs | 1 + window/src/os/wayland/pointer.rs | 9 +- window/src/os/wayland/window.rs | 7 +- 7 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 window/src/os/wayland/drag_and_drop.rs diff --git a/Cargo.lock b/Cargo.lock index 201452eb2..2dd1f318c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5030,6 +5030,7 @@ dependencies = [ "smithay-client-toolkit", "thiserror", "tiny-skia", + "url", "wayland-client", "wayland-egl", "wayland-protocols", diff --git a/window/Cargo.toml b/window/Cargo.toml index 4f3bd63aa..e6aa63b85 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -40,6 +40,7 @@ resize = "0.5" serde = {version="1.0", features = ["rc", "derive"]} tiny-skia = "0.6" glium = { version = "0.31", default-features = false } +url = "2" wezterm-bidi = { path = "../bidi" } wezterm-color-types = { path = "../color-types" } wezterm-font = { path = "../wezterm-font" } diff --git a/window/src/os/wayland/copy_and_paste.rs b/window/src/os/wayland/copy_and_paste.rs index ffd00f1eb..37e21a1bb 100644 --- a/window/src/os/wayland/copy_and_paste.rs +++ b/window/src/os/wayland/copy_and_paste.rs @@ -140,11 +140,8 @@ impl CopyAndPaste { offer.accept(self.last_serial, None); } } - DataOfferEvent::SourceActions { source_actions } => { - log::error!("Offer source_actions {:?}", source_actions); - } - DataOfferEvent::Action { dnd_action } => { - log::error!("Offer dnd_action {:?}", dnd_action); + DataOfferEvent::SourceActions { .. } | DataOfferEvent::Action { .. } => { + // ignore drag and drop events } _ => {} } diff --git a/window/src/os/wayland/drag_and_drop.rs b/window/src/os/wayland/drag_and_drop.rs new file mode 100644 index 000000000..2df5030f8 --- /dev/null +++ b/window/src/os/wayland/drag_and_drop.rs @@ -0,0 +1,119 @@ +use crate::connection::ConnectionOps; +use crate::wayland::{read_pipe_with_timeout, WaylandConnection}; +use filedescriptor::{FileDescriptor, Pipe}; +use smithay_client_toolkit as toolkit; +use std::os::unix::io::AsRawFd; +use std::path::PathBuf; +use toolkit::reexports::client::protocol::wl_data_device::Event as DataDeviceEvent; +use toolkit::reexports::client::protocol::wl_data_offer::WlDataOffer; +use url::Url; +use wayland_client::protocol::wl_data_device_manager::DndAction; + +#[derive(Default)] +pub struct DragAndDrop { + offer: Option, +} + +struct SurfaceAndOffer { + surface_id: u32, + offer: WlDataOffer, +} + +struct SurfaceAndPipe { + surface_id: u32, + read: FileDescriptor, +} + +pub const URI_MIME_TYPE: &str = "text/uri-list"; + +impl DragAndDrop { + /// Takes the current offer, if any, and initiates a receive into a pipe, + /// returning that surface and pipe descriptor. + fn create_pipe_for_drop(&mut self) -> Option { + let SurfaceAndOffer { surface_id, offer } = self.offer.take()?; + let pipe = Pipe::new() + .map_err(|err| log::error!("Unable to create pipe: {:#}", err)) + .ok()?; + offer.receive(URI_MIME_TYPE.to_string(), pipe.write.as_raw_fd()); + let read = pipe.read; + offer.finish(); + Some(SurfaceAndPipe { surface_id, read }) + } + + fn read_paths_from_pipe(read: FileDescriptor) -> Option> { + read_pipe_with_timeout(read) + .map_err(|err| { + log::error!("Error while reading pipe from drop result: {:#}", err); + }) + .ok()? + .lines() + .filter_map(|line| { + if line.starts_with('#') || line.trim().is_empty() { + // text/uri-list: Any lines beginning with the '#' character + // are comment lines and are ignored during processing + return None; + } + let url = Url::parse(line) + .map_err(|err| { + log::error!("Error parsing dropped file line {} as url: {:#}", line, err); + }) + .ok()?; + url.to_file_path() + .map_err(|_| { + log::error!("Error converting url {} from line {} to pathbuf", url, line); + }) + .ok() + }) + .collect::>() + .into() + } + + fn dispatch_dropped_files(surface_id: u32, paths: Vec) { + promise::spawn::spawn_into_main_thread(async move { + let conn = WaylandConnection::get().unwrap().wayland(); + if let Some(&window_id) = conn.surface_to_window_id.borrow().get(&surface_id) { + if let Some(handle) = conn.window_by_id(window_id) { + let mut inner = handle.borrow_mut(); + inner.dispatch_dropped_files(paths); + } + }; + }) + .detach(); + } + + pub fn handle_data_event(&mut self, event: DataDeviceEvent) { + match event { + DataDeviceEvent::Enter { + serial, + surface, + id, + .. + } => { + if let Some(offer) = id { + offer.accept(serial, Some(URI_MIME_TYPE.to_string())); + offer.set_actions(DndAction::None | DndAction::Copy, DndAction::None); + self.offer = Some(SurfaceAndOffer { + surface_id: surface.as_ref().id(), + offer, + }); + } + } + DataDeviceEvent::Leave => { + if let Some(SurfaceAndOffer { offer, .. }) = self.offer.take() { + offer.destroy(); + } + } + DataDeviceEvent::Motion { .. } => {} + DataDeviceEvent::Drop => { + if let Some(SurfaceAndPipe { surface_id, read }) = self.create_pipe_for_drop() { + std::thread::spawn(move || { + if let Some(paths) = Self::read_paths_from_pipe(read) { + Self::dispatch_dropped_files(surface_id, paths); + } + }); + } + } + _ => {} + } + } +} diff --git a/window/src/os/wayland/mod.rs b/window/src/os/wayland/mod.rs index ea567eedc..093618831 100644 --- a/window/src/os/wayland/mod.rs +++ b/window/src/os/wayland/mod.rs @@ -5,5 +5,6 @@ pub mod window; pub use self::window::*; pub use connection::*; mod copy_and_paste; +mod drag_and_drop; mod frame; mod pointer; diff --git a/window/src/os/wayland/pointer.rs b/window/src/os/wayland/pointer.rs index ebda5c1fb..c619480ac 100644 --- a/window/src/os/wayland/pointer.rs +++ b/window/src/os/wayland/pointer.rs @@ -1,4 +1,5 @@ use super::copy_and_paste::*; +use super::drag_and_drop::*; use crate::os::wayland::connection::WaylandConnection; use smithay_client_toolkit as toolkit; use std::collections::HashMap; @@ -22,6 +23,7 @@ use wezterm_input_types::*; struct Inner { active_surface_id: u32, surface_to_pending: HashMap>>, + drag_and_drop: DragAndDrop, serial: u32, } @@ -72,10 +74,13 @@ impl Inner { } }); } + DataDeviceEvent::Enter { .. } - | DataDeviceEvent::Leave { .. } + | DataDeviceEvent::Leave | DataDeviceEvent::Motion { .. } - | DataDeviceEvent::Drop => {} + | DataDeviceEvent::Drop => { + self.drag_and_drop.handle_data_event(event); + } DataDeviceEvent::Selection { id } => { if let Some(offer) = id { diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index af1834b0c..ae709342f 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -22,6 +22,7 @@ use std::cell::RefCell; use std::convert::TryInto; use std::io::Read; use std::os::unix::io::AsRawFd; +use std::path::PathBuf; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -468,6 +469,10 @@ impl WaylandWindowInner { self.events.dispatch(WindowEvent::FocusChanged(focused)); } + pub(crate) fn dispatch_dropped_files(&mut self, paths: Vec) { + self.events.dispatch(WindowEvent::DroppedFile(paths)); + } + pub(crate) fn dispatch_pending_mouse(&mut self) { // Dancing around the borrow checker and the call to self.refresh_frame() let pending_mouse = Arc::clone(&self.pending_mouse); @@ -968,7 +973,7 @@ impl WindowOps for WaylandWindow { } } -fn read_pipe_with_timeout(mut file: FileDescriptor) -> anyhow::Result { +pub(crate) fn read_pipe_with_timeout(mut file: FileDescriptor) -> anyhow::Result { let mut result = Vec::new(); file.set_non_blocking(true)?;