home-config/hypr/wallpaper-daemon.sh

7d887a179c698d64c8f0339f36e8e6c166bb336c · 3.0 KB · 88 lines raw

1 #!/usr/bin/env bash
2
3 # Script initialization
4 # Check if animated wallpapers exist, set env var if so
5 # Sleep for swww init to finish
6 sleep 3
7 script_init() {
8 if [ `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | wc -l` -gt 0 ]; then
9 echo "true" > /tmp/$USER-desktop-anim
10 ANIM_READY=true
11 else
12 # Download or bail here?
13 echo "no animations found, static only!"
14 fi
15 }
16 # check if on ac or battery
17 check_if_ac() {
18 ON_AC=`cat /sys/class/power_supply/AC/online`
19 if [ $ON_AC -eq 1 ] && [ "$ANIM_READY" = true ]; then
20 echo "Anim"
21 init_anim
22 else
23 echo "Static"
24 init_static
25 fi
26 }
27
28 init_static() {
29 # On battery or missing wallpapers, just load a random image
30 IMG=`find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
31 echo $IMG >> /tmp/$USER-desktop-anim
32 swww img $IMG
33 # This is hacky but sometimes we get into a race condition where the
34 # animated background will load anyways, so this will ensure we stay static
35 # until we definitely control swww and its state is known
36 sleep 60
37 swww img $IMG
38 }
39
40 init_anim() {
41 # Preload a static image until the animated one is ready
42 swww img --transition-step 255 `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
43 sleep 2
44 IMG=`find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
45 echo $IMG >> /tmp/$USER-desktop-anim
46 swww img $IMG
47 }
48
49 begin_randomizer() {
50 while :; do
51 echo "Invoke selector"
52 sleep 3600
53 if [ `cat /sys/class/power_supply/AC/online` -eq 1 ] && [ `cat /tmp/$USER-desktop-anim | head -n1` == "true" ]; then
54 switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
55 else
56 switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
57 fi
58 done
59 }
60
61 switch_check() {
62 # Simply query what image is displayed and make sure we do not overwrite
63 # it. This is more for animated wallpapers since it causes corruption
64 CURRENT_FILE=`swww query | awk -F'/' '{print $NF}'`
65 SELECTION=`echo $1 | awk -F'/' '{print $NF}'`
66
67 if ! [ "$CURRENT_FILE" == "$SELECTION" ]; then
68 # Update the file
69 sed -i '2c'"$1" /tmp/$USER-desktop-anim
70 # Set the background!
71 swww img $1
72 fi
73 }
74
75 script_init
76 check_if_ac
77 begin_randomizer &
78
79 dbus-monitor --system "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',sender=':1.2',path='/org/freedesktop/UPower/devices/line_power_AC'" 2>/dev/null | stdbuf -o0 awk -F' ' '/variant boolean/ {print $(NF)}' |
80 while read -r line; do
81 if [ "$line" == "true" ]; then
82 echo "switching to ac"
83 switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
84 elif [ "$line" == "false" ]; then
85 echo "switching to bat"
86 switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
87 fi
88 done