pulseaudio-sink-port-flip (1527B)
1 #!/usr/bin/env python3 2 3 from subprocess import Popen, PIPE 4 5 6 NOTIFICATION_EXPIRATION_TIME = 3000 7 8 9 def get_current_active_port() -> str: 10 pactl_list_sinks = Popen(["pactl", "list", "sinks"], stdout=PIPE) 11 12 output, stderr = pactl_list_sinks.communicate() 13 14 current_active_port = None 15 for line in output.decode().split("\n"): 16 if "Active Port:" in line: 17 current_active_port = line.split(": ")[1].strip() 18 19 return current_active_port 20 21 22 def main() -> None: 23 current_active_port = get_current_active_port() 24 print("Current active port:", current_active_port) 25 26 if current_active_port == "analog-output-headphones": 27 Popen( 28 [ 29 "pactl", 30 "set-sink-port", 31 "alsa_output.pci-0000_00_1f.3.analog-stereo", 32 "analog-output-speaker", 33 ] 34 ).wait() 35 elif current_active_port == "analog-output-speaker": 36 Popen( 37 [ 38 "pactl", 39 "set-sink-port", 40 "alsa_output.pci-0000_00_1f.3.analog-stereo", 41 "analog-output-headphones", 42 ] 43 ).wait() 44 45 current_active_port = get_current_active_port() 46 print("New current active port:", current_active_port) 47 Popen( 48 [ 49 "notify-send", 50 "Pulseaudio Flip", 51 f"--expire-time={NOTIFICATION_EXPIRATION_TIME}", 52 f"New current active port: {current_active_port}", 53 ] 54 ) 55 56 57 if __name__ == "__main__": 58 main()