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

# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.

readonly BIN=${OPT_BIN:-/usr/bin/glava}

main()
{
	# Handle opts
	opts "$@"
	shift "$OPTS"
	unset OPTS

	# Show help
	[ "$OPT_HELP_ONLY" ] && usage && exit 0

	# Get geometry
	split_xrandr_output "$(filter_size)"

	width=$GEOMETRY_X
	height=$GEOMETRY_Y

	# Calculate dimensions
	glava_x=$(( ((GEOMETRY_X - width) / 2) + GEOMETRY_OFFSET_X ))
	glava_y=$(( ((GEOMETRY_Y - height) / 2) + GEOMETRY_OFFSET_Y ))

	# Run glava
	"$BIN" \
		--force-mod="${OPT_MOD:-radial}" \
		--audio="${OPT_AUDIO:-pulseaudio}" \
		--request="setgeometry $glava_x $glava_y $width $height" \
		--desktop \
		"$@"
}

opts()
{
	OPTS=0

	while getopts ":b:g:hm:" opt
	do
		case "$opt" in
			a) OPT_AUDIO=$OPTARG ; OPTS=$(( OPTS + 2 )) ;;
			b) OPT_BIN=$OPTARG ; OPTS=$(( OPTS + 1 )) ;;
			g) OPT_GEOMETRY=$OPTARG ; OPTS=$(( OPTS + 2)) ;;
			h) OPT_HELP_ONLY=1 ;;
			m) OPT_MOD=$OPTARG ; OPTS=$(( OPTS + 2 )) ;;
			*)
				printf "Invalid option passed: %s\n" "$OPTARG" >&2
				;;
		esac
	done

	unset opt
}

get_primary_monitor_geometry()
{
	xrandr | grep 'connected primary' | awk '{ print $4 }'
}

split_xrandr_output()
{
	fields=$(printf "%s" "$1" | sed 's/[x+]/ /g')

	if [ $(printf "%s" "$fields" | wc -w) -ne 4 ]
	then
		printf "Invalid size: $1\n" >&2
		exit 3
	fi

	GEOMETRY_X=$(printf "%s" "$fields" | awk '{ print $1 }')
	GEOMETRY_Y=$(printf "%s" "$fields" | awk '{ print $2 }')
	GEOMETRY_OFFSET_X=$(printf "%s" "$fields" | awk '{ print $3 }')
	GEOMETRY_OFFSET_Y=$(printf "%s" "$fields" | awk '{ print $4 }')

	unset fields
}

filter_size()
{
	if [ "$OPT_GEOMETRY" ]
	then
		printf "%s" "$OPT_GEOMETRY"
		return
	fi

	if [ -f "$HOME/.local/etc/glava-xrandr-geometry" ]
	then
		head -n 1 "$HOME/.local/etc/glava-xrandr-geometry"
		return
	fi

	get_primary_monitor_geometry
}

usage()
{
	cat <<EOF
Usage:
	${0##*/} -h

Wrapper script for glava.

Options:
	-a  Set audio source to visualize. Defaults to pulseaudio.
	-b  Set the original glava binary to wrap around. Defaults to
	    /usr/bin/glava.
	-g  Set the geometry of the visualization. This output can be retrieved
	    from xrandr. Defaults to using the full size of your primary
	    monitor.
	-h  Show this help text and exit.
	-m  Set the module to use. Defaults to radial.
EOF
}

main "$@"