5 =head1 NAME $RCSfile: setbg,v $
15 Andrew DeFaria <Andrew@ClearSCM.com>
23 Fri Mar 18 01:14:38 PST 2005
27 $Date: 2012/11/09 15:31:30 $
33 Usage: setbg [-u|sage] [-h|elp] [-ve|rbose] [-d|ebug] [-s|leep <n>]
34 [-bgdirs <bgdir> -bgdirs ...]
38 -u|sage: Displays this usage
39 -h|elp: Display full help
41 -d|ebug: Output debug messages
43 -s|leep: Number of minutes to sleep between setting the background
45 -b|gdirs: Directories to scan for images
49 This script sets the background image randomly based on images found in bgdirs.
50 Note if this script is run again it senses that it was previously run and sends
51 the previous script a SIGUSR2 which the script intrprets as "Change the
52 background now", then exits.
54 Data is written to the following files:
56 ~/.setbg: Contains the filename of the current background image
57 ~/.setbg.hist Contains a history of all images displayed for this run
58 ~/.setbg.stats Contains statistical information for the current run
60 Also note that this script will process a SIGUSR1 to mean "re-evaluate the
61 contents of the bgdirs incase it has changed and display a new image". This is
62 useful for script to be able to alert setbg that something has changed. For
63 example, a script named rmbg might look at ~/.setbg to get the name of the
64 current background image file and remove it then signal setbg with SIGUSR1 to
65 have it re-evaluate the state of bgdirs.
67 Finally setbg will perform the this re-evaluation at midnight everyday. This is
68 useful because we point setbg to look at -bgdirs from Dropbox where Camera
69 Uploads is included and new pictures can arrive everyday.
78 use Proc::ProcessTable;
80 use CGI qw/:standard/;
82 use lib "$FindBin::Bin/../lib";
91 my $VERSION = '$Revision: 1.11 $';
92 ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
94 my $processes = Proc::ProcessTable->new;
97 usage => sub { pod2usage },
98 help => sub { pod2usage (-verbose => 2)},
99 verbose => sub { set_verbose },
100 debug => sub { set_debug },
105 sub displayStats () {
106 my $statsFile = Logger->new(
107 name => ".$FindBin::Script.stats",
112 $statsFile->log('At ' . localtime());
113 $statsFile->log('Sleep: ' . $opts{sleep});
114 $statsFile->log('Image directories:');
116 for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) {
117 $statsFile->log("\t$opts{bgdirs}[$i]: $opts{bgdircnt}[$i]")
120 Stats \%totals, $statsFile;
125 sub fillPictures () {
130 for (@{$opts{bgdirs}}) {
131 my @pics = `find "$_" -type f -name "*.jpg"`;
136 push @{$opts{bgdircnt}}, scalar @pics;
141 $totals{images} = scalar @images;
146 sub updateSetBG ($) {
149 open my $setbg, '>', "$ENV{HOME}/.$FindBin::Script"
150 or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
152 display $image, $setbg;
156 my $msg = localtime() . ":$image";
158 open my $hist, '>>', "$ENV{HOME}/.$FindBin::Script.hist"
159 or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1;
168 sub SwitchWallPaper {
169 # We don't need to do anything here, just handle the interrupt and
170 # let the while loop continue.
171 debug 'SwitchWallPaper: Interrupt received';
178 verbose "$FindBin::Script v$VERSION";
180 my @argvCopy = @ARGV;
192 local $0 = "$FindBin::Script " . join ' ', @argvCopy;
194 for my $process (@{$processes->table}) {
195 if ($process->cmndline =~ /setbg/ and
196 $process->pid != $$) {
197 kill 12, $process->pid;
203 for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) {
204 error "$opts{bgdirs}[$i] is not a directory", 1 unless -d $opts{bgdirs}[$i];
206 $opts{bgdirs}[$i] = File::Spec->rel2abs ($opts{bgdirs}[$i]);
210 my $setbg = "gsettings";
211 my $setbgOpts= "set org.gnome.desktop.background picture-uri \"file://";
213 my @images = fillPictures;
215 Usage "No images to display. Must specify -bgdirs" unless @images;
217 $SIG{USR2} = \&SwitchWallPaper;
218 $SIG{USR1} = \&fillPictures;
220 my $debugger = $DB::OUT;
223 truncate "$ENV{HOME}/.$FindBin::Script.hist", 0;
225 EnterDaemonMode unless defined $DB::OUT;
228 my $image = escapeHTML ($images[int (rand $#images)]);
230 my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null";
235 error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" .
239 $totals{'Images displayed'}++;
248 sleep $opts{sleep} * 60;
251 @images = fillPictures;