595a984d5e9baca3e1769cfea603d5af8ba86bd0
[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 running custom "copying script"
13 #   kbdo $HOME/bin/mycp $HOME/Pictures/snap 4
14 # .. where $HOME/bin/mycp could be a file with the following line:
15 #   cp $3 $1/$(printf %0${2}d.png $(ls $1|wc -l))
16 # .. for saving screenshots by serial numbering instead
17 #
18 # Example for custom copying by inline scripting (note the backslashes):
19 # kbdo 'bash -c "X(){ cp \$1 \$HOME/Pictures; }; X \$1" mycp'
20
21 ARGS="$*"
22 if [ -z "$ARGS" ] ; then
23     PRG=( /usr/bin/xfce4-screenshooter -w )
24 else
25     PRG=( /usr/bin/xfce4-screenshooter -w -o "$ARGS" )
26 fi
27
28 EVL=""
29
30 foo() {
31     echo $1 >&2
32 }
33
34 stdbuf -o0 xxd -c 24 -p < /dev/input/event0 | \
35     stdbuf -o0 sed 's/.\{32\}//;s/\(.\{12\}\).*/\1/' | \
36     while read EV ; do
37         if [ "$EV" != "000000000000" ] ; then
38             EVL+=" $EV"
39             continue
40         fi
41         if [ -z "${EVL%%*010063000100}" ] ; then
42             echo "${PRG[@]}" >&2
43             "${PRG[@]}" &
44         fi
45         EVL=""
46     done