1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997, 1998, 2006
2 * 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
13 #include "screenhack.h"
44 unsigned int default_fg_pixel;
45 long maxx, maxy, max_spread, max_size;
47 Bool random_p, solid_p, xor_p, transparent_p, gravity_p;
64 get_geom (struct state *st)
66 XWindowAttributes xgwa;
67 XGetWindowAttributes (st->dpy, st->window, &xgwa);
68 st->maxx = ((long)(xgwa.width+1)<<SCALE) - 1;
69 st->maxy = ((long)(xgwa.height+1)<<SCALE) - 1;
73 init_one_qix (struct state *st, int nlines)
76 struct qix *qix = (struct qix *) calloc (1, sizeof (struct qix));
78 qix->lines = (struct qline *) calloc (qix->nlines, sizeof (struct qline));
79 qix->npoly = st->npoly;
80 for (i = 0; i < qix->nlines; i++)
81 qix->lines[i].p = (struct qpoint *)
82 calloc(qix->npoly, sizeof(struct qpoint));
85 if (!mono_p && !st->transparent_p)
88 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
89 &qix->lines[0].color.red, &qix->lines[0].color.green,
90 &qix->lines[0].color.blue);
91 if (!XAllocColor (st->dpy, st->cmap, &qix->lines[0].color))
93 qix->lines[0].color.pixel = st->default_fg_pixel;
94 XQueryColor (st->dpy, st->cmap, &qix->lines[0].color);
95 if (!XAllocColor (st->dpy, st->cmap, &qix->lines[0].color))
100 if (st->max_size == 0)
102 for (i = 0; i < qix->npoly; i++)
104 qix->lines[0].p[i].x = random () % st->maxx;
105 qix->lines[0].p[i].y = random () % st->maxy;
110 /*assert(qix->npoly == 2);*/
111 qix->lines[0].p[0].x = random () % st->maxx;
112 qix->lines[0].p[0].y = random () % st->maxy;
113 qix->lines[0].p[1].x = qix->lines[0].p[0].x + (random () % (st->max_size/2));
114 qix->lines[0].p[1].y = qix->lines[0].p[0].y + (random () % (st->max_size/2));
115 if (qix->lines[0].p[1].x > st->maxx) qix->lines[0].p[1].x = st->maxx;
116 if (qix->lines[0].p[1].y > st->maxy) qix->lines[0].p[1].y = st->maxy;
119 for (i = 0; i < qix->npoly; i++)
121 qix->lines[0].p[i].dx = (random () % (st->max_spread + 1)) - (st->max_spread /2);
122 qix->lines[0].p[i].dy = (random () % (st->max_spread + 1)) - (st->max_spread /2);
124 qix->lines[0].dead = True;
126 for (i = 1; i < qix->nlines; i++)
128 for(j=0; j<qix->npoly; j++)
129 qix->lines[i].p[j] = qix->lines[0].p[j];
130 qix->lines[i].color = qix->lines[0].color;
131 qix->lines[i].dead = qix->lines[0].dead;
134 if (!mono_p && !st->transparent_p)
136 if (!XAllocColor (st->dpy, st->cmap, &qix->lines[i].color))
146 qix_init (Display *dpy, Window window)
148 struct state *st = (struct state *) calloc (1, sizeof(*st));
151 XWindowAttributes xgwa;
154 XGetWindowAttributes (st->dpy, st->window, &xgwa);
155 st->cmap = xgwa.colormap;
156 st->count = get_integer_resource (st->dpy, "count", "Integer");
157 if (st->count <= 0) st->count = 1;
158 nlines = get_integer_resource (st->dpy, "segments", "Integer");
159 if (nlines <= 0) nlines = 20;
160 st->npoly = get_integer_resource(st->dpy, "poly", "Integer");
161 if (st->npoly <= 2) st->npoly = 2;
162 if (st->npoly > MAXPOLY) st->npoly = MAXPOLY;
164 st->max_spread = get_integer_resource (st->dpy, "spread", "Integer");
165 if (st->max_spread <= 0) st->max_spread = 10;
166 st->max_spread <<= SCALE;
167 st->max_size = get_integer_resource (st->dpy, "size", "Integer");
168 if (st->max_size < 0) st->max_size = 0;
169 st->max_size <<= SCALE;
170 st->random_p = get_boolean_resource (st->dpy, "random", "Boolean");
171 st->solid_p = get_boolean_resource (st->dpy, "solid", "Boolean");
172 st->xor_p = get_boolean_resource (st->dpy, "xor", "Boolean");
173 st->transparent_p = get_boolean_resource (st->dpy, "transparent", "Boolean");
174 st->gravity_p = get_boolean_resource(st->dpy, "gravity", "Boolean");
175 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
176 st->color_shift = get_integer_resource (st->dpy, "colorShift", "Integer");
177 if (st->color_shift < 0 || st->color_shift >= 360) st->color_shift = 5;
178 if (st->delay < 0) st->delay = 0;
180 /* Clear up ambiguities regarding npoly */
184 fprintf(stderr, "%s: Can't have -solid and -poly; using -poly 2\n",
191 fprintf(stderr, "%s: Can't have -poly and -size; using -size 0\n",
196 if (st->count == 1 && st->transparent_p)
197 st->transparent_p = False; /* it's a no-op */
199 if (st->transparent_p && CellsOfScreen (DefaultScreenOfDisplay (st->dpy)) <= 2)
201 fprintf (stderr, "%s: -transparent only works on color displays.\n",
203 st->transparent_p = False;
206 if (st->xor_p && !st->transparent_p)
209 st->gcs[0] = st->gcs[1] = 0;
210 gcv.foreground = st->default_fg_pixel =
211 get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
213 st->additive_p = get_boolean_resource (st->dpy, "additive", "Boolean");
214 st->cmap_p = has_writable_cells (xgwa.screen, xgwa.visual);
217 if (st->transparent_p)
219 unsigned long *plane_masks = 0;
220 unsigned long base_pixel;
221 int nplanes = st->count;
224 allocate_alpha_colors (xgwa.screen, xgwa.visual, st->cmap,
225 &nplanes, st->additive_p, &plane_masks,
231 "%s: couldn't allocate any color planes; turning -transparent off.\n",
233 st->transparent_p = False;
235 goto NON_TRANSPARENT_XOR;
237 goto NON_TRANSPARENT;
239 else if (nplanes != st->count)
242 "%s: only allocated %d color planes (instead of %d).\n",
243 progname, nplanes, st->count);
247 st->gcs[0] = (GC *) malloc (st->count * sizeof (GC));
248 st->gcs[1] = (st->xor_p
250 : (GC *) malloc (st->count * sizeof (GC)));
252 for (i = 0; i < st->count; i++)
254 gcv.plane_mask = plane_masks [i];
257 /* argh, I'm not sure how to make "-subtractive" work in truecolor...
258 if (!cmap_p && !additive_p)
259 gcv.function = GXclear;
264 gcv.function = GXxor;
265 st->gcs [0][i] = XCreateGC (st->dpy, st->window,
266 GCForeground|GCFunction|GCPlaneMask,
271 st->gcs [0][i] = XCreateGC (st->dpy, st->window,
272 GCForeground|GCPlaneMask,
275 st->gcs [1][i] = XCreateGC (st->dpy, st->window,
276 GCForeground|GCPlaneMask,
279 /* jwxyz_XSetAntiAliasing (st->dpy, st->gcs [0][i], False);
280 jwxyz_XSetAntiAliasing (st->dpy, st->gcs [1][i], False); */
281 if (st->transparent_p)
283 jwxyz_XSetAlphaAllowed (dpy, st->gcs [0][i], True);
284 jwxyz_XSetAlphaAllowed (dpy, st->gcs [1][i], True);
286 # endif /* HAVE_COCOA */
293 XSetWindowBackground (st->dpy, st->window, base_pixel);
294 XClearWindow (st->dpy, st->window);
297 #endif /* !HAVE_COCOA */
303 gcv.function = GXxor;
305 (st->default_fg_pixel /* ^ get_pixel_resource (st->dpy, st->cmap,
306 "background", "Background")*/);
307 st->draw_gc = st->erase_gc = XCreateGC(st->dpy,st->window,GCForeground|GCFunction,&gcv);
314 st->draw_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
315 gcv.foreground = get_pixel_resource (st->dpy, st->cmap,
316 "background", "Background");
317 st->erase_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
321 if (st->transparent_p)
322 jwxyz_XSetAlphaAllowed (dpy, st->draw_gc, True);
325 st->qixes = (struct qix **) malloc ((st->count + 1) * sizeof (struct qix *));
326 st->qixes [st->count] = 0;
329 st->qixes [st->count] = init_one_qix (st, nlines);
330 st->qixes [st->count]->id = st->count;
334 /* line-mode leaves turds without this. */
335 jwxyz_XSetAntiAliasing (st->dpy, st->erase_gc, False);
336 jwxyz_XSetAntiAliasing (st->dpy, st->draw_gc, False);
343 free_qline (struct state *st,
349 if (qline->dead || !prev)
351 else if (st->solid_p)
354 /*assert(qix->npoly == 2);*/
355 points [0].x = qline->p[0].x >> SCALE;
356 points [0].y = qline->p[0].y >> SCALE;
357 points [1].x = qline->p[1].x >> SCALE;
358 points [1].y = qline->p[1].y >> SCALE;
359 points [2].x = prev->p[1].x >> SCALE;
360 points [2].y = prev->p[1].y >> SCALE;
361 points [3].x = prev->p[0].x >> SCALE;
362 points [3].y = prev->p[0].y >> SCALE;
363 XFillPolygon (st->dpy, st->window,
364 (st->transparent_p && st->gcs[1]
365 ? st->gcs[1][qix->id]
367 points, 4, Complex, CoordModeOrigin);
371 /* XDrawLine (dpy, window, (transparent_p ? gcs[1][qix->id] : erase_gc),
372 qline->p1.x, qline->p1.y, qline->p2.x, qline->p2.y);*/
373 XPoint points[MAXPOLY+1];
374 for(i = 0; i < qix->npoly; i++)
376 points[i].x = qline->p[i].x >> SCALE;
377 points[i].y = qline->p[i].y >> SCALE;
379 points[qix->npoly] = points[0];
380 XDrawLines(st->dpy, st->window,
381 (st->transparent_p && st->gcs[1]
382 ? st->gcs[1][qix->id]
384 points, qix->npoly+1, CoordModeOrigin);
387 if (!mono_p && !st->transparent_p)
388 XFreeColors (st->dpy, st->cmap, &qline->color.pixel, 1, 0);
394 add_qline (struct state *st,
396 struct qline *prev_qline,
401 for(i=0; i<qix->npoly; i++)
402 qline->p[i] = prev_qline->p[i];
403 qline->color = prev_qline->color;
404 qline->dead = prev_qline->dead;
406 #define wiggle(point,delta,max) \
407 if (st->random_p) delta += (random () % (1 << (SCALE+1))) - (1 << SCALE); \
408 if (delta > st->max_spread) delta = st->max_spread; \
409 else if (delta < -st->max_spread) delta = -st->max_spread; \
411 if (point < 0) point = 0, delta = -delta, point += delta<<1; \
412 else if (point > max) point = max, delta = -delta, point += delta<<1;
415 for(i=0; i<qix->npoly; i++)
418 for (i = 0; i < qix->npoly; i++)
420 wiggle (qline->p[i].x, qline->p[i].dx, st->maxx);
421 wiggle (qline->p[i].y, qline->p[i].dy, st->maxy);
426 /*assert(qix->npoly == 2);*/
427 if (qline->p[0].x - qline->p[1].x > st->max_size)
428 qline->p[0].x = qline->p[1].x + st->max_size
429 - (st->random_p ? random() % st->max_spread : 0);
430 else if (qline->p[1].x - qline->p[0].x > st->max_size)
431 qline->p[1].x = qline->p[0].x + st->max_size
432 - (st->random_p ? random() % st->max_spread : 0);
433 if (qline->p[0].y - qline->p[1].y > st->max_size)
434 qline->p[0].y = qline->p[1].y + st->max_size
435 - (st->random_p ? random() % st->max_spread : 0);
436 else if (qline->p[1].y - qline->p[0].y > st->max_size)
437 qline->p[1].y = qline->p[0].y + st->max_size
438 - (st->random_p ? random() % st->max_spread : 0);
442 if (!mono_p && !st->transparent_p)
449 rgb_to_hsv (qline->color.red, qline->color.green, qline->color.blue,
451 h = (h + st->color_shift) % 360;
453 &qline->color.red, &qline->color.green, &qline->color.blue);
455 qline->color.flags = DoRed | DoGreen | DoBlue;
456 desired = qline->color;
457 if (XAllocColor (st->dpy, st->cmap, &qline->color))
459 /* XAllocColor returns the actual RGB that the hardware let us
460 allocate. Restore the requested values into the XColor struct
461 so that limited-resolution hardware doesn't cause the cycle to
463 qline->color.red = desired.red;
464 qline->color.green = desired.green;
465 qline->color.blue = desired.blue;
469 qline->color = prev_qline->color;
470 if (!XAllocColor (st->dpy, st->cmap, &qline->color))
471 abort (); /* same color should work */
475 if (st->transparent_p)
477 /* give a non-opaque alpha to the color */
478 unsigned long pixel = qline->color.pixel;
479 unsigned long amask = BlackPixelOfScreen (0);
480 unsigned long a = (0xBBBBBBBB & amask);
481 pixel = (pixel & (~amask)) | a;
482 qline->color.pixel = pixel;
484 # endif /* HAVE_COCOA */
486 XSetForeground (st->dpy, st->draw_gc, qline->color.pixel);
490 /* XDrawLine (dpy, window, (transparent_p ? gcs[0][qix->id] : draw_gc),
491 qline->p1.x, qline->p1.y, qline->p2.x, qline->p2.y);*/
492 XPoint points[MAXPOLY+1];
493 for (i = 0; i < qix->npoly; i++)
495 points[i].x = qline->p[i].x >> SCALE;
496 points[i].y = qline->p[i].y >> SCALE;
498 points[qix->npoly] = points[0];
499 XDrawLines(st->dpy, st->window,
500 (st->transparent_p && st->gcs[0]
501 ? st->gcs[0][qix->id]
503 points, qix->npoly+1, CoordModeOrigin);
505 else if (!prev_qline->dead)
508 points [0].x = qline->p[0].x >> SCALE;
509 points [0].y = qline->p[0].y >> SCALE;
510 points [1].x = qline->p[1].x >> SCALE;
511 points [1].y = qline->p[1].y >> SCALE;
512 points [2].x = prev_qline->p[1].x >> SCALE;
513 points [2].y = prev_qline->p[1].y >> SCALE;
514 points [3].x = prev_qline->p[0].x >> SCALE;
515 points [3].y = prev_qline->p[0].y >> SCALE;
516 XFillPolygon (st->dpy, st->window,
517 (st->transparent_p && st->gcs[0]
518 ? st->gcs[0][qix->id]
520 points, 4, Complex, CoordModeOrigin);
527 qix1 (struct state *st, struct qix *qix)
529 int ofp = qix->fp - 1;
530 if (ofp < 0) ofp = qix->nlines - 1;
531 if (st->gtick++ == 500)
532 get_geom (st), st->gtick = 0;
533 free_qline (st, &qix->lines [qix->fp],
534 &qix->lines[(qix->fp + 1) % qix->nlines], qix);
535 add_qline (st, &qix->lines[qix->fp], &qix->lines[ofp], qix);
536 if ((++qix->fp) >= qix->nlines)
542 qix_draw (Display *dpy, Window window, void *closure)
544 struct state *st = (struct state *) closure;
545 struct qix **q1 = st->qixes;
547 for (qn = q1; *qn; qn++)
553 qix_reshape (Display *dpy, Window window, void *closure,
554 unsigned int w, unsigned int h)
556 struct state *st = (struct state *) closure;
561 qix_event (Display *dpy, Window window, void *closure, XEvent *event)
567 qix_free (Display *dpy, Window window, void *closure)
569 struct state *st = (struct state *) closure;
572 if (st->gcs[1] && st->gcs[0] != st->gcs[1])
579 static const char *qix_defaults [] = {
580 ".background: black",
581 ".foreground: white",
597 "*transparent:false",
606 static XrmOptionDescRec qix_options [] = {
607 { "-count", ".count", XrmoptionSepArg, 0 },
608 { "-segments", ".segments", XrmoptionSepArg, 0 },
609 { "-poly", ".poly", XrmoptionSepArg, 0 },
610 { "-spread", ".spread", XrmoptionSepArg, 0 },
611 { "-size", ".size", XrmoptionSepArg, 0 },
612 { "-delay", ".delay", XrmoptionSepArg, 0 },
613 { "-color-shift", ".colorShift", XrmoptionSepArg, 0 },
614 { "-random", ".random", XrmoptionNoArg, "true" },
615 { "-linear", ".random", XrmoptionNoArg, "false" },
616 { "-solid", ".solid", XrmoptionNoArg, "true" },
617 { "-hollow", ".solid", XrmoptionNoArg, "false" },
618 { "-xor", ".xor", XrmoptionNoArg, "true" },
619 { "-no-xor", ".xor", XrmoptionNoArg, "false" },
620 { "-transparent", ".transparent", XrmoptionNoArg, "true" },
621 { "-non-transparent", ".transparent", XrmoptionNoArg, "false" },
622 { "-gravity", ".gravity", XrmoptionNoArg, "true" },
623 { "-no-gravity", ".gravity", XrmoptionNoArg, "false" },
624 { "-additive", ".additive", XrmoptionNoArg, "true" },
625 { "-subtractive", ".additive", XrmoptionNoArg, "false" },
629 XSCREENSAVER_MODULE ("Qix", qix)