Files
ab_glyph_rasterizer
adler
adler32
andrew
bitflags
bytemuck
byteorder
calloop
cfg_if
color_quant
crc32fast
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_utils
deflate
dlib
downcast_rs
draw_state
either
event_loop
float
fnv
gfx
gfx_core
gfx_device_gl
gfx_gl
gfx_graphics
gfx_texture
gif
gl
glutin
glutin_egl_sys
glutin_glx_sys
glutin_window
graphics
graphics_api_version
image
input
instant
interpolation
iovec
jpeg_decoder
lazy_static
lazycell
libc
libloading
lock_api
log
maybe_uninit
memchr
memmap2
memoffset
miniz_oxide
mio
mio_extras
net2
nix
nom
num_cpus
num_integer
num_iter
num_rational
num_traits
once_cell
osmesa_sys
owned_ttf_parser
parking_lot
parking_lot_core
percent_encoding
piston
piston_window
png
proc_macro2
quote
raw_window_handle
rayon
rayon_core
read_color
rusttype
same_file
scoped_threadpool
scoped_tls
scopeguard
serde
serde_derive
shader_version
shaders_graphics2d
colored
textured
textured_color
shared_library
slab
smallvec
smithay_client_toolkit
spin_sleep
syn
texture
tiff
ttf_parser
unicode_xid
vecmath
viewport
walkdir
wayland_client
wayland_commons
wayland_cursor
wayland_egl
wayland_protocols
wayland_sys
weezl
window
winit
x11_dl
xcursor
xdg
xml
  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),
}