From fffbb5d5e0107898a80a2cffcd4f77d8399f611d Mon Sep 17 00:00:00 2001 From: John Mertz Date: Fri, 19 Apr 2024 15:10:54 -0600 Subject: [PATCH] Load cached outputs if none given Fallback to all available outputs --- sway/wallpaper.pl | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/sway/wallpaper.pl b/sway/wallpaper.pl index 18446e5..1ef0486 100755 --- a/sway/wallpaper.pl +++ b/sway/wallpaper.pl @@ -57,6 +57,7 @@ sub new($class, %args) $args{error} = (); $args{pidfile} = "$ENV{HOME}/.local/state/wallpaper.pid"; + $args{outputfile} = "$ENV{HOME}/.local/state/wallpaper_outputs"; return bless { %args }; } @@ -396,7 +397,7 @@ die "Invalid wallpaper path '$path'. Not a directory." if (defined($path) && !-d die "Invalid verbosity level: '$verbose'\n" if (defined($verbose) && $verbose !~ m/^[1-8]$/); $wp->do_log("LOG_DEBUG", "Configuring object"); -$wp->{targets} = \@targets || undef; +$wp->{targets} = \@targets || \(); $wp->{daemon} = $daemon || 0; $wp->{path} = $path || "$ENV{HOME}/wallpapers"; $wp->{crop} = $crop || 1; @@ -405,6 +406,27 @@ $wp->{verbose} = $verbose || 0; $wp->{recursive}= $recursive; $wp->{error} = []; +unless(defined($wp->{targets})) { + if (open(my $fh, '<', $wp->{outputfile})) { + $wp->do_log("LOG_DEBUG", "No outputs requested, using cache file $wp->{outputfile}\n"); + while (my $o = <$fh>) { + chomp($o); + push (@{$wp->{targets}}, $o) if ($o); + } + $wp->do_log("LOG_DEBUG", "Found cached outputs: ".join(', ', @{$wp->{targets}})."\n") if (scalar(@{$wp->{targets}})."\n"); + } else { + $wp->do_log("LOG_WARNING", "Failed to load $wp->{outputfile}\n"); + } + unless (scalar(@{$wp->{targets}})) { + $wp->{'targets'} = \$wp->get_outputs(); + if (scalar(@{$wp->{targets}})) { + $wp->do_log("LOG_WARNING", "Using all available outputs ".join(', ', @{$wp->{targets}})."\n"); + } else { + $wp->do_log("LOG_INFO", "Failed to discover any available outputs.\n", 1); + } + } +} + ################################################################################ # Fork if daemonized ################################################################################