Skip to content

An open-source remote desktop application designed for self-hosting, as an alternative to TeamViewer.

78.9 KB
Raw Blame
use std::collections::HashMap;
use std::error::Error;
use std::os::unix::io::AsRawFd;
use std::process::Command;
use std::sync::{
    atomic::{AtomicBool, AtomicU8, Ordering},
    Arc, Mutex,
};
use std::time::Duration;
use tracing::{debug, error, trace, warn};

use dbus::{
    arg::{OwnedFd, PropMap, RefArg, Variant},
    blocking::{Proxy, SyncConnection},
    message::{MatchRule, MessageType},
    Message,
};

use gstreamer as gst;
use gstreamer::prelude::*;
use gstreamer_app::AppSink;

use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};

use base::platform::linux::{WaylandDisplayInfo, CMD_SH};
use hbb_common::{anyhow::anyhow, bail, config, serde_json, tokio, ResultType};

use super::capturable::PixelProvider;
use super::capturable::{Capturable, Recorder};
use super::display::{clear_wayland_displays_cache, get_displays, Displays};
use super::remote_desktop_portal::OrgFreedesktopPortalRemoteDesktop as remote_desktop_portal;
use super::request_portal::OrgFreedesktopPortalRequestResponse;
use super::screencast_portal::OrgFreedesktopPortalScreenCast as screencast_portal;

lazy_static! {
    pub static ref RDP_SESSION_INFO: Mutex<Option<RdpSessionInfo>> = Mutex::new(None);
}

#[derive(Serialize, Deserialize)]
// For KDE Plasma only, because GNOME provides position info.
struct PipewireDisplayOffsetCache {
    // We need to compare the displays, because:
    // 1. On Archlinux KDE Plasma
    // 2. One display, and connect, remember share choice.
    // 3. Plug in another monitor.
    // 4. The portal will reuse the restore token, no new share choice dialog, but the share screen is different.
    //    The controlling side will see the new monitor.
    // All displays as one string for easy comparison
    // name1-x1-y1-width1-height1;name2-x2-y2-width2-height2;...
    display_key: String,
    restore_token: String,
    offsets: Vec<(i32, i32)>,
}

// KDE Plasma may not provide position info
static HAS_POSITION_ATTR: AtomicBool = AtomicBool::new(false);
static IS_SERVER_RUNNING: AtomicU8 = AtomicU8::new(0); // 0: uninitialized, 1:true, 2: false

impl PipewireDisplayOffsetCache {
    fn displays_to_key(displays: &Arc<Displays>) -> String {
        displays
            .displays
            .iter()
            .map(|d| format!("{}-{}-{}-{}-{}", d.name, d.x, d.y, d.width, d.height))
            .collect::<Vec<String>>()
            .join(";")
    }
}

#[inline]
pub fn close_session() {
    let _ = RDP_SESSION_INFO.lock().unwrap().take();
    clear_wayland_displays_cache();
    HAS_POSITION_ATTR.store(false, Ordering::SeqCst);
}

#[inline]
pub fn is_rdp_session_hold() -> bool {
    // try_lock: a request waiting for the user holds the slot (no session yet), and so do the short
    // looks while a session is set up. Busy answers no session rather than wait.
    matches!(RDP_SESSION_INFO.try_lock(), Ok(info) if info.is_some())
}

pub fn try_close_session() {
    let mut rdp_info = RDP_SESSION_INFO.lock().unwrap();
    let mut close = false;
    if let Some(rdp_info) = &*rdp_info {
        // If is server running and restore token is supported, there's no need to keep the session.
        if is_server_running() && rdp_info.is_support_restore_token {
            close = true;
        }
    }
    if close {
        *rdp_info = None;
        clear_wayland_displays_cache();
        HAS_POSITION_ATTR.store(false, Ordering::SeqCst);
    }
}

pub struct RdpSessionInfo {
    pub conn: Arc<SyncConnection>,
    pub streams: Vec<PwStreamInfo>,
    pub fd: OwnedFd,
    pub session: dbus::Path<'static>,
    pub is_support_restore_token: bool,
    pub resolution: Arc<Mutex<Option<(usize, usize)>>>,
}
#[derive(Debug, Clone, Copy)]
pub struct PwStreamInfo {
    pub path: u64,
    source_type: u64,
    position: (i32, i32),
    size: (usize, usize),
}

impl PwStreamInfo {
    pub fn get_size(&self) -> (usize, usize) {
        self.size
    }

    pub fn get_position(&self) -> (i32, i32) {
        self.position
    }
}

#[derive(Debug)]
pub struct DBusError(String);

impl std::fmt::Display for DBusError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Self(s) = self;
        write!(f, "{}", s)
    }
}

impl Error for DBusError {}

#[derive(Debug)]
pub struct GStreamerError(String);

impl std::fmt::Display for GStreamerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Self(s) = self;
        write!(f, "{}", s)
    }
}

impl Error for GStreamerError {}

#[derive(Clone)]
pub struct PipeWireCapturable {
    // connection needs to be kept alive for recording
    dbus_conn: Arc<SyncConnection>,
    fd: OwnedFd,
    path: u64,
    source_type: u64,
    pub primary: bool,
    pub position: (i32, i32),
    pub logical_size: (usize, usize),
    pub physical_size: (usize, usize),
    // False when `physical_size` is the portal size standing in for a resolution get_res() could
    // not read.
    physical_size_measured: bool,
}

impl PipeWireCapturable {
    fn new(
        conn: Arc<SyncConnection>,
        fd: OwnedFd,
        resolution: Arc<Mutex<Option<(usize, usize)>>>,
        stream: &PwStreamInfo,
    ) -> Self {
        // alternative to get screen resolution as stream.size is not always correct ex: on fractional scaling
        // https://github.com/rustdesk/rustdesk/issues/6116#issuecomment-1817724244
        let measured = get_res(Self {
            dbus_conn: conn.clone(),
            fd: fd.clone(),
            path: stream.path,
            source_type: stream.source_type,
            primary: false,
            position: stream.position,
            logical_size: stream.size,
            physical_size: (0, 0),
            physical_size_measured: false,
        });
        let physical_size_measured = measured.is_ok();
        let physical_size = measured.unwrap_or(stream.size);
        *resolution.lock().unwrap() = Some(physical_size);
        Self {
            dbus_conn: conn,
            fd,
            path: stream.path,
            source_type: stream.source_type,
            primary: false,
            position: stream.position,
            logical_size: stream.size,
            physical_size,
            physical_size_measured,
        }
    }
}

impl std::fmt::Debug for PipeWireCapturable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "PipeWireCapturable {{dbus: {}, fd: {}, path: {}, source_type: {}}}",
            self.dbus_conn.unique_name(),
            self.fd.as_raw_fd(),
            self.path,
            self.source_type
        )
    }
}

impl Capturable for PipeWireCapturable {
    fn name(&self) -> String {
        let type_str = match self.source_type {
            1 => "Desktop",
            2 => "Window",
            _ => "Unknow",
        };
        format!("Pipewire {}, path: {}", type_str, self.path)
    }

    fn geometry_relative(&self) -> Result<(f64, f64, f64, f64), Box<dyn Error>> {
        Ok((0.0, 0.0, 1.0, 1.0))
    }

    fn before_input(&mut self) -> Result<(), Box<dyn Error>> {
        Ok(())
    }

    fn recorder(&self, _capture_cursor: bool) -> Result<Box<dyn Recorder>, Box<dyn Error>> {
        Ok(Box::new(PipeWireRecorder::new(self.clone())?))
    }
}

fn get_res(capturable: PipeWireCapturable) -> Result<(usize, usize), Box<dyn Error>> {
    let rec = PipeWireRecorder::new(capturable)?;
    if let Some(sample) = rec
        .appsink
        .try_pull_sample(gst::ClockTime::from_mseconds(300))
    {
        let cap = sample
            .get_caps()
            .ok_or("Failed get caps")?
            .get_structure(0)
            .ok_or("Failed to get structure")?;
        let w: i32 = cap.get_value("width")?.get_some()?;
        let h: i32 = cap.get_value("height")?.get_some()?;
        let w = w as usize;
        let h = h as usize;
        Ok((w, h))
    } else {
        Err(Box::new(GStreamerError(
            "Error getting screen resolution".into(),
        )))
    }
}

pub struct PipeWireRecorder {
    buffer: Option<gst::MappedBuffer<gst::buffer::Readable>>,
    buffer_cropped: Vec<u8>,
    pix_fmt: String,
    is_cropped: bool,
    pipeline: gst::Pipeline,
    appsink: AppSink,
    width: usize,
    height: usize,
    saved_raw_data: Vec<u8>, // for faster compare and copy
    dmabuf_frame: Vec<u8>,   // last frame read from a linear DMA-BUF, rows packed to width * 4
}

// Compositors such as niri offer screencast buffers only as DMA-BUF, so the system-memory
// pipeline never negotiates ("no more input formats"). Linear DMA-BUF can be read by the CPU,
// so the fallback pipeline accepts it and `read_linear_dmabuf` copies the frame out.
// A drm-format without a ":0x<modifier>" suffix means the linear modifier.
const DMABUF_CAPS: &str = "video/x-raw(memory:DMABuf),format=DMA_DRM,drm-format={XR24,XB24}";

// Element creation fails the same way for a plugin that is not installed as for one that is
// broken, so the tag does not claim which. Only the name travels to the peer -- it is what
// says which package to look at -- and the factory's own error stays here in the log.
fn gst_element(name: &str) -> ResultType<gst::Element> {
    gst::ElementFactory::make(name, None).map_err(|e| {
        error!("Failed to create GStreamer element {}: {}", name, e);
        anyhow!(stage_err("gst-plugin", "unavailable", name))
    })
}

