1 #!/scratchbox/tools/bin/perl -w
2 # Copyright © 2001-2008 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 chooses a random file from under the given directory, and
13 # prints its name. The file will be an image file whose dimensions are
14 # larger than a certain minimum size.
16 # The various xscreensaver hacks that manipulate images ("jigsaw", etc.) get
17 # the image to manipulate by running the "xscreensaver-getimage" program.
19 # Under X11, the "xscreensaver-getimage" program invokes this script,
20 # depending on the value of the "chooseRandomImages" and "imageDirectory"
21 # settings in the ~/.xscreensaver file (or .../app-defaults/XScreenSaver).
23 # Under Cocoa, this script lives inside the .saver bundle, and is invoked
24 # directly from utils/grabclient.c.
29 #use diagnostics; # Fails on some MacOS 10.5 systems
35 use Fcntl ':flock'; # import LOCK_* constants
37 use POSIX ':fcntl_h'; # S_ISDIR was here in Perl 5.6
38 import Fcntl ':mode' unless defined &S_ISDIR; # but it is here in Perl 5.8
40 use bytes; # Larry can take Unicode and shove it up his ass sideways.
41 # Perl 5.8.0 causes us to start getting incomprehensible
42 # errors about UTF-8 all over the place without this.
44 my $progname = $0; $progname =~ s@.*/@@g;
45 my $version = q{ $Revision: 1.26 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
49 # Whether to use MacOS X's Spotlight to generate the list of files.
50 # When set to -1, uses Spotlight if "mdfind" exists.
52 # (In my experience, this isn't actually any faster, and might not find
53 # everything if your Spotlight index is out of date, which happens often.)
55 my $use_spotlight_p = 0;
57 # Whether to cache the results of the last run.
61 # Regenerate the cache if it is older than this many seconds.
63 my $cache_max_age = 60 * 60 * 3; # 3 hours
66 # This matches files that we are allowed to use as images (case-insensitive.)
67 # Anything not matching this is ignored. This is so you can point your
68 # imageDirectory at directory trees that have things other than images in
69 # them, but it assumes that you gave your images sensible file extensions.
71 my @good_extensions = ('jpg', 'jpeg', 'pjpeg', 'pjpg', 'png', 'gif',
72 'tif', 'tiff', 'xbm', 'xpm');
73 my $good_file_re = '\.(' . join("|", @good_extensions) . ')$';
75 # This matches file extensions that might occur in an image directory,
76 # and that are never used in the name of a subdirectory. This is an
77 # optimization that prevents us from having to stat() those files to
78 # tell whether they are directories or not. (It speeds things up a
79 # lot. Don't give your directories stupid names.)
81 my @nondir_extensions = ('ai', 'bmp', 'bz2', 'cr2', 'crw', 'db',
82 'dmg', 'eps', 'gz', 'hqx', 'htm', 'html', 'icns', 'ilbm', 'mov',
83 'nef', 'pbm', 'pdf', 'pl', 'ppm', 'ps', 'psd', 'sea', 'sh', 'shtml',
84 'tar', 'tgz', 'thb', 'txt', 'xcf', 'xmp', 'Z', 'zip' );
85 my $nondir_re = '\.(' . join("|", @nondir_extensions) . ')$';
88 # JPEG, GIF, and PNG files that are are smaller than this are rejected:
89 # this is so that you can use an image directory that contains both big
90 # images and thumbnails, and have it only select the big versions.
92 my $min_image_width = 255;
93 my $min_image_height = 255;
95 my @all_files = (); # list of "good" files we've collected
96 my %seen_inodes; # for breaking recursive symlink loops
98 # For diagnostic messages:
100 my $dir_count = 1; # number of directories seen
101 my $stat_count = 0; # number of files/dirs stat'ed
102 my $skip_count_unstat = 0; # number of files skipped without stat'ing
103 my $skip_count_stat = 0; # number of files skipped after stat
108 print STDERR "$progname: + reading dir $dir/...\n" if ($verbose > 1);
111 if (! opendir (DIR, $dir)) {
112 print STDERR "$progname: couldn't open $dir: $!\n" if ($verbose);
115 my @files = readdir (DIR);
120 foreach my $file (@files) {
121 next if ($file =~ m/^\./); # silently ignore dot files/dirs
123 if ($file =~ m/[~%\#]$/) { # ignore backup files (and dirs...)
124 $skip_count_unstat++;
125 print STDERR "$progname: - skip file $file\n" if ($verbose > 1);
128 $file = "$dir/$file";
130 if ($file =~ m/$good_file_re/io) {
132 # Assume that files ending in .jpg exist and are not directories.
134 push @all_files, $file;
135 print STDERR "$progname: - found file $file\n" if ($verbose > 1);
137 } elsif ($file =~ m/$nondir_re/io) {
139 # Assume that files ending in .html are not directories.
141 $skip_count_unstat++;
142 print STDERR "$progname: -- skip file $file\n" if ($verbose > 1);
146 # Now we need to stat the file to see if it's a subdirectory.
148 # Note: we could use the trick of checking "nlinks" on the parent
149 # directory to see if this directory contains any subdirectories,
150 # but that would exclude any symlinks to directories.
152 my @st = stat($file);
153 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
154 $atime,$mtime,$ctime,$blksize,$blocks) = @st;
160 my $ll = readlink $file;
162 print STDERR "$progname: + dangling symlink: $file -> $ll\n";
164 print STDERR "$progname: + unreadable: $file\n";
170 next if ($seen_inodes{"$dev:$ino"}); # break symlink loops
171 $seen_inodes{"$dev:$ino"} = 1;
173 if (S_ISDIR($mode)) {
176 print STDERR "$progname: + found dir $file\n" if ($verbose > 1);
180 print STDERR "$progname: + skip file $file\n" if ($verbose > 1);
191 sub spotlight_all_files {
195 # "public.image" matches all (indexed) images, including Photoshop, etc.
196 # push @terms, "kMDItemContentTypeTree == 'public.image'";
197 foreach (@good_extensions) {
199 # kMDItemFSName hits the file system every time: much worse than "find".
200 # push @terms, "kMDItemFSName == '*.$_'";
202 # kMDItemDisplayName matches against the name in the Spotlight index,
203 # but won't find files that (for whatever reason) didn't get indexed.
204 push @terms, "kMDItemDisplayName == '*.$_'";
207 $dir =~ s@([^-_/a-z\d.,])@\\$1@gsi; # quote for sh
208 my $cmd = "mdfind -onlyin $dir \"" . join (' || ', @terms) . "\"";
210 print STDERR "$progname: executing: $cmd\n" if ($verbose > 1);
211 @all_files = split (/[\r\n]+/, `$cmd`);
215 # If we're using cacheing, read the cache file and return its contents,
216 # if any. This also holds an exclusive lock on the cache file, which
217 # has the additional benefit that if two copies of this program are
218 # running at once, one will wait for the other, instead of both of
219 # them spanking the same file system at the same time.
222 my $cache_file_name = undef;
223 my $read_cache_p = 0;
228 return () unless ($cache_p);
230 my $dd = "$ENV{HOME}/Library/Caches"; # MacOS location
232 $cache_file_name = "$dd/org.jwz.xscreensaver.getimage.cache";
233 } elsif (-d "$ENV{HOME}/tmp") {
234 $cache_file_name = "$ENV{HOME}/tmp/.xscreensaver-getimage.cache";
236 $cache_file_name = "$ENV{HOME}/.xscreensaver-getimage.cache";
239 my $file = $cache_file_name;
240 open (CACHE_FILE, "+>>$file") || error ("unable to write $file: $!");
241 flock (CACHE_FILE, LOCK_EX) || error ("unable to lock $file: $!");
242 seek (CACHE_FILE, 0, 0) || error ("unable to rewind $file: $!");
244 print STDERR "$progname: reading cache $cache_file_name\n"
247 my $mtime = (stat(CACHE_FILE))[9];
249 if ($mtime + $cache_max_age < time) {
250 print STDERR "$progname: cache is too old\n" if ($verbose);
254 my $odir = <CACHE_FILE>;
255 $odir =~ s/[\r\n]+$//s if defined ($odir);
256 if (!defined ($odir) || ($dir ne $odir)) {
257 print STDERR "$progname: cache is for $odir, not $dir\n"
258 if ($verbose && $odir);
263 while (<CACHE_FILE>) {
265 push @files, "$odir/$_";
268 print STDERR "$progname: " . ($#files+1) . " files in cache\n"
279 return unless ($cache_p);
281 # If we read the cache, just close it without rewriting it.
282 # If we didn't read it, then write it now.
284 if (! $read_cache_p) {
286 truncate (CACHE_FILE, 0) ||
287 error ("unable to truncate $cache_file_name: $!");
288 seek (CACHE_FILE, 0, 0) ||
289 error ("unable to rewind $cache_file_name: $!");
291 if ($#all_files >= 0) {
292 print CACHE_FILE "$dir\n";
294 foreach (@all_files) {
295 my $f = $_; # stupid Perl. do this to avoid modifying @all_files!
296 $f =~ s@^$re/@@so || die;
297 print CACHE_FILE "$f\n";
301 print STDERR "$progname: cached " . ($#all_files+1) . " files\n"
305 flock (CACHE_FILE, LOCK_UN) ||
306 error ("unable to unlock $cache_file_name: $!");
311 sub find_random_file($) {
316 if ($use_spotlight_p == -1) {
317 $use_spotlight_p = 0;
318 if (-x '/usr/bin/mdfind') {
319 $use_spotlight_p = 1;
323 @all_files = read_cache ($dir);
325 if ($#all_files >= 0) {
326 # got it from the cache...
328 } elsif ($use_spotlight_p) {
329 print STDERR "$progname: spotlighting $dir...\n" if ($verbose);
330 spotlight_all_files ($dir);
331 print STDERR "$progname: found " . ($#all_files+1) .
332 " file" . ($#all_files == 0 ? "" : "s") .
336 print STDERR "$progname: recursively reading $dir...\n" if ($verbose);
337 find_all_files ($dir);
338 print STDERR "$progname: " .
339 "f=" . ($#all_files+1) . "; " .
342 "skip=${skip_count_unstat}+$skip_count_stat=" .
343 ($skip_count_unstat + $skip_count_stat) .
350 @all_files = sort(@all_files);
352 if ($#all_files < 0) {
353 print STDERR "$progname: no files in $dir\n";
358 for (my $i = 0; $i < $max_tries; $i++) {
360 my $n = int (rand ($#all_files + 1));
361 my $file = $all_files[$n];
362 if (large_enough_p ($file)) {
367 print STDERR "$progname: no suitable images in $dir " .
368 "(after $max_tries tries)\n";
376 my ($w, $h) = image_file_size ($file);
379 print STDERR "$progname: $file: unable to determine image size\n"
381 # Assume that unknown files are of good sizes: this will happen if
382 # they matched $good_file_re, but we don't have code to parse them.
383 # (This will also happen if the file is junk...)
387 if ($w < $min_image_width || $h < $min_image_height) {
388 print STDERR "$progname: $file: too small ($w x $h)\n" if ($verbose);
392 print STDERR "$progname: $file: $w x $h\n" if ($verbose);
398 # Given the raw body of a GIF document, returns the dimensions of the image.
402 my $type = substr($body, 0, 6);
404 return () unless ($type =~ /GIF8[7,9]a/);
405 $s = substr ($body, 6, 10);
406 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
407 return (($b<<8|$a), ($d<<8|$c));
410 # Given the raw body of a JPEG document, returns the dimensions of the image.
415 my $L = length($body);
417 my $c1 = substr($body, $i, 1); $i++;
418 my $c2 = substr($body, $i, 1); $i++;
419 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
422 while (ord($ch) != 0xDA && $i < $L) {
423 # Find next marker, beginning with 0xFF.
424 while (ord($ch) != 0xFF) {
425 return () if (length($body) <= $i);
426 $ch = substr($body, $i, 1); $i++;
428 # markers can be padded with any number of 0xFF.
429 while (ord($ch) == 0xFF) {
430 return () if (length($body) <= $i);
431 $ch = substr($body, $i, 1); $i++;
434 # $ch contains the value of the marker.
435 my $marker = ord($ch);
437 if (($marker >= 0xC0) &&
440 ($marker != 0xCC)) { # it's a SOFn marker
442 return () if (length($body) <= $i);
443 my $s = substr($body, $i, 4); $i += 4;
444 my ($a,$b,$c,$d) = unpack("C"x4, $s);
445 return (($c<<8|$d), ($a<<8|$b));
448 # We must skip variables, since FFs in variable names aren't
449 # valid JPEG markers.
450 return () if (length($body) <= $i);
451 my $s = substr($body, $i, 2); $i += 2;
452 my ($c1, $c2) = unpack ("C"x2, $s);
453 my $length = ($c1 << 8) | $c2;
454 return () if ($length < 2);
461 # Given the raw body of a PNG document, returns the dimensions of the image.
465 return () unless ($body =~ m/^\211PNG\r/s);
466 my ($bits) = ($body =~ m/^.{12}(.{12})/s);
467 return () unless defined ($bits);
468 return () unless ($bits =~ /^IHDR/);
469 my ($ign, $w, $h) = unpack("a4N2", $bits);
474 # Given the raw body of a GIF, JPEG, or PNG document, returns the dimensions
479 return () if (length($body) < 10);
480 my ($w, $h) = gif_size ($body);
481 if ($w && $h) { return ($w, $h); }
482 ($w, $h) = jpeg_size ($body);
483 if ($w && $h) { return ($w, $h); }
484 # #### TODO: need image parsers for TIFF, XPM, XBM.
485 return png_size ($body);
488 # Returns the dimensions of the image file.
490 sub image_file_size {
493 if (! open (IN, "<$file")) {
494 print STDERR "$progname: $file: $!\n" if ($verbose);
497 binmode (IN); # Larry can take Unicode and shove it up his ass sideways.
499 sysread (IN, $body, 1024 * 50); # The first 50k should be enough.
500 close IN; # (It's not for certain huge jpegs...
501 return image_size ($body); # but we know they're huge!)
507 print STDERR "$progname: $err\n";
512 print STDERR "usage: $progname [--verbose] directory\n" .
513 " Prints the name of a randomly-selected image file. The directory\n" .
514 " is searched recursively. Images smaller than " .
515 "${min_image_width}x${min_image_height} are excluded.\n";
522 while ($_ = $ARGV[0]) {
524 if ($_ eq "--verbose") { $verbose++; }
525 elsif (m/^-v+$/) { $verbose += length($_)-1; }
526 elsif ($_ eq "--name") { } # ignored, for compatibility
527 elsif ($_ eq "--spotlight") { $use_spotlight_p = 1; }
528 elsif ($_ eq "--no-spotlight") { $use_spotlight_p = 0; }
529 elsif ($_ eq "--cache") { $cache_p = 1; }
530 elsif ($_ eq "--no-cache") { $cache_p = 0; }
531 elsif (m/^-./) { usage; }
532 elsif (!defined($dir)) { $dir = $_; }
536 usage unless (defined($dir));
538 $dir =~ s@^~/@$ENV{HOME}/@s; # allow literal "~/"
541 print STDERR "$progname: $dir: not a directory\n";
545 my $file = find_random_file ($dir);
546 print STDOUT "$file\n";