ftp://ftp.uni-heidelberg.de/pub/X11/contrib/applications/xscreensaver-2.07.tar.gz
[xscreensaver] / hacks / blitspin.c
1 /* xscreensaver, Copyright (c) 1992-1997 Jamie Zawinski <jwz@netscape.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 /* Rotate a bitmap using using bitblts.
13    The bitmap must be square, and must be a power of 2 in size.
14    This was translated from SmallTalk code which appeared in the
15    August 1981 issue of Byte magazine.
16
17    The input bitmap may be non-square, it is padded and centered
18    with the background color.  Another way would be to subdivide
19    the bitmap into square components and rotate them independently
20    (and preferably in parallel), but I don't think that would be as
21    interesting looking.
22
23    It's too bad almost nothing uses blitter hardware these days,
24    or this might actually win.
25  */
26
27 #include "screenhack.h"
28 #include <stdio.h>
29
30 #ifdef HAVE_XPM
31 # include <X11/xpm.h>
32 # ifndef PIXEL_ALREADY_TYPEDEFED
33 #  define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
34 # endif
35 #endif
36
37 #ifdef HAVE_XMU
38 # ifndef VMS
39 #  include <X11/Xmu/Drawing.h>
40 #else  /* VMS */
41 #  include <Xmu/Drawing.h>
42 # endif /* VMS */
43 #endif
44
45 #include "default.xbm"
46
47 static Display *dpy;
48 static Window window;
49 static unsigned int size;
50 static Pixmap self, temp, mask;
51 static GC SET, CLR, CPY, IOR, AND, XOR;
52 static GC gc;
53 static int delay, delay2;
54 static Pixmap bitmap;
55 static int depth;
56 static unsigned int fg, bg;
57
58 static void display (Pixmap);
59
60 #define copy_all_to(from, xoff, yoff, to, gc)           \
61   XCopyArea (dpy, (from), (to), (gc), 0, 0,             \
62              size-(xoff), size-(yoff), (xoff), (yoff))
63
64 #define copy_all_from(to, xoff, yoff, from, gc)         \
65   XCopyArea (dpy, (from), (to), (gc), (xoff), (yoff),   \
66              size-(xoff), size-(yoff), 0, 0)
67
68 static void
69 rotate (void)
70 {
71   int qwad; /* fuckin' C, man... who needs namespaces? */
72   XFillRectangle (dpy, mask, CLR, 0, 0, size, size);
73   XFillRectangle (dpy, mask, SET, 0, 0, size>>1, size>>1);
74   for (qwad = size>>1; qwad > 0; qwad>>=1)
75     {
76       if (delay) usleep (delay);
77       copy_all_to   (mask,       0,       0, temp, CPY);  /* 1 */
78       copy_all_to   (mask,       0,    qwad, temp, IOR);  /* 2 */
79       copy_all_to   (self,       0,       0, temp, AND);  /* 3 */
80       copy_all_to   (temp,       0,       0, self, XOR);  /* 4 */
81       copy_all_from (temp,    qwad,       0, self, XOR);  /* 5 */
82       copy_all_from (self,    qwad,       0, self, IOR);  /* 6 */
83       copy_all_to   (temp,    qwad,       0, self, XOR);  /* 7 */
84       copy_all_to   (self,       0,       0, temp, CPY);  /* 8 */
85       copy_all_from (temp,    qwad,    qwad, self, XOR);  /* 9 */
86       copy_all_to   (mask,       0,       0, temp, AND);  /* A */
87       copy_all_to   (temp,       0,       0, self, XOR);  /* B */
88       copy_all_to   (temp,    qwad,    qwad, self, XOR);  /* C */
89       copy_all_from (mask, qwad>>1, qwad>>1, mask, AND);  /* D */
90       copy_all_to   (mask,    qwad,       0, mask, IOR);  /* E */
91       copy_all_to   (mask,       0,    qwad, mask, IOR);  /* F */
92       display (self);
93     }
94 }
95
96 static void
97 read_bitmap (char *bitmap_name, int *widthP, int *heightP)
98 {
99 #ifdef HAVE_XPM
100   XWindowAttributes xgwa;
101   XpmAttributes xpmattrs;
102   int result;
103   xpmattrs.valuemask = 0;
104   bitmap = 0;
105
106   XGetWindowAttributes (dpy, window, &xgwa);
107
108 # ifdef XpmCloseness
109   xpmattrs.valuemask |= XpmCloseness;
110   xpmattrs.closeness = 40000;
111 # endif
112 # ifdef XpmVisual
113   xpmattrs.valuemask |= XpmVisual;
114   xpmattrs.visual = xgwa.visual;
115 # endif
116 # ifdef XpmDepth
117   xpmattrs.valuemask |= XpmDepth;
118   xpmattrs.depth = xgwa.depth;
119 # endif
120 # ifdef XpmColormap
121   xpmattrs.valuemask |= XpmColormap;
122   xpmattrs.colormap = xgwa.colormap;
123 # endif
124
125   result = XpmReadFileToPixmap (dpy, window, bitmap_name, &bitmap, 0,
126                                 &xpmattrs);
127   switch (result)
128     {
129     case XpmColorError:
130       fprintf (stderr, "%s: warning: xpm color substitution performed\n",
131                progname);
132       /* fall through */
133     case XpmSuccess:
134       *widthP = xpmattrs.width;
135       *heightP = xpmattrs.height;
136       break;
137     case XpmFileInvalid:
138     case XpmOpenFailed:
139       bitmap = 0;
140       break;
141     case XpmColorFailed:
142       fprintf (stderr, "%s: xpm: color allocation failed\n", progname);
143       exit (-1);
144     case XpmNoMemory:
145       fprintf (stderr, "%s: xpm: out of memory\n", progname);
146       exit (-1);
147     default:
148       fprintf (stderr, "%s: xpm: unknown error code %d\n", progname, result);
149       exit (-1);
150     }
151   if (! bitmap)
152 #endif
153
154 #ifdef HAVE_XMU
155     {
156       int xh, yh;
157       Pixmap b2;
158       bitmap = XmuLocateBitmapFile (DefaultScreenOfDisplay (dpy), bitmap_name,
159                                     0, 0, widthP, heightP, &xh, &yh);
160       if (! bitmap)
161         {
162           fprintf (stderr, "%s: couldn't find bitmap %s\n", progname,
163                    bitmap_name);
164           exit (1);
165         }
166       b2 = XmuCreatePixmapFromBitmap (dpy, window, bitmap, *widthP, *heightP,
167                                       depth, fg, bg);
168       XFreePixmap (dpy, bitmap);
169       bitmap = b2;
170     }
171 #else  /* !XMU */
172     {
173       fprintf (stderr,
174                "%s: your vendor doesn't ship the standard Xmu library.\n",
175                progname);
176       fprintf (stderr, "\tWe can't load XBM files without it.\n");
177       exit (1);
178     }
179 #endif /* !XMU */
180 }
181
182
183 static Pixmap
184 read_screen (Display *dpy, Window window, int *widthP, int *heightP)
185 {
186   Pixmap p;
187   XWindowAttributes xgwa;
188   XGCValues gcv;
189   GC gc;
190   XGetWindowAttributes (dpy, window, &xgwa);
191   *widthP = xgwa.width;
192   *heightP = xgwa.height;
193
194   grab_screen_image(xgwa.screen, window);
195   p = XCreatePixmap(dpy, window, xgwa.width, xgwa.height, xgwa.depth);
196   gcv.function = GXcopy;
197   gc = XCreateGC (dpy, window, GCFunction, &gcv);
198   XCopyArea (dpy, window, p, gc, 0, 0, xgwa.width, xgwa.height, 0, 0);
199   XFreeGC (dpy, gc);
200
201
202   /* Reset this... */
203   XSetWindowBackground (dpy, window,
204                         get_pixel_resource ("background", "Background",
205                                             dpy, xgwa.colormap));
206   XClearWindow (dpy, window);
207
208   return p;
209 }
210
211
212 static void
213 init (void)
214 {
215   XWindowAttributes xgwa;
216   Colormap cmap;
217   XGCValues gcv;
218   int width, height;
219   unsigned int real_size;
220   char *bitmap_name;
221   int i;
222
223   XGetWindowAttributes (dpy, window, &xgwa);
224   cmap = xgwa.colormap;
225   depth = xgwa.depth;
226
227   fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
228   bg = get_pixel_resource ("background", "Background", dpy, cmap);
229   delay = get_integer_resource ("delay", "Integer");
230   delay2 = get_integer_resource ("delay2", "Integer");
231   if (delay < 0) delay = 0;
232   if (delay2 < 0) delay2 = 0;
233   bitmap_name = get_string_resource ("bitmap", "Bitmap");
234   if (! bitmap_name || !*bitmap_name)
235     bitmap_name = "(default)";
236
237   if (!strcmp (bitmap_name, "(default)"))
238     {
239       width = logo_width;
240       height = logo_height;
241       bitmap = XCreatePixmapFromBitmapData (dpy, window, (char *) logo_bits,
242                                             width, height, fg, bg, depth);
243     }
244   else if (!strcmp (bitmap_name, "(screen)"))
245     {
246       bitmap = read_screen (dpy, window, &width, &height);
247     }
248   else
249     {
250       read_bitmap (bitmap_name, &width, &height);
251     }
252
253   real_size = (width > height) ? width : height;
254
255   size = real_size;
256   /* semi-sleazy way of doing (setq size (expt 2 (ceiling (log size 2)))). */
257   for (i = 31; i > 0; i--)
258     if (size & (1<<i)) break;
259   if (size & (~(1<<i)))
260     size = (size>>i)<<(i+1);
261   self = XCreatePixmap (dpy, window, size, size, depth);
262   temp = XCreatePixmap (dpy, window, size, size, depth);
263   mask = XCreatePixmap (dpy, window, size, size, depth);
264   gcv.foreground = (depth == 1 ? 1 : (~0));
265   gcv.function=GXset;  SET = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
266   gcv.function=GXclear;CLR = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
267   gcv.function=GXcopy; CPY = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
268   gcv.function=GXor;   IOR = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
269   gcv.function=GXand;  AND = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
270   gcv.function=GXxor;  XOR = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
271
272   gcv.foreground = gcv.background = bg;
273   gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
274   /* Clear self to the background color (not to 0, which CLR does.) */
275   XFillRectangle (dpy, self, gc, 0, 0, size, size);
276   XSetForeground (dpy, gc, fg);
277
278   XCopyArea (dpy, bitmap, self, CPY, 0, 0, width, height,
279              (size - width)>>1, (size - height)>>1);
280 }
281
282 static void
283 display (Pixmap pixmap)
284 {
285   XWindowAttributes xgwa;
286   static int last_w = 0, last_h = 0;
287   XGetWindowAttributes (dpy, window, &xgwa);
288   if (xgwa.width != last_w || xgwa.height != last_h)
289     {
290       XClearWindow (dpy, window);
291       last_w = xgwa.width;
292       last_h = xgwa.height;
293     }
294 #ifdef HAVE_XPM
295   if (depth != 1)
296     XCopyArea (dpy, pixmap, window, gc, 0, 0, size, size,
297                (xgwa.width-size)>>1, (xgwa.height-size)>>1);
298   else
299 #endif
300     XCopyPlane (dpy, pixmap, window, gc, 0, 0, size, size,
301                 (xgwa.width-size)>>1, (xgwa.height-size)>>1, 1);
302 /*
303   XDrawRectangle (dpy, window, gc,
304                   ((xgwa.width-size)>>1)-1, ((xgwa.height-size)>>1)-1,
305                   size+2, size+2);
306 */
307   XSync (dpy, True);
308 }
309
310 \f
311 char *progclass = "BlitSpin";
312
313 char *defaults [] = {
314   "BlitSpin.background: black",         /* to placate SGI */
315   "BlitSpin.foreground: white",
316   "*delay:      500000",
317   "*delay2:     500000",
318   "*bitmap:     (default)",
319   0
320 };
321
322 XrmOptionDescRec options [] = {
323   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
324   { "-delay2",          ".delay2",      XrmoptionSepArg, 0 },
325   { "-bitmap",          ".bitmap",      XrmoptionSepArg, 0 },
326   { "-grab-screen",     ".bitmap",      XrmoptionNoArg, "(screen)" },
327   { 0, 0, 0, 0 }
328 };
329
330 void
331 screenhack (Display *d, Window w)
332 {
333   dpy = d;
334   window = w;
335   init ();
336   while (1)
337     {
338       rotate ();
339       if (delay2) usleep (delay2);
340     }
341 }