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..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 @@ -98,12 +109,22 @@ 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) + 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 + ;; 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..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,6 +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" +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 ]] && 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"