From 5bfbaae180c401968b314366f7fa9e1f0303c875 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 26 Mar 2024 17:32:09 -0400 Subject: [PATCH] refactor: update smithay-clipboard --- dnd/src/lib.rs | 4 ++-- dnd/src/platform/linux.rs | 3 ++- macos/src/lib.rs | 8 +++---- mime/src/lib.rs | 17 ++++++++++++++ src/dnd/mod.rs | 4 ++-- src/platform/linux.rs | 2 +- wayland/src/lib.rs | 13 ++++++----- x11/src/error.rs | 6 +++-- x11/src/lib.rs | 48 +++++++++++++++++++++++++-------------- 9 files changed, 70 insertions(+), 35 deletions(-) diff --git a/dnd/src/lib.rs b/dnd/src/lib.rs index 192acae..1a17bd8 100644 --- a/dnd/src/lib.rs +++ b/dnd/src/lib.rs @@ -99,8 +99,8 @@ pub trait Sender { pub trait RawSurface { /// # Safety /// - /// returned pointer must be a valid pointer to the underlying surface, and it must - /// remain valid for as long as `RawSurface` object is alive. + /// returned pointer must be a valid pointer to the underlying surface, and + /// it must remain valid for as long as `RawSurface` object is alive. unsafe fn get_ptr(&mut self) -> *mut c_void; } diff --git a/dnd/src/platform/linux.rs b/dnd/src/platform/linux.rs index bcb7db0..9036dd0 100644 --- a/dnd/src/platform/linux.rs +++ b/dnd/src/platform/linux.rs @@ -44,7 +44,8 @@ impl AsMimeTypes for DataWrapper { impl smithay_clipboard::dnd::RawSurface for DndSurface { unsafe fn get_ptr(&mut self) -> *mut c_void { - // XXX won't panic because this is only called once before it could be cloned + // XXX won't panic because this is only called once before it could be + // cloned Arc::get_mut(&mut self.0).unwrap().get_ptr() } } diff --git a/macos/src/lib.rs b/macos/src/lib.rs index ca9047e..deeb52f 100644 --- a/macos/src/lib.rs +++ b/macos/src/lib.rs @@ -15,11 +15,11 @@ extern crate objc; use objc::runtime::{Class, Object}; -use objc_foundation::{INSArray, INSObject, INSString}; -use objc_foundation::{NSArray, NSDictionary, NSObject, NSString}; +use objc_foundation::{ + INSArray, INSObject, INSString, NSArray, NSDictionary, NSObject, NSString, +}; use objc_id::{Id, Owned}; -use std::error::Error; -use std::mem::transmute; +use std::{error::Error, mem::transmute}; pub struct Clipboard { pasteboard: Id, diff --git a/mime/src/lib.rs b/mime/src/lib.rs index 98f8d29..983827d 100644 --- a/mime/src/lib.rs +++ b/mime/src/lib.rs @@ -4,6 +4,23 @@ pub mod platform; use std::{borrow::Cow, error, fmt}; +/// Raw data from the clipboard +pub struct ClipboardData(pub Vec, pub String); + +impl AllowedMimeTypes for ClipboardData { + fn allowed() -> Cow<'static, [String]> { + Cow::Owned(vec![]) + } +} + +impl TryFrom<(Vec, String)> for ClipboardData { + type Error = Error; + + fn try_from((data, mime): (Vec, String)) -> Result { + Ok(ClipboardData(data, mime)) + } +} + /// Data that can be loaded from the clipboard. pub struct ClipboardLoadData(pub T); diff --git a/src/dnd/mod.rs b/src/dnd/mod.rs index c43a76e..f06f0d1 100644 --- a/src/dnd/mod.rs +++ b/src/dnd/mod.rs @@ -43,7 +43,7 @@ pub trait DndProvider { /// Peek at the contents of a DnD offer fn peek_offer( &self, - _mime_type: Cow<'static, str>, + _mime_type: Option>, ) -> std::io::Result { Err(std::io::Error::new( std::io::ErrorKind::Other, @@ -95,7 +95,7 @@ impl DndProvider for crate::PlatformClipboard { fn peek_offer( &self, - mime_type: Cow<'static, str>, + mime_type: Option>, ) -> std::io::Result { self.raw.peek_offer::(mime_type) } diff --git a/src/platform/linux.rs b/src/platform/linux.rs index d3cb4f2..9ea9b41 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -189,7 +189,7 @@ impl DndProvider for Clipboard { fn peek_offer( &self, - mime_type: Cow<'static, str>, + mime_type: Option>, ) -> std::io::Result { match self { Clipboard::Wayland(c) => c.peek_offer::(mime_type), diff --git a/wayland/src/lib.rs b/wayland/src/lib.rs index 94ef900..ae94714 100644 --- a/wayland/src/lib.rs +++ b/wayland/src/lib.rs @@ -22,6 +22,7 @@ use std::{ use dnd::{ DataWrapper, DndAction, DndDestinationRectangle, DndSurface, Sender, }; +use mime::ClipboardData; use smithay_clipboard::dnd::Rectangle; pub use smithay_clipboard::mime::{AllowedMimeTypes, AsMimeTypes, MimeType}; @@ -178,13 +179,13 @@ impl Clipboard { .context .lock() .unwrap() - .load_primary_raw( + .load_primary_mime::>( allowed .into_iter() .map(|s| MimeType::from(Cow::Owned(s))) .collect::>(), ) - .map(|(d, m)| (d, m.to_string()))?) + .map(|d| (d.0 .0, d.0 .1.to_string()))?) } pub fn read_raw( @@ -195,13 +196,13 @@ impl Clipboard { .context .lock() .unwrap() - .load_raw( + .load_mime::>( allowed .into_iter() .map(|s| MimeType::from(Cow::Owned(s))) .collect::>(), ) - .map(|(d, m)| (d, m.to_string()))?) + .map(|d| (d.0 .0, d.0 .1))?) } pub fn init_dnd(&self, tx: DndSender) { @@ -257,13 +258,13 @@ impl Clipboard { /// Peek at the contents of a DnD offer pub fn peek_offer( &self, - mime_type: Cow<'static, str>, + mime_type: Option>, ) -> std::io::Result { let d = self .context .lock() .unwrap() - .peek_offer::>(mime_type.into()); + .peek_offer::>(mime_type.map(MimeType::from)); d.map(|d| d.0) } } diff --git a/x11/src/error.rs b/x11/src/error.rs index cf04d2f..a08b07a 100644 --- a/x11/src/error.rs +++ b/x11/src/error.rs @@ -1,5 +1,7 @@ -use x11rb::errors::{ConnectError, ConnectionError, ReplyError}; -use x11rb::protocol::xproto::Atom; +use x11rb::{ + errors::{ConnectError, ConnectionError, ReplyError}, + protocol::xproto::Atom, +}; use std::sync::mpsc; diff --git a/x11/src/lib.rs b/x11/src/lib.rs index 0a1e310..f951252 100644 --- a/x11/src/lib.rs +++ b/x11/src/lib.rs @@ -3,17 +3,23 @@ mod error; pub use error::Error; -use x11rb::connection::Connection as _; -use x11rb::errors::ConnectError; -use x11rb::protocol::xproto::{self, Atom, AtomEnum, EventMask, Window}; -use x11rb::protocol::Event; -use x11rb::rust_connection::RustConnection as Connection; -use x11rb::wrapper::ConnectionExt; +use x11rb::{ + connection::Connection as _, + errors::ConnectError, + protocol::{ + xproto::{self, Atom, AtomEnum, EventMask, Window}, + Event, + }, + rust_connection::RustConnection as Connection, + wrapper::ConnectionExt, +}; -use std::collections::HashMap; -use std::sync::{Arc, RwLock}; -use std::thread; -use std::time::{Duration, Instant}; +use std::{ + collections::HashMap, + sync::{Arc, RwLock}, + thread, + time::{Duration, Instant}, +}; const POLL_DURATION: std::time::Duration = Duration::from_micros(50); @@ -60,13 +66,16 @@ impl Clipboard { self.read_selection(self.reader.atoms.clipboard) } - /// Read the current PRIMARY [`Clipboard`] value. pub fn read_primary(&self) -> Result { self.read_selection(self.reader.atoms.primary) } - fn write_selection(&mut self, selection: Atom, contents: String) -> Result<(), Error> { + fn write_selection( + &mut self, + selection: Atom, + contents: String, + ) -> Result<(), Error> { let target = self.writer.atoms.utf8_string; self.selections @@ -124,9 +133,13 @@ impl Clipboard { selection, target, property, - x11rb::CURRENT_TIME, // FIXME ^ - // Clients should not use CurrentTime for the time argument of a ConvertSelection request. - // Instead, they should use the timestamp of the event that caused the request to be made. + x11rb::CURRENT_TIME, /* FIXME ^ + * Clients should not use CurrentTime for + * the time argument of a ConvertSelection + * request. + * Instead, they should use the timestamp + * of the event that caused the request to + * be made. */ )?; let _ = self.reader.connection.flush()?; @@ -186,8 +199,9 @@ impl Clipboard { continue; }; - // Note that setting the property argument to None indicates that the - // conversion requested could not be made. + // Note that setting the property argument to None indicates + // that the conversion requested could + // not be made. if event.property == AtomEnum::NONE.into() { break; }