| 1 | //! Smithay Clipboard |
| 2 | //! |
| 3 | //! Provides access to the Wayland clipboard for gui applications. The user |
| 4 | //! should have surface around. |
| 5 | |
| 6 | #![deny (clippy::all, clippy::if_not_else, clippy::enum_glob_use)] |
| 7 | use std::ffi::c_void; |
| 8 | use std::io::Result; |
| 9 | use std::sync::mpsc::{self, Receiver}; |
| 10 | |
| 11 | use sctk::reexports::calloop::channel::{self, Sender}; |
| 12 | use sctk::reexports::client::backend::Backend; |
| 13 | use sctk::reexports::client::Connection; |
| 14 | |
| 15 | mod mime; |
| 16 | mod state; |
| 17 | mod worker; |
| 18 | |
| 19 | /// Access to a Wayland clipboard. |
| 20 | pub struct Clipboard { |
| 21 | request_sender: Sender<worker::Command>, |
| 22 | request_receiver: Receiver<Result<String>>, |
| 23 | clipboard_thread: Option<std::thread::JoinHandle<()>>, |
| 24 | } |
| 25 | |
| 26 | impl Clipboard { |
| 27 | /// Creates new clipboard which will be running on its own thread with its |
| 28 | /// own event queue to handle clipboard requests. |
| 29 | /// |
| 30 | /// # Safety |
| 31 | /// |
| 32 | /// `display` must be a valid `*mut wl_display` pointer, and it must remain |
| 33 | /// valid for as long as `Clipboard` object is alive. |
| 34 | pub unsafe fn new(display: *mut c_void) -> Self { |
| 35 | let backend = unsafe { Backend::from_foreign_display(display.cast()) }; |
| 36 | let connection = Connection::from_backend(backend); |
| 37 | |
| 38 | // Create channel to send data to clipboard thread. |
| 39 | let (request_sender, rx_chan) = channel::channel(); |
| 40 | // Create channel to get data from the clipboard thread. |
| 41 | let (clipboard_reply_sender, request_receiver) = mpsc::channel(); |
| 42 | |
| 43 | let name = String::from("smithay-clipboard" ); |
| 44 | let clipboard_thread = worker::spawn(name, connection, rx_chan, clipboard_reply_sender); |
| 45 | |
| 46 | Self { request_receiver, request_sender, clipboard_thread } |
| 47 | } |
| 48 | |
| 49 | /// Load clipboard data. |
| 50 | /// |
| 51 | /// Loads content from a clipboard on a last observed seat. |
| 52 | pub fn load(&self) -> Result<String> { |
| 53 | let _ = self.request_sender.send(worker::Command::Load); |
| 54 | |
| 55 | if let Ok(reply) = self.request_receiver.recv() { |
| 56 | reply |
| 57 | } else { |
| 58 | // The clipboard thread is dead, however we shouldn't crash downstream, so |
| 59 | // propogating an error. |
| 60 | Err(std::io::Error::new(std::io::ErrorKind::Other, "clipboard is dead." )) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// Store to a clipboard. |
| 65 | /// |
| 66 | /// Stores to a clipboard on a last observed seat. |
| 67 | pub fn store<T: Into<String>>(&self, text: T) { |
| 68 | let request = worker::Command::Store(text.into()); |
| 69 | let _ = self.request_sender.send(request); |
| 70 | } |
| 71 | |
| 72 | /// Load primary clipboard data. |
| 73 | /// |
| 74 | /// Loads content from a primary clipboard on a last observed seat. |
| 75 | pub fn load_primary(&self) -> Result<String> { |
| 76 | let _ = self.request_sender.send(worker::Command::LoadPrimary); |
| 77 | |
| 78 | if let Ok(reply) = self.request_receiver.recv() { |
| 79 | reply |
| 80 | } else { |
| 81 | // The clipboard thread is dead, however we shouldn't crash downstream, so |
| 82 | // propogating an error. |
| 83 | Err(std::io::Error::new(std::io::ErrorKind::Other, "clipboard is dead." )) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Store to a primary clipboard. |
| 88 | /// |
| 89 | /// Stores to a primary clipboard on a last observed seat. |
| 90 | pub fn store_primary<T: Into<String>>(&self, text: T) { |
| 91 | let request = worker::Command::StorePrimary(text.into()); |
| 92 | let _ = self.request_sender.send(request); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl Drop for Clipboard { |
| 97 | fn drop(&mut self) { |
| 98 | // Shutdown smithay-clipboard. |
| 99 | let _ = self.request_sender.send(worker::Command::Exit); |
| 100 | if let Some(clipboard_thread: JoinHandle<()>) = self.clipboard_thread.take() { |
| 101 | let _ = clipboard_thread.join(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |