poll_integration/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        slf: &UntypedBorrowedProxy,
429        opcode: u32,
430        args: *mut wl_argument,
431    ) {
432        // SAFETY: This function required that slf has the interface INTERFACE
433        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlDataSourceRef>(slf) };
434        match opcode {
435            0 => {
436                // SAFETY: INTERFACE requires that there are 1 arguments
437                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
438                // SAFETY: - INTERFACE requires that args[0] contains a string
439                //         - if the pointer is not null, then it is a c string
440                let arg0 = unsafe {
441                    convert_optional_string_arg("wl_data_source", "mime_type", args[0].s)
442                };
443                self.0.target(slf, arg0);
444            }
445            1 => {
446                // SAFETY: INTERFACE requires that there are 2 arguments
447                let args = unsafe { &*args.cast::<[wl_argument; 2]>() };
448                // SAFETY: - INTERFACE requires that args[0] contains a string
449                //         - if the pointer is not null, then it is a c string
450                let arg0 = unsafe { convert_string_arg("wl_data_source", "mime_type", args[0].s) };
451                // SAFETY: - INTERFACE requires that args[1] contains a file descriptor
452                let arg1 = unsafe { OwnedFd::from_raw_fd(args[1].h) };
453                self.0.send(slf, arg0, arg1);
454            }
455            2 => {
456                self.0.cancelled(slf);
457            }
458            3 => {
459                self.0.dnd_drop_performed(slf);
460            }
461            4 => {
462                self.0.dnd_finished(slf);
463            }
464            5 => {
465                // SAFETY: INTERFACE requires that there are 1 arguments
466                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
467                // SAFETY: - INTERFACE requires that args[0] contains a uint
468                let arg0 = unsafe { WlDataDeviceManagerDndAction(args[0].u) };
469                self.0.action(slf, arg0);
470            }
471            _ => {
472                invalid_opcode("wl_data_source", opcode);
473            }
474        }
475    }
476}
477
478impl<H> CreateEventHandler<H> for private::ProxyApi
479where
480    H: WlDataSourceEventHandler,
481{
482    type EventHandler = private::EventHandler<H>;
483
484    #[inline]
485    fn create_event_handler(handler: H) -> Self::EventHandler {
486        private::EventHandler(handler)
487    }
488}
489
490impl WlDataSource {
491    /// Since when the error.invalid_action_mask enum variant is available.
492    #[allow(dead_code)]
493    pub const ENM__ERROR_INVALID_ACTION_MASK__SINCE: u32 = 1;
494    /// Since when the error.invalid_source enum variant is available.
495    #[allow(dead_code)]
496    pub const ENM__ERROR_INVALID_SOURCE__SINCE: u32 = 1;
497}
498
499#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
500#[allow(dead_code)]
501pub struct WlDataSourceError(pub u32);
502
503impl WlDataSourceError {
504    /// action mask contains invalid values
505    #[allow(dead_code)]
506    pub const INVALID_ACTION_MASK: Self = Self(0);
507
508    /// source doesn't accept this request
509    #[allow(dead_code)]
510    pub const INVALID_SOURCE: Self = Self(1);
511}
512
513impl Debug for WlDataSourceError {
514    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
515        let name = match *self {
516            Self::INVALID_ACTION_MASK => "INVALID_ACTION_MASK",
517            Self::INVALID_SOURCE => "INVALID_SOURCE",
518            _ => return Debug::fmt(&self.0, f),
519        };
520        f.write_str(name)
521    }
522}
523
524/// Functional event handlers.
525pub mod event_handlers {
526    use super::*;
527
528    /// Event handler for target events.
529    pub struct Target<F>(F);
530    impl<F> WlDataSourceEventHandler for Target<F>
531    where
532        F: Fn(&WlDataSourceRef, Option<&str>),
533    {
534        #[inline]
535        fn target(&self, _slf: &WlDataSourceRef, mime_type: Option<&str>) {
536            self.0(_slf, mime_type)
537        }
538    }
539
540    /// Event handler for send events.
541    pub struct Send<F>(F);
542    impl<F> WlDataSourceEventHandler for Send<F>
543    where
544        F: Fn(&WlDataSourceRef, &str, OwnedFd),
545    {
546        #[inline]
547        fn send(&self, _slf: &WlDataSourceRef, mime_type: &str, fd: OwnedFd) {
548            self.0(_slf, mime_type, fd)
549        }
550    }
551
552    /// Event handler for cancelled events.
553    pub struct Cancelled<F>(F);
554    impl<F> WlDataSourceEventHandler for Cancelled<F>
555    where
556        F: Fn(&WlDataSourceRef),
557    {
558        #[inline]
559        fn cancelled(&self, _slf: &WlDataSourceRef) {
560            self.0(_slf)
561        }
562    }
563
564    /// Event handler for dnd_drop_performed events.
565    pub struct DndDropPerformed<F>(F);
566    impl<F> WlDataSourceEventHandler for DndDropPerformed<F>
567    where
568        F: Fn(&WlDataSourceRef),
569    {
570        #[inline]
571        fn dnd_drop_performed(&self, _slf: &WlDataSourceRef) {
572            self.0(_slf)
573        }
574    }
575
576    /// Event handler for dnd_finished events.
577    pub struct DndFinished<F>(F);
578    impl<F> WlDataSourceEventHandler for DndFinished<F>
579    where
580        F: Fn(&WlDataSourceRef),
581    {
582        #[inline]
583        fn dnd_finished(&self, _slf: &WlDataSourceRef) {
584            self.0(_slf)
585        }
586    }
587
588    /// Event handler for action events.
589    pub struct Action<F>(F);
590    impl<F> WlDataSourceEventHandler for Action<F>
591    where
592        F: Fn(&WlDataSourceRef, WlDataDeviceManagerDndAction),
593    {
594        #[inline]
595        fn action(&self, _slf: &WlDataSourceRef, dnd_action: WlDataDeviceManagerDndAction) {
596            self.0(_slf, dnd_action)
597        }
598    }
599
600    impl WlDataSource {
601        /// Creates an event handler for target events.
602        ///
603        /// The event handler ignores all other events.
604        #[allow(dead_code)]
605        pub fn on_target<F>(f: F) -> Target<F>
606        where
607            F: Fn(&WlDataSourceRef, Option<&str>),
608        {
609            Target(f)
610        }
611
612        /// Creates an event handler for send events.
613        ///
614        /// The event handler ignores all other events.
615        #[allow(dead_code)]
616        pub fn on_send<F>(f: F) -> Send<F>
617        where
618            F: Fn(&WlDataSourceRef, &str, OwnedFd),
619        {
620            Send(f)
621        }
622
623        /// Creates an event handler for cancelled events.
624        ///
625        /// The event handler ignores all other events.
626        #[allow(dead_code)]
627        pub fn on_cancelled<F>(f: F) -> Cancelled<F>
628        where
629            F: Fn(&WlDataSourceRef),
630        {
631            Cancelled(f)
632        }
633
634        /// Creates an event handler for dnd_drop_performed events.
635        ///
636        /// The event handler ignores all other events.
637        #[allow(dead_code)]
638        pub fn on_dnd_drop_performed<F>(f: F) -> DndDropPerformed<F>
639        where
640            F: Fn(&WlDataSourceRef),
641        {
642            DndDropPerformed(f)
643        }
644
645        /// Creates an event handler for dnd_finished events.
646        ///
647        /// The event handler ignores all other events.
648        #[allow(dead_code)]
649        pub fn on_dnd_finished<F>(f: F) -> DndFinished<F>
650        where
651            F: Fn(&WlDataSourceRef),
652        {
653            DndFinished(f)
654        }
655
656        /// Creates an event handler for action events.
657        ///
658        /// The event handler ignores all other events.
659        #[allow(dead_code)]
660        pub fn on_action<F>(f: F) -> Action<F>
661        where
662            F: Fn(&WlDataSourceRef, WlDataDeviceManagerDndAction),
663        {
664            Action(f)
665        }
666    }
667}