]> Dogcows Code - chaz/openbox/blob - render/render.c
9b721db30ada08708cbf54bd25a46eb92cbd02b2
[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 "theme.h"
11 #include "kernel/openbox.h"
12
13 #ifdef HAVE_STDLIB_H
14 # include <stdlib.h>
15 #endif
16
17 int render_depth;
18 Visual *render_visual;
19 Colormap render_colormap;
20 int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0;
21 int render_red_shift, render_green_shift, render_blue_shift;
22 int render_red_mask, render_green_mask, render_blue_mask;
23
24 void render_startup(void)
25 {
26 paint = x_paint;
27
28 render_depth = DefaultDepth(ob_display, ob_screen);
29 render_visual = DefaultVisual(ob_display, ob_screen);
30 render_colormap = DefaultColormap(ob_display, ob_screen);
31
32 if (render_depth < 8) {
33 XVisualInfo vinfo_template, *vinfo_return;
34 /* search for a TrueColor Visual... if we can't find one...
35 we will use the default visual for the screen */
36 int vinfo_nitems;
37 int best = -1;
38
39 vinfo_template.screen = ob_screen;
40 vinfo_template.class = TrueColor;
41 vinfo_return = XGetVisualInfo(ob_display,
42 VisualScreenMask | VisualClassMask,
43 &vinfo_template, &vinfo_nitems);
44 if (vinfo_return) {
45 int i;
46 int max_depth = 1;
47 for (i = 0; i < vinfo_nitems; ++i) {
48 if (vinfo_return[i].depth > max_depth) {
49 if (max_depth == 24 && vinfo_return[i].depth > 24)
50 break; /* prefer 24 bit over 32 */
51 max_depth = vinfo_return[i].depth;
52 best = i;
53 }
54 }
55 if (max_depth < render_depth) best = -1;
56 }
57 if (best != -1) {
58 render_depth = vinfo_return[best].depth;
59 render_visual = vinfo_return[best].visual;
60 render_colormap = XCreateColormap(ob_display, ob_root, render_visual,
61 AllocNone);
62 }
63 XFree(vinfo_return);
64 }
65 switch (render_visual->class) {
66 case TrueColor:
67 truecolor_startup();
68 break;
69 case PseudoColor:
70 case StaticColor:
71 case GrayScale:
72 case StaticGray:
73 pseudocolor_startup();
74 break;
75 default:
76 g_critical("unsupported visual class.\n");
77 exit(EXIT_FAILURE);
78
79 }
80 }
81
82 void truecolor_startup(void)
83 {
84 unsigned long red_mask, green_mask, blue_mask;
85 XImage *timage = NULL;
86
87 timage = XCreateImage(ob_display, render_visual, render_depth,
88 ZPixmap, 0, NULL, 1, 1, 32, 0);
89 g_assert(timage != NULL);
90 /* find the offsets for each color in the visual's masks */
91 render_red_mask = red_mask = timage->red_mask;
92 render_green_mask = green_mask = timage->green_mask;
93 render_blue_mask = blue_mask = timage->blue_mask;
94
95 render_red_offset = 0;
96 render_green_offset = 0;
97 render_blue_offset = 0;
98
99 while (! (red_mask & 1)) { render_red_offset++; red_mask >>= 1; }
100 while (! (green_mask & 1)) { render_green_offset++; green_mask >>= 1; }
101 while (! (blue_mask & 1)) { render_blue_offset++; blue_mask >>= 1; }
102
103 render_red_shift = render_green_shift = render_blue_shift = 8;
104 while (red_mask) { red_mask >>= 1; render_red_shift--; }
105 while (green_mask) { green_mask >>= 1; render_green_shift--; }
106 while (blue_mask) { blue_mask >>= 1; render_blue_shift--; }
107 XFree(timage);
108 }
109
110 void pseudocolor_startup(void)
111 {
112 XColor icolors[256];
113 int tr, tg, tb, n, r, g, b, i, incolors, ii;
114 unsigned long dev;
115 int cpc, _ncolors;
116 g_message("Initializing PseudoColor RenderControl\n");
117
118 /* determine the number of colors and the bits-per-color */
119 pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
120 g_assert(pseudo_bpc >= 1);
121 _ncolors = pseudo_ncolors();
122
123 if (_ncolors > 1 << render_depth) {
124 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
125 pseudo_bpc = 1 << (render_depth/3) >> 3;
126 _ncolors = 1 << (pseudo_bpc * 3);
127 }
128
129 /* build a color cube */
130 pseudo_colors = malloc(_ncolors * sizeof(XColor));
131 cpc = 1 << pseudo_bpc; /* colors per channel */
132
133 for (n = 0, r = 0; r < cpc; r++)
134 for (g = 0; g < cpc; g++)
135 for (b = 0; b < cpc; b++, n++) {
136 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
137 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
138 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
139 pseudo_colors[n].red = tr | tr << 8;
140 pseudo_colors[n].green = tg | tg << 8;
141 pseudo_colors[n].blue = tb | tb << 8;
142 pseudo_colors[n].flags = DoRed|DoGreen|DoBlue; /* used to track
143 allocation */
144 }
145
146 /* allocate the colors */
147 for (i = 0; i < _ncolors; i++)
148 if (!XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
149 pseudo_colors[i].flags = 0; /* mark it as unallocated */
150
151 /* try allocate any colors that failed allocation above */
152
153 /* get the allocated values from the X server (only the first 256 XXX why!?)
154 */
155 incolors = (((1 << render_depth) > 256) ? 256 : (1 << render_depth));
156 for (i = 0; i < incolors; i++)
157 icolors[i].pixel = i;
158 XQueryColors(ob_display, render_colormap, icolors, incolors);
159
160 /* try match unallocated ones */
161 for (i = 0; i < _ncolors; i++) {
162 if (!pseudo_colors[i].flags) { /* if it wasn't allocated... */
163 unsigned long closest = 0xffffffff, close = 0;
164 for (ii = 0; ii < incolors; ii++) {
165 /* find deviations */
166 r = (pseudo_colors[i].red - icolors[ii].red) & 0xff;
167 g = (pseudo_colors[i].green - icolors[ii].green) & 0xff;
168 b = (pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
169 /* find a weighted absolute deviation */
170 dev = (r * r) * (0xff - (icolors[ii].red & 0xff)) +
171 (g * g) * (0xff - (icolors[ii].green & 0xff)) +
172 (b * b) * (0xff - (icolors[ii].blue & 0xff));
173
174 if (dev < closest) {
175 closest = dev;
176 close = ii;
177 }
178 }
179
180 pseudo_colors[i].red = icolors[close].red;
181 pseudo_colors[i].green = icolors[close].green;
182 pseudo_colors[i].blue = icolors[close].blue;
183 pseudo_colors[i].pixel = icolors[close].pixel;
184
185 /* try alloc this closest color, it had better succeed! */
186 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
187 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
188 else
189 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
190 chissake! */
191 }
192 }
193 }
194
195 void x_paint(Window win, Appearance *l)
196 {
197 int i, transferred = 0, sw;
198 pixel32 *source, *dest;
199 Pixmap oldp;
200 int x = l->area.x;
201 int y = l->area.y;
202 int w = l->area.width;
203 int h = l->area.height;
204 Rect tarea; /* area in which to draw textures */
205
206 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
207
208 g_assert(l->surface.type == Surface_Planar);
209
210 oldp = l->pixmap; /* save to free after changing the visible pixmap */
211 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
212 g_assert(l->pixmap != None);
213
214 if (l->xftdraw != NULL)
215 XftDrawDestroy(l->xftdraw);
216 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
217 render_colormap);
218 g_assert(l->xftdraw != NULL);
219
220 g_free(l->surface.data.planar.pixel_data);
221 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
222
223
224 if (l->surface.data.planar.grad == Background_ParentRelative) {
225 sw = l->surface.data.planar.parent->area.width;
226 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
227 + l->surface.data.planar.parentx
228 + sw * l->surface.data.planar.parenty;
229 dest = l->surface.data.planar.pixel_data;
230 for (i = 0; i < h; i++, source += sw, dest += w) {
231 memcpy(dest, source, w * sizeof(pixel32));
232 }
233 }
234 else if (l->surface.data.planar.grad == Background_Solid)
235 gradient_solid(l, x, y, w, h);
236 else gradient_render(&l->surface, w, h);
237
238 for (i = 0; i < l->textures; i++) {
239 tarea = l->texture[i].position;
240 if (l->surface.data.planar.grad != Background_ParentRelative) {
241 if (l->surface.data.planar.relief != Flat) {
242 switch (l->surface.data.planar.bevel) {
243 case Bevel1:
244 tarea.x += 1; tarea.y += 1;
245 tarea.width -= 2; tarea.height -= 2;
246 break;
247 case Bevel2:
248 tarea.x += 2; tarea.y += 2;
249 tarea.width -= 4; tarea.height -= 4;
250 break;
251 }
252 } else if (l->surface.data.planar.border) {
253 tarea.x += 1; tarea.y += 1;
254 tarea.width -= 2; tarea.height -= 2;
255 }
256 }
257
258 switch (l->texture[i].type) {
259 case Text:
260 if (!transferred) {
261 transferred = 1;
262 if (l->surface.data.planar.grad != Background_Solid)
263 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
264 l->pixmap,x,y,w,h);
265 }
266 if (l->xftdraw == NULL) {
267 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
268 render_visual, render_colormap);
269 }
270 font_draw(l->xftdraw, &l->texture[i].data.text,
271 &tarea);
272 break;
273 case Bitmask:
274 if (!transferred) {
275 transferred = 1;
276 if (l->surface.data.planar.grad != Background_Solid)
277 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
278 l->pixmap,x,y,w,h);
279 }
280 if (l->texture[i].data.mask.color->gc == None)
281 color_allocate_gc(l->texture[i].data.mask.color);
282 mask_draw(l->pixmap, &l->texture[i].data.mask,
283 &tarea);
284 break;
285 case RGBA:
286 image_draw(l->surface.data.planar.pixel_data,
287 &l->texture[i].data.rgba,
288 &tarea, &l->area);
289 break;
290 }
291 }
292
293 if (!transferred) {
294 transferred = 1;
295 if (l->surface.data.planar.grad != Background_Solid)
296 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
297 ,x,y,w,h);
298 }
299
300
301 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
302 XClearWindow(ob_display, win);
303 if (oldp != None) XFreePixmap(ob_display, oldp);
304 }
305
306 /*
307 void gl_paint(Window win, Appearance *l)
308 {
309 glXMakeCurrent(ob_display, win, gl_context);
310 }
311 */
312
313 void render_shutdown(void)
314 {
315 }
316
317 Appearance *appearance_new(SurfaceType type, int numtex)
318 {
319 PlanarSurface *p;
320 Appearance *out;
321
322 out = g_new(Appearance, 1);
323 out->surface.type = type;
324 out->textures = numtex;
325 out->xftdraw = NULL;
326 if (numtex) out->texture = g_new0(Texture, numtex);
327 else out->texture = NULL;
328 out->pixmap = None;
329
330 switch (type) {
331 case Surface_Planar:
332 p = &out->surface.data.planar;
333 p->primary = NULL;
334 p->secondary = NULL;
335 p->border_color = NULL;
336 p->bevel_dark = NULL;
337 p->bevel_light = NULL;
338 p->pixel_data = NULL;
339 break;
340 }
341 return out;
342 }
343
344 Appearance *appearance_copy(Appearance *orig)
345 {
346 PlanarSurface *spo, *spc;
347 Appearance *copy = g_new(Appearance, 1);
348 copy->surface.type = orig->surface.type;
349 switch (orig->surface.type) {
350 case Surface_Planar:
351 spo = &(orig->surface.data.planar);
352 spc = &(copy->surface.data.planar);
353 spc->grad = spo->grad;
354 spc->relief = spo->relief;
355 spc->bevel = spo->bevel;
356 if (spo->primary != NULL)
357 spc->primary = color_new(spo->primary->r,
358 spo->primary->g,
359 spo->primary->b);
360 else spc->primary = NULL;
361
362 if (spo->secondary != NULL)
363 spc->secondary = color_new(spo->secondary->r,
364 spo->secondary->g,
365 spo->secondary->b);
366 else spc->secondary = NULL;
367
368 if (spo->border_color != NULL)
369 spc->border_color = color_new(spo->border_color->r,
370 spo->border_color->g,
371 spo->border_color->b);
372 else spc->border_color = NULL;
373
374 if (spo->bevel_dark != NULL)
375 spc->bevel_dark = color_new(spo->bevel_dark->r,
376 spo->bevel_dark->g,
377 spo->bevel_dark->b);
378 else spc->bevel_dark = NULL;
379
380 if (spo->bevel_light != NULL)
381 spc->bevel_light = color_new(spo->bevel_light->r,
382 spo->bevel_light->g,
383 spo->bevel_light->b);
384 else spc->bevel_light = NULL;
385
386 spc->interlaced = spo->interlaced;
387 spc->border = spo->border;
388 spc->pixel_data = NULL;
389 break;
390 }
391 copy->textures = orig->textures;
392 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
393 copy->pixmap = None;
394 copy->xftdraw = NULL;
395 return copy;
396 }
397
398 void appearance_free(Appearance *a)
399 {
400 if (a) {
401 PlanarSurface *p;
402 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
403 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
404 if (a->textures)
405 g_free(a->texture);
406 if (a->surface.type == Surface_Planar) {
407 p = &a->surface.data.planar;
408 color_free(p->primary);
409 color_free(p->secondary);
410 color_free(p->border_color);
411 color_free(p->bevel_dark);
412 color_free(p->bevel_light);
413 g_free(p->pixel_data);
414 }
415 g_free(a);
416 }
417 }
418
419
420 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
421 {
422 pixel32 *scratch;
423 XImage *im = NULL;
424 im = XCreateImage(ob_display, render_visual, render_depth,
425 ZPixmap, 0, NULL, w, h, 32, 0);
426 g_assert(im != NULL);
427 im->byte_order = render_endian;
428 /* this malloc is a complete waste of time on normal 32bpp
429 as reduce_depth just sets im->data = data and returns
430 */
431 scratch = g_new(pixel32, im->width * im->height);
432 im->data = (char*) scratch;
433 reduce_depth(in, im);
434 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
435 im, 0, 0, x, y, w, h);
436 im->data = NULL;
437 XDestroyImage(im);
438 g_free(scratch);
439 }
440
441 void appearance_minsize(Appearance *l, int *w, int *h)
442 {
443 int i;
444 int m;
445 *w = *h = 1;
446
447 switch (l->surface.type) {
448 case Surface_Planar:
449 if (l->surface.data.planar.relief != Flat) {
450 switch (l->surface.data.planar.bevel) {
451 case Bevel1:
452 *w = *h = 2;
453 break;
454 case Bevel2:
455 *w = *h = 4;
456 break;
457 }
458 } else if (l->surface.data.planar.border)
459 *w = *h = 2;
460
461 for (i = 0; i < l->textures; ++i) {
462 switch (l->texture[i].type) {
463 case Bitmask:
464 *w += l->texture[i].data.mask.mask->w;
465 *h += l->texture[i].data.mask.mask->h;
466 break;
467 case Text:
468 m = font_measure_string(l->texture[i].data.text.font,
469 l->texture[i].data.text.string,
470 l->texture[i].data.text.shadow,
471 l->texture[i].data.text.offset);
472 *w += m;
473 m = font_height(l->texture[i].data.text.font,
474 l->texture[i].data.text.shadow,
475 l->texture[i].data.text.offset);
476 *h += m;
477 break;
478 case RGBA:
479 *w += l->texture[i].data.rgba.width;
480 *h += l->texture[i].data.rgba.height;
481 break;
482 case NoTexture:
483 break;
484 }
485 }
486 break;
487 }
488 }
489
490 gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask,
491 int *w, int *h, pixel32 **data)
492 {
493 Window xr;
494 int xx, xy;
495 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
496 XImage *xi, *xm = NULL;
497
498 if (!XGetGeometry(ob_display, pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
499 return FALSE;
500 if (mask) {
501 if (!XGetGeometry(ob_display, mask, &xr, &xx, &xy, &mw, &mh, &xb, &xd))
502 return FALSE;
503 if (pw != mw || ph != mh || xd != 1)
504 return FALSE;
505 }
506
507 xi = XGetImage(ob_display, pmap, 0, 0, pw, ph, 0xffffffff, ZPixmap);
508 if (!xi)
509 return FALSE;
510
511 if (mask) {
512 xm = XGetImage(ob_display, mask, 0, 0, mw, mh, 0xffffffff, ZPixmap);
513 if (!xm)
514 return FALSE;
515 }
516
517 *data = g_new(pixel32, pw * ph);
518 increase_depth(*data, xi);
519
520 if (mask) {
521 /* apply transparency from the mask */
522 di = 0;
523 for (i = 0, y = 0; y < ph; ++y) {
524 for (x = 0; x < pw; ++x, ++i) {
525 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
526 (*data)[i] &= ~(0xff << default_alpha_offset);
527 }
528 di += xm->bytes_per_line;
529 }
530 }
531
532 *w = pw;
533 *h = ph;
534
535 return TRUE;
536 }
This page took 0.058534 seconds and 4 git commands to generate.