blob: f8f2c705baa4dd8e8f14c04f8f0435850941d939 (
plain)
1 #!/usr/bin/env bash
2 #
3 # Performs a standard screen lock, with an extra bit of style. Before locking
4 # the screen, takes a screenshot of the current display and programatically
5 # blurs and desaturates the image, using that for the lock screen background. A
6 # mighty neat effect.
7 #
8 # Requirements: i3-lock imagemagick [scrot] [xbacklight]
9 #
10
11
12 #
13 # Generates the command to execute for taking a screenshot and applying affects
14 # for use as the lockscreen wallpaper. Note that this does not execute the
15 # command. It only generates it.
16 #
17 # @param out_file Path to save the lockscreen wallpaper to
18 #
19 function gen_wallpaper_cmd {
20 # Return early if argument 1 not specified
21 [[ -z ${1} ]] && return
22
23 out_file=${1}
24 tmp_file="/tmp/i3lock$(date '+%s')screen.jpg"
25
26 screenshot_cmd=""
27 if [[ $(which import 2>/dev/null) ]]; then
28 screenshot_cmd="$(which import) -window root ${tmp_file}"
29 elif [[ $(which scrot 2>/dev/null) ]]; then
30 screenshot_cmd="$(which scrot) -d0 ${tmp_file}"
31 fi
32
33 # Generate the wallpaper gen command
34 if [[ ${screenshot_cmd} != "" ]]; then
35 # Generate the lockscreen background/screenshot (blurred and slightly less
36 # saturated)
37 out=${screenshot_cmd}
38 # Add the image blur command
39 out=${out}' && '
40 out=${out}"convert ${tmp_file} -blur 0x4 -modulate 110,50 ${out_file}"
41 # Add command to clean up the original screenshot file
42 out="${out} && "
43 out="${out} rm ${tmp_file}"
44 echo "${out}"
45 fi
46 # Else we echo nothing, indicating no screenshotter was found
47 }
48
49
50 #
51 # Locks the screen using i3lock command. Sets
52 #
53 # @param bg Path to image to be used for the lockscreen wallpaper
54 #
55 function lock_screen {
56 # Return early if argument 1 not specified
57 [[ -z ${1} ]] && return
58
59 bg=${1}
60 dim=0
61 [[ $(which xbacklight) ]] && dim=1
62
63 if [[ ${dim} == 1 ]]; then
64 # Get the previous brightness so we can return to it.
65 prev_bright=$(xbacklight | cut -d '.' -f 1)
66
67 # Dim the screen, backgrounded so it continues fading after lock
68 xbacklight -set 3% -time 2000 -steps 150&
69 fi
70
71 # Lock, but remain in foreground so the unlock processes aren't executed yet
72 i3lock -i "${bg}" -n
73
74 # Resume original brightness
75 [[ ${dim} == 1 ]] && xbacklight -set ${prev_bright}% -time 2000 -steps 150
76 }
77
78
79 #
80 # La fonction primaire
81 #
82 function main {
83 blurry_wallpaper='/tmp/i3lockwall.png'
84 # Get the program path that will screenshot for the background image
85 lock_wallpaper_cmd=$(gen_wallpaper_cmd ${blurry_wallpaper})
86
87 if [[ ${lock_wallpaper_cmd} == "" ]]; then
88 echo "Screenshot command not found (scrot or import). Exiting."
89 exit 1
90 fi
91
92 # Execute in a subshell, or the double amperstands (&&) will fail. Seems I've
93 # discovered a querk of bash.
94 bash -c "${lock_wallpaper_cmd}"
95
96 # Execute the lock screen with the new fancy wallpaper
97 lock_screen ${blurry_wallpaper}
98
99 # Cleanup the original screenshot and blurred counterpart
100 rm "${bg}"
101 }
102
103
104 # Enter the script here
105 main ${ARGV}
|