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