Skip to main content

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