forked from jcf/i3lock-fancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock
executable file
·121 lines (99 loc) · 4.66 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
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
# Author: Dolores Portalatin <[email protected]>
# Dependencies: imagemagick, i3lock-color-git, scrot, wmctrl (optional)
set -o errexit -o noclobber -o nounset
HUE=(-level 0%,100%,0.6)
EFFECT=(-filter Gaussian -resize 20% -define filter:sigma=1.5 -resize 500.5%)
# default system sans-serif font
FONT="$(convert -list font | awk "{ a[NR] = \$2 } /family: $(fc-match sans -f "%{family}\n")/ { print a[NR-1]; exit }")"
IMAGE="$(mktemp).png"
SHOT=(import -window root)
DESKTOP=""
OPTIONS="Options:
-h, --help This help menu.
-d, --desktop Attempt to minimize all windows before locking.
-g, --greyscale Set background to greyscale instead of color.
-p, --pixelate Pixelate the background instead of blur, runs faster.
-f <fontname>, --font <fontname> Set a custom font.
-t <text>, --text <text> Set a custom text prompt.
-l, --listfonts Display a list of possible fonts for use with -f/--font.
Note: this option will not lock the screen, it displays
the list and exits immediately.
-- Must be last option. Set command to use for taking a
screenshot. Default is 'import -window root'. Using 'scrot'
or 'maim' will increase script speed and allow setting
custom flags like haing a delay."
# move pipefail down as for some reason "convert -list font" returns 1
set -o pipefail
trap 'rm -f "$IMAGE"' EXIT
TEMP="$(getopt -o :hdpglt:s:f: -l help,pixelate,greyscale,text:,screenshot:,font: --name "$0" -- "$@")"
eval set -- "$TEMP"
# l10n support
TEXT="Type password to unlock"
case "$LANG" in
de_* ) TEXT="Bitte Passwort eingeben" ;; # Deutsch
en_* ) TEXT="Type password to unlock" ;; # English
es_* ) TEXT="Ingrese su contraseña" ;; # Española
fr_* ) TEXT="Entrez votre mot de passe" ;; # Français
it_* ) TEXT="Inserisci la password" ;; # Italian
pl_* ) TEXT="Podaj hasło" ;; # Polish
pt_* ) TEXT="Digite a senha para desbloquear" ;; # Português
ru_* ) TEXT="Введите пароль" ;; # Russian
* ) TEXT="Type password to unlock" ;; # Default to English
esac
while true ; do
case "$1" in
-h|--help)
printf "Usage: $(basename $0) [options]\n\n$OPTIONS\n\n" ; exit 1 ;;
-d|--desktop) DESKTOP=$(which wmctrl) ; shift ;;
-g|--greyscale) HUE=(-level 0%,100%,0.6 -set colorspace Gray -separate -average) ; shift ;;
-p|--pixelate) EFFECT=(-scale 10% -scale 1000%) ; shift ;;
-f|--font)
case "$2" in
"") shift 2 ;;
*) FONT=$2 ; shift 2 ;;
esac ;;
-t|--text) TEXT=$2 ; shift 2 ;;
-l|--listfonts) convert -list font | awk -F: '/Font: / { print $2 }' | sort -du | ${PAGER:-less} ; exit 0 ;;
--) shift; SET=true; break ;;
*) echo "error" ; exit 1 ;;
esac
done
if "$SET" && [[ $# -gt 0 ]]; then
SHOT=("$@");
fi
"${SHOT[@]}" "$IMAGE"
# get path where the script is located to find the lock icon
SCRIPTPATH=$(realpath "$0")
SCRIPTPATH=${SCRIPTPATH%/*}
VALUE="60" #brightness value to compare to
COLOR=$(convert "$IMAGE" -gravity center -crop 100x100+0+0 +repage -colorspace hsb \
-resize 1x1 txt:- | awk -F '[%$]' 'NR==2{gsub(",",""); printf "%.0f\n", $(NF-1)}');
if [ "$COLOR" -gt "$VALUE" ]; then #white background image and black text
BW="black"
ICON="$SCRIPTPATH/icons/lockdark.png"
PARAM=(--textcolor=00000000 --insidecolor=0000001c --ringcolor=0000003e \
--linecolor=00000000 --keyhlcolor=ffffff80 --ringvercolor=ffffff00 \
--separatorcolor=22222260 --insidevercolor=ffffff1c \
--ringwrongcolor=ffffff55 --insidewrongcolor=ffffff1c)
else #black
BW="white"
ICON="$SCRIPTPATH/icons/lock.png"
PARAM=(--textcolor=ffffff00 --insidecolor=ffffff1c --ringcolor=ffffff3e \
--linecolor=ffffff00 --keyhlcolor=00000080 --ringvercolor=00000000 \
--separatorcolor=22222260 --insidevercolor=0000001c \
--ringwrongcolor=00000055 --insidewrongcolor=0000001c)
fi
convert "$IMAGE" "${HUE[@]}" "${EFFECT[@]}" -font "$FONT" -pointsize 26 -fill "$BW" -gravity center \
-annotate +0+160 "$TEXT" "$ICON" -gravity center -composite "$IMAGE"
# If invoked with -d/--desktop, we'll attempt to minimize all windows (ie. show
# the desktop) before locking.
${DESKTOP} ${DESKTOP:+-k on}
# try to use a forked version of i3lock with prepared parameters
if ! i3lock -n "${PARAM[@]}" -i "$IMAGE" > /dev/null 2>&1; then
# We have failed, lets get back to stock one
i3lock -n -i "$IMAGE"
fi
# As above, if we were passed -d/--desktop, we'll attempt to restore all windows
# after unlocking.
${DESKTOP} ${DESKTOP:+-k off}