2 # Copyright © 2001, 2002, 2003 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).
34 # Apparently the "old way" to get S_ISLNK and friends is to do this:
38 # But apparently the "new way" is to do this:
42 # but of course that will generate an error on "old" (pre-5.6?) Perl versions.
43 # So we do it like this instead:
46 if (! defined(&S_ISLNK)) { # perhaps defined by "POSIX"?
48 import Fcntl ':mode'; # if not, look for it in "Fcntl".
53 my $progname = $0; $progname =~ s@.*/@@g;
54 my $version = q{ $Revision: 1.12 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
58 # These are programs that can be used to put an image file on the root
59 # window (including virtual root windows.) The first one of these programs
60 # that exists on $PATH will be used (with the file name as the last arg.)
62 # If you add other programs to this list, please let me know!
65 "chbg -once -xscreensaver -max_grow 4 -max_size 100",
66 "xv -root -quit -viewonly -maxpect +noresetroot -quick24 -rmode 5" .
67 " -rfg black -rbg black",
68 "xli -quiet -fullscreen -onroot -center -border black",
69 "xloadimage -quiet -fullscreen -onroot -center -border black",
71 # this lame program wasn't built with vroot.h:
72 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
78 foreach my $cmd (@programs) {
80 my ($name) = m/^([^ ]+)/;
81 push @names, "\"$name\"";
82 print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
83 foreach my $dir (split (/:/, $ENV{PATH})) {
84 print STDERR "$progname: checking $dir/$name\n" if ($verbose > 3);
85 return $cmd if (-x "$dir/$name");
89 $names[$#names] = "or " . $names[$#names];
90 printf STDERR "$progname: none of: " . join (", ", @names) .
91 " were found on \$PATH.\n";
102 print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
105 if (! opendir (DIR, $dir)) {
106 print STDERR "$progname: couldn't open $dir: $!\n" if ($verbose);
109 my @files = readdir (DIR);
114 foreach my $file (@files) {
115 next if ($file =~ m/^\./); # ignore dot files
116 next if ($file =~ m/[~%\#]$/); # ignore backup files
117 next if ($file =~ m/\.(BAK|bak|tmp|orig|rej|rpmsave)$/);
118 next if ($file eq "core");
120 $file = "$dir/$file";
121 my @st = stat($file);
122 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
123 $atime,$mtime,$ctime,$blksize,$blocks) = @st;
127 my $ll = readlink $file;
129 print STDERR "$progname: dangling symlink: $file -> $ll\n";
131 print STDERR "$progname: unreadable: $file\n";
137 next if ($seen_inodes{$ino}); # break symlink loops
138 $seen_inodes{$ino} = 1;
140 if (S_ISDIR($mode)) {
142 print STDERR "$progname: found dir $file\n" if ($verbose > 2);
143 } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
144 push @all_files, $file;
145 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) . " files\n"
167 @all_files = sort(@all_files);
169 if ($#all_files < 0) {
170 print STDERR "$progname: no files in $dir\n";
174 my $n = int (rand ($#all_files + 1));
175 my $file = $all_files[$n];
177 print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
184 my ($file, $displayer) = @_;
186 if (!defined($displayer)) {
187 print STDOUT "$file\n";
189 my @cmd = split (/ +/, $displayer);
190 push @cmd, $file; # do it this way to allow file names with spaces.
191 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
198 sub find_and_display {
199 my ($dir, $displayer) = @_;
200 my $file = find_random_file ($dir);
201 display_file ($file, $displayer);
206 print STDERR "usage: $progname [--verbose] [--name] directory\n";
207 print STDERR "Puts a randomly selected image on the root window.\n";
208 print STDERR "With --name, merely prints the filename to stdout.\n";
216 while ($_ = $ARGV[0]) {
218 if ($_ eq "--verbose") { $verbose++; }
219 elsif (m/^-v+$/) { $verbose += length($_)-1; }
220 elsif ($_ eq "--name") { $do_name++; }
221 elsif (m/^-./) { usage; }
222 elsif (!defined($dir)) { $dir = $_; }
226 usage unless (defined($dir));
227 my $displayer = undef;
229 $displayer = pick_displayer() unless $do_name;
232 find_and_display ($dir, $displayer);
234 display_file ($dir, $displayer);