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

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

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

	# Fail early if there's no wallpaper directory
	if [ ! -d "$walldir" ]
	then
		printf "No such directory: %s\n" "$walldir" >&2
		exit 1
	fi

	CACHEFILE="$(mktemp)"

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

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

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

set_papes()
{
	pape="$(make_pape)"

	feh --bg-fill "$pape"
	rm -f -- "$pape"
}

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

	xrandr -q | grep -F ' connected ' > "$monitorlist"

	while read -r monitor
	do
		if [ -f "$HOME/.local/etc/x/chwp-filter" ]
		then
			if printf "%s" "$monitor" | grep -qf "$HOME/.local/etc/x/chwp-filter"
			then
				printf "Filtering %s\n" "$monitor" >&2
				continue
			fi
		fi

		resolution="$(printf "%s" "$monitor" | grep -Eo '\b[[:digit:]]+x[[:digit:]]+')"
		current_pape=$(get_pape "$resolution")

		if [ "$current_pape" = "" ]
		then
			printf "No wallpapers for %s\n" "$resolution" >&2

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

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

		unset resolution
		unset current_pape
	done < "$monitorlist"

	# shellcheck disable=SC2046
	# shellcheck disable=SC2086
	eval convert +append $(cat $imagelist) "$output"

	printf "%s" "$output"

	rm -f -- "$imagelist"
}

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

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

main "$@"