use std::{
os::unix::io::{AsFd, OwnedFd},
sync::Mutex,
};
use crate::reexports::client::{Connection, Dispatch, QueueHandle, Proxy};
use crate::reexports::protocols::wp::primary_selection::zv1::client::zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1;
use crate::data_device_manager::ReadPipe;
use super::PrimarySelectionManagerState;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrimarySelectionOffer {
pub(crate) offer: ZwpPrimarySelectionOfferV1,
}
impl PrimarySelectionOffer {
pub fn with_mime_types<T, F: Fn(&[String]) -> T>(&self, callback: F) -> T {
let mime_types =
self.offer.data::<PrimarySelectionOfferData>().unwrap().mimes.lock().unwrap();
callback(mime_types.as_ref())
}
pub fn receive(&self, mime_type: String) -> std::io::Result<ReadPipe> {
use rustix::pipe::{pipe_with, PipeFlags};
let (readfd, writefd) = pipe_with(PipeFlags::CLOEXEC)?;
self.receive_to_fd(mime_type, writefd);
Ok(ReadPipe::from(readfd))
}
pub fn receive_to_fd(&self, mime_type: String, writefd: OwnedFd) {
self.offer.receive(mime_type, writefd.as_fd());
}
}
impl<State> Dispatch<ZwpPrimarySelectionOfferV1, PrimarySelectionOfferData, State>
for PrimarySelectionManagerState
where
State: Dispatch<ZwpPrimarySelectionOfferV1, PrimarySelectionOfferData>,
{
fn event(
_: &mut State,
_: &ZwpPrimarySelectionOfferV1,
event: <ZwpPrimarySelectionOfferV1 as wayland_client::Proxy>::Event,
data: &PrimarySelectionOfferData,
_: &Connection,
_: &QueueHandle<State>,
) {
use wayland_protocols::wp::primary_selection::zv1::client::zwp_primary_selection_offer_v1::Event;
match event {
Event::Offer { mime_type } => {
data.mimes.lock().unwrap().push(mime_type);
}
_ => unreachable!(),
}
}
}
#[derive(Debug, Default)]
pub struct PrimarySelectionOfferData {
mimes: Mutex<Vec<String>>,
}