1 /* erase.c: Erase the screen in various more or less interesting ways.
2 * Copyright (c) 1997-2008 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 * Portions (c) 1997 by Johannes Keukelaar <johannes@nada.kth.se>:
13 * Permission to use in any way granted. Provided "as is" without expressed
14 * or implied warranty. NO WARRANTY, NO EXPRESSION OF SUITABILITY FOR ANY
15 * PURPOSE. (I.e.: Use in any way, but at your own risk!)
21 #include "resources.h"
23 #include <sys/time.h> /* for gettimeofday() */
25 extern char *progname;
28 #define countof(x) (sizeof(x)/sizeof(*(x)))
30 typedef void (*Eraser) (eraser_state *);
39 double start_time, stop_time;
40 double ratio, prev_ratio;
42 /* data for random_lines, venetian, random_squares */
47 /* data for triple_wipe, quad_wipe */
50 /* data for circle_wipe, three_circle_wipe */
53 /* data for random_squares */
57 unsigned short *fizzle_rnd;
66 # ifdef GETTIMEOFDAY_TWO_ARGS
68 gettimeofday(&now, &tzp);
73 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
78 random_lines (eraser_state *st)
82 if (! st->lines) /* first time */
84 st->horiz_p = (random() & 1);
85 st->nlines = (st->horiz_p ? st->height : st->width);
86 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
88 for (i = 0; i < st->nlines; i++) /* every line */
91 for (i = 0; i < st->nlines; i++) /* shuffle */
95 r = random() % st->nlines;
96 st->lines[i] = st->lines[r];
101 for (i = st->nlines * st->prev_ratio;
102 i < st->nlines * st->ratio;
106 XDrawLine (st->dpy, st->window, st->bg_gc,
107 0, st->lines[i], st->width, st->lines[i]);
109 XDrawLine (st->dpy, st->window, st->bg_gc,
110 st->lines[i], 0, st->lines[i], st->height);
113 if (st->ratio >= 1.0)
122 venetian (eraser_state *st)
125 if (st->ratio == 0.0)
128 st->horiz_p = (random() & 1);
129 st->flip_p = (random() & 1);
130 st->nlines = (st->horiz_p ? st->height : st->width);
131 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
133 for (i = 0; i < st->nlines * 2; i++)
135 int line = ((i / 16) * 16) - ((i % 16) * 15);
136 if (line >= 0 && line < st->nlines)
137 st->lines[j++] = (st->flip_p ? st->nlines - line : line);
142 for (i = st->nlines * st->prev_ratio;
143 i < st->nlines * st->ratio;
147 XDrawLine (st->dpy, st->window, st->bg_gc,
148 0, st->lines[i], st->width, st->lines[i]);
150 XDrawLine (st->dpy, st->window, st->bg_gc,
151 st->lines[i], 0, st->lines[i], st->height);
154 if (st->ratio >= 1.0)
163 triple_wipe (eraser_state *st)
166 if (st->ratio == 0.0)
168 st->flip_x = random() & 1;
169 st->flip_y = random() & 1;
170 st->nlines = st->width + (st->height / 2);
171 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
173 for (i = 0; i < st->width / 2; i++)
174 st->lines[i] = i * 2 + st->height;
175 for (i = 0; i < st->height / 2; i++)
176 st->lines[i + st->width / 2] = i*2;
177 for (i = 0; i < st->width / 2; i++)
178 st->lines[i + st->width / 2 + st->height / 2] =
179 st->width - i * 2 - (st->width % 2 ? 0 : 1) + st->height;
182 for (i = st->nlines * st->prev_ratio;
183 i < st->nlines * st->ratio;
188 if (st->lines[i] < st->height)
189 x = 0, y = st->lines[i], x2 = st->width, y2 = y;
191 x = st->lines[i]-st->height, y = 0, x2 = x, y2 = st->height;
194 x = st->width - x, x2 = st->width - x2;
196 y = st->height - y, y2 = st->height - y2;
198 XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2);
201 if (st->ratio >= 1.0)
210 quad_wipe (eraser_state *st)
213 if (st->ratio == 0.0)
215 st->flip_x = random() & 1;
216 st->flip_y = random() & 1;
217 st->nlines = st->width + st->height;
218 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
220 for (i = 0; i < st->nlines/4; i++)
222 st->lines[i*4] = i*2;
223 st->lines[i*4+1] = st->height - i*2 - (st->height % 2 ? 0 : 1);
224 st->lines[i*4+2] = st->height + i*2;
225 st->lines[i*4+3] = st->height + st->width - i*2
226 - (st->width % 2 ? 0 : 1);
230 for (i = st->nlines * st->prev_ratio;
231 i < st->nlines * st->ratio;
235 if (st->lines[i] < st->height)
236 x = 0, y = st->lines[i], x2 = st->width, y2 = y;
238 x = st->lines[i] - st->height, y = 0, x2 = x, y2 = st->height;
241 x = st->width-x, x2 = st->width-x2;
243 y = st->height-y, y2 = st->height-y2;
245 XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2);
248 if (st->ratio >= 1.0)
257 circle_wipe (eraser_state *st)
259 int rad = (st->width > st->height ? st->width : st->height);
263 if (st->ratio == 0.0)
265 st->flip_p = random() & 1;
266 st->start = random() % max;
269 th = max * st->ratio;
270 oth = max * st->prev_ratio;
276 XFillArc (st->dpy, st->window, st->bg_gc,
277 (st->width / 2) - rad,
278 (st->height / 2) - rad,
280 (st->start + oth) % max,
286 three_circle_wipe (eraser_state *st)
288 int rad = (st->width > st->height ? st->width : st->height);
293 if (st->ratio == 0.0)
294 st->start = random() % max;
296 th = max/6 * st->ratio;
297 oth = max/6 * st->prev_ratio;
299 for (i = 0; i < 3; i++)
301 int off = i * max / 3;
302 XFillArc (st->dpy, st->window, st->bg_gc,
303 (st->width / 2) - rad,
304 (st->height / 2) - rad,
306 (st->start + off + oth) % max,
308 XFillArc (st->dpy, st->window, st->bg_gc,
309 (st->width / 2) - rad,
310 (st->height / 2) - rad,
312 (st->start + off - oth) % max,
319 squaretate (eraser_state *st)
321 int max = ((st->width > st->height ? st->width : st->height) * 2);
323 int i = max * st->ratio;
325 if (st->ratio == 0.0)
326 st->flip_p = random() & 1;
330 points[0].x = st->width - points[0].x; \
331 points[1].x = st->width - points[1].x; \
332 points[2].x = st->width - points[2].x; \
334 XFillPolygon (st->dpy, st->window, st->bg_gc, \
335 points, 3, Convex, CoordModeOrigin)
339 points[1].x = st->width;
342 points[2].y = points[0].y + ((i * st->height) / max);
348 points[1].y = st->height;
349 points[2].x = ((i * st->width) / max);
350 points[2].y = st->height;
353 points[0].x = st->width;
354 points[0].y = st->height;
356 points[1].y = st->height;
357 points[2].x = st->width;
358 points[2].y = st->height - ((i * st->height) / max);
361 points[0].x = st->width;
362 points[0].y = st->height;
363 points[1].x = st->width;
365 points[2].x = st->width - ((i * st->width) / max);
373 fizzle (eraser_state *st)
375 const double overshoot = 1.0625;
377 unsigned int x, y, i;
378 const unsigned int size = 256;
381 unsigned int npoints =
382 (unsigned int)(size * size * st->ratio * overshoot) -
383 (unsigned int)(size * size * st->prev_ratio * overshoot);
385 if (st->ratio >= 1.0)
387 free (st->fizzle_rnd);
388 st->fizzle_rnd = NULL;
392 if (! st->fizzle_rnd)
394 unsigned int chunks =
395 ((st->width + size - 1) / size) * ((st->height + size - 1) / size);
399 (unsigned short *) malloc (sizeof(unsigned short) * chunks);
401 if (! st->fizzle_rnd)
404 for (i = 0; i != chunks; i++)
405 st->fizzle_rnd[i] = NRAND(0x10000) | 1; /* Seed can't be 0. */
408 points = (XPoint *) malloc ((npoints + 1) * sizeof(*points));
409 if (! points) return;
411 rnd = st->fizzle_rnd;
413 for (y = 0; y < st->height; y += 256)
415 for (x = 0; x < st->width; x += 256)
417 unsigned int need0 = 0;
418 unsigned short r = *rnd;
419 for (i = 0; i != npoints; i++)
421 points[i].x = r % size + x;
422 points[i].y = (r >> 8) % size + y;
424 /* Xorshift. This has a period of 2^16, which exactly matches
425 the number of pixels in each 256x256 chunk.
427 Other shift constants are possible, but it's hard to say
428 which constants are best: a 2^16 period PRNG will never score
431 r = (r ^ (r << 3)) & 0xffff;
433 r = (r ^ (r << 11)) & 0xffff;
434 need0 |= (r == 0x8080); /* Can be anything, really. */
439 points[npoints].x = x;
440 points[npoints].y = y;
443 XDrawPoints (st->dpy, st->window, st->bg_gc,
444 points, npoints + need0, CoordModeOrigin);
454 spiral (eraser_state *st)
456 int max_radius = (st->width > st->height ? st->width : st->height) * 0.7;
458 float max_th = M_PI * 2 * loops;
460 int steps = 360 * loops / 4;
463 if (st->ratio == 0.0)
465 st->flip_p = random() & 1;
466 st->start = random() % 360;
469 off = st->start * M_PI / 180;
471 for (i = steps * st->prev_ratio;
472 i < steps * st->ratio;
475 float th1 = i * max_th / steps;
476 float th2 = (i+1) * max_th / steps;
477 int r1 = i * max_radius / steps;
478 int r2 = (i+1) * max_radius / steps;
487 points[0].x = st->width / 2;
488 points[0].y = st->height / 2;
489 points[1].x = points[0].x + r1 * cos (off + th1);
490 points[1].y = points[0].y + r1 * sin (off + th1);
491 points[2].x = points[0].x + r2 * cos (off + th2);
492 points[2].y = points[0].y + r2 * sin (off + th2);
493 /* XFillRectangle(st->dpy, st->window, st->fg_gc,0,0,st->width, st->height);*/
494 XFillPolygon (st->dpy, st->window, st->bg_gc,
495 points, 3, Convex, CoordModeOrigin);
501 random_squares (eraser_state *st)
505 if (st->ratio == 0.0)
507 st->cols = 10 + random() % 30;
508 size = st->width / st->cols;
509 rows = (size ? (st->height / size) : 0) + 1;
510 st->nlines = st->cols * rows;
511 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
513 for (i = 0; i < st->nlines; i++) /* every square */
516 for (i = 0; i < st->nlines; i++) /* shuffle */
520 r = random() % st->nlines;
521 st->lines[i] = st->lines[r];
526 size = st->width / st->cols;
527 rows = (size ? (st->height / size) : 0) + 1;
529 for (i = st->nlines * st->prev_ratio;
530 i < st->nlines * st->ratio;
533 int x = st->lines[i] % st->cols;
534 int y = st->lines[i] / st->cols;
535 XFillRectangle (st->dpy, st->window, st->bg_gc,
536 st->width * x / st->cols,
537 st->height * y / rows,
541 if (st->ratio >= 1.0)
549 /* I first saw something like this, albeit in reverse, in an early Tetris
550 implementation for the Mac.
551 -- Torbjörn Andersson <torbjorn@dev.eurotime.se>
554 slide_lines (eraser_state *st)
556 int max = st->width * 1.1;
558 int h = st->height / nlines;
565 step = (max * st->ratio) - (max * st->prev_ratio);
569 for (y = 0; y < st->height; y += h)
571 if (st->width <= step)
575 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
576 0, y, st->width-step, h, step, y);
577 XFillRectangle (st->dpy, st->window, st->bg_gc,
582 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
583 step, y, st->width-step, h, 0, y);
584 XFillRectangle (st->dpy, st->window, st->bg_gc,
585 st->width-step, y, step, h);
593 /* from Frederick Roeber <roeber@xigo.com> */
595 losira (eraser_state *st)
598 double mode2 = mode1 + 0.30;
602 if (st->ratio < mode1) /* squeeze from the sides */
604 double ratio = st->ratio / mode1;
605 double prev_ratio = st->prev_ratio / mode1;
606 int max = st->width / 2;
607 int step = (max * ratio) - (max * prev_ratio);
613 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
614 0, 0, max - step, st->height, step, 0);
615 XFillRectangle (st->dpy, st->window, st->bg_gc,
616 0, 0, max * ratio, st->height);
618 /* pull from right */
619 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
620 max+step, 0, max - step, st->height, max, 0);
621 XFillRectangle (st->dpy, st->window, st->bg_gc,
622 max + max*(1-ratio), 0, max, st->height);
624 /* expand white from center */
625 XFillRectangle (st->dpy, st->window, st->fg_gc,
626 max - (radius * ratio), 0,
627 radius * ratio * 2, st->height);
629 else if (st->ratio < mode2) /* squeeze from the top/bottom */
631 double ratio = (st->ratio - mode1) / (mode2 - mode1);
632 int max = st->height / 2;
635 XFillRectangle (st->dpy, st->window, st->fg_gc,
636 st->width/2 - radius,
638 radius*2, st->height * (1 - ratio));
640 /* fill left and right */
641 XFillRectangle (st->dpy, st->window, st->bg_gc,
642 0, 0, st->width/2 - radius, st->height);
643 XFillRectangle (st->dpy, st->window, st->bg_gc,
644 st->width/2 + radius, 0, st->width/2, st->height);
646 /* fill top and bottom */
647 XFillRectangle (st->dpy, st->window, st->bg_gc,
648 0, 0, st->width, max * ratio);
649 XFillRectangle (st->dpy, st->window, st->bg_gc,
650 0, st->height - (max * ratio),
654 XFillArc (st->dpy, st->window, st->fg_gc,
655 st->width/2 - radius,
656 max * ratio - radius,
661 XFillArc (st->dpy, st->window, st->fg_gc,
662 st->width/2 - radius,
663 st->height - (max * ratio + radius),
667 else if (st->ratio < mode3) /* starburst */
669 double ratio = (st->ratio - mode2) / (mode3 - mode2);
670 double r2 = ratio * radius * 4;
673 int angle = 360*64/countof(arc);
675 for (i = 0; i < countof(arc); i++)
678 arc[i].angle1 = angle * i;
679 arc[i].angle2 = angle;
680 arc[i].width = radius*2 * (1 + ratio);
681 arc[i].height = radius*2 * (1 + ratio);
682 arc[i].x = st->width / 2 - radius;
683 arc[i].y = st->height / 2 - radius;
685 th = ((arc[i].angle1 + (arc[i].angle2 / 2)) / 64.0 / 180 * M_PI);
687 arc[i].x += r2 * cos (th);
688 arc[i].y -= r2 * sin (th);
691 XFillRectangle (st->dpy, st->window, st->bg_gc,
692 0, 0, st->width, st->height);
693 XFillArcs (st->dpy, st->window, st->fg_gc, arc, countof(arc));
698 static Eraser erasers[] = {
714 static eraser_state *
715 eraser_init (Display *dpy, Window window)
717 eraser_state *st = (eraser_state *) calloc (1, sizeof(*st));
718 XWindowAttributes xgwa;
720 unsigned long fg, bg;
728 XGetWindowAttributes (dpy, window, &xgwa);
729 st->width = xgwa.width;
730 st->height = xgwa.height;
732 bg = get_pixel_resource (dpy, xgwa.colormap, "background", "Background");
733 fg = get_pixel_resource (dpy, xgwa.colormap, "foreground", "Foreground");
737 st->fg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
740 st->bg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
743 /* Pretty much all of these leave turds if AA is on. */
744 jwxyz_XSetAntiAliasing (st->dpy, st->fg_gc, False);
745 jwxyz_XSetAntiAliasing (st->dpy, st->bg_gc, False);
748 s = get_string_resource (dpy, "eraseMode", "Integer");
752 which = get_integer_resource(dpy, "eraseMode", "Integer");
755 if (which < 0 || which >= countof(erasers))
756 which = random() % countof(erasers);
757 st->fn = erasers[which];
759 duration = get_float_resource (dpy, "eraseSeconds", "Float");
760 if (duration < 0.1 || duration > 10)
763 st->start_time = double_time();
764 st->stop_time = st->start_time + duration;
766 XSync (st->dpy, False);
773 eraser_free (eraser_state *st)
775 XClearWindow (st->dpy, st->window); /* Final draw is black-on-black. */
777 st->fn (st); /* Free any memory. May also draw, but that doesn't matter. */
778 XFreeGC (st->dpy, st->fg_gc);
779 XFreeGC (st->dpy, st->bg_gc);
784 erase_window (Display *dpy, Window window, eraser_state *st)
786 double now, duration;
787 Bool first_p = False;
791 st = eraser_init (dpy, window);
794 now = (first_p ? st->start_time : double_time());
795 duration = st->stop_time - st->start_time;
797 st->prev_ratio = st->ratio;
798 st->ratio = (now - st->start_time) / duration;
803 XSync (st->dpy, False);