support drag and drop files for wayland

This commit is contained in:
Funami580 authored and Wez Furlong committed 2022-06-20 13:25:32 -07:00
1 parent b3e160ff4a
commit b29089ebdc
7 files changed
+137 -8

No files matched your search

Generated
+1
View File
@@ -5030,6 +5030,7 @@ dependencies = [
"smithay-client-toolkit",
"thiserror",
"tiny-skia",
"url",
"wayland-client",
"wayland-egl",
"wayland-protocols",
+1
View File
@@ -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" }
+2 -5
View File
@@ -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
}
_ => {}
}
+119
View File
@@ -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<SurfaceAndOffer>,
}
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<SurfaceAndPipe> {
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<Vec<PathBuf>> {
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::<Vec<_>>()
.into()
}
fn dispatch_dropped_files(surface_id: u32, paths: Vec<PathBuf>) {
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);
}
});
}
}
_ => {}
}
}
}
+1
View File
@@ -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;
+7 -2
View File
@@ -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<u32, Arc<Mutex<PendingMouse>>>,
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 {
+6 -1
View File
@@ -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<PathBuf>) {
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<String> {
pub(crate) fn read_pipe_with_timeout(mut file: FileDescriptor) -> anyhow::Result<String> {
let mut result = Vec::new();
file.set_non_blocking(true)?;