1 /* xscreensaver, Copyright (c) 2007 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
11 * cwaves -- languid sinusoidal colors.
14 #include "screenhack.h"
16 #include "xpm-pixmap.h"
31 XWindowAttributes xgwa;
44 cwaves_init (Display *dpy, Window window)
48 state *st = (state *) calloc (1, sizeof (*st));
52 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
54 st->ncolors = get_integer_resource (dpy, "ncolors", "Integer");
55 if (st->ncolors < 4) st->ncolors = 4;
56 st->colors = (XColor *) malloc (sizeof(*st->colors) * (st->ncolors+1));
57 make_smooth_colormap (st->dpy, st->xgwa.visual, st->xgwa.colormap,
58 st->colors, &st->ncolors,
61 st->gc = XCreateGC (st->dpy, st->window, 0, &gcv);
62 st->delay = get_integer_resource (dpy, "delay", "Integer");
64 st->nwaves = get_integer_resource (dpy, "nwaves", "Integer");
65 st->waves = (wave *) calloc (st->nwaves, sizeof(*st->waves));
67 for (i = 0; i < st->nwaves; i++)
69 st->waves[i].scale = frand(0.05) + 0.005;
70 st->waves[i].offset = frand(M_PI);
71 st->waves[i].delta = (frand(2) - 1) / 20.0;
79 cwaves_draw (Display *dpy, Window window, void *closure)
81 state *st = (state *) closure;
84 for (i = 0; i < st->nwaves; i++)
85 st->waves[i].offset += st->waves[i].delta;
87 for (x = 0; x < st->xgwa.width; x++)
91 for (i = 0; i < st->nwaves; i++)
92 v += cos ((x * st->waves[i].scale) - st->waves[i].offset);
95 j = st->ncolors * (v/2 + 0.5);
96 if (j < 0 || j >= st->ncolors) abort();
97 XSetForeground (st->dpy, st->gc, st->colors[j].pixel);
98 XDrawLine (st->dpy, st->window, st->gc, x, 0, x, st->xgwa.height);
106 cwaves_reshape (Display *dpy, Window window, void *closure,
107 unsigned int w, unsigned int h)
109 state *st = (state *) closure;
110 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
114 cwaves_event (Display *dpy, Window window, void *closure, XEvent *event)
116 state *st = (state *) closure;
117 if (event->type == ButtonPress)
119 make_smooth_colormap (st->dpy, st->xgwa.visual, st->xgwa.colormap,
120 st->colors, &st->ncolors,
128 cwaves_free (Display *dpy, Window window, void *closure)
133 static const char *cwaves_defaults [] = {
134 ".background: black",
135 ".foreground: white",
142 static XrmOptionDescRec cwaves_options [] = {
143 { "-delay", ".delay", XrmoptionSepArg, 0 },
144 { "-waves", ".nwaves", XrmoptionSepArg, 0 },
145 { "-colors", ".ncolors", XrmoptionSepArg, 0 },
150 XSCREENSAVER_MODULE ("CWaves", cwaves)