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