simple_window/common/protocols_data/wayland/
wl_data_device_manager.rs

1//! data transfer interface
2//!
3//! The wl_data_device_manager is a singleton global object that
4//! provides access to inter-client data transfer mechanisms such as
5//! copy-and-paste and drag-and-drop.  These mechanisms are tied to
6//! a wl_seat and this interface lets a client get a wl_data_device
7//! corresponding to a wl_seat.
8//!
9//! Depending on the version bound, the objects created from the bound
10//! wl_data_device_manager object will have different requirements for
11//! functioning properly. See wl_data_source.set_actions,
12//! wl_data_offer.accept and wl_data_offer.finish for details.
13
14use {super::super::all_types::*, ::wl_client::builder::prelude::*};
15
16static INTERFACE: wl_interface = wl_interface {
17    name: c"wl_data_device_manager".as_ptr(),
18    version: 3,
19    method_count: 2,
20    methods: {
21        static MESSAGES: [wl_message; 2] = [
22            wl_message {
23                name: c"create_data_source".as_ptr(),
24                signature: c"n".as_ptr(),
25                types: {
26                    static TYPES: [Option<&'static wl_interface>; 1] =
27                        [Some(WlDataSource::WL_INTERFACE)];
28                    TYPES.as_ptr().cast()
29                },
30            },
31            wl_message {
32                name: c"get_data_device".as_ptr(),
33                signature: c"no".as_ptr(),
34                types: {
35                    static TYPES: [Option<&'static wl_interface>; 2] =
36                        [Some(WlDataDevice::WL_INTERFACE), Some(WlSeat::WL_INTERFACE)];
37                    TYPES.as_ptr().cast()
38                },
39            },
40        ];
41        MESSAGES.as_ptr()
42    },
43    event_count: 0,
44    events: ptr::null(),
45};
46
47/// An owned wl_data_device_manager proxy.
48///
49/// See the documentation of [the module][self] for the interface description.
50#[derive(Clone, Eq, PartialEq)]
51#[repr(transparent)]
52pub struct WlDataDeviceManager {
53    /// This proxy has the interface INTERFACE.
54    proxy: UntypedOwnedProxy,
55}
56
57/// A borrowed wl_data_device_manager proxy.
58///
59/// See the documentation of [the module][self] for the interface description.
60#[derive(Eq, PartialEq)]
61#[repr(transparent)]
62pub struct WlDataDeviceManagerRef {
63    /// This proxy has the interface INTERFACE.
64    proxy: UntypedBorrowedProxy,
65}
66
67// SAFETY: WlDataDeviceManager is a transparent wrapper around UntypedOwnedProxy
68unsafe impl UntypedOwnedProxyWrapper for WlDataDeviceManager {}
69
70// SAFETY: - INTERFACE is a valid wl_interface
71//         - The only invariant is that self.proxy has a compatible interface
72unsafe impl OwnedProxy for WlDataDeviceManager {
73    const INTERFACE: &'static str = "wl_data_device_manager";
74    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
75    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
76        private::EventHandler(private::NoOpEventHandler);
77    const MAX_VERSION: u32 = 3;
78
79    type Borrowed = WlDataDeviceManagerRef;
80    type Api = private::ProxyApi;
81    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
82}
83
84// SAFETY: WlDataDeviceManagerRef is a transparent wrapper around UntypedBorrowedProxy
85unsafe impl UntypedBorrowedProxyWrapper for WlDataDeviceManagerRef {}
86
87// SAFETY: - The only invariant is that self.proxy has a compatible interface
88unsafe impl BorrowedProxy for WlDataDeviceManagerRef {
89    type Owned = WlDataDeviceManager;
90}
91
92impl Deref for WlDataDeviceManager {
93    type Target = WlDataDeviceManagerRef;
94
95    fn deref(&self) -> &Self::Target {
96        proxy::low_level::deref(self)
97    }
98}
99
100mod private {
101    pub struct ProxyApi;
102
103    #[allow(dead_code)]
104    pub struct EventHandler<H>(pub(super) H);
105
106    #[allow(dead_code)]
107    pub struct NoOpEventHandler;
108}
109
110impl Debug for WlDataDeviceManager {
111    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
112        write!(f, "wl_data_device_manager#{}", self.proxy.id())
113    }
114}
115
116impl Debug for WlDataDeviceManagerRef {
117    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
118        write!(f, "wl_data_device_manager#{}", self.proxy.id())
119    }
120}
121
122impl PartialEq<WlDataDeviceManagerRef> for WlDataDeviceManager {
123    fn eq(&self, other: &WlDataDeviceManagerRef) -> bool {
124        self.proxy == other.proxy
125    }
126}
127
128impl PartialEq<WlDataDeviceManager> for WlDataDeviceManagerRef {
129    fn eq(&self, other: &WlDataDeviceManager) -> bool {
130        self.proxy == other.proxy
131    }
132}
133
134#[allow(dead_code)]
135impl WlDataDeviceManager {
136    /// Since when the create_data_source request is available.
137    #[allow(dead_code)]
138    pub const REQ__CREATE_DATA_SOURCE__SINCE: u32 = 1;
139
140    /// create a new data source
141    ///
142    /// Create a new data source.
143    #[inline]
144    pub fn create_data_source(&self) -> WlDataSource {
145        let mut args = [wl_argument { n: 0 }];
146        // SAFETY: - self.proxy has the interface INTERFACE
147        //         - 0 < INTERFACE.method_count = 2
148        //         - the request signature is `n`
149        //         - OwnedProxy::WL_INTERFACE is always a valid interface
150        let data = unsafe {
151            self.proxy
152                .send_constructor::<false>(0, &mut args, WlDataSource::WL_INTERFACE, None)
153        };
154        // SAFETY: data has the interface WlDataSource::WL_INTERFACE
155        unsafe { proxy::low_level::from_untyped_owned(data) }
156    }
157
158    /// Since when the get_data_device request is available.
159    #[allow(dead_code)]
160    pub const REQ__GET_DATA_DEVICE__SINCE: u32 = 1;
161
162    /// create a new data device
163    ///
164    /// Create a new data device for a given seat.
165    ///
166    /// # Arguments
167    ///
168    /// - `seat`: seat associated with the data device
169    #[inline]
170    pub fn get_data_device(&self, seat: &WlSeatRef) -> WlDataDevice {
171        let (arg1,) = (seat,);
172        let obj1_lock = proxy::lock(arg1);
173        let obj1 = check_argument_proxy("seat", obj1_lock.wl_proxy());
174        let mut args = [wl_argument { n: 0 }, wl_argument { o: obj1 }];
175        // SAFETY: - self.proxy has the interface INTERFACE
176        //         - 1 < INTERFACE.method_count = 2
177        //         - the request signature is `no`
178        //         - OwnedProxy::WL_INTERFACE is always a valid interface
179        let data = unsafe {
180            self.proxy
181                .send_constructor::<false>(1, &mut args, WlDataDevice::WL_INTERFACE, None)
182        };
183        // SAFETY: data has the interface WlDataDevice::WL_INTERFACE
184        unsafe { proxy::low_level::from_untyped_owned(data) }
185    }
186}
187
188#[allow(dead_code)]
189impl WlDataDeviceManagerRef {
190    /// create a new data source
191    ///
192    /// Create a new data source.
193    ///
194    /// # Arguments
195    ///
196    /// - `_queue`: The queue that the returned proxy is assigned to.
197    #[inline]
198    pub fn create_data_source(&self, _queue: &Queue) -> WlDataSource {
199        let mut args = [wl_argument { n: 0 }];
200        // SAFETY: - self.proxy has the interface INTERFACE
201        //         - 0 < INTERFACE.method_count = 2
202        //         - the request signature is `n`
203        //         - OwnedProxy::WL_INTERFACE is always a valid interface
204        let data = unsafe {
205            self.proxy
206                .send_constructor(_queue, 0, &mut args, WlDataSource::WL_INTERFACE, None)
207        };
208        // SAFETY: data has the interface WlDataSource::WL_INTERFACE
209        unsafe { proxy::low_level::from_untyped_owned(data) }
210    }
211
212    /// create a new data device
213    ///
214    /// Create a new data device for a given seat.
215    ///
216    /// # Arguments
217    ///
218    /// - `_queue`: The queue that the returned proxy is assigned to.
219    /// - `seat`: seat associated with the data device
220    #[inline]
221    pub fn get_data_device(&self, _queue: &Queue, seat: &WlSeatRef) -> WlDataDevice {
222        let (arg1,) = (seat,);
223        let obj1_lock = proxy::lock(arg1);
224        let obj1 = check_argument_proxy("seat", obj1_lock.wl_proxy());
225        let mut args = [wl_argument { n: 0 }, wl_argument { o: obj1 }];
226        // SAFETY: - self.proxy has the interface INTERFACE
227        //         - 1 < INTERFACE.method_count = 2
228        //         - the request signature is `no`
229        //         - OwnedProxy::WL_INTERFACE is always a valid interface
230        let data = unsafe {
231            self.proxy
232                .send_constructor(_queue, 1, &mut args, WlDataDevice::WL_INTERFACE, None)
233        };
234        // SAFETY: data has the interface WlDataDevice::WL_INTERFACE
235        unsafe { proxy::low_level::from_untyped_owned(data) }
236    }
237}
238
239/// An event handler for [WlDataDeviceManager] proxies.
240#[allow(dead_code)]
241pub trait WlDataDeviceManagerEventHandler {
242    type Data: 'static;
243}
244
245impl WlDataDeviceManagerEventHandler for private::NoOpEventHandler {
246    type Data = ();
247}
248
249// SAFETY: - INTERFACE is a valid wl_interface
250//         - mutable_type always returns the same value
251unsafe impl<H> EventHandler for private::EventHandler<H>
252where
253    H: WlDataDeviceManagerEventHandler,
254{
255    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
256
257    #[inline]
258    fn mutable_type() -> Option<(TypeId, &'static str)> {
259        let id = TypeId::of::<H::Data>();
260        let name = std::any::type_name::<H::Data>();
261        Some((id, name))
262    }
263
264    #[allow(unused_variables)]
265    unsafe fn handle_event(
266        &self,
267        queue: &Queue,
268        data: *mut u8,
269        slf: &UntypedBorrowedProxy,
270        opcode: u32,
271        args: *mut wl_argument,
272    ) {
273        invalid_opcode("wl_data_device_manager", opcode);
274    }
275}
276
277impl<H> CreateEventHandler<H> for private::ProxyApi
278where
279    H: WlDataDeviceManagerEventHandler,
280{
281    type EventHandler = private::EventHandler<H>;
282
283    #[inline]
284    fn create_event_handler(handler: H) -> Self::EventHandler {
285        private::EventHandler(handler)
286    }
287}
288
289impl WlDataDeviceManager {
290    /// Since when the dnd_action.none enum variant is available.
291    #[allow(dead_code)]
292    pub const ENM__DND_ACTION_NONE__SINCE: u32 = 1;
293    /// Since when the dnd_action.copy enum variant is available.
294    #[allow(dead_code)]
295    pub const ENM__DND_ACTION_COPY__SINCE: u32 = 1;
296    /// Since when the dnd_action.move enum variant is available.
297    #[allow(dead_code)]
298    pub const ENM__DND_ACTION_MOVE__SINCE: u32 = 1;
299    /// Since when the dnd_action.ask enum variant is available.
300    #[allow(dead_code)]
301    pub const ENM__DND_ACTION_ASK__SINCE: u32 = 1;
302}
303
304/// drag and drop actions
305///
306/// This is a bitmask of the available/preferred actions in a
307/// drag-and-drop operation.
308///
309/// In the compositor, the selected action is a result of matching the
310/// actions offered by the source and destination sides.  "action" events
311/// with a "none" action will be sent to both source and destination if
312/// there is no match. All further checks will effectively happen on
313/// (source actions ∩ destination actions).
314///
315/// In addition, compositors may also pick different actions in
316/// reaction to key modifiers being pressed. One common design that
317/// is used in major toolkits (and the behavior recommended for
318/// compositors) is:
319///
320/// - If no modifiers are pressed, the first match (in bit order)
321///   will be used.
322/// - Pressing Shift selects "move", if enabled in the mask.
323/// - Pressing Control selects "copy", if enabled in the mask.
324///
325/// Behavior beyond that is considered implementation-dependent.
326/// Compositors may for example bind other modifiers (like Alt/Meta)
327/// or drags initiated with other buttons than BTN_LEFT to specific
328/// actions (e.g. "ask").
329#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
330#[allow(dead_code)]
331pub struct WlDataDeviceManagerDndAction(pub u32);
332
333/// An iterator over the set bits in a [WlDataDeviceManagerDndAction].
334///
335/// You can construct this with the `IntoIterator` implementation of `WlDataDeviceManagerDndAction`.
336#[derive(Clone, Debug)]
337pub struct WlDataDeviceManagerDndActionIter(pub u32);
338
339impl WlDataDeviceManagerDndAction {
340    /// no action
341    #[allow(dead_code)]
342    pub const NONE: Self = Self(0);
343
344    /// copy action
345    #[allow(dead_code)]
346    pub const COPY: Self = Self(1);
347
348    /// move action
349    #[allow(dead_code)]
350    pub const MOVE: Self = Self(2);
351
352    /// ask action
353    #[allow(dead_code)]
354    pub const ASK: Self = Self(4);
355}
356
357#[allow(dead_code)]
358impl WlDataDeviceManagerDndAction {
359    #[inline]
360    pub const fn empty() -> Self {
361        Self(0)
362    }
363
364    #[inline]
365    #[must_use]
366    pub const fn is_empty(self) -> bool {
367        self.0 == 0
368    }
369
370    #[inline]
371    #[must_use]
372    pub const fn contains(self, other: Self) -> bool {
373        self.0 & other.0 == other.0
374    }
375
376    #[inline]
377    #[must_use]
378    pub const fn intersects(self, other: Self) -> bool {
379        self.0 & other.0 != 0
380    }
381
382    #[inline]
383    pub const fn insert(&mut self, other: Self) {
384        *self = self.union(other);
385    }
386
387    #[inline]
388    pub const fn remove(&mut self, other: Self) {
389        *self = self.difference(other);
390    }
391
392    #[inline]
393    pub const fn toggle(&mut self, other: Self) {
394        *self = self.symmetric_difference(other);
395    }
396
397    #[inline]
398    pub const fn set(&mut self, other: Self, value: bool) {
399        if value {
400            self.insert(other);
401        } else {
402            self.remove(other);
403        }
404    }
405
406    #[inline]
407    #[must_use]
408    pub const fn intersection(self, other: Self) -> Self {
409        Self(self.0 & other.0)
410    }
411
412    #[inline]
413    #[must_use]
414    pub const fn union(self, other: Self) -> Self {
415        Self(self.0 | other.0)
416    }
417
418    #[inline]
419    #[must_use]
420    pub const fn difference(self, other: Self) -> Self {
421        Self(self.0 & !other.0)
422    }
423
424    #[inline]
425    #[must_use]
426    pub const fn complement(self) -> Self {
427        Self(!self.0)
428    }
429
430    #[inline]
431    #[must_use]
432    pub const fn symmetric_difference(self, other: Self) -> Self {
433        Self(self.0 ^ other.0)
434    }
435
436    #[inline]
437    pub const fn all_known() -> Self {
438        #[allow(clippy::eq_op, clippy::identity_op)]
439        Self(0 | 0 | 1 | 2 | 4)
440    }
441}
442
443impl Iterator for WlDataDeviceManagerDndActionIter {
444    type Item = WlDataDeviceManagerDndAction;
445
446    fn next(&mut self) -> Option<Self::Item> {
447        if self.0 == 0 {
448            return None;
449        }
450        let bit = 1 << self.0.trailing_zeros();
451        self.0 &= !bit;
452        Some(WlDataDeviceManagerDndAction(bit))
453    }
454}
455
456impl IntoIterator for WlDataDeviceManagerDndAction {
457    type Item = WlDataDeviceManagerDndAction;
458    type IntoIter = WlDataDeviceManagerDndActionIter;
459
460    fn into_iter(self) -> Self::IntoIter {
461        WlDataDeviceManagerDndActionIter(self.0)
462    }
463}
464
465impl BitAnd for WlDataDeviceManagerDndAction {
466    type Output = Self;
467
468    fn bitand(self, rhs: Self) -> Self::Output {
469        self.intersection(rhs)
470    }
471}
472
473impl BitAndAssign for WlDataDeviceManagerDndAction {
474    fn bitand_assign(&mut self, rhs: Self) {
475        *self = self.intersection(rhs);
476    }
477}
478
479impl BitOr for WlDataDeviceManagerDndAction {
480    type Output = Self;
481
482    fn bitor(self, rhs: Self) -> Self::Output {
483        self.union(rhs)
484    }
485}
486
487impl BitOrAssign for WlDataDeviceManagerDndAction {
488    fn bitor_assign(&mut self, rhs: Self) {
489        *self = self.union(rhs);
490    }
491}
492
493impl BitXor for WlDataDeviceManagerDndAction {
494    type Output = Self;
495
496    fn bitxor(self, rhs: Self) -> Self::Output {
497        self.symmetric_difference(rhs)
498    }
499}
500
501impl BitXorAssign for WlDataDeviceManagerDndAction {
502    fn bitxor_assign(&mut self, rhs: Self) {
503        *self = self.symmetric_difference(rhs);
504    }
505}
506
507impl Sub for WlDataDeviceManagerDndAction {
508    type Output = Self;
509
510    fn sub(self, rhs: Self) -> Self::Output {
511        self.difference(rhs)
512    }
513}
514
515impl SubAssign for WlDataDeviceManagerDndAction {
516    fn sub_assign(&mut self, rhs: Self) {
517        *self = self.difference(rhs);
518    }
519}
520
521impl Not for WlDataDeviceManagerDndAction {
522    type Output = Self;
523
524    fn not(self) -> Self::Output {
525        self.complement()
526    }
527}
528
529impl Debug for WlDataDeviceManagerDndAction {
530    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
531        let mut v = self.0;
532        let mut first = true;
533        if v & 1 == 1 {
534            v &= !1;
535            if first {
536                first = false;
537            } else {
538                f.write_str(" | ")?;
539            }
540            f.write_str("COPY")?;
541        }
542        if v & 2 == 2 {
543            v &= !2;
544            if first {
545                first = false;
546            } else {
547                f.write_str(" | ")?;
548            }
549            f.write_str("MOVE")?;
550        }
551        if v & 4 == 4 {
552            v &= !4;
553            if first {
554                first = false;
555            } else {
556                f.write_str(" | ")?;
557            }
558            f.write_str("ASK")?;
559        }
560        if v != 0 {
561            if first {
562                first = false;
563            } else {
564                f.write_str(" | ")?;
565            }
566            write!(f, "0x{v:032x}")?;
567        }
568        if first {
569            f.write_str("NONE")?;
570        }
571        Ok(())
572    }
573}
574
575/// Functional event handlers.
576pub mod event_handlers {
577    use super::*;
578
579    impl WlDataDeviceManager {}
580}