impl PipeWireRecorder {
    pub fn new(capturable: PipeWireCapturable) -> ResultType<Self> {
        let pipeline = gst::Pipeline::new(None);
        // Clock waits on stale capture timestamps can exhaust the compositor's buffers.
        pipeline.use_clock(None::<&gst::Clock>);

        let src = gst_element("pipewiresrc")?;
        src.set_property("fd", &capturable.fd.as_raw_fd())?;
        src.set_property("path", &format!("{}", capturable.path))?;
        src.set_property("keepalive_time", &1_000.as_raw_fd())?;

        // For some reason pipewire blocks on destruction of AppSink if this is not set to true,
        // see: https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/982
        src.set_property("always-copy", &true)?;

        // COSMIC/Wayland fix: insert videoconvert between pipewiresrc and appsink.
        // xdg-desktop-portal-cosmic's modifier negotiation fails when the downstream
        // format set is too narrow (appsink only accepts BGRx/RGBx), producing
        // "no more output formats" / not-negotiated (-4). videoconvert accepts any
        // system-memory video/x-raw format, widening negotiation so the portal can
        // settle on a format it can deliver via its SHM path.
        let convert = gst_element("videoconvert")?;

        let sink = gst_element("appsink")?;
        sink.set_property("drop", &true)?;
        sink.set_property("max-buffers", &1u32)?;

        pipeline.add_many(&[&src, &convert, &sink])?;
        src.link(&convert)?;
        convert.link(&sink)?;

        let appsink = sink
            .dynamic_cast::<AppSink>()
            .map_err(|_| GStreamerError("Sink element is expected to be an appsink!".into()))?;
        let mut caps = gst::Caps::new_empty();
        caps.merge_structure(gst::structure::Structure::new(
            "video/x-raw",
            &[("format", &"BGRx")],
        ));
        caps.merge_structure(gst::structure::Structure::new(
            "video/x-raw",
            &[("format", &"RGBx")],
        ));
        appsink.set_caps(Some(&caps));

        // [Workaround]
        // Crash may occur if there are multiple pipelines started at the same time.
        // `pipeline.get_state()` can significantly reduce the probability of crashes,
        // but cannot completely resolve this issue.
        // Adding a short sleep period can also reduce the probability of crashes.
        debug!(
            "[gstreamer] Setting pipeline {} to PLAYING state...",
            capturable.fd.as_raw_fd()
        );
        let (pipeline, appsink) = match pipeline.set_state(gst::State::Playing) {
            Ok(_) => (pipeline, appsink),
            Err(e) => {
                warn!(
                    "[gstreamer] System-memory pipeline {} failed ({}), retrying with linear DMA-BUF",
                    capturable.fd.as_raw_fd(),
                    e
                );
                // Release the PipeWire stream before the next pipeline connects to the same node.
                if let Err(e) = pipeline.set_state(gst::State::Null) {
                    warn!("[gstreamer] Failed to stop pipeline: {}", e);
                }
                build_dmabuf_pipeline(&capturable)?
            }
        };

        // If `is_server_running()` is false, it means using remote_desktop_portal,
        // which does not use multiple streams, so no need to wait for state change.
        if is_server_running() {
            // Wait for the state change to actually complete before proceeding.
            // The 2000ms timeout for pipeline state change was chosen based on empirical testing.
            let state_change = pipeline.get_state(gst::ClockTime::from_mseconds(2000));
            match state_change {
                (Ok(_), gst::State::Playing, _) => {
                    debug!(
                        "[gstreamer] Pipeline {} state confirmed as PLAYING.",
                        capturable.fd.as_raw_fd()
                    );
                }
                (result, state, pending) => {
                    warn!(
                    "[gstreamer] Pipeline {} state change incomplete: result={:?}, state={:?}, pending={:?}",
                    capturable.fd.as_raw_fd(), result, state, pending
                );
                }
            }
            std::thread::sleep(std::time::Duration::from_millis(150));
        }

        Ok(Self {
            pipeline,
            appsink,
            buffer: None,
            pix_fmt: "".into(),
            width: 0,
            height: 0,
            buffer_cropped: vec![],
            is_cropped: false,
            saved_raw_data: Vec::new(),
            dmabuf_frame: Vec::new(),
        })
    }
}

// Same as the pipeline in `PipeWireRecorder::new`, but for DMA-BUF: no videoconvert (it cannot
// import DMA-BUF) and no always-copy (it copies only the PipeWire chunk, whose size is a
// placeholder for DMA-BUF, 1 byte on niri; the frame is read through the fd instead).
fn build_dmabuf_pipeline(capturable: &PipeWireCapturable) -> ResultType<(gst::Pipeline, AppSink)> {
    let pipeline = gst::Pipeline::new(None);
    // Clock waits on stale capture timestamps can exhaust the compositor's buffers.
    pipeline.use_clock(None::<&gst::Clock>);

    let src = gst_element("pipewiresrc")?;
    src.set_property("fd", &capturable.fd.as_raw_fd())?;
    src.set_property("path", &format!("{}", capturable.path))?;
    src.set_property("keepalive_time", &1_000.as_raw_fd())?;

    let sink = gst_element("appsink")?;
    sink.set_property("drop", &true)?;
    sink.set_property("max-buffers", &1u32)?;

    pipeline.add_many(&[&src, &sink])?;
    src.link(&sink)?;

    let appsink = sink
        .dynamic_cast::<AppSink>()
        .map_err(|_| GStreamerError("Sink element is expected to be an appsink!".into()))?;
    let caps = DMABUF_CAPS
        .parse::<gst::Caps>()
        .map_err(|_| GStreamerError("Invalid DMA-BUF caps".into()))?;
    appsink.set_caps(Some(&caps));

    debug!(
        "[gstreamer] Setting DMA-BUF pipeline {} to PLAYING state...",
        capturable.fd.as_raw_fd()
    );
    if let Err(e) = pipeline.set_state(gst::State::Playing) {
        if let Err(e) = pipeline.set_state(gst::State::Null) {
            warn!("[gstreamer] Failed to stop DMA-BUF pipeline: {}", e);
        }
        return Err(e.into());
    }
    Ok((pipeline, appsink))
}

// libgstallocators is part of gst-plugins-base, which pipewiresrc and gstreamer-video need anyway.
#[link(name = "gstallocators-1.0")]
extern "C" {
    fn gst_is_dmabuf_memory(mem: *mut std::ffi::c_void) -> std::os::raw::c_int;
    fn gst_dmabuf_memory_get_fd(mem: *mut std::ffi::c_void) -> std::os::raw::c_int;
}

/// Copy a linear BGRx/RGBx DMA-BUF frame into `out` as packed rows of `w * 4` bytes.
/// A normal map of such a buffer covers only the PipeWire chunk size, so read through the fd.
/// Returns `Ok(false)`, with `out` unchanged, for a frame that does not fit its dmabuf.
fn read_linear_dmabuf(
    buf: &gst::Buffer,
    w: usize,
    h: usize,
    out: &mut Vec<u8>,
) -> Result<bool, Box<dyn Error>> {
    use hbb_common::libc;
    #[repr(C)]
    struct DmaBufSync {
        flags: u64,
    }
    // _IOW('b', 0, struct dma_buf_sync)
    const DMA_BUF_IOCTL_SYNC: u64 = 0x4008_6200;
    const DMA_BUF_SYNC_READ: u64 = 1;
    const DMA_BUF_SYNC_END: u64 = 4;

    if buf.n_memory() != 1 {
        return Err(GStreamerError(format!("DMA-BUF with {} memories", buf.n_memory())).into());
    }
    let mem = buf.peek_memory(0);
    let mem_ptr = unsafe { mem.as_ptr() } as *mut std::ffi::c_void;
    if unsafe { gst_is_dmabuf_memory(mem_ptr) } == 0 {
        return Err(GStreamerError("Buffer memory is not DMA-BUF".into()).into());
    }
    let fd = unsafe { gst_dmabuf_memory_get_fd(mem_ptr) };
    if fd < 0 {
        return Err(GStreamerError("DMA-BUF memory has no fd".into()).into());
    }
    let (stride, plane_offset) = match buf.get_meta::<gstreamer_video::VideoMeta>() {
        Some(m) => (m.get_stride()[0].max(0) as usize, m.get_offset()[0]),
        None => (w * 4, 0),
    };
    // Map only what the dmabuf holds, so a bad stride or offset is an error, not a SIGBUS.
    let size = unsafe { libc::lseek(fd, 0, libc::SEEK_END) };
    if size <= 0 {
        return Err(std::io::Error::last_os_error().into());
    }
    let len = size as usize;
    let map = unsafe {
        libc::mmap(
            std::ptr::null_mut(),
            len,
            libc::PROT_READ,
            libc::MAP_SHARED,
            fd,
            0,
        )
    };
    if map == libc::MAP_FAILED {
        return Err(std::io::Error::last_os_error().into());
    }
    // Sync makes the CPU view coherent with the GPU around the read.
    // The kernel asks callers to restart it on EINTR / EAGAIN.
    let sync = |flags: u64| -> Result<(), Box<dyn Error>> {
        let mut arg = DmaBufSync { flags };
        loop {
            if unsafe { libc::ioctl(fd, DMA_BUF_IOCTL_SYNC as _, &mut arg) } == 0 {
                return Ok(());
            }
            let err = std::io::Error::last_os_error();
            if !matches!(err.raw_os_error(), Some(libc::EINTR) | Some(libc::EAGAIN)) {
                return Err(err.into());
            }
        }
    };
    let copied = sync(DMA_BUF_SYNC_READ).and_then(|_| {
        let src = unsafe { std::slice::from_raw_parts(map as *const u8, len) };
        let start = mem.get_offset() + plane_offset;
        let packed = pack_rows(src, start, stride, w * 4, h, out);
        if !packed {
            // Caps can carry a new size while the buffer is still from the old allocation,
            // like the system-memory size mismatch (pipewire#985): drop the frame.
            trace!(
                "DMA-BUF frame of {}x{} at offset {} and stride {} does not fit {} bytes, dropping it!",
                w,
                h,
                start,
                stride,
                len
            );
        }
        sync(DMA_BUF_SYNC_READ | DMA_BUF_SYNC_END).map(|_| packed)
    });
    if unsafe { libc::munmap(map, len) } != 0 {
        let err = std::io::Error::last_os_error();
        // Runs once per frame, so a persistent failure must not write a line per frame.
        hbb_common::throttled_log!(
            Duration::from_secs(60),
            warn,
            "[gstreamer] Failed to unmap DMA-BUF frame: {}",
            err
        );
    }
    copied
}

