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

Clipboard adapter for Wayland environments (e.g. Sway) #624

Open
Iss-in opened this issue Feb 18, 2024 · 6 comments
Open

Clipboard adapter for Wayland environments (e.g. Sway) #624

Iss-in opened this issue Feb 18, 2024 · 6 comments
Labels
feature New feature or enhancement help wanted An issue that needs contributors

Comments

@Iss-in
Copy link

Iss-in commented Feb 18, 2024

Current Behavior

clipboard isnt being synced from pc to android until the valent window is focused via mouse.

Since im facing this issue on sway, this might only be related to sway/wlroots compositor behaviour specifically.
i faced a similar issue with firefox where subtitles werent being updated when browser window wasnt focused, which is resolved by now.

linking that issue incase its of any help

https://bugzilla.mozilla.org/show_bug.cgi?id=1770429

Expected Behavior

clipboard should sync to android device the moment its changed.

Desktop

Other (please specify)

Other Desktop

Sway/Wlroots

Operating System

Aech

Installed from

Distribution Package

Version

1.0.0.alpha

Devices

KDE Connect (Android), Valent (Desktop Linux)

Plugins

clipboard

Logs

20:57:26.5143                       GLib-GIO:    DEBUG: _g_io_module_get_default: Found default implementation dconf (DConfSettingsBackend) for ‘gsettings-backend’
20:57:26.5150                       GLib-GIO:    DEBUG: Using cross-namespace EXTERNAL authentication (this will deadlock if server is GDBus < 2.73.3)
20:57:26.8288                       GLib-GIO:    DEBUG: _g_io_module_get_default: Found default implementation gvfs (GDaemonVfs) for ‘gio-vfs’
20:57:26.8859                       GLib-GIO:    DEBUG: _g_io_module_get_default: Found default implementation gnutls (GTlsBackendGnutls) for ‘gio-tls-backend’
20:57:26.8864                       GLib-GIO:    DEBUG: Failed to initialize portal (GNetworkMonitorPortal) for gio-network-monitor: Not using portals
20:57:26.8866                       GLib-GIO:    DEBUG: Using cross-namespace EXTERNAL authentication (this will deadlock if server is GDBus < 2.73.3)
20:57:26.8873                       GLib-GIO:    DEBUG: _g_io_module_get_default: Found default implementation networkmanager (GNetworkMonitorNM) for ‘gio-network-monitor’
20:57:34.2562                 valent-battery:    DEBUG: ValentBattery: not a battery
20:57:34.3243            valent-mpris-player:    DEBUG: valent_mpris_player_get_position(): GDBus.Error:org.freedesktop.DBus.Error.NotSupported: /org/mpris/MediaPlayer2.org.mpris.MediaPlayer2.Player Position is not supported
20:57:41.7540        valent-clipboard-plugin:    DEBUG: valent_clipboard_read_text_cb(): text not available
20:57:41.7546        valent-clipboard-plugin:    DEBUG: valent_clipboard_read_text_cb(): text not available

Screenshots

No response

@Iss-in Iss-in added the triage An issue that needs confirmation and labeling label Feb 18, 2024
@andyholmes
Copy link
Owner

That's correct, without a better option, Valent will fallback to the GDK-based clipboard which requires the application to gain focus to acquire the clipboard selection.

For better support, someone will have to contribute a ClipboardAdapter, maybe backed by something like wl-clipboard. Ideally it would be done in code, but a subprocess would be acceptable, too.

@andyholmes andyholmes added feature New feature or enhancement help wanted An issue that needs contributors and removed triage An issue that needs confirmation and labeling labels Feb 18, 2024
@andyholmes andyholmes changed the title clipboard not syncing until window is foccussed Clipboard adapter for Wayland environments (e.g. Sway) Feb 18, 2024
@orowith2os
Copy link

It may be preferable to use the RemoteDesktop portal, and by that path, request access to the Clipboard portal, rather than relying on Wayland protocols here. It's not intended behavior (in the Wayland protocol) to allow any unfocused Wayland client (yes, that includes wl-clipboard, a shell script) to manipulate the clipboard.

@Vaisakhkm2625
Copy link

It may be preferable to use the RemoteDesktop portal, and by that path, request access to the Clipboard portal, rather than relying on Wayland protocols here. It's not intended behavior (in the Wayland protocol) to allow any unfocused Wayland client (yes, that includes wl-clipboard, a shell script) to manipulate the clipboard.

:( RemoteDesktop portal not implimented in hyprland, and vaxry doesn't have plans to impliment it anytime soon...,

also for now insolved the issue (atleast in kdeconnect) with a sync script between xclip and wl-clipboard..

@Iss-in
Copy link
Author

Iss-in commented Nov 14, 2024

It may be preferable to use the RemoteDesktop portal, and by that path, request access to the Clipboard portal, rather than relying on Wayland protocols here. It's not intended behavior (in the Wayland protocol) to allow any unfocused Wayland client (yes, that includes wl-clipboard, a shell script) to manipulate the clipboard.

:( RemoteDesktop portal not implimented in hyprland, and vaxry doesn't have plans to impliment it anytime soon...,

also for now insolved the issue (atleast in kdeconnect) with a sync script between xclip and wl-clipboard..

can you please explain how, I am also using hyprland

@Vaisakhkm2625
Copy link

Vaisakhkm2625 commented Nov 14, 2024

can you please explain how, I am also using hyprland

chatgpt script, i haven't even properly read it, but it works... so here you go...

replace first line with #!/bin/bash and install xclip and wl-clilpboard if you are not using nixos

#!/usr/bin/env -S nix shell nixpkgs#xclip nixpkgs#wl-clipboard -c bash

# Store previous clipboard contents
prev_clip_xclip=""
prev_clip_wl=""

# Sync frequency (in seconds)
sync_frequency=0.5

while true; do
	# Get clipboard contents from xclip and wl-clipboard
	curr_clip_xclip=$(xclip -selection clipboard -o 2>/dev/null || echo "")
	curr_clip_wl=$(wl-paste --no-newline || echo "")

	# If xclip has new content, copy it to Wayland clipboard
	if [ "$curr_clip_xclip" != "$prev_clip_xclip" ]; then
		echo "$curr_clip_xclip" | wl-copy
		prev_clip_xclip="$curr_clip_xclip"
		prev_clip_wl="$curr_clip_xclip"
	fi

	# If wl-clipboard has new content, copy it to xclip clipboard
	if [ "$curr_clip_wl" != "$prev_clip_wl" ]; then
		echo "$curr_clip_wl" | xclip -selection clipboard
		prev_clip_xclip="$curr_clip_wl"
		prev_clip_wl="$curr_clip_wl"
	fi

	# Wait before the next sync cycle
	sleep "$sync_frequency"
done

wl-clipboard has mechanism to run a command when something copied.. so i am sure there is better way to do this script than a infinite loop.. i will update in the future when i have time..

@Vaisakhkm2625
Copy link

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

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

go and give him a star..

script just in case, in the future in @armenr wanted to remove repo/ change username something, just copy pasting the script here too
#!/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or enhancement help wanted An issue that needs contributors
Projects
None yet
Development

No branches or pull requests

4 participants