Repository navigation
feat: data device handling #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3b864f4
feat: data_device with abstractions and version checks
wash2 31975f9
chore: update data_device example
wash2 de8975c
cleanup(data_device): improve comments and Option assignments
wash2 c8c0522
fix: improve example mime type & error handling
wash2 b55947c
refactor(selection): move unset_selection to DataDevice
wash2 e741b2b
feat(data source): derive Clone on sources
wash2 c206c37
refactor: separate mime types from each enum variant and improve example
wash2 9ef0763
refactor: DataOfferHandler actions handlers should always be given a …
wash2 e40f4cf
cleanup: clippy
wash2 9e13a76
cleanup: remove the option from drag offer and selection offer & bett…
wash2 55837b0
update to handle new configure
wash2 51c83b3
feat: add WritePipe
wash2 b93dfac
cleanup (data-device): comments
wash2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,295 @@ | ||
| use std::sync::Mutex; | ||
|
|
||
| use super::{ | ||
| data_offer::{DataOfferData, DataOfferDataExt, DataOfferHandler, DragOffer, SelectionOffer}, | ||
| DataDeviceManagerState, | ||
| }; | ||
| use std::sync::Arc; | ||
| use wayland_client::{ | ||
| event_created_child, | ||
| protocol::{ | ||
| wl_data_device::{self, WlDataDevice}, | ||
| wl_data_offer::{self, WlDataOffer}, | ||
| }, | ||
| Connection, Dispatch, Proxy, QueueHandle, | ||
| }; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct DataDevice { | ||
| pub(crate) device: WlDataDevice, | ||
| } | ||
|
|
||
| impl DataDevice { | ||
| pub fn release(&self) { | ||
| if self.device.version() >= 2 { | ||
| self.device.release() | ||
| } | ||
| } | ||
|
|
||
| pub fn data(&self) -> Option<&DataDeviceData> { | ||
| self.device.data() | ||
| } | ||
|
|
||
| /// Unset the selection of the provided data device as a response to the event with with provided serial. | ||
| pub fn unset_selection(&self, serial: u32) { | ||
| self.device.set_selection(None, serial); | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct DataDeviceInner { | ||
| /// the active dnd offer and its data | ||
| pub drag_offer: Arc<Mutex<Option<WlDataOffer>>>, | ||
| /// the active selection offer and its data | ||
| pub selection_offer: Arc<Mutex<Option<WlDataOffer>>>, | ||
| /// the active undetermined offers and their data | ||
| pub undetermined_offers: Arc<Mutex<Vec<WlDataOffer>>>, | ||
| } | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct DataDeviceData { | ||
| pub(super) inner: Arc<Mutex<DataDeviceInner>>, | ||
| } | ||
|
|
||
| pub trait DataDeviceDataExt: Send + Sync { | ||
| type DataOfferInner: DataOfferDataExt + Send + Sync + 'static; | ||
|
|
||
| fn data_device_data(&self) -> &DataDeviceData; | ||
|
|
||
| fn selection_mime_types(&self) -> Vec<String> { | ||
| let inner = self.data_device_data(); | ||
| inner | ||
| .inner | ||
| .lock() | ||
| .unwrap() | ||
| .selection_offer | ||
| .lock() | ||
| .unwrap() | ||
| .as_ref() | ||
| .map(|offer| { | ||
| let data = offer.data::<Self::DataOfferInner>().unwrap(); | ||
| data.mime_types() | ||
| }) | ||
| .unwrap_or_default() | ||
| } | ||
|
|
||
| fn drag_mime_types(&self) -> Vec<String> { | ||
| let inner = self.data_device_data(); | ||
| inner | ||
| .inner | ||
| .lock() | ||
| .unwrap() | ||
| .drag_offer | ||
| .lock() | ||
| .unwrap() | ||
| .as_ref() | ||
| .map(|offer| { | ||
| let data = offer.data::<Self::DataOfferInner>().unwrap(); | ||
| data.mime_types() | ||
| }) | ||
| .unwrap_or_default() | ||
| } | ||
|
|
||
| /// Get the active dnd offer if it exists. | ||
| fn drag_offer(&self) -> Option<DragOffer> { | ||
| let inner = self.data_device_data(); | ||
| inner.inner.lock().unwrap().drag_offer.lock().unwrap().as_ref().and_then(|offer| { | ||
| let data = offer.data::<Self::DataOfferInner>().unwrap(); | ||
| data.as_drag_offer() | ||
| }) | ||
| } | ||
|
|
||
| /// Get the active selection offer if it exists. | ||
| fn selection_offer(&self) -> Option<SelectionOffer> { | ||
| let inner = self.data_device_data(); | ||
| inner.inner.lock().unwrap().selection_offer.lock().unwrap().as_ref().and_then(|offer| { | ||
| let data = offer.data::<Self::DataOfferInner>().unwrap(); | ||
| data.as_selection_offer() | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl DataDeviceDataExt for DataDevice { | ||
| type DataOfferInner = DataOfferData; | ||
| fn data_device_data(&self) -> &DataDeviceData { | ||
| self.device.data().unwrap() | ||
| } | ||
| } | ||
|
|
||
| impl DataDeviceDataExt for DataDeviceData { | ||
| type DataOfferInner = DataOfferData; | ||
| fn data_device_data(&self) -> &DataDeviceData { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| /// Handler trait for DataDevice events. | ||
| /// | ||
| /// The functions defined in this trait are called as DataDevice events are received from the compositor. | ||
| pub trait DataDeviceHandler: Sized { | ||
| // Introduces a new data offer | ||
| // ASHLEY left out because the data offer will be introduced to the user once the type is known | ||
| // either through the enter method or the selection method. | ||
| // fn data_offer( | ||
| // &mut self, | ||
| // conn: &Connection, | ||
| // qh: &QueueHandle<Self>, | ||
| // data_device: DataDevice, | ||
| // offer: WlDataOffer, | ||
| // ); | ||
|
|
||
| /// The data device pointer has entered a surface at the provided location | ||
| fn enter(&mut self, conn: &Connection, qh: &QueueHandle<Self>, data_device: DataDevice); | ||
|
|
||
| /// The drag and drop pointer has left the surface and the session ends. | ||
| /// The offer will be destroyed. | ||
| fn leave(&mut self, conn: &Connection, qh: &QueueHandle<Self>, data_device: DataDevice); | ||
|
|
||
| /// Drag and Drop motion. | ||
| fn motion(&mut self, conn: &Connection, qh: &QueueHandle<Self>, data_device: DataDevice); | ||
|
|
||
| /// Advertises a new selection. | ||
| fn selection(&mut self, conn: &Connection, qh: &QueueHandle<Self>, data_device: DataDevice); | ||
|
|
||
| /// Drop performed. | ||
| /// After the next data offer action event, data may be able to be received, unless the action is "ask". | ||
| fn drop_performed( | ||
| &mut self, | ||
| conn: &Connection, | ||
| qh: &QueueHandle<Self>, | ||
| data_device: DataDevice, | ||
| ); | ||
| } | ||
|
|
||
| impl<D, U, V> Dispatch<wl_data_device::WlDataDevice, U, D> for DataDeviceManagerState<V> | ||
| where | ||
| D: Dispatch<wl_data_device::WlDataDevice, U> | ||
| + Dispatch<wl_data_offer::WlDataOffer, V> | ||
| + DataDeviceHandler | ||
| + DataOfferHandler | ||
| + 'static, | ||
| U: DataDeviceDataExt, | ||
| V: DataOfferDataExt + Default + 'static + Send + Sync, | ||
| { | ||
| event_created_child!(D, WlDataDevice, [ | ||
| 0 => (WlDataOffer, V::default()) | ||
| ]); | ||
|
|
||
| fn event( | ||
| state: &mut D, | ||
| data_device: &wl_data_device::WlDataDevice, | ||
| event: wl_data_device::Event, | ||
| data: &U, | ||
| conn: &Connection, | ||
| qh: &QueueHandle<D>, | ||
| ) { | ||
| let data = data.data_device_data(); | ||
| let inner = data.inner.lock().unwrap(); | ||
|
|
||
| match event { | ||
| wayland_client::protocol::wl_data_device::Event::DataOffer { id } => { | ||
| // XXX Drop done here to prevent Mutex deadlocks.S | ||
|
|
||
| inner.undetermined_offers.lock().unwrap().push(id.clone()); | ||
| let data = id.data::<V>().unwrap().data_offer_data(); | ||
| data.init_undetermined_offer(&id); | ||
|
|
||
| // Append the data offer to our list of offers. | ||
| drop(inner); | ||
| } | ||
| wayland_client::protocol::wl_data_device::Event::Enter { | ||
| serial, | ||
| surface, | ||
| x, | ||
| y, | ||
| id, | ||
| } => { | ||
| let mut drag_offer = inner.drag_offer.lock().unwrap(); | ||
|
|
||
| if let Some(offer) = id { | ||
| let mut undetermined = inner.undetermined_offers.lock().unwrap(); | ||
| if let Some(i) = undetermined.iter().position(|o| o == &offer) { | ||
| undetermined.remove(i); | ||
| } | ||
| drop(undetermined); | ||
|
|
||
| let data = offer.data::<V>().unwrap().data_offer_data(); | ||
| data.to_dnd_offer(serial, surface, x, y, None); | ||
|
|
||
| // XXX Drop done here to prevent Mutex deadlocks. | ||
| *drag_offer = Some(offer.clone()); | ||
| drop(drag_offer); | ||
| drop(inner); | ||
| state.enter(conn, qh, DataDevice { device: data_device.clone() }); | ||
| } else { | ||
| *drag_offer = None; | ||
| } | ||
| } | ||
| wayland_client::protocol::wl_data_device::Event::Leave => { | ||
| // XXX Drop done here to prevent Mutex deadlocks. | ||
| inner.drag_offer.lock().unwrap().take(); | ||
| drop(inner); | ||
| state.leave(conn, qh, DataDevice { device: data_device.clone() }); | ||
| } | ||
| wayland_client::protocol::wl_data_device::Event::Motion { time, x, y } => { | ||
| let mut drag = inner.drag_offer.lock().unwrap(); | ||
| if let Some(offer) = drag.take() { | ||
| let data = offer.data::<V>().unwrap().data_offer_data(); | ||
| data.motion(x, y, time); | ||
| *drag = Some(offer); | ||
| } | ||
| // Update the data offer location. | ||
| // XXX Drop done here to prevent Mutex deadlocks. | ||
| drop(drag); | ||
| drop(inner); | ||
| state.motion(conn, qh, DataDevice { device: data_device.clone() }); | ||
| } | ||
| wayland_client::protocol::wl_data_device::Event::Drop => { | ||
| // XXX Drop done here to prevent Mutex deadlocks. | ||
| // Pass the info about the drop to the user. | ||
| drop(inner); | ||
| state.drop_performed(conn, qh, DataDevice { device: data_device.clone() }); | ||
| } | ||
| wayland_client::protocol::wl_data_device::Event::Selection { id } => { | ||
| let mut selection_offer = inner.selection_offer.lock().unwrap(); | ||
|
|
||
| if let Some(offer) = id { | ||
| let mut undetermined = inner.undetermined_offers.lock().unwrap(); | ||
| if let Some(i) = undetermined.iter().position(|o| o == &offer) { | ||
| undetermined.remove(i); | ||
| } | ||
| drop(undetermined); | ||
|
|
||
| let data = offer.data::<V>().unwrap().data_offer_data(); | ||
| data.to_selection_offer(); | ||
| // XXX Drop done here to prevent Mutex deadlocks. | ||
| *selection_offer = Some(offer.clone()); | ||
| drop(selection_offer); | ||
| drop(inner); | ||
| state.selection(conn, qh, DataDevice { device: data_device.clone() }); | ||
| } else { | ||
| *selection_offer = None; | ||
| } | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[macro_export] | ||
| macro_rules! delegate_data_device { | ||
| ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty, udata: [$($udata: ty),*$(,)?]) => { | ||
| $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: | ||
| [ | ||
| $crate::reexports::client::protocol::wl_data_device::WlDataDevice: $udata, | ||
| ] => $crate::data_device_manager::DataDeviceManagerState | ||
| ); | ||
| }; | ||
| ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { | ||
| $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: | ||
| [ | ||
| $crate::reexports::client::protocol::wl_data_device::WlDataDevice: $crate::data_device_manager::data_device::DataDeviceData | ||
| ] => $crate::data_device_manager::DataDeviceManagerState | ||
| ); | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.