aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/grimshot
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/grimshot')
-rwxr-xr-xcontrib/grimshot154
1 files changed, 0 insertions, 154 deletions
diff --git a/contrib/grimshot b/contrib/grimshot
deleted file mode 100755
index 461a5eef..00000000
--- a/contrib/grimshot
+++ /dev/null
@@ -1,154 +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:-~/.config}/user-dirs.dirs && \
17 . ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs
18
19 echo ${XDG_SCREENSHOTS_DIR:-${XDG_PICTURES_DIR:-$HOME}}
20}
21
22if [ "$1" = "--notify" ]; then
23 NOTIFY=yes
24 shift 1
25else
26 NOTIFY=no
27fi
28
29ACTION=${1:-usage}
30SUBJECT=${2:-screen}
31FILE=${3:-$(getTargetDirectory)/$(date -Ins).png}
32
33if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "check" ]; then
34 echo "Usage:"
35 echo " grimshot [--notify] (copy|save) [active|screen|output|area|window] [FILE]"
36 echo " grimshot check"
37 echo " grimshot usage"
38 echo ""
39 echo "Commands:"
40 echo " copy: Copy the screenshot data into the clipboard."
41 echo " save: Save the screenshot to a regular file."
42 echo " check: Verify if required tools are installed and exit."
43 echo " usage: Show this message and exit."
44 echo ""
45 echo "Targets:"
46 echo " active: Currently active window."
47 echo " screen: All visible outputs."
48 echo " output: Currently active output."
49 echo " area: Manually select a region."
50 echo " window: Manually select a window."
51 exit
52fi
53
54notify() {
55 notify-send -t 3000 -a grimshot "$@"
56}
57notifyOk() {
58 [ "$NOTIFY" = "no" ] && return
59
60 TITLE=${2:-"Screenshot"}
61 MESSAGE=${1:-"OK"}
62 notify "$TITLE" "$MESSAGE"
63}
64notifyError() {
65 if [ $NOTIFY = "yes" ]; then
66 TITLE=${2:-"Screenshot"}
67 MESSAGE=${1:-"Error taking screenshot with grim"}
68 notify -u critical "$TITLE" "$MESSAGE"
69 else
70 echo $1
71 fi
72}
73
74die() {
75 MSG=${1:-Bye}
76 notifyError "Error: $MSG"
77 exit 2
78}
79
80check() {
81 COMMAND=$1
82 if command -v "$COMMAND" > /dev/null 2>&1; then
83 RESULT="OK"
84 else
85 RESULT="NOT FOUND"
86 fi
87 echo " $COMMAND: $RESULT"
88}
89
90takeScreenshot() {
91 FILE=$1
92 GEOM=$2
93 OUTPUT=$3
94 if [ ! -z "$OUTPUT" ]; then
95 grim -o "$OUTPUT" "$FILE" || die "Unable to invoke grim"
96 elif [ -z "$GEOM" ]; then
97 grim "$FILE" || die "Unable to invoke grim"
98 else
99 grim -g "$GEOM" "$FILE" || die "Unable to invoke grim"
100 fi
101}
102
103if [ "$ACTION" = "check" ] ; then
104 echo "Checking if required tools are installed. If something is missing, install it to your system and make it available in PATH..."
105 check grim
106 check slurp
107 check swaymsg
108 check wl-copy
109 check jq
110 check notify-send
111 exit
112elif [ "$SUBJECT" = "area" ] ; then
113 GEOM=$(slurp -d)
114 # Check if user exited slurp without selecting the area
115 if [ -z "$GEOM" ]; then
116 exit
117 fi
118 WHAT="Area"
119elif [ "$SUBJECT" = "active" ] ; then
120 FOCUSED=$(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?, .floating_nodes[]?) | select(.focused)')
121 GEOM=$(echo "$FOCUSED" | jq -r '.rect | "\(.x),\(.y) \(.width)x\(.height)"')
122 APP_ID=$(echo "$FOCUSED" | jq -r '.app_id')
123 WHAT="$APP_ID window"
124elif [ "$SUBJECT" = "screen" ] ; then
125 GEOM=""
126 WHAT="Screen"
127elif [ "$SUBJECT" = "output" ] ; then
128 GEOM=""
129 OUTPUT=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused)' | jq -r '.name')
130 WHAT="$OUTPUT"
131elif [ "$SUBJECT" = "window" ] ; then
132 GEOM=$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp)
133 # Check if user exited slurp without selecting the area
134 if [ -z "$GEOM" ]; then
135 exit
136 fi
137 WHAT="Window"
138else
139 die "Unknown subject to take a screen shot from" "$SUBJECT"
140fi
141
142if [ "$ACTION" = "copy" ] ; then
143 takeScreenshot - "$GEOM" "$OUTPUT" | wl-copy --type image/png || die "Clipboard error"
144 notifyOk "$WHAT copied to buffer"
145else
146 if takeScreenshot "$FILE" "$GEOM" "$OUTPUT"; then
147 TITLE="Screenshot of $SUBJECT"
148 MESSAGE=$(basename "$FILE")
149 notifyOk "$MESSAGE" "$TITLE"
150 echo $FILE
151 else
152 notifyError "Error taking screenshot with grim"
153 fi
154fi