Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shell/plugins/clipboard/Clipboard.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
33 changes: 27 additions & 6 deletions shell/plugins/clipboard/capture.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion test/shell.d/clipboard-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 '<svg xmlns="http://www.w3.org/2000/svg"/>' | 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"
Expand Down