| 1 |
use std::fmt; |
| 2 |
|
| 3 |
use wayland_backend::sys::client::Backend; |
| 4 |
use wayland_client::{ |
| 5 |
Proxy, QueueHandle, |
| 6 |
protocol::{wl_registry, wl_surface::WlSurface}, |
| 7 |
}; |
| 8 |
use wayland_protocols::xdg::foreign::{ |
| 9 |
zv1::client::{ |
| 10 |
zxdg_exported_v1::{self, ZxdgExportedV1}, |
| 11 |
zxdg_exporter_v1::ZxdgExporterV1, |
| 12 |
}, |
| 13 |
zv2::client::{ |
| 14 |
zxdg_exported_v2::{self, ZxdgExportedV2}, |
| 15 |
zxdg_exporter_v2::ZxdgExporterV2, |
| 16 |
}, |
| 17 |
}; |
| 18 |
|
| 19 |
use super::WindowIdentifierType; |
| 20 |
|
| 21 |
// Supported versions. |
| 22 |
const ZXDG_EXPORTER_V1: u32 = 1; |
| 23 |
const ZXDG_EXPORTER_V2: u32 = 1; |
| 24 |
|
| 25 |
#[derive(Debug)] |
| 26 |
pub struct WaylandWindowIdentifier { |
| 27 |
exported: Exported, |
| 28 |
type_: WindowIdentifierType, |
| 29 |
} |
| 30 |
|
| 31 |
#[derive(Debug)] |
| 32 |
enum Exported { |
| 33 |
V1(ZxdgExportedV1), |
| 34 |
V2(ZxdgExportedV2), |
| 35 |
} |
| 36 |
|
| 37 |
impl Exported { |
| 38 |
fn destroy(&self) { |
| 39 |
match self { |
| 40 |
Self::V1(exported) => exported.destroy(), |
| 41 |
Self::V2(exported) => exported.destroy(), |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#[derive(Debug)] |
| 47 |
enum Exporter { |
| 48 |
V1(ZxdgExporterV1), |
| 49 |
V2(ZxdgExporterV2), |
| 50 |
} |
| 51 |
|
| 52 |
impl WaylandWindowIdentifier { |
| 53 |
pub async fn new(surface: &WlSurface) -> Option<Self> { |
| 54 |
let backend = surface.backend().upgrade()?; |
| 55 |
let conn = wayland_client::Connection::from_backend(backend); |
| 56 |
|
| 57 |
Self::new_inner(conn, surface).await |
| 58 |
} |
| 59 |
|
| 60 |
pub async unsafe fn from_raw( |
| 61 |
surface_ptr: *mut std::ffi::c_void, |
| 62 |
display_ptr: *mut std::ffi::c_void, |
| 63 |
) -> Option<Self> { |
| 64 |
if surface_ptr.is_null() || display_ptr.is_null() { |
| 65 |
return None; |
| 66 |
} |
| 67 |
|
| 68 |
let backend = unsafe { Backend::from_foreign_display(display_ptr as *mut _) }; |
| 69 |
let conn = wayland_client::Connection::from_backend(backend); |
| 70 |
let obj_id = unsafe { |
| 71 |
wayland_backend::sys::client::ObjectId::from_ptr( |
| 72 |
WlSurface::interface(), |
| 73 |
surface_ptr as *mut _, |
| 74 |
) |
| 75 |
} |
| 76 |
.ok()?; |
| 77 |
|
| 78 |
let surface = WlSurface::from_id(&conn, obj_id).ok()?; |
| 79 |
|
| 80 |
Self::new_inner(conn, &surface).await |
| 81 |
} |
| 82 |
|
| 83 |
async fn new_inner(conn: wayland_client::Connection, surface: &WlSurface) -> Option<Self> { |
| 84 |
let (sender, receiver) = |
| 85 |
futures_channel::oneshot::channel::<Option<WaylandWindowIdentifier>>(); |
| 86 |
|
| 87 |
// Cheap clone, protocol objects are essentially smart pointers |
| 88 |
let surface = surface.clone(); |
| 89 |
std::thread::spawn(move || match wayland_export_handle(conn, &surface) { |
| 90 |
Ok(window_handle) => sender.send(Some(window_handle)).unwrap(), |
| 91 |
Err(_err) => { |
| 92 |
#[cfg(feature = "tracing")] |
| 93 |
tracing::info!("Could not get wayland window identifier: {_err}"); |
| 94 |
sender.send(None).unwrap(); |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
receiver.await.unwrap() |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl fmt::Display for WaylandWindowIdentifier { |
| 103 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 104 |
self.type_.fmt(f) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
impl Drop for WaylandWindowIdentifier { |
| 109 |
fn drop(&mut self) { |
| 110 |
self.exported.destroy(); |
| 111 |
#[cfg(feature = "tracing")] |
| 112 |
if let WindowIdentifierType::Wayland(ref handle) = self.type_ { |
| 113 |
tracing::debug!("Unexporting handle: {handle}"); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[derive(Default, Debug)] |
| 119 |
struct State { |
| 120 |
handle: String, |
| 121 |
exporter: Option<Exporter>, |
| 122 |
} |
| 123 |
|
| 124 |
impl wayland_client::Dispatch<ZxdgExportedV1, ()> for State { |
| 125 |
fn event( |
| 126 |
state: &mut Self, |
| 127 |
_proxy: &ZxdgExportedV1, |
| 128 |
event: <ZxdgExportedV1 as Proxy>::Event, |
| 129 |
_data: &(), |
| 130 |
_connhandle: &wayland_client::Connection, |
| 131 |
_qhandle: &QueueHandle<Self>, |
| 132 |
) { |
| 133 |
if let zxdg_exported_v1::Event::Handle { handle } = event { |
| 134 |
state.handle = handle; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl wayland_client::Dispatch<ZxdgExportedV2, ()> for State { |
| 140 |
fn event( |
| 141 |
state: &mut Self, |
| 142 |
_proxy: &ZxdgExportedV2, |
| 143 |
event: <ZxdgExportedV2 as Proxy>::Event, |
| 144 |
_data: &(), |
| 145 |
_connhandle: &wayland_client::Connection, |
| 146 |
_qhandle: &QueueHandle<Self>, |
| 147 |
) { |
| 148 |
if let zxdg_exported_v2::Event::Handle { handle } = event { |
| 149 |
state.handle = handle; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl wayland_client::Dispatch<ZxdgExporterV1, ()> for State { |
| 155 |
fn event( |
| 156 |
_state: &mut Self, |
| 157 |
_proxy: &ZxdgExporterV1, |
| 158 |
_event: <ZxdgExporterV1 as Proxy>::Event, |
| 159 |
_data: &(), |
| 160 |
_connhandle: &wayland_client::Connection, |
| 161 |
_qhandle: &QueueHandle<Self>, |
| 162 |
) { |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
impl wayland_client::Dispatch<ZxdgExporterV2, ()> for State { |
| 167 |
fn event( |
| 168 |
_state: &mut Self, |
| 169 |
_proxy: &ZxdgExporterV2, |
| 170 |
_event: <ZxdgExporterV2 as Proxy>::Event, |
| 171 |
_data: &(), |
| 172 |
_connhandle: &wayland_client::Connection, |
| 173 |
_qhandle: &QueueHandle<Self>, |
| 174 |
) { |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
impl wayland_client::Dispatch<wl_registry::WlRegistry, ()> for State { |
| 179 |
fn event( |
| 180 |
state: &mut Self, |
| 181 |
registry: &wl_registry::WlRegistry, |
| 182 |
event: wl_registry::Event, |
| 183 |
_: &(), |
| 184 |
_: &wayland_client::Connection, |
| 185 |
qhandle: &QueueHandle<Self>, |
| 186 |
) { |
| 187 |
if let wl_registry::Event::Global { |
| 188 |
name, |
| 189 |
interface, |
| 190 |
version, |
| 191 |
} = event |
| 192 |
{ |
| 193 |
match interface.as_str() { |
| 194 |
"zxdg_exporter_v1" => { |
| 195 |
#[cfg(feature = "tracing")] |
| 196 |
tracing::info!("Found wayland interface {interface} v{version}"); |
| 197 |
let exporter = registry.bind::<ZxdgExporterV1, (), State>( |
| 198 |
name, |
| 199 |
version.min(ZXDG_EXPORTER_V1), |
| 200 |
qhandle, |
| 201 |
(), |
| 202 |
); |
| 203 |
match state.exporter { |
| 204 |
Some(Exporter::V2(_)) => (), |
| 205 |
_ => state.exporter = Some(Exporter::V1(exporter)), |
| 206 |
} |
| 207 |
} |
| 208 |
"zxdg_exporter_v2" => { |
| 209 |
#[cfg(feature = "tracing")] |
| 210 |
tracing::info!("Found wayland interface {interface} v{version}"); |
| 211 |
let exporter = registry.bind::<ZxdgExporterV2, (), State>( |
| 212 |
name, |
| 213 |
version.min(ZXDG_EXPORTER_V2), |
| 214 |
qhandle, |
| 215 |
(), |
| 216 |
); |
| 217 |
state.exporter = Some(Exporter::V2(exporter)); |
| 218 |
} |
| 219 |
_ => (), |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/// A helper to export a wayland handle from a surface and a connection |
| 226 |
/// |
| 227 |
/// Needed for converting a RawWindowHandle to a WindowIdentifier. |
| 228 |
fn wayland_export_handle( |
| 229 |
conn: wayland_client::Connection, |
| 230 |
surface: &WlSurface, |
| 231 |
) -> Result<WaylandWindowIdentifier, Box<dyn std::error::Error>> { |
| 232 |
let display = conn.display(); |
| 233 |
let mut event_queue = conn.new_event_queue(); |
| 234 |
let qhandle = event_queue.handle(); |
| 235 |
let mut state = State::default(); |
| 236 |
display.get_registry(&qhandle, ()); |
| 237 |
event_queue.roundtrip(&mut state)?; |
| 238 |
|
| 239 |
let exported = match state.exporter.take() { |
| 240 |
Some(Exporter::V2(exporter)) => { |
| 241 |
let exp = exporter.export_toplevel(surface, &qhandle, ()); |
| 242 |
event_queue.roundtrip(&mut state)?; |
| 243 |
exporter.destroy(); |
| 244 |
|
| 245 |
Some(Exported::V2(exp)) |
| 246 |
} |
| 247 |
Some(Exporter::V1(exporter)) => { |
| 248 |
let exp = exporter.export(surface, &qhandle, ()); |
| 249 |
event_queue.roundtrip(&mut state)?; |
| 250 |
exporter.destroy(); |
| 251 |
|
| 252 |
Some(Exported::V1(exp)) |
| 253 |
} |
| 254 |
None => { |
| 255 |
#[cfg(feature = "tracing")] |
| 256 |
tracing::error!( |
| 257 |
"The compositor does not support the zxdg_exporter_v1 nor zxdg_exporter_v2 protocols" |
| 258 |
); |
| 259 |
None |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
if let Some(exported) = exported { |
| 264 |
Ok(WaylandWindowIdentifier { |
| 265 |
exported, |
| 266 |
type_: WindowIdentifierType::Wayland(state.handle), |
| 267 |
}) |
| 268 |
} else { |
| 269 |
Err(Box::new(crate::Error::NoResponse)) |
| 270 |
} |
| 271 |
} |