Skip to main content

get_registry/common/protocols/wayland/
wl_data_offer.rs

1//! offer to transfer data
2//!
3//! A wl_data_offer represents a piece of data offered for transfer
4//! by another client (the source client).  It is used by the
5//! copy-and-paste and drag-and-drop mechanisms.  The offer
6//! describes the different mime types that the data can be
7//! converted to and provides the mechanism for transferring the
8//! data directly from the source client.
9
10use super::super::all_types::*;
11use ::wl_client::builder::prelude::*;
12
13static INTERFACE: wl_interface = wl_interface {
14    name: c"wl_data_offer".as_ptr(),
15    version: 3,
16    method_count: 5,
17    methods: {
18        static MESSAGES: [wl_message; 5] = [
19            wl_message {
20                name: c"accept".as_ptr(),
21                signature: c"u?s".as_ptr(),
22                types: {
23                    static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
24                    TYPES.as_ptr().cast()
25                },
26            },
27            wl_message {
28                name: c"receive".as_ptr(),
29                signature: c"sh".as_ptr(),
30                types: {
31                    static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
32                    TYPES.as_ptr().cast()
33                },
34            },
35            wl_message {
36                name: c"destroy".as_ptr(),
37                signature: c"".as_ptr(),
38                types: {
39                    static TYPES: [Option<&'static wl_interface>; 0] = [];
40                    TYPES.as_ptr().cast()
41                },
42            },
43            wl_message {
44                name: c"finish".as_ptr(),
45                signature: c"".as_ptr(),
46                types: {
47                    static TYPES: [Option<&'static wl_interface>; 0] = [];
48                    TYPES.as_ptr().cast()
49                },
50            },
51            wl_message {
52                name: c"set_actions".as_ptr(),
53                signature: c"uu".as_ptr(),
54                types: {
55                    static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
56                    TYPES.as_ptr().cast()
57                },
58            },
59        ];
60        MESSAGES.as_ptr()
61    },
62    event_count: 3,
63    events: {
64        static MESSAGES: [wl_message; 3] = [
65            wl_message {
66                name: c"offer".as_ptr(),
67                signature: c"s".as_ptr(),
68                types: {
69                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
70                    TYPES.as_ptr().cast()
71                },
72            },
73            wl_message {
74                name: c"source_actions".as_ptr(),
75                signature: c"u".as_ptr(),
76                types: {
77                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
78                    TYPES.as_ptr().cast()
79                },
80            },
81            wl_message {
82                name: c"action".as_ptr(),
83                signature: c"u".as_ptr(),
84                types: {
85                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
86                    TYPES.as_ptr().cast()
87                },
88            },
89        ];
90        MESSAGES.as_ptr()
91    },
92};
93
94/// An owned wl_data_offer proxy.
95///
96/// See the documentation of [the module][self] for the interface description.
97#[derive(Clone, Eq, PartialEq)]
98#[repr(transparent)]
99pub struct WlDataOffer {
100    /// This proxy has the interface INTERFACE.
101    proxy: UntypedOwnedProxy,
102}
103
104/// A borrowed wl_data_offer proxy.
105///
106/// See the documentation of [the module][self] for the interface description.
107#[derive(Eq, PartialEq)]
108#[repr(transparent)]
109pub struct WlDataOfferRef {
110    /// This proxy has the interface INTERFACE.
111    proxy: UntypedBorrowedProxy,
112}
113
114// SAFETY: WlDataOffer is a transparent wrapper around UntypedOwnedProxy
115unsafe impl UntypedOwnedProxyWrapper for WlDataOffer {}
116
117// SAFETY: - INTERFACE is a valid wl_interface
118//         - The only invariant is that self.proxy has a compatible interface
119unsafe impl OwnedProxy for WlDataOffer {
120    const INTERFACE: &'static str = "wl_data_offer";
121    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
122    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
123        private::EventHandler(private::NoOpEventHandler);
124    const MAX_VERSION: u32 = 3;
125
126    type Borrowed = WlDataOfferRef;
127    type Api = private::ProxyApi;
128    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
129}
130
131// SAFETY: WlDataOfferRef is a transparent wrapper around UntypedBorrowedProxy
132unsafe impl UntypedBorrowedProxyWrapper for WlDataOfferRef {}
133
134// SAFETY: - The only invariant is that self.proxy has a compatible interface
135unsafe impl BorrowedProxy for WlDataOfferRef {
136    type Owned = WlDataOffer;
137}
138
139impl Deref for WlDataOffer {
140    type Target = WlDataOfferRef;
141
142    fn deref(&self) -> &Self::Target {
143        proxy::low_level::deref(self)
144    }
145}
146
147mod private {
148    pub struct ProxyApi;
149
150    #[allow(dead_code)]
151    pub struct EventHandler<H>(pub(super) H);
152
153    #[allow(dead_code)]
154    pub struct NoOpEventHandler;
155}
156
157impl Debug for WlDataOffer {
158    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
159        write!(f, "wl_data_offer#{}", self.proxy.id())
160    }
161}
162
163impl Debug for WlDataOfferRef {
164    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
165        write!(f, "wl_data_offer#{}", self.proxy.id())
166    }
167}
168
169impl PartialEq<WlDataOfferRef> for WlDataOffer {
170    fn eq(&self, other: &WlDataOfferRef) -> bool {
171        self.proxy == other.proxy
172    }
173}
174
175impl PartialEq<WlDataOffer> for WlDataOfferRef {
176    fn eq(&self, other: &WlDataOffer) -> bool {
177        self.proxy == other.proxy
178    }
179}
180
181#[allow(dead_code)]
182impl WlDataOffer {
183    /// Since when the accept request is available.
184    #[allow(dead_code)]
185    pub const REQ__ACCEPT__SINCE: u32 = 1;
186
187    /// Since when the receive request is available.
188    #[allow(dead_code)]
189    pub const REQ__RECEIVE__SINCE: u32 = 1;
190
191    /// Since when the destroy request is available.
192    #[allow(dead_code)]
193    pub const REQ__DESTROY__SINCE: u32 = 1;
194
195    /// Since when the finish request is available.
196    #[allow(dead_code)]
197    pub const REQ__FINISH__SINCE: u32 = 3;
198
199    /// Since when the set_actions request is available.
200    #[allow(dead_code)]
201    pub const REQ__SET_ACTIONS__SINCE: u32 = 3;
202}
203
204#[allow(dead_code)]
205impl WlDataOffer {
206    /// destroy data offer
207    ///
208    /// Destroy the data offer.
209    #[inline]
210    pub fn destroy(&self) {
211        let mut args = [];
212        // SAFETY: - self.proxy has the interface INTERFACE
213        //         - 2 < INTERFACE.method_count = 5
214        //         - the request signature is ``
215        unsafe {
216            self.proxy.send_destructor(2, &mut args);
217        }
218    }
219}
220
221#[allow(dead_code)]
222impl WlDataOfferRef {
223    /// accept one of the offered mime types
224    ///
225    /// Indicate that the client can accept the given mime type, or
226    /// NULL for not accepted.
227    ///
228    /// For objects of version 2 or older, this request is used by the
229    /// client to give feedback whether the client can receive the given
230    /// mime type, or NULL if none is accepted; the feedback does not
231    /// determine whether the drag-and-drop operation succeeds or not.
232    ///
233    /// For objects of version 3 or newer, this request determines the
234    /// final result of the drag-and-drop operation. If the end result
235    /// is that no mime types were accepted, the drag-and-drop operation
236    /// will be cancelled and the corresponding drag source will receive
237    /// wl_data_source.cancelled. Clients may still use this event in
238    /// conjunction with wl_data_source.action for feedback.
239    ///
240    /// # Arguments
241    ///
242    /// - `serial`: serial number of the accept request
243    /// - `mime_type`: mime type accepted by the client
244    #[inline]
245    pub fn accept(&self, serial: u32, mime_type: Option<&str>) {
246        let (arg0, arg1) = (serial, mime_type);
247        with_cstr_cache(|cache| {
248            let str1_offset = cache.len();
249            if let Some(arg1) = arg1 {
250                cache.extend_from_slice(arg1.as_bytes());
251                cache.push(0);
252            }
253            let mut str1 = ptr::null();
254            if arg1.is_some() {
255                str1 = cache[str1_offset..].as_ptr().cast();
256            }
257            let mut args = [wl_argument { u: arg0 }, wl_argument { s: str1 }];
258            // SAFETY: - self.proxy has the interface INTERFACE
259            //         - 0 < INTERFACE.method_count = 5
260            //         - the request signature is `u?s`
261            unsafe {
262                self.proxy.send_request(0, &mut args);
263            }
264        })
265    }
266
267    /// request that the data is transferred
268    ///
269    /// To transfer the offered data, the client issues this request
270    /// and indicates the mime type it wants to receive.  The transfer
271    /// happens through the passed file descriptor (typically created
272    /// with the pipe system call).  The source client writes the data
273    /// in the mime type representation requested and then closes the
274    /// file descriptor.
275    ///
276    /// The receiving client reads from the read end of the pipe until
277    /// EOF and then closes its end, at which point the transfer is
278    /// complete.
279    ///
280    /// This request may happen multiple times for different mime types,
281    /// both before and after wl_data_device.drop. Drag-and-drop destination
282    /// clients may preemptively fetch data or examine it more closely to
283    /// determine acceptance.
284    ///
285    /// # Arguments
286    ///
287    /// - `mime_type`: mime type desired by receiver
288    /// - `fd`: file descriptor for data transfer
289    #[inline]
290    pub fn receive(&self, mime_type: &str, fd: BorrowedFd<'_>) {
291        let (arg0, arg1) = (mime_type, fd);
292        with_cstr_cache(|cache| {
293            let str0_offset = cache.len();
294            cache.extend_from_slice(arg0.as_bytes());
295            cache.push(0);
296            let str0 = cache[str0_offset..].as_ptr().cast();
297            let mut args = [
298                wl_argument { s: str0 },
299                wl_argument {
300                    h: arg1.as_raw_fd(),
301                },
302            ];
303            // SAFETY: - self.proxy has the interface INTERFACE
304            //         - 1 < INTERFACE.method_count = 5
305            //         - the request signature is `sh`
306            unsafe {
307                self.proxy.send_request(1, &mut args);
308            }
309        })
310    }
311
312    /// the offer will no longer be used
313    ///
314    /// Notifies the compositor that the drag destination successfully
315    /// finished the drag-and-drop operation.
316    ///
317    /// Upon receiving this request, the compositor will emit
318    /// wl_data_source.dnd_finished on the drag source client.
319    ///
320    /// It is a client error to perform other requests than
321    /// wl_data_offer.destroy after this one. It is also an error to perform
322    /// this request after a NULL mime type has been set in
323    /// wl_data_offer.accept or no action was received through
324    /// wl_data_offer.action.
325    ///
326    /// If wl_data_offer.finish request is received for a non drag and drop
327    /// operation, the invalid_finish protocol error is raised.
328    #[inline]
329    pub fn finish(&self) {
330        let mut args = [];
331        // SAFETY: - self.proxy has the interface INTERFACE
332        //         - 3 < INTERFACE.method_count = 5
333        //         - the request signature is ``
334        unsafe {
335            self.proxy.send_request(3, &mut args);
336        }
337    }
338
339    /// set the available/preferred drag-and-drop actions
340    ///
341    /// Sets the actions that the destination side client supports for
342    /// this operation. This request may trigger the emission of
343    /// wl_data_source.action and wl_data_offer.action events if the compositor
344    /// needs to change the selected action.
345    ///
346    /// This request can be called multiple times throughout the
347    /// drag-and-drop operation, typically in response to wl_data_device.enter
348    /// or wl_data_device.motion events.
349    ///
350    /// This request determines the final result of the drag-and-drop
351    /// operation. If the end result is that no action is accepted,
352    /// the drag source will receive wl_data_source.cancelled.
353    ///
354    /// The dnd_actions argument must contain only values expressed in the
355    /// wl_data_device_manager.dnd_actions enum, and the preferred_action
356    /// argument must only contain one of those values set, otherwise it
357    /// will result in a protocol error.
358    ///
359    /// While managing an "ask" action, the destination drag-and-drop client
360    /// may perform further wl_data_offer.receive requests, and is expected
361    /// to perform one last wl_data_offer.set_actions request with a preferred
362    /// action other than "ask" (and optionally wl_data_offer.accept) before
363    /// requesting wl_data_offer.finish, in order to convey the action selected
364    /// by the user. If the preferred action is not in the
365    /// wl_data_offer.source_actions mask, an error will be raised.
366    ///
367    /// If the "ask" action is dismissed (e.g. user cancellation), the client
368    /// is expected to perform wl_data_offer.destroy right away.
369    ///
370    /// This request can only be made on drag-and-drop offers, a protocol error
371    /// will be raised otherwise.
372    ///
373    /// # Arguments
374    ///
375    /// - `dnd_actions`: actions supported by the destination client
376    /// - `preferred_action`: action preferred by the destination client
377    #[inline]
378    pub fn set_actions(
379        &self,
380        dnd_actions: WlDataDeviceManagerDndAction,
381        preferred_action: WlDataDeviceManagerDndAction,
382    ) {
383        let (arg0, arg1) = (dnd_actions, preferred_action);
384        let mut args = [wl_argument { u: arg0.0 }, wl_argument { u: arg1.0 }];
385        // SAFETY: - self.proxy has the interface INTERFACE
386        //         - 4 < INTERFACE.method_count = 5
387        //         - the request signature is `uu`
388        unsafe {
389            self.proxy.send_request(4, &mut args);
390        }
391    }
392}
393
394impl WlDataOffer {
395    /// Since when the offer event is available.
396    #[allow(dead_code)]
397    pub const EVT__OFFER__SINCE: u32 = 1;
398
399    /// Since when the source_actions event is available.
400    #[allow(dead_code)]
401    pub const EVT__SOURCE_ACTIONS__SINCE: u32 = 3;
402
403    /// Since when the action event is available.
404    #[allow(dead_code)]
405    pub const EVT__ACTION__SINCE: u32 = 3;
406}
407
408/// An event handler for [WlDataOffer] proxies.
409#[allow(dead_code)]
410pub trait WlDataOfferEventHandler {
411    /// advertise offered mime type
412    ///
413    /// Sent immediately after creating the wl_data_offer object.  One
414    /// event per offered mime type.
415    ///
416    /// # Arguments
417    ///
418    /// - `mime_type`: offered mime type
419    #[inline]
420    fn offer(&self, _slf: &WlDataOfferRef, mime_type: &str) {
421        let _ = mime_type;
422    }
423
424    /// notify the source-side available actions
425    ///
426    /// This event indicates the actions offered by the data source. It
427    /// will be sent immediately after creating the wl_data_offer object,
428    /// or anytime the source side changes its offered actions through
429    /// wl_data_source.set_actions.
430    ///
431    /// # Arguments
432    ///
433    /// - `source_actions`: actions offered by the data source
434    #[inline]
435    fn source_actions(&self, _slf: &WlDataOfferRef, source_actions: WlDataDeviceManagerDndAction) {
436        let _ = source_actions;
437    }
438
439    /// notify the selected action
440    ///
441    /// This event indicates the action selected by the compositor after
442    /// matching the source/destination side actions. Only one action (or
443    /// none) will be offered here.
444    ///
445    /// This event can be emitted multiple times during the drag-and-drop
446    /// operation in response to destination side action changes through
447    /// wl_data_offer.set_actions.
448    ///
449    /// This event will no longer be emitted after wl_data_device.drop
450    /// happened on the drag-and-drop destination, the client must
451    /// honor the last action received, or the last preferred one set
452    /// through wl_data_offer.set_actions when handling an "ask" action.
453    ///
454    /// Compositors may also change the selected action on the fly, mainly
455    /// in response to keyboard modifier changes during the drag-and-drop
456    /// operation.
457    ///
458    /// The most recent action received is always the valid one. Prior to
459    /// receiving wl_data_device.drop, the chosen action may change (e.g.
460    /// due to keyboard modifiers being pressed). At the time of receiving
461    /// wl_data_device.drop the drag-and-drop destination must honor the
462    /// last action received.
463    ///
464    /// Action changes may still happen after wl_data_device.drop,
465    /// especially on "ask" actions, where the drag-and-drop destination
466    /// may choose another action afterwards. Action changes happening
467    /// at this stage are always the result of inter-client negotiation, the
468    /// compositor shall no longer be able to induce a different action.
469    ///
470    /// Upon "ask" actions, it is expected that the drag-and-drop destination
471    /// may potentially choose a different action and/or mime type,
472    /// based on wl_data_offer.source_actions and finally chosen by the
473    /// user (e.g. popping up a menu with the available options). The
474    /// final wl_data_offer.set_actions and wl_data_offer.accept requests
475    /// must happen before the call to wl_data_offer.finish.
476    ///
477    /// # Arguments
478    ///
479    /// - `dnd_action`: action selected by the compositor
480    #[inline]
481    fn action(&self, _slf: &WlDataOfferRef, dnd_action: WlDataDeviceManagerDndAction) {
482        let _ = dnd_action;
483    }
484}
485
486impl WlDataOfferEventHandler for private::NoOpEventHandler {}
487
488// SAFETY: - INTERFACE is a valid wl_interface
489unsafe impl<H> EventHandler for private::EventHandler<H>
490where
491    H: WlDataOfferEventHandler,
492{
493    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
494
495    #[allow(unused_variables)]
496    unsafe fn handle_event(
497        &self,
498        queue: &Queue,
499        data: *mut u8,
500        slf: &UntypedBorrowedProxy,
501        opcode: u32,
502        args: *mut wl_argument,
503    ) {
504        // SAFETY: This function requires that slf has the interface INTERFACE
505        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlDataOfferRef>(slf) };
506        match opcode {
507            0 => {
508                // SAFETY: INTERFACE requires that there are 1 arguments
509                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
510                // SAFETY: - INTERFACE requires that args[0] contains a string
511                //         - if the pointer is not null, then it is a c string
512                let arg0 = unsafe { convert_string_arg("wl_data_offer", "mime_type", args[0].s) };
513                self.0.offer(slf, arg0);
514            }
515            1 => {
516                // SAFETY: INTERFACE requires that there are 1 arguments
517                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
518                // SAFETY: - INTERFACE requires that args[0] contains a uint
519                let arg0 = unsafe { WlDataDeviceManagerDndAction(args[0].u) };
520                self.0.source_actions(slf, arg0);
521            }
522            2 => {
523                // SAFETY: INTERFACE requires that there are 1 arguments
524                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
525                // SAFETY: - INTERFACE requires that args[0] contains a uint
526                let arg0 = unsafe { WlDataDeviceManagerDndAction(args[0].u) };
527                self.0.action(slf, arg0);
528            }
529            _ => {
530                invalid_opcode("wl_data_offer", opcode);
531            }
532        }
533    }
534}
535
536impl<H> CreateEventHandler<H> for private::ProxyApi
537where
538    H: WlDataOfferEventHandler,
539{
540    type EventHandler = private::EventHandler<H>;
541
542    #[inline]
543    fn create_event_handler(handler: H) -> Self::EventHandler {
544        private::EventHandler(handler)
545    }
546}
547
548impl WlDataOffer {
549    /// Since when the error.invalid_finish enum variant is available.
550    #[allow(dead_code)]
551    pub const ENM__ERROR_INVALID_FINISH__SINCE: u32 = 1;
552    /// Since when the error.invalid_action_mask enum variant is available.
553    #[allow(dead_code)]
554    pub const ENM__ERROR_INVALID_ACTION_MASK__SINCE: u32 = 1;
555    /// Since when the error.invalid_action enum variant is available.
556    #[allow(dead_code)]
557    pub const ENM__ERROR_INVALID_ACTION__SINCE: u32 = 1;
558    /// Since when the error.invalid_offer enum variant is available.
559    #[allow(dead_code)]
560    pub const ENM__ERROR_INVALID_OFFER__SINCE: u32 = 1;
561}
562
563#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
564#[allow(dead_code)]
565pub struct WlDataOfferError(pub u32);
566
567impl WlDataOfferError {
568    /// finish request was called untimely
569    #[allow(dead_code)]
570    pub const INVALID_FINISH: Self = Self(0);
571
572    /// action mask contains invalid values
573    #[allow(dead_code)]
574    pub const INVALID_ACTION_MASK: Self = Self(1);
575
576    /// action argument has an invalid value
577    #[allow(dead_code)]
578    pub const INVALID_ACTION: Self = Self(2);
579
580    /// offer doesn't accept this request
581    #[allow(dead_code)]
582    pub const INVALID_OFFER: Self = Self(3);
583}
584
585impl Debug for WlDataOfferError {
586    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
587        let name = match *self {
588            Self::INVALID_FINISH => "INVALID_FINISH",
589            Self::INVALID_ACTION_MASK => "INVALID_ACTION_MASK",
590            Self::INVALID_ACTION => "INVALID_ACTION",
591            Self::INVALID_OFFER => "INVALID_OFFER",
592            _ => return Debug::fmt(&self.0, f),
593        };
594        f.write_str(name)
595    }
596}
597
598/// Functional event handlers.
599pub mod event_handlers {
600    use super::*;
601
602    /// Event handler for offer events.
603    pub struct Offer<F>(F);
604    impl<F> WlDataOfferEventHandler for Offer<F>
605    where
606        F: Fn(&WlDataOfferRef, &str),
607    {
608        #[inline]
609        fn offer(&self, _slf: &WlDataOfferRef, mime_type: &str) {
610            self.0(_slf, mime_type)
611        }
612    }
613
614    /// Event handler for source_actions events.
615    pub struct SourceActions<F>(F);
616    impl<F> WlDataOfferEventHandler for SourceActions<F>
617    where
618        F: Fn(&WlDataOfferRef, WlDataDeviceManagerDndAction),
619    {
620        #[inline]
621        fn source_actions(
622            &self,
623            _slf: &WlDataOfferRef,
624            source_actions: WlDataDeviceManagerDndAction,
625        ) {
626            self.0(_slf, source_actions)
627        }
628    }
629
630    /// Event handler for action events.
631    pub struct Action<F>(F);
632    impl<F> WlDataOfferEventHandler for Action<F>
633    where
634        F: Fn(&WlDataOfferRef, WlDataDeviceManagerDndAction),
635    {
636        #[inline]
637        fn action(&self, _slf: &WlDataOfferRef, dnd_action: WlDataDeviceManagerDndAction) {
638            self.0(_slf, dnd_action)
639        }
640    }
641
642    impl WlDataOffer {
643        /// Creates an event handler for offer events.
644        ///
645        /// The event handler ignores all other events.
646        #[allow(dead_code)]
647        pub fn on_offer<F>(f: F) -> Offer<F>
648        where
649            F: Fn(&WlDataOfferRef, &str),
650        {
651            Offer(f)
652        }
653
654        /// Creates an event handler for source_actions events.
655        ///
656        /// The event handler ignores all other events.
657        #[allow(dead_code)]
658        pub fn on_source_actions<F>(f: F) -> SourceActions<F>
659        where
660            F: Fn(&WlDataOfferRef, WlDataDeviceManagerDndAction),
661        {
662            SourceActions(f)
663        }
664
665        /// Creates an event handler for action events.
666        ///
667        /// The event handler ignores all other events.
668        #[allow(dead_code)]
669        pub fn on_action<F>(f: F) -> Action<F>
670        where
671            F: Fn(&WlDataOfferRef, WlDataDeviceManagerDndAction),
672        {
673            Action(f)
674        }
675    }
676}