/// Copy `h` rows of `row_len` bytes that start `stride` bytes apart at `start` into `out`.
/// Returns false, with `out` unchanged, when the rows do not fit in `src`.
fn pack_rows(
    src: &[u8],
    start: usize,
    stride: usize,
    row_len: usize,
    h: usize,
    out: &mut Vec<u8>,
) -> bool {
    // Checked: stride and offset come from the compositor, and a wrapped sum would pass the guard.
    let end = stride
        .checked_mul(h.saturating_sub(1))
        .and_then(|n| n.checked_add(start))
        .and_then(|n| n.checked_add(row_len));
    if stride < row_len || end.map_or(true, |end| end > src.len()) {
        return false;
    }
    out.clear();
    out.reserve(row_len * h);
    for y in 0..h {
        let row = start + y * stride;
        out.extend_from_slice(&src[row..row + row_len]);
    }
    true
}

/// Whether a `VideoCropMeta` rectangle `(x, y, width, height)` lies inside a `w` x `h` frame.
fn crop_fits((x, y, crop_w, crop_h): (u32, u32, u32, u32), w: usize, h: usize) -> bool {
    x.checked_add(crop_w).map_or(false, |r| r as usize <= w)
        && y.checked_add(crop_h).map_or(false, |b| b as usize <= h)
}

/// GStreamer pixel format for a DRM fourcc name with the same byte order.
fn drm_format_pix_fmt(drm_format: Option<&str>) -> Option<&'static str> {
    match drm_format {
        Some("XR24") => Some("BGRx"),
        Some("XB24") => Some("RGBx"),
        _ => None,
    }
}

fn is_dmabuf_sample(sample: &gst::Sample) -> bool {
    sample
        .get_caps()
        .and_then(|caps| caps.get_features(0))
        .map_or(false, |f| f.contains("memory:DMABuf"))
}

impl PipeWireRecorder {
    // Same steps as `capture` for system memory, with the frame read through the dmabuf fd.
    fn capture_dmabuf(&mut self, sample: gst::Sample) -> Result<PixelProvider<'_>, Box<dyn Error>> {
        let cap = sample
            .get_caps()
            .ok_or("Failed get caps")?
            .get_structure(0)
            .ok_or("Failed to get structure")?;
        let w: i32 = cap.get_value("width")?.get_some()?;
        let h: i32 = cap.get_value("height")?.get_some()?;
        let w = w as usize;
        let h = h as usize;
        let drm_format = cap.get::<&str>("drm-format")?;
        let pix_fmt = drm_format_pix_fmt(drm_format).ok_or_else(|| {
            GStreamerError(format!("Unsupported DMA-BUF format {:?}", drm_format))
        })?;

        let buf = sample
            .get_buffer_owned()
            .ok_or_else(|| GStreamerError("Failed to get owned buffer.".into()))?;
        let mut crop = buf
            .get_meta::<gstreamer_video::VideoCropMeta>()
            .map(|m| m.get_rect());
        // only crop if necessary
        if Some((0, 0, w as u32, h as u32)) == crop {
            crop = None;
        }
        // The compositor sets the crop; one left from an old allocation must not slice past the frame.
        if let Some(c) = crop.filter(|&c| !crop_fits(c, w, h)) {
            trace!(
                "DMA-BUF crop {:?} does not fit {}x{}, dropping it!",
                c,
                w,
                h
            );
            return Ok(PixelProvider::NONE);
        }
        if !read_linear_dmabuf(&buf, w, h, &mut self.dmabuf_frame)? {
            return Ok(PixelProvider::NONE);
        }
        if let Err(..) = crate::would_block_if_equal(&mut self.saved_raw_data, &self.dmabuf_frame) {
            return Ok(PixelProvider::NONE);
        }
        let (width, height, data) = if let Some((x_off, y_off, w_crop, h_crop)) = crop {
            let x_off = x_off as usize;
            let y_off = y_off as usize;
            let w_crop = w_crop as usize;
            let h_crop = h_crop as usize;
            self.buffer_cropped.clear();
            // BGRx is 4 bytes per pixel
            self.buffer_cropped.reserve(w_crop * h_crop * 4);
            for y in y_off..(y_off + h_crop) {
                let i = 4 * (w * y + x_off);
                self.buffer_cropped
                    .extend(&self.dmabuf_frame[i..i + 4 * w_crop]);
            }
            (w_crop, h_crop, self.buffer_cropped.as_slice())
        } else {
            (w, h, self.dmabuf_frame.as_slice())
        };
        match pix_fmt {
            "BGRx" => Ok(PixelProvider::BGR0(width, height, data)),
            "RGBx" => Ok(PixelProvider::RGB0(width, height, data)),
            _ => Err(Box::new(GStreamerError(format!(
                "Unreachable! Unknown pix_fmt, {}",
                pix_fmt
            )))),
        }
    }
}

impl Recorder for PipeWireRecorder {
    fn capture(&mut self, timeout_ms: u64) -> Result<PixelProvider<'_>, Box<dyn Error>> {
        if let Some(sample) = self
            .appsink
            .try_pull_sample(gst::ClockTime::from_mseconds(timeout_ms))
        {
            if is_dmabuf_sample(&sample) {
                return self.capture_dmabuf(sample);
            }
            let cap = sample
                .get_caps()
                .ok_or("Failed get caps")?
                .get_structure(0)
                .ok_or("Failed to get structure")?;
            let w: i32 = cap.get_value("width")?.get_some()?;
            let h: i32 = cap.get_value("height")?.get_some()?;
            let w = w as usize;
            let h = h as usize;
            self.pix_fmt = cap
                .get::<&str>("format")?
                .ok_or("Failed to get pixel format")?
                .to_string();

            let buf = sample
                .get_buffer_owned()
                .ok_or_else(|| GStreamerError("Failed to get owned buffer.".into()))?;
            let mut crop = buf
                .get_meta::<gstreamer_video::VideoCropMeta>()
                .map(|m| m.get_rect());
            // only crop if necessary
            if Some((0, 0, w as u32, h as u32)) == crop {
                crop = None;
            }
            let buf = buf
                .into_mapped_buffer_readable()
                .map_err(|_| GStreamerError("Failed to map buffer.".into()))?;
            if let Err(..) = crate::would_block_if_equal(&mut self.saved_raw_data, buf.as_slice()) {
                return Ok(PixelProvider::NONE);
            }
            let buf_size = buf.get_size();
            // BGRx is 4 bytes per pixel
            if buf_size != (w * h * 4) {
                // for some reason the width and height of the caps do not guarantee correct buffer
                // size, so ignore those buffers, see:
                // https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/985
                trace!(
                    "Size of mapped buffer: {} does NOT match size of capturable {}x{}@BGRx, \
                    dropping it!",
                    buf_size,
                    w,
                    h
                );
            } else {
                // Copy region specified by crop into self.buffer_cropped
                // TODO: Figure out if ffmpeg provides a zero copy alternative
                if let Some((x_off, y_off, w_crop, h_crop)) = crop {
                    let x_off = x_off as usize;
                    let y_off = y_off as usize;
                    let w_crop = w_crop as usize;
                    let h_crop = h_crop as usize;
                    self.buffer_cropped.clear();
                    let data = buf.as_slice();
                    // BGRx is 4 bytes per pixel
                    self.buffer_cropped.reserve(w_crop * h_crop * 4);
                    for y in y_off..(y_off + h_crop) {
                        let i = 4 * (w * y + x_off);
                        self.buffer_cropped.extend(&data[i..i + 4 * w_crop]);
                    }
                    self.width = w_crop;
                    self.height = h_crop;
                } else {
                    self.width = w;
                    self.height = h;
                }
                self.is_cropped = crop.is_some();
                self.buffer = Some(buf);
            }
        } else {
            return Ok(PixelProvider::NONE);
        }
        if self.buffer.is_none() {
            return Err(Box::new(GStreamerError("No buffer available!".into())));
        }
        let buf = if self.is_cropped {
            self.buffer_cropped.as_slice()
        } else {
            self.buffer
                .as_ref()
                .ok_or("Failed to get buffer as ref")?
                .as_slice()
        };
        match self.pix_fmt.as_str() {
            "BGRx" => Ok(PixelProvider::BGR0(self.width, self.height, buf)),
            "RGBx" => Ok(PixelProvider::RGB0(self.width, self.height, buf)),
            _ => Err(Box::new(GStreamerError(format!(
                "Unreachable! Unknown pix_fmt, {}",
                &self.pix_fmt
            )))),
        }
    }
}

impl Drop for PipeWireRecorder {
    fn drop(&mut self) {
        if let Err(err) = self.pipeline.set_state(gst::State::Null) {
            warn!("Failed to stop GStreamer pipeline: {}.", err);
        }
        // Wait for state change to complete to avoid races during PipeWire teardown.
        let _ = self.pipeline.get_state(gst::ClockTime::from_mseconds(2000));
    }
}

// The portal handshake is four sequential requests whose outcomes arrive as asynchronous
// `Response` signals, so where and why it failed is known only inside the signal handler.
// Recording it here, instead of collapsing every outcome into one `failed` flag, is what lets
// the app side name the real cause rather than guess it from the error text.
#[derive(Clone, Copy)]
enum PortalStage {
    CreateSession = 1,
    SelectDevices = 2,
    SelectSources = 3,
    Start = 4,
    OpenPipeWireRemote = 5,
}

impl PortalStage {
    fn as_str(&self) -> &'static str {
        match self {
            Self::CreateSession => "create-session",
            Self::SelectDevices => "select-devices",
            Self::SelectSources => "select-sources",
            Self::Start => "start",
            Self::OpenPipeWireRemote => "open-pipewire-remote",
        }
    }

    fn from_u8(v: u8) -> Self {
        match v {
            2 => Self::SelectDevices,
            3 => Self::SelectSources,
            4 => Self::Start,
            5 => Self::OpenPipeWireRemote,
            _ => Self::CreateSession,
        }
    }
}

// `wl-stage:<stage>:<kind>:<detail>`, parsed by `map_err_scrap` on the app side. The detail
// reaches the user through a `{}` placeholder in a translated string, so it must not bring
// braces, control characters or unbounded length of its own.
const STAGE_TAG: &str = "wl-stage:";

fn stage_err(stage: &str, kind: &str, detail: &str) -> String {
    let detail: String = detail
        .chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .filter(|c| *c != '{' && *c != '}')
        .take(200)
        .collect();
    format!("{}{}:{}:{}", STAGE_TAG, stage, kind, detail.trim())
}

// The name alone is usually the generic `org.freedesktop.DBus.Error.Failed`; the message is
// where a backend says what it objected to. This ends up in the log, so carry both.
fn dbus_stage_err(stage: &str, err: &dbus::Error) -> String {
    let detail = match (err.name(), err.message()) {
        (Some(name), Some(message)) if !name.is_empty() && !message.is_empty() => {
            format!("{}: {}", name, message)
        }
        (Some(name), _) if !name.is_empty() => name.to_owned(),
        (_, message) => message.unwrap_or_default().to_owned(),
    };
    let kind = match err.name().unwrap_or_default() {
        "org.freedesktop.DBus.Error.UnknownMethod"
        | "org.freedesktop.DBus.Error.UnknownInterface" => "unsupported",
        _ => "dbus",
    };
    stage_err(stage, kind, &detail)
}

#[derive(Clone)]
struct PortalTrace {
    failed: Arc<AtomicBool>,
    reason: Arc<Mutex<Option<String>>>,
    // The stage whose `Response` we are still waiting for, so the polling loop can tell a
    // non-interactive step apart from the one that waits for a human.
    waiting_for: Arc<AtomicU8>,
}

impl PortalTrace {
    fn new() -> Self {
        Self {
            failed: Arc::new(AtomicBool::new(false)),
            reason: Arc::new(Mutex::new(None)),
            waiting_for: Arc::new(AtomicU8::new(PortalStage::CreateSession as u8)),
        }
    }

    fn fail(&self, stage: PortalStage, kind: &str, detail: &str) {
        self.record(stage_err(stage.as_str(), kind, detail));
        self.failed.store(true, Ordering::SeqCst);
    }

    // The first failure is the cause; whatever follows it is a consequence.
    fn record(&self, tag: String) {
        if let Ok(mut reason) = self.reason.lock() {
            if reason.is_none() {
                *reason = Some(tag);
            }
        }
    }

    fn waiting(&self, stage: PortalStage) {
        self.waiting_for.store(stage as u8, Ordering::SeqCst);
    }

    fn waiting_stage(&self) -> PortalStage {
        PortalStage::from_u8(self.waiting_for.load(Ordering::SeqCst))
    }

    fn take_reason(&self) -> Option<String> {
        self.reason.lock().ok().and_then(|mut r| r.take())
    }
}

fn handle_response<F>(
    conn: &SyncConnection,
    path: dbus::Path<'static>,
    mut f: F,
    trace: PortalTrace,
    stage: PortalStage,
) -> Result<dbus::channel::Token, dbus::Error>
where
    F: FnMut(
            OrgFreedesktopPortalRequestResponse,
            &SyncConnection,
            &Message,
        ) -> Result<(), Box<dyn Error>>
        + Send
        + Sync
        + 'static,
{
    let mut m = MatchRule::new();
    m.path = Some(path);
    m.msg_type = Some(MessageType::Signal);
    m.sender = Some("org.freedesktop.portal.Desktop".into());
    m.interface = Some("org.freedesktop.portal.Request".into());
    conn.add_match(m, move |r: OrgFreedesktopPortalRequestResponse, c, m| {
        debug!("Response from DBus: response: {:?}, message: {:?}", r, m);
        match r.response {
            0 => {}
            1 => {
                warn!("DBus response: User cancelled interaction.");
                trace.fail(stage, "declined", "");
                return true;
            }
            2 => {
                warn!("DBus response: User interaction ended in some other way.");
                trace.fail(stage, "ended", "");
                return true;
            }
            c => {
                warn!("DBus response: Unknown error, code: {}.", c);
                trace.fail(stage, "portal-error", &c.to_string());
                return true;
            }
        }
        if let Err(err) = f(r, c, m) {
            let text = err.to_string();
            warn!("Error requesting screen capture via dbus: {}", text);
            if text.starts_with(STAGE_TAG) {
                trace.record(text);
                trace.failed.store(true, Ordering::SeqCst);
            } else {
                trace.fail(trace.waiting_stage(), "internal", &text);
            }
        }
        true
    })
}

// The request object path a portal method call will use, derived from our unique
// bus name and the `handle_token` we pass in the call arguments. Knowing it up
// front lets us subscribe to the `Response` signal *before* making the call.
// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Request.html
fn get_request_path(
    conn: &SyncConnection,
    handle_token: &str,
) -> Result<dbus::Path<'static>, dbus::Error> {
    let sender = conn.unique_name().trim_start_matches(':').replace('.', "_");
    dbus::Path::new(format!(
        "/org/freedesktop/portal/desktop/request/{}/{}",
        sender, handle_token
    ))
    .map_err(|_| dbus::Error::new_failed("Failed to construct portal request path"))
}

pub fn get_portal(conn: &SyncConnection) -> Proxy<&SyncConnection> {
    conn.with_proxy(
        "org.freedesktop.portal.Desktop",
        "/org/freedesktop/portal/desktop",
        Duration::from_millis(1000),
    )
}

fn streams_from_response(response: OrgFreedesktopPortalRequestResponse) -> Vec<PwStreamInfo> {
    (move || {
        Some(
            response
                .results
                .get("streams")?
                .as_iter()?
                .next()?
                .as_iter()?
                .filter_map(|stream| {
                    let mut itr = stream.as_iter()?;
                    let path = itr.next()?.as_u64()?;
                    let (keys, values): (Vec<(usize, &dyn RefArg)>, Vec<(usize, &dyn RefArg)>) =
                        itr.next()?
                            .as_iter()?
                            .enumerate()
                            .partition(|(i, _)| i % 2 == 0);
                    let attributes = keys
                        .iter()
                        .filter_map(|(_, key)| Some(key.as_str()?.to_owned()))
                        .zip(
                            values
                                .iter()
                                .map(|(_, arg)| *arg)
                                .collect::<Vec<&dyn RefArg>>(),
                        )
                        .collect::<HashMap<String, &dyn RefArg>>();
                    let mut info = PwStreamInfo {
                        path,
                        source_type: attributes
                            .get("source_type")
                            .map_or(Some(0), |v| v.as_u64())?,
                        position: (0, 0),
                        size: (0, 0),
                    };
                    let v = attributes
                        .get("size")?
                        .as_iter()?
                        .filter_map(|v| {
                            Some(
                                v.as_iter()?
                                    .map(|x| x.as_i64().unwrap_or(0))
                                    .collect::<Vec<i64>>(),
                            )
                        })
                        .next();
                    if let Some(v) = v {
                        if v.len() == 2 {
                            info.size.0 = v[0] as _;
                            info.size.1 = v[1] as _;
                        }
                    }
                    if let Some(pos) = attributes.get("position") {
                        let v = pos
                            .as_iter()?
                            .filter_map(|v| {
                                Some(
                                    v.as_iter()?
                                        .map(|x| x.as_i64().unwrap_or(0))
                                        .collect::<Vec<i64>>(),
                                )
                            })
                            .next();
                        if let Some(v) = v {
                            if v.len() == 2 {
                                info.position.0 = v[0] as _;
                                info.position.1 = v[1] as _;
                                HAS_POSITION_ATTR.store(true, Ordering::SeqCst);
                            }
                        }
                    }
                    Some(info)
                })
                .collect::<Vec<PwStreamInfo>>(),
        )
    })()
    .unwrap_or_default()
}

static mut INIT: bool = false;
const RESTORE_TOKEN: &str = "restore_token";
const RESTORE_TOKEN_CONF_KEY: &str = "wayland-restore-token";
const PIPEWIRE_DISPLAY_OFFSET_CONF_KEY: &str = "wayland-pipewire-display-offset";

