Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # vim: ai:sw=4:ts=4:fenc=utf-8
- import json
- from subprocess import Popen, DEVNULL, PIPE
- def list_sinks():
- p = Popen(["pactl", "-f", "json", "list", "sinks"], stdout=PIPE)
- r = p.communicate()
- if p.returncode != 0:
- return None
- data = json.loads(r[0])
- return data
- def get_default_sink():
- p = Popen(["pactl", "get-default-sink"], stdout=PIPE)
- r = p.communicate()
- if p.returncode != 0:
- return None
- return r[0].decode().removesuffix("\n")
- def show_dialog(data, default = None):
- cmd = ["kdialog", "--geometry", "640x240", "--radiolist", "Select audio sink"]
- for i in range(len(data)):
- #text = "{} ({})".format(data[i]["description"], data[i]["properties"]["alsa.id"])
- text = data[i]["description"]
- selected = "on" if default is not None and default == data[i]["name"] else "off"
- cmd.extend([
- str(i),
- text,
- selected
- ])
- p = Popen(cmd, stdout=PIPE, stderr=DEVNULL)
- r = p.communicate()
- return int(r[0]) if p.returncode == 0 else None
- def set_sink(name):
- print("set audio sink: {}".format(name))
- Popen(["pactl", "set-default-sink", name])
- def main():
- sinks = list_sinks()
- default = get_default_sink()
- choice = show_dialog(sinks, default)
- if choice is not None:
- set_sink(sinks[choice]["name"])
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement