-
Notifications
You must be signed in to change notification settings - Fork 33
/
lock
executable file
·104 lines (90 loc) · 2.48 KB
/
lock
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
#!/bin/bash
# Defaults
DISPLAY_RE="([0-9]+)x([0-9]+)\\+([0-9]+)\\+([0-9]+)"
IMAGE_RE="([0-9]+)x([0-9]+)"
FOLDER="$(dirname "$(readlink -f "$0")")"
LOCK="$FOLDER/lock.png"
TEXT="$FOLDER/text.png"
PARAMS=""
OUTPUT_IMAGE="/tmp/i3lock.png"
DISPLAY_TEXT=true
PIXELATE=false
BLURTYPE="1x1"
# Read user input
POSITIONAL=()
for i in "$@"
do
case $i in
-h|--help)
echo "lock: Syntax: lock [-n|--no-text] [-p|--pixelate] [-b=VAL|--blur=VAL]"
echo "for correct blur values, read: http://www.imagemagick.org/Usage/blur/#blur_args"
exit
shift
;;
-b=*|--blur=*)
VAL="${i#*=}"
BLURTYPE=(${VAL//=/ })
shift
;;
-n|--no-text)
DISPLAY_TEXT=false
shift # past argument
;;
-p|--pixelate)
PIXELATE=true
shift # past argument
;;
*) # unknown option
echo "unknown option: $i"
exit
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
#Take screenshot:
scrot -z $OUTPUT_IMAGE
#Get dimensions of the lock image:
LOCK_IMAGE_INFO=`identify $LOCK`
[[ $LOCK_IMAGE_INFO =~ $IMAGE_RE ]]
IMAGE_WIDTH=${BASH_REMATCH[1]}
IMAGE_HEIGHT=${BASH_REMATCH[2]}
if $DISPLAY_TEXT ; then
#Get dimensions of the text image:
TEXT_IMAGE_INFO=`identify $TEXT`
[[ $TEXT_IMAGE_INFO =~ $IMAGE_RE ]]
TEXT_WIDTH=${BASH_REMATCH[1]}
TEXT_HEIGHT=${BASH_REMATCH[2]}
fi
#Execute xrandr to get information about the monitors:
while read LINE
do
#If we are reading the line that contains the position information:
if [[ $LINE =~ $DISPLAY_RE ]]; then
#Extract information and append some parameters to the ones that will be given to ImageMagick:
WIDTH=${BASH_REMATCH[1]}
HEIGHT=${BASH_REMATCH[2]}
X=${BASH_REMATCH[3]}
Y=${BASH_REMATCH[4]}
POS_X=$(($X+$WIDTH/2-$IMAGE_WIDTH/2))
POS_Y=$(($Y+$HEIGHT/2-$IMAGE_HEIGHT/2))
PARAMS="$PARAMS '$LOCK' '-geometry' '+$POS_X+$POS_Y' '-composite'"
if $DISPLAY_TEXT ; then
TEXT_X=$(($X+$WIDTH/2-$TEXT_WIDTH/2))
TEXT_Y=$(($Y+$HEIGHT/2-$TEXT_HEIGHT/2+200))
PARAMS="$PARAMS '$TEXT' '-geometry' '+$TEXT_X+$TEXT_Y' '-composite'"
fi
fi
done <<<"`xrandr`"
#Execute ImageMagick:
if $PIXELATE ; then
PARAMS="'$OUTPUT_IMAGE' '-scale' '10%' '-scale' '1000%' $PARAMS '$OUTPUT_IMAGE'"
else
PARAMS="'$OUTPUT_IMAGE' '-level' '0%,100%,0.6' '-blur' '$BLURTYPE' $PARAMS '$OUTPUT_IMAGE'"
fi
eval convert $PARAMS
#Lock the screen:
i3lock -i $OUTPUT_IMAGE -t
#Remove the generated image:
rm $OUTPUT_IMAGE