#!/usr/bin/perl -w
# rotateroot.pl - change l'image de fond périodiquement.
#
# Pioche au hasard dans une liste un fichier pour utiliser en fond d'écran
# puis change toutes les 15 minutes sur le même modèle.
#
# - Rappeler le programme alors qu'il tourne lui fait changer
# immédiatement le fond ;
# - Rappeler le programme avec comme seul argument "stop" l'arrête ;
# - Rappeler le programme avec comme seul argument "reload" le relance.
#
# Par : oz <oz@tuxaco.net> ven 17 jun 2005 22:13:40 CEST
use strict;
use warnings;
my $self = $0;
my $time = 15 * 60;
my $dir = $ENV{'HOME'} . '/img';
my $pid_file = $ENV{'HOME'} . '/.rotateroot.pid';
my $img_list = shift || 'rotatelist.txt';
my $rootprog = 'Esetroot -center';
my $sighup = 1;
my $sigterm = 15;
my @list = ();
sub change_root;
sub clean;
sub link_pid_file;
sub replace;
sub set_list;
sub send_signal;
$SIG{'HUP'} = \&change_root;
$SIG{'TERM'} = \&clean;
$SIG{'INT'} = \&clean;
exit send_signal($sigterm) if ( -f $pid_file && 'stop' eq $img_list );
exit replace($pid_file) if ( -f $pid_file && 'reload' eq $img_list );
exit send_signal($sighup) if ( -f $pid_file );
fork and exit;
link_pid_file;
set_list;
# Change l'image et attend $time secondes.
srand(time ^ $$);
change_root;
while (1) { ($time == sleep $time) && change_root; }
# -----------------------------------------------------------------------
# Change le fond avec une image au hasard dans la liste
sub change_root
{
my $num = int rand(@list);
my $foo = `$rootprog $dir$list[$num]`;
}
# Nettoie avant de sortir
sub clean
{
my $sig = shift;
(-f $pid_file) && unlink $pid_file;
exit 0;
}
# Créé le fichier avec ton zoli PID dedans
sub link_pid_file
{
open G, ">$pid_file" or die "can't create pid file.\n";
print G $$;
close G;
}
# Envoie le signal passé en argument, et sort.
sub send_signal
{
my ( $signal ) = @_;
defined $signal or return;
my $pid = `cat $pid_file`;
kill $signal, $pid;
0;
}
# End running process, and replace it with a new one
sub replace
{
send_signal($sigterm);
exec($self);
}
# Récupère la liste de fichiers
sub set_list
{
@list = ();
open F, "$dir/$img_list" or die "no list.\n";
while (<F>) {
s/^\.//;
push @list, $_;
}
close F;
}