Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

75.9 KB
Raw Blame
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::cmp::max;
use std::convert::TryInto;
use std::io::Read;
use std::num::NonZeroU32;
use std::os::fd::AsRawFd;
use std::path::PathBuf;
use std::ptr::NonNull;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use anyhow::{anyhow, bail};
use async_io::Timer;
use async_trait::async_trait;
use config::ConfigHandle;
use promise::{Future, Promise};
use raw_window_handle::{
    DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawWindowHandle,
    WaylandWindowHandle, WindowHandle,
};
use smithay_client_toolkit::compositor::{CompositorHandler, SurfaceData, SurfaceDataExt};
use smithay_client_toolkit::data_device_manager::ReadPipe;
use smithay_client_toolkit::globals::GlobalData;
use smithay_client_toolkit::reexports::csd_frame::{
    DecorationsFrame, FrameAction, ResizeEdge, WindowState as SCTKWindowState,
};
use smithay_client_toolkit::reexports::protocols::xdg::shell::client::xdg_toplevel::ResizeEdge as XdgResizeEdge;
use smithay_client_toolkit::seat::pointer::CursorIcon;
use smithay_client_toolkit::shell::xdg::fallback_frame::FallbackFrame;
use smithay_client_toolkit::shell::xdg::window::{
    DecorationMode, Window as XdgWindow, WindowConfigure, WindowDecorations as Decorations,
    WindowHandler,
};
use smithay_client_toolkit::shell::xdg::XdgSurface;
use smithay_client_toolkit::shell::WaylandSurface;
use wayland_client::protocol::wl_callback::WlCallback;
use wayland_client::protocol::wl_keyboard::{Event as WlKeyboardEvent, KeyState};
use wayland_client::protocol::wl_pointer::{ButtonState, WlPointer};
use wayland_client::protocol::wl_region::WlRegion;
use wayland_client::protocol::wl_surface::WlSurface;
use wayland_client::{Connection as WConnection, Dispatch, Proxy, QueueHandle};
use wayland_egl::{is_available as egl_is_available, WlEglSurface};
use wayland_protocols_plasma::blur::client::org_kde_kwin_blur::OrgKdeKwinBlur;
use wayland_protocols_plasma::blur::client::org_kde_kwin_blur_manager::OrgKdeKwinBlurManager;
use wezterm_font::FontConfiguration;
use wezterm_input_types::{
    KeyboardLedStatus, Modifiers, MouseButtons, MouseEvent, MouseEventKind, MousePress,
    PreciseScrollDelta, ScreenPoint, WindowDecorations,
};

use crate::wayland::WaylandConnection;
use crate::x11::KeyboardWithFallback;
use crate::{
    Appearance, Clipboard, ClipboardContents, Connection, ConnectionOps, Dimensions, MouseCursor,
    Point, Rect, RequestedWindowGeometry, ResizeIncrement, ResolvedGeometry, Window, WindowEvent,
    WindowEventSender, WindowKeyEvent, WindowOps, WindowState,
};

/// Wayland-specific coordinate conversion methods for Dimensions
trait WaylandDimensions {
    fn dpi_factor(&self) -> f64;
    fn pixels_to_surface(&self, pixels: i32) -> i32;
    fn surface_to_pixels(&self, surface: i32) -> i32;
}

impl WaylandDimensions for Dimensions {
    fn dpi_factor(&self) -> f64 {
        self.dpi as f64 / crate::DEFAULT_DPI as f64
    }

    fn pixels_to_surface(&self, pixels: i32) -> i32 {
        // Take care to round up, otherwise we can lose a pixel
        // and that can effectively lose the final row of the terminal
        (pixels as f64 / self.dpi_factor()).ceil() as i32
    }

    fn surface_to_pixels(&self, surface: i32) -> i32 {
        (surface as f64 * self.dpi_factor()).ceil() as i32
    }
}

const WAYLAND_RESIZE_EDGE_LOGICAL_PIXELS: f64 = 6.0;

fn requested_wayland_decoration_mode(decorations: WindowDecorations) -> Option<DecorationMode> {
    if decorations == WindowDecorations::NONE {
        None
    } else if decorations.contains(WindowDecorations::INTEGRATED_BUTTONS) {
        Some(DecorationMode::Client)
    } else if decorations == WindowDecorations::default() {
        Some(DecorationMode::Server)
    } else {
        Some(DecorationMode::Client)
    }
}

