Debian Codegraph @

src/backend/xdg_desktop_portal/window_identifier/mod.rs

raw
1 use std::fmt;
2
3 use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
4
5 #[derive(Debug)]
6 #[non_exhaustive]
7 pub enum WindowIdentifier {
8 #[cfg(feature = "wayland")]
9 Wayland(WaylandWindowIdentifier),
10 X11(WindowIdentifierType),
11 }
12
13 impl std::fmt::Display for WindowIdentifier {
14 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15 match self {
16 #[cfg(feature = "wayland")]
17 Self::Wayland(identifier) => identifier.fmt(f),
18 Self::X11(identifier) => identifier.fmt(f),
19 }
20 }
21 }
22
23 impl WindowIdentifier {
24 pub async fn from_raw_handle(
25 window_handle: &RawWindowHandle,
26 display_handle: Option<&RawDisplayHandle>,
27 ) -> Option<Self> {
28 use raw_window_handle::RawWindowHandle::{Xcb, Xlib};
29 #[cfg(feature = "wayland")]
30 use raw_window_handle::{
31 RawDisplayHandle::Wayland as DisplayHandle, RawWindowHandle::Wayland,
32 };
33 match (window_handle, display_handle) {
34 #[cfg(feature = "wayland")]
35 (Wayland(wl_handle), Some(DisplayHandle(wl_display))) => unsafe {
36 Self::from_wayland_raw(wl_handle.surface.as_ptr(), wl_display.display.as_ptr())
37 .await
38 },
39 (Xlib(x_handle), _) => Some(Self::from_xid(x_handle.window)),
40 (Xcb(x_handle), _) => Some(Self::from_xid(x_handle.window.get().into())),
41 _ => None,
42 }
43 }
44
45 pub fn from_xid(xid: std::os::raw::c_ulong) -> Self {
46 Self::X11(WindowIdentifierType::X11(xid))
47 }
48
49 #[cfg(feature = "wayland")]
50 pub async unsafe fn from_wayland_raw(
51 surface_ptr: *mut std::ffi::c_void,
52 display_ptr: *mut std::ffi::c_void,
53 ) -> Option<Self> {
54 WaylandWindowIdentifier::from_raw(surface_ptr, display_ptr)
55 .await
56 .map(Self::Wayland)
57 }
58 }
59
60 /// Supported WindowIdentifier kinds
61 #[derive(Debug, Clone, PartialEq, Eq)]
62 pub enum WindowIdentifierType {
63 /// X11.
64 X11(std::os::raw::c_ulong),
65 #[allow(dead_code)]
66 /// Wayland.
67 Wayland(String),
68 }
69
70 impl fmt::Display for WindowIdentifierType {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self {
73 Self::X11(xid) => {
74 f.write_str("x11:")?;
75 write!(f, "{xid:x}")
76 }
77 Self::Wayland(handle) => {
78 f.write_str("wayland:")?;
79 f.write_str(handle)
80 }
81 }
82 }
83 }
84
85 #[cfg(feature = "wayland")]
86 mod wayland;
87
88 #[cfg(feature = "wayland")]
89 pub use self::wayland::WaylandWindowIdentifier;