Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(linux): capture Wayland screenshots without leaking other windows

Richie McIlroy917f794adc78
diff --git a/apps/desktop-gpui/src/screenshot_editor.rs b/apps/desktop-gpui/src/screenshot_editor.rs
index a49a200ec..562265e85 100644
--- a/apps/desktop-gpui/src/screenshot_editor.rs
+++ b/apps/desktop-gpui/src/screenshot_editor.rs
@@ -964,6 +964,10 @@ pub struct ScreenshotEditorWindow {
}
impl ScreenshotEditorWindow {
+ pub(crate) fn export_in_flight(&self) -> bool {
+ self.exporting
+ }
+
pub fn new(bundle: PathBuf, window: &mut Window, cx: &mut Context<Self>) -> Self {
let close_bundle = bundle.clone();
window.on_window_should_close(cx, move |_window, cx| {
diff --git a/apps/desktop-gpui/src/target_thumbnails.rs b/apps/desktop-gpui/src/target_thumbnails.rs
index 8afc2d04f..dc6a36649 100644
--- a/apps/desktop-gpui/src/target_thumbnails.rs
+++ b/apps/desktop-gpui/src/target_thumbnails.rs
@@ -441,6 +441,7 @@ pub fn display_signature(list: &[DisplayOption]) -> String {
/// (the card's slot background shows through, as in the Tauri picker).
/// Requesting this size FROM SCK instead is not an option: for some sizes
/// `capture_sample_buf` never resolves -- no error, the future hangs.
+#[cfg(any(target_os = "macos", test))]
pub fn fitted_capture_size(source: Option<(f64, f64)>) -> (usize, usize) {
let Some((width, height)) = source else {
return (THUMBNAIL_WIDTH as usize, THUMBNAIL_HEIGHT as usize);
@@ -938,13 +939,14 @@ mod platform {
}
}
-/// Windows and Linux have their own `thumbnails/{windows,linux}.rs` in the
-/// Tauri app; neither is ported yet, and the picker falls back to the icon
-/// card, which is exactly what it did before this unit.
#[cfg(not(target_os = "macos"))]
mod platform {
+ use cap_recording::screenshot::capture_screenshot;
+ use cap_recording::sources::screen_capture::ScreenCaptureTarget;
use image::RgbaImage;
+ use super::normalize_thumbnail_dimensions;
+
#[derive(Clone)]
pub struct ShareableContent;
@@ -955,21 +957,42 @@ mod platform {
}
pub async fn shareable_content() -> Option<ShareableContent> {
- None
+ Some(ShareableContent)
}
pub async fn capture_display_thumbnail(
- _display: &scap_targets::Display,
+ display: &scap_targets::Display,
_content: ShareableContent,
) -> Option<RgbaImage> {
- None
+ capture_target_thumbnail(ScreenCaptureTarget::Display { id: display.id() }).await
}
pub async fn capture_window_thumbnail(
- _window: &scap_targets::Window,
+ window: &scap_targets::Window,
_content: ShareableContent,
) -> Option<RgbaImage> {
- None
+ capture_target_thumbnail(ScreenCaptureTarget::Window { id: window.id() }).await
+ }
+
+ async fn capture_target_thumbnail(target: ScreenCaptureTarget) -> Option<RgbaImage> {
+ #[cfg(target_os = "linux")]
+ if cap_recording::screenshot::is_pure_wayland_session() {
+ return None;
+ }
+
+ let image = match capture_screenshot(target).await {
+ Ok(image) => image.into_rgba8(),
+ Err(error) => {
+ tracing::warn!(%error, "target thumbnail capture failed");
+ return None;
+ }
+ };
+
+ if image.width() == 0 || image.height() == 0 {
+ return None;
+ }
+
+ Some(normalize_thumbnail_dimensions(&image))
}
}
@@ -984,6 +1007,7 @@ use platform::{capture_display_thumbnail, capture_window_thumbnail, shareable_co
/// The four 32-bit orders `capture_thumbnail_from_filter` accepts
/// (`thumbnails/mac.rs:73-78`).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+#[cfg(any(target_os = "macos", test))]
pub enum ChannelOrder {
Bgra,
Rgba,
@@ -996,6 +1020,7 @@ pub enum ChannelOrder {
/// # Safety
/// `base_ptr` must be the base address of a locked pixel buffer holding at
/// least `bytes_per_row * height` readable bytes.
+#[cfg(target_os = "macos")]
unsafe fn convert_32bit_pixel_buffer(
base_ptr: *const u8,
bytes_per_row: usize,
@@ -1014,6 +1039,7 @@ unsafe fn convert_32bit_pixel_buffer(
}
/// The row loop of `convert_32bit_pixel_buffer`, over a slice.
+#[cfg(any(target_os = "macos", test))]
fn convert_32bit_rows(
raw_data: &[u8],
bytes_per_row: usize,
@@ -1056,12 +1082,14 @@ fn convert_32bit_rows(
}
#[derive(Copy, Clone)]
+#[cfg(any(target_os = "macos", test))]
pub enum Nv12Range {
Video,
_Full,
}
/// The plane geometry `convert_nv12_pixel_buffer` reads out of the lock.
+#[cfg(target_os = "macos")]
struct Nv12Planes {
y: *const u8,
y_stride: usize,
@@ -1076,7 +1104,7 @@ struct Nv12Planes {
/// # Safety
/// Both plane pointers must address at least `stride * plane_height` readable
/// bytes of a locked pixel buffer.
-#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
+#[cfg(target_os = "macos")]
unsafe fn convert_nv12_pixel_buffer(
planes: Nv12Planes,
width: usize,
@@ -1125,6 +1153,7 @@ unsafe fn convert_nv12_pixel_buffer(
}
/// The pixel loop of `convert_nv12_pixel_buffer`, over slices.
+#[cfg(any(target_os = "macos", test))]
fn convert_nv12_planes(
y_plane: &[u8],
y_stride: usize,
@@ -1188,6 +1217,7 @@ fn convert_nv12_planes(
/// `ycbcr_to_rgb` (`thumbnails/mac.rs:256-275`): BT.601 coefficients, with the
/// video-range 16..235 luma expansion.
+#[cfg(any(target_os = "macos", test))]
fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8, range: Nv12Range) -> (u8, u8, u8) {
let y = y as f32;
let cb = cb as f32 - 128.0;
@@ -1205,6 +1235,7 @@ fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8, range: Nv12Range) -> (u8, u8, u8) {
(clamp_channel(r), clamp_channel(g), clamp_channel(b))
}
+#[cfg(any(target_os = "macos", test))]
fn clamp_channel(value: f32) -> u8 {
value.clamp(0.0, 255.0) as u8
}
diff --git a/apps/desktop/src-tauri/src/thumbnails/linux.rs b/apps/desktop/src-tauri/src/thumbnails/linux.rs
index df6136851..fbb3d7a29 100644
--- a/apps/desktop/src-tauri/src/thumbnails/linux.rs
+++ b/apps/desktop/src-tauri/src/thumbnails/linux.rs
@@ -14,6 +14,10 @@ pub async fn capture_window_thumbnail(window: &scap_targets::Window) -> Option<S
}
async fn capture_target_thumbnail(target: ScreenCaptureTarget) -> Option<String> {
+ if cap_recording::screenshot::is_pure_wayland_session() {
+ return None;
+ }
+
let image = match capture_screenshot(target).await {
Ok(image) => image,
Err(error) => {
diff --git a/crates/recording/src/screenshot.rs b/crates/recording/src/screenshot.rs
index 01a4787e6..11f1bfb9b 100644
--- a/crates/recording/src/screenshot.rs
+++ b/crates/recording/src/screenshot.rs
@@ -819,10 +819,215 @@ fn try_fast_capture(target: &ScreenCaptureTarget) -> Option<DynamicImage> {
#[cfg(target_os = "linux")]
pub async fn capture_screenshot(target: ScreenCaptureTarget) -> anyhow::Result<DynamicImage> {
+ if is_pure_wayland_session() {
+ let image = capture_screenshot_wayland(&target).await?;
+ return Ok(finalize_screenshot(image, &target));
+ }
+
let image = capture_screenshot_x11(&target).await?;
Ok(finalize_screenshot(image, &target))
}
+#[cfg(target_os = "linux")]
+pub fn is_pure_wayland_session() -> bool {
+ is_pure_wayland_environment(
+ std::env::var_os("WAYLAND_DISPLAY").as_deref(),
+ std::env::var_os("DISPLAY").as_deref(),
+ )
+}
+
+#[cfg(target_os = "linux")]
+fn is_pure_wayland_environment(
+ wayland_display: Option<&std::ffi::OsStr>,
+ x11_display: Option<&std::ffi::OsStr>,
+) -> bool {
+ wayland_display.is_some() && x11_display.is_none()
+}
+
+#[cfg(target_os = "linux")]
+async fn capture_screenshot_wayland(target: &ScreenCaptureTarget) -> anyhow::Result<RgbImage> {
+ let display_id = wayland_screenshot_display_id(target)?;
+
+ let displays = scap_targets::Display::list();
+ let [display] = displays.as_slice() else {
+ return Err(anyhow!(
+ "Wayland screenshot portal cannot safely isolate the selected display"
+ ));
+ };
+
+ if display.id() != *display_id {
+ return Err(anyhow!(
+ "Wayland screenshot portal cannot safely isolate the selected display"
+ ));
+ }
+
+ let display_size = display
+ .physical_size()
+ .ok_or_else(|| anyhow!("Selected Wayland display size unavailable"))?;
+ let (width, height) =
+ checked_wayland_display_size(display_size.width(), display_size.height())?;
+ let crop = checked_wayland_screenshot_crop(target, width, height)?;
+
+ let screenshot = ashpd::desktop::screenshot::Screenshot::request()
+ .interactive(false)
+ .send()
+ .await
+ .context("Request Wayland screenshot from desktop portal")?
+ .response()
+ .context("Wayland screenshot portal request was cancelled or rejected")?;
+
+ let uri = screenshot.uri();
+ if !is_local_wayland_screenshot_uri(uri.scheme(), uri.host_str()) {
+ return Err(anyhow!(
+ "Wayland screenshot portal returned a non-local file URI"
+ ));
+ }
+
+ let path = uri
+ .to_file_path()
+ .map_err(|()| anyhow!("Wayland screenshot portal returned a non-local file URI"))?;
+
+ let metadata = tokio::fs::symlink_metadata(&path)
+ .await
+ .context("Read Wayland screenshot portal file metadata")?;
+ if !metadata.file_type().is_file() {
+ return Err(anyhow!(
+ "Wayland screenshot portal returned a non-regular file"
+ ));
+ }
+
+ let image = tokio::task::spawn_blocking(move || {
+ use std::os::unix::fs::MetadataExt;
+
+ let file = std::fs::File::open(&path).context("Open Wayland screenshot portal image")?;
+ let opened_metadata = file
+ .metadata()
+ .context("Read opened Wayland screenshot portal file metadata")?;
+ if !opened_metadata.file_type().is_file()
+ || opened_metadata.dev() != metadata.dev()
+ || opened_metadata.ino() != metadata.ino()
+ {
+ return Err(anyhow!(
+ "Wayland screenshot portal image changed before it could be read"
+ ));
+ }
+
+ image::ImageReader::new(std::io::BufReader::new(file))
+ .with_guessed_format()
+ .context("Identify Wayland screenshot portal image format")?
+ .decode()
+ .map(DynamicImage::into_rgb8)
+ .context("Decode Wayland screenshot portal image")
+ })
+ .await
+ .context("Wayland screenshot image task failed")??;
+
+ if !wayland_screenshot_matches_display(image.width(), image.height(), width, height) {
+ return Err(anyhow!(
+ "Wayland screenshot contains content outside the selected display"
+ ));
+ }
+
+ match crop {
+ Some((x, y, width, height)) => {
+ Ok(image::imageops::crop_imm(&image, x, y, width, height).to_image())
+ }
+ None => Ok(image),
+ }
+}
+
+#[cfg(target_os = "linux")]
+fn wayland_screenshot_display_id(
+ target: &ScreenCaptureTarget,
+) -> anyhow::Result<&scap_targets::DisplayId> {
+ match target {
+ ScreenCaptureTarget::Display { id } => Ok(id),
+ ScreenCaptureTarget::Area { screen, .. } => Ok(screen),
+ ScreenCaptureTarget::Window { .. } => Err(anyhow!(
+ "Wayland screenshot portal cannot safely isolate the selected window"
+ )),
+ ScreenCaptureTarget::CameraOnly => {
+ Err(anyhow!("Camera-only not supported for screenshots"))
+ }
+ }
+}
+
+#[cfg(target_os = "linux")]
+fn is_local_wayland_screenshot_uri(scheme: &str, host: Option<&str>) -> bool {
+ scheme == "file" && (host.is_none() || host == Some("localhost"))
+}
+
+#[cfg(target_os = "linux")]
+fn wayland_screenshot_matches_display(
+ image_width: u32,
+ image_height: u32,
+ display_width: u32,
+ display_height: u32,
+) -> bool {
+ image_width == display_width && image_height == display_height
+}
+
+#[cfg(target_os = "linux")]
+fn checked_wayland_display_size(width: f64, height: f64) -> anyhow::Result<(u32, u32)> {
+ if !width.is_finite()
+ || !height.is_finite()
+ || width <= 0.0
+ || height <= 0.0
+ || width.fract() != 0.0
+ || height.fract() != 0.0
+ || width > f64::from(u32::MAX)
+ || height > f64::from(u32::MAX)
+ {
+ return Err(anyhow!("Selected Wayland display size is invalid"));
+ }
+
+ Ok((width as u32, height as u32))
+}
+
+#[cfg(target_os = "linux")]
+fn checked_wayland_screenshot_crop(
+ target: &ScreenCaptureTarget,
+ image_width: u32,
+ image_height: u32,
+) -> anyhow::Result<Option<(u32, u32, u32, u32)>> {
+ let ScreenCaptureTarget::Area { bounds, .. } = target else {
+ return Ok(None);
+ };
+
+ let x = bounds.position().x();
+ let y = bounds.position().y();
+ let width = bounds.size().width();
+ let height = bounds.size().height();
+ let right = x + width;
+ let bottom = y + height;
+
+ if ![x, y, width, height, right, bottom]
+ .iter()
+ .all(|value| value.is_finite())
+ || x < 0.0
+ || y < 0.0
+ || width <= 0.0
+ || height <= 0.0
+ || right > f64::from(image_width)
+ || bottom > f64::from(image_height)
+ {
+ return Err(anyhow!(
+ "Selected Wayland screenshot area exceeds the selected display"
+ ));
+ }
+
+ let x = x.ceil() as u32;
+ let y = y.ceil() as u32;
+ let right = right.floor() as u32;
+ let bottom = bottom.floor() as u32;
+
+ if right <= x || bottom <= y {
+ return Err(anyhow!("Selected Wayland screenshot area is empty"));
+ }
+
+ Ok(Some((x, y, right - x, bottom - y)))
+}
+
#[cfg(not(target_os = "linux"))]
pub async fn capture_screenshot(target: ScreenCaptureTarget) -> anyhow::Result<DynamicImage> {
#[cfg(target_os = "macos")]
@@ -988,7 +1193,7 @@ pub async fn capture_screenshot(target: ScreenCaptureTarget) -> anyhow::Result<D
display
.raw_handle()
.try_as_capture_item()
- .map_err(|e| anyhow!("Failed to get capture item: {e:?}"))?
+ .map_err(|e| anyhow!("Failed to get capture item: {e:?}"))
}
ScreenCaptureTarget::Window { id } => {
let window = scap_targets::Window::from_id(&id)
@@ -996,7 +1201,7 @@ pub async fn capture_screenshot(target: ScreenCaptureTarget) -> anyhow::Result<D
window
.raw_handle()
.try_as_capture_item()
- .map_err(|e| anyhow!("Failed to get capture item: {e:?}"))?
+ .map_err(|e| anyhow!("Failed to get capture item: {e:?}"))
}
ScreenCaptureTarget::Area { screen, .. } => {
let display = scap_targets::Display::from_id(&screen)
@@ -1004,13 +1209,22 @@ pub async fn capture_screenshot(target: ScreenCaptureTarget) -> anyhow::Result<D
display
.raw_handle()
.try_as_capture_item()
- .map_err(|e| anyhow!("Failed to get capture item: {e:?}"))?
+ .map_err(|e| anyhow!("Failed to get capture item: {e:?}"))
}
ScreenCaptureTarget::CameraOnly => {
return Err(anyhow!("Camera-only not supported for screenshots"));
}
};
+ let item = match item {
+ Ok(item) => item,
+ Err(error) => {
+ let fallback_image = gdi_or_error(&target, error)?;
+ return crop_area_if_needed(fallback_image, &target, false)
+ .map(|image| finalize_screenshot(image, &target));
+ }
+ };
+
let (settings, cropped) = windows_capture_settings(&target)?;
cropped_in_capture = cropped;
@@ -1322,3 +1536,122 @@ fn linux_capture_geometry(
}
}
}
+
+#[cfg(all(test, target_os = "linux"))]
+mod wayland_screenshot_tests {
+ use super::*;
+ use scap_targets::bounds::{LogicalBounds, LogicalPosition, LogicalSize};
+ use std::ffi::OsStr;
+
+ fn area_target(x: f64, y: f64, width: f64, height: f64) -> ScreenCaptureTarget {
+ ScreenCaptureTarget::Area {
+ screen: "0".parse().expect("valid display id"),
+ bounds: LogicalBounds::new(LogicalPosition::new(x, y), LogicalSize::new(width, height)),
+ }
+ }
+
+ #[test]
+ fn pure_wayland_requires_wayland_socket_without_x11_display() {
+ assert!(is_pure_wayland_environment(
+ Some(OsStr::new("wayland-1")),
+ None
+ ));
+ assert!(!is_pure_wayland_environment(
+ Some(OsStr::new("wayland-1")),
+ Some(OsStr::new(":99"))
+ ));
+ assert!(!is_pure_wayland_environment(None, None));
+ assert!(!is_pure_wayland_environment(None, Some(OsStr::new(":99"))));
+ }
+
+ #[test]
+ fn only_local_file_screenshot_uris_are_accepted() {
+ assert!(is_local_wayland_screenshot_uri("file", None));
+ assert!(is_local_wayland_screenshot_uri("file", Some("localhost")));
+ assert!(!is_local_wayland_screenshot_uri("https", None));
+ assert!(!is_local_wayland_screenshot_uri("https", Some("localhost")));
+ assert!(!is_local_wayland_screenshot_uri("file", Some("remote")));
+ }
+
+ #[test]
+ fn valid_wayland_display_dimensions_must_be_exact_positive_pixels() {
+ assert_eq!(
+ checked_wayland_display_size(1920.0, 1080.0).expect("valid display"),
+ (1920, 1080)
+ );
+
+ for (width, height) in [
+ (0.0, 1080.0),
+ (1920.0, -1.0),
+ (1920.5, 1080.0),
+ (f64::NAN, 1080.0),
+ (1920.0, f64::INFINITY),
+ (f64::from(u32::MAX) + 1.0, 1080.0),
+ ] {
+ assert!(checked_wayland_display_size(width, height).is_err());
+ }
+ }
+
+ #[test]
+ fn screenshot_must_match_the_selected_display_exactly() {
+ assert!(wayland_screenshot_matches_display(1920, 1080, 1920, 1080));
+ assert!(!wayland_screenshot_matches_display(3840, 1080, 1920, 1080));
+ assert!(!wayland_screenshot_matches_display(1920, 2160, 1920, 1080));
+ assert!(!wayland_screenshot_matches_display(1280, 720, 1920, 1080));
+ }
+
+ #[test]
+ fn selected_wayland_area_is_cropped_exactly() {
+ let target = area_target(100.0, 120.0, 640.0, 360.0);
+ assert_eq!(
+ checked_wayland_screenshot_crop(&target, 1920, 1080).expect("valid crop"),
+ Some((100, 120, 640, 360))
+ );
+ }
+
+ #[test]
+ fn fractional_wayland_areas_never_include_pixels_outside_selection() {
+ let target = area_target(10.25, 20.25, 30.75, 40.75);
+ assert_eq!(
+ checked_wayland_screenshot_crop(&target, 1920, 1080).expect("valid crop"),
+ Some((11, 21, 30, 40))
+ );
+ }
+
+ #[test]
+ fn out_of_bounds_or_non_finite_wayland_areas_are_rejected() {
+ for (x, y, width, height) in [
+ (-1.0, 0.0, 10.0, 10.0),
+ (0.0, -1.0, 10.0, 10.0),
+ (1915.0, 0.0, 10.0, 10.0),
+ (0.0, 1075.0, 10.0, 10.0),
+ (0.0, 0.0, 0.0, 10.0),
+ (0.0, 0.0, 10.0, -1.0),
+ (f64::NAN, 0.0, 10.0, 10.0),
+ (0.0, 0.0, f64::INFINITY, 10.0),
+ (f64::MAX, 0.0, f64::MAX, 10.0),
+ ] {
+ let target = area_target(x, y, width, height);
+ assert!(checked_wayland_screenshot_crop(&target, 1920, 1080).is_err());
+ }
+ }
+
+ #[test]
+ fn too_small_fractional_wayland_areas_are_rejected() {
+ let target = area_target(10.25, 20.25, 0.25, 0.25);
+ assert!(checked_wayland_screenshot_crop(&target, 1920, 1080).is_err());
+ }
+
+ #[test]
+ fn wayland_window_screenshots_are_rejected_before_portal_capture() {
+ let target = ScreenCaptureTarget::Window {
+ id: "0".parse().expect("valid window id"),
+ };
+ assert!(wayland_screenshot_display_id(&target).is_err());
+ }
+
+ #[test]
+ fn wayland_camera_only_screenshots_are_rejected_before_portal_capture() {
+ assert!(wayland_screenshot_display_id(&ScreenCaptureTarget::CameraOnly).is_err());
+ }
+}