get_registry/common/protocols/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 /// check if the client is alive
351 ///
352 /// The ping event asks the client if it's still alive. Pass the
353 /// serial specified in the event back to the compositor by sending
354 /// a "pong" request back with the specified serial. See xdg_wm_base.pong.
355 ///
356 /// Compositors can use this to determine if the client is still
357 /// alive. It's unspecified what will happen if the client doesn't
358 /// respond to the ping request, or in what timeframe. Clients should
359 /// try to respond in a reasonable amount of time. The “unresponsive”
360 /// error is provided for compositors that wish to disconnect unresponsive
361 /// clients.
362 ///
363 /// A compositor is free to ping in any way it wants, but a client must
364 /// always respond to any xdg_wm_base object it created.
365 ///
366 /// # Arguments
367 ///
368 /// - `serial`: pass this to the pong request
369 #[inline]
370 fn ping(&self, _slf: &XdgWmBaseRef, serial: u32) {
371 let _ = serial;
372 }
373}
374
375impl XdgWmBaseEventHandler for private::NoOpEventHandler {}
376
377// SAFETY: - INTERFACE is a valid wl_interface
378unsafe impl<H> EventHandler for private::EventHandler<H>
379where
380 H: XdgWmBaseEventHandler,
381{
382 const WL_INTERFACE: &'static wl_interface = &INTERFACE;
383
384 #[allow(unused_variables)]
385 unsafe fn handle_event(
386 &self,
387 queue: &Queue,
388 data: *mut u8,
389 slf: &UntypedBorrowedProxy,
390 opcode: u32,
391 args: *mut wl_argument,
392 ) {
393 // SAFETY: This function requires that slf has the interface INTERFACE
394 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<XdgWmBaseRef>(slf) };
395 match opcode {
396 0 => {
397 // SAFETY: INTERFACE requires that there are 1 arguments
398 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
399 // SAFETY: - INTERFACE requires that args[0] contains a uint
400 let arg0 = unsafe { args[0].u };
401 self.0.ping(slf, arg0);
402 }
403 _ => {
404 invalid_opcode("xdg_wm_base", opcode);
405 }
406 }
407 }
408}
409
410impl<H> CreateEventHandler<H> for private::ProxyApi
411where
412 H: XdgWmBaseEventHandler,
413{
414 type EventHandler = private::EventHandler<H>;
415
416 #[inline]
417 fn create_event_handler(handler: H) -> Self::EventHandler {
418 private::EventHandler(handler)
419 }
420}
421
422impl XdgWmBase {
423 /// Since when the error.role enum variant is available.
424 #[allow(dead_code)]
425 pub const ENM__ERROR_ROLE__SINCE: u32 = 1;
426 /// Since when the error.defunct_surfaces enum variant is available.
427 #[allow(dead_code)]
428 pub const ENM__ERROR_DEFUNCT_SURFACES__SINCE: u32 = 1;
429 /// Since when the error.not_the_topmost_popup enum variant is available.
430 #[allow(dead_code)]
431 pub const ENM__ERROR_NOT_THE_TOPMOST_POPUP__SINCE: u32 = 1;
432 /// Since when the error.invalid_popup_parent enum variant is available.
433 #[allow(dead_code)]
434 pub const ENM__ERROR_INVALID_POPUP_PARENT__SINCE: u32 = 1;
435 /// Since when the error.invalid_surface_state enum variant is available.
436 #[allow(dead_code)]
437 pub const ENM__ERROR_INVALID_SURFACE_STATE__SINCE: u32 = 1;
438 /// Since when the error.invalid_positioner enum variant is available.
439 #[allow(dead_code)]
440 pub const ENM__ERROR_INVALID_POSITIONER__SINCE: u32 = 1;
441 /// Since when the error.unresponsive enum variant is available.
442 #[allow(dead_code)]
443 pub const ENM__ERROR_UNRESPONSIVE__SINCE: u32 = 1;
444}
445
446#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
447#[allow(dead_code)]
448pub struct XdgWmBaseError(pub u32);
449
450impl XdgWmBaseError {
451 /// given wl_surface has another role
452 #[allow(dead_code)]
453 pub const ROLE: Self = Self(0);
454
455 /// xdg_wm_base was destroyed before children
456 #[allow(dead_code)]
457 pub const DEFUNCT_SURFACES: Self = Self(1);
458
459 /// the client tried to map or destroy a non-topmost popup
460 #[allow(dead_code)]
461 pub const NOT_THE_TOPMOST_POPUP: Self = Self(2);
462
463 /// the client specified an invalid popup parent surface
464 #[allow(dead_code)]
465 pub const INVALID_POPUP_PARENT: Self = Self(3);
466
467 /// the client provided an invalid surface state
468 #[allow(dead_code)]
469 pub const INVALID_SURFACE_STATE: Self = Self(4);
470
471 /// the client provided an invalid positioner
472 #[allow(dead_code)]
473 pub const INVALID_POSITIONER: Self = Self(5);
474
475 /// the client didn’t respond to a ping event in time
476 #[allow(dead_code)]
477 pub const UNRESPONSIVE: Self = Self(6);
478}
479
480impl Debug for XdgWmBaseError {
481 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
482 let name = match *self {
483 Self::ROLE => "ROLE",
484 Self::DEFUNCT_SURFACES => "DEFUNCT_SURFACES",
485 Self::NOT_THE_TOPMOST_POPUP => "NOT_THE_TOPMOST_POPUP",
486 Self::INVALID_POPUP_PARENT => "INVALID_POPUP_PARENT",
487 Self::INVALID_SURFACE_STATE => "INVALID_SURFACE_STATE",
488 Self::INVALID_POSITIONER => "INVALID_POSITIONER",
489 Self::UNRESPONSIVE => "UNRESPONSIVE",
490 _ => return Debug::fmt(&self.0, f),
491 };
492 f.write_str(name)
493 }
494}
495
496/// Functional event handlers.
497pub mod event_handlers {
498 use super::*;
499
500 /// Event handler for ping events.
501 pub struct Ping<F>(F);
502 impl<F> XdgWmBaseEventHandler for Ping<F>
503 where
504 F: Fn(&XdgWmBaseRef, u32),
505 {
506 #[inline]
507 fn ping(&self, _slf: &XdgWmBaseRef, serial: u32) {
508 self.0(_slf, serial)
509 }
510 }
511
512 impl XdgWmBase {
513 /// Creates an event handler for ping events.
514 ///
515 /// The event handler ignores all other events.
516 #[allow(dead_code)]
517 pub fn on_ping<F>(f: F) -> Ping<F>
518 where
519 F: Fn(&XdgWmBaseRef, u32),
520 {
521 Ping(f)
522 }
523 }
524}