Skip to main content

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