updated git and svn scripts
[xrzperl.git] / r-wallpapers-daemon
blobcd1d4780af1f9059a7ec33d91f76e4ec9ec5546d
1 #!/usr/bin/perl -w
2 ###APPNAME: r-wallpapers-daemon
3 ###APPAUTHOR: xiaoranzzz
4 ###APPDATE: Tue Mar 25 21:36:40 2008
5 ###APPVER: 0.1
6 ###APPDESC: set wallpaper randomly,timely
7 ###APPUSAGE: [seconds] [directories...]
8 ###APPEXAMPLE:
9 ###APPOPTION:
10 use strict;
11 use Env qw/HOME/;
12 foreach(@ARGV){
13 exit(system("plhelp",$0,@ARGV)) if($_ eq "-h" || $_ eq "--help");
16 my $CONFIG_D = "$HOME/.xiaoranzzz";
17 unless(-d $CONFIG_D) {mkdir $CONFIG_D or die("$!\n");};
18 unless(-d $CONFIG_D) {mkdir $CONFIG_D or die("$!\n");};
19 my $WP_LIST = "$CONFIG_D/wallpapers";
20 #my $WP_LAST = "$CONFIG_D/last-wallpaper";
21 my $wait=shift;
22 $wait=300 unless($wait);
23 my @dirs=@ARGV;
24 my @WPS;
25 if(@dirs) {
26 print STDERR "Building wallpaper list...";
27 open FO,">",$WP_LIST;
28 foreach(@dirs) {
29 next unless(-d $_);
30 open FI,"-|","find '$_' -name '*.png' -or -name '*.jpg'";
31 foreach my $png(<FI>) {
32 chomp($png);
33 push(@WPS,$png);
34 print FO $png,"\n";
36 close FI;
38 close FO;
39 print STDERR " [Done]\n";
41 else {
42 print STDERR "Reading wallpaper list...";
43 if(-f $WP_LIST) {
44 open FI,"<",$WP_LIST;
45 while(<FI>) {
46 chomp;
47 push(@WPS,$_);
49 close FI;
51 print " [DONE]\n";
54 unless(@WPS) {
55 print STDERR "No wallpaper found, the list is empty\n";
56 exit 1;
59 my $nextid=0;
61 #sub on_exit {
62 # print STDERR "$0 quiting ...\n";
63 # print STDERR "Save $WPS[$nextid] as last wallpaper\n";
64 # open FO,">",$WP_LAST;
65 # print FO $WPS[$nextid],"\n";
66 # close FO;
67 # print STDERR "$0 quit\n";
68 # exit 0;
71 my $action="";
72 sub do_exit {
73 $action="exit";
74 print STDERR "\n$0 quit\n";
75 exit 0;
77 sub do_change {
78 $action="change";
81 $SIG{INT}=\&do_exit;
82 $SIG{USR1}=\&do_change;
84 while(1) {
85 $nextid=int(rand(@WPS));
86 my @app = ($nextid % 9) ? qw/set_wallpaper/ : qw/set_wallpaper 1/;
87 if( -f $WPS[$nextid] ) {
88 print STDERR "\nSetting wallpaper to $WPS[$nextid] ...";
89 if(system(@app,$WPS[$nextid])==0) {
90 print STDERR "\rSetting wallpaper to $WPS[$nextid] ... [Done]";
92 else {
93 print STDERR "\rSetting wallpaper to $WPS[$nextid] ... [Failed]";
96 if($action eq "change") {
97 $action = "";
99 elsif($action eq "exit") {
100 do_exit;
101 exit 0;
103 else {
104 sleep $wait;
105 $action="" if($action eq "change");