Debian Codegraph @

src/backend/xdg_desktop_portal/window_identifier/wayland.rs

raw
1 use std::fmt;
2
3 use wayland_backend::sys::client::Backend;
4 use wayland_client::{
5 protocol::{wl_registry, wl_surface::WlSurface},
6 Proxy, QueueHandle,
7 };
8 use wayland_protocols::xdg::foreign::zv2::client::{
9 zxdg_exported_v2::{self, ZxdgExportedV2},
10 zxdg_exporter_v2::ZxdgExporterV2,
11 };
12
13 use super::WindowIdentifierType;
14
15 // Supported versions.
16 const ZXDG_EXPORTER_V2: u32 = 1;
17
18 #[derive(Debug)]
19 pub struct WaylandWindowIdentifier {
20 exported: ZxdgExportedV2,
21 type_: WindowIdentifierType,
22 }
23
24 impl WaylandWindowIdentifier {
25 pub async unsafe fn from_raw(
26 surface_ptr: *mut std::ffi::c_void,
27 display_ptr: *mut std::ffi::c_void,
28 ) -> Option<Self> {
29 if surface_ptr.is_null() || display_ptr.is_null() {
30 return None;
31 }
32
33 let backend = Backend::from_foreign_display(display_ptr as *mut _);
34 let conn = wayland_client::Connection::from_backend(backend);
35 let obj_id = wayland_backend::sys::client::ObjectId::from_ptr(
36 WlSurface::interface(),
37 surface_ptr as *mut _,
38 )
39 .ok()?;
40
41 let surface = WlSurface::from_id(&conn, obj_id).ok()?;
42
43 Self::new_inner(conn, &surface).await
44 }
45
46 async fn new_inner(conn: wayland_client::Connection, surface: &WlSurface) -> Option<Self> {
47 let (sender, receiver) = crate::oneshot::channel::<Option<WaylandWindowIdentifier>>();
48
49 let surface = surface.clone();
50 std::thread::spawn(move || match wayland_export_handle(conn, &surface) {
51 Some(window_handle) => sender.send(Some(window_handle)).unwrap(),
52 None => {
53 sender.send(None).unwrap();
54 }
55 });
56
57 receiver.await.unwrap()
58 }
59 }
60
61 impl fmt::Display for WaylandWindowIdentifier {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 self.type_.fmt(f)
64 }
65 }
66
67 impl Drop for WaylandWindowIdentifier {
68 fn drop(&mut self) {
69 self.exported.destroy();
70 }
71 }
72
73 #[derive(Default, Debug)]
74 struct State {
75 handle: String,
76 exporter: Option<ZxdgExporterV2>,
77 }
78
79 impl wayland_client::Dispatch<ZxdgExportedV2, ()> for State {
80 fn event(
81 state: &mut Self,
82 _proxy: &ZxdgExportedV2,
83 event: <ZxdgExportedV2 as Proxy>::Event,
84 _data: &(),
85 _connhandle: &wayland_client::Connection,
86 _qhandle: &QueueHandle<Self>,
87 ) {
88 if let zxdg_exported_v2::Event::Handle { handle } = event {
89 state.handle = handle;
90 }
91 }
92 }
93
94 impl wayland_client::Dispatch<ZxdgExporterV2, ()> for State {
95 fn event(
96 _state: &mut Self,
97 _proxy: &ZxdgExporterV2,
98 _event: <ZxdgExporterV2 as Proxy>::Event,
99 _data: &(),
100 _connhandle: &wayland_client::Connection,
101 _qhandle: &QueueHandle<Self>,
102 ) {
103 }
104 }
105
106 impl wayland_client::Dispatch<wl_registry::WlRegistry, ()> for State {
107 fn event(
108 state: &mut Self,
109 registry: &wl_registry::WlRegistry,
110 event: wl_registry::Event,
111 _: &(),
112 _: &wayland_client::Connection,
113 qhandle: &QueueHandle<Self>,
114 ) {
115 if let wl_registry::Event::Global {
116 name,
117 interface,
118 version,
119 } = event
120 {
121 if interface.as_str() == "zxdg_exporter_v2" {
122 let exporter = registry.bind::<ZxdgExporterV2, (), State>(
123 name,
124 version.min(ZXDG_EXPORTER_V2),
125 qhandle,
126 (),
127 );
128 state.exporter = Some(exporter);
129 }
130 }
131 }
132 }
133
134 fn wayland_export_handle(
135 conn: wayland_client::Connection,
136 surface: &WlSurface,
137 ) -> Option<WaylandWindowIdentifier> {
138 let display = conn.display();
139 let mut event_queue = conn.new_event_queue();
140 let qhandle = event_queue.handle();
141 let mut state = State::default();
142 display.get_registry(&qhandle, ());
143 event_queue.roundtrip(&mut state).ok()?;
144
145 let exported = match state.exporter.take() {
146 Some(exporter) => {
147 let exp = exporter.export_toplevel(surface, &qhandle, ());
148 event_queue.roundtrip(&mut state).ok()?;
149 exporter.destroy();
150
151 Some(exp)
152 }
153 None => None,
154 };
155
156 if let Some(exported) = exported {
157 Some(WaylandWindowIdentifier {
158 exported,
159 type_: WindowIdentifierType::Wayland(state.handle),
160 })
161 } else {
162 log::warn!("Wayland compositor did not reposne with xdg foreign token");
163 None
164 }
165 }