summaryrefslogtreecommitdiff
path: root/home-manager/scripts/matugen-sync.sh
blob: a9119bfb22bfd4388d5805ff7b12dcba7b8da72a (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
#!/usr/bin/env bash

# We watch the wallpaper state directory; Serpantinum writes monitor-specific
# files like current_DP-2 or current_eDP-1.
STATE_DIR="$HOME/.cache/serpantinum/wallpaper/"

reload_foot_live_colors() {
    local colors_file="$HOME/.cache/matugen/foot-colors.ini"
    local -A colors
    local -A seen_tty
    local key
    local value
    local tty_path
    local pid
    local seq=""

    [ -r "$colors_file" ] || return 0

    while IFS='=' read -r key value; do
        # Skip section headers and malformed lines.
        [[ "$key" == "["* || -z "$key" || -z "$value" ]] && continue
        key="${key//[[:space:]]/}"
        value="${value%% *}"
        value="${value//[[:space:]]/}"
        [[ "$value" =~ ^[0-9A-Fa-f]{6}$ ]] || continue
        colors["$key"]="#$value"
    done < "$colors_file"

    [ -n "${colors[foreground]}" ] && seq+="\e]10;${colors[foreground]}\a"
    [ -n "${colors[background]}" ] && seq+="\e]11;${colors[background]}\a"
    [ -n "${colors[cursor]}" ] && seq+="\e]12;${colors[cursor]}\a"

    for i in 0 1 2 3 4 5 6 7; do
        [ -n "${colors[regular$i]}" ] && seq+="\e]4;$i;${colors[regular$i]}\a"
        [ -n "${colors[bright$i]}" ] && seq+="\e]4;$((i + 8));${colors[bright$i]}\a"
    done

    [ -n "$seq" ] || return 0

    for pid in $(pgrep -x foot 2>/dev/null) $(pgrep -x footclient 2>/dev/null); do
        tty_path=$(readlink "/proc/$pid/fd/0" 2>/dev/null) || continue
        [[ "$tty_path" == /dev/pts/* ]] || continue
        [ -n "${seen_tty[$tty_path]}" ] && continue
        printf '%b' "$seq" > "$tty_path" 2>/dev/null || true
        seen_tty["$tty_path"]=1
    done
}

inotifywait -m -e close_write "$STATE_DIR" |
while read -r directory events filename; do
    # Only react to monitor pointer files like current_DP-2, current_eDP-1,
    # current_HDMI-A-1. Ignore binary/image/json side files.
    if [[ "$filename" =~ ^current_[[:alnum:]]+-[[:alnum:]-]+$ ]]; then
        
        # Give Serpantinum a split second to finish its operations
        sleep 0.5
        
        # Read one line from the monitor pointer file.
        IFS= read -r WP_PATH < "$STATE_DIR$filename"

        # Skip empty, non-absolute, or missing paths.
        [[ -n "$WP_PATH" && "$WP_PATH" = /* && -f "$WP_PATH" ]] || continue
        
        # 1. Run Matugen against the ORIGINAL valid image file
        matugen image "$WP_PATH"
        
        # 2. Reload Hyprland
        hyprctl reload
        
        # 3. Live-apply colors in running Foot terminals.
        reload_foot_live_colors
        
    fi
done