From c9e7a8e2fae144336dd45835545216b521d1d2ac Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 29 Mar 2024 15:48:29 -0400 Subject: [PATCH] refactor: use rwh --- dnd/Cargo.toml | 5 ++- dnd/src/lib.rs | 91 +++++++++++++++++++++++++++++++++------ dnd/src/platform/linux.rs | 7 ++- mime/Cargo.toml | 2 +- wayland/Cargo.toml | 2 +- 5 files changed, 89 insertions(+), 18 deletions(-) diff --git a/dnd/Cargo.toml b/dnd/Cargo.toml index 202a186..a9e3b34 100644 --- a/dnd/Cargo.toml +++ b/dnd/Cargo.toml @@ -6,11 +6,12 @@ edition = "2021" [dependencies] mime = { path = "../mime" } bitflags = "2.5.0" +raw-window-handle = "0.6" [target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies] -smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", branch = "dnd", features = [ +smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd", features = [ "dnd", ] } sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit", default-features = false, features = [ "calloop", -] } +], rev = "3bed072" } diff --git a/dnd/src/lib.rs b/dnd/src/lib.rs index c1b7ac3..16c99f9 100644 --- a/dnd/src/lib.rs +++ b/dnd/src/lib.rs @@ -1,10 +1,11 @@ use std::{ borrow::Cow, - ffi::c_void, + fmt::Debug, sync::{mpsc::SendError, Arc}, }; use bitflags::bitflags; +use raw_window_handle::HasWindowHandle; #[cfg(all( unix, @@ -30,7 +31,7 @@ bitflags! { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum DndEvent { /// Dnd Offer event with the corresponding destination rectangle ID. Offer(Option, OfferEvent), @@ -38,7 +39,19 @@ pub enum DndEvent { Source(SourceEvent), } -#[derive(Debug)] +impl PartialEq for DndEvent { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (DndEvent::Offer(a, b), DndEvent::Offer(a2, b2)) => { + a == a2 && b == b2 + } + (DndEvent::Source(a), DndEvent::Source(b)) => a == b, + _ => false, + } + } +} + +#[derive(Debug, Clone)] pub enum SourceEvent { /// DnD operation ended. Finished, @@ -54,7 +67,20 @@ pub enum SourceEvent { Dropped, } -#[derive(Debug)] +impl PartialEq for SourceEvent { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (SourceEvent::Finished, SourceEvent::Finished) + | (SourceEvent::Cancelled, SourceEvent::Cancelled) + | (SourceEvent::Dropped, SourceEvent::Dropped) => true, + (SourceEvent::Action(a), SourceEvent::Action(b)) => a == b, + (SourceEvent::Mime(a), SourceEvent::Mime(b)) => a == b, + _ => false, + } + } +} + +#[derive(Debug, Clone)] pub enum OfferEvent { Enter { x: f64, @@ -82,6 +108,45 @@ pub enum OfferEvent { }, } +impl PartialEq for OfferEvent { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + ( + OfferEvent::Enter { + x, + y, + mime_types, + surface: _, + }, + OfferEvent::Enter { + x: x2, + y: y2, + mime_types: mime_types2, + surface: _, + }, + ) => x == x2 && y == y2 && mime_types == mime_types2, + ( + OfferEvent::Motion { x, y }, + OfferEvent::Motion { x: x2, y: y2 }, + ) => x == x2 && y == y2, + (OfferEvent::LeaveDestination, OfferEvent::LeaveDestination) + | (OfferEvent::Leave, OfferEvent::Leave) + | (OfferEvent::Drop, OfferEvent::Drop) => true, + (OfferEvent::SelectedAction(a), OfferEvent::SelectedAction(b)) => { + a == b + } + ( + OfferEvent::Data { data, mime_type }, + OfferEvent::Data { + data: data2, + mime_type: mime_type2, + }, + ) => data == data2 && mime_type == mime_type2, + _ => false, + } + } +} + /// A rectangle with a logical location and size relative to a [`DndSurface`] #[derive(Debug, Default, Clone)] pub struct Rectangle { @@ -96,14 +161,6 @@ pub trait Sender { fn send(&self, t: DndEvent) -> Result<(), SendError>>; } -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. - unsafe fn get_ptr(&mut self) -> *mut c_void; -} - /// A rectangle with a logical location and size relative to a [`DndSurface`] #[derive(Debug, Clone)] pub struct DndDestinationRectangle { @@ -132,7 +189,15 @@ pub enum Icon { } #[derive(Clone)] -pub struct DndSurface(pub Arc>); +pub struct DndSurface( + pub Arc>, +); + +impl Debug for DndSurface { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DndSurface").finish() + } +} #[derive(Clone)] pub struct DataWrapper(pub T); diff --git a/dnd/src/platform/linux.rs b/dnd/src/platform/linux.rs index 5a5766e..abafb4b 100644 --- a/dnd/src/platform/linux.rs +++ b/dnd/src/platform/linux.rs @@ -1,6 +1,7 @@ use std::{borrow::Cow, ffi::c_void, sync::Arc}; use crate::{DataWrapper, DndAction, DndSurface, Icon}; +use raw_window_handle::HasWindowHandle; use smithay_clipboard::mime::{AllowedMimeTypes, AsMimeTypes, MimeType}; impl< @@ -46,7 +47,11 @@ 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 - Arc::get_mut(&mut self.0).unwrap().get_ptr() + Arc::get_mut(&mut self.0) + .unwrap() + .window_handle() + .unwrap() + .get_ptr() } } diff --git a/mime/Cargo.toml b/mime/Cargo.toml index 247c364..2d94eef 100644 --- a/mime/Cargo.toml +++ b/mime/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten", target_os="ios", target_os="redox"))))'.dependencies] -smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", branch = "dnd" } +smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd" } diff --git a/wayland/Cargo.toml b/wayland/Cargo.toml index 6e0c01d..3f2b079 100644 --- a/wayland/Cargo.toml +++ b/wayland/Cargo.toml @@ -11,7 +11,7 @@ keywords = ["clipboard", "wayland"] [dependencies] -smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", branch = "dnd", features = [ +smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard", tag = "pop-dnd", features = [ "dnd", ] } mime = { path = "../mime" }