http://ftp.aanet.ru/pub/Linux/X11/apps/xscreensaver-2.31.tar.gz
[xscreensaver] / hacks / blitspin.c
old mode 100755 (executable)
new mode 100644 (file)
index 091d84d..cf2941b
@@ -1,4 +1,4 @@
-/* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.com>
+/* xscreensaver, Copyright (c) 1992-1997 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
 # endif
 #endif
 
-#ifndef VMS
-#include <X11/Xmu/Drawing.h>
-#else
-#include "sys$common:[decw$include.xmu]Drawing.h"
+#ifdef HAVE_XMU
+# ifndef VMS
+#  include <X11/Xmu/Drawing.h>
+#else  /* VMS */
+#  include <Xmu/Drawing.h>
+# endif /* VMS */
 #endif
 
-#include "default.xbm"
+#include "images/som.xbm"
 
 static Display *dpy;
 static Window window;
@@ -53,7 +55,7 @@ static Pixmap bitmap;
 static int depth;
 static unsigned int fg, bg;
 
-static void rotate(), init (), display ();
+static void display (Pixmap);
 
 #define copy_all_to(from, xoff, yoff, to, gc)          \
   XCopyArea (dpy, (from), (to), (gc), 0, 0,            \
@@ -64,7 +66,7 @@ static void rotate(), init (), display ();
             size-(xoff), size-(yoff), 0, 0)
 
 static void
-rotate ()
+rotate (void)
 {
   int qwad; /* fuckin' C, man... who needs namespaces? */
   XFillRectangle (dpy, mask, CLR, 0, 0, size, size);
@@ -92,15 +94,34 @@ rotate ()
 }
 
 static void
-read_bitmap (bitmap_name, widthP, heightP)
-     char *bitmap_name;
-     int *widthP, *heightP;
+read_bitmap (char *bitmap_name, int *widthP, int *heightP)
 {
 #ifdef HAVE_XPM
+  XWindowAttributes xgwa;
   XpmAttributes xpmattrs;
   int result;
   xpmattrs.valuemask = 0;
   bitmap = 0;
+
+  XGetWindowAttributes (dpy, window, &xgwa);
+
+# ifdef XpmCloseness
+  xpmattrs.valuemask |= XpmCloseness;
+  xpmattrs.closeness = 40000;
+# endif
+# ifdef XpmVisual
+  xpmattrs.valuemask |= XpmVisual;
+  xpmattrs.visual = xgwa.visual;
+# endif
+# ifdef XpmDepth
+  xpmattrs.valuemask |= XpmDepth;
+  xpmattrs.depth = xgwa.depth;
+# endif
+# ifdef XpmColormap
+  xpmattrs.valuemask |= XpmColormap;
+  xpmattrs.colormap = xgwa.colormap;
+# endif
+
   result = XpmReadFileToPixmap (dpy, window, bitmap_name, &bitmap, 0,
                                &xpmattrs);
   switch (result)
@@ -129,6 +150,8 @@ read_bitmap (bitmap_name, widthP, heightP)
     }
   if (! bitmap)
 #endif
+
+#ifdef HAVE_XMU
     {
       int xh, yh;
       Pixmap b2;
@@ -145,18 +168,70 @@ read_bitmap (bitmap_name, widthP, heightP)
       XFreePixmap (dpy, bitmap);
       bitmap = b2;
     }
+#else  /* !XMU */
+    {
+      fprintf (stderr,
+              "%s: your vendor doesn't ship the standard Xmu library.\n",
+              progname);
+      fprintf (stderr, "\tWe can't load XBM files without it.\n");
+      exit (1);
+    }
+#endif /* !XMU */
+}
+
+
+static Pixmap
+read_screen (Display *dpy, Window window, int *widthP, int *heightP)
+{
+  Pixmap p;
+  XWindowAttributes xgwa;
+  XGCValues gcv;
+  GC gc;
+  XGetWindowAttributes (dpy, window, &xgwa);
+  *widthP = xgwa.width;
+  *heightP = xgwa.height;
+
+  grab_screen_image(xgwa.screen, window);
+  p = XCreatePixmap(dpy, window, *widthP, *heightP, xgwa.depth);
+  gcv.function = GXcopy;
+  gc = XCreateGC (dpy, window, GCFunction, &gcv);
+  XCopyArea (dpy, window, p, gc, 0, 0, *widthP, *heightP, 0, 0);
+
+  /* Reset the window's background color... */
+  XSetWindowBackground (dpy, window,
+                       get_pixel_resource ("background", "Background",
+                                           dpy, xgwa.colormap));
+  XCopyArea (dpy, p, window, gc, 0, 0, *widthP, *heightP, 0, 0);
+  XFreeGC (dpy, gc);
+
+  return p;
+}
+
+
+static int 
+to_pow2(int n, Bool up)
+{
+  /* sizeof(Dimension) == 2. */
+  int powers_of_2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
+                       2048, 4096, 8192, 16384, 32768, 65536 };
+  int i = 0;
+  if (n > 65536) size = 65536;
+  while (n >= powers_of_2[i]) i++;
+  if (n == powers_of_2[i-1])
+    return n;
+  else
+    return powers_of_2[up ? i : i-1];
 }
 
 static void