pub fn get_available_cursor_modes() -> Result<u32, dbus::Error> {
    let conn = SyncConnection::new_session()?;
    let portal = get_portal(&conn);
    portal.available_cursor_modes()
}

// mostly inspired by https://gitlab.gnome.org/-/snippets/39
pub fn request_remote_desktop(
    capture_cursor: bool,
) -> ResultType<(
    SyncConnection,
    OwnedFd,
    Vec<PwStreamInfo>,
    dbus::Path<'static>,
    bool,
)> {
    unsafe {
        if !INIT {
            gstreamer::init()?;
            INIT = true;
        }
    }
    let conn =
        SyncConnection::new_session().map_err(|e| anyhow!(dbus_stage_err("session-bus", &e)))?;
    let portal = get_portal(&conn);
    let mut args: PropMap = HashMap::new();
    let fd: Arc<Mutex<Option<OwnedFd>>> = Arc::new(Mutex::new(None));
    let fd_res = fd.clone();
    let streams: Arc<Mutex<Vec<PwStreamInfo>>> = Arc::new(Mutex::new(Vec::new()));
    let streams_res = streams.clone();
    let trace = PortalTrace::new();
    let trace_res = trace.clone();
    let session: Arc<Mutex<Option<dbus::Path>>> = Arc::new(Mutex::new(None));
    let session_res = session.clone();
    let create_session_handle_token = "u1";
    args.insert(
        "session_handle_token".to_string(),
        Variant(Box::new(create_session_handle_token.to_string())),
    );
    args.insert(
        "handle_token".to_string(),
        Variant(Box::new(create_session_handle_token.to_string())),
    );

    let mut is_support_restore_token = false;
    if let Ok(version) = screencast_portal::version(&portal) {
        if version >= 4 {
            is_support_restore_token = true;
        }
    }

    // The following code may be improved.
    // https://flatpak.github.io/xdg-desktop-portal/#:~:text=To%20avoid%20a%20race%20condition
    // To avoid a race condition
    // between the caller subscribing to the signal after receiving the reply for the method call and the signal getting emitted,
    // a convention for Request object paths has been established that allows
    // the caller to subscribe to the signal before making the method call.
    handle_response(
        &conn,
        get_request_path(&conn, create_session_handle_token)
            .map_err(|e| anyhow!(dbus_stage_err("create-session", &e)))?,
        on_create_session_response(
            fd.clone(),
            streams.clone(),
            session.clone(),
            trace.clone(),
            is_support_restore_token,
            capture_cursor,
        ),
        trace.clone(),
        PortalStage::CreateSession,
    )
    .map_err(|e| anyhow!(dbus_stage_err("create-session", &e)))?;
    if is_server_running() {
        let _ = screencast_portal::create_session(&portal, args)
            .map_err(|e| anyhow!(dbus_stage_err("create-session", &e)))?;
    } else {
        let _ = remote_desktop_portal::create_session(&portal, args)
            .map_err(|e| anyhow!(dbus_stage_err("create-session", &e)))?;
    }

    // wait 3 minutes for user interaction
    for _ in 0..1800 {
        conn.process(Duration::from_millis(100))
            .map_err(|e| anyhow!(dbus_stage_err(trace_res.waiting_stage().as_str(), &e)))?;
        // Once we got a file descriptor we are done!
        if fd_res.lock().unwrap().is_some() {
            break;
        }

        if trace_res.failed.load(Ordering::SeqCst) {
            break;
        }
    }
    let fd_res = fd_res.lock().unwrap();
    let streams_res = streams_res.lock().unwrap();
    let session_res = session_res.lock().unwrap();
    let have_fd = fd_res.is_some();

    if let Some(fd_res) = fd_res.clone() {
        if let Some(session) = session_res.clone() {
            if !streams_res.is_empty() {
                return Ok((
                    conn,
                    fd_res,
                    streams_res.clone(),
                    session,
                    is_support_restore_token,
                ));
            }
        }
    }
    bail!(trace_res.take_reason().unwrap_or_else(|| {
        if have_fd {
            stage_err("streams", "empty", "")
        } else {
            stage_err(trace_res.waiting_stage().as_str(), "no-response", "")
        }
    }))
}

fn on_create_session_response(
    fd: Arc<Mutex<Option<OwnedFd>>>,
    streams: Arc<Mutex<Vec<PwStreamInfo>>>,
    session: Arc<Mutex<Option<dbus::Path<'static>>>>,
    trace: PortalTrace,
    is_support_restore_token: bool,
    capture_cursor: bool,
) -> impl Fn(
    OrgFreedesktopPortalRequestResponse,
    &SyncConnection,
    &dbus::Message,
) -> Result<(), Box<dyn Error>> {
    move |r: OrgFreedesktopPortalRequestResponse, c, _| {
        let ses: dbus::Path = r
            .results
            .get("session_handle")
            .ok_or_else(|| {
                DBusError(format!(
                    "Failed to obtain session_handle from response: {:?}",
                    r
                ))
            })?
            .as_str()
            .ok_or_else(|| DBusError("Failed to convert session_handle to string.".into()))?
            .to_string()
            .into();

        let mut session = match session.lock() {
            Ok(session) => session,
            Err(_) => return Err(Box::new(DBusError("Failed to lock session.".into()))),
        };
        session.replace(ses.clone());

        let portal = get_portal(c);
        let mut args: PropMap = HashMap::new();
        // See `is_server_running()` to understand the following code.
        if is_server_running() {
            if is_support_restore_token {
                let restore_token = config::LocalConfig::get_option(RESTORE_TOKEN_CONF_KEY);
                if !restore_token.is_empty() {
                    args.insert(RESTORE_TOKEN.to_string(), Variant(Box::new(restore_token)));
                }
                // persist_mode may be configured by the user.
                args.insert("persist_mode".to_string(), Variant(Box::new(2u32)));
            }
            let select_sources_handle_token = "u3";
            args.insert(
                "handle_token".to_string(),
                Variant(Box::new(select_sources_handle_token.to_string())),
            );
            // https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.ScreenCast.html
            if is_server_running() {
                args.insert("multiple".into(), Variant(Box::new(true)));
            }
            args.insert("types".into(), Variant(Box::new(1u32))); //| 2u32)));

            if capture_cursor {
                get_available_cursor_modes().ok().map(|modes| {
                    if modes & 0x2 != 0 {
                        args.insert("cursor_mode".to_string(), Variant(Box::new(2u32)));
                    }
                });
            }

            trace.waiting(PortalStage::SelectSources);
            handle_response(
                c,
                get_request_path(c, select_sources_handle_token)?,
                on_select_sources_response(
                    fd.clone(),
                    streams.clone(),
                    trace.clone(),
                    ses.clone(),
                    is_support_restore_token,
                ),
                trace.clone(),
                PortalStage::SelectSources,
            )?;
            let _ = portal
                .select_sources(ses.clone(), args)
                .map_err(|e| DBusError(dbus_stage_err("select-sources", &e)))?;
        } else {
            // TODO: support persist_mode for remote_desktop_portal
            // https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.RemoteDesktop.html

            let select_devices_handle_token = "u2";
            args.insert(
                "handle_token".to_string(),
                Variant(Box::new(select_devices_handle_token.to_string())),
            );
            args.insert("types".to_string(), Variant(Box::new(7u32)));

            trace.waiting(PortalStage::SelectDevices);
            handle_response(
                c,
                get_request_path(c, select_devices_handle_token)?,
                on_select_devices_response(
                    fd.clone(),
                    streams.clone(),
                    trace.clone(),
                    ses.clone(),
                    is_support_restore_token,
                ),
                trace.clone(),
                PortalStage::SelectDevices,
            )?;
            let _ = portal
                .select_devices(ses.clone(), args)
                .map_err(|e| DBusError(dbus_stage_err("select-devices", &e)))?;
        }

        Ok(())
    }
}

fn on_select_devices_response(
    fd: Arc<Mutex<Option<OwnedFd>>>,
    streams: Arc<Mutex<Vec<PwStreamInfo>>>,
    trace: PortalTrace,
    session: dbus::Path<'static>,
    is_support_restore_token: bool,
) -> impl Fn(
    OrgFreedesktopPortalRequestResponse,
    &SyncConnection,
    &dbus::Message,
) -> Result<(), Box<dyn Error>> {
    move |_: OrgFreedesktopPortalRequestResponse, c, _| {
        let portal = get_portal(c);
        let mut args: PropMap = HashMap::new();
        let select_sources_handle_token = "u3";
        args.insert(
            "handle_token".to_string(),
            Variant(Box::new(select_sources_handle_token.to_string())),
        );
        // https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.ScreenCast.html
        if is_server_running() {
            args.insert("multiple".into(), Variant(Box::new(true)));
        }
        args.insert("types".into(), Variant(Box::new(1u32))); //| 2u32)));

        let session = session.clone();
        trace.waiting(PortalStage::SelectSources);
        handle_response(
            c,
            get_request_path(c, select_sources_handle_token)?,
            on_select_sources_response(
                fd.clone(),
                streams.clone(),
                trace.clone(),
                session.clone(),
                is_support_restore_token,
            ),
            trace.clone(),
            PortalStage::SelectSources,
        )?;
        let _ = portal
            .select_sources(session.clone(), args)
            .map_err(|e| DBusError(dbus_stage_err("select-sources", &e)))?;

        Ok(())
    }
}

