aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/grimshot
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/grimshot')
-rwxr-xr-xcontrib/grimshot168
1 files changed, 0 insertions, 168 deletions
diff --git a/contrib/grimshot b/contrib/grimshot
deleted file mode 100755
index 1ec19def..00000000
--- a/contrib/grimshot
+++ /dev/null
@@ -1,168 +0,0 @@
1#!/bin/sh
2
3## Grimshot: a helper for screenshots within sway
4## Requirements:
5## - `grim`: screenshot utility for wayland
6## - `slurp`: to select an area
7## - `swaymsg`: to read properties of current window
8## - `wl-copy`: clipboard utility
9## - `jq`: json utility to parse swaymsg output
10## - `notify-send`: to show notifications
11## Those are needed to be installed, if unsure, run `grimshot check`
12##
13## See `man 1 grimshot` or `grimshot usage` for further details.
14
15getTargetDirectory() {
16 test -f "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs" && \
17 . "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
18
19 echo "${XDG_SCREENSHOTS_DIR:-${XDG_PICTURES_DIR:-$HOME}}"
20}
21
22NOTIFY=no
23CURSOR=
24
25while [ $# -gt 0 ]; do
26 key="$1"
27
28 case $key in
29 -n|--notify)
30 NOTIFY=yes
31 shift # past argument
32 ;;
33 -c|--cursor)
34 CURSOR=yes
35 shift # past argument
36 ;;
37 *) # unknown option
38 break # done with parsing --flags
39 ;;
40 esac
41done
42
43ACTION=${1:-usage}
44SUBJECT=${2:-screen}
45FILE=${3:-$(getTargetDirectory)/$(date -Ins).png}
46
47if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "check" ]; then
48 echo "Usage:"
49 echo " grimshot [--notify] [--cursor] (copy|save) [active|screen|output|area|window] [FILE|-]"
50 echo " grimshot check"
51 echo " grimshot usage"
52 echo ""
53 echo "Commands:"
54 echo " copy: Copy the screenshot data into the clipboard."
55 echo " save: Save the screenshot to a regular file or '-' to pipe to STDOUT."
56 echo " check: Verify if required tools are installed and exit."
57 echo " usage: Show this message and exit."
58 echo ""
59 echo "Targets:"
60 echo " active: Currently active window."
61 echo " screen: All visible outputs."
62 echo " output: Currently active output."
63 echo " area: Manually select a region."
64 echo " window: Manually select a window."
65 exit
66fi
67
68notify() {
69 notify-send -t 3000 -a grimshot "$@"
70}
71notifyOk() {
72 [ "$NOTIFY" = "no" ] && return
73
74 TITLE=${2:-"Screenshot"}
75 MESSAGE=${1:-"OK"}
76 notify "$TITLE" "$MESSAGE"
77}
78notifyError() {
79 if [ $NOTIFY = "yes" ]; then
80 TITLE=${2:-"Screenshot"}
81 MESSAGE=${1:-"Error taking screenshot with grim"}
82 notify -u critical "$TITLE" "$MESSAGE"
83 else
84 echo "$1"
85 fi
86}
87
88die() {
89 MSG=${1:-Bye}
90 notifyError "Error: $MSG"
91 exit 2
92}
93
94check() {
95 COMMAND=$1
96 if command -v "$COMMAND" > /dev/null 2>&1; then
97 RESULT="OK"
98 else
99 RESULT="NOT FOUND"
100 fi
101 echo " $COMMAND: $RESULT"
102}
103
104takeScreenshot() {
105 FILE=$1
106 GEOM=$2
107 OUTPUT=$3
108 if [ -n "$OUTPUT" ]; then
109 grim ${CURSOR:+-c} -o "$OUTPUT" "$FILE" || die "Unable to invoke grim"
110 elif [ -z "$GEOM" ]; then
111 grim ${CURSOR:+-c} "$FILE" || die "Unable to invoke grim"
112 else
113 grim ${CURSOR:+-c} -g "$GEOM" "$FILE" || die "Unable to invoke grim"
114 fi
115}
116
117if [ "$ACTION" = "check" ] ; then
118 echo "Checking if required tools are installed. If something is missing, install it to your system and make it available in PATH..."
119 check grim
120 check slurp
121 check swaymsg
122 check wl-copy
123 check jq
124 check notify-send
125 exit
126elif [ "$SUBJECT" = "area" ] ; then
127 GEOM=$(slurp -d)
128 # Check if user exited slurp without selecting the area
129 if [ -z "$GEOM" ]; then
130 exit 1
131 fi
132 WHAT="Area"
133elif [ "$SUBJECT" = "active" ] ; then
134 FOCUSED=$(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?, .floating_nodes[]?) | select(.focused)')
135 GEOM=$(echo "$FOCUSED" | jq -r '.rect | "\(.x),\(.y) \(.width)x\(.height)"')
136 APP_ID=$(echo "$FOCUSED" | jq -r '.app_id')
137 WHAT="$APP_ID window"
138elif [ "$SUBJECT" = "screen" ] ; then
139 GEOM=""
140 WHAT="Screen"
141elif [ "$SUBJECT" = "output" ] ; then
142 GEOM=""
143 OUTPUT=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused)' | jq -r '.name')
144 WHAT="$OUTPUT"
145elif [ "$SUBJECT" = "window" ] ; then
146 GEOM=$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp)
147 # Check if user exited slurp without selecting the area
148 if [ -z "$GEOM" ]; then
149 exit 1
150 fi
151 WHAT="Window"
152else
153 die "Unknown subject to take a screen shot from" "$SUBJECT"
154fi
155
156if [ "$ACTION" = "copy" ] ; then
157 takeScreenshot - "$GEOM" "$OUTPUT" | wl-copy --type image/png || die "Clipboard error"
158 notifyOk "$WHAT copied to buffer"
159else
160 if takeScreenshot "$FILE" "$GEOM" "$OUTPUT"; then
161 TITLE="Screenshot of $SUBJECT"
162 MESSAGE=$(basename "$FILE")
163 notifyOk "$MESSAGE" "$TITLE"
164 echo "$FILE"
165 else
166 notifyError "Error taking screenshot with grim"
167 fi
168fi