Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(linux): AppImage screen capture on NixOS, niri DMA-BUF screencasts, paste test (#3234)

Richie McIlroyeb4e4c49442e
diff --git a/.github/workflows/desktop-release.yml b/.github/workflows/desktop-release.yml
index db087d09b..f4c6d54a1 100644
--- a/.github/workflows/desktop-release.yml
+++ b/.github/workflows/desktop-release.yml
@@ -745,6 +745,17 @@ jobs:
echo "::error::The Cap AppImage bundles PipeWire that can conflict with host ALSA plugins"
exit 1
fi
+ if [[ -n "$(find "$APPROOT/usr/lib" -maxdepth 1 -name 'libva*.so*' -print -quit)" ]]; then
+ echo "::error::The Cap AppImage puts its libva where Cap loads it ahead of the host's, which breaks host PipeWire plugins"
+ exit 1
+ fi
+ test -e "$APPROOT/usr/lib/cap/libva/libva.so.2"
+ test -e "$APPROOT/usr/lib/cap/libva/libva-drm.so.2"
+ grep -qx 'exec "$cap_appdir/usr/bin/Cap" "$@"' "$APPROOT/AppRun"
+ if grep -q 'AppRun.cap-original' "$APPROOT/AppRun"; then
+ echo "::error::The Cap AppImage still starts through the generic AppImage launcher"
+ exit 1
+ fi
- name: Build Arch Linux package
id: build_arch
diff --git a/.github/workflows/linux-desktop-reliability.yml b/.github/workflows/linux-desktop-reliability.yml
index 6ba6abe2c..eeb232f7c 100644
--- a/.github/workflows/linux-desktop-reliability.yml
+++ b/.github/workflows/linux-desktop-reliability.yml
@@ -15,9 +15,14 @@ on:
- "crates/scap-targets/**"
- "scripts/linux-desktop-harness/**"
- "scripts/prepare-gpui-dependency.sh"
+ - "scripts/finalize-linux-appimage.mjs"
- ".github/workflows/linux-desktop-reliability.yml"
workflow_dispatch:
+permissions:
+ contents: read
+ pull-requests: read
+
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
@@ -27,6 +32,9 @@ env:
# binary under test is this commit's build.
CAP_DEB_URL: https://cdn.crabnebula.app/asset/01M5CHFS77QM2HSZ69KT784SAV
CAP_DEB_SHA256: bb03dddc5268f64d3e6eb4c11534462e5b7a8a5c1c7bb5bca5f5bdaa99d11c56
+ # The 0.6.1 AppImage, whose libraries the NixOS check lays out both ways.
+ CAP_APPIMAGE_URL: https://cdn.crabnebula.app/asset/01M5CHFYYW4JXX4M9SPZ1NF6HM
+ CAP_APPIMAGE_SHA256: dd579d76b91173a2e620034c407e63ec3ae4ae5420540de80f5fededb7519b97
jobs:
# A pull request from a fork that changes only this workflow or the harness
@@ -52,7 +60,7 @@ jobs:
echo "run=true" >> "$GITHUB_OUTPUT"; exit 0
fi
gh api "repos/$GITHUB_REPOSITORY/pulls/$PR/files" --paginate --jq '.[].filename' > "$RUNNER_TEMP/changed.txt"
- if grep -qE '^(apps/desktop-gpui/|crates/(recording|utils|scap-targets)/|scripts/prepare-gpui-dependency\.sh$)' "$RUNNER_TEMP/changed.txt"; then
+ if grep -qE '^(apps/desktop-gpui/|crates/(recording|utils|scap-targets)/|scripts/(prepare-gpui-dependency\.sh|finalize-linux-appimage\.mjs)$)' "$RUNNER_TEMP/changed.txt"; then
echo "run=true" >> "$GITHUB_OUTPUT"
else
echo "run=false" >> "$GITHUB_OUTPUT"
@@ -99,6 +107,16 @@ jobs:
- name: Build cap-gpui
run: cargo +1.95.0 build --manifest-path apps/desktop-gpui/Cargo.toml --locked --bin cap-gpui
+ # niri 26.04 shares screencasts only as DMA-BUFs with a mandatory
+ # modifier (GitHub #2941). niri itself refuses software rendering, so a
+ # producer that negotiates and shares frames the way it does stands in.
+ - name: Capture from a niri-style DMA-BUF screencast
+ env:
+ LD_LIBRARY_PATH: ${{ github.workspace }}/target/native-deps/lib
+ run: |
+ sudo apt-get install -y -qq pipewire wireplumber libpipewire-0.3-dev dbus >/dev/null
+ RUSTUP_TOOLCHAIN=1.95.0 bash scripts/linux-desktop-harness/niri-screencast/run.sh
+
- name: Confirmation dialog without zenity (X11 input)
run: |
sudo apt-get install -y -qq xdotool xvfb >/dev/null
@@ -258,6 +276,17 @@ jobs:
"$RUNNER_TEMP/out/hyprland-gl" LAUNCH=dock VK_ICD_FILENAMES=/nonexistent.json
grep -q 'Selected GPU adapter: .*(Gl)' "$RUNNER_TEMP/out/hyprland-gl/cap.log"
+ - name: Hyprland clipboard (Ctrl+V into and Ctrl+C out of a text field)
+ env:
+ SCENARIO: clipboard
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ bash scripts/linux-desktop-harness/run-desktop.sh hyprland "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/hyprland-clipboard"
+ for key in typing_reached_field paste_reached_field copy_reached_clipboard; do
+ grep -qx "$key=yes" "$RUNNER_TEMP/out/hyprland-clipboard/result.txt"
+ done
+
- name: Check results
run: |
bash scripts/linux-desktop-harness/check-results.sh \
@@ -270,3 +299,118 @@ jobs:
name: linux-desktop-reliability-hyprland
path: ${{ runner.temp }}/out
if-no-files-found: ignore
+
+ appimage-nixos:
+ name: AppImage screen capture on NixOS
+ needs: scope
+ if: needs.scope.outputs.run == 'true'
+ # NixOS users run the AppImage through appimage-run. The PipeWire plugins
+ # that load into Cap there link NixOS's FFmpeg, which needs a newer libva
+ # than the one 0.6.1 bundled ahead of the host's (GitHub #2941). This
+ # connects a capture stream through the real launchers, with the AppDir as
+ # 0.6.1 shipped it and as scripts/finalize-linux-appimage.mjs lays it out.
+ runs-on: ubuntu-24.04
+ timeout-minutes: 30
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Fetch the 0.6.1 AppImage
+ run: |
+ curl --fail --location --retry 3 -o "$RUNNER_TEMP/Cap.AppImage" "$CAP_APPIMAGE_URL"
+ echo "$CAP_APPIMAGE_SHA256 $RUNNER_TEMP/Cap.AppImage" | sha256sum --check
+
+ - name: Connect a capture stream in NixOS appimage-run
+ run: |
+ sudo bash scripts/linux-desktop-harness/appimage-pipewire/run.sh \
+ "$RUNNER_TEMP/Cap.AppImage" "$RUNNER_TEMP/out/appimage-nixos"
+
+ - name: Check results
+ run: |
+ set -euo pipefail
+ result="$RUNNER_TEMP/out/appimage-nixos/result.txt"
+ cat "$result"
+ # The shipped layout is the control: it must still reproduce the
+ # failure, or the check proves nothing.
+ grep -qx "shipped_connects=no" "$result"
+ grep -qx "fixed_connects=yes" "$result"
+ # Programs Cap starts (xdg-open, file managers) no longer inherit
+ # the bundle's libraries.
+ grep -qx "shipped_children_get_bundle_libraries=yes" "$result"
+ grep -qx "fixed_children_get_bundle_libraries=no" "$result"
+
+ - name: Upload logs
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: linux-desktop-reliability-appimage-nixos
+ path: ${{ runner.temp }}/out
+ if-no-files-found: ignore
+
+ niri:
+ name: Record on niri
+ needs: desktops
+ # niri 26.04 shares screencasts only as DMA-BUFs with a mandatory
+ # modifier (GitHub #2941). It refuses software rendering, so this builds
+ # the release with only that check removed and runs it on vkms.
+ runs-on: ubuntu-24.04
+ timeout-minutes: 60
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - uses: actions/download-artifact@v4
+ with:
+ name: cap-gpui-linux
+ path: ${{ runner.temp }}/build
+
+ - name: Virtual display devices
+ run: |
+ sudo apt-get update -qq
+ sudo apt-get install -y -qq "linux-modules-extra-$(uname -r)"
+ sudo modprobe vkms
+ ls -la /dev/dri
+
+ - name: Build niri
+ run: |
+ cd scripts/linux-desktop-harness
+ sudo docker build -q -f arch.Dockerfile -t cap-harness-arch . >/dev/null
+ sudo docker build -q -f niri-software.Dockerfile -t cap-harness-arch-niri . >/dev/null
+
+ # The runner's Hyper-V display (card1, Virtual-1) is a second output; with
+ # two, the screen-share dialog waits for a pick the harness never makes.
+ - name: niri (DMA-BUF screencast, tray Stop control)
+ run: |
+ chmod +x "$RUNNER_TEMP/build/cap-gpui"
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ HARNESS_IMAGE=cap-harness-arch-niri \
+ bash scripts/linux-desktop-harness/run-desktop.sh niri "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/niri" LAUNCH=dock NIRI_RENDER_DEVICE=vkms NIRI_OUTPUTS_OFF=Virtual-1 \
+ GSK_RENDERER=cairo
+
+ - name: niri clipboard (Ctrl+V into and Ctrl+C out of a text field)
+ env:
+ SCENARIO: clipboard
+ run: |
+ sudo -E env DEV_BIN="$RUNNER_TEMP/build/cap-gpui" DEV_LIBS="$RUNNER_TEMP/build/libs" \
+ HARNESS_IMAGE=cap-harness-arch-niri \
+ bash scripts/linux-desktop-harness/run-desktop.sh niri "$RUNNER_TEMP/build/cap-gpui" \
+ "$RUNNER_TEMP/out/niri-clipboard" NIRI_RENDER_DEVICE=vkms NIRI_OUTPUTS_OFF=Virtual-1 \
+ GSK_RENDERER=cairo
+ for key in typing_reached_field paste_reached_field copy_reached_clipboard; do
+ grep -qx "$key=yes" "$RUNNER_TEMP/out/niri-clipboard/result.txt"
+ done
+
+ - name: Check results
+ run: |
+ bash scripts/linux-desktop-harness/check-results.sh "$RUNNER_TEMP/out/niri"
+ grep -q "negotiated DMA-BUF frames" "$RUNNER_TEMP/out/niri/cap.log"
+
+ - name: Upload logs
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: linux-desktop-reliability-niri
+ path: ${{ runner.temp }}/out
+ if-no-files-found: ignore
+
diff --git a/Cargo.lock b/Cargo.lock
index 92f63bd7c..b25b55e39 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1947,11 +1947,13 @@ dependencies = [
"flume",
"foreign-types-shared 0.3.1",
"futures",
+ "glow",
"hex",
"image 0.25.8",
"indexmap 2.11.4",
"inquire",
"kameo",
+ "khronos-egl",
"libc",
"libproc",
"objc",
diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock
index 5a1a20f08..b4d93e689 100644
--- a/apps/desktop-gpui/Cargo.lock
+++ b/apps/desktop-gpui/Cargo.lock
@@ -1904,11 +1904,13 @@ dependencies = [
"flume 0.11.1",
"foreign-types-shared 0.3.1",
"futures",
+ "glow 0.16.0",
"hex",
"image 0.25.10",
"indexmap 2.14.0",
"inquire",
"kameo",
+ "khronos-egl",
"libc",
"objc",
"objc2 0.6.4",
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 4c0e1cc97..26f750e90 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -89,11 +89,13 @@ fn platform_display_for_capture(
cx: &App,
) -> Option<std::rc::Rc<dyn gpui::PlatformDisplay>> {
#[cfg(target_os = "linux")]
- if let Some(uuid) = target.raw_handle().wayland_uuid() {
- return cx
+ if let Some(uuid) = target.raw_handle().wayland_uuid()
+ && let Some(display) = cx
.displays()
.into_iter()
- .find(|display| display.uuid().is_ok_and(|candidate| candidate == uuid));
+ .find(|display| display.uuid().is_ok_and(|candidate| candidate == uuid))
+ {
+ return Some(display);
}
#[cfg(target_os = "windows")]
if let Ok(id) = target.id().to_string().parse::<u64>()
@@ -101,17 +103,45 @@ fn platform_display_for_capture(
{
return Some(display);
}
- let bounds = capture_display_bounds(target)?;
- let mut matching = cx
- .displays()
- .into_iter()
- .filter(|display| display.bounds().contains(&bounds.center()));
- let display = matching.next()?;
+ let displays = cx.displays();
+ // A named output that matched nothing is not matched by position either.
#[cfg(target_os = "linux")]
- if uses_wayland() && matching.next().is_some() {
- return None;
+ let named = target.raw_handle().wayland_uuid().is_some();
+ #[cfg(not(target_os = "linux"))]
+ let named = false;
+ let bounds = capture_display_bounds(target).filter(|_| !named);
+ let mut matching = displays
+ .iter()
+ .filter(|display| bounds.is_some_and(|bounds| display.bounds().contains(&bounds.center())));
+ let found = matching.next().cloned();
+ #[cfg(target_os = "linux")]
+ let found = found.filter(|_| !(uses_wayland() && matching.next().is_some()));
+ #[cfg(target_os = "linux")]
+ if found.is_none() && uses_wayland() {
+ // One output on each side cannot be the wrong one, whatever its name
+ // or position (a compositor may report neither the same way to both).
+ if displays.len() == 1 && scap_targets::Display::list().len() == 1 {
+ return displays.into_iter().next();
+ }
+ tracing::warn!(
+ uuid = ?target.raw_handle().wayland_uuid(),
+ ?bounds,
+ candidates = ?displays
+ .iter()
+ .map(|display| {
+ (
+ display
+ .uuid()
+ .map(|uuid| uuid.to_string())
+ .unwrap_or_else(|error| error.to_string()),
+ display.bounds(),
+ )
+ })
+ .collect::<Vec<_>>(),
+ "no Wayland output matches the capture display"
+ );
}
- Some(display)
+ found
}
fn gpui_display_id(target: Option<&scap_targets::Display>, cx: &App) -> Option<gpui::DisplayId> {
diff --git a/apps/desktop-gpui/src/ui/text_input.rs b/apps/desktop-gpui/src/ui/text_input.rs
index 18d4cbac0..dfbe702ca 100644
--- a/apps/desktop-gpui/src/ui/text_input.rs
+++ b/apps/desktop-gpui/src/ui/text_input.rs
@@ -2720,6 +2720,58 @@ mod tests {
}
}
+ struct PasteProbe {
+ state: Entity<TextInputState>,
+ }
+
+ impl Render for PasteProbe {
+ fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
+ div().size_full().child(TextInput::settings(
+ &Theme::light(),
+ "paste-probe",
+ &self.state,
+ ))
+ }
+ }
+
+ /// A 0.6.1 report from Linux: pasting into Cap's text fields did nothing.
+ /// The fields bound only `cmd-v`, which the fork reads as Super off macOS.
+ /// This presses the platform's paste chord through the real key dispatch,
+ /// and copies back out with the copy chord.
+ #[gpui::test]
+ fn the_platform_paste_and_copy_chords_reach_a_focused_field(cx: &mut gpui::TestAppContext) {
+ cx.update(bind_keys);
+ let (probe, cx) = cx.add_window_view(|window, cx| PasteProbe {
+ state: cx.new(|cx| TextInputState::single_line(window, cx)),
+ });
+ let state = probe.read_with(cx, |probe, _| probe.state.clone());
+ cx.update(|window, cx| {
+ let focus = state.read(cx).focus_handle();
+ window.focus(&focus, cx);
+ cx.write_to_clipboard(ClipboardItem::new_string(
+ "pasted from the clipboard".into(),
+ ));
+ });
+ cx.run_until_parked();
+ let (paste, select_all, copy) = if cfg!(target_os = "macos") {
+ ("cmd-v", "cmd-a", "cmd-c")
+ } else {
+ ("ctrl-v", "ctrl-a", "ctrl-c")
+ };
+ cx.simulate_keystrokes(paste);
+ assert_eq!(
+ state.read_with(cx, |state, _| state.text().to_string()),
+ "pasted from the clipboard"
+ );
+
+ cx.update(|_, cx| cx.write_to_clipboard(ClipboardItem::new_string(String::new())));
+ cx.simulate_keystrokes(&format!("{select_all} {copy}"));
+ assert_eq!(
+ cx.update(|_, cx| cx.read_from_clipboard().and_then(|item| item.text())),
+ Some("pasted from the clipboard".to_string())
+ );
+ }
+
// -- Word navigation ------------------------------------------------------
/// `alt-left` skips the run of separators first, then the word, which is
diff --git a/crates/recording/Cargo.toml b/crates/recording/Cargo.toml
index 29b1ee5fa..84d8000ac 100644
--- a/crates/recording/Cargo.toml
+++ b/crates/recording/Cargo.toml
@@ -103,6 +103,10 @@ scap-cpal = { path = "../scap-cpal" }
[target.'cfg(target_os = "linux")'.dependencies]
ashpd = { version = "0.11.0", default-features = false, features = ["tokio"] }
+# Reads screencast DMA-BUFs with GPU-tiled modifiers (niri on NVIDIA). Both
+# are already in the tree through wgpu's GL backend; libEGL loads at runtime.
+glow = "0.16"
+khronos-egl = { version = "6", features = ["dynamic"] }
pipewire = "0.10.0"
x11rb = { version = "0.13.2", features = ["composite", "shm", "xfixes"] }
diff --git a/crates/recording/src/sources/screen_capture/linux.rs b/crates/recording/src/sources/screen_capture/linux.rs
index 129a67096..7ca0ef3be 100644
--- a/crates/recording/src/sources/screen_capture/linux.rs
+++ b/crates/recording/src/sources/screen_capture/linux.rs
@@ -1,4 +1,5 @@
use super::cadence::FrameCadenceGate;
+use super::linux_dmabuf_import;
use super::linux_shm::{self, ShmSegment};
use super::*;
use crate::feeds::microphone::{self, MicrophoneFeed, MicrophoneFeedLock};
@@ -309,6 +310,9 @@ struct PipewireCaptureState {
rate_limited: Arc<AtomicU64>,
capture_clock: Instant,
cadence_gate: FrameCadenceGate,
+ /// Reads frames with a GPU-tiled modifier; `None` without a usable EGL.
+ importer: Option<linux_dmabuf_import::DmaBufImporter>,
+ imported: Vec<u8>,
}
impl PipewireCaptureState {
@@ -483,7 +487,43 @@ fn capture_wayland(
stop_token: CancellationToken,
health_tx: output_pipeline::HealthSender,
) -> anyhow::Result<()> {
- let _portal_session = input.portal_session;
+ let WaylandInputConfig {
+ fd,
+ node_id,
+ fps,
+ crop_bounds,
+ portal_session: _portal_session,
+ } = input;
+ capture_pipewire_node(
+ video_info,
+ PipewireNodeInput {
+ fd,
+ node_id,
+ fps,
+ crop_bounds,
+ },
+ video_tx,
+ stop_token,
+ health_tx,
+ )
+}
+
+/// A PipeWire video node to capture: the portal's remote and node, or in
+/// tests any producer on a PipeWire daemon.
+struct PipewireNodeInput {
+ fd: OwnedFd,
+ node_id: u32,
+ fps: u32,
+ crop_bounds: Option<CropBounds>,
+}
+
+fn capture_pipewire_node(
+ video_info: VideoInfo,
+ input: PipewireNodeInput,
+ video_tx: mpsc::Sender<FFmpegVideoFrame>,
+ stop_token: CancellationToken,
+ health_tx: output_pipeline::HealthSender,
+) -> anyhow::Result<()> {
let stop_requested = Arc::new(AtomicBool::new(false));
let fatal_error = Arc::new(parking_lot::Mutex::new(None));
let sent = Arc::new(AtomicU64::new(0));
@@ -499,6 +539,18 @@ fn capture_wayland(
.connect_fd(input.fd, None)
.context("connect to PipeWire remote")?;
+ let mut importer = match linux_dmabuf_import::DmaBufImporter::new() {
+ Ok(importer) => Some(importer),
+ Err(error) => {
+ tracing::info!(%error, "no GPU import for tiled DMA-BUF screencasts");
+ None
+ }
+ };
+ let imported_params = importer
+ .as_mut()
+ .map(|importer| pipewire_imported_format_params(input.fps, importer))
+ .unwrap_or_default();
+
let state = PipewireCaptureState {
format: Default::default(),
scaler: None,
@@ -513,6 +565,8 @@ fn capture_wayland(
rate_limited: rate_limited.clone(),
capture_clock: started,
cadence_gate: FrameCadenceGate::new(1_000_000_000 / i64::from(input.fps.max(1))),
+ importer,
+ imported: Vec::new(),
};
let stream = pw::stream::StreamBox::new(
@@ -533,9 +587,17 @@ fn capture_wayland(
state.set_fatal_error(format!("PipeWire screen capture stream failed: {error}"));
}
})
- .param_changed(|_, state, id, param| {
- if let Err(error) = update_pipewire_format(state, id, param) {
- state.set_fatal_error(error.to_string());
+ .param_changed(|stream, state, id, param| {
+ match update_pipewire_format(state, id, param) {
+ Ok(PipewireFormatChange::DmaBuf) => {
+ // A compositor that only shares DMA-BUFs (niri) still has to
+ // be told the consumer takes them.
+ if let Err(error) = request_dmabuf_buffers(stream) {
+ state.set_fatal_error(error.to_string());
+ }
+ }
+ Ok(PipewireFormatChange::None | PipewireFormatChange::Memory) => {}
+ Err(error) => state.set_fatal_error(error.to_string()),
}
})
.process(|stream, state| {
@@ -560,9 +622,23 @@ fn capture_wayland(
.register()
.context("register PipeWire stream listener")?;
- let param_bytes = pipewire_format_param(input.fps)?;
- let mut params = [spa::pod::Pod::from_bytes(&param_bytes)
- .ok_or_else(|| anyhow!("create PipeWire format parameter"))?];
+ // Shared memory first, so compositors that offer it (GNOME, KDE, wlroots)
+ // keep negotiating it; LINEAR DMA-BUFs for those that share nothing else.
+ // Shared memory, then LINEAR DMA-BUFs, both read on the CPU; then the
+ // tiled modifiers a GPU can import, for compositors that cannot render
+ // LINEAR. PipeWire takes the first offer the producer can meet.
+ let mut param_bytes = vec![
+ pipewire_format_param(input.fps)?,
+ pipewire_dmabuf_format_param(input.fps)?,
+ ];
+ param_bytes.extend(imported_params);
+ let mut params = param_bytes
+ .iter()
+ .map(|bytes| {
+ spa::pod::Pod::from_bytes(bytes)
+ .ok_or_else(|| anyhow!("create PipeWire format parameter"))
+ })
+ .collect::<anyhow::Result<Vec<_>>>()?;
stream
.connect(
@@ -601,16 +677,23 @@ fn capture_wayland(
}
}
+#[derive(Debug, PartialEq, Eq)]
+enum PipewireFormatChange {
+ None,
+ Memory,
+ DmaBuf,
+}
+
fn update_pipewire_format(
state: &mut PipewireCaptureState,
id: u32,
param: Option<&spa::pod::Pod>,
-) -> anyhow::Result<()> {
+) -> anyhow::Result<PipewireFormatChange> {
let Some(param) = param else {
- return Ok(());
+ return Ok(PipewireFormatChange::None);
};
if id != spa::param::ParamType::Format.as_raw() {
- return Ok(());
+ return Ok(PipewireFormatChange::None);
}
let (media_type, media_subtype) =
@@ -618,7 +701,7 @@ fn update_pipewire_format(
if media_type != spa::param::format::MediaType::Video
|| media_subtype != spa::param::format::MediaSubtype::Raw
{
- return Ok(());
+ return Ok(PipewireFormatChange::None);
}
let mut format = spa::param::video::VideoInfoRaw::default();
@@ -633,7 +716,141 @@ fn update_pipewire_format(
})?;
state.format = format;
- Ok(())
+ let dmabuf = param.as_object().is_ok_and(|object| {
+ object
+ .find_prop(spa::utils::Id(
+ spa::param::format::FormatProperties::VideoModifier.0,
+ ))
+ .is_some()
+ });
+ if dmabuf {
+ tracing::info!(
+ modifier = format.modifier(),
+ format = ?format.format(),
+ "PipeWire screen capture negotiated DMA-BUF frames"
+ );
+ }
+ Ok(if dmabuf {
+ PipewireFormatChange::DmaBuf
+ } else {
+ PipewireFormatChange::Memory
+ })
+}
+
+fn request_dmabuf_buffers(stream: &pw::stream::Stream) -> anyhow::Result<()> {
+ let dmabuf = 1 << spa::buffer::DataType::DmaBuf.as_raw();
+ let object = spa::pod::object!(
+ spa::utils::SpaTypes::ObjectParamBuffers,
+ spa::param::ParamType::Buffers,
+ spa::pod::Property::new(
+ spa::sys::SPA_PARAM_BUFFERS_dataType,
+ spa::pod::Value::Choice(spa::pod::ChoiceValue::Int(spa::utils::Choice(
+ spa::utils::ChoiceFlags::empty(),
+ spa::utils::ChoiceEnum::Flags {
+ default: dmabuf,
+ flags: vec![dmabuf],
+ },
+ ))),
+ )
+ );
+ let bytes = serialize_pod(object, "PipeWire buffers parameter")?;
+ let pod = spa::pod::Pod::from_bytes(&bytes)
+ .ok_or_else(|| anyhow!("create PipeWire buffers parameter"))?;
+ stream
+ .update_params(&mut [pod])
+ .context("ask PipeWire for DMA-BUF screen capture buffers")
+}
+
+/// A DMA-BUF the producer did not map, mapped for reading one frame. LINEAR
+/// buffers are plain rows a CPU can read once the kernel syncs the buffer.
+struct DmaBufMapping {
+ fd: std::os::fd::RawFd,
+ pointer: *mut libc::c_void,
+ len: usize,
+}
+
+const DMA_BUF_IOCTL_SYNC: libc::c_ulong = 0x4008_6200;
+const DMA_BUF_SYNC_READ: u64 = 1;
+const DMA_BUF_SYNC_END: u64 = 1 << 2;
+
+impl DmaBufMapping {
+ fn map(fd: std::os::fd::RawFd, minimum_len: usize) -> anyhow::Result<Self> {
+ if fd < 0 {
+ bail!("PipeWire screen capture DMA-BUF had no file descriptor");
+ }
+ let size = unsafe { libc::lseek(fd, 0, libc::SEEK_END) };
+ let len = usize::try_from(size)
+ .ok()
+ .filter(|len| *len > 0)
+ .unwrap_or(minimum_len);
+ if len < minimum_len {
+ bail!(
+ "PipeWire screen capture DMA-BUF holds {len} bytes, the frame needs {minimum_len}"
+ );
+ }
+ let pointer = unsafe {
+ libc::mmap(
+ std::ptr::null_mut(),
+ len,
+ libc::PROT_READ,
+ libc::MAP_SHARED,
+ fd,
+ 0,
+ )
+ };
+ if pointer == libc::MAP_FAILED {
+ return Err(std::io::Error::last_os_error())
+ .context("map the PipeWire screen capture DMA-BUF");
+ }
+ let mapping = Self { fd, pointer, len };
+ mapping
+ .sync(DMA_BUF_SYNC_READ)
+ .context("sync the PipeWire screen capture DMA-BUF for reading")?;
+ Ok(mapping)
+ }
+
+ /// The kernel's DMA-BUF read fence. A buffer that is not a DMA-BUF (a
+ /// memfd standing in for one) has none and answers ENOTTY.
+ fn sync(&self, flags: u64) -> std::io::Result<()> {
+ let sync = flags;
+ loop {
+ let result = unsafe { libc::ioctl(self.fd, DMA_BUF_IOCTL_SYNC, &sync as *const u64) };
+ if result == 0 {
+ return Ok(());
+ }
+ let error = std::io::Error::last_os_error();
+ match error.raw_os_error() {
+ Some(libc::EINTR | libc::EAGAIN) => continue,
+ Some(libc::ENOTTY) => return Ok(()),
+ _ => return Err(error),
+ }
+ }
+ }
+
+ fn bytes(&self) -> &[u8] {
+ unsafe { std::slice::from_raw_parts(self.pointer as *const u8, self.len) }
+ }
+}
+
+impl Drop for DmaBufMapping {
+ fn drop(&mut self) {
+ if let Err(error) = self.sync(DMA_BUF_SYNC_READ | DMA_BUF_SYNC_END) {
+ tracing::warn!(%error, "ending the PipeWire screen capture DMA-BUF read failed");
+ }
+ unsafe {
+ libc::munmap(self.pointer, self.len);
+ }
+ }
+}
+
+fn serialize_pod(object: spa::pod::Object, what: &str) -> anyhow::Result<Vec<u8>> {
+ Ok(spa::pod::serialize::PodSerializer::serialize(
+ std::io::Cursor::new(Vec::new()),
+ &spa::pod::Value::Object(object),
+ )
+ .map_err(|error| anyhow!("serialize {what}: {error:?}"))?
+ .0
+ .into_inner())
}
fn process_pipewire_frame(
@@ -660,8 +877,25 @@ fn process_pipewire_frame(
return Ok(None);
}
- let Some(raw_frame) = frame_from_pipewire_data(&mut datas[0], state.format, state.crop_bounds)?
- else {
+ let modifier = state.format.modifier();
+ let tiled = datas[0].type_() == spa::buffer::DataType::DmaBuf
+ && modifier != linux_dmabuf_import::DRM_FORMAT_MOD_LINEAR
+ && modifier != linux_dmabuf_import::DRM_FORMAT_MOD_INVALID;
+ let raw_frame = if tiled {
+ let Some(importer) = state.importer.as_mut() else {
+ bail!("PipeWire screen capture sent a tiled DMA-BUF Cap cannot import");
+ };
+ imported_pipewire_frame(
+ &datas[0],
+ state.format,
+ state.crop_bounds,
+ importer,
+ &mut state.imported,
+ )?
+ } else {
+ frame_from_pipewire_data(&mut datas[0], state.format, state.crop_bounds)?
+ };
+ let Some(raw_frame) = raw_frame else {
return Ok(Some(StallSendOutcome::StalledAndDropped { waited_ms: 0 }));
};
let frame = prepare_pipewire_frame(raw_frame, &mut state.scaler, state.video_info)?;
@@ -743,11 +977,22 @@ fn frame_from_pipewire_data(
};
let (crop_x, crop_y, crop_width, crop_height) =
pipewire_crop(source_width, source_height, crop_bounds)?;
- let source = data
- .data()
- .ok_or_else(|| anyhow!("PipeWire screen capture buffer was not memory-mapped"))?;
let offset = chunk_offset as usize;
- let source_limit = if chunk_size > 0 {
+ let dmabuf_fd = (data.type_() == spa::buffer::DataType::DmaBuf).then(|| data.fd() as i32);
+ let mapping;
+ let source: &[u8] = match data.data() {
+ Some(source) => source,
+ None => {
+ let Some(fd) = dmabuf_fd else {
+ bail!("PipeWire screen capture buffer was not memory-mapped");
+ };
+ mapping = DmaBufMapping::map(fd, offset + source_stride * source_height)?;
+ mapping.bytes()
+ }
+ };
+ // DMA-BUF producers may leave the chunk size unset (the PipeWire DMA-BUF
+ // docs tell consumers to ignore it), so the mapping bounds the frame.
+ let source_limit = if chunk_size > 0 && dmabuf_fd.is_none() {
offset
.checked_add(chunk_size as usize)
.map(|limit| limit.min(source.len()))
@@ -788,6 +1033,61 @@ fn frame_from_pipewire_data(
Ok(Some(frame))
}
+/// A frame with a GPU-tiled modifier, read back through EGL as RGBA.
+fn imported_pipewire_frame(
+ data: &spa::buffer::Data,
+ format: spa::param::video::VideoInfoRaw,
+ crop_bounds: Option<CropBounds>,
+ importer: &mut linux_dmabuf_import::DmaBufImporter,
+ scratch: &mut Vec<u8>,
+) -> anyhow::Result<Option<ffmpeg::frame::Video>> {
+ if data
+ .chunk()
+ .flags()
+ .contains(spa::buffer::ChunkFlags::CORRUPTED)
+ {
+ return Ok(None);
+ }
+ let fourcc = linux_dmabuf_import::drm_fourcc(format.format()).ok_or_else(|| {
+ anyhow!(
+ "Unsupported tiled PipeWire screen capture format: {:?}",
+ format.format()
+ )
+ })?;
+ let size = format.size();
+ let (width, height) = (size.width as usize, size.height as usize);
+ if width == 0 || height == 0 {
+ bail!("PipeWire screen capture stream did not provide frame dimensions");
+ }
+ let stride = u32::try_from(data.chunk().stride())
+ .map_err(|_| anyhow!("PipeWire screen capture frame used a negative stride"))?;
+ scratch.resize(width * height * 4, 0);
+ importer.read_rgba(
+ data.fd(),
+ fourcc,
+ format.modifier(),
+ size.width,
+ size.height,
+ data.chunk().offset(),
+ stride,
+ scratch,
+ )?;
+ let (crop_x, crop_y, crop_width, crop_height) = pipewire_crop(width, height, crop_bounds)?;
+ let mut frame = ffmpeg::frame::Video::new(
+ ffmpeg::format::Pixel::RGBA,
+ crop_width as u32,
+ crop_height as u32,
+ );
+ let target_stride = frame.stride(0);
+ let row_bytes = crop_width * 4;
+ for y in 0..crop_height {
+ let source = ((crop_y + y) * width + crop_x) * 4;
+ frame.data_mut(0)[y * target_stride..y * target_stride + row_bytes]
+ .copy_from_slice(&scratch[source..source + row_bytes]);
+ }
+ Ok(Some(frame))
+}
+
fn pipewire_crop(
source_width: usize,
source_height: usize,
@@ -910,6 +1210,136 @@ fn pipewire_format_param(fps: u32) -> anyhow::Result<Vec<u8>> {
.into_inner())
}
+/// The offer for compositors that share only DMA-BUFs with a mandatory
+/// modifier (niri 26.04): LINEAR, so the frames can be read on the CPU.
+fn pipewire_dmabuf_format_param(fps: u32) -> anyhow::Result<Vec<u8>> {
+ use spa::param::video::VideoFormat;
+ pipewire_dmabuf_format_param_for(
+ fps,
+ &[
+ VideoFormat::BGRx,
+ VideoFormat::BGRA,
+ VideoFormat::RGBx,
+ VideoFormat::RGBA,
+ ],
+ &[linux_dmabuf_import::DRM_FORMAT_MOD_LINEAR as i64],
+ )
+}
+
+/// A DMA-BUF offer for `formats` with `modifiers`. More than one modifier
+/// lets the producer choose (DONT_FIXATE), as the PipeWire DMA-BUF protocol
+/// asks.
+fn pipewire_dmabuf_format_param_for(
+ fps: u32,
+ formats: &[spa::param::video::VideoFormat],
+ modifiers: &[i64],
+) -> anyhow::Result<Vec<u8>> {
+ let fps = fps.max(1);
+ let (Some(first_format), Some(first_modifier)) = (formats.first(), modifiers.first()) else {
+ bail!("a DMA-BUF offer needs a format and a modifier");
+ };
+ let mut modifier_flags = spa::pod::PropertyFlags::MANDATORY;
+ if modifiers.len() > 1 {
+ modifier_flags = spa::pod::PropertyFlags::from_bits_retain(
+ modifier_flags.bits() | spa::sys::SPA_POD_PROP_FLAG_DONT_FIXATE,
+ );
+ }
+ let object = spa::pod::object!(
+ spa::utils::SpaTypes::ObjectParamFormat,
+ spa::param::ParamType::EnumFormat,
+ spa::pod::property!(
+ spa::param::format::FormatProperties::MediaType,
+ Id,
+ spa::param::format::MediaType::Video
+ ),
+ spa::pod::property!(
+ spa::param::format::FormatProperties::MediaSubtype,
+ Id,
+ spa::param::format::MediaSubtype::Raw
+ ),
+ spa::pod::Property::new(
+ spa::param::format::FormatProperties::VideoFormat.as_raw(),
+ spa::pod::Value::Choice(spa::pod::ChoiceValue::Id(spa::utils::Choice(
+ spa::utils::ChoiceFlags::empty(),
+ spa::utils::ChoiceEnum::Enum {
+ default: spa::utils::Id(first_format.as_raw()),
+ alternatives: formats
+ .iter()
+ .map(|format| spa::utils::Id(format.as_raw()))
+ .collect(),
+ },
+ ))),
+ ),
+ spa::pod::Property {
+ key: spa::param::format::FormatProperties::VideoModifier.as_raw(),
+ flags: modifier_flags,
+ value: spa::pod::Value::Choice(spa::pod::ChoiceValue::Long(spa::utils::Choice(
+ spa::utils::ChoiceFlags::empty(),
+ spa::utils::ChoiceEnum::Enum {
+ default: *first_modifier,
+ alternatives: modifiers.to_vec(),
+ },
+ ))),
+ },
+ spa::pod::property!(
+ spa::param::format::FormatProperties::VideoSize,
+ Choice,
+ Range,
+ Rectangle,
+ spa::utils::Rectangle {
+ width: 1920,
+ height: 1080
+ },
+ spa::utils::Rectangle {
+ width: 1,
+ height: 1
+ },
+ spa::utils::Rectangle {
+ width: 8192,
+ height: 8192
+ }
+ ),
+ spa::pod::property!(
+ spa::param::format::FormatProperties::VideoFramerate,
+ Choice,
+ Range,
+ Fraction,
+ spa::utils::Fraction { num: fps, denom: 1 },
+ spa::utils::Fraction { num: 0, denom: 1 },
+ spa::utils::Fraction {
+ num: 1000,
+ denom: 1
+ }
+ )
+ );
+ serialize_pod(object, "PipeWire DMA-BUF format parameter")
+}
+
+/// Offers for the GPU-tiled modifiers this machine can import, one per
+/// format: what a compositor that cannot render LINEAR (niri on NVIDIA)
+/// shares.
+fn pipewire_imported_format_params(
+ fps: u32,
+ importer: &mut linux_dmabuf_import::DmaBufImporter,
+) -> Vec<Vec<u8>> {
+ use spa::param::video::VideoFormat;
+ [VideoFormat::BGRx, VideoFormat::BGRA]
+ .into_iter()
+ .filter_map(|format| {
+ let fourcc = linux_dmabuf_import::drm_fourcc(format)?;
+ let modifiers = importer
+ .modifiers(fourcc)
+ .into_iter()
+ .filter(|modifier| *modifier != linux_dmabuf_import::DRM_FORMAT_MOD_LINEAR)
+ .map(|modifier| modifier as i64)
+ .collect::<Vec<_>>();
+ (!modifiers.is_empty())
+ .then(|| pipewire_dmabuf_format_param_for(fps, &[format], &modifiers).ok())
+ .flatten()
+ })
+ .collect()
+}
+
pub struct SystemAudioSourceConfig {
feed_lock: Arc<MicrophoneFeedLock>,
device_name: String,
@@ -2991,9 +3421,189 @@ mod system_audio_tests {
#[cfg(test)]
mod pipewire_frame_tests {
use super::{
- FrameScaler, LinuxCaptureSource, VideoInfo, prefers_wayland_environment,
- prepare_pipewire_frame, wayland_area_matches_display, wayland_video_info,
+ FrameScaler, LinuxCaptureSource, PipewireNodeInput, VideoInfo, capture_pipewire_node,
+ frame_from_pipewire_data, pipewire_dmabuf_format_param, pipewire_format_param,
+ prefers_wayland_environment, prepare_pipewire_frame, spa, wayland_area_matches_display,
+ wayland_video_info,
};
+ use std::os::fd::AsRawFd;
+
+ /// GitHub #2941: niri 26.04 shares screencasts only as DMA-BUFs with a
+ /// mandatory modifier, which 0.6.1 never offered, so nothing was ever
+ /// captured. This records from a producer that negotiates and shares
+ /// frames the way niri does (scripts/linux-desktop-harness/niri-screencast)
+ /// through Cap's real PipeWire capture loop.
+ #[test]
+ #[ignore = "needs a PipeWire daemon and CAP_FAKE_NIRI_PRODUCER"]
+ fn frames_from_a_producer_that_shares_only_dmabufs_are_captured() {
+ use std::io::BufRead;
+
+ let producer = std::env::var("CAP_FAKE_NIRI_PRODUCER")
+ .expect("set CAP_FAKE_NIRI_PRODUCER to the built producer");
+ let mut child = std::process::Command::new(producer)
+ .stdout(std::process::Stdio::piped())
+ .spawn()
+ .unwrap();
+ struct Kill(std::process::Child);
+ impl Drop for Kill {
+ fn drop(&mut self) {
+ let _ = self.0.kill();
+ let _ = self.0.wait();
+ }
+ }
+ let stdout = child.stdout.take().unwrap();
+ let child = Kill(child);
+ let (node_tx, node_rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ for line in std::io::BufReader::new(stdout)
+ .lines()
+ .map_while(Result::ok)
+ {
+ if let Some(node) = line.strip_prefix("node ") {
+ let _ = node_tx.send(node.trim().parse::<u32>().unwrap());
+ }
+ }
+ });
+ let node_id = node_rx
+ .recv_timeout(std::time::Duration::from_secs(10))
+ .expect("the producer announced no node");
+
+ let runtime = std::env::var("PIPEWIRE_RUNTIME_DIR")
+ .or_else(|_| std::env::var("XDG_RUNTIME_DIR"))
+ .unwrap();
+ let socket =
+ std::os::unix::net::UnixStream::connect(format!("{runtime}/pipewire-0")).unwrap();
+ let (video_tx, mut video_rx) = futures::channel::mpsc::channel(4);
+ let (health_tx, _health_rx) = tokio::sync::mpsc::channel(16);
+ let stop = tokio_util::sync::CancellationToken::new();
+ let capture = std::thread::spawn({
+ let stop = stop.clone();
+ move || {
+ capture_pipewire_node(
+ VideoInfo::from_raw_ffmpeg(ffmpeg::format::Pixel::BGRZ, 640, 360, 30),
+ PipewireNodeInput {
+ fd: socket.into(),
+ node_id,
+ fps: 30,
+ crop_bounds: None,
+ },
+ video_tx,
+ stop,
+ health_tx,
+ )
+ }
+ });
+
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
+ let mut frames = Vec::new();
+ loop {
+ if frames.len() >= 10 || std::time::Instant::now() >= deadline || capture.is_finished()
+ {
+ break;
+ }
+ match video_rx.try_next() {
+ Ok(Some(frame)) => frames.push(frame.inner),
+ Ok(None) => break,
+ Err(_) => std::thread::park_timeout(std::time::Duration::from_millis(10)),
+ }
+ }
+ stop.cancel();
+ let result = capture.join().unwrap();
+ drop(child);
+ assert!(
+ frames.len() >= 10,
+ "captured {} frames; capture ended with {result:?}",
+ frames.len()
+ );
+ result.unwrap();
+ let mut levels = std::collections::BTreeSet::new();
+ for frame in &frames {
+ assert_eq!((frame.width(), frame.height()), (640, 360));
+ let pixel = &frame.data(0)[..4];
+ assert_eq!(&pixel[1..3], &[0x40, 0x80], "{pixel:?}");
+ levels.insert(pixel[0]);
+ }
+ assert!(
+ levels.len() > 1,
+ "every captured frame was the same picture"
+ );
+ }
+
+ /// niri 26.04 shares only DMA-BUFs: the buffer is not mapped for the
+ /// consumer, `maxsize` is 1 and the chunk carries just stride and offset.
+ /// 0.6.1 rejected every such frame ("buffer was not memory-mapped").
+ #[test]
+ fn an_unmapped_dmabuf_frame_is_read_from_its_file_descriptor() {
+ let (width, height, stride) = (4usize, 3usize, 24usize);
+ let mut bytes = vec![0u8; stride * height];
+ for y in 0..height {
+ for x in 0..width {
+ let pixel = &mut bytes[y * stride + x * 4..][..4];
+ pixel.copy_from_slice(&[x as u8, y as u8, 0x80, 0xff]);
+ }
+ }
+ let file = tempfile::tempfile().unwrap();
+ std::io::Write::write_all(&mut &file, &bytes).unwrap();
+
+ let mut chunk = spa::sys::spa_chunk {
+ offset: 0,
+ size: 0,
+ stride: stride as i32,
+ flags: 0,
+ };
+ let mut raw: spa::sys::spa_data = unsafe { std::mem::zeroed() };
+ raw.type_ = spa::sys::SPA_DATA_DmaBuf;
+ raw.flags = spa::sys::SPA_DATA_FLAG_READWRITE;
+ raw.fd = i64::from(file.as_raw_fd());
+ raw.maxsize = 1;
+ raw.data = std::ptr::null_mut();
+ raw.chunk = &mut chunk;
+ let data = unsafe { &mut *(&mut raw as *mut spa::sys::spa_data as *mut spa::buffer::Data) };
+
+ let mut format = spa::param::video::VideoInfoRaw::new();
+ format.set_format(spa::param::video::VideoFormat::BGRx);
+ format.set_size(spa::utils::Rectangle {
+ width: width as u32,
+ height: height as u32,
+ });
+ format.set_modifier(0);
+ let frame = frame_from_pipewire_data(data, format, None)
+ .unwrap()
+ .expect("a frame");
+ assert_eq!((frame.width(), frame.height()), (4, 3));
+ assert_eq!(frame.format(), ffmpeg::format::Pixel::BGRZ);
+ for y in 0..height {
+ let row = &frame.data(0)[y * frame.stride(0)..][..width * 4];
+ assert_eq!(row, &bytes[y * stride..][..width * 4], "row {y}");
+ }
+ }
+
+ #[test]
+ fn dmabuf_offer_follows_shared_memory_with_a_mandatory_linear_modifier() {
+ let shm = pipewire_format_param(30).unwrap();
+ let shm = spa::pod::Pod::from_bytes(&shm)
+ .unwrap()
+ .as_object()
+ .unwrap();
+ let modifier = spa::utils::Id(spa::param::format::FormatProperties::VideoModifier.0);
+ assert!(shm.find_prop(modifier).is_none());
+
+ let bytes = pipewire_dmabuf_format_param(30).unwrap();
+ let object = spa::pod::Pod::from_bytes(&bytes)
+ .unwrap()
+ .as_object()
+ .unwrap();
+ let prop = object.find_prop(modifier).expect("a modifier property");
+ assert!(prop.flags().contains(spa::pod::PodPropFlags::MANDATORY));
+ let (_, value) = spa::pod::deserialize::PodDeserializer::deserialize_from::<
+ spa::utils::Choice<i64>,
+ >(prop.value().as_bytes())
+ .unwrap();
+ let spa::utils::ChoiceEnum::Enum { alternatives, .. } = value.1 else {
+ panic!("modifier is not an enum choice");
+ };
+ assert_eq!(alternatives, vec![0]);
+ }
#[test]
fn wayland_area_requires_the_selected_monitor_in_the_portal() {
diff --git a/crates/recording/src/sources/screen_capture/linux_dmabuf_import.rs b/crates/recording/src/sources/screen_capture/linux_dmabuf_import.rs
new file mode 100644
index 000000000..b2a45547f
--- /dev/null
+++ b/crates/recording/src/sources/screen_capture/linux_dmabuf_import.rs
@@ -0,0 +1,604 @@
+//! Reads DMA-BUF screencast frames whose modifier a CPU cannot read directly.
+//!
+//! A compositor that shares only DMA-BUFs allocates them on its GPU with a
+//! modifier both sides accept. On NVIDIA no LINEAR buffer can be rendered to
+//! (`eglQueryDmaBufModifiersEXT` lists LINEAR as external only), so niri
+//! offers only NVIDIA's block-linear modifiers. Those frames are imported into
+//! EGL as images, attached to a framebuffer and read back as plain RGBA.
+
+use anyhow::{Context as _, anyhow, bail};
+use glow::HasContext as _;
+use khronos_egl as egl;
+use std::ffi::c_void;
+use std::os::fd::RawFd;
+
+const EGL_PLATFORM_DEVICE_EXT: egl::Enum = 0x313F;
+const EGL_DRM_RENDER_NODE_FILE_EXT: egl::Int = 0x3377;
+const EGL_LINUX_DMA_BUF_EXT: egl::Enum = 0x3270;
+const EGL_LINUX_DRM_FOURCC_EXT: egl::Attrib = 0x3271;
+const EGL_DMA_BUF_PLANE0_FD_EXT: egl::Attrib = 0x3272;
+const EGL_DMA_BUF_PLANE0_OFFSET_EXT: egl::Attrib = 0x3273;
+const EGL_DMA_BUF_PLANE0_PITCH_EXT: egl::Attrib = 0x3274;
+const EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT: egl::Attrib = 0x3443;
+const EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT: egl::Attrib = 0x3444;
+const EGL_NO_CONTEXT: egl::EGLContext = std::ptr::null_mut();
+
+pub(super) const DRM_FORMAT_MOD_LINEAR: u64 = 0;
+pub(super) const DRM_FORMAT_MOD_INVALID: u64 = 0x00ff_ffff_ffff_ffff;
+
+type QueryDevices = unsafe extern "C" fn(egl::Int, *mut *mut c_void, *mut egl::Int) -> egl::Boolean;
+type QueryDeviceString = unsafe extern "C" fn(*mut c_void, egl::Int) -> *const std::ffi::c_char;
+type GetPlatformDisplay =
+ unsafe extern "C" fn(egl::Enum, *mut c_void, *const egl::Int) -> egl::EGLDisplay;
+type QueryDmaBufModifiers = unsafe extern "C" fn(
+ egl::EGLDisplay,
+ egl::Int,
+ egl::Int,
+ *mut u64,
+ *mut egl::Boolean,
+ *mut egl::Int,
+) -> egl::Boolean;
+type ImageTargetTexture = unsafe extern "C" fn(u32, *mut c_void);
+
+/// The DRM fourcc for a PipeWire raw video format.
+pub(super) fn drm_fourcc(format: pipewire::spa::param::video::VideoFormat) -> Option<u32> {
+ use pipewire::spa::param::video::VideoFormat;
+ let code = |s: &[u8; 4]| u32::from_le_bytes(*s);
+ if format == VideoFormat::BGRx {
+ Some(code(b"XR24"))
+ } else if format == VideoFormat::BGRA {
+ Some(code(b"AR24"))
+ } else if format == VideoFormat::RGBx {
+ Some(code(b"XB24"))
+ } else if format == VideoFormat::RGBA {
+ Some(code(b"AB24"))
+ } else {
+ None
+ }
+}
+
+/// One GPU's EGL device, with a context that can read its imports back.
+struct GpuImporter {
+ egl: egl::DynamicInstance<egl::EGL1_5>,
+ display: egl::Display,
+ context: egl::Context,
+ gl: Option<glow::Context>,
+ image_target_texture: ImageTargetTexture,
+ query_modifiers: QueryDmaBufModifiers,
+ framebuffer: Option<glow::Framebuffer>,
+ texture: Option<glow::Texture>,
+ /// The kernel driver of the GPU's render node (`amdgpu`, `nvidia`, ...).
+ driver: Option<String>,
+ /// The GPU's render node, which identifies it between recordings.
+ node: Option<String>,
+}
+
+// The EGL context is made current on whichever single thread uses the
+// importer, and released before it moves.
+unsafe impl Send for GpuImporter {}
+
+/// Every GPU that can import screencast DMA-BUFs. The compositor allocates
+/// on whichever GPU it renders with, and a hybrid laptop's first GPU is often
+/// not that one, so modifiers are offered from all of them and each frame
+/// goes to a GPU that lists its modifier.
+pub(super) struct DmaBufImporter {
+ gpus: Vec<GpuImporter>,
+ supported: Vec<std::collections::HashMap<u32, Vec<u64>>>,
+ last: usize,
+ remembered: bool,
+}
+
+/// The render node of the GPU that last read a screencast frame, kept for the
+/// rest of the session.
+static LAST_IMPORT_NODE: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
+
+fn remembered_gpu(nodes: &[Option<&str>], remembered: Option<&str>) -> usize {
+ remembered
+ .and_then(|remembered| nodes.iter().position(|node| *node == Some(remembered)))
+ .unwrap_or(0)
+}
+
+impl DmaBufImporter {
+ pub(super) fn new() -> anyhow::Result<Self> {
+ let mut gpus = Vec::new();
+ let mut last_error = None;
+ for index in 0..16 {
+ match GpuImporter::open(index) {
+ Ok(Some(gpu)) => gpus.push(gpu),
+ Ok(None) => break,
+ Err(error) => {
+ tracing::debug!(index, %error, "EGL device cannot import DMA-BUFs");
+ last_error = Some(error);
+ }
+ }
+ }
+ if gpus.is_empty() {
+ return Err(last_error.unwrap_or_else(|| anyhow!("EGL lists no devices")));
+ }
+ let supported = gpus.iter().map(|_| Default::default()).collect();
+ // Several GPUs of one vendor can't be told apart by the frame's
+ // exporter; the one that read the previous recording is tried first.
+ let last = remembered_gpu(
+ &gpus
+ .iter()
+ .map(|gpu| gpu.node.as_deref())
+ .collect::<Vec<_>>(),
+ LAST_IMPORT_NODE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .as_deref(),
+ );
+ Ok(Self {
+ gpus,
+ supported,
+ last,
+ remembered: false,
+ })
+ }
+
+ fn supported(&mut self, gpu: usize, fourcc: u32) -> &[u64] {
+ let gpus = &self.gpus;
+ self.supported[gpu]
+ .entry(fourcc)
+ .or_insert_with(|| gpus[gpu].modifiers(fourcc))
+ }
+
+ /// The modifiers some GPU can import `fourcc` with and read back.
+ pub(super) fn modifiers(&mut self, fourcc: u32) -> Vec<u64> {
+ let mut all = Vec::new();
+ for gpu in 0..self.gpus.len() {
+ for &modifier in self.supported(gpu, fourcc) {
+ if !all.contains(&modifier) {
+ all.push(modifier);
+ }
+ }
+ }
+ all
+ }
+
+ /// Reads one single-plane DMA-BUF into `out` as tightly packed RGBA,
+ /// starting with the GPU that read the last frame.
+ #[allow(clippy::too_many_arguments)]
+ pub(super) fn read_rgba(
+ &mut self,
+ fd: RawFd,
+ fourcc: u32,
+ modifier: u64,
+ width: u32,
+ height: u32,
+ offset: u32,
+ stride: u32,
+ out: &mut [u8],
+ ) -> anyhow::Result<()> {
+ let exporter = dma_buf_exporter(fd);
+ let drivers = self
+ .gpus
+ .iter()
+ .map(|gpu| gpu.driver.as_deref())
+ .collect::<Vec<_>>();
+ let order = import_order(&drivers, exporter.as_deref(), self.last);
+ let mut last_error = None;
+ for gpu in order {
+ if !self.supported(gpu, fourcc).contains(&modifier) {
+ continue;
+ }
+ match self.gpus[gpu].read_rgba(fd, fourcc, modifier, width, height, offset, stride, out)
+ {
+ Ok(()) => {
+ if self.last != gpu || !self.remembered {
+ *LAST_IMPORT_NODE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) =
+ self.gpus[gpu].node.clone();
+ self.remembered = true;
+ }
+ self.last = gpu;
+ return Ok(());
+ }
+ Err(error) => last_error = Some(error),
+ }
+ }
+ Err(last_error
+ .unwrap_or_else(|| anyhow!("no GPU imports modifier {modifier:#x} for {fourcc:#x}")))
+ }
+}
+
+impl GpuImporter {
+ /// The `index`th GPU EGL lists, or `None` past the last.
+ fn open(index: usize) -> anyhow::Result<Option<Self>> {
+ let egl = unsafe {
+ egl::DynamicInstance::<egl::EGL1_5>::load_required_from_filename("libEGL.so.1")
+ }
+ .map_err(|error| anyhow!("load libEGL: {error}"))?;
+ let proc = |name: &str| {
+ egl.get_proc_address(name)
+ .map(|f| f as *const c_void)
+ .ok_or_else(|| anyhow!("EGL has no {name}"))
+ };
+ let query_devices: QueryDevices =
+ unsafe { std::mem::transmute(proc("eglQueryDevicesEXT")?) };
+ let query_device_string: QueryDeviceString =
+ unsafe { std::mem::transmute(proc("eglQueryDeviceStringEXT")?) };
+ let get_platform_display: GetPlatformDisplay =
+ unsafe { std::mem::transmute(proc("eglGetPlatformDisplayEXT")?) };
+ let query_modifiers: QueryDmaBufModifiers =
+ unsafe { std::mem::transmute(proc("eglQueryDmaBufModifiersEXT")?) };
+ let image_target_texture: ImageTargetTexture =
+ unsafe { std::mem::transmute(proc("glEGLImageTargetTexture2DOES")?) };
+
+ let mut devices = [std::ptr::null_mut(); 16];
+ let mut count = 0;
+ if unsafe { query_devices(devices.len() as egl::Int, devices.as_mut_ptr(), &mut count) }
+ == egl::FALSE
+ {
+ bail!("eglQueryDevicesEXT failed");
+ }
+ // A GPU (a device with a render node) can import what a GPU
+ // allocated; without one, the devices EGL has are all there is.
+ let devices = &devices[..count.max(0) as usize];
+ let device_string = |device: *mut c_void, name: egl::Int| {
+ let value = unsafe { query_device_string(device, name) };
+ (!value.is_null()).then(|| {
+ unsafe { std::ffi::CStr::from_ptr(value) }
+ .to_string_lossy()
+ .into_owned()
+ })
+ };
+ let gpus = devices
+ .iter()
+ .filter_map(|&device| {
+ let node = device_string(device, EGL_DRM_RENDER_NODE_FILE_EXT)?;
+ let driver = render_node_driver(&node);
+ let extensions = device_string(device, egl::EXTENSIONS).unwrap_or_default();
+ // Opening a device no EGL vendor can drive only logs errors.
+ drivable(driver.as_deref(), &extensions).then_some((device, driver, Some(node)))
+ })
+ .collect::<Vec<_>>();
+ let candidates = if gpus.is_empty() {
+ devices.iter().map(|&device| (device, None, None)).collect()
+ } else {
+ gpus
+ };
+ let Some((device, driver, node)) = candidates.get(index).cloned() else {
+ return Ok(None);
+ };
+ let raw =
+ unsafe { get_platform_display(EGL_PLATFORM_DEVICE_EXT, device, std::ptr::null()) };
+ if raw.is_null() {
+ bail!("no EGL display for the device");
+ }
+ let display = unsafe { egl::Display::from_ptr(raw) };
+ egl.initialize(display).context("initialize EGL")?;
+ let extensions = egl
+ .query_string(Some(display), egl::EXTENSIONS)
+ .context("query EGL extensions")?
+ .to_string_lossy()
+ .into_owned();
+ for wanted in [
+ "EGL_EXT_image_dma_buf_import_modifiers",
+ "EGL_KHR_surfaceless_context",
+ ] {
+ if !extensions.split(' ').any(|extension| extension == wanted) {
+ bail!("EGL has no {wanted}");
+ }
+ }
+ egl.bind_api(egl::OPENGL_ES_API).context("bind GLES")?;
+ let config = egl
+ .choose_first_config(
+ display,
+ // Surfaceless: any surface type. The default asks for window
+ // surfaces, which a GPU's EGL device (NVIDIA's) never has.
+ &[
+ egl::SURFACE_TYPE,
+ 0,
+ egl::RENDERABLE_TYPE,
+ egl::OPENGL_ES2_BIT,
+ egl::NONE,
+ ],
+ )
+ .context("choose an EGL config")?
+ .ok_or_else(|| anyhow!("no GLES EGL config"))?;
+ let context = egl
+ .create_context(
+ display,
+ config,
+ None,
+ &[egl::CONTEXT_CLIENT_VERSION, 2, egl::NONE],
+ )
+ .context("create a GLES context")?;
+ let mut importer = Self {
+ egl,
+ display,
+ context,
+ gl: None,
+ image_target_texture,
+ query_modifiers,
+ framebuffer: None,
+ texture: None,
+ driver,
+ node,
+ };
+ importer.make_current()?;
+ importer.release();
+ Ok(Some(importer))
+ }
+
+ fn make_current(&mut self) -> anyhow::Result<()> {
+ self.egl
+ .make_current(self.display, None, None, Some(self.context))
+ .context("make the EGL context current")?;
+ if self.gl.is_none() {
+ let egl = &self.egl;
+ let gl = unsafe {
+ glow::Context::from_loader_function(|name| {
+ egl.get_proc_address(name)
+ .map_or(std::ptr::null(), |f| f as *const c_void)
+ })
+ };
+ self.gl = Some(gl);
+ }
+ Ok(())
+ }
+
+ /// Lets another thread make the context current.
+ pub(super) fn release(&self) {
+ let _ = self.egl.make_current(self.display, None, None, None);
+ }
+
+ /// The modifiers this GPU can import `fourcc` with and render to, so a
+ /// framebuffer can read them back.
+ pub(super) fn modifiers(&self, fourcc: u32) -> Vec<u64> {
+ let mut modifiers = vec![0u64; 64];
+ let mut external = vec![egl::FALSE; 64];
+ let mut count = 0;
+ let ok = unsafe {
+ (self.query_modifiers)(
+ self.display.as_ptr(),
+ fourcc as egl::Int,
+ modifiers.len() as egl::Int,
+ modifiers.as_mut_ptr(),
+ external.as_mut_ptr(),
+ &mut count,
+ )
+ };
+ if ok == egl::FALSE {
+ return Vec::new();
+ }
+ modifiers
+ .into_iter()
+ .zip(external)
+ .take(count.max(0) as usize)
+ .filter(|(modifier, external)| {
+ *external == egl::FALSE && *modifier != DRM_FORMAT_MOD_INVALID
+ })
+ .map(|(modifier, _)| modifier)
+ .collect()
+ }
+
+ /// Reads one single-plane DMA-BUF into `out` as tightly packed RGBA.
+ #[allow(clippy::too_many_arguments)]
+ pub(super) fn read_rgba(
+ &mut self,
+ fd: RawFd,
+ fourcc: u32,
+ modifier: u64,
+ width: u32,
+ height: u32,
+ offset: u32,
+ stride: u32,
+ out: &mut [u8],
+ ) -> anyhow::Result<()> {
+ let needed = width as usize * height as usize * 4;
+ if out.len() < needed {
+ bail!(
+ "RGBA readback buffer holds {} bytes, needs {needed}",
+ out.len()
+ );
+ }
+ self.make_current()?;
+ let attributes = [
+ egl::WIDTH as egl::Attrib,
+ width as egl::Attrib,
+ egl::HEIGHT as egl::Attrib,
+ height as egl::Attrib,
+ EGL_LINUX_DRM_FOURCC_EXT,
+ fourcc as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_FD_EXT,
+ fd as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_OFFSET_EXT,
+ offset as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_PITCH_EXT,
+ stride as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT,
+ (modifier & 0xffff_ffff) as egl::Attrib,
+ EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT,
+ (modifier >> 32) as egl::Attrib,
+ egl::ATTRIB_NONE,
+ ];
+ let image = self
+ .egl
+ .create_image(
+ self.display,
+ unsafe { egl::Context::from_ptr(EGL_NO_CONTEXT) },
+ EGL_LINUX_DMA_BUF_EXT,
+ unsafe { egl::ClientBuffer::from_ptr(std::ptr::null_mut()) },
+ &attributes,
+ )
+ .context("import the DMA-BUF into EGL")?;
+ let result = self.read_image(&image, width, height, &mut out[..needed]);
+ let _ = self.egl.destroy_image(self.display, image);
+ self.release();
+ result
+ }
+
+ fn read_image(
+ &mut self,
+ image: &egl::Image,
+ width: u32,
+ height: u32,
+ out: &mut [u8],
+ ) -> anyhow::Result<()> {
+ let gl = self.gl.as_ref().expect("GL loaded with the context");
+ unsafe {
+ let texture = match self.texture {
+ Some(texture) => texture,
+ None => {
+ let texture = gl.create_texture().map_err(|error| anyhow!(error))?;
+ self.texture = Some(texture);
+ texture
+ }
+ };
+ gl.bind_texture(glow::TEXTURE_2D, Some(texture));
+ (self.image_target_texture)(glow::TEXTURE_2D, image.as_ptr());
+ let framebuffer = match self.framebuffer {
+ Some(framebuffer) => framebuffer,
+ None => {
+ let framebuffer = gl.create_framebuffer().map_err(|error| anyhow!(error))?;
+ self.framebuffer = Some(framebuffer);
+ framebuffer
+ }
+ };
+ gl.bind_framebuffer(glow::FRAMEBUFFER, Some(framebuffer));
+ gl.framebuffer_texture_2d(
+ glow::FRAMEBUFFER,
+ glow::COLOR_ATTACHMENT0,
+ glow::TEXTURE_2D,
+ Some(texture),
+ 0,
+ );
+ let status = gl.check_framebuffer_status(glow::FRAMEBUFFER);
+ if status != glow::FRAMEBUFFER_COMPLETE {
+ bail!("the imported DMA-BUF is not readable (framebuffer status {status:#x})");
+ }
+ gl.pixel_store_i32(glow::PACK_ALIGNMENT, 1);
+ gl.read_pixels(
+ 0,
+ 0,
+ width as i32,
+ height as i32,
+ glow::RGBA,
+ glow::UNSIGNED_BYTE,
+ glow::PixelPackData::Slice(Some(out)),
+ );
+ let error = gl.get_error();
+ if error != glow::NO_ERROR {
+ bail!("reading the imported DMA-BUF failed (GL error {error:#x})");
+ }
+ }
+ Ok(())
+ }
+}
+
+impl Drop for GpuImporter {
+ fn drop(&mut self) {
+ if self.make_current().is_ok() {
+ if let Some(gl) = &self.gl {
+ unsafe {
+ if let Some(framebuffer) = self.framebuffer.take() {
+ gl.delete_framebuffer(framebuffer);
+ }
+ if let Some(texture) = self.texture.take() {
+ gl.delete_texture(texture);
+ }
+ }
+ }
+ self.release();
+ }
+ let _ = self.egl.destroy_context(self.display, self.context);
+ }
+}
+
+/// The kernel driver bound to a DRM node, from sysfs.
+fn render_node_driver(node: &str) -> Option<String> {
+ let name = std::path::Path::new(node).file_name()?;
+ let link = std::fs::read_link(
+ std::path::Path::new("/sys/class/drm")
+ .join(name)
+ .join("device/driver"),
+ )
+ .ok()?;
+ Some(link.file_name()?.to_string_lossy().into_owned())
+}
+
+/// Whether an EGL device can be initialized at all. vgem and vkms have no
+/// GPU behind them, and Mesa lists NVIDIA's nodes but only NVIDIA's own EGL
+/// (the one with `EGL_NV_device_cuda`) drives them.
+fn drivable(driver: Option<&str>, extensions: &str) -> bool {
+ match driver {
+ Some("vgem" | "vkms") => false,
+ Some(driver) if driver.starts_with("nvidia") => extensions
+ .split_ascii_whitespace()
+ .any(|extension| extension == "EGL_NV_device_cuda"),
+ _ => true,
+ }
+}
+
+/// The driver that exported a DMA-BUF (`exp_name` in its fdinfo).
+fn dma_buf_exporter(fd: RawFd) -> Option<String> {
+ let info = std::fs::read_to_string(format!("/proc/self/fdinfo/{fd}")).ok()?;
+ info.lines()
+ .find_map(|line| line.strip_prefix("exp_name:"))
+ .map(|name| name.trim().to_string())
+}
+
+/// GPUs to try a frame on: those whose driver exported it, then the rest,
+/// each starting from the GPU that read the last frame.
+fn import_order(drivers: &[Option<&str>], exporter: Option<&str>, last: usize) -> Vec<usize> {
+ let count = drivers.len();
+ let rotated = (0..count).map(|step| (last + step) % count);
+ let exported_by = |gpu: usize| {
+ matches!((drivers[gpu], exporter), (Some(driver), Some(exporter))
+ if exporter.starts_with(driver) || driver.starts_with(exporter))
+ };
+ let (mut order, rest): (Vec<_>, Vec<_>) = rotated.partition(|&gpu| exported_by(gpu));
+ order.extend(rest);
+ order
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn devices_without_a_driver_for_them_are_not_opened() {
+ assert!(!drivable(Some("vgem"), "EGL_EXT_device_drm"));
+ assert!(!drivable(Some("vkms"), ""));
+ assert!(!drivable(
+ Some("nvidia"),
+ "EGL_EXT_device_drm EGL_EXT_device_drm_render_node"
+ ));
+ assert!(drivable(
+ Some("nvidia"),
+ "EGL_NV_device_cuda EGL_EXT_device_drm"
+ ));
+ assert!(drivable(Some("amdgpu"), "EGL_EXT_device_drm"));
+ assert!(drivable(None, ""));
+ }
+
+ #[test]
+ fn the_gpu_that_read_the_last_recording_is_tried_first() {
+ let nodes = [
+ Some("/dev/dri/renderD128"),
+ Some("/dev/dri/renderD129"),
+ Some("/dev/dri/renderD131"),
+ ];
+ assert_eq!(remembered_gpu(&nodes, Some("/dev/dri/renderD131")), 2);
+ assert_eq!(remembered_gpu(&nodes, None), 0);
+ // A GPU that has gone since (unplugged, driver reloaded) is ignored.
+ assert_eq!(remembered_gpu(&nodes, Some("/dev/dri/renderD130")), 0);
+ }
+
+ #[test]
+ fn a_frame_goes_first_to_the_gpu_whose_driver_exported_it() {
+ // An Intel and NVIDIA laptop: niri renders on NVIDIA, EGL lists Intel first.
+ let drivers = [Some("i915"), Some("nvidia")];
+ assert_eq!(import_order(&drivers, Some("nvidia-drm"), 0), vec![1, 0]);
+ assert_eq!(import_order(&drivers, Some("i915"), 1), vec![0, 1]);
+ // Unknown exporter: the GPU that read the last frame first.
+ assert_eq!(import_order(&drivers, None, 1), vec![1, 0]);
+ // Several GPUs of one vendor keep the last one first among them.
+ let same = [Some("nvidia"), Some("nvidia"), Some("nvidia")];
+ assert_eq!(import_order(&same, Some("nvidia-drm"), 2), vec![2, 0, 1]);
+ assert!(import_order(&[], None, 0).is_empty());
+ }
+}
diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs
index 9e8a19c51..688cee38b 100644
--- a/crates/recording/src/sources/screen_capture/mod.rs
+++ b/crates/recording/src/sources/screen_capture/mod.rs
@@ -23,6 +23,8 @@ pub use macos::*;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
+mod linux_dmabuf_import;
+#[cfg(target_os = "linux")]
mod linux_shm;
#[cfg(target_os = "linux")]
pub use linux::*;
diff --git a/scripts/finalize-linux-appimage.mjs b/scripts/finalize-linux-appimage.mjs
index b1917414a..adb38f432 100644
--- a/scripts/finalize-linux-appimage.mjs
+++ b/scripts/finalize-linux-appimage.mjs
@@ -6,6 +6,7 @@ import {
chmod,
copyFile,
lstat,
+ mkdir,
mkdtemp,
open,
readdir,
@@ -104,11 +105,142 @@ esac
exec "$cap_appdir/AppRun.cap-original" "$@"
`;
-export async function preserveAppImageWorkingDirectory(appDir) {
+// The GPUI app finds every bundled library through RUNPATH, so it starts Cap
+// directly. The generic AppImage launcher's LD_LIBRARY_PATH, PYTHONHOME and
+// friends reached the host's PipeWire plugins inside Cap and every program Cap
+// starts (file managers, xdg-open), which then loaded Ubuntu's libraries: on
+// NixOS and Arch screen capture failed to connect and folders did not open.
+// It keeps what that launcher did for Cap: `usr/bin` first on PATH (Cap runs
+// the bundled pactl), the bundled data directory, and `usr` as the working
+// directory. The bundled libva is only used when the host has none, because
+// host PipeWire plugins link a host FFmpeg that needs the host's newer libva.
+export const directAppRunWrapper = `#!/bin/sh
+unset OWD
+if cap_original_directory="$(pwd -P && printf '.')"; then
+ OWD="\${cap_original_directory%?}"
+ OWD="\${OWD%?}"
+ export OWD
+fi
+case "$0" in
+ */*) cap_appdir="\${0%/*}" ;;
+ *) cap_appdir=. ;;
+esac
+cap_appdir="$(cd "$cap_appdir" && pwd -P)" || exit 1
+export PATH="$cap_appdir/usr/bin:\${PATH:-/usr/local/bin:/usr/bin:/bin}"
+export XDG_DATA_DIRS="$cap_appdir/usr/share:\${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
+cap_host_libva=0
+for cap_directory in /usr/lib/x86_64-linux-gnu /lib/x86_64-linux-gnu /usr/lib/aarch64-linux-gnu /lib/aarch64-linux-gnu /usr/lib64 /lib64 /usr/lib /lib; do
+ if [ -e "$cap_directory/libva.so.2" ] && [ -e "$cap_directory/libva-drm.so.2" ]; then
+ cap_host_libva=1
+ break
+ fi
+done
+if [ "$cap_host_libva" = 0 ] && ldconfig -p 2>/dev/null | grep -q 'libva-drm[.]so[.]2 '; then
+ cap_host_libva=1
+fi
+if [ "$cap_host_libva" = 0 ]; then
+ export LD_LIBRARY_PATH="$cap_appdir/usr/lib/cap/libva\${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+fi
+unset cap_original_directory cap_directory cap_host_libva
+cd "$cap_appdir/usr" || exit 1
+exec "$cap_appdir/usr/bin/Cap" "$@"
+`;
+
+const libvaPattern = /^libva(?:-drm)?\.so\.2(?:\..+)?$/;
+
+// The fallback libva, for hosts without one. It must be new enough for the
+// host's own FFmpeg, which host PipeWire plugins load into Cap: NixOS's
+// FFmpeg 9 needs vaMapBuffer2, which the build image's libva 2.20 lacks. A
+// newer libva keeps the ABI the bundled FFmpeg was built against. These need
+// glibc 2.38, older than the build image's.
+export const fallbackLibvaPackages = [
+ {
+ url: "http://archive.ubuntu.com/ubuntu/pool/main/libv/libva/libva2_2.23.0-1ubuntu1_amd64.deb",
+ sha256: "83053abb11b8b0169bfb2abbd209a44ddeb4e56223980cdcfc0218b09e845f00",
+ },
+ {
+ url: "http://archive.ubuntu.com/ubuntu/pool/main/libv/libva/libva-drm2_2.23.0-1ubuntu1_amd64.deb",
+ sha256: "f56b0cb850dcd8af45929b2da7cc1fbdf64245fbcec99033298499a8db2765a3",
+ },
+];
+
+/**
+ * The directory holding the fallback libva: `CAP_APPIMAGE_LIBVA_DIR`, or the
+ * pinned packages above, downloaded, checked and unpacked into `work`.
+ */
+export async function fallbackLibvaDirectory(
+ work,
+ { env = process.env, run = runCommand, download = fetch } = {},
+) {
+ if (env.CAP_APPIMAGE_LIBVA_DIR) return env.CAP_APPIMAGE_LIBVA_DIR;
+ const root = path.join(work, "libva");
+ await mkdir(root, { recursive: true });
+ for (const { url, sha256 } of fallbackLibvaPackages) {
+ const response = await download(url);
+ if (!response.ok)
+ throw new Error(`Downloading ${url} failed: ${response.status}`);
+ const bytes = Buffer.from(await response.arrayBuffer());
+ const digest = createHash("sha256").update(bytes).digest("hex");
+ if (digest !== sha256) {
+ throw new Error(`${url} has sha256 ${digest}, expected ${sha256}`);
+ }
+ const deb = path.join(root, path.basename(url));
+ await writeFile(deb, bytes);
+ await run("dpkg-deb", ["-x", deb, root], { env, stdio: "inherit" });
+ }
+ return path.join(root, "usr/lib/x86_64-linux-gnu");
+}
+
+/**
+ * Moves the bundled libva out of RUNPATH's reach into the launcher's fallback
+ * directory, replaced by the libraries in `replacement` when given.
+ */
+export async function isolateBundledLibva(appDir, { replacement } = {}) {
+ const libraries = path.join(appDir, "usr/lib");
+ const fallback = path.join(libraries, "cap/libva");
+ const moved = [];
+ for (const entry of await readdir(libraries, { withFileTypes: true })) {
+ if (!libvaPattern.test(entry.name) || entry.isDirectory()) continue;
+ await mkdir(fallback, { recursive: true });
+ await rename(
+ path.join(libraries, entry.name),
+ path.join(fallback, entry.name),
+ );
+ moved.push(entry.name);
+ }
+ if (replacement) {
+ const libraries = (await readdir(replacement)).filter((name) =>
+ libvaPattern.test(name),
+ );
+ for (const name of ["libva.so.2", "libva-drm.so.2"]) {
+ if (!libraries.includes(name)) {
+ throw new Error(`${replacement} has no ${name}`);
+ }
+ }
+ await rm(fallback, { recursive: true, force: true });
+ await mkdir(fallback, { recursive: true });
+ for (const name of libraries) {
+ const source = path.join(replacement, name);
+ const entry = await lstat(source);
+ if (entry.isSymbolicLink()) {
+ await symlink(await readlink(source), path.join(fallback, name));
+ } else {
+ await copyFile(source, path.join(fallback, name));
+ }
+ }
+ }
+ return moved.sort();
+}
+
+export async function preserveAppImageWorkingDirectory(
+ appDir,
+ { direct = false } = {},
+) {
+ const wrapper = direct ? directAppRunWrapper : appRunWrapper;
const launcher = path.join(appDir, "AppRun");
const original = path.join(appDir, "AppRun.cap-original");
if ((await readdir(appDir)).includes(path.basename(original))) {
- if ((await readFile(launcher, "utf8")) !== appRunWrapper) {
+ if ((await readFile(launcher, "utf8")) !== wrapper) {
throw new Error("AppImage already contains a different AppRun wrapper");
}
await access(original, constants.X_OK);
@@ -116,7 +248,7 @@ export async function preserveAppImageWorkingDirectory(appDir) {
}
await access(launcher, constants.X_OK);
await rename(launcher, original);
- await writeFile(launcher, appRunWrapper, { mode: 0o755, flag: "wx" });
+ await writeFile(launcher, wrapper, { mode: 0o755, flag: "wx" });
}
// Desktop launchers (GNOME's app grid among them) only hand an app an
@@ -523,13 +655,16 @@ export async function finalizeLinuxAppImage(
);
}
if (!webview) {
+ await isolateBundledLibva(appDir, {
+ replacement: await fallbackLibvaDirectory(work, { env: tools, run }),
+ });
await notifyStartupInDesktopEntries(appDir, [
"Cap.desktop",
"usr/share/applications/Cap.desktop",
]);
}
await relativizeDirIcon(appDir);
- await preserveAppImageWorkingDirectory(appDir);
+ await preserveAppImageWorkingDirectory(appDir, { direct: !webview });
const output = path.join(work, path.basename(image));
await run(
outputPlugin,
diff --git a/scripts/finalize-linux-appimage.test.mjs b/scripts/finalize-linux-appimage.test.mjs
index a9f92995d..38f0287f9 100644
--- a/scripts/finalize-linux-appimage.test.mjs
+++ b/scripts/finalize-linux-appimage.test.mjs
@@ -20,8 +20,12 @@ import { tmpdir } from "node:os";
import path from "node:path";
import { test } from "node:test";
import {
+ directAppRunWrapper,
+ fallbackLibvaDirectory,
+ fallbackLibvaPackages,
finalizeLinuxAppImage,
findConflictingLibraries,
+ isolateBundledLibva,
preserveAppImageWorkingDirectory,
relativizeDirIcon,
removeWebviewRuntime,
@@ -852,14 +856,33 @@ test("webview-free finalization hands the pruned AppDir to the output plugin", a
stat(path.join(appDir, "usr/lib/libwebkit2gtk-4.1.so.0")),
);
await stat(path.join(appDir, "usr/lib/libssl.so.3"));
+ assert.equal(
+ await readFile(path.join(appDir, "AppRun"), "utf8"),
+ directAppRunWrapper,
+ );
+ await assert.rejects(stat(path.join(appDir, "usr/lib/libva.so.2")));
+ assert.equal(
+ await readFile(path.join(appDir, "usr/lib/cap/libva/libva.so.2"), "utf8"),
+ "libva 2.23",
+ );
await writeFile(options.env.OUTPUT, finalImage());
return {};
};
+ const newLibva = path.join(path.dirname(image), "libva-2.23");
+ await mkdir(newLibva);
+ await writeFile(path.join(newLibva, "libva.so.2.2300.0"), "libva 2.23");
+ await writeFile(
+ path.join(newLibva, "libva-drm.so.2.2300.0"),
+ "libva-drm 2.23",
+ );
+ await symlink("libva.so.2.2300.0", path.join(newLibva, "libva.so.2"));
+ await symlink("libva-drm.so.2.2300.0", path.join(newLibva, "libva-drm.so.2"));
const excluded = await finalizeLinuxAppImage(image, {
outputPlugin: plugin,
unsigned: true,
webview: false,
env: {
+ CAP_APPIMAGE_LIBVA_DIR: newLibva,
PATH: "/usr/bin",
TAURI_SIGNING_PRIVATE_KEY: "key",
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: "password",
@@ -872,3 +895,167 @@ test("webview-free finalization hands the pruned AppDir to the output plugin", a
assert.ok(excluded.includes("usr/share/glib-2.0"));
assert.deepEqual(await readFile(image), finalImage());
});
+
+async function directAppDir(root) {
+ const appDir = path.join(root, "Cap's AppDir");
+ await mkdir(path.join(appDir, "usr/bin"), { recursive: true });
+ await writeFile(
+ path.join(appDir, "usr/bin/Cap"),
+ `#!/bin/sh
+printf '%s\\0' "$PWD" "\${OWD-unset}" "\${LD_LIBRARY_PATH-unset}" "\${PATH%%:*}" "\${XDG_DATA_DIRS%%:*}" "\${PYTHONHOME-unset}" "$@"
+exit 23
+`,
+ { mode: 0o755 },
+ );
+ await writeFile(path.join(appDir, "AppRun"), "generic launcher", {
+ mode: 0o755,
+ });
+ await preserveAppImageWorkingDirectory(appDir, { direct: true });
+ return appDir;
+}
+
+test("the GPUI AppRun starts Cap directly without the generic launcher's library path", async (t) => {
+ const { root } = await fixture(t);
+ const appDir = await directAppDir(root);
+ assert.equal(
+ await readFile(path.join(appDir, "AppRun"), "utf8"),
+ directAppRunWrapper,
+ );
+ await preserveAppImageWorkingDirectory(appDir, { direct: true });
+ const caller = path.join(root, "caller's directory");
+ await mkdir(caller);
+ const args = ["--cap-cli", "two words", "$(literal)"];
+ const env = { ...process.env, PATH: "/usr/bin:/bin" };
+ delete env.LD_LIBRARY_PATH;
+ delete env.PYTHONHOME;
+ const result = spawnSync(path.join(appDir, "AppRun"), args, {
+ cwd: caller,
+ env,
+ });
+ assert.equal(result.status, 23, result.stderr.toString());
+ const [cwd, owd, libraries, firstPath, firstData, pythonHome, ...rest] =
+ result.stdout.toString().split("\0");
+ const real = await realpath(appDir);
+ assert.equal(cwd, path.join(real, "usr"));
+ assert.equal(owd, await realpath(caller));
+ assert.equal(firstPath, path.join(real, "usr/bin"));
+ assert.equal(firstData, path.join(real, "usr/share"));
+ assert.equal(pythonHome, "unset");
+ assert.deepEqual(rest, [...args, ""]);
+ // This machine either has libva, so nothing is added, or it does not, so
+ // only the bundled libva directory is.
+ assert.ok(
+ libraries === "unset" || libraries === path.join(real, "usr/lib/cap/libva"),
+ libraries,
+ );
+});
+
+test("the bundled libva moves out of RUNPATH's reach, symlinks and all", async (t) => {
+ const { root } = await fixture(t);
+ const libraries = path.join(root, "usr/lib");
+ await mkdir(libraries, { recursive: true });
+ for (const name of [
+ "libva.so.2.2100.0",
+ "libva-drm.so.2.2100.0",
+ "libvarlink.so.0",
+ "libavutil.so.59",
+ ]) {
+ await writeFile(path.join(libraries, name), name);
+ }
+ await symlink("libva.so.2.2100.0", path.join(libraries, "libva.so.2"));
+ await symlink(
+ "libva-drm.so.2.2100.0",
+ path.join(libraries, "libva-drm.so.2"),
+ );
+ assert.deepEqual(await isolateBundledLibva(root), [
+ "libva-drm.so.2",
+ "libva-drm.so.2.2100.0",
+ "libva.so.2",
+ "libva.so.2.2100.0",
+ ]);
+ assert.deepEqual((await readdir(libraries)).sort(), [
+ "cap",
+ "libavutil.so.59",
+ "libvarlink.so.0",
+ ]);
+ assert.equal(
+ await readFile(path.join(libraries, "cap/libva/libva.so.2"), "utf8"),
+ "libva.so.2.2100.0",
+ );
+ assert.deepEqual(await isolateBundledLibva(root), []);
+});
+
+test("the bundled libva is replaced by the fallback directory's", async (t) => {
+ const { root } = await fixture(t);
+ const libraries = path.join(root, "usr/lib");
+ const replacement = path.join(root, "new");
+ await mkdir(libraries, { recursive: true });
+ await mkdir(replacement);
+ await writeFile(path.join(libraries, "libva.so.2"), "old");
+ await writeFile(path.join(libraries, "libva-drm.so.2"), "old");
+ await writeFile(path.join(replacement, "libva.so.2.2300.0"), "new");
+ await writeFile(path.join(replacement, "libva-drm.so.2.2300.0"), "new drm");
+ await writeFile(path.join(replacement, "README"), "not a library");
+ await symlink("libva.so.2.2300.0", path.join(replacement, "libva.so.2"));
+ await symlink(
+ "libva-drm.so.2.2300.0",
+ path.join(replacement, "libva-drm.so.2"),
+ );
+ await isolateBundledLibva(root, { replacement });
+ const fallback = path.join(libraries, "cap/libva");
+ assert.deepEqual((await readdir(fallback)).sort(), [
+ "libva-drm.so.2",
+ "libva-drm.so.2.2300.0",
+ "libva.so.2",
+ "libva.so.2.2300.0",
+ ]);
+ assert.equal(
+ await readlink(path.join(fallback, "libva.so.2")),
+ "libva.so.2.2300.0",
+ );
+ assert.equal(
+ await readFile(path.join(fallback, "libva.so.2"), "utf8"),
+ "new",
+ );
+
+ const incomplete = path.join(root, "incomplete");
+ await mkdir(incomplete);
+ await writeFile(path.join(incomplete, "libva.so.2"), "new");
+ await assert.rejects(
+ isolateBundledLibva(root, { replacement: incomplete }),
+ /has no libva-drm\.so\.2/,
+ );
+});
+
+test("the pinned fallback libva is checked before it is unpacked", async (t) => {
+ const { root } = await fixture(t);
+ const unpacked = [];
+ const run = async (command, args) => {
+ assert.equal(command, "dpkg-deb");
+ unpacked.push(path.basename(args[1]));
+ };
+ const bytes = new Map(
+ fallbackLibvaPackages.map(({ url }) => [url, Buffer.from(url)]),
+ );
+ const download = async (url) => ({
+ ok: true,
+ status: 200,
+ arrayBuffer: async () => bytes.get(url),
+ });
+ await assert.rejects(
+ fallbackLibvaDirectory(root, { env: {}, run, download }),
+ /has sha256 [0-9a-f]{64}, expected/,
+ );
+ assert.deepEqual(unpacked, []);
+ assert.equal(
+ await fallbackLibvaDirectory(root, {
+ env: { CAP_APPIMAGE_LIBVA_DIR: "/given" },
+ run,
+ download,
+ }),
+ "/given",
+ );
+ for (const { url } of fallbackLibvaPackages) {
+ assert.match(url, /libva(-drm)?2_2\.23\.0-1ubuntu1_amd64\.deb$/);
+ }
+});
diff --git a/scripts/linux-desktop-harness/README.md b/scripts/linux-desktop-harness/README.md
index 57d63f11b..3be777a53 100644
--- a/scripts/linux-desktop-harness/README.md
+++ b/scripts/linux-desktop-harness/README.md
@@ -9,7 +9,7 @@ runs it on every pull request that touches the desktop app.
| --- | --- | --- |
| `gnome` | Ubuntu 26.04, GNOME 50 headless, AppIndicator, xdg-desktop-portal 1.21+ | Ubuntu users: shortcut approval dialog, `ShortcutsChanged`, Share Screen dialog |
| `hyprland` | Arch, Hyprland on vkms, waybar tray, xdg-desktop-portal-hyprland | Omarchy users: the portal refuses host apps ("An app id is required"), tray Stop control, no configure after a remap |
-| `sway`, `niri` | Arch on vkms | Exploration only; their portals need a render node vkms does not have |
+| `sway`, `niri` | Arch on vkms | Exploration only: their portals need a render node vkms does not have, and niri refuses software rendering |
Each run (`run-desktop.sh <desktop> <package> <out-dir> [KEY=VALUE...]`):
@@ -27,6 +27,9 @@ Each run (`run-desktop.sh <desktop> <package> <out-dir> [KEY=VALUE...]`):
`mutter-pointer.py`);
4. writes `result.txt`, which `check-results.sh` turns into pass or fail.
+`SCENARIO=clipboard` (niri, Hyprland) opens Cap's teleprompter, pastes text
+another app copied with Ctrl+V and copies it back out with Ctrl+A, Ctrl+C,
+pressed through the compositor's virtual keyboard (`wtype`).
`GNOME_TRAY=0` runs GNOME in its plain session without the AppIndicator
extension and without the harness's own tray host, as Fedora ships it. With
`CAP_GPUI_PORTAL_APP_ID=0` and `LAUNCH=terminal` the portal grants no shortcut
@@ -51,3 +54,18 @@ Harness-only workarounds (not in the app): Mesa's device_select Vulkan layer is
disabled on the vkms images because it crashes on their virtual devices, and
the GNOME session gets a target bound to `graphical-session.target`, which a
real login activates.
+
+## Checks without a desktop
+
+- `niri-screencast/run.sh` records through Cap's PipeWire capture loop from a
+ producer that negotiates and shares frames the way niri 26.04 does: a
+ mandatory DMA-BUF modifier, DMA-BUF buffers the consumer must map itself,
+ and a chunk size of 1 (GitHub #2941). It needs a host with PipeWire and
+ WirePlumber, and runs in the GNOME job.
+- `appimage-pipewire/run.sh <Cap.AppImage> <out-dir>` connects a screen
+ capture stream inside NixOS's `appimage-run`, through the AppImage's real
+ launchers, with the AppDir as the given AppImage shipped it and as
+ `scripts/finalize-linux-appimage.mjs` lays it out now. A stand-in for Cap
+ links the bundled FFmpeg the way Cap does, so the bundled libva loads ahead
+ of the host's PipeWire plugins, as in Cap.
+
diff --git a/scripts/linux-desktop-harness/appimage-pipewire/probe.c b/scripts/linux-desktop-harness/appimage-pipewire/probe.c
new file mode 100644
index 000000000..6b10b907a
--- /dev/null
+++ b/scripts/linux-desktop-harness/appimage-pipewire/probe.c
@@ -0,0 +1,61 @@
+// Stands in for Cap inside an AppImage: it links the bundled FFmpeg the way
+// Cap does (so the bundled libva loads with it, or the host's), then connects a
+// PipeWire screen-capture stream with Cap's format offer. pw_stream_connect
+// loads the host's video converter plugin, which links the host's FFmpeg.
+#define _GNU_SOURCE
+#include <errno.h>
+#include <link.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <pipewire/pipewire.h>
+#include <spa/param/video/format-utils.h>
+
+static int print_library(struct dl_phdr_info *info, size_t size, void *data) {
+ (void)size;
+ (void)data;
+ if (strstr(info->dlpi_name, "libva") || strstr(info->dlpi_name, "libavutil"))
+ printf("loaded %s\n", info->dlpi_name);
+ return 0;
+}
+
+// From the bundled libavutil, declared here so no FFmpeg headers are needed.
+const char *av_version_info(void);
+
+int main(int argc, char **argv) {
+ printf("bundled libavutil %s\n", av_version_info());
+ pw_init(&argc, &argv);
+ struct pw_thread_loop *loop = pw_thread_loop_new("probe", NULL);
+ struct pw_context *context = pw_context_new(pw_thread_loop_get_loop(loop), NULL, 0);
+ if (!context) {
+ printf("context failed: %s\n", strerror(errno));
+ return 1;
+ }
+ struct pw_core *core = pw_context_connect(context, NULL, 0);
+ if (!core) {
+ printf("core connect failed: %s\n", strerror(errno));
+ return 1;
+ }
+ struct pw_stream *stream = pw_stream_new(core, "cap-wayland-screen",
+ pw_properties_new(PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture",
+ PW_KEY_MEDIA_ROLE, "Screen", NULL));
+ uint8_t buffer[1024];
+ struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
+ const struct spa_pod *params[1];
+ params[0] = spa_pod_builder_add_object(&builder, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
+ SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
+ SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(7, SPA_VIDEO_FORMAT_BGRx, SPA_VIDEO_FORMAT_BGRx,
+ SPA_VIDEO_FORMAT_BGRA, SPA_VIDEO_FORMAT_RGBx, SPA_VIDEO_FORMAT_RGBA, SPA_VIDEO_FORMAT_RGB,
+ SPA_VIDEO_FORMAT_BGR),
+ SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(&SPA_RECTANGLE(1920, 1080),
+ &SPA_RECTANGLE(1, 1), &SPA_RECTANGLE(8192, 8192)),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(&SPA_FRACTION(30, 1),
+ &SPA_FRACTION(0, 1), &SPA_FRACTION(1000, 1)));
+ int result = pw_stream_connect(stream, PW_DIRECTION_INPUT, PW_ID_ANY,
+ PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, 1);
+ printf("LD_LIBRARY_PATH=%s\n", getenv("LD_LIBRARY_PATH") ? getenv("LD_LIBRARY_PATH") : "(unset)");
+ dl_iterate_phdr(print_library, NULL);
+ printf("pw_stream_connect=%d (%s)\n", result, result < 0 ? strerror(-result) : "ok");
+ return result < 0 ? 2 : 0;
+}
diff --git a/scripts/linux-desktop-harness/appimage-pipewire/run.sh b/scripts/linux-desktop-harness/appimage-pipewire/run.sh
new file mode 100755
index 000000000..ce37cee21
--- /dev/null
+++ b/scripts/linux-desktop-harness/appimage-pipewire/run.sh
@@ -0,0 +1,97 @@
+#!/usr/bin/env bash
+# The AppImage's libraries against a NixOS PipeWire (GitHub #2941): a stand-in
+# for Cap that links the bundled FFmpeg connects a screen-capture stream
+# inside NixOS's appimage-run, once with the AppDir as shipped and once laid
+# out by scripts/finalize-linux-appimage.mjs. Prints key=value results.
+# Usage: run.sh <Cap.AppImage> <out-dir>
+set -euo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+repo=$(cd "$here/../../.." && pwd)
+appimage=$(realpath "$1")
+out=$(realpath -m "$2")
+mkdir -p "$out"
+work=$(mktemp -d)
+trap 'chmod -R u+w "$work" 2>/dev/null; find "$work" -mindepth 1 -delete 2>/dev/null' EXIT
+cd "$work"
+cp "$appimage" Cap.AppImage
+chmod +x Cap.AppImage
+./Cap.AppImage --appimage-extract >/dev/null
+chmod -R a+rX squashfs-root
+
+# Built like Cap: a host-ABI binary (Ubuntu's toolchain) that finds the
+# bundled FFmpeg through RUNPATH $ORIGIN/../lib.
+docker run --rm -v "$here":/src:ro -v "$work":/work -w /work ubuntu:24.04 bash -euc '
+ apt-get update -qq >/dev/null && apt-get install -y -qq gcc pkg-config libpipewire-0.3-dev >/dev/null
+ gcc -o probe /src/probe.c $(pkg-config --cflags --libs libpipewire-0.3) \
+ -L squashfs-root/usr/lib -l:libavutil.so.59 -Wl,--allow-shlib-undefined -Wl,-rpath,"\$ORIGIN/../lib"
+ chmod 755 probe'
+
+cp -a squashfs-root shipped
+cp -a squashfs-root fixed
+# The fixed layout is what scripts/finalize-linux-appimage.mjs ships: the
+# direct launcher and the pinned fallback libva (CAP_APPIMAGE_LIBVA_DIR to
+# use another).
+mkdir -p libva-work
+FINALIZE="$repo/scripts/finalize-linux-appimage.mjs" APPDIR_FIXED="$work/fixed" LIBVA_WORK="$work/libva-work" \
+ node --input-type=module -e '
+ const { isolateBundledLibva, directAppRunWrapper, fallbackLibvaDirectory } = await import(process.env.FINALIZE);
+ const { writeFile } = await import("node:fs/promises");
+ const replacement = await fallbackLibvaDirectory(process.env.LIBVA_WORK);
+ const moved = await isolateBundledLibva(process.env.APPDIR_FIXED, { replacement });
+ await writeFile(process.env.APPDIR_FIXED + "/AppRun", directAppRunWrapper, { mode: 0o755 });
+ console.log("moved " + moved.join(" ") + "; fallback from " + replacement);
+'
+for layout in shipped fixed; do
+ install -m 755 probe "$layout/usr/bin/Cap"
+done
+chmod -R a+rX "$work"
+# appimage-run takes AppImage files: the original runtime and each layout
+# packed the way the original was.
+offset=$(./Cap.AppImage --appimage-offset)
+head -c "$offset" Cap.AppImage >runtime
+docker run --rm -v "$work":/work -w /work ubuntu:24.04 bash -euc '
+ apt-get update -qq >/dev/null && apt-get install -y -qq squashfs-tools >/dev/null
+ for layout in shipped fixed; do
+ mksquashfs "$layout" "$layout.squashfs" -root-owned -noappend -comp zstd -quiet >/dev/null
+ cat runtime "$layout.squashfs" >"$layout.AppImage"
+ chmod 755 "$layout.AppImage"
+ done'
+
+docker run --rm --privileged -e PIPEWIRE_DEBUG -e SHOW_LINES -v "$work":/work:ro nixos/nix:latest bash -c '
+ export NIX_CONFIG="experimental-features = nix-command flakes
+sandbox = false
+filter-syscalls = false"
+ export XDG_RUNTIME_DIR=/tmp/xdg
+ mkdir -p $XDG_RUNTIME_DIR && chmod 700 $XDG_RUNTIME_DIR
+ nix build -o /tmp/pipewire nixpkgs#pipewire nixpkgs#appimage-run >/dev/null 2>&1
+ /tmp/pipewire/bin/pipewire >/tmp/pipewire.log 2>&1 &
+ sleep 4
+ run=$(ls -d /nix/store/*-appimage-run*/bin/appimage-run | head -1)
+ echo "versions $(/tmp/pipewire/bin/pipewire --version | tail -1)"
+ for layout in shipped fixed; do
+ echo "== $layout"
+ PIPEWIRE_DEBUG=${PIPEWIRE_DEBUG:-1} "$run" /work/$layout.AppImage >/tmp/$layout.log 2>&1
+ status=$?
+ grep -v "rtkit\|=====\|Uncompress\|is now installed" /tmp/$layout.log | tail -${SHOW_LINES:-40}
+ echo "exit $layout=$status"
+ done
+' 2>&1 | tee "$out/appimage-pipewire.log"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+: >"$out/result.txt"
+for layout in shipped fixed; do
+ if grep -q "^exit $layout=0" "$out/appimage-pipewire.log"; then
+ result "${layout}_connects" yes
+ else
+ result "${layout}_connects" no
+ fi
+ # What a program Cap starts (a file manager through xdg-open) inherits:
+ # any bundle library directory other than the libva fallback leaks Cap's
+ # libraries into it, which is why folders did not open on NixOS in 0.6.1.
+ libraries=$(sed -n "/^== $layout\$/,/^exit $layout=/p" "$out/appimage-pipewire.log" | sed -n 's/^LD_LIBRARY_PATH=//p')
+ if tr ':' '\n' <<<"$libraries" | grep '/appimage-run/' | grep -qv '/usr/lib/cap/libva$'; then
+ result "${layout}_children_get_bundle_libraries" yes
+ else
+ result "${layout}_children_get_bundle_libraries" no
+ fi
+done
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/arch-up.sh b/scripts/linux-desktop-harness/arch-up.sh
index 796ba750b..6a1a80288 100755
--- a/scripts/linux-desktop-harness/arch-up.sh
+++ b/scripts/linux-desktop-harness/arch-up.sh
@@ -40,16 +40,32 @@ CONF
env "${common[@]}" AQ_DRM_DEVICES="$kms" XDG_CURRENT_DESKTOP=Hyprland Hyprland
;;
niri)
- # vkms has no render node of its own; niri needs one named.
- render=$(ls /dev/dri/renderD* 2>/dev/null | head -1)
+ # vkms has no render node of its own; niri needs one named. On a host
+ # with no GPU, NIRI_RENDER_DEVICE=vkms renders on the vkms card itself,
+ # for a niri built to accept software rendering (niri-software.Dockerfile).
+ render=${NIRI_RENDER_DEVICE:-$(ls /dev/dri/renderD* 2>/dev/null | head -1)}
+ [[ $render == vkms ]] && render=$kms
cat >"$HOME/.config/niri/config.kdl" <<KDL
spawn-at-startup "sh" "-c" "$export_session; waybar"
debug {
render-drm-device "$render"
}
KDL
- systemd-run --user --unit=desktop --collect -p StandardOutput=file:"$out/compositor.log" -p StandardError=inherit \
- env "${common[@]}" XDG_CURRENT_DESKTOP=niri niri
+ # NIRI_OUTPUTS_OFF: outputs to switch off (a cloud GPU's EFI framebuffer),
+ # so the screen-share dialog has the one screen to pick.
+ for output in ${NIRI_OUTPUTS_OFF:-}; do
+ printf 'output "%s" {\n off\n}\n' "$output" >>"$HOME/.config/niri/config.kdl"
+ done
+ # The packaged session unit, as niri-session starts it: it brings up
+ # graphical-session.target, which the GNOME portal backend (niri's
+ # ScreenCast portal) requires.
+ mkdir -p "$HOME/.config/systemd/user/niri.service.d"
+ printf '[Service]\nStandardOutput=file:%s\nStandardError=inherit\nEnvironment=LIBSEAT_BACKEND=seatd XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=niri RUST_LOG=%s\n' \
+ "$out/compositor.log" "${NIRI_LOG:-niri=debug}" >"$HOME/.config/systemd/user/niri.service.d/harness.conf"
+ # GSK_RENDERER: the portal dialogs' GTK renderer (cairo on software GL).
+ [[ -n ${GSK_RENDERER:-} ]] && systemctl --user set-environment GSK_RENDERER="$GSK_RENDERER"
+ systemctl --user daemon-reload
+ systemctl --user start niri.service
;;
sway)
# xdg-desktop-portal-wlr: share the one output, as a person picks it.
diff --git a/scripts/linux-desktop-harness/arch.Dockerfile b/scripts/linux-desktop-harness/arch.Dockerfile
index 69fc07578..5d3df7c23 100644
--- a/scripts/linux-desktop-harness/arch.Dockerfile
+++ b/scripts/linux-desktop-harness/arch.Dockerfile
@@ -8,7 +8,7 @@ RUN pacman -Syu --noconfirm --needed \
systemd dbus seatd sway hyprland niri waybar \
xdg-desktop-portal xdg-desktop-portal-hyprland xdg-desktop-portal-gnome xdg-desktop-portal-wlr \
pipewire wireplumber pipewire-pulse mesa vulkan-swrast vulkan-icd-loader \
- python python-gobject glib2 procps-ng psmisc fuse2 ttf-dejavu \
+ python python-gobject glib2 procps-ng psmisc fuse2 ttf-dejavu wtype wl-clipboard \
&& pacman -Scc --noconfirm
RUN useradd -m -u 1001 -G seat,video,render tester \
&& mkdir -p /var/lib/systemd/linger && touch /var/lib/systemd/linger/tester \
diff --git a/scripts/linux-desktop-harness/gnome-up.sh b/scripts/linux-desktop-harness/gnome-up.sh
index b74190b8e..0fb548484 100644
--- a/scripts/linux-desktop-harness/gnome-up.sh
+++ b/scripts/linux-desktop-harness/gnome-up.sh
@@ -8,8 +8,12 @@ export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
out=${HARNESS_OUT:-/out}
mkdir -p "$out"
+# GNOME_GPU=1 leaves rendering to the host's GPU (passed in with
+# DOCKER_EXTRA); otherwise everything renders in software, as on CI.
+software=(LIBGL_ALWAYS_SOFTWARE=1)
+[[ ${GNOME_GPU:-} == 1 ]] && software=()
systemctl --user set-environment XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=ubuntu:GNOME \
- XDG_SESSION_DESKTOP=ubuntu GNOME_SHELL_SESSION_MODE=ubuntu LIBGL_ALWAYS_SOFTWARE=1
+ XDG_SESSION_DESKTOP=ubuntu GNOME_SHELL_SESSION_MODE=ubuntu "${software[@]}"
# A logged-in GNOME session has graphical-session.target active, and the GNOME
# portal backend will not start without it. It refuses a manual start, so bind
# a harness target to it the way gnome-session's own targets do.
@@ -53,7 +57,7 @@ for _ in $(seq 300); do
--method org.freedesktop.DBus.Properties.Get org.gnome.Shell ShellVersion >/dev/null 2>&1 && break
sleep 0.1
done
-export WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=ubuntu:GNOME XDG_SESSION_DESKTOP=ubuntu GSK_RENDERER=cairo LIBGL_ALWAYS_SOFTWARE=1
+export WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=ubuntu:GNOME XDG_SESSION_DESKTOP=ubuntu GSK_RENDERER=cairo "${software[@]}"
systemctl --user set-environment WAYLAND_DISPLAY=wayland-0
-dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP GSK_RENDERER LIBGL_ALWAYS_SOFTWARE
+dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_SESSION_TYPE XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP GSK_RENDERER "${software[@]%%=*}"
echo "gnome-up: $(gnome-shell --version) on wayland-0"
diff --git a/scripts/linux-desktop-harness/niri-screencast/producer.c b/scripts/linux-desktop-harness/niri-screencast/producer.c
new file mode 100644
index 000000000..be45042ca
--- /dev/null
+++ b/scripts/linux-desktop-harness/niri-screencast/producer.c
@@ -0,0 +1,201 @@
+// A screencast producer that shares frames the way niri 26.04 does
+// (src/screencasting/pw_utils.rs): BGRx with a MANDATORY modifier offered
+// from a list (DONT_FIXATE), fixated to one modifier in a second round,
+// DMA-BUF buffers only, flagged READWRITE but not MAPPABLE, maxsize 1, and a
+// chunk size of 1. The buffers are memfds, which a consumer maps like a
+// LINEAR DMA-BUF. Each frame is a solid colour that steps every frame, so a
+// consumer can check what it read.
+//
+// Prints "node <id>" once the stream is ready, then runs until killed.
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <pipewire/pipewire.h>
+#include <spa/param/video/format-utils.h>
+#include <spa/param/buffers.h>
+#include <spa/pod/builder.h>
+#include <spa/pod/iter.h>
+
+#define WIDTH 640
+#define HEIGHT 360
+#define STRIDE (WIDTH * 4)
+#define LINEAR 0LL
+#define X_TILED 0x0100000000000001LL
+
+struct data {
+ struct pw_main_loop *loop;
+ struct pw_stream *stream;
+ struct spa_hook listener;
+ struct spa_source *timer;
+ int fixated;
+ uint32_t frame;
+ int announced;
+};
+
+static const struct spa_pod *video_format(struct spa_pod_builder *b, const int64_t *modifiers,
+ uint32_t count, int dont_fixate) {
+ struct spa_pod_frame object, choice;
+ spa_pod_builder_push_object(b, &object, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
+ spa_pod_builder_add(b, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
+ SPA_FORMAT_VIDEO_format, SPA_POD_Id(SPA_VIDEO_FORMAT_BGRx), 0);
+ spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier,
+ SPA_POD_PROP_FLAG_MANDATORY | (dont_fixate ? SPA_POD_PROP_FLAG_DONT_FIXATE : 0));
+ spa_pod_builder_push_choice(b, &choice, SPA_CHOICE_Enum, 0);
+ spa_pod_builder_long(b, modifiers[0]);
+ for (uint32_t i = 0; i < count; i++)
+ spa_pod_builder_long(b, modifiers[i]);
+ spa_pod_builder_pop(b, &choice);
+ spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&SPA_RECTANGLE(WIDTH, HEIGHT)),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&SPA_FRACTION(0, 1)),
+ SPA_FORMAT_VIDEO_maxFramerate, SPA_POD_CHOICE_RANGE_Fraction(&SPA_FRACTION(60, 1),
+ &SPA_FRACTION(1, 1), &SPA_FRACTION(60, 1)), 0);
+ return spa_pod_builder_pop(b, &object);
+}
+
+static void on_param_changed(void *userdata, uint32_t id, const struct spa_pod *param) {
+ struct data *data = userdata;
+ if (param == NULL || id != SPA_PARAM_Format)
+ return;
+ const struct spa_pod_prop *modifier = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier);
+ if (modifier == NULL) {
+ fprintf(stderr, "modifier prop missing\n");
+ pw_main_loop_quit(data->loop);
+ return;
+ }
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
+ if (modifier->flags & SPA_POD_PROP_FLAG_DONT_FIXATE) {
+ uint32_t count, choice;
+ const struct spa_pod *values = spa_pod_get_values(&modifier->value, &count, &choice);
+ const int64_t *modifiers = SPA_POD_BODY_CONST(values);
+ int linear = 0;
+ for (uint32_t i = 0; i < count; i++)
+ linear |= modifiers[i] == LINEAR;
+ if (!linear) {
+ fprintf(stderr, "consumer offers no LINEAR modifier\n");
+ pw_main_loop_quit(data->loop);
+ return;
+ }
+ const int64_t fixated[] = {LINEAR};
+ const struct spa_pod *params[1] = {video_format(&b, fixated, 1, 0)};
+ fprintf(stderr, "fixating LINEAR\n");
+ pw_stream_update_params(data->stream, params, 1);
+ return;
+ }
+ const struct spa_pod *params[1];
+ params[0] = spa_pod_builder_add_object(&b, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
+ SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(8, 2, 16),
+ SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
+ SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int(1 << SPA_DATA_DmaBuf));
+ pw_stream_update_params(data->stream, params, 1);
+ data->fixated = 1;
+ fprintf(stderr, "format fixated\n");
+}
+
+static void on_add_buffer(void *userdata, struct pw_buffer *buffer) {
+ (void)userdata;
+ struct spa_data *d = &buffer->buffer->datas[0];
+ if (!(d->type & (1 << SPA_DATA_DmaBuf))) {
+ fprintf(stderr, "consumer refused DMA-BUF buffers\n");
+ return;
+ }
+ int fd = memfd_create("fake-niri-dmabuf", MFD_CLOEXEC);
+ if (fd < 0 || ftruncate(fd, STRIDE * HEIGHT) < 0) {
+ perror("memfd");
+ return;
+ }
+ d->type = SPA_DATA_DmaBuf;
+ d->flags = SPA_DATA_FLAG_READWRITE;
+ d->fd = fd;
+ d->mapoffset = 0;
+ d->maxsize = 1;
+ d->data = NULL;
+ d->chunk->offset = 0;
+ d->chunk->stride = STRIDE;
+}
+
+static void on_remove_buffer(void *userdata, struct pw_buffer *buffer) {
+ (void)userdata;
+ close(buffer->buffer->datas[0].fd);
+}
+
+static void on_state_changed(void *userdata, enum pw_stream_state old, enum pw_stream_state state,
+ const char *error) {
+ struct data *data = userdata;
+ (void)old;
+ fprintf(stderr, "state %s %s\n", pw_stream_state_as_string(state), error ? error : "");
+ if (!data->announced && state == PW_STREAM_STATE_PAUSED) {
+ printf("node %u\n", pw_stream_get_node_id(data->stream));
+ fflush(stdout);
+ data->announced = 1;
+ }
+}
+
+static void on_timeout(void *userdata, uint64_t expirations) {
+ struct data *data = userdata;
+ (void)expirations;
+ if (data->fixated)
+ pw_stream_trigger_process(data->stream);
+}
+
+static void on_process(void *userdata) {
+ struct data *data = userdata;
+ struct pw_buffer *buffer = pw_stream_dequeue_buffer(data->stream);
+ if (buffer == NULL)
+ return;
+ struct spa_data *d = &buffer->buffer->datas[0];
+ uint8_t *pixels = mmap(NULL, STRIDE * HEIGHT, PROT_READ | PROT_WRITE, MAP_SHARED, d->fd, 0);
+ if (pixels != MAP_FAILED) {
+ uint8_t level = (uint8_t)(16 + (data->frame * 8) % 224);
+ for (int i = 0; i < STRIDE * HEIGHT; i += 4) {
+ pixels[i] = level;
+ pixels[i + 1] = 0x40;
+ pixels[i + 2] = 0x80;
+ pixels[i + 3] = 0xff;
+ }
+ munmap(pixels, STRIDE * HEIGHT);
+ }
+ d->chunk->offset = 0;
+ d->chunk->stride = STRIDE;
+ d->chunk->size = 1;
+ d->chunk->flags = SPA_CHUNK_FLAG_NONE;
+ data->frame++;
+ pw_stream_queue_buffer(data->stream, buffer);
+}
+
+static const struct pw_stream_events events = {
+ PW_VERSION_STREAM_EVENTS,
+ .state_changed = on_state_changed,
+ .param_changed = on_param_changed,
+ .add_buffer = on_add_buffer,
+ .remove_buffer = on_remove_buffer,
+ .process = on_process,
+};
+
+int main(int argc, char **argv) {
+ struct data data = {0};
+ pw_init(&argc, &argv);
+ data.loop = pw_main_loop_new(NULL);
+ data.stream = pw_stream_new_simple(pw_main_loop_get_loop(data.loop), "fake-niri-screencast",
+ pw_properties_new(PW_KEY_MEDIA_CLASS, "Video/Source", PW_KEY_MEDIA_TYPE, "Video",
+ PW_KEY_MEDIA_CATEGORY, "Source", PW_KEY_MEDIA_ROLE, "Screen", NULL),
+ &events, &data);
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
+ const int64_t modifiers[] = {X_TILED, LINEAR};
+ const struct spa_pod *params[1] = {video_format(&b, modifiers, 2, 1)};
+ if (pw_stream_connect(data.stream, PW_DIRECTION_OUTPUT, PW_ID_ANY,
+ PW_STREAM_FLAG_DRIVER | PW_STREAM_FLAG_ALLOC_BUFFERS, params, 1) < 0) {
+ fprintf(stderr, "connect failed\n");
+ return 1;
+ }
+ data.timer = pw_loop_add_timer(pw_main_loop_get_loop(data.loop), on_timeout, &data);
+ struct timespec interval = {0, 1000000000 / 30};
+ pw_loop_update_timer(pw_main_loop_get_loop(data.loop), data.timer, &interval, &interval, false);
+ pw_main_loop_run(data.loop);
+ return 0;
+}
diff --git a/scripts/linux-desktop-harness/niri-screencast/run.sh b/scripts/linux-desktop-harness/niri-screencast/run.sh
new file mode 100755
index 000000000..b6e99dfdb
--- /dev/null
+++ b/scripts/linux-desktop-harness/niri-screencast/run.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+# Records from a producer that shares frames the way niri 26.04 does, through
+# Cap's PipeWire capture loop (GitHub #2941). Starts a private PipeWire and
+# WirePlumber, so it runs on any Linux host with them installed.
+# Usage: run.sh (from the repository root; needs gcc, pkg-config,
+# libpipewire-0.3-dev, pipewire, wireplumber, dbus)
+set -euo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+work=$(mktemp -d)
+export XDG_RUNTIME_DIR="$work/runtime" CAP_FAKE_NIRI_PRODUCER="$work/producer"
+mkdir -m 700 "$XDG_RUNTIME_DIR"
+trap 'find "$work" -mindepth 1 -delete 2>/dev/null || true' EXIT
+gcc -o "$CAP_FAKE_NIRI_PRODUCER" "$here/producer.c" $(pkg-config --cflags --libs libpipewire-0.3)
+# The daemons belong to the inner shell, so it stops them before the outer
+# trap removes their runtime directory.
+WORK="$work" dbus-run-session -- bash -euc '
+ pipewire >"$WORK/pipewire.log" 2>&1 &
+ daemons=$!
+ trap "kill \$daemons 2>/dev/null; wait \$daemons 2>/dev/null || true" EXIT
+ for _ in $(seq 50); do [[ -S "$XDG_RUNTIME_DIR/pipewire-0" ]] && break; sleep 0.1; done
+ wireplumber >"$WORK/wireplumber.log" 2>&1 &
+ daemons="$daemons $!"
+ sleep 2
+ cargo test -p cap-recording --lib \
+ frames_from_a_producer_that_shares_only_dmabufs_are_captured -- --ignored --nocapture
+'
diff --git a/scripts/linux-desktop-harness/niri-software.Dockerfile b/scripts/linux-desktop-harness/niri-software.Dockerfile
new file mode 100644
index 000000000..dc67712ba
--- /dev/null
+++ b/scripts/linux-desktop-harness/niri-software.Dockerfile
@@ -0,0 +1,14 @@
+# niri 26.04 as released, with only its refusal of software renderers removed
+# so it runs on vkms with llvmpipe: CI has no GPU. Its screencast code (format
+# negotiation, GBM DMA-BUF allocation, PipeWire) is untouched.
+FROM cap-harness-arch AS build
+RUN pacman -Sy --noconfirm --needed base-devel git rust clang libinput libxkbcommon mesa \
+ pipewire seatd pango libdisplay-info libglvnd systemd-libs >/dev/null
+RUN git clone --depth 1 --branch v26.04 https://github.com/YaLTeR/niri /niri
+WORKDIR /niri
+RUN grep -n 'software EGL renderers are skipped' src/backend/tty.rs \
+ && sed -i 's/!egl_device.is_software(),/true,/' src/backend/tty.rs \
+ && grep -n 'software EGL renderers are skipped' -B2 src/backend/tty.rs
+RUN cargo build --release --locked 2>&1 | tail -3 && ls -la target/release/niri
+FROM cap-harness-arch
+COPY --from=build /niri/target/release/niri /usr/bin/niri
diff --git a/scripts/linux-desktop-harness/run-desktop.sh b/scripts/linux-desktop-harness/run-desktop.sh
index ec6543ce5..8e4468487 100644
--- a/scripts/linux-desktop-harness/run-desktop.sh
+++ b/scripts/linux-desktop-harness/run-desktop.sh
@@ -19,7 +19,13 @@ esac
name=cap-$desktop-$$
[[ -d "$out" ]] && find "$out" -mindepth 1 -delete
mkdir -p "$out" && chmod 777 "$out"
-docker build -q -f "$here/$dockerfile" -t "$image" "$here" >/dev/null
+# HARNESS_IMAGE: an image built from this one (a patched compositor) to run
+# instead.
+if [[ -n "${HARNESS_IMAGE:-}" ]]; then
+ image=$HARNESS_IMAGE
+else
+ docker build -q -f "$here/$dockerfile" -t "$image" "$here" >/dev/null
+fi
# DEV_BIN=<path to a built cap-gpui> with DEV_LIBS=<its FFmpeg lib dir> runs a
# local build in place of the packaged binary (the package still provides the
# desktop file and dependencies).
@@ -27,7 +33,10 @@ dev_mounts=()
if [[ -n "${DEV_BIN:-}" ]]; then
dev_mounts=(-v "$(realpath "$DEV_BIN")":/dev-bin/cap-gpui:ro -v "$(realpath "${DEV_LIBS:?set DEV_LIBS}")":/dev-libs:ro)
fi
-docker run -d --name "$name" --privileged --cgroupns=host \
+# DOCKER_EXTRA: more `docker run` flags, e.g. `--gpus all -e
+# NVIDIA_DRIVER_CAPABILITIES=all` to hand a real GPU's userspace to the desktop.
+read -r -a docker_extra <<<"${DOCKER_EXTRA:-}"
+docker run -d --name "$name" --privileged --cgroupns=host "${docker_extra[@]}" \
-v /sys/fs/cgroup:/sys/fs/cgroup:rw --tmpfs /run --tmpfs /run/lock \
-v "$package":/pkgs/"$(basename "$package")":ro -v "$here":/harness:ro -v "$out":/out \
"${dev_mounts[@]}" "$image" >/dev/null
diff --git a/scripts/linux-desktop-harness/scenario-clipboard.sh b/scripts/linux-desktop-harness/scenario-clipboard.sh
new file mode 100755
index 000000000..2e164f454
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-clipboard.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+# Paste into and copy out of a Cap text field with the keys a person presses.
+# Cap opens its teleprompter, whose script field takes focus on open; the
+# compositor's virtual keyboard (wtype) presses Ctrl+V with text another app
+# put on the Wayland clipboard (wl-copy), then Ctrl+A, Ctrl+C, and wl-paste
+# reads back what Cap copied. The script is saved to Cap's store, so the paste
+# is checked there too. Needs a wlroots-style compositor (niri, Hyprland).
+# Prints key=value results to $HARNESS_OUT/result.txt.
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|HYPRLAND_INSTANCE_SIGNATURE)=" | sed 's/^/export /')"
+
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+wait_log() {
+ for _ in $(seq $(($2 * 4))); do
+ grep -aq -- "$1" "$out/cap.log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+# Hyprland drops the first key each new virtual keyboard sends (any client,
+# not only Cap), so every wtype run leads with a Shift tap no field reacts to.
+press() { wtype -k Shift_L "$@" 2>>"$out/actions.log" || echo "wtype $* failed" >>"$out/actions.log"; sleep 0.5; }
+chord() { press -M ctrl "$1" -m ctrl; }
+
+# A profile past onboarding, so the teleprompter is the window that has focus.
+mkdir -p "$out/profile"
+cat >"$out/profile/store" <<'JSON'
+{
+ "general_settings": {
+ "hasCompletedStartup": true,
+ "hasCompletedOnboarding": true,
+ "enableNotifications": false,
+ "enableTelemetry": false
+ }
+}
+JSON
+bash "$here/launch-cap.sh" cap CAP_GPUI_AUTO_TELEPROMPTER=1 >/dev/null
+if ! wait_log "teleprompter window opened" 30; then
+ result teleprompter_opened no
+ exit 0
+fi
+result teleprompter_opened yes
+sleep 2
+
+probe="pasted from another app $RANDOM"
+wl-copy "$probe" 2>>"$out/actions.log"
+echo "clipboard holds: $(timeout 5 wl-paste -n 2>&1)" >>"$out/actions.log"
+if [[ ${XDG_CURRENT_DESKTOP:-} == niri ]]; then
+ NIRI_SOCKET=$(ls "$XDG_RUNTIME_DIR"/niri.*.sock 2>/dev/null | head -1) niri msg focused-window >>"$out/actions.log" 2>&1
+fi
+# Plain typing first, so a failed paste is told apart from keys not arriving.
+press "typed by hand "
+typed=no
+for _ in $(seq 20); do
+ grep -aqF "typed by hand" "$out/profile/store" 2>/dev/null && { typed=yes; break; }
+ sleep 0.25
+done
+result typing_reached_field "$typed"
+chord v
+# The script saves on a short debounce.
+saved=no
+for _ in $(seq 20); do
+ grep -aqF "$probe" "$out/profile/store" 2>/dev/null && { saved=yes; break; }
+ sleep 0.25
+done
+result paste_reached_field "$saved"
+
+wl-copy "clipboard before copy"
+chord a
+chord c
+copied=$(timeout 5 wl-paste -n 2>/dev/null || true)
+[[ $copied == *"$probe"* ]] && result copy_reached_clipboard yes || result copy_reached_clipboard no
+kill -0 "$(cat "$XDG_RUNTIME_DIR/cap.pid" 2>/dev/null)" 2>/dev/null && result cap_alive yes || result cap_alive no
+cat "$out/result.txt"
diff --git a/scripts/linux-desktop-harness/scenario-start-stop.sh b/scripts/linux-desktop-harness/scenario-start-stop.sh
index 8867e2b1d..38435beb3 100644
--- a/scripts/linux-desktop-harness/scenario-start-stop.sh
+++ b/scripts/linux-desktop-harness/scenario-start-stop.sh
@@ -122,7 +122,8 @@ attempt() {
result "attempt${n}_control" "$control"
[[ $control == tray || $control == shortcut ]] && use_control "$control"
# The screen-share dialog: a person picks the screen and presses Share.
- if [[ ${DESKTOP:-gnome} == gnome ]] && ! wait_log "recording started" 4 "$started_before"; then
+ # GNOME's portal draws this dialog on GNOME, and on niri, which uses it.
+ if [[ ${DESKTOP:-gnome} == gnome || $DESKTOP == niri ]] && ! wait_log "recording started" 4 "$started_before"; then
use_control approve "Share Screen|xdg-desktop-portal-gnome" "^Share$"
fi
if wait_log "recording started" 30 "$started_before"; then