get_registry/common/protocols/wayland/wl_surface.rs
1//! an onscreen surface
2//!
3//! A surface is a rectangular area that may be displayed on zero
4//! or more outputs, and shown any number of times at the compositor's
5//! discretion. They can present wl_buffers, receive user input, and
6//! define a local coordinate system.
7//!
8//! The size of a surface (and relative positions on it) is described
9//! in surface-local coordinates, which may differ from the buffer
10//! coordinates of the pixel content, in case a buffer_transform
11//! or a buffer_scale is used.
12//!
13//! A surface without a "role" is fairly useless: a compositor does
14//! not know where, when or how to present it. The role is the
15//! purpose of a wl_surface. Examples of roles are a cursor for a
16//! pointer (as set by wl_pointer.set_cursor), a drag icon
17//! (wl_data_device.start_drag), a sub-surface
18//! (wl_subcompositor.get_subsurface), and a window as defined by a
19//! shell protocol (e.g. wl_shell.get_shell_surface).
20//!
21//! A surface can have only one role at a time. Initially a
22//! wl_surface does not have a role. Once a wl_surface is given a
23//! role, it is set permanently for the whole lifetime of the
24//! wl_surface object. Giving the current role again is allowed,
25//! unless explicitly forbidden by the relevant interface
26//! specification.
27//!
28//! Surface roles are given by requests in other interfaces such as
29//! wl_pointer.set_cursor. The request should explicitly mention
30//! that this request gives a role to a wl_surface. Often, this
31//! request also creates a new protocol object that represents the
32//! role and adds additional functionality to wl_surface. When a
33//! client wants to destroy a wl_surface, they must destroy this role
34//! object before the wl_surface, otherwise a defunct_role_object error is
35//! sent.
36//!
37//! Destroying the role object does not remove the role from the
38//! wl_surface, but it may stop the wl_surface from "playing the role".
39//! For instance, if a wl_subsurface object is destroyed, the wl_surface
40//! it was created for will be unmapped and forget its position and
41//! z-order. It is allowed to create a wl_subsurface for the same
42//! wl_surface again, but it is not allowed to use the wl_surface as
43//! a cursor (cursor is a different role than sub-surface, and role
44//! switching is not allowed).
45
46use super::super::all_types::*;
47use ::wl_client::builder::prelude::*;
48
49static INTERFACE: wl_interface = wl_interface {
50 name: c"wl_surface".as_ptr(),
51 version: 6,
52 method_count: 11,
53 methods: {
54 static MESSAGES: [wl_message; 11] = [
55 wl_message {
56 name: c"destroy".as_ptr(),
57 signature: c"".as_ptr(),
58 types: {
59 static TYPES: [Option<&'static wl_interface>; 0] = [];
60 TYPES.as_ptr().cast()
61 },
62 },
63 wl_message {
64 name: c"attach".as_ptr(),
65 signature: c"?oii".as_ptr(),
66 types: {
67 static TYPES: [Option<&'static wl_interface>; 3] =
68 [Some(WlBuffer::WL_INTERFACE), None, None];
69 TYPES.as_ptr().cast()
70 },
71 },
72 wl_message {
73 name: c"damage".as_ptr(),
74 signature: c"iiii".as_ptr(),
75 types: {
76 static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
77 TYPES.as_ptr().cast()
78 },
79 },
80 wl_message {
81 name: c"frame".as_ptr(),
82 signature: c"n".as_ptr(),
83 types: {
84 static TYPES: [Option<&'static wl_interface>; 1] =
85 [Some(WlCallback::WL_INTERFACE)];
86 TYPES.as_ptr().cast()
87 },
88 },
89 wl_message {
90 name: c"set_opaque_region".as_ptr(),
91 signature: c"?o".as_ptr(),
92 types: {
93 static TYPES: [Option<&'static wl_interface>; 1] =
94 [Some(WlRegion::WL_INTERFACE)];
95 TYPES.as_ptr().cast()
96 },
97 },
98 wl_message {
99 name: c"set_input_region".as_ptr(),
100 signature: c"?o".as_ptr(),
101 types: {
102 static TYPES: [Option<&'static wl_interface>; 1] =
103 [Some(WlRegion::WL_INTERFACE)];
104 TYPES.as_ptr().cast()
105 },
106 },
107 wl_message {
108 name: c"commit".as_ptr(),
109 signature: c"".as_ptr(),
110 types: {
111 static TYPES: [Option<&'static wl_interface>; 0] = [];
112 TYPES.as_ptr().cast()
113 },
114 },
115 wl_message {
116 name: c"set_buffer_transform".as_ptr(),
117 signature: c"i".as_ptr(),
118 types: {
119 static TYPES: [Option<&'static wl_interface>; 1] = [None];
120 TYPES.as_ptr().cast()
121 },
122 },
123 wl_message {
124 name: c"set_buffer_scale".as_ptr(),
125 signature: c"i".as_ptr(),
126 types: {
127 static TYPES: [Option<&'static wl_interface>; 1] = [None];
128 TYPES.as_ptr().cast()
129 },
130 },
131 wl_message {
132 name: c"damage_buffer".as_ptr(),
133 signature: c"iiii".as_ptr(),
134 types: {
135 static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
136 TYPES.as_ptr().cast()
137 },
138 },
139 wl_message {
140 name: c"offset".as_ptr(),
141 signature: c"ii".as_ptr(),
142 types: {
143 static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
144 TYPES.as_ptr().cast()
145 },
146 },
147 ];
148 MESSAGES.as_ptr()
149 },
150 event_count: 4,
151 events: {
152 static MESSAGES: [wl_message; 4] = [
153 wl_message {
154 name: c"enter".as_ptr(),
155 signature: c"o".as_ptr(),
156 types: {
157 static TYPES: [Option<&'static wl_interface>; 1] =
158 [Some(WlOutput::WL_INTERFACE)];
159 TYPES.as_ptr().cast()
160 },
161 },
162 wl_message {
163 name: c"leave".as_ptr(),
164 signature: c"o".as_ptr(),
165 types: {
166 static TYPES: [Option<&'static wl_interface>; 1] =
167 [Some(WlOutput::WL_INTERFACE)];
168 TYPES.as_ptr().cast()
169 },
170 },
171 wl_message {
172 name: c"preferred_buffer_scale".as_ptr(),
173 signature: c"i".as_ptr(),
174 types: {
175 static TYPES: [Option<&'static wl_interface>; 1] = [None];
176 TYPES.as_ptr().cast()
177 },
178 },
179 wl_message {
180 name: c"preferred_buffer_transform".as_ptr(),
181 signature: c"u".as_ptr(),
182 types: {
183 static TYPES: [Option<&'static wl_interface>; 1] = [None];
184 TYPES.as_ptr().cast()
185 },
186 },
187 ];
188 MESSAGES.as_ptr()
189 },
190};
191
192/// An owned wl_surface proxy.
193///
194/// See the documentation of [the module][self] for the interface description.
195#[derive(Clone, Eq, PartialEq)]
196#[repr(transparent)]
197pub struct WlSurface {
198 /// This proxy has the interface INTERFACE.
199 proxy: UntypedOwnedProxy,
200}
201
202/// A borrowed wl_surface proxy.
203///
204/// See the documentation of [the module][self] for the interface description.
205#[derive(Eq, PartialEq)]
206#[repr(transparent)]
207pub struct WlSurfaceRef {
208 /// This proxy has the interface INTERFACE.
209 proxy: UntypedBorrowedProxy,
210}
211
212// SAFETY: WlSurface is a transparent wrapper around UntypedOwnedProxy
213unsafe impl UntypedOwnedProxyWrapper for WlSurface {}
214
215// SAFETY: - INTERFACE is a valid wl_interface
216// - The only invariant is that self.proxy has a compatible interface
217unsafe impl OwnedProxy for WlSurface {
218 const INTERFACE: &'static str = "wl_surface";
219 const WL_INTERFACE: &'static wl_interface = &INTERFACE;
220 const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
221 private::EventHandler(private::NoOpEventHandler);
222 const MAX_VERSION: u32 = 6;
223
224 type Borrowed = WlSurfaceRef;
225 type Api = private::ProxyApi;
226 type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
227}
228
229// SAFETY: WlSurfaceRef is a transparent wrapper around UntypedBorrowedProxy
230unsafe impl UntypedBorrowedProxyWrapper for WlSurfaceRef {}
231
232// SAFETY: - The only invariant is that self.proxy has a compatible interface
233unsafe impl BorrowedProxy for WlSurfaceRef {
234 type Owned = WlSurface;
235}
236
237impl Deref for WlSurface {
238 type Target = WlSurfaceRef;
239
240 fn deref(&self) -> &Self::Target {
241 proxy::low_level::deref(self)
242 }
243}
244
245mod private {
246 pub struct ProxyApi;
247
248 #[allow(dead_code)]
249 pub struct EventHandler<H>(pub(super) H);
250
251 #[allow(dead_code)]
252 pub struct NoOpEventHandler;
253}
254
255impl Debug for WlSurface {
256 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
257 write!(f, "wl_surface#{}", self.proxy.id())
258 }
259}
260
261impl Debug for WlSurfaceRef {
262 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
263 write!(f, "wl_surface#{}", self.proxy.id())
264 }
265}
266
267impl PartialEq<WlSurfaceRef> for WlSurface {
268 fn eq(&self, other: &WlSurfaceRef) -> bool {
269 self.proxy == other.proxy
270 }
271}
272
273impl PartialEq<WlSurface> for WlSurfaceRef {
274 fn eq(&self, other: &WlSurface) -> bool {
275 self.proxy == other.proxy
276 }
277}
278
279#[allow(dead_code)]
280impl WlSurface {
281 /// Since when the destroy request is available.
282 #[allow(dead_code)]
283 pub const REQ__DESTROY__SINCE: u32 = 1;
284
285 /// Since when the attach request is available.
286 #[allow(dead_code)]
287 pub const REQ__ATTACH__SINCE: u32 = 1;
288
289 /// Since when the damage request is available.
290 #[allow(dead_code)]
291 pub const REQ__DAMAGE__SINCE: u32 = 1;
292
293 /// Since when the frame request is available.
294 #[allow(dead_code)]
295 pub const REQ__FRAME__SINCE: u32 = 1;
296
297 /// Since when the set_opaque_region request is available.
298 #[allow(dead_code)]
299 pub const REQ__SET_OPAQUE_REGION__SINCE: u32 = 1;
300
301 /// Since when the set_input_region request is available.
302 #[allow(dead_code)]
303 pub const REQ__SET_INPUT_REGION__SINCE: u32 = 1;
304
305 /// Since when the commit request is available.
306 #[allow(dead_code)]
307 pub const REQ__COMMIT__SINCE: u32 = 1;
308
309 /// Since when the set_buffer_transform request is available.
310 #[allow(dead_code)]
311 pub const REQ__SET_BUFFER_TRANSFORM__SINCE: u32 = 2;
312
313 /// Since when the set_buffer_scale request is available.
314 #[allow(dead_code)]
315 pub const REQ__SET_BUFFER_SCALE__SINCE: u32 = 3;
316
317 /// Since when the damage_buffer request is available.
318 #[allow(dead_code)]
319 pub const REQ__DAMAGE_BUFFER__SINCE: u32 = 4;
320
321 /// Since when the offset request is available.
322 #[allow(dead_code)]
323 pub const REQ__OFFSET__SINCE: u32 = 5;
324}
325
326#[allow(dead_code)]
327impl WlSurface {
328 /// delete surface
329 ///
330 /// Deletes the surface and invalidates its object ID.
331 #[inline]
332 pub fn destroy(&self) {
333 let mut args = [];
334 // SAFETY: - self.proxy has the interface INTERFACE
335 // - 0 < INTERFACE.method_count = 11
336 // - the request signature is ``
337 unsafe {
338 self.proxy.send_destructor(0, &mut args);
339 }
340 }
341
342 /// request a frame throttling hint
343 ///
344 /// Request a notification when it is a good time to start drawing a new
345 /// frame, by creating a frame callback. This is useful for throttling
346 /// redrawing operations, and driving animations.
347 ///
348 /// When a client is animating on a wl_surface, it can use the 'frame'
349 /// request to get notified when it is a good time to draw and commit the
350 /// next frame of animation. If the client commits an update earlier than
351 /// that, it is likely that some updates will not make it to the display,
352 /// and the client is wasting resources by drawing too often.
353 ///
354 /// The frame request will take effect on the next wl_surface.commit.
355 /// The notification will only be posted for one frame unless
356 /// requested again. For a wl_surface, the notifications are posted in
357 /// the order the frame requests were committed.
358 ///
359 /// The server must send the notifications so that a client
360 /// will not send excessive updates, while still allowing
361 /// the highest possible update rate for clients that wait for the reply
362 /// before drawing again. The server should give some time for the client
363 /// to draw and commit after sending the frame callback events to let it
364 /// hit the next output refresh.
365 ///
366 /// A server should avoid signaling the frame callbacks if the
367 /// surface is not visible in any way, e.g. the surface is off-screen,
368 /// or completely obscured by other opaque surfaces.
369 ///
370 /// The object returned by this request will be destroyed by the
371 /// compositor after the callback is fired and as such the client must not
372 /// attempt to use it after that point.
373 ///
374 /// The callback_data passed in the callback is the current time, in
375 /// milliseconds, with an undefined base.
376 #[inline]
377 pub fn frame(&self) -> WlCallback {
378 let mut args = [wl_argument { n: 0 }];
379 // SAFETY: - self.proxy has the interface INTERFACE
380 // - 3 < INTERFACE.method_count = 11
381 // - the request signature is `n`
382 // - OwnedProxy::WL_INTERFACE is always a valid interface
383 let data = unsafe {
384 self.proxy
385 .send_constructor::<false>(3, &mut args, WlCallback::WL_INTERFACE, None)
386 };
387 // SAFETY: data has the interface WlCallback::WL_INTERFACE
388 unsafe { proxy::low_level::from_untyped_owned(data) }
389 }
390}
391
392#[allow(dead_code)]
393impl WlSurfaceRef {
394 /// set the surface contents
395 ///
396 /// Set a buffer as the content of this surface.
397 ///
398 /// The new size of the surface is calculated based on the buffer
399 /// size transformed by the inverse buffer_transform and the
400 /// inverse buffer_scale. This means that at commit time the supplied
401 /// buffer size must be an integer multiple of the buffer_scale. If
402 /// that's not the case, an invalid_size error is sent.
403 ///
404 /// The x and y arguments specify the location of the new pending
405 /// buffer's upper left corner, relative to the current buffer's upper
406 /// left corner, in surface-local coordinates. In other words, the
407 /// x and y, combined with the new surface size define in which
408 /// directions the surface's size changes. Setting anything other than 0
409 /// as x and y arguments is discouraged, and should instead be replaced
410 /// with using the separate wl_surface.offset request.
411 ///
412 /// When the bound wl_surface version is 5 or higher, passing any
413 /// non-zero x or y is a protocol violation, and will result in an
414 /// 'invalid_offset' error being raised. The x and y arguments are ignored
415 /// and do not change the pending state. To achieve equivalent semantics,
416 /// use wl_surface.offset.
417 ///
418 /// Surface contents are double-buffered state, see wl_surface.commit.
419 ///
420 /// The initial surface contents are void; there is no content.
421 /// wl_surface.attach assigns the given wl_buffer as the pending
422 /// wl_buffer. wl_surface.commit makes the pending wl_buffer the new
423 /// surface contents, and the size of the surface becomes the size
424 /// calculated from the wl_buffer, as described above. After commit,
425 /// there is no pending buffer until the next attach.
426 ///
427 /// Committing a pending wl_buffer allows the compositor to read the
428 /// pixels in the wl_buffer. The compositor may access the pixels at
429 /// any time after the wl_surface.commit request. When the compositor
430 /// will not access the pixels anymore, it will send the
431 /// wl_buffer.release event. Only after receiving wl_buffer.release,
432 /// the client may reuse the wl_buffer. A wl_buffer that has been
433 /// attached and then replaced by another attach instead of committed
434 /// will not receive a release event, and is not used by the
435 /// compositor.
436 ///
437 /// If a pending wl_buffer has been committed to more than one wl_surface,
438 /// the delivery of wl_buffer.release events becomes undefined. A well
439 /// behaved client should not rely on wl_buffer.release events in this
440 /// case. Alternatively, a client could create multiple wl_buffer objects
441 /// from the same backing storage or use wp_linux_buffer_release.
442 ///
443 /// Destroying the wl_buffer after wl_buffer.release does not change
444 /// the surface contents. Destroying the wl_buffer before wl_buffer.release
445 /// is allowed as long as the underlying buffer storage isn't re-used (this
446 /// can happen e.g. on client process termination). However, if the client
447 /// destroys the wl_buffer before receiving the wl_buffer.release event and
448 /// mutates the underlying buffer storage, the surface contents become
449 /// undefined immediately.
450 ///
451 /// If wl_surface.attach is sent with a NULL wl_buffer, the
452 /// following wl_surface.commit will remove the surface content.
453 ///
454 /// If a pending wl_buffer has been destroyed, the result is not specified.
455 /// Many compositors are known to remove the surface content on the following
456 /// wl_surface.commit, but this behaviour is not universal. Clients seeking to
457 /// maximise compatibility should not destroy pending buffers and should
458 /// ensure that they explicitly remove content from surfaces, even after
459 /// destroying buffers.
460 ///
461 /// # Arguments
462 ///
463 /// - `buffer`: buffer of surface contents
464 /// - `x`: surface-local x coordinate
465 /// - `y`: surface-local y coordinate
466 #[inline]
467 pub fn attach(&self, buffer: Option<&WlBufferRef>, x: i32, y: i32) {
468 let (arg0, arg1, arg2) = (buffer, x, y);
469 let obj0_lock = arg0.map(|arg0| proxy::lock(arg0));
470 let obj0 = obj0_lock
471 .map(|obj0_lock| check_argument_proxy("buffer", obj0_lock.wl_proxy()))
472 .unwrap_or(ptr::null_mut());
473 let mut args = [
474 wl_argument { o: obj0 },
475 wl_argument { i: arg1 },
476 wl_argument { i: arg2 },
477 ];
478 // SAFETY: - self.proxy has the interface INTERFACE
479 // - 1 < INTERFACE.method_count = 11
480 // - the request signature is `?oii`
481 unsafe {
482 self.proxy.send_request(1, &mut args);
483 }
484 }
485
486 /// mark part of the surface damaged
487 ///
488 /// This request is used to describe the regions where the pending
489 /// buffer is different from the current surface contents, and where
490 /// the surface therefore needs to be repainted. The compositor
491 /// ignores the parts of the damage that fall outside of the surface.
492 ///
493 /// Damage is double-buffered state, see wl_surface.commit.
494 ///
495 /// The damage rectangle is specified in surface-local coordinates,
496 /// where x and y specify the upper left corner of the damage rectangle.
497 ///
498 /// The initial value for pending damage is empty: no damage.
499 /// wl_surface.damage adds pending damage: the new pending damage
500 /// is the union of old pending damage and the given rectangle.
501 ///
502 /// wl_surface.commit assigns pending damage as the current damage,
503 /// and clears pending damage. The server will clear the current
504 /// damage as it repaints the surface.
505 ///
506 /// Note! New clients should not use this request. Instead damage can be
507 /// posted with wl_surface.damage_buffer which uses buffer coordinates
508 /// instead of surface coordinates.
509 ///
510 /// # Arguments
511 ///
512 /// - `x`: surface-local x coordinate
513 /// - `y`: surface-local y coordinate
514 /// - `width`: width of damage rectangle
515 /// - `height`: height of damage rectangle
516 #[inline]
517 pub fn damage(&self, x: i32, y: i32, width: i32, height: i32) {
518 let (arg0, arg1, arg2, arg3) = (x, y, width, height);
519 let mut args = [
520 wl_argument { i: arg0 },
521 wl_argument { i: arg1 },
522 wl_argument { i: arg2 },
523 wl_argument { i: arg3 },
524 ];
525 // SAFETY: - self.proxy has the interface INTERFACE
526 // - 2 < INTERFACE.method_count = 11
527 // - the request signature is `iiii`
528 unsafe {
529 self.proxy.send_request(2, &mut args);
530 }
531 }
532
533 /// request a frame throttling hint
534 ///
535 /// Request a notification when it is a good time to start drawing a new
536 /// frame, by creating a frame callback. This is useful for throttling
537 /// redrawing operations, and driving animations.
538 ///
539 /// When a client is animating on a wl_surface, it can use the 'frame'
540 /// request to get notified when it is a good time to draw and commit the
541 /// next frame of animation. If the client commits an update earlier than
542 /// that, it is likely that some updates will not make it to the display,
543 /// and the client is wasting resources by drawing too often.
544 ///
545 /// The frame request will take effect on the next wl_surface.commit.
546 /// The notification will only be posted for one frame unless
547 /// requested again. For a wl_surface, the notifications are posted in
548 /// the order the frame requests were committed.
549 ///
550 /// The server must send the notifications so that a client
551 /// will not send excessive updates, while still allowing
552 /// the highest possible update rate for clients that wait for the reply
553 /// before drawing again. The server should give some time for the client
554 /// to draw and commit after sending the frame callback events to let it
555 /// hit the next output refresh.
556 ///
557 /// A server should avoid signaling the frame callbacks if the
558 /// surface is not visible in any way, e.g. the surface is off-screen,
559 /// or completely obscured by other opaque surfaces.
560 ///
561 /// The object returned by this request will be destroyed by the
562 /// compositor after the callback is fired and as such the client must not
563 /// attempt to use it after that point.
564 ///
565 /// The callback_data passed in the callback is the current time, in
566 /// milliseconds, with an undefined base.
567 ///
568 /// # Arguments
569 ///
570 /// - `_queue`: The queue that the returned proxy is assigned to.
571 #[inline]
572 pub fn frame(&self, _queue: &Queue) -> WlCallback {
573 let mut args = [wl_argument { n: 0 }];
574 // SAFETY: - self.proxy has the interface INTERFACE
575 // - 3 < INTERFACE.method_count = 11
576 // - the request signature is `n`
577 // - OwnedProxy::WL_INTERFACE is always a valid interface
578 let data = unsafe {
579 self.proxy
580 .send_constructor(_queue, 3, &mut args, WlCallback::WL_INTERFACE, None)
581 };
582 // SAFETY: data has the interface WlCallback::WL_INTERFACE
583 unsafe { proxy::low_level::from_untyped_owned(data) }
584 }
585
586 /// set opaque region
587 ///
588 /// This request sets the region of the surface that contains
589 /// opaque content.
590 ///
591 /// The opaque region is an optimization hint for the compositor
592 /// that lets it optimize the redrawing of content behind opaque
593 /// regions. Setting an opaque region is not required for correct
594 /// behaviour, but marking transparent content as opaque will result
595 /// in repaint artifacts.
596 ///
597 /// The opaque region is specified in surface-local coordinates.
598 ///
599 /// The compositor ignores the parts of the opaque region that fall
600 /// outside of the surface.
601 ///
602 /// Opaque region is double-buffered state, see wl_surface.commit.
603 ///
604 /// wl_surface.set_opaque_region changes the pending opaque region.
605 /// wl_surface.commit copies the pending region to the current region.
606 /// Otherwise, the pending and current regions are never changed.
607 ///
608 /// The initial value for an opaque region is empty. Setting the pending
609 /// opaque region has copy semantics, and the wl_region object can be
610 /// destroyed immediately. A NULL wl_region causes the pending opaque
611 /// region to be set to empty.
612 ///
613 /// # Arguments
614 ///
615 /// - `region`: opaque region of the surface
616 #[inline]
617 pub fn set_opaque_region(&self, region: Option<&WlRegionRef>) {
618 let (arg0,) = (region,);
619 let obj0_lock = arg0.map(|arg0| proxy::lock(arg0));
620 let obj0 = obj0_lock
621 .map(|obj0_lock| check_argument_proxy("region", obj0_lock.wl_proxy()))
622 .unwrap_or(ptr::null_mut());
623 let mut args = [wl_argument { o: obj0 }];
624 // SAFETY: - self.proxy has the interface INTERFACE
625 // - 4 < INTERFACE.method_count = 11
626 // - the request signature is `?o`
627 unsafe {
628 self.proxy.send_request(4, &mut args);
629 }
630 }
631
632 /// set input region
633 ///
634 /// This request sets the region of the surface that can receive
635 /// pointer and touch events.
636 ///
637 /// Input events happening outside of this region will try the next
638 /// surface in the server surface stack. The compositor ignores the
639 /// parts of the input region that fall outside of the surface.
640 ///
641 /// The input region is specified in surface-local coordinates.
642 ///
643 /// Input region is double-buffered state, see wl_surface.commit.
644 ///
645 /// wl_surface.set_input_region changes the pending input region.
646 /// wl_surface.commit copies the pending region to the current region.
647 /// Otherwise the pending and current regions are never changed,
648 /// except cursor and icon surfaces are special cases, see
649 /// wl_pointer.set_cursor and wl_data_device.start_drag.
650 ///
651 /// The initial value for an input region is infinite. That means the
652 /// whole surface will accept input. Setting the pending input region
653 /// has copy semantics, and the wl_region object can be destroyed
654 /// immediately. A NULL wl_region causes the input region to be set
655 /// to infinite.
656 ///
657 /// # Arguments
658 ///
659 /// - `region`: input region of the surface
660 #[inline]
661 pub fn set_input_region(&self, region: Option<&WlRegionRef>) {
662 let (arg0,) = (region,);
663 let obj0_lock = arg0.map(|arg0| proxy::lock(arg0));
664 let obj0 = obj0_lock
665 .map(|obj0_lock| check_argument_proxy("region", obj0_lock.wl_proxy()))
666 .unwrap_or(ptr::null_mut());
667 let mut args = [wl_argument { o: obj0 }];
668 // SAFETY: - self.proxy has the interface INTERFACE
669 // - 5 < INTERFACE.method_count = 11
670 // - the request signature is `?o`
671 unsafe {
672 self.proxy.send_request(5, &mut args);
673 }
674 }
675
676 /// commit pending surface state
677 ///
678 /// Surface state (input, opaque, and damage regions, attached buffers,
679 /// etc.) is double-buffered. Protocol requests modify the pending state,
680 /// as opposed to the active state in use by the compositor.
681 ///
682 /// A commit request atomically creates a content update from the pending
683 /// state, even if the pending state has not been touched. The content
684 /// update is placed in a queue until it becomes active. After commit, the
685 /// new pending state is as documented for each related request.
686 ///
687 /// When the content update is applied, the wl_buffer is applied before all
688 /// other state. This means that all coordinates in double-buffered state
689 /// are relative to the newly attached wl_buffers, except for
690 /// wl_surface.attach itself. If there is no newly attached wl_buffer, the
691 /// coordinates are relative to the previous content update.
692 ///
693 /// All requests that need a commit to become effective are documented
694 /// to affect double-buffered state.
695 ///
696 /// Other interfaces may add further double-buffered surface state.
697 #[inline]
698 pub fn commit(&self) {
699 let mut args = [];
700 // SAFETY: - self.proxy has the interface INTERFACE
701 // - 6 < INTERFACE.method_count = 11
702 // - the request signature is ``
703 unsafe {
704 self.proxy.send_request(6, &mut args);
705 }
706 }
707
708 /// sets the buffer transformation
709 ///
710 /// This request sets the transformation that the client has already applied
711 /// to the content of the buffer. The accepted values for the transform
712 /// parameter are the values for wl_output.transform.
713 ///
714 /// The compositor applies the inverse of this transformation whenever it
715 /// uses the buffer contents.
716 ///
717 /// Buffer transform is double-buffered state, see wl_surface.commit.
718 ///
719 /// A newly created surface has its buffer transformation set to normal.
720 ///
721 /// wl_surface.set_buffer_transform changes the pending buffer
722 /// transformation. wl_surface.commit copies the pending buffer
723 /// transformation to the current one. Otherwise, the pending and current
724 /// values are never changed.
725 ///
726 /// The purpose of this request is to allow clients to render content
727 /// according to the output transform, thus permitting the compositor to
728 /// use certain optimizations even if the display is rotated. Using
729 /// hardware overlays and scanning out a client buffer for fullscreen
730 /// surfaces are examples of such optimizations. Those optimizations are
731 /// highly dependent on the compositor implementation, so the use of this
732 /// request should be considered on a case-by-case basis.
733 ///
734 /// Note that if the transform value includes 90 or 270 degree rotation,
735 /// the width of the buffer will become the surface height and the height
736 /// of the buffer will become the surface width.
737 ///
738 /// If transform is not one of the values from the
739 /// wl_output.transform enum the invalid_transform protocol error
740 /// is raised.
741 ///
742 /// # Arguments
743 ///
744 /// - `transform`: transform for interpreting buffer contents
745 #[inline]
746 pub fn set_buffer_transform(&self, transform: WlOutputTransform) {
747 let (arg0,) = (transform,);
748 let mut args = [wl_argument { u: arg0.0 }];
749 // SAFETY: - self.proxy has the interface INTERFACE
750 // - 7 < INTERFACE.method_count = 11
751 // - the request signature is `i`
752 unsafe {
753 self.proxy.send_request(7, &mut args);
754 }
755 }
756
757 /// sets the buffer scaling factor
758 ///
759 /// This request sets an optional scaling factor on how the compositor
760 /// interprets the contents of the buffer attached to the window.
761 ///
762 /// Buffer scale is double-buffered state, see wl_surface.commit.
763 ///
764 /// A newly created surface has its buffer scale set to 1.
765 ///
766 /// wl_surface.set_buffer_scale changes the pending buffer scale.
767 /// wl_surface.commit copies the pending buffer scale to the current one.
768 /// Otherwise, the pending and current values are never changed.
769 ///
770 /// The purpose of this request is to allow clients to supply higher
771 /// resolution buffer data for use on high resolution outputs. It is
772 /// intended that you pick the same buffer scale as the scale of the
773 /// output that the surface is displayed on. This means the compositor
774 /// can avoid scaling when rendering the surface on that output.
775 ///
776 /// Note that if the scale is larger than 1, then you have to attach
777 /// a buffer that is larger (by a factor of scale in each dimension)
778 /// than the desired surface size.
779 ///
780 /// If scale is not greater than 0 the invalid_scale protocol error is
781 /// raised.
782 ///
783 /// # Arguments
784 ///
785 /// - `scale`: scale for interpreting buffer contents
786 #[inline]
787 pub fn set_buffer_scale(&self, scale: i32) {
788 let (arg0,) = (scale,);
789 let mut args = [wl_argument { i: arg0 }];
790 // SAFETY: - self.proxy has the interface INTERFACE
791 // - 8 < INTERFACE.method_count = 11
792 // - the request signature is `i`
793 unsafe {
794 self.proxy.send_request(8, &mut args);
795 }
796 }
797
798 /// mark part of the surface damaged using buffer coordinates
799 ///
800 /// This request is used to describe the regions where the pending
801 /// buffer is different from the current surface contents, and where
802 /// the surface therefore needs to be repainted. The compositor
803 /// ignores the parts of the damage that fall outside of the surface.
804 ///
805 /// Damage is double-buffered state, see wl_surface.commit.
806 ///
807 /// The damage rectangle is specified in buffer coordinates,
808 /// where x and y specify the upper left corner of the damage rectangle.
809 ///
810 /// The initial value for pending damage is empty: no damage.
811 /// wl_surface.damage_buffer adds pending damage: the new pending
812 /// damage is the union of old pending damage and the given rectangle.
813 ///
814 /// wl_surface.commit assigns pending damage as the current damage,
815 /// and clears pending damage. The server will clear the current
816 /// damage as it repaints the surface.
817 ///
818 /// This request differs from wl_surface.damage in only one way - it
819 /// takes damage in buffer coordinates instead of surface-local
820 /// coordinates. While this generally is more intuitive than surface
821 /// coordinates, it is especially desirable when using wp_viewport
822 /// or when a drawing library (like EGL) is unaware of buffer scale
823 /// and buffer transform.
824 ///
825 /// Note: Because buffer transformation changes and damage requests may
826 /// be interleaved in the protocol stream, it is impossible to determine
827 /// the actual mapping between surface and buffer damage until
828 /// wl_surface.commit time. Therefore, compositors wishing to take both
829 /// kinds of damage into account will have to accumulate damage from the
830 /// two requests separately and only transform from one to the other
831 /// after receiving the wl_surface.commit.
832 ///
833 /// # Arguments
834 ///
835 /// - `x`: buffer-local x coordinate
836 /// - `y`: buffer-local y coordinate
837 /// - `width`: width of damage rectangle
838 /// - `height`: height of damage rectangle
839 #[inline]
840 pub fn damage_buffer(&self, x: i32, y: i32, width: i32, height: i32) {
841 let (arg0, arg1, arg2, arg3) = (x, y, width, height);
842 let mut args = [
843 wl_argument { i: arg0 },
844 wl_argument { i: arg1 },
845 wl_argument { i: arg2 },
846 wl_argument { i: arg3 },
847 ];
848 // SAFETY: - self.proxy has the interface INTERFACE
849 // - 9 < INTERFACE.method_count = 11
850 // - the request signature is `iiii`
851 unsafe {
852 self.proxy.send_request(9, &mut args);
853 }
854 }
855
856 /// set the surface contents offset
857 ///
858 /// The x and y arguments specify the location of the new pending
859 /// buffer's upper left corner, relative to the current buffer's upper
860 /// left corner, in surface-local coordinates. In other words, the
861 /// x and y, combined with the new surface size define in which
862 /// directions the surface's size changes.
863 ///
864 /// The exact semantics of wl_surface.offset are role-specific. Refer to
865 /// the documentation of specific roles for more information.
866 ///
867 /// Surface location offset is double-buffered state, see
868 /// wl_surface.commit.
869 ///
870 /// This request is semantically equivalent to and the replaces the x and y
871 /// arguments in the wl_surface.attach request in wl_surface versions prior
872 /// to 5. See wl_surface.attach for details.
873 ///
874 /// # Arguments
875 ///
876 /// - `x`: surface-local x coordinate
877 /// - `y`: surface-local y coordinate
878 #[inline]
879 pub fn offset(&self, x: i32, y: i32) {
880 let (arg0, arg1) = (x, y);
881 let mut args = [wl_argument { i: arg0 }, wl_argument { i: arg1 }];
882 // SAFETY: - self.proxy has the interface INTERFACE
883 // - 10 < INTERFACE.method_count = 11
884 // - the request signature is `ii`
885 unsafe {
886 self.proxy.send_request(10, &mut args);
887 }
888 }
889}
890
891impl WlSurface {
892 /// Since when the enter event is available.
893 #[allow(dead_code)]
894 pub const EVT__ENTER__SINCE: u32 = 1;
895
896 /// Since when the leave event is available.
897 #[allow(dead_code)]
898 pub const EVT__LEAVE__SINCE: u32 = 1;
899
900 /// Since when the preferred_buffer_scale event is available.
901 #[allow(dead_code)]
902 pub const EVT__PREFERRED_BUFFER_SCALE__SINCE: u32 = 6;
903
904 /// Since when the preferred_buffer_transform event is available.
905 #[allow(dead_code)]
906 pub const EVT__PREFERRED_BUFFER_TRANSFORM__SINCE: u32 = 6;
907}
908
909/// An event handler for [WlSurface] proxies.
910#[allow(dead_code)]
911pub trait WlSurfaceEventHandler {
912 /// surface enters an output
913 ///
914 /// This is emitted whenever a surface's creation, movement, or resizing
915 /// results in some part of it being within the scanout region of an
916 /// output.
917 ///
918 /// Note that a surface may be overlapping with zero or more outputs.
919 ///
920 /// # Arguments
921 ///
922 /// - `output`: output entered by the surface
923 ///
924 /// All borrowed proxies passed to this function are guaranteed to be
925 /// immutable and non-null.
926 #[inline]
927 fn enter(&self, _slf: &WlSurfaceRef, output: Option<&WlOutputRef>) {
928 let _ = output;
929 }
930
931 /// surface leaves an output
932 ///
933 /// This is emitted whenever a surface's creation, movement, or resizing
934 /// results in it no longer having any part of it within the scanout region
935 /// of an output.
936 ///
937 /// Clients should not use the number of outputs the surface is on for frame
938 /// throttling purposes. The surface might be hidden even if no leave event
939 /// has been sent, and the compositor might expect new surface content
940 /// updates even if no enter event has been sent. The frame event should be
941 /// used instead.
942 ///
943 /// # Arguments
944 ///
945 /// - `output`: output left by the surface
946 ///
947 /// All borrowed proxies passed to this function are guaranteed to be
948 /// immutable and non-null.
949 #[inline]
950 fn leave(&self, _slf: &WlSurfaceRef, output: Option<&WlOutputRef>) {
951 let _ = output;
952 }
953
954 /// preferred buffer scale for the surface
955 ///
956 /// This event indicates the preferred buffer scale for this surface. It is
957 /// sent whenever the compositor's preference changes.
958 ///
959 /// Before receiving this event the preferred buffer scale for this surface
960 /// is 1.
961 ///
962 /// It is intended that scaling aware clients use this event to scale their
963 /// content and use wl_surface.set_buffer_scale to indicate the scale they
964 /// have rendered with. This allows clients to supply a higher detail
965 /// buffer.
966 ///
967 /// The compositor shall emit a scale value greater than 0.
968 ///
969 /// # Arguments
970 ///
971 /// - `factor`: preferred scaling factor
972 #[inline]
973 fn preferred_buffer_scale(&self, _slf: &WlSurfaceRef, factor: i32) {
974 let _ = factor;
975 }
976
977 /// preferred buffer transform for the surface
978 ///
979 /// This event indicates the preferred buffer transform for this surface.
980 /// It is sent whenever the compositor's preference changes.
981 ///
982 /// Before receiving this event the preferred buffer transform for this
983 /// surface is normal.
984 ///
985 /// Applying this transformation to the surface buffer contents and using
986 /// wl_surface.set_buffer_transform might allow the compositor to use the
987 /// surface buffer more efficiently.
988 ///
989 /// # Arguments
990 ///
991 /// - `transform`: preferred transform
992 #[inline]
993 fn preferred_buffer_transform(&self, _slf: &WlSurfaceRef, transform: WlOutputTransform) {
994 let _ = transform;
995 }
996}
997
998impl WlSurfaceEventHandler for private::NoOpEventHandler {}
999
1000// SAFETY: - INTERFACE is a valid wl_interface
1001unsafe impl<H> EventHandler for private::EventHandler<H>
1002where
1003 H: WlSurfaceEventHandler,
1004{
1005 const WL_INTERFACE: &'static wl_interface = &INTERFACE;
1006
1007 #[allow(unused_variables)]
1008 unsafe fn handle_event(
1009 &self,
1010 queue: &Queue,
1011 data: *mut u8,
1012 slf: &UntypedBorrowedProxy,
1013 opcode: u32,
1014 args: *mut wl_argument,
1015 ) {
1016 // SAFETY: This function requires that slf has the interface INTERFACE
1017 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(slf) };
1018 match opcode {
1019 0 => {
1020 // SAFETY: INTERFACE requires that there are 1 arguments
1021 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1022 // SAFETY: - INTERFACE requires that args[0] contains an object
1023 let arg0 = unsafe {
1024 if let Some(p) = NonNull::new(args[0].o.cast()) {
1025 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
1026 } else {
1027 None
1028 }
1029 };
1030 // SAFETY: - INTERFACE requires that the object has the interface WlOutput::WL_INTERFACE
1031 let arg0 = arg0.as_ref().map(|arg0| unsafe {
1032 proxy::low_level::from_untyped_borrowed::<WlOutputRef>(arg0)
1033 });
1034 self.0.enter(slf, arg0);
1035 }
1036 1 => {
1037 // SAFETY: INTERFACE requires that there are 1 arguments
1038 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1039 // SAFETY: - INTERFACE requires that args[0] contains an object
1040 let arg0 = unsafe {
1041 if let Some(p) = NonNull::new(args[0].o.cast()) {
1042 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
1043 } else {
1044 None
1045 }
1046 };
1047 // SAFETY: - INTERFACE requires that the object has the interface WlOutput::WL_INTERFACE
1048 let arg0 = arg0.as_ref().map(|arg0| unsafe {
1049 proxy::low_level::from_untyped_borrowed::<WlOutputRef>(arg0)
1050 });
1051 self.0.leave(slf, arg0);
1052 }
1053 2 => {
1054 // SAFETY: INTERFACE requires that there are 1 arguments
1055 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1056 // SAFETY: - INTERFACE requires that args[0] contains an int
1057 let arg0 = unsafe { args[0].i };
1058 self.0.preferred_buffer_scale(slf, arg0);
1059 }
1060 3 => {
1061 // SAFETY: INTERFACE requires that there are 1 arguments
1062 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1063 // SAFETY: - INTERFACE requires that args[0] contains a uint
1064 let arg0 = unsafe { WlOutputTransform(args[0].u) };
1065 self.0.preferred_buffer_transform(slf, arg0);
1066 }
1067 _ => {
1068 invalid_opcode("wl_surface", opcode);
1069 }
1070 }
1071 }
1072}
1073
1074impl<H> CreateEventHandler<H> for private::ProxyApi
1075where
1076 H: WlSurfaceEventHandler,
1077{
1078 type EventHandler = private::EventHandler<H>;
1079
1080 #[inline]
1081 fn create_event_handler(handler: H) -> Self::EventHandler {
1082 private::EventHandler(handler)
1083 }
1084}
1085
1086impl WlSurface {
1087 /// Since when the error.invalid_scale enum variant is available.
1088 #[allow(dead_code)]
1089 pub const ENM__ERROR_INVALID_SCALE__SINCE: u32 = 1;
1090 /// Since when the error.invalid_transform enum variant is available.
1091 #[allow(dead_code)]
1092 pub const ENM__ERROR_INVALID_TRANSFORM__SINCE: u32 = 1;
1093 /// Since when the error.invalid_size enum variant is available.
1094 #[allow(dead_code)]
1095 pub const ENM__ERROR_INVALID_SIZE__SINCE: u32 = 1;
1096 /// Since when the error.invalid_offset enum variant is available.
1097 #[allow(dead_code)]
1098 pub const ENM__ERROR_INVALID_OFFSET__SINCE: u32 = 1;
1099 /// Since when the error.defunct_role_object enum variant is available.
1100 #[allow(dead_code)]
1101 pub const ENM__ERROR_DEFUNCT_ROLE_OBJECT__SINCE: u32 = 1;
1102}
1103
1104/// wl_surface error values
1105///
1106/// These errors can be emitted in response to wl_surface requests.
1107#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
1108#[allow(dead_code)]
1109pub struct WlSurfaceError(pub u32);
1110
1111impl WlSurfaceError {
1112 /// buffer scale value is invalid
1113 #[allow(dead_code)]
1114 pub const INVALID_SCALE: Self = Self(0);
1115
1116 /// buffer transform value is invalid
1117 #[allow(dead_code)]
1118 pub const INVALID_TRANSFORM: Self = Self(1);
1119
1120 /// buffer size is invalid
1121 #[allow(dead_code)]
1122 pub const INVALID_SIZE: Self = Self(2);
1123
1124 /// buffer offset is invalid
1125 #[allow(dead_code)]
1126 pub const INVALID_OFFSET: Self = Self(3);
1127
1128 /// surface was destroyed before its role object
1129 #[allow(dead_code)]
1130 pub const DEFUNCT_ROLE_OBJECT: Self = Self(4);
1131}
1132
1133impl Debug for WlSurfaceError {
1134 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1135 let name = match *self {
1136 Self::INVALID_SCALE => "INVALID_SCALE",
1137 Self::INVALID_TRANSFORM => "INVALID_TRANSFORM",
1138 Self::INVALID_SIZE => "INVALID_SIZE",
1139 Self::INVALID_OFFSET => "INVALID_OFFSET",
1140 Self::DEFUNCT_ROLE_OBJECT => "DEFUNCT_ROLE_OBJECT",
1141 _ => return Debug::fmt(&self.0, f),
1142 };
1143 f.write_str(name)
1144 }
1145}
1146
1147/// Functional event handlers.
1148pub mod event_handlers {
1149 use super::*;
1150
1151 /// Event handler for enter events.
1152 pub struct Enter<F>(F);
1153 impl<F> WlSurfaceEventHandler for Enter<F>
1154 where
1155 F: Fn(&WlSurfaceRef, Option<&WlOutputRef>),
1156 {
1157 #[inline]
1158 fn enter(&self, _slf: &WlSurfaceRef, output: Option<&WlOutputRef>) {
1159 self.0(_slf, output)
1160 }
1161 }
1162
1163 /// Event handler for leave events.
1164 pub struct Leave<F>(F);
1165 impl<F> WlSurfaceEventHandler for Leave<F>
1166 where
1167 F: Fn(&WlSurfaceRef, Option<&WlOutputRef>),
1168 {
1169 #[inline]
1170 fn leave(&self, _slf: &WlSurfaceRef, output: Option<&WlOutputRef>) {
1171 self.0(_slf, output)
1172 }
1173 }
1174
1175 /// Event handler for preferred_buffer_scale events.
1176 pub struct PreferredBufferScale<F>(F);
1177 impl<F> WlSurfaceEventHandler for PreferredBufferScale<F>
1178 where
1179 F: Fn(&WlSurfaceRef, i32),
1180 {
1181 #[inline]
1182 fn preferred_buffer_scale(&self, _slf: &WlSurfaceRef, factor: i32) {
1183 self.0(_slf, factor)
1184 }
1185 }
1186
1187 /// Event handler for preferred_buffer_transform events.
1188 pub struct PreferredBufferTransform<F>(F);
1189 impl<F> WlSurfaceEventHandler for PreferredBufferTransform<F>
1190 where
1191 F: Fn(&WlSurfaceRef, WlOutputTransform),
1192 {
1193 #[inline]
1194 fn preferred_buffer_transform(&self, _slf: &WlSurfaceRef, transform: WlOutputTransform) {
1195 self.0(_slf, transform)
1196 }
1197 }
1198
1199 impl WlSurface {
1200 /// Creates an event handler for enter events.
1201 ///
1202 /// The event handler ignores all other events.
1203 #[allow(dead_code)]
1204 pub fn on_enter<F>(f: F) -> Enter<F>
1205 where
1206 F: Fn(&WlSurfaceRef, Option<&WlOutputRef>),
1207 {
1208 Enter(f)
1209 }
1210
1211 /// Creates an event handler for leave events.
1212 ///
1213 /// The event handler ignores all other events.
1214 #[allow(dead_code)]
1215 pub fn on_leave<F>(f: F) -> Leave<F>
1216 where
1217 F: Fn(&WlSurfaceRef, Option<&WlOutputRef>),
1218 {
1219 Leave(f)
1220 }
1221
1222 /// Creates an event handler for preferred_buffer_scale events.
1223 ///
1224 /// The event handler ignores all other events.
1225 #[allow(dead_code)]
1226 pub fn on_preferred_buffer_scale<F>(f: F) -> PreferredBufferScale<F>
1227 where
1228 F: Fn(&WlSurfaceRef, i32),
1229 {
1230 PreferredBufferScale(f)
1231 }
1232
1233 /// Creates an event handler for preferred_buffer_transform events.
1234 ///
1235 /// The event handler ignores all other events.
1236 #[allow(dead_code)]
1237 pub fn on_preferred_buffer_transform<F>(f: F) -> PreferredBufferTransform<F>
1238 where
1239 F: Fn(&WlSurfaceRef, WlOutputTransform),
1240 {
1241 PreferredBufferTransform(f)
1242 }
1243 }
1244}