@yhshzh0 这个脚本稍微有点性能问题,我们可以用clipnotify这个包来监听 X11 剪贴板的变化,然后把图片哈希比较换成了cmp -s:
从 X11 到 Wayland 的脚本:
#!/bin/bash
LOCK_FILE="/tmp/clipboard_bridge_lock"
TMP_DIR="/tmp/clipboard_sync_$$"
mkdir -p "$TMP_DIR"
trap "rm -rf '$TMP_DIR'; exit" INT TERM EXIT
COLOR_RESET="\033[0m"
COLOR_GREEN="\033[32m"
COLOR_BLUE="\033[34m"
COLOR_YELLOW="\033[33m"
COLOR_CYAN="\033[36m"
log() {
local timestamp=$(date '+%H:%M:%S')
echo -e "${COLOR_CYAN}[$timestamp]${COLOR_RESET} $1"
}
log_sync() {
local direction=$1
local type=$2
local format=$3
case "$direction" in
"x11->wl")
echo -e "${COLOR_CYAN}[$(date '+%H:%M:%S')]${COLOR_RESET} ${COLOR_GREEN}✓${COLOR_RESET} X11 → Wayland | ${COLOR_YELLOW}${type}${COLOR_RESET}${format}"
;;
esac
}
for cmd in xclip wl-copy clipnotify cmp; do
if ! command -v "$cmd" &> /dev/null; then
echo "Error: Missing dependency '$cmd'"
exit 1
fi
done
echo "Starting X11 -> Wayland sync (Event-driven with clipnotify)..."
clipboard_sync() {
local current_x11_img="$TMP_DIR/curr_x11.img"
local current_wl_img="$TMP_DIR/curr_wl.img"
local last_x11_img="$TMP_DIR/last_x11.img"
local last_text=""
rm -f "$LOCK_FILE"
while clipnotify; do
if [ -f "$LOCK_FILE" ]; then
sleep 0.2
continue
fi
# ==========================
sleep 0.05
img_synced=false
x11_targets=$(xclip -selection clipboard -t TARGETS -o 2>/dev/null)
[ -z "$x11_targets" ] && continue
mime_type=""
if [[ "$x11_targets" == *"image/png"* ]]; then
mime_type="image/png"
elif [[ "$x11_targets" == *"image/jpeg"* ]]; then
mime_type="image/jpeg"
elif [[ "$x11_targets" == *"image/gif"* ]]; then
mime_type="image/gif"
fi
if [[ -n "$mime_type" ]]; then
xclip -selection clipboard -t "$mime_type" -o > "$current_x11_img" 2>/dev/null
wl-paste -t "$mime_type" > "$current_wl_img" 2>/dev/null
if cmp -s "$current_x11_img" "$current_wl_img"; then
continue
fi
if ! cmp -s "$current_x11_img" "$last_x11_img"; then
wl-copy -t "$mime_type" < "$current_x11_img"
cp "$current_x11_img" "$last_x11_img"
log_sync "x11->wl" "Image" " ($mime_type)"
img_synced=true
fi
continue
fi
# -------- Text sync --------
if [[ "$img_synced" == false ]]; then
current_text=$(wl-paste --type text/plain 2>/dev/null || true)
x11_text=$(xclip -selection clipboard -o 2>/dev/null || true)
if [[ -n "$x11_text" && "$x11_text" != "$last_text" && "$x11_text" != "$current_text" ]]; then
echo -n "$x11_text" | wl-copy --type text/plain
last_text="$x11_text"
local preview="${x11_text:0:50}"
preview="${preview//$'\n'/↵}"
preview="${preview//$'\r'/}"
preview="${preview//$'\t'/⇥}"
[[ ${#x11_text} -gt 50 ]] && preview="${preview}..."
log_sync "x11->wl" "Text" " \"$preview\""
fi
fi
done
}
log "Clipboard sync service started"
log "Monitoring Wayland ↔ X11 clipboard sync..."
clipboard_sync
从 Wayland 到 X11 的脚本:
后台运行 wl-paste --watch bash -c ~/.local/bin/clipboard-wltox11.sh
#!/bin/bash
LOCK_FILE="/tmp/clipboard_bridge_lock"
trap "rm -f '$LOCK_FILE'" EXIT
touch "$LOCK_FILE"
types=$(wl-paste --list-types)
if [[ "$types" == *"image/png"* ]]; then
wl-paste --type image/png | xclip -selection clipboard -t image/png
elif [[ "$types" == *"image/jpeg"* ]]; then
wl-paste --type image/jpeg | xclip -selection clipboard -t image/jpeg
elif [[ "$types" == *"image/gif"* ]]; then
wl-paste --type image/gif | xclip -selection clipboard -t image/gif
else
wl-paste | xclip -selection clipboard
fi
sleep 0.1
rm -f "$LOCK_FILE"
Pinned Comments
Integral commented on 2023-03-14 09:20 (UTC)
目前通过删除 linuxqq 包自带的 libvips 临时解决了浏览图片时崩溃的问题 PS:感谢 @ayatale 的建议