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

use strict;
use warnings;
use Term::ANSIColor;

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 $curr_vol = 0;
	my $curr_vol_cmd = "/usr/sbin/mixer vol | /usr/bin/awk -F ':' '{print \$2}'";
	if ($ARGV[0] eq "lower") {
		system('/usr/local/bin/dash',
			   '-c',
			   '/usr/sbin/mixer vol -5 >/dev/null 2>&1');
		$curr_vol = `$curr_vol_cmd`;
		chomp $curr_vol;
		system('/usr/local/bin/dash',
			   '-c',
			   '/usr/local/bin/notify-send -u low -t 400 "Volume - ' . $curr_vol . '%"');
		exit;
	}
	elsif ($ARGV[0] eq "raise") {
		system('/usr/local/bin/dash',
			   '-c',
			   '/usr/sbin/mixer vol +5 >/dev/null 2>&1');
		$curr_vol = `$curr_vol_cmd`;
		chomp $curr_vol;
		system('/usr/local/bin/dash',
			   '-c',
			   '/usr/local/bin/notify-send -u low -t 400 "Volume - ' . $curr_vol . '%"');
		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);
			system('/usr/local/bin/dash',
				   '-c',
				   '/usr/sbin/mixer vol 0 >/dev/null 2>&1');
			system('/usr/local/bin/dash',
				   '-c',
				   '/usr/local/bin/notify-send -u low -t 1750 "Volume muted"');
		}
		else {
			open(my $fh, '<:encoding(UTF-8)', $tmp_file);
			$curr_vol = <$fh>;
			close($fh);
			system('/usr/local/bin/dash',
				   '-c',
				   '/usr/sbin/mixer vol ' . $curr_vol . ' >/dev/null 2>&1');
			system('/usr/local/bin/dash',
				   '-c',
				   '/usr/local/bin/notify-send -u low -t 1750 "Volume restored - ' . $curr_vol . '%"');
		}
		exit;
	}
	else {
		print "Unrecognized argument: " . $ARGV[0] . "\n";
	}
	exit;
}

main();

__END__