2 # Copyright © 2003-2013 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.6 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
39 my @grabber = ("screencapture", "-x");
40 my @converter = ("pdf2jpeg");
47 print STDERR "$progname: $_\n";
51 # returns the full path of the named program, or undef.
55 foreach (split (/:/, $ENV{PATH})) {
65 foreach ($grabber[0], $converter[0]) {
67 print STDERR "$progname: \"$_\" not found on \$PATH.\n";
79 my $tmpdir = $ENV{TMPDIR};
80 $tmpdir = "/tmp" unless $tmpdir;
82 my $tmpfile = sprintf ("%s/xssgrab.%08x.pdf", $tmpdir, rand(0xffffffff));
83 my @cmd = (@grabber, $tmpfile);
87 print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
89 system (join(' ', @cmd) . ' 2>/dev/null');
91 my @st = stat($tmpfile);
92 my $size = (@st ? $st[7] : 0);
96 error "\"" . join(' ', @cmd) . "\" produced no data.";
98 error "\"" . join(' ', @cmd) . "\" produced only $size bytes.";
102 # On MacOS 10.3, "screencapture -x" always wrote a PDF.
103 # On 10.4.2, it writes a PNG by default, and the output format can be
104 # changed with the new "-t" argument.
106 # So, for maximal compatibility, we run it without "-t", but look at
107 # the first few bytes to see if it's a PDF, and if it is, convert it
108 # to a JPEG first. Otherwise, we assume that whatever screencapture
109 # wrote is a file format that xscreensaver-getimage-file can already
110 # cope with (though it will have the extension ".pdf", regardless of
111 # what is actually in the file).
115 open (my $in, '<:raw', $tmpfile) || error ("$tmpfile: $!");
117 read ($in, $buf, 10);
119 $pdf_p = ($buf =~ m/^%PDF-/s);
122 # If it's a PDF, convert it to a JPEG.
126 my $jpgfile = $tmpfile;
127 $jpgfile =~ s/\.[^.]+$//;
130 @cmd = (@converter, $tmpfile, $jpgfile);
131 push @cmd, "--verbose" if ($verbose);
133 print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
140 @st = stat($tmpfile);
141 $size = (@st ? $st[7] : 0);
145 error "\"" . join(' ', @cmd) . "\" produced no data.";
147 error "\"" . join(' ', @cmd) . "\" produced only $size bytes.";
151 print STDERR "$progname: wrote \"$tmpfile\"\n" if ($verbose);
152 print STDOUT "$tmpfile\n";
157 print STDERR "usage: $progname [--verbose]\n";
162 while ($_ = $ARGV[0]) {
164 if (m/^--?verbose$/s) { $verbose++; }
165 elsif (m/^-v+$/s) { $verbose += length($_)-1; }
166 elsif (m/^--?name$/s) { } # ignored, for compatibility
167 elsif (m/^-./) { usage; }