| 1 | use crate::globals::GlobalData; |
| 2 | use crate::reexports::client::{ |
| 3 | globals::{BindError, GlobalList}, |
| 4 | protocol::wl_seat::WlSeat, |
| 5 | Dispatch, QueueHandle, |
| 6 | }; |
| 7 | use crate::reexports::protocols::wp::primary_selection::zv1::client::{ |
| 8 | zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1, |
| 9 | zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1, |
| 10 | zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1, |
| 11 | }; |
| 12 | |
| 13 | pub mod device; |
| 14 | pub mod offer; |
| 15 | pub mod selection; |
| 16 | |
| 17 | use self::device::{PrimarySelectionDevice, PrimarySelectionDeviceData}; |
| 18 | use selection::PrimarySelectionSource; |
| 19 | |
| 20 | #[derive (Debug)] |
| 21 | pub struct PrimarySelectionManagerState { |
| 22 | manager: ZwpPrimarySelectionDeviceManagerV1, |
| 23 | } |
| 24 | |
| 25 | impl PrimarySelectionManagerState { |
| 26 | pub fn bind<State>(globals: &GlobalList, qh: &QueueHandle<State>) -> Result<Self, BindError> |
| 27 | where |
| 28 | State: Dispatch<ZwpPrimarySelectionDeviceManagerV1, GlobalData, State> + 'static, |
| 29 | { |
| 30 | let manager = globals.bind(qh, 1..=1, GlobalData)?; |
| 31 | Ok(Self { manager }) |
| 32 | } |
| 33 | |
| 34 | /// The underlying wayland object. |
| 35 | pub fn primary_selection_manager(&self) -> &ZwpPrimarySelectionDeviceManagerV1 { |
| 36 | &self.manager |
| 37 | } |
| 38 | |
| 39 | /// Create a primary selection source. |
| 40 | pub fn create_selection_source<State, I, T>( |
| 41 | &self, |
| 42 | qh: &QueueHandle<State>, |
| 43 | mime_types: I, |
| 44 | ) -> PrimarySelectionSource |
| 45 | where |
| 46 | State: Dispatch<ZwpPrimarySelectionSourceV1, GlobalData, State> + 'static, |
| 47 | I: IntoIterator<Item = T>, |
| 48 | T: ToString, |
| 49 | { |
| 50 | let source = self.manager.create_source(qh, GlobalData); |
| 51 | |
| 52 | for mime_type in mime_types { |
| 53 | source.offer(mime_type.to_string()); |
| 54 | } |
| 55 | |
| 56 | PrimarySelectionSource::new(source) |
| 57 | } |
| 58 | |
| 59 | /// Get the primary selection data device for the given seat. |
| 60 | pub fn get_selection_device<State>( |
| 61 | &self, |
| 62 | qh: &QueueHandle<State>, |
| 63 | seat: &WlSeat, |
| 64 | ) -> PrimarySelectionDevice |
| 65 | where |
| 66 | State: Dispatch<ZwpPrimarySelectionDeviceV1, PrimarySelectionDeviceData, State> + 'static, |
| 67 | { |
| 68 | PrimarySelectionDevice { |
| 69 | device: self.manager.get_device( |
| 70 | seat, |
| 71 | qh, |
| 72 | PrimarySelectionDeviceData::new(seat.clone()), |
| 73 | ), |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl Drop for PrimarySelectionManagerState { |
| 79 | fn drop(&mut self) { |
| 80 | self.manager.destroy(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl<D> Dispatch<ZwpPrimarySelectionDeviceManagerV1, GlobalData, D> for PrimarySelectionManagerState |
| 85 | where |
| 86 | D: Dispatch<ZwpPrimarySelectionDeviceManagerV1, GlobalData>, |
| 87 | { |
| 88 | fn event( |
| 89 | _: &mut D, |
| 90 | _: &ZwpPrimarySelectionDeviceManagerV1, |
| 91 | _: <ZwpPrimarySelectionDeviceManagerV1 as wayland_client::Proxy>::Event, |
| 92 | _: &GlobalData, |
| 93 | _: &wayland_client::Connection, |
| 94 | _: &QueueHandle<D>, |
| 95 | ) { |
| 96 | unreachable!("zwp_primary_selection_device_manager_v1 has no events" ) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[macro_export ] |
| 101 | macro_rules! delegate_primary_selection { |
| 102 | ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 103 | $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: |
| 104 | [ |
| 105 | $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1: $crate::globals::GlobalData |
| 106 | ] => $crate::primary_selection::PrimarySelectionManagerState); |
| 107 | $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: |
| 108 | [ |
| 109 | $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1: $crate::primary_selection::device::PrimarySelectionDeviceData |
| 110 | ] => $crate::primary_selection::PrimarySelectionManagerState); |
| 111 | $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: |
| 112 | [ |
| 113 | $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1: $crate::primary_selection::offer::PrimarySelectionOfferData |
| 114 | ] => $crate::primary_selection::PrimarySelectionManagerState); |
| 115 | $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: |
| 116 | [ |
| 117 | $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1: $crate::globals::GlobalData |
| 118 | ] => $crate::primary_selection::PrimarySelectionManagerState); |
| 119 | }; |
| 120 | } |
| 121 | |