pulseaudio: fix dependencies for openssl-3
[oi-userland.git] / components / x11 / cmap_compact / src / cmcinit.c
blob847b7adb40ca4a237546d0b8d57522f84ca63abc
1 /*
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
13 * Software.
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.
26 #include <stdio.h>
27 #ifndef SYSV
28 #include <alloca.h>
29 #endif
30 #include <X11/Xlib.h>
31 #include <X11/Xatom.h>
32 #include "cmc.h"
33 #include "cmcutil.h"
35 #define MIN(a,b) ((a)<(b)?(a):(b))
38 static void
39 cmc_alloc (
40 Screen *screen,
41 int ncolors,
42 XColor *colors)
45 register Colormap cmap = DefaultColormapOfScreen(screen);
46 register Pixel *p;
47 register XColor *c;
48 register int nalloc;
49 register int total_alloc = 0;
50 register int ntotal;
51 unsigned long mask;
52 Pixel *pixels;
53 int nc;
55 ntotal = 1<<PlanesOfScreen(screen);
56 #ifdef SYSV
57 pixels = (Pixel *) malloc(ntotal * sizeof(Pixel));
58 #else
59 pixels = (Pixel *) alloca(ntotal * sizeof(Pixel));
60 #endif
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,
65 p, nalloc))
66 continue;
67 p += nalloc;
68 total_alloc += nalloc;
71 /*
72 ** May not be able to get as many colors as requested.
73 */
74 nc = MIN(ncolors, total_alloc);
75 if (nc != ncolors) {
76 (void) fprintf(stderr, "Warning: can only allocate %d of the requested %d colors.\n", nc, ncolors);
77 (void) fprintf(stderr, "Continuing anyway.\n");
79 ncolors = nc;
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);
93 /*
94 ** Tell everybody how many workspace colors there are.
95 ** Be sure to include white/black if they're at the
96 ** high end.
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)
102 ncolors++;
103 if (BlackPixelOfScreen(screen) == cmap_size-1 ||
104 BlackPixelOfScreen(screen) == cmap_size-2)
105 ncolors++;
106 prop_update(DisplayOfScreen(screen), RootWindowOfScreen(screen),
107 COUNT_PROP_NAME, XA_INTEGER, 32, ncolors, 1);
109 #ifdef SYSV
110 free(pixels);
111 #endif
115 static void
116 cmc_alloc_protected (
117 Screen *screen,
118 int ncolors,
119 XColor *colors)
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.
133 void
134 cmc_init (void)
137 Display *dpy;
138 FILE *f;
139 const char *filename;
141 /* Open display */
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
146 run synchronously */
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 */
153 return;
155 /* Check magic number and version */
156 cmc_header_test(f);
158 /* Abandon any previously allocated workspace colors (all screens) */
159 resource_discard(dpy);
161 /* For each screen of display ... */
162 for (;;) {
163 Screen *screen;
164 int scr_num;
165 int ncolors;
166 XColor *colors;
168 if (!cmc_read(f, &scr_num, &ncolors, &colors))
169 break;
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);
174 continue;
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
181 ** it still is.
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);
185 continue;
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);
196 XCloseDisplay(dpy);