home-config/hypr/wallpaper-daemon-animated-old.sh
2be2139465e3f142905b368ad0b085e2ca5c8da6
· 3.7 KB · 106 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 | check_load() { |
| 76 | # If system is under stress, disable animated wallpaper. If not, allow them |
| 77 | while :; do |
| 78 | sleep 60 |
| 79 | ANIM_ENABLE=`cat /tmp/$USER-desktop-anim` |
| 80 | LOAD=`cat /proc/loadavg | cut -d' ' -f1` |
| 81 | |
| 82 | if [ "$ANIM_ENABLE" == "true" ] && [ $LOAD -gt 8.00 ] && [ $ANIM_READY = true ]; then |
| 83 | sed -i '1c\false' /tmp/$USER-desktop-anim |
| 84 | switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1` |
| 85 | elif [ "$ANIM_ENABLE" == "false" ] && [ $LOAD -lt 8.00 ] && [ $ANIM_READY = true ]; then |
| 86 | sed -i '1c\true' /tmp/$USER-desktop-anim |
| 87 | switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1` |
| 88 | fi |
| 89 | done |
| 90 | } |
| 91 | |
| 92 | script_init |
| 93 | check_if_ac |
| 94 | begin_randomizer & |
| 95 | check_load & |
| 96 | |
| 97 | 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)}' | |
| 98 | while read -r line; do |
| 99 | if [ "$line" == "true" ]; then |
| 100 | echo "switching to ac" |
| 101 | switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1` |
| 102 | elif [ "$line" == "false" ]; then |
| 103 | echo "switching to bat" |
| 104 | switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1` |
| 105 | fi |
| 106 | done |