-
Notifications
You must be signed in to change notification settings - Fork 0
/
grimblast.sh
executable file
·275 lines (248 loc) · 7.59 KB
/
grimblast.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env bash
## Grimblast: a helper for screenshots within hyprland
## Requirements:
## - `grim`: screenshot utility for wayland
## - `slurp`: to select an area
## - `hyprctl`: to read properties of current window (provided by Hyprland)
## - `hyprpicker`: to freeze the screen when selecting area
## - `wl-copy`: clipboard utility (provided by wl-clipboard)
## - `jq`: json utility to parse hyprctl output
## - `notify-send`: to show notifications (provided by libnotify)
## Those are needed to be installed, if unsure, run `grimblast check`
##
## See `man 1 grimblast` or `grimblast usage` for further details.
## Author: Misterio (https://github.com/misterio77)
## This tool is based on grimshot, with swaymsg commands replaced by their
## hyprctl equivalents.
## https://github.com/swaywm/sway/blob/master/contrib/grimshot
getTargetDirectory() {
test -f "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs" &&
. "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
echo "${XDG_SCREENSHOTS_DIR:-${XDG_PICTURES_DIR:-$HOME}}"
}
tmp_editor_directory() {
echo "/tmp"
}
#Detect if $GRIMBLAST_EDITOR env exist
env_editor_confirm() {
if [ -n "$GRIMBLAST_EDITOR" ]; then
echo "GRIMBLAST_EDITOR is set. Continuing..."
else
echo "GRIMBLAST_EDITOR is not set. Defaulting to gimp"
GRIMBLAST_EDITOR=gimp
fi
}
NOTIFY=no
CURSOR=
FREEZE=
WAIT=no
SCALE=
HYPRPICKER_PID=-1
while [ $# -gt 0 ]; do
key="$1"
case $key in
-n | --notify)
NOTIFY=yes
shift # past argument
;;
-c | --cursor)
CURSOR=yes
shift # past argument
;;
-f | --freeze)
FREEZE=yes
shift # past argument
;;
-w | --wait)
shift
WAIT=$1
if echo "$WAIT" | grep "[^0-9]" -q; then
echo "Invalid value for wait '$WAIT'" >&2
exit 3
fi
shift
;;
-s | --scale)
shift # past argument
if [ $# -gt 0 ]; then
SCALE="$1" # assign the next argument to SCALE
shift # past argument
else
echo "Error: Missing argument for --scale option."
exit 1
fi
;;
*) # unknown option
break # done with parsing --flags
;;
esac
done
ACTION=${1:-usage}
SUBJECT=${2:-screen}
FILE=${3:-$(getTargetDirectory)/$(date -Ins).png}
FILE_EDITOR=${3:-$(tmp_editor_directory)/$(date -Ins).png}
if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "edit" ] && [ "$ACTION" != "copysave" ] && [ "$ACTION" != "check" ]; then
echo "Usage:"
echo " grimblast [--notify] [--cursor] [--freeze] [--wait N] [--scale <scale>] (copy|save|copysave|edit) [active|screen|output|area] [FILE|-]"
echo " grimblast check"
echo " grimblast usage"
echo ""
echo "Commands:"
echo " copy: Copy the screenshot data into the clipboard."
echo " save: Save the screenshot to a regular file or '-' to pipe to STDOUT."
echo " copysave: Combine the previous 2 options."
echo " edit: Open screenshot in the image editor of your choice (default is gimp). See man page for info."
echo " check: Verify if required tools are installed and exit."
echo " usage: Show this message and exit."
echo ""
echo "Targets:"
echo " active: Currently active window."
echo " screen: All visible outputs."
echo " output: Currently active output."
echo " area: Manually select a region or window."
exit
fi
notify() {
notify-send -t 3000 -a grimblast "$@"
}
notifyOk() {
[ "$NOTIFY" = "no" ] && return
notify "$@"
}
notifyError() {
if [ $NOTIFY = "yes" ]; then
TITLE=${2:-"Screenshot"}
MESSAGE=${1:-"Error taking screenshot with grim"}
notify -u critical "$TITLE" "$MESSAGE"
else
echo "$1"
fi
}
resetFade() {
if [[ -n $FADELAYERS ]]; then
hyprctl keyword animation "$FADELAYERS" >/dev/null
fi
}
killHyprpicker() {
if [ ! $HYPRPICKER_PID -eq -1 ]; then
kill $HYPRPICKER_PID
fi
}
die() {
killHyprpicker
MSG=${1:-Bye}
notifyError "Error: $MSG"
exit 2
}
check() {
COMMAND=$1
if command -v "$COMMAND" >/dev/null 2>&1; then
RESULT="OK"
else
RESULT="NOT FOUND"
fi
echo " $COMMAND: $RESULT"
}
takeScreenshot() {
FILE=$1
GEOM=$2
OUTPUT=$3
if [ -n "$OUTPUT" ]; then
grim ${CURSOR:+-c} ${SCALE:+-s "$SCALE"} -o "$OUTPUT" "$FILE" || die "Unable to invoke grim"
elif [ -z "$GEOM" ]; then
grim ${CURSOR:+-c} ${SCALE:+-s "$SCALE"} "$FILE" || die "Unable to invoke grim"
else
grim ${CURSOR:+-c} ${SCALE:+-s "$SCALE"} -g "$GEOM" "$FILE" || die "Unable to invoke grim"
resetFade
fi
}
wait() {
if [ "$WAIT" != "no" ]; then
sleep "$WAIT"
fi
}
if [ "$ACTION" = "check" ]; then
echo "Checking if required tools are installed. If something is missing, install it to your system and make it available in PATH..."
check grim
check slurp
check hyprctl
check hyprpicker
check wl-copy
check jq
check notify-send
exit
elif [ "$SUBJECT" = "active" ]; then
wait
FOCUSED=$(hyprctl activewindow -j)
GEOM=$(echo "$FOCUSED" | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"')
APP_ID=$(echo "$FOCUSED" | jq -r '.class')
WHAT="$APP_ID window"
elif [ "$SUBJECT" = "screen" ]; then
wait
GEOM=""
WHAT="Screen"
elif [ "$SUBJECT" = "output" ]; then
wait
GEOM=""
OUTPUT=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)' | jq -r '.name')
WHAT="$OUTPUT"
elif [ "$SUBJECT" = "area" ]; then
if [ "$FREEZE" = "yes" ] && [ "$(command -v "hyprpicker")" ] >/dev/null 2>&1; then
hyprpicker -r -z &
sleep 0.2
HYPRPICKER_PID=$!
fi
# get fade & fadeOut animation and unset it
# this removes the black border seen around screenshots
FADELAYERS="$(hyprctl -j animations | jq -jr '.[0][] | select(.name == "fadeLayers") | .name, ",", (if .enabled == true then "1" else "0" end), ",", (.speed|floor), ",", .bezier')"
hyprctl keyword animation 'fadeLayers,0,1,default' >/dev/null
WORKSPACES="$(hyprctl monitors -j | jq -r '[(foreach .[] as $monitor (0; if $monitor.specialWorkspace.name == "" then $monitor.activeWorkspace else $monitor.specialWorkspace end)).id]')"
WINDOWS="$(hyprctl clients -j | jq -r --argjson workspaces "$WORKSPACES" 'map(select([.workspace.id] | inside($workspaces)))')"
# shellcheck disable=2086 # if we don't split, spaces mess up slurp
GEOM=$(echo "$WINDOWS" | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | slurp $SLURP_ARGS)
# Check if user exited slurp without selecting the area
if [ -z "$GEOM" ]; then
killHyprpicker
resetFade
exit 1
fi
WHAT="Area"
wait
elif [ "$SUBJECT" = "window" ]; then
die "Subject 'window' is now included in 'area'"
else
die "Unknown subject to take a screen shot from" "$SUBJECT"
fi
if [ "$ACTION" = "copy" ]; then
takeScreenshot - "$GEOM" "$OUTPUT" | wl-copy --type image/png || die "Clipboard error"
notifyOk "$WHAT copied to buffer"
elif [ "$ACTION" = "save" ]; then
if takeScreenshot "$FILE" "$GEOM" "$OUTPUT"; then
TITLE="Screenshot of $SUBJECT"
MESSAGE=$(basename "$FILE")
notifyOk "$TITLE" "$MESSAGE" -i "$FILE"
echo "$FILE"
else
notifyError "Error taking screenshot with grim"
fi
elif [ "$ACTION" = "edit" ]; then
env_editor_confirm
if takeScreenshot "$FILE_EDITOR" "$GEOM" "$OUTPUT"; then
TITLE="Screenshot of $SUBJECT"
MESSAGE="Open screenshot in image editor"
notifyOk "$TITLE" "$MESSAGE" -i "$FILE_EDITOR"
$GRIMBLAST_EDITOR "$FILE_EDITOR"
echo "$FILE_EDITOR"
else
notifyError "Error taking screenshot"
fi
else
if [ "$ACTION" = "copysave" ]; then
takeScreenshot - "$GEOM" "$OUTPUT" | tee "$FILE" | wl-copy --type image/png || die "Clipboard error"
notifyOk "$WHAT copied to buffer and saved to $FILE" -i "$FILE"
echo "$FILE"
else
notifyError "Error taking screenshot with grim"
fi
fi
killHyprpicker