Skip to main content

get_registry/common/protocols_data/wayland/
wl_output.rs

1//! compositor output region
2//!
3//! An output describes part of the compositor geometry.  The
4//! compositor works in the 'compositor coordinate system' and an
5//! output corresponds to a rectangular area in that space that is
6//! actually visible.  This typically corresponds to a monitor that
7//! displays part of the compositor space.  This object is published
8//! as global during start up, or when a monitor is hotplugged.
9
10use super::super::all_types::*;
11use ::wl_client::builder::prelude::*;
12
13static INTERFACE: wl_interface = wl_interface {
14    name: c"wl_output".as_ptr(),
15    version: 4,
16    method_count: 1,
17    methods: {
18        static MESSAGES: [wl_message; 1] = [wl_message {
19            name: c"release".as_ptr(),
20            signature: c"".as_ptr(),
21            types: {
22                static TYPES: [Option<&'static wl_interface>; 0] = [];
23                TYPES.as_ptr().cast()
24            },
25        }];
26        MESSAGES.as_ptr()
27    },
28    event_count: 6,
29    events: {
30        static MESSAGES: [wl_message; 6] = [
31            wl_message {
32                name: c"geometry".as_ptr(),
33                signature: c"iiiiissi".as_ptr(),
34                types: {
35                    static TYPES: [Option<&'static wl_interface>; 8] =
36                        [None, None, None, None, None, None, None, None];
37                    TYPES.as_ptr().cast()
38                },
39            },
40            wl_message {
41                name: c"mode".as_ptr(),
42                signature: c"uiii".as_ptr(),
43                types: {
44                    static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
45                    TYPES.as_ptr().cast()
46                },
47            },
48            wl_message {
49                name: c"done".as_ptr(),
50                signature: c"".as_ptr(),
51                types: {
52                    static TYPES: [Option<&'static wl_interface>; 0] = [];
53                    TYPES.as_ptr().cast()
54                },
55            },
56            wl_message {
57                name: c"scale".as_ptr(),
58                signature: c"i".as_ptr(),
59                types: {
60                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
61                    TYPES.as_ptr().cast()
62                },
63            },
64            wl_message {
65                name: c"name".as_ptr(),
66                signature: c"s".as_ptr(),
67                types: {
68                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
69                    TYPES.as_ptr().cast()
70                },
71            },
72            wl_message {
73                name: c"description".as_ptr(),
74                signature: c"s".as_ptr(),
75                types: {
76                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
77                    TYPES.as_ptr().cast()
78                },
79            },
80        ];
81        MESSAGES.as_ptr()
82    },
83};
84
85/// An owned wl_output proxy.
86///
87/// See the documentation of [the module][self] for the interface description.
88#[derive(Clone, Eq, PartialEq)]
89#[repr(transparent)]
90pub struct WlOutput {
91    /// This proxy has the interface INTERFACE.
92    proxy: UntypedOwnedProxy,
93}
94
95/// A borrowed wl_output proxy.
96///
97/// See the documentation of [the module][self] for the interface description.
98#[derive(Eq, PartialEq)]
99#[repr(transparent)]
100pub struct WlOutputRef {
101    /// This proxy has the interface INTERFACE.
102    proxy: UntypedBorrowedProxy,
103}
104
105// SAFETY: WlOutput is a transparent wrapper around UntypedOwnedProxy
106unsafe impl UntypedOwnedProxyWrapper for WlOutput {}
107
108// SAFETY: - INTERFACE is a valid wl_interface
109//         - The only invariant is that self.proxy has a compatible interface
110unsafe impl OwnedProxy for WlOutput {
111    const INTERFACE: &'static str = "wl_output";
112    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
113    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
114        private::EventHandler(private::NoOpEventHandler);
115    const MAX_VERSION: u32 = 4;
116
117    type Borrowed = WlOutputRef;
118    type Api = private::ProxyApi;
119    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
120}
121
122// SAFETY: WlOutputRef is a transparent wrapper around UntypedBorrowedProxy
123unsafe impl UntypedBorrowedProxyWrapper for WlOutputRef {}
124
125// SAFETY: - The only invariant is that self.proxy has a compatible interface
126unsafe impl BorrowedProxy for WlOutputRef {
127    type Owned = WlOutput;
128}
129
130impl Deref for WlOutput {
131    type Target = WlOutputRef;
132
133    fn deref(&self) -> &Self::Target {
134        proxy::low_level::deref(self)
135    }
136}
137
138mod private {
139    pub struct ProxyApi;
140
141    #[allow(dead_code)]
142    pub struct EventHandler<H>(pub(super) H);
143
144    #[allow(dead_code)]
145    pub struct NoOpEventHandler;
146}
147
148impl Debug for WlOutput {
149    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
150        write!(f, "wl_output#{}", self.proxy.id())
151    }
152}
153
154impl Debug for WlOutputRef {
155    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
156        write!(f, "wl_output#{}", self.proxy.id())
157    }
158}
159
160impl PartialEq<WlOutputRef> for WlOutput {
161    fn eq(&self, other: &WlOutputRef) -> bool {
162        self.proxy == other.proxy
163    }
164}
165
166impl PartialEq<WlOutput> for WlOutputRef {
167    fn eq(&self, other: &WlOutput) -> bool {
168        self.proxy == other.proxy
169    }
170}
171
172#[allow(dead_code)]
173impl WlOutput {
174    /// Since when the release request is available.
175    #[allow(dead_code)]
176    pub const REQ__RELEASE__SINCE: u32 = 3;
177}
178
179#[allow(dead_code)]
180impl WlOutput {
181    /// release the output object
182    ///
183    /// Using this request a client can tell the server that it is not going to
184    /// use the output object anymore.
185    #[inline]
186    pub fn release(&self) {
187        let mut args = [];
188        // SAFETY: - self.proxy has the interface INTERFACE
189        //         - 0 < INTERFACE.method_count = 1
190        //         - the request signature is ``
191        unsafe {
192            self.proxy.send_destructor(0, &mut args);
193        }
194    }
195}
196
197impl WlOutput {
198    /// Since when the geometry event is available.
199    #[allow(dead_code)]
200    pub const EVT__GEOMETRY__SINCE: u32 = 1;
201
202    /// Since when the mode event is available.
203    #[allow(dead_code)]
204    pub const EVT__MODE__SINCE: u32 = 1;
205
206    /// Since when the done event is available.
207    #[allow(dead_code)]
208    pub const EVT__DONE__SINCE: u32 = 2;
209
210    /// Since when the scale event is available.
211    #[allow(dead_code)]
212    pub const EVT__SCALE__SINCE: u32 = 2;
213
214    /// Since when the name event is available.
215    #[allow(dead_code)]
216    pub const EVT__NAME__SINCE: u32 = 4;
217
218    /// Since when the description event is available.
219    #[allow(dead_code)]
220    pub const EVT__DESCRIPTION__SINCE: u32 = 4;
221}
222
223/// An event handler for [WlOutput] proxies.
224#[allow(dead_code)]
225pub trait WlOutputEventHandler {
226    type Data: 'static;
227
228    /// properties of the output
229    ///
230    /// The geometry event describes geometric properties of the output.
231    /// The event is sent when binding to the output object and whenever
232    /// any of the properties change.
233    ///
234    /// The physical size can be set to zero if it doesn't make sense for this
235    /// output (e.g. for projectors or virtual outputs).
236    ///
237    /// The geometry event will be followed by a done event (starting from
238    /// version 2).
239    ///
240    /// Clients should use wl_surface.preferred_buffer_transform instead of the
241    /// transform advertised by this event to find the preferred buffer
242    /// transform to use for a surface.
243    ///
244    /// Note: wl_output only advertises partial information about the output
245    /// position and identification. Some compositors, for instance those not
246    /// implementing a desktop-style output layout or those exposing virtual
247    /// outputs, might fake this information. Instead of using x and y, clients
248    /// should use xdg_output.logical_position. Instead of using make and model,
249    /// clients should use name and description.
250    ///
251    /// # Arguments
252    ///
253    /// - `x`: x position within the global compositor space
254    /// - `y`: y position within the global compositor space
255    /// - `physical_width`: width in millimeters of the output
256    /// - `physical_height`: height in millimeters of the output
257    /// - `subpixel`: subpixel orientation of the output
258    /// - `make`: textual description of the manufacturer
259    /// - `model`: textual description of the model
260    /// - `transform`: additional transformation applied to buffer contents during presentation
261    #[inline]
262    fn geometry(
263        &self,
264        _data: &mut Self::Data,
265        _slf: &WlOutputRef,
266        x: i32,
267        y: i32,
268        physical_width: i32,
269        physical_height: i32,
270        subpixel: WlOutputSubpixel,
271        make: &str,
272        model: &str,
273        transform: WlOutputTransform,
274    ) {
275        let _ = x;
276        let _ = y;
277        let _ = physical_width;
278        let _ = physical_height;
279        let _ = subpixel;
280        let _ = make;
281        let _ = model;
282        let _ = transform;
283    }
284
285    /// advertise available modes for the output
286    ///
287    /// The mode event describes an available mode for the output.
288    ///
289    /// The event is sent when binding to the output object and there
290    /// will always be one mode, the current mode.  The event is sent
291    /// again if an output changes mode, for the mode that is now
292    /// current.  In other words, the current mode is always the last
293    /// mode that was received with the current flag set.
294    ///
295    /// Non-current modes are deprecated. A compositor can decide to only
296    /// advertise the current mode and never send other modes. Clients
297    /// should not rely on non-current modes.
298    ///
299    /// The size of a mode is given in physical hardware units of
300    /// the output device. This is not necessarily the same as
301    /// the output size in the global compositor space. For instance,
302    /// the output may be scaled, as described in wl_output.scale,
303    /// or transformed, as described in wl_output.transform. Clients
304    /// willing to retrieve the output size in the global compositor
305    /// space should use xdg_output.logical_size instead.
306    ///
307    /// The vertical refresh rate can be set to zero if it doesn't make
308    /// sense for this output (e.g. for virtual outputs).
309    ///
310    /// The mode event will be followed by a done event (starting from
311    /// version 2).
312    ///
313    /// Clients should not use the refresh rate to schedule frames. Instead,
314    /// they should use the wl_surface.frame event or the presentation-time
315    /// protocol.
316    ///
317    /// Note: this information is not always meaningful for all outputs. Some
318    /// compositors, such as those exposing virtual outputs, might fake the
319    /// refresh rate or the size.
320    ///
321    /// # Arguments
322    ///
323    /// - `flags`: bitfield of mode flags
324    /// - `width`: width of the mode in hardware units
325    /// - `height`: height of the mode in hardware units
326    /// - `refresh`: vertical refresh rate in mHz
327    #[inline]
328    fn mode(
329        &self,
330        _data: &mut Self::Data,
331        _slf: &WlOutputRef,
332        flags: WlOutputMode,
333        width: i32,
334        height: i32,
335        refresh: i32,
336    ) {
337        let _ = flags;
338        let _ = width;
339        let _ = height;
340        let _ = refresh;
341    }
342
343    /// sent all information about output
344    ///
345    /// This event is sent after all other properties have been
346    /// sent after binding to the output object and after any
347    /// other property changes done after that. This allows
348    /// changes to the output properties to be seen as
349    /// atomic, even if they happen via multiple events.
350    #[inline]
351    fn done(&self, _data: &mut Self::Data, _slf: &WlOutputRef) {}
352
353    /// output scaling properties
354    ///
355    /// This event contains scaling geometry information
356    /// that is not in the geometry event. It may be sent after
357    /// binding the output object or if the output scale changes
358    /// later. The compositor will emit a non-zero, positive
359    /// value for scale. If it is not sent, the client should
360    /// assume a scale of 1.
361    ///
362    /// A scale larger than 1 means that the compositor will
363    /// automatically scale surface buffers by this amount
364    /// when rendering. This is used for very high resolution
365    /// displays where applications rendering at the native
366    /// resolution would be too small to be legible.
367    ///
368    /// Clients should use wl_surface.preferred_buffer_scale
369    /// instead of this event to find the preferred buffer
370    /// scale to use for a surface.
371    ///
372    /// The scale event will be followed by a done event.
373    ///
374    /// # Arguments
375    ///
376    /// - `factor`: scaling factor of output
377    #[inline]
378    fn scale(&self, _data: &mut Self::Data, _slf: &WlOutputRef, factor: i32) {
379        let _ = factor;
380    }
381
382    /// name of this output
383    ///
384    /// Many compositors will assign user-friendly names to their outputs, show
385    /// them to the user, allow the user to refer to an output, etc. The client
386    /// may wish to know this name as well to offer the user similar behaviors.
387    ///
388    /// The name is a UTF-8 string with no convention defined for its contents.
389    /// Each name is unique among all wl_output globals. The name is only
390    /// guaranteed to be unique for the compositor instance.
391    ///
392    /// The same output name is used for all clients for a given wl_output
393    /// global. Thus, the name can be shared across processes to refer to a
394    /// specific wl_output global.
395    ///
396    /// The name is not guaranteed to be persistent across sessions, thus cannot
397    /// be used to reliably identify an output in e.g. configuration files.
398    ///
399    /// Examples of names include 'HDMI-A-1', 'WL-1', 'X11-1', etc. However, do
400    /// not assume that the name is a reflection of an underlying DRM connector,
401    /// X11 connection, etc.
402    ///
403    /// The name event is sent after binding the output object. This event is
404    /// only sent once per output object, and the name does not change over the
405    /// lifetime of the wl_output global.
406    ///
407    /// Compositors may re-use the same output name if the wl_output global is
408    /// destroyed and re-created later. Compositors should avoid re-using the
409    /// same name if possible.
410    ///
411    /// The name event will be followed by a done event.
412    ///
413    /// # Arguments
414    ///
415    /// - `name`: output name
416    #[inline]
417    fn name(&self, _data: &mut Self::Data, _slf: &WlOutputRef, name: &str) {
418        let _ = name;
419    }
420
421    /// human-readable description of this output
422    ///
423    /// Many compositors can produce human-readable descriptions of their
424    /// outputs. The client may wish to know this description as well, e.g. for
425    /// output selection purposes.
426    ///
427    /// The description is a UTF-8 string with no convention defined for its
428    /// contents. The description is not guaranteed to be unique among all
429    /// wl_output globals. Examples might include 'Foocorp 11" Display' or
430    /// 'Virtual X11 output via :1'.
431    ///
432    /// The description event is sent after binding the output object and
433    /// whenever the description changes. The description is optional, and may
434    /// not be sent at all.
435    ///
436    /// The description event will be followed by a done event.
437    ///
438    /// # Arguments
439    ///
440    /// - `description`: output description
441    #[inline]
442    fn description(&self, _data: &mut Self::Data, _slf: &WlOutputRef, description: &str) {
443        let _ = description;
444    }
445}
446
447impl WlOutputEventHandler for private::NoOpEventHandler {
448    type Data = ();
449}
450
451// SAFETY: - INTERFACE is a valid wl_interface
452//         - mutable_type always returns the same value
453unsafe impl<H> EventHandler for private::EventHandler<H>
454where
455    H: WlOutputEventHandler,
456{
457    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
458
459    #[inline]
460    fn mutable_type() -> Option<(TypeId, &'static str)> {
461        let id = TypeId::of::<H::Data>();
462        let name = std::any::type_name::<H::Data>();
463        Some((id, name))
464    }
465
466    #[allow(unused_variables)]
467    unsafe fn handle_event(
468        &self,
469        queue: &Queue,
470        data: *mut u8,
471        slf: &UntypedBorrowedProxy,
472        opcode: u32,
473        args: *mut wl_argument,
474    ) {
475        // SAFETY: This function requires that slf has the interface INTERFACE
476        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlOutputRef>(slf) };
477        // SAFETY: This function requires that data is `&mut T` where `T`
478        //         has the type id returned by `Self::mutable_type`, i.e.,
479        //         `T = H::Data`.
480        let data: &mut H::Data = unsafe { &mut *data.cast() };
481        match opcode {
482            0 => {
483                // SAFETY: INTERFACE requires that there are 8 arguments
484                let args = unsafe { &*args.cast::<[wl_argument; 8]>() };
485                // SAFETY: - INTERFACE requires that args[0] contains an int
486                let arg0 = unsafe { args[0].i };
487                // SAFETY: - INTERFACE requires that args[1] contains an int
488                let arg1 = unsafe { args[1].i };
489                // SAFETY: - INTERFACE requires that args[2] contains an int
490                let arg2 = unsafe { args[2].i };
491                // SAFETY: - INTERFACE requires that args[3] contains an int
492                let arg3 = unsafe { args[3].i };
493                // SAFETY: - INTERFACE requires that args[4] contains an int
494                let arg4 = unsafe { WlOutputSubpixel(args[4].u) };
495                // SAFETY: - INTERFACE requires that args[5] contains a string
496                //         - if the pointer is not null, then it is a c string
497                let arg5 = unsafe { convert_string_arg("wl_output", "make", args[5].s) };
498                // SAFETY: - INTERFACE requires that args[6] contains a string
499                //         - if the pointer is not null, then it is a c string
500                let arg6 = unsafe { convert_string_arg("wl_output", "model", args[6].s) };
501                // SAFETY: - INTERFACE requires that args[7] contains an int
502                let arg7 = unsafe { WlOutputTransform(args[7].u) };
503                self.0
504                    .geometry(data, slf, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
505            }
506            1 => {
507                // SAFETY: INTERFACE requires that there are 4 arguments
508                let args = unsafe { &*args.cast::<[wl_argument; 4]>() };
509                // SAFETY: - INTERFACE requires that args[0] contains a uint
510                let arg0 = unsafe { WlOutputMode(args[0].u) };
511                // SAFETY: - INTERFACE requires that args[1] contains an int
512                let arg1 = unsafe { args[1].i };
513                // SAFETY: - INTERFACE requires that args[2] contains an int
514                let arg2 = unsafe { args[2].i };
515                // SAFETY: - INTERFACE requires that args[3] contains an int
516                let arg3 = unsafe { args[3].i };
517                self.0.mode(data, slf, arg0, arg1, arg2, arg3);
518            }
519            2 => {
520                self.0.done(data, slf);
521            }
522            3 => {
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 an int
526                let arg0 = unsafe { args[0].i };
527                self.0.scale(data, slf, arg0);
528            }
529            4 => {
530                // SAFETY: INTERFACE requires that there are 1 arguments
531                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
532                // SAFETY: - INTERFACE requires that args[0] contains a string
533                //         - if the pointer is not null, then it is a c string
534                let arg0 = unsafe { convert_string_arg("wl_output", "name", args[0].s) };
535                self.0.name(data, slf, arg0);
536            }
537            5 => {
538                // SAFETY: INTERFACE requires that there are 1 arguments
539                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
540                // SAFETY: - INTERFACE requires that args[0] contains a string
541                //         - if the pointer is not null, then it is a c string
542                let arg0 = unsafe { convert_string_arg("wl_output", "description", args[0].s) };
543                self.0.description(data, slf, arg0);
544            }
545            _ => {
546                invalid_opcode("wl_output", opcode);
547            }
548        }
549    }
550}
551
552impl<H> CreateEventHandler<H> for private::ProxyApi
553where
554    H: WlOutputEventHandler,
555{
556    type EventHandler = private::EventHandler<H>;
557
558    #[inline]
559    fn create_event_handler(handler: H) -> Self::EventHandler {
560        private::EventHandler(handler)
561    }
562}
563
564impl WlOutput {
565    /// Since when the subpixel.unknown enum variant is available.
566    #[allow(dead_code)]
567    pub const ENM__SUBPIXEL_UNKNOWN__SINCE: u32 = 1;
568    /// Since when the subpixel.none enum variant is available.
569    #[allow(dead_code)]
570    pub const ENM__SUBPIXEL_NONE__SINCE: u32 = 1;
571    /// Since when the subpixel.horizontal_rgb enum variant is available.
572    #[allow(dead_code)]
573    pub const ENM__SUBPIXEL_HORIZONTAL_RGB__SINCE: u32 = 1;
574    /// Since when the subpixel.horizontal_bgr enum variant is available.
575    #[allow(dead_code)]
576    pub const ENM__SUBPIXEL_HORIZONTAL_BGR__SINCE: u32 = 1;
577    /// Since when the subpixel.vertical_rgb enum variant is available.
578    #[allow(dead_code)]
579    pub const ENM__SUBPIXEL_VERTICAL_RGB__SINCE: u32 = 1;
580    /// Since when the subpixel.vertical_bgr enum variant is available.
581    #[allow(dead_code)]
582    pub const ENM__SUBPIXEL_VERTICAL_BGR__SINCE: u32 = 1;
583
584    /// Since when the transform.normal enum variant is available.
585    #[allow(dead_code)]
586    pub const ENM__TRANSFORM_NORMAL__SINCE: u32 = 1;
587    /// Since when the transform.90 enum variant is available.
588    #[allow(dead_code)]
589    pub const ENM__TRANSFORM_90__SINCE: u32 = 1;
590    /// Since when the transform.180 enum variant is available.
591    #[allow(dead_code)]
592    pub const ENM__TRANSFORM_180__SINCE: u32 = 1;
593    /// Since when the transform.270 enum variant is available.
594    #[allow(dead_code)]
595    pub const ENM__TRANSFORM_270__SINCE: u32 = 1;
596    /// Since when the transform.flipped enum variant is available.
597    #[allow(dead_code)]
598    pub const ENM__TRANSFORM_FLIPPED__SINCE: u32 = 1;
599    /// Since when the transform.flipped_90 enum variant is available.
600    #[allow(dead_code)]
601    pub const ENM__TRANSFORM_FLIPPED_90__SINCE: u32 = 1;
602    /// Since when the transform.flipped_180 enum variant is available.
603    #[allow(dead_code)]
604    pub const ENM__TRANSFORM_FLIPPED_180__SINCE: u32 = 1;
605    /// Since when the transform.flipped_270 enum variant is available.
606    #[allow(dead_code)]
607    pub const ENM__TRANSFORM_FLIPPED_270__SINCE: u32 = 1;
608
609    /// Since when the mode.current enum variant is available.
610    #[allow(dead_code)]
611    pub const ENM__MODE_CURRENT__SINCE: u32 = 1;
612    /// Since when the mode.preferred enum variant is available.
613    #[allow(dead_code)]
614    pub const ENM__MODE_PREFERRED__SINCE: u32 = 1;
615}
616
617/// subpixel geometry information
618///
619/// This enumeration describes how the physical
620/// pixels on an output are laid out.
621#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
622#[allow(dead_code)]
623pub struct WlOutputSubpixel(pub u32);
624
625impl WlOutputSubpixel {
626    /// unknown geometry
627    #[allow(dead_code)]
628    pub const UNKNOWN: Self = Self(0);
629
630    /// no geometry
631    #[allow(dead_code)]
632    pub const NONE: Self = Self(1);
633
634    /// horizontal RGB
635    #[allow(dead_code)]
636    pub const HORIZONTAL_RGB: Self = Self(2);
637
638    /// horizontal BGR
639    #[allow(dead_code)]
640    pub const HORIZONTAL_BGR: Self = Self(3);
641
642    /// vertical RGB
643    #[allow(dead_code)]
644    pub const VERTICAL_RGB: Self = Self(4);
645
646    /// vertical BGR
647    #[allow(dead_code)]
648    pub const VERTICAL_BGR: Self = Self(5);
649}
650
651impl Debug for WlOutputSubpixel {
652    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
653        let name = match *self {
654            Self::UNKNOWN => "UNKNOWN",
655            Self::NONE => "NONE",
656            Self::HORIZONTAL_RGB => "HORIZONTAL_RGB",
657            Self::HORIZONTAL_BGR => "HORIZONTAL_BGR",
658            Self::VERTICAL_RGB => "VERTICAL_RGB",
659            Self::VERTICAL_BGR => "VERTICAL_BGR",
660            _ => return Debug::fmt(&self.0, f),
661        };
662        f.write_str(name)
663    }
664}
665
666/// transformation applied to buffer contents
667///
668/// This describes transformations that clients and compositors apply to
669/// buffer contents.
670///
671/// The flipped values correspond to an initial flip around a
672/// vertical axis followed by rotation.
673///
674/// The purpose is mainly to allow clients to render accordingly and
675/// tell the compositor, so that for fullscreen surfaces, the
676/// compositor will still be able to scan out directly from client
677/// surfaces.
678#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
679#[allow(dead_code)]
680pub struct WlOutputTransform(pub u32);
681
682impl WlOutputTransform {
683    /// no transform
684    #[allow(dead_code)]
685    pub const NORMAL: Self = Self(0);
686
687    /// 90 degrees counter-clockwise
688    #[allow(dead_code)]
689    pub const _90: Self = Self(1);
690
691    /// 180 degrees counter-clockwise
692    #[allow(dead_code)]
693    pub const _180: Self = Self(2);
694
695    /// 270 degrees counter-clockwise
696    #[allow(dead_code)]
697    pub const _270: Self = Self(3);
698
699    /// 180 degree flip around a vertical axis
700    #[allow(dead_code)]
701    pub const FLIPPED: Self = Self(4);
702
703    /// flip and rotate 90 degrees counter-clockwise
704    #[allow(dead_code)]
705    pub const FLIPPED_90: Self = Self(5);
706
707    /// flip and rotate 180 degrees counter-clockwise
708    #[allow(dead_code)]
709    pub const FLIPPED_180: Self = Self(6);
710
711    /// flip and rotate 270 degrees counter-clockwise
712    #[allow(dead_code)]
713    pub const FLIPPED_270: Self = Self(7);
714}
715
716impl Debug for WlOutputTransform {
717    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
718        let name = match *self {
719            Self::NORMAL => "NORMAL",
720            Self::_90 => "_90",
721            Self::_180 => "_180",
722            Self::_270 => "_270",
723            Self::FLIPPED => "FLIPPED",
724            Self::FLIPPED_90 => "FLIPPED_90",
725            Self::FLIPPED_180 => "FLIPPED_180",
726            Self::FLIPPED_270 => "FLIPPED_270",
727            _ => return Debug::fmt(&self.0, f),
728        };
729        f.write_str(name)
730    }
731}
732
733/// mode information
734///
735/// These flags describe properties of an output mode.
736/// They are used in the flags bitfield of the mode event.
737#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
738#[allow(dead_code)]
739pub struct WlOutputMode(pub u32);
740
741/// An iterator over the set bits in a [WlOutputMode].
742///
743/// You can construct this with the `IntoIterator` implementation of `WlOutputMode`.
744#[derive(Clone, Debug)]
745#[allow(dead_code)]
746pub struct WlOutputModeIter(pub u32);
747
748impl WlOutputMode {
749    /// indicates this is the current mode
750    #[allow(dead_code)]
751    pub const CURRENT: Self = Self(0x1);
752
753    /// indicates this is the preferred mode
754    #[allow(dead_code)]
755    pub const PREFERRED: Self = Self(0x2);
756}
757
758#[allow(dead_code)]
759impl WlOutputMode {
760    #[inline]
761    pub const fn empty() -> Self {
762        Self(0)
763    }
764
765    #[inline]
766    #[must_use]
767    pub const fn is_empty(self) -> bool {
768        self.0 == 0
769    }
770
771    #[inline]
772    #[must_use]
773    pub const fn contains(self, other: Self) -> bool {
774        self.0 & other.0 == other.0
775    }
776
777    #[inline]
778    #[must_use]
779    pub const fn intersects(self, other: Self) -> bool {
780        self.0 & other.0 != 0
781    }
782
783    #[inline]
784    pub const fn insert(&mut self, other: Self) {
785        *self = self.union(other);
786    }
787
788    #[inline]
789    pub const fn remove(&mut self, other: Self) {
790        *self = self.difference(other);
791    }
792
793    #[inline]
794    pub const fn toggle(&mut self, other: Self) {
795        *self = self.symmetric_difference(other);
796    }
797
798    #[inline]
799    pub const fn set(&mut self, other: Self, value: bool) {
800        if value {
801            self.insert(other);
802        } else {
803            self.remove(other);
804        }
805    }
806
807    #[inline]
808    #[must_use]
809    pub const fn intersection(self, other: Self) -> Self {
810        Self(self.0 & other.0)
811    }
812
813    #[inline]
814    #[must_use]
815    pub const fn union(self, other: Self) -> Self {
816        Self(self.0 | other.0)
817    }
818
819    #[inline]
820    #[must_use]
821    pub const fn difference(self, other: Self) -> Self {
822        Self(self.0 & !other.0)
823    }
824
825    #[inline]
826    #[must_use]
827    pub const fn complement(self) -> Self {
828        Self(!self.0)
829    }
830
831    #[inline]
832    #[must_use]
833    pub const fn symmetric_difference(self, other: Self) -> Self {
834        Self(self.0 ^ other.0)
835    }
836
837    #[inline]
838    pub const fn all_known() -> Self {
839        #[allow(clippy::eq_op, clippy::identity_op)]
840        Self(0 | 0x1 | 0x2)
841    }
842}
843
844impl Iterator for WlOutputModeIter {
845    type Item = WlOutputMode;
846
847    fn next(&mut self) -> Option<Self::Item> {
848        if self.0 == 0 {
849            return None;
850        }
851        let bit = 1 << self.0.trailing_zeros();
852        self.0 &= !bit;
853        Some(WlOutputMode(bit))
854    }
855}
856
857impl IntoIterator for WlOutputMode {
858    type Item = WlOutputMode;
859    type IntoIter = WlOutputModeIter;
860
861    fn into_iter(self) -> Self::IntoIter {
862        WlOutputModeIter(self.0)
863    }
864}
865
866impl BitAnd for WlOutputMode {
867    type Output = Self;
868
869    fn bitand(self, rhs: Self) -> Self::Output {
870        self.intersection(rhs)
871    }
872}
873
874impl BitAndAssign for WlOutputMode {
875    fn bitand_assign(&mut self, rhs: Self) {
876        *self = self.intersection(rhs);
877    }
878}
879
880impl BitOr for WlOutputMode {
881    type Output = Self;
882
883    fn bitor(self, rhs: Self) -> Self::Output {
884        self.union(rhs)
885    }
886}
887
888impl BitOrAssign for WlOutputMode {
889    fn bitor_assign(&mut self, rhs: Self) {
890        *self = self.union(rhs);
891    }
892}
893
894impl BitXor for WlOutputMode {
895    type Output = Self;
896
897    fn bitxor(self, rhs: Self) -> Self::Output {
898        self.symmetric_difference(rhs)
899    }
900}
901
902impl BitXorAssign for WlOutputMode {
903    fn bitxor_assign(&mut self, rhs: Self) {
904        *self = self.symmetric_difference(rhs);
905    }
906}
907
908impl Sub for WlOutputMode {
909    type Output = Self;
910
911    fn sub(self, rhs: Self) -> Self::Output {
912        self.difference(rhs)
913    }
914}
915
916impl SubAssign for WlOutputMode {
917    fn sub_assign(&mut self, rhs: Self) {
918        *self = self.difference(rhs);
919    }
920}
921
922impl Not for WlOutputMode {
923    type Output = Self;
924
925    fn not(self) -> Self::Output {
926        self.complement()
927    }
928}
929
930impl Debug for WlOutputMode {
931    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
932        let mut v = self.0;
933        let mut first = true;
934        if v & 0x1 == 0x1 {
935            v &= !0x1;
936            if first {
937                first = false;
938            } else {
939                f.write_str(" | ")?;
940            }
941            f.write_str("CURRENT")?;
942        }
943        if v & 0x2 == 0x2 {
944            v &= !0x2;
945            if first {
946                first = false;
947            } else {
948                f.write_str(" | ")?;
949            }
950            f.write_str("PREFERRED")?;
951        }
952        if v != 0 {
953            if first {
954                first = false;
955            } else {
956                f.write_str(" | ")?;
957            }
958            write!(f, "0x{v:032x}")?;
959        }
960        if first {
961            f.write_str("0")?;
962        }
963        Ok(())
964    }
965}
966
967/// Functional event handlers.
968pub mod event_handlers {
969    use super::*;
970
971    /// Event handler for geometry events.
972    pub struct Geometry<T, F>(F, PhantomData<fn(&mut T)>);
973    impl<T, F> WlOutputEventHandler for Geometry<T, F>
974    where
975        T: 'static,
976        F: Fn(
977            &mut T,
978            &WlOutputRef,
979            i32,
980            i32,
981            i32,
982            i32,
983            WlOutputSubpixel,
984            &str,
985            &str,
986            WlOutputTransform,
987        ),
988    {
989        type Data = T;
990
991        #[inline]
992        fn geometry(
993            &self,
994            _data: &mut T,
995            _slf: &WlOutputRef,
996            x: i32,
997            y: i32,
998            physical_width: i32,
999            physical_height: i32,
1000            subpixel: WlOutputSubpixel,
1001            make: &str,
1002            model: &str,
1003            transform: WlOutputTransform,
1004        ) {
1005            self.0(
1006                _data,
1007                _slf,
1008                x,
1009                y,
1010                physical_width,
1011                physical_height,
1012                subpixel,
1013                make,
1014                model,
1015                transform,
1016            )
1017        }
1018    }
1019
1020    /// Event handler for mode events.
1021    pub struct Mode<T, F>(F, PhantomData<fn(&mut T)>);
1022    impl<T, F> WlOutputEventHandler for Mode<T, F>
1023    where
1024        T: 'static,
1025        F: Fn(&mut T, &WlOutputRef, WlOutputMode, i32, i32, i32),
1026    {
1027        type Data = T;
1028
1029        #[inline]
1030        fn mode(
1031            &self,
1032            _data: &mut T,
1033            _slf: &WlOutputRef,
1034            flags: WlOutputMode,
1035            width: i32,
1036            height: i32,
1037            refresh: i32,
1038        ) {
1039            self.0(_data, _slf, flags, width, height, refresh)
1040        }
1041    }
1042
1043    /// Event handler for done events.
1044    pub struct Done<T, F>(F, PhantomData<fn(&mut T)>);
1045    impl<T, F> WlOutputEventHandler for Done<T, F>
1046    where
1047        T: 'static,
1048        F: Fn(&mut T, &WlOutputRef),
1049    {
1050        type Data = T;
1051
1052        #[inline]
1053        fn done(&self, _data: &mut T, _slf: &WlOutputRef) {
1054            self.0(_data, _slf)
1055        }
1056    }
1057
1058    /// Event handler for scale events.
1059    pub struct Scale<T, F>(F, PhantomData<fn(&mut T)>);
1060    impl<T, F> WlOutputEventHandler for Scale<T, F>
1061    where
1062        T: 'static,
1063        F: Fn(&mut T, &WlOutputRef, i32),
1064    {
1065        type Data = T;
1066
1067        #[inline]
1068        fn scale(&self, _data: &mut T, _slf: &WlOutputRef, factor: i32) {
1069            self.0(_data, _slf, factor)
1070        }
1071    }
1072
1073    /// Event handler for name events.
1074    pub struct Name<T, F>(F, PhantomData<fn(&mut T)>);
1075    impl<T, F> WlOutputEventHandler for Name<T, F>
1076    where
1077        T: 'static,
1078        F: Fn(&mut T, &WlOutputRef, &str),
1079    {
1080        type Data = T;
1081
1082        #[inline]
1083        fn name(&self, _data: &mut T, _slf: &WlOutputRef, name: &str) {
1084            self.0(_data, _slf, name)
1085        }
1086    }
1087
1088    /// Event handler for description events.
1089    pub struct Description<T, F>(F, PhantomData<fn(&mut T)>);
1090    impl<T, F> WlOutputEventHandler for Description<T, F>
1091    where
1092        T: 'static,
1093        F: Fn(&mut T, &WlOutputRef, &str),
1094    {
1095        type Data = T;
1096
1097        #[inline]
1098        fn description(&self, _data: &mut T, _slf: &WlOutputRef, description: &str) {
1099            self.0(_data, _slf, description)
1100        }
1101    }
1102
1103    impl WlOutput {
1104        /// Creates an event handler for geometry events.
1105        ///
1106        /// The event handler ignores all other events.
1107        #[allow(dead_code)]
1108        pub fn on_geometry<T, F>(f: F) -> Geometry<T, F>
1109        where
1110            T: 'static,
1111            F: Fn(
1112                &mut T,
1113                &WlOutputRef,
1114                i32,
1115                i32,
1116                i32,
1117                i32,
1118                WlOutputSubpixel,
1119                &str,
1120                &str,
1121                WlOutputTransform,
1122            ),
1123        {
1124            Geometry(f, PhantomData)
1125        }
1126
1127        /// Creates an event handler for mode events.
1128        ///
1129        /// The event handler ignores all other events.
1130        #[allow(dead_code)]
1131        pub fn on_mode<T, F>(f: F) -> Mode<T, F>
1132        where
1133            T: 'static,
1134            F: Fn(&mut T, &WlOutputRef, WlOutputMode, i32, i32, i32),
1135        {
1136            Mode(f, PhantomData)
1137        }
1138
1139        /// Creates an event handler for done events.
1140        ///
1141        /// The event handler ignores all other events.
1142        #[allow(dead_code)]
1143        pub fn on_done<T, F>(f: F) -> Done<T, F>
1144        where
1145            T: 'static,
1146            F: Fn(&mut T, &WlOutputRef),
1147        {
1148            Done(f, PhantomData)
1149        }
1150
1151        /// Creates an event handler for scale events.
1152        ///
1153        /// The event handler ignores all other events.
1154        #[allow(dead_code)]
1155        pub fn on_scale<T, F>(f: F) -> Scale<T, F>
1156        where
1157            T: 'static,
1158            F: Fn(&mut T, &WlOutputRef, i32),
1159        {
1160            Scale(f, PhantomData)
1161        }
1162
1163        /// Creates an event handler for name events.
1164        ///
1165        /// The event handler ignores all other events.
1166        #[allow(dead_code)]
1167        pub fn on_name<T, F>(f: F) -> Name<T, F>
1168        where
1169            T: 'static,
1170            F: Fn(&mut T, &WlOutputRef, &str),
1171        {
1172            Name(f, PhantomData)
1173        }
1174
1175        /// Creates an event handler for description events.
1176        ///
1177        /// The event handler ignores all other events.
1178        #[allow(dead_code)]
1179        pub fn on_description<T, F>(f: F) -> Description<T, F>
1180        where
1181            T: 'static,
1182            F: Fn(&mut T, &WlOutputRef, &str),
1183        {
1184            Description(f, PhantomData)
1185        }
1186    }
1187}