fn wayland_fallback_frame_visible(
    decorations: WindowDecorations,
    effective_mode: DecorationMode,
) -> bool {
    effective_mode == DecorationMode::Client && decorations.shows_separate_title_bar()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WaylandSurfaceResizeEdge {
    TopLeft,
    Top,
    TopRight,
    Right,
    BottomRight,
    Bottom,
    BottomLeft,
    Left,
}

impl WaylandSurfaceResizeEdge {
    fn cursor(self) -> MouseCursor {
        match self {
            Self::Top | Self::Bottom => MouseCursor::SizeUpDown,
            Self::Left | Self::Right => MouseCursor::SizeLeftRight,
            Self::TopLeft | Self::BottomRight => MouseCursor::SizeNorthWestSouthEast,
            Self::TopRight | Self::BottomLeft => MouseCursor::SizeNorthEastSouthWest,
        }
    }

    fn xdg_edge(self) -> XdgResizeEdge {
        match self {
            Self::TopLeft => XdgResizeEdge::TopLeft,
            Self::Top => XdgResizeEdge::Top,
            Self::TopRight => XdgResizeEdge::TopRight,
            Self::Right => XdgResizeEdge::Right,
            Self::BottomRight => XdgResizeEdge::BottomRight,
            Self::Bottom => XdgResizeEdge::Bottom,
            Self::BottomLeft => XdgResizeEdge::BottomLeft,
            Self::Left => XdgResizeEdge::Left,
        }
    }
}

fn wayland_surface_resize_edge_at(
    x: isize,
    y: isize,
    width: usize,
    height: usize,
    edge_pixels: usize,
) -> Option<WaylandSurfaceResizeEdge> {
    if x < 0 || y < 0 || x as usize >= width || y as usize >= height || width == 0 || height == 0 {
        return None;
    }

    let x = x as usize;
    let y = y as usize;
    let edge_x = edge_pixels.max(1).min((width / 2).max(1));
    let edge_y = edge_pixels.max(1).min((height / 2).max(1));
    let left = x < edge_x;
    let right = x >= width - edge_x;
    let top = y < edge_y;
    let bottom = y >= height - edge_y;

    match (left, right, top, bottom) {
        (true, _, true, _) => Some(WaylandSurfaceResizeEdge::TopLeft),
        (_, true, true, _) => Some(WaylandSurfaceResizeEdge::TopRight),
        (true, _, _, true) => Some(WaylandSurfaceResizeEdge::BottomLeft),
        (_, true, _, true) => Some(WaylandSurfaceResizeEdge::BottomRight),
        (_, _, true, _) => Some(WaylandSurfaceResizeEdge::Top),
        (_, _, _, true) => Some(WaylandSurfaceResizeEdge::Bottom),
        (true, _, _, _) => Some(WaylandSurfaceResizeEdge::Left),
        (_, true, _, _) => Some(WaylandSurfaceResizeEdge::Right),
        _ => None,
    }
}

use super::copy_and_paste::{ClipboardOfferKind, CopyAndPaste};
use super::pointer::{PendingMouse, PointerUserData};
use super::state::WaylandState;

#[derive(Debug)]
pub(super) struct KeyRepeatState {
    pub(super) when: Instant,
    pub(super) event: WindowKeyEvent,
}

impl KeyRepeatState {
    pub(super) fn schedule(state: Arc<Mutex<Self>>, window_id: usize) {
        promise::spawn::spawn_into_main_thread(async move {
            let delay;
            let gap;
            {
                let conn = WaylandConnection::get().unwrap().wayland();
                let (rate, ddelay) = {
                    let wstate = conn.wayland_state.borrow();
                    (
                        wstate.key_repeat_rate as u64,
                        wstate.key_repeat_delay as u64,
                    )
                };
                if rate == 0 {
                    return;
                }
                delay = Duration::from_millis(ddelay);
                gap = Duration::from_millis(1000 / rate);
            }

            let mut initial = true;
            Timer::after(delay).await;
            loop {
                {
                    let handle = {
                        let conn = WaylandConnection::get().unwrap().wayland();
                        match conn.window_by_id(window_id) {
                            Some(handle) => handle,
                            None => return,
                        }
                    };

                    let mut inner = handle.borrow_mut();

                    if inner.key_repeat.as_ref().map(|(_, k)| Arc::as_ptr(k))
                        != Some(Arc::as_ptr(&state))
                    {
                        // Key was released and/or some other key is doing
                        // its own repetition now
                        return;
                    }

                    let mut st = state.lock().unwrap();

                    let mut repeat_count = 1;

                    let mut elapsed = st.when.elapsed();
                    if initial {
                        elapsed -= delay;
                        initial = false;
                    }

                    // If our scheduling interval is longer than the repeat
                    // gap, we need to inflate the repeat count to match
                    // the intended rate
                    while elapsed >= gap {
                        repeat_count += 1;
                        elapsed -= gap;
                    }

                    let event = match st.event.clone() {
                        WindowKeyEvent::KeyEvent(mut key) => {
                            key.repeat_count = repeat_count;
                            WindowEvent::KeyEvent(key)
                        }
                        WindowKeyEvent::RawKeyEvent(mut raw) => {
                            raw.repeat_count = repeat_count;
                            WindowEvent::RawKeyEvent(raw)
                        }
                    };

                    inner.events.dispatch(event);

                    st.when = Instant::now();
                }

                Timer::after(gap).await;
            }
        })
        .detach();
    }
}

enum WaylandWindowEvent {
    Close,
    Request(WindowConfigure),
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct WaylandWindow(usize);

impl WaylandWindow {
    pub async fn new_window<F>(
        class_name: &str,
        name: &str,
        geometry: RequestedWindowGeometry,
        config: Option<&ConfigHandle>,
        _font_config: Rc<FontConfiguration>,
        event_handler: F,
    ) -> anyhow::Result<Window>
    where
        F: 'static + FnMut(WindowEvent, &Window),
    {
        let config = match config {
            Some(c) => c.clone(),
            None => config::configuration(),
        };

        let conn = WaylandConnection::get()
            .ok_or_else(|| {
                anyhow!(
                    "new_window must be called on the gui thread after Connection:init has succeed",
                )
            })?
            .wayland();

        let window_id = conn.next_window_id();
        let pending_event = Arc::new(Mutex::new(PendingEvent::default()));

        let (pending_first_configure, wait_configure) = async_channel::bounded(1);

        let qh = conn.event_queue.borrow().handle();

        // We need user data so we can get the window_id => WaylandWindowInner during a handler
        let surface_data = SurfaceUserData {
            surface_data: SurfaceData::default(),
            window_id,
        };
        let surface = {
            let compositor = &conn.wayland_state.borrow().compositor;
            compositor.create_surface_with_data(&qh, surface_data)
        };

        let ResolvedGeometry {
            x: _,
            y: _,
            width,
            height,
        } = conn.resolve_geometry(geometry);

        let dimensions = Dimensions {
            pixel_width: width,
            pixel_height: height,
            dpi: config.dpi.unwrap_or(crate::DEFAULT_DPI) as usize,
        };

        let window = {
            let xdg_shell = &conn.wayland_state.borrow().xdg;
            xdg_shell.create_window(surface.clone(), Decorations::RequestServer, &qh)
        };

        window.set_app_id(class_name.to_string());
        window.set_title(name.to_string());
        let decorations = config.window_decorations;
        let decor_mode = requested_wayland_decoration_mode(decorations);
        window.request_decoration_mode(decor_mode);

        let mut window_frame = {
            let wayland_state = &conn.wayland_state.borrow();
            let shm = &wayland_state.shm;
            let subcompositor = wayland_state.subcompositor.clone();
            FallbackFrame::new(&window, shm, subcompositor, qh.clone())
                .expect("failed to create csd frame")
        };
        let hidden = !decor_mode
            .map(|mode| wayland_fallback_frame_visible(decorations, mode))
            .unwrap_or(false);
        window_frame.set_hidden(hidden);
        if !hidden {
            window_frame.resize(
                NonZeroU32::new(dimensions.pixel_width as u32)
                    .ok_or_else(|| anyhow!("dimensions {dimensions:?} are invalid"))?,
                NonZeroU32::new(dimensions.pixel_height as u32)
                    .ok_or_else(|| anyhow!("dimensions {dimensions:?} are invalid"))?,
            );
        }

        window.set_min_size(Some((32, 32)));
        let (x, y) = window_frame.location();
        let surface_width = dimensions.pixels_to_surface(dimensions.pixel_width as i32);
        let surface_height = dimensions.pixels_to_surface(dimensions.pixel_height as i32);
        window
            .xdg_surface()
            .set_window_geometry(x, y, surface_width, surface_height);
        window.commit();

        let copy_and_paste = CopyAndPaste::create();
        let pending_mouse = PendingMouse::create(window_id, &copy_and_paste);

        {
            let surface_to_pending = &mut conn.wayland_state.borrow_mut().surface_to_pending;
            surface_to_pending.insert(surface.id(), Arc::clone(&pending_mouse));
        }

        let appearance = conn.get_appearance();

        let presented_since_frame_request = Rc::new(Cell::new(false));
        let inner = Rc::new(RefCell::new(WaylandWindowInner {
            events: WindowEventSender::new(event_handler),
            surface_factor: 1.0,
            copy_and_paste,
            invalidated: false,
            window: Some(window),
            window_frame,
            dimensions,
            resize_increments: None,
            window_state: WindowState::default(),
            last_mouse_coords: Point::new(0, 0),
            mouse_buttons: MouseButtons::NONE,
            surface_resize_cursor_active: false,
            surface_resize_in_progress: false,
            last_pointer_press_serial: None,
            hscroll_remainder: 0.0,
            vscroll_remainder: 0.0,

            modifiers: Modifiers::NONE,
            leds: KeyboardLedStatus::empty(),

            key_repeat: None,
            pending_event,
            pending_mouse,

            pending_first_configure: Some(pending_first_configure),
            frame_callback: None,
            presented_since_frame_request: Rc::clone(&presented_since_frame_request),
            unpresented_retry_armed: false,
            unpresented_retry_generation: 0,
            unpresented_retries: 0,
            window_id,

            text_cursor: None,
            appearance,

            config,

            title: None,

            wegl_surface: None,
            gl_state: None,
        }));

        let window_handle = Window::Wayland(WaylandWindow(window_id));

        inner
            .borrow_mut()
            .events
            .assign_window(window_handle.clone());

        inner.borrow().update_window_background_blur();

        {
            let windows = &conn.wayland_state.borrow().windows;
            windows.borrow_mut().insert(window_id, inner.clone());
        };

        wait_configure.recv().await?;

        // Nothing presents before the first configure, and a window that
        // never got one leaves no flag behind.
        conn.frame_presented_flags
            .borrow_mut()
            .insert(window_id, presented_since_frame_request);

        Ok(window_handle)
    }
}

#[async_trait(?Send)]
impl WindowOps for WaylandWindow {
    fn show(&self) {
        WaylandConnection::with_window_inner(self.0, |inner| {
            inner.show();
            Ok(())
        });
    }

    fn notify<T: Any + Send + Sync>(&self, t: T)
    where
        Self: Sized,
    {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner
                .events
                .dispatch(WindowEvent::Notification(Box::new(t)));
            Ok(())
        });
    }

    async fn enable_opengl(&self) -> anyhow::Result<Rc<glium::backend::Context>> {
        let window = self.0;
        promise::spawn::spawn(async move {
            if let Some(handle) = Connection::get().unwrap().wayland().window_by_id(window) {
                let mut inner = handle.borrow_mut();
                inner.enable_opengl()
            } else {
                anyhow::bail!("invalid window");
            }
        })
        .await
    }

    fn hide(&self) {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner.window.as_ref().unwrap().set_minimized();
            Ok(())
        });
    }

    fn focus_with_activation_token(&self, token: Option<&str>) {
        // Without a token the compositor would refuse: nothing to ask for.
        let Some(token) = token.map(str::to_string) else {
            return;
        };
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner.activate(token);
            Ok(())
        });
    }

    fn close(&self) {
        WaylandConnection::with_window_inner(self.0, |inner| {
            inner.close();
            Ok(())
        });
    }

    fn set_cursor(&self, cursor: Option<MouseCursor>) {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner.set_cursor(cursor);
            Ok(())
        });
    }

    fn request_drag_move(&self) {
        WaylandConnection::with_window_inner(self.0, |inner| {
            inner.begin_surface_move();
            Ok(())
        });
    }

    fn invalidate(&self) {
        WaylandConnection::with_window_inner(self.0, |inner| {
            inner.invalidate();
            Ok(())
        });
    }

    fn finish_frame(&self, frame: glium::Frame) -> anyhow::Result<()> {
        frame.finish()?;
        self.frame_presented();
        Ok(())
    }

    fn frame_presented(&self) {
        // Set right away, not through with_window_inner: the window's state
        // is borrowed by the repaint reporting this. A closed window has no
        // flag left to set.
        if let Some(conn) = WaylandConnection::get() {
            if let Some(flag) = conn.wayland().frame_presented_flags.borrow().get(&self.0) {
                flag.set(true);
            }
        }
    }

    fn set_text_cursor_position(&self, cursor: Rect) {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner.set_text_cursor_position(cursor);
            Ok(())
        });
    }

    fn set_title(&self, title: &str) {
        let title = title.to_owned();
        WaylandConnection::with_window_inner(self.0, |inner| {
            inner.set_title(title);
            Ok(())
        });
    }

    fn set_inner_size(&self, width: usize, height: usize) {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner.set_inner_size(width, height);
            Ok(())
        });
    }

    fn set_resize_increments(&self, incr: ResizeIncrement) {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner.set_resize_increments(incr)
        });
    }

    fn get_clipboard(&self, clipboard: Clipboard) -> Future<String> {
        let mut promise = Promise::new();
        let future = promise.get_future().unwrap();
        let promise = Arc::new(Mutex::new(promise));
        WaylandConnection::with_window_inner(self.0, move |inner| {
            let read = inner
                .copy_and_paste
                .lock()
                .unwrap()
                .get_clipboard_data(clipboard)?;
            let promise = Arc::clone(&promise);
            std::thread::spawn(move || {
                let mut promise = promise.lock().unwrap();
                match read_pipe_with_timeout(read) {
                    Ok(result) => {
                        // Normalize the text to unix line endings, otherwise
                        // copying from eg: firefox inserts a lot of blank
                        // lines, and that is super annoying.
                        promise.ok(result.replace("\r\n", "\n"));
                    }
                    Err(e) => {
                        log::error!("while reading clipboard: {}", e);
                        promise.err(anyhow!("{}", e));
                    }
                };
            });
            Ok(())
        });
        future
    }

    fn get_clipboard_contents(&self, clipboard: Clipboard) -> Future<ClipboardContents> {
        let mut promise = Promise::new();
        let future = promise.get_future().unwrap();
        let promise = Arc::new(Mutex::new(promise));
        let request = WaylandConnection::with_window_inner(self.0, move |inner| {
            inner
                .copy_and_paste
                .lock()
                .unwrap()
                .get_clipboard_contents_pipe(clipboard, true)
        });
        promise::spawn::spawn(async move {
            let result = match request.await {
                Ok((kind, read, text_fallback)) => {
                    promise::spawn::spawn_into_new_thread(move || {
                        // Drain an advertised text fallback concurrently. Some
                        // clipboard owners do not write the second pipe until the
                        // first has a reader, and either pipe may be large.
                        let fallback_reader = text_fallback.map(|read| {
                            std::thread::spawn(move || read_pipe_bytes_with_timeout(read))
                        });
                        match read_pipe_bytes_with_timeout(read) {
                            Ok(bytes) => {
                                let contents = match kind {
                                    ClipboardOfferKind::UriList => {
                                        let fallback = fallback_reader
                                            .and_then(|reader| reader.join().ok())
                                            .and_then(Result::ok);
                                        crate::os::uri_list::clipboard_contents_from_uri_list(
                                            &bytes,
                                            fallback.as_deref(),
                                        )
                                    }
                                    ClipboardOfferKind::Png => ClipboardContents::Image {
                                        format: crate::ClipboardImageFormat::Png,
                                        bytes,
                                    },
                                    ClipboardOfferKind::Text => ClipboardContents::Text(
                                        // Same unix line-ending normalization as the
                                        // textual read.
                                        String::from_utf8_lossy(&bytes).replace("\r\n", "\n"),
                                    ),
                                };
                                Ok(contents)
                            }
                            Err(e) => {
                                log::error!("while reading clipboard: {}", e);
                                Err(anyhow!("{}", e))
                            }
                        }
                    })
                    .await
                }
                Err(err) => Err(err),
            };
            promise.lock().unwrap().result(result);
        })
        .detach();
        future
    }

    fn set_clipboard(&self, clipboard: Clipboard, text: String) {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner
                .copy_and_paste
                .lock()
                .unwrap()
                .set_clipboard_data(clipboard, text);
            Ok(())
        });
    }

    fn toggle_fullscreen(&self) {
        WaylandConnection::with_window_inner(self.0, move |inner| {
            if inner.window_state.contains(WindowState::FULL_SCREEN) {
                inner.window.as_ref().unwrap().unset_fullscreen();
            } else {
                inner.window.as_ref().unwrap().set_fullscreen(None);
            }
            Ok(())
        });
    }

    fn maximize(&self) {
        WaylandConnection::with_window_inner(self.0, move |inner| Ok(inner.maximize()));
    }

    fn restore(&self) {
        WaylandConnection::with_window_inner(self.0, move |inner| Ok(inner.restore()));
    }

    fn config_did_change(&self, config: &ConfigHandle) {
        let config = config.clone();
        WaylandConnection::with_window_inner(self.0, move |inner| {
            inner.config_did_change(config);
            Ok(())
        });
    }
}
#[derive(Default, Clone, Debug)]
pub(crate) struct PendingEvent {
    pub(crate) close: bool,
    pub(crate) had_configure_event: bool,
    refresh_decorations: bool,
    // XXX: configure and window_configure could probably be combined, but right now configure only
    // queues a new size, so it can be out of sync. Example would be maximizing and minimizing winodw
    pub(crate) configure: Option<(u32, u32)>,
    pub(crate) window_configure: Option<WindowConfigure>,
    pub(crate) dpi: Option<i32>,
    pub(crate) window_state: Option<WindowState>,
}

