ftp://netsw.org/x11/tools/desktop/xscreensaver-4.07.tar.gz
[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 use diagnostics;
28 use strict;
29
30 use POSIX;
31 use Fcntl;
32
33 # Apparently the "old way" to get S_ISLNK and friends is to do this:
34 #
35 use POSIX ':fcntl_h';
36
37 # But apparently the "new way" is to do this:
38 #
39 #  use Fcntl ':mode';
40 #
41 # but of course that will generate an error on "old" (pre-5.6?) Perl versions.
42 # So we do it like this instead:
43 #
44 BEGIN {
45   if (! defined(&S_ISLNK)) {  # perhaps defined by "POSIX"?
46     require Fcntl;
47     import  Fcntl ':mode';    # if not, look for it in "Fcntl".
48   }
49 }
50
51
52 my $progname = $0; $progname =~ s@.*/@@g;
53 my $version = q{ $Revision: 1.11 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
54
55 my $verbose = 0;
56
57 # These are programs that can be used to put an image file on the root
58 # window (including virtual root windows.)  The first one of these programs
59 # that exists on $PATH will be used (with the file name as the last arg.)
60 #
61 # If you add other programs to this list, please let me know!
62 #
63 my @programs = (
64   "chbg       -once -xscreensaver -max_grow 4 -max_size 100",
65   "xv         -root -quit -viewonly -maxpect +noresetroot -quick24 -rmode 5" .
66   "           -rfg black -rbg black",
67   "xli        -quiet -fullscreen -onroot -center -border black",
68   "xloadimage -quiet -fullscreen -onroot -center -border black",
69
70 # this lame program wasn't built with vroot.h:
71 # "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
72 );
73
74 sub pick_displayer {
75   my @names = ();
76
77   foreach my $cmd (@programs) {
78     $_ = $cmd;
79     my ($name) = m/^([^ ]+)/;
80     push @names, "\"$name\"";
81     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
82     foreach my $dir (split (/:/, $ENV{PATH})) {
83       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
84       return $cmd if (-x "$dir/$name");
85     }
86   }
87
88   $names[$#names] = "or " . $names[$#names];
89   printf STDERR "$progname: none of: " . join (", ", @names) .
90                 " were found on \$PATH.\n";
91   exit 1;
92 }
93
94
95 my @all_files = ();
96 my %seen_inodes;
97
98 sub find_all_files {
99   my ($dir) = @_;
100
101   print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
102
103   local *DIR;
104   if (! opendir (DIR, $dir)) {
105     print STDERR "$progname: couldn't open $dir: $!\n" if ($verbose);
106     return;
107   }
108   my @files = readdir (DIR);
109   closedir (DIR);
110
111   my @dirs = ();
112
113   foreach my $file (@files) {
114     next if ($file =~ m/^\./);      # ignore dot files
115     next if ($file =~ m/[~%\#]$/);  # ignore backup files
116     next if ($file =~ m/\.(BAK|bak|tmp|orig|rej|rpmsave)$/);
117     next if ($file eq "core");
118
119     $file = "$dir/$file";
120     my @st = stat($file);
121     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
122         $atime,$mtime,$ctime,$blksize,$blocks) = @st;
123
124     if ($#st == -1) {
125       if ($verbose) {
126         my $ll = readlink $file;
127         if (defined ($ll)) {
128           print STDERR "$progname: dangling symlink: $file -> $ll\n";
129         } else {
130           print STDERR "$progname: unreadable: $file\n";
131         }
132       }
133       next;
134     }
135
136     next if ($seen_inodes{$ino}); # break symlink loops
137     $seen_inodes{$ino} = 1;
138
139     if (S_ISDIR($mode)) {
140       push @dirs, $file;
141       print STDERR "$progname:   found dir  $file\n" if ($verbose > 2);
142     } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
143       push @all_files, $file;
144       print STDERR "$progname:   found file $file\n" if ($verbose > 2);
145     } elsif ($verbose > 2) {
146       print STDERR "$progname:   nonreg $file\n";
147     }
148   }
149
150   foreach (@dirs) {
151     find_all_files ($_);
152   }
153 }
154
155
156 sub find_random_file {
157   my ($dir) = @_;
158
159   $dir =~ s@/+$@@g;
160
161   print STDERR "$progname: recursively reading $dir...\n" if ($verbose > 1);
162   find_all_files ($dir);
163   print STDERR "$progname: found " . ($#all_files+1) . " files\n"
164     if ($verbose > 1);
165
166   @all_files = sort(@all_files);
167
168   if ($#all_files < 0) {
169     print STDERR "$progname: no files in $dir\n";
170     exit 1;
171   }
172
173   my $n = int (rand ($#all_files + 1));
174   my $file = $all_files[$n];
175
176   print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
177   return $file;
178 }
179
180
181
182 sub display_file {
183   my ($file, $displayer) = @_;
184
185   if (!defined($displayer)) {
186     print STDOUT "$file\n";
187   }  else {
188     my @cmd = split (/ +/, $displayer);
189     push @cmd, $file;   # do it this way to allow file names with spaces.
190     print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
191       if ($verbose);
192     exec (@cmd) || die;
193   }
194 }
195
196
197 sub find_and_display {
198   my ($dir, $displayer) = @_;
199   my $file = find_random_file ($dir);
200   display_file ($file, $displayer);
201 }
202
203
204 sub usage {
205   print STDERR "usage: $progname [--verbose] [--name] directory\n";
206   print STDERR "Puts a randomly selected image on the root window.\n";
207   print STDERR "With --name, merely prints the filename to stdout.\n";
208   exit 1;
209 }
210
211 sub main {
212   my $dir = undef;
213   my $do_name = 0;
214
215   while ($_ = $ARGV[0]) {
216     shift @ARGV;
217     if ($_ eq "--verbose") { $verbose++; }
218     elsif (m/^-v+$/) { $verbose += length($_)-1; }
219     elsif ($_ eq "--name") { $do_name++; }
220     elsif (m/^-./) { usage; }
221     elsif (!defined($dir)) { $dir = $_; }
222     else { usage; }
223   }
224
225   usage unless (defined($dir));
226   my $displayer = undef;
227
228   $displayer = pick_displayer() unless $do_name;
229
230   if (-d $dir) {
231     find_and_display ($dir, $displayer);
232   } elsif (-f $dir) {
233     display_file ($dir, $displayer);
234   } else {
235     usage();
236   }
237 }
238
239 main;
240 exit 0;