From d0c00410aebc70d9a2ff48c6323b7debeeac7a9a Mon Sep 17 00:00:00 2001 From: Uyanide Date: Wed, 8 Oct 2025 16:12:58 +0200 Subject: [PATCH] niri-autoblur -> wallpaper-daemon --- .scripts/change-wallpaper | 19 ++- .scripts/{niri-autoblur => wallpaper-daemon} | 119 ++++++++++++++----- hypr/hyprland/execs.conf | 3 +- hypr/hyprpaper.conf | 2 - niri/config.kdl | 4 +- niri/config.kdl.template | 4 +- 6 files changed, 107 insertions(+), 44 deletions(-) rename .scripts/{niri-autoblur => wallpaper-daemon} (67%) delete mode 100644 hypr/hyprpaper.conf diff --git a/.scripts/change-wallpaper b/.scripts/change-wallpaper index 63e2870..7373b50 100755 --- a/.scripts/change-wallpaper +++ b/.scripts/change-wallpaper @@ -10,8 +10,9 @@ fi [ ! -f "$image" ] && exit 1 ext=${image##*.} +random_name=$(tr -dc 'a-zA-Z0-9' /dev/null 2> /dev/null & swww img -n backdrop "$blurred_image" --transition-type fade --transition-duration 2 > /dev/null 2> /dev/null - notify-send "Blurred Wallpaper set" "Selected wallpaper has been successfully applied for overview" + notify-send "Blurred Wallpaper Generated" "$blurred_image" ) & swww img -n background "$image_copied" --transition-type fade --transition-duration 2 > /dev/null 2> /dev/null diff --git a/.scripts/niri-autoblur b/.scripts/wallpaper-daemon similarity index 67% rename from .scripts/niri-autoblur rename to .scripts/wallpaper-daemon index 1bbd70a..7f7fd07 100755 --- a/.scripts/niri-autoblur +++ b/.scripts/wallpaper-daemon @@ -13,8 +13,8 @@ NORMAL_WALLPAPER_DIR = Path("/home/kolkas/.local/share/wallpaper/current") BLURRED_WALLPAPER_DIR = Path("/home/kolkas/.local/share/wallpaper/blurred") -def _get_first_file(dir: Path) -> Path | None: - return next(dir.glob("*"), None) +def _get_first_file(dir: Path, pattern: str = "*") -> Path | None: + return next(dir.glob(pattern), None) def get_niri_socket(): @@ -22,7 +22,8 @@ def get_niri_socket(): def _log(msg: str): - print(msg) + # print(msg) + # logFIle = Path("/tmp/niri-autoblur.log") # try: # with logFIle.open("a") as f: @@ -30,8 +31,10 @@ def _log(msg: str): # except Exception: # pass + pass -def swwwCommand(namespace: str, wallpaper: Path): + +def swwwLoadImg(namespace: str, wallpaper: Path): cmd = [ "swww", "img", @@ -58,6 +61,31 @@ def swwwCommand(namespace: str, wallpaper: Path): return True +def swwwStartDaemon(namespace: str): + # Check if daemon is already running + cmd = ["pgrep", "-f", f"swww daemon -n {namespace}"] + try: + output = subprocess.check_output(cmd, text=True) + pids = output.strip().splitlines() + if pids: + _log(f"[SWWW] daemon already running with PIDs: {', '.join(pids)}") + return True + except subprocess.CalledProcessError: + # pgrep returns non-zero exit code if no process is found + pass + except Exception as e: + _log(f"[SWWW] failed to check if daemon is running: {e}") + pass + + try: + 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: + _log(f"[SWWW] failed to start daemon non-blockingly: {e}") + return False + + class AutoBlur: _interval: float _normalDir: Path @@ -90,9 +118,9 @@ class AutoBlur: def setBlurred(self, isBlurred: bool) -> None: # Cache state, avoid starting thread unnecessarily - if not self._isFirst and self._isBlurred.is_set() == isBlurred: - _log("[AutoBlur] state unchanged") - return + # if not self._isFirst and self._isBlurred.is_set() == isBlurred: + # _log("[AutoBlur] state unchanged") + # return self._isFirst = False if isBlurred: @@ -106,16 +134,6 @@ class AutoBlur: self._thread = threading.Thread(target=self._run, daemon=True) self._thread.start() - def _apply(self, wallpaper: Path) -> bool: - if wallpaper == self._lastWallpaer: - return True - - if not swwwCommand("background", wallpaper): - return False - - self._lastWallpaer = wallpaper - return True - def _run(self) -> None: '''Wait until wallpapers are ready & apply the correct one according to the current state''' while True: @@ -130,12 +148,23 @@ class AutoBlur: sleep(self._interval) + def _apply(self, wallpaper: Path) -> bool: + if wallpaper == self._lastWallpaer: + return True + + if not swwwLoadImg("background", wallpaper): + return False + + self._lastWallpaer = wallpaper + return True + autoBlurInst = AutoBlur(NORMAL_WALLPAPER_DIR, BLURRED_WALLPAPER_DIR) def handleEvent(event_name, payload): if event_name == "WindowFocusChanged": + _log(f"[EventHandler] WindowFocusChanged event received") id = payload.get("id", "") if isinstance(id, int): _log(f"[EventHandler] focused window id: {id}") @@ -147,6 +176,7 @@ def handleEvent(event_name, payload): _log(f"[EventHandler] unknown id: {id}, assuming no focused window") autoBlurInst.setBlurred(False) elif event_name == "WindowsChanged": + _log(f"[EventHandler] WindowsChanged event received") windows = payload.get("windows", []) for window in windows: if window.get("is_focused", False): @@ -156,6 +186,7 @@ def handleEvent(event_name, payload): _log("[EventHandler] no focused window found") autoBlurInst.setBlurred(False) elif event_name == "WindowOpenedOrChanged": + _log(f"[EventHandler] WindowOpenedOrChanged event received") window = payload.get("window", {}) if window.get("is_focused", False): _log(f"[EventHandler] opened/changed focused window") @@ -174,7 +205,7 @@ def connect_niri(niriSocket: str, handler) -> bool: sock.connect(niriSocket) except Exception as e: sock.close() - _log(f"[Socket] Failed to connect to {niriSocket}: {e}") + _log(f"[Socket] failed to connect to {niriSocket}: {e}") return False f = sock.makefile("rwb") @@ -229,15 +260,47 @@ def connect_niri(niriSocket: str, handler) -> bool: if __name__ == "__main__": - # Init wallpaper for backdrop - wallpaper = _get_first_file(BLURRED_WALLPAPER_DIR) - if wallpaper: - swwwCommand("backdrop", wallpaper) + desktop = environ.get("XDG_CURRENT_DESKTOP", "") + if desktop == "niri": + _log("[Main] running in Niri") + _log("[Main] starting swww daemons") + if not swwwStartDaemon("background"): + exit(1) + if not swwwStartDaemon("backdrop"): + exit(1) + sleep(1) # give some time to start - niri_socket = get_niri_socket() - if not niri_socket: - _log("[Main] NIRI_SOCKET environment variable is not set.") - exit(1) + _log("[Main] loading initial wallpapers") + # Init wallpaper for backdrop + blurred = _get_first_file(BLURRED_WALLPAPER_DIR) + if blurred: + swwwLoadImg("backdrop", blurred) + # Init wallpaper for background + normal = _get_first_file(NORMAL_WALLPAPER_DIR) + if normal: + swwwLoadImg("background", normal) - if not connect_niri(niri_socket, handleEvent): - exit(1) + # Connect to Niri socket + _log(f"[Main] connecting to Niri socket") + niri_socket = get_niri_socket() + if not niri_socket: + _log("[Main] NIRI_SOCKET environment variable is not set.") + exit(1) + + if not connect_niri(niri_socket, handleEvent): + exit(1) + elif desktop == "Hyprland": + _log("[Main] running in Hyprland") + _log("[Main] starting swww daemon") + if not swwwStartDaemon("background"): + exit(1) + sleep(1) # similarly + + _log("[Main] loading initial wallpaper") + normal = _get_first_file(NORMAL_WALLPAPER_DIR) + if normal: + swwwLoadImg("background", normal) + + # Wait indefinitely + while True: + sleep(3600) diff --git a/hypr/hyprland/execs.conf b/hypr/hyprland/execs.conf index e79f5a1..07d0bec 100755 --- a/hypr/hyprland/execs.conf +++ b/hypr/hyprland/execs.conf @@ -3,8 +3,7 @@ exec-once = config-switch Hyprland # Bar, wallpaper exec-once = waybar -exec-once = swww-daemon -n background -exec-once = sleep 1 && swww img -n background $(find $HOME/.local/share/wallpaper/current -type f | head -n 1) --transition-type fade --transition-duration 2 +exec-once = wallpaper-daemon # Input method exec-once = fcitx5 diff --git a/hypr/hyprpaper.conf b/hypr/hyprpaper.conf deleted file mode 100644 index 6bbb104..0000000 --- a/hypr/hyprpaper.conf +++ /dev/null @@ -1,2 +0,0 @@ -preload = /home/kolkas/.local/share/wallpaper/current/wallpaper.jpg -wallpaper = , /home/kolkas/.local/share/wallpaper/current/wallpaper.jpg diff --git a/niri/config.kdl b/niri/config.kdl index 9634d64..3934f0d 100644 --- a/niri/config.kdl +++ b/niri/config.kdl @@ -122,9 +122,7 @@ spawn-sh-at-startup "config-switch niri" spawn-at-startup "waybar" // Wallpaper -spawn-sh-at-startup "swww-daemon -n background" -spawn-sh-at-startup "swww-daemon -n backdrop" -spawn-at-startup "niri-autoblur" +spawn-at-startup "wallpaper-daemon" // Not necessary maybe ... spawn-at-startup "fcitx5" diff --git a/niri/config.kdl.template b/niri/config.kdl.template index fd0430b..0806288 100644 --- a/niri/config.kdl.template +++ b/niri/config.kdl.template @@ -122,9 +122,7 @@ spawn-sh-at-startup "config-switch niri" spawn-at-startup "waybar" // Wallpaper -spawn-sh-at-startup "swww-daemon -n background" -spawn-sh-at-startup "swww-daemon -n backdrop" -spawn-at-startup "niri-autoblur" +spawn-at-startup "wallpaper-daemon" // Not necessary maybe ... spawn-at-startup "fcitx5"