Skip to content

Instantly share code, notes, and snippets.

@akomakom
Forked from rif/cycle_sinks.py
Last active May 29, 2024 22:29
Show Gist options
  • Select an option

  • Save akomakom/1740e64fe07d9551177797f3c3c80cbe to your computer and use it in GitHub Desktop.

Select an option

Save akomakom/1740e64fe07d9551177797f3c3c80cbe to your computer and use it in GitHub Desktop.

Revisions

  1. akomakom revised this gist Apr 19, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cycle_sinks.py
    Original file line number Diff line number Diff line change
    @@ -28,4 +28,4 @@ def log(s):

    log("Switching app %s to input %s" % (app[0], sinks[index]))
    check_output(['pacmd', 'move-sink-input', app[0], sinks[index] ])
    break
    #break
  2. akomakom revised this gist Apr 19, 2021. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions cycle_sinks.py
    Original file line number Diff line number Diff line change
    @@ -2,20 +2,30 @@

    from subprocess import check_output
    from re import findall, DOTALL, MULTILINE
    import psutil

    def log(s):
    check_output(['logger', '-t', __file__, s])

    inputs = findall(r'.*?index: (\d+).*?sink: (\d+).*?application\.process\.id = "(\d+)"',
    check_output(['pacmd', 'list-sink-inputs']).decode("utf-8"), DOTALL|MULTILINE)
    log("Discovered inputs %s" % inputs)
    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:
    p = psutil.Process(int(app[2]))
    ppid = p.ppid()
    log("Checking if app with pid %s (ppid %s, sink %s) is pid %s \n" % (app[2], ppid, app[0], focused_pid ))
    if app[2] == focused_pid or str(ppid) == 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

    log("Switching app %s to input %s" % (app[0], sinks[index]))
    check_output(['pacmd', 'move-sink-input', app[0], sinks[index] ])
    break
    break
  3. @rif rif created this gist Jun 22, 2018.
    21 changes: 21 additions & 0 deletions cycle_sinks.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/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