]> Dogcows Code - chaz/openbox/blob - render/instance.c
fullscreen overrides aspect hints (because i say so)
[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("color %d (%d,%d,%d) in hash table with %d "
37 "leftover references",
38 c->id, RrColorRed(c), RrColorGreen(c), RrColorBlue(c),
39 c->refcount);
40 #endif
41 }
42
43 #if 0
44 static void f(gpointer key, gpointer value, gpointer n)
45 {
46 RrColor *c = value;
47 if (c->id == *(gint*)n)
48 g_message("color %d has %d references", c->id, c->refcount);
49 }
50
51 void print_refs(gint id)
52 {
53 g_hash_table_foreach(RrColorHash(definst), f, &id);
54 }
55 #endif
56
57 RrInstance* RrInstanceNew (Display *display, gint screen)
58 {
59 definst = g_new (RrInstance, 1);
60 definst->display = display;
61 definst->screen = screen;
62
63 definst->depth = DefaultDepth(display, screen);
64 definst->visual = DefaultVisual(display, screen);
65 definst->colormap = DefaultColormap(display, screen);
66
67 definst->pseudo_colors = NULL;
68
69 definst->color_hash = g_hash_table_new_full(g_int_hash, g_int_equal,
70 NULL, dest);
71
72 switch (definst->visual->class) {
73 case TrueColor:
74 RrTrueColorSetup(definst);
75 break;
76 case PseudoColor:
77 case StaticColor:
78 case GrayScale:
79 case StaticGray:
80 RrPseudoColorSetup(definst);
81 break;
82 default:
83 g_critical("Unsupported visual class");
84 g_free (definst);
85 return definst = NULL;
86 }
87 return definst;
88 }
89
90 void RrTrueColorSetup (RrInstance *inst)
91 {
92 gulong red_mask, green_mask, blue_mask;
93 XImage *timage = NULL;
94
95 timage = XCreateImage(inst->display, inst->visual, inst->depth,
96 ZPixmap, 0, NULL, 1, 1, 32, 0);
97 g_assert(timage != NULL);
98 /* find the offsets for each color in the visual's masks */
99 inst->red_mask = red_mask = timage->red_mask;
100 inst->green_mask = green_mask = timage->green_mask;
101 inst->blue_mask = blue_mask = timage->blue_mask;
102
103 inst->red_offset = 0;
104 inst->green_offset = 0;
105 inst->blue_offset = 0;
106
107 while (! (red_mask & 1)) { inst->red_offset++; red_mask >>= 1; }
108 while (! (green_mask & 1)) { inst->green_offset++; green_mask >>= 1; }
109 while (! (blue_mask & 1)) { inst->blue_offset++; blue_mask >>= 1; }
110
111 inst->red_shift = inst->green_shift = inst->blue_shift = 8;
112 while (red_mask) { red_mask >>= 1; inst->red_shift--; }
113 while (green_mask) { green_mask >>= 1; inst->green_shift--; }
114 while (blue_mask) { blue_mask >>= 1; inst->blue_shift--; }
115 XFree(timage);
116 }
117
118 #define RrPseudoNcolors(isnt) (1 << (inst->pseudo_bpc * 3))
119
120 void RrPseudoColorSetup (RrInstance *inst)
121 {
122 XColor icolors[256];
123 gint tr, tg, tb, n, r, g, b, i, incolors, ii;
124 gulong dev;
125 gint cpc, _ncolors;
126
127 /* determine the number of colors and the bits-per-color */
128 inst->pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
129 g_assert(inst->pseudo_bpc >= 1);
130 _ncolors = RrPseudoNcolors(inst);
131
132 if (_ncolors > 1 << inst->depth) {
133 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
134 inst->pseudo_bpc = 1 << (inst->depth/3) >> 3;
135 _ncolors = 1 << (inst->pseudo_bpc * 3);
136 }
137
138 /* build a color cube */
139 inst->pseudo_colors = g_new(XColor, _ncolors);
140 cpc = 1 << inst->pseudo_bpc; /* colors per channel */
141
142 for (n = 0, r = 0; r < cpc; r++)
143 for (g = 0; g < cpc; g++)
144 for (b = 0; b < cpc; b++, n++) {
145 tr = (gint)(((gfloat)(r)/(gfloat)(cpc-1)) * 0xFF);
146 tg = (gint)(((gfloat)(g)/(gfloat)(cpc-1)) * 0xFF);
147 tb = (gint)(((gfloat)(b)/(gfloat)(cpc-1)) * 0xFF);
148 inst->pseudo_colors[n].red = tr | tr << 8;
149 inst->pseudo_colors[n].green = tg | tg << 8;
150 inst->pseudo_colors[n].blue = tb | tb << 8;
151 /* used to track allocation */
152 inst->pseudo_colors[n].flags = DoRed|DoGreen|DoBlue;
153 }
154
155 /* allocate the colors */
156 for (i = 0; i < _ncolors; i++)
157 if (!XAllocColor(inst->display, inst->colormap,
158 &inst->pseudo_colors[i]))
159 inst->pseudo_colors[i].flags = 0; /* mark it as unallocated */
160
161 /* try allocate any colors that failed allocation above */
162
163 /* get the allocated values from the X server
164 (only the first 256 XXX why!?)
165 */
166 incolors = (((1 << inst->depth) > 256) ? 256 : (1 << inst->depth));
167 for (i = 0; i < incolors; i++)
168 icolors[i].pixel = i;
169 XQueryColors(inst->display, inst->colormap, icolors, incolors);
170
171 /* try match unallocated ones */
172 for (i = 0; i < _ncolors; i++) {
173 if (!inst->pseudo_colors[i].flags) { /* if it wasn't allocated... */
174 gulong closest = 0xffffffff, close = 0;
175 for (ii = 0; ii < incolors; ii++) {
176 /* find deviations */
177 r = (inst->pseudo_colors[i].red - icolors[ii].red) & 0xff;
178 g = (inst->pseudo_colors[i].green - icolors[ii].green) & 0xff;
179 b = (inst->pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
180 /* find a weighted absolute deviation */
181 dev = (r * r) + (g * g) + (b * b);
182
183 if (dev < closest) {
184 closest = dev;
185 close = ii;
186 }
187 }
188
189 inst->pseudo_colors[i].red = icolors[close].red;
190 inst->pseudo_colors[i].green = icolors[close].green;
191 inst->pseudo_colors[i].blue = icolors[close].blue;
192 inst->pseudo_colors[i].pixel = icolors[close].pixel;
193
194 /* try alloc this closest color, it had better succeed! */
195 if (XAllocColor(inst->display, inst->colormap,
196 &inst->pseudo_colors[i]))
197 /* mark as alloced */
198 inst->pseudo_colors[i].flags = DoRed|DoGreen|DoBlue;
199 else
200 /* wtf has gone wrong, its already alloced for chissake! */
201 g_assert_not_reached();
202 }
203 }
204 }
205
206 void RrInstanceFree (RrInstance *inst)
207 {
208 if (inst) {
209 if (inst == definst) definst = NULL;
210 g_free(inst->pseudo_colors);
211 g_hash_table_destroy(inst->color_hash);
212 }
213 }
214
215 Display* RrDisplay (const RrInstance *inst)
216 {
217 return (inst ? inst : definst)->display;
218 }
219
220 gint RrScreen (const RrInstance *inst)
221 {
222 return (inst ? inst : definst)->screen;
223 }
224
225 Window RrRootWindow (const RrInstance *inst)
226 {
227 return RootWindow (RrDisplay (inst), RrScreen (inst));
228 }
229
230 Visual *RrVisual (const RrInstance *inst)
231 {
232 return (inst ? inst : definst)->visual;
233 }
234
235 gint RrDepth (const RrInstance *inst)
236 {
237 return (inst ? inst : definst)->depth;
238 }
239
240 Colormap RrColormap (const RrInstance *inst)
241 {
242 return (inst ? inst : definst)->colormap;
243 }
244
245 gint RrRedOffset (const RrInstance *inst)
246 {
247 return (inst ? inst : definst)->red_offset;
248 }
249
250 gint RrGreenOffset (const RrInstance *inst)
251 {
252 return (inst ? inst : definst)->green_offset;
253 }
254
255 gint RrBlueOffset (const RrInstance *inst)
256 {
257 return (inst ? inst : definst)->blue_offset;
258 }
259
260 gint RrRedShift (const RrInstance *inst)
261 {
262 return (inst ? inst : definst)->red_shift;
263 }
264
265 gint RrGreenShift (const RrInstance *inst)
266 {
267 return (inst ? inst : definst)->green_shift;
268 }
269
270 gint RrBlueShift (const RrInstance *inst)
271 {
272 return (inst ? inst : definst)->blue_shift;
273 }
274
275 gint RrRedMask (const RrInstance *inst)
276 {
277 return (inst ? inst : definst)->red_mask;
278 }
279
280 gint RrGreenMask (const RrInstance *inst)
281 {
282 return (inst ? inst : definst)->green_mask;
283 }
284
285 gint RrBlueMask (const RrInstance *inst)
286 {
287 return (inst ? inst : definst)->blue_mask;
288 }
289
290 guint RrPseudoBPC (const RrInstance *inst)
291 {
292 return (inst ? inst : definst)->pseudo_bpc;
293 }
294
295 XColor *RrPseudoColors (const RrInstance *inst)
296 {
297 return (inst ? inst : definst)->pseudo_colors;
298 }
299
300 GHashTable* RrColorHash (const RrInstance *inst)
301 {
302 return (inst ? inst : definst)->color_hash;
303 }
This page took 0.054071 seconds and 4 git commands to generate.