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
#![deny(clippy::all, clippy::if_not_else, clippy::enum_glob_use)]
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;
pub struct Clipboard {
request_sender: Sender<worker::Command>,
request_receiver: Receiver<Result<String>>,
clipboard_thread: Option<std::thread::JoinHandle<()>>,
}
impl Clipboard {
pub unsafe fn new(display: *mut c_void) -> Self {
let display = Display::from_external_display(display as *mut _);
let (request_sender, clipboard_request_receiver) = mpsc::channel();
let (clipboard_reply_sender, request_receiver) = mpsc::channel();
let name = String::from("smithay-clipboard");
let clipboard_thread =
worker::spawn(name, display, clipboard_request_receiver, clipboard_reply_sender);
Self { request_receiver, request_sender, clipboard_thread }
}
pub fn load(&self) -> Result<String> {
let _ = self.request_sender.send(worker::Command::Load);
if let Ok(reply) = self.request_receiver.recv() {
reply
} else {
Err(std::io::Error::new(std::io::ErrorKind::Other, "clipboard is dead."))
}
}
pub fn store<T: Into<String>>(&self, text: T) {
let request = worker::Command::Store(text.into());
let _ = self.request_sender.send(request);
}
pub fn load_primary(&self) -> Result<String> {
let _ = self.request_sender.send(worker::Command::LoadPrimary);
if let Ok(reply) = self.request_receiver.recv() {
reply
} else {
Err(std::io::Error::new(std::io::ErrorKind::Other, "clipboard is dead."))
}
}
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) {
let _ = self.request_sender.send(worker::Command::Exit);
if let Some(clipboard_thread) = self.clipboard_thread.take() {
let _ = clipboard_thread.join();
}
}
}