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