Skip to content

Instantly share code, notes, and snippets.

@sochotnicky
Created March 27, 2021 10:48
Show Gist options
  • Select an option

  • Save sochotnicky/b970e28c50b1489fe6d8482f33f58fd3 to your computer and use it in GitHub Desktop.

Select an option

Save sochotnicky/b970e28c50b1489fe6d8482f33f58fd3 to your computer and use it in GitHub Desktop.
stdin
#!/usr/bin/python3
import os
import json
import subprocess
import sys
import psutil
import logging
#logging.basicConfig(filename="/tmp/swayurg.log", level=logging.DEBUG)
def find_own_and_focused(tree, term_pid, refs):
if tree['focused']:
refs['focused'] = tree
if 'pid' in tree and tree['pid'] == term_pid:
refs['own_term'] = tree
if 'focused' in refs and 'own_term' in refs:
return
for node in tree['nodes']:
find_own_and_focused(node, term_pid, refs)
if 'focused' in refs and 'own_term' in refs:
return
if __name__ == "__main__":
logging.debug("Getting sway tree to search for focused/own window")
tree = subprocess.run(["swaymsg", "-t", "get_tree"], capture_output=True)
tree = json.loads(tree.stdout)
term_pid = psutil.Process().ppid()
logging.debug("My own terminal pid: %s", term_pid)
refs = {}
find_own_and_focused(tree, term_pid, refs)
logging.debug("References: %s", refs.keys())
if 'own_term' not in refs:
sys.exit(0)
focused = refs['focused'] if 'focused' in refs else {}
own = refs['own_term']
logging.debug("focused id: %s", focused.get('id'))
logging.debug("own id: %", own['id'])
if focused.get("id") != own.get("id"):
cmd = ["swaymsg", f'[pid="{own["pid"]}"]', "mark", "urgent"]
logging.debug(cmd)
subprocess.run(cmd)
cmd = ["swaymsg", f'[con_mark="urgent"]', "urgent", "enable"]
subprocess.run(cmd)
logging.debug(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment