1 /* erase.c: Erase the screen in various more or less interesting ways.
2 * Copyright (c) 1997-2001, 2006 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 */
63 # ifdef GETTIMEOFDAY_TWO_ARGS
65 gettimeofday(&now, &tzp);
70 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
75 random_lines (eraser_state *st)
79 if (! st->lines) /* first time */
81 st->horiz_p = (random() & 1);
82 st->nlines = (st->horiz_p ? st->height : st->width);
83 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
85 for (i = 0; i < st->nlines; i++) /* every line */
88 for (i = 0; i < st->nlines; i++) /* shuffle */
92 r = random() % st->nlines;
93 st->lines[i] = st->lines[r];
98 for (i = st->nlines * st->prev_ratio;
99 i < st->nlines * st->ratio;
103 XDrawLine (st->dpy, st->window, st->bg_gc,
104 0, st->lines[i], st->width, st->lines[i]);
106 XDrawLine (st->dpy, st->window, st->bg_gc,
107 st->lines[i], 0, st->lines[i], st->height);
110 if (st->ratio >= 1.0)
119 venetian (eraser_state *st)
122 if (st->ratio == 0.0)
125 st->horiz_p = (random() & 1);
126 st->flip_p = (random() & 1);
127 st->nlines = (st->horiz_p ? st->height : st->width);
128 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
130 for (i = 0; i < st->nlines * 2; i++)
132 int line = ((i / 16) * 16) - ((i % 16) * 15);
133 if (line >= 0 && line < st->nlines)
134 st->lines[j++] = (st->flip_p ? st->nlines - line : line);
139 for (i = st->nlines * st->prev_ratio;
140 i < st->nlines * st->ratio;
144 XDrawLine (st->dpy, st->window, st->bg_gc,
145 0, st->lines[i], st->width, st->lines[i]);
147 XDrawLine (st->dpy, st->window, st->bg_gc,
148 st->lines[i], 0, st->lines[i], st->height);
151 if (st->ratio >= 1.0)
160 triple_wipe (eraser_state *st)
163 if (st->ratio == 0.0)
165 st->flip_x = random() & 1;
166 st->flip_y = random() & 1;
167 st->nlines = st->width + (st->height / 2);
168 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
170 for (i = 0; i < st->width / 2; i++)
171 st->lines[i] = i * 2 + st->height;
172 for (i = 0; i < st->height / 2; i++)
173 st->lines[i + st->width / 2] = i*2;
174 for (i = 0; i < st->width / 2; i++)
175 st->lines[i + st->width / 2 + st->height / 2] =
176 st->width - i * 2 - (st->width % 2 ? 0 : 1) + st->height;
179 for (i = st->nlines * st->prev_ratio;
180 i < st->nlines * st->ratio;
185 if (st->lines[i] < st->height)
186 x = 0, y = st->lines[i], x2 = st->width, y2 = y;
188 x = st->lines[i]-st->height, y = 0, x2 = x, y2 = st->height;
191 x = st->width - x, x2 = st->width - x2;
193 y = st->height - y, y2 = st->height - y2;
195 XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2);
198 if (st->ratio >= 1.0)
207 quad_wipe (eraser_state *st)
210 if (st->ratio == 0.0)
212 st->flip_x = random() & 1;
213 st->flip_y = random() & 1;
214 st->nlines = st->width + st->height;
215 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
217 for (i = 0; i < st->nlines/4; i++)
219 st->lines[i*4] = i*2;
220 st->lines[i*4+1] = st->height - i*2 - (st->height % 2 ? 0 : 1);
221 st->lines[i*4+2] = st->height + i*2;
222 st->lines[i*4+3] = st->height + st->width - i*2
223 - (st->width % 2 ? 0 : 1);
227 for (i = st->nlines * st->prev_ratio;
228 i < st->nlines * st->ratio;
232 if (st->lines[i] < st->height)
233 x = 0, y = st->lines[i], x2 = st->width, y2 = y;
235 x = st->lines[i] - st->height, y = 0, x2 = x, y2 = st->height;
238 x = st->width-x, x2 = st->width-x2;
240 y = st->height-y, y2 = st->height-y2;
242 XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2);
245 if (st->ratio >= 1.0)
254 circle_wipe (eraser_state *st)
256 int rad = (st->width > st->height ? st->width : st->height);
260 if (st->ratio == 0.0)
262 st->flip_p = random() & 1;
263 st->start = random() % max;
266 th = max * st->ratio;
267 oth = max * st->prev_ratio;
273 XFillArc (st->dpy, st->window, st->bg_gc,
274 (st->width / 2) - rad,
275 (st->height / 2) - rad,
277 (st->start + oth) % max,
283 three_circle_wipe (eraser_state *st)
285 int rad = (st->width > st->height ? st->width : st->height);
290 if (st->ratio == 0.0)
291 st->start = random() % max;
293 th = max/6 * st->ratio;
294 oth = max/6 * st->prev_ratio;
296 for (i = 0; i < 3; i++)
298 int off = i * max / 3;
299 XFillArc (st->dpy, st->window, st->bg_gc,
300 (st->width / 2) - rad,
301 (st->height / 2) - rad,
303 (st->start + off + oth) % max,
305 XFillArc (st->dpy, st->window, st->bg_gc,
306 (st->width / 2) - rad,
307 (st->height / 2) - rad,
309 (st->start + off - oth) % max,
316 squaretate (eraser_state *st)
318 int max = ((st->width > st->height ? st->width : st->height) * 2);
320 int i = max * st->ratio;
322 if (st->ratio == 0.0)
323 st->flip_p = random() & 1;
327 points[0].x = st->width - points[0].x; \
328 points[1].x = st->width - points[1].x; \
329 points[2].x = st->width - points[2].x; \
331 XFillPolygon (st->dpy, st->window, st->bg_gc, \
332 points, 3, Convex, CoordModeOrigin)
336 points[1].x = st->width;
339 points[2].y = points[0].y + ((i * st->height) / max);
345 points[1].y = st->height;
346 points[2].x = ((i * st->width) / max);
347 points[2].y = st->height;
350 points[0].x = st->width;
351 points[0].y = st->height;
353 points[1].y = st->height;
354 points[2].x = st->width;
355 points[2].y = st->height - ((i * st->height) / max);
358 points[0].x = st->width;
359 points[0].y = st->height;
360 points[1].x = st->width;
362 points[2].x = st->width - ((i * st->width) / max);
370 fizzle (eraser_state *st)
374 int npoints = st->width * st->height * 4;
375 npoints *= (st->ratio - st->prev_ratio);
377 points = (XPoint *) malloc (chunk * sizeof(*points));
378 if (! points) return;
382 int remain = (chunk > npoints ? npoints : chunk);
384 for (i = 0; i < remain; i++)
387 points[i].x = r % st->width;
388 points[i].y = (r >> 16) % st->height;
390 XDrawPoints (st->dpy, st->window, st->bg_gc,
391 points, remain, CoordModeOrigin);
399 spiral (eraser_state *st)
401 int max_radius = (st->width > st->height ? st->width : st->height) * 0.7;
403 float max_th = M_PI * 2 * loops;
405 int steps = 360 * loops / 4;
408 if (st->ratio == 0.0)
410 st->flip_p = random() & 1;
411 st->start = random() % 360;
414 off = st->start * M_PI / 180;
416 for (i = steps * st->prev_ratio;
417 i < steps * st->ratio;
420 float th1 = i * max_th / steps;
421 float th2 = (i+1) * max_th / steps;
422 int r1 = i * max_radius / steps;
423 int r2 = (i+1) * max_radius / steps;
432 points[0].x = st->width / 2;
433 points[0].y = st->height / 2;
434 points[1].x = points[0].x + r1 * cos (off + th1);
435 points[1].y = points[0].y + r1 * sin (off + th1);
436 points[2].x = points[0].x + r2 * cos (off + th2);
437 points[2].y = points[0].y + r2 * sin (off + th2);
438 /* XFillRectangle(st->dpy, st->window, st->fg_gc,0,0,st->width, st->height);*/
439 XFillPolygon (st->dpy, st->window, st->bg_gc,
440 points, 3, Convex, CoordModeOrigin);
446 random_squares (eraser_state *st)
450 if (st->ratio == 0.0)
452 st->cols = 10 + random() % 30;
453 size = st->width / st->cols;
454 rows = (st->height / size) + 1;
455 st->nlines = st->cols * rows;
456 st->lines = (int *) calloc (st->nlines, sizeof(*st->lines));
458 for (i = 0; i < st->nlines; i++) /* every square */
461 for (i = 0; i < st->nlines; i++) /* shuffle */
465 r = random() % st->nlines;
466 st->lines[i] = st->lines[r];
471 size = st->width / st->cols;
472 rows = (st->height / size) + 1;
474 for (i = st->nlines * st->prev_ratio;
475 i < st->nlines * st->ratio;
478 int x = st->lines[i] % st->cols;
479 int y = st->lines[i] / st->cols;
480 XFillRectangle (st->dpy, st->window, st->bg_gc,
481 st->width * x / st->cols,
482 st->height * y / rows,
486 if (st->ratio >= 1.0)
494 /* I first saw something like this, albeit in reverse, in an early Tetris
495 implementation for the Mac.
496 -- Torbjörn Andersson <torbjorn@dev.eurotime.se>
499 slide_lines (eraser_state *st)
501 int max = st->width * 1.1;
503 int h = st->height / nlines;
510 step = (max * st->ratio) - (max * st->prev_ratio);
514 for (y = 0; y < st->height; y += h)
518 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
519 0, y, st->width-step, h, step, y);
520 XFillRectangle (st->dpy, st->window, st->bg_gc,
525 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
526 step, y, st->width-step, h, 0, y);
527 XFillRectangle (st->dpy, st->window, st->bg_gc,
528 st->width-step, y, step, h);
536 /* from Frederick Roeber <roeber@xigo.com> */
538 losira (eraser_state *st)
541 double mode2 = mode1 + 0.30;
545 if (st->ratio < mode1) /* squeeze from the sides */
547 double ratio = st->ratio / mode1;
548 double prev_ratio = st->prev_ratio / mode1;
549 int max = st->width / 2;
550 int step = (max * ratio) - (max * prev_ratio);
556 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
557 0, 0, max - step, st->height, step, 0);
558 XFillRectangle (st->dpy, st->window, st->bg_gc,
559 0, 0, max * ratio, st->height);
561 /* pull from right */
562 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
563 max+step, 0, max - step, st->height, max, 0);
564 XFillRectangle (st->dpy, st->window, st->bg_gc,
565 max + max*(1-ratio), 0, max, st->height);
567 /* expand white from center */
568 XFillRectangle (st->dpy, st->window, st->fg_gc,
569 max - (radius * ratio), 0,
570 radius * ratio * 2, st->height);
572 else if (st->ratio < mode2) /* squeeze from the top/bottom */
574 double ratio = (st->ratio - mode1) / (mode2 - mode1);
575 double prev_ratio = (st->prev_ratio - mode1) / (mode2 - mode1);
576 int max = st->height / 2;
577 int step = (max * ratio) - (max * prev_ratio);
583 XFillRectangle (st->dpy, st->window, st->fg_gc,
584 st->width/2 - radius,
586 radius*2, st->height * (1 - ratio));
588 /* fill left and right */
589 XFillRectangle (st->dpy, st->window, st->bg_gc,
590 0, 0, st->width/2 - radius, st->height);
591 XFillRectangle (st->dpy, st->window, st->bg_gc,
592 st->width/2 + radius, 0, st->width/2, st->height);
594 /* fill top and bottom */
595 XFillRectangle (st->dpy, st->window, st->bg_gc,
596 0, 0, st->width, max * ratio);
597 XFillRectangle (st->dpy, st->window, st->bg_gc,
598 0, st->height - (max * ratio),
602 XFillArc (st->dpy, st->window, st->fg_gc,
603 st->width/2 - radius,
604 max * ratio - radius,
609 XFillArc (st->dpy, st->window, st->fg_gc,
610 st->width/2 - radius,
611 st->height - (max * ratio + radius),
617 double ratio = (st->ratio - mode2) / (mode3 - mode2);
618 double r2 = ratio * radius * 4;
621 int angle = 360*64/countof(arc);
623 for (i = 0; i < countof(arc); i++)
626 arc[i].angle1 = angle * i;
627 arc[i].angle2 = angle;
628 arc[i].width = radius*2 * (1 + ratio);
629 arc[i].height = radius*2 * (1 + ratio);
630 arc[i].x = st->width / 2 - radius;
631 arc[i].y = st->height / 2 - radius;
633 th = ((arc[i].angle1 + (arc[i].angle2 / 2)) / 64.0 / 180 * M_PI);
635 arc[i].x += r2 * cos (th);
636 arc[i].y -= r2 * sin (th);
639 XFillRectangle (st->dpy, st->window, st->bg_gc,
640 0, 0, st->width, st->height);
641 XFillArcs (st->dpy, st->window, st->fg_gc, arc, countof(arc));
646 static Eraser erasers[] = {
662 static eraser_state *
663 eraser_init (Display *dpy, Window window)
665 eraser_state *st = (eraser_state *) calloc (1, sizeof(*st));
666 XWindowAttributes xgwa;
668 unsigned long fg, bg;
676 XGetWindowAttributes (dpy, window, &xgwa);
677 st->width = xgwa.width;
678 st->height = xgwa.height;
680 bg = get_pixel_resource (dpy, xgwa.colormap, "background", "Background");
681 fg = get_pixel_resource (dpy, xgwa.colormap, "foreground", "Foreground");
685 st->fg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
688 st->bg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
691 /* Pretty much all of these leave turds if AA is on. */
692 jwxyz_XSetAntiAliasing (st->dpy, st->fg_gc, False);
693 jwxyz_XSetAntiAliasing (st->dpy, st->bg_gc, False);
696 s = get_string_resource (dpy, "eraseMode", "Integer");
700 which = get_integer_resource(dpy, "eraseMode", "Integer");
702 if (which < 0 || which >= countof(erasers))
703 which = random() % countof(erasers);
704 st->fn = erasers[which];
706 duration = get_float_resource (dpy, "eraseSeconds", "Float");
707 if (duration < 0.1 || duration > 10)
710 st->start_time = double_time();
711 st->stop_time = st->start_time + duration;
713 XSync (st->dpy, False);
720 eraser_draw (eraser_state *st, Bool first_p)
722 double now = (first_p ? st->start_time : double_time());
723 double duration = st->stop_time - st->start_time;
725 st->prev_ratio = st->ratio;
726 st->ratio = (now - st->start_time) / duration;
732 XSync (st->dpy, False);
734 return (st->ratio < 1.0);
738 eraser_free (eraser_state *st)
740 XClearWindow (st->dpy, st->window);
741 XFreeGC (st->dpy, st->fg_gc);
742 XFreeGC (st->dpy, st->bg_gc);
747 erase_window (Display *dpy, Window window, eraser_state *st)
749 Bool first_p = False;
753 st = eraser_init (dpy, window);
755 if (! eraser_draw (st, first_p))