wallpaper-daemon: I hate threading

This commit is contained in:
2026-01-08 19:22:09 +01:00
parent 54031fe915
commit 864a89ba4e
2 changed files with 29 additions and 12 deletions
+23 -11
View File
@@ -25,20 +25,20 @@ BLURRED_WALLPAPER_DIR = Path("~/.local/share/wallpaper/blurred").expanduser()
def getFirstFile(dir: Path, pattern: str = "*") -> Path | None:
'''`find $dir -type f | head -n 1`'''
"""`find $dir -type f | head -n 1`"""
return next(dir.glob(pattern), None)
def getNiriSocket():
return environ['NIRI_SOCKET']
return environ["NIRI_SOCKET"]
def _log(msg: str):
# print(msg)
# logFIle = Path("/tmp/niri-autoblur.log")
# logFile = Path("/tmp/niri-autoblur.log")
# try:
# with logFIle.open("a") as f:
# with logFile.open("a") as f:
# f.write(msg + "\n")
# except Exception:
# pass
@@ -58,7 +58,7 @@ def swwwLoadImg(namespace: str, wallpaper: Path):
"--transition-duration",
"0.5",
]
_log(f"[SWWW] {" ".join(cmd)}")
_log(f"[SWWW] {' '.join(cmd)}")
ret = 0
try:
ret = subprocess.run(cmd, check=True).returncode
@@ -90,7 +90,11 @@ def swwwStartDaemon(namespace: str):
pass
try:
subprocess.Popen(["swww-daemon", "-n", namespace], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.Popen(
["swww-daemon", "-n", namespace],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
_log(f"[SWWW] daemon started for namespace: {namespace}")
return True
except Exception as e:
@@ -163,7 +167,7 @@ class AutoBlur:
@staticmethod
def initIsBlurred() -> bool:
'''[ $(niri msg focused-window | wc -l) -gt 1 ]'''
"""[ $(niri msg focused-window | wc -l) -gt 1 ]"""
cmd = ["niri", "msg", "focused-window"]
try:
output = subprocess.check_output(cmd, text=True)
@@ -192,15 +196,21 @@ class AutoBlur:
self._thread.start()
def _run(self) -> None:
'''Wait until wallpapers are ready & apply the correct one according to the current state'''
"""Wait until wallpapers are ready & apply the correct one according to the current state"""
while True:
if self._isBlurred.is_set():
setBlurred = self._isBlurred.is_set()
if setBlurred:
wallpaper = getFirstFile(self._blurredDir)
else:
wallpaper = getFirstFile(self._normalDir)
if wallpaper is not None and wallpaper.exists():
if self._apply(wallpaper):
success = self._apply(wallpaper)
if setBlurred != self._isBlurred.is_set():
# State changed during apply, loop again immediately
continue
if success:
# Applied successfully
break
sleep(self._interval)
@@ -254,7 +264,9 @@ def handleEvent(event_name, payload):
def printEvent(eventName, payload):
_log(f"[EventHandler] event: {eventName}, payload:\n{json.dumps(payload, indent=2, ensure_ascii=False)}")
_log(
f"[EventHandler] event: {eventName}, payload:\n{json.dumps(payload, indent=2, ensure_ascii=False)}"
)
def connectNiri(niriSocket: str, handler) -> bool: