1 /* Copyright (c) 2003 Levi Burton <donburton@sbcglobal.net>
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"
15 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
17 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
19 typedef struct _square {
25 randomize_square_colors(square *squares, int nsquares, int ncolors)
29 for (i = 0; i < nsquares; i++)
30 s[i].color = random() % ncolors;
34 char *progclass = "popsquares";
44 "*doubleBuffer: False",
45 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
48 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
52 XrmOptionDescRec options [] = {
53 { "-fg", ".foreground", XrmoptionSepArg, 0},
54 { "-bg", ".background", XrmoptionSepArg, 0},
55 { "-delay", ".delay", XrmoptionSepArg, 0 },
56 { "-subdivision", ".subdivision", XrmoptionSepArg, 0 },
57 { "-border", ".border", XrmoptionSepArg, 0},
58 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
59 { "-twitch", ".twitch", XrmoptionNoArg, "True" },
60 { "-no-twitch", ".twitch", XrmoptionNoArg, "False" },
61 { "-db", ".doubleBuffer", XrmoptionNoArg, "True" },
62 { "-no-db", ".doubleBuffer", XrmoptionNoArg, "False" },
67 screenhack (Display *dpy, Window window)
69 int delay = get_integer_resource ("delay", "Integer");
70 int subdivision = get_integer_resource("subdivision", "Integer");
71 int border = get_integer_resource("border", "Integer");
72 int ncolors = get_integer_resource("ncolors", "Integer");
73 Bool twitch = get_boolean_resource("twitch", "Boolean");
74 Bool dbuf = get_boolean_resource("doubleBuffer", "Boolean");
76 int sw, sh, gw, gh, nsquares = 0;
77 double s1, v1, s2, v2 = 0;
79 /* Not sure how to use DBEClear */
80 /* Bool dbeclear_p = get_boolean_resource("useDBEClear", "Boolean"); */
86 XWindowAttributes xgwa;
87 Pixmap b=0, ba=0, bb=0;
88 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
89 XdbeBackBuffer backb = 0;
90 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
92 XGetWindowAttributes (dpy, window, &xgwa);
94 fg.pixel = get_pixel_resource ("foreground", "Foreground", dpy, xgwa.colormap);
95 bg.pixel = get_pixel_resource ("background", "Background", dpy, xgwa.colormap);
97 XQueryColor (dpy, xgwa.colormap, &fg);
98 XQueryColor (dpy, xgwa.colormap, &bg);
100 sw = xgwa.width / subdivision;
101 sh = xgwa.height / subdivision;
102 gw = xgwa.width / sw;
103 gh = xgwa.height / sh;
106 gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv);
108 colors = (XColor *) calloc (ncolors, sizeof(XColor));
109 squares = (square *) calloc (nsquares, sizeof(square));
111 rgb_to_hsv (fg.red, fg.green, fg.blue, &h1, &s1, &v1);
112 rgb_to_hsv (bg.red, bg.green, bg.blue, &h2, &s2, &v2);
113 make_color_ramp (dpy, xgwa.colormap,
116 colors, &ncolors, /* would this be considered a value-result argument? */
120 fprintf (stderr, "%s: insufficient colors!\n", progname);
124 for (y = 0; y < gh; y++)
125 for (x = 0; x < gw; x++)
127 square *s = (square *) &squares[gw * y + x];
134 randomize_square_colors(squares, nsquares, ncolors);
138 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
139 b = xdbe_get_backbuffer (dpy, window, XdbeUndefined);
141 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
144 ba = XCreatePixmap (dpy, window, xgwa.width, xgwa.height, xgwa.depth);
145 bb = XCreatePixmap (dpy, window, xgwa.width, xgwa.height, xgwa.depth);
156 for (y = 0; y < gh; y++)
157 for (x = 0; x < gw; x++)
159 square *s = (square *) &squares[gw * y + x];
160 XSetForeground (dpy, gc, colors[s->color].pixel);
161 XFillRectangle (dpy, b, gc, s->x, s->y,
162 border ? s->w - border : s->w,
163 border ? s->h - border : s->h);
165 if (s->color == ncolors)
167 if (twitch && ((random() % 4) == 0))
168 randomize_square_colors (squares, nsquares, ncolors);
170 s->color = random() % ncolors;
173 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
176 XdbeSwapInfo info[1];
177 info[0].swap_window = window;
178 info[0].swap_action = XdbeUndefined;
179 XdbeSwapBuffers (dpy, info, 1);
182 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
185 XCopyArea (dpy, b, window, gc, 0, 0,
186 xgwa.width, xgwa.height, 0, 0);
187 b = (b == ba ? bb : ba);
191 screenhack_handle_events (dpy);