X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2Fsetbg;h=8de93c618fb5804a08dce4a37034bc511d5207b2;hb=d3c598399f93a1ca4ffc9f4aedd7907f4157ca43;hp=09311f582ebf069f2b65488942a5879abfea4700;hpb=8d39a0e8bbb3a01a9eb5a801ed67b204ec8357c2;p=clearscm.git diff --git a/bin/setbg b/bin/setbg index 09311f5..8de93c6 100755 --- a/bin/setbg +++ b/bin/setbg @@ -30,29 +30,50 @@ $Date: 2012/11/09 15:31:30 $ =head1 SYNOPSIS - Usage: setbg [-u|sage] [-ve|rbose] [-d|ebug] [-s|leep ] [-bgdir ] - + Usage: setbg [-u|sage] [-h|elp] [-ve|rbose] [-d|ebug] [-s|leep ] + [-bgdirs -bgdirs ...] + Where: - -u|sage: Displays this usage - -ve|rbose: Be verbose - -d|ebug: Output debug messages + -u|sage: Displays this usage + -h|elp: Display full help + -ve|rbose: Be verbose + -d|ebug: Output debug messages - -s|leep: Number of minutes to sleep between setting the background - (Default: 1 hour) - -b|gdir: Directory to scan for images (Default: /web/Pictures) + -s|leep: Number of minutes to sleep between setting the background + (Default: 1 hour) + -l|ockscreen: Change lockscreen backround (Default: False) + -b|gdirs: Directories to scan for images =head1 DESCRIPTION -This script sets the background image randomly based on images $imgDir. Note -if this script is run again it senses that it was previously run and sends the -previous script a SIGUSR2 which the script intrprets as "Change the background -now", then exits. +This script sets the background image randomly based on images found in bgdirs. +Note if this script is run again it senses that it was previously run and sends +the previous script a SIGUSR2 which the script intrprets as "Change the +background now", then exits. + +Data is written to the following files: + + ~/.setbg: Contains the filename of the current background image + ~/.setbg.hist Contains a history of all images displayed for this run + ~/.setbg.stats Contains statistical information for the current run + +Also note that this script will process a SIGUSR1 to mean "re-evaluate the +contents of the bgdirs incase it has changed and display a new image". This is +useful for script to be able to alert setbg that something has changed. For +example, a script named rmbg might look at ~/.setbg to get the name of the +current background image file and remove it then signal setbg with SIGUSR1 to +have it re-evaluate the state of bgdirs. + +Finally setbg will perform the this re-evaluation at midnight everyday. This is +useful because we point setbg to look at -bgdirs from Dropbox where Camera +Uploads is included and new pictures can arrive everyday. =cut use strict; use warnings; +use experimental qw(signatures); use FindBin; use Getopt::Long; @@ -62,71 +83,165 @@ use CGI qw/:standard/; use lib "$FindBin::Bin/../lib"; +use Pod::Usage; + use DateUtils; use Display; +use Logger; use Utils; -my $VERSION = '$Revision: 1.10 $'; +my $VERSION = '$Revision: 1.12 $'; ($VERSION) = ($VERSION =~ /\$Revision: (.*) /); -my $processes = new Proc::ProcessTable; -my @imgDirs; +my $processes = Proc::ProcessTable->new; +my %opts = ( + sleep => 60, + lockscreen => 0, + usage => sub { pod2usage }, + help => sub { pod2usage (-verbose => 2)}, + verbose => sub { set_verbose }, + debug => sub { set_debug }, +); -foreach my $process (@{$processes->table}) { - if ($process->cmndline =~ /setbg/ and - $process->pid != $$) { - kill 12, $process->pid; +my %totals; - exit 0; - } # if -} # foreach +sub displayStats() { + my $statsFile = Logger->new( + name => ".$FindBin::Script.stats", + path => $ENV{HOME}, + extension => '', + ); + + $statsFile->log('At ' . localtime()); + $statsFile->log('Sleep: ' . $opts{sleep}); + $statsFile->log('Image directories:'); + + for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) { + $statsFile->log("\t$opts{bgdirs}[$i]: $opts{bgdircnt}[$i]") + } # for + + Stats \%totals, $statsFile; -sub fillPictures () { + return; +} # displayStats + +sub fillPictures($signame = undef) { my @images; - - foreach (@imgDirs) { - my @pics = `find "$_" -type f -name "*.jpg"`; - + + $totals{bgdirs} = 0; + + for (@{$opts{bgdirs}}) { + my ($status, @pics) = Execute "find \"$_/\" -type f"; + chomp @pics; - push @images, @pics; - } # foreach - return @images; + push @images, grep(/jpg$|png$|gif$/i, @pics); + + @pics = grep(/jpg$|png$|gif$/i, @pics); + + push @{$opts{bgdircnt}}, scalar @pics; + + $totals{bgdirs}++; + } # for + + $totals{images} = scalar @images; + + displayStats; + + return @images; } # fillPictures -$0 = "$FindBin::Script " . join ' ', @ARGV; +sub writeHistory($msg) { + open my $hist, '>>', "$ENV{HOME}/.$FindBin::Script.hist" + or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1; + + $msg = localtime() . $msg; + + print $hist "$msg\n"; + + close $hist; +} # writeHistory +sub writeSetBG($filename, $image) { + open my $file, '>', $filename + or error "Unable to open $filename for writing - $!", 1; + + print $file "$image\n"; + + close $file; +} # writeSetBG + +sub updateSetBG($bgimage, $lockimage) { + writeSetBG "$ENV{HOME}/.$FindBin::Script", $bgimage; + writeSetBG "$ENV{HOME}/.$FindBin::Script.lock", $lockimage; + + my $msg = ":$bgimage"; + $msg .= " lock:$lockimage" if $opts{lockscreen}; + + writeHistory $msg; + + return; +} # updateSetBG + +sub SwitchWallPaper($saigname) { + # We don't need to do anything here, just handle the interrupt and + # let the while loop continue. + debug 'SwitchWallPaper: Interrupt received'; + displayStats; + + return; +} # SwitchWallPaper + +## Main verbose "$FindBin::Script v$VERSION"; -my $sleep = 60 * 60; +my @argvCopy = @ARGV; GetOptions ( - 'usage' => sub { Usage }, - 'verbose' => sub { set_verbose }, - 'debug' => sub { set_debug }, - 'sleep=i' => \$sleep, - 'bgdir=s' => \@imgDirs, + \%opts, + 'usage', + 'help', + 'verbose', + 'debug', + 'sleep=i', + 'lockscreen', + 'bgdirs=s@', + 'mate', ) || Usage; -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 +local $0 = "$FindBin::Script " . join ' ', @argvCopy; + +for my $process (@{$processes->table}) { + if ($process->cmndline =~ /setbg/ and + $process->pid != $$) { + kill 12, $process->pid; + + exit 0; + } # if +} # for + +for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) { + error "$opts{bgdirs}[$i] is not a directory", 1 unless -d $opts{bgdirs}[$i]; + + $opts{bgdirs}[$i] = File::Spec->rel2abs ($opts{bgdirs}[$i]); +} # for # Using gsettings -my $setbg = "gsettings"; -my $setbgOpts = "set org.gnome.desktop.background picture-uri \"file://"; +my $setbg = "gsettings"; + +my ($setbgOpts, $setLockOpts); + +if ($opts{mate}) { + $setbgOpts = 'set org.mate.background picture-filename '; + $setLockOpts = 'set org.mate.screensaver picture-filename '; +} else { + $setbgOpts = "set org.gnome.desktop.background picture-uri \"file://"; + $setLockOpts = "set org.gnome.desktop.screensaver picture-uri \"file://"; +} # if 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'; -} # SwitchWallPaper +Usage "No images to display. Must specify -bgdirs" unless @images; $SIG{USR2} = \&SwitchWallPaper; $SIG{USR1} = \&fillPictures; @@ -134,33 +249,70 @@ $SIG{USR1} = \&fillPictures; my $debugger = $DB::OUT; my $today; +truncate "$ENV{HOME}/.$FindBin::Script.hist", 0; + EnterDaemonMode unless defined $DB::OUT; +my $pickNewImages = 1; +my ($bgimage, $lockimage); + while () { - my $image = escapeHTML ($images[int (rand $#images)]); + if ($pickNewImages) { + $bgimage = escapeHTML ($images[int (rand $#images)]); + $lockimage = escapeHTML ($images[int (rand $#images)]); + } # if + + my $monitorIsOn; + + my ($status, @output) = Execute("xset q | grep Monitor | awk '{print \$3}'"); + + if ($status or $output[0] eq 'Off') { + writeHistory ":Monitor off, not setting background to $bgimage - will keep trying"; - open my $log, '>', "$ENV{HOME}/.$FindBin::Script" - or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1; + $pickNewImages = 0; + } else { + $pickNewImages = 1; - display $image, $log; + my $cmd = $opts{mate} ? "$setbg $setbgOpts\"$bgimage\" 2> /dev/null" + : "$setbg $setbgOpts$bgimage\" 2> /dev/null"; - my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null"; + ($status, @output) = Execute $cmd; - my @output = `$cmd`; + if ($status) { + error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" . + join "\n", @output; + $totals{errors}++; + } else { + $totals{'Images displayed'}++; + } # if - if ($? != 0) { - display "ERROR Trying to set background - command used \"$cmd\"", $log; - display "Output:"; - display join "\n", @output, $log; + if ($opts{lockscreen}) { + $cmd = $opts{mate} ? "$setbg $setLockOpts\"$lockimage\" 2> /dev/null" + : "$setbg $setLockOpts$lockimage\" 2> /dev/null"; + + ($status, @output) = Execute $cmd; + + if ($status != 0) { + error "Trying to set lock screen - command used \"$cmd\"\n\nOutput\n\n" . + join "\n", @output; + $totals{errors}++; + } else { + $totals{'Lock screens displayed'}++; + } # if + } # if + + updateSetBG $bgimage, $lockimage; } # if - - close $log; - - $today = YMD; - - sleep $sleep; - + + displayStats; + + $today = YMD; + + sleep $opts{sleep} * 60; + if ($today ne YMD){ @images = fillPictures; + + displayStats; } # if } # while