]> Dogcows Code - chaz/openbox/blob - render/render.c
add function to give the minimum size of an appearance to fully render it's data
[chaz/openbox] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3 #include <glib.h>
4 #include "render.h"
5 #include "gradient.h"
6 #include "font.h"
7 #include "mask.h"
8 #include "color.h"
9 #include "image.h"
10 #include "kernel/openbox.h"
11
12 int render_depth;
13 Visual *render_visual;
14 Colormap render_colormap;
15 int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0;
16 int render_red_shift, render_green_shift, render_blue_shift;
17
18 void render_startup(void)
19 {
20 paint = x_paint;
21
22 render_depth = DefaultDepth(ob_display, ob_screen);
23 render_visual = DefaultVisual(ob_display, ob_screen);
24 render_colormap = DefaultColormap(ob_display, ob_screen);
25
26 if (render_depth < 8) {
27 XVisualInfo vinfo_template, *vinfo_return;
28 /* search for a TrueColor Visual... if we can't find one...
29 we will use the default visual for the screen */
30 int vinfo_nitems;
31 int best = -1;
32
33 vinfo_template.screen = ob_screen;
34 vinfo_template.class = TrueColor;
35 vinfo_return = XGetVisualInfo(ob_display,
36 VisualScreenMask | VisualClassMask,
37 &vinfo_template, &vinfo_nitems);
38 if (vinfo_return) {
39 int i;
40 int max_depth = 1;
41 for (i = 0; i < vinfo_nitems; ++i) {
42 if (vinfo_return[i].depth > max_depth) {
43 if (max_depth == 24 && vinfo_return[i].depth > 24)
44 break; /* prefer 24 bit over 32 */
45 max_depth = vinfo_return[i].depth;
46 best = i;
47 }
48 }
49 if (max_depth < render_depth) best = -1;
50 }
51 if (best != -1) {
52 render_depth = vinfo_return[best].depth;
53 render_visual = vinfo_return[best].visual;
54 render_colormap = XCreateColormap(ob_display, ob_root, render_visual,
55 AllocNone);
56 }
57 XFree(vinfo_return);
58 }
59 switch (render_visual->class) {
60 case TrueColor:
61 truecolor_startup();
62 break;
63 case PseudoColor:
64 case StaticColor:
65 case GrayScale:
66 case StaticGray:
67 pseudocolor_startup();
68 break;
69 default:
70 g_critical("unsupported visual class.\n");
71 exit(EXIT_FAILURE);
72
73 }
74 }
75
76 void truecolor_startup(void)
77 {
78 unsigned long red_mask, green_mask, blue_mask;
79 XImage *timage = NULL;
80
81 timage = XCreateImage(ob_display, render_visual, render_depth,
82 ZPixmap, 0, NULL, 1, 1, 32, 0);
83 g_assert(timage != NULL);
84 /* find the offsets for each color in the visual's masks */
85 red_mask = timage->red_mask;
86 green_mask = timage->green_mask;
87 blue_mask = timage->blue_mask;
88
89 render_red_offset = 0;
90 render_green_offset = 0;
91 render_blue_offset = 0;
92
93 while (! (red_mask & 1)) { render_red_offset++; red_mask >>= 1; }
94 while (! (green_mask & 1)) { render_green_offset++; green_mask >>= 1; }
95 while (! (blue_mask & 1)) { render_blue_offset++; blue_mask >>= 1; }
96
97 render_red_shift = render_green_shift = render_blue_shift = 8;
98 while (red_mask) { red_mask >>= 1; render_red_shift--; }
99 while (green_mask) { green_mask >>= 1; render_green_shift--; }
100 while (blue_mask) { blue_mask >>= 1; render_blue_shift--; }
101 XFree(timage);
102 }
103
104 void pseudocolor_startup(void)
105 {
106 XColor icolors[256];
107 int tr, tg, tb, n, r, g, b, i, incolors, ii;
108 unsigned long dev;
109 int cpc, _ncolors;
110 g_message("Initializing PseudoColor RenderControl\n");
111
112 /* determine the number of colors and the bits-per-color */
113 pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
114 g_assert(pseudo_bpc >= 1);
115 _ncolors = 1 << (pseudo_bpc * 3);
116
117 if (_ncolors > 1 << render_depth) {
118 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
119 pseudo_bpc = 1 << (render_depth/3) >> 3;
120 _ncolors = 1 << (pseudo_bpc * 3);
121 }
122
123 /* build a color cube */
124 pseudo_colors = malloc(_ncolors * sizeof(XColor));
125 cpc = 1 << pseudo_bpc; /* colors per channel */
126
127 for (n = 0, r = 0; r < cpc; r++)
128 for (g = 0; g < cpc; g++)
129 for (b = 0; b < cpc; b++, n++) {
130 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
131 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
132 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
133 pseudo_colors[n].red = tr | tr << 8;
134 pseudo_colors[n].green = tg | tg << 8;
135 pseudo_colors[n].blue = tb | tb << 8;
136 pseudo_colors[n].flags = DoRed|DoGreen|DoBlue; /* used to track
137 allocation */
138 }
139
140 /* allocate the colors */
141 for (i = 0; i < _ncolors; i++)
142 if (!XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
143 pseudo_colors[i].flags = 0; /* mark it as unallocated */
144
145 /* try allocate any colors that failed allocation above */
146
147 /* get the allocated values from the X server (only the first 256 XXX why!?)
148 */
149 incolors = (((1 << render_depth) > 256) ? 256 : (1 << render_depth));
150 for (i = 0; i < incolors; i++)
151 icolors[i].pixel = i;
152 XQueryColors(ob_display, render_colormap, icolors, incolors);
153
154 /* try match unallocated ones */
155 for (i = 0; i < _ncolors; i++) {
156 if (!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 = (pseudo_colors[i].red - icolors[ii].red) & 0xff;
161 g = (pseudo_colors[i].green - icolors[ii].green) & 0xff;
162 b = (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 pseudo_colors[i].red = icolors[close].red;
173 pseudo_colors[i].green = icolors[close].green;
174 pseudo_colors[i].blue = icolors[close].blue;
175 pseudo_colors[i].pixel = icolors[close].pixel;
176
177 /* try alloc this closest color, it had better succeed! */
178 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
179 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
180 else
181 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
182 chissake! */
183 }
184 }
185 }
186
187 void x_paint(Window win, Appearance *l)
188 {
189 int i, transferred = 0, sw;
190 pixel32 *source, *dest;
191 Pixmap oldp;
192 int x = l->area.x;
193 int y = l->area.y;
194 int w = l->area.width;
195 int h = l->area.height;
196
197 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
198
199 g_assert(l->surface.type == Surface_Planar);
200
201 oldp = l->pixmap; /* save to free after changing the visible pixmap */
202 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
203 g_assert(l->pixmap != None);
204
205 if (l->xftdraw != NULL)
206 XftDrawDestroy(l->xftdraw);
207 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
208 render_colormap);
209 g_assert(l->xftdraw != NULL);
210
211 g_free(l->surface.data.planar.pixel_data);
212 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
213
214
215 if (l->surface.data.planar.grad == Background_ParentRelative) {
216 sw = l->surface.data.planar.parent->area.width;
217 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
218 + l->surface.data.planar.parentx
219 + sw * l->surface.data.planar.parenty;
220 dest = l->surface.data.planar.pixel_data;
221 for (i = 0; i < h; i++, source += sw, dest += w) {
222 memcpy(dest, source, w * sizeof(pixel32));
223 }
224 }
225 else if (l->surface.data.planar.grad == Background_Solid)
226 gradient_solid(l, x, y, w, h);
227 else gradient_render(&l->surface, w, h);
228
229 for (i = 0; i < l->textures; i++) {
230 switch (l->texture[i].type) {
231 case Text:
232 if (!transferred) {
233 transferred = 1;
234 if (l->surface.data.planar.grad != Background_Solid)
235 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
236 l->pixmap,x,y,w,h);
237 }
238 if (l->xftdraw == NULL) {
239 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
240 render_visual, render_colormap);
241 }
242 font_draw(l->xftdraw, &l->texture[i].data.text,
243 &l->texture[i].position);
244 break;
245 case Bitmask:
246 if (!transferred) {
247 transferred = 1;
248 if (l->surface.data.planar.grad != Background_Solid)
249 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
250 l->pixmap,x,y,w,h);
251 }
252 if (l->texture[i].data.mask.color->gc == None)
253 color_allocate_gc(l->texture[i].data.mask.color);
254 mask_draw(l->pixmap, &l->texture[i].data.mask,
255 &l->texture[i].position);
256 break;
257 case RGBA:
258 image_draw(l->surface.data.planar.pixel_data,
259 &l->texture[i].data.rgba,
260 &l->texture[i].position);
261 break;
262 }
263 }
264
265 if (!transferred) {
266 transferred = 1;
267 if (l->surface.data.planar.grad != Background_Solid)
268 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
269 ,x,y,w,h);
270 }
271
272
273 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
274 XClearWindow(ob_display, win);
275 if (oldp != None) XFreePixmap(ob_display, oldp);
276 }
277
278 /*
279 void gl_paint(Window win, Appearance *l)
280 {
281 glXMakeCurrent(ob_display, win, gl_context);
282 }
283 */
284
285 void render_shutdown(void)
286 {
287 }
288
289 Appearance *appearance_new(SurfaceType type, int numtex)
290 {
291 PlanarSurface *p;
292 Appearance *out;
293
294 out = g_new(Appearance, 1);
295 out->surface.type = type;
296 out->textures = numtex;
297 out->xftdraw = NULL;
298 if (numtex) out->texture = g_new0(Texture, numtex);
299 else out->texture = NULL;
300 out->pixmap = None;
301
302 switch (type) {
303 case Surface_Planar:
304 p = &out->surface.data.planar;
305 p->primary = NULL;
306 p->secondary = NULL;
307 p->border_color = NULL;
308 p->pixel_data = NULL;
309 break;
310 }
311 return out;
312 }
313
314 Appearance *appearance_copy(Appearance *orig)
315 {
316 PlanarSurface *spo, *spc;
317 Appearance *copy = g_new(Appearance, 1);
318 copy->surface.type = orig->surface.type;
319 switch (orig->surface.type) {
320 case Surface_Planar:
321 spo = &(orig->surface.data.planar);
322 spc = &(copy->surface.data.planar);
323 spc->grad = spo->grad;
324 spc->relief = spo->relief;
325 spc->bevel = spo->bevel;
326 if (spo->primary != NULL)
327 spc->primary = color_new(spo->primary->r,
328 spo->primary->g,
329 spo->primary->b);
330 else spc->primary = NULL;
331
332 if (spo->secondary != NULL)
333 spc->secondary = color_new(spo->secondary->r,
334 spo->secondary->g,
335 spo->secondary->b);
336 else spc->secondary = NULL;
337
338 if (spo->border_color != NULL)
339 spc->border_color = color_new(spo->border_color->r,
340 spo->border_color->g,
341 spo->border_color->b);
342 else spc->border_color = NULL;
343
344 spc->interlaced = spo->interlaced;
345 spc->border = spo->border;
346 spc->pixel_data = NULL;
347 break;
348 }
349 copy->textures = orig->textures;
350 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
351 copy->pixmap = None;
352 copy->xftdraw = NULL;
353 return copy;
354 }
355
356 void appearance_free(Appearance *a)
357 {
358 PlanarSurface *p;
359 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
360 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
361 if (a->textures)
362 g_free(a->texture);
363 if (a->surface.type == Surface_Planar) {
364 p = &a->surface.data.planar;
365 if (p->primary != NULL) color_free(p->primary);
366 if (p->secondary != NULL) color_free(p->secondary);
367 if (p->border_color != NULL) color_free(p->border_color);
368 if (p->pixel_data != NULL) g_free(p->pixel_data);
369 }
370 g_free(a);
371 }
372
373
374 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
375 {
376 XImage *im = NULL;
377 im = XCreateImage(ob_display, render_visual, render_depth,
378 ZPixmap, 0, NULL, w, h, 32, 0);
379 g_assert(im != NULL);
380 im->byte_order = endian;
381 im->data = (char *)in;
382 reduce_depth((pixel32*)im->data, im);
383 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
384 im, 0, 0, x, y, w, h);
385 im->data = NULL;
386 XDestroyImage(im);
387 }
388
389 void appearance_minsize(Appearance *l, Size *s)
390 {
391 int i;
392 SIZE_SET(*s, 0, 0);
393
394 switch (l->surface.type) {
395 case Surface_Planar:
396 if (l->surface.data.planar.border ||
397 l->surface.data.planar.bevel == Bevel1)
398 SIZE_SET(*s, 2, 2);
399 else if (l->surface.data.planar.bevel == Bevel2)
400 SIZE_SET(*s, 4, 4);
401
402 for (i = 0; i < l->textures; ++i)
403 switch (l->texture[i].type) {
404 case Bitmask:
405 s->width += l->texture[i].data.mask.mask->w;
406 s->height += l->texture[i].data.mask.mask->h;
407 break;
408 case Text:
409 s->width +=font_measure_string(l->texture[i].data.text.font,
410 l->texture[i].data.text.string,
411 l->texture[i].data.text.shadow,
412 l->texture[i].data.text.offset);
413 s->height += font_height(l->texture[i].data.text.font,
414 l->texture[i].data.text.shadow,
415 l->texture[i].data.text.offset);
416 break;
417 case RGBA:
418 s->width += l->texture[i].data.rgba.width;
419 s->height += l->texture[i].data.rgba.height;
420 break;
421 case NoTexture:
422 break;
423 }
424 break;
425 }
426 return s;
427 }
This page took 0.059506 seconds and 5 git commands to generate.