2023-02-25 14:30:45 +00:00
|
|
|
pub mod device;
|
|
|
|
pub mod manager;
|
|
|
|
pub mod offer;
|
|
|
|
pub mod source;
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
use self::device::{DataControlDeviceDataExt, DataControlDeviceHandler};
|
|
|
|
use self::offer::{DataControlDeviceOffer, DataControlOfferHandler, SelectionOffer};
|
|
|
|
use self::source::DataControlSourceHandler;
|
|
|
|
use crate::clients::wayland::Environment;
|
2023-05-29 14:01:42 +01:00
|
|
|
use crate::unique_id::get_unique_usize;
|
2023-04-29 22:08:02 +01:00
|
|
|
use crate::{lock, send};
|
|
|
|
use device::DataControlDevice;
|
2023-02-25 14:30:45 +00:00
|
|
|
use glib::Bytes;
|
2023-04-30 22:50:43 +01:00
|
|
|
use nix::fcntl::{fcntl, F_GETPIPE_SZ, F_SETPIPE_SZ};
|
2023-09-05 22:43:29 +01:00
|
|
|
use nix::sys::epoll::{Epoll, EpollCreateFlags, EpollEvent, EpollFlags};
|
2023-04-29 22:08:02 +01:00
|
|
|
use smithay_client_toolkit::data_device_manager::WritePipe;
|
|
|
|
use smithay_client_toolkit::reexports::calloop::RegistrationToken;
|
2023-04-30 22:50:43 +01:00
|
|
|
use std::cmp::min;
|
2023-04-29 22:08:02 +01:00
|
|
|
use std::fmt::{Debug, Formatter};
|
2023-02-25 14:30:45 +00:00
|
|
|
use std::fs::File;
|
2023-04-30 22:50:43 +01:00
|
|
|
use std::io::{ErrorKind, Read, Write};
|
|
|
|
use std::os::fd::{AsRawFd, OwnedFd, RawFd};
|
2023-02-25 14:30:45 +00:00
|
|
|
use std::sync::Arc;
|
2023-04-30 22:50:43 +01:00
|
|
|
use std::{fs, io};
|
|
|
|
use tracing::{debug, error, trace};
|
2023-04-29 22:08:02 +01:00
|
|
|
use wayland_client::{Connection, QueueHandle};
|
|
|
|
use wayland_protocols_wlr::data_control::v1::client::zwlr_data_control_source_v1::ZwlrDataControlSourceV1;
|
2023-02-25 14:30:45 +00:00
|
|
|
|
|
|
|
const INTERNAL_MIME_TYPE: &str = "x-ironbar-internal";
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
pub struct SelectionOfferItem {
|
|
|
|
offer: SelectionOffer,
|
|
|
|
token: Option<RegistrationToken>,
|
|
|
|
}
|
|
|
|
|
2023-02-25 14:30:45 +00:00
|
|
|
#[derive(Debug, Clone, Eq)]
|
|
|
|
pub struct ClipboardItem {
|
|
|
|
pub id: usize,
|
|
|
|
pub value: ClipboardValue,
|
|
|
|
pub mime_type: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<Self> for ClipboardItem {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2023-02-25 14:30:45 +00:00
|
|
|
pub enum ClipboardValue {
|
|
|
|
Text(String),
|
|
|
|
Image(Bytes),
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
impl Debug for ClipboardValue {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
match self {
|
|
|
|
Self::Text(text) => text.clone(),
|
|
|
|
Self::Image(bytes) => {
|
|
|
|
format!("[{} Bytes]", bytes.len())
|
|
|
|
}
|
|
|
|
Self::Other => "[Unknown]".to_string(),
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
2023-04-29 22:08:02 +01:00
|
|
|
)
|
|
|
|
}
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct MimeType {
|
|
|
|
value: String,
|
|
|
|
category: MimeTypeCategory,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum MimeTypeCategory {
|
|
|
|
Text,
|
|
|
|
Image,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MimeType {
|
2023-04-29 22:08:02 +01:00
|
|
|
fn parse(mime_type: &str) -> Option<Self> {
|
|
|
|
match mime_type.to_lowercase().as_str() {
|
|
|
|
"text"
|
|
|
|
| "string"
|
|
|
|
| "utf8_string"
|
|
|
|
| "text/plain"
|
|
|
|
| "text/plain;charset=utf-8"
|
|
|
|
| "text/plain;charset=iso-8859-1"
|
|
|
|
| "text/plain;charset=us-ascii"
|
|
|
|
| "text/plain;charset=unicode" => Some(Self {
|
|
|
|
value: mime_type.to_string(),
|
|
|
|
category: MimeTypeCategory::Text,
|
|
|
|
}),
|
|
|
|
"image/png" | "image/jpg" | "image/jpeg" | "image/tiff" | "image/bmp"
|
|
|
|
| "image/x-bmp" | "image/icon" => Some(Self {
|
|
|
|
value: mime_type.to_string(),
|
|
|
|
category: MimeTypeCategory::Image,
|
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_multiple(mime_types: &[String]) -> Option<Self> {
|
|
|
|
mime_types.iter().find_map(|mime| Self::parse(mime))
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
impl Environment {
|
|
|
|
pub fn copy_to_clipboard(&mut self, item: Arc<ClipboardItem>, qh: &QueueHandle<Self>) {
|
2023-04-30 22:50:43 +01:00
|
|
|
debug!("Copying item to clipboard: {item:?}");
|
2023-04-29 22:08:02 +01:00
|
|
|
|
|
|
|
// TODO: Proper device tracking
|
|
|
|
let device = self.data_control_devices.first();
|
|
|
|
if let Some(device) = device {
|
|
|
|
let source = self
|
|
|
|
.data_control_device_manager_state
|
2023-04-30 22:50:43 +01:00
|
|
|
.create_copy_paste_source(qh, [INTERNAL_MIME_TYPE, item.mime_type.as_str()]);
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
source.set_selection(&device.device);
|
|
|
|
self.copy_paste_sources.push(source);
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
lock!(self.clipboard).replace(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_file(mime_type: &MimeType, file: &mut File) -> io::Result<ClipboardItem> {
|
|
|
|
let value = match mime_type.category {
|
|
|
|
MimeTypeCategory::Text => {
|
|
|
|
let mut txt = String::new();
|
|
|
|
file.read_to_string(&mut txt)?;
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
ClipboardValue::Text(txt)
|
|
|
|
}
|
|
|
|
MimeTypeCategory::Image => {
|
|
|
|
let mut bytes = vec![];
|
|
|
|
file.read_to_end(&mut bytes)?;
|
|
|
|
let bytes = Bytes::from(&bytes);
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
ClipboardValue::Image(bytes)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ClipboardItem {
|
2023-05-29 14:01:42 +01:00
|
|
|
id: get_unique_usize(),
|
2023-04-29 22:08:02 +01:00
|
|
|
value,
|
|
|
|
mime_type: mime_type.value.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DataControlDeviceHandler for Environment {
|
|
|
|
fn selection(
|
|
|
|
&mut self,
|
|
|
|
_conn: &Connection,
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
data_device: DataControlDevice,
|
|
|
|
) {
|
|
|
|
debug!("Handler received selection event");
|
|
|
|
|
|
|
|
let mime_types = data_device.selection_mime_types();
|
2023-02-25 14:30:45 +00:00
|
|
|
|
|
|
|
if mime_types.contains(&INTERNAL_MIME_TYPE.to_string()) {
|
2023-04-29 22:08:02 +01:00
|
|
|
return;
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
if let Some(offer) = data_device.selection_offer() {
|
|
|
|
self.selection_offers
|
|
|
|
.push(SelectionOfferItem { offer, token: None });
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
let cur_offer = self
|
|
|
|
.selection_offers
|
|
|
|
.last_mut()
|
|
|
|
.expect("Failed to get current offer");
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
let Some(mime_type) = MimeType::parse_multiple(&mime_types) else {
|
|
|
|
lock!(self.clipboard).take();
|
2023-02-25 14:30:45 +00:00
|
|
|
// send an event so the clipboard module is aware it's changed
|
|
|
|
send!(
|
2023-04-29 22:08:02 +01:00
|
|
|
self.clipboard_tx,
|
2023-02-25 14:30:45 +00:00
|
|
|
Arc::new(ClipboardItem {
|
|
|
|
id: usize::MAX,
|
|
|
|
mime_type: String::new(),
|
|
|
|
value: ClipboardValue::Other
|
|
|
|
})
|
|
|
|
);
|
2023-04-29 22:08:02 +01:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(read_pipe) = cur_offer.offer.receive(mime_type.value.clone()) {
|
|
|
|
let offer_clone = cur_offer.offer.clone();
|
|
|
|
|
|
|
|
let tx = self.clipboard_tx.clone();
|
|
|
|
let clipboard = self.clipboard.clone();
|
|
|
|
|
|
|
|
let token = self
|
|
|
|
.loop_handle
|
2023-10-19 21:11:56 +01:00
|
|
|
.insert_source(read_pipe, move |(), file, state| {
|
2023-04-29 22:08:02 +01:00
|
|
|
let item = state
|
|
|
|
.selection_offers
|
|
|
|
.iter()
|
|
|
|
.position(|o| o.offer == offer_clone)
|
|
|
|
.map(|p| state.selection_offers.remove(p))
|
|
|
|
.expect("Failed to find selection offer item");
|
|
|
|
|
|
|
|
match Self::read_file(&mime_type, file) {
|
|
|
|
Ok(item) => {
|
|
|
|
let item = Arc::new(item);
|
|
|
|
lock!(clipboard).replace(item.clone());
|
|
|
|
send!(tx, item);
|
|
|
|
}
|
|
|
|
Err(err) => error!("{err:?}"),
|
|
|
|
}
|
|
|
|
|
|
|
|
state
|
|
|
|
.loop_handle
|
|
|
|
.remove(item.token.expect("Missing item token"));
|
|
|
|
});
|
|
|
|
|
|
|
|
match token {
|
|
|
|
Ok(token) => {
|
|
|
|
cur_offer.token.replace(token);
|
|
|
|
}
|
|
|
|
Err(err) => error!("{err:?}"),
|
|
|
|
}
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
impl DataControlOfferHandler for Environment {
|
|
|
|
fn offer(
|
|
|
|
&mut self,
|
|
|
|
_conn: &Connection,
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
_offer: &mut DataControlDeviceOffer,
|
|
|
|
_mime_type: String,
|
|
|
|
) {
|
2023-08-16 20:27:24 +01:00
|
|
|
trace!("Handler received offer");
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
impl DataControlSourceHandler for Environment {
|
|
|
|
fn accept_mime(
|
|
|
|
&mut self,
|
|
|
|
_conn: &Connection,
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
_source: &ZwlrDataControlSourceV1,
|
|
|
|
mime: Option<String>,
|
|
|
|
) {
|
|
|
|
debug!("Accepted mime type: {mime:?}");
|
|
|
|
}
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
fn send_request(
|
|
|
|
&mut self,
|
|
|
|
_conn: &Connection,
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
source: &ZwlrDataControlSourceV1,
|
|
|
|
mime: String,
|
|
|
|
write_pipe: WritePipe,
|
|
|
|
) {
|
2023-04-30 22:50:43 +01:00
|
|
|
debug!("Handler received source send request event ({mime})");
|
2023-04-29 22:08:02 +01:00
|
|
|
|
|
|
|
if let Some(item) = lock!(self.clipboard).clone() {
|
|
|
|
let fd = OwnedFd::from(write_pipe);
|
2023-04-30 22:50:43 +01:00
|
|
|
if self
|
2023-04-29 22:08:02 +01:00
|
|
|
.copy_paste_sources
|
|
|
|
.iter_mut()
|
2023-04-30 22:50:43 +01:00
|
|
|
.any(|s| s.inner() == source && MimeType::parse(&mime).is_some())
|
2023-04-29 22:08:02 +01:00
|
|
|
{
|
2023-04-30 22:50:43 +01:00
|
|
|
trace!("Source found, writing to file");
|
2023-04-29 22:08:02 +01:00
|
|
|
|
2023-04-30 22:50:43 +01:00
|
|
|
let mut bytes = match &item.value {
|
2023-04-29 22:08:02 +01:00
|
|
|
ClipboardValue::Text(text) => text.as_bytes(),
|
|
|
|
ClipboardValue::Image(bytes) => bytes.as_ref(),
|
|
|
|
ClipboardValue::Other => panic!(
|
|
|
|
"{:?}",
|
2023-04-30 22:50:43 +01:00
|
|
|
io::Error::new(ErrorKind::Other, "Attempted to copy unsupported mime type",)
|
2023-04-29 22:08:02 +01:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2023-04-30 22:50:43 +01:00
|
|
|
let pipe_size = set_pipe_size(fd.as_raw_fd(), bytes.len())
|
|
|
|
.expect("Failed to increase pipe size");
|
|
|
|
let mut file = File::from(fd.try_clone().expect("Failed to clone fd"));
|
|
|
|
|
|
|
|
trace!("Num bytes: {}", bytes.len());
|
|
|
|
|
|
|
|
let mut events = (0..16).map(|_| EpollEvent::empty()).collect::<Vec<_>>();
|
2023-09-05 22:43:29 +01:00
|
|
|
let epoll_event = EpollEvent::new(EpollFlags::EPOLLOUT, 0);
|
2023-04-30 22:50:43 +01:00
|
|
|
|
2023-09-05 22:43:29 +01:00
|
|
|
let epoll_fd =
|
|
|
|
Epoll::new(EpollCreateFlags::empty()).expect("to get valid file descriptor");
|
|
|
|
epoll_fd
|
|
|
|
.add(fd, epoll_event)
|
|
|
|
.expect("to send valid epoll operation");
|
2023-04-30 22:50:43 +01:00
|
|
|
|
|
|
|
while !bytes.is_empty() {
|
|
|
|
let chunk = &bytes[..min(pipe_size as usize, bytes.len())];
|
|
|
|
|
|
|
|
trace!("Writing {} bytes ({} remain)", chunk.len(), bytes.len());
|
|
|
|
|
2023-09-05 22:43:29 +01:00
|
|
|
epoll_fd
|
|
|
|
.wait(&mut events, 100)
|
|
|
|
.expect("Failed to wait to epoll");
|
2023-04-30 22:50:43 +01:00
|
|
|
|
|
|
|
match file.write(chunk) {
|
|
|
|
Ok(_) => bytes = &bytes[chunk.len()..],
|
|
|
|
Err(err) => {
|
|
|
|
error!("{err:?}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
2023-04-30 22:50:43 +01:00
|
|
|
} else {
|
|
|
|
error!("Failed to find source");
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
fn cancelled(
|
|
|
|
&mut self,
|
|
|
|
_conn: &Connection,
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
source: &ZwlrDataControlSourceV1,
|
|
|
|
) {
|
|
|
|
debug!("Handler received source cancelled event");
|
2023-02-25 14:30:45 +00:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
self.copy_paste_sources
|
|
|
|
.iter()
|
|
|
|
.position(|s| s.inner() == source)
|
|
|
|
.map(|pos| self.copy_paste_sources.remove(pos));
|
|
|
|
source.destroy();
|
|
|
|
}
|
2023-02-25 14:30:45 +00:00
|
|
|
}
|
2023-04-30 22:50:43 +01:00
|
|
|
|
|
|
|
/// Attempts to increase the fd pipe size to the requested number of bytes.
|
|
|
|
/// The kernel will automatically round this up to the nearest page size.
|
|
|
|
/// If the requested size is larger than the kernel max (normally 1MB),
|
|
|
|
/// it will be clamped at this.
|
|
|
|
///
|
|
|
|
/// Returns the new size if succeeded
|
|
|
|
fn set_pipe_size(fd: RawFd, size: usize) -> io::Result<i32> {
|
|
|
|
// clamp size at kernel max
|
|
|
|
let max_pipe_size = fs::read_to_string("/proc/sys/fs/pipe-max-size")
|
|
|
|
.expect("Failed to find pipe-max-size virtual kernel file")
|
|
|
|
.trim()
|
|
|
|
.parse::<usize>()
|
|
|
|
.expect("Failed to parse pipe-max-size contents");
|
|
|
|
|
|
|
|
let size = min(size, max_pipe_size);
|
|
|
|
|
|
|
|
let curr_size = fcntl(fd, F_GETPIPE_SZ)? as usize;
|
|
|
|
|
|
|
|
trace!("Current pipe size: {curr_size}");
|
|
|
|
|
|
|
|
let new_size = if size > curr_size {
|
|
|
|
trace!("Requesting pipe size increase to (at least): {size}");
|
2023-07-16 20:09:22 +01:00
|
|
|
|
2023-04-30 22:50:43 +01:00
|
|
|
let res = fcntl(fd, F_SETPIPE_SZ(size as i32))?;
|
|
|
|
trace!("New pipe size: {res}");
|
2023-07-16 20:09:22 +01:00
|
|
|
|
2023-04-30 22:50:43 +01:00
|
|
|
if res < size as i32 {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
2023-07-16 20:09:22 +01:00
|
|
|
|
2023-04-30 22:50:43 +01:00
|
|
|
res
|
|
|
|
} else {
|
|
|
|
size as i32
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(new_size)
|
|
|
|
}
|