pub(crate) fn read_pipe_with_timeout(file: ReadPipe) -> anyhow::Result<String> {
    Ok(String::from_utf8(read_pipe_bytes_with_timeout(file)?)?)
}

pub(crate) fn read_pipe_bytes_with_timeout(mut file: ReadPipe) -> anyhow::Result<Vec<u8>> {
    let mut result = Vec::new();

    // set non-blocking I/O on the pipe
    // (adapted from FileDescriptor::set_non_blocking_impl in /filedescriptor/src/unix.rs)
    if unsafe { libc::fcntl(file.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK) } != 0 {
        bail!(
            "failed to change non-blocking mode: {}",
            std::io::Error::last_os_error()
        )
    }

    let mut pfd = libc::pollfd {
        fd: file.as_raw_fd(),
        events: libc::POLLIN,
        revents: 0,
    };

    let mut buf = [0u8; 8192];

    loop {
        if unsafe { libc::poll(&mut pfd, 1, 3000) == 1 } {
            match file.read(&mut buf) {
                Ok(size) if size == 0 => {
                    break;
                }
                Ok(size) => {
                    result.extend_from_slice(&buf[..size]);
                }
                Err(e) => bail!("error reading from pipe: {}", e),
            }
        } else {
            bail!("timed out reading from pipe");
        }
    }

    Ok(result)
}

pub struct WaylandWindowInner {
    pub(crate) events: WindowEventSender,
    surface_factor: f64,
    copy_and_paste: Arc<Mutex<CopyAndPaste>>,
    window: Option<XdgWindow>,
    pub(super) window_frame: FallbackFrame<WaylandState>,
    dimensions: Dimensions,
    resize_increments: Option<ResizeIncrement>,
    window_state: WindowState,
    last_mouse_coords: Point,
    mouse_buttons: MouseButtons,
    surface_resize_cursor_active: bool,
    surface_resize_in_progress: bool,
    last_pointer_press_serial: Option<u32>,
    hscroll_remainder: f64,
    vscroll_remainder: f64,
    modifiers: Modifiers,
    leds: KeyboardLedStatus,
    pub(super) key_repeat: Option<(u32, Arc<Mutex<KeyRepeatState>>)>,
    pub(super) pending_event: Arc<Mutex<PendingEvent>>,
    pub(super) pending_mouse: Arc<Mutex<PendingMouse>>,
    pending_first_configure: Option<async_channel::Sender<()>>,
    frame_callback: Option<WlCallback>,
    /// Set by WindowOps::frame_presented and cleared when do_paint asks for
    /// a frame callback: until it is set again, no commit has applied that
    /// request and the callback cannot fire.
    presented_since_frame_request: Rc<Cell<bool>>,
    /// A retry of a repaint that presented nothing is on its way; see
    /// schedule_unpresented_retry.
    unpresented_retry_armed: bool,
    /// Identifies the armed retry, so that one disowned when a present got
    /// through does nothing when its timer fires.
    unpresented_retry_generation: u64,
    /// Retries since the last present; each one doubles the next delay.
    unpresented_retries: u32,
    window_id: usize,
    invalidated: bool,
    // font_config: Rc<FontConfiguration>,
    text_cursor: Option<Rect>,
    appearance: Appearance,
    config: ConfigHandle,
    // cache the title for comparison to avoid spamming
    // the compositor with updates that don't actually change it
    title: Option<String>,
    // wegl_surface is listed before gl_state because it
    // must be dropped before gl_state otherwise the underlying
    // libraries will segfault on shutdown
    wegl_surface: Option<WlEglSurface>,
    gl_state: Option<Rc<glium::backend::Context>>,
}

