http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / driver / xscreensaver-getimage-video
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 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-01.
26
27 require 5;
28 use diagnostics;
29 use strict;
30
31 my $progname = $0; $progname =~ s@.*/@@g;
32 my $version = q{ $Revision: 1.7 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
33
34 my $verbose = 0;
35
36 # These are programs that can be used to grab a video frame.  The first one
37 # of these programs that exists on $PATH will be used, and the image file
38 # is assumed to be written to stdout (in some image format acceptable to
39 # "xscreensaver-getimage-file", e.g., PPM or JPEG.)
40 #
41 # If you add other programs to this list, please let me know!
42 #
43
44 my $tmpdir = $ENV{TMPDIR};
45 $tmpdir = "/tmp" unless $tmpdir;
46
47 my $tmpfile = "$tmpdir/xssgv.$$.ppm";
48
49 # this crap is because "vidtomem" can only write to a file, and uses
50 # a stupid, non-overridable file name format.
51 my $sgi_bogosity = "$tmpfile-00000.rgb";
52
53 my @programs = (
54
55   "bttvgrab -d q -Q -l 1 -o ppm -f $tmpfile",   # BTTV
56   "qcam > $tmpfile",                            # Connectix Qcam
57   "streamer -s 768x576 -o $tmpfile",            # XawTV
58   "atitv snap $tmpfile",                        # ATI video capture card
59
60   "grab -type ppm -format ntsc -source 1 " .
61         "-settle 0.75 -output $tmpfile",        # *BSD BT848 module
62
63   "vidtomem -f $tmpfile 2>&- && mv $sgi_bogosity $tmpfile",  # Silicon Graphics
64 );
65
66
67 sub error {
68   ($_) = @_;
69   print STDERR "$progname: $_\n";
70   exit 1;
71 }
72
73 my $displayer = undef;
74
75 sub pick_grabber {
76   my @names = ();
77
78   foreach my $cmd (@programs) {
79     $_ = $cmd;
80     my ($name) = m/^([^ ]+)/;
81     push @names, "\"$name\"";
82     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
83     foreach my $dir (split (/:/, $ENV{PATH})) {
84       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
85       if (-x "$dir/$name") {
86         $displayer = $name;
87         return $cmd;
88       }
89     }
90   }
91
92   $names[$#names] = "or " . $names[$#names];
93   error "none of: " . join (", ", @names) . " were found on \$PATH.";
94 }
95
96
97 my $use_stdout = 0;
98
99 sub grab_image {
100   my $cmd = pick_grabber();
101   unlink $tmpfile;
102
103   print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
104   system ($cmd);
105
106   if (-z $tmpfile)
107     {
108       unlink $tmpfile;
109       error "\"$cmd\" produced no data.";
110     }
111
112   if ($use_stdout) {
113     local *IN;
114     my $ppm = "";
115     my $reader  = "<$tmpfile";
116
117     # horrid kludge for SGIs, since they don't use PPM...
118     if ($displayer eq "vidtomem") {
119       $reader = "sgitopnm $tmpfile";
120       $reader .= " 2>/dev/null" if ($verbose <= 1);
121       $reader .= " |";
122     }
123
124     open(IN, $reader) || error "reading $tmpfile: $!";
125     print STDERR "$progname: reading $tmpfile\n" if ($verbose > 1);
126     while (<IN>) { $ppm .= $_; }
127     close IN;
128     unlink $tmpfile;
129     print STDOUT $ppm;
130
131   } else {
132
133     $cmd = "xscreensaver-getimage-file";
134     $cmd .= " --verbose" if ($verbose);
135     $cmd .= " $tmpfile";
136
137     print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
138     system ($cmd);
139
140     unlink $tmpfile;
141   }
142 }
143
144
145 sub usage {
146   print STDERR "usage: $progname [--verbose] [--stdout]\n";
147   exit 1;
148 }
149
150 sub main {
151   while ($_ = $ARGV[0]) {
152     shift @ARGV;
153     if ($_ eq "--verbose") { $verbose++; }
154     elsif (m/^-v+$/) { $verbose += length($_)-1; }
155     elsif (m/^--?stdout$/) { $use_stdout = 1; }
156     elsif (m/^-./) { usage; }
157     else { usage; }
158   }
159
160   grab_image();
161 }
162
163 main;
164 exit 0;