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