summaryrefslogtreecommitdiff
path: root/.config/niri/apply-colors.sh
blob: 3d3b4336d094cecc72f5151deba947da55a2f078 (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
#!/bin/sh
# Regenerate wallust colors from the current wallpaper and reload running apps.
# Also keeps the niri backdrop in sync with the wallpaper.
#
# This is the single startup entry point: it derives the last used wallpaper
# from awww's own cache (so "main = last used wallpaper"), reads the saved
# palette/threshold from the runtime state file, runs `wallust run` (which
# regenerates EVERY template -> adding a new app only needs a wallust.toml
# template, never a new autostart line), and reloads live apps.

CACHE="${XDG_CACHE_HOME:-$HOME/.cache}"
STATE="$CACHE/wallust_runtime"
WP_DIR="$HOME/Pictures/wallpapers"

# Determine the current wallpaper.
# Primary: the marker file written by theme-switcher.sh (last used wallpaper).
# Fallback: parse awww's cache (the last image set via `awww img`), excluding
# the blurred backdrop layer. awww stores the cache as one concatenated line,
# so we grep for the first absolute image path that is not a backdrop blur.
get_wp() {
    marker="$CACHE/current_wallpaper"
    if [ -f "$marker" ]; then
        wp="$(cat "$marker" 2>/dev/null)"
        [ -n "$wp" ] && [ -f "$wp" ] && { echo "$wp"; return 0; }
    fi
    for f in "$CACHE"/awww/*/*; do
        [ -f "$f" ] || continue
        # awww stores the cache as one concatenated line; the main-namespace
        # image is everything before the first "backdrop" delimiter. Cut there
        # so the greedy path match can't swallow the (blurred) backdrop path.
        wp=$(sed 's/backdrop.*//' "$f" \
            | tr '\0' '\n' \
            | grep -oE '/[^[:space:]]+\.(png|jpg|jpeg|webp|gif)' \
            | head -1)
        [ -n "$wp" ] && [ -f "$wp" ] && { echo "$wp"; return 0; }
    done
    return 1
}

# Read saved palette/threshold/saturation/colorspace from the runtime state.
palette=""; threshold=""; sat=""; cs=""
if [ -f "$STATE" ]; then
    while IFS='=' read -r k v; do
        case "$k" in
            palette)    palette="$v" ;;
            threshold)  threshold="$v" ;;
            saturation) sat="$v" ;;
            colorspace) cs="$v" ;;
        esac
    done < "$STATE"
fi

WP="$(get_wp)" || WP="$(ls -1 "$WP_DIR"/*.* 2>/dev/null | head -1)" || true

if [ -n "$WP" ] && [ -f "$WP" ]; then
    args=""
    [ -n "$palette" ]   && args="$args -p $palette"
    [ -n "$threshold" ] && args="$args -t $threshold"
    [ -n "$sat" ]       && args="$args --saturation $sat"
    [ -n "$cs" ]        && args="$args -c $cs"
    # shellcheck disable=SC2086
    wallust run $args "$WP" || true
    "$HOME/.config/niri/set-backdrop.sh" "$WP" || true
fi

# Reload apps that support live reload.
makoctl reload 2>/dev/null || true

# Refresh niri's own theme (focus ring, etc.) from the freshly generated
# wallust niri template. Optional because niri may still be starting up.
niri msg action reload-config 2>/dev/null || true