rust/cargo-c: update to 0.10.7+cargo-0.84.0
[oi-userland.git] / components / x11 / xcolor / src / xcolor.c
blob216edc1f0ae78cdb1946e035219ea0a0ba84bde5
1 /*-
2 * xcolor.c - X11 client to display all colors in current colormap.
4 * Copyright (c) 1989, 2011, Oracle and/or its affiliates. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Author: Patrick J. Naughton
26 * naughton@wind.sun.com
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <X11/X.h>
34 #include <X11/Xos.h>
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37 #include <X11/keysym.h>
38 #include "xcolor.h"
40 #define DEFX 256
41 #define DEFY 256
42 #define DEFW 128
43 #define DEFH 128
45 #define ICON_WIDTH 48
46 #define ICON_HEIGHT 48
48 Display *dsp = NULL; /* current display (must be inited) */
49 static int screen;
50 static Visual *visual;
51 static Window win; /* window used to cover screen */
52 static GC gc; /* graphics context */
53 static int height = 0,
54 width = 0;
55 static Colormap cmap;
56 static Atom ATOM_WM_PROTOCOLS;
57 static Atom ATOM_WM_DELETE_WINDOW;
59 static int cmap_size;
61 #define error(...) do { fprintf(stderr, __VA_ARGS__) ; exit(1); } while (0)
63 static void
64 dumpCmap(void)
66 register int i;
67 XColor color[256];
69 for (i = 0; i < 256; i++)
70 color[i].pixel = i;
71 XQueryColors(dsp, DefaultColormap(dsp, screen), color, 256);
72 for (i = 0; i < 256; i++) {
73 printf("/name%02x %3d %3d %3d RGBcolor def\n",
74 i, color[i].red >> 8, color[i].green >> 8, color[i].blue >> 8);
78 static void
79 redisplay_indexed(Window window)
81 int max;
82 register int i,
84 register float fw;
85 register float fh;
87 switch (cmap_size) {
88 case 2: max = 2; break;
89 case 16: max = 4; break;
90 case 256: max = 16; break;
91 default: max = (int)sqrt((double)cmap_size);
94 if (window == win) {
95 fw = (float) width / (float)max;
96 fh = (float) height / (float)max;
97 } else {
98 fw = (float) ICON_WIDTH / (float)max;
99 fh = (float) ICON_HEIGHT / (float)max;
102 for (i = 0; i < max; i++) {
103 register int y = (int) (i * fh);
104 register int h = (i + 1) * fh - y;
105 for (j = 0; j < max; j++) {
106 register int x = (int) (j * fw);
107 register int w = (j + 1) * fw - x;
108 XSetForeground(dsp, gc, i * max + j);
109 XFillRectangle(dsp, window, gc, x, y, w, h);
114 static void
115 redisplay_direct(Window window)
117 register int i,
118 j, k;
119 register float fw;
120 register float fh;
121 static int roffset, goffset, boffset;
122 static int got_offsets = 0;
124 if (window == win) {
125 fw = (float) width / 16.0;
126 fh = (float) height / (3 * 16.0);
127 } else {
128 fw = (float) ICON_WIDTH / 16.0;
129 fh = (float) ICON_HEIGHT / (3 * 16.0);
132 if (!got_offsets) {
133 got_offsets = 1;
134 roffset = ffs(visual->red_mask)-1;
135 goffset = ffs(visual->green_mask)-1;
136 boffset = ffs(visual->blue_mask)-1;
139 /* red channel */
140 k = 0;
141 for (i = 0; i < 16; i++, k++) {
142 register int y = (int) (k * fh);
143 register int h = (k + 1) * fh - y;
144 for (j = 0; j < 16; j++) {
145 register int x = (int) (j * fw);
146 register int w = (j + 1) * fw - x;
147 XSetForeground(dsp, gc, (i * 16 + j)<<roffset);
148 XFillRectangle(dsp, window, gc, x, y, w, h);
152 /* green channel */
153 for (i = 0; i < 16; i++, k++) {
154 register int y = (int) (k * fh);
155 register int h = (k + 1) * fh - y;
156 for (j = 0; j < 16; j++) {
157 register int x = (int) (j * fw);
158 register int w = (j + 1) * fw - x;
159 XSetForeground(dsp, gc, (i * 16 + j)<<goffset);
160 XFillRectangle(dsp, window, gc, x, y, w, h);
164 /* blue channel */
165 for (i = 0; i < 16; i++, k++) {
166 register int y = (int) (k * fh);
167 register int h = (k + 1) * fh - y;
168 for (j = 0; j < 16; j++) {
169 register int x = (int) (j * fw);
170 register int w = (j + 1) * fw - x;
171 XSetForeground(dsp, gc, (i * 16 + j)<<boffset);
172 XFillRectangle(dsp, window, gc, x, y, w, h);
177 static void
178 redisplay (Window w)
180 if (visual->class == TrueColor || visual->class == DirectColor)
181 redisplay_direct(w);
182 else
183 redisplay_indexed(w);
186 static void
187 HandleEvents(void)
189 XEvent event;
190 XConfigureEvent *xce = (XConfigureEvent *) & event;
191 XKeyEvent *xke = (XKeyEvent *) & event;
193 while (1) {
194 XNextEvent(dsp, &event);
195 switch (event.type) {
196 case Expose:
197 redisplay(event.xexpose.window);
198 continue;
199 case GraphicsExpose:
200 case NoExpose:
201 case MapNotify:
202 case UnmapNotify:
203 continue;
204 case ConfigureNotify:
205 if ((height == xce->height) && (width == xce->width))
206 continue;
207 height = xce->height;
208 width = xce->width;
209 redisplay(event.xconfigure.window);
210 continue;
211 case KeyPress:
212 switch (XLookupKeysym(xke, 0)) {
213 case XK_Escape:
214 return;
215 default:
216 continue;
218 case ClientMessage:
219 if (event.xclient.message_type == ATOM_WM_PROTOCOLS &&
220 event.xclient.format == 32 &&
221 event.xclient.data.l[0] == ATOM_WM_DELETE_WINDOW)
223 return;
225 continue;
231 main(
232 int argc,
233 char *argv[]
236 XSetWindowAttributes xswa;
237 XGCValues xgcv;
238 XSizeHints size_hints;
239 XWMHints wmhints;
240 int i;
241 int geomStat;
242 char *geom = NULL;
243 char *displayName = NULL;
244 Status stat;
245 int dump = 0;
246 int nobw = 0;
247 int half = 0;
248 int install = 1;
249 int mask;
250 int useiconwin = 0;
251 const char *pname;
253 if (getenv("_SYNC")) {
254 extern int _Xdebug;
255 _Xdebug = 1;
258 pname = argv[0];
259 for (i = 1; i < argc; i++) {
260 if (!strcmp(argv[i], "-geometry"))
261 geom = argv[++i];
262 else if (!strcmp(argv[i], "-display"))
263 displayName = argv[++i];
264 else if (!strcmp(argv[i], "-dump"))
265 dump = 1;
266 else if (!strcmp(argv[i], "-nobw"))
267 nobw = 1;
268 else if (!strcmp(argv[i], "-half"))
269 half = 1;
270 else if (!strcmp(argv[i], "-noinst"))
271 install = 0;
272 else if (!strcmp(argv[i], "-iconwin"))
273 useiconwin = 1;
274 else
275 error("usage: %s [-display dpystr] [-geometry geomstr]\n"
276 "\t[-dump] [-nobw] [-half] [-noinst] [-iconwin]\n", pname);
279 if (!(dsp = XOpenDisplay(displayName))) {
280 error("%s: unable to open display, %s.\n", pname,
281 displayName ? displayName : "no display specified");
284 screen = DefaultScreen(dsp);
285 visual = DefaultVisualOfScreen(DefaultScreenOfDisplay(dsp));
286 cmap_size = visual->map_entries;
288 ATOM_WM_PROTOCOLS = XInternAtom(dsp, "WM_PROTOCOLS", False);
289 ATOM_WM_DELETE_WINDOW = XInternAtom(dsp, "WM_DELETE_WINDOW", False);
291 if (dump)
292 dumpCmap();
294 if (install) {
295 stat = XCreateHSBColormap(dsp, screen, &cmap, half
296 ? (cmap_size>>1) : cmap_size,
297 0.0, 1.0, 1.0,
298 1.0, 1.0, 1.0, !nobw, &visual);
299 if (stat != Success)
300 error("%s: unable to create colormap (%d).\n", pname, stat);
303 size_hints.x = DEFX;
304 size_hints.y = DEFY;
305 size_hints.width = DEFW;
306 size_hints.height = DEFH;
308 size_hints.flags = 0;
309 if (geom) {
310 geomStat = XParseGeometry(geom, &(size_hints.x),
311 &(size_hints.y),
312 (unsigned int *) &(size_hints.width),
313 (unsigned int *) &(size_hints.height));
314 if (geomStat & (XValue | YValue)) {
315 size_hints.flags |= USPosition;
317 if (geomStat & (WidthValue | HeightValue)) {
318 size_hints.flags |= USSize;
322 width = size_hints.width;
323 height = size_hints.height;
325 xswa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
326 mask = CWEventMask;
328 if (install) {
329 xswa.colormap = cmap;
330 mask |= CWColormap;
333 win = XCreateWindow(
334 dsp,
335 DefaultRootWindow(dsp),
336 size_hints.x,
337 size_hints.y,
338 size_hints.width,
339 size_hints.height,
340 1, /* borderwidth */
342 DefaultDepth(dsp, screen), /* depth */
343 InputOutput, /* class */
344 visual, /* visual */
345 mask, /* window attribute mask */
346 &xswa /* the attributes */
349 XSetStandardProperties(dsp, win, " XColor ", " XCOLOR ",
350 None, argv, argc, &size_hints);
352 wmhints.flags = InputHint;
353 wmhints.input = True;
355 if (useiconwin) {
356 Window icon;
358 xswa.event_mask = ExposureMask;
359 mask = CWEventMask;
361 if (install) {
362 xswa.colormap = cmap;
363 mask |= CWColormap;
366 icon = XCreateWindow(dsp, DefaultRootWindow(dsp),
367 0, 0, ICON_WIDTH, ICON_HEIGHT, 0, DefaultDepth(dsp, screen),
368 InputOutput, visual, mask, &xswa);
370 wmhints.flags |= IconWindowHint;
371 wmhints.icon_window = icon;
373 if (install) {
374 Window cmapwins[2];
376 cmapwins[0] = icon;
377 cmapwins[1] = win;
378 XSetWMColormapWindows(dsp, win, cmapwins, 2);
382 XSetWMHints(dsp, win, &wmhints);
383 XSetWMProtocols(dsp, win, &ATOM_WM_DELETE_WINDOW, 1);
385 xgcv.foreground = BlackPixel(dsp, screen);
386 xgcv.background = WhitePixel(dsp, screen);
387 gc = XCreateGC(dsp, win, GCForeground | GCBackground, &xgcv);
389 XMapWindow(dsp, win);
391 HandleEvents();
393 XUnmapWindow(dsp, win);
394 XDestroyWindow(dsp, win);
395 XFlush(dsp);
396 XCloseDisplay(dsp);
397 exit(0);