1 /* xscreensaver, Copyright (c) 1992-2008 Jamie Zawinski <jwz@jwz.org>
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
12 #include "screenhack.h"
43 unsigned int default_fg_pixel;
44 long maxx, maxy, max_spread, max_size;
46 Bool random_p, solid_p, xor_p, transparent_p, gravity_p;
63 get_geom (struct state *st)
65 XWindowAttributes xgwa;
66 XGetWindowAttributes (st->dpy, st->window, &xgwa);
67 st->maxx = ((long)(xgwa.width+1)<<SCALE) - 1;
68 st->maxy = ((long)(xgwa.height+1)<<SCALE) - 1;
72 init_one_qix (struct state *st, int nlines)
75 struct qix *qix = (struct qix *) calloc (1, sizeof (struct qix));
77 qix->lines = (struct qline *) calloc (qix->nlines, sizeof (struct qline));
78 qix->npoly = st->npoly;
79 for (i = 0; i < qix->nlines; i++)
80 qix->lines[i].p = (struct qpoint *)
81 calloc(qix->npoly, sizeof(struct qpoint));
84 if (!mono_p && !st->transparent_p)
87 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
88 &qix->lines[0].color.red, &qix->lines[0].color.green,
89 &qix->lines[0].color.blue);
90 if (!XAllocColor (st->dpy, st->cmap, &qix->lines[0].color))
92 qix->lines[0].color.pixel = st->default_fg_pixel;
93 XQueryColor (st->dpy, st->cmap, &qix->lines[0].color);
94 if (!XAllocColor (st->dpy, st->cmap, &qix->lines[0].color))
99 if (st->max_size == 0)
101 for (i = 0; i < qix->npoly; i++)
103 qix->lines[0].p[i].x = random () % st->maxx;
104 qix->lines[0].p[i].y = random () % st->maxy;
109 /*assert(qix->npoly == 2);*/
110 qix->lines[0].p[0].x = random () % st->maxx;
111 qix->lines[0].p[0].y = random () % st->maxy;
112 qix->lines[0].p[1].x = qix->lines[0].p[0].x + (random () % (st->max_size/2));
113 qix->lines[0].p[1].y = qix->lines[0].p[0].y + (random () % (st->max_size/2));
114 if (qix->lines[0].p[1].x > st->maxx) qix->lines[0].p[1].x = st->maxx;
115 if (qix->lines[0].p[1].y > st->maxy) qix->lines[0].p[1].y = st->maxy;
118 for (i = 0; i < qix->npoly; i++)
120 qix->lines[0].p[i].dx = (random () % (st->max_spread + 1)) - (st->max_spread /2);
121 qix->lines[0].p[i].dy = (random () % (st->max_spread + 1)) - (st->max_spread /2);
123 qix->lines[0].dead = True;
125 for (i = 1; i < qix->nlines; i++)
127 for(j=0; j<qix->npoly; j++)
128 qix->lines[i].p[j] = qix->lines[0].p[j];
129 qix->lines[i].color = qix->lines[0].color;
130 qix->lines[i].dead = qix->lines[0].dead;
133 if (!mono_p && !st->transparent_p)
135 if (!XAllocColor (st->dpy, st->cmap, &qix->lines[i].color))
145 qix_init (Display *dpy, Window window)
147 struct state *st = (struct state *) calloc (1, sizeof(*st));
150 XWindowAttributes xgwa;
153 XGetWindowAttributes (st->dpy, st->window, &xgwa);
154 st->cmap = xgwa.colormap;
155 st->count = get_integer_resource (st->dpy, "count", "Integer");
156 if (st->count <= 0) st->count = 1;
157 nlines = get_integer_resource (st->dpy, "segments", "Integer");
158 if (nlines <= 0) nlines = 20;
159 st->npoly = get_integer_resource(st->dpy, "poly", "Integer");
160 if (st->npoly <= 2) st->npoly = 2;
161 if (st->npoly > MAXPOLY) st->npoly = MAXPOLY;
163 st->max_spread = get_integer_resource (st->dpy, "spread", "Integer");
164 if (st->max_spread <= 0) st->max_spread = 10;
165 st->max_spread <<= SCALE;
166 st->max_size = get_integer_resource (st->dpy, "size", "Integer");
167 if (st->max_size < 0) st->max_size = 0;
168 st->max_size <<= SCALE;
169 st->random_p = get_boolean_resource (st->dpy, "random", "Boolean");
170 st->solid_p = get_boolean_resource (st->dpy, "solid", "Boolean");
171 st->xor_p = get_boolean_resource (st->dpy, "xor", "Boolean");
172 st->transparent_p = get_boolean_resource (st->dpy, "transparent", "Boolean");
173 st->gravity_p = get_boolean_resource(st->dpy, "gravity", "Boolean");
174 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
175 st->color_shift = get_integer_resource (st->dpy, "colorShift", "Integer");
176 if (st->color_shift < 0 || st->color_shift >= 360) st->color_shift = 5;
177 if (st->delay < 0) st->delay = 0;
179 /* Clear up ambiguities regarding npoly */
183 fprintf(stderr, "%s: Can't have -solid and -poly; using -poly 2\n",
190 fprintf(stderr, "%s: Can't have -poly and -size; using -size 0\n",
195 if (st->count == 1 && st->transparent_p)
196 st->transparent_p = False; /* it's a no-op */
198 if (st->transparent_p && CellsOfScreen (DefaultScreenOfDisplay (st->dpy)) <= 2)
200 fprintf (stderr, "%s: -transparent only works on color displays.\n",
202 st->transparent_p = False;
205 if (st->xor_p && !st->transparent_p)
208 st->gcs[0] = st->gcs[1] = 0;
209 gcv.foreground = st->default_fg_pixel =
210 get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
212 st->additive_p = get_boolean_resource (st->dpy, "additive", "Boolean");
213 st->cmap_p = has_writable_cells (xgwa.screen, xgwa.visual);
216 if (st->transparent_p)
218 unsigned long *plane_masks = 0;
219 unsigned long base_pixel;
220 int nplanes = st->count;
223 allocate_alpha_colors (xgwa.screen, xgwa.visual, st->cmap,
224 &nplanes, st->additive_p, &plane_masks,
230 "%s: couldn't allocate any color planes; turning -transparent off.\n",
232 st->transparent_p = False;
234 goto NON_TRANSPARENT_XOR;
236 goto NON_TRANSPARENT;
238 else if (nplanes != st->count)
241 "%s: only allocated %d color planes (instead of %d).\n",
242 progname, nplanes, st->count);
246 st->gcs[0] = (GC *) malloc (st->count * sizeof (GC));
247 st->gcs[1] = (st->xor_p
249 : (GC *) malloc (st->count * sizeof (GC)));
251 for (i = 0; i < st->count; i++)
253 gcv.plane_mask = plane_masks [i];
256 /* argh, I'm not sure how to make "-subtractive" work in truecolor...
257 if (!cmap_p && !additive_p)
258 gcv.function = GXclear;
263 gcv.function = GXxor;
264 st->gcs [0][i] = XCreateGC (st->dpy, st->window,
265 GCForeground|GCFunction|GCPlaneMask,
270 st->gcs [0][i] = XCreateGC (st->dpy, st->window,
271 GCForeground|GCPlaneMask,
274 st->gcs [1][i] = XCreateGC (st->dpy, st->window,
275 GCForeground|GCPlaneMask,
278 /* jwxyz_XSetAntiAliasing (st->dpy, st->gcs [0][i], False);
279 jwxyz_XSetAntiAliasing (st->dpy, st->gcs [1][i], False); */
280 if (st->transparent_p)
282 jwxyz_XSetAlphaAllowed (dpy, st->gcs [0][i], True);
283 jwxyz_XSetAlphaAllowed (dpy, st->gcs [1][i], True);
285 # endif /* HAVE_COCOA */
292 XSetWindowBackground (st->dpy, st->window, base_pixel);
293 XClearWindow (st->dpy, st->window);
296 #endif /* !HAVE_COCOA */
302 gcv.function = GXxor;
304 (st->default_fg_pixel /* ^ get_pixel_resource (st->dpy, st->cmap,
305 "background", "Background")*/);
306 st->draw_gc = st->erase_gc = XCreateGC(st->dpy,st->window,GCForeground|GCFunction,&gcv);
313 st->draw_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
314 gcv.foreground = get_pixel_resource (st->dpy, st->cmap,
315 "background", "Background");
316 st->erase_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
320 if (st->transparent_p)
321 jwxyz_XSetAlphaAllowed (dpy, st->draw_gc, True);
324 st->qixes = (struct qix **) malloc ((st->count + 1) * sizeof (struct qix *));
325 st->qixes [st->count] = 0;
328 st->qixes [st->count] = init_one_qix (st, nlines);
329 st->qixes [st->count]->id = st->count;
333 /* line-mode leaves turds without this. */
334 jwxyz_XSetAntiAliasing (st->dpy, st->erase_gc, False);
335 jwxyz_XSetAntiAliasing (st->dpy, st->draw_gc, False);
342 free_qline (struct state *st,
348 if (qline->dead || !prev)
350 else if (st->solid_p)
353 /*assert(qix->npoly == 2);*/
354 points [0].x = qline->p[0].x >> SCALE;
355 points [0].y = qline->p[0].y >> SCALE;
356 points [1].x = qline->p[1].x >> SCALE;
357 points [1].y = qline->p[1].y >> SCALE;
358 points [2].x = prev->p[1].x >> SCALE;
359 points [2].y = prev->p[1].y >> SCALE;
360 points [3].x = prev->p[0].x >> SCALE;
361 points [3].y = prev->p[0].y >> SCALE;
362 XFillPolygon (st->dpy, st->window,
363 (st->transparent_p && st->gcs[1]
364 ? st->gcs[1][qix->id]
366 points, 4, Complex, CoordModeOrigin);
370 /* XDrawLine (dpy, window, (transparent_p ? gcs[1][qix->id] : erase_gc),
371 qline->p1.x, qline->p1.y, qline->p2.x, qline->p2.y);*/
372 XPoint points[MAXPOLY+1];
373 for(i = 0; i < qix->npoly; i++)
375 points[i].x = qline->p[i].x >> SCALE;
376 points[i].y = qline->p[i].y >> SCALE;
378 points[qix->npoly] = points[0];
379 XDrawLines(st->dpy, st->window,
380 (st->transparent_p && st->gcs[1]
381 ? st->gcs[1][qix->id]
383 points, qix->npoly+1, CoordModeOrigin);
386 if (!mono_p && !st->transparent_p)
387 XFreeColors (st->dpy, st->cmap, &qline->color.pixel, 1, 0);
393 add_qline (struct state *st,
395 struct qline *prev_qline,
400 for(i=0; i<qix->npoly; i++)
401 qline->p[i] = prev_qline->p[i];
402 qline->color = prev_qline->color;
403 qline->dead = prev_qline->dead;
405 #define wiggle(point,delta,max) \
406 if (st->random_p) delta += (random () % (1 << (SCALE+1))) - (1 << SCALE); \
407 if (delta > st->max_spread) delta = st->max_spread; \
408 else if (delta < -st->max_spread) delta = -st->max_spread; \
410 if (point < 0) point = 0, delta = -delta, point += delta<<1; \
411 else if (point > max) point = max, delta = -delta, point += delta<<1;
414 for(i=0; i<qix->npoly; i++)
417 for (i = 0; i < qix->npoly; i++)
419 wiggle (qline->p[i].x, qline->p[i].dx, st->maxx);
420 wiggle (qline->p[i].y, qline->p[i].dy, st->maxy);
425 /*assert(qix->npoly == 2);*/
426 if (qline->p[0].x - qline->p[1].x > st->max_size)
427 qline->p[0].x = qline->p[1].x + st->max_size
428 - (st->random_p ? random() % st->max_spread : 0);
429 else if (qline->p[1].x - qline->p[0].x > st->max_size)
430 qline->p[1].x = qline->p[0].x + st->max_size
431 - (st->random_p ? random() % st->max_spread : 0);
432 if (qline->p[0].y - qline->p[1].y > st->max_size)
433 qline->p[0].y = qline->p[1].y + st->max_size
434 - (st->random_p ? random() % st->max_spread : 0);
435 else if (qline->p[1].y - qline->p[0].y > st->max_size)
436 qline->p[1].y = qline->p[0].y + st->max_size
437 - (st->random_p ? random() % st->max_spread : 0);
441 if (!mono_p && !st->transparent_p)
448 rgb_to_hsv (qline->color.red, qline->color.green, qline->color.blue,
450 h = (h + st->color_shift) % 360;
452 &qline->color.red, &qline->color.green, &qline->color.blue);
454 qline->color.flags = DoRed | DoGreen | DoBlue;
455 desired = qline->color;
456 if (XAllocColor (st->dpy, st->cmap, &qline->color))
458 /* XAllocColor returns the actual RGB that the hardware let us
459 allocate. Restore the requested values into the XColor struct
460 so that limited-resolution hardware doesn't cause the cycle to
462 qline->color.red = desired.red;
463 qline->color.green = desired.green;
464 qline->color.blue = desired.blue;
468 qline->color = prev_qline->color;
469 if (!XAllocColor (st->dpy, st->cmap, &qline->color))
470 abort (); /* same color should work */
474 if (st->transparent_p)
476 /* give a non-opaque alpha to the color */
477 unsigned long pixel = qline->color.pixel;
478 unsigned long amask = BlackPixelOfScreen (0);
479 unsigned long a = (0xBBBBBBBB & amask);
480 pixel = (pixel & (~amask)) | a;
481 qline->color.pixel = pixel;
483 # endif /* HAVE_COCOA */
485 XSetForeground (st->dpy, st->draw_gc, qline->color.pixel);
489 /* XDrawLine (dpy, window, (transparent_p ? gcs[0][qix->id] : draw_gc),
490 qline->p1.x, qline->p1.y, qline->p2.x, qline->p2.y);*/
491 XPoint points[MAXPOLY+1];
492 for (i = 0; i < qix->npoly; i++)
494 points[i].x = qline->p[i].x >> SCALE;
495 points[i].y = qline->p[i].y >> SCALE;
497 points[qix->npoly] = points[0];
498 XDrawLines(st->dpy, st->window,
499 (st->transparent_p && st->gcs[0]
500 ? st->gcs[0][qix->id]
502 points, qix->npoly+1, CoordModeOrigin);
504 else if (!prev_qline->dead)
507 points [0].x = qline->p[0].x >> SCALE;
508 points [0].y = qline->p[0].y >> SCALE;
509 points [1].x = qline->p[1].x >> SCALE;
510 points [1].y = qline->p[1].y >> SCALE;
511 points [2].x = prev_qline->p[1].x >> SCALE;
512 points [2].y = prev_qline->p[1].y >> SCALE;
513 points [3].x = prev_qline->p[0].x >> SCALE;
514 points [3].y = prev_qline->p[0].y >> SCALE;
515 XFillPolygon (st->dpy, st->window,
516 (st->transparent_p && st->gcs[0]
517 ? st->gcs[0][qix->id]
519 points, 4, Complex, CoordModeOrigin);
526 qix1 (struct state *st, struct qix *qix)
528 int ofp = qix->fp - 1;
529 if (ofp < 0) ofp = qix->nlines - 1;
530 if (st->gtick++ == 500)
531 get_geom (st), st->gtick = 0;
532 free_qline (st, &qix->lines [qix->fp],
533 &qix->lines[(qix->fp + 1) % qix->nlines], qix);
534 add_qline (st, &qix->lines[qix->fp], &qix->lines[ofp], qix);
535 if ((++qix->fp) >= qix->nlines)
541 qix_draw (Display *dpy, Window window, void *closure)
543 struct state *st = (struct state *) closure;
544 struct qix **q1 = st->qixes;
546 for (qn = q1; *qn; qn++)
552 qix_reshape (Display *dpy, Window window, void *closure,
553 unsigned int w, unsigned int h)
555 struct state *st = (struct state *) closure;
560 qix_event (Display *dpy, Window window, void *closure, XEvent *event)
566 qix_free (Display *dpy, Window window, void *closure)
568 struct state *st = (struct state *) closure;
571 if (st->gcs[1] && st->gcs[0] != st->gcs[1])
578 static const char *qix_defaults [] = {
579 ".background: black",
580 ".foreground: white",
598 static XrmOptionDescRec qix_options [] = {
599 { "-count", ".count", XrmoptionSepArg, 0 },
600 { "-segments", ".segments", XrmoptionSepArg, 0 },
601 { "-poly", ".poly", XrmoptionSepArg, 0 },
602 { "-spread", ".spread", XrmoptionSepArg, 0 },
603 { "-size", ".size", XrmoptionSepArg, 0 },
604 { "-delay", ".delay", XrmoptionSepArg, 0 },
605 { "-color-shift", ".colorShift", XrmoptionSepArg, 0 },
606 { "-random", ".random", XrmoptionNoArg, "true" },
607 { "-linear", ".random", XrmoptionNoArg, "false" },
608 { "-solid", ".solid", XrmoptionNoArg, "true" },
609 { "-hollow", ".solid", XrmoptionNoArg, "false" },
610 { "-xor", ".xor", XrmoptionNoArg, "true" },
611 { "-no-xor", ".xor", XrmoptionNoArg, "false" },
612 { "-transparent", ".transparent", XrmoptionNoArg, "true" },
613 { "-non-transparent", ".transparent", XrmoptionNoArg, "false" },
614 { "-gravity", ".gravity", XrmoptionNoArg, "true" },
615 { "-no-gravity", ".gravity", XrmoptionNoArg, "false" },
616 { "-additive", ".additive", XrmoptionNoArg, "true" },
617 { "-subtractive", ".additive", XrmoptionNoArg, "false" },
621 XSCREENSAVER_MODULE ("Qix", qix)