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