impl WaylandWindowInner {
    fn close(&mut self) {
        self.events.dispatch(WindowEvent::Destroyed);
        self.window.take();
        WaylandConnection::get()
            .unwrap()
            .wayland()
            .frame_presented_flags
            .borrow_mut()
            .remove(&self.window_id);
    }

    fn activate(&mut self, token: String) {
        let Some(window) = self.window.as_ref() else {
            return;
        };
        let conn = Connection::get().unwrap().wayland();
        let state = conn.wayland_state.borrow();
        match state.activation.as_ref() {
            Some(activation) => activation.activate::<WaylandState>(window.wl_surface(), token),
            None => log::debug!("the compositor has no xdg-activation to come forward with"),
        }
    }

    fn show(&mut self) {
        log::trace!("WaylandWindowInner show: {:?}", self.window);
        if self.window.is_none() {
            return;
        }

        // If the do_paint function has been called previously, calling it again will not
        // send the NeedRepaint event. This results in the window not being displayed
        // correctly.
        // Therefore, when frame_callback is set to some, we need to send the NeedRepaint
        // event again to ensure the window is displayed.
        // Fix: https://github.com/wezterm/wezterm/issues/5103
        if self.frame_callback.is_some() {
            self.events.dispatch(WindowEvent::NeedRepaint);
        }

        self.do_paint().unwrap();
    }

    fn apply_fallback_frame_visibility(&mut self, effective_mode: DecorationMode) {
        let visible =
            wayland_fallback_frame_visible(self.config.window_decorations, effective_mode);
        self.window_frame.set_hidden(!visible);
        if visible {
            let width = NonZeroU32::new(
                self.pixels_to_surface(self.dimensions.pixel_width as i32)
                    .max(1) as u32,
            )
            .unwrap();
            let height = NonZeroU32::new(
                self.pixels_to_surface(self.dimensions.pixel_height as i32)
                    .max(1) as u32,
            )
            .unwrap();
            self.window_frame.resize(width, height);
        }
        if self.window.is_some() {
            let (x, y) = self.window_frame.location();
            let surface_width = self.pixels_to_surface(self.dimensions.pixel_width as i32);
            let surface_height = self.pixels_to_surface(self.dimensions.pixel_height as i32);
            self.window
                .as_mut()
                .unwrap()
                .xdg_surface()
                .set_window_geometry(x, y, surface_width, surface_height);
        }
    }

    fn surface_resize_edge_at(&self, coords: Point) -> Option<WaylandSurfaceResizeEdge> {
        if !self
            .config
            .window_decorations
            .contains(WindowDecorations::RESIZE)
            || !self.window_state.can_resize()
            || self.window_state.contains(WindowState::SERVER_DECORATED)
            || !self.window_frame.is_hidden()
        {
            return None;
        }

        let edge_pixels = (WAYLAND_RESIZE_EDGE_LOGICAL_PIXELS * self.dimensions.dpi as f64
            / crate::DEFAULT_DPI)
            .round() as usize;
        wayland_surface_resize_edge_at(
            coords.x,
            coords.y,
            self.dimensions.pixel_width,
            self.dimensions.pixel_height,
            edge_pixels,
        )
    }

    fn begin_surface_resize(&mut self, serial: u32, edge: WaylandSurfaceResizeEdge) {
        let conn = Connection::get().unwrap().wayland();
        let state = conn.wayland_state.borrow();
        let Some(seat) = state.pointer_seat.as_ref() else {
            return;
        };
        if let Some(window) = self.window.as_ref() {
            window.resize(seat, serial, edge.xdg_edge());
            self.surface_resize_in_progress = true;
        }
    }

    fn begin_surface_move(&mut self) {
        let Some(serial) = self.last_pointer_press_serial.take() else {
            return;
        };
        let conn = Connection::get().unwrap().wayland();
        let state = conn.wayland_state.borrow();
        let Some(seat) = state.pointer_seat.as_ref() else {
            return;
        };
        if let Some(window) = self.window.as_ref() {
            window.move_(seat, serial);
        }
    }

    fn refresh_frame(&mut self) {
        if self.window_frame.is_dirty() && !self.window_frame.is_hidden() {
            self.window_frame.draw();
        }
    }

    fn enable_opengl(&mut self) -> anyhow::Result<Rc<glium::backend::Context>> {
        let wayland_conn = Connection::get().unwrap().wayland();
        let mut wegl_surface = None;

        log::trace!("Enable opengl");

        let gl_state = if !egl_is_available() {
            Err(anyhow!("!egl_is_available"))
        } else {
            let window = self
                .window
                .as_ref()
                .ok_or(anyhow!("Window does not exist"))?;
            let object_id = window.wl_surface().id();

            wegl_surface = Some(WlEglSurface::new(
                object_id,
                self.dimensions.pixel_width as i32,
                self.dimensions.pixel_height as i32,
            )?);

            log::trace!("WEGL Surface here {:?}", wegl_surface);

            match wayland_conn.gl_connection.borrow().as_ref() {
                Some(glconn) => crate::egl::GlState::create_wayland_with_existing_connection(
                    glconn,
                    wegl_surface.as_ref().unwrap(),
                ),
                None => crate::egl::GlState::create_wayland(
                    Some(wayland_conn.connection.backend().display_ptr() as *const _),
                    wegl_surface.as_ref().unwrap(),
                ),
            }
        };
        let gl_state = gl_state.map(Rc::new).and_then(|state| unsafe {
            wayland_conn
                .gl_connection
                .borrow_mut()
                .replace(Rc::clone(state.get_connection()));
            Ok(glium::backend::Context::new(
                Rc::clone(&state),
                true,
                if cfg!(debug_assertions) {
                    glium::debug::DebugCallbackBehavior::DebugMessageOnError
                } else {
                    glium::debug::DebugCallbackBehavior::Ignore
                },
            )?)
        })?;

        self.gl_state.replace(gl_state.clone());
        self.wegl_surface = wegl_surface;

        Ok(gl_state)
    }

    fn get_dpi_factor(&self) -> f64 {
        self.dimensions.dpi_factor()
    }

    fn surface_to_pixels(&self, surface: i32) -> i32 {
        self.dimensions.surface_to_pixels(surface)
    }

    fn pixels_to_surface(&self, pixels: i32) -> i32 {
        self.dimensions.pixels_to_surface(pixels)
    }

    pub(super) fn dispatch_dropped_files(&mut self, paths: Vec<PathBuf>) {
        // The Wayland data-device plumbing here does not carry the drop
        // position through to this point, so the drop keeps its long-standing
        // behaviour of pasting the paths into the terminal.
        self.events.dispatch(WindowEvent::DroppedFile {
            paths,
            coords: None,
        });
    }

