summaryrefslogtreecommitdiff
path: root/gui-wm/wayfire/files/wayfire-background-cycle
blob: e7d6a904f4ebd3ea49c80cd2b8f638840289ca84 (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
#!/bin/sh
# Switch the live sky behind the desktop: off, Hyperspace, Nebula, Digital Rain.
#
# Usage: wayfire-background-cycle [next|off|hyperspace|nebula|rain]
#
# Edits [live-wallpaper] enabled and effect in the user's wayfire.ini, which
# Wayfire reloads by itself, so the choice applies at once and stays after the
# next login. "off" shows the generated background again. Bound to Super+Ctrl+W
# in the shipped wayfire.ini.

config="${WAYFIRE_CONFIG_FILE:-${XDG_CONFIG_HOME:-$HOME/.config}/wayfire.ini}"

current=$(awk '
	/^[ \t]*\[.*\][ \t]*$/ { s = $0; gsub(/^[ \t]*\[|\][ \t]*$/, "", s); in_lw = (s == "live-wallpaper"); next }
	in_lw && /^[ \t]*enabled[ \t]*=/ { v = $0; sub(/^[^=]*=[ \t]*/, "", v); sub(/[ \t\r]*$/, "", v); enabled = tolower(v) }
	in_lw && /^[ \t]*effect[ \t]*=/ { v = $0; sub(/^[^=]*=[ \t]*/, "", v); sub(/[ \t\r]*$/, "", v); effect = v }
	END { if (enabled == "true" || enabled == "1") print (effect == "" ? "hyperspace" : effect); else print "off" }
' "$config") || exit 1

case "${1:-next}" in
	next)
		case "$current" in
			off) choice=hyperspace ;;
			hyperspace) choice=nebula ;;
			nebula) choice=rain ;;
			rain) choice=off ;;
			*) choice=hyperspace ;;
		esac ;;
	off|hyperspace|nebula|rain) choice=$1 ;;
	*) sed -n '2,9s/^# \{0,1\}//p' "$0" >&2; exit 2 ;;
esac
enabled=true
[ "$choice" = off ] && enabled=false

updated=$(awk -v enabled="$enabled" -v effect="$choice" '
	function add() {
		if (!has_enabled) print "enabled = " enabled
		if (!has_effect && effect != "off") print "effect = " effect
		has_enabled = has_effect = 1
	}
	/^[ \t]*\[.*\][ \t]*$/ {
		if (in_lw) add()
		s = $0; gsub(/^[ \t]*\[|\][ \t]*$/, "", s); in_lw = (s == "live-wallpaper"); if (in_lw) found = 1
		print; next
	}
	in_lw && /^[ \t]*enabled[ \t]*=/ { print "enabled = " enabled; has_enabled = 1; next }
	in_lw && /^[ \t]*effect[ \t]*=/ { print (effect == "off" ? $0 : "effect = " effect); has_effect = 1; next }
	{ print }
	END { if (in_lw) add(); else if (!found) { print ""; print "[live-wallpaper]"; add() } }
' "$config") || exit 1
[ -n "$updated" ] || exit 1
# Written in place: Wayfire watches this file, also when it is a symlink.
printf '%s\n' "$updated" > "$config" || exit 1

case "$choice" in
	off) name="Generated background" ;;
	hyperspace) name="Hyperspace" ;;
	nebula) name="Nebula" ;;
	rain) name="Digital Rain" ;;
esac
command -v notify-send > /dev/null &&
	notify-send -a Background -t 2500 "$name" "Super+Ctrl+W for the next one." 2> /dev/null
exit 0