wallpaper-daemon: I hate threading
This commit is contained in:
@@ -25,20 +25,20 @@ BLURRED_WALLPAPER_DIR = Path("~/.local/share/wallpaper/blurred").expanduser()
|
|||||||
|
|
||||||
|
|
||||||
def getFirstFile(dir: Path, pattern: str = "*") -> Path | None:
|
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)
|
return next(dir.glob(pattern), None)
|
||||||
|
|
||||||
|
|
||||||
def getNiriSocket():
|
def getNiriSocket():
|
||||||
return environ['NIRI_SOCKET']
|
return environ["NIRI_SOCKET"]
|
||||||
|
|
||||||
|
|
||||||
def _log(msg: str):
|
def _log(msg: str):
|
||||||
# print(msg)
|
# print(msg)
|
||||||
|
|
||||||
# logFIle = Path("/tmp/niri-autoblur.log")
|
# logFile = Path("/tmp/niri-autoblur.log")
|
||||||
# try:
|
# try:
|
||||||
# with logFIle.open("a") as f:
|
# with logFile.open("a") as f:
|
||||||
# f.write(msg + "\n")
|
# f.write(msg + "\n")
|
||||||
# except Exception:
|
# except Exception:
|
||||||
# pass
|
# pass
|
||||||
@@ -58,7 +58,7 @@ def swwwLoadImg(namespace: str, wallpaper: Path):
|
|||||||
"--transition-duration",
|
"--transition-duration",
|
||||||
"0.5",
|
"0.5",
|
||||||
]
|
]
|
||||||
_log(f"[SWWW] {" ".join(cmd)}")
|
_log(f"[SWWW] {' '.join(cmd)}")
|
||||||
ret = 0
|
ret = 0
|
||||||
try:
|
try:
|
||||||
ret = subprocess.run(cmd, check=True).returncode
|
ret = subprocess.run(cmd, check=True).returncode
|
||||||
@@ -90,7 +90,11 @@ def swwwStartDaemon(namespace: str):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
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}")
|
_log(f"[SWWW] daemon started for namespace: {namespace}")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -163,7 +167,7 @@ class AutoBlur:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def initIsBlurred() -> bool:
|
def initIsBlurred() -> bool:
|
||||||
'''[ $(niri msg focused-window | wc -l) -gt 1 ]'''
|
"""[ $(niri msg focused-window | wc -l) -gt 1 ]"""
|
||||||
cmd = ["niri", "msg", "focused-window"]
|
cmd = ["niri", "msg", "focused-window"]
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_output(cmd, text=True)
|
output = subprocess.check_output(cmd, text=True)
|
||||||
@@ -192,15 +196,21 @@ class AutoBlur:
|
|||||||
self._thread.start()
|
self._thread.start()
|
||||||
|
|
||||||
def _run(self) -> None:
|
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:
|
while True:
|
||||||
if self._isBlurred.is_set():
|
setBlurred = self._isBlurred.is_set()
|
||||||
|
if setBlurred:
|
||||||
wallpaper = getFirstFile(self._blurredDir)
|
wallpaper = getFirstFile(self._blurredDir)
|
||||||
else:
|
else:
|
||||||
wallpaper = getFirstFile(self._normalDir)
|
wallpaper = getFirstFile(self._normalDir)
|
||||||
|
|
||||||
if wallpaper is not None and wallpaper.exists():
|
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
|
break
|
||||||
|
|
||||||
sleep(self._interval)
|
sleep(self._interval)
|
||||||
@@ -254,7 +264,9 @@ def handleEvent(event_name, payload):
|
|||||||
|
|
||||||
|
|
||||||
def printEvent(eventName, 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:
|
def connectNiri(niriSocket: str, handler) -> bool:
|
||||||
|
|||||||
@@ -41,7 +41,12 @@
|
|||||||
"options": {
|
"options": {
|
||||||
"branch_icon": "\ue725 ",
|
"branch_icon": "\ue725 ",
|
||||||
"fetch_status": true,
|
"fetch_status": true,
|
||||||
"fetch_upstream_icon": true
|
"fetch_upstream_icon": true,
|
||||||
|
"mapped_branches": {
|
||||||
|
"feat/*": "🚀 ",
|
||||||
|
"bug/*": "🐛 ",
|
||||||
|
"dev/*": "🚧 "
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"style": "powerline",
|
"style": "powerline",
|
||||||
"template": " {{ .UpstreamIcon }} {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
|
"template": " {{ .UpstreamIcon }} {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
|
||||||
|
|||||||
Reference in New Issue
Block a user