    pub(crate) fn dispatch_pending_mouse(&mut self) {
        let pending_mouse = Arc::clone(&self.pending_mouse);

        if let Some((x, y)) = PendingMouse::coords(&pending_mouse) {
            let coords = Point::new(
                self.surface_to_pixels(x as i32) as isize,
                self.surface_to_pixels(y as i32) as isize,
            );
            self.last_mouse_coords = coords;
            if let Some(edge) = self.surface_resize_edge_at(coords) {
                self.set_cursor(Some(edge.cursor()));
                self.surface_resize_cursor_active = true;
            } else {
                if self.surface_resize_cursor_active {
                    // None hides the pointer entirely; restore the arrow and
                    // let the application pick a more specific cursor on the
                    // next dispatched motion.
                    self.set_cursor(Some(MouseCursor::Arrow));
                    self.surface_resize_cursor_active = false;
                }
                let event = MouseEvent {
                    kind: MouseEventKind::Move,
                    coords,
                    screen_coords: ScreenPoint::new(
                        coords.x + self.dimensions.pixel_width as isize,
                        coords.y + self.dimensions.pixel_height as isize,
                    ),
                    mouse_buttons: self.mouse_buttons,
                    modifiers: self.modifiers,
                    precise_scroll_delta: None,
                    scroll_phase: None,
                    momentum_phase: None,
                    precise_wheel_lines: None,
                };
                self.events.dispatch(WindowEvent::MouseEvent(event));
            }
            self.refresh_frame();
        }

        while let Some((button, state, serial)) = PendingMouse::next_button(&pending_mouse) {
            if button == MousePress::Left && self.surface_resize_in_progress {
                if state == ButtonState::Released {
                    self.surface_resize_in_progress = false;
                }
                continue;
            }
            if button == MousePress::Left && state == ButtonState::Pressed {
                if let Some(edge) = self.surface_resize_edge_at(self.last_mouse_coords) {
                    self.begin_surface_resize(serial, edge);
                    continue;
                }
                self.last_pointer_press_serial = Some(serial);
            } else if button == MousePress::Left && state == ButtonState::Released {
                self.last_pointer_press_serial = None;
            }

            let button_mask = match button {
                MousePress::Left => MouseButtons::LEFT,
                MousePress::Right => MouseButtons::RIGHT,
                MousePress::Middle => MouseButtons::MIDDLE,
            };

            if state == ButtonState::Pressed {
                self.mouse_buttons |= button_mask;
            } else {
                self.mouse_buttons -= button_mask;
            }

            let event = MouseEvent {
                kind: match state {
                    ButtonState::Pressed => MouseEventKind::Press(button),
                    ButtonState::Released => MouseEventKind::Release(button),
                    _ => continue,
                },
                coords: self.last_mouse_coords,
                screen_coords: ScreenPoint::new(
                    self.last_mouse_coords.x + self.dimensions.pixel_width as isize,
                    self.last_mouse_coords.y + self.dimensions.pixel_height as isize,
                ),
                mouse_buttons: self.mouse_buttons,
                modifiers: self.modifiers,
                precise_scroll_delta: None,
                scroll_phase: None,
                momentum_phase: None,
                precise_wheel_lines: None,
            };
            self.events.dispatch(WindowEvent::MouseEvent(event));
        }

        if let Some((value_x, value_y)) = PendingMouse::scroll(&pending_mouse) {
            let factor = self.get_dpi_factor() as f64;

            if value_x.signum() != self.hscroll_remainder.signum() {
                // reset accumulator when changing scroll direction
                self.hscroll_remainder = 0.0;
            }
            let scaled_x = (value_x * factor) + self.hscroll_remainder;
            let discrete_x = scaled_x.trunc();
            self.hscroll_remainder = scaled_x - discrete_x;
            if scaled_x.abs() > f64::EPSILON {
                let event = MouseEvent {
                    kind: MouseEventKind::HorzWheel(-discrete_x as i16),
                    coords: self.last_mouse_coords,
                    screen_coords: ScreenPoint::new(
                        self.last_mouse_coords.x + self.dimensions.pixel_width as isize,
                        self.last_mouse_coords.y + self.dimensions.pixel_height as isize,
                    ),
                    mouse_buttons: self.mouse_buttons,
                    modifiers: self.modifiers,
                    precise_scroll_delta: Some(PreciseScrollDelta {
                        x: -scaled_x as f32,
                        y: 0.0,
                    }),
                    scroll_phase: None,
                    momentum_phase: None,
                    precise_wheel_lines: None,
                };
                self.events.dispatch(WindowEvent::MouseEvent(event));
            }

            if value_y.signum() != self.vscroll_remainder.signum() {
                self.vscroll_remainder = 0.0;
            }
            let scaled_y = (value_y * factor) + self.vscroll_remainder;
            let discrete_y = scaled_y.trunc();
            self.vscroll_remainder = scaled_y - discrete_y;
            if scaled_y.abs() > f64::EPSILON {
                let event = MouseEvent {
                    kind: MouseEventKind::VertWheel(-discrete_y as i16),
                    coords: self.last_mouse_coords,
                    screen_coords: ScreenPoint::new(
                        self.last_mouse_coords.x + self.dimensions.pixel_width as isize,
                        self.last_mouse_coords.y + self.dimensions.pixel_height as isize,
                    ),
                    mouse_buttons: self.mouse_buttons,
                    modifiers: self.modifiers,
                    precise_scroll_delta: Some(PreciseScrollDelta {
                        x: 0.0,
                        y: -scaled_y as f32,
                    }),
                    scroll_phase: None,
                    momentum_phase: None,
                    precise_wheel_lines: None,
                };
                self.events.dispatch(WindowEvent::MouseEvent(event));
            }
        }

        if !PendingMouse::in_window(&pending_mouse) {
            self.events.dispatch(WindowEvent::MouseLeave);
            self.refresh_frame();
        }
    }

    pub(crate) fn dispatch_pending_event(&mut self) {
        let mut pending;
        {
            let mut pending_events = self.pending_event.lock().unwrap();
            pending = pending_events.clone();
            *pending_events = PendingEvent::default();
        }

        if pending.close {
            self.events.dispatch(WindowEvent::CloseRequested);
        }

        let mut window_state_changed = false;
        if let Some(window_state) = pending.window_state.take() {
            log::debug!(
                "dispatch_pending_event self.window_state={:?}, pending:{:?}",
                self.window_state,
                window_state
            );
            window_state_changed = self.window_state != window_state;
            self.window_state = window_state;
        }

        if pending.configure.is_none() {
            if pending.dpi.is_some() {
                // Synthesize a pending configure event for the dpi change
                pending.configure.replace((
                    self.pixels_to_surface(self.dimensions.pixel_width as i32) as u32,
                    self.pixels_to_surface(self.dimensions.pixel_height as i32) as u32,
                ));
                log::debug!("synthesize configure with {:?}", pending.configure);
            }
        }

        if let Some(ref window_config) = pending.window_configure {
            self.apply_fallback_frame_visibility(window_config.decoration_mode);
            self.window_frame.update_state(window_config.state);
            self.window_frame
                .update_wm_capabilities(window_config.capabilities);
        }

        if let Some((mut w, mut h)) = pending.configure.take() {
            log::trace!("Pending configure: w:{w}, h{h} -- {:?}", self.window);
            if self.window.is_some() {
                let surface_udata = SurfaceUserData::from_wl(self.surface());
                let factor = surface_udata.surface_data.scale_factor() as f64;
                let old_dimensions = self.dimensions;

                // FIXME: teach this how to resolve dpi_by_screen
                let dpi = self.config.dpi.unwrap_or(factor * crate::DEFAULT_DPI) as usize;

                // Do this early because this affects surface_to_pixels/pixels_to_surface
                self.dimensions.dpi = dpi;

                let mut pixel_width = self.surface_to_pixels(w.try_into().unwrap());
                let mut pixel_height = self.surface_to_pixels(h.try_into().unwrap());

                if self.window_state.can_resize() {
                    self.window_frame.set_resizable(true);
                    if let Some(incr) = self.resize_increments {
                        let min_width = incr.base_width + incr.x;
                        let min_height = incr.base_height + incr.y;
                        let extra_width = (pixel_width - incr.base_width as i32) % incr.x as i32;
                        let extra_height = (pixel_height - incr.base_height as i32) % incr.y as i32;
                        let desired_pixel_width = max(pixel_width - extra_width, min_width as i32);
                        let desired_pixel_height =
                            max(pixel_height - extra_height, min_height as i32);
                        w = self.pixels_to_surface(desired_pixel_width) as u32;
                        h = self.pixels_to_surface(desired_pixel_height) as u32;
                        pixel_width = self.surface_to_pixels(w.try_into().unwrap());
                        pixel_height = self.surface_to_pixels(h.try_into().unwrap());
                    }
                }

                log::trace!("Resizing frame");
                if !self.window_frame.is_hidden() {
                    // Clamp the size to at least one surface heigh/width.
                    let width = NonZeroU32::new(w).unwrap_or(NonZeroU32::new(1).unwrap());
                    let height = NonZeroU32::new(h).unwrap_or(NonZeroU32::new(1).unwrap());
                    self.window_frame.resize(width, height);
                    pending.refresh_decorations = true
                }
                let (x, y) = self.window_frame.location();
                let surface_width = self.pixels_to_surface(pixel_width);
                let surface_height = self.pixels_to_surface(pixel_height);
                self.window
                    .as_mut()
                    .unwrap()
                    .xdg_surface()
                    .set_window_geometry(x, y, surface_width, surface_height);
                // Compute the new pixel dimensions
                let new_dimensions = Dimensions {
                    pixel_width: pixel_width.try_into().unwrap(),
                    pixel_height: pixel_height.try_into().unwrap(),
                    dpi,
                };

                let dimensions_changed = new_dimensions != old_dimensions;
                if dimensions_changed || window_state_changed {
                    self.dimensions = new_dimensions;
                    window_state_changed = false;

                    self.events.dispatch(WindowEvent::Resized {
                        dimensions: self.dimensions,
                        window_state: self.window_state,
                        // We don't know if we're live resizing or not, so
                        // assume no.
                        live_resizing: false,
                    });
                }
                if dimensions_changed {
                    // Avoid blurring by matching the scaling factor of the
                    // compositor; if it is going to double the size then
                    // we render at double the size anyway and tell it that
                    // the buffer is already doubled.
                    // Take care to detach the current buffer (managed by EGL),
                    // so that the compositor doesn't get annoyed by it not
                    // having dimensions that match the scale.
                    // The wegl_surface.resize won't take effect until
                    // we paint later on.
                    // We do this only if the scale has actually changed,
                    // otherwise interactive window resize will keep removing
                    // the window contents!
                    if let Some(wegl_surface) = self.wegl_surface.as_mut() {
                        wegl_surface.resize(pixel_width, pixel_height, 0, 0);
                    }
                    if self.surface_factor != factor {
                        let wayland_conn = Connection::get().unwrap().wayland();
                        let wayland_state = wayland_conn.wayland_state.borrow();
                        let mut pool = wayland_state.mem_pool.borrow_mut();

                        // Make a "fake" buffer with the right dimensions, as
                        // simply detaching the buffer can cause wlroots-derived
                        // compositors consider the window to be unconfigured.
                        if let Ok((buffer, _bytes)) = pool.create_buffer(
                            factor as i32,
                            factor as i32,
                            (factor * 4.0) as i32,
                            wayland_client::protocol::wl_shm::Format::Argb8888,
                        ) {
                            self.surface().attach(Some(buffer.wl_buffer()), 0, 0);
                            self.surface().set_buffer_scale(factor as i32);
                            self.surface_factor = factor;
                        }
                    }
                }
                self.do_paint().unwrap();
            }
        }
        if window_state_changed && self.window.is_some() {
            self.events.dispatch(WindowEvent::Resized {
                dimensions: self.dimensions,
                window_state: self.window_state,
                live_resizing: false,
            });
        }
        if pending.refresh_decorations && self.window.is_some() {
            self.refresh_frame();
        }
        if pending.had_configure_event && self.window.is_some() {
            log::debug!("Had configured an event");
            if let Some(notify) = self.pending_first_configure.take() {
                // Allow window creation to complete
                notify.try_send(()).ok();
            }
        }
    }

    fn set_cursor(&mut self, cursor: Option<MouseCursor>) {
        if !PendingMouse::in_window(&self.pending_mouse) {
            return;
        }

        let conn = Connection::get().unwrap().wayland();
        let state = conn.wayland_state.borrow_mut();
        let pointer = match &state.pointer {
            Some(pointer) => pointer,
            None => return,
        };

        match cursor {
            Some(cursor) => {
                if let Err(err) = pointer.set_cursor(
                    &conn.connection,
                    match cursor {
                        MouseCursor::Arrow => CursorIcon::Default,
                        MouseCursor::Hand => CursorIcon::Pointer,
                        MouseCursor::SizeUpDown => CursorIcon::NsResize,
                        MouseCursor::SizeLeftRight => CursorIcon::EwResize,
                        MouseCursor::SizeNorthWestSouthEast => CursorIcon::NwseResize,
                        MouseCursor::SizeNorthEastSouthWest => CursorIcon::NeswResize,
                        MouseCursor::Text => CursorIcon::Text,
                    },
                ) {
                    log::error!("set_cursor: {}", err);
                }
            }
            None => {
                if let Err(err) = pointer.hide_cursor() {
                    log::error!("hide_cursor: {}", err)
                }
            }
        }
    }

    fn invalidate(&mut self) {
        self.do_paint().unwrap();
    }

    fn set_text_cursor_position(&mut self, rect: Rect) {
        let conn = WaylandConnection::get().unwrap().wayland();
        let state = conn.wayland_state.borrow();
        let surface = self.surface().clone();
        let active_surface_id = state.active_surface_id.borrow();
        let surface_id = surface.id();

        if let Some(active_surface_id) = active_surface_id.as_ref() {
            if surface_id == active_surface_id.clone() {
                if self.text_cursor.map(|prior| prior != rect).unwrap_or(true) {
                    self.text_cursor.replace(rect);

                    let surface_udata = SurfaceUserData::from_wl(&surface);
                    let factor = surface_udata.surface_data().scale_factor();

                    if let Some(text_input) = &state.text_input {
                        if let Some(input) = text_input.get_text_input_for_surface(&surface) {
                            input.set_cursor_rectangle(
                                rect.min_x() as i32 / factor,
                                rect.min_y() as i32 / factor,
                                rect.width() as i32 / factor,
                                rect.height() as i32 / factor,
                            );
                            input.commit();
                        }
                    }
                }
            }
        }
    }

    fn set_title(&mut self, title: String) {
        if let Some(last_title) = self.title.as_ref() {
            if last_title == &title {
                return;
            }
        }
        if let Some(window) = self.window.as_ref() {
            window.set_title(title.clone());
        }
        self.refresh_frame();
        self.title = Some(title);
    }

    fn set_resize_increments(&mut self, incr: ResizeIncrement) -> anyhow::Result<()> {
        self.resize_increments.replace(incr);
        Ok(())
    }

    fn set_inner_size(&mut self, width: usize, height: usize) {
        let pixel_width = width as i32;
        let pixel_height = height as i32;
        let surface_width = self.pixels_to_surface(pixel_width) as u32;
        let surface_height = self.pixels_to_surface(pixel_height) as u32;
        // window.resize() doesn't generate a configure event,
        // so we're going to fake one up, otherwise the window
        // contents don't reflect the real size until eg:
        // the focus is changed.
        self.pending_event
            .lock()
            .unwrap()
            .configure
            .replace((surface_width, surface_height));
        // apply the synthetic configure event to the inner surfaces
        self.dispatch_pending_event();

        self.events.dispatch(WindowEvent::SetInnerSizeCompleted);
    }

    fn do_paint(&mut self) -> anyhow::Result<()> {
        if self.window.is_none() {
            // We're likely in the middle of closing/destroying
            // the window; we've nothing to do here.
            return Ok(());
        }

        if self.frame_callback.is_some() {
            // Painting now won't be productive, so skip it but
            // remember that we need to be painted so that when
            // the compositor is ready for us, we can paint then.
            self.invalidated = true;
            if !self.presented_since_frame_request.get() {
                // Unless nothing has presented since the callback was
                // asked for: only a commit lets it fire, and only a
                // repaint that presents makes one.
                self.schedule_unpresented_retry();
            }
            return Ok(());
        }

        self.invalidated = false;

        // Ask the compositor to wake us up when its time to paint the next frame,
        // note that this only happens _after_ the next commit
        let conn = WaylandConnection::get().unwrap().wayland();
        let qh = conn.event_queue.borrow().handle();

        let callback = self.surface().frame(&qh, self.surface().clone());

        log::trace!("do_paint - callback: {:?}", callback);
        self.frame_callback.replace(callback);
        // Uncommitted until the repaint below reports a present.
        self.presented_since_frame_request.set(false);

        // The repaint has the side of effect of committing the surface,
        // which is necessary for the frame callback to get triggered.
        // Ordering the repaint after requesting the callback ensures that
        // we will get woken at the appropriate time.
        // <https://github.com/wezterm/wezterm/issues/3468>
        // <https://github.com/wezterm/wezterm/issues/3126>
        self.events.dispatch(WindowEvent::NeedRepaint);

        Ok(())
    }

    const MAX_UNPRESENTED_RETRY_DELAY: Duration = Duration::from_secs(1);

    /// Repaint again when nothing has presented since the frame callback
    /// was asked for, since waiting for that callback would wait forever.
    /// The first retry comes one frame interval later, soon enough that a
    /// kept frame goes unnoticed; each further one without a present
    /// doubles the delay, so a renderer failing every frame settles at one
    /// attempt a second instead of spinning.
    fn schedule_unpresented_retry(&mut self) {
        if self.unpresented_retry_armed {
            return;
        }
        self.unpresented_retry_armed = true;
        self.unpresented_retry_generation = self.unpresented_retry_generation.wrapping_add(1);
        let generation = self.unpresented_retry_generation;
        let frame = Duration::from_secs_f64(1.0 / self.config.max_fps.max(1) as f64);
        let delay = frame
            .saturating_mul(1 << self.unpresented_retries.min(20))
            .clamp(Duration::from_millis(1), Self::MAX_UNPRESENTED_RETRY_DELAY);
        let window_id = self.window_id;
        promise::spawn::spawn(async move {
            Timer::after(delay).await;
            let Some(conn) = WaylandConnection::get() else {
                return;
            };
            if let Some(handle) = conn.wayland().window_by_id(window_id) {
                handle.borrow_mut().retry_unpresented(generation);
            }
        })
        .detach();
    }

