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