Debian Codegraph @

src/window_identifier/gtk4.rs

raw
1 #[cfg(feature = "raw_handle")]
2 use std::ptr::NonNull;
3 #[cfg(feature = "gtk4_wayland")]
4 use std::sync::Arc;
5
6 #[cfg(feature = "gtk4_wayland")]
7 use futures_util::lock::Mutex;
8 use gdk::Backend;
9 #[cfg(feature = "raw_handle")]
10 use glib::translate::ToGlibPtr;
11 use gtk4::{gdk, glib, prelude::*};
12 #[cfg(feature = "raw_handle")]
13 use raw_window_handle::{
14 DisplayHandle, RawDisplayHandle, RawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle,
15 WindowHandle, XlibDisplayHandle, XlibWindowHandle,
16 };
17
18 use super::WindowIdentifierType;
19
20 #[cfg(feature = "gtk4_wayland")]
21 const WINDOW_HANDLE_KEY: &str = "ashpd-wayland-gtk4-window-handle";
22
23 pub struct Gtk4WindowIdentifier {
24 #[allow(dead_code)]
25 native: gtk4::Native,
26 type_: WindowIdentifierType,
27 exported: bool,
28 }
29
30 impl Gtk4WindowIdentifier {
31 pub async fn new(native: &impl glib::prelude::IsA<gtk4::Native>) -> Option<Self> {
32 let surface = native.surface()?;
33 match surface.display().backend() {
34 #[cfg(feature = "gtk4_wayland")]
35 Backend::Wayland => {
36 let top_level = surface
37 .downcast_ref::<gdk4wayland::WaylandToplevel>()
38 .unwrap();
39 let handle = unsafe {
40 if let Some(mut handle) = top_level.data(WINDOW_HANDLE_KEY) {
41 let (handle, ref_count): &mut (Option<String>, u8) = handle.as_mut();
42 *ref_count += 1;
43 handle.clone()
44 } else {
45 let (sender, receiver) =
46 futures_channel::oneshot::channel::<Option<String>>();
47 let sender = Arc::new(Mutex::new(Some(sender)));
48
49 let result = top_level.export_handle(glib::clone!(#[strong] sender, move |_, handle| {
50 let handle = handle.map(ToOwned::to_owned);
51 glib::spawn_future_local(glib::clone!(#[strong] sender, #[strong] handle, async move {
52 if let Some(m) = sender.lock().await.take() {
53 match handle {
54 Ok(h) => {
55 let _ = m.send(Some(h.to_string()));
56 },
57 Err(_err) => {
58 let _ = m.send(None);
59 #[cfg(feature = "tracing")]
60 tracing::warn!("Failed to export window identifier. The compositor doesn't support xdg-foreign protocol. {_err}");
61 }
62 }
63 }
64 }));
65 }));
66
67 if !result {
68 return None;
69 }
70 let handle = receiver.await.ok().flatten();
71 top_level.set_data(WINDOW_HANDLE_KEY, (handle.clone(), 1));
72 handle
73 }
74 };
75 Some(Gtk4WindowIdentifier {
76 native: native.clone().upcast(),
77 exported: handle.is_some(),
78 type_: WindowIdentifierType::Wayland(handle.unwrap_or_default()),
79 })
80 }
81 #[cfg(feature = "gtk4_x11")]
82 Backend::X11 => {
83 let xid = surface
84 .downcast_ref::<gdk4x11::X11Surface>()
85 .map(|w| w.xid())?;
86 Some(Gtk4WindowIdentifier {
87 native: native.clone().upcast(),
88 exported: false,
89 type_: WindowIdentifierType::X11(xid),
90 })
91 }
92 _ => None,
93 }
94 }
95
96 #[cfg(feature = "raw_handle")]
97 pub fn as_raw_window_handle(&self) -> WindowHandle<'_> {
98 unsafe {
99 let raw_handle = match self.type_ {
100 #[cfg(feature = "gtk4_wayland")]
101 WindowIdentifierType::Wayland(_) => {
102 let surface = self.native.surface().unwrap();
103 RawWindowHandle::Wayland(WaylandWindowHandle::new(
104 NonNull::new(gdk4wayland::ffi::gdk_wayland_surface_get_wl_surface(
105 surface
106 .downcast_ref::<gdk4wayland::WaylandSurface>()
107 .unwrap()
108 .to_glib_none()
109 .0,
110 ))
111 .expect("Identifier must be attached to a wl_surface"),
112 ))
113 }
114 #[cfg(feature = "gtk4_x11")]
115 WindowIdentifierType::X11(xid) => RawWindowHandle::Xlib(XlibWindowHandle::new(xid)),
116 };
117 WindowHandle::borrow_raw(raw_handle)
118 }
119 }
120
121 #[cfg(feature = "raw_handle")]
122 pub fn as_raw_display_handle(&self) -> DisplayHandle<'_> {
123 let surface = self.native.surface().unwrap();
124 let display = surface.display();
125 unsafe {
126 let raw_handle = match self.type_ {
127 #[cfg(feature = "gtk4_wayland")]
128 WindowIdentifierType::Wayland(_) => {
129 RawDisplayHandle::Wayland(WaylandDisplayHandle::new(
130 NonNull::new(gdk4wayland::ffi::gdk_wayland_display_get_wl_display(
131 display
132 .downcast_ref::<gdk4wayland::WaylandDisplay>()
133 .unwrap()
134 .to_glib_none()
135 .0,
136 ))
137 .expect("Identifier must be attached to a wl_display"),
138 ))
139 }
140 #[cfg(feature = "gtk4_x11")]
141 WindowIdentifierType::X11(_xid) => RawDisplayHandle::Xlib(XlibDisplayHandle::new(
142 NonNull::new(gdk4x11::ffi::gdk_x11_display_get_xdisplay(
143 display
144 .downcast_ref::<gdk4x11::X11Display>()
145 .unwrap()
146 .to_glib_none()
147 .0,
148 )),
149 display
150 .downcast_ref::<gdk4x11::X11Display>()
151 .unwrap()
152 .screen()
153 .screen_number(),
154 )),
155 };
156 DisplayHandle::borrow_raw(raw_handle)
157 }
158 }
159 }
160
161 impl std::fmt::Display for Gtk4WindowIdentifier {
162 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163 self.type_.fmt(f)
164 }
165 }
166
167 impl Drop for Gtk4WindowIdentifier {
168 fn drop(&mut self) {
169 if !self.exported {
170 return;
171 }
172 match self.type_ {
173 #[cfg(feature = "gtk4_wayland")]
174 WindowIdentifierType::Wayland(_) => {
175 let surface = self.native.surface().unwrap();
176 let top_level = surface
177 .downcast_ref::<gdk4wayland::WaylandToplevel>()
178 .unwrap();
179 unsafe {
180 let (_handle, ref_count): &mut (Option<String>, u8) =
181 top_level.data(WINDOW_HANDLE_KEY).unwrap().as_mut();
182 if ref_count > &mut 1 {
183 *ref_count -= 1;
184 return;
185 }
186 top_level.unexport_handle();
187 #[cfg(feature = "tracing")]
188 tracing::debug!("Unexporting handle: {_handle:?}");
189 let _ = top_level.steal_data::<(Option<String>, u8)>(WINDOW_HANDLE_KEY);
190 }
191 }
192 _ => (),
193 }
194 }
195 }