ftp://ftp.linux.ncsu.edu/mirror/ftp.redhat.com/pub/redhat/linux/enterprise/4/en/os...
[xscreensaver] / hacks / vidwhacker
index 5dc85b67b0a9e2025701bcb64abf5f7aaa4e1df6..d958c5623c7eddfbf533cbc6f0339360ce4aa0db 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# vidwhacker, for xscreensaver.  Copyright (c) 1998-2001 Jamie Zawinski.
+# vidwhacker, for xscreensaver.  Copyright (c) 1998-2004 Jamie Zawinski.
 #
 # Permission to use, copy, modify, distribute, and sell this software and its
 # documentation for any purpose is hereby granted without fee, provided that
@@ -21,7 +21,7 @@ use diagnostics;
 use strict;
 
 my $progname = $0; $progname =~ s@.*/@@g;
-my $version = q{ $Revision: 1.17 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
+my $version = q{ $Revision: 1.25 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
 
 my $verbose = 0;
 my $use_stdin = 0;
@@ -33,26 +33,12 @@ my $imagedir;
 
 my $screen_width = -1;
 
+my $displayer = "xscreensaver-getimage -root -file";
 
 
-# ####  This list was lifted from driver/xscreensaver-getimage-file
+# apparently some versions of netpbm call it "pamoil" instead of "pgmoil"...
 #
-# These are programs that can be used to put an image file on the root
-# window (including virtual root windows.)  The first one of these programs
-# that exists on $PATH will be used (with the file name as the last arg.)
-#
-# If you add other programs to this list, please let me know!
-#
-my @displayer_programs = (
-  "xv         -root -quit -viewonly -maxpect -noresetroot -quick24 -rmode 5" .
-  "           -rfg black -rbg black",
-  "xli        -quiet -fullscreen -onroot -center -border black",
-  "xloadimage -quiet -fullscreen -onroot -center -border black",
-  "chbg       -once -xscreensaver -max_grow 4",
-
-# this lame program wasn't built with vroot.h:
-# "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
-);
+my $pgmoil = (which("pamoil") ? "pamoil" : "pgmoil");
 
 
 # List of interesting PPM filter pipelines.
@@ -68,7 +54,7 @@ my @displayer_programs = (
 my @filters = (
   "ppmtopgm FILE1 | pgmedge | pgmtoppm COLORS | ppmnorm",
   "ppmtopgm FILE1 | pgmenhance | pgmtoppm COLORS",
-  "ppmtopgm FILE1 | pgmoil | pgmtoppm COLORS",
+  "ppmtopgm FILE1 | $pgmoil | pgmtoppm COLORS",
   "ppmtopgm FILE1 | pgmbentley | pgmtoppm COLORS",
 
   "ppmrelief FILE1 | ppmtopgm | pgmedge | ppmrelief | ppmtopgm |" .
@@ -110,7 +96,7 @@ my @filters = (
   " pnmflip -tb FILE3 | ppmnorm > FILE2 ; " .
   " pnmarith -multiply FILE1 FILE2",
 
-  "ppmshift 30 FILE1 | ppmtopgm | pgmoil | pgmedge | " .
+  "ppmshift 30 FILE1 | ppmtopgm | $pgmoil | pgmedge | " .
   "   pgmtoppm COLORS > FILE2 ; " .
   " pnmarith -difference FILE1 FILE2",
 
@@ -134,28 +120,51 @@ sub error {
   exit 1;
 }
 
-# ####  Lifted from driver/xscreensaver-getimage-file
+# Any files on this list will be deleted at exit.
 #
-sub pick_displayer {
-  my @names = ();
-
-  foreach my $cmd (@displayer_programs) {
-    $_ = $cmd;
-    my ($name) = m/^([^ ]+)/;
-    push @names, "\"$name\"";
-    print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
-    foreach my $dir (split (/:/, $ENV{PATH})) {
-      print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
-      return $cmd if (-x "$dir/$name");
-    }
-  }
+my @all_tmpfiles = ();
+
+sub exit_cleanup {
+  print STDERR "$progname: delete tmp files\n" if ($verbose);
+  unlink @all_tmpfiles;
+}
 
-  $names[$#names] = "or " . $names[$#names];
-  printf STDERR "$progname: none of: " . join (", ", @names) .
-                " were found on \$PATH.\n";
+sub signal_cleanup {
+  my ($sig) = @_;
+  print STDERR "$progname: caught SIG$sig\n" if ($verbose);
   exit 1;
 }
 
+sub init_signals {
+
+  $SIG{HUP}  = \&signal_cleanup;
+  $SIG{INT}  = \&signal_cleanup;
+  $SIG{QUIT} = \&signal_cleanup;
+  $SIG{ABRT} = \&signal_cleanup;
+  $SIG{KILL} = \&signal_cleanup;
+  $SIG{TERM} = \&signal_cleanup;
+
+  # Need this so that if giftopnm dies, we don't die.
+  $SIG{PIPE} = 'IGNORE';
+}
+
+END { exit_cleanup(); }
+
+
+
+
+# returns the full path of the named program, or undef.
+#
+sub which {
+  my ($prog) = @_;
+  foreach (split (/:/, $ENV{PATH})) {
+    if (-x "$_/$prog") {
+      return $prog;
+    }
+  }
+  return undef;
+}
+
 
 # Choose random foreground and background colors
 #
@@ -170,6 +179,7 @@ sub randcolors {
 }
 
 
+
 sub filter_subst {
   my ($filter, $width, $height, @tmpfiles) = @_;
   my $colors = randcolors();
@@ -201,8 +211,12 @@ sub frob_ppm {
 
   my $tmpdir = $ENV{TMPDIR};
   $tmpdir = "/tmp" unless $tmpdir;
-  my $fn = sprintf("$tmpdir/vw.%04x", $$);
-  my @files = ( "$fn", "$fn.1", "$fn.2", "$fn.3" );
+  my $fn =  sprintf ("%s/vidwhacker-0-%08x", $tmpdir, rand(0xFFFFFFFF));
+  my $fn1 = sprintf ("%s/vidwhacker-1-%08x", $tmpdir, rand(0xFFFFFFFF));
+  my $fn2 = sprintf ("%s/vidwhacker-2-%08x", $tmpdir, rand(0xFFFFFFFF));
+  my $fn3 = sprintf ("%s/vidwhacker-3-%08x", $tmpdir, rand(0xFFFFFFFF));
+  my @files = ( "$fn", "$fn1", "$fn2", "$fn3" );
+  push @all_tmpfiles, @files;
 
   my $n = int(rand($#filters+1));
   my $filter = $filters[$n];
@@ -249,7 +263,7 @@ sub read_config {
   local *IN;
   open (IN, "<$conf") ||  error "reading $conf: $!";
   while (<IN>) {
-    if (!$imagedir && m/^imageDirectory:\s+([^\s]+)\s*$/i) { $imagedir = $1; }
+    if (!$imagedir && m/^imageDirectory:\s+(.*)\s*$/i) { $imagedir = $1; }
     elsif (m/^grabVideoFrames:\s+true\s*$/i)     { $video_p = 1; }
     elsif (m/^grabVideoFrames:\s+false\s*$/i)    { $video_p = 0; }
     elsif (m/^chooseRandomImages:\s+true\s*$/i)  { $file_p  = 1; }
@@ -259,6 +273,7 @@ sub read_config {
 
   $file_p = 1 if $had_dir;
 
+  $imagedir = undef unless ($imagedir && $imagedir ne '');
 
   if (!$file_p && !$video_p) {
 #    error "neither grabVideoFrames nor chooseRandomImages are set\n\t" .
@@ -305,7 +320,7 @@ sub get_ppm {
     my $v = ($verbose <= 1 ? "" : "-" . ("v" x ($verbose-1)));
     my $cmd;
     if ($do_file_p) {
-      $cmd = "xscreensaver-getimage-file  $v --name $imagedir";
+      $cmd = "xscreensaver-getimage-file  $v --name \"$imagedir\"";
     } else {
       $cmd = "xscreensaver-getimage-video $v --stdout";
     }
@@ -314,26 +329,31 @@ sub get_ppm {
 
     if ($do_file_p) {
 
-      print STDERR "$progname: running \"$cmd\"\n" if ($verbose > 1);
+      print STDERR "$progname: running: $cmd\n" if ($verbose > 1);
       my $fn = `$cmd`;
       $fn =~ s/\n$//s;
       error "didn't get a file?" if ($fn eq "");
 
       print STDERR "$progname: selected file $fn\n" if ($verbose > 1);
 
-      if    ($fn =~ m/\.gif/i)   { $cmd = "giftopnm < $fn"; }
-      elsif ($fn =~ m/\.jpe?g/i) { $cmd = "djpeg < $fn"; }
+      if    ($fn =~ m/\.gif/i)   { $cmd = "giftopnm < \"$fn\""; }
+      elsif ($fn =~ m/\.jpe?g/i) { $cmd = "djpeg < \"$fn\""; }
+      elsif ($fn =~ m/\.png/i)   { $cmd = "pngtopnm < \"$fn\""; }
+      elsif ($fn =~ m/\.xpm/i)   { $cmd = "xpmtoppm < \"$fn\""; }
+      elsif ($fn =~ m/\.bmp/i)   { $cmd = "bmptoppm < \"$fn\""; }
+      elsif ($fn =~ m/\.tiff?/i) { $cmd = "tifftopnm < \"$fn\""; }
+      elsif ($fn =~ m/\.p[bgp]m/i) { return `cat \"$fn\"`; }
       else {
         error "unrecognized file extension on $fn";
       }
 
-      print STDERR "$progname: converting with \"$cmd\"\n" if ($verbose > 1);
+      print STDERR "$progname: converting with: $cmd\n" if ($verbose > 1);
       $cmd .= " 2>/dev/null" unless ($verbose > 1);
       $ppm = `$cmd`;
 
     } else {
 
-      print STDERR "$progname: running \"$cmd\"\n" if ($verbose > 1);
+      print STDERR "$progname: running: $cmd\n" if ($verbose > 1);
       $ppm = `$cmd`;
       error "no data?" if ($ppm eq "");
       error "not a PPM file" unless ($ppm =~ m/^P\d\n/s);
@@ -361,13 +381,12 @@ sub dispose_ppm {
     print $ppm;
 
   } else {
-    my $displayer = pick_displayer();
-
     my $tmpdir = $ENV{TMPDIR};
     $tmpdir = "/tmp" unless $tmpdir;
-    my $fn = sprintf("$tmpdir/vw.%04x", $$);
+    my $fn =  sprintf ("%s/vidwhacker-%08x", $tmpdir, rand(0xFFFFFFFF));
     local *OUT;
     unlink $fn;
+    push @all_tmpfiles, $fn;
     open (OUT, ">$fn") || error "writing $fn: $!";
     print OUT $ppm;
     close OUT;
@@ -378,7 +397,15 @@ sub dispose_ppm {
       if ($verbose);
     system (@cmd);
 
+    my $exit_value  = $? >> 8;
+    my $signal_num  = $? & 127;
+    my $dumped_core = $? & 128;
+
     unlink $fn;
+
+    error ("$cmd[0]: core dumped!") if ($dumped_core);
+    error ("$cmd[0]: signal $signal_num!") if ($signal_num);
+    error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
   }
 }
 
@@ -393,7 +420,14 @@ sub vidwhack {
     }
     $ppm = $stdin_ppm;
   } else {
-    $ppm = get_ppm();
+    my $max_err_count = 20;
+    my $err_count = 0;
+    while (!defined($ppm)) {
+      $ppm = get_ppm();
+      $err_count++ if (!defined ($ppm));
+      error ("too many errors, too few images!")
+        if ($err_count > $max_err_count);
+    }
   }
 
   $ppm = frob_ppm ($ppm);
@@ -406,9 +440,10 @@ sub usage {
   print STDERR "VidWhacker, Copyright (c) 2001 Jamie Zawinski <jwz\@jwz.org>\n";
   print STDERR "            http://www.jwz.org/xscreensaver/";
   print STDERR "\n";
-  print STDERR "usage: $0 [-display dpy] [-verbose] [-root | -window]\n";
-  print STDERR "                  [-stdin] [-stdout] [-delay secs]\n";
-  print STDERR "                  [-directory image_directory]\n";
+  print STDERR "usage: $0 [-display dpy] [-verbose]\n";
+  print STDERR "\t\t[-root | -window | -window-id 0xXXXXX ]\n";
+  print STDERR "\t\t[-stdin] [-stdout] [-delay secs]\n";
+  print STDERR "\t\t[-directory image_directory]\n";
   exit 1;
 }
 
@@ -423,6 +458,13 @@ sub main {
     elsif (m/^--?delay$/) { $delay = shift @ARGV; }
     elsif (m/^--?dir(ectory)?$/) { $imagedir = shift @ARGV; }
     elsif (m/^--?root$/) { }
+    elsif (m/^--?window-id$/) {
+      my $id = shift @ARGV;
+      error ("unparsable window id: $id")
+        unless ($id =~ m/^\d+$|^0x[\da-f]+$/i);
+      $displayer =~ s/--?root\b/$id/ ||
+        error ("unable to munge displayer: $displayer");
+    }
     elsif (m/^--?window$/) {
       print STDERR "$progname: sorry, \"-window\" is unimplemented.\n";
       print STDERR "$progname: use \"-stdout\" and pipe to a displayer.\n";
@@ -432,10 +474,12 @@ sub main {
     else { usage; }
   }
 
+  init_signals();
+
   read_config;
 
   if (!$use_stdout) {
-    $_ = `xdpyinfo 2>-`;
+    $_ = `xdpyinfo 2>&-`;
     ($screen_width) =~ m/ dimensions: +(\d+)x(\d+) pixels/;
     $screen_width = 800 unless $screen_width > 0;
   }