Skip to content

Instantly share code, notes, and snippets.

@rif
Created June 22, 2018 20:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rif/01ffb5e3eb45d2e4e6ebb4652b3b19ce to your computer and use it in GitHub Desktop.
Save rif/01ffb5e3eb45d2e4e6ebb4652b3b19ce to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from subprocess import check_output
from re import findall, DOTALL, MULTILINE
inputs = findall(r'.*?index: (\d+).*?sink: (\d+).*?application\.process\.id = "(\d+)"',
check_output(['pacmd', 'list-sink-inputs']).decode("utf-8"), DOTALL|MULTILINE)
sinks = findall(r'index: (\d+)', check_output(['pacmd', 'list-sinks']).decode("utf-8"))
focused_window = check_output(['xdotool', 'getwindowfocus'])
focused_pid = check_output(['xdotool', 'getwindowpid', focused_window]).strip().decode("utf-8")
for app in inputs:
if app[2] == focused_pid:
# find new sink index
index = 0
for i in range(len(sinks)):
if sinks[i] == app[1]:
index = (i + 1) % len(sinks) # rotation
break
check_output(['pacmd', 'move-sink-input', app[0], sinks[index] ])
break
@rif
Copy link
Author

rif commented Jun 22, 2018

requires pacmd and xdotool. I use it assigning a shortcut key in i3wm

@akomakom
Copy link

akomakom commented Apr 19, 2021

I'm trying to modify this to behave intelligently with chrome windows (same PPID, different sinks). This isn't working well because xdotool getwindowpid returns the same pid for all chrome windows, regardless of which one is focused. If multiple chrome windows are currently playing, then things go south.

I added a PPID check and now it does work for chrome, but it can only switch the first window it finds.

If I remove the break at the end, I can at least cycle all chrome windows with one button-press.

Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment