http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.02.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
33 # use Fcntl ':mode';   # Perl 5.6ism?
34 use POSIX ':fcntl_h';  # more portable?
35
36
37 my $progname = $0; $progname =~ s@.*/@@g;
38 my $version = q{ $Revision: 1.8 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
39
40 my $verbose = 0;
41
42 # These are programs that can be used to put an image file on the root
43 # window (including virtual root windows.)  The first one of these programs
44 # that exists on $PATH will be used (with the file name as the last arg.)
45 #
46 # If you add other programs to this list, please let me know!
47 #
48 my @programs = (
49   "chbg       -once -xscreensaver -max_grow 4 -max_size 100",
50   "xv         -root -quit -viewonly -maxpect -noresetroot -quick24 -rmode 5" .
51   "           -rfg black -rbg black",
52   "xli        -quiet -fullscreen -onroot -center -border black",
53   "xloadimage -quiet -fullscreen -onroot -center -border black",
54
55 # this lame program wasn't built with vroot.h:
56 # "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
57 );
58
59 sub pick_displayer {
60   my @names = ();
61
62   foreach my $cmd (@programs) {
63     $_ = $cmd;
64     my ($name) = m/^([^ ]+)/;
65     push @names, "\"$name\"";
66     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
67     foreach my $dir (split (/:/, $ENV{PATH})) {
68       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
69       return $cmd if (-x "$dir/$name");
70     }
71   }
72
73   $names[$#names] = "or " . $names[$#names];
74   printf STDERR "$progname: none of: " . join (", ", @names) .
75                 " were found on \$PATH.\n";
76   exit 1;
77 }
78
79
80 my @all_files = ();
81 my %seen_inodes;
82
83 sub find_all_files {
84   my ($dir) = @_;
85
86   print STDERR "$progname: reading dir $dir/...\n" if ($verbose > 2);
87
88   local *DIR;
89   if (! opendir (DIR, $dir)) {
90     print STDERR "$progname: couldn't open $dir: $!\n";
91     return;
92   }
93   my @files = readdir (DIR);
94   closedir (DIR);
95
96   my @dirs = ();
97
98   foreach my $file (@files) {
99     next if ($file =~ m/^\./);      # ignore dot files
100     next if ($file =~ m/[~%\#]$/);  # ignore backup files
101     next if ($file =~ m/\.(BAK|bak|tmp|orig|rej|rpmsave)$/);
102     next if ($file eq "core");
103
104     $file = "$dir/$file";
105     my @st = stat($file);
106     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
107         $atime,$mtime,$ctime,$blksize,$blocks) = @st;
108
109     if ($#st == -1) {
110       if ($verbose) {
111         my $ll = readlink $file;
112         if (defined ($ll)) {
113           print STDERR "$progname: dangling symlink: $file -> $ll\n";
114         } else {
115           print STDERR "$progname: unreadable: $file\n";
116         }
117       }
118       next;
119     }
120
121     next if ($seen_inodes{$ino}); # break symlink loops
122     $seen_inodes{$ino} = 1;
123
124     if (S_ISDIR($mode)) {
125       push @dirs, $file;
126       print STDERR "$progname:   found dir  $file\n" if ($verbose > 2);
127     } elsif (S_ISREG($mode) || S_ISLNK($mode)) {
128       push @all_files, $file;
129       print STDERR "$progname:   found file $file\n" if ($verbose > 2);
130     } elsif ($verbose > 2) {
131       print STDERR "$progname:   nonreg $file\n";
132     }
133   }
134
135   foreach (@dirs) {
136     find_all_files ($_);
137   }
138 }
139
140
141 sub find_random_file {
142   my ($dir) = @_;
143
144   $dir =~ s@/+$@@g;
145
146   print STDERR "$progname: recursively reading $dir...\n" if ($verbose > 1);
147   find_all_files ($dir);
148   print STDERR "$progname: found " . ($#all_files+1) . " files\n"
149     if ($verbose > 1);
150
151   @all_files = sort(@all_files);
152
153   if ($#all_files < 0) {
154     print STDERR "$progname: no files in $dir\n";
155     exit 1;
156   }
157
158   my $n = int (rand ($#all_files + 1));
159   my $file = $all_files[$n];
160
161   print STDERR "$progname: chose file $n: $file\n" if ($verbose > 1);
162   return $file;
163 }
164
165
166
167 sub display_file {
168   my ($file, $displayer) = @_;
169
170   if (!defined($displayer)) {
171     print STDOUT "$file\n";
172   }  else {
173     my @cmd = split (/ +/, $displayer);
174     push @cmd, $file;   # do it this way to allow file names with spaces.
175     print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
176       if ($verbose);
177     exec (@cmd) || die;
178   }
179 }
180
181
182 sub find_and_display {
183   my ($dir, $displayer) = @_;
184   my $file = find_random_file ($dir);
185   display_file ($file, $displayer);
186 }
187
188
189 sub usage {
190   print STDERR "usage: $progname [--verbose] [--name] directory\n";
191   print STDERR "Puts a randomly selected image on the root window.\n";
192   print STDERR "With --name, merely prints the filename to stdout.\n";
193   exit 1;
194 }
195
196 sub main {
197   my $dir = undef;
198   my $do_name = 0;
199
200   while ($_ = $ARGV[0]) {
201     shift @ARGV;
202     if ($_ eq "--verbose") { $verbose++; }
203     elsif (m/^-v+$/) { $verbose += length($_)-1; }
204     elsif ($_ eq "--name") { $do_name++; }
205     elsif (m/^-./) { usage; }
206     elsif (!defined($dir)) { $dir = $_; }
207     else { usage; }
208   }
209
210   usage unless (defined($dir));
211   my $displayer = undef;
212
213   $displayer = pick_displayer() unless $do_name;
214
215   if (-d $dir) {
216     find_and_display ($dir, $displayer);
217   } elsif (-f $dir) {
218     display_file ($dir, $displayer);
219   } else {
220     usage();
221   }
222 }
223
224 main;
225 exit 0;