http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.14.tar.gz
[xscreensaver] / driver / xscreensaver-getimage-desktop
1 #!/usr/bin/perl -w
2 # Copyright © 2003 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 an image of the desktop, and then load
13 # it on to the root window using the "xscreensaver-getimage-file"
14 # program.  Various frame-grabbing programs are known, and the first
15 # one found is used.
16 #
17 # NOTE:  This script is only used on MacOS X / XDarwin systems, because
18 #        on those systems, it's necessary to use the "screencapture"
19 #        program to get an image of the desktop -- the usual X11
20 #        mechanism for grabbing the screen doesn't work on OSX.
21 #
22 # The various xscreensaver hacks that manipulate images ("slidescreen",
23 # "jigsaw", etc.) get the image to manipulate by running the
24 # "xscreensaver-getimage" program.
25 #
26 # "xscreensaver-getimage" will invoke this program, depending on the
27 # value of the "grabDesktopImages" setting in the ~/.xscreensaver file
28 # (or in /usr/lib/X11/app-defaults/XScreenSaver).
29 #
30 # Created: 20-Oct-2003.
31
32 require 5;
33 use diagnostics;
34 use strict;
35
36 my $progname = $0; $progname =~ s@.*/@@g;
37 my $version = q{ $Revision: 1.1 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
38
39 my @grabber   = ("screencapture", "-x");
40 my @converter = ("pdf2jpeg");
41
42 my $verbose = 0;
43 my $use_stdout_p = 0;
44 my $return_filename_p = 0;
45
46
47 sub error {
48   ($_) = @_;
49   print STDERR "$progname: $_\n";
50   exit 1;
51 }
52
53 # returns the full path of the named program, or undef.
54 #
55 sub which {
56   my ($prog) = @_;
57   foreach (split (/:/, $ENV{PATH})) {
58     if (-x "$_/$prog") {
59       return $prog;
60     }
61   }
62   return undef;
63 }
64
65 sub check_path {
66   my $ok = 1;
67   foreach ($grabber[0], $converter[0]) {
68     if (! which ($_)) {
69       print STDERR "$progname: \"$_\" not found on \$PATH.\n";
70       $ok = 0;
71     }
72   }
73   exit (1) unless $ok;
74 }
75
76
77 sub grab_image {
78
79   check_path();
80
81   my $tmpdir = $ENV{TMPDIR};
82   $tmpdir = "/tmp" unless $tmpdir;
83
84   my $tmpfile = "$tmpdir/xssgrab.$$.pdf";
85   my @cmd     = (@grabber, $tmpfile);
86
87   unlink $tmpfile;
88
89   print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
90     if ($verbose);
91   system (@cmd);
92
93   my @st = stat($tmpfile);
94   my $size = (@st ? $st[7] : 0);
95   if ($size <= 2048) {
96     unlink $tmpfile;
97     if ($size == 0) {
98       error "\"" . join(' ', @cmd) . "\" produced no data.";
99     } else {
100       error "\"" . join(' ', @cmd) . "\" produced only $size bytes.";
101     }
102   }
103
104   # Convert the PDF to a JPEG
105   {
106     my $jpgfile = $tmpfile;
107     $jpgfile =~ s/\.[^.]+$//;
108     $jpgfile .= ".jpg";
109
110     @cmd = (@converter, $tmpfile, $jpgfile);
111     push @cmd, "--verbose" if ($verbose);
112
113     print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
114       if ($verbose);
115     system (@cmd);
116     unlink $tmpfile;
117     $tmpfile = $jpgfile;
118   }
119
120   @st = stat($tmpfile);
121   $size = (@st ? $st[7] : 0);
122   if ($size <= 2048) {
123     unlink $tmpfile;
124     if ($size == 0) {
125       error "\"" . join(' ', @cmd) . "\" produced no data.";
126     } else {
127       error "\"" . join(' ', @cmd) . "\" produced only $size bytes.";
128     }
129   }
130
131   if ($return_filename_p) {
132     print STDERR "$progname: wrote \"$tmpfile\"\n" if ($verbose);
133     print STDOUT "$tmpfile\n";
134
135   } elsif ($use_stdout_p) {
136     local *IN;
137     my $ppm = "";
138     my $reader = "djpeg $tmpfile";
139     $reader .= " 2>/dev/null" if ($verbose <= 1);
140     $reader .= " |";
141
142     open(IN, $reader) || error "reading $tmpfile: $!";
143     print STDERR "$progname: reading $tmpfile\n" if ($verbose > 1);
144     while (<IN>) { $ppm .= $_; }
145     close IN;
146     unlink $tmpfile;
147     print STDOUT $ppm;
148
149   } else {
150
151     @cmd = ("xscreensaver-getimage-file");
152     push @cmd, "--verbose" if ($verbose);
153     push @cmd, $tmpfile;
154
155     print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
156       if ($verbose);
157     system (@cmd);
158
159     unlink $tmpfile;
160   }
161 }
162
163
164 sub usage {
165   print STDERR "usage: $progname [--verbose] [--name | --stdout]\n";
166   exit 1;
167 }
168
169 sub main {
170   while ($_ = $ARGV[0]) {
171     shift @ARGV;
172     if ($_ eq "--verbose") { $verbose++; }
173     elsif (m/^-v+$/) { $verbose += length($_)-1; }
174     elsif (m/^--?stdout$/) { $use_stdout_p = 1; }
175     elsif (m/^--?name$/)   { $return_filename_p = 1; }
176     elsif (m/^-./) { usage; }
177     else { usage; }
178   }
179
180   grab_image();
181 }
182
183 main;
184 exit 0;