fn on_select_sources_response(
    fd: Arc<Mutex<Option<OwnedFd>>>,
    streams: Arc<Mutex<Vec<PwStreamInfo>>>,
    trace: PortalTrace,
    session: dbus::Path<'static>,
    is_support_restore_token: bool,
) -> impl Fn(
    OrgFreedesktopPortalRequestResponse,
    &SyncConnection,
    &dbus::Message,
) -> Result<(), Box<dyn Error>> {
    move |_: OrgFreedesktopPortalRequestResponse, c, _| {
        let portal = get_portal(c);
        let mut args: PropMap = HashMap::new();
        let start_handle_token = "u4";
        args.insert(
            "handle_token".to_string(),
            Variant(Box::new(start_handle_token.to_string())),
        );
        trace.waiting(PortalStage::Start);
        handle_response(
            c,
            get_request_path(c, start_handle_token)?,
            on_start_response(
                fd.clone(),
                streams.clone(),
                session.clone(),
                trace.clone(),
                is_support_restore_token,
            ),
            trace.clone(),
            PortalStage::Start,
        )?;
        if is_server_running() {
            let _ = screencast_portal::start(&portal, session.clone(), "", args)
                .map_err(|e| DBusError(dbus_stage_err("start", &e)))?;
        } else {
            let _ = remote_desktop_portal::start(&portal, session.clone(), "", args)
                .map_err(|e| DBusError(dbus_stage_err("start", &e)))?;
        }

        Ok(())
    }
}

fn on_start_response(
    fd: Arc<Mutex<Option<OwnedFd>>>,
    streams: Arc<Mutex<Vec<PwStreamInfo>>>,
    session: dbus::Path<'static>,
    trace: PortalTrace,
    is_support_restore_token: bool,
) -> impl Fn(
    OrgFreedesktopPortalRequestResponse,
    &SyncConnection,
    &dbus::Message,
) -> Result<(), Box<dyn Error>> {
    move |r: OrgFreedesktopPortalRequestResponse, c, _| {
        let portal = get_portal(c);
        // See `is_server_running()` to understand the following code.
        if is_server_running() {
            if is_support_restore_token {
                if let Some(restore_token) = r.results.get(RESTORE_TOKEN) {
                    if let Some(restore_token) = restore_token.as_str() {
                        config::LocalConfig::set_option(
                            RESTORE_TOKEN_CONF_KEY.to_owned(),
                            restore_token.to_owned(),
                        );
                    }
                }
            }
        }

        streams
            .clone()
            .lock()
            .unwrap()
            .append(&mut streams_from_response(r));
        // Past this point the user has granted the request; anything that fails now is the
        // hand-over of the PipeWire fd, which is a different thing to go looking at.
        trace.waiting(PortalStage::OpenPipeWireRemote);
        fd.clone().lock().unwrap().replace(
            portal
                .open_pipe_wire_remote(session.clone(), HashMap::new())
                .map_err(|e| DBusError(dbus_stage_err("open-pipewire-remote", &e)))?,
        );

        Ok(())
    }
}

pub fn get_capturables() -> Result<Vec<PipeWireCapturable>, Box<dyn Error>> {
    let mut rdp_connection = match RDP_SESSION_INFO.lock() {
        Ok(conn) => conn,
        Err(err) => return Err(Box::new(err)),
    };

    if rdp_connection.is_none() {
        let (conn, fd, streams, session, is_support_restore_token) = request_remote_desktop(false)?;
        let conn = Arc::new(conn);

        let rdp_info = RdpSessionInfo {
            conn,
            streams,
            fd,
            session,
            is_support_restore_token,
            resolution: Arc::new(Mutex::new(None)),
        };
        *rdp_connection = Some(rdp_info);
    }

    let rdp_info = match rdp_connection.as_mut() {
        Some(res) => res,
        None => {
            return Err(Box::new(DBusError("RDP response is None.".into())));
        }
    };

    Ok(rdp_info
        .streams
        .iter()
        .map(|s| {
            PipeWireCapturable::new(
                rdp_info.conn.clone(),
                rdp_info.fd.clone(),
                rdp_info.resolution.clone(),
                s,
            )
        })
        .collect())
}

// If `is_server_running()` is true, then `screencast_portal::start` is called.
// Otherwise, `remote_desktop_portal::start` is called.
//
// If `is_server_running()` is true, `--service` process is running,
// then we can use uinput as the input method.
// Otherwise, we have to use remote_desktop_portal's input method.
//
// `screencast_portal` supports restore_token and persist_mode if the version is greater than or equal to 4.
// `remote_desktop_portal` does not support restore_token and persist_mode.
pub(crate) fn is_server_running() -> bool {
    let v = IS_SERVER_RUNNING.load(Ordering::SeqCst);
    if v > 0 {
        return v == 1;
    }

    let app_name = config::APP_NAME.read().unwrap().clone().to_lowercase();
    let output = match Command::new(CMD_SH.as_str())
        .arg("-c")
        .arg(&format!("ps aux | grep {}", app_name))
        .output()
    {
        Ok(output) => output,
        Err(_) => {
            return false;
        }
    };

    let output_str = String::from_utf8_lossy(&output.stdout);
    let is_running = output_str.contains(&format!("{} --server", app_name));
    IS_SERVER_RUNNING.store(if is_running { 1 } else { 2 }, Ordering::SeqCst);
    is_running
}

// The logical size reported by portal may be different from the size reported by `get_displays()`.
// So we need to use the workaround here.
// 1. openSUSE, KDE Plasma
// 2. Kubuntu 24.04 TLS, after running `sudo apt install plasma-workspace-wayland`
// Maybe it's a bug, and we can remove this workaround in the future.
pub fn try_fix_logical_size(shared_displays: &mut Vec<crate::Display>) {
    if !is_server_running() {
        return;
    }

    let wayland_displays = get_displays();
    if wayland_displays.displays.is_empty() {
        return;
    }

    for sd in shared_displays.iter_mut() {
        if let crate::Display::WAYLAND(d) = sd {
            let capturable = &mut d.0;
            for wd in wayland_displays.displays.iter() {
                if capturable.position.0 == wd.x && capturable.position.1 == wd.y {
                    if let Some(logical_size) = wd.logical_size {
                        if capturable.physical_size.0 != wd.width as usize
                            || capturable.physical_size.1 != wd.height as usize
                        {
                            // If "Full Workspace" is selected in the portal dialog,
                            // the physical size reported by portal may not match the display info.
                            debug!(
                            "Physical size of capturable ({:?}) does not match display info: ({:?}) - ({:?}). Skipping logical size fix.",
                            capturable.position,
                            capturable.physical_size,
                            (wd.width as usize, wd.height as usize)
                        );
                            break;
                        }

                        if capturable.logical_size.0 != logical_size.0 as usize
                            || capturable.logical_size.1 != logical_size.1 as usize
                        {
                            warn!(
                            "Fixing logical size of capturable from {:?} to {:?} based on display info {:?}.",
                            capturable.logical_size,
                            logical_size,
                            wd
                        );
                            capturable.logical_size =
                                (logical_size.0 as usize, logical_size.1 as usize);
                        }
                    }
                    break;
                }
            }
        }
    }
}

pub fn fill_displays(
    mouse_move_to: impl Fn(i32, i32),
    get_cursor_pos: fn() -> Option<(i32, i32)>,
    shared_displays: &mut Vec<crate::Display>,
) -> ResultType<()> {
    if !is_server_running() {
        return Ok(());
    }

    let mut rdp_connection = RDP_SESSION_INFO.lock().unwrap();
    let rdp_info = match rdp_connection.as_mut() {
        Some(res) => res,
        None => {
            // Unreachable
            bail!("RDP session info is None when filling display positions.");
        }
    };

    let all_displays = get_displays();
    if !HAS_POSITION_ATTR.load(Ordering::SeqCst) {
        if all_displays.displays.len() > 1 {
            debug!("Multiple Wayland displays detected, adjusting stream positions accordingly.");
            try_fill_positions(
                mouse_move_to,
                get_cursor_pos,
                &all_displays,
                shared_displays,
                &mut rdp_info.streams,
            )?;
        }
        HAS_POSITION_ATTR.store(true, Ordering::SeqCst);
    }

    if all_displays.displays.len() > 1 {
        for (i, sd) in shared_displays.iter_mut().enumerate() {
            if let crate::Display::WAYLAND(d) = sd {
                let capturable = &mut d.0;
                if let Some(origin) = reconciled_origin(
                    capturable.position,
                    capturable.physical_size,
                    capturable.physical_size_measured,
                    &all_displays.displays,
                ) {
                    warn!(
                        "Position {:?} names no output of size {:?}, using {:?}.",
                        capturable.position, capturable.physical_size, origin
                    );
                    capturable.position = origin;
                    if let Some(pw_stream) = rdp_info.streams.get_mut(i) {
                        pw_stream.position = origin;
                    }
                }
            }
        }
        sort_streams(&all_displays, shared_displays, &mut rdp_info.streams);
    }

    shared_displays.iter_mut().next().map(|d| {
        if let crate::Display::WAYLAND(d) = d {
            d.0.primary = true;
        }
    });

    Ok(())
}

// The origin a stream takes when its position names no output of its size. Only a measured size
// moves a position: when get_res() failed, the size is the portal size, which can be the mode of
// another output and says less than the position does, whether the portal sent that position or
// try_fill_positions restored it from its cache.
fn reconciled_origin(
    position: (i32, i32),
    size: (usize, usize),
    size_measured: bool,
    outputs: &[WaylandDisplayInfo],
) -> Option<(i32, i32)> {
    if !size_measured {
        return None;
    }
    corrected_origin(position, size, outputs)
}

