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
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
36 #include <X11/Xutil.h>
37 #include <X11/keysym.h>
46 #define ICON_HEIGHT 48
48 Display
*dsp
= NULL
; /* current display (must be inited) */
50 static Visual
*visual
;
51 static Window win
; /* window used to cover screen */
52 static GC gc
; /* graphics context */
53 static int height
= 0,
56 static Atom ATOM_WM_PROTOCOLS
;
57 static Atom ATOM_WM_DELETE_WINDOW
;
61 #define error(...) do { fprintf(stderr, __VA_ARGS__) ; exit(1); } while (0)
69 for (i
= 0; i
< 256; 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);
79 redisplay_indexed(Window window
)
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
);
95 fw
= (float) width
/ (float)max
;
96 fh
= (float) height
/ (float)max
;
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
);
115 redisplay_direct(Window window
)
121 static int roffset
, goffset
, boffset
;
122 static int got_offsets
= 0;
125 fw
= (float) width
/ 16.0;
126 fh
= (float) height
/ (3 * 16.0);
128 fw
= (float) ICON_WIDTH
/ 16.0;
129 fh
= (float) ICON_HEIGHT
/ (3 * 16.0);
134 roffset
= ffs(visual
->red_mask
)-1;
135 goffset
= ffs(visual
->green_mask
)-1;
136 boffset
= ffs(visual
->blue_mask
)-1;
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
);
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
);
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
);
180 if (visual
->class == TrueColor
|| visual
->class == DirectColor
)
183 redisplay_indexed(w
);
190 XConfigureEvent
*xce
= (XConfigureEvent
*) & event
;
191 XKeyEvent
*xke
= (XKeyEvent
*) & event
;
194 XNextEvent(dsp
, &event
);
195 switch (event
.type
) {
197 redisplay(event
.xexpose
.window
);
204 case ConfigureNotify
:
205 if ((height
== xce
->height
) && (width
== xce
->width
))
207 height
= xce
->height
;
209 redisplay(event
.xconfigure
.window
);
212 switch (XLookupKeysym(xke
, 0)) {
219 if (event
.xclient
.message_type
== ATOM_WM_PROTOCOLS
&&
220 event
.xclient
.format
== 32 &&
221 event
.xclient
.data
.l
[0] == ATOM_WM_DELETE_WINDOW
)
236 XSetWindowAttributes xswa
;
238 XSizeHints size_hints
;
243 char *displayName
= NULL
;
253 if (getenv("_SYNC")) {
259 for (i
= 1; i
< argc
; i
++) {
260 if (!strcmp(argv
[i
], "-geometry"))
262 else if (!strcmp(argv
[i
], "-display"))
263 displayName
= argv
[++i
];
264 else if (!strcmp(argv
[i
], "-dump"))
266 else if (!strcmp(argv
[i
], "-nobw"))
268 else if (!strcmp(argv
[i
], "-half"))
270 else if (!strcmp(argv
[i
], "-noinst"))
272 else if (!strcmp(argv
[i
], "-iconwin"))
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
);
295 stat
= XCreateHSBColormap(dsp
, screen
, &cmap
, half
296 ? (cmap_size
>>1) : cmap_size
,
298 1.0, 1.0, 1.0, !nobw
, &visual
);
300 error("%s: unable to create colormap (%d).\n", pname
, stat
);
305 size_hints
.width
= DEFW
;
306 size_hints
.height
= DEFH
;
308 size_hints
.flags
= 0;
310 geomStat
= XParseGeometry(geom
, &(size_hints
.x
),
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
;
329 xswa
.colormap
= cmap
;
335 DefaultRootWindow(dsp
),
342 DefaultDepth(dsp
, screen
), /* depth */
343 InputOutput
, /* class */
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
;
358 xswa
.event_mask
= ExposureMask
;
362 xswa
.colormap
= cmap
;
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
;
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
);
393 XUnmapWindow(dsp
, win
);
394 XDestroyWindow(dsp
, win
);