Skip to content

Instantly share code, notes, and snippets.

@sochotnicky
Created September 7, 2021 14:23
Show Gist options
  • Select an option

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

Select an option

Save sochotnicky/b2683c123ac0497184e12719a65cf38e to your computer and use it in GitHub Desktop.
sway_mark_urgent.py
#!/usr/bin/python3
import json
import logging
import subprocess
import sys
import psutil
# 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", '[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