Files
sxmo-utils/scripts/appscripts/sxmo_screenshot.sh
Maarten van Gompel b6b70ed1f9 sxmo_screenshot: workaround for slurp issues
We've already had a number of patches attempting to resolve this issue,
and an upstream PR to slurp pending for quite a while. I think we need a
solution in the meantime that would work even on the broken version of
slurp (in case distros are behind once a fix is released).

This version also copes with the fat-trembling-finger problem and will
simply take a screenshot of the whole screen if the selection is too
tiny to be reasonable (which may be an issue even in the fixed slurp).

Ref: https://lists.sr.ht/~mil/sxmo-devel/patches/49367
Ref: https://lists.sr.ht/~mil/sxmo-devel/patches/49719
Ref: https://lists.sr.ht/~mil/sxmo-devel/patches/44258
Ref: https://github.com/emersion/slurp/issues/140
Ref: https://github.com/emersion/slurp/pull/141
--
Changes in v2:
- select only one output (adapted from magdesign's patch earlier)
- better error feedback

Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
2024-06-04 15:02:53 +02:00

81 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
# title="$icon_cam Screenshot"
# scrot refuses to work with double quotes
# shellcheck disable=SC2016
exit_msg() {
printf "%s\n" "$1" > /dev/stderr
notify-send "$1"
exit 1
}
check_command() {
command -v "$1" > /dev/null
}
sway_screenshot() {
check_command grim || exit_msg "grim command must be available to take a screenshot."
check_command slurp || exit_msg "slurp command must be available to make a selection."
area="$(slurp -o)"
if [ -z "$area" ]; then
area="$(swaymsg -t get_outputs | jq -r '.[] |select (.focused) | .rect | "\(.x),\(.y) \(.width)x\(.height)"')"
fi
wxh=$(echo "$area" | cut -d " " -f 2)
w=$(echo "$wxh" | cut -d "x" -f 1)
h=$(echo "$wxh" | cut -d "x" -f 2)
if [ -n "$wxh" ] && [ "$w" -gt 9 ] && [ "$h" -gt 9 ]; then
#we have a selection (bigger than 9x9)
grim -g "$area" "$1" || exit_msg "Screenshot failed"
else
exit_msg "Invalid screenshot selection (too small)"
fi
}
xorg_screenshot() {
check_command scrot || exit_msg "scrot command must be available to take a screenshot"
scrot -d 1 -q 1 -s "$1"
}
screenshot() {
case "$SXMO_WM" in
sway)
sway_screenshot "$@"
;;
dwm)
xorg_screenshot "$@"
;;
*)
exit_msg "We dont know the WM, cannot screenshot."
;;
esac
}
yank() {
printf %s "$1" | case "$SXMO_WM" in
sway)
wl-copy
;;
dwm)
xsel -b -i
;;
*)
exit_msg "We dont know the WM, cannot yank."
;;
esac
}
SXMO_SCREENSHOT_DIR="${SXMO_SCREENSHOT_DIR:-$HOME/screenshots}"
mkdir -p "$SXMO_SCREENSHOT_DIR"
FILENAME="$SXMO_SCREENSHOT_DIR/$(date +%Y-%m-%d-%T).png"
screenshot "$FILENAME"
yank "$FILENAME"
printf %s "$FILENAME"
notify-send --urgency=low "Screenshot taken" "$FILENAME"