get_registry/common/protocols_data/xdg_shell/xdg_wm_base.rs
1//! create desktop-style surfaces
2//!
3//! The xdg_wm_base interface is exposed as a global object enabling clients
4//! to turn their wl_surfaces into windows in a desktop environment. It
5//! defines the basic functionality needed for clients and the compositor to
6//! create windows that can be dragged, resized, maximized, etc, as well as
7//! creating transient windows such as popup menus.
8
9use super::super::all_types::*;
10use ::wl_client::builder::prelude::*;
11
12static INTERFACE: wl_interface = wl_interface {
13 name: c"xdg_wm_base".as_ptr(),
14 version: 6,
15 method_count: 4,
16 methods: {
17 static MESSAGES: [wl_message; 4] = [
18 wl_message {
19 name: c"destroy".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 wl_message {
27 name: c"create_positioner".as_ptr(),
28 signature: c"n".as_ptr(),
29 types: {
30 static TYPES: [Option<&'static wl_interface>; 1] =
31 [Some(XdgPositioner::WL_INTERFACE)];
32 TYPES.as_ptr().cast()
33 },
34 },
35 wl_message {
36 name: c"get_xdg_surface".as_ptr(),
37 signature: c"no".as_ptr(),
38 types: {
39 static TYPES: [Option<&'static wl_interface>; 2] = [
40 Some(XdgSurface::WL_INTERFACE),
41 Some(WlSurface::WL_INTERFACE),
42 ];
43 TYPES.as_ptr().cast()
44 },
45 },
46 wl_message {
47 name: c"pong".as_ptr(),
48 signature: c"u".as_ptr(),
49 types: {
50 static TYPES: [Option<&'static wl_interface>; 1] = [None];
51 TYPES.as_ptr().cast()
52 },
53 },
54 ];
55 MESSAGES.as_ptr()
56 },
57 event_count: 1,
58 events: {
59 static MESSAGES: [wl_message; 1] = [wl_message {
60 name: c"ping".as_ptr(),
61 signature: c"u".as_ptr(),
62 types: {
63 static TYPES: [Option<&'static wl_interface>; 1] = [None];
64 TYPES.as_ptr().cast()
65 },
66 }];
67 MESSAGES.as_ptr()
68 },
69};
70
71/// An owned xdg_wm_base proxy.
72///
73/// See the documentation of [the module][self] for the interface description.
74#[derive(Clone, Eq, PartialEq)]
75#[repr(transparent)]
76pub struct XdgWmBase {
77 /// This proxy has the interface INTERFACE.
78 proxy: UntypedOwnedProxy,
79}
80
81/// A borrowed xdg_wm_base proxy.
82///
83/// See the documentation of [the module][self] for the interface description.
84#[derive(Eq, PartialEq)]
85#[repr(transparent)]
86pub struct XdgWmBaseRef {
87 /// This proxy has the interface INTERFACE.
88 proxy: UntypedBorrowedProxy,
89}
90
91// SAFETY: XdgWmBase is a transparent wrapper around UntypedOwnedProxy
92unsafe impl UntypedOwnedProxyWrapper for XdgWmBase {}
93
94// SAFETY: - INTERFACE is a valid wl_interface
95// - The only invariant is that self.proxy has a compatible interface
96unsafe impl OwnedProxy for XdgWmBase {
97 const INTERFACE: &'static str = "xdg_wm_base";
98 const WL_INTERFACE: &'static wl_interface = &INTERFACE;
99 const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
100 private::EventHandler(private::NoOpEventHandler);
101 const MAX_VERSION: u32 = 6;
102
103 type Borrowed = XdgWmBaseRef;
104 type Api = private::ProxyApi;
105 type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
106}
107
108// SAFETY: XdgWmBaseRef is a transparent wrapper around UntypedBorrowedProxy
109unsafe impl UntypedBorrowedProxyWrapper for XdgWmBaseRef {}
110
111// SAFETY: - The only invariant is that self.proxy has a compatible interface
112unsafe impl BorrowedProxy for XdgWmBaseRef {
113 type Owned = XdgWmBase;
114}
115
116impl Deref for XdgWmBase {
117 type Target = XdgWmBaseRef;
118
119 fn deref(&self) -> &Self::Target {
120 proxy::low_level::deref(self)
121 }
122}
123
124mod private {
125 pub struct ProxyApi;
126
127 #[allow(dead_code)]
128 pub struct EventHandler<H>(pub(super) H);
129
130 #[allow(dead_code)]
131 pub struct NoOpEventHandler;
132}
133
134impl Debug for XdgWmBase {
135 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
136 write!(f, "xdg_wm_base#{}", self.proxy.id())
137 }
138}
139
140impl Debug for XdgWmBaseRef {
141 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
142 write!(f, "xdg_wm_base#{}", self.proxy.id())
143 }
144}
145
146impl PartialEq<XdgWmBaseRef> for XdgWmBase {
147 fn eq(&self, other: &XdgWmBaseRef) -> bool {
148 self.proxy == other.proxy
149 }
150}
151
152impl PartialEq<XdgWmBase> for XdgWmBaseRef {
153 fn eq(&self, other: &XdgWmBase) -> bool {
154 self.proxy == other.proxy
155 }
156}
157
158#[allow(dead_code)]
159impl XdgWmBase {
160 /// Since when the destroy request is available.
161 #[allow(dead_code)]
162 pub const REQ__DESTROY__SINCE: u32 = 1;
163
164 /// Since when the create_positioner request is available.
165 #[allow(dead_code)]
166 pub const REQ__CREATE_POSITIONER__SINCE: u32 = 1;
167
168 /// Since when the get_xdg_surface request is available.
169 #[allow(dead_code)]
170 pub const REQ__GET_XDG_SURFACE__SINCE: u32 = 1;
171
172 /// Since when the pong request is available.
173 #[allow(dead_code)]
174 pub const REQ__PONG__SINCE: u32 = 1;
175}
176
177#[allow(dead_code)]
178impl XdgWmBase {
179 /// destroy xdg_wm_base
180 ///
181 /// Destroy this xdg_wm_base object.
182 ///
183 /// Destroying a bound xdg_wm_base object while there are surfaces
184 /// still alive created by this xdg_wm_base object instance is illegal
185 /// and will result in a defunct_surfaces error.
186 #[inline]
187 pub fn destroy(&self) {
188 let mut args = [];
189 // SAFETY: - self.proxy has the interface INTERFACE
190 // - 0 < INTERFACE.method_count = 4
191 // - the request signature is ``
192 unsafe {
193 self.proxy.send_destructor(0, &mut args);
194 }
195 }
196
197 /// create a positioner object
198 ///
199 /// Create a positioner object. A positioner object is used to position
200 /// surfaces relative to some parent surface. See the interface description
201 /// and xdg_surface.get_popup for details.
202 #[inline]
203 pub fn create_positioner(&self) -> XdgPositioner {
204 let mut args = [wl_argument { n: 0 }];
205 // SAFETY: - self.proxy has the interface INTERFACE
206 // - 1 < INTERFACE.method_count = 4
207 // - the request signature is `n`
208 // - OwnedProxy::WL_INTERFACE is always a valid interface
209 let data = unsafe {
210 self.proxy
211 .send_constructor::<false>(1, &mut args, XdgPositioner::WL_INTERFACE, None)
212 };
213 // SAFETY: data has the interface XdgPositioner::WL_INTERFACE
214 unsafe { proxy::low_level::from_untyped_owned(data) }
215 }
216
217 /// create a shell surface from a surface
218 ///
219 /// This creates an xdg_surface for the given surface. While xdg_surface
220 /// itself is not a role, the corresponding surface may only be assigned
221 /// a role extending xdg_surface, such as xdg_toplevel or xdg_popup. It is
222 /// illegal to create an xdg_surface for a wl_surface which already has an
223 /// assigned role and this will result in a role error.
224 ///
225 /// This creates an xdg_surface for the given surface. An xdg_surface is
226 /// used as basis to define a role to a given surface, such as xdg_toplevel
227 /// or xdg_popup. It also manages functionality shared between xdg_surface
228 /// based surface roles.
229 ///
230 /// See the documentation of xdg_surface for more details about what an
231 /// xdg_surface is and how it is used.
232 ///
233 /// # Arguments
234 ///
235 /// - `surface`:
236 #[inline]
237 pub fn get_xdg_surface(&self, surface: &WlSurfaceRef) -> XdgSurface {
238 let (arg1,) = (surface,);
239 let obj1_lock = proxy::lock(arg1);
240 let obj1 = check_argument_proxy("surface", obj1_lock.wl_proxy());
241 let mut args = [wl_argument { n: 0 }, wl_argument { o: obj1 }];
242 // SAFETY: - self.proxy has the interface INTERFACE
243 // - 2 < INTERFACE.method_count = 4
244 // - the request signature is `no`
245 // - OwnedProxy::WL_INTERFACE is always a valid interface
246 let data = unsafe {
247 self.proxy
248 .send_constructor::<false>(2, &mut args, XdgSurface::WL_INTERFACE, None)
249 };
250 // SAFETY: data has the interface XdgSurface::WL_INTERFACE
251 unsafe { proxy::low_level::from_untyped_owned(data) }
252 }
253}
254
255#[allow(dead_code)]
256impl XdgWmBaseRef {
257 /// create a positioner object
258 ///
259 /// Create a positioner object. A positioner object is used to position
260 /// surfaces relative to some parent surface. See the interface description
261 /// and xdg_surface.get_popup for details.
262 ///
263 /// # Arguments
264 ///
265 /// - `_queue`: The queue that the returned proxy is assigned to.
266 #[inline]
267 pub fn create_positioner(&self, _queue: &Queue) -> XdgPositioner {
268 let mut args = [wl_argument { n: 0 }];
269 // SAFETY: - self.proxy has the interface INTERFACE
270 // - 1 < INTERFACE.method_count = 4
271 // - the request signature is `n`
272 // - OwnedProxy::WL_INTERFACE is always a valid interface
273 let data = unsafe {
274 self.proxy
275 .send_constructor(_queue, 1, &mut args, XdgPositioner::WL_INTERFACE, None)
276 };
277 // SAFETY: data has the interface XdgPositioner::WL_INTERFACE
278 unsafe { proxy::low_level::from_untyped_owned(data) }
279 }
280
281 /// create a shell surface from a surface
282 ///
283 /// This creates an xdg_surface for the given surface. While xdg_surface
284 /// itself is not a role, the corresponding surface may only be assigned
285 /// a role extending xdg_surface, such as xdg_toplevel or xdg_popup. It is
286 /// illegal to create an xdg_surface for a wl_surface which already has an
287 /// assigned role and this will result in a role error.
288 ///
289 /// This creates an xdg_surface for the given surface. An xdg_surface is
290 /// used as basis to define a role to a given surface, such as xdg_toplevel
291 /// or xdg_popup. It also manages functionality shared between xdg_surface
292 /// based surface roles.
293 ///
294 /// See the documentation of xdg_surface for more details about what an
295 /// xdg_surface is and how it is used.
296 ///
297 /// # Arguments
298 ///
299 /// - `_queue`: The queue that the returned proxy is assigned to.
300 /// - `surface`:
301 #[inline]
302 pub fn get_xdg_surface(&self, _queue: &Queue, surface: &WlSurfaceRef) -> XdgSurface {
303 let (arg1,) = (surface,);
304 let obj1_lock = proxy::lock(arg1);
305 let obj1 = check_argument_proxy("surface", obj1_lock.wl_proxy());
306 let mut args = [wl_argument { n: 0 }, wl_argument { o: obj1 }];
307 // SAFETY: - self.proxy has the interface INTERFACE
308 // - 2 < INTERFACE.method_count = 4
309 // - the request signature is `no`
310 // - OwnedProxy::WL_INTERFACE is always a valid interface
311 let data = unsafe {
312 self.proxy
313 .send_constructor(_queue, 2, &mut args, XdgSurface::WL_INTERFACE, None)
314 };
315 // SAFETY: data has the interface XdgSurface::WL_INTERFACE
316 unsafe { proxy::low_level::from_untyped_owned(data) }
317 }
318
319 /// respond to a ping event
320 ///
321 /// A client must respond to a ping event with a pong request or
322 /// the client may be deemed unresponsive. See xdg_wm_base.ping
323 /// and xdg_wm_base.error.unresponsive.
324 ///
325 /// # Arguments
326 ///
327 /// - `serial`: serial of the ping event
328 #[inline]
329 pub fn pong(&self, serial: u32) {
330 let (arg0,) = (serial,);
331 let mut args = [wl_argument { u: arg0 }];
332 // SAFETY: - self.proxy has the interface INTERFACE
333 // - 3 < INTERFACE.method_count = 4
334 // - the request signature is `u`
335 unsafe {
336 self.proxy.send_request(3, &mut args);
337 }
338 }
339}
340
341impl XdgWmBase {
342 /// Since when the ping event is available.
343 #[allow(dead_code)]
344 pub const EVT__PING__SINCE: u32 = 1;
345}
346
347/// An event handler for [XdgWmBase] proxies.
348#[allow(dead_code)]
349pub trait XdgWmBaseEventHandler {
350 type Data: 'static;
351
352 /// check if the client is alive
353 ///
354 /// The ping event asks the client if it's still alive. Pass the
355 /// serial specified in the event back to the compositor by sending
356 /// a "pong" request back with the specified serial. See xdg_wm_base.pong.
357 ///
358 /// Compositors can use this to determine if the client is still
359 /// alive. It's unspecified what will happen if the client doesn't
360 /// respond to the ping request, or in what timeframe. Clients should
361 /// try to respond in a reasonable amount of time. The “unresponsive”
362 /// error is provided for compositors that wish to disconnect unresponsive
363 /// clients.
364 ///
365 /// A compositor is free to ping in any way it wants, but a client must
366 /// always respond to any xdg_wm_base object it created.
367 ///
368 /// # Arguments
369 ///
370 /// - `serial`: pass this to the pong request
371 #[inline]
372 fn ping(&self, _data: &mut Self::Data, _slf: &XdgWmBaseRef, serial: u32) {
373 let _ = serial;
374 }
375}
376
377impl XdgWmBaseEventHandler for private::NoOpEventHandler {
378 type Data = ();
379}
380
381// SAFETY: - INTERFACE is a valid wl_interface
382// - mutable_type always returns the same value
383unsafe impl<H> EventHandler for private::EventHandler<H>
384where
385 H: XdgWmBaseEventHandler,
386{
387 const WL_INTERFACE: &'static wl_interface = &INTERFACE;
388
389 #[inline]
390 fn mutable_type() -> Option<(TypeId, &'static str)> {
391 let id = TypeId::of::<H::Data>();
392 let name = std::any::type_name::<H::Data>();
393 Some((id, name))
394 }
395
396 #[allow(unused_variables)]
397 unsafe fn handle_event(
398 &self,
399 queue: &Queue,
400 data: *mut u8,
401 slf: &UntypedBorrowedProxy,
402 opcode: u32,
403 args: *mut wl_argument,
404 ) {
405 // SAFETY: This function requires that slf has the interface INTERFACE
406 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<XdgWmBaseRef>(slf) };
407 // SAFETY: This function requires that data is `&mut T` where `T`
408 // has the type id returned by `Self::mutable_type`, i.e.,
409 // `T = H::Data`.
410 let data: &mut H::Data = unsafe { &mut *data.cast() };
411 match opcode {
412 0 => {
413 // SAFETY: INTERFACE requires that there are 1 arguments
414 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
415 // SAFETY: - INTERFACE requires that args[0] contains a uint
416 let arg0 = unsafe { args[0].u };
417 self.0.ping(data, slf, arg0);
418 }
419 _ => {
420 invalid_opcode("xdg_wm_base", opcode);
421 }
422 }
423 }
424}
425
426impl<H> CreateEventHandler<H> for private::ProxyApi
427where
428 H: XdgWmBaseEventHandler,
429{
430 type EventHandler = private::EventHandler<H>;
431
432 #[inline]
433 fn create_event_handler(handler: H) -> Self::EventHandler {
434 private::EventHandler(handler)
435 }
436}
437
438impl XdgWmBase {
439 /// Since when the error.role enum variant is available.
440 #[allow(dead_code)]
441 pub const ENM__ERROR_ROLE__SINCE: u32 = 1;
442 /// Since when the error.defunct_surfaces enum variant is available.
443 #[allow(dead_code)]
444 pub const ENM__ERROR_DEFUNCT_SURFACES__SINCE: u32 = 1;
445 /// Since when the error.not_the_topmost_popup enum variant is available.
446 #[allow(dead_code)]
447 pub const ENM__ERROR_NOT_THE_TOPMOST_POPUP__SINCE: u32 = 1;
448 /// Since when the error.invalid_popup_parent enum variant is available.
449 #[allow(dead_code)]
450 pub const ENM__ERROR_INVALID_POPUP_PARENT__SINCE: u32 = 1;
451 /// Since when the error.invalid_surface_state enum variant is available.
452 #[allow(dead_code)]
453 pub const ENM__ERROR_INVALID_SURFACE_STATE__SINCE: u32 = 1;
454 /// Since when the error.invalid_positioner enum variant is available.
455 #[allow(dead_code)]
456 pub const ENM__ERROR_INVALID_POSITIONER__SINCE: u32 = 1;
457 /// Since when the error.unresponsive enum variant is available.
458 #[allow(dead_code)]
459 pub const ENM__ERROR_UNRESPONSIVE__SINCE: u32 = 1;
460}
461
462#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
463#[allow(dead_code)]
464pub struct XdgWmBaseError(pub u32);
465
466impl XdgWmBaseError {
467 /// given wl_surface has another role
468 #[allow(dead_code)]
469 pub const ROLE: Self = Self(0);
470
471 /// xdg_wm_base was destroyed before children
472 #[allow(dead_code)]
473 pub const DEFUNCT_SURFACES: Self = Self(1);
474
475 /// the client tried to map or destroy a non-topmost popup
476 #[allow(dead_code)]
477 pub const NOT_THE_TOPMOST_POPUP: Self = Self(2);
478
479 /// the client specified an invalid popup parent surface
480 #[allow(dead_code)]
481 pub const INVALID_POPUP_PARENT: Self = Self(3);
482
483 /// the client provided an invalid surface state
484 #[allow(dead_code)]
485 pub const INVALID_SURFACE_STATE: Self = Self(4);
486
487 /// the client provided an invalid positioner
488 #[allow(dead_code)]
489 pub const INVALID_POSITIONER: Self = Self(5);
490
491 /// the client didn’t respond to a ping event in time
492 #[allow(dead_code)]
493 pub const UNRESPONSIVE: Self = Self(6);
494}
495
496impl Debug for XdgWmBaseError {
497 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
498 let name = match *self {
499 Self::ROLE => "ROLE",
500 Self::DEFUNCT_SURFACES => "DEFUNCT_SURFACES",
501 Self::NOT_THE_TOPMOST_POPUP => "NOT_THE_TOPMOST_POPUP",
502 Self::INVALID_POPUP_PARENT => "INVALID_POPUP_PARENT",
503 Self::INVALID_SURFACE_STATE => "INVALID_SURFACE_STATE",
504 Self::INVALID_POSITIONER => "INVALID_POSITIONER",
505 Self::UNRESPONSIVE => "UNRESPONSIVE",
506 _ => return Debug::fmt(&self.0, f),
507 };
508 f.write_str(name)
509 }
510}
511
512/// Functional event handlers.
513pub mod event_handlers {
514 use super::*;
515
516 /// Event handler for ping events.
517 pub struct Ping<T, F>(F, PhantomData<fn(&mut T)>);
518 impl<T, F> XdgWmBaseEventHandler for Ping<T, F>
519 where
520 T: 'static,
521 F: Fn(&mut T, &XdgWmBaseRef, u32),
522 {
523 type Data = T;
524
525 #[inline]
526 fn ping(&self, _data: &mut T, _slf: &XdgWmBaseRef, serial: u32) {
527 self.0(_data, _slf, serial)
528 }
529 }
530
531 impl XdgWmBase {
532 /// Creates an event handler for ping events.
533 ///
534 /// The event handler ignores all other events.
535 #[allow(dead_code)]
536 pub fn on_ping<T, F>(f: F) -> Ping<T, F>
537 where
538 T: 'static,
539 F: Fn(&mut T, &XdgWmBaseRef, u32),
540 {
541 Ping(f, PhantomData)
542 }
543 }
544}