1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.com>
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
26 extern int get_visual_class P((Display *, Visual *));
27 extern void screenhack_usleep P((int));
28 #define usleep screenhack_usleep
30 #define MAX_COLORS 4096
31 static XColor orig_colors [MAX_COLORS];
32 static XColor current_colors [MAX_COLORS];
36 copy_colormap (dpy, cmap, into_cmap)
38 Colormap cmap, into_cmap;
41 Screen *screen = DefaultScreenOfDisplay (dpy);
42 Visual *visual = DefaultVisualOfScreen (screen);
43 Window window = RootWindowOfScreen (screen);
44 int vclass = get_visual_class (dpy, visual);
46 ncolors = CellsOfScreen (screen);
48 /* If this is a colormap on a mono visual, or one with insanely many
49 color cells, bug out. */
50 if (ncolors <= 2 || ncolors > MAX_COLORS)
52 /* If this is a non-writable visual, bug out. */
53 if (vclass == StaticGray || vclass == StaticColor || vclass == TrueColor)
57 into_cmap = XCreateColormap (dpy, window, visual, AllocAll);
59 cmap = DefaultColormap (dpy, DefaultScreen (dpy));
60 for (i = 0; i < ncolors; i++)
61 orig_colors [i].pixel = i;
62 XQueryColors (dpy, cmap, orig_colors, ncolors);
63 XStoreColors (dpy, into_cmap, orig_colors, ncolors);
68 blacken_colormap (dpy, cmap)
73 for (i = 0; i < ncolors; i++)
75 current_colors [i].pixel = i;
76 current_colors [i].red = current_colors [i].green =
77 current_colors [i].blue = 0;
79 XStoreColors (dpy, cmap, current_colors, ncolors);
84 fade_colormap (dpy, cmap, cmap2, seconds, ticks, out_p)
91 int steps = seconds * ticks;
97 for (i = 0; i < ncolors; i++)
98 orig_colors [i].pixel = i;
99 XQueryColors (dpy, cmap, orig_colors, ncolors);
100 memcpy (current_colors, orig_colors, ncolors * sizeof (XColor));
102 for (i = (out_p ? steps : 0);
103 (out_p ? i > 0 : i < steps);
107 for (j = 0; j < ncolors; j++)
109 current_colors[j].red = orig_colors[j].red * i / steps;
110 current_colors[j].green = orig_colors[j].green * i / steps;
111 current_colors[j].blue = orig_colors[j].blue * i / steps;
113 XStoreColors (dpy, cmap2, current_colors, ncolors);
116 /* If there is user activity, bug out.
117 We put the event back so that the calling code can notice it too.
118 It would be better to not remove it at all, but that's harder
119 because Xlib has such a non-design for this kind of crap, and
120 in this application it doesn't matter if the events end up out
121 of order, so in the grand unix tradition we say "fuck it" and
122 do something that mostly works for the time being.
124 if (XCheckMaskEvent (dpy, (KeyPressMask | KeyReleaseMask |
125 ButtonPressMask | ButtonReleaseMask |
129 XPutBackEvent (dpy, &dummy_event);
133 usleep (1000000 / (ticks * 2)); /* the 2 is a hack... */
140 char *progclass = "foo";
142 char *defaults [] = {
146 XrmOptionDescRec options [] = {0};
147 int options_size = 0;
154 Colormap cmap = DefaultColormap (dpy, DefaultScreen (dpy));
155 Colormap cmap2 = copy_colormap (dpy, cmap, 0);
158 int ticks = 30 * seconds;
165 XInstallColormap (dpy, cmap2);
166 fade_colormap (dpy, cmap, cmap2, seconds, ticks, True);
167 if (delay) sleep (delay);
168 fade_colormap (dpy, cmap, cmap2, seconds, ticks, False);
169 XInstallColormap (dpy, cmap);
172 if (delay) sleep (delay);