summaryrefslogtreecommitdiff
path: root/home-config/hypr/wallpaper-daemon.sh
blob: 5f53d339f1533bbeed045e700257a5fab6764bd3 (plain)
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
#!/usr/bin/env bash

# Script initialization
rm /tmp/$USER-desktop-anim
# Check if animated wallpapers exist, set env var if so
# Sleep for swww init to finish
sleep 3
script_init() {
    if [ `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | wc -l` -gt 0 ]; then
        echo "true" > /tmp/$USER-desktop-anim
        ANIM_READY=true
    else
        # Download or bail here?
        echo "no animations found, static only!"
    fi
}
# check if on ac or battery
check_if_ac() {
    ON_AC=`cat /sys/class/power_supply/AC/online`
    if [ $ON_AC -eq 1 ] && [ "$ANIM_READY" = true ]; then
        echo "Anim"
        init_anim
    else
        echo "Static"
        init_static
    fi
}

init_static() {
    # On battery or missing wallpapers, just load a random image
    IMG=`find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
    echo $IMG >> /tmp/$USER-desktop-anim
    swww img $IMG
    # This is hacky but sometimes we get into a race condition where the
    # animated background will load anyways, so this will ensure we stay static
    # until we definitely control swww and its state is known
    sleep 60
    swww img $IMG
}

init_anim() {
    # Preload a static image until the animated one is ready
    swww img --transition-step 255 `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
    sleep 2
    IMG=`find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
    echo $IMG >> /tmp/$USER-desktop-anim
    swww img $IMG
}

begin_randomizer() {
    while :; do
        echo "Invoke selector"
        sleep 3600
        if [ `cat /sys/class/power_supply/AC/online` -eq 1 ] && [ `cat /tmp/$USER-desktop-anim | head -n1` == "true" ]; then
            switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
        else
            switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
        fi
    done
}

switch_check() {
    # Simply query what image is displayed and make sure we do not overwrite
    # it. This is more for animated wallpapers since it causes corruption
    CURRENT_FILE=`swww query | awk -F'/' '{print $NF}'`
    SELECTION=`echo $1 | awk -F'/' '{print $NF}'`

    if ! [ "$CURRENT_FILE" == "$SELECTION" ]; then
        # Update the file
        sed -i '2c'"$1" /tmp/$USER-desktop-anim
        # Set the background!
        swww img $1
    fi
}

script_init
check_if_ac
begin_randomizer &

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)}' |
while read -r line; do
    if [ "$line" == "true" ]; then
        echo "switching to ac"
        switch_check `find ~/.config/hypr/Wallpapers/animated/ -name '*.gif' | sort -R | tail -n1`
    elif [ "$line" == "false" ]; then
        echo "switching to bat"
        switch_check `find ~/.config/hypr/Wallpapers/static/ -type f | sort -R | tail -n1`
    fi
done