    fn retry_unpresented(&mut self, generation: u64) {
        if !self.unpresented_retry_armed || generation != self.unpresented_retry_generation {
            // Disowned: a present got through after this one was armed.
            return;
        }
        self.unpresented_retry_armed = false;
        // Armed only while a callback is pending and a repaint is owed, and
        // both outlast the arming: what can change is a close, or a present
        // from show() or a resize completing.
        if self.window.is_none() || self.presented_since_frame_request.get() {
            return;
        }
        self.unpresented_retries = self.unpresented_retries.saturating_add(1);
        self.invalidated = false;
        // The callback already asked for rides along with this repaint's
        // commit; asking for another would have it fire twice.
        self.events.dispatch(WindowEvent::NeedRepaint);
    }

    fn surface(&self) -> &WlSurface {
        self.window
            .as_ref()
            .expect("Window should exist")
            .wl_surface()
    }

    pub(crate) fn next_frame_is_ready(&mut self) {
        self.frame_callback.take();
        // A present got through: start over at one frame interval. An armed
        // retry still on its long delay must not hold back the next one.
        self.unpresented_retries = 0;
        self.unpresented_retry_armed = false;
        if self.invalidated {
            self.do_paint().ok();
        }
    }

    pub(crate) fn emit_focus(&mut self, mapper: &mut KeyboardWithFallback, focused: bool) {
        // Clear the modifiers when we change focus, otherwise weird
        // things can happen.  For instance, if we lost focus because
        // CTRL+SHIFT+N was pressed to spawn a new window, we'd be
        // left stuck with CTRL+SHIFT held down and the window would
        // be left in a broken state.

        self.modifiers = Modifiers::NONE;
        mapper.update_modifier_state(0, 0, 0, 0);
        self.key_repeat.take();
        self.events.dispatch(WindowEvent::FocusChanged(focused));
        self.text_cursor.take();
    }

    pub(crate) fn appearance_changed(&mut self, appearance: Appearance) {
        if appearance != self.appearance {
            self.appearance = appearance;
            self.events
                .dispatch(WindowEvent::AppearanceChanged(appearance));
        }
    }

    pub(super) fn keyboard_event(
        &mut self,
        mapper: &mut KeyboardWithFallback,
        event: WlKeyboardEvent,
    ) {
        match event {
            WlKeyboardEvent::Enter { keys, .. } => {
                let key_codes = keys
                    .chunks_exact(4)
                    .map(|c| u32::from_ne_bytes(c.try_into().unwrap()))
                    .collect::<Vec<_>>();
                log::trace!("keyboard event: Enter with keys: {:?}", key_codes);
                self.emit_focus(mapper, true);
            }
            WlKeyboardEvent::Leave { .. } => {
                self.emit_focus(mapper, false);
            }
            WlKeyboardEvent::Key { key, state, .. } => {
                if let Some(event) = mapper.process_wayland_key(
                    key,
                    state.into_result().unwrap() == KeyState::Pressed,
                    &mut self.events,
                ) {
                    let rep = Arc::new(Mutex::new(KeyRepeatState {
                        when: Instant::now(),
                        event,
                    }));
                    self.key_repeat.replace((key, Arc::clone(&rep)));
                    let window_id = SurfaceUserData::from_wl(
                        self.window
                            .as_ref()
                            .expect("window should exist")
                            .wl_surface(),
                    )
                    .window_id;
                    KeyRepeatState::schedule(rep, window_id);
                } else if let Some((cur_key, _)) = self.key_repeat.as_ref() {
                    // important to check that it's the same key, because the release of the previously
                    // repeated key can come right after the press of the newly held key
                    if *cur_key == key {
                        self.key_repeat.take();
                    }
                }
            }
            WlKeyboardEvent::Modifiers {
                mods_depressed,
                mods_latched,
                mods_locked,
                group,
                ..
            } => {
                mapper.update_modifier_state(mods_depressed, mods_latched, mods_locked, group);

                let mods = mapper.get_key_modifiers();
                let leds = mapper.get_led_status();

                let changed = (mods != self.modifiers) || (leds != self.leds);

                self.modifiers = mapper.get_key_modifiers();
                self.leds = mapper.get_led_status();

                if changed {
                    self.events
                        .dispatch(WindowEvent::AdviseModifiersLedStatus(mods, leds));
                }
            }
            _ => {}
        }
    }

    pub(super) fn frame_action(&mut self, pointer: &WlPointer, serial: u32, action: FrameAction) {
        let pointer_data = pointer.data::<PointerUserData>().unwrap();
        let seat = pointer_data.pdata.seat();
        match action {
            FrameAction::Close => self.events.dispatch(WindowEvent::CloseRequested),
            FrameAction::Minimize => self.window.as_ref().unwrap().set_minimized(),
            FrameAction::Maximize => self.window.as_ref().unwrap().set_maximized(),
            FrameAction::UnMaximize => self.window.as_ref().unwrap().unset_maximized(),
            FrameAction::ShowMenu(x, y) => {
                self.window
                    .as_ref()
                    .unwrap()
                    .show_window_menu(seat, serial, (x, y))
            }
            FrameAction::Resize(edge) => {
                let edge = match edge {
                    ResizeEdge::None => XdgResizeEdge::None,
                    ResizeEdge::Top => XdgResizeEdge::Top,
                    ResizeEdge::Bottom => XdgResizeEdge::Bottom,
                    ResizeEdge::Left => XdgResizeEdge::Left,
                    ResizeEdge::TopLeft => XdgResizeEdge::TopLeft,
                    ResizeEdge::BottomLeft => XdgResizeEdge::BottomLeft,
                    ResizeEdge::Right => XdgResizeEdge::Right,
                    ResizeEdge::TopRight => XdgResizeEdge::TopRight,
                    ResizeEdge::BottomRight => XdgResizeEdge::BottomRight,
                    _ => return, // Realistically, there probably won't be any new edges added.
                };
                self.window.as_ref().unwrap().resize(seat, serial, edge)
            }
            FrameAction::Move => self.window.as_ref().unwrap().move_(seat, serial),
            _ => log::warn!("unhandled FrameAction: {:?}", action),
        }
    }

    fn maximize(&mut self) {
        if let Some(window) = self.window.as_mut() {
            window.set_maximized();
        }
    }

    fn restore(&mut self) {
        if let Some(window) = self.window.as_mut() {
            window.unset_maximized();
        }
    }

    fn config_did_change(&mut self, config: ConfigHandle) {
        self.config = config;
        let decor_mode = requested_wayland_decoration_mode(self.config.window_decorations);
        if let Some(window) = self.window.as_ref() {
            window.request_decoration_mode(decor_mode);
        }
        self.apply_fallback_frame_visibility(decor_mode.unwrap_or(DecorationMode::Client));
        self.refresh_frame();
        self.update_window_background_blur();
    }

    fn update_window_background_blur(&self) {
        let conn = WaylandConnection::get().unwrap().wayland();
        let qh = conn.event_queue.borrow().handle();
        let wayland_state = conn.wayland_state.borrow();
        if let Some(manager) = &wayland_state.kde_blur_manager {
            let kde_blur = manager.create(self.surface(), &qh, GlobalData);
            if self.config.kde_window_background_blur {
                kde_blur.set_region(None);
            } else {
                kde_blur.release();
            }
            kde_blur.commit();
        }
    }
}

impl WaylandState {
    pub(super) fn window_by_id(&self, window_id: usize) -> Option<Rc<RefCell<WaylandWindowInner>>> {
        self.windows.borrow().get(&window_id).map(Rc::clone)
    }

