Skip to content

Instantly share code, notes, and snippets.

@rif
Created October 2, 2019 13:32
Show Gist options
  • Save rif/292020ac959b49e0106834a2c1212ca4 to your computer and use it in GitHub Desktop.
Save rif/292020ac959b49e0106834a2c1212ca4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from subprocess import check_output
from json import loads
from re import findall, DOTALL, MULTILINE
def find_focused(tree):
if tree["focused"] == True:
return tree["pid"]
elif 'nodes' in tree:
for n in tree['nodes']:
pid = find_focused(n)
if pid: return pid
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"))
tree = loads(check_output(['swaymsg', '-t', 'get_tree']))
focused_pid = find_focused(tree)
for app in inputs:
if app[2] == str(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment