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 */
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 = (size ? (st->height / size) : 0) + 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 = (size ? (st->height / size) : 0) + 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)
516 if (st->width <= step)
520 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
521 0, y, st->width-step, h, step, y);
522 XFillRectangle (st->dpy, st->window, st->bg_gc,
527 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
528 step, y, st->width-step, h, 0, y);
529 XFillRectangle (st->dpy, st->window, st->bg_gc,
530 st->width-step, y, step, h);
538 /* from Frederick Roeber <roeber@xigo.com> */
540 losira (eraser_state *st)
543 double mode2 = mode1 + 0.30;
547 if (st->ratio < mode1) /* squeeze from the sides */
549 double ratio = st->ratio / mode1;
550 double prev_ratio = st->prev_ratio / mode1;
551 int max = st->width / 2;
552 int step = (max * ratio) - (max * prev_ratio);
558 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
559 0, 0, max - step, st->height, step, 0);
560 XFillRectangle (st->dpy, st->window, st->bg_gc,
561 0, 0, max * ratio, st->height);
563 /* pull from right */
564 XCopyArea (st->dpy, st->window, st->window, st->fg_gc,
565 max+step, 0, max - step, st->height, max, 0);
566 XFillRectangle (st->dpy, st->window, st->bg_gc,
567 max + max*(1-ratio), 0, max, st->height);
569 /* expand white from center */
570 XFillRectangle (st->dpy, st->window, st->fg_gc,
571 max - (radius * ratio), 0,
572 radius * ratio * 2, st->height);
574 else if (st->ratio < mode2) /* squeeze from the top/bottom */
576 double ratio = (st->ratio - mode1) / (mode2 - mode1);
577 int max = st->height / 2;
580 XFillRectangle (st->dpy, st->window, st->fg_gc,
581 st->width/2 - radius,
583 radius*2, st->height * (1 - ratio));
585 /* fill left and right */
586 XFillRectangle (st->dpy, st->window, st->bg_gc,
587 0, 0, st->width/2 - radius, st->height);
588 XFillRectangle (st->dpy, st->window, st->bg_gc,
589 st->width/2 + radius, 0, st->width/2, st->height);
591 /* fill top and bottom */
592 XFillRectangle (st->dpy, st->window, st->bg_gc,
593 0, 0, st->width, max * ratio);
594 XFillRectangle (st->dpy, st->window, st->bg_gc,
595 0, st->height - (max * ratio),
599 XFillArc (st->dpy, st->window, st->fg_gc,
600 st->width/2 - radius,
601 max * ratio - radius,
606 XFillArc (st->dpy, st->window, st->fg_gc,
607 st->width/2 - radius,
608 st->height - (max * ratio + radius),
614 double ratio = (st->ratio - mode2) / (mode3 - mode2);
615 double r2 = ratio * radius * 4;
618 int angle = 360*64/countof(arc);
620 for (i = 0; i < countof(arc); i++)
623 arc[i].angle1 = angle * i;
624 arc[i].angle2 = angle;
625 arc[i].width = radius*2 * (1 + ratio);
626 arc[i].height = radius*2 * (1 + ratio);
627 arc[i].x = st->width / 2 - radius;
628 arc[i].y = st->height / 2 - radius;
630 th = ((arc[i].angle1 + (arc[i].angle2 / 2)) / 64.0 / 180 * M_PI);
632 arc[i].x += r2 * cos (th);
633 arc[i].y -= r2 * sin (th);
636 XFillRectangle (st->dpy, st->window, st->bg_gc,
637 0, 0, st->width, st->height);
638 XFillArcs (st->dpy, st->window, st->fg_gc, arc, countof(arc));
643 static Eraser erasers[] = {
659 static eraser_state *
660 eraser_init (Display *dpy, Window window)
662 eraser_state *st = (eraser_state *) calloc (1, sizeof(*st));
663 XWindowAttributes xgwa;
665 unsigned long fg, bg;
673 XGetWindowAttributes (dpy, window, &xgwa);
674 st->width = xgwa.width;
675 st->height = xgwa.height;
677 bg = get_pixel_resource (dpy, xgwa.colormap, "background", "Background");
678 fg = get_pixel_resource (dpy, xgwa.colormap, "foreground", "Foreground");
682 st->fg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
685 st->bg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
688 /* Pretty much all of these leave turds if AA is on. */
689 jwxyz_XSetAntiAliasing (st->dpy, st->fg_gc, False);
690 jwxyz_XSetAntiAliasing (st->dpy, st->bg_gc, False);
693 s = get_string_resource (dpy, "eraseMode", "Integer");
697 which = get_integer_resource(dpy, "eraseMode", "Integer");
699 if (which < 0 || which >= countof(erasers))
700 which = random() % countof(erasers);
701 st->fn = erasers[which];
703 duration = get_float_resource (dpy, "eraseSeconds", "Float");
704 if (duration < 0.1 || duration > 10)
707 st->start_time = double_time();
708 st->stop_time = st->start_time + duration;
710 XSync (st->dpy, False);
717 eraser_draw (eraser_state *st, Bool first_p)
719 double now = (first_p ? st->start_time : double_time());
720 double duration = st->stop_time - st->start_time;
722 st->prev_ratio = st->ratio;
723 st->ratio = (now - st->start_time) / duration;
729 XSync (st->dpy, False);
731 return (st->ratio < 1.0);
735 eraser_free (eraser_state *st)
737 XClearWindow (st->dpy, st->window);
738 XFreeGC (st->dpy, st->fg_gc);
739 XFreeGC (st->dpy, st->bg_gc);
744 erase_window (Display *dpy, Window window, eraser_state *st)
746 Bool first_p = False;
750 st = eraser_init (dpy, window);
752 if (! eraser_draw (st, first_p))