summaryrefslogtreecommitdiff
path: root/.config/niri/apply-colors.sh
blob: aebf5092bc1a7d55065ccd167574e29f23c8d452 (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
#!/bin/bash
# Apply a wallpaper and generate colors with wallust (16-color palette) + matugen (Material You / M3).
# Usage: apply-colors.sh <image> [light|dark]
# Called by theme-switcher.sh and the waybar random-wallpaper click.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
IMG="${1:-}"
MODE="${2:-auto}"
LOG="${THEME_LOG:-/tmp/theme-switcher.log}"

log() {
  printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >>"$LOG"
}

# Record the exact failing command + line on any error.
trap 'log "apply-colors FAILED (exit $?) in ${BASH_SOURCE[0]}:$LINENO: $BASH_COMMAND"' ERR

if [ -z "$IMG" ] || [ ! -f "$IMG" ]; then
  echo "usage: $0 <image> [light|dark]" >&2
  exit 1
fi

# wait for the awww daemon to be ready (it is spawned next to this script at login)
log "awww img $IMG"
for _ in $(seq 1 10); do
  if awww query >/dev/null 2>&1; then
    break
  fi
  sleep 1
done

# set the wallpaper FIRST and unconditionally — this is the critical, must-work
# step. Color generation below is best-effort: a failure there must never block
# the wallpaper from changing (that coupling is what froze the wallpaper before).
awww img "$IMG" || { log "awww img FAILED ($IMG)"; exit 1; }

# blurred copy for the Overview backdrop (sibling or on-the-fly generation)
"$SCRIPT_DIR/set-backdrop.sh" "$IMG" || log "set-backdrop FAILED ($IMG)"

# wallust: classic 16-color palette (foot, btop, ...)
# fastresize (the config default) is flaky on some images — always use resized.
# Best-effort: a wallust failure must not prevent the wallpaper from sticking.
log "wallust palette=$MODE $IMG"
case "$MODE" in
light|dark) WALLUST_PALETTE=("--palette" "$MODE") ;;
*) WALLUST_PALETTE=() ;;
esac
# wallust's `resized` backend is intermittently flaky on some images; retry a
# few times, but ultimately tolerate failure.
ok=0
for attempt in 1 2 3; do
  if wallust run -b resized "${WALLUST_PALETTE[@]}" "$IMG"; then
    ok=1
    break
  fi
  log "wallust attempt $attempt FAILED ($IMG, mode=$MODE)"
  [ "$attempt" -lt 3 ] && sleep 2
done
if [ "$ok" -ne 1 ]; then
  log "wallust FAILED after 3 attempts ($IMG, mode=$MODE) — continuing without wallust colors"
else
  # foot needs bare RRGGBB, not #RRGGBB — strip the leading '#'
  sed -i 's/#\([0-9a-fA-F]\)/\1/g' ~/.cache/wallust/foot-theme.ini 2>/dev/null || true
fi

# matugen: Material You / M3 colors (niri focus ring, ...)
# --prefer is required for non-interactive runs (multiple source colors).
# Best-effort: a matugen failure (e.g. bad config after an upgrade) must not
# block the wallpaper.
log "matugen mode=$MODE $IMG"
case "$MODE" in
light) matugen image "$IMG" -m "light" --prefer saturation ;;
dark) matugen image "$IMG" -m "dark" --prefer saturation ;;
*) matugen image "$IMG" --prefer saturation ;;
esac || log "matugen FAILED ($IMG, mode=$MODE) — continuing without matugen colors"

# reload consumers
killall -SIGUSR2 waybar 2>/dev/null || true
killall -USR1 foot 2>/dev/null || true
niri msg action load-config-file 2>/dev/null || true
makoctl reload 2>/dev/null || true

log "apply-colors OK ($IMG, $MODE)"