2 # Copyright © 2003 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
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
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.
22 # The various xscreensaver hacks that manipulate images ("slidescreen",
23 # "jigsaw", etc.) get the image to manipulate by running the
24 # "xscreensaver-getimage" program.
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).
30 # Created: 20-Oct-2003.
36 my $progname = $0; $progname =~ s@.*/@@g;
37 my $version = q{ $Revision: 1.2 $ }; $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 # Convert the PDF to a JPEG
106 my $jpgfile = $tmpfile;
107 $jpgfile =~ s/\.[^.]+$//;
110 @cmd = (@converter, $tmpfile, $jpgfile);
111 push @cmd, "--verbose" if ($verbose);
113 print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
120 @st = stat($tmpfile);
121 $size = (@st ? $st[7] : 0);
125 error "\"" . join(' ', @cmd) . "\" produced no data.";
127 error "\"" . join(' ', @cmd) . "\" produced only $size bytes.";
131 if ($return_filename_p) {
132 print STDERR "$progname: wrote \"$tmpfile\"\n" if ($verbose);
133 print STDOUT "$tmpfile\n";
135 } elsif ($use_stdout_p) {
138 my $reader = "djpeg $tmpfile";
139 $reader .= " 2>/dev/null" if ($verbose <= 1);
142 open(IN, $reader) || error "reading $tmpfile: $!";
143 print STDERR "$progname: reading $tmpfile\n" if ($verbose > 1);
144 while (<IN>) { $ppm .= $_; }
151 @cmd = ("xscreensaver-getimage-file");
152 push @cmd, "--verbose" if ($verbose);
155 print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
165 print STDERR "usage: $progname [--verbose] [--name | --stdout]\n";
170 while ($_ = $ARGV[0]) {
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; }