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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#!/usr/bin/env bash
# ==================================================
# KoolDots (2026)
# Project URL: https://github.com/LinuxBeginnings
# License: GNU GPLv3
# SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Resolve focused-monitor wallpaper and refresh rofi focused wallpaper link.
SCRIPTSDIR="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts"
ROFI_FOCUSED_LINK="${XDG_CONFIG_HOME:-$HOME/.config}/rofi/.current_wallpaper_focused"
ROFI_GLOBAL_LINK="${XDG_CONFIG_HOME:-$HOME/.config}/rofi/.current_wallpaper"
WALLPAPER_CURRENT="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/wallpaper_effects/.wallpaper_current"
# shellcheck source=/dev/null
. "$SCRIPTSDIR/WallpaperCmd.sh" 2>/dev/null || true
read_cached_wallpaper() {
local cache_file="$1"
[[ -f "$cache_file" ]] || return 1
awk 'NF && $0 !~ /^filter/ {print; exit}' "$cache_file"
}
read_wallpaper_from_query() {
local monitor="$1"
[[ -n "$monitor" ]] || return 1
[[ -n "${WWW_CMD:-}" ]] || return 1
command -v "$WWW_CMD" >/dev/null 2>&1 || return 1
"$WWW_CMD" query 2>/dev/null | awk -v mon="$monitor" '
{
line=$0
sub(/^Monitor[[:space:]]+/, "", line)
sub(/^:[[:space:]]*/, "", line)
mon_name=line
sub(/:.*/, "", mon_name)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", mon_name)
if (mon_name != mon) next
path=line
sub(/^.*image:[[:space:]]*/, "", path)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", path)
if (path != line && length(path) > 0) {
print path
exit
}
}
'
}
resolve_link_or_file() {
local path="$1"
local resolved=""
if [[ -L "$path" ]]; then
resolved="$(readlink -f "$path" 2>/dev/null || true)"
if [[ -n "$resolved" && -f "$resolved" ]]; then
printf '%s\n' "$resolved"
return 0
fi
fi
if [[ -f "$path" ]]; then
printf '%s\n' "$path"
return 0
fi
return 1
}
get_active_workspace_monitor() {
command -v hyprctl >/dev/null 2>&1 || return 1
if command -v jq >/dev/null 2>&1; then
hyprctl activeworkspace -j 2>/dev/null | jq -r '.monitor // empty' | head -n1
else
hyprctl monitors 2>/dev/null | awk '/^Monitor/{name=$2} /focused: yes/{print name; exit}'
fi
}
get_focused_monitor() {
command -v hyprctl >/dev/null 2>&1 || return 1
if command -v jq >/dev/null 2>&1; then
hyprctl monitors -j 2>/dev/null | jq -r '.[] | select(.focused) | .name' | head -n1
else
hyprctl monitors 2>/dev/null | awk '/^Monitor/{name=$2} /focused: yes/{print name; exit}'
fi
}
monitor_exists() {
local monitor="$1"
[[ -n "$monitor" ]] || return 1
command -v hyprctl >/dev/null 2>&1 || return 1
if command -v jq >/dev/null 2>&1; then
hyprctl monitors -j 2>/dev/null | jq -r --arg mon "$monitor" '.[] | select(.name == $mon) | .name' | grep -qx "$monitor"
else
hyprctl monitors 2>/dev/null | awk '/^Monitor/{print $2}' | grep -qx "$monitor"
fi
}
resolve_target_monitor() {
local monitor=""
monitor="$(get_active_workspace_monitor 2>/dev/null || true)"
if monitor_exists "$monitor"; then
printf '%s\n' "$monitor"
return 0
fi
monitor="$(get_focused_monitor 2>/dev/null || true)"
if [[ -n "$monitor" ]]; then
printf '%s\n' "$monitor"
return 0
fi
return 1
}
read_wallpaper_from_cache() {
local monitor="$1"
local cache_root="${WWW_CACHE_DIR:-$HOME/.cache/awww}"
local cache_file="$cache_root/$monitor"
local fallback_cache=""
local path=""
case "$cache_root" in
"$HOME/.cache/awww")
fallback_cache="$HOME/.cache/swww/$monitor"
;;
"$HOME/.cache/swww")
fallback_cache="$HOME/.cache/awww/$monitor"
;;
esac
path="$(read_cached_wallpaper "$cache_file" 2>/dev/null || true)"
if [[ -z "$path" && -n "$fallback_cache" ]]; then
path="$(read_cached_wallpaper "$fallback_cache" 2>/dev/null || true)"
fi
[[ -n "$path" && -f "$path" ]] || return 1
printf '%s\n' "$path"
}
resolve_focused_wallpaper() {
local monitor="$1"
local path=""
local per_monitor_link=""
local per_monitor_current=""
if [[ -n "$monitor" ]]; then
per_monitor_link="${XDG_CONFIG_HOME:-$HOME/.config}/rofi/.current_wallpaper_${monitor}"
per_monitor_current="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/wallpaper_effects/.wallpaper_current_${monitor}"
path="$(read_wallpaper_from_query "$monitor" 2>/dev/null || true)"
if [[ -z "$path" ]]; then
path="$(resolve_link_or_file "$per_monitor_link" 2>/dev/null || true)"
fi
if [[ -z "$path" ]]; then
path="$(resolve_link_or_file "$per_monitor_current" 2>/dev/null || true)"
fi
if [[ -z "$path" ]]; then
path="$(read_wallpaper_from_cache "$monitor" 2>/dev/null || true)"
fi
fi
if [[ -z "$path" ]]; then
path="$(resolve_link_or_file "$ROFI_GLOBAL_LINK" 2>/dev/null || true)"
fi
if [[ -z "$path" ]]; then
path="$(resolve_link_or_file "$WALLPAPER_CURRENT" 2>/dev/null || true)"
fi
[[ -n "$path" && -f "$path" ]] || return 1
printf '%s\n' "$path"
}
main() {
local target_monitor=""
local wallpaper_path=""
target_monitor="$(resolve_target_monitor 2>/dev/null || true)"
wallpaper_path="$(resolve_focused_wallpaper "$target_monitor" 2>/dev/null || true)"
[[ -n "$wallpaper_path" && -f "$wallpaper_path" ]] || exit 1
mkdir -p "$(dirname "$ROFI_FOCUSED_LINK")"
ln -sf "$wallpaper_path" "$ROFI_FOCUSED_LINK"
}
main "$@"
|