ef0c5d557715ff3265b846c96fcc310030357460
[xscreensaver] / driver / xscreensaver-getimage-file
1 #!/usr/bin/perl -w
2 # Copyright © 2001, 2002, 2003, 2004 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 use POSIX ':fcntl_h';                           # S_ISLNK was here in Perl 5.6
34 import Fcntl ':mode' unless defined &S_ISLNK;   # but it is here in Perl 5.8
35
36
37 my $progname = $0; $progname =~ s@.*/@@g;
38 my $version = q{ $Revision: 1.15 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
39
40 my $verbose = 0;
41
42 # This matches files that we are allowed to use as images (case-insensitive.)
43 # Anything not matching this is ignored.  This is so you can point your
44 # imageDirectory at directory trees that have things other than images in
45 # them, but it assumes that you gave your images sensible file extensions.
46 #
47 my $good_file_re = '\.(gif|p?jpe?g|png|tiff?|xbm|xpm)$';
48
49
50 # These are programs that can be used to put an image file on the root
51 # window (including virtual root windows.)  The first one of these programs
52 # that exists on $PATH will be used (with the file name as the last arg.)
53 #
54 # If you add other programs to this list, please let me know!
55 #
56 my @programs = (
57   "chbg       -once -xscreensaver -max_grow 4 -max_size 100",
58   "xv         -root -quit -viewonly -maxpect +noresetroot -quick24 -rmode 5" .
59   "           -rfg black -rbg black",
60   "xli        -quiet -fullscreen -onroot -center -border black",
61   "xloadimage -quiet -fullscreen -onroot -center -border black",
62
63 # this lame program wasn't built with vroot.h:
64 # "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
65 );
66
67
68 sub pick_displayer {
69   my @names = ();
70
71   foreach my $cmd (@programs) {
72     $_ = $cmd;
73     my ($name) = m/^([^ ]+)/;
74     push @names, "\"$name\"";
75     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
76     foreach my $dir (split (/:/, $ENV{PATH})) {
77       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
78       return $cmd if (-x "$dir/$name");
79     }
80   }
81
82   $names[$#names] = "or " . $names[$#names];
83   printf STDERR "$progname: none of: " . join (", ", @names) .
84                 " were found on \$PATH.\n";
85   exit 1;
86 }
87
88
89 my @all_files = ();
90 my %seen_inodes;
91 my $skip_count = 0;
92 my $dir_count = 1;
93
94 sub find_all_files {
95   my ($dir) = @_;
96
97   print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
98
99   local *DIR;
100   if (! opendir (DIR, $dir)) {
101     print STDERR "$progname: couldn't open $dir: $!\n" if ($verbose);
102     return;
103   }
104   my @files = readdir (DIR);
105   closedir (DIR);
106
107   my @dirs = ();
108
109   foreach my $file (@files) {
110     next if ($file =~ m/^\./);      # ignore dot files/dirs
111
112     $file = "$dir/$file";
113     my @st = stat($file);
114     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
115         $atime,$mtime,$ctime,$blksize,$blocks) = @st;
116
117     if ($#st == -1) {
118       if ($verbose) {
119         my $ll = readlink $file;
120         if (defined ($ll)) {
121           print STDERR "$progname: dangling symlink: $file -> $ll\n";
122         } else {
123           print STDERR "$progname: unreadable: $file\n";
124         }
125       }
126       next;
127     }
128
129     next if ($seen_inodes{"$dev:$ino"}); # break symlink loops
130     $seen_inodes{"$dev:$ino"} = 1;
131
132     if (S_ISDIR($mode)) {
133       push @dirs, $file;
134       $dir_count++;
135       print STDERR "$progname:   found dir  $file\n" if ($verbose > 2);
136     } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
137
138       if ($file =~ m/[~%\#]$/ ||               # backup file, or
139           ! ($file =~ m/$good_file_re/io)) {   # no image extension
140         $skip_count++;
141         print STDERR "$progname:   skip file $file\n" if ($verbose > 2);
142       } else {
143         push @all_files, $file;
144         print STDERR "$progname:   found file $file\n" if ($verbose > 2);
145       }
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) .
165                " file" . ($#all_files == 0 ? "" : "s") .
166                " in $dir_count dir" . ($dir_count == 1 ? "" : "s") .
167                "; skipped $skip_count file" . ($skip_count == 1 ? "" : "s") .
168                ".\n"
169     if ($verbose > 1);
170
171   @all_files = sort(@all_files);
172
173   if ($#all_files < 0) {
174     print STDERR "$progname: no files in $dir\n";
175     exit 1;
176   }
177
178   my $n = int (rand ($#all_files + 1));
179   my $file = $all_files[$n];
180
181   print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
182   return $file;
183 }
184
185
186
187 sub display_file {
188   my ($file, $displayer) = @_;
189
190   if (!defined($displayer)) {
191     print STDOUT "$file\n";
192   }  else {
193     my @cmd = split (/ +/, $displayer);
194     push @cmd, $file;   # do it this way to allow file names with spaces.
195     print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
196       if ($verbose);
197     exec (@cmd) || die;
198   }
199 }
200
201
202 sub find_and_display {
203   my ($dir, $displayer) = @_;
204   my $file = find_random_file ($dir);
205   display_file ($file, $displayer);
206 }
207
208
209 sub usage {
210   print STDERR "usage: $progname [--verbose] [--name] file-or-directory\n\n" .
211   "       Puts the given image file (or a randomly selected image from the\n" .
212   "       given directory) on the root window.  If --name is specified,\n" .
213   "       just prints the selected filename to stdout instead.\n\n";
214   exit 1;
215 }
216
217 sub main {
218   my $dir = undef;
219   my $do_name = 0;
220
221   while ($_ = $ARGV[0]) {
222     shift @ARGV;
223     if ($_ eq "--verbose") { $verbose++; }
224     elsif (m/^-v+$/) { $verbose += length($_)-1; }
225     elsif ($_ eq "--name") { $do_name++; }
226     elsif (m/^-./) { usage; }
227     elsif (!defined($dir)) { $dir = $_; }
228     else { usage; }
229   }
230
231   usage unless (defined($dir));
232   my $displayer = undef;
233
234   $displayer = pick_displayer() unless $do_name;
235
236   if (-d $dir) {
237     find_and_display ($dir, $displayer);
238   } elsif (-f $dir) {
239     display_file ($dir, $displayer);
240   } else {
241     print STDERR "$progname: $dir does not exist\n";
242     usage();
243   }
244 }
245
246 main;
247 exit 0;