home/wayland: combine hyprland and sway randomWallpaper scripts
Some checks are pending
flakehub / flakehub-publish (push) Waiting to run
git-mirror / gitlab-sync (push) Waiting to run
nix-build / default-build (push) Waiting to run
nix-build / fallarbor-build (push) Waiting to run
nix-build / lavaridge-build (push) Waiting to run
nix-build / mauville-build (push) Waiting to run
nix-build / petalburg-build (push) Waiting to run
nix-build / rustboro-build (push) Waiting to run
nix-check / fmt-check (push) Waiting to run
nix-check / eval-check (push) Waiting to run

This commit is contained in:
Aly Raffauf 2024-07-27 18:34:56 -04:00
parent 6d4945103a
commit c85e597584
5 changed files with 70 additions and 94 deletions

View file

@ -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}";
}

View file

@ -122,7 +122,7 @@ in {
exec-once =
[
scripts.wallpaperD
helpers.wallpaperD
(lib.getExe pkgs.waybar)
scripts.idleD
(lib.getExe pkgs.wayland-pipewire-idle-inhibit)

View file

@ -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
'';
}

View file

@ -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 = [

View file

@ -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
'';
}