1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::globals::GlobalData;
use crate::reexports::client::{
    globals::{BindError, GlobalList},
    protocol::wl_seat::WlSeat,
    Dispatch, QueueHandle,
};
use crate::reexports::protocols::wp::primary_selection::zv1::client::{
    zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1,
    zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1,
    zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1,
};

pub mod device;
pub mod offer;
pub mod selection;

use self::device::{PrimarySelectionDevice, PrimarySelectionDeviceData};
use selection::PrimarySelectionSource;

#[derive(Debug)]
pub struct PrimarySelectionManagerState {
    manager: ZwpPrimarySelectionDeviceManagerV1,
}

impl PrimarySelectionManagerState {
    pub fn bind<State>(globals: &GlobalList, qh: &QueueHandle<State>) -> Result<Self, BindError>
    where
        State: Dispatch<ZwpPrimarySelectionDeviceManagerV1, GlobalData, State> + 'static,
    {
        let manager = globals.bind(qh, 1..=1, GlobalData)?;
        Ok(Self { manager })
    }

    /// The underlying wayland object.
    pub fn primary_selection_manager(&self) -> &ZwpPrimarySelectionDeviceManagerV1 {
        &self.manager
    }

    /// Create a primary selection source.
    pub fn create_selection_source<State, I, T>(
        &self,
        qh: &QueueHandle<State>,
        mime_types: I,
    ) -> PrimarySelectionSource
    where
        State: Dispatch<ZwpPrimarySelectionSourceV1, GlobalData, State> + 'static,
        I: IntoIterator<Item = T>,
        T: ToString,
    {
        let source = self.manager.create_source(qh, GlobalData);

        for mime_type in mime_types {
            source.offer(mime_type.to_string());
        }

        PrimarySelectionSource::new(source)
    }

    /// Get the primary selection data device for the given seat.
    pub fn get_selection_device<State>(
        &self,
        qh: &QueueHandle<State>,
        seat: &WlSeat,
    ) -> PrimarySelectionDevice
    where
        State: Dispatch<ZwpPrimarySelectionDeviceV1, PrimarySelectionDeviceData, State> + 'static,
    {
        PrimarySelectionDevice {
            device: self.manager.get_device(
                seat,
                qh,
                PrimarySelectionDeviceData::new(seat.clone()),
            ),
        }
    }
}

impl Drop for PrimarySelectionManagerState {
    fn drop(&mut self) {
        self.manager.destroy();
    }
}

impl<D> Dispatch<ZwpPrimarySelectionDeviceManagerV1, GlobalData, D> for PrimarySelectionManagerState
where
    D: Dispatch<ZwpPrimarySelectionDeviceManagerV1, GlobalData>,
{
    fn event(
        _: &mut D,
        _: &ZwpPrimarySelectionDeviceManagerV1,
        _: <ZwpPrimarySelectionDeviceManagerV1 as wayland_client::Proxy>::Event,
        _: &GlobalData,
        _: &wayland_client::Connection,
        _: &QueueHandle<D>,
    ) {
        unreachable!("zwp_primary_selection_device_manager_v1 has no events")
    }
}

#[macro_export]
macro_rules! delegate_primary_selection {
    ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
        $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
            [
                $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1: $crate::globals::GlobalData
            ] => $crate::primary_selection::PrimarySelectionManagerState);
        $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
            [
                $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1: $crate::primary_selection::device::PrimarySelectionDeviceData
            ] => $crate::primary_selection::PrimarySelectionManagerState);
        $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
            [
                $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1: $crate::primary_selection::offer::PrimarySelectionOfferData
            ] => $crate::primary_selection::PrimarySelectionManagerState);
        $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
            [
                $crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1: $crate::globals::GlobalData
            ] => $crate::primary_selection::PrimarySelectionManagerState);
    };
}