home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / xscreensaver-getimage-video < prev    next >
Encoding:
Text File  |  2007-02-07  |  4.9 KB  |  180 lines

  1. #!/usr/bin/perl -w
  2. # Copyright ⌐ 2001, 2002, 2003, 2005 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 grab a single frame of video from the system's
  13. # video capture card, and then load it on to the root window using the
  14. # "xscreensaver-getimage-file" program.  Various frame-grabbing programs
  15. # are known, and the first one found is used.
  16. #
  17. # The various xscreensaver hacks that manipulate images ("slidescreen",
  18. # "jigsaw", etc.) get the image to manipulate by running the
  19. # "xscreensaver-getimage" program.
  20. #
  21. # "xscreensaver-getimage" will invoke this program, depending on the
  22. # value of the "grabVideoFrames" setting in the ~/.xscreensaver file
  23. # (or in /usr/lib/X11/app-defaults/XScreenSaver).
  24. #
  25. # Created: 13-Apr-2001.
  26.  
  27. require 5;
  28. use diagnostics;
  29. use strict;
  30.  
  31. my $progname = $0; $progname =~ s@.*/@@g;
  32. my $version = q{ $Revision: 1.16 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
  33.  
  34. my $verbose = 0;
  35.  
  36. my $tmpdir  = $ENV{TMPDIR} || "/tmp";
  37. my $tmpfile1 = sprintf ("%s/xssgv01.ppm", $tmpdir, rand(0xFFFFFFFF));
  38. my $tmpfile3 = sprintf ("%s/xssgv03.ppm", $tmpdir, rand(0xFFFFFFFF));
  39. my $tmpfile = sprintf ("%s/xssgv.%08x.ppm", $tmpdir, rand(0xFFFFFFFF));
  40.  
  41.  
  42. # These are programs that can be used to grab a video frame.  The first one
  43. # of these programs that exists on $PATH will be used, and the image file
  44. # is assumed to be written to $tmpfile (in some image format acceptable to
  45. # "xscreensaver-getimage-file", e.g., PPM or JPEG.)
  46. #
  47. # If you add other programs to this list, please let me know!
  48. #
  49. my @programs = (
  50.   "streamer -a -t3 -r1 -o $tmpfile1; mv $tmpfile3 $tmpfile",        # XawTV after 2 sec
  51.  
  52.   "bttvgrab -d q -Q -l 1 -o ppm -f $tmpfile",    # BTTV
  53.   "qcam > $tmpfile",                # Connectix Qcam
  54.   "gqcam -t PPM -d $tmpfile",            # GTK+ Qcam clone
  55.  
  56.   "v4lctl snap ppm full $tmpfile",        # XawTV 3.94.
  57.  
  58.   "streamer -a -s 768x576 -o $tmpfile",        # XawTV
  59.   #   the "-a" option ("mute audio") arrived with XawTV 3.76.
  60.  
  61.   "atitv snap $tmpfile",            # ATI video capture card
  62.  
  63.   "grab -type ppm -format ntsc -source 1 " .    # *BSD BT848 module
  64.     "-settle 0.75 -output $tmpfile",
  65.  
  66.   "motioneye -j $tmpfile",            # Sony Vaio MotionEye
  67.                         # (hardware jpeg encoder)
  68.  
  69.   "vidcat -b -f ppm -s 640x480 > $tmpfile 2>-",    # w3cam/ovcam
  70.  
  71.   "vidtomem -f $tmpfile 2>&- " .        # Silicon Graphics
  72.     "&& mv $tmpfile-00000.rgb $tmpfile",
  73. );
  74.  
  75.  
  76. sub error {
  77.   ($_) = @_;
  78.   print STDERR "$progname: $_\n";
  79.   exit 1;
  80. }
  81.  
  82. my $displayer = undef;
  83.  
  84. sub pick_grabber {
  85.   my @names = ();
  86.  
  87.   foreach my $cmd (@programs) {
  88.     $_ = $cmd;
  89.     my ($name) = m/^([^ ]+)/;
  90.     push @names, "\"$name\"";
  91.     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
  92.     foreach my $dir (split (/:/, $ENV{PATH})) {
  93.       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
  94.       if (-x "$dir/$name") {
  95.         $displayer = $name;
  96.         return $cmd;
  97.       }
  98.     }
  99.   }
  100.  
  101.   $names[$#names] = "or " . $names[$#names];
  102.   error "none of: " . join (", ", @names) . " were found on \$PATH.";
  103. }
  104.  
  105.  
  106. my $use_stdout_p = 0;
  107. my $return_filename_p = 0;
  108.  
  109. sub grab_image {
  110.   my $cmd = pick_grabber();
  111.   unlink $tmpfile;
  112.  
  113.   print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
  114.   system ($cmd);
  115.  
  116.   if (-z $tmpfile)
  117.     {
  118.       unlink $tmpfile;
  119.       error "\"$cmd\" produced no data.";
  120.     }
  121.  
  122.   if ($return_filename_p) {
  123.     print STDERR "$progname: wrote \"$tmpfile\"\n" if ($verbose);
  124.     print STDOUT "$tmpfile\n";
  125.  
  126.   } elsif ($use_stdout_p) {
  127.     local *IN;
  128.     my $ppm = "";
  129.     my $reader  = "<$tmpfile";
  130.  
  131.     # horrid kludge for SGIs, since they don't use PPM...
  132.     if ($displayer eq "vidtomem") {
  133.       $reader = "sgitopnm $tmpfile";
  134.       $reader .= " 2>/dev/null" if ($verbose <= 1);
  135.       $reader .= " |";
  136.     }
  137.  
  138.     open(IN, $reader) || error "reading $tmpfile: $!";
  139.     print STDERR "$progname: reading $tmpfile\n" if ($verbose > 1);
  140.     while (<IN>) { $ppm .= $_; }
  141.     close IN;
  142.     unlink $tmpfile;
  143.     print STDOUT $ppm;
  144.  
  145.   } else {
  146.  
  147.     $cmd = "xscreensaver-getimage-file";
  148.     $cmd .= " --verbose" if ($verbose);
  149.     $cmd .= " $tmpfile";
  150.  
  151.     print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
  152.     system ($cmd);
  153.  
  154.     unlink $tmpfile;
  155.   }
  156. }
  157.  
  158.  
  159. sub usage {
  160.   print STDERR "usage: $progname [--verbose] [--name | --stdout]\n";
  161.   exit 1;
  162. }
  163.  
  164. sub main {
  165.   while ($_ = $ARGV[0]) {
  166.     shift @ARGV;
  167.     if ($_ eq "--verbose") { $verbose++; }
  168.     elsif (m/^-v+$/) { $verbose += length($_)-1; }
  169.     elsif (m/^--?stdout$/) { $use_stdout_p = 1; }
  170.     elsif (m/^--?name$/)   { $return_filename_p = 1; }
  171.     elsif (m/^-./) { usage; }
  172.     else { usage; }
  173.   }
  174.  
  175.   grab_image();
  176. }
  177.  
  178. main;
  179. exit 0;
  180.