2 # Copyright © 2001 Jamie Zawinski <jwz@jwz.org>, all rights reserved.
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 my $progname = $0; $progname =~ s@.*/@@g;
35 my $version = q{ $Revision: 1.6 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
39 # These are programs that can be used to put an image file on the root
40 # window (including virtual root windows.) The first one of these programs
41 # that exists on $PATH will be used (with the file name as the last arg.)
43 # If you add other programs to this list, please let me know!
46 "chbg -once -xscreensaver -max_grow 4 -max_size 100",
47 "xv -root -quit -viewonly -maxpect -noresetroot -quick24 -rmode 5" .
48 " -rfg black -rbg black",
49 "xli -quiet -fullscreen -onroot -center -border black",
50 "xloadimage -quiet -fullscreen -onroot -center -border black",
52 # this lame program wasn't built with vroot.h:
53 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
59 foreach my $cmd (@programs) {
61 my ($name) = m/^([^ ]+)/;
62 push @names, "\"$name\"";
63 print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
64 foreach my $dir (split (/:/, $ENV{PATH})) {
65 print STDERR "$progname: checking $dir/$name\n" if ($verbose > 3);
66 return $cmd if (-x "$dir/$name");
70 $names[$#names] = "or " . $names[$#names];
71 printf STDERR "$progname: none of: " . join (", ", @names) .
72 " were found on \$PATH.\n";
83 print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
86 if (! opendir (DIR, $dir)) {
87 print STDERR "$progname: couldn't open $dir: $!\n";
90 my @files = readdir (DIR);
95 foreach my $file (@files) {
96 next if ($file =~ m/^\./); # ignore dot files
97 next if ($file =~ m/[~%\#]$/); # ignore backup files
98 next if ($file =~ m/\.(BAK|bak|tmp|orig|rej|rpmsave)$/);
99 next if ($file eq "core");
101 $file = "$dir/$file";
102 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
103 $atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
105 next if ($seen_inodes{$ino}); # break symlink loops
106 $seen_inodes{$ino} = 1;
108 if (S_ISDIR($mode)) {
110 print STDERR "$progname: found dir $file\n" if ($verbose > 2);
111 } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
112 push @all_files, $file;
113 print STDERR "$progname: found file $file\n" if ($verbose > 2);
114 } elsif ($verbose > 2) {
115 print STDERR "$progname: nonreg $file\n";
125 sub find_random_file {
130 print STDERR "$progname: recursively reading $dir...\n" if ($verbose > 1);
131 find_all_files ($dir);
132 print STDERR "$progname: found $#all_files files\n" if ($verbose > 1);
134 @all_files = sort(@all_files);
136 my $n = int (rand ($#all_files + 1));
137 my $file = $all_files[$n];
139 print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
146 my ($file, $displayer) = @_;
148 if (!defined($displayer)) {
149 print STDOUT "$file\n";
151 my @cmd = split (/ +/, $displayer);
152 push @cmd, $file; # do it this way to allow file names with spaces.
153 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
160 sub find_and_display {
161 my ($dir, $displayer) = @_;
162 my $file = find_random_file ($dir);
163 display_file ($file, $displayer);
168 print STDERR "usage: $progname [--verbose] [--name] directory\n";
169 print STDERR "Puts a randomly selected image on the root window.\n";
170 print STDERR "With --name, merely prints the filename to stdout.\n";
178 while ($_ = $ARGV[0]) {
180 if ($_ eq "--verbose") { $verbose++; }
181 elsif (m/^-v+$/) { $verbose += length($_)-1; }
182 elsif ($_ eq "--name") { $do_name++; }
183 elsif (m/^-./) { usage; }
184 elsif (!defined($dir)) { $dir = $_; }
188 usage unless (defined($dir));
189 my $displayer = undef;
191 $displayer = pick_displayer() unless $do_name;
194 find_and_display ($dir, $displayer);
196 display_file ($dir, $displayer);