Skip to main content

get_registry/common/protocols/wayland/
wl_shm_pool.rs

1//! a shared memory pool
2//!
3//! The wl_shm_pool object encapsulates a piece of memory shared
4//! between the compositor and client.  Through the wl_shm_pool
5//! object, the client can allocate shared memory wl_buffer objects.
6//! All objects created through the same pool share the same
7//! underlying mapped memory. Reusing the mapped memory avoids the
8//! setup/teardown overhead and is useful when interactively resizing
9//! a surface or for many small buffers.
10
11use super::super::all_types::*;
12use ::wl_client::builder::prelude::*;
13
14static INTERFACE: wl_interface = wl_interface {
15    name: c"wl_shm_pool".as_ptr(),
16    version: 2,
17    method_count: 3,
18    methods: {
19        static MESSAGES: [wl_message; 3] = [
20            wl_message {
21                name: c"create_buffer".as_ptr(),
22                signature: c"niiiiu".as_ptr(),
23                types: {
24                    static TYPES: [Option<&'static wl_interface>; 6] =
25                        [Some(WlBuffer::WL_INTERFACE), None, None, None, None, None];
26                    TYPES.as_ptr().cast()
27                },
28            },
29            wl_message {
30                name: c"destroy".as_ptr(),
31                signature: c"".as_ptr(),
32                types: {
33                    static TYPES: [Option<&'static wl_interface>; 0] = [];
34                    TYPES.as_ptr().cast()
35                },
36            },
37            wl_message {
38                name: c"resize".as_ptr(),
39                signature: c"i".as_ptr(),
40                types: {
41                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
42                    TYPES.as_ptr().cast()
43                },
44            },
45        ];
46        MESSAGES.as_ptr()
47    },
48    event_count: 0,
49    events: ptr::null(),
50};
51
52/// An owned wl_shm_pool proxy.
53///
54/// See the documentation of [the module][self] for the interface description.
55#[derive(Clone, Eq, PartialEq)]
56#[repr(transparent)]
57pub struct WlShmPool {
58    /// This proxy has the interface INTERFACE.
59    proxy: UntypedOwnedProxy,
60}
61
62/// A borrowed wl_shm_pool proxy.
63///
64/// See the documentation of [the module][self] for the interface description.
65#[derive(Eq, PartialEq)]
66#[repr(transparent)]
67pub struct WlShmPoolRef {
68    /// This proxy has the interface INTERFACE.
69    proxy: UntypedBorrowedProxy,
70}
71
72// SAFETY: WlShmPool is a transparent wrapper around UntypedOwnedProxy
73unsafe impl UntypedOwnedProxyWrapper for WlShmPool {}
74
75// SAFETY: - INTERFACE is a valid wl_interface
76//         - The only invariant is that self.proxy has a compatible interface
77unsafe impl OwnedProxy for WlShmPool {
78    const INTERFACE: &'static str = "wl_shm_pool";
79    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
80    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
81        private::EventHandler(private::NoOpEventHandler);
82    const MAX_VERSION: u32 = 2;
83
84    type Borrowed = WlShmPoolRef;
85    type Api = private::ProxyApi;
86    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
87}
88
89// SAFETY: WlShmPoolRef is a transparent wrapper around UntypedBorrowedProxy
90unsafe impl UntypedBorrowedProxyWrapper for WlShmPoolRef {}
91
92// SAFETY: - The only invariant is that self.proxy has a compatible interface
93unsafe impl BorrowedProxy for WlShmPoolRef {
94    type Owned = WlShmPool;
95}
96
97impl Deref for WlShmPool {
98    type Target = WlShmPoolRef;
99
100    fn deref(&self) -> &Self::Target {
101        proxy::low_level::deref(self)
102    }
103}
104
105mod private {
106    pub struct ProxyApi;
107
108    #[allow(dead_code)]
109    pub struct EventHandler<H>(pub(super) H);
110
111    #[allow(dead_code)]
112    pub struct NoOpEventHandler;
113}
114
115impl Debug for WlShmPool {
116    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
117        write!(f, "wl_shm_pool#{}", self.proxy.id())
118    }
119}
120
121impl Debug for WlShmPoolRef {
122    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
123        write!(f, "wl_shm_pool#{}", self.proxy.id())
124    }
125}
126
127impl PartialEq<WlShmPoolRef> for WlShmPool {
128    fn eq(&self, other: &WlShmPoolRef) -> bool {
129        self.proxy == other.proxy
130    }
131}
132
133impl PartialEq<WlShmPool> for WlShmPoolRef {
134    fn eq(&self, other: &WlShmPool) -> bool {
135        self.proxy == other.proxy
136    }
137}
138
139#[allow(dead_code)]
140impl WlShmPool {
141    /// Since when the create_buffer request is available.
142    #[allow(dead_code)]
143    pub const REQ__CREATE_BUFFER__SINCE: u32 = 1;
144
145    /// Since when the destroy request is available.
146    #[allow(dead_code)]
147    pub const REQ__DESTROY__SINCE: u32 = 1;
148
149    /// Since when the resize request is available.
150    #[allow(dead_code)]
151    pub const REQ__RESIZE__SINCE: u32 = 1;
152}
153
154#[allow(dead_code)]
155impl WlShmPool {
156    /// create a buffer from the pool
157    ///
158    /// Create a wl_buffer object from the pool.
159    ///
160    /// The buffer is created offset bytes into the pool and has
161    /// width and height as specified.  The stride argument specifies
162    /// the number of bytes from the beginning of one row to the beginning
163    /// of the next.  The format is the pixel format of the buffer and
164    /// must be one of those advertised through the wl_shm.format event.
165    ///
166    /// A buffer will keep a reference to the pool it was created from
167    /// so it is valid to destroy the pool immediately after creating
168    /// a buffer from it.
169    ///
170    /// # Arguments
171    ///
172    /// - `offset`: buffer byte offset within the pool
173    /// - `width`: buffer width, in pixels
174    /// - `height`: buffer height, in pixels
175    /// - `stride`: number of bytes from the beginning of one row to the beginning of the next row
176    /// - `format`: buffer pixel format
177    #[inline]
178    pub fn create_buffer(
179        &self,
180        offset: i32,
181        width: i32,
182        height: i32,
183        stride: i32,
184        format: WlShmFormat,
185    ) -> WlBuffer {
186        let (arg1, arg2, arg3, arg4, arg5) = (offset, width, height, stride, format);
187        let mut args = [
188            wl_argument { n: 0 },
189            wl_argument { i: arg1 },
190            wl_argument { i: arg2 },
191            wl_argument { i: arg3 },
192            wl_argument { i: arg4 },
193            wl_argument { u: arg5.0 },
194        ];
195        // SAFETY: - self.proxy has the interface INTERFACE
196        //         - 0 < INTERFACE.method_count = 3
197        //         - the request signature is `niiiiu`
198        //         - OwnedProxy::WL_INTERFACE is always a valid interface
199        let data = unsafe {
200            self.proxy
201                .send_constructor::<false>(0, &mut args, WlBuffer::WL_INTERFACE, None)
202        };
203        // SAFETY: data has the interface WlBuffer::WL_INTERFACE
204        unsafe { proxy::low_level::from_untyped_owned(data) }
205    }
206
207    /// destroy the pool
208    ///
209    /// Destroy the shared memory pool.
210    ///
211    /// The mmapped memory will be released when all
212    /// buffers that have been created from this pool
213    /// are gone.
214    #[inline]
215    pub fn destroy(&self) {
216        let mut args = [];
217        // SAFETY: - self.proxy has the interface INTERFACE
218        //         - 1 < INTERFACE.method_count = 3
219        //         - the request signature is ``
220        unsafe {
221            self.proxy.send_destructor(1, &mut args);
222        }
223    }
224}
225
226#[allow(dead_code)]
227impl WlShmPoolRef {
228    /// create a buffer from the pool
229    ///
230    /// Create a wl_buffer object from the pool.
231    ///
232    /// The buffer is created offset bytes into the pool and has
233    /// width and height as specified.  The stride argument specifies
234    /// the number of bytes from the beginning of one row to the beginning
235    /// of the next.  The format is the pixel format of the buffer and
236    /// must be one of those advertised through the wl_shm.format event.
237    ///
238    /// A buffer will keep a reference to the pool it was created from
239    /// so it is valid to destroy the pool immediately after creating
240    /// a buffer from it.
241    ///
242    /// # Arguments
243    ///
244    /// - `_queue`: The queue that the returned proxy is assigned to.
245    /// - `offset`: buffer byte offset within the pool
246    /// - `width`: buffer width, in pixels
247    /// - `height`: buffer height, in pixels
248    /// - `stride`: number of bytes from the beginning of one row to the beginning of the next row
249    /// - `format`: buffer pixel format
250    #[inline]
251    pub fn create_buffer(
252        &self,
253        _queue: &Queue,
254        offset: i32,
255        width: i32,
256        height: i32,
257        stride: i32,
258        format: WlShmFormat,
259    ) -> WlBuffer {
260        let (arg1, arg2, arg3, arg4, arg5) = (offset, width, height, stride, format);
261        let mut args = [
262            wl_argument { n: 0 },
263            wl_argument { i: arg1 },
264            wl_argument { i: arg2 },
265            wl_argument { i: arg3 },
266            wl_argument { i: arg4 },
267            wl_argument { u: arg5.0 },
268        ];
269        // SAFETY: - self.proxy has the interface INTERFACE
270        //         - 0 < INTERFACE.method_count = 3
271        //         - the request signature is `niiiiu`
272        //         - OwnedProxy::WL_INTERFACE is always a valid interface
273        let data = unsafe {
274            self.proxy
275                .send_constructor(_queue, 0, &mut args, WlBuffer::WL_INTERFACE, None)
276        };
277        // SAFETY: data has the interface WlBuffer::WL_INTERFACE
278        unsafe { proxy::low_level::from_untyped_owned(data) }
279    }
280
281    /// change the size of the pool mapping
282    ///
283    /// This request will cause the server to remap the backing memory
284    /// for the pool from the file descriptor passed when the pool was
285    /// created, but using the new size.  This request can only be
286    /// used to make the pool bigger.
287    ///
288    /// This request only changes the amount of bytes that are mmapped
289    /// by the server and does not touch the file corresponding to the
290    /// file descriptor passed at creation time. It is the client's
291    /// responsibility to ensure that the file is at least as big as
292    /// the new pool size.
293    ///
294    /// # Arguments
295    ///
296    /// - `size`: new size of the pool, in bytes
297    #[inline]
298    pub fn resize(&self, size: i32) {
299        let (arg0,) = (size,);
300        let mut args = [wl_argument { i: arg0 }];
301        // SAFETY: - self.proxy has the interface INTERFACE
302        //         - 2 < INTERFACE.method_count = 3
303        //         - the request signature is `i`
304        unsafe {
305            self.proxy.send_request(2, &mut args);
306        }
307    }
308}
309
310/// An event handler for [WlShmPool] proxies.
311#[allow(dead_code)]
312pub trait WlShmPoolEventHandler {}
313
314impl WlShmPoolEventHandler for private::NoOpEventHandler {}
315
316// SAFETY: - INTERFACE is a valid wl_interface
317unsafe impl<H> EventHandler for private::EventHandler<H>
318where
319    H: WlShmPoolEventHandler,
320{
321    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
322
323    #[allow(unused_variables)]
324    unsafe fn handle_event(
325        &self,
326        queue: &Queue,
327        data: *mut u8,
328        slf: &UntypedBorrowedProxy,
329        opcode: u32,
330        args: *mut wl_argument,
331    ) {
332        invalid_opcode("wl_shm_pool", opcode);
333    }
334}
335
336impl<H> CreateEventHandler<H> for private::ProxyApi
337where
338    H: WlShmPoolEventHandler,
339{
340    type EventHandler = private::EventHandler<H>;
341
342    #[inline]
343    fn create_event_handler(handler: H) -> Self::EventHandler {
344        private::EventHandler(handler)
345    }
346}
347
348/// Functional event handlers.
349pub mod event_handlers {
350    use super::*;
351
352    impl WlShmPool {}
353}