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
37 my $progname = $0; $progname =~ s@.*/@@g;
38 my $version = q{ $Revision: 1.15 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
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.
47 my $good_file_re = '\.(gif|p?jpe?g|png|tiff?|xbm|xpm)$';
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.)
54 # If you add other programs to this list, please let me know!
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",
63 # this lame program wasn't built with vroot.h:
64 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
71 foreach my $cmd (@programs) {
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");
82 $names[$#names] = "or " . $names[$#names];
83 printf STDERR "$progname: none of: " . join (", ", @names) .
84 " were found on \$PATH.\n";
97 print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
100 if (! opendir (DIR, $dir)) {
101 print STDERR "$progname: couldn't open $dir: $!\n" if ($verbose);
104 my @files = readdir (DIR);
109 foreach my $file (@files) {
110 next if ($file =~ m/^\./); # ignore dot files/dirs
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;
119 my $ll = readlink $file;
121 print STDERR "$progname: dangling symlink: $file -> $ll\n";
123 print STDERR "$progname: unreadable: $file\n";
129 next if ($seen_inodes{"$dev:$ino"}); # break symlink loops
130 $seen_inodes{"$dev:$ino"} = 1;
132 if (S_ISDIR($mode)) {
135 print STDERR "$progname: found dir $file\n" if ($verbose > 2);
136 } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
138 if ($file =~ m/[~%\#]$/ || # backup file, or
139 ! ($file =~ m/$good_file_re/io)) { # no image extension
141 print STDERR "$progname: skip file $file\n" if ($verbose > 2);
143 push @all_files, $file;
144 print STDERR "$progname: found file $file\n" if ($verbose > 2);
146 } elsif ($verbose > 2) {
147 print STDERR "$progname: nonreg $file\n";
157 sub find_random_file {
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") .
171 @all_files = sort(@all_files);
173 if ($#all_files < 0) {
174 print STDERR "$progname: no files in $dir\n";
178 my $n = int (rand ($#all_files + 1));
179 my $file = $all_files[$n];
181 print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
188 my ($file, $displayer) = @_;
190 if (!defined($displayer)) {
191 print STDOUT "$file\n";
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"
202 sub find_and_display {
203 my ($dir, $displayer) = @_;
204 my $file = find_random_file ($dir);
205 display_file ($file, $displayer);
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";
221 while ($_ = $ARGV[0]) {
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 = $_; }
231 usage unless (defined($dir));
232 my $displayer = undef;
234 $displayer = pick_displayer() unless $do_name;
237 find_and_display ($dir, $displayer);
239 display_file ($dir, $displayer);
241 print STDERR "$progname: $dir does not exist\n";