Skip to main content

smithay_client_toolkit/primary_selection/
selection.rs

1use crate::dispatch2::Dispatch2;
2use crate::reexports::client::{Connection, QueueHandle};
3use crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1;
4use crate::{data_device_manager::WritePipe, globals::GlobalData};
5
6use super::device::PrimarySelectionDevice;
7
8/// Handler trait for `PrimarySelectionSource` events.
9///
10/// The functions defined in this trait are called as DataSource events are received from the compositor.
11pub trait PrimarySelectionSourceHandler: Sized {
12    /// The client has requested the data for this source to be sent.
13    /// Send the data, then close the fd.
14    fn send_request(
15        &mut self,
16        conn: &Connection,
17        qh: &QueueHandle<Self>,
18        source: &ZwpPrimarySelectionSourceV1,
19        mime: String,
20        write_pipe: WritePipe,
21    );
22
23    /// The data source is no longer valid
24    /// Cleanup & destroy this resource
25    fn cancelled(
26        &mut self,
27        conn: &Connection,
28        qh: &QueueHandle<Self>,
29        source: &ZwpPrimarySelectionSourceV1,
30    );
31}
32
33/// Wrapper around the [`ZwpPrimarySelectionSourceV1`].
34#[derive(Debug, PartialEq, Eq)]
35pub struct PrimarySelectionSource {
36    source: ZwpPrimarySelectionSourceV1,
37}
38
39impl PrimarySelectionSource {
40    pub(crate) fn new(source: ZwpPrimarySelectionSourceV1) -> Self {
41        Self { source }
42    }
43
44    /// Set the selection on the given [`PrimarySelectionDevice`].
45    pub fn set_selection(&self, device: &PrimarySelectionDevice, serial: u32) {
46        device.device.set_selection(Some(&self.source), serial);
47    }
48
49    /// The underlying wayland object.
50    pub fn inner(&self) -> &ZwpPrimarySelectionSourceV1 {
51        &self.source
52    }
53}
54
55impl Drop for PrimarySelectionSource {
56    fn drop(&mut self) {
57        self.source.destroy();
58    }
59}
60
61impl<State> Dispatch2<ZwpPrimarySelectionSourceV1, State> for GlobalData
62where
63    State: PrimarySelectionSourceHandler,
64{
65    fn event(
66        &self,
67        state: &mut State,
68        proxy: &ZwpPrimarySelectionSourceV1,
69        event: <ZwpPrimarySelectionSourceV1 as wayland_client::Proxy>::Event,
70        conn: &wayland_client::Connection,
71        qhandle: &QueueHandle<State>,
72    ) {
73        use wayland_protocols::wp::primary_selection::zv1::client::zwp_primary_selection_source_v1::Event as PrimarySelectionSourceEvent;
74        match event {
75            PrimarySelectionSourceEvent::Send { mime_type, fd } => {
76                state.send_request(conn, qhandle, proxy, mime_type, fd.into())
77            }
78            PrimarySelectionSourceEvent::Cancelled => state.cancelled(conn, qhandle, proxy),
79            _ => unreachable!(),
80        }
81    }
82}