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