lune-tty 0.1.0

A GPU-accelerated Wayland terminal emulator for Linux
Documentation
//! Optional native Wayland `xdg_activation_v1` integration for raising the
//! window when a desktop notification is activated.

use wayland_client::backend::{Backend, ObjectId};
use wayland_client::globals::{GlobalListContents, registry_queue_init};
use wayland_client::protocol::{wl_registry, wl_surface};
use wayland_client::{Connection, Dispatch, EventQueue, Proxy, QueueHandle, delegate_noop};
use wayland_protocols::xdg::activation::v1::client::xdg_activation_v1::XdgActivationV1;
use winit::raw_window_handle::{
    HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use winit::window::Window;

#[derive(Default)]
struct ActivationState;

impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for ActivationState {
    fn event(
        _state: &mut Self,
        _proxy: &wl_registry::WlRegistry,
        _event: wl_registry::Event,
        _data: &GlobalListContents,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
    }
}

delegate_noop!(ActivationState: ignore XdgActivationV1);

pub(super) struct WindowActivation {
    connection: Connection,
    _queue: EventQueue<ActivationState>,
    surface: wl_surface::WlSurface,
    activation: XdgActivationV1,
}

impl WindowActivation {
    /// Attaches native Wayland `xdg_activation_v1` support to `window` when
    /// advertised by the compositor.
    pub(super) fn attach(window: &Window) -> Option<Self> {
        let display_handle = window.display_handle().ok()?;
        let RawDisplayHandle::Wayland(wayland_display) = display_handle.as_raw() else {
            return None;
        };
        let window_handle = window.window_handle().ok()?;
        let RawWindowHandle::Wayland(wayland_window) = window_handle.as_raw() else {
            return None;
        };

        // SAFETY: `window` belongs to the active Wayland event loop and owns a live
        // `wl_display` pointer for the entire lifetime of `Client`. `WindowActivation`
        // is declared before `Client.window` so the guest backend drops before `wl_display`.
        let backend =
            unsafe { Backend::from_foreign_display(wayland_display.display.as_ptr().cast()) };
        let connection = Connection::from_backend(backend);
        let (globals, queue) = registry_queue_init::<ActivationState>(&connection).ok()?;
        let qh = queue.handle();

        // SAFETY: `wayland_window.surface` is the live `wl_surface` owned by `window`
        // on the same `wl_display` wrapped by `connection`.
        let surface_id = unsafe {
            ObjectId::from_ptr(
                wl_surface::WlSurface::interface(),
                wayland_window.surface.as_ptr().cast(),
            )
        }
        .ok()?;
        let surface = wl_surface::WlSurface::from_id(&connection, surface_id).ok()?;

        let Ok(activation) = globals.bind::<XdgActivationV1, _, _>(&qh, 1..=1, ()) else {
            log::debug!("Compositor does not advertise Wayland xdg_activation_v1");
            return None;
        };

        log::debug!("Attached Wayland xdg_activation_v1");
        Some(Self {
            connection,
            _queue: queue,
            surface,
            activation,
        })
    }

    pub(super) fn activate(&self, token: &str) -> bool {
        if token.is_empty() {
            return false;
        }
        self.activation.activate(token.to_owned(), &self.surface);
        if let Err(error) = self.connection.flush() {
            log::debug!("Failed to flush xdg_activation_v1 request: {error}");
            return false;
        }
        true
    }
}

impl Drop for WindowActivation {
    fn drop(&mut self) {
        self.activation.destroy();
        let _ = self.connection.flush();
    }
}