summaryrefslogtreecommitdiffstats
path: root/.local/bin/mixer-set
blob: 9963c91afb9c152825d94b3b8525f6e57c583d97 (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
#!/usr/bin/env perl

use strict;
use warnings;
use Term::ANSIColor;
use Capture::Tiny qw(capture);
use Sys::Hostname qw(hostname);

sub main {
	my $argc = $#ARGV + 1;
	if ($argc == 0) {
		print "Argument needed - ["
			. colored("lower", 'bold')
			. " - "
			. colored("raise", 'bold')
			. " - "
			. colored("toggle", 'bold')
			. "]\n";
		exit 1;
	}
	elsif ($argc > 1) {
		print "Too many arguments, only one needed\n";
		exit 2;
	}
	my $host = hostname();
	my $curr_vol = 0;
	my $curr_vol_cmd = "/usr/sbin/mixer vol | /usr/bin/awk -F ':' '{print \$2}'";
	if ($host eq "po-rbo.ln.ysosecure.com") {
		$curr_vol_cmd = "/usr/bin/pactl get-sink-volume \@DEFAULT_SINK\@ | /usr/bin/awk '{print \$5}'";
	}
	my $ns = '/usr/local/bin/notify-send';
	if (hostname() eq "po-rbo.ln.ysosecure.com") {
		$ns = '/usr/bin/notify-send';
	}
	if ($ARGV[0] eq "lower" || $ARGV[0] eq "raise") {
		if ($ARGV[0] eq "lower") {
			if ($host eq "po-rbo.ln.ysosecure.com") {
				system('/usr/bin/pactl', 'set-sink-volume', '@DEFAULT_SINK@', '-5%');
			}
			else {
				capture {
					system('/usr/sbin/mixer', 'vol', '-5');
				};
			}
		}
		else {
			if ($host eq "po-rbo.ln.ysosecure.com") {
				system('/usr/bin/pactl', 'set-sink-volume', '@DEFAULT_SINK@', '+5%');
			}
			else {
				capture {
					system('/usr/sbin/mixer', 'vol', '+5');
				};
			}
		}
		$curr_vol = `$curr_vol_cmd`;
		chomp $curr_vol;
		system(
			$ns,
			'-h',
			'int:value:' . $curr_vol,
			'-u',
			'low',
			'-t',
			'1000',
			'mixer-set',
			'  volume'
		);
		system('kill -68 $(pidof dwmblocks)');
		exit;
	}
	elsif ($ARGV[0] eq "toggle") {
		my $tmp_file = '/tmp/mixervol';
		$curr_vol = `$curr_vol_cmd`;
		chomp $curr_vol;
		if ($curr_vol > 0) {
			open(my $fh, '>:encoding(UTF-8)', $tmp_file);
			print $fh $curr_vol;
			close($fh);
			if ($host eq "po-rbo.ln.ysosecure.com") {
				system('/usr/bin/pactl', 'set-sink-volume', '@DEFAULT_SINK@', '0%');
			}
			else {
				capture {
					system(
						'/usr/sbin/mixer',
						'vol',
						'0'
					);
				};
			}
			system(
				$ns,
				'-u',
				'low',
				'-t',
				'1750',
				'mixer-set',
				'  muted'
			);
			system('kill -68 $(pidof dwmblocks)');
		}
		else {
			open(my $fh, '<:encoding(UTF-8)', $tmp_file);
			$curr_vol = <$fh>;
			close($fh);
			if ($host eq "po-rbo.ln.ysosecure.com") {
				chomp $curr_vol;
				system('/usr/bin/pactl', 'set-sink-volume', '@DEFAULT_SINK@', $curr_vol);
			}
			else {
				capture {
					system(
						'/usr/sbin/mixer',
						'vol',
						$curr_vol
					);
				};
			}
			system(
				$ns,
				'-h',
				'int:value:' . $curr_vol,
				'-u',
				'low',
				'-t',
				'1750',
				'mixer-set',
				'  restored'
			);
			system('kill -68 $(pidof dwmblocks)');
		}
		exit;
	}
	else {
		print "Unrecognized argument: " . $ARGV[0] . "\n";
	}
	exit;
}

main();

__END__