Files
antidrift/antidrift/xwindow.py
2022-06-25 19:24:06 -04:00

47 lines
1.3 KiB
Python

import os
import re
import subprocess
import logging
class XWindow:
def __init__(self):
self.window = self._run(["getactivewindow"])
if not self.window:
self.name = ""
self.cls = ""
self.pid = ""
else:
self.name = self._run(["getwindowname", self.window])
self.cls = self._run(["getwindowclassname", self.window])
self.pid = self._run(["getwindowpid", self.window])
self.keywords = list(re.findall(r"\w+", self.name.lower()))
def _run(self, cmd) -> str:
cmd = ["xdotool"] + cmd
p = subprocess.run(cmd, capture_output=True)
if p.returncode != 0:
return ""
return p.stdout.decode().strip()
def minimize(self):
self._run(["windowminimize", self.window])
def quit(self):
self._run(["windowquit", self.window])
def notify(message: str) -> None:
""" Notify user via the Xorg notify-send command. """
logging.debug(f"notify: {message}")
env = {
**os.environ,
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus"
}
user = env.get("SUDO_USER", None)
if user is None:
cmd = ["notify-send", message]
else:
cmd = ["runuser", "-m", "-u", user, "notify-send", message]
subprocess.run(cmd, env=env)