]> Dogcows Code - chaz/openbox/blob - render/render.c
put the devation back how it used to be, it aws definately no better..
[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) + (g * g) + (b * b);
171
172 if (dev < closest) {
173 closest = dev;
174 close = ii;
175 }
176 }
177
178 pseudo_colors[i].red = icolors[close].red;
179 pseudo_colors[i].green = icolors[close].green;
180 pseudo_colors[i].blue = icolors[close].blue;
181 pseudo_colors[i].pixel = icolors[close].pixel;
182
183 /* try alloc this closest color, it had better succeed! */
184 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
185 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
186 else
187 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
188 chissake! */
189 }
190 }
191 }
192
193 void x_paint(Window win, Appearance *l)
194 {
195 int i, transferred = 0, sw;
196 pixel32 *source, *dest;
197 Pixmap oldp;
198 int x = l->area.x;
199 int y = l->area.y;
200 int w = l->area.width;
201 int h = l->area.height;
202 Rect tarea; /* area in which to draw textures */
203
204 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
205
206 g_assert(l->surface.type == Surface_Planar);
207
208 oldp = l->pixmap; /* save to free after changing the visible pixmap */
209 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
210 g_assert(l->pixmap != None);
211
212 if (l->xftdraw != NULL)
213 XftDrawDestroy(l->xftdraw);
214 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
215 render_colormap);
216 g_assert(l->xftdraw != NULL);
217
218 g_free(l->surface.data.planar.pixel_data);
219 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
220
221
222 if (l->surface.data.planar.grad == Background_ParentRelative) {
223 sw = l->surface.data.planar.parent->area.width;
224 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
225 + l->surface.data.planar.parentx
226 + sw * l->surface.data.planar.parenty;
227 dest = l->surface.data.planar.pixel_data;
228 for (i = 0; i < h; i++, source += sw, dest += w) {
229 memcpy(dest, source, w * sizeof(pixel32));
230 }
231 }
232 else if (l->surface.data.planar.grad == Background_Solid)
233 gradient_solid(l, x, y, w, h);
234 else gradient_render(&l->surface, w, h);
235
236 for (i = 0; i < l->textures; i++) {
237 tarea = l->texture[i].position;
238 if (l->surface.data.planar.grad != Background_ParentRelative) {
239 if (l->surface.data.planar.relief != Flat) {
240 switch (l->surface.data.planar.bevel) {
241 case Bevel1:
242 tarea.x += 1; tarea.y += 1;
243 tarea.width -= 2; tarea.height -= 2;
244 break;
245 case Bevel2:
246 tarea.x += 2; tarea.y += 2;
247 tarea.width -= 4; tarea.height -= 4;
248 break;
249 }
250 } else if (l->surface.data.planar.border) {
251 tarea.x += 1; tarea.y += 1;
252 tarea.width -= 2; tarea.height -= 2;
253 }
254 }
255
256 switch (l->texture[i].type) {
257 case Text:
258 if (!transferred) {
259 transferred = 1;
260 if (l->surface.data.planar.grad != Background_Solid)
261 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
262 l->pixmap,x,y,w,h);
263 }
264 if (l->xftdraw == NULL) {
265 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
266 render_visual, render_colormap);
267 }
268 font_draw(l->xftdraw, &l->texture[i].data.text,
269 &tarea);
270 break;
271 case Bitmask:
272 if (!transferred) {
273 transferred = 1;
274 if (l->surface.data.planar.grad != Background_Solid)
275 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
276 l->pixmap,x,y,w,h);
277 }
278 if (l->texture[i].data.mask.color->gc == None)
279 color_allocate_gc(l->texture[i].data.mask.color);
280 mask_draw(l->pixmap, &l->texture[i].data.mask,
281 &tarea);
282 break;
283 case RGBA:
284 image_draw(l->surface.data.planar.pixel_data,
285 &l->texture[i].data.rgba,
286 &tarea, &l->area);
287 break;
288 }
289 }
290
291 if (!transferred) {
292 transferred = 1;
293 if (l->surface.data.planar.grad != Background_Solid)
294 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
295 ,x,y,w,h);
296 }
297
298
299 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
300 XClearWindow(ob_display, win);
301 if (oldp != None) XFreePixmap(ob_display, oldp);
302 }
303
304 /*
305 void gl_paint(Window win, Appearance *l)
306 {
307 glXMakeCurrent(ob_display, win, gl_context);
308 }
309 */
310
311 void render_shutdown(void)
312 {
313 }
314
315 Appearance *appearance_new(SurfaceType type, int numtex)
316 {
317 PlanarSurface *p;
318 Appearance *out;
319
320 out = g_new(Appearance, 1);
321 out->surface.type = type;
322 out->textures = numtex;
323 out->xftdraw = NULL;
324 if (numtex) out->texture = g_new0(Texture, numtex);
325 else out->texture = NULL;
326 out->pixmap = None;
327
328 switch (type) {
329 case Surface_Planar:
330 p = &out->surface.data.planar;
331 p->primary = NULL;
332 p->secondary = NULL;
333 p->border_color = NULL;
334 p->bevel_dark = NULL;
335 p->bevel_light = NULL;
336 p->pixel_data = NULL;
337 break;
338 }
339 return out;
340 }
341
342 Appearance *appearance_copy(Appearance *orig)
343 {
344 PlanarSurface *spo, *spc;
345 Appearance *copy = g_new(Appearance, 1);
346 copy->surface.type = orig->surface.type;
347 switch (orig->surface.type) {
348 case Surface_Planar:
349 spo = &(orig->surface.data.planar);
350 spc = &(copy->surface.data.planar);
351 spc->grad = spo->grad;
352 spc->relief = spo->relief;
353 spc->bevel = spo->bevel;
354 if (spo->primary != NULL)
355 spc->primary = color_new(spo->primary->r,
356 spo->primary->g,
357 spo->primary->b);
358 else spc->primary = NULL;
359
360 if (spo->secondary != NULL)
361 spc->secondary = color_new(spo->secondary->r,
362 spo->secondary->g,
363 spo->secondary->b);
364 else spc->secondary = NULL;
365
366 if (spo->border_color != NULL)
367 spc->border_color = color_new(spo->border_color->r,
368 spo->border_color->g,
369 spo->border_color->b);
370 else spc->border_color = NULL;
371
372 if (spo->bevel_dark != NULL)
373 spc->bevel_dark = color_new(spo->bevel_dark->r,
374 spo->bevel_dark->g,
375 spo->bevel_dark->b);
376 else spc->bevel_dark = NULL;
377
378 if (spo->bevel_light != NULL)
379 spc->bevel_light = color_new(spo->bevel_light->r,
380 spo->bevel_light->g,
381 spo->bevel_light->b);
382 else spc->bevel_light = NULL;
383
384 spc->interlaced = spo->interlaced;
385 spc->border = spo->border;
386 spc->pixel_data = NULL;
387 break;
388 }
389 copy->textures = orig->textures;
390 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
391 copy->pixmap = None;
392 copy->xftdraw = NULL;
393 return copy;
394 }
395
396 void appearance_free(Appearance *a)
397 {
398 if (a) {
399 PlanarSurface *p;
400 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
401 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
402 if (a->textures)
403 g_free(a->texture);
404 if (a->surface.type == Surface_Planar) {
405 p = &a->surface.data.planar;
406 color_free(p->primary);
407 color_free(p->secondary);
408 color_free(p->border_color);
409 color_free(p->bevel_dark);
410 color_free(p->bevel_light);
411 g_free(p->pixel_data);
412 }
413 g_free(a);
414 }
415 }
416
417
418 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
419 {
420 pixel32 *scratch;
421 XImage *im = NULL;
422 im = XCreateImage(ob_display, render_visual, render_depth,
423 ZPixmap, 0, NULL, w, h, 32, 0);
424 g_assert(im != NULL);
425 im->byte_order = render_endian;
426 /* this malloc is a complete waste of time on normal 32bpp
427 as reduce_depth just sets im->data = data and returns
428 */
429 scratch = g_new(pixel32, im->width * im->height);
430 im->data = (char*) scratch;
431 reduce_depth(in, im);
432 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
433 im, 0, 0, x, y, w, h);
434 im->data = NULL;
435 XDestroyImage(im);
436 g_free(scratch);
437 }
438
439 void appearance_minsize(Appearance *l, int *w, int *h)
440 {
441 int i;
442 int m;
443 *w = *h = 1;
444
445 switch (l->surface.type) {
446 case Surface_Planar:
447 if (l->surface.data.planar.relief != Flat) {
448 switch (l->surface.data.planar.bevel) {
449 case Bevel1:
450 *w = *h = 2;
451 break;
452 case Bevel2:
453 *w = *h = 4;
454 break;
455 }
456 } else if (l->surface.data.planar.border)
457 *w = *h = 2;
458
459 for (i = 0; i < l->textures; ++i) {
460 switch (l->texture[i].type) {
461 case Bitmask:
462 *w += l->texture[i].data.mask.mask->w;
463 *h += l->texture[i].data.mask.mask->h;
464 break;
465 case Text:
466 m = font_measure_string(l->texture[i].data.text.font,
467 l->texture[i].data.text.string,
468 l->texture[i].data.text.shadow,
469 l->texture[i].data.text.offset);
470 *w += m;
471 m = font_height(l->texture[i].data.text.font,
472 l->texture[i].data.text.shadow,
473 l->texture[i].data.text.offset);
474 *h += m;
475 break;
476 case RGBA:
477 *w += l->texture[i].data.rgba.width;
478 *h += l->texture[i].data.rgba.height;
479 break;
480 case NoTexture:
481 break;
482 }
483 }
484 break;
485 }
486 }
487
488 gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask,
489 int *w, int *h, pixel32 **data)
490 {
491 Window xr;
492 int xx, xy;
493 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
494 XImage *xi, *xm = NULL;
495
496 if (!XGetGeometry(ob_display, pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
497 return FALSE;
498 if (mask) {
499 if (!XGetGeometry(ob_display, mask, &xr, &xx, &xy, &mw, &mh, &xb, &xd))
500 return FALSE;
501 if (pw != mw || ph != mh || xd != 1)
502 return FALSE;
503 }
504
505 xi = XGetImage(ob_display, pmap, 0, 0, pw, ph, 0xffffffff, ZPixmap);
506 if (!xi)
507 return FALSE;
508
509 if (mask) {
510 xm = XGetImage(ob_display, mask, 0, 0, mw, mh, 0xffffffff, ZPixmap);
511 if (!xm)
512 return FALSE;
513 }
514
515 *data = g_new(pixel32, pw * ph);
516 increase_depth(*data, xi);
517
518 if (mask) {
519 /* apply transparency from the mask */
520 di = 0;
521 for (i = 0, y = 0; y < ph; ++y) {
522 for (x = 0; x < pw; ++x, ++i) {
523 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
524 (*data)[i] &= ~(0xff << default_alpha_offset);
525 }
526 di += xm->bytes_per_line;
527 }
528 }
529
530 *w = pw;
531 *h = ph;
532
533 return TRUE;
534 }
This page took 0.05864 seconds and 5 git commands to generate.