-init ()
+init (void)
 {
   XWindowAttributes xgwa;
   Colormap cmap;
   XGCValues gcv;
   int width, height;
-  unsigned int real_size;
   char *bitmap_name;
-  int i;
+  Bool scale_up;
 
   XGetWindowAttributes (dpy, window, &xgwa);
   cmap = xgwa.colormap;
@@ -174,24 +249,33 @@ init ()
 
   if (!strcmp (bitmap_name, "(default)"))
     {
-      width = logo_width;
-      height = logo_height;
-      bitmap = XCreatePixmapFromBitmapData (dpy, window, (char *) logo_bits,
+      width = som_width;
+      height = som_height;
+      bitmap = XCreatePixmapFromBitmapData (dpy, window, (char *) som_bits,
                                            width, height, fg, bg, depth);
+      scale_up = True; /* definitely. */
+    }
+  else if (!strcmp (bitmap_name, "(screen)"))
+    {
+      bitmap = read_screen (dpy, window, &width, &height);
+      scale_up = True; /* maybe? */
     }
   else
     {
       read_bitmap (bitmap_name, &width, &height);
+      scale_up = True; /* probably? */
     }
 
-  real_size = (width > height) ? width : height;
+  size = (width < height) ? height : width;    /* make it square */
+  size = to_pow2(size, scale_up);              /* round up to power of 2 */
+  {                                            /* don't exceed screen size */
+    int s = XScreenNumberOfScreen(xgwa.screen);
+    int w = to_pow2(XDisplayWidth(dpy, s), False);
+    int h = to_pow2(XDisplayHeight(dpy, s), False);
+    if (size > w) size = w;
+    if (size > h) size = h;
+  }
 
-  size = real_size;
-  /* semi-sleazy way of doing (setq size (expt 2 (ceiling (log size 2)))). */
-  for (i = 31; i > 0; i--)
-    if (size & (1<<i)) break;
-  if (size & (~(1<<i)))
-    size = (size>>i)<<(i+1);
   self = XCreatePixmap (dpy, window, size, size, depth);
   temp = XCreatePixmap (dpy, window, size, size, depth);
   mask = XCreatePixmap (dpy, window, size, size, depth);
@@ -211,11 +295,14 @@ init ()
 
   XCopyArea (dpy, bitmap, self, CPY, 0, 0, width, height,
             (size - width)>>1, (size - height)>>1);
+  XFreePixmap(dpy, bitmap);
+
+  display (self);
+  XSync(dpy, False);
 }
 
 static void
-display (pixmap)
-     Pixmap pixmap;
+display (Pixmap pixmap)
 {
   XWindowAttributes xgwa;
   static int last_w = 0, last_h = 0;
@@ -246,29 +333,30 @@ display (pixmap)
 char *progclass = "BlitSpin";
 
 char *defaults [] = {
-  "BlitSpin.background:        black",         /* to placate SGI */
-  "BlitSpin.foreground:        white",
+  ".background:        black",
+  ".foreground:        white",
   "*delay:     500000",
   "*delay2:    500000",
   "*bitmap:    (default)",
+  "*geometry:  512x512",
   0
 };
 
 XrmOptionDescRec options [] = {
   { "-delay",          ".delay",       XrmoptionSepArg, 0 },
   { "-delay2",         ".delay2",      XrmoptionSepArg, 0 },
-  { "-bitmap",         ".bitmap",      XrmoptionSepArg, 0 }
+  { "-bitmap",         ".bitmap",      XrmoptionSepArg, 0 },
+  { "-grab-screen",    ".bitmap",      XrmoptionNoArg, "(screen)" },
+  { 0, 0, 0, 0 }
 };
-int options_size = (sizeof (options) / sizeof (options[0]));
 
 void
-screenhack (d, w)
-     Display *d;
-     Window w;
+screenhack (Display *d, Window w)
 {
   dpy = d;
   window = w;
   init ();
+  if (delay2) usleep (delay2 * 2);
   while (1)
     {
       rotate ();