Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KDE Connect clipboard sync issue and --watch #241

Closed
Vaisakhkm2625 opened this issue Nov 8, 2024 · 5 comments
Closed

KDE Connect clipboard sync issue and --watch #241

Vaisakhkm2625 opened this issue Nov 8, 2024 · 5 comments

Comments

@Vaisakhkm2625
Copy link

First of all, thanks for making wl-clipboard.. such a excellent replacement for xclip..

ISSUE: KDE Connect not properly integrating with wl-clipboard

Since i switched to hyprland (over a year ago), KDE Connect hasn't worked properly, Like remote desktop/clipboard sync etc
Among which clipboard sync is one of the most important feature i used before that.. I haven't able to figure out if it's issue with my setup, or with KDE Connect not working with wayland (more likely) or with wl-clipboard

Workaround (or proper way? on pov of unix philosophy )

As a workaround, i decided to make shell script only to find myself more confused how wl-paste --watch works..

 wl-paste -t text --watch cat | xargs -I{} kdeconnect-cli -d a6780b4540540a1c --share-text {}

The issue is, it's only copying till the last \n char, which i understand why, but i am not sure how to get around it..

image

 wl-paste --watch kdeconnect-cli -d a6780b4540540a1c --share-text "$(wl-paste)" 

which is just sending last thing copied before running this...

image

Should i be doing some read looping to get the text continuously?

If possible, can you please help me when you have time?
feel free to close the tickets if you feel like this issue don't belong here..

I am glad to help in case any help needed from my side..

@bugaevc
Copy link
Owner

bugaevc commented Nov 9, 2024

Hi!

Note that the way you wrote it, cat is the command you pass to wl-paste --watch, and then you pipe the complete output of all cat invocations through the pipe to xargs on the other side, which will split it into lines and then call kdeconnect-cli. I don't understand what you were trying to achieve that way, and why you need the xargs here for example.

Maybe this is closer to what you wanted?

$ wl-paste -t text --watch bash -c 'kdeconnect-cli -d a6780b4540540a1c --share-text "$(</dev/stdin)"'

To make this a bit cleaner, you could make a little script out of it:

#! /bin/bash

kdeconnect-cli -d a6780b4540540a1c --share-text "$(</dev/stdin)"

save that into e.g. share-text.sh, and then run wl-paste -t text --watch share-text.sh

wl-paste --watch kdeconnect-cli -d a6780b4540540a1c --share-text "$(wl-paste)"
which is just sending last thing copied before running this...

This is because your shell substitutes "$(wl-paste)" first, and only then invokes wl-paste --watch ... --share-text the-specific-thing. If you want to use shell features like pipes or command substitution inside the --watch command, you need to use an(other) instance of shell as the --watch command. Either bash -c 'actual command' or making an explicit script containing the command(s) will achieve that.

Does that make sense?

@Vaisakhkm2625
Copy link
Author

Vaisakhkm2625 commented Nov 9, 2024

Maybe this is closer to what you wanted?

$ wl-paste -t text --watch bash -c 'kdeconnect-cli -d a6780b4540540a1c --share-text "$(</dev/stdin)"'

Wow! exactly what i wanted.. Thanks.. 😅

This is because your shell substitutes "$(wl-paste)" first, and only then invokes wl-paste --watch ... --share-text the-specific-thing. If you want to use shell features like pipes or command substitution inside the --watch command, you need to use an(other) instance of shell as the --watch command. Either bash -c 'actual command' or making an explicit script containing the command(s) will achieve that.

Does that make sense?

Yes 100% make sense.....

But actually i raised this issue is to resolve the real problem of kde connect not integrating with system clipboard...
what i were trying to do it was a workaround,
So, is it a issue with kde connect or something i am doing wrong, like missing some dependency ? or is it because kde connect uses some of kde specific apis under the hood to get and set clipboards? i don't remember exactly, but few of other apps also had the same issue... (like copy button on websites in firefox not working??)

for example, in kdeconnect, now even though i can sent clipboard from system to android, senting clipboard from android to linux doesn't work.. and there is no cli option like --share-text to get clipboard in the kdeconnect-cli.. :(

is there any fix needed to done? else we can close this issue...

Really thanks for responding to me..

@bugaevc
Copy link
Owner

bugaevc commented Nov 10, 2024

But actually i raised this issue is to resolve the real problem of kde connect not integrating with system clipboard...
what i were trying to do it was a workaround,
So, is it a issue with kde connect or something i am doing wrong, like missing some dependency ? or is it because kde connect uses some of kde specific apis under the hood to get and set clipboards? i don't remember exactly, but few of other apps also had the same issue... (like copy button on websites in firefox not working??)

for example, in kdeconnect, now even though i can sent clipboard from system to android, senting clipboard from android to linux doesn't work.. and there is no cli option like --share-text to get clipboard in the kdeconnect-cli.. :(

is there any fix needed to done? else we can close this issue...

You should raise this with KDE Connect developers, or perhaps with Hyprland developers. There's nothing to fix on the wl-clipboard side, so let's close this indeed.

@bugaevc bugaevc closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2024
@Vaisakhkm2625
Copy link
Author

You should raise this with KDE Connect developers, or perhaps with Hyprland developers. There's nothing to fix on the wl-clipboard side, so let's close this indeed.

:) ok thanks...

@Vaisakhkm2625
Copy link
Author

Issue is, itseems clipboard can't be set in bg in wayland, valent(kdeconnect alt impl) has a issue on it.. andyholmes/valent#624

KDE Connect was running in xwayland mode.... so xclip is working..

