http://www.jwz.org/xscreensaver/xscreensaver-5.13.tar.gz
[xscreensaver] / driver / xscreensaver-getimage-video
1 #!/usr/bin/perl -w
2 # Copyright © 2001-2011 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 # The various screen savers invoke "xscreensaver-getimage", which will in
22 # turn invoke this program, depending on the value of the "grabVideoFrames"
23 # setting in the ~/.xscreensaver file (or in the app-defaults file, usually
24 # /usr/lib/X11/app-defaults/XScreenSaver).
25 #
26 # Created: 13-Apr-2001.
27
28 require 5;
29 #use diagnostics;       # Fails on some MacOS 10.5 systems
30 use strict;
31
32 my $progname = $0; $progname =~ s@.*/@@g;
33 my $version  = q{ $Revision: 1.21 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
34
35 my $tmpdir   = $ENV{TMPDIR} || "/tmp";
36 my $tmpfile  = sprintf("%s/xssv.%08x.ppm", $tmpdir, rand(0xFFFFFFFF));
37
38 my $verbose           = 0;
39 my $use_stdout_p      = 0;
40 my $return_filename_p = 0;
41
42
43 # These are programs that can be used to grab a video frame.  The first one
44 # of these programs that exists on $PATH will be used, and the image file
45 # is assumed to be written to $tmpfile (in some image format acceptable to
46 # "xscreensaver-getimage-file", e.g., PPM or JPEG.)
47 #
48 # If you add other programs to this list, please let me know!
49 #
50 my @programs = (
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   # "mplayer -really-quiet tv://0 " .           # Maybe works with some cams?
75   #         "-ao null -vo pnm -frames 1 2>&- " .
76   #     "&& mv 00000001.ppm $tmpfile",
77
78 );
79
80
81 sub error($) {
82   my ($e) = @_;
83   print STDERR "$progname: $e\n";
84   exit 1;
85 }
86
87 sub pick_grabber() {
88   my @names = ();
89
90   foreach my $cmd (@programs) {
91     $_ = $cmd;
92     my ($name) = m/^([^ ]+)/;
93     push @names, "\"$name\"";
94     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
95     foreach my $dir (split (/:/, $ENV{PATH})) {
96       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
97       if (-x "$dir/$name") {
98         return $cmd;
99       }
100     }
101   }
102
103   $names[$#names] = "or " . $names[$#names];
104   error ("none of: " . join (", ", @names) . " were found on \$PATH.");
105 }
106
107
108 sub grab_image() {
109   my $cmd = pick_grabber();
110   unlink $tmpfile;
111
112   print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
113   system ($cmd);
114
115   if (-z $tmpfile)
116     {
117       unlink $tmpfile;
118       error ("\"$cmd\" produced no data.");
119     }
120
121   if ($return_filename_p) {
122     print STDERR "$progname: wrote \"$tmpfile\"\n" if ($verbose);
123     print STDOUT "$tmpfile\n";
124
125   } elsif ($use_stdout_p) {
126     my $ppm = "";
127     my $reader  = "<$tmpfile";
128
129     # horrid kludge for SGIs, since they don't use PPM...
130     if ($cmd =~ m/^vidtomem\s/) {
131       $reader  = "sgitopnm $tmpfile";
132       $reader .= " 2>/dev/null" if ($verbose <= 1);
133       $reader .= " |";
134     }
135
136     open (my $in, $reader) || error ("reading $tmpfile: $!");
137     print STDERR "$progname: reading $tmpfile\n" if ($verbose > 1);
138     local $/ = undef;  # read entire file
139     $ppm = <$in>;
140     close $in;
141     unlink $tmpfile;
142     print STDOUT $ppm;
143
144   } else {
145
146     $cmd = "xscreensaver-getimage-file";
147     $cmd .= " --verbose" if ($verbose);
148     $cmd .= " $tmpfile";
149
150     print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
151     system ($cmd);
152
153     unlink $tmpfile;
154   }
155 }
156
157
158 sub usage() {
159   print STDERR "usage: $progname [--verbose] [--name | --stdout]\n";
160   exit 1;
161 }
162
163 sub main() {
164   while ($_ = $ARGV[0]) {
165     shift @ARGV;
166     if ($_ eq "--verbose") { $verbose++; }
167     elsif (m/^-v+$/) { $verbose += length($_)-1; }
168     elsif (m/^--?stdout$/) { $use_stdout_p = 1; }
169     elsif (m/^--?name$/)   { $return_filename_p = 1; }
170     elsif (m/^-./) { usage; }
171     else { usage; }
172   }
173
174   grab_image();
175 }
176
177 main;
178 exit 0;