// The origin for a stream whose position names no output of the stream's size (a rotated output
// counts in either orientation), when exactly one output has that size. xdg-desktop-portal-hyprland reports every stream at (0, 0):
// with no output there `sort_streams` dropped the stream, and with another output there the
// stream took its origin and missed `try_fix_logical_size`, so the pointer landed on the wrong
// output (#15731). With several outputs of that size the position is kept.
fn corrected_origin(
    position: (i32, i32),
    size: (usize, usize),
    outputs: &[WaylandDisplayInfo],
) -> Option<(i32, i32)> {
    let has_size = |o: &&WaylandDisplayInfo| {
        let mode = (o.width as usize, o.height as usize);
        mode == size || (o.transform % 180 != 0 && (mode.1, mode.0) == size)
    };
    if outputs
        .iter()
        .filter(has_size)
        .any(|o| (o.x, o.y) == position)
    {
        return None;
    }
    let mut same_size = outputs.iter().filter(has_size);
    match (same_size.next(), same_size.next()) {
        (Some(o), None) => Some((o.x, o.y)),
        _ => None,
    }
}

fn try_fill_positions(
    mouse_move_to: impl Fn(i32, i32),
    get_cursor_pos: fn() -> Option<(i32, i32)>,
    displays: &Arc<Displays>,
    shared_displays: &mut Vec<crate::Display>,
    streams: &mut Vec<PwStreamInfo>,
) -> ResultType<()> {
    let pipewire_display_offset = config::LocalConfig::get_option(PIPEWIRE_DISPLAY_OFFSET_CONF_KEY);
    if !pipewire_display_offset.is_empty() {
        if try_fill_positions_from_cache(
            pipewire_display_offset,
            displays,
            shared_displays,
            streams,
        ) {
            return Ok(());
        }
        config::LocalConfig::set_option(PIPEWIRE_DISPLAY_OFFSET_CONF_KEY.to_owned(), "".to_owned());
    }

    let mut multi_matched_indices = Vec::new();
    for (i, sd) in shared_displays.iter_mut().enumerate() {
        if let crate::Display::WAYLAND(d) = sd {
            let capturable = &mut d.0;
            let mut match_count = 0;
            for wd in displays.displays.iter() {
                if capturable.physical_size.0 == wd.width as usize
                    && capturable.physical_size.1 == wd.height as usize
                {
                    capturable.position = (wd.x, wd.y);
                    if let Some(pw_stream) = streams.get_mut(i) {
                        pw_stream.position = (wd.x, wd.y);
                    }
                    match_count += 1;
                }
            }
            if match_count == 0 {
                warn!(
                    "No matching display found for capturable with size {:?}.",
                    capturable.physical_size
                );
            } else if match_count > 1 {
                multi_matched_indices.push(i);
            }
        }
    }

    if !multi_matched_indices.is_empty() {
        fill_multi_matched_positions(
            mouse_move_to,
            get_cursor_pos,
            displays,
            shared_displays,
            streams,
            multi_matched_indices,
        )?;
    }

    save_positions_to_cache(displays, shared_displays);
    Ok(())
}

fn try_fill_positions_from_cache(
    cache_str: String,
    displays: &Arc<Displays>,
    shared_displays: &mut Vec<crate::Display>,
    streams: &mut Vec<PwStreamInfo>,
) -> bool {
    let Ok(cache) = serde_json::from_str::<PipewireDisplayOffsetCache>(&cache_str) else {
        return false;
    };

    if cache.offsets.len() != shared_displays.len() {
        return false;
    }

    let display_key = PipewireDisplayOffsetCache::displays_to_key(displays);
    if cache.display_key != display_key {
        return false;
    }

    let restore_token = config::LocalConfig::get_option(RESTORE_TOKEN_CONF_KEY);
    if cache.restore_token != restore_token {
        return false;
    }

    for (i, sd) in shared_displays.iter_mut().enumerate() {
        if let crate::Display::WAYLAND(d) = sd {
            let capturable = &mut d.0;
            if let Some((x_off, y_off)) = cache.offsets.get(i) {
                capturable.position = (*x_off, *y_off);
                if let Some(pw_stream) = streams.get_mut(i) {
                    pw_stream.position = (*x_off, *y_off);
                }
            }
        }
    }
    true
}

fn save_positions_to_cache(displays: &Arc<Displays>, shared_displays: &Vec<crate::Display>) {
    let restore_token = config::LocalConfig::get_option(RESTORE_TOKEN_CONF_KEY);
    if restore_token.is_empty() {
        return;
    }

    let mut offsets = Vec::new();
    for sd in shared_displays.iter() {
        if let crate::Display::WAYLAND(d) = sd {
            let capturable = &d.0;
            offsets.push((capturable.position.0, capturable.position.1));
        }
    }

    let display_key = PipewireDisplayOffsetCache::displays_to_key(displays);
    let cache = PipewireDisplayOffsetCache {
        display_key,
        restore_token,
        offsets,
    };

    if let Ok(s) = serde_json::to_string(&cache) {
        config::LocalConfig::set_option(PIPEWIRE_DISPLAY_OFFSET_CONF_KEY.to_owned(), s);
    }
}

fn compare_left_up_corner(w: usize, d1: &[u8], d2: &[u8]) -> bool {
    if w == 0 {
        return false;
    }
    if d1.len() != d2.len() {
        return false;
    }
    let bpp = 4; // BGR0/RGB0
    let stride = w.saturating_mul(bpp);
    if stride == 0 || d1.len() < stride || d2.len() < stride {
        return false;
    }
    let h = d1.len() / stride;
    if h == 0 {
        return false;
    }

    let roi_w = std::cmp::min(36, w);
    let roi_h = std::cmp::min(36, h);
    let mut diff_px = 0usize;
    let total_px = roi_w * roi_h;
    // Minimum number of differing pixels required to consider images different.
    const MIN_DIFF_PIXELS: usize = 8;
    // Divisor for threshold calculation: allows up to 1/8 of ROI pixels to differ before returning true.
    const DIFF_THRESHOLD_DIVISOR: usize = 8;
    let threshold = std::cmp::max(MIN_DIFF_PIXELS, total_px / DIFF_THRESHOLD_DIVISOR);

    for y in 0..roi_h {
        let row_off = y * stride;
        for x in 0..roi_w {
            let i = row_off + x * bpp;
            let a = &d1[i..i + bpp];
            let b = &d2[i..i + bpp];
            if a != b {
                diff_px += 1;
                if diff_px >= threshold {
                    return true;
                }
            }
        }
    }
    false
}

fn fill_multi_matched_positions(
    mouse_move_to: impl Fn(i32, i32),
    get_cursor_pos: fn() -> Option<(i32, i32)>,
    displays: &Arc<Displays>,
    shared_displays: &mut Vec<crate::Display>,
    streams: &mut Vec<PwStreamInfo>,
    multi_matched_indices: Vec<usize>,
) -> ResultType<()> {
    debug!(
        "Multiple capturables ({:?}) match the same display size, attempting to disambiguate positions.",
    &multi_matched_indices);
    if multi_matched_indices.is_empty() {
        return Ok(());
    }

    let is_support_embeded_cursor = get_available_cursor_modes()
        .ok()
        .map(|modes| modes & 0x2 != 0)
        .unwrap_or(false);
    if is_support_embeded_cursor {
        fill_multi_matched_positions_cursor(
            mouse_move_to,
            get_cursor_pos,
            displays,
            shared_displays,
            streams,
            multi_matched_indices,
        )?;
    }

    Ok(())
}

fn mouse_move_to_(
    mouse_move_to: &impl Fn(i32, i32),
    get_cursor_pos: fn() -> Option<(i32, i32)>,
    x: i32,
    y: i32,
) {
    const MOVE_MOUSE_TIMEOUT: Duration = Duration::from_millis(150);
    let start = std::time::Instant::now();
    while start.elapsed() < MOVE_MOUSE_TIMEOUT {
        mouse_move_to(x, y);
        std::thread::sleep(Duration::from_millis(20));
        if let Some((x1, y1)) = get_cursor_pos() {
            if x1 == x && y1 == y {
                return;
            }
        }
    }
    warn!(
        "Failed to move mouse to ({}, {}) within timeout: {:?}.",
        x, y, &MOVE_MOUSE_TIMEOUT
    );
}

fn fill_multi_matched_positions_cursor(
    mouse_move_to: impl Fn(i32, i32),
    get_cursor_pos: fn() -> Option<(i32, i32)>,
    displays: &Arc<Displays>,
    shared_displays: &mut Vec<crate::Display>,
    streams: &mut Vec<PwStreamInfo>,
    multi_matched_indices: Vec<usize>,
) -> ResultType<()> {
    // This creates a new remote desktop session for cursor-based position detection.
    // The session is temporary, used only for disambiguation, and is dropped after detection completes.
    let (conn, fd, streams_with_cursor, _session, _is_support_restore_token) =
        request_remote_desktop(true)?;
    let conn = Arc::new(conn);

    let mut matched_indices = Vec::new();
    const CAPTURE_TIMEOUT_MS: u64 = 1_000;
    for idx in multi_matched_indices {
        match (
            shared_displays.get_mut(idx),
            streams.get_mut(idx),
            streams_with_cursor.get(idx),
        ) {
            (Some(crate::Display::WAYLAND(d)), Some(pw_stream), Some(pw_stream_with_cursor)) => {
                // Check if only one display matches the size
                let mut match_count = 0;
                for (i, wd) in displays.displays.iter().enumerate() {
                    if matched_indices.contains(&i) {
                        continue;
                    }
                    if d.0.physical_size.0 == wd.width as usize
                        && d.0.physical_size.1 == wd.height as usize
                    {
                        match_count += 1;
                    }
                }
                if match_count == 0 {
                    error!(
                        "No matching display found for capturable with size {:?}.",
                        d.0.physical_size
                    );
                    continue;
                }
                if match_count == 1 {
                    for (i, wd) in displays.displays.iter().enumerate() {
                        if matched_indices.contains(&i) {
                            continue;
                        }
                        if d.0.physical_size.0 == wd.width as usize
                            && d.0.physical_size.1 == wd.height as usize
                        {
                            d.0.position = (wd.x, wd.y);
                            pw_stream.position = (wd.x, wd.y);
                            matched_indices.push(i);
                            debug!(
                                "Disambiguated position for capturable with size {:?} to ({}, {}).",
                                d.0.physical_size, wd.x, wd.y
                            );
                            break;
                        }
                    }
                    continue;
                }

                // Move the mouse to a neutral position first,
                // to avoid interference from previous position.
                mouse_move_to_(&mouse_move_to, get_cursor_pos, 300, 300);

                let mut rec = PipeWireRecorder::new(PipeWireCapturable {
                    dbus_conn: conn.clone(),
                    fd: fd.clone(),
                    path: pw_stream_with_cursor.path,
                    source_type: pw_stream_with_cursor.source_type,
                    primary: false,
                    position: pw_stream_with_cursor.position,
                    logical_size: pw_stream_with_cursor.size,
                    physical_size: (0, 0),
                    physical_size_measured: false,
                })?;
                // Take first frame and copy owned buffer to avoid borrow across second capture
                let (is_bgr, w, first_buf): (bool, usize, Vec<u8>) =
                    match rec.capture(CAPTURE_TIMEOUT_MS) {
                        Ok(PixelProvider::BGR0(w, _, data1)) => (true, w, data1.to_vec()),
                        Ok(PixelProvider::RGB0(w, _, data1)) => (false, w, data1.to_vec()),
                        Ok(_) => {
                            error!("Unexpected pixel format on first capture.");
                            continue;
                        }
                        Err(e) => {
                            error!(
                                "Failed to capture screen for position disambiguation: {}",
                                e
                            );
                            continue;
                        }
                    };

                let matched_len = matched_indices.len();
                for (i, wd) in displays.displays.iter().enumerate() {
                    if matched_indices.contains(&i) {
                        continue;
                    }

                    if wd.width as usize == d.0.physical_size.0
                        && wd.height as usize == d.0.physical_size.1
                    {
                        mouse_move_to_(&mouse_move_to, get_cursor_pos, wd.x + 8, wd.y + 8);
                        rec.saved_raw_data.clear();
                        match rec.capture(CAPTURE_TIMEOUT_MS) {
                            Ok(PixelProvider::BGR0(_, _, data2)) if is_bgr => {
                                if compare_left_up_corner(w, &first_buf, data2) {
                                    d.0.position = (wd.x, wd.y);
                                    pw_stream.position = (wd.x, wd.y);
                                    matched_indices.push(i);
                                    debug!(
                                        "Disambiguated position for capturable with size {:?} to ({}, {}).",
                                        d.0.physical_size, wd.x, wd.y
                                    );
                                    break;
                                }
                            }
                            Ok(PixelProvider::RGB0(_, _, data2)) if !is_bgr => {
                                if compare_left_up_corner(w, &first_buf, data2) {
                                    d.0.position = (wd.x, wd.y);
                                    pw_stream.position = (wd.x, wd.y);
                                    matched_indices.push(i);
                                    debug!(
                                        "Disambiguated position for capturable with size {:?} to ({}, {}).",
                                        d.0.physical_size, wd.x, wd.y
                                    );
                                    break;
                                }
                            }
                            Ok(_) => {
                                // unreachable
                                error!("Pixel format changed between captures, cannot disambiguate position.");
                            }
                            Err(e) => {
                                error!(
                                    "Failed to capture screen for position disambiguation: {}",
                                    e
                                );
                            }
                        }
                    }
                }
                if matched_len == matched_indices.len() {
                    error!(
                        "Failed to disambiguate position for capturable with size {:?}.",
                        d.0.physical_size
                    );
                }
            }
            _ => {}
        }
    }

    Ok(())
}

fn sort_streams(
    displays: &Arc<Displays>,
    shared_displays: &mut Vec<crate::Display>,
    streams: &mut Vec<PwStreamInfo>,
) {
    if streams.is_empty() {
        // unreachable
        error!("No streams available to sort.");
        return;
    }

    // put the main display first, then the rest by the order of displays
    let mut display_order: Vec<(i32, i32)> = Vec::new();
    if let Some(d) = displays.displays.get(displays.primary) {
        display_order.push((d.x, d.y));
    }
    for (i, d) in displays.displays.iter().enumerate() {
        if i != displays.primary {
            display_order.push((d.x, d.y));
        }
    }

    let mut sorted_streams = Vec::new();
    let mut sorted_shared_displays = Vec::new();
    // Move matching items in order without cloning
    for (x, y) in display_order.into_iter() {
        for i in 0..streams.len() {
            if streams[i].position.0 == x && streams[i].position.1 == y {
                sorted_streams.push(streams.remove(i));
                // shared_displays.len() must be equal to streams.len()
                // But we still check the length to avoid panic
                if shared_displays.len() > i {
                    sorted_shared_displays.push(shared_displays.remove(i));
                }
                break;
            }
        }
    }
    // Keep a stream whose position names no display, e.g. a full-workspace stream at (0, 0) when
    // no display sits there (#17027). A stream at an origin another stream already took is still
    // dropped.
    for (s, d) in streams.drain(..).zip(shared_displays.drain(..)) {
        if !displays
            .displays
            .iter()
            .any(|wd| (wd.x, wd.y) == s.position)
        {
            sorted_streams.push(s);
            sorted_shared_displays.push(d);
        }
    }
    *streams = sorted_streams;
    *shared_displays = sorted_shared_displays;
}

#[cfg(test)]
mod tests {
    use super::{corrected_origin, drm_format_pix_fmt, pack_rows, WaylandDisplayInfo};

    #[test]
    fn a_strided_dmabuf_frame_comes_out_as_packed_rows() {
        // 2x2 BGRx frame at offset 3 with stride 12: 4 bytes of padding after each row.
        let mut src = vec![0xEE; 3 + 12 * 2];
        src[3..11].copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8]);
        src[15..23].copy_from_slice(&[9, 10, 11, 12, 13, 14, 15, 16]);
        let mut out = Vec::new();
        assert!(pack_rows(&src, 3, 12, 8, 2, &mut out));
        assert_eq!(out, (1..=16).collect::<Vec<u8>>());
    }

    #[test]
    fn a_dmabuf_too_small_for_its_stride_is_dropped() {
        let mut out = Vec::new();
        assert!(pack_rows(&[1; 20], 0, 12, 8, 2, &mut out));
        let frame = out.clone();
        assert!(!pack_rows(&[2; 19], 0, 12, 8, 2, &mut out));
        // A dropped frame leaves the last good one in place.
        assert_eq!(out, frame);
    }

    #[test]
    fn niri_drm_formats_map_to_encoder_pixel_formats() {
        assert_eq!(drm_format_pix_fmt(Some("XR24")), Some("BGRx"));
        assert_eq!(drm_format_pix_fmt(Some("XB24")), Some("RGBx"));
        assert_eq!(drm_format_pix_fmt(Some("NV12")), None);
    }

    fn output(x: i32, y: i32, mode: (i32, i32)) -> WaylandDisplayInfo {
        WaylandDisplayInfo {
            name: String::new(),
            x,
            y,
            width: mode.0,
            height: mode.1,
            logical_size: None,
            refresh_rate: 60,
            transform: 0,
        }
    }

    #[test]
    fn a_portal_position_naming_no_output_of_its_size_moves_to_the_only_one() {
        // 1920x1080 at the origin, 2880x1800 to its right: xdph puts the right one at (0, 0).
        let pair = [output(0, 0, (1920, 1080)), output(1920, 0, (2880, 1800))];
        assert_eq!(
            corrected_origin((0, 0), (2880, 1800), &pair),
            Some((1920, 0))
        );
        // A real origin is kept.
        assert_eq!(corrected_origin((1920, 0), (2880, 1800), &pair), None);
        // Nothing starts at the origin (the hyprland layout in #15731).
        let three = [
            output(0, 120, (1920, 1200)),
            output(1920, 0, (3440, 1440)),
            output(5360, 180, (1920, 1080)),
        ];
        assert_eq!(
            corrected_origin((0, 0), (3440, 1440), &three),
            Some((1920, 0))
        );
        // Two outputs of that size cannot be told apart: the position is kept.
        let twins = [
            output(0, 0, (2560, 1600)),
            output(2560, 0, (1920, 1080)),
            output(4480, 0, (1920, 1080)),
        ];
        assert_eq!(corrected_origin((0, 0), (1920, 1080), &twins), None);
        // A portrait panel turned to landscape keeps its position next to a 1920x1080 output.
        let turned = [
            WaylandDisplayInfo {
                transform: 90,
                ..output(0, 0, (1080, 1920))
            },
            output(1920, 0, (1920, 1080)),
        ];
        assert_eq!(corrected_origin((0, 0), (1920, 1080), &turned), None);
        // A rotated output that try_fill_positions could not match by its mode is found in its
        // delivered orientation.
        let turned_right = [
            output(0, 0, (2560, 1440)),
            WaylandDisplayInfo {
                transform: 90,
                ..output(2560, 0, (1080, 1920))
            },
        ];
        assert_eq!(
            corrected_origin((0, 0), (1920, 1080), &turned_right),
            Some((2560, 0))
        );
    }
}