-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappvol
executable file
·96 lines (72 loc) · 2.26 KB
/
appvol
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
#!/usr/bin/python
import subprocess
import sys
import re
def usage():
print("Usage: appvol.py m|int")
print("m - toggles mute for input sink")
print("int - percentage to set volume")
exit(0)
def toggle_mute(id, name, stat):
if subprocess.call(
["pactl", "set-sink-input-mute", id, "toggle"]) == 0:
if(stat == "yes"):
print("%s was unmuted" % name)
else:
print("%s was muted" % name)
exit(0)
else:
print("Error occured")
exit(1)
def set_volume(id, vol, name):
if subprocess.call(
["pactl", "set-sink-input-volume", id, vol + "%"]) == 0:
print("Volume for %s was set to %s%%" % (name, vol))
exit(0)
else:
print("Error occured")
exit(1)
def main():
arg = ""
if len(sys.argv) == 2:
arg = sys.argv[1]
else:
usage()
if(not arg.isdigit() and not arg == "m" and not arg == "l"):
usage()
res = subprocess.check_output(["pacmd", "list-sink-inputs"])
res = res.decode("utf-8")
indexes = re.findall(r"index:\s+(\d+)", res)
volumes = re.findall(r"volume:.+/\s+(\d?\d?\d)%", res)
app_names = re.findall(r"application.name\s+=\s+\"(.+)\"", res)
muted = re.findall(r"muted:\s+(yes|no)", res)
if(len(indexes) == 0):
print("No input sinks present")
exit(0)
if(arg == "l"):
for i, item in enumerate(app_names):
name = "%s) %s - %s%%" % (i+1, item, volumes[i])
if(muted[i] == "yes"):
name += " MUTED"
print(name)
exit(0)
if(len(indexes) == 1):
if arg == "m":
toggle_mute(indexes[0], app_names[0], muted[0])
set_volume(indexes[0], arg, app_names[0])
for i, item in enumerate(app_names):
print("%s) %s" % (i+1, item))
try:
choice = input("Select an input sink: ")
except KeyboardInterrupt:
print("\nProgram was interrupted")
exit(0)
list_index = int(choice) - 1
choice_id = indexes[list_index]
app_name = app_names[list_index]
muted_stat = muted[list_index]
if arg == "m":
toggle_mute(choice_id, app_name, muted_stat)
set_volume(choice_id, arg, app_name)
if __name__ == '__main__':
main()