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