forked from Sebastiaan76/waybar_wireplumber_audio_changer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_changer.py
60 lines (48 loc) · 2.57 KB
/
audio_changer.py
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
#!/usr/bin/env python
import subprocess
# function to parse output of command "wpctl status" and return a dictionary of sinks with their id and name.
def parse_wpctl_status():
# Execute the wpctl status command and store the output in a variable.
output = str(subprocess.check_output("wpctl status", shell=True, encoding='utf-8'))
# remove the ascii tree characters and return a list of lines
lines = output.replace("├", "").replace("─", "").replace("│", "").replace("└", "").splitlines()
# get the index of the Sinks line as a starting point
sinks_index = None
for index, line in enumerate(lines):
if "Sinks:" in line:
sinks_index = index
break
# start by getting the lines after "Sinks:" and before the next blank line and store them in a list
sinks = []
for line in lines[sinks_index +1:]:
if not line.strip():
break
sinks.append(line.strip())
# remove the "[vol:" from the end of the sink name
for index, sink in enumerate(sinks):
sinks[index] = sink.split("[vol:")[0].strip()
# strip the * from the default sink and instead append "- Default" to the end. Looks neater in the wofi list this way.
for index, sink in enumerate(sinks):
if sink.startswith("*"):
sinks[index] = sink.strip().replace("*", "").strip() + " - Default"
# make the dictionary in this format {'sink_id': <int>, 'sink_name': <str>}
sinks_dict = [{"sink_id": int(sink.split(".")[0]), "sink_name": sink.split(".")[1].strip()} for sink in sinks]
return sinks_dict
# get the list of sinks ready to put into wofi - highlight the current default sink
output = ''
sinks = parse_wpctl_status()
for items in sinks:
if items['sink_name'].endswith(" - Default"):
output += f"<b>-> {items['sink_name']}</b>\n"
else:
output += f"{items['sink_name']}\n"
# Call wofi and show the list. take the selected sink name and set it as the default sink
wofi_command = f"echo '{output}' | wofi --show=dmenu --hide-scroll --allow-markup --define=hide_search=true --location=top_right --width=600 --height=200 --xoffset=-60"
wofi_process = subprocess.run(wofi_command, shell=True, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if wofi_process.returncode != 0:
print("User cancelled the operation.")
exit(0)
selected_sink_name = wofi_process.stdout.strip()
sinks = parse_wpctl_status()
selected_sink = next(sink for sink in sinks if sink['sink_name'] == selected_sink_name)
subprocess.run(f"wpctl set-default {selected_sink['sink_id']}", shell=True)