commenting
[rrq/kbdo.git] / kbdo
1 #!/bin/bash
2 #
3 # Trigger xfce4-screenshooter on PrtSc/SysRq key
4 # Doesn't consume the key!!
5 # Arguments, if given, is a command to use for processing the screenshot,
6 # instead of getting a dialog about it. The capture is added as an
7 # additional final argument, as a file in /tmp.
8 #
9 # Example for simply saving it in the ~/Pictures directory.
10 #   kbdo cp -t $HOME/Pictures
11 #
12 # Example for custom copying by inline scripting (same effect):
13 # kbdo 'bash -c "X(){ cp $1 $HOME/Pictures; }; X $1"' CP
14 #
15 # Note above, that bash needs CP as the $0 for the -c scripting
16 #
17 # Example for post-processing with convert into fix resolution
18 # kbdo 'bash -c "X(){ convert $1 -resize 800x600 ${1#.*}-x.png; }; X $1"' CVT
19 #
20 # Example for running custom script
21 #   kbdo $HOME/bin/mycp arg1 arg2
22 #
23 # Note above, that the temporary snapshot file is added as an additional
24 # last argument ("arg3" as it were); something like /tmp/Screenshot-date.png
25
26 ARGS="$*"
27 if [ -z "$ARGS" ] ; then
28     PRG=( /usr/bin/xfce4-screenshooter -w )
29 else
30     PRG=( /usr/bin/xfce4-screenshooter -w -o "$ARGS" )
31 fi
32
33 EVL=""
34
35 stdbuf -o0 xxd -c 24 -p < /dev/input/event0 | \
36     stdbuf -o0 sed 's/.\{32\}//;s/\(.\{12\}\).*/\1/' | \
37     while read EV ; do
38         if [ "$EV" != "000000000000" ] ; then
39             EVL+=" $EV"
40             continue
41         fi
42         # echo "$EVL" >&2
43         if [ -z "${EVL%%*010063000100}" ] ; then
44             # Act on the [PrtSc/SysRq] key
45             echo "${PRG[@]}" >&2
46             "${PRG[@]}" &
47         fi
48         EVL=""
49     done