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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use wayland_client::protocol::{wl_data_device_manager, wl_data_source};
use wayland_client::{Proxy, QueueToken};
use wayland_client::protocol::wl_data_device_manager::RequestsTrait as MgrRequests;
use wayland_client::protocol::wl_data_source::RequestsTrait as SourceRequests;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::{fs, io};
pub struct DataSource {
pub(crate) source: Proxy<wl_data_source::WlDataSource>,
}
pub enum DataSourceEvent {
Send {
mime_type: String,
pipe: WritePipe,
},
Target {
mime_type: Option<String>,
},
Action {
action: wl_data_device_manager::DndAction,
},
Cancelled,
Dropped,
Finished,
}
fn data_source_impl<Impl>(
evt: wl_data_source::Event,
source: &Proxy<wl_data_source::WlDataSource>,
implem: &mut Impl,
) where
Impl: FnMut(DataSourceEvent),
{
use self::wl_data_source::Event;
let event = match evt {
Event::Target { mime_type } => DataSourceEvent::Target { mime_type },
Event::Send { mime_type, fd } => DataSourceEvent::Send {
mime_type,
pipe: unsafe { FromRawFd::from_raw_fd(fd) },
},
Event::Action { dnd_action } => DataSourceEvent::Action {
action: wl_data_device_manager::DndAction::from_bits_truncate(dnd_action),
},
Event::Cancelled => {
source.destroy();
DataSourceEvent::Cancelled
}
Event::DndDropPerformed => DataSourceEvent::Dropped,
Event::DndFinished => {
source.destroy();
DataSourceEvent::Finished
}
};
implem(event);
}
impl DataSource {
pub fn new<Impl>(
mgr: &Proxy<wl_data_device_manager::WlDataDeviceManager>,
mime_types: &[&str],
mut implem: Impl,
) -> DataSource
where
Impl: FnMut(DataSourceEvent) + Send + 'static,
{
let source = mgr
.create_data_source(|source| {
source.implement(
move |evt, source: Proxy<_>| data_source_impl(evt, &source, &mut implem),
(),
)
})
.expect("Provided a dead data device manager to create a data source.");
for &mime in mime_types {
source.offer(mime.into());
}
DataSource { source }
}
pub unsafe fn new_nonsend<Impl>(
mgr: &Proxy<wl_data_device_manager::WlDataDeviceManager>,
mime_types: &[&str],
mut implem: Impl,
token: &QueueToken,
) -> DataSource
where
Impl: FnMut(DataSourceEvent) + 'static,
{
let source = mgr
.create_data_source(|source| {
source.implement_nonsend(
move |evt, source: Proxy<_>| data_source_impl(evt, &source, &mut implem),
(),
token,
)
})
.expect("Provided a dead data device manager to create a data source.");
for &mime in mime_types {
source.offer(mime.into());
}
DataSource { source }
}
}
pub struct WritePipe {
file: fs::File,
}
impl io::Write for WritePipe {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.file.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.file.flush()
}
}
impl FromRawFd for WritePipe {
unsafe fn from_raw_fd(fd: RawFd) -> WritePipe {
WritePipe {
file: FromRawFd::from_raw_fd(fd),
}
}
}
impl AsRawFd for WritePipe {
fn as_raw_fd(&self) -> RawFd {
self.file.as_raw_fd()
}
}
impl IntoRawFd for WritePipe {
fn into_raw_fd(self) -> RawFd {
self.file.into_raw_fd()
}
}