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