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 -l|ockscreen: Change lockscreen backround (Default: False)
46 -b|gdirs: Directories to scan for images
50 This script sets the background image randomly based on images found in bgdirs.
51 Note if this script is run again it senses that it was previously run and sends
52 the previous script a SIGUSR2 which the script intrprets as "Change the
53 background now", then exits.
55 Data is written to the following files:
57 ~/.setbg: Contains the filename of the current background image
58 ~/.setbg.hist Contains a history of all images displayed for this run
59 ~/.setbg.stats Contains statistical information for the current run
61 Also note that this script will process a SIGUSR1 to mean "re-evaluate the
62 contents of the bgdirs incase it has changed and display a new image". This is
63 useful for script to be able to alert setbg that something has changed. For
64 example, a script named rmbg might look at ~/.setbg to get the name of the
65 current background image file and remove it then signal setbg with SIGUSR1 to
66 have it re-evaluate the state of bgdirs.
68 Finally setbg will perform the this re-evaluation at midnight everyday. This is
69 useful because we point setbg to look at -bgdirs from Dropbox where Camera
70 Uploads is included and new pictures can arrive everyday.
79 use Proc::ProcessTable;
81 use CGI qw/:standard/;
83 use lib "$FindBin::Bin/../lib";
92 my $VERSION = '$Revision: 1.12 $';
93 ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
95 my $processes = Proc::ProcessTable->new;
99 usage => sub { pod2usage },
100 help => sub { pod2usage (-verbose => 2)},
101 verbose => sub { set_verbose },
102 debug => sub { set_debug },
107 sub displayStats () {
108 my $statsFile = Logger->new(
109 name => ".$FindBin::Script.stats",
114 $statsFile->log('At ' . localtime());
115 $statsFile->log('Sleep: ' . $opts{sleep});
116 $statsFile->log('Image directories:');
118 for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) {
119 $statsFile->log("\t$opts{bgdirs}[$i]: $opts{bgdircnt}[$i]")
122 Stats \%totals, $statsFile;
127 sub fillPictures () {
132 for (@{$opts{bgdirs}}) {
133 my ($status, @pics) = Execute "find \"$_\" -type f";
137 push @images, grep(/jpg$|png$|gif$/i, @pics);
139 push @{$opts{bgdircnt}}, scalar @pics;
144 $totals{images} = scalar @images;
148 sub writeHistory($) {
151 open my $hist, '>>', "$ENV{HOME}/.$FindBin::Script.hist"
152 or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1;
154 $msg = localtime() . $msg;
161 sub updateSetBG($$) {
162 my ($bgimage, $lockimage) = @_;
164 open my $setbg, '>', "$ENV{HOME}/.$FindBin::Script"
165 or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1;
167 display $bgimage, $setbg;
171 my $msg = ":$bgimage";
172 $msg .= " lock:$lockimage" if $opts{lockscreen};
179 sub SwitchWallPaper {
180 # We don't need to do anything here, just handle the interrupt and
181 # let the while loop continue.
182 debug 'SwitchWallPaper: Interrupt received';
189 verbose "$FindBin::Script v$VERSION";
191 my @argvCopy = @ARGV;
204 local $0 = "$FindBin::Script " . join ' ', @argvCopy;
206 for my $process (@{$processes->table}) {
207 if ($process->cmndline =~ /setbg/ and
208 $process->pid != $$) {
209 kill 12, $process->pid;
215 for (my $i = 0; $i < scalar @{$opts{bgdirs}}; $i++) {
216 error "$opts{bgdirs}[$i] is not a directory", 1 unless -d $opts{bgdirs}[$i];
218 $opts{bgdirs}[$i] = File::Spec->rel2abs ($opts{bgdirs}[$i]);
222 my $setbg = "gsettings";
223 my $setbgOpts = "set org.gnome.desktop.background picture-uri \"file://";
224 my $setLockOpts = "set org.gnome.desktop.screensaver picture-uri \"file://";
226 my @images = fillPictures;
228 Usage "No images to display. Must specify -bgdirs" unless @images;
230 $SIG{USR2} = \&SwitchWallPaper;
231 $SIG{USR1} = \&fillPictures;
233 my $debugger = $DB::OUT;
236 truncate "$ENV{HOME}/.$FindBin::Script.hist", 0;
238 EnterDaemonMode unless defined $DB::OUT;
240 my $pickNewImages = 1;
243 my ($bgimage, $lockimage);
245 if ($pickNewImages) {
246 $bgimage = escapeHTML ($images[int (rand $#images)]);
247 $lockimage = escapeHTML ($images[int (rand $#images)]);
252 my ($status, @output) = Execute("xset q | grep Monitor | awk '{print \$3}'");
254 if ($status or $output[0] eq 'Off') {
255 writeHistory ":Monitor off, not setting background to $bgimage - will keep trying";
261 my $cmd = "$setbg $setbgOpts$bgimage\" 2> /dev/null";
263 ($status, @output) = Execute $cmd;
266 error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" .
270 $totals{'Images displayed'}++;
273 if ($opts{lockscreen}) {
274 $cmd = "$setbg $setLockOpts$lockimage\" 2> /dev/null";
276 ($status, @output) = Execute $cmd;
279 error "Trying to set lock screen - command used \"$cmd\"\n\nOutput\n\n" .
283 $totals{'Lock screens displayed'}++;
287 updateSetBG $bgimage, $lockimage;
294 sleep $opts{sleep} * 60;
297 @images = fillPictures;