2 # Copyright © 2001, 2002, 2003, 2004 Jamie Zawinski <jwz@jwz.org>.
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
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.)
16 # The various xscreensaver hacks that manipulate images ("slidescreen",
17 # "jigsaw", etc.) get the image to manipulate by running the
18 # "xscreensaver-getimage" program.
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).
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
36 use bytes; # Larry can take Unicode and shove it up his ass sideways.
37 # Perl 5.8.0 causes us to start getting incomprehensible
38 # errors about UTF-8 all over the place without this.
40 my $progname = $0; $progname =~ s@.*/@@g;
41 my $version = q{ $Revision: 1.18 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
45 # This matches files that we are allowed to use as images (case-insensitive.)
46 # Anything not matching this is ignored. This is so you can point your
47 # imageDirectory at directory trees that have things other than images in
48 # them, but it assumes that you gave your images sensible file extensions.
50 my $good_file_re = '\.(gif|p?jpe?g|png|tiff?|xbm|xpm)$';
52 # JPEG, GIF, and PNG files that are are smaller than this size in either
53 # direction are rejected: this is so that you can use an image directory
54 # that contains both big images and thumbnails, and have it only select
57 my $min_image_width = 255;
58 my $min_image_height = 255;
61 # These are programs that can be used to put an image file on the root
62 # window (including virtual root windows.) The first one of these programs
63 # that exists on $PATH will be used (with the file name as the last arg.)
65 # Generally this isn't used any more; when "xscreensaver-getimage" invokes
66 # this program, it does so with the "-file" argument (meaning that we just
67 # return the file name) and then xscreensaver-getimage loads that file
68 # directly. However, if you invoke "xscreensaver-getimage-file" directly,
69 # without "-file", this will be used to actually load the image.
72 "chbg -once -xscreensaver -max_grow 4 -max_size 100",
73 "xv -root -quit -viewonly -maxpect +noresetroot -quick24 -rmode 5" .
74 " -rfg black -rbg black",
75 "xli -quiet -fullscreen -onroot -center -border black",
76 "xloadimage -quiet -fullscreen -onroot -center -border black",
78 # this lame program wasn't built with vroot.h:
79 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
86 foreach my $cmd (@programs) {
88 my ($name) = m/^([^ ]+)/;
89 push @names, "\"$name\"";
90 print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
91 foreach my $dir (split (/:/, $ENV{PATH})) {
92 print STDERR "$progname: checking $dir/$name\n" if ($verbose > 3);
93 return $cmd if (-x "$dir/$name");
97 $names[$#names] = "or " . $names[$#names];
98 printf STDERR "$progname: none of: " . join (", ", @names) .
99 " were found on \$PATH.\n";
104 my @all_files = (); # list of "good" files we've collected
105 my %seen_inodes; # for breaking recursive symlink loops
106 my $skip_count = 0; # number of files skipped, for diagnostic messages
107 my $dir_count = 1; # number of directories seen, for diagnostic messages
112 print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
115 if (! opendir (DIR, $dir)) {
116 print STDERR "$progname: couldn't open $dir: $!\n" if ($verbose);
119 my @files = readdir (DIR);
124 foreach my $file (@files) {
125 next if ($file =~ m/^\./); # ignore dot files/dirs
127 $file = "$dir/$file";
128 my @st = stat($file);
129 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
130 $atime,$mtime,$ctime,$blksize,$blocks) = @st;
134 my $ll = readlink $file;
136 print STDERR "$progname: dangling symlink: $file -> $ll\n";
138 print STDERR "$progname: unreadable: $file\n";
144 next if ($seen_inodes{"$dev:$ino"}); # break symlink loops
145 $seen_inodes{"$dev:$ino"} = 1;
147 if (S_ISDIR($mode)) {
150 print STDERR "$progname: found dir $file\n" if ($verbose > 2);
151 } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
153 if ($file =~ m/[~%\#]$/ || # backup file, or
154 ! ($file =~ m/$good_file_re/io)) { # no image extension
156 print STDERR "$progname: skip file $file\n" if ($verbose > 2);
158 push @all_files, $file;
159 print STDERR "$progname: found file $file\n" if ($verbose > 2);
161 } elsif ($verbose > 2) {
162 print STDERR "$progname: nonreg $file\n";
172 sub find_random_file {
177 print STDERR "$progname: recursively reading $dir...\n" if ($verbose > 1);
178 find_all_files ($dir);
179 print STDERR "$progname: found " . ($#all_files+1) .
180 " file" . ($#all_files == 0 ? "" : "s") .
181 " in $dir_count dir" . ($dir_count == 1 ? "" : "s") .
182 "; skipped $skip_count file" . ($skip_count == 1 ? "" : "s") .
186 @all_files = sort(@all_files);
188 if ($#all_files < 0) {
189 print STDERR "$progname: no files in $dir\n";
194 for (my $i = 0; $i < $max_tries; $i++) {
196 my $n = int (rand ($#all_files + 1));
197 my $file = $all_files[$n];
198 if (large_enough_p ($file)) {
203 print STDERR "$progname: no suitable images in $dir " .
204 "(after $max_tries tries)\n";
210 my ($file, $displayer) = @_;
212 if (!defined($displayer)) {
213 print STDOUT "$file\n";
215 my @cmd = split (/ +/, $displayer);
216 push @cmd, $file; # do it this way to allow file names with spaces.
217 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
224 sub find_and_display {
225 my ($dir, $displayer) = @_;
226 my $file = find_random_file ($dir);
227 display_file ($file, $displayer);
234 my ($w, $h) = image_file_size ($file);
237 print STDERR "$progname: $file: unable to determine image size\n"
239 # Assume that unknown files are of good sizes: this will happen if
240 # they matched $good_file_re, but we don't have code to parse them.
241 # (This will also happen if the file is junk...)
245 if ($w < $min_image_width || $h < $min_image_height) {
246 print STDERR "$progname: $file: too small ($w x $h)\n" if ($verbose > 1);
250 print STDERR "$progname: $file: $w x $h\n" if ($verbose > 1);
256 # Given the raw body of a GIF document, returns the dimensions of the image.
260 my $type = substr($body, 0, 6);
262 return () unless ($type =~ /GIF8[7,9]a/);
263 $s = substr ($body, 6, 10);
264 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
265 return (($b<<8|$a), ($d<<8|$c));
268 # Given the raw body of a JPEG document, returns the dimensions of the image.
273 my $L = length($body);
275 my $c1 = substr($body, $i, 1); $i++;
276 my $c2 = substr($body, $i, 1); $i++;
277 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
280 while (ord($ch) != 0xDA && $i < $L) {
281 # Find next marker, beginning with 0xFF.
282 while (ord($ch) != 0xFF) {
283 return () if (length($body) <= $i);
284 $ch = substr($body, $i, 1); $i++;
286 # markers can be padded with any number of 0xFF.
287 while (ord($ch) == 0xFF) {
288 return () if (length($body) <= $i);
289 $ch = substr($body, $i, 1); $i++;
292 # $ch contains the value of the marker.
293 my $marker = ord($ch);
295 if (($marker >= 0xC0) &&
298 ($marker != 0xCC)) { # it's a SOFn marker
300 return () if (length($body) <= $i);
301 my $s = substr($body, $i, 4); $i += 4;
302 my ($a,$b,$c,$d) = unpack("C"x4, $s);
303 return (($c<<8|$d), ($a<<8|$b));
306 # We must skip variables, since FFs in variable names aren't
307 # valid JPEG markers.
308 return () if (length($body) <= $i);
309 my $s = substr($body, $i, 2); $i += 2;
310 my ($c1, $c2) = unpack ("C"x2, $s);
311 my $length = ($c1 << 8) | $c2;
312 return () if ($length < 2);
319 # Given the raw body of a PNG document, returns the dimensions of the image.
323 return () unless ($body =~ m/^\211PNG\r/s);
324 my ($bits) = ($body =~ m/^.{12}(.{12})/s);
325 return () unless defined ($bits);
326 return () unless ($bits =~ /^IHDR/);
327 my ($ign, $w, $h) = unpack("a4N2", $bits);
332 # Given the raw body of a GIF, JPEG, or PNG document, returns the dimensions
337 my ($w, $h) = gif_size ($body);
338 if ($w && $h) { return ($w, $h); }
339 ($w, $h) = jpeg_size ($body);
340 if ($w && $h) { return ($w, $h); }
341 # #### TODO: need image parsers for TIFF, XPM, XBM.
342 return png_size ($body);
345 # Returns the dimensions of the image file.
347 sub image_file_size {
351 if (! open (IN, "<$file")) {
352 print STDERR "$progname: $file: $!\n" if ($verbose);
355 binmode (IN); # Larry can take Unicode and shove it up his ass sideways.
358 last if (length($body) > 1024 * 100); # the first 100k should be enough
361 return image_size ($body);
366 print STDERR "usage: $progname [--verbose] [--name] file-or-directory\n\n" .
367 " Puts the given image file (or a randomly selected image from the\n" .
368 " given directory) on the root window. If --name is specified,\n" .
369 " just prints the selected filename to stdout instead.\n\n";
377 while ($_ = $ARGV[0]) {
379 if ($_ eq "--verbose") { $verbose++; }
380 elsif (m/^-v+$/) { $verbose += length($_)-1; }
381 elsif ($_ eq "--name") { $do_name++; }
382 elsif (m/^-./) { usage; }
383 elsif (!defined($dir)) { $dir = $_; }
387 usage unless (defined($dir));
388 my $displayer = undef;
390 $displayer = pick_displayer() unless $do_name;
393 find_and_display ($dir, $displayer);
395 display_file ($dir, $displayer);
397 print STDERR "$progname: $dir does not exist\n";