http://svn.poeml.de/viewvc/ppc/src-unpacked/xscreensaver/xscreensaver-4.12.tar.bz2...
[xscreensaver] / driver / xscreensaver-getimage-file
1 #!/usr/bin/perl -w
2 # Copyright © 2001, 2002, 2003 Jamie Zawinski <jwz@jwz.org>.
3 #
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation.  No representations are made about the suitability of this
9 # software for any purpose.  It is provided "as is" without express or 
10 # implied warranty.
11 #
12 # This program attempts to locate a random image from the specified directory,
13 # and load it on to the root window, using some other program that can decode
14 # image files.  (It attempts to find such a program.)
15 #
16 # The various xscreensaver hacks that manipulate images ("slidescreen",
17 # "jigsaw", etc.) get the image to manipulate by running the
18 # "xscreensaver-getimage" program.
19 #
20 # "xscreensaver-getimage" will invoke this program, depending on the
21 # value of the "chooseRandomImages" and "imageDirectory" settings in
22 # the ~/.xscreensaver file (or /usr/lib/X11/app-defaults/XScreenSaver).
23 #
24 # Created: 12-Apr-01.
25
26 require 5;
27 #require v5.6;
28 use diagnostics;
29 use strict;
30
31 use POSIX;
32 use Fcntl;
33
34 # Apparently the "old way" to get S_ISLNK and friends is to do this:
35 #
36 use POSIX ':fcntl_h';
37
38 # But apparently the "new way" is to do this:
39 #
40 #  use Fcntl ':mode';
41 #
42 # but of course that will generate an error on "old" (pre-5.6?) Perl versions.
43 # So we do it like this instead:
44 #
45 BEGIN {
46   if (! defined(&S_ISLNK)) {  # perhaps defined by "POSIX"?
47     require Fcntl;
48     import  Fcntl ':mode';    # if not, look for it in "Fcntl".
49   }
50 }
51
52
53 my $progname = $0; $progname =~ s@.*/@@g;
54 my $version = q{ $Revision: 1.13 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
55
56 my $verbose = 0;
57
58 # This matches files that we are allowed to use as images (case-insensitive.)
59 # Anything not matching this is ignored.  This is so you can point your
60 # imageDirectory at directory trees that have things other than images in
61 # them, but it assumes that you gave your images sensible file extensions.
62 #
63 my $good_file_re = '\.(gif|p?jpe?g|png|tiff?|xbm|xpm)$';
64
65
66 # These are programs that can be used to put an image file on the root
67 # window (including virtual root windows.)  The first one of these programs
68 # that exists on $PATH will be used (with the file name as the last arg.)
69 #
70 # If you add other programs to this list, please let me know!
71 #
72 my @programs = (
73   "chbg       -once -xscreensaver -max_grow 4 -max_size 100",
74   "xv         -root -quit -viewonly -maxpect +noresetroot -quick24 -rmode 5" .
75   "           -rfg black -rbg black",
76   "xli        -quiet -fullscreen -onroot -center -border black",
77   "xloadimage -quiet -fullscreen -onroot -center -border black",
78
79 # this lame program wasn't built with vroot.h:
80 # "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
81 );
82
83
84 sub pick_displayer {
85   my @names = ();
86
87   foreach my $cmd (@programs) {
88     $_ = $cmd;
89     my ($name) = m/^([^ ]+)/;
90     push @names, "\"$name\"";
91     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
92     foreach my $dir (split (/:/, $ENV{PATH})) {
93       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
94       return $cmd if (-x "$dir/$name");
95     }
96   }
97
98   $names[$#names] = "or " . $names[$#names];
99   printf STDERR "$progname: none of: " . join (", ", @names) .
100                 " were found on \$PATH.\n";
101   exit 1;
102 }
103
104
105 my @all_files = ();
106 my %seen_inodes;
107 my $skip_count = 0;
108 my $dir_count = 1;
109
110 sub find_all_files {
111   my ($dir) = @_;
112
113   print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
114
115   local *DIR;
116   if (! opendir (DIR, $dir)) {
117     print STDERR "$progname: couldn't open $dir: $!\n" if ($verbose);
118     return;
119   }
120   my @files = readdir (DIR);
121   closedir (DIR);
122
123   my @dirs = ();
124
125   foreach my $file (@files) {
126     next if ($file =~ m/^\./);      # ignore dot files/dirs
127
128     $file = "$dir/$file";
129     my @st = stat($file);
130     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
131         $atime,$mtime,$ctime,$blksize,$blocks) = @st;
132
133     if ($#st == -1) {
134       if ($verbose) {
135         my $ll = readlink $file;
136         if (defined ($ll)) {
137           print STDERR "$progname: dangling symlink: $file -> $ll\n";
138         } else {
139           print STDERR "$progname: unreadable: $file\n";
140         }
141       }
142       next;
143     }
144
145     next if ($seen_inodes{$ino}); # break symlink loops
146     $seen_inodes{$ino} = 1;
147
148     if (S_ISDIR($mode)) {
149       push @dirs, $file;
150       $dir_count++;
151       print STDERR "$progname:   found dir  $file\n" if ($verbose > 2);
152     } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
153
154       if ($file =~ m/[~%\#]$/ ||               # backup file, or
155           ! ($file =~ m/$good_file_re/io)) {   # no image extension
156         $skip_count++;
157         print STDERR "$progname:   skip file $file\n" if ($verbose > 2);
158       } else {
159         push @all_files, $file;
160         print STDERR "$progname:   found file $file\n" if ($verbose > 2);
161       }
162     } elsif ($verbose > 2) {
163       print STDERR "$progname:   nonreg $file\n";
164     }
165   }
166
167   foreach (@dirs) {
168     find_all_files ($_);
169   }
170 }
171
172
173 sub find_random_file {
174   my ($dir) = @_;
175
176   $dir =~ s@/+$@@g;
177
178   print STDERR "$progname: recursively reading $dir...\n" if ($verbose > 1);
179   find_all_files ($dir);
180   print STDERR "$progname: found " . ($#all_files+1) .
181                " file" . ($#all_files == 0 ? "" : "s") .
182                " in $dir_count dir" . ($dir_count == 1 ? "" : "s") .
183                "; skipped $skip_count file" . ($skip_count == 1 ? "" : "s") .
184                ".\n"
185     if ($verbose > 1);
186
187   @all_files = sort(@all_files);
188
189   if ($#all_files < 0) {
190     print STDERR "$progname: no files in $dir\n";
191     exit 1;
192   }
193
194   my $n = int (rand ($#all_files + 1));
195   my $file = $all_files[$n];
196
197   print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
198   return $file;
199 }
200
201
202
203 sub display_file {
204   my ($file, $displayer) = @_;
205
206   if (!defined($displayer)) {
207     print STDOUT "$file\n";
208   }  else {
209     my @cmd = split (/ +/, $displayer);
210     push @cmd, $file;   # do it this way to allow file names with spaces.
211     print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
212       if ($verbose);
213     exec (@cmd) || die;
214   }
215 }
216
217
218 sub find_and_display {
219   my ($dir, $displayer) = @_;
220   my $file = find_random_file ($dir);
221   display_file ($file, $displayer);
222 }
223
224
225 sub usage {
226   print STDERR "usage: $progname [--verbose] [--name] directory\n";
227   print STDERR "Puts a randomly selected image on the root window.\n";
228   print STDERR "With --name, merely prints the filename to stdout.\n";
229   exit 1;
230 }
231
232 sub main {
233   my $dir = undef;
234   my $do_name = 0;
235
236   while ($_ = $ARGV[0]) {
237     shift @ARGV;
238     if ($_ eq "--verbose") { $verbose++; }
239     elsif (m/^-v+$/) { $verbose += length($_)-1; }
240     elsif ($_ eq "--name") { $do_name++; }
241     elsif (m/^-./) { usage; }
242     elsif (!defined($dir)) { $dir = $_; }
243     else { usage; }
244   }
245
246   usage unless (defined($dir));
247   my $displayer = undef;
248
249   $displayer = pick_displayer() unless $do_name;
250
251   if (-d $dir) {
252     find_and_display ($dir, $displayer);
253   } elsif (-f $dir) {
254     display_file ($dir, $displayer);
255   } else {
256     usage();
257   }
258 }
259
260 main;
261 exit 0;