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] [-ve|rbose] [-d|ebug] [-s|leep <n>] [-bgdir <bgdir>]
37 -u|sage: Displays this usage
39 -d|ebug: Output debug messages
41 -s|leep: Number of minutes to sleep between setting the background
43 -b|gdir: Directory to scan for images (Default: /web/Pictures)
47 This script sets the background image randomly based on images $imgDir. Note
48 if this script is run again it senses that it was previously run and sends the
49 previous script a SIGUSR2 which the script intrprets as "Change the background
59 use Proc::ProcessTable;
61 use CGI qw/:standard/;
63 use lib "$FindBin::Bin/../lib";
70 my $VERSION = '$Revision: 1.10 $';
71 ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
73 my $processes = new Proc::ProcessTable;
77 foreach my $process (@{$processes->table}) {
78 if ($process->cmndline =~ /setbg/ and
79 $process->pid != $$) {
80 kill 12, $process->pid;
87 my $statsFile = Logger->new(
88 name => ".$FindBin::Script.stats",
93 $statsFile->log('At ' . localtime());
95 Stats \%totals, $statsFile;
100 sub fillPictures () {
104 my @pics = `find "$_" -type f -name "*.jpg"`;
110 $totals{images} = scalar @images;
117 sub updateSetBG ($) {
120 open my $log, '>', "$ENV{HOME}/.$FindBin::Script"
121 or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
123 display $image, $log;
127 open $log, '>>', "$ENV{HOME}/.$FindBin::Script.hist"
128 or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1;
130 my $msg = localtime() . ":$image";
139 $0 = "$FindBin::Script " . join ' ', @ARGV;
141 verbose "$FindBin::Script v$VERSION";
146 'usage' => sub { Usage },
147 'verbose' => sub { set_verbose },
148 'debug' => sub { set_debug },
149 'sleep=i' => \$sleep,
150 'bgdir=s' => \@imgDirs,
153 for (my $i = 0; $i < scalar @imgDirs; $i++) {
154 error "$imgDirs[$i] is not a directory", 1 unless -d $imgDirs[$i];
156 $imgDirs[$i] = File::Spec->rel2abs ($imgDirs[$i]);
160 my $setbg = "gsettings";
161 my $setbgOpts= "set org.gnome.desktop.background picture-uri \"file://";
163 my @images = fillPictures;
165 Usage "No images to display. Must specify -bgdir" unless @images;
167 sub SwitchWallPaper {
168 # We don't need to do anything here, just handle the interrupt and
169 # let the while loop continue.
170 debug 'SwitchWallPaper: Interrupt received';
174 $SIG{USR2} = \&SwitchWallPaper;
175 $SIG{USR1} = \&fillPictures;
177 my $debugger = $DB::OUT;
180 truncate "$ENV{HOME}/.$FindBin::Script.hist", 0;
182 EnterDaemonMode unless defined $DB::OUT;
185 my $image = escapeHTML ($images[int (rand $#images)]);
187 my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null";
192 error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" .
196 $totals{'Images displayed'}++;
206 @images = fillPictures;