From c85e597584762462dc9ae6f03b77430e519d57a4 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sat, 27 Jul 2024 18:34:56 -0400 Subject: [PATCH] home/wayland: combine hyprland and sway randomWallpaper scripts --- .../desktop/hyprland/scripts.nix | 60 ---------------- .../desktop/hyprland/settings.nix | 2 +- homeManagerModules/desktop/sway/scripts.nix | 32 --------- homeManagerModules/desktop/sway/settings.nix | 2 +- .../desktop/wayland/helpers.nix | 68 +++++++++++++++++++ 5 files changed, 70 insertions(+), 94 deletions(-) diff --git a/homeManagerModules/desktop/hyprland/scripts.nix b/homeManagerModules/desktop/hyprland/scripts.nix index 0c8daf26..d5704a26 100644 --- a/homeManagerModules/desktop/hyprland/scripts.nix +++ b/homeManagerModules/desktop/hyprland/scripts.nix @@ -60,64 +60,4 @@ in { ${lib.getExe pkgs.dconf} write /org/gnome/desktop/a11y/applications/screen-keyboard-enabled false fi ''; - - wallpaperD = - if cfg.desktop.hyprland.randomWallpaper - then - pkgs.writers.writeRuby "hyprland-randomWallpaper" {} '' - require 'fileutils' - - directory = "${config.xdg.dataHome}/backgrounds/" - hyprctl = "${hyprctl}" - current_pids = {} - known_monitors = {} - last_update_time = {} - - update_interval = 900 # 15 minutes in seconds - - sleep 1 - - if Dir.exist?(directory) - loop do - outputs = IO.popen([hyprctl, 'monitors']).read - active_monitors = outputs.each_line.map { |line| line.split[1] if line.include?('Monitor') }.compact - - added_monitors = active_monitors - known_monitors.keys - removed_monitors = known_monitors.keys - active_monitors - - # Handle newly added monitors - added_monitors.each do |monitor| - random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample - pid = spawn("${lib.getExe pkgs.swaybg}", '-o', monitor, '-i', random_background, '-m', 'fill') - current_pids[monitor] = pid - last_update_time[monitor] = Time.now - known_monitors[monitor] = random_background - end - - # Remove wallpapers from removed monitors - removed_monitors.each do |monitor| - Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] - current_pids.delete(monitor) - last_update_time.delete(monitor) - known_monitors.delete(monitor) - end - - # Update wallpapers after the set interval - active_monitors.each do |monitor| - if Time.now - last_update_time[monitor] >= update_interval - random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample - pid = spawn("${lib.getExe pkgs.swaybg}", '-o', monitor, '-i', random_background, '-m', 'fill') - sleep 1 - Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] - current_pids[monitor] = pid - last_update_time[monitor] = Time.now - known_monitors[monitor] = random_background - end - end - - sleep 5 # Check for monitor changes and update intervals every 5 seconds - end - end - '' - else "${lib.getExe pkgs.swaybg} -i ${cfg.theme.wallpaper}"; } diff --git a/homeManagerModules/desktop/hyprland/settings.nix b/homeManagerModules/desktop/hyprland/settings.nix index abc00b50..f16b1f74 100644 --- a/homeManagerModules/desktop/hyprland/settings.nix +++ b/homeManagerModules/desktop/hyprland/settings.nix @@ -122,7 +122,7 @@ in { exec-once = [ - scripts.wallpaperD + helpers.wallpaperD (lib.getExe pkgs.waybar) scripts.idleD (lib.getExe pkgs.wayland-pipewire-idle-inhibit) diff --git a/homeManagerModules/desktop/sway/scripts.nix b/homeManagerModules/desktop/sway/scripts.nix index 606af31a..952b7cc1 100644 --- a/homeManagerModules/desktop/sway/scripts.nix +++ b/homeManagerModules/desktop/sway/scripts.nix @@ -28,36 +28,4 @@ in { in pkgs.writeShellScript "sway-idled" "${lib.getExe pkgs.swayidle} -w ${lib.strings.concatStringsSep " " (timeouts ++ beforeSleeps)}"; - - randomWallpaper = pkgs.writeShellScript "sway-randomWallpaper" '' - kill `pidof swaybg` - - OLD_PIDS=() - directory=${config.xdg.dataHome}/backgrounds - - if [ -d "$directory" ]; then - while true; do - NEW_PIDS=() - - monitor=`${lib.getExe' config.wayland.windowManager.sway.package "swaymsg"} -t get_outputs -p | grep Output | awk '{print $2}'` - for m in ''${monitor[@]}; do - random_background=$(ls $directory/*.{png,jpg} | shuf -n 1) - ${lib.getExe pkgs.swaybg} -o $m -i $random_background -m fill & - NEW_PIDS+=($!) - done - - if [[ ''${OLD_PIDS[@]} -gt 0 ]]; then - sleep 5 - fi - - for pid in ''${OLD_PIDS[@]}; do - kill $pid - done - - OLD_PIDS=$NEW_PIDS - - sleep 895 - done - fi - ''; } diff --git a/homeManagerModules/desktop/sway/settings.nix b/homeManagerModules/desktop/sway/settings.nix index 469832f6..f1cc3b7a 100644 --- a/homeManagerModules/desktop/sway/settings.nix +++ b/homeManagerModules/desktop/sway/settings.nix @@ -166,7 +166,7 @@ in { ] ++ lib.optional cfg.desktop.sway.redShift {command = "${lib.getExe pkgs.gammastep} -l 33.74:-84.38";} - ++ lib.optional cfg.desktop.sway.randomWallpaper {command = "${scripts.randomWallpaper}";} + ++ lib.optional cfg.desktop.sway.randomWallpaper {command = "${helpers.wallpaperD}";} ++ lib.optional (!cfg.desktop.sway.randomWallpaper) {command = "${lib.getExe pkgs.swaybg} -i ${cfg.theme.wallpaper}";}; floating.criteria = [ diff --git a/homeManagerModules/desktop/wayland/helpers.nix b/homeManagerModules/desktop/wayland/helpers.nix index 1e99663d..ec140b57 100644 --- a/homeManagerModules/desktop/wayland/helpers.nix +++ b/homeManagerModules/desktop/wayland/helpers.nix @@ -48,4 +48,72 @@ mute = "${bin} --output-volume=mute-toggle"; micMute = "${bin} --input-volume=mute-toggle"; }; + + wallpaperD = pkgs.writers.writeRuby "randomWallpaperD" {} '' + require 'fileutils' + + directory = "${config.xdg.dataHome}/backgrounds/" + current_pids = {} + known_monitors = {} + last_update_time = {} + + update_interval = 900 # 15 minutes in seconds + + def process_running?(process_name) + system("pidof #{process_name} > /dev/null 2>&1") + end + + def get_outputs + if process_running?("sway") + outputs = IO.popen(["${lib.getExe' config.wayland.windowManager.sway.package "swaymsg"}", '-t', 'get_outputs', '-p']).readlines + outputs.select { |line| line.include?('Output') }.map { |line| line.split[1] } + elsif process_running?("Hyprland") + outputs = IO.popen(["${lib.getExe' config.wayland.windowManager.hyprland.package "hyprctl"}", 'monitors']).read + outputs.each_line.map { |line| line.split[1] if line.include?('Monitor') }.compact + end + end + + sleep 1 + + if Dir.exist?(directory) + loop do + active_monitors = get_outputs + + added_monitors = active_monitors - known_monitors.keys + removed_monitors = known_monitors.keys - active_monitors + + # Handle newly added monitors + added_monitors.each do |monitor| + random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample + pid = spawn("${lib.getExe pkgs.swaybg}", '-o', monitor, '-i', random_background, '-m', 'fill') + current_pids[monitor] = pid + last_update_time[monitor] = Time.now + known_monitors[monitor] = random_background + end + + # Remove wallpapers from removed monitors + removed_monitors.each do |monitor| + Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] + current_pids.delete(monitor) + last_update_time.delete(monitor) + known_monitors.delete(monitor) + end + + # Update wallpapers after the set interval + active_monitors.each do |monitor| + if Time.now - last_update_time[monitor] >= update_interval + random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample + pid = spawn("${lib.getExe pkgs.swaybg}", '-o', monitor, '-i', random_background, '-m', 'fill') + sleep 1 + Process.kill('TERM', current_pids[monitor]) if current_pids[monitor] + current_pids[monitor] = pid + last_update_time[monitor] = Time.now + known_monitors[monitor] = random_background + end + end + + sleep 5 # Check for monitor changes and update intervals every 5 seconds + end + end + ''; }