Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(linux): start on X11 when WAYLAND_DISPLAY names no running compositor (#3299)
Richie McIlroy5fcf8e9cb426
diff --git a/.github/workflows/linux-desktop-reliability.yml b/.github/workflows/linux-desktop-reliability.ymlindex 13803c2f2..5e2e45009 100644--- a/.github/workflows/linux-desktop-reliability.yml+++ b/.github/workflows/linux-desktop-reliability.yml@@ -129,6 +129,13 @@ jobs:bash scripts/linux-desktop-harness/x11-capture.sh \apps/desktop-gpui/target/debug/cap-gpui target/native-deps/lib "$RUNNER_TEMP/out/x11-capture"+ # A systemd user environment keeps WAYLAND_DISPLAY after a Wayland+ # session ends.+ - name: X11 launch with a stale WAYLAND_DISPLAY+ run: |+ bash scripts/linux-desktop-harness/x11-stale-wayland.sh \+ apps/desktop-gpui/target/debug/cap-gpui target/native-deps/lib "$RUNNER_TEMP/out/x11-stale-wayland"+- name: Fetch the Cap package for desktop integrationrun: |curl --fail --location --retry 3 -o "$RUNNER_TEMP/cap.deb" "$CAP_DEB_URL"diff --git a/apps/desktop-gpui/src/linux_display.rs b/apps/desktop-gpui/src/linux_display.rsnew file mode 100644index 000000000..12eb8d613--- /dev/null+++ b/apps/desktop-gpui/src/linux_display.rs@@ -0,0 +1,82 @@+//! A `WAYLAND_DISPLAY` that names no running compositor, as a systemd user+//! environment keeps after the Wayland session that set it has ended. gpui+//! picks Wayland whenever the variable is set and cannot start without the+//! compositor, so when the session also has an X display Cap uses that.++use std::{+ ffi::{OsStr, OsString},+ io::ErrorKind,+ os::unix::net::UnixStream,+ path::{Path, PathBuf},+};++fn socket_path(display: &OsStr, runtime_dir: Option<&OsStr>) -> Option<PathBuf> {+ let display = Path::new(display);+ if display.is_absolute() {+ return Some(display.to_path_buf());+ }+ let runtime_dir = Path::new(runtime_dir?);+ runtime_dir+ .is_absolute()+ .then(|| runtime_dir.join(display))+}++/// Only a missing socket or a refused connection counts: anything else, such+/// as a permission error, leaves the session as it is.+fn nothing_listens(path: &Path) -> bool {+ match UnixStream::connect(path) {+ Ok(_) => false,+ Err(error) => matches!(+ error.kind(),+ ErrorKind::NotFound | ErrorKind::ConnectionRefused+ ),+ }+}++pub fn stale_wayland_display() -> Option<OsString> {+ if std::env::var_os("WAYLAND_SOCKET").is_some() {+ return None;+ }+ let display = std::env::var_os("WAYLAND_DISPLAY").filter(|display| !display.is_empty())?;+ std::env::var_os("DISPLAY").filter(|display| !display.is_empty())?;+ let path = socket_path(&display, std::env::var_os("XDG_RUNTIME_DIR").as_deref())?;+ nothing_listens(&path).then_some(display)+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::os::unix::net::UnixListener;++ #[test]+ fn display_names_resolve_in_the_runtime_dir_unless_absolute() {+ assert_eq!(+ socket_path(OsStr::new("wayland-0"), Some(OsStr::new("/run/user/1000"))),+ Some(PathBuf::from("/run/user/1000/wayland-0"))+ );+ assert_eq!(+ socket_path(OsStr::new("/tmp/wl"), None),+ Some(PathBuf::from("/tmp/wl"))+ );+ assert_eq!(socket_path(OsStr::new("wayland-0"), None), None);+ assert_eq!(+ socket_path(OsStr::new("wayland-0"), Some(OsStr::new("relative"))),+ None+ );+ }++ #[test]+ fn only_a_missing_or_refusing_socket_is_stale() {+ let dir = tempfile::tempdir().unwrap();+ let live = dir.path().join("wayland-live");+ let listener = UnixListener::bind(&live).unwrap();+ assert!(!nothing_listens(&live));++ let gone = dir.path().join("wayland-gone");+ drop(UnixListener::bind(&gone).unwrap());+ assert!(nothing_listens(&gone));++ assert!(nothing_listens(&dir.path().join("wayland-missing")));+ drop(listener);+ }+}diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rsindex efb655641..d0fa0e978 100644--- a/apps/desktop-gpui/src/main.rs+++ b/apps/desktop-gpui/src/main.rs@@ -50,6 +50,8 @@ mod jpeg_thumbnail;#[cfg(not(any(target_os = "macos", target_os = "windows")))]mod linux_dialog;#[cfg(target_os = "linux")]+mod linux_display;+#[cfg(target_os = "linux")]mod linux_portal_id;#[cfg(target_os = "linux")]mod linux_wallpaper;@@ -293,6 +295,17 @@ fn main() {}}+ #[cfg(target_os = "linux")]+ let stale_wayland_display = linux_display::stale_wayland_display();+ #[cfg(target_os = "linux")]+ if stale_wayland_display.is_some() {+ // gpui and screen capture both choose Wayland from this variable; drop+ // it before logging starts threads.+ unsafe {+ std::env::remove_var("WAYLAND_DISPLAY");+ }+ }+// The AppImage CLI launcher re-enters the AppImage with `--cap-cli`; hand// it to the bundled CLI before logging, single-instance or the GUI start.#[cfg(target_os = "linux")]@@ -311,6 +324,13 @@ fn main() {let _log_guard = init_logging();crash_capture::install(&diagnostics::logs_dir());+ #[cfg(target_os = "linux")]+ if let Some(wayland_display) = stale_wayland_display {+ tracing::warn!(+ wayland_display = %wayland_display.to_string_lossy(),+ "WAYLAND_DISPLAY names no running compositor; using the X display"+ );+ }single_instance::acquire();let previous_session = crash_capture::begin_session(&diagnostics::logs_dir());diff --git a/scripts/linux-desktop-harness/README.md b/scripts/linux-desktop-harness/README.mdindex 168ff44c2..0fa7add3e 100644--- a/scripts/linux-desktop-harness/README.md+++ b/scripts/linux-desktop-harness/README.md@@ -96,6 +96,10 @@ real login activates.a screenshot, and checks the size, length and loudness of each result. It alsochecks the window picker offers the other app's window rather than one ofCap's own.+- `x11-stale-wayland.sh <cap-gpui> <ffmpeg lib dir> <out-dir>` starts Cap on+ Xvfb with `WAYLAND_DISPLAY` naming a socket nothing listens on, as a systemd+ user environment keeps it after a Wayland session ends, and checks Cap falls+ back to the X display instead of failing to start.- `niri-screencast/run.sh` records through Cap's PipeWire capture loop from aproducer that negotiates and shares frames the way niri 26.04 does: adiff --git a/scripts/linux-desktop-harness/x11-stale-wayland.sh b/scripts/linux-desktop-harness/x11-stale-wayland.shnew file mode 100755index 000000000..42cc6fe88--- /dev/null+++ b/scripts/linux-desktop-harness/x11-stale-wayland.sh@@ -0,0 +1,41 @@+#!/usr/bin/env bash+# Usage: x11-stale-wayland.sh <cap-gpui> <ffmpeg lib dir> <out-dir>+set -uo pipefail+command -v Xvfb >/dev/null || { echo "install xvfb" >&2; exit 2; }+if [[ -z ${X11_STALE_BUS:-} ]]; then+ exec env X11_STALE_BUS=1 dbus-run-session -- bash "$0" "$@"+fi+bin=$1 libs=$2 out=$3+mkdir -p "$out" && : >"$out/result.txt" || { echo "x11-stale-wayland: cannot write to $out" >&2; exit 2; }+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }+export DISPLAY=:72 XDG_SESSION_TYPE=wayland+Xvfb "$DISPLAY" -screen 0 1280x800x24 >/dev/null 2>&1 &+xvfb=$!+trap 'kill $xvfb 2>/dev/null' EXIT+sleep 1++# A runtime dir whose Wayland socket outlived its compositor, as a systemd+# user environment keeps WAYLAND_DISPLAY after a Wayland session ends.+runtime=$(mktemp -d)+python3 -c 'import socket, sys; socket.socket(socket.AF_UNIX).bind(sys.argv[1])' "$runtime/wayland-0"+profile=$out/profile+log=$out/cap.log+mkdir -p "$profile/logs"+printf '%s' '{"general_settings":{"hasCompletedStartup":true,"hasCompletedOnboarding":true,"enableNotifications":false,"enableTelemetry":false}}' >"$profile/store"+env LD_LIBRARY_PATH="$libs" XDG_RUNTIME_DIR="$runtime" WAYLAND_DISPLAY=wayland-0 CAP_GPUI_APP_DATA_DIR="$profile" \+ CAP_GPUI_LOG_DIR="$profile/logs" CAP_GPUI_NO_TRAY=1 RUST_LOG=info "$bin" >"$log" 2>&1 &+pid=$!+for _ in $(seq 120); do+ grep -aq "main window opened\|panic" "$log" 2>/dev/null && break+ sleep 0.25+done+sleep 2+plain=$(sed 's/\x1b\[[0-9;]*m//g' "$log")+result fell_back_to_x11 "$(grep -q "names no running compositor" <<<"$plain" && echo yes || echo no)"+result main_window "$(grep -q "main window opened" <<<"$plain" && echo yes || echo no)"+result cap_alive "$(kill -0 "$pid" 2>/dev/null && echo yes || echo no)"+result panics "$(grep -c "panic" <<<"$plain")"+kill "$pid" 2>/dev/null+cat "$out/result.txt"+grep -qx "fell_back_to_x11=yes" "$out/result.txt" && grep -qx "main_window=yes" "$out/result.txt" &&+ grep -qx "cap_alive=yes" "$out/result.txt" && grep -qx "panics=0" "$out/result.txt"