use DateUtils;
use Display;
+use Logger;
use Utils;
my $VERSION = '$Revision: 1.10 $';
my $processes = new Proc::ProcessTable;
my @imgDirs;
+my %totals;
foreach my $process (@{$processes->table}) {
if ($process->cmndline =~ /setbg/ and
} # if
} # foreach
+sub displayStats () {
+ my $statsFile = Logger->new(
+ name => ".$FindBin::Script.stats",
+ path => $ENV{HOME},
+ extension => '',
+ );
+
+ $statsFile->log('At ' . localtime());
+
+ Stats \%totals, $statsFile;
+
+ return;
+} # displayStats
+
sub fillPictures () {
my @images;
-
+
foreach (@imgDirs) {
my @pics = `find "$_" -type f -name "*.jpg"`;
-
+
chomp @pics;
push @images, @pics;
} # foreach
- return @images;
+ $totals{images} = scalar @images;
+
+ displayStats;
+
+ return @images;
} # fillPictures
+sub updateSetBG ($) {
+ my ($image) = @_;
+
+ open my $log, '>', "$ENV{HOME}/.$FindBin::Script"
+ or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
+
+ display $image, $log;
+
+ close $log;
+
+ open $log, '>>', "$ENV{HOME}/.$FindBin::Script.hist"
+ or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1;
+
+ my $msg = localtime() . ":$image";
+
+ display $msg, $log;
+
+ close $log;
+
+ return;
+} # updateSetBG
+
$0 = "$FindBin::Script " . join ' ', @ARGV;
verbose "$FindBin::Script v$VERSION";
for (my $i = 0; $i < scalar @imgDirs; $i++) {
error "$imgDirs[$i] is not a directory", 1 unless -d $imgDirs[$i];
-
+
$imgDirs[$i] = File::Spec->rel2abs ($imgDirs[$i]);
} # foreach
# Using gsettings
-my $setbg = "gsettings";
-my $setbgOpts = "set org.gnome.desktop.background picture-uri \"file://";
+my $setbg = "gsettings";
+my $setbgOpts= "set org.gnome.desktop.background picture-uri \"file://";
my @images = fillPictures;
Usage "No images to display. Must specify -bgdir" unless @images;
-
+
sub SwitchWallPaper {
# We don't need to do anything here, just handle the interrupt and
# let the while loop continue.
debug 'SwitchWallPaper: Interrupt received';
+ displayStats;
} # SwitchWallPaper
$SIG{USR2} = \&SwitchWallPaper;
my $debugger = $DB::OUT;
my $today;
+truncate "$ENV{HOME}/.$FindBin::Script.hist", 0;
+
EnterDaemonMode unless defined $DB::OUT;
while () {
my $image = escapeHTML ($images[int (rand $#images)]);
- open my $log, '>', "$ENV{HOME}/.$FindBin::Script"
- or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
-
- display $image, $log;
-
my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null";
my @output = `$cmd`;
if ($? != 0) {
- display "ERROR Trying to set background - command used \"$cmd\"", $log;
- display "Output:";
- display join "\n", @output, $log;
+ error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" .
+ join "\n", @output;
+ $totals{errors}++;
+ } else {
+ $totals{'Images displayed'}++;
+
+ updateSetBG $image;
} # if
-
- close $log;
-
- $today = YMD;
-
+
+ $today = YMD;
+
sleep $sleep;
-
+
if ($today ne YMD){
@images = fillPictures;
} # if
BEGIN {
# Extract relative path and basename from script name.
$me = $FindBin::Script;
-
+
# Remove .pl for Perl scripts that have that extension
$me =~ s/\.pl$//;
} # BEGIN
my $disposition = $parms{disposition} ? $parms{disposition} : 'perm';
my $timestamped = $parms{timestamped} ? $parms{timestamped} : 'FALSE';
my $append = $parms{append} ? '>>' : '>';
- my $extension = $parms{extension} ? $parms{extension} : 'log';
my $logfile;
- $name = "$name.$extension";
+ if (defined $parms{extension}) {
+ $name .= ".$parms{extension}" unless $parms{extension} eq '';
+ } # if
open $logfile, $append, "$path/$name"
or error "Unable to open logfile $path/$name - $!", 1;
# Set unbuffered output
$logfile->autoflush ();
- set_verbose
- if $ENV{VERBOSE};
- set_debug
- if $ENV{DEBUG};
+ set_verbose if $ENV{VERBOSE};
+ set_debug if $ENV{DEBUG};
return bless {
path => $path,
} # while
close $file;
-
+
return;
} # append
sub name () {
my ($self) = @_;
-
+
=pod
=head3 name
sub fullname () {
my ($self) = @_;
-
+
=pod
=head3 fullname
=cut
$self->log ($msg, $nolinefeed);
-
+
verbose $msg, undef, $nolinefeed;
-
+
return;
} # msg
=cut
$self->log ($msg, $nolinefeed);
-
+
display $msg, undef, $nolinefeed;
-
+
return;
} # disp
=cut
$increment ||= 1;
-
+
$self->{errors} += $increment;
+
+ return;
} # incrementErr
sub err ($;$) {
my ($self, $msg, $errno) = @_;
-
+
=pod
=head3 err ($msg, $errno)
} # if
$self->log ($msg);
-
+
$self->incrementErr;
-
+
exit $errno if $errno;
-
+
return;
} # err
footing => $footing,
data => $logfile
);
-
+
close $logfile
or error "Unable to close logfile $log_filename", 1;
-
+
return;
} # maillog
$msg = "$me: " . YMDHM . ": $msg" if $self->{timestamped};
display $msg, $self->{handle}, $nolinefeed;
-
+
return;
} # log
sub loglines () {
my ($self) = @_;
-
+
=pod
=head3 loglines
=cut
warning $msg, $warnno;
-
+
if ($warnno) {
$msg = "WARNING #$warnno: $msg";
} else {
$self->log ($msg);
$self->{warnings}++;
-
+
return;
} # warn
sub errors () {
my ($self) = @_;
-
+
=pod
=head3 errors ()
sub warnings () {
my ($self) = @_;
-
+
=pod
=head3 warnings ()
if ($self->{disposition} eq 'temp') {
if ($self->{errors} == 0 and
- $self->{warnings} == 0) {
+ $self->{warnings} == 0) {
unlink $self->fullname;
} # if
} # if
-
+
return;
} # destroy
sub _restoreTerm () {
# In case the user hits Ctrl-C
print "\nControl-C\n";
-
+
ReadMode 'normal';
-
+
exit;
} # _restoreTerm
$errorlog ||= $NULL;
my $file;
-
+
# Redirect STDIN to $NULL
open STDIN, '<', $NULL
or error "Can't read $NULL ($!)", 1;
# Now the parent exits
exit if $pid;
-
+
# Write pidfile if specified
if ($pidfile) {
$pidfile = File::Spec->rel2abs ($pidfile);
or warning "Unable to open pidfile $pidfile for writing - $!";
print $file "$$\n";
-
+
close $file;
} # if
-
+
# Set process to be session leader
setsid ()
or error "Can't start a new session ($!)", 1;
-
+
return;
} # EnterDaemonMode
my @output = `$cmd`;
my $status = $?;
-
+
local $SIG{CHLD} = $sigchld;
chomp @output;
$prompt ||= 'Password';
-
+
my $password;
-
+
local $| = 1;
-
+
print "$prompt:";
-
+
$SIG{INT} = \&_restoreTerm;
-
+
ReadMode 'cbreak';
while () {
my $key;
-
+
while (not defined ($key = ReadKey -1)) { }
if ($key =~ /(\r|\n)/) {
} # if
print '*';
-
+
$password .= $key;
} # while
-
+
ReadMode 'restore'; # Reset tty mode before exiting.
$SIG{INT} = 'DEFAULT';
-
+
return $password;
} # GetPassword
# TODO: Make it work on Windows...
return if $^O =~ /win/i;
-
+
open my $loadAvg, '/proc/loadavg'
or croak "Unable to open /proc/loadavg\n";
-
+
my $load = <$loadAvg>;
-
+
close $loadAvg;
-
+
my @loadAvgs = split /\s/, $load;
-
+
if (wantarray) {
return @loadAvgs;
} else {
if ($existingPipe) {
close $existingPipe;
-
+
open $existingPipe, '|-', $to
or error "Unable to open pipe - $!", 1;
-
+
return $existingPipe;
} else {
open $pipe, '|-', $to
$pipeToStop ||= $pipe;
close $pipeToStop if $pipeToStop;
-
+
return;
} # StopPipe
sub RedirectOutput ($$@) {
my ($to, $mode, @output) = @_;
-
+
=pod
=head2 RedirectOutput ($to, @ouput)
chomp;
print $out "$_\n";
} # foreach
-
+
return;
} # RedirectOutput
open my $file, '<', $filename
or error "Unable to open $filename ($!)", 1;
-
+
if (wantarray) {
local $/ = "\n";
my @lines = <$file>;
-
+
close $file
or error "Unable to close $filename ($!)", 1;
-
+
my @cleansed_lines;
-
+
foreach (@lines) {
chomp;
chop if /\r/;
push @cleansed_lines, $_ if !/^#/; # Discard comment lines
} # foreach
-
+
return @cleansed_lines;
} else {
local $/ = undef;
-
+
return <$file>;
} # if
} # ReadFile
=cut
my $msg = "$FindBin::Script Run Statistics:";
-
+
if ($log and ref $log eq 'Logger') {
$total->{errors} = $log->{errors};
$total->{warnings} = $log->{warnings};
} # if
-
+
if (keys %$total) {
# Display statistics (if any)
if ($log) {
foreach (sort keys %$total) {
$msg = $total->{$_} . "\t $_";
-
+
if ($log) {
$log->msg ($total->{$_} . "\t $_");
} else {
} # if
} # foreach
} # if
-
+
return;
} # Stats