2 * Copyright (c) 1990, 2015, Oracle and/or its affiliates. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
31 #include <X11/Xatom.h>
35 #define MIN(a,b) ((a)<(b)?(a):(b))
45 register Colormap cmap
= DefaultColormapOfScreen(screen
);
49 register int total_alloc
= 0;
55 ntotal
= 1<<PlanesOfScreen(screen
);
57 pixels
= (Pixel
*) malloc(ntotal
* sizeof(Pixel
));
59 pixels
= (Pixel
*) alloca(ntotal
* sizeof(Pixel
));
62 /* Grab as many private cells as we can */
63 for (nalloc
=ntotal
, p
=pixels
; nalloc
> 0; nalloc
>>=1) {
64 if (!XAllocColorCells(DisplayOfScreen(screen
), cmap
, 0, &mask
, 0,
68 total_alloc
+= nalloc
;
72 ** May not be able to get as many colors as requested.
74 nc
= MIN(ncolors
, total_alloc
);
76 (void) fprintf(stderr
, "Warning: can only allocate %d of the requested %d colors.\n", nc
, ncolors
);
77 (void) fprintf(stderr
, "Continuing anyway.\n");
81 /* Free enough space for workspace colors */
82 nalloc
= total_alloc
- ncolors
;
83 XFreeColors(DisplayOfScreen(screen
), cmap
, pixels
+nalloc
, ncolors
, 0);
85 /* Allocate the workspace colors */
86 for (c
=colors
+ncolors
-1; c
>=colors
; c
--)
87 if (!XAllocColor(DisplayOfScreen(screen
), cmap
, c
))
88 fatal_error("attempt to allocate color failed");
90 /* Free placeholder pixels */
91 XFreeColors(DisplayOfScreen(screen
), cmap
, pixels
, nalloc
, 0);
94 ** Tell everybody how many workspace colors there are.
95 ** Be sure to include white/black if they're at the
98 { Visual
*vis
= DefaultVisualOfScreen(screen
);
99 int cmap_size
= vis
->map_entries
;
100 if (WhitePixelOfScreen(screen
) == cmap_size
-1 ||
101 WhitePixelOfScreen(screen
) == cmap_size
-2)
103 if (BlackPixelOfScreen(screen
) == cmap_size
-1 ||
104 BlackPixelOfScreen(screen
) == cmap_size
-2)
106 prop_update(DisplayOfScreen(screen
), RootWindowOfScreen(screen
),
107 COUNT_PROP_NAME
, XA_INTEGER
, 32, ncolors
, 1);
116 cmc_alloc_protected (
121 XGrabServer(DisplayOfScreen(screen
));
122 cmc_alloc(screen
, ncolors
, colors
);
123 XUngrabServer(DisplayOfScreen(screen
));
128 ** For each screen for which there is saved workspace colors,
129 ** allocates these colors as high as possible in the default
130 ** colormap of the screen.
139 const char *filename
;
142 if (!(dpy
= open_display(display_name
)))
143 fatal_error("cannot open display '%s'\n", display_name
);
145 /* For some strange reason, cmc_alloc_protected fails if not
147 XSynchronize(dpy
, 1);
149 /* Open file where workspace colors are stored */
150 filename
= comp_colors_filename(basename_arg
);
151 if ((f
= fopen(filename
, "r")) == NULL
)
152 /* Do nothing if not found */
155 /* Check magic number and version */
158 /* Abandon any previously allocated workspace colors (all screens) */
159 resource_discard(dpy
);
161 /* For each screen of display ... */
168 if (!cmc_read(f
, &scr_num
, &ncolors
, &colors
))
171 /* See if screen is still present */
172 if (scr_num
>= ScreenCount(dpy
)) {
173 warning("Warning: cannot allocated saved colors for screen %d because\nthe display no longer has this many screens\n", scr_num
);
176 screen
= ScreenOfDisplay(dpy
, scr_num
);
179 ** Existence of workspace colors for the screen implies
180 ** that its default visual was dynamic indexed. Make sure
183 if (!dynamic_indexed_default_visual(screen
)) {
184 warning("default visual for screen %d is no longer dynamic indexed\nsaved colors not allocated for screen %d", scr_num
, scr_num
);
188 /* Allocate workspace colors at high end of colormap */
189 cmc_alloc_protected(screen
, ncolors
, colors
);
190 free((char *)colors
);
193 /* Preserve newly allocated client resources */
194 resource_preserve(dpy
);