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