2020-03-24 23:07:28 +00:00
|
|
|
#!/bin/bash
|
2018-10-22 20:25:04 +00:00
|
|
|
set -e
|
|
|
|
|
|
|
|
NOTIFY_APPNAME="$(basename "$0")"
|
|
|
|
NOTIFY_ICONPATH="/usr/share/icons/Xenlism-Wildfire/Apps/screenshot.svg"
|
|
|
|
|
2019-02-18 13:11:33 +00:00
|
|
|
FILENAME="$(date '+%s%N' | sha256sum | head -c7).png"
|
2018-10-22 20:25:04 +00:00
|
|
|
DIRECTORY="$HOME/pictures/screenshots"
|
|
|
|
|
|
|
|
SCP_HOST=frebib@Poseidon.nerdhouse.io
|
2020-03-21 22:36:58 +00:00
|
|
|
SCP_PATH=/services/www/files/frebib.net/s
|
2018-10-22 20:25:04 +00:00
|
|
|
URL_PATH=https://frebib.net/s
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2020-08-11 08:04:41 +00:00
|
|
|
params=$(getopt -o ac -l active,clip -n screenshot -- "$@")
|
|
|
|
eval set -- "$params"
|
|
|
|
unset params
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
'-a'|'--active')
|
|
|
|
active=1
|
|
|
|
shift
|
|
|
|
continue;;
|
|
|
|
'-c'|'--clip')
|
|
|
|
clip=1
|
|
|
|
shift
|
|
|
|
continue;;
|
|
|
|
'--')
|
|
|
|
shift
|
|
|
|
break;;
|
|
|
|
*)
|
|
|
|
echo 'getopt error' >&2
|
|
|
|
exit 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2018-10-22 20:25:04 +00:00
|
|
|
send_notification() {
|
|
|
|
title="$1"
|
|
|
|
body="$2"
|
|
|
|
replace_id="$3"
|
|
|
|
|
|
|
|
notify-send \
|
|
|
|
-a "$NOTIFY_APPNAME" \
|
|
|
|
-i "$NOTIFY_ICONPATH" \
|
|
|
|
"$title" "$body" \
|
|
|
|
$(if [ -n "$replace_id" ]; then printf "%s" "-r $replace_id"; fi) \
|
|
|
|
-p
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make the screenshot directory
|
|
|
|
mkdir -p "$DIRECTORY"
|
|
|
|
out_path="$DIRECTORY/$FILENAME"
|
2020-03-24 23:07:28 +00:00
|
|
|
|
2020-08-11 08:04:41 +00:00
|
|
|
if [ "$active" = '1' ]; then
|
2020-05-29 23:12:21 +00:00
|
|
|
args+=(-i "$(xdotool getactivewindow)")
|
2020-08-11 08:04:41 +00:00
|
|
|
else
|
|
|
|
# Take screenshot geometry
|
|
|
|
read -r winid geom < <(slop -f "%i %g") || :
|
|
|
|
if [ -z "$geom" -o -z "$winid" ]; then
|
|
|
|
send_notification "Screenshot error" "Failed to capture screenshot area" > /dev/null
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
args=(-g "$geom")
|
|
|
|
|
|
|
|
# If selection is the root window
|
|
|
|
if xwininfo -id "$winid" | grep -qw root; then
|
|
|
|
isroot=1
|
|
|
|
args+=(-i "$winid")
|
|
|
|
fi
|
2020-03-24 23:07:28 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Take the screenshot and save it
|
|
|
|
if ! error=$(maim -m 10 -u -f png ${args[@]} "$out_path" 2>&1); then
|
2018-10-22 20:25:04 +00:00
|
|
|
send_notification "Screenshot error" "Failed to take a screenshot:<br><br>$error" >/dev/null
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-03-24 23:07:28 +00:00
|
|
|
# If the screenshot is a complete window, add a shadow
|
|
|
|
if [ "$isroot" != "1" ]; then
|
|
|
|
# but don't worry if it fails
|
2020-04-02 11:49:11 +00:00
|
|
|
convert "$out_path" \( +clone -background black -alpha set -channel Alpha -evaluate set 60% -shadow 100x16+0+0 \) +swap -background none -layers merge +repage "$out_path" || :
|
2020-03-24 23:07:28 +00:00
|
|
|
fi
|
|
|
|
|
2020-08-11 08:04:41 +00:00
|
|
|
if [ "$clip" = '1' ]; then
|
2020-05-29 23:12:21 +00:00
|
|
|
# Add image to clipboard
|
|
|
|
xclip -i "$out_path" -selection clipboard -t image/png
|
2020-08-11 08:04:41 +00:00
|
|
|
send_notification "Screenshot captured" "$FILENAME<br><br>The image is available in the clipboard" >/dev/null
|
2018-10-22 20:25:04 +00:00
|
|
|
|
2020-05-29 23:12:21 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
notif_id=$(send_notification "Screenshot uploading" "$FILENAME" || true)
|
2018-10-22 20:25:04 +00:00
|
|
|
|
|
|
|
# 'Upload' the screenshot
|
|
|
|
if ! error=$(scp -C $out_path $SCP_HOST:$SCP_PATH 2>&1); then
|
|
|
|
send_notification "Screenshot upload error" "Failed to upload image:<br><br>$error" >/dev/null
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Add URL to clipboard
|
|
|
|
out_url="$URL_PATH/$FILENAME"
|
|
|
|
echo "$out_url" | xclip -i -selection clipboard
|
|
|
|
send_notification "Screenshot uploaded!" "$out_url" $notif_id >/dev/null || true
|
|
|
|
|