aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/chwp
blob: f2d0d1890467c08507b2c452145df51fdbe7d353 (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
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
#!/bin/sh

main()
{
	# Load custom configuration if available
	if [ -f "$HOME/.local/etc/x/chwp" ]
	then
		log main "Sourcing $HOME/.local/etc/x/chwp"
		. "$HOME/.local/etc/x/chwp"
	fi

	directory=${CHWP_BASEDIR:-$HOME/pictures/wallpapers}
	size=${CHWP_SIZE:-1920x1080}
	timeout=${1:-0}
	walldir="$directory/$size"

	log main "Checking $walldir for wallpapers"

	# Fail early if there's no wallpaper directory
	if [ ! -d "$walldir" ]
	then
		log main "No such directory: $walldir"
		exit 1
	fi

	CACHEFILE="$(mktemp)"

	log main "Using $CACHEFILE as CACHEFILE"

	find "${walldir}" -type f > "$CACHEFILE"

	log main "Found $(wc -l "$CACHEFILE" | awk '{ print $1 }') wallpapers"

	# If no timeout was set, just change it once
	if [ "$timeout" -eq 0 ]
	then
		log main "No timeout set, only updating once"
		set_papes
		exit
	fi

	# Otherwise, change it every so often
	while :
	do
		set_papes
		sleep "${timeout}"
	done
}

set_papes()
{
	log set_papes "Gathering wallpapers"

	pape="$(make_pape)"

	log set_papes "Setting wallpaper to $pape"

	feh --bg-fill "$pape" 2>&1 | log_pipe "feh"

	log set_papes "Cleaning up temporary wallpaper image"

	rm -f -- "$pape"
}

make_pape()
{
	log make_pape "Creating wallpaper"

	output="$(mktemp)"
	imagelist="$(mktemp)"
	monitorlist="$(mktemp)"

	if [ -z "$CHWP_MONITORS" ]
	then
		log make_pape "Trying to determine screens"

		xrandr -q \
			| grep -F ' connected ' \
			| grep -Eo '[[:digit:]]+x[[:digit:]]+' \
			> "$monitorlist"
	else
		log make_pape "Using \$CHWP_MONITORS for monitorlist"

		printf "%s" "$CHWP_MONITORS" \
			| tr " " "\n" \
			| grep -v "^$" \
			> "$monitorlist"
	fi

	log make_pape "Found $(wc -l "$monitorlist" | awk '{ print $1 }') screen(s)"

	while read -r resolution
	do
		log make_pape "Adding wallpaper for $resolution sized screen"

		current_pape=$(get_pape "$resolution")

		if [ "$current_pape" = "" ]
		then
			log make_pape "No wallpapers for $resolution"

			current_pape="$(mktemp --tmpdir XXXXX.png)"
			convert -size "$resolution" canvas:gray "$current_pape"
		else
			log make_pape "Using $current_pape"
		fi

		printf '"%s"\n' "$current_pape" >> "$imagelist"

		unset resolution
		unset current_pape
	done < "$monitorlist"

	# shellcheck disable=SC2046
	# shellcheck disable=SC2086
	log make_pape "Combine all resolutions into a single image"

	eval convert +append $(cat $imagelist) "$output" 2>&1 | log_pipe "convert"

	printf "%s" "$output"

	rm -f -- "$imagelist"
}

get_pape()
{
	if [ ! -d "$directory/$1" ]
	then
		log get_pape "$directory/$1 does not exist"
		return 1
	fi

	printf "%s\n" "$(find "$directory/$1" -type f | shuf | head -n 1)"
}

log()
{
	printf "[%s] %s: %s\n" "$(date +%FT%T)" "$1" "$2" >&2
}

log_pipe()
{
	pipe_log="$(mktemp)"

	cat - > "$pipe_log"

	while read -r line
	do
		log "$1" "$line"
	done < "$pipe_log"

	rm -fr -- "$pipe_log"
	unset pipe_log
}

main "$@"