2 # Copyright © 2003-2005 Jamie Zawinski <jwz@jwz.org>.
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
13 # This script is invoked by "xscreensaver-getimage" on X11 MacOS systems
14 # to grab an image of the desktop, and then load it on to the given X11
15 # Drawable using the "xscreensaver-getimage-file" program.
17 # This script is only used in an *X11* build on MacOS systems.
19 # When running on non-Mac X11 systems, utils/grabscreen.c is used.
21 # However, when running under X11 on MacOS, that usual X11-based
22 # screen-grabbing mechanism doesn't work, so we need to invoke the
23 # "/usr/bin/screencapture" program to do it instead. (This script).
25 # However again, for the MacOS-native (Cocoa) build of the screen savers,
26 # "utils/grabclient.c" instead links against "OSX/osxgrabscreen.m", which
27 # grabs screen images directly without invoking a sub-process to do it.
29 # Created: 20-Oct-2003.
33 #use diagnostics; # Fails on some MacOS 10.5 systems
36 my $progname = $0; $progname =~ s@.*/@@g;
37 my $version = q{ $Revision: 1.5 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
39 my @grabber = ("screencapture", "-x");
40 my @converter = ("pdf2jpeg");
44 my $return_filename_p = 0;
49 print STDERR "$progname: $_\n";
53 # returns the full path of the named program, or undef.
57 foreach (split (/:/, $ENV{PATH})) {
67 foreach ($grabber[0], $converter[0]) {
69 print STDERR "$progname: \"$_\" not found on \$PATH.\n";
81 my $tmpdir = $ENV{TMPDIR};
82 $tmpdir = "/tmp" unless $tmpdir;
84 my $tmpfile = sprintf ("%s/xssgrab.%08x.pdf", $tmpdir, rand(0xffffffff));
85 my @cmd = (@grabber, $tmpfile);
89 print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
93 my @st = stat($tmpfile);
94 my $size = (@st ? $st[7] : 0);
98 error "\"" . join(' ', @cmd) . "\" produced no data.";
100 error "\"" . join(' ', @cmd) . "\" produced only $size bytes.";
104 # On MacOS 10.3, "screencapture -x" always wrote a PDF.
105 # On 10.4.2, it writes a PNG by default, and the output format can be
106 # changed with the new "-t" argument.
108 # So, for maximal compatibility, we run it without "-t", but look at
109 # the first few bytes to see if it's a PDF, and if it is, convert it
110 # to a JPEG first. Otherwise, we assume that whatever screencapture
111 # wrote is a file format that xscreensaver-getimage-file can already
112 # cope with (though it will have the extension ".pdf", regardless of
113 # what is actually in the file).
118 open (IN, "<$tmpfile") || error ("$tmpfile: $!");
122 $pdf_p = ($buf =~ m/^%PDF-/s);
125 # If it's a PDF, convert it to a JPEG.
129 my $jpgfile = $tmpfile;
130 $jpgfile =~ s/\.[^.]+$//;
133 @cmd = (@converter, $tmpfile, $jpgfile);
134 push @cmd, "--verbose" if ($verbose);
136 print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
143 @st = stat($tmpfile);
144 $size = (@st ? $st[7] : 0);
148 error "\"" . join(' ', @cmd) . "\" produced no data.";
150 error "\"" . join(' ', @cmd) . "\" produced only $size bytes.";
154 if ($return_filename_p) {
155 print STDERR "$progname: wrote \"$tmpfile\"\n" if ($verbose);
156 print STDOUT "$tmpfile\n";
158 } elsif ($use_stdout_p) {
161 my $reader = "djpeg $tmpfile";
162 $reader .= " 2>/dev/null" if ($verbose <= 1);
165 open(IN, $reader) || error "reading $tmpfile: $!";
166 print STDERR "$progname: reading $tmpfile\n" if ($verbose > 1);
167 while (<IN>) { $ppm .= $_; }
174 @cmd = ("xscreensaver-getimage-file");
175 push @cmd, "--verbose" if ($verbose);
178 print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
188 print STDERR "usage: $progname [--verbose] [--name | --stdout]\n";
193 while ($_ = $ARGV[0]) {
195 if ($_ eq "--verbose") { $verbose++; }
196 elsif (m/^-v+$/) { $verbose += length($_)-1; }
197 elsif (m/^--?stdout$/) { $use_stdout_p = 1; }
198 elsif (m/^--?name$/) { $return_filename_p = 1; }
199 elsif (m/^-./) { usage; }