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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use std::{cell::RefCell, rc::Rc};
use wayland_protocols::misc::gtk_primary_selection::client::gtk_primary_selection_device_manager::GtkPrimarySelectionDeviceManager;
use wayland_protocols::unstable::primary_selection::v1::client::zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1;
use wayland_client::{
protocol::{wl_registry::WlRegistry, wl_seat::WlSeat},
Attached, DispatchData,
};
use crate::environment::GlobalHandler;
use crate::lazy_global::LazyGlobal;
use crate::seat::{SeatHandling, SeatListener};
mod device;
mod offer;
mod source;
pub use self::device::PrimarySelectionDevice;
pub use self::offer::PrimarySelectionOffer;
pub use self::source::{PrimarySelectionSource, PrimarySelectionSourceEvent};
pub struct PrimarySelectionHandler {
inner: Rc<RefCell<PrimarySelectionDeviceManagerInner>>,
_listener: SeatListener,
}
pub enum PrimarySelectionDeviceManager {
Zwp(Attached<ZwpPrimarySelectionDeviceManagerV1>),
Gtk(Attached<GtkPrimarySelectionDeviceManager>),
}
impl PrimarySelectionHandler {
pub fn init<S: SeatHandling>(seat_handler: &mut S) -> Self {
let inner = Rc::new(RefCell::new(PrimarySelectionDeviceManagerInner {
registry: None,
zwp_mgr: LazyGlobal::Unknown,
gtk_mgr: LazyGlobal::Unknown,
state: PrimarySelectionDeviceManagerInitState::Pending { seats: Vec::new() },
}));
let seat_inner = inner.clone();
let listener = seat_handler.listen(move |seat, seat_data, _| {
if seat_data.defunct {
seat_inner.borrow_mut().remove_seat(&seat);
} else {
seat_inner.borrow_mut().new_seat(&seat);
}
});
Self { inner, _listener: listener }
}
}
pub trait PrimarySelectionHandling {
fn with_primary_selection<F: FnOnce(&PrimarySelectionDevice)>(
&self,
seat: &WlSeat,
f: F,
) -> Result<(), ()>;
fn get_primary_selection_manager(&self) -> Option<PrimarySelectionDeviceManager>;
}
impl<E: PrimarySelectionHandling> crate::environment::Environment<E> {
pub fn get_primary_selection_manager(&self) -> Option<PrimarySelectionDeviceManager> {
self.with_inner(|manager| manager.get_primary_selection_manager())
}
pub fn with_primary_selection<F: FnOnce(&PrimarySelectionDevice)>(
&self,
seat: &WlSeat,
f: F,
) -> Result<(), ()> {
self.with_inner(|inner| inner.with_primary_selection(seat, f))
}
pub fn new_primary_selection_source<F>(
&self,
mime_types: Vec<String>,
callback: F,
) -> PrimarySelectionSource
where
F: FnMut(PrimarySelectionSourceEvent, DispatchData) + 'static,
{
let manager = match self.get_primary_selection_manager() {
Some(manager) => manager,
None => panic!("[SCTK] primary selection was required"),
};
PrimarySelectionSource::new(&manager, mime_types, callback)
}
}
impl PrimarySelectionHandling for PrimarySelectionHandler {
fn get_primary_selection_manager(&self) -> Option<PrimarySelectionDeviceManager> {
if let Some(zwp) = GlobalHandler::<ZwpPrimarySelectionDeviceManagerV1>::get(self) {
Some(PrimarySelectionDeviceManager::Zwp(zwp))
} else if let Some(gtk) = GlobalHandler::<GtkPrimarySelectionDeviceManager>::get(self) {
Some(PrimarySelectionDeviceManager::Gtk(gtk))
} else {
None
}
}
fn with_primary_selection<F: FnOnce(&PrimarySelectionDevice)>(
&self,
seat: &WlSeat,
f: F,
) -> Result<(), ()> {
self.inner.borrow().with_primary_selection(seat, f)
}
}
enum PrimarySelectionDeviceManagerInitState {
Ready { manager: PrimarySelectionDeviceManager, devices: Vec<(WlSeat, PrimarySelectionDevice)> },
Pending { seats: Vec<WlSeat> },
}
struct PrimarySelectionDeviceManagerInner {
registry: Option<Attached<WlRegistry>>,
zwp_mgr: LazyGlobal<ZwpPrimarySelectionDeviceManagerV1>,
gtk_mgr: LazyGlobal<GtkPrimarySelectionDeviceManager>,
pub state: PrimarySelectionDeviceManagerInitState,
}
impl PrimarySelectionDeviceManagerInner {
fn init_selection_manager(&mut self, manager: PrimarySelectionDeviceManager) {
let seats =
if let PrimarySelectionDeviceManagerInitState::Pending { seats } = &mut self.state {
std::mem::replace(seats, Vec::new())
} else {
log::warn!("Ignoring second primary selection manager.");
return;
};
let mut devices = Vec::new();
for seat in seats {
let device = PrimarySelectionDevice::init_for_seat(&manager, &seat);
devices.push((seat.clone(), device));
}
self.state = PrimarySelectionDeviceManagerInitState::Ready { devices, manager }
}
fn new_seat(&mut self, seat: &WlSeat) {
match &mut self.state {
PrimarySelectionDeviceManagerInitState::Ready { devices, manager } => {
if devices.iter().any(|(s, _)| s == seat) {
return;
}
let device = PrimarySelectionDevice::init_for_seat(&manager, seat);
devices.push((seat.clone(), device));
}
PrimarySelectionDeviceManagerInitState::Pending { seats } => {
seats.push(seat.clone());
}
}
}
fn remove_seat(&mut self, seat: &WlSeat) {
match &mut self.state {
PrimarySelectionDeviceManagerInitState::Ready { devices, .. } => {
devices.retain(|(s, _)| s != seat)
}
PrimarySelectionDeviceManagerInitState::Pending { seats } => {
seats.retain(|s| s != seat)
}
}
}
fn with_primary_selection<F: FnOnce(&PrimarySelectionDevice)>(
&self,
seat: &WlSeat,
f: F,
) -> Result<(), ()> {
match &self.state {
PrimarySelectionDeviceManagerInitState::Pending { .. } => Err(()),
PrimarySelectionDeviceManagerInitState::Ready { devices, .. } => {
for (s, device) in devices {
if s == seat {
f(device);
return Ok(());
}
}
Err(())
}
}
}
}
impl GlobalHandler<ZwpPrimarySelectionDeviceManagerV1> for PrimarySelectionHandler {
fn created(&mut self, registry: Attached<WlRegistry>, id: u32, version: u32, _: DispatchData) {
let mut inner = self.inner.borrow_mut();
if inner.registry.is_none() {
inner.registry = Some(registry);
}
if let LazyGlobal::Unknown = inner.zwp_mgr {
inner.zwp_mgr = LazyGlobal::Seen { id, version };
} else {
log::warn!(
"Compositor advertised zwp_primary_selection_device_manager_v1 multiple\
times, ignoring."
)
}
}
fn get(&self) -> Option<Attached<ZwpPrimarySelectionDeviceManagerV1>> {
let mut inner = self.inner.borrow_mut();
match inner.zwp_mgr {
LazyGlobal::Bound(ref mgr) => Some(mgr.clone()),
LazyGlobal::Unknown => None,
LazyGlobal::Seen { id, version } => {
let registry = inner.registry.as_ref().unwrap();
let version = std::cmp::min(1, version);
let mgr = registry.bind::<ZwpPrimarySelectionDeviceManagerV1>(version, id);
let manager = PrimarySelectionDeviceManager::Zwp((*mgr).clone());
inner.init_selection_manager(manager);
inner.zwp_mgr = LazyGlobal::Bound((*mgr).clone());
Some((*mgr).clone())
}
}
}
}
impl GlobalHandler<GtkPrimarySelectionDeviceManager> for PrimarySelectionHandler {
fn created(&mut self, registry: Attached<WlRegistry>, id: u32, version: u32, _: DispatchData) {
let mut inner = self.inner.borrow_mut();
if inner.registry.is_none() {
inner.registry = Some(registry);
}
if let LazyGlobal::Unknown = inner.gtk_mgr {
inner.gtk_mgr = LazyGlobal::Seen { id, version };
} else {
log::warn!(
"Compositor advertised gtk_primary_selection_device_manager multiple times,\
ignoring."
)
}
}
fn get(&self) -> Option<Attached<GtkPrimarySelectionDeviceManager>> {
let mut inner = self.inner.borrow_mut();
match inner.gtk_mgr {
LazyGlobal::Bound(ref mgr) => Some(mgr.clone()),
LazyGlobal::Unknown => None,
LazyGlobal::Seen { id, version } => {
let registry = inner.registry.as_ref().unwrap();
let version = std::cmp::min(1, version);
let mgr = registry.bind::<GtkPrimarySelectionDeviceManager>(version, id);
let manager = PrimarySelectionDeviceManager::Gtk((*mgr).clone());
inner.init_selection_manager(manager);
inner.gtk_mgr = LazyGlobal::Bound((*mgr).clone());
Some((*mgr).clone())
}
}
}
}