began for maemo
[xscreensaver] / xscreensaver / hacks / qix.c
1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997, 1998, 2006
2  *  Jamie Zawinski <jwz@jwz.org>
3  *
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 
10  * implied warranty.
11  */
12
13 #include "screenhack.h"
14 #include "alpha.h"
15 #include <stdio.h>
16
17 #define MAXPOLY 16
18 #define SCALE   6
19
20 struct qpoint {
21   int x, y;
22   int dx, dy;
23 };
24
25 struct qline {
26   struct qpoint *p;
27   XColor color;
28   Bool dead;
29 };
30
31 struct qix {
32   int id;
33   int fp;
34   int nlines;
35   int npoly;
36   struct qline *lines;
37 };
38
39 struct state {
40   Display *dpy;
41   Window window;
42
43   GC draw_gc, erase_gc;
44   unsigned int default_fg_pixel;
45   long maxx, maxy, max_spread, max_size;
46   int color_shift;
47   Bool random_p, solid_p, xor_p, transparent_p, gravity_p;
48   int delay;
49   int count;
50   Colormap cmap;
51   int npoly;
52   Bool additive_p;
53   Bool cmap_p;
54
55   GC *gcs[2];
56
57   int gtick;
58
59   struct qix **qixes;
60 };
61
62
63 static void
64 get_geom (struct state *st)
65 {
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;
70 }
71
72 static struct qix *
73 init_one_qix (struct state *st, int nlines)
74 {
75   int i, j;
76   struct qix *qix = (struct qix *) calloc (1, sizeof (struct qix));
77   qix->nlines = nlines;
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));
83
84 # ifndef HAVE_COCOA
85   if (!mono_p && !st->transparent_p)
86 # endif
87     {
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))
92         {
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))
96             abort ();
97         }
98     }
99
100   if (st->max_size == 0)
101     {
102       for (i = 0; i < qix->npoly; i++)
103         {
104           qix->lines[0].p[i].x = random () % st->maxx;
105           qix->lines[0].p[i].y = random () % st->maxy;
106         }
107     }
108   else
109     {
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;
117     }
118
119   for (i = 0; i < qix->npoly; i++)
120     {
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);
123     }
124   qix->lines[0].dead = True;
125
126   for (i = 1; i < qix->nlines; i++)
127     {
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;
132   
133 # ifndef HAVE_COCOA
134       if (!mono_p && !st->transparent_p)
135 # endif
136         if (!XAllocColor (st->dpy, st->cmap, &qix->lines[i].color))
137           abort ();
138     }
139   return qix;
140 }
141
142
143
144
145 static void *
146 qix_init (Display *dpy, Window window)
147 {
148   struct state *st = (struct state *) calloc (1, sizeof(*st));
149   int nlines;
150   XGCValues gcv;
151   XWindowAttributes xgwa;
152   st->dpy = dpy;
153   st->window = window;
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;
163   get_geom (st);
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;
179
180   /* Clear up ambiguities regarding npoly */
181   if (st->solid_p) 
182     {
183       if (st->npoly != 2)
184         fprintf(stderr, "%s: Can't have -solid and -poly; using -poly 2\n",
185                 progname);
186       st->npoly = 2;
187     }      
188   if (st->npoly > 2)
189     {
190       if (st->max_size)
191         fprintf(stderr, "%s: Can't have -poly and -size; using -size 0\n",
192                 progname);
193       st->max_size = 0;
194     }
195
196   if (st->count == 1 && st->transparent_p)
197     st->transparent_p = False; /* it's a no-op */
198
199   if (st->transparent_p && CellsOfScreen (DefaultScreenOfDisplay (st->dpy)) <= 2)
200     {
201       fprintf (stderr, "%s: -transparent only works on color displays.\n",
202                progname);
203       st->transparent_p = False;
204     }
205
206   if (st->xor_p && !st->transparent_p)
207     mono_p = True;
208
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");
212
213   st->additive_p = get_boolean_resource (st->dpy, "additive", "Boolean");
214   st->cmap_p = has_writable_cells (xgwa.screen, xgwa.visual);
215
216 # ifndef HAVE_COCOA
217   if (st->transparent_p)
218     {
219       unsigned long *plane_masks = 0;
220       unsigned long base_pixel;
221       int nplanes = st->count;
222       int i;
223
224       allocate_alpha_colors (xgwa.screen, xgwa.visual, st->cmap,
225                              &nplanes, st->additive_p, &plane_masks,
226                              &base_pixel);
227
228       if (nplanes <= 1)
229         {
230           fprintf (stderr,
231          "%s: couldn't allocate any color planes; turning -transparent off.\n",
232                    progname);
233           st->transparent_p = False;
234           if (st->xor_p)
235             goto NON_TRANSPARENT_XOR;
236           else
237             goto NON_TRANSPARENT;
238         }
239       else if (nplanes != st->count)
240         {
241           fprintf (stderr,
242                    "%s: only allocated %d color planes (instead of %d).\n",
243                    progname, nplanes, st->count);
244           st->count = nplanes;
245         }
246
247       st->gcs[0] = (GC *) malloc (st->count * sizeof (GC));
248       st->gcs[1] = (st->xor_p
249                     ? st->gcs[0]
250                     : (GC *) malloc (st->count * sizeof (GC)));
251
252       for (i = 0; i < st->count; i++)
253         {
254           gcv.plane_mask = plane_masks [i];
255           gcv.foreground = ~0;
256
257 /*  argh, I'm not sure how to make "-subtractive" work in truecolor...
258           if (!cmap_p && !additive_p)
259             gcv.function = GXclear;
260  */
261
262           if (st->xor_p)
263             {
264               gcv.function = GXxor;
265               st->gcs [0][i] = XCreateGC (st->dpy, st->window,
266                                           GCForeground|GCFunction|GCPlaneMask,
267                                           &gcv);
268             }
269           else
270             {
271               st->gcs [0][i] = XCreateGC (st->dpy, st->window, 
272                                           GCForeground|GCPlaneMask,
273                                           &gcv);
274               gcv.foreground = 0;
275               st->gcs [1][i] = XCreateGC (st->dpy, st->window, 
276                                           GCForeground|GCPlaneMask,
277                                           &gcv);
278 # ifdef HAVE_COCOA
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)
282                 {
283                   jwxyz_XSetAlphaAllowed (dpy, st->gcs [0][i], True);
284                   jwxyz_XSetAlphaAllowed (dpy, st->gcs [1][i], True);
285                 }
286 # endif /* HAVE_COCOA */
287             }
288         }
289
290       if (plane_masks)
291         free (plane_masks);
292
293       XSetWindowBackground (st->dpy, st->window, base_pixel);
294       XClearWindow (st->dpy, st->window);
295     }
296   else
297 #endif /* !HAVE_COCOA */
298   if (st->xor_p)
299     {
300 #ifndef HAVE_COCOA
301     NON_TRANSPARENT_XOR:
302 #endif
303       gcv.function = GXxor;
304       gcv.foreground =
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);
308     }
309   else
310     {
311 #ifndef HAVE_COCOA
312     NON_TRANSPARENT:
313 #endif
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);
318     }
319
320 #ifdef HAVE_COCOA
321   if (st->transparent_p)
322     jwxyz_XSetAlphaAllowed (dpy, st->draw_gc, True);
323 #endif
324
325   st->qixes = (struct qix **) malloc ((st->count + 1) * sizeof (struct qix *));
326   st->qixes [st->count] = 0;
327   while (st->count--)
328     {
329       st->qixes [st->count] = init_one_qix (st, nlines);
330       st->qixes [st->count]->id = st->count;
331     }
332
333 # ifdef HAVE_COCOA
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);
337 # endif
338
339   return st;
340 }
341
342 static void
343 free_qline (struct state *st,
344             struct qline *qline,
345             struct qline *prev,
346             struct qix *qix)
347 {
348   int i;
349   if (qline->dead || !prev)
350     ;
351   else if (st->solid_p)
352     {
353       XPoint points [4];
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]
366                      : st->erase_gc),
367                     points, 4, Complex, CoordModeOrigin);
368     }
369   else
370     {
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++)
375         {
376           points[i].x = qline->p[i].x >> SCALE;
377           points[i].y = qline->p[i].y >> SCALE;
378         }
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]
383                   : st->erase_gc),
384                  points, qix->npoly+1, CoordModeOrigin);
385     }
386
387   if (!mono_p && !st->transparent_p)
388     XFreeColors (st->dpy, st->cmap, &qline->color.pixel, 1, 0);
389
390   qline->dead = True;
391 }
392
393 static void
394 add_qline (struct state *st,
395            struct qline *qline,
396            struct qline *prev_qline,
397            struct qix *qix)
398 {
399   int i;
400
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;
405
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;                    \
410   point += delta;                                                       \
411   if (point < 0) point = 0, delta = -delta, point += delta<<1;          \
412   else if (point > max) point = max, delta = -delta, point += delta<<1;
413
414   if (st->gravity_p)
415     for(i=0; i<qix->npoly; i++)
416       qline->p[i].dy += 3;
417
418   for (i = 0; i < qix->npoly; i++)
419     {
420       wiggle (qline->p[i].x, qline->p[i].dx, st->maxx);
421       wiggle (qline->p[i].y, qline->p[i].dy, st->maxy);
422     }
423
424   if (st->max_size)
425     {
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);
439     }
440
441 #ifndef HAVE_COCOA
442   if (!mono_p && !st->transparent_p)
443 #endif
444     {
445       XColor desired;
446
447       int h;
448       double s, v;
449       rgb_to_hsv (qline->color.red, qline->color.green, qline->color.blue,
450                   &h, &s, &v);
451       h = (h + st->color_shift) % 360;
452       hsv_to_rgb (h, s, v,
453                   &qline->color.red, &qline->color.green, &qline->color.blue);
454
455       qline->color.flags = DoRed | DoGreen | DoBlue;
456       desired = qline->color;
457       if (XAllocColor (st->dpy, st->cmap, &qline->color))
458         {
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
462              get "stuck". */
463           qline->color.red = desired.red;
464           qline->color.green = desired.green;
465           qline->color.blue = desired.blue;
466         }
467       else
468         {
469           qline->color = prev_qline->color;
470           if (!XAllocColor (st->dpy, st->cmap, &qline->color))
471             abort (); /* same color should work */
472         }
473
474 # ifdef HAVE_COCOA
475           if (st->transparent_p)
476             {
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;
483             }
484 # endif /* HAVE_COCOA */
485
486       XSetForeground (st->dpy, st->draw_gc, qline->color.pixel);
487     }
488   if (! st->solid_p)
489     {
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++)
494         {
495           points[i].x = qline->p[i].x >> SCALE;
496           points[i].y = qline->p[i].y >> SCALE;
497         }
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]
502                   : st->draw_gc),
503                  points, qix->npoly+1, CoordModeOrigin);
504     }
505   else if (!prev_qline->dead)
506     {
507       XPoint points [4];
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]
519                      : st->draw_gc),
520                     points, 4, Complex, CoordModeOrigin);
521     }
522
523   qline->dead = False;
524 }
525
526 static void
527 qix1 (struct state *st, struct qix *qix)
528 {
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)
537     qix->fp = 0;
538 }
539
540
541 static unsigned long
542 qix_draw (Display *dpy, Window window, void *closure)
543 {
544   struct state *st = (struct state *) closure;
545   struct qix **q1 = st->qixes;
546   struct qix **qn;
547   for (qn = q1; *qn; qn++)
548     qix1 (st, *qn);
549   return st->delay;
550 }
551
552 static void
553 qix_reshape (Display *dpy, Window window, void *closure, 
554                  unsigned int w, unsigned int h)
555 {
556   struct state *st = (struct state *) closure;
557   get_geom (st);
558 }
559
560 static Bool
561 qix_event (Display *dpy, Window window, void *closure, XEvent *event)
562 {
563   return False;
564 }
565
566 static void
567 qix_free (Display *dpy, Window window, void *closure)
568 {
569   struct state *st = (struct state *) closure;
570   if (st->gcs[0])
571     free (st->gcs[0]);
572   if (st->gcs[1] && st->gcs[0] != st->gcs[1])
573     free (st->gcs[1]);
574   free (st->qixes);
575   free (st);
576 }
577
578 \f
579 static const char *qix_defaults [] = {
580   ".background: black",
581   ".foreground: white",
582 #if 0
583   "*count:      1",
584 #else
585   "*count:      5",
586 #endif
587   "*segments:   50",
588   "*poly:       2",
589   "*spread:     8",
590   "*size:       0",
591   "*colorShift: 3",
592   "*solid:      false",
593   "*delay:      10000",
594   "*random:     true",
595   "*xor:        false",
596 #if 0
597   "*transparent:false",
598 #else
599   "*transparent:true",
600 #endif
601   "*gravity:    false",
602   "*additive:   true",
603   0
604 };
605
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" },
626   { 0, 0, 0, 0 }
627 };
628
629 XSCREENSAVER_MODULE ("Qix", qix)