http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / driver / xscreensaver-getimage-file
1 #!/usr/bin/perl -w
2 # Copyright © 2001, 2002 Jamie Zawinski <jwz@jwz.org>.
3 #
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 
10 # implied warranty.
11 #
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.)
15 #
16 # The various xscreensaver hacks that manipulate images ("slidescreen",
17 # "jigsaw", etc.) get the image to manipulate by running the
18 # "xscreensaver-getimage" program.
19 #
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).
23 #
24 # Created: 12-Apr-01.
25
26 require 5;
27 use diagnostics;
28 use strict;
29
30 use POSIX;
31 use Fcntl;
32 use Fcntl ':mode';
33
34 my $progname = $0; $progname =~ s@.*/@@g;
35 my $version = q{ $Revision: 1.8 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
36
37 my $verbose = 0;
38
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.)
42 #
43 # If you add other programs to this list, please let me know!
44 #
45 my @programs = (
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",
51
52 # this lame program wasn't built with vroot.h:
53 # "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
54 );
55
56 sub pick_displayer {
57   my @names = ();
58
59   foreach my $cmd (@programs) {
60     $_ = $cmd;
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");
67     }
68   }
69
70   $names[$#names] = "or " . $names[$#names];
71   printf STDERR "$progname: none of: " . join (", ", @names) .
72                 " were found on \$PATH.\n";
73   exit 1;
74 }
75
76
77 my @all_files = ();
78 my %seen_inodes;
79
80 sub find_all_files {
81   my ($dir) = @_;
82
83   print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
84
85   local *DIR;
86   if (! opendir (DIR, $dir)) {
87     print STDERR "$progname: couldn't open $dir: $!\n";
88     return;
89   }
90   my @files = readdir (DIR);
91   closedir (DIR);
92
93   my @dirs = ();
94
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");
100
101     $file = "$dir/$file";
102     my @st = stat($file);
103     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
104         $atime,$mtime,$ctime,$blksize,$blocks) = @st;
105
106     if ($#st == -1) {
107       if ($verbose) {
108         my $ll = readlink $file;
109         if (defined ($ll)) {
110           print STDERR "$progname: dangling symlink: $file -> $ll\n";
111         } else {
112           print STDERR "$progname: unreadable: $file\n";
113         }
114       }
115       next;
116     }
117
118     next if ($seen_inodes{$ino}); # break symlink loops
119     $seen_inodes{$ino} = 1;
120
121     if (S_ISDIR($mode)) {
122       push @dirs, $file;
123       print STDERR "$progname:   found dir  $file\n" if ($verbose > 2);
124     } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
125       push @all_files, $file;
126       print STDERR "$progname:   found file $file\n" if ($verbose > 2);
127     } elsif ($verbose > 2) {
128       print STDERR "$progname:   nonreg $file\n";
129     }
130   }
131
132   foreach (@dirs) {
133     find_all_files ($_);
134   }
135 }
136
137
138 sub find_random_file {
139   my ($dir) = @_;
140
141   $dir =~ s@/+$@@g;
142
143   print STDERR "$progname: recursively reading $dir...\n" if ($verbose > 1);
144   find_all_files ($dir);
145   print STDERR "$progname: found " . ($#all_files+1) . " files\n"
146     if ($verbose > 1);
147
148   @all_files = sort(@all_files);
149
150   if ($#all_files < 0) {
151     print STDERR "$progname: no files in $dir\n";
152     exit 1;
153   }
154
155   my $n = int (rand ($#all_files + 1));
156   my $file = $all_files[$n];
157
158   print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
159   return $file;
160 }
161
162
163
164 sub display_file {
165   my ($file, $displayer) = @_;
166
167   if (!defined($displayer)) {
168     print STDOUT "$file\n";
169   }  else {
170     my @cmd = split (/ +/, $displayer);
171     push @cmd, $file;   # do it this way to allow file names with spaces.
172     print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
173       if ($verbose);
174     exec (@cmd) || die;
175   }
176 }
177
178
179 sub find_and_display {
180   my ($dir, $displayer) = @_;
181   my $file = find_random_file ($dir);
182   display_file ($file, $displayer);
183 }
184
185
186 sub usage {
187   print STDERR "usage: $progname [--verbose] [--name] directory\n";
188   print STDERR "Puts a randomly selected image on the root window.\n";
189   print STDERR "With --name, merely prints the filename to stdout.\n";
190   exit 1;
191 }
192
193 sub main {
194   my $dir = undef;
195   my $do_name = 0;
196
197   while ($_ = $ARGV[0]) {
198     shift @ARGV;
199     if ($_ eq "--verbose") { $verbose++; }
200     elsif (m/^-v+$/) { $verbose += length($_)-1; }
201     elsif ($_ eq "--name") { $do_name++; }
202     elsif (m/^-./) { usage; }
203     elsif (!defined($dir)) { $dir = $_; }
204     else { usage; }
205   }
206
207   usage unless (defined($dir));
208   my $displayer = undef;
209
210   $displayer = pick_displayer() unless $do_name;
211
212   if (-d $dir) {
213     find_and_display ($dir, $displayer);
214   } elsif (-f $dir) {
215     display_file ($dir, $displayer);
216   } else {
217     usage();
218   }
219 }
220
221 main;
222 exit 0;