simple_window/common/protocols/wayland/wl_data_source.rs
1//! offer to transfer data
2//!
3//! The wl_data_source object is the source side of a wl_data_offer.
4//! It is created by the source client in a data transfer and
5//! provides a way to describe the offered data and a way to respond
6//! to requests to transfer the data.
7
8use {super::super::all_types::*, ::wl_client::builder::prelude::*};
9
10static INTERFACE: wl_interface = wl_interface {
11 name: c"wl_data_source".as_ptr(),
12 version: 3,
13 method_count: 3,
14 methods: {
15 static MESSAGES: [wl_message; 3] = [
16 wl_message {
17 name: c"offer".as_ptr(),
18 signature: c"s".as_ptr(),
19 types: {
20 static TYPES: [Option<&'static wl_interface>; 1] = [None];
21 TYPES.as_ptr().cast()
22 },
23 },
24 wl_message {
25 name: c"destroy".as_ptr(),
26 signature: c"".as_ptr(),
27 types: {
28 static TYPES: [Option<&'static wl_interface>; 0] = [];
29 TYPES.as_ptr().cast()
30 },
31 },
32 wl_message {
33 name: c"set_actions".as_ptr(),
34 signature: c"u".as_ptr(),
35 types: {
36 static TYPES: [Option<&'static wl_interface>; 1] = [None];
37 TYPES.as_ptr().cast()
38 },
39 },
40 ];
41 MESSAGES.as_ptr()
42 },
43 event_count: 6,
44 events: {
45 static MESSAGES: [wl_message; 6] = [
46 wl_message {
47 name: c"target".as_ptr(),
48 signature: c"?s".as_ptr(),
49 types: {
50 static TYPES: [Option<&'static wl_interface>; 1] = [None];
51 TYPES.as_ptr().cast()
52 },
53 },
54 wl_message {
55 name: c"send".as_ptr(),
56 signature: c"sh".as_ptr(),
57 types: {
58 static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
59 TYPES.as_ptr().cast()
60 },
61 },
62 wl_message {
63 name: c"cancelled".as_ptr(),
64 signature: c"".as_ptr(),
65 types: {
66 static TYPES: [Option<&'static wl_interface>; 0] = [];
67 TYPES.as_ptr().cast()
68 },
69 },
70 wl_message {
71 name: c"dnd_drop_performed".as_ptr(),
72 signature: c"".as_ptr(),
73 types: {
74 static TYPES: [Option<&'static wl_interface>; 0] = [];
75 TYPES.as_ptr().cast()
76 },
77 },
78 wl_message {
79 name: c"dnd_finished".as_ptr(),
80 signature: c"".as_ptr(),
81 types: {
82 static TYPES: [Option<&'static wl_interface>; 0] = [];
83 TYPES.as_ptr().cast()
84 },
85 },
86 wl_message {
87 name: c"action".as_ptr(),
88 signature: c"u".as_ptr(),
89 types: {
90 static TYPES: [Option<&'static wl_interface>; 1] = [None];
91 TYPES.as_ptr().cast()
92 },
93 },
94 ];
95 MESSAGES.as_ptr()
96 },
97};
98
99/// An owned wl_data_source proxy.
100///
101/// See the documentation of [the module][self] for the interface description.
102#[derive(Clone, Eq, PartialEq)]
103#[repr(transparent)]
104pub struct WlDataSource {
105 /// This proxy has the interface INTERFACE.
106 proxy: UntypedOwnedProxy,
107}
108
109/// A borrowed wl_data_source proxy.
110///
111/// See the documentation of [the module][self] for the interface description.
112#[derive(Eq, PartialEq)]
113#[repr(transparent)]
114pub struct WlDataSourceRef {
115 /// This proxy has the interface INTERFACE.
116 proxy: UntypedBorrowedProxy,
117}
118
119// SAFETY: WlDataSource is a transparent wrapper around UntypedOwnedProxy
120unsafe impl UntypedOwnedProxyWrapper for WlDataSource {}
121
122// SAFETY: - INTERFACE is a valid wl_interface
123// - The only invariant is that self.proxy has a compatible interface
124unsafe impl OwnedProxy for WlDataSource {
125 const INTERFACE: &'static str = "wl_data_source";
126 const WL_INTERFACE: &'static wl_interface = &INTERFACE;
127 const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
128 private::EventHandler(private::NoOpEventHandler);
129 const MAX_VERSION: u32 = 3;
130
131 type Borrowed = WlDataSourceRef;
132 type Api = private::ProxyApi;
133 type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
134}
135
136// SAFETY: WlDataSourceRef is a transparent wrapper around UntypedBorrowedProxy
137unsafe impl UntypedBorrowedProxyWrapper for WlDataSourceRef {}
138
139// SAFETY: - The only invariant is that self.proxy has a compatible interface
140unsafe impl BorrowedProxy for WlDataSourceRef {
141 type Owned = WlDataSource;
142}
143
144impl Deref for WlDataSource {
145 type Target = WlDataSourceRef;
146
147 fn deref(&self) -> &Self::Target {
148 proxy::low_level::deref(self)
149 }
150}
151
152mod private {
153 pub struct ProxyApi;
154
155 #[allow(dead_code)]
156 pub struct EventHandler<H>(pub(super) H);
157
158 #[allow(dead_code)]
159 pub struct NoOpEventHandler;
160}
161
162impl Debug for WlDataSource {
163 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
164 write!(f, "wl_data_source#{}", self.proxy.id())
165 }
166}
167
168impl Debug for WlDataSourceRef {
169 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
170 write!(f, "wl_data_source#{}", self.proxy.id())
171 }
172}
173
174impl PartialEq<WlDataSourceRef> for WlDataSource {
175 fn eq(&self, other: &WlDataSourceRef) -> bool {
176 self.proxy == other.proxy
177 }
178}
179
180impl PartialEq<WlDataSource> for WlDataSourceRef {
181 fn eq(&self, other: &WlDataSource) -> bool {
182 self.proxy == other.proxy
183 }
184}
185
186#[allow(dead_code)]
187impl WlDataSource {
188 /// Since when the destroy request is available.
189 #[allow(dead_code)]
190 pub const REQ__DESTROY__SINCE: u32 = 1;
191
192 /// destroy the data source
193 ///
194 /// Destroy the data source.
195 #[inline]
196 pub fn destroy(&self) {
197 let mut args = [];
198 // SAFETY: - self.proxy has the interface INTERFACE
199 // - 1 < INTERFACE.method_count = 3
200 // - the request signature is ``
201 unsafe {
202 self.proxy.send_destructor(1, &mut args);
203 }
204 }
205}
206
207#[allow(dead_code)]
208impl WlDataSourceRef {
209 /// add an offered mime type
210 ///
211 /// This request adds a mime type to the set of mime types
212 /// advertised to targets. Can be called several times to offer
213 /// multiple types.
214 ///
215 /// # Arguments
216 ///
217 /// - `mime_type`: mime type offered by the data source
218 #[inline]
219 pub fn offer(&self, mime_type: &str) {
220 let (arg0,) = (mime_type,);
221 with_cstr_cache(|cache| {
222 let str0_offset = cache.len();
223 cache.extend_from_slice(arg0.as_bytes());
224 cache.push(0);
225 let str0 = cache[str0_offset..].as_ptr().cast();
226 let mut args = [wl_argument { s: str0 }];
227 // SAFETY: - self.proxy has the interface INTERFACE
228 // - 0 < INTERFACE.method_count = 3
229 // - the request signature is `s`
230 unsafe {
231 self.proxy.send_request(0, &mut args);
232 }
233 })
234 }
235
236 /// set the available drag-and-drop actions
237 ///
238 /// Sets the actions that the source side client supports for this
239 /// operation. This request may trigger wl_data_source.action and
240 /// wl_data_offer.action events if the compositor needs to change the
241 /// selected action.
242 ///
243 /// The dnd_actions argument must contain only values expressed in the
244 /// wl_data_device_manager.dnd_actions enum, otherwise it will result
245 /// in a protocol error.
246 ///
247 /// This request must be made once only, and can only be made on sources
248 /// used in drag-and-drop, so it must be performed before
249 /// wl_data_device.start_drag. Attempting to use the source other than
250 /// for drag-and-drop will raise a protocol error.
251 ///
252 /// # Arguments
253 ///
254 /// - `dnd_actions`: actions supported by the data source
255 #[inline]
256 pub fn set_actions(&self, dnd_actions: WlDataDeviceManagerDndAction) {
257 let (arg0,) = (dnd_actions,);
258 let mut args = [wl_argument { u: arg0.0 }];
259 // SAFETY: - self.proxy has the interface INTERFACE
260 // - 2 < INTERFACE.method_count = 3
261 // - the request signature is `u`
262 unsafe {
263 self.proxy.send_request(2, &mut args);
264 }
265 }
266}
267
268impl WlDataSource {
269 /// Since when the target event is available.
270 #[allow(dead_code)]
271 pub const EVT__TARGET__SINCE: u32 = 1;
272
273 /// Since when the send event is available.
274 #[allow(dead_code)]
275 pub const EVT__SEND__SINCE: u32 = 1;
276
277 /// Since when the cancelled event is available.
278 #[allow(dead_code)]
279 pub const EVT__CANCELLED__SINCE: u32 = 1;
280
281 /// Since when the dnd_drop_performed event is available.
282 #[allow(dead_code)]
283 pub const EVT__DND_DROP_PERFORMED__SINCE: u32 = 3;
284
285 /// Since when the dnd_finished event is available.
286 #[allow(dead_code)]
287 pub const EVT__DND_FINISHED__SINCE: u32 = 3;
288
289 /// Since when the action event is available.
290 #[allow(dead_code)]
291 pub const EVT__ACTION__SINCE: u32 = 3;
292}
293
294/// An event handler for [WlDataSource] proxies.
295#[allow(dead_code)]
296pub trait WlDataSourceEventHandler {
297 /// a target accepts an offered mime type
298 ///
299 /// Sent when a target accepts pointer_focus or motion events. If
300 /// a target does not accept any of the offered types, type is NULL.
301 ///
302 /// Used for feedback during drag-and-drop.
303 ///
304 /// # Arguments
305 ///
306 /// - `mime_type`: mime type accepted by the target
307 #[inline]
308 fn target(&self, _slf: &WlDataSourceRef, mime_type: Option<&str>) {
309 let _ = mime_type;
310 }
311
312 /// send the data
313 ///
314 /// Request for data from the client. Send the data as the
315 /// specified mime type over the passed file descriptor, then
316 /// close it.
317 ///
318 /// # Arguments
319 ///
320 /// - `mime_type`: mime type for the data
321 /// - `fd`: file descriptor for the data
322 #[inline]
323 fn send(&self, _slf: &WlDataSourceRef, mime_type: &str, fd: OwnedFd) {
324 let _ = mime_type;
325 let _ = fd;
326 }
327
328 /// selection was cancelled
329 ///
330 /// This data source is no longer valid. There are several reasons why
331 /// this could happen:
332 ///
333 /// - The data source has been replaced by another data source.
334 /// - The drag-and-drop operation was performed, but the drop destination
335 /// did not accept any of the mime types offered through
336 /// wl_data_source.target.
337 /// - The drag-and-drop operation was performed, but the drop destination
338 /// did not select any of the actions present in the mask offered through
339 /// wl_data_source.action.
340 /// - The drag-and-drop operation was performed but didn't happen over a
341 /// surface.
342 /// - The compositor cancelled the drag-and-drop operation (e.g. compositor
343 /// dependent timeouts to avoid stale drag-and-drop transfers).
344 ///
345 /// The client should clean up and destroy this data source.
346 ///
347 /// For objects of version 2 or older, wl_data_source.cancelled will
348 /// only be emitted if the data source was replaced by another data
349 /// source.
350 #[inline]
351 fn cancelled(&self, _slf: &WlDataSourceRef) {}
352
353 /// the drag-and-drop operation physically finished
354 ///
355 /// The user performed the drop action. This event does not indicate
356 /// acceptance, wl_data_source.cancelled may still be emitted afterwards
357 /// if the drop destination does not accept any mime type.
358 ///
359 /// However, this event might however not be received if the compositor
360 /// cancelled the drag-and-drop operation before this event could happen.
361 ///
362 /// Note that the data_source may still be used in the future and should
363 /// not be destroyed here.
364 #[inline]
365 fn dnd_drop_performed(&self, _slf: &WlDataSourceRef) {}
366
367 /// the drag-and-drop operation concluded
368 ///
369 /// The drop destination finished interoperating with this data
370 /// source, so the client is now free to destroy this data source and
371 /// free all associated data.
372 ///
373 /// If the action used to perform the operation was "move", the
374 /// source can now delete the transferred data.
375 #[inline]
376 fn dnd_finished(&self, _slf: &WlDataSourceRef) {}
377
378 /// notify the selected action
379 ///
380 /// This event indicates the action selected by the compositor after
381 /// matching the source/destination side actions. Only one action (or
382 /// none) will be offered here.
383 ///
384 /// This event can be emitted multiple times during the drag-and-drop
385 /// operation, mainly in response to destination side changes through
386 /// wl_data_offer.set_actions, and as the data device enters/leaves
387 /// surfaces.
388 ///
389 /// It is only possible to receive this event after
390 /// wl_data_source.dnd_drop_performed if the drag-and-drop operation
391 /// ended in an "ask" action, in which case the final wl_data_source.action
392 /// event will happen immediately before wl_data_source.dnd_finished.
393 ///
394 /// Compositors may also change the selected action on the fly, mainly
395 /// in response to keyboard modifier changes during the drag-and-drop
396 /// operation.
397 ///
398 /// The most recent action received is always the valid one. The chosen
399 /// action may change alongside negotiation (e.g. an "ask" action can turn
400 /// into a "move" operation), so the effects of the final action must
401 /// always be applied in wl_data_offer.dnd_finished.
402 ///
403 /// Clients can trigger cursor surface changes from this point, so
404 /// they reflect the current action.
405 ///
406 /// # Arguments
407 ///
408 /// - `dnd_action`: action selected by the compositor
409 #[inline]
410 fn action(&self, _slf: &WlDataSourceRef, dnd_action: WlDataDeviceManagerDndAction) {
411 let _ = dnd_action;
412 }
413}
414
415impl WlDataSourceEventHandler for private::NoOpEventHandler {}
416
417// SAFETY: - INTERFACE is a valid wl_interface
418unsafe impl<H> EventHandler for private::EventHandler<H>
419where
420 H: WlDataSourceEventHandler,
421{
422 const WL_INTERFACE: &'static wl_interface = &INTERFACE;
423
424 #[allow(unused_variables)]
425 unsafe fn handle_event(
426 &self,
427 queue: &Queue,
428 data: *mut u8,
429 slf: &UntypedBorrowedProxy,
430 opcode: u32,
431 args: *mut wl_argument,
432 ) {
433 // SAFETY: This function requires that slf has the interface INTERFACE
434 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlDataSourceRef>(slf) };
435 match opcode {
436 0 => {
437 // SAFETY: INTERFACE requires that there are 1 arguments
438 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
439 // SAFETY: - INTERFACE requires that args[0] contains a string
440 // - if the pointer is not null, then it is a c string
441 let arg0 = unsafe {
442 convert_optional_string_arg("wl_data_source", "mime_type", args[0].s)
443 };
444 self.0.target(slf, arg0);
445 }
446 1 => {
447 // SAFETY: INTERFACE requires that there are 2 arguments
448 let args = unsafe { &*args.cast::<[wl_argument; 2]>() };
449 // SAFETY: - INTERFACE requires that args[0] contains a string
450 // - if the pointer is not null, then it is a c string
451 let arg0 = unsafe { convert_string_arg("wl_data_source", "mime_type", args[0].s) };
452 // SAFETY: - INTERFACE requires that args[1] contains a file descriptor
453 let arg1 = unsafe { OwnedFd::from_raw_fd(args[1].h) };
454 self.0.send(slf, arg0, arg1);
455 }
456 2 => {
457 self.0.cancelled(slf);
458 }
459 3 => {
460 self.0.dnd_drop_performed(slf);
461 }
462 4 => {
463 self.0.dnd_finished(slf);
464 }
465 5 => {
466 // SAFETY: INTERFACE requires that there are 1 arguments
467 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
468 // SAFETY: - INTERFACE requires that args[0] contains a uint
469 let arg0 = unsafe { WlDataDeviceManagerDndAction(args[0].u) };
470 self.0.action(slf, arg0);
471 }
472 _ => {
473 invalid_opcode("wl_data_source", opcode);
474 }
475 }
476 }
477}
478
479impl<H> CreateEventHandler<H> for private::ProxyApi
480where
481 H: WlDataSourceEventHandler,
482{
483 type EventHandler = private::EventHandler<H>;
484
485 #[inline]
486 fn create_event_handler(handler: H) -> Self::EventHandler {
487 private::EventHandler(handler)
488 }
489}
490
491impl WlDataSource {
492 /// Since when the error.invalid_action_mask enum variant is available.
493 #[allow(dead_code)]
494 pub const ENM__ERROR_INVALID_ACTION_MASK__SINCE: u32 = 1;
495 /// Since when the error.invalid_source enum variant is available.
496 #[allow(dead_code)]
497 pub const ENM__ERROR_INVALID_SOURCE__SINCE: u32 = 1;
498}
499
500#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
501#[allow(dead_code)]
502pub struct WlDataSourceError(pub u32);
503
504impl WlDataSourceError {
505 /// action mask contains invalid values
506 #[allow(dead_code)]
507 pub const INVALID_ACTION_MASK: Self = Self(0);
508
509 /// source doesn't accept this request
510 #[allow(dead_code)]
511 pub const INVALID_SOURCE: Self = Self(1);
512}
513
514impl Debug for WlDataSourceError {
515 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
516 let name = match *self {
517 Self::INVALID_ACTION_MASK => "INVALID_ACTION_MASK",
518 Self::INVALID_SOURCE => "INVALID_SOURCE",
519 _ => return Debug::fmt(&self.0, f),
520 };
521 f.write_str(name)
522 }
523}
524
525/// Functional event handlers.
526pub mod event_handlers {
527 use super::*;
528
529 /// Event handler for target events.
530 pub struct Target<F>(F);
531 impl<F> WlDataSourceEventHandler for Target<F>
532 where
533 F: Fn(&WlDataSourceRef, Option<&str>),
534 {
535 #[inline]
536 fn target(&self, _slf: &WlDataSourceRef, mime_type: Option<&str>) {
537 self.0(_slf, mime_type)
538 }
539 }
540
541 /// Event handler for send events.
542 pub struct Send<F>(F);
543 impl<F> WlDataSourceEventHandler for Send<F>
544 where
545 F: Fn(&WlDataSourceRef, &str, OwnedFd),
546 {
547 #[inline]
548 fn send(&self, _slf: &WlDataSourceRef, mime_type: &str, fd: OwnedFd) {
549 self.0(_slf, mime_type, fd)
550 }
551 }
552
553 /// Event handler for cancelled events.
554 pub struct Cancelled<F>(F);
555 impl<F> WlDataSourceEventHandler for Cancelled<F>
556 where
557 F: Fn(&WlDataSourceRef),
558 {
559 #[inline]
560 fn cancelled(&self, _slf: &WlDataSourceRef) {
561 self.0(_slf)
562 }
563 }
564
565 /// Event handler for dnd_drop_performed events.
566 pub struct DndDropPerformed<F>(F);
567 impl<F> WlDataSourceEventHandler for DndDropPerformed<F>
568 where
569 F: Fn(&WlDataSourceRef),
570 {
571 #[inline]
572 fn dnd_drop_performed(&self, _slf: &WlDataSourceRef) {
573 self.0(_slf)
574 }
575 }
576
577 /// Event handler for dnd_finished events.
578 pub struct DndFinished<F>(F);
579 impl<F> WlDataSourceEventHandler for DndFinished<F>
580 where
581 F: Fn(&WlDataSourceRef),
582 {
583 #[inline]
584 fn dnd_finished(&self, _slf: &WlDataSourceRef) {
585 self.0(_slf)
586 }
587 }
588
589 /// Event handler for action events.
590 pub struct Action<F>(F);
591 impl<F> WlDataSourceEventHandler for Action<F>
592 where
593 F: Fn(&WlDataSourceRef, WlDataDeviceManagerDndAction),
594 {
595 #[inline]
596 fn action(&self, _slf: &WlDataSourceRef, dnd_action: WlDataDeviceManagerDndAction) {
597 self.0(_slf, dnd_action)
598 }
599 }
600
601 impl WlDataSource {
602 /// Creates an event handler for target events.
603 ///
604 /// The event handler ignores all other events.
605 #[allow(dead_code)]
606 pub fn on_target<F>(f: F) -> Target<F>
607 where
608 F: Fn(&WlDataSourceRef, Option<&str>),
609 {
610 Target(f)
611 }
612
613 /// Creates an event handler for send events.
614 ///
615 /// The event handler ignores all other events.
616 #[allow(dead_code)]
617 pub fn on_send<F>(f: F) -> Send<F>
618 where
619 F: Fn(&WlDataSourceRef, &str, OwnedFd),
620 {
621 Send(f)
622 }
623
624 /// Creates an event handler for cancelled events.
625 ///
626 /// The event handler ignores all other events.
627 #[allow(dead_code)]
628 pub fn on_cancelled<F>(f: F) -> Cancelled<F>
629 where
630 F: Fn(&WlDataSourceRef),
631 {
632 Cancelled(f)
633 }
634
635 /// Creates an event handler for dnd_drop_performed events.
636 ///
637 /// The event handler ignores all other events.
638 #[allow(dead_code)]
639 pub fn on_dnd_drop_performed<F>(f: F) -> DndDropPerformed<F>
640 where
641 F: Fn(&WlDataSourceRef),
642 {
643 DndDropPerformed(f)
644 }
645
646 /// Creates an event handler for dnd_finished events.
647 ///
648 /// The event handler ignores all other events.
649 #[allow(dead_code)]
650 pub fn on_dnd_finished<F>(f: F) -> DndFinished<F>
651 where
652 F: Fn(&WlDataSourceRef),
653 {
654 DndFinished(f)
655 }
656
657 /// Creates an event handler for action events.
658 ///
659 /// The event handler ignores all other events.
660 #[allow(dead_code)]
661 pub fn on_action<F>(f: F) -> Action<F>
662 where
663 F: Fn(&WlDataSourceRef, WlDataDeviceManagerDndAction),
664 {
665 Action(f)
666 }
667 }
668}