i noticed this when i was working with copykitten package... i made a sync script to sync between wl-clipboard and xclip..

but today i found a better script.. from a hyprland issue (hyprwm/Hyprland#6132),

https://gist.github.com/armenr/81b77587c1dda1d00d1c1c9541dcda94

script
#!/usr/bin/env sh
#
# Two-way clipboard syncronization between Wayland and X11, with cliphy support!
# !! Recommended use: Drop this file off @ /usr/local/bin/clipsync && make it executable
# Requires: wl-clipboard, xclip, clipnotify.
# Modified from: https://github.com/hyprwm/Hyprland/issues/6132#issuecomment-2127153823
#
# Usage:
#   clipsync watch [with-notifications|without-notifications] - run in background.
#   clipsync stop - kill all background processes.
#   echo -n any | clipsync insert [with-notifications|without-notifications] - insert clipboard content from stdin.
#   clipsync help - display help information.
#
# Workaround for issue:
# "Clipboard synchronization between wayland and xwayland clients broken"
# https://github.com/hyprwm/Hyprland/issues/6132
#
# Also pertains to:
# https://github.com/hyprwm/Hyprland/issues/6247
# https://github.com/hyprwm/Hyprland/issues/6443
# https://github.com/hyprwm/Hyprland/issues/6148

# Updates clipboard content of both Wayland and X11 if current clipboard content differs.
# Usage: echo -e "1\n2" | clipsync insert [with-notifications|without-notifications]
get_mime_type() {
  wl-paste --list-types | head -n 1
}

insert() {
  mime_type=$(get_mime_type)
  case "$mime_type" in
  text/*)
    value=$(wl-paste -n | tr -d '\0')
    insert_text "$value" "$1"
    ;;
  image/*)
    insert_image "$1"
    ;;
  *)
    insert_file "$1"
    ;;
  esac
}

insert_text() {
  value="$1"
  notification_mode="$2"
  wValue=$(wl-paste -n 2>/dev/null | tr -d '\0' || printf "")
  xValue=$(xclip -o -selection clipboard 2>/dev/null | tr -d '\0' || printf "")

  notify() {
    if [ "$notification_mode" != "without-notifications" ]; then
      truncated_value=$(printf "%.50s" "$value")
      notify-send -u low -c clipboard "Text copied" "$truncated_value"
    fi
  }

  update_clipboard() {
    if [ "$value" != "$1" ]; then
      notify
      printf "%s" "$value" | $2
      command -v cliphist >/dev/null 2>&1 && printf "%s" "$value" | cliphist store
    fi
  }

  update_clipboard "$wValue" "wl-copy"
  update_clipboard "$xValue" "xclip -selection clipboard"
}

insert_image() {
  notification_mode="$1"
  tmp_file=$(mktemp)
  wl-paste >"$tmp_file"

  [ "$notification_mode" != "without-notifications" ] && notify-send -u low -c clipboard "Image copied" "Image data copied to clipboard"

  wl-copy <"$tmp_file"
  xclip -selection clipboard -t "$(get_mime_type)" <"$tmp_file"
  command -v cliphist >/dev/null 2>&1 && wl-copy <"$tmp_file" | cliphist store

  rm "$tmp_file"
}

insert_file() {
  notification_mode="$1"
  tmp_file=$(mktemp)
  wl-paste >"$tmp_file"

  [ "$notification_mode" != "without-notifications" ] && notify-send -u low -c clipboard "File copied" "File data copied to clipboard"

  wl-copy <"$tmp_file"
  xclip -selection clipboard -t "$(get_mime_type)" <"$tmp_file"
  command -v cliphist >/dev/null 2>&1 && wl-copy <"$tmp_file" | cliphist store

  rm "$tmp_file"
}

# Watch for clipboard changes and synchronize between Wayland and X11
# Usage: clipsync watch [with-notifications|without-notifications]
watch() {
  sleep 1
  notification_mode=${1:-with-notifications}

  watch_clipboard() {
    $1 | while read -r _; do
      clipsync insert "$notification_mode"
    done &
  }

  watch_clipboard "wl-paste --watch printf ''"
  watch_clipboard "wl-paste --primary --watch printf ''"
  watch_clipboard "clipnotify"
  watch_clipboard "clipnotify -s PRIMARY"
}

# Kill all background processes related to clipsync
stop_clipsync() {
  pkill -f "wl-paste --type text --watch"
  pkill clipnotify
  pkill -f "xclip -selection clipboard"
  pkill -f "clipsync insert"
}

help() {
  cat <<EOF
clipsync - Two-way clipboard synchronization between Wayland and X11, with cliphist support

Usage:
  clipsync watch [with-notifications|without-notifications]
    Run clipboard synchronization in the background.
    Options:
      with-notifications (default): Show desktop notifications for clipboard changes.
      without-notifications: Operate silently without notifications.

  clipsync stop
    Stop all background processes related to clipsync.

  echo -n "text" | clipsync insert [with-notifications|without-notifications]
    Insert clipboard content from stdin.
    Notification options work the same as in the watch command.

  clipsync help
    Display this help information.

Requirements: wl-clipboard, xclip, clipnotify
Optional: cliphist (for Hyprland users)
EOF
}

case "$1" in
watch)
  watch "$2"
  ;;
stop)
  stop_clipsync
  ;;
insert)
  insert "$2"
  ;;
help)
  help
  ;;
*)
  echo "Usage: $0 {watch [with-notifications|without-notifications]|stop|insert [with-notifications|without-notifications]|help}"
  echo "Run '$0 help' for more information."
  exit 1
  ;;
esac

so that fixes this...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants