X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2Fsetbg;h=a1f71d190b4bf200c919a9176073674aa20f8db5;hb=290e8df6876439cd18325d55b6dc8e2e5e1fc8f2;hp=86a647acaf177e9129f7c490ca4c430d8e8025fa;hpb=042a9df59dc4f33c79ea8e253f13e76efeba4e73;p=clearscm.git diff --git a/bin/setbg b/bin/setbg index 86a647a..a1f71d1 100755 --- a/bin/setbg +++ b/bin/setbg @@ -32,17 +32,18 @@ $Date: 2012/11/09 15:31:30 $ Usage: setbg [-u|sage] [-h|elp] [-ve|rbose] [-d|ebug] [-s|leep ] [-bgdirs -bgdirs ...] - + Where: - -u|sage: Displays this usage - -h|elp: Display full help - -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|gdirs: Directories to scan for images + -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 @@ -62,16 +63,17 @@ 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. +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 +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; @@ -88,16 +90,17 @@ use Display; use Logger; use Utils; -my $VERSION = '$Revision: 1.11 $'; +my $VERSION = '$Revision: 1.12 $'; ($VERSION) = ($VERSION =~ /\$Revision: (.*) /); my $processes = Proc::ProcessTable->new; my %opts = ( - sleep => 60, - usage => sub { pod2usage }, - help => sub { pod2usage (-verbose => 2)}, - verbose => sub { set_verbose }, - debug => sub { set_debug }, + sleep => 60, + lockscreen => 0, + usage => sub { pod2usage }, + help => sub { pod2usage (-verbose => 2)}, + verbose => sub { set_verbose }, + debug => sub { set_debug }, ); my %totals; @@ -128,11 +131,14 @@ sub fillPictures () { $totals{bgdirs} = 0; for (@{$opts{bgdirs}}) { - my @pics = `find "$_" -type f -name "*.jpg"`; + my ($status, @pics) = Execute "find \"$_/\" -type f"; chomp @pics; - push @images, @pics; + push @images, grep(/jpg$|png$|gif$/i, @pics); + + @pics = grep(/jpg$|png$|gif$/i, @pics); + push @{$opts{bgdircnt}}, scalar @pics; $totals{bgdirs}++; @@ -140,27 +146,39 @@ sub fillPictures () { $totals{images} = scalar @images; + displayStats; + return @images; } # fillPictures -sub updateSetBG ($$) { - my ($bgimage, $lockimage) = @_; +sub writeHistory($msg) { + open my $hist, '>>', "$ENV{HOME}/.$FindBin::Script.hist" + or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1; - open my $setbg, '>', "$ENV{HOME}/.$FindBin::Script" - or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1; + $msg = localtime() . $msg; - display $bgimage, $setbg; + print $hist $msg; - close $setbg; + close $hist; +} # writeHistory - my $msg = localtime() . ":$bgimage lock:$lockimage"; +sub writeSetBG ($filename, $image) { + open my $file, '>', $filename + or error "Unable to open $filename for writing - $!", 1; - open my $hist, '>>', "$ENV{HOME}/.$FindBin::Script.hist" - or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1; + print $file "$image\n"; - display $msg, $hist; + close $file; +} # writeSetBG - close $hist; +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 @@ -186,7 +204,9 @@ GetOptions ( 'verbose', 'debug', 'sleep=i', + 'lockscreen', 'bgdirs=s@', + 'mate', ) || Usage; local $0 = "$FindBin::Script " . join ' ', @argvCopy; @@ -208,8 +228,16 @@ for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) { # Using gsettings my $setbg = "gsettings"; -my $setbgOpts = "set org.gnome.desktop.background picture-uri \"file://"; -my $setLockOpts = "set org.gnome.desktop.screensaver picture-uri \"file://"; + +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; @@ -225,35 +253,56 @@ truncate "$ENV{HOME}/.$FindBin::Script.hist", 0; EnterDaemonMode unless defined $DB::OUT; +my $pickNewImages = 1; +my ($bgimage, $lockimage); + while () { - my $bgimage = escapeHTML ($images[int (rand $#images)]); - my $lockimage = escapeHTML ($images[int (rand $#images)]); + if ($pickNewImages) { + $bgimage = escapeHTML ($images[int (rand $#images)]); + $lockimage = escapeHTML ($images[int (rand $#images)]); + } # if + + my $monitorIsOn; - my $cmd = "$setbg $setbgOpts$bgimage\" 2> /dev/null"; + my ($status, @output) = Execute("xset q | grep Monitor | awk '{print \$3}'"); - my @output = `$cmd`; + if ($status or $output[0] eq 'Off') { + writeHistory ":Monitor off, not setting background to $bgimage - will keep trying"; - if ($? != 0) { - error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" . - join "\n", @output; - $totals{errors}++; + $pickNewImages = 0; } else { - $totals{'Images displayed'}++; - } # if + $pickNewImages = 1; - $cmd = "$setbg $setLockOpts$lockimage\" 2> /dev/null"; + my $cmd = $opts{mate} ? "$setbg $setbgOpts\"$bgimage\" 2> /dev/null" + : "$setbg $setbgOpts$bgimage\" 2> /dev/null"; - @output = `$cmd`; + ($status, @output) = Execute $cmd; - if ($? != 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 ($status) { + error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" . + join "\n", @output; + $totals{errors}++; + } else { + $totals{'Images displayed'}++; + } # if + + if ($opts{lockscreen}) { + $cmd = $opts{mate} ? "$setbg $setLockOpts\"$lockimage\" 2> /dev/null" + : "$setbg $setLockOpts$lockimage\" 2> /dev/null"; - updateSetBG $bgimage, $lockimage; + ($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 displayStats;