X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?p=xscreensaver;a=blobdiff_plain;f=driver%2Fxscreensaver-getimage-file;h=9d038e8e07836dbb85ea75818e8e2fd3a42b0559;hp=860ffc8a82f5c98cc8e46d995c4306d760e61b83;hb=49f5b54f312fe4ac2e9bc47581a72451bd0e8439;hpb=ccb7f4903325f92555a9722bba74b58346654ba0 diff --git a/driver/xscreensaver-getimage-file b/driver/xscreensaver-getimage-file index 860ffc8a..9d038e8e 100755 --- a/driver/xscreensaver-getimage-file +++ b/driver/xscreensaver-getimage-file @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# Copyright © 2001, 2002, 2003, 2004 Jamie Zawinski . +# Copyright © 2001-2006 Jamie Zawinski . # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that @@ -9,17 +9,19 @@ # software for any purpose. It is provided "as is" without express or # implied warranty. # -# This program attempts to locate a random image from the specified directory, -# and load it on to the root window, using some other program that can decode -# image files. (It attempts to find such a program.) +# This program chooses a random file from under the given directory, and +# prints its name. The file will be an image file whose dimensions are +# larger than a certain minimum size. # -# The various xscreensaver hacks that manipulate images ("slidescreen", -# "jigsaw", etc.) get the image to manipulate by running the -# "xscreensaver-getimage" program. +# The various xscreensaver hacks that manipulate images ("jigsaw", etc.) get +# the image to manipulate by running the "xscreensaver-getimage" program. # -# "xscreensaver-getimage" will invoke this program, depending on the -# value of the "chooseRandomImages" and "imageDirectory" settings in -# the ~/.xscreensaver file (or /usr/lib/X11/app-defaults/XScreenSaver). +# Under X11, the "xscreensaver-getimage" program invokes this script, +# depending on the value of the "chooseRandomImages" and "imageDirectory" +# settings in the ~/.xscreensaver file (or .../app-defaults/XScreenSaver). +# +# Under Cocoa, this script lives inside the .saver bundle, and is invoked +# directly from utils/grabclient.c. # # Created: 12-Apr-01. @@ -30,86 +32,80 @@ use strict; use POSIX; use Fcntl; -use POSIX ':fcntl_h'; # S_ISLNK was here in Perl 5.6 -import Fcntl ':mode' unless defined &S_ISLNK; # but it is here in Perl 5.8 +use Fcntl ':flock'; # import LOCK_* constants + +use POSIX ':fcntl_h'; # S_ISDIR was here in Perl 5.6 +import Fcntl ':mode' unless defined &S_ISDIR; # but it is here in Perl 5.8 use bytes; # Larry can take Unicode and shove it up his ass sideways. # Perl 5.8.0 causes us to start getting incomprehensible # errors about UTF-8 all over the place without this. my $progname = $0; $progname =~ s@.*/@@g; -my $version = q{ $Revision: 1.18 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; +my $version = q{ $Revision: 1.25 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; my $verbose = 0; +# Whether to use MacOS X's Spotlight to generate the list of files. +# When set to -1, uses Spotlight if "mdfind" exists. +# +# (In my experience, this isn't actually any faster, and might not find +# everything if your Spotlight index is out of date, which happens often.) +# +my $use_spotlight_p = 0; + +# Whether to cache the results of the last run. +# +my $cache_p = 1; + +# Regenerate the cache if it is older than this many seconds. +# +my $cache_max_age = 60 * 60 * 3; # 3 hours + + # This matches files that we are allowed to use as images (case-insensitive.) # Anything not matching this is ignored. This is so you can point your # imageDirectory at directory trees that have things other than images in # them, but it assumes that you gave your images sensible file extensions. # -my $good_file_re = '\.(gif|p?jpe?g|png|tiff?|xbm|xpm)$'; +my @good_extensions = ('jpg', 'jpeg', 'pjpeg', 'pjpg', 'png', 'gif', + 'tif', 'tiff', 'xbm', 'xpm'); +my $good_file_re = '\.(' . join("|", @good_extensions) . ')$'; + +# This matches file extensions that might occur in an image directory, +# and that are never used in the name of a subdirectory. This is an +# optimization that prevents us from having to stat() those files to +# tell whether they are directories or not. (It speeds things up a +# lot. Don't give your directories stupid names.) +# +my @nondir_extensions = ('ai', 'bmp', 'bz2', 'cr2', 'crw', 'db', + 'dmg', 'eps', 'gz', 'hqx', 'htm', 'html', 'icns', 'ilbm', 'mov', + 'nef', 'pbm', 'pdf', 'pl', 'ppm', 'ps', 'psd', 'sea', 'sh', 'shtml', + 'tar', 'tgz', 'thb', 'txt', 'xcf', 'xmp', 'Z', 'zip' ); +my $nondir_re = '\.(' . join("|", @nondir_extensions) . ')$'; + -# JPEG, GIF, and PNG files that are are smaller than this size in either -# direction are rejected: this is so that you can use an image directory -# that contains both big images and thumbnails, and have it only select -# the big versions. +# JPEG, GIF, and PNG files that are are smaller than this are rejected: +# this is so that you can use an image directory that contains both big +# images and thumbnails, and have it only select the big versions. # my $min_image_width = 255; my $min_image_height = 255; +my @all_files = (); # list of "good" files we've collected +my %seen_inodes; # for breaking recursive symlink loops -# These are programs that can be used to put an image file on the root -# window (including virtual root windows.) The first one of these programs -# that exists on $PATH will be used (with the file name as the last arg.) -# -# Generally this isn't used any more; when "xscreensaver-getimage" invokes -# this program, it does so with the "-file" argument (meaning that we just -# return the file name) and then xscreensaver-getimage loads that file -# directly. However, if you invoke "xscreensaver-getimage-file" directly, -# without "-file", this will be used to actually load the image. +# For diagnostic messages: # -my @programs = ( - "chbg -once -xscreensaver -max_grow 4 -max_size 100", - "xv -root -quit -viewonly -maxpect +noresetroot -quick24 -rmode 5" . - " -rfg black -rbg black", - "xli -quiet -fullscreen -onroot -center -border black", - "xloadimage -quiet -fullscreen -onroot -center -border black", - -# this lame program wasn't built with vroot.h: -# "xsri -scale -keep-aspect -center-horizontal -center-vertical", -); - - -sub pick_displayer { - my @names = (); - - foreach my $cmd (@programs) { - $_ = $cmd; - my ($name) = m/^([^ ]+)/; - push @names, "\"$name\""; - print STDERR "$progname: looking for $name...\n" if ($verbose > 2); - foreach my $dir (split (/:/, $ENV{PATH})) { - print STDERR "$progname: checking $dir/$name\n" if ($verbose > 3); - return $cmd if (-x "$dir/$name"); - } - } - - $names[$#names] = "or " . $names[$#names]; - printf STDERR "$progname: none of: " . join (", ", @names) . - " were found on \$PATH.\n"; - exit 1; -} - - -my @all_files = (); # list of "good" files we've collected -my %seen_inodes; # for breaking recursive symlink loops -my $skip_count = 0; # number of files skipped, for diagnostic messages -my $dir_count = 1; # number of directories seen, for diagnostic messages +my $dir_count = 1; # number of directories seen +my $stat_count = 0; # number of files/dirs stat'ed +my $skip_count_unstat = 0; # number of files skipped without stat'ing +my $skip_count_stat = 0; # number of files skipped after stat sub find_all_files { my ($dir) = @_; - print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2); + print STDERR "$progname: + reading dir $dir/...\n" if ($verbose > 1); local *DIR; if (! opendir (DIR, $dir)) { @@ -122,44 +118,67 @@ sub find_all_files { my @dirs = (); foreach my $file (@files) { - next if ($file =~ m/^\./); # ignore dot files/dirs + next if ($file =~ m/^\./); # silently ignore dot files/dirs + + if ($file =~ m/[~%\#]$/) { # ignore backup files (and dirs...) + $skip_count_unstat++; + print STDERR "$progname: - skip file $file\n" if ($verbose > 1); + } $file = "$dir/$file"; - my @st = stat($file); - my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, - $atime,$mtime,$ctime,$blksize,$blocks) = @st; - - if ($#st == -1) { - if ($verbose) { - my $ll = readlink $file; - if (defined ($ll)) { - print STDERR "$progname: dangling symlink: $file -> $ll\n"; - } else { - print STDERR "$progname: unreadable: $file\n"; + + if ($file =~ m/$good_file_re/io) { + # + # Assume that files ending in .jpg exist and are not directories. + # + push @all_files, $file; + print STDERR "$progname: - found file $file\n" if ($verbose > 1); + + } elsif ($file =~ m/$nondir_re/io) { + # + # Assume that files ending in .html are not directories. + # + $skip_count_unstat++; + print STDERR "$progname: -- skip file $file\n" if ($verbose > 1); + + } else { + # + # Now we need to stat the file to see if it's a subdirectory. + # + # Note: we could use the trick of checking "nlinks" on the parent + # directory to see if this directory contains any subdirectories, + # but that would exclude any symlinks to directories. + # + my @st = stat($file); + my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, + $atime,$mtime,$ctime,$blksize,$blocks) = @st; + + $stat_count++; + + if ($#st == -1) { + if ($verbose) { + my $ll = readlink $file; + if (defined ($ll)) { + print STDERR "$progname: + dangling symlink: $file -> $ll\n"; + } else { + print STDERR "$progname: + unreadable: $file\n"; + } } + next; } - next; - } - next if ($seen_inodes{"$dev:$ino"}); # break symlink loops - $seen_inodes{"$dev:$ino"} = 1; + next if ($seen_inodes{"$dev:$ino"}); # break symlink loops + $seen_inodes{"$dev:$ino"} = 1; - if (S_ISDIR($mode)) { - push @dirs, $file; - $dir_count++; - print STDERR "$progname: found dir $file\n" if ($verbose > 2); - } elsif (S_ISREG($mode) || S_ISLNK($mode)) { + if (S_ISDIR($mode)) { + push @dirs, $file; + $dir_count++; + print STDERR "$progname: + found dir $file\n" if ($verbose > 1); - if ($file =~ m/[~%\#]$/ || # backup file, or - ! ($file =~ m/$good_file_re/io)) { # no image extension - $skip_count++; - print STDERR "$progname: skip file $file\n" if ($verbose > 2); } else { - push @all_files, $file; - print STDERR "$progname: found file $file\n" if ($verbose > 2); + $skip_count_stat++; + print STDERR "$progname: + skip file $file\n" if ($verbose > 1); } - } elsif ($verbose > 2) { - print STDERR "$progname: nonreg $file\n"; } } @@ -169,20 +188,165 @@ sub find_all_files { } -sub find_random_file { +sub spotlight_all_files { my ($dir) = @_; - $dir =~ s@/+$@@g; + my @terms = (); + # "public.image" matches all (indexed) images, including Photoshop, etc. +# push @terms, "kMDItemContentTypeTree == 'public.image'"; + foreach (@good_extensions) { + + # kMDItemFSName hits the file system every time: much worse than "find". +# push @terms, "kMDItemFSName == '*.$_'"; + + # kMDItemDisplayName matches against the name in the Spotlight index, + # but won't find files that (for whatever reason) didn't get indexed. + push @terms, "kMDItemDisplayName == '*.$_'"; + } + + $dir =~ s@([^-_/a-z\d.,])@\\$1@gsi; # quote for sh + my $cmd = "mdfind -onlyin $dir \"" . join (' || ', @terms) . "\""; + + print STDERR "$progname: executing: $cmd\n" if ($verbose > 1); + @all_files = split (/[\r\n]+/, `$cmd`); +} + + +# If we're using cacheing, read the cache file and return its contents, +# if any. This also holds an exclusive lock on the cache file, which +# has the additional benefit that if two copies of this program are +# running at once, one will wait for the other, instead of both of +# them spanking the same file system at the same time. +# +local *CACHE_FILE; +my $cache_file_name = undef; +my $read_cache_p = 0; + +sub read_cache($) { + my ($dir) = @_; - print STDERR "$progname: recursively reading $dir...\n" if ($verbose > 1); - find_all_files ($dir); - print STDERR "$progname: found " . ($#all_files+1) . - " file" . ($#all_files == 0 ? "" : "s") . - " in $dir_count dir" . ($dir_count == 1 ? "" : "s") . - "; skipped $skip_count file" . ($skip_count == 1 ? "" : "s") . - ".\n" + return () unless ($cache_p); + + my $dd = "$ENV{HOME}/Library/Caches"; # MacOS location + if (-d $dd) { + $cache_file_name = "$dd/org.jwz.xscreensaver.getimage.cache"; + } elsif (-d "$ENV{HOME}/tmp") { + $cache_file_name = "$ENV{HOME}/tmp/.xscreensaver-getimage.cache"; + } else { + $cache_file_name = "$ENV{HOME}/.xscreensaver-getimage.cache"; + } + + my $file = $cache_file_name; + open (CACHE_FILE, "+>>$file") || error ("unable to write $file: $!"); + flock (CACHE_FILE, LOCK_EX) || error ("unable to lock $file: $!"); + seek (CACHE_FILE, 0, 0) || error ("unable to rewind $file: $!"); + + print STDERR "$progname: reading cache $cache_file_name\n" if ($verbose > 1); + my $mtime = (stat(CACHE_FILE))[9]; + + if ($mtime + $cache_max_age < time) { + print STDERR "$progname: cache is too old\n" if ($verbose); + return (); + } + + my $odir = ; + $odir =~ s/[\r\n]+$//s if defined ($odir); + if (!defined ($odir) || ($dir ne $odir)) { + print STDERR "$progname: cache is for $odir, not $dir\n" + if ($verbose && $odir); + return (); + } + + my @files = (); + while () { + s/[\r\n]+$//s; + push @files, "$odir/$_"; + } + + print STDERR "$progname: " . ($#files+1) . " files in cache\n" + if ($verbose); + + $read_cache_p = 1; + return @files; +} + + +sub write_cache($) { + my ($dir) = @_; + + return unless ($cache_p); + + # If we read the cache, just close it without rewriting it. + # If we didn't read it, then write it now. + + if (! $read_cache_p) { + + truncate (CACHE_FILE, 0) || + error ("unable to truncate $cache_file_name: $!"); + seek (CACHE_FILE, 0, 0) || + error ("unable to rewind $cache_file_name: $!"); + + if ($#all_files >= 0) { + print CACHE_FILE "$dir\n"; + my $re = qr/$dir/; + foreach (@all_files) { + my $f = $_; # stupid Perl. do this to avoid modifying @all_files! + $f =~ s@^$re/@@so || die; + print CACHE_FILE "$f\n"; + } + } + + print STDERR "$progname: cached " . ($#all_files+1) . " files\n" + if ($verbose); + } + + flock (CACHE_FILE, LOCK_UN) || + error ("unable to unlock $cache_file_name: $!"); + close (CACHE_FILE); +} + + +sub find_random_file($) { + my ($dir) = @_; + + $dir =~ s@/+$@@g; + + if ($use_spotlight_p == -1) { + $use_spotlight_p = 0; + if (-x '/usr/bin/mdfind') { + $use_spotlight_p = 1; + } + } + + @all_files = read_cache ($dir); + + if ($#all_files >= 0) { + # got it from the cache... + + } elsif ($use_spotlight_p) { + print STDERR "$progname: spotlighting $dir...\n" if ($verbose); + spotlight_all_files ($dir); + print STDERR "$progname: found " . ($#all_files+1) . + " file" . ($#all_files == 0 ? "" : "s") . + " via Spotlight\n" + if ($verbose); + } else { + print STDERR "$progname: recursively reading $dir...\n" if ($verbose); + find_all_files ($dir); + print STDERR "$progname: " . + "f=" . ($#all_files+1) . "; " . + "d=$dir_count; " . + "s=$stat_count; " . + "skip=${skip_count_unstat}+$skip_count_stat=" . + ($skip_count_unstat + $skip_count_stat) . + ".\n" + if ($verbose); + } + + write_cache ($dir); + @all_files = sort(@all_files); if ($#all_files < 0) { @@ -206,28 +370,6 @@ sub find_random_file { } -sub display_file { - my ($file, $displayer) = @_; - - if (!defined($displayer)) { - print STDOUT "$file\n"; - } else { - my @cmd = split (/ +/, $displayer); - push @cmd, $file; # do it this way to allow file names with spaces. - print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n" - if ($verbose); - exec (@cmd) || die; - } -} - - -sub find_and_display { - my ($dir, $displayer) = @_; - my $file = find_random_file ($dir); - display_file ($file, $displayer); -} - - sub large_enough_p { my ($file) = @_; @@ -243,11 +385,11 @@ sub large_enough_p { } if ($w < $min_image_width || $h < $min_image_height) { - print STDERR "$progname: $file: too small ($w x $h)\n" if ($verbose > 1); + print STDERR "$progname: $file: too small ($w x $h)\n" if ($verbose); return 0; } - print STDERR "$progname: $file: $w x $h\n" if ($verbose > 1); + print STDERR "$progname: $file: $w x $h\n" if ($verbose); return 1; } @@ -334,6 +476,7 @@ sub png_size { # sub image_size { my ($body) = @_; + return () if (length($body) < 10); my ($w, $h) = gif_size ($body); if ($w && $h) { return ($w, $h); } ($w, $h) = jpeg_size ($body); @@ -346,57 +489,61 @@ sub image_size { # sub image_file_size { my ($file) = @_; - my $body = ''; local *IN; if (! open (IN, "<$file")) { print STDERR "$progname: $file: $!\n" if ($verbose); return undef; } binmode (IN); # Larry can take Unicode and shove it up his ass sideways. - while () { - $body .= $_; - last if (length($body) > 1024 * 100); # the first 100k should be enough - } - close IN; - return image_size ($body); + my $body = ''; + sysread (IN, $body, 1024 * 50); # The first 50k should be enough. + close IN; # (It's not for certain huge jpegs... + return image_size ($body); # but we know they're huge!) } +sub error($) { + my ($err) = @_; + print STDERR "$progname: $err\n"; + exit 1; +} + sub usage { - print STDERR "usage: $progname [--verbose] [--name] file-or-directory\n\n" . - " Puts the given image file (or a randomly selected image from the\n" . - " given directory) on the root window. If --name is specified,\n" . - " just prints the selected filename to stdout instead.\n\n"; + print STDERR "usage: $progname [--verbose] directory\n" . + " Prints the name of a randomly-selected image file. The directory\n" . + " is searched recursively. Images smaller than " . + "${min_image_width}x${min_image_height} are excluded.\n"; exit 1; } sub main { my $dir = undef; - my $do_name = 0; while ($_ = $ARGV[0]) { shift @ARGV; if ($_ eq "--verbose") { $verbose++; } elsif (m/^-v+$/) { $verbose += length($_)-1; } - elsif ($_ eq "--name") { $do_name++; } + elsif ($_ eq "--name") { } # ignored, for compatibility + elsif ($_ eq "--spotlight") { $use_spotlight_p = 1; } + elsif ($_ eq "--no-spotlight") { $use_spotlight_p = 0; } + elsif ($_ eq "--cache") { $cache_p = 1; } + elsif ($_ eq "--no-cache") { $cache_p = 0; } elsif (m/^-./) { usage; } elsif (!defined($dir)) { $dir = $_; } else { usage; } } usage unless (defined($dir)); - my $displayer = undef; - $displayer = pick_displayer() unless $do_name; + $dir =~ s@^~/@$ENV{HOME}/@s; # allow literal "~/" - if (-d $dir) { - find_and_display ($dir, $displayer); - } elsif (-f $dir) { - display_file ($dir, $displayer); - } else { - print STDERR "$progname: $dir does not exist\n"; - usage(); + if (! -d $dir) { + print STDERR "$progname: $dir: not a directory\n"; + usage; } + + my $file = find_random_file ($dir); + print STDOUT "$file\n"; } main;