    fn handle_window_event(&self, window: &XdgWindow, event: WaylandWindowEvent) {
        let surface_data = SurfaceUserData::from_wl(window.wl_surface());
        let window_id = surface_data.window_id;

        let window_inner = self
            .window_by_id(window_id)
            .expect("Inner Window should exist");

        let p = window_inner.borrow().pending_event.clone();
        let mut pending_event = p.lock().unwrap();

        let changed = match event {
            WaylandWindowEvent::Close => {
                // TODO: This should the new queue function
                // p.queue_close()
                if !pending_event.close {
                    pending_event.close = true;
                    true
                } else {
                    false
                }
            }
            WaylandWindowEvent::Request(configure) => {
                pending_event.window_configure.replace(configure.clone());
                // TODO: This should the new queue function
                // p.queue_configure(&configure)
                //
                let mut changed;
                pending_event.had_configure_event = true;
                if let (Some(w), Some(h)) = configure.new_size {
                    changed = pending_event.configure.is_none();
                    pending_event.configure.replace((w.get(), h.get()));
                } else {
                    changed = true;
                }

                let mut state = WindowState::COMPOSITED;
                if configure.state.contains(SCTKWindowState::FULLSCREEN) {
                    state |= WindowState::FULL_SCREEN;
                }
                if configure.state.contains(SCTKWindowState::MAXIMIZED) {
                    state |= WindowState::MAXIMIZED;
                }
                if configure.state.intersects(
                    SCTKWindowState::TILED_LEFT
                        | SCTKWindowState::TILED_RIGHT
                        | SCTKWindowState::TILED_TOP
                        | SCTKWindowState::TILED_BOTTOM,
                ) {
                    state |= WindowState::TILED;
                }
                if configure.decoration_mode == DecorationMode::Server {
                    state |= WindowState::SERVER_DECORATED;
                }

                log::debug!(
                    "Config: self.window_state={:?}, states: {:?} {:?}",
                    pending_event.window_state,
                    state,
                    configure.state
                );

                if pending_event.window_state != Some(state) {
                    changed = true;
                }

                pending_event.window_state.replace(state);
                changed
            }
        };
        if changed {
            WaylandConnection::with_window_inner(window_id, move |inner| {
                inner.dispatch_pending_event();
                Ok(())
            });
        }
    }
}

impl CompositorHandler for WaylandState {
    fn scale_factor_changed(
        &mut self,
        _conn: &WConnection,
        _qh: &wayland_client::QueueHandle<Self>,
        _surface: &wayland_client::protocol::wl_surface::WlSurface,
        _new_factor: i32,
    ) {
        // We do nothing, we get the scale_factor from surface_data
    }

    fn frame(
        &mut self,
        _conn: &WConnection,
        _qh: &wayland_client::QueueHandle<Self>,
        surface: &wayland_client::protocol::wl_surface::WlSurface,
        _time: u32,
    ) {
        log::trace!("frame: CompositorHandler");
        let surface_data = SurfaceUserData::from_wl(surface);
        let window_id = surface_data.window_id;

        WaylandConnection::with_window_inner(window_id, |inner| {
            inner.next_frame_is_ready();
            Ok(())
        });
    }

    fn transform_changed(
        &mut self,
        _conn: &WConnection,
        _qh: &wayland_client::QueueHandle<Self>,
        _surface: &wayland_client::protocol::wl_surface::WlSurface,
        _new_transform: wayland_client::protocol::wl_output::Transform,
    ) {
        // TODO: do we need to do anything here?
    }

    fn surface_enter(
        &mut self,
        _conn: &WConnection,
        _qh: &wayland_client::QueueHandle<Self>,
        _surface: &wayland_client::protocol::wl_surface::WlSurface,
        _output: &wayland_client::protocol::wl_output::WlOutput,
    ) {
    }

    fn surface_leave(
        &mut self,
        _conn: &WConnection,
        _qh: &wayland_client::QueueHandle<Self>,
        _surface: &wayland_client::protocol::wl_surface::WlSurface,
        _output: &wayland_client::protocol::wl_output::WlOutput,
    ) {
    }
}

impl WindowHandler for WaylandState {
    fn request_close(
        &mut self,
        _conn: &WConnection,
        _qh: &wayland_client::QueueHandle<Self>,
        window: &XdgWindow,
    ) {
        self.handle_window_event(window, WaylandWindowEvent::Close);
    }

    fn configure(
        &mut self,
        _conn: &WConnection,
        _qh: &wayland_client::QueueHandle<Self>,
        window: &XdgWindow,
        configure: WindowConfigure,
        _serial: u32,
    ) {
        self.handle_window_event(window, WaylandWindowEvent::Request(configure));
    }
}

impl Dispatch<OrgKdeKwinBlurManager, GlobalData> for WaylandState {
    fn event(
        _state: &mut Self,
        _proxy: &OrgKdeKwinBlurManager,
        _event: <OrgKdeKwinBlurManager as Proxy>::Event,
        _data: &GlobalData,
        _conn: &WConnection,
        _qhandle: &wayland_client::QueueHandle<Self>,
    ) {
        // No events from OrgKdeKwinBlurManager...
    }
}

impl Dispatch<OrgKdeKwinBlur, GlobalData> for WaylandState {
    fn event(
        _state: &mut Self,
        _proxy: &OrgKdeKwinBlur,
        _event: <OrgKdeKwinBlur as Proxy>::Event,
        _data: &GlobalData,
        _conn: &WConnection,
        _qhandle: &wayland_client::QueueHandle<Self>,
    ) {
        // No events from OrgKdeKwinBlur...
    }
}

impl Dispatch<WlRegion, GlobalData> for WaylandState {
    fn event(
        _state: &mut Self,
        _proxy: &WlRegion,
        _event: <WlRegion as Proxy>::Event,
        _data: &GlobalData,
        _conn: &WConnection,
        _qhandle: &QueueHandle<Self>,
    ) {
    }
}

pub(super) struct SurfaceUserData {
    surface_data: SurfaceData,
    pub(super) window_id: usize,
}

impl SurfaceUserData {
    pub(super) fn from_wl(wl: &WlSurface) -> &Self {
        wl.data()
            .expect("User data should be associated with WlSurface")
    }
    pub(super) fn try_from_wl(wl: &WlSurface) -> Option<&SurfaceUserData> {
        wl.data()
    }
}

impl SurfaceDataExt for SurfaceUserData {
    fn surface_data(&self) -> &SurfaceData {
        &self.surface_data
    }
}

impl HasDisplayHandle for WaylandWindowInner {
    fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
        let conn = WaylandConnection::get().unwrap().wayland();
        let backend = conn.connection.backend();
        let handle = backend.display_handle()?;
        Ok(unsafe { DisplayHandle::borrow_raw(handle.as_raw()) })
    }
}

impl HasWindowHandle for WaylandWindowInner {
    fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
        let handle = WaylandWindowHandle::new(
            NonNull::new(self.surface().id().as_ptr() as _).expect("non-null"),
        );
        unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::Wayland(handle))) }
    }
}

impl HasDisplayHandle for WaylandWindow {
    fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
        let conn = WaylandConnection::get().unwrap().wayland();
        let backend = conn.connection.backend();
        let handle = backend.display_handle()?;
        Ok(unsafe { DisplayHandle::borrow_raw(handle.as_raw()) })
    }
}

impl HasWindowHandle for WaylandWindow {
    fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
        let conn = Connection::get().expect("raw_window_handle only callable on main thread");
        let handle = conn
            .wayland()
            .window_by_id(self.0)
            .expect("window handle invalid!?");

        let inner = handle.borrow();
        let handle = inner.window_handle()?;
        unsafe { Ok(WindowHandle::borrow_raw(handle.as_raw())) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn integrated_buttons_use_client_mode_without_fallback_header() {
        let decorations = WindowDecorations::INTEGRATED_BUTTONS | WindowDecorations::RESIZE;
        assert_eq!(
            requested_wayland_decoration_mode(decorations),
            Some(DecorationMode::Client)
        );
        assert!(!wayland_fallback_frame_visible(
            decorations,
            DecorationMode::Client
        ));
    }

    #[test]
    fn fallback_header_requires_title_and_client_mode() {
        let decorations = WindowDecorations::TITLE | WindowDecorations::RESIZE;
        assert!(wayland_fallback_frame_visible(
            decorations,
            DecorationMode::Client
        ));
        assert!(!wayland_fallback_frame_visible(
            decorations,
            DecorationMode::Server
        ));
        assert!(!wayland_fallback_frame_visible(
            WindowDecorations::RESIZE,
            DecorationMode::Client
        ));
    }

    #[test]
    fn surface_resize_hit_testing_covers_all_directions() {
        let width = 100;
        let height = 80;
        let edge = 6;
        for (x, y, expected) in [
            (0, 0, WaylandSurfaceResizeEdge::TopLeft),
            (50, 0, WaylandSurfaceResizeEdge::Top),
            (99, 0, WaylandSurfaceResizeEdge::TopRight),
            (99, 40, WaylandSurfaceResizeEdge::Right),
            (99, 79, WaylandSurfaceResizeEdge::BottomRight),
            (50, 79, WaylandSurfaceResizeEdge::Bottom),
            (0, 79, WaylandSurfaceResizeEdge::BottomLeft),
            (0, 40, WaylandSurfaceResizeEdge::Left),
        ] {
            assert_eq!(
                wayland_surface_resize_edge_at(x, y, width, height, edge),
                Some(expected)
            );
        }
        assert_eq!(
            wayland_surface_resize_edge_at(50, 40, width, height, edge),
            None
        );
    }
}