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
121
122
123
124
use std::os::unix::io::FromRawFd;
use std::sync::{Arc, Mutex};

use wayland_client::Main;

use wayland_protocols::{
    misc::gtk_primary_selection::client::gtk_primary_selection_offer::{
        self, GtkPrimarySelectionOffer,
    },
    unstable::primary_selection::v1::client::zwp_primary_selection_offer_v1::{
        self, ZwpPrimarySelectionOfferV1,
    },
};

use crate::data_device::ReadPipe;

/// A primary selection offer for receiving data through copy/paste.
pub struct PrimarySelectionOffer {
    pub(crate) offer: PrimarySelectionOfferImpl,
    inner: Arc<Mutex<PrimarySelectionOfferInner>>,
}

impl PrimarySelectionOffer {
    /// Access the list of mime types proposed by this offer.
    pub fn with_mime_types<F, T>(&self, f: F) -> T
    where
        F: FnOnce(&[String]) -> T,
    {
        let inner = self.inner.lock().unwrap();
        f(&inner.mime_types)
    }

    /// Request to receive the data of a given mime type.
    ///
    /// Note that you should **not** read the contents right away in a blocking way,
    /// as you may deadlock your application.
    pub fn receive(&self, mime_type: String) -> Result<ReadPipe, ()> {
        use nix::fcntl::OFlag;
        use nix::unistd::{close, pipe2};
        // create a pipe
        let (readfd, writefd) = pipe2(OFlag::O_CLOEXEC).map_err(|_| ())?;

        match &self.offer {
            PrimarySelectionOfferImpl::Zwp(offer) => {
                offer.receive(mime_type, writefd);
            }
            PrimarySelectionOfferImpl::Gtk(offer) => {
                offer.receive(mime_type, writefd);
            }
        }

        if let Err(err) = close(writefd) {
            log::warn!("Failed to close write pipe: {}", err);
        }

        Ok(unsafe { FromRawFd::from_raw_fd(readfd) })
    }

    /// Initialize `PrimarySelectionOffer` from the `Zwp` offer.
    pub(crate) fn from_zwp(offer: Main<ZwpPrimarySelectionOfferV1>) -> Self {
        let inner = Arc::new(Mutex::new(PrimarySelectionOfferInner::new()));
        let inner2 = inner.clone();

        offer.quick_assign(move |_, event, _| {
            use zwp_primary_selection_offer_v1::Event;
            let mut inner = inner2.lock().unwrap();
            match event {
                Event::Offer { mime_type } => {
                    inner.mime_types.push(mime_type);
                }
                _ => unreachable!(),
            }
        });

        Self { offer: PrimarySelectionOfferImpl::Zwp(offer.detach()), inner }
    }

    /// Initialize `PrimarySelectionOffer` from the `Gtk` offer.
    pub(crate) fn from_gtk(offer: Main<GtkPrimarySelectionOffer>) -> Self {
        let inner = Arc::new(Mutex::new(PrimarySelectionOfferInner::new()));
        let inner2 = inner.clone();

        offer.quick_assign(move |_, event, _| {
            use gtk_primary_selection_offer::Event;
            let mut inner = inner2.lock().unwrap();
            match event {
                Event::Offer { mime_type } => {
                    inner.mime_types.push(mime_type);
                }
                _ => unreachable!(),
            }
        });

        Self { offer: PrimarySelectionOfferImpl::Gtk(offer.detach()), inner }
    }
}

impl Drop for PrimarySelectionOffer {
    fn drop(&mut self) {
        match &self.offer {
            PrimarySelectionOfferImpl::Zwp(offer) => offer.destroy(),
            PrimarySelectionOfferImpl::Gtk(offer) => offer.destroy(),
        }
    }
}

/// Inner state for `PrimarySelectionOffer`.
#[derive(Default)]
struct PrimarySelectionOfferInner {
    mime_types: Vec<String>,
}

impl PrimarySelectionOfferInner {
    fn new() -> Self {
        Self::default()
    }
}

/// Possible supported primary selection offers.
#[derive(Eq, PartialEq)]
pub(crate) enum PrimarySelectionOfferImpl {
    Zwp(ZwpPrimarySelectionOfferV1),
    Gtk(GtkPrimarySelectionOffer),
}