Skip to main content

get_registry/common/protocols_data/wayland/
wl_data_device.rs

1//! data transfer device
2//!
3//! There is one wl_data_device per seat which can be obtained
4//! from the global wl_data_device_manager singleton.
5//!
6//! A wl_data_device provides access to inter-client data transfer
7//! mechanisms such as copy-and-paste and drag-and-drop.
8
9use super::super::all_types::*;
10use ::wl_client::builder::prelude::*;
11
12static INTERFACE: wl_interface = wl_interface {
13    name: c"wl_data_device".as_ptr(),
14    version: 3,
15    method_count: 3,
16    methods: {
17        static MESSAGES: [wl_message; 3] = [
18            wl_message {
19                name: c"start_drag".as_ptr(),
20                signature: c"?oo?ou".as_ptr(),
21                types: {
22                    static TYPES: [Option<&'static wl_interface>; 4] = [
23                        Some(WlDataSource::WL_INTERFACE),
24                        Some(WlSurface::WL_INTERFACE),
25                        Some(WlSurface::WL_INTERFACE),
26                        None,
27                    ];
28                    TYPES.as_ptr().cast()
29                },
30            },
31            wl_message {
32                name: c"set_selection".as_ptr(),
33                signature: c"?ou".as_ptr(),
34                types: {
35                    static TYPES: [Option<&'static wl_interface>; 2] =
36                        [Some(WlDataSource::WL_INTERFACE), None];
37                    TYPES.as_ptr().cast()
38                },
39            },
40            wl_message {
41                name: c"release".as_ptr(),
42                signature: c"".as_ptr(),
43                types: {
44                    static TYPES: [Option<&'static wl_interface>; 0] = [];
45                    TYPES.as_ptr().cast()
46                },
47            },
48        ];
49        MESSAGES.as_ptr()
50    },
51    event_count: 6,
52    events: {
53        static MESSAGES: [wl_message; 6] = [
54            wl_message {
55                name: c"data_offer".as_ptr(),
56                signature: c"n".as_ptr(),
57                types: {
58                    static TYPES: [Option<&'static wl_interface>; 1] =
59                        [Some(WlDataOffer::WL_INTERFACE)];
60                    TYPES.as_ptr().cast()
61                },
62            },
63            wl_message {
64                name: c"enter".as_ptr(),
65                signature: c"uoff?o".as_ptr(),
66                types: {
67                    static TYPES: [Option<&'static wl_interface>; 5] = [
68                        None,
69                        Some(WlSurface::WL_INTERFACE),
70                        None,
71                        None,
72                        Some(WlDataOffer::WL_INTERFACE),
73                    ];
74                    TYPES.as_ptr().cast()
75                },
76            },
77            wl_message {
78                name: c"leave".as_ptr(),
79                signature: c"".as_ptr(),
80                types: {
81                    static TYPES: [Option<&'static wl_interface>; 0] = [];
82                    TYPES.as_ptr().cast()
83                },
84            },
85            wl_message {
86                name: c"motion".as_ptr(),
87                signature: c"uff".as_ptr(),
88                types: {
89                    static TYPES: [Option<&'static wl_interface>; 3] = [None, None, None];
90                    TYPES.as_ptr().cast()
91                },
92            },
93            wl_message {
94                name: c"drop".as_ptr(),
95                signature: c"".as_ptr(),
96                types: {
97                    static TYPES: [Option<&'static wl_interface>; 0] = [];
98                    TYPES.as_ptr().cast()
99                },
100            },
101            wl_message {
102                name: c"selection".as_ptr(),
103                signature: c"?o".as_ptr(),
104                types: {
105                    static TYPES: [Option<&'static wl_interface>; 1] =
106                        [Some(WlDataOffer::WL_INTERFACE)];
107                    TYPES.as_ptr().cast()
108                },
109            },
110        ];
111        MESSAGES.as_ptr()
112    },
113};
114
115/// An owned wl_data_device proxy.
116///
117/// See the documentation of [the module][self] for the interface description.
118#[derive(Clone, Eq, PartialEq)]
119#[repr(transparent)]
120pub struct WlDataDevice {
121    /// This proxy has the interface INTERFACE.
122    proxy: UntypedOwnedProxy,
123}
124
125/// A borrowed wl_data_device proxy.
126///
127/// See the documentation of [the module][self] for the interface description.
128#[derive(Eq, PartialEq)]
129#[repr(transparent)]
130pub struct WlDataDeviceRef {
131    /// This proxy has the interface INTERFACE.
132    proxy: UntypedBorrowedProxy,
133}
134
135// SAFETY: WlDataDevice is a transparent wrapper around UntypedOwnedProxy
136unsafe impl UntypedOwnedProxyWrapper for WlDataDevice {}
137
138// SAFETY: - INTERFACE is a valid wl_interface
139//         - The only invariant is that self.proxy has a compatible interface
140unsafe impl OwnedProxy for WlDataDevice {
141    const INTERFACE: &'static str = "wl_data_device";
142    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
143    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
144        private::EventHandler(private::NoOpEventHandler);
145    const MAX_VERSION: u32 = 3;
146
147    type Borrowed = WlDataDeviceRef;
148    type Api = private::ProxyApi;
149    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
150}
151
152// SAFETY: WlDataDeviceRef is a transparent wrapper around UntypedBorrowedProxy
153unsafe impl UntypedBorrowedProxyWrapper for WlDataDeviceRef {}
154
155// SAFETY: - The only invariant is that self.proxy has a compatible interface
156unsafe impl BorrowedProxy for WlDataDeviceRef {
157    type Owned = WlDataDevice;
158}
159
160impl Deref for WlDataDevice {
161    type Target = WlDataDeviceRef;
162
163    fn deref(&self) -> &Self::Target {
164        proxy::low_level::deref(self)
165    }
166}
167
168mod private {
169    pub struct ProxyApi;
170
171    #[allow(dead_code)]
172    pub struct EventHandler<H>(pub(super) H);
173
174    #[allow(dead_code)]
175    pub struct NoOpEventHandler;
176}
177
178impl Debug for WlDataDevice {
179    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
180        write!(f, "wl_data_device#{}", self.proxy.id())
181    }
182}
183
184impl Debug for WlDataDeviceRef {
185    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
186        write!(f, "wl_data_device#{}", self.proxy.id())
187    }
188}
189
190impl PartialEq<WlDataDeviceRef> for WlDataDevice {
191    fn eq(&self, other: &WlDataDeviceRef) -> bool {
192        self.proxy == other.proxy
193    }
194}
195
196impl PartialEq<WlDataDevice> for WlDataDeviceRef {
197    fn eq(&self, other: &WlDataDevice) -> bool {
198        self.proxy == other.proxy
199    }
200}
201
202#[allow(dead_code)]
203impl WlDataDevice {
204    /// Since when the start_drag request is available.
205    #[allow(dead_code)]
206    pub const REQ__START_DRAG__SINCE: u32 = 1;
207
208    /// Since when the set_selection request is available.
209    #[allow(dead_code)]
210    pub const REQ__SET_SELECTION__SINCE: u32 = 1;
211
212    /// Since when the release request is available.
213    #[allow(dead_code)]
214    pub const REQ__RELEASE__SINCE: u32 = 2;
215}
216
217#[allow(dead_code)]
218impl WlDataDevice {
219    /// destroy data device
220    ///
221    /// This request destroys the data device.
222    #[inline]
223    pub fn release(&self) {
224        let mut args = [];
225        // SAFETY: - self.proxy has the interface INTERFACE
226        //         - 2 < INTERFACE.method_count = 3
227        //         - the request signature is ``
228        unsafe {
229            self.proxy.send_destructor(2, &mut args);
230        }
231    }
232}
233
234#[allow(dead_code)]
235impl WlDataDeviceRef {
236    /// start drag-and-drop operation
237    ///
238    /// This request asks the compositor to start a drag-and-drop
239    /// operation on behalf of the client.
240    ///
241    /// The source argument is the data source that provides the data
242    /// for the eventual data transfer. If source is NULL, enter, leave
243    /// and motion events are sent only to the client that initiated the
244    /// drag and the client is expected to handle the data passing
245    /// internally. If source is destroyed, the drag-and-drop session will be
246    /// cancelled.
247    ///
248    /// The origin surface is the surface where the drag originates and
249    /// the client must have an active implicit grab that matches the
250    /// serial.
251    ///
252    /// The icon surface is an optional (can be NULL) surface that
253    /// provides an icon to be moved around with the cursor.  Initially,
254    /// the top-left corner of the icon surface is placed at the cursor
255    /// hotspot, but subsequent wl_surface.offset requests can move the
256    /// relative position. Attach requests must be confirmed with
257    /// wl_surface.commit as usual. The icon surface is given the role of
258    /// a drag-and-drop icon. If the icon surface already has another role,
259    /// it raises a protocol error.
260    ///
261    /// The input region is ignored for wl_surfaces with the role of a
262    /// drag-and-drop icon.
263    ///
264    /// The given source may not be used in any further set_selection or
265    /// start_drag requests. Attempting to reuse a previously-used source
266    /// may send a used_source error.
267    ///
268    /// # Arguments
269    ///
270    /// - `source`: data source for the eventual transfer
271    /// - `origin`: surface where the drag originates
272    /// - `icon`: drag-and-drop icon surface
273    /// - `serial`: serial number of the implicit grab on the origin
274    #[inline]
275    pub fn start_drag(
276        &self,
277        source: Option<&WlDataSourceRef>,
278        origin: &WlSurfaceRef,
279        icon: Option<&WlSurfaceRef>,
280        serial: u32,
281    ) {
282        let (arg0, arg1, arg2, arg3) = (source, origin, icon, serial);
283        let obj0_lock = arg0.map(|arg0| proxy::lock(arg0));
284        let obj0 = obj0_lock
285            .map(|obj0_lock| check_argument_proxy("source", obj0_lock.wl_proxy()))
286            .unwrap_or(ptr::null_mut());
287        let obj1_lock = proxy::lock(arg1);
288        let obj1 = check_argument_proxy("origin", obj1_lock.wl_proxy());
289        let obj2_lock = arg2.map(|arg2| proxy::lock(arg2));
290        let obj2 = obj2_lock
291            .map(|obj2_lock| check_argument_proxy("icon", obj2_lock.wl_proxy()))
292            .unwrap_or(ptr::null_mut());
293        let mut args = [
294            wl_argument { o: obj0 },
295            wl_argument { o: obj1 },
296            wl_argument { o: obj2 },
297            wl_argument { u: arg3 },
298        ];
299        // SAFETY: - self.proxy has the interface INTERFACE
300        //         - 0 < INTERFACE.method_count = 3
301        //         - the request signature is `?oo?ou`
302        unsafe {
303            self.proxy.send_request(0, &mut args);
304        }
305    }
306
307    /// copy data to the selection
308    ///
309    /// This request asks the compositor to set the selection
310    /// to the data from the source on behalf of the client.
311    ///
312    /// To unset the selection, set the source to NULL.
313    ///
314    /// The given source may not be used in any further set_selection or
315    /// start_drag requests. Attempting to reuse a previously-used source
316    /// may send a used_source error.
317    ///
318    /// # Arguments
319    ///
320    /// - `source`: data source for the selection
321    /// - `serial`: serial number of the event that triggered this request
322    #[inline]
323    pub fn set_selection(&self, source: Option<&WlDataSourceRef>, serial: u32) {
324        let (arg0, arg1) = (source, serial);
325        let obj0_lock = arg0.map(|arg0| proxy::lock(arg0));
326        let obj0 = obj0_lock
327            .map(|obj0_lock| check_argument_proxy("source", obj0_lock.wl_proxy()))
328            .unwrap_or(ptr::null_mut());
329        let mut args = [wl_argument { o: obj0 }, wl_argument { u: arg1 }];
330        // SAFETY: - self.proxy has the interface INTERFACE
331        //         - 1 < INTERFACE.method_count = 3
332        //         - the request signature is `?ou`
333        unsafe {
334            self.proxy.send_request(1, &mut args);
335        }
336    }
337}
338
339impl WlDataDevice {
340    /// Since when the data_offer event is available.
341    #[allow(dead_code)]
342    pub const EVT__DATA_OFFER__SINCE: u32 = 1;
343
344    /// Since when the enter event is available.
345    #[allow(dead_code)]
346    pub const EVT__ENTER__SINCE: u32 = 1;
347
348    /// Since when the leave event is available.
349    #[allow(dead_code)]
350    pub const EVT__LEAVE__SINCE: u32 = 1;
351
352    /// Since when the motion event is available.
353    #[allow(dead_code)]
354    pub const EVT__MOTION__SINCE: u32 = 1;
355
356    /// Since when the drop event is available.
357    #[allow(dead_code)]
358    pub const EVT__DROP__SINCE: u32 = 1;
359
360    /// Since when the selection event is available.
361    #[allow(dead_code)]
362    pub const EVT__SELECTION__SINCE: u32 = 1;
363}
364
365/// An event handler for [WlDataDevice] proxies.
366#[allow(dead_code)]
367pub trait WlDataDeviceEventHandler {
368    type Data: 'static;
369
370    /// introduce a new wl_data_offer
371    ///
372    /// The data_offer event introduces a new wl_data_offer object,
373    /// which will subsequently be used in either the
374    /// data_device.enter event (for drag-and-drop) or the
375    /// data_device.selection event (for selections).  Immediately
376    /// following the data_device.data_offer event, the new data_offer
377    /// object will send out data_offer.offer events to describe the
378    /// mime types it offers.
379    ///
380    /// # Arguments
381    ///
382    /// - `id`: the new data_offer object
383    #[inline]
384    fn data_offer(&self, _data: &mut Self::Data, _slf: &WlDataDeviceRef, id: WlDataOffer) {
385        let _ = id;
386    }
387
388    /// initiate drag-and-drop session
389    ///
390    /// This event is sent when an active drag-and-drop pointer enters
391    /// a surface owned by the client.  The position of the pointer at
392    /// enter time is provided by the x and y arguments, in surface-local
393    /// coordinates.
394    ///
395    /// # Arguments
396    ///
397    /// - `serial`: serial number of the enter event
398    /// - `surface`: client surface entered
399    /// - `x`: surface-local x coordinate
400    /// - `y`: surface-local y coordinate
401    /// - `id`: source data_offer object
402    ///
403    /// All borrowed proxies passed to this function are guaranteed to be
404    /// immutable and non-null.
405    #[inline]
406    fn enter(
407        &self,
408        _data: &mut Self::Data,
409        _slf: &WlDataDeviceRef,
410        serial: u32,
411        surface: Option<&WlSurfaceRef>,
412        x: Fixed,
413        y: Fixed,
414        id: Option<&WlDataOfferRef>,
415    ) {
416        let _ = serial;
417        let _ = surface;
418        let _ = x;
419        let _ = y;
420        let _ = id;
421    }
422
423    /// end drag-and-drop session
424    ///
425    /// This event is sent when the drag-and-drop pointer leaves the
426    /// surface and the session ends.  The client must destroy the
427    /// wl_data_offer introduced at enter time at this point.
428    #[inline]
429    fn leave(&self, _data: &mut Self::Data, _slf: &WlDataDeviceRef) {}
430
431    /// drag-and-drop session motion
432    ///
433    /// This event is sent when the drag-and-drop pointer moves within
434    /// the currently focused surface. The new position of the pointer
435    /// is provided by the x and y arguments, in surface-local
436    /// coordinates.
437    ///
438    /// # Arguments
439    ///
440    /// - `time`: timestamp with millisecond granularity
441    /// - `x`: surface-local x coordinate
442    /// - `y`: surface-local y coordinate
443    #[inline]
444    fn motion(
445        &self,
446        _data: &mut Self::Data,
447        _slf: &WlDataDeviceRef,
448        time: u32,
449        x: Fixed,
450        y: Fixed,
451    ) {
452        let _ = time;
453        let _ = x;
454        let _ = y;
455    }
456
457    /// end drag-and-drop session successfully
458    ///
459    /// The event is sent when a drag-and-drop operation is ended
460    /// because the implicit grab is removed.
461    ///
462    /// The drag-and-drop destination is expected to honor the last action
463    /// received through wl_data_offer.action, if the resulting action is
464    /// "copy" or "move", the destination can still perform
465    /// wl_data_offer.receive requests, and is expected to end all
466    /// transfers with a wl_data_offer.finish request.
467    ///
468    /// If the resulting action is "ask", the action will not be considered
469    /// final. The drag-and-drop destination is expected to perform one last
470    /// wl_data_offer.set_actions request, or wl_data_offer.destroy in order
471    /// to cancel the operation.
472    #[inline]
473    fn drop(&self, _data: &mut Self::Data, _slf: &WlDataDeviceRef) {}
474
475    /// advertise new selection
476    ///
477    /// The selection event is sent out to notify the client of a new
478    /// wl_data_offer for the selection for this device.  The
479    /// data_device.data_offer and the data_offer.offer events are
480    /// sent out immediately before this event to introduce the data
481    /// offer object.  The selection event is sent to a client
482    /// immediately before receiving keyboard focus and when a new
483    /// selection is set while the client has keyboard focus.  The
484    /// data_offer is valid until a new data_offer or NULL is received
485    /// or until the client loses keyboard focus.  Switching surface with
486    /// keyboard focus within the same client doesn't mean a new selection
487    /// will be sent.  The client must destroy the previous selection
488    /// data_offer, if any, upon receiving this event.
489    ///
490    /// # Arguments
491    ///
492    /// - `id`: selection data_offer object
493    ///
494    /// All borrowed proxies passed to this function are guaranteed to be
495    /// immutable and non-null.
496    #[inline]
497    fn selection(
498        &self,
499        _data: &mut Self::Data,
500        _slf: &WlDataDeviceRef,
501        id: Option<&WlDataOfferRef>,
502    ) {
503        let _ = id;
504    }
505}
506
507impl WlDataDeviceEventHandler for private::NoOpEventHandler {
508    type Data = ();
509}
510
511// SAFETY: - INTERFACE is a valid wl_interface
512//         - mutable_type always returns the same value
513unsafe impl<H> EventHandler for private::EventHandler<H>
514where
515    H: WlDataDeviceEventHandler,
516{
517    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
518
519    #[inline]
520    fn mutable_type() -> Option<(TypeId, &'static str)> {
521        let id = TypeId::of::<H::Data>();
522        let name = std::any::type_name::<H::Data>();
523        Some((id, name))
524    }
525
526    #[allow(unused_variables)]
527    unsafe fn handle_event(
528        &self,
529        queue: &Queue,
530        data: *mut u8,
531        slf: &UntypedBorrowedProxy,
532        opcode: u32,
533        args: *mut wl_argument,
534    ) {
535        // SAFETY: This function requires that slf has the interface INTERFACE
536        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlDataDeviceRef>(slf) };
537        // SAFETY: This function requires that data is `&mut T` where `T`
538        //         has the type id returned by `Self::mutable_type`, i.e.,
539        //         `T = H::Data`.
540        let data: &mut H::Data = unsafe { &mut *data.cast() };
541        match opcode {
542            0 => {
543                // SAFETY: INTERFACE requires that there are 1 arguments
544                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
545                // SAFETY: - INTERFACE requires that args[0] contains an object
546                //         - ownership is transferred to this function
547                //         - INTERFACE requires that the object has the interface WlDataOffer::WL_INTERFACE
548                let arg0 = unsafe {
549                    UntypedOwnedProxy::from_plain_wl_proxy(
550                        queue,
551                        NonNull::new_unchecked(args[0].o.cast()),
552                        WlDataOffer::WL_INTERFACE,
553                    )
554                };
555                // SAFETY: - INTERFACE requires that the object has the interface WlDataOffer::WL_INTERFACE
556                let arg0 = unsafe { proxy::low_level::from_untyped_owned::<WlDataOffer>(arg0) };
557                self.0.data_offer(data, slf, arg0);
558            }
559            1 => {
560                // SAFETY: INTERFACE requires that there are 5 arguments
561                let args = unsafe { &*args.cast::<[wl_argument; 5]>() };
562                // SAFETY: - INTERFACE requires that args[0] contains a uint
563                let arg0 = unsafe { args[0].u };
564                // SAFETY: - INTERFACE requires that args[1] contains an object
565                let arg1 = unsafe {
566                    if let Some(p) = NonNull::new(args[1].o.cast()) {
567                        Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
568                    } else {
569                        None
570                    }
571                };
572                // SAFETY: - INTERFACE requires that the object has the interface WlSurface::WL_INTERFACE
573                let arg1 = arg1.as_ref().map(|arg1| unsafe {
574                    proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(arg1)
575                });
576                // SAFETY: - INTERFACE requires that args[2] contains a fixed
577                let arg2 = unsafe { Fixed::from_wire(args[2].f) };
578                // SAFETY: - INTERFACE requires that args[3] contains a fixed
579                let arg3 = unsafe { Fixed::from_wire(args[3].f) };
580                // SAFETY: - INTERFACE requires that args[4] contains an object
581                let arg4 = unsafe {
582                    if let Some(p) = NonNull::new(args[4].o.cast()) {
583                        Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
584                    } else {
585                        None
586                    }
587                };
588                // SAFETY: - INTERFACE requires that the object has the interface WlDataOffer::WL_INTERFACE
589                let arg4 = arg4.as_ref().map(|arg4| unsafe {
590                    proxy::low_level::from_untyped_borrowed::<WlDataOfferRef>(arg4)
591                });
592                self.0.enter(data, slf, arg0, arg1, arg2, arg3, arg4);
593            }
594            2 => {
595                self.0.leave(data, slf);
596            }
597            3 => {
598                // SAFETY: INTERFACE requires that there are 3 arguments
599                let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
600                // SAFETY: - INTERFACE requires that args[0] contains a uint
601                let arg0 = unsafe { args[0].u };
602                // SAFETY: - INTERFACE requires that args[1] contains a fixed
603                let arg1 = unsafe { Fixed::from_wire(args[1].f) };
604                // SAFETY: - INTERFACE requires that args[2] contains a fixed
605                let arg2 = unsafe { Fixed::from_wire(args[2].f) };
606                self.0.motion(data, slf, arg0, arg1, arg2);
607            }
608            4 => {
609                self.0.drop(data, slf);
610            }
611            5 => {
612                // SAFETY: INTERFACE requires that there are 1 arguments
613                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
614                // SAFETY: - INTERFACE requires that args[0] contains an object
615                let arg0 = unsafe {
616                    if let Some(p) = NonNull::new(args[0].o.cast()) {
617                        Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
618                    } else {
619                        None
620                    }
621                };
622                // SAFETY: - INTERFACE requires that the object has the interface WlDataOffer::WL_INTERFACE
623                let arg0 = arg0.as_ref().map(|arg0| unsafe {
624                    proxy::low_level::from_untyped_borrowed::<WlDataOfferRef>(arg0)
625                });
626                self.0.selection(data, slf, arg0);
627            }
628            _ => {
629                invalid_opcode("wl_data_device", opcode);
630            }
631        }
632    }
633}
634
635impl<H> CreateEventHandler<H> for private::ProxyApi
636where
637    H: WlDataDeviceEventHandler,
638{
639    type EventHandler = private::EventHandler<H>;
640
641    #[inline]
642    fn create_event_handler(handler: H) -> Self::EventHandler {
643        private::EventHandler(handler)
644    }
645}
646
647impl WlDataDevice {
648    /// Since when the error.role enum variant is available.
649    #[allow(dead_code)]
650    pub const ENM__ERROR_ROLE__SINCE: u32 = 1;
651    /// Since when the error.used_source enum variant is available.
652    #[allow(dead_code)]
653    pub const ENM__ERROR_USED_SOURCE__SINCE: u32 = 1;
654}
655
656#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
657#[allow(dead_code)]
658pub struct WlDataDeviceError(pub u32);
659
660impl WlDataDeviceError {
661    /// given wl_surface has another role
662    #[allow(dead_code)]
663    pub const ROLE: Self = Self(0);
664
665    /// source has already been used
666    #[allow(dead_code)]
667    pub const USED_SOURCE: Self = Self(1);
668}
669
670impl Debug for WlDataDeviceError {
671    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
672        let name = match *self {
673            Self::ROLE => "ROLE",
674            Self::USED_SOURCE => "USED_SOURCE",
675            _ => return Debug::fmt(&self.0, f),
676        };
677        f.write_str(name)
678    }
679}
680
681/// Functional event handlers.
682pub mod event_handlers {
683    use super::*;
684
685    /// Event handler for data_offer events.
686    pub struct DataOffer<T, F>(F, PhantomData<fn(&mut T)>);
687    impl<T, F> WlDataDeviceEventHandler for DataOffer<T, F>
688    where
689        T: 'static,
690        F: Fn(&mut T, &WlDataDeviceRef, WlDataOffer),
691    {
692        type Data = T;
693
694        #[inline]
695        fn data_offer(&self, _data: &mut T, _slf: &WlDataDeviceRef, id: WlDataOffer) {
696            self.0(_data, _slf, id)
697        }
698    }
699
700    /// Event handler for enter events.
701    pub struct Enter<T, F>(F, PhantomData<fn(&mut T)>);
702    impl<T, F> WlDataDeviceEventHandler for Enter<T, F>
703    where
704        T: 'static,
705        F: Fn(
706            &mut T,
707            &WlDataDeviceRef,
708            u32,
709            Option<&WlSurfaceRef>,
710            Fixed,
711            Fixed,
712            Option<&WlDataOfferRef>,
713        ),
714    {
715        type Data = T;
716
717        #[inline]
718        fn enter(
719            &self,
720            _data: &mut T,
721            _slf: &WlDataDeviceRef,
722            serial: u32,
723            surface: Option<&WlSurfaceRef>,
724            x: Fixed,
725            y: Fixed,
726            id: Option<&WlDataOfferRef>,
727        ) {
728            self.0(_data, _slf, serial, surface, x, y, id)
729        }
730    }
731
732    /// Event handler for leave events.
733    pub struct Leave<T, F>(F, PhantomData<fn(&mut T)>);
734    impl<T, F> WlDataDeviceEventHandler for Leave<T, F>
735    where
736        T: 'static,
737        F: Fn(&mut T, &WlDataDeviceRef),
738    {
739        type Data = T;
740
741        #[inline]
742        fn leave(&self, _data: &mut T, _slf: &WlDataDeviceRef) {
743            self.0(_data, _slf)
744        }
745    }
746
747    /// Event handler for motion events.
748    pub struct Motion<T, F>(F, PhantomData<fn(&mut T)>);
749    impl<T, F> WlDataDeviceEventHandler for Motion<T, F>
750    where
751        T: 'static,
752        F: Fn(&mut T, &WlDataDeviceRef, u32, Fixed, Fixed),
753    {
754        type Data = T;
755
756        #[inline]
757        fn motion(&self, _data: &mut T, _slf: &WlDataDeviceRef, time: u32, x: Fixed, y: Fixed) {
758            self.0(_data, _slf, time, x, y)
759        }
760    }
761
762    /// Event handler for drop events.
763    pub struct Drop<T, F>(F, PhantomData<fn(&mut T)>);
764    impl<T, F> WlDataDeviceEventHandler for Drop<T, F>
765    where
766        T: 'static,
767        F: Fn(&mut T, &WlDataDeviceRef),
768    {
769        type Data = T;
770
771        #[inline]
772        fn drop(&self, _data: &mut T, _slf: &WlDataDeviceRef) {
773            self.0(_data, _slf)
774        }
775    }
776
777    /// Event handler for selection events.
778    pub struct Selection<T, F>(F, PhantomData<fn(&mut T)>);
779    impl<T, F> WlDataDeviceEventHandler for Selection<T, F>
780    where
781        T: 'static,
782        F: Fn(&mut T, &WlDataDeviceRef, Option<&WlDataOfferRef>),
783    {
784        type Data = T;
785
786        #[inline]
787        fn selection(&self, _data: &mut T, _slf: &WlDataDeviceRef, id: Option<&WlDataOfferRef>) {
788            self.0(_data, _slf, id)
789        }
790    }
791
792    impl WlDataDevice {
793        /// Creates an event handler for data_offer events.
794        ///
795        /// The event handler ignores all other events.
796        #[allow(dead_code)]
797        pub fn on_data_offer<T, F>(f: F) -> DataOffer<T, F>
798        where
799            T: 'static,
800            F: Fn(&mut T, &WlDataDeviceRef, WlDataOffer),
801        {
802            DataOffer(f, PhantomData)
803        }
804
805        /// Creates an event handler for enter events.
806        ///
807        /// The event handler ignores all other events.
808        #[allow(dead_code)]
809        pub fn on_enter<T, F>(f: F) -> Enter<T, F>
810        where
811            T: 'static,
812            F: Fn(
813                &mut T,
814                &WlDataDeviceRef,
815                u32,
816                Option<&WlSurfaceRef>,
817                Fixed,
818                Fixed,
819                Option<&WlDataOfferRef>,
820            ),
821        {
822            Enter(f, PhantomData)
823        }
824
825        /// Creates an event handler for leave events.
826        ///
827        /// The event handler ignores all other events.
828        #[allow(dead_code)]
829        pub fn on_leave<T, F>(f: F) -> Leave<T, F>
830        where
831            T: 'static,
832            F: Fn(&mut T, &WlDataDeviceRef),
833        {
834            Leave(f, PhantomData)
835        }
836
837        /// Creates an event handler for motion events.
838        ///
839        /// The event handler ignores all other events.
840        #[allow(dead_code)]
841        pub fn on_motion<T, F>(f: F) -> Motion<T, F>
842        where
843            T: 'static,
844            F: Fn(&mut T, &WlDataDeviceRef, u32, Fixed, Fixed),
845        {
846            Motion(f, PhantomData)
847        }
848
849        /// Creates an event handler for drop events.
850        ///
851        /// The event handler ignores all other events.
852        #[allow(dead_code)]
853        pub fn on_drop<T, F>(f: F) -> Drop<T, F>
854        where
855            T: 'static,
856            F: Fn(&mut T, &WlDataDeviceRef),
857        {
858            Drop(f, PhantomData)
859        }
860
861        /// Creates an event handler for selection events.
862        ///
863        /// The event handler ignores all other events.
864        #[allow(dead_code)]
865        pub fn on_selection<T, F>(f: F) -> Selection<T, F>
866        where
867            T: 'static,
868            F: Fn(&mut T, &WlDataDeviceRef, Option<&WlDataOfferRef>),
869        {
870            Selection(f, PhantomData)
871        }
872    }
873}