Skip to main content

get_registry/common/protocols/viewporter/
wp_viewport.rs

1//! crop and scale interface to a wl_surface
2//!
3//! An additional interface to a wl_surface object, which allows the
4//! client to specify the cropping and scaling of the surface
5//! contents.
6//!
7//! This interface works with two concepts: the source rectangle (src_x,
8//! src_y, src_width, src_height), and the destination size (dst_width,
9//! dst_height). The contents of the source rectangle are scaled to the
10//! destination size, and content outside the source rectangle is ignored.
11//! This state is double-buffered, see wl_surface.commit.
12//!
13//! The two parts of crop and scale state are independent: the source
14//! rectangle, and the destination size. Initially both are unset, that
15//! is, no scaling is applied. The whole of the current wl_buffer is
16//! used as the source, and the surface size is as defined in
17//! wl_surface.attach.
18//!
19//! If the destination size is set, it causes the surface size to become
20//! dst_width, dst_height. The source (rectangle) is scaled to exactly
21//! this size. This overrides whatever the attached wl_buffer size is,
22//! unless the wl_buffer is NULL. If the wl_buffer is NULL, the surface
23//! has no content and therefore no size. Otherwise, the size is always
24//! at least 1x1 in surface local coordinates.
25//!
26//! If the source rectangle is set, it defines what area of the wl_buffer is
27//! taken as the source. If the source rectangle is set and the destination
28//! size is not set, then src_width and src_height must be integers, and the
29//! surface size becomes the source rectangle size. This results in cropping
30//! without scaling. If src_width or src_height are not integers and
31//! destination size is not set, the bad_size protocol error is raised when
32//! the surface state is applied.
33//!
34//! The coordinate transformations from buffer pixel coordinates up to
35//! the surface-local coordinates happen in the following order:
36//!   1. buffer_transform (wl_surface.set_buffer_transform)
37//!   2. buffer_scale (wl_surface.set_buffer_scale)
38//!   3. crop and scale (wp_viewport.set*)
39//! This means, that the source rectangle coordinates of crop and scale
40//! are given in the coordinates after the buffer transform and scale,
41//! i.e. in the coordinates that would be the surface-local coordinates
42//! if the crop and scale was not applied.
43//!
44//! If src_x or src_y are negative, the bad_value protocol error is raised.
45//! Otherwise, if the source rectangle is partially or completely outside of
46//! the non-NULL wl_buffer, then the out_of_buffer protocol error is raised
47//! when the surface state is applied. A NULL wl_buffer does not raise the
48//! out_of_buffer error.
49//!
50//! If the wl_surface associated with the wp_viewport is destroyed,
51//! all wp_viewport requests except 'destroy' raise the protocol error
52//! no_surface.
53//!
54//! If the wp_viewport object is destroyed, the crop and scale
55//! state is removed from the wl_surface. The change will be applied
56//! on the next wl_surface.commit.
57
58use super::super::all_types::*;
59use ::wl_client::builder::prelude::*;
60
61static INTERFACE: wl_interface = wl_interface {
62    name: c"wp_viewport".as_ptr(),
63    version: 1,
64    method_count: 3,
65    methods: {
66        static MESSAGES: [wl_message; 3] = [
67            wl_message {
68                name: c"destroy".as_ptr(),
69                signature: c"".as_ptr(),
70                types: {
71                    static TYPES: [Option<&'static wl_interface>; 0] = [];
72                    TYPES.as_ptr().cast()
73                },
74            },
75            wl_message {
76                name: c"set_source".as_ptr(),
77                signature: c"ffff".as_ptr(),
78                types: {
79                    static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
80                    TYPES.as_ptr().cast()
81                },
82            },
83            wl_message {
84                name: c"set_destination".as_ptr(),
85                signature: c"ii".as_ptr(),
86                types: {
87                    static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
88                    TYPES.as_ptr().cast()
89                },
90            },
91        ];
92        MESSAGES.as_ptr()
93    },
94    event_count: 0,
95    events: ptr::null(),
96};
97
98/// An owned wp_viewport proxy.
99///
100/// See the documentation of [the module][self] for the interface description.
101#[derive(Clone, Eq, PartialEq)]
102#[repr(transparent)]
103pub struct WpViewport {
104    /// This proxy has the interface INTERFACE.
105    proxy: UntypedOwnedProxy,
106}
107
108/// A borrowed wp_viewport proxy.
109///
110/// See the documentation of [the module][self] for the interface description.
111#[derive(Eq, PartialEq)]
112#[repr(transparent)]
113pub struct WpViewportRef {
114    /// This proxy has the interface INTERFACE.
115    proxy: UntypedBorrowedProxy,
116}
117
118// SAFETY: WpViewport is a transparent wrapper around UntypedOwnedProxy
119unsafe impl UntypedOwnedProxyWrapper for WpViewport {}
120
121// SAFETY: - INTERFACE is a valid wl_interface
122//         - The only invariant is that self.proxy has a compatible interface
123unsafe impl OwnedProxy for WpViewport {
124    const INTERFACE: &'static str = "wp_viewport";
125    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
126    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
127        private::EventHandler(private::NoOpEventHandler);
128    const MAX_VERSION: u32 = 1;
129
130    type Borrowed = WpViewportRef;
131    type Api = private::ProxyApi;
132    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
133}
134
135// SAFETY: WpViewportRef is a transparent wrapper around UntypedBorrowedProxy
136unsafe impl UntypedBorrowedProxyWrapper for WpViewportRef {}
137
138// SAFETY: - The only invariant is that self.proxy has a compatible interface
139unsafe impl BorrowedProxy for WpViewportRef {
140    type Owned = WpViewport;
141}
142
143impl Deref for WpViewport {
144    type Target = WpViewportRef;
145
146    fn deref(&self) -> &Self::Target {
147        proxy::low_level::deref(self)
148    }
149}
150
151mod private {
152    pub struct ProxyApi;
153
154    #[allow(dead_code)]
155    pub struct EventHandler<H>(pub(super) H);
156
157    #[allow(dead_code)]
158    pub struct NoOpEventHandler;
159}
160
161impl Debug for WpViewport {
162    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
163        write!(f, "wp_viewport#{}", self.proxy.id())
164    }
165}
166
167impl Debug for WpViewportRef {
168    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
169        write!(f, "wp_viewport#{}", self.proxy.id())
170    }
171}
172
173impl PartialEq<WpViewportRef> for WpViewport {
174    fn eq(&self, other: &WpViewportRef) -> bool {
175        self.proxy == other.proxy
176    }
177}
178
179impl PartialEq<WpViewport> for WpViewportRef {
180    fn eq(&self, other: &WpViewport) -> bool {
181        self.proxy == other.proxy
182    }
183}
184
185#[allow(dead_code)]
186impl WpViewport {
187    /// Since when the destroy request is available.
188    #[allow(dead_code)]
189    pub const REQ__DESTROY__SINCE: u32 = 1;
190
191    /// Since when the set_source request is available.
192    #[allow(dead_code)]
193    pub const REQ__SET_SOURCE__SINCE: u32 = 1;
194
195    /// Since when the set_destination request is available.
196    #[allow(dead_code)]
197    pub const REQ__SET_DESTINATION__SINCE: u32 = 1;
198}
199
200#[allow(dead_code)]
201impl WpViewport {
202    /// remove scaling and cropping from the surface
203    ///
204    /// The associated wl_surface's crop and scale state is removed.
205    /// The change is applied on the next wl_surface.commit.
206    #[inline]
207    pub fn destroy(&self) {
208        let mut args = [];
209        // SAFETY: - self.proxy has the interface INTERFACE
210        //         - 0 < INTERFACE.method_count = 3
211        //         - the request signature is ``
212        unsafe {
213            self.proxy.send_destructor(0, &mut args);
214        }
215    }
216}
217
218#[allow(dead_code)]
219impl WpViewportRef {
220    /// set the source rectangle for cropping
221    ///
222    /// Set the source rectangle of the associated wl_surface. See
223    /// wp_viewport for the description, and relation to the wl_buffer
224    /// size.
225    ///
226    /// If all of x, y, width and height are -1.0, the source rectangle is
227    /// unset instead. Any other set of values where width or height are zero
228    /// or negative, or x or y are negative, raise the bad_value protocol
229    /// error.
230    ///
231    /// The crop and scale state is double-buffered, see wl_surface.commit.
232    ///
233    /// # Arguments
234    ///
235    /// - `x`: source rectangle x
236    /// - `y`: source rectangle y
237    /// - `width`: source rectangle width
238    /// - `height`: source rectangle height
239    #[inline]
240    pub fn set_source(&self, x: Fixed, y: Fixed, width: Fixed, height: Fixed) {
241        let (arg0, arg1, arg2, arg3) = (x, y, width, height);
242        let mut args = [
243            wl_argument { f: arg0.to_wire() },
244            wl_argument { f: arg1.to_wire() },
245            wl_argument { f: arg2.to_wire() },
246            wl_argument { f: arg3.to_wire() },
247        ];
248        // SAFETY: - self.proxy has the interface INTERFACE
249        //         - 1 < INTERFACE.method_count = 3
250        //         - the request signature is `ffff`
251        unsafe {
252            self.proxy.send_request(1, &mut args);
253        }
254    }
255
256    /// set the surface size for scaling
257    ///
258    /// Set the destination size of the associated wl_surface. See
259    /// wp_viewport for the description, and relation to the wl_buffer
260    /// size.
261    ///
262    /// If width is -1 and height is -1, the destination size is unset
263    /// instead. Any other pair of values for width and height that
264    /// contains zero or negative values raises the bad_value protocol
265    /// error.
266    ///
267    /// The crop and scale state is double-buffered, see wl_surface.commit.
268    ///
269    /// # Arguments
270    ///
271    /// - `width`: surface width
272    /// - `height`: surface height
273    #[inline]
274    pub fn set_destination(&self, width: i32, height: i32) {
275        let (arg0, arg1) = (width, height);
276        let mut args = [wl_argument { i: arg0 }, wl_argument { i: arg1 }];
277        // SAFETY: - self.proxy has the interface INTERFACE
278        //         - 2 < INTERFACE.method_count = 3
279        //         - the request signature is `ii`
280        unsafe {
281            self.proxy.send_request(2, &mut args);
282        }
283    }
284}
285
286/// An event handler for [WpViewport] proxies.
287#[allow(dead_code)]
288pub trait WpViewportEventHandler {}
289
290impl WpViewportEventHandler for private::NoOpEventHandler {}
291
292// SAFETY: - INTERFACE is a valid wl_interface
293unsafe impl<H> EventHandler for private::EventHandler<H>
294where
295    H: WpViewportEventHandler,
296{
297    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
298
299    #[allow(unused_variables)]
300    unsafe fn handle_event(
301        &self,
302        queue: &Queue,
303        data: *mut u8,
304        slf: &UntypedBorrowedProxy,
305        opcode: u32,
306        args: *mut wl_argument,
307    ) {
308        invalid_opcode("wp_viewport", opcode);
309    }
310}
311
312impl<H> CreateEventHandler<H> for private::ProxyApi
313where
314    H: WpViewportEventHandler,
315{
316    type EventHandler = private::EventHandler<H>;
317
318    #[inline]
319    fn create_event_handler(handler: H) -> Self::EventHandler {
320        private::EventHandler(handler)
321    }
322}
323
324impl WpViewport {
325    /// Since when the error.bad_value enum variant is available.
326    #[allow(dead_code)]
327    pub const ENM__ERROR_BAD_VALUE__SINCE: u32 = 1;
328    /// Since when the error.bad_size enum variant is available.
329    #[allow(dead_code)]
330    pub const ENM__ERROR_BAD_SIZE__SINCE: u32 = 1;
331    /// Since when the error.out_of_buffer enum variant is available.
332    #[allow(dead_code)]
333    pub const ENM__ERROR_OUT_OF_BUFFER__SINCE: u32 = 1;
334    /// Since when the error.no_surface enum variant is available.
335    #[allow(dead_code)]
336    pub const ENM__ERROR_NO_SURFACE__SINCE: u32 = 1;
337}
338
339#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
340#[allow(dead_code)]
341pub struct WpViewportError(pub u32);
342
343impl WpViewportError {
344    /// negative or zero values in width or height
345    #[allow(dead_code)]
346    pub const BAD_VALUE: Self = Self(0);
347
348    /// destination size is not integer
349    #[allow(dead_code)]
350    pub const BAD_SIZE: Self = Self(1);
351
352    /// source rectangle extends outside of the content area
353    #[allow(dead_code)]
354    pub const OUT_OF_BUFFER: Self = Self(2);
355
356    /// the wl_surface was destroyed
357    #[allow(dead_code)]
358    pub const NO_SURFACE: Self = Self(3);
359}
360
361impl Debug for WpViewportError {
362    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
363        let name = match *self {
364            Self::BAD_VALUE => "BAD_VALUE",
365            Self::BAD_SIZE => "BAD_SIZE",
366            Self::OUT_OF_BUFFER => "OUT_OF_BUFFER",
367            Self::NO_SURFACE => "NO_SURFACE",
368            _ => return Debug::fmt(&self.0, f),
369        };
370        f.write_str(name)
371    }
372}
373
374/// Functional event handlers.
375pub mod event_handlers {
376    use super::*;
377
378    impl WpViewport {}
379}