From 2636f15cfaa186dd926ed4d79384f51a1f035a76 Mon Sep 17 00:00:00 2001 From: Pedro Sanchez Date: Mon, 16 Nov 2026 13:39:29 -0400 Subject: [PATCH 1/2] Record clipboard images copied in any stored format The clipboard history watched only image/png, so an image an app offered as JPEG, WebP, GIF, BMP or TIFF alone never reached the history. Watch for any image type instead, and fall back to a stored format when the first one on offer isn't, so an SVG offered ahead of PNG still records the PNG. --- shell/plugins/clipboard/Clipboard.qml | 2 +- shell/plugins/clipboard/capture.sh | 13 ++++++++++++- test/shell.d/clipboard-test.sh | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/shell/plugins/clipboard/Clipboard.qml b/shell/plugins/clipboard/Clipboard.qml index bb2b4c0fd1f3..428e6042e91d 100644 --- a/shell/plugins/clipboard/Clipboard.qml +++ b/shell/plugins/clipboard/Clipboard.qml @@ -299,7 +299,7 @@ Item { Process { id: imageWatchProc - command: ["setpriv", "--pdeathsig", "TERM", "wl-paste", "--type", "image/png", "--watch", root.captureScript, "image/png"] + command: ["setpriv", "--pdeathsig", "TERM", "wl-paste", "--type", "image", "--watch", root.captureScript, "image"] onExited: watchRestartTimer.restart() stdout: SplitParser { onRead: function(data) { root.addClipboardJson(data) } diff --git a/shell/plugins/clipboard/capture.sh b/shell/plugins/clipboard/capture.sh index fee496584585..d0d958b222b6 100755 --- a/shell/plugins/clipboard/capture.sh +++ b/shell/plugins/clipboard/capture.sh @@ -98,12 +98,23 @@ emit_text() { ' <"$tmp" } +image_types=(image/png image/jpeg image/webp image/gif image/bmp image/tiff) + case "${1:-}" in text) emit_text cat; exit 0 ;; image/*) emit_image "$1" cat; exit 0 ;; +image) + # wl-paste --type image hands over the first image type on offer. Keep that + # copy when it's a type history stores, else read one that is below. + offered=$(grep -m1 '^image/' <<<"$types") + if [[ " ${image_types[*]} " == *" $offered "* ]]; then + emit_image "$offered" cat + exit 0 + fi + ;; esac -for mime in image/png image/jpeg image/webp image/gif image/bmp image/tiff; do +for mime in "${image_types[@]}"; do if grep -qx "$mime" <<<"$types"; then emit_image "$mime" wl-paste --type "$mime" exit 0 diff --git a/test/shell.d/clipboard-test.sh b/test/shell.d/clipboard-test.sh index 00e34a2d4298..7636b90177d3 100644 --- a/test/shell.d/clipboard-test.sh +++ b/test/shell.d/clipboard-test.sh @@ -366,6 +366,11 @@ image_path=$(jq -r '.path' <<<"$capture_output") jq -e '.mime == "image/jpeg"' <<<"$capture_output" >/dev/null && [[ $image_path == *.jpg ]] || fail "clipboard capture stores watched jpeg images with jpg extension" pass "clipboard capture stores watched jpeg images with jpg extension" +capture_output=$(printf 'jpg-only-data' | WL_PASTE_TYPES="text/html\nimage/jpeg\n" XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" image) +image_path=$(jq -r '.path' <<<"$capture_output") +jq -e '.mime == "image/jpeg"' <<<"$capture_output" >/dev/null && [[ $image_path == *.jpg && $(<"$image_path") == "jpg-only-data" ]] || fail "clipboard capture records watched images offered only as jpeg" +pass "clipboard capture records watched images offered only as jpeg" + capture_output=$(printf 'secret' | CLIPBOARD_STATE=sensitive XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" text) [[ -z $capture_output ]] || fail "clipboard capture ignores sensitive watched text" pass "clipboard capture ignores sensitive watched text" From 015d3f1966b7c5a19ea675506e7ad7739fcb3907 Mon Sep 17 00:00:00 2001 From: Pedro Sanchez Date: Mon, 16 Nov 2026 16:28:17 -0400 Subject: [PATCH 2/2] Identify watched clipboard images from their captured bytes --- shell/plugins/clipboard/capture.sh | 30 ++++++++++++++++++++---------- test/shell.d/clipboard-test.sh | 21 ++++++++++++++++++--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/shell/plugins/clipboard/capture.sh b/shell/plugins/clipboard/capture.sh index d0d958b222b6..53397a14dfcd 100755 --- a/shell/plugins/clipboard/capture.sh +++ b/shell/plugins/clipboard/capture.sh @@ -1,8 +1,8 @@ #!/bin/bash # Captures the current clipboard as a JSON entry on stdout. In watch mode, -# wl-paste invokes this with the payload on stdin and the mime as $1. Without -# arguments, it snapshots the current selection itself. +# wl-paste invokes this with the payload on stdin and a type as $1 (text, image, +# or an explicit MIME type). Without arguments, it snapshots the selection itself. set -o pipefail @@ -30,12 +30,23 @@ emit_image() { local ext hash file shift - ext=${mime#image/} - [[ $ext == jpeg ]] && ext=jpg - tmp=$(mktemp --tmpdir="$IMAGE_DIR" clipboard.XXXXXX) || return 0 read_copy "$@" || return + if [[ $mime == "image" ]]; then + # The clipboard may have changed since wl-paste handed us this copy. + # Identify its bytes, not the types offered by the current selection. + mime=$(file --brief --mime-type -- "$tmp") || return + [[ $mime == "image/x-ms-bmp" ]] && mime="image/bmp" + if [[ " ${image_types[*]} " != *" $mime "* ]]; then + rm -f -- "$tmp" + return 2 # Unsupported format: try a supported representation instead. + fi + fi + + ext=${mime#image/} + [[ $ext == jpeg ]] && ext=jpg + hash=$(sha256sum "$tmp" | awk '{print $1}') file="$IMAGE_DIR/$hash.$ext" if [[ -e $file ]]; then @@ -104,11 +115,10 @@ case "${1:-}" in text) emit_text cat; exit 0 ;; image/*) emit_image "$1" cat; exit 0 ;; image) - # wl-paste --type image hands over the first image type on offer. Keep that - # copy when it's a type history stores, else read one that is below. - offered=$(grep -m1 '^image/' <<<"$types") - if [[ " ${image_types[*]} " == *" $offered "* ]]; then - emit_image "$offered" cat + emit_image image cat + # Only an unsupported format (e.g. SVG) needs a second read. Drop an empty + # or stalled copy rather than replacing it with a different clipboard entry. + if (( $? != 2 )); then exit 0 fi ;; diff --git a/test/shell.d/clipboard-test.sh b/test/shell.d/clipboard-test.sh index 7636b90177d3..e41c905ff4ef 100644 --- a/test/shell.d/clipboard-test.sh +++ b/test/shell.d/clipboard-test.sh @@ -186,6 +186,8 @@ elif [[ $1 == "--type" && $2 == "text" ]]; then sleep 10 fi printf '%s' "${WL_PASTE_TEXT:-terminal copy}" +elif [[ $1 == "--type" && $2 == "image/png" ]]; then + cat "$WL_PASTE_IMAGE" fi SH @@ -252,7 +254,7 @@ fi pass "clipboard snapshot text read stalled midway uses its internal timeout" pass "clipboard snapshot stalled midway emits no partial entry" -for watched_mime in text image/png; do +for watched_mime in text image/png image; do if capture_output=$({ printf 'copy whose owner never closes'; sleep 1; } | XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" timeout --preserve-status 4s "$ROOT/shell/plugins/clipboard/capture.sh" "$watched_mime"); then capture_status=0 else @@ -366,11 +368,24 @@ image_path=$(jq -r '.path' <<<"$capture_output") jq -e '.mime == "image/jpeg"' <<<"$capture_output" >/dev/null && [[ $image_path == *.jpg ]] || fail "clipboard capture stores watched jpeg images with jpg extension" pass "clipboard capture stores watched jpeg images with jpg extension" -capture_output=$(printf 'jpg-only-data' | WL_PASTE_TYPES="text/html\nimage/jpeg\n" XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" image) +magick -size 1x1 xc:red "$TMPDIR/watched.png" +magick "$TMPDIR/watched.png" "$TMPDIR/watched.jpg" + +capture_output=$(WL_PASTE_TYPES="text/html\nimage/jpeg\n" XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" image <"$TMPDIR/watched.jpg") image_path=$(jq -r '.path' <<<"$capture_output") -jq -e '.mime == "image/jpeg"' <<<"$capture_output" >/dev/null && [[ $image_path == *.jpg && $(<"$image_path") == "jpg-only-data" ]] || fail "clipboard capture records watched images offered only as jpeg" +jq -e '.mime == "image/jpeg"' <<<"$capture_output" >/dev/null && [[ $image_path == *.jpg ]] && cmp -s "$image_path" "$TMPDIR/watched.jpg" || fail "clipboard capture records watched images offered only as jpeg" pass "clipboard capture records watched images offered only as jpeg" +capture_output=$(WL_PASTE_TYPES="image/jpeg\n" XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" image <"$TMPDIR/watched.png") +image_path=$(jq -r '.path' <<<"$capture_output") +jq -e '.mime == "image/png"' <<<"$capture_output" >/dev/null && [[ $image_path == *.png ]] && cmp -s "$image_path" "$TMPDIR/watched.png" || fail "clipboard capture keeps the watched format when the clipboard changes" +pass "clipboard capture keeps the watched format when the clipboard changes" + +capture_output=$(printf '' | WL_PASTE_TYPES="image/svg+xml\nimage/png\n" WL_PASTE_IMAGE="$TMPDIR/watched.png" XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" image) +image_path=$(jq -r '.path' <<<"$capture_output") +jq -e '.mime == "image/png"' <<<"$capture_output" >/dev/null && [[ $image_path == *.png ]] && cmp -s "$image_path" "$TMPDIR/watched.png" || fail "clipboard capture falls back to png when svg is offered first" +pass "clipboard capture falls back to png when svg is offered first" + capture_output=$(printf 'secret' | CLIPBOARD_STATE=sensitive XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh" text) [[ -z $capture_output ]] || fail "clipboard capture ignores sensitive watched text" pass "clipboard capture ignores sensitive watched text"