http://www.ibiblio.org/pub/historic-linux/ftp-archives/sunsite.unc.edu/Sep-29-1996...
[xscreensaver] / hacks / blitspin.c
1 /* xscreensaver, Copyright (c) 1992 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 #include <X11/Xmu/Drawing.h>
38
39 #include "default.xbm"
40
41 static Display *dpy;
42 static Window window;
43 static unsigned int size;
44 static Pixmap self, temp, mask;
45 static GC SET, CLR, CPY, IOR, AND, XOR;
46 static GC gc;
47 static int delay, delay2;
48 static Pixmap bitmap;
49 static int depth;
50 static unsigned int fg, bg;
51
52 static void rotate(), init (), display ();
53
54 #define copy_all_to(from, xoff, yoff, to, gc)           \
55   XCopyArea (dpy, (from), (to), (gc), 0, 0,             \
56              size-(xoff), size-(yoff), (xoff), (yoff))
57
58 #define copy_all_from(to, xoff, yoff, from, gc)         \
59   XCopyArea (dpy, (from), (to), (gc), (xoff), (yoff),   \
60              size-(xoff), size-(yoff), 0, 0)
61
62 static void
63 rotate ()
64 {
65   int qwad; /* fuckin' C, man... who needs namespaces? */
66   XFillRectangle (dpy, mask, CLR, 0, 0, size, size);
67   XFillRectangle (dpy, mask, SET, 0, 0, size>>1, size>>1);
68   for (qwad = size>>1; qwad > 0; qwad>>=1)
69     {
70       if (delay) usleep (delay);
71       copy_all_to   (mask,       0,       0, temp, CPY);  /* 1 */
72       copy_all_to   (mask,       0,    qwad, temp, IOR);  /* 2 */
73       copy_all_to   (self,       0,       0, temp, AND);  /* 3 */
74       copy_all_to   (temp,       0,       0, self, XOR);  /* 4 */
75       copy_all_from (temp,    qwad,       0, self, XOR);  /* 5 */
76       copy_all_from (self,    qwad,       0, self, IOR);  /* 6 */
77       copy_all_to   (temp,    qwad,       0, self, XOR);  /* 7 */
78       copy_all_to   (self,       0,       0, temp, CPY);  /* 8 */
79       copy_all_from (temp,    qwad,    qwad, self, XOR);  /* 9 */
80       copy_all_to   (mask,       0,       0, temp, AND);  /* A */
81       copy_all_to   (temp,       0,       0, self, XOR);  /* B */
82       copy_all_to   (temp,    qwad,    qwad, self, XOR);  /* C */
83       copy_all_from (mask, qwad>>1, qwad>>1, mask, AND);  /* D */
84       copy_all_to   (mask,    qwad,       0, mask, IOR);  /* E */
85       copy_all_to   (mask,       0,    qwad, mask, IOR);  /* F */
86       display (self);
87     }
88 }
89
90 static void
91 read_bitmap (bitmap_name, widthP, heightP)
92      char *bitmap_name;
93      int *widthP, *heightP;
94 {
95 #ifdef HAVE_XPM
96   XpmAttributes xpmattrs;
97   int result;
98   xpmattrs.valuemask = 0;
99   bitmap = 0;
100   result = XpmReadFileToPixmap (dpy, window, bitmap_name, &bitmap, 0,
101                                 &xpmattrs);
102   switch (result)
103     {
104     case XpmColorError:
105       fprintf (stderr, "%s: warning: xpm color substitution performed\n",
106                progname);
107       /* fall through */
108     case XpmSuccess:
109       *widthP = xpmattrs.width;
110       *heightP = xpmattrs.height;
111       break;
112     case XpmFileInvalid:
113     case XpmOpenFailed:
114       bitmap = 0;
115       break;
116     case XpmColorFailed:
117       fprintf (stderr, "%s: xpm: color allocation failed\n", progname);
118       exit (-1);
119     case XpmNoMemory:
120       fprintf (stderr, "%s: xpm: out of memory\n", progname);
121       exit (-1);
122     default:
123       fprintf (stderr, "%s: xpm: unknown error code %d\n", progname, result);
124       exit (-1);
125     }
126   if (! bitmap)
127 #endif
128     {
129       int xh, yh;
130       Pixmap b2;
131       bitmap = XmuLocateBitmapFile (DefaultScreenOfDisplay (dpy), bitmap_name,
132                                     0, 0, widthP, heightP, &xh, &yh);
133       if (! bitmap)
134         {
135           fprintf (stderr, "%s: couldn't find bitmap %s\n", progname,
136                    bitmap_name);
137           exit (1);
138         }
139       b2 = XmuCreatePixmapFromBitmap (dpy, window, bitmap, *widthP, *heightP,
140                                       depth, fg, bg);
141       XFreePixmap (dpy, bitmap);
142       bitmap = b2;
143     }
144 }
145
146 static void
147 init ()
148 {
149   XWindowAttributes xgwa;
150   Colormap cmap;
151   XGCValues gcv;
152   int width, height;
153   unsigned int real_size;
154   char *bitmap_name;
155   int i;
156
157   XGetWindowAttributes (dpy, window, &xgwa);
158   cmap = xgwa.colormap;
159   depth = xgwa.depth;
160
161   fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
162   bg = get_pixel_resource ("background", "Background", dpy, cmap);
163   delay = get_integer_resource ("delay", "Integer");
164   delay2 = get_integer_resource ("delay2", "Integer");
165   if (delay < 0) delay = 0;
166   if (delay2 < 0) delay2 = 0;
167   bitmap_name = get_string_resource ("bitmap", "Bitmap");
168   if (! bitmap_name || !*bitmap_name)
169     bitmap_name = "(default)";
170
171   if (!strcmp (bitmap_name, "(default)"))
172     {
173       width = logo_width;
174       height = logo_height;
175       bitmap = XCreatePixmapFromBitmapData (dpy, window, (char *) logo_bits,
176                                             width, height, fg, bg, depth);
177     }
178   else
179     {
180       read_bitmap (bitmap_name, &width, &height);
181     }
182
183   real_size = (width > height) ? width : height;
184
185   size = real_size;
186   /* semi-sleazy way of doing (setq size (expt 2 (ceiling (log size 2)))). */
187   for (i = 31; i > 0; i--)
188     if (size & (1<<i)) break;
189   if (size & (~(1<<i)))
190     size = (size>>i)<<(i+1);
191   self = XCreatePixmap (dpy, window, size, size, depth);
192   temp = XCreatePixmap (dpy, window, size, size, depth);
193   mask = XCreatePixmap (dpy, window, size, size, depth);
194   gcv.foreground = (depth == 1 ? 1 : (~0));
195   gcv.function=GXset;  SET = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
196   gcv.function=GXclear;CLR = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
197   gcv.function=GXcopy; CPY = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
198   gcv.function=GXor;   IOR = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
199   gcv.function=GXand;  AND = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
200   gcv.function=GXxor;  XOR = XCreateGC(dpy,self,GCFunction|GCForeground,&gcv);
201
202   gcv.foreground = gcv.background = bg;
203   gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
204   /* Clear self to the background color (not to 0, which CLR does.) */
205   XFillRectangle (dpy, self, gc, 0, 0, size, size);
206   XSetForeground (dpy, gc, fg);
207
208   XCopyArea (dpy, bitmap, self, CPY, 0, 0, width, height,
209              (size - width)>>1, (size - height)>>1);
210 }
211
212 static void
213 display (pixmap)
214      Pixmap pixmap;
215 {
216   XWindowAttributes xgwa;
217   static int last_w = 0, last_h = 0;
218   XGetWindowAttributes (dpy, window, &xgwa);
219   if (xgwa.width != last_w || xgwa.height != last_h)
220     {
221       XClearWindow (dpy, window);
222       last_w = xgwa.width;
223       last_h = xgwa.height;
224     }
225 #ifdef HAVE_XPM
226   if (depth != 1)
227     XCopyArea (dpy, pixmap, window, gc, 0, 0, size, size,
228                (xgwa.width-size)>>1, (xgwa.height-size)>>1);
229   else
230 #endif
231     XCopyPlane (dpy, pixmap, window, gc, 0, 0, size, size,
232                 (xgwa.width-size)>>1, (xgwa.height-size)>>1, 1);
233 /*
234   XDrawRectangle (dpy, window, gc,
235                   ((xgwa.width-size)>>1)-1, ((xgwa.height-size)>>1)-1,
236                   size+2, size+2);
237 */
238   XSync (dpy, True);
239 }
240
241 \f
242 char *progclass = "BlitSpin";
243
244 char *defaults [] = {
245   "BlitSpin.background: black",         /* to placate SGI */
246   "BlitSpin.foreground: white",
247   "*delay:      500000",
248   "*delay2:     500000",
249   "*bitmap:     (default)",
250   0
251 };
252
253 XrmOptionDescRec options [] = {
254   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
255   { "-delay2",          ".delay2",      XrmoptionSepArg, 0 },
256   { "-bitmap",          ".bitmap",      XrmoptionSepArg, 0 }
257 };
258 int options_size = (sizeof (options) / sizeof (options[0]));
259
260 void
261 screenhack (d, w)
262      Display *d;
263      Window w;
264 {
265   dpy = d;
266   window = w;
267   init ();
268   while (1)
269     {
270       rotate ();
271       if (delay2) usleep (delay2);
272     }
273 }