-
Notifications
You must be signed in to change notification settings - Fork 19
/
lock
executable file
·113 lines (96 loc) · 3.35 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
105
106
107
108
109
110
111
112
113
#!/bin/bash
#Constants
DISPLAY_RE="([0-9]+)x([0-9]+)\\+([0-9]+)\\+([0-9]+)" # Regex to find display dimensions
PARAMS="-colorspace sRGB" # ensure that images are created in sRGB colorspace, to avoid greyscale output
DEFAULT_CACHE_FOLDER="$HOME"/.cache/i3lock-multimonitor/img/ # Cache folder
# Use XDG cache folder if that variable is set.
if [ -z "$XDG_CACHE_HOME" ]
then
CACHE_FOLDER=$DEFAULT_CACHE_FOLDER
else
CACHE_FOLDER=$XDG_CACHE_HOME
fi
if ! [[ "$CACHE_FOLDER" == "*/" ]]; then
CACHE_FOLDER="$CACHE_FOLDER/"
fi
# Create the cache folder if it does not exist
if ! [ -e $CACHE_FOLDER ]; then
mkdir -p $CACHE_FOLDER
fi
#Passed arguments
while getopts ":i:a:d:" opt; do
case $opt in
i) arg_image="$OPTARG"
;;
a) lock_args="$OPTARG"
;;
d) lock_cmd=":"
arg_image="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2 && exit 1
;;
esac
done
#Image paths
if [ "$arg_image" ]; then
BKG_IMG="$arg_image" # Passed image
elif [ -f "/usr/share/i3lock-multimonitor/img/background.png" ]; then
BKG_IMG="/usr/share/i3lock-multimonitor/img/background.png" # Default image
else
BKG_IMG="$(dirname "$BASH_SOURCE")/img/background.png" # Fallback to current folder
fi
if ! [ -e "$BKG_IMG" ]; then
echo "No background image! Exiting..."
exit 2
fi
MD5_BKG_IMG=$(md5sum $BKG_IMG | cut -c 1-10)
MD5_SCREEN_CONFIG=$(xrandr | md5sum - | cut -c 1-32) # Hash of xrandr output
OUTPUT_IMG="$CACHE_FOLDER""$MD5_SCREEN_CONFIG"."$MD5_BKG_IMG".png # Path of final image
OUTPUT_IMG_WIDTH=0 # Decide size to cover all screens
OUTPUT_IMG_HEIGHT=0 # Decide size to cover all screens
#i3lock command
if [ "$lock_cmd" ]; then
LOCK_BASE_CMD="$lock_cmd"
else
LOCK_BASE_CMD="i3lock -i $OUTPUT_IMG"
fi
if [ "$lock_args" ]; then
LOCK_ARGS="$lock_args" # Passed command
else
LOCK_ARGS="-t -e" # Default
fi
LOCK_CMD="$LOCK_BASE_CMD $LOCK_ARGS"
if [ -e $OUTPUT_IMG ]
then
# Lock screen since image already exists
$LOCK_CMD
exit 0
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:
SCREEN_WIDTH=${BASH_REMATCH[1]}
SCREEN_HEIGHT=${BASH_REMATCH[2]}
SCREEN_X=${BASH_REMATCH[3]}
SCREEN_Y=${BASH_REMATCH[4]}
CACHE_IMG="$CACHE_FOLDER""$SCREEN_WIDTH"x"$SCREEN_HEIGHT"."$MD5_BKG_IMG".png
## if cache for that screensize doesnt exist
if ! [ -e $CACHE_IMG ]
then
# Create image for that screensize
eval convert '$BKG_IMG' '-resize' '${SCREEN_WIDTH}X${SCREEN_HEIGHT}^' '-gravity' 'Center' '-crop' '${SCREEN_WIDTH}X${SCREEN_HEIGHT}+0+0' '+repage' '$CACHE_IMG'
fi
# Decide size of output image
if (( $OUTPUT_IMG_WIDTH < $SCREEN_WIDTH+$SCREEN_X )); then OUTPUT_IMG_WIDTH=$(($SCREEN_WIDTH+$SCREEN_X)); fi;
if (( $OUTPUT_IMG_HEIGHT < $SCREEN_HEIGHT+$SCREEN_Y )); then OUTPUT_IMG_HEIGHT=$(( $SCREEN_HEIGHT+$SCREEN_Y )); fi;
PARAMS="$PARAMS -type TrueColor $CACHE_IMG -geometry +$SCREEN_X+$SCREEN_Y -composite "
fi
done <<<"`xrandr`"
#Execute ImageMagick:
eval convert -size ${OUTPUT_IMG_WIDTH}x${OUTPUT_IMG_HEIGHT} 'xc:black' $OUTPUT_IMG
eval convert $OUTPUT_IMG $PARAMS $OUTPUT_IMG
#Lock the screen:
$LOCK_CMD