http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.tar.gz
[xscreensaver] / driver / xscreensaver-getimage-desktop
index f7d9e7650aa371aaf5bd049dfa95f330099246ac..627cd254b2273f46c5b69dcd2ff09f91917e20ef 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright © 2003 Jamie Zawinski <jwz@jwz.org>.
+# Copyright © 2003, 2005 Jamie Zawinski <jwz@jwz.org>.
 #
 # Permission to use, copy, modify, distribute, and sell this software and its
 # documentation for any purpose is hereby granted without fee, provided that
 # Created: 20-Oct-2003.
 
 require 5;
-use diagnostics;
+#use diagnostics;      # Fails on some MacOS 10.5 systems
 use strict;
 
 my $progname = $0; $progname =~ s@.*/@@g;
-my $version = q{ $Revision: 1.1 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
+my $version = q{ $Revision: 1.4 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
 
 my @grabber   = ("screencapture", "-x");
 my @converter = ("pdf2jpeg");
@@ -81,7 +81,7 @@ sub grab_image {
   my $tmpdir = $ENV{TMPDIR};
   $tmpdir = "/tmp" unless $tmpdir;
 
-  my $tmpfile = "$tmpdir/xssgrab.$$.pdf";
+  my $tmpfile = sprintf ("%s/xssgrab.%08x.pdf", $tmpdir, rand(0xffffffff));
   my @cmd     = (@grabber, $tmpfile);
 
   unlink $tmpfile;
@@ -101,22 +101,45 @@ sub grab_image {
     }
   }
 
-  # Convert the PDF to a JPEG
+  # On MacOS 10.3, "screencapture -x" always wrote a PDF.
+  # On 10.4.2, it writes a PNG by default, and the output format can be
+  # changed with the new "-t" argument.
+  #
+  # So, for maximal compatibility, we run it without "-t", but look at
+  # the first few bytes to see if it's a PDF, and if it is, convert it
+  # to a JPEG first.  Otherwise, we assume that whatever screencapture
+  # wrote is a file format that xscreensaver-getimage-file can already
+  # cope with (though it will have the extension ".pdf", regardless of
+  # what is actually in the file).
+  #
+  my $pdf_p = 0;
   {
-    my $jpgfile = $tmpfile;
-    $jpgfile =~ s/\.[^.]+$//;
-    $jpgfile .= ".jpg";
-
-    @cmd = (@converter, $tmpfile, $jpgfile);
-    push @cmd, "--verbose" if ($verbose);
-
-    print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
-      if ($verbose);
-    system (@cmd);
-    unlink $tmpfile;
-    $tmpfile = $jpgfile;
+    local *IN;
+    open (IN, "<$tmpfile") || error ("$tmpfile: $!");
+    my $buf = '';
+    read (IN, $buf, 10);
+    close IN;
+    $pdf_p = ($buf =~ m/^%PDF-/s);
   }
 
+  # If it's a PDF, convert it to a JPEG.
+  #
+  if ($pdf_p)
+    {
+      my $jpgfile = $tmpfile;
+      $jpgfile =~ s/\.[^.]+$//;
+      $jpgfile .= ".jpg";
+
+      @cmd = (@converter, $tmpfile, $jpgfile);
+      push @cmd, "--verbose" if ($verbose);
+
+      print STDERR "$progname: executing \"" . join(' ', @cmd) . "\"\n"
+        if ($verbose);
+      system (@cmd);
+      unlink $tmpfile;
+      $tmpfile = $jpgfile;
+    }
+
   @st = stat($tmpfile);
   $size = (@st ? $st[7] : 0);
   if ($size <= 2048) {