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

Add support for Linux/Wayland with wl-paste #1

Merged
merged 1 commit into from Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 77 additions & 12 deletions res/linux.sh
@@ -1,14 +1,79 @@
#!/bin/sh

# require xclip(see http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script/677212#677212)
command -v xclip >/dev/null 2>&1 || { echo >&1 "no xclip"; exit 1; }

# write image in clipboard to file (see http://unix.stackexchange.com/questions/145131/copy-image-from-clipboard-to-file)
if
xclip -selection clipboard -target image/png -o >/dev/null 2>&1
then
xclip -selection clipboard -target image/png -o >$1 2>/dev/null
echo $1
else
echo "no image"
fi
# Paste an image from the display system's clipboard to a file for use in Dendron
#
# This script attempts to determine if X11/Xorg or Wayland are in use, and utilize an
# appropriate/common command-line utility for each to interact with the clipboard.

cleanup_bad_image_write() {
# Check if the file exists
if [ -r "${1}" ] ; then
# Check if the file is larger than 0 bytes, if not, it was probably a write from a command's
# redirect that had an error - delete it
if ! [ -s "${1}" ] ; then
rm -f "${1}"
fi
fi
}

paste_xclip() {
# require xclip (see http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script/677212#677212)
if ! command -v xclip >/dev/null 2>&1 ; then
echo "no xclip"
exit 1
fi

# write image in clipboard to file (see http://unix.stackexchange.com/questions/145131/copy-image-from-clipboard-to-file)
if xclip -selection clipboard -target image/png -o > "${1}" 2>/dev/null ; then
echo "${1}"
exit 0
else
echo "no image"
cleanup_bad_image_write "${1}"
exit 1
fi
}

paste_wlclipboard() {
# require wl-paste from the wl-clipboard package
if ! command -v wl-paste >/dev/null 2>&1 ; then
echo "no wl-paste"
exit 1
fi

# write image in clipboard to file
if wl-paste --type image/png > "${1}" 2>/dev/null; then
echo "${1}"
exit 0
else
echo "no image"
cleanup_bad_image_write "${1}"
exit 1
fi
}

main() {
# Determine the Linux display type (X11, Wayland, etc)
# https://unix.stackexchange.com/questions/202891/how-to-know-whether-wayland-or-x11-is-being-used#comment1133584_371164
if command -v loginctl >/dev/null 2>&1 ; then
display_type="$(loginctl show-session $(loginctl show-user ${USER} -p Display --value) -p Type --value)"
else
echo "no loginctl command present, unable to determine display type - default will assume X11/Xorg" >&2
fi

case "${display_type}" in
wayland)
paste_wlclipboard "${1}"
;;
x11)
paste_xclip "${1}"
;;
*)
# To maintain backwards compatabiltiy after adding Wayland suport, assume
# any unknown display type uses xclip
paste_xclip "${1}"
;;
esac
}

main "$@"
5 changes: 4 additions & 1 deletion src/extension.ts
Expand Up @@ -352,7 +352,10 @@ class Paster {
ascript.stdout.on('data', function (data: Buffer) {
let result = data.toString().trim();
if (result == "no xclip") {
Logger.showInformationMessage('You need to install xclip command first.');
Logger.showInformationMessage('You need to install the xclip command first.');
return;
} else if (result == "no wl-paste") {
Logger.showInformationMessage('You need to install the wl-paste command first.');
return;
}
cb(imagePath, result);
Expand Down