]> Dogcows Code - chaz/openbox/blob - render/instance.c
add a check to make sure the color hash is empty on shutdown
[chaz/openbox] / render / instance.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 instance.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "render.h"
20 #include "instance.h"
21
22 static RrInstance *definst = NULL;
23
24 static void RrTrueColorSetup (RrInstance *inst);
25 static void RrPseudoColorSetup (RrInstance *inst);
26
27 #ifdef DEBUG
28 #include "color.h"
29 #endif
30 static void
31 dest(gpointer data)
32 {
33 #ifdef DEBUG
34 RrColor *c = data;
35 if (c->refcount > 0)
36 g_error("removing color from hash table with references");
37 #endif
38 }
39
40 RrInstance* RrInstanceNew (Display *display, gint screen)
41 {
42 definst = g_new (RrInstance, 1);
43 definst->display = display;
44 definst->screen = screen;
45
46 definst->depth = DefaultDepth(display, screen);
47 definst->visual = DefaultVisual(display, screen);
48 definst->colormap = DefaultColormap(display, screen);
49
50 definst->pseudo_colors = NULL;
51
52 definst->color_hash = g_hash_table_new_full(g_int_hash, g_int_equal,
53 NULL, dest);
54
55 switch (definst->visual->class) {
56 case TrueColor:
57 RrTrueColorSetup(definst);
58 break;
59 case PseudoColor:
60 case StaticColor:
61 case GrayScale:
62 case StaticGray:
63 RrPseudoColorSetup(definst);
64 break;
65 default:
66 g_critical("Unsupported visual class");
67 g_free (definst);
68 return definst = NULL;
69 }
70 return definst;
71 }
72
73 void RrTrueColorSetup (RrInstance *inst)
74 {
75 unsigned long red_mask, green_mask, blue_mask;
76 XImage *timage = NULL;
77
78 timage = XCreateImage(inst->display, inst->visual, inst->depth,
79 ZPixmap, 0, NULL, 1, 1, 32, 0);
80 g_assert(timage != NULL);
81 /* find the offsets for each color in the visual's masks */
82 inst->red_mask = red_mask = timage->red_mask;
83 inst->green_mask = green_mask = timage->green_mask;
84 inst->blue_mask = blue_mask = timage->blue_mask;
85
86 inst->red_offset = 0;
87 inst->green_offset = 0;
88 inst->blue_offset = 0;
89
90 while (! (red_mask & 1)) { inst->red_offset++; red_mask >>= 1; }
91 while (! (green_mask & 1)) { inst->green_offset++; green_mask >>= 1; }
92 while (! (blue_mask & 1)) { inst->blue_offset++; blue_mask >>= 1; }
93
94 inst->red_shift = inst->green_shift = inst->blue_shift = 8;
95 while (red_mask) { red_mask >>= 1; inst->red_shift--; }
96 while (green_mask) { green_mask >>= 1; inst->green_shift--; }
97 while (blue_mask) { blue_mask >>= 1; inst->blue_shift--; }
98 XFree(timage);
99 }
100
101 #define RrPseudoNcolors(isnt) (1 << (inst->pseudo_bpc * 3))
102
103 void RrPseudoColorSetup (RrInstance *inst)
104 {
105 XColor icolors[256];
106 int tr, tg, tb, n, r, g, b, i, incolors, ii;
107 unsigned long dev;
108 int cpc, _ncolors;
109
110 /* determine the number of colors and the bits-per-color */
111 inst->pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
112 g_assert(inst->pseudo_bpc >= 1);
113 _ncolors = RrPseudoNcolors(inst);
114
115 if (_ncolors > 1 << inst->depth) {
116 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
117 inst->pseudo_bpc = 1 << (inst->depth/3) >> 3;
118 _ncolors = 1 << (inst->pseudo_bpc * 3);
119 }
120
121 /* build a color cube */
122 inst->pseudo_colors = g_new(XColor, _ncolors);
123 cpc = 1 << inst->pseudo_bpc; /* colors per channel */
124
125 for (n = 0, r = 0; r < cpc; r++)
126 for (g = 0; g < cpc; g++)
127 for (b = 0; b < cpc; b++, n++) {
128 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
129 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
130 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
131 inst->pseudo_colors[n].red = tr | tr << 8;
132 inst->pseudo_colors[n].green = tg | tg << 8;
133 inst->pseudo_colors[n].blue = tb | tb << 8;
134 /* used to track allocation */
135 inst->pseudo_colors[n].flags = DoRed|DoGreen|DoBlue;
136 }
137
138 /* allocate the colors */
139 for (i = 0; i < _ncolors; i++)
140 if (!XAllocColor(inst->display, inst->colormap,
141 &inst->pseudo_colors[i]))
142 inst->pseudo_colors[i].flags = 0; /* mark it as unallocated */
143
144 /* try allocate any colors that failed allocation above */
145
146 /* get the allocated values from the X server
147 (only the first 256 XXX why!?)
148 */
149 incolors = (((1 << inst->depth) > 256) ? 256 : (1 << inst->depth));
150 for (i = 0; i < incolors; i++)
151 icolors[i].pixel = i;
152 XQueryColors(inst->display, inst->colormap, icolors, incolors);
153
154 /* try match unallocated ones */
155 for (i = 0; i < _ncolors; i++) {
156 if (!inst->pseudo_colors[i].flags) { /* if it wasn't allocated... */
157 unsigned long closest = 0xffffffff, close = 0;
158 for (ii = 0; ii < incolors; ii++) {
159 /* find deviations */
160 r = (inst->pseudo_colors[i].red - icolors[ii].red) & 0xff;
161 g = (inst->pseudo_colors[i].green - icolors[ii].green) & 0xff;
162 b = (inst->pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
163 /* find a weighted absolute deviation */
164 dev = (r * r) + (g * g) + (b * b);
165
166 if (dev < closest) {
167 closest = dev;
168 close = ii;
169 }
170 }
171
172 inst->pseudo_colors[i].red = icolors[close].red;
173 inst->pseudo_colors[i].green = icolors[close].green;
174 inst->pseudo_colors[i].blue = icolors[close].blue;
175 inst->pseudo_colors[i].pixel = icolors[close].pixel;
176
177 /* try alloc this closest color, it had better succeed! */
178 if (XAllocColor(inst->display, inst->colormap,
179 &inst->pseudo_colors[i]))
180 /* mark as alloced */
181 inst->pseudo_colors[i].flags = DoRed|DoGreen|DoBlue;
182 else
183 /* wtf has gone wrong, its already alloced for chissake! */
184 g_assert_not_reached();
185 }
186 }
187 }
188
189 void RrInstanceFree (RrInstance *inst)
190 {
191 if (inst) {
192 if (inst == definst) definst = NULL;
193 g_free(inst->pseudo_colors);
194 g_hash_table_destroy(inst->color_hash);
195 }
196 }
197
198 Display* RrDisplay (const RrInstance *inst)
199 {
200 return (inst ? inst : definst)->display;
201 }
202
203 gint RrScreen (const RrInstance *inst)
204 {
205 return (inst ? inst : definst)->screen;
206 }
207
208 Window RrRootWindow (const RrInstance *inst)
209 {
210 return RootWindow (RrDisplay (inst), RrScreen (inst));
211 }
212
213 Visual *RrVisual (const RrInstance *inst)
214 {
215 return (inst ? inst : definst)->visual;
216 }
217
218 gint RrDepth (const RrInstance *inst)
219 {
220 return (inst ? inst : definst)->depth;
221 }
222
223 Colormap RrColormap (const RrInstance *inst)
224 {
225 return (inst ? inst : definst)->colormap;
226 }
227
228 gint RrRedOffset (const RrInstance *inst)
229 {
230 return (inst ? inst : definst)->red_offset;
231 }
232
233 gint RrGreenOffset (const RrInstance *inst)
234 {
235 return (inst ? inst : definst)->green_offset;
236 }
237
238 gint RrBlueOffset (const RrInstance *inst)
239 {
240 return (inst ? inst : definst)->blue_offset;
241 }
242
243 gint RrRedShift (const RrInstance *inst)
244 {
245 return (inst ? inst : definst)->red_shift;
246 }
247
248 gint RrGreenShift (const RrInstance *inst)
249 {
250 return (inst ? inst : definst)->green_shift;
251 }
252
253 gint RrBlueShift (const RrInstance *inst)
254 {
255 return (inst ? inst : definst)->blue_shift;
256 }
257
258 gint RrRedMask (const RrInstance *inst)
259 {
260 return (inst ? inst : definst)->red_mask;
261 }
262
263 gint RrGreenMask (const RrInstance *inst)
264 {
265 return (inst ? inst : definst)->green_mask;
266 }
267
268 gint RrBlueMask (const RrInstance *inst)
269 {
270 return (inst ? inst : definst)->blue_mask;
271 }
272
273 guint RrPseudoBPC (const RrInstance *inst)
274 {
275 return (inst ? inst : definst)->pseudo_bpc;
276 }
277
278 XColor *RrPseudoColors (const RrInstance *inst)
279 {
280 return (inst ? inst : definst)->pseudo_colors;
281 }
282
283 GHashTable* RrColorHash (const RrInstance *inst)
284 {
285 return (inst ? inst : definst)->color_hash;
286 }
This page took 0.050446 seconds and 5 git commands to generate.