4dc449ff53fe52c5088cec86d68b9d2690312554
[xscreensaver] / driver / xscreensaver-getimage-video
1 #!/usr/bin/perl -w
2 # Copyright © 2001 Jamie Zawinski <jwz@jwz.org>, all rights reserved.
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.6 $ }; $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   "vidtomem -f $tmpfile 2>&- && mv $sgi_bogosity $tmpfile",  # Silicon Graphics
61 );
62
63
64 sub error {
65   ($_) = @_;
66   print STDERR "$progname: $_\n";
67   exit 1;
68 }
69
70 my $displayer = undef;
71
72 sub pick_grabber {
73   my @names = ();
74
75   foreach my $cmd (@programs) {
76     $_ = $cmd;
77     my ($name) = m/^([^ ]+)/;
78     push @names, "\"$name\"";
79     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
80     foreach my $dir (split (/:/, $ENV{PATH})) {
81       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
82       if (-x "$dir/$name") {
83         $displayer = $name;
84         return $cmd;
85       }
86     }
87   }
88
89   $names[$#names] = "or " . $names[$#names];
90   error "none of: " . join (", ", @names) . " were found on \$PATH.";
91 }
92
93
94 my $use_stdout = 0;
95
96 sub grab_image {
97   my $cmd = pick_grabber();
98   unlink $tmpfile;
99
100   print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
101   system ($cmd);
102
103   if (-z $tmpfile)
104     {
105       unlink $tmpfile;
106       error "\"$cmd\" produced no data.";
107     }
108
109   if ($use_stdout) {
110     local *IN;
111     my $ppm = "";
112     my $reader  = "<$tmpfile";
113
114     # horrid kludge for SGIs, since they don't use PPM...
115     if ($displayer eq "vidtomem") {
116       $reader = "sgitopnm $tmpfile";
117       $reader .= " 2>/dev/null" if ($verbose <= 1);
118       $reader .= " |";
119     }
120
121     open(IN, $reader) || error "reading $tmpfile: $!";
122     print STDERR "$progname: reading $tmpfile\n" if ($verbose > 1);
123     while (<IN>) { $ppm .= $_; }
124     close IN;
125     unlink $tmpfile;
126     print STDOUT $ppm;
127
128   } else {
129
130     $cmd = "xscreensaver-getimage-file";
131     $cmd .= " --verbose" if ($verbose);
132     $cmd .= " $tmpfile";
133
134     print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
135     system ($cmd);
136
137     unlink $tmpfile;
138   }
139 }
140
141
142 sub usage {
143   print STDERR "usage: $progname [--verbose] [--stdout]\n";
144   exit 1;
145 }
146
147 sub main {
148   while ($_ = $ARGV[0]) {
149     shift @ARGV;
150     if ($_ eq "--verbose") { $verbose++; }
151     elsif (m/^-v+$/) { $verbose += length($_)-1; }
152     elsif (m/^--?stdout$/) { $use_stdout = 1; }
153     elsif (m/^-./) { usage; }
154     else { usage; }
155   }
156
157   grab_image();
158 }
159
160 main;
161 exit 0;