2019-02-14 18:01:57 +08:00
|
|
|
//! Smithay Clipboard
|
2019-02-10 01:28:53 +08:00
|
|
|
//!
|
2020-05-10 16:40:03 +03:00
|
|
|
//! Provides access to the Wayland clipboard for gui applications. The user should have surface
|
|
|
|
|
//! around.
|
|
|
|
|
|
2021-08-14 00:59:18 +03:00
|
|
|
#![deny(clippy::all, clippy::if_not_else, clippy::enum_glob_use)]
|
2020-05-10 16:40:03 +03:00
|
|
|
use std::ffi::c_void;
|
|
|
|
|
use std::io::Result;
|
|
|
|
|
use std::sync::mpsc::{self, Receiver, Sender};
|
|
|
|
|
|
|
|
|
|
use sctk::reexports::client::Display;
|
|
|
|
|
|
|
|
|
|
mod env;
|
|
|
|
|
mod mime;
|
|
|
|
|
mod worker;
|
|
|
|
|
|
|
|
|
|
/// Access to a Wayland clipboard.
|
|
|
|
|
pub struct Clipboard {
|
|
|
|
|
request_sender: Sender<worker::Command>,
|
|
|
|
|
request_receiver: Receiver<Result<String>>,
|
|
|
|
|
clipboard_thread: Option<std::thread::JoinHandle<()>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clipboard {
|
|
|
|
|
/// Creates new clipboard which will be running on its own thread with its own event queue to
|
|
|
|
|
/// handle clipboard requests.
|
2020-09-30 19:13:15 +03:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `display` must be a valid `*mut wl_display` pointer, and it must remain
|
|
|
|
|
/// valid for as long as `Clipboard` object is alive.
|
|
|
|
|
pub unsafe fn new(display: *mut c_void) -> Self {
|
|
|
|
|
let display = Display::from_external_display(display as *mut _);
|
2020-05-10 16:40:03 +03:00
|
|
|
|
|
|
|
|
// Create channel to send data to clipboard thread.
|
|
|
|
|
let (request_sender, clipboard_request_receiver) = mpsc::channel();
|
|
|
|
|
// Create channel to get data from the clipboard thread.
|
|
|
|
|
let (clipboard_reply_sender, request_receiver) = mpsc::channel();
|
|
|
|
|
|
|
|
|
|
let name = String::from("smithay-clipboard");
|
2020-08-06 02:04:14 +03:00
|
|
|
let clipboard_thread =
|
|
|
|
|
worker::spawn(name, display, clipboard_request_receiver, clipboard_reply_sender);
|
|
|
|
|
|
|
|
|
|
Self { request_receiver, request_sender, clipboard_thread }
|
2020-05-10 16:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Load clipboard data.
|
|
|
|
|
///
|
|
|
|
|
/// Loads content from a clipboard on a last observed seat.
|
|
|
|
|
pub fn load(&self) -> Result<String> {
|
|
|
|
|
let _ = self.request_sender.send(worker::Command::Load);
|
2020-08-27 14:08:26 +03:00
|
|
|
|
|
|
|
|
if let Ok(reply) = self.request_receiver.recv() {
|
|
|
|
|
reply
|
|
|
|
|
} else {
|
|
|
|
|
// The clipboard thread is dead, however we shouldn't crash downstream, so
|
|
|
|
|
// propogating an error.
|
|
|
|
|
Err(std::io::Error::new(std::io::ErrorKind::Other, "clipboard is dead."))
|
|
|
|
|
}
|
2020-05-10 16:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
2020-08-27 14:08:26 +03:00
|
|
|
/// Store to a clipboard.
|
2020-05-10 16:40:03 +03:00
|
|
|
///
|
|
|
|
|
/// Stores to a clipboard on a last observed seat.
|
|
|
|
|
pub fn store<T: Into<String>>(&self, text: T) {
|
|
|
|
|
let request = worker::Command::Store(text.into());
|
|
|
|
|
let _ = self.request_sender.send(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Load primary clipboard data.
|
|
|
|
|
///
|
|
|
|
|
/// Loads content from a primary clipboard on a last observed seat.
|
|
|
|
|
pub fn load_primary(&self) -> Result<String> {
|
|
|
|
|
let _ = self.request_sender.send(worker::Command::LoadPrimary);
|
2020-08-27 14:08:26 +03:00
|
|
|
|
|
|
|
|
if let Ok(reply) = self.request_receiver.recv() {
|
|
|
|
|
reply
|
|
|
|
|
} else {
|
|
|
|
|
// The clipboard thread is dead, however we shouldn't crash downstream, so
|
|
|
|
|
// propogating an error.
|
|
|
|
|
Err(std::io::Error::new(std::io::ErrorKind::Other, "clipboard is dead."))
|
|
|
|
|
}
|
2020-05-10 16:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Store to a primary clipboard.
|
|
|
|
|
///
|
|
|
|
|
/// Stores to a primary clipboard on a last observed seat.
|
|
|
|
|
pub fn store_primary<T: Into<String>>(&self, text: T) {
|
|
|
|
|
let request = worker::Command::StorePrimary(text.into());
|
|
|
|
|
let _ = self.request_sender.send(request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Clipboard {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// Shutdown smithay-clipboard.
|
2020-08-27 14:08:26 +03:00
|
|
|
let _ = self.request_sender.send(worker::Command::Exit);
|
2020-05-10 16:40:03 +03:00
|
|
|
if let Some(clipboard_thread) = self.clipboard_thread.take() {
|
|
|
|
|
let _ = clipboard_thread.join();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|