2024-08-02 17:58:08 -04:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
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
|
|
|
|
|
2024-08-05 00:42:11 -04:00
|
|
|
xdg_runtime_dir = ENV['XDG_RUNTIME_DIR']
|
|
|
|
hyprland_instance_signature = ENV['HYPRLAND_INSTANCE_SIGNATURE']
|
|
|
|
sway_sock = ENV['SWAYSOCK']
|
|
|
|
|
|
|
|
hyprland_lock_path = nil
|
|
|
|
|
|
|
|
unless hyprland_instance_signature.nil?
|
|
|
|
hyprland_lock_path = File.join(xdg_runtime_dir, 'hypr', hyprland_instance_signature, 'hyprland.lock')
|
|
|
|
end
|
|
|
|
|
|
|
|
def wm_dead? (hyprland_lock_path, sway_sock)
|
|
|
|
sway_running = system("pidof sway > /dev/null")
|
|
|
|
hypr_running = system("pidof Hyprland > /dev/null")
|
|
|
|
(hyprland_lock_path.nil? || !File.exist?(hyprland_lock_path) || !hypr_running) && (sway_sock.nil? || !File.exist?(sway_sock) || !sway_running)
|
|
|
|
end
|
|
|
|
|
2024-08-02 17:58:08 -04:00
|
|
|
def get_outputs
|
2024-08-03 12:16:07 -04:00
|
|
|
hyprctl = IO.popen(["${lib.getExe' config.wayland.windowManager.hyprland.package "hyprctl"}", 'monitors']).read
|
|
|
|
swaymsg = IO.popen(["${lib.getExe' config.wayland.windowManager.sway.package "swaymsg"}", '-t', 'get_outputs', '-p']).readlines
|
2024-08-05 00:42:11 -04:00
|
|
|
|
2024-08-03 12:16:07 -04:00
|
|
|
hypr_outputs = hyprctl.each_line.map { |line| line.split[1] if line.include?('Monitor') }.compact
|
|
|
|
sway_outputs = swaymsg.select { |line| line.include?('Output') }.map { |line| line.split[1] }
|
2024-08-05 00:42:11 -04:00
|
|
|
|
|
|
|
return (sway_outputs | hypr_outputs)
|
2024-08-02 17:58:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
2024-08-05 00:42:11 -04:00
|
|
|
loop do
|
|
|
|
active_monitors = get_outputs
|
|
|
|
|
|
|
|
break if wm_dead? hyprland_lock_path, sway_sock
|
|
|
|
next if active_monitors.empty?
|
2024-08-03 12:36:55 -04:00
|
|
|
|
2024-08-05 00:42:11 -04:00
|
|
|
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
|
2024-08-02 17:58:08 -04:00
|
|
|
|
2024-08-05 00:42:11 -04:00
|
|
|
# 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
|
2024-08-02 17:58:08 -04:00
|
|
|
random_background = Dir.glob(File.join(directory, '*.{png,jpg}')).sample
|
|
|
|
pid = spawn("${lib.getExe pkgs.swaybg}", '-o', monitor, '-i', random_background, '-m', 'fill')
|
2024-08-05 00:42:11 -04:00
|
|
|
sleep 1
|
|
|
|
Process.kill('TERM', current_pids[monitor]) if current_pids[monitor]
|
2024-08-02 17:58:08 -04:00
|
|
|
current_pids[monitor] = pid
|
|
|
|
last_update_time[monitor] = Time.now
|
|
|
|
known_monitors[monitor] = random_background
|
|
|
|
end
|
|
|
|
end
|
2024-08-05 00:42:11 -04:00
|
|
|
|
|
|
|
sleep 3
|
2024-08-02 17:58:08 -04:00
|
|
|
end
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
config = lib.mkIf config.ar.home.services.randomWallpaper.enable {
|
2024-08-15 02:32:43 -04:00
|
|
|
services.hyprpaper.enable = lib.mkDefault false;
|
|
|
|
stylix.targets.hyprpaper.enable = lib.mkDefault false;
|
2024-08-15 02:21:49 -04:00
|
|
|
wayland.windowManager.sway.config.output."*" = lib.mkForce {}; # Dirty. TODO: make more elegant.
|
|
|
|
|
2024-08-02 17:58:08 -04:00
|
|
|
systemd.user.services.randomWallpaper = {
|
|
|
|
Unit = {
|
|
|
|
After = "graphical-session.target";
|
2024-08-12 10:17:21 -04:00
|
|
|
BindsTo = lib.optional (config.ar.home.desktop.hyprland.enable) "hyprland-session.target" ++ lib.optional (config.ar.home.desktop.sway.enable) "sway-session.target";
|
2024-08-02 17:58:08 -04:00
|
|
|
Description = "Lightweight swaybg-based random wallpaper daemon.";
|
|
|
|
PartOf = "graphical-session.target";
|
|
|
|
};
|
|
|
|
|
|
|
|
Service = {
|
|
|
|
ExecStart = "${wallpaperD}";
|
2024-08-11 23:43:48 -04:00
|
|
|
Restart = "no";
|
2024-08-02 17:58:08 -04:00
|
|
|
};
|
|
|
|
|
2024-08-12 10:17:21 -04:00
|
|
|
Install.WantedBy = lib.optional (config.ar.home.desktop.hyprland.enable) "hyprland-session.target" ++ lib.optional (config.ar.home.desktop.sway.enable) "sway-session.target";
|
2024-08-02 17:58:08 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|