2022-07-23 05:12:03 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
# TODO: add option to allow recursive sub-directories (in choose_image())
|
2022-07-23 07:58:11 +00:00
|
|
|
# TODO: license, etc.
|
|
|
|
|
2022-07-23 05:12:03 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2022-07-23 07:43:34 +00:00
|
|
|
use POSIX;
|
2022-07-25 21:03:18 +00:00
|
|
|
use File::Copy;
|
2022-07-23 05:12:03 +00:00
|
|
|
|
|
|
|
use lib "$ENV{HOME}/perl5/lib/perl5";
|
2022-07-25 21:03:18 +00:00
|
|
|
$ENV{PWD} = '/tmp' unless (defined($ENV{PWD}));
|
|
|
|
|
|
|
|
use constant ERROR => {
|
|
|
|
LOG_EMERG => 8,
|
|
|
|
LOG_ALERT => 7,
|
|
|
|
LOG_CRIT => 6,
|
|
|
|
LOG_ERR => 5,
|
|
|
|
LOG_WARNING => 4,
|
|
|
|
LOG_NOTICE => 3,
|
|
|
|
LOG_INFO => 2,
|
|
|
|
LOG_DEBUG => 1
|
|
|
|
};
|
2022-07-23 05:12:03 +00:00
|
|
|
|
2022-07-23 07:43:34 +00:00
|
|
|
our @e;
|
2022-07-23 05:12:03 +00:00
|
|
|
|
|
|
|
sub usage()
|
|
|
|
{
|
2022-07-25 21:03:18 +00:00
|
|
|
my $self = shift;
|
2022-07-23 07:43:34 +00:00
|
|
|
print("$0 [output(s)] [-d|--daemon]\n
|
2022-07-23 05:12:03 +00:00
|
|
|
Sets a wallpaper by cropping an appropriate sized section of a larger image.\n
|
|
|
|
All arguments other than the below are assumed to be output names, as defined
|
|
|
|
in my sway/displays.pl script. If no outputs are provided, all available that
|
|
|
|
are currently enabled will be set.\n
|
|
|
|
--path=<path> Path to wallpaper directory.
|
|
|
|
-p <path> Default: $ENV{HOME}/wallpapers\n
|
|
|
|
--daemon=N Configures the wallpaper to be periodically rotated for all
|
|
|
|
-d N of the given outputs. N indicates the number of seconds between
|
2022-07-23 07:43:34 +00:00
|
|
|
each rotation, if provided (default: 300).\n
|
2022-07-23 07:58:11 +00:00
|
|
|
--nocrop Don't crop a selection from the image. Instead, pass the whole
|
|
|
|
-n image to swaybg and let it handle the scaling\n
|
2022-07-25 21:03:18 +00:00
|
|
|
--verbose=N Define minimum log level. Counting from 1: LOG_DEBUG, LOG_INFO,
|
|
|
|
-v N LOG_NOTICE, LOG_WARNING, LOG_ERR, LOG_CRIT, LOG_ALERT, LOG_EMERG
|
|
|
|
Default: 5; If provided without a value for N then all (1).
|
2022-07-26 02:27:47 +00:00
|
|
|
--recursive=N Enumerate images recursively through directories in the path.
|
|
|
|
-r N N indicates the directory depth, unlimited if no N is provided.
|
2022-07-23 05:12:03 +00:00
|
|
|
--help This menu
|
2022-07-23 07:58:11 +00:00
|
|
|
-h\n
|
|
|
|
You can send SIGUSR1 to force the daemon to reload immediately. Rotation timer
|
|
|
|
will be reset.\n");
|
2022-07-23 07:43:34 +00:00
|
|
|
exit(0);
|
2022-07-23 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my ($class, %args) = @_;
|
|
|
|
|
|
|
|
use AnyEvent::Sway;
|
|
|
|
$args{ipc} = AnyEvent::Sway->new();
|
|
|
|
use Image::Magick;
|
|
|
|
$args{im} = Image::Magick->new();
|
|
|
|
$args{error} = ();
|
|
|
|
|
|
|
|
$args{pidfile} = "/tmp/$ENV{USER}-wallpaper.pid";
|
|
|
|
|
|
|
|
return bless { %args };
|
|
|
|
}
|
|
|
|
|
|
|
|
my $wp = new("Wallpapers");
|
|
|
|
|
|
|
|
if (-e $wp->{pidfile}) {
|
|
|
|
if (-r $wp->{pidfile}) {
|
|
|
|
if (open(my $fh, '<', $wp->{pidfile})) {
|
|
|
|
my $pid = <$fh>;
|
|
|
|
chomp($pid);
|
|
|
|
use Proc::ProcessTable;
|
|
|
|
my $pt = Proc::ProcessTable->new();
|
|
|
|
foreach my $p ( @{ $pt->table() } ) {
|
|
|
|
if ($p->{pid} eq $pid) {
|
|
|
|
my $name = $0;
|
|
|
|
$name =~ s/$ENV{PWD}//;
|
|
|
|
if ($p->{'cmndline'} =~ m#$name#) {
|
|
|
|
$wp->do_log("LOG_CRIT", "Another process is already running with PID: $pid (running and listed in $wp->{pidfile})");
|
|
|
|
# Die locally because do_log will remove pidfile that this iteration does not belong to
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
$wp->do_log("LOG_CRIT", "Found matching $pid with different cmdline: $p->{cmndline} (not $0)",1);
|
|
|
|
# PID in pidfile doesn't look like it is another wallpaper
|
|
|
|
unlink($wp->{pidfile});
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
return $p->{'pid'};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$wp->do_log("LOG_CRIT", "Pidfile $wp->{pidfile} exists, but cannot be opened. Assuming it is running already.");
|
|
|
|
# Die locally because do_log will remove pidfile that this iteration does not belong to
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$wp->do_log("LOG_CRIT", "Pidfile $wp->{pidfile} exists, but is not readable. Assuming it is running already.");
|
|
|
|
# Die locally because do_log will remove pidfile that this iteration does not belong to
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:43:34 +00:00
|
|
|
# SIGUSR reset the timer, force immediate reload, continue looping
|
2022-07-23 05:12:03 +00:00
|
|
|
$SIG{USR1} = sub {
|
2022-07-23 07:43:34 +00:00
|
|
|
alarm 0;
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_INFO", "Reloading due to SIGUSR1");
|
2022-07-23 05:12:03 +00:00
|
|
|
};
|
|
|
|
|
2022-07-23 07:43:34 +00:00
|
|
|
# SIGTERM reset the timer, clean pid, and allow for the main loop to finish run
|
2022-07-23 05:12:03 +00:00
|
|
|
$SIG{TERM} = sub {
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_INFO", "Going down clean due to SIGTERM");
|
|
|
|
clean($wp);
|
|
|
|
$wp->{daemon} = 0;
|
2022-07-23 05:12:03 +00:00
|
|
|
};
|
|
|
|
|
2022-07-23 07:43:34 +00:00
|
|
|
# SIGKILL clean pid then exit immediately
|
|
|
|
$SIG{KILL} = sub {
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_INFO", "Hard kill with SIGKILL, attempting to remove pidfile");
|
|
|
|
clean($wp);
|
2022-07-23 07:43:34 +00:00
|
|
|
exit(0);
|
2022-07-23 05:12:03 +00:00
|
|
|
};
|
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
# simply returns the array of hashes provided by swaymsg
|
|
|
|
sub get_outputs
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $o = $self->{ipc}->get_outputs->recv() || $self->do_log('LOG_WARNING',"Failed to query 'get_outputs'");
|
|
|
|
die "No outputs detected.\n" unless (scalar(@$o) > 0);
|
|
|
|
return $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
# returns the same as above but with the 'name' as a hash key for easier lookup
|
|
|
|
sub get_active
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (defined($self->{outputs}) && scalar($self->{outputs})) {
|
|
|
|
my %active = ();
|
|
|
|
foreach my $o (@{$self->{outputs}}) {
|
|
|
|
my $name;
|
|
|
|
my %details;
|
|
|
|
next unless ($o->{active});
|
|
|
|
$active{$o->{name}} = ();
|
|
|
|
foreach (keys(%$o)) {
|
|
|
|
$active{$o->{name}}->{$_} = $o->{$_} unless ($_ eq 'name');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$self->do_log('LOG_WARNING',"No outputs detected.") unless (scalar(keys(%active)));
|
|
|
|
return \%active;
|
|
|
|
} else {
|
|
|
|
print "You haven't initialized get_outputs";
|
|
|
|
$self->do_log('LOG_WARNING',"Output list not defined yet. Try 'get_outputs()' first");
|
|
|
|
return \{};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 05:12:03 +00:00
|
|
|
sub clean
|
|
|
|
{
|
2022-07-25 21:03:18 +00:00
|
|
|
my $self = shift;
|
|
|
|
if (-e $self->{pidfile}) {
|
|
|
|
open (my $fh, '<', $self->{pidfile});
|
2022-07-23 07:43:34 +00:00
|
|
|
my $p = <$fh>;
|
|
|
|
close($fh);
|
|
|
|
chomp $p;
|
|
|
|
kill($p);
|
2022-07-25 21:03:18 +00:00
|
|
|
unlink($self->{pidfile});
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
2022-07-23 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 02:27:47 +00:00
|
|
|
sub dig_dirs
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $paths_ref = shift;
|
|
|
|
my $path = shift;
|
|
|
|
my $depth = shift || 0;
|
|
|
|
unless (-e $path) {
|
|
|
|
return(undef);
|
|
|
|
}
|
|
|
|
if ($path =~ m/\/\.\.?$/) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach (glob("$path/*"), glob("$path/.*")) {
|
|
|
|
if ($path =~ m/\/\.\.?$/) {
|
|
|
|
next();
|
|
|
|
} elsif (-l $_) {
|
|
|
|
push(@$paths_ref, $_);
|
|
|
|
} elsif (-d $_ && ($self->{recursive} == '-1' || $depth < $self->{recursive})) {
|
|
|
|
$self->dig_dirs($paths_ref,$_,$depth+1);
|
|
|
|
} else {
|
|
|
|
push(@$paths_ref, $_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 05:12:03 +00:00
|
|
|
sub choose_image
|
|
|
|
{
|
2022-07-25 21:03:18 +00:00
|
|
|
my $self = shift;
|
2022-07-23 07:43:34 +00:00
|
|
|
|
|
|
|
my @i;
|
2022-07-26 02:27:47 +00:00
|
|
|
my $depth = 0;
|
|
|
|
my @w;
|
|
|
|
$self->dig_dirs(\@w,$self->{path});
|
|
|
|
return undef unless (scalar(@w));
|
|
|
|
$self->do_log("LOG_DEBUG", "Found ".scalar(@w)." files in $self->{path} up to depth $self->{recursive}");
|
2022-07-23 07:43:34 +00:00
|
|
|
foreach (@w) {
|
2022-07-25 21:03:18 +00:00
|
|
|
if (-d $_) {
|
|
|
|
$self->do_log("LOG_DEBUG", "Ignoring sub-directory $_");
|
2022-07-23 07:43:34 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($_ =~ m/\.(png|jpg)$/) {
|
|
|
|
push(@i,$_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $i[rand(scalar(@i))] || return undef;
|
2022-07-23 05:12:03 +00:00
|
|
|
}
|
2022-07-23 05:32:23 +00:00
|
|
|
|
2022-07-23 05:12:03 +00:00
|
|
|
sub crop
|
|
|
|
{
|
2022-07-25 21:03:18 +00:00
|
|
|
my $self = shift;
|
2022-07-23 07:43:34 +00:00
|
|
|
my $image = shift;
|
|
|
|
my $ow = shift;
|
|
|
|
my $oh = shift;
|
|
|
|
|
|
|
|
my $cropped = $image;
|
2022-09-22 18:32:39 +00:00
|
|
|
$cropped =~ s#^.*/([^/]*)\.([^\.]+)$#$self->{'path'}$1-cropped.$2#;
|
2022-07-23 07:43:34 +00:00
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
#$image = "/tmp/Pharma-out.png.jpg";
|
2022-07-23 07:43:34 +00:00
|
|
|
use Image::Magick;
|
|
|
|
my $im = Image::Magick->new();
|
2022-07-25 21:03:18 +00:00
|
|
|
die "$image is not readable" unless (-r $image);
|
|
|
|
my $ret = $im->Read($image);
|
|
|
|
return 0 if ($ret);
|
2022-07-23 07:43:34 +00:00
|
|
|
my ($iw, $ih) = $im->Get("columns", "rows");
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log("LOG_DEBUG", "Image has dimensions ${iw}x${ih}");
|
2022-07-23 07:43:34 +00:00
|
|
|
|
|
|
|
# Return full size if it is smaller in either dimension then the output
|
|
|
|
if ($iw <= $ow || $ih <= $oh) {
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log("LOG_DEBUG", "Not cropping because ${iw}x${ih} is smaller or equal to the output (${ow}x${oh}) in at least 1 dimension.");
|
2022-07-23 07:43:34 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($x, $y);
|
|
|
|
$x = int(rand($iw-$ow));
|
|
|
|
$y = int(rand($ih-$oh));
|
|
|
|
|
|
|
|
my $err = $im->Crop(geometry=>"${ow}x${oh}+${x}+${y}");
|
|
|
|
die "$err" if ($err);
|
|
|
|
$err = $im->Write($cropped);
|
|
|
|
die "$err" if ($err);
|
|
|
|
return $cropped if ( -e $cropped );
|
2022-07-23 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub set_background
|
|
|
|
{
|
2022-07-25 21:03:18 +00:00
|
|
|
my $self = shift;
|
2022-07-23 07:43:34 +00:00
|
|
|
my $target = shift || return "No target or image provided";
|
|
|
|
my $cropped = shift || return "No image provided";
|
|
|
|
# TODO get fallback from javascript
|
|
|
|
my $cmd = "output $target background $cropped fill #000000";
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log("LOG_DEBUG", "Running $cmd\n");
|
|
|
|
my $ret = $self->{ipc}->message(0,$cmd)->recv;
|
2022-07-23 07:43:34 +00:00
|
|
|
if ($ret->[0]->{success}) {
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log("LOG_DEBUG", "Success!");
|
2022-07-23 07:43:34 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
return "Failed to run Sway IPC command '$cmd'";
|
2022-07-23 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
sub do_log
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $level = shift;
|
|
|
|
my $msg = shift;
|
|
|
|
my $die = shift || 0;
|
|
|
|
my $min = $self->{verbose} || 5;
|
|
|
|
|
2022-11-07 19:50:51 +00:00
|
|
|
# Journald is borked. Just don't bother logging
|
|
|
|
return 0;
|
2022-07-25 21:03:18 +00:00
|
|
|
if ($self->{daemon}) {
|
|
|
|
use Log::Journald qw(send);
|
|
|
|
send(
|
|
|
|
PRIORITY => ERROR->{$level},
|
|
|
|
MESSAGE => $msg,
|
|
|
|
PERL_PACKAGE => 'Sway Wallpapers'
|
|
|
|
) || warn "Could not send log ($level $msg): $!";
|
|
|
|
if ($die) {
|
|
|
|
$msg = '(FATAL) ' . $msg;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ERROR->{$level} >= $min) {
|
|
|
|
printf("%11s %s\n", $level, $msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($die) {
|
|
|
|
$self->clean();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:43:34 +00:00
|
|
|
sub run
|
|
|
|
{
|
2022-07-25 21:03:18 +00:00
|
|
|
my $self = shift;
|
|
|
|
$self->do_log("LOG_DEBUG", "Fetching outputs from IPC");
|
|
|
|
$self->{outputs} = $self->get_outputs();
|
2022-07-23 07:43:34 +00:00
|
|
|
# Local copy of targets so that it will re-check active every time
|
2022-07-25 21:03:18 +00:00
|
|
|
my @t = @{$self->{targets}} if (scalar(@{$self->{targets}}));
|
|
|
|
$self->do_log("LOG_DEBUG", "Removing inactive ouputs");
|
|
|
|
my $active = $self->get_active();
|
|
|
|
# If specific targets were not defined, use all active
|
2022-07-23 07:43:34 +00:00
|
|
|
unless (scalar(@t)) {
|
2022-07-25 21:03:18 +00:00
|
|
|
@t = keys(%$active);
|
|
|
|
push(@{$self->{error}}, "No target outputs") unless (scalar(@t));
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log("LOG_DEBUG", "Looping desired ouputs");
|
|
|
|
foreach my $target (@t) {
|
|
|
|
$self->do_log("LOG_DEBUG", "Ensuring that desired output is active");
|
|
|
|
unless (defined($active->{$target})) {
|
|
|
|
$self->do_log('LOG_DEBUG', "Target $target is not an active output");
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$self->do_log("LOG_DEBUG", "Selecting image for $target");
|
|
|
|
my $image = $self->choose_image();
|
2022-07-23 07:43:34 +00:00
|
|
|
if (defined($image)) {
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log('LOG_DEBUG', "Selected $image");
|
2022-07-23 07:43:34 +00:00
|
|
|
if ( -r "$image" ) {
|
|
|
|
my $cropped;
|
2022-07-25 21:03:18 +00:00
|
|
|
if ($self->{crop}) {
|
|
|
|
$self->do_log('LOG_DEBUG',"Cropping image for '$target' using '$image'");
|
|
|
|
$cropped = $self->crop(
|
|
|
|
$image,
|
|
|
|
$active->{$target}->{rect}->{width},
|
|
|
|
$active->{$target}->{rect}->{height}
|
|
|
|
);
|
|
|
|
if ($cropped) {
|
|
|
|
} else {
|
|
|
|
# Create a tmp copy since it will be deleted later
|
|
|
|
# If PWD is the wallpaper path, make the tmp in /tmp
|
|
|
|
if ($self->{path} eq $ENV{PWD}) {
|
|
|
|
$cropped = $image;
|
|
|
|
$cropped =~ s%$ENV{PWD}%/tmp%;
|
|
|
|
# Else make the tmp in PWD
|
|
|
|
} else {
|
|
|
|
$cropped = '/'.$image;
|
|
|
|
$cropped =~ s%.*/([^/+])%$ENV{PWD}/$1%;
|
|
|
|
}
|
|
|
|
$self->do_log('LOG_DEBUG',"Creating copy of original: $cropped");
|
|
|
|
File::Copy::copy($image,$cropped) || $self->do_log('LOG_WARNING',"Failed to copy to $cropped: $!");
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log('LOG_DEBUG', "Cropping is disabled");
|
2022-07-23 07:43:34 +00:00
|
|
|
$cropped = $image;
|
|
|
|
}
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->set_background($target,$cropped);
|
|
|
|
if ($self->{crop}) {
|
|
|
|
# Give swaybg a second, otherwise the file will be missing before it ends
|
2022-09-22 18:32:39 +00:00
|
|
|
sleep(1);
|
|
|
|
$self->do_log('LOG_DEBUG', "Deleting $cropped");
|
2022-07-25 21:03:18 +00:00
|
|
|
unlink($cropped) || $self->do_log("LOG_WARNING", "Failed to delete $cropped after setting: $!");
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log("LOG_WARNING", "Failed to read $image");
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-25 21:03:18 +00:00
|
|
|
$self->do_log("LOG_WARNING", "Failed to select image from $self->{path}");
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Collect arguments
|
|
|
|
################################################################################
|
2022-07-23 05:12:03 +00:00
|
|
|
my @targets;
|
2022-07-23 07:43:34 +00:00
|
|
|
my $daemon;
|
|
|
|
my $delay;
|
|
|
|
my $crop;
|
2022-07-23 05:12:03 +00:00
|
|
|
my $path;
|
2022-07-25 21:03:18 +00:00
|
|
|
my $verbose;
|
2022-07-26 02:27:47 +00:00
|
|
|
my $recursive = 0;
|
2022-07-23 05:12:03 +00:00
|
|
|
while (my $arg = shift(@ARGV)) {
|
2022-07-23 07:43:34 +00:00
|
|
|
if ($arg eq '-h' || $arg eq '--help') {
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->usage();
|
2022-07-23 07:43:34 +00:00
|
|
|
} elsif ($arg =~ m/^\-\-path=?(.+)$/) {
|
|
|
|
die "Redundant argument '$arg'. Wallpaper path already set.\n" if ($path);
|
|
|
|
$path = $1;
|
|
|
|
} elsif ($arg eq '-p') {
|
|
|
|
die "Redundant argument '$arg'. Wallpaper path already set.\n" if ($path);
|
|
|
|
$path = shift(@ARGV);
|
2022-07-25 21:03:18 +00:00
|
|
|
} elsif ($arg =~ m/^\-\-daemon=?(.+)?$/) {
|
2022-07-23 07:43:34 +00:00
|
|
|
die "Redundant argument '$arg'. Daemon mode already set.\n" if ($daemon);
|
2022-07-25 21:03:18 +00:00
|
|
|
$delay = $1 || 300;
|
2022-07-23 07:43:34 +00:00
|
|
|
$daemon = 1;
|
|
|
|
} elsif ($arg eq '-d') {
|
|
|
|
die "Redundant argument '$arg'. Daemon mode already set.\n" if ($daemon);
|
2022-07-25 21:03:18 +00:00
|
|
|
if (scalar(@ARGV) && $ARGV[0] =~ m/^\d+$/) {
|
2022-07-23 07:43:34 +00:00
|
|
|
$delay = shift(@ARGV);
|
|
|
|
}
|
|
|
|
$daemon = 1;
|
2022-07-25 21:03:18 +00:00
|
|
|
} elsif ($arg eq '--nocrop' || $arg eq '-n') {
|
|
|
|
die "Redundant argument '$arg'. No-crop already set.\n" if (defined($crop));
|
|
|
|
$crop = 0;
|
|
|
|
} elsif ($arg =~ m/^\-\-verbose=?(.+)?$/) {
|
|
|
|
die "Redundant argument '$arg'. Verbosity already set.\n" if ($verbose);
|
|
|
|
$verbose = $1 || 1;
|
2022-07-26 02:27:47 +00:00
|
|
|
} elsif ($arg eq '-v') {
|
2022-07-25 21:03:18 +00:00
|
|
|
die "Redundant argument '$arg'. Verbosity already set.\n" if ($verbose);
|
|
|
|
if (scalar(@ARGV) && $ARGV[0] =~ m/^\d$/) {
|
|
|
|
$verbose = shift(@ARGV);
|
|
|
|
}
|
2022-07-26 02:27:47 +00:00
|
|
|
} elsif ($arg =~ m/^\-\-recursive=?(.+)?$/) {
|
|
|
|
die "Redundant argument '$arg'. Recursive search already set.\n" unless ($recursive == 0);
|
|
|
|
$recursive = $1 || -1;
|
|
|
|
} elsif ($arg eq '-r') {
|
|
|
|
die "Redundant argument '$arg'. Recursive search already set.\n" unless ($recursive == 0);
|
|
|
|
if (scalar(@ARGV) && $ARGV[0] =~ m/^\d+$/) {
|
|
|
|
$recursive = shift(@ARGV);
|
|
|
|
} else {
|
|
|
|
$recursive = -1;
|
|
|
|
}
|
2022-07-25 21:03:18 +00:00
|
|
|
} elsif ($arg =~ m/^-/) {
|
|
|
|
die "Unrecognized argument: $arg\n";
|
2022-07-23 07:43:34 +00:00
|
|
|
} else {
|
|
|
|
push(@targets,$arg);
|
|
|
|
}
|
2022-07-23 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 07:43:34 +00:00
|
|
|
################################################################################
|
|
|
|
# Validate arguments
|
|
|
|
################################################################################
|
2022-07-23 05:12:03 +00:00
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_DEBUG", "Validating arguments");
|
2022-07-23 07:43:34 +00:00
|
|
|
die "Invalid rotation delay: $delay" if (defined($delay) && $delay !~ m/^\d+$/);
|
|
|
|
die "Invalid wallpaper path '$path'. Not a directory." if (defined($path) && !-d $path);
|
2022-09-03 05:05:44 +00:00
|
|
|
die "Invalid verbosity level: '$verbose'\n" if (defined($verbose) && $verbose !~ m/^[1-8]$/);
|
2022-07-23 05:12:03 +00:00
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_DEBUG", "Configuring object");
|
|
|
|
$wp->{targets} = \@targets || undef;
|
|
|
|
$wp->{daemon} = $daemon || 0;
|
|
|
|
$wp->{path} = $path || "$ENV{HOME}/wallpapers";
|
|
|
|
$wp->{crop} = $crop || 1;
|
|
|
|
$wp->{delay} = $delay || 300;
|
|
|
|
$wp->{verbose} = $verbose || 0;
|
2022-07-26 02:27:47 +00:00
|
|
|
$wp->{recursive}= $recursive;
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->{error} = [];
|
2022-07-23 07:43:34 +00:00
|
|
|
|
|
|
|
################################################################################
|
2022-07-25 21:03:18 +00:00
|
|
|
# Fork if daemonized
|
2022-07-23 07:43:34 +00:00
|
|
|
################################################################################
|
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
if ($wp->{daemon}) {
|
|
|
|
$wp->do_log("LOG_DEBUG", "Forking daemon");
|
2022-07-23 07:43:34 +00:00
|
|
|
my $p = fork();
|
|
|
|
if ($p) {
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_DEBUG", "Writing PID ($p) to pidfile ($wp->{pidfile})");
|
|
|
|
if (open(my $fh, ">", $wp->{pidfile})) {
|
|
|
|
print $fh "$p" || die "Failed to write pid ($p) to pidfile ".$wp->{pidfile};
|
2022-07-23 07:43:34 +00:00
|
|
|
close($fh);
|
|
|
|
} else {
|
2022-07-25 21:03:18 +00:00
|
|
|
print "Failed to open pidfile ".$wp->{pidfile}.": $!\n";
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
2022-07-25 21:03:18 +00:00
|
|
|
# Short delay necessary for SystemD to find PID
|
|
|
|
sleep(1);
|
2022-07-23 07:43:34 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_DEBUG", "Daemon running");
|
|
|
|
setpgrp(0, 0);
|
2022-07-23 07:43:34 +00:00
|
|
|
umask 0;
|
2022-07-23 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 07:43:34 +00:00
|
|
|
################################################################################
|
|
|
|
# Main
|
|
|
|
################################################################################
|
2022-07-23 05:12:03 +00:00
|
|
|
|
2022-07-25 21:03:18 +00:00
|
|
|
my $first = 1;
|
2022-07-23 05:12:03 +00:00
|
|
|
do {
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_INFO", "Reloading wallpaper") unless ($first);
|
|
|
|
$wp->run();
|
|
|
|
if ($wp->{daemon}) {
|
2022-07-23 07:43:34 +00:00
|
|
|
my $normal = "reload wallpaper";
|
|
|
|
eval {
|
2022-07-25 21:03:18 +00:00
|
|
|
$SIG{ALRM} = sub { return "$normal" };
|
|
|
|
alarm $wp->{delay};
|
2022-07-23 07:43:34 +00:00
|
|
|
POSIX::pause();
|
|
|
|
alarm 0;
|
|
|
|
};
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_WARNING", "Reload failed: $@") if ($@ && $@ !~ quotemeta($normal));
|
2022-07-23 07:43:34 +00:00
|
|
|
}
|
2022-07-25 21:03:18 +00:00
|
|
|
$first = 0 if ($first);
|
|
|
|
} while ($wp->{daemon});
|
2022-07-23 07:43:34 +00:00
|
|
|
|
|
|
|
# If we made it to here, it was not daemonized. Output errors and exit
|
2022-07-25 21:03:18 +00:00
|
|
|
$wp->do_log("LOG_DEBUG", "Finishing") unless ($wp->{daemon});
|
2022-07-23 05:12:03 +00:00
|
|
|
|
|
|
|
exit(0);
|