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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use wayland_client::protocol::wl_data_device_manager::DndAction;
use wayland_client::protocol::wl_data_offer;
use wayland_client::NewProxy;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::sync::{Arc, Mutex};
use std::{fs, io};
struct Inner {
mime_types: Vec<String>,
actions: DndAction,
current_action: DndAction,
serial: u32,
}
pub struct DataOffer {
pub(crate) offer: wl_data_offer::WlDataOffer,
inner: Arc<Mutex<Inner>>,
}
impl DataOffer {
pub(crate) fn new(offer: NewProxy<wl_data_offer::WlDataOffer>) -> DataOffer {
let inner = Arc::new(Mutex::new(Inner {
mime_types: Vec::new(),
actions: DndAction::None,
current_action: DndAction::None,
serial: 0,
}));
let inner2 = inner.clone();
let offer = offer.implement_closure(
move |event, _| {
use self::wl_data_offer::Event;
let mut inner = inner2.lock().unwrap();
match event {
Event::Offer { mime_type } => {
inner.mime_types.push(mime_type);
}
Event::SourceActions { source_actions } => {
inner.actions = DndAction::from_bits_truncate(source_actions);
}
Event::Action { dnd_action } => {
inner.current_action = DndAction::from_bits_truncate(dnd_action);
}
_ => unreachable!(),
}
},
(),
);
DataOffer { offer, inner }
}
pub fn with_mime_types<F, T>(&self, f: F) -> T
where
F: FnOnce(&[String]) -> T,
{
let inner = self.inner.lock().unwrap();
f(&inner.mime_types)
}
pub fn get_available_actions(&self) -> DndAction {
self.inner.lock().unwrap().actions
}
pub fn get_current_action(&self) -> DndAction {
self.inner.lock().unwrap().current_action
}
pub fn accept(&self, mime_type: Option<String>) {
let serial = self.inner.lock().unwrap().serial;
self.offer.accept(serial, mime_type);
}
pub fn receive(&self, mime_type: String) -> Result<ReadPipe, ()> {
use nix::fcntl::OFlag;
use nix::unistd::{close, pipe2};
let (readfd, writefd) = pipe2(OFlag::O_CLOEXEC).map_err(|_| ())?;
self.offer.receive(mime_type, writefd);
if let Err(err) = close(writefd) {
eprintln!("[SCTK] Data offer: failed to close write pipe: {}", err);
}
Ok(unsafe { FromRawFd::from_raw_fd(readfd) })
}
pub fn set_actions(&self, supported: DndAction, preferred: DndAction) {
self.offer
.set_actions(supported.to_raw(), preferred.to_raw());
}
pub fn finish(&self) {
self.offer.finish();
self.offer.destroy();
}
}
impl Drop for DataOffer {
fn drop(&mut self) {
self.offer.destroy();
}
}
pub struct ReadPipe {
file: fs::File,
}
impl io::Read for ReadPipe {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.file.read(buf)
}
}
impl FromRawFd for ReadPipe {
unsafe fn from_raw_fd(fd: RawFd) -> ReadPipe {
ReadPipe {
file: FromRawFd::from_raw_fd(fd),
}
}
}
impl AsRawFd for ReadPipe {
fn as_raw_fd(&self) -> RawFd {
self.file.as_raw_fd()
}
}
impl IntoRawFd for ReadPipe {
fn into_raw_fd(self) -> RawFd {
self.file.into_raw_fd()
}
}