]> icculus.org git repositories - dana/openbox.git/blob - render/render.c
make icons use pixel32 data, and image_draw takes pixel32 data.
[dana/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 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->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         spc->interlaced = spo->interlaced;
373         spc->border = spo->border;
374         spc->pixel_data = NULL;
375     break;
376     }
377     copy->textures = orig->textures;
378     copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
379     copy->pixmap = None;
380     copy->xftdraw = NULL;
381     return copy;
382 }
383
384 void appearance_free(Appearance *a)
385 {
386     if (a) {
387         PlanarSurface *p;
388         if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
389         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
390         if (a->textures)
391             g_free(a->texture);
392         if (a->surface.type == Surface_Planar) {
393             p = &a->surface.data.planar;
394             if (p->primary != NULL) color_free(p->primary);
395             if (p->secondary != NULL) color_free(p->secondary);
396             if (p->border_color != NULL) color_free(p->border_color);
397             if (p->pixel_data != NULL) g_free(p->pixel_data);
398         }
399         g_free(a);
400     }
401 }
402
403
404 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
405 {
406     pixel32 *scratch;
407     XImage *im = NULL;
408     im = XCreateImage(ob_display, render_visual, render_depth,
409                       ZPixmap, 0, NULL, w, h, 32, 0);
410     g_assert(im != NULL);
411     im->byte_order = render_endian;
412 /* this malloc is a complete waste of time on normal 32bpp
413    as reduce_depth just sets im->data = data and returns
414 */
415     scratch = g_new(pixel32, im->width * im->height);
416     im->data = (char*) scratch;
417     reduce_depth(in, im);
418     XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
419               im, 0, 0, x, y, w, h);
420     im->data = NULL;
421     XDestroyImage(im);
422     g_free(scratch);
423 }
424
425 void appearance_minsize(Appearance *l, int *w, int *h)
426 {
427     int i;
428     int m;
429     *w = *h = 1;
430
431     switch (l->surface.type) {
432     case Surface_Planar:
433         if (l->surface.data.planar.relief != Flat) {
434             switch (l->surface.data.planar.bevel) {
435             case Bevel1:
436                 *w = *h = 2;
437                 break;
438             case Bevel2:
439                 *w = *h = 4;
440                 break;
441             }
442         } else if (l->surface.data.planar.border)
443             *w = *h = 2;
444
445         for (i = 0; i < l->textures; ++i) {
446             switch (l->texture[i].type) {
447             case Bitmask:
448                 *w += l->texture[i].data.mask.mask->w;
449                 *h += l->texture[i].data.mask.mask->h;
450                 break;
451             case Text:
452                 m = font_measure_string(l->texture[i].data.text.font,
453                                         l->texture[i].data.text.string,
454                                         l->texture[i].data.text.shadow,
455                                         l->texture[i].data.text.offset);
456                 *w += m;
457                 m = font_height(l->texture[i].data.text.font,
458                                 l->texture[i].data.text.shadow,
459                                 l->texture[i].data.text.offset);
460                 *h += m;
461                 break;
462             case RGBA:
463                 *w += l->texture[i].data.rgba.width;
464                 *h += l->texture[i].data.rgba.height;
465                 break;
466             case NoTexture:
467                 break;
468             }
469         }
470         break;
471     }
472 }
473
474 gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask,
475                                int *w, int *h, pixel32 **data)
476 {
477     Window xr;
478     int xx, xy;
479     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
480     XImage *xi, *xm = NULL;
481
482     if (!XGetGeometry(ob_display, pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
483         return FALSE;
484     if (mask) {
485         if (!XGetGeometry(ob_display, mask, &xr, &xx, &xy, &mw, &mh, &xb, &xd))
486             return FALSE;
487         if (pw != mw || ph != mh || xd != 1)
488             return FALSE;
489     }
490
491     xi = XGetImage(ob_display, pmap, 0, 0, pw, ph, 0xffffffff, ZPixmap);
492     if (!xi)
493         return FALSE;
494
495     if (mask) {
496         xm = XGetImage(ob_display, mask, 0, 0, mw, mh, 0xffffffff, ZPixmap);
497         if (!xm)
498             return FALSE;
499     }
500
501     *data = g_new(pixel32, pw * ph);
502     increase_depth(*data, xi);
503
504     if (mask) {
505         /* apply transparency from the mask */
506         di = 0;
507         for (i = 0, y = 0; y < ph; ++y) {
508             for (x = 0; x < pw; ++x, ++i) {
509                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
510                     (*data)[i] &= ~(0xff << default_alpha_offset);
511             }
512             di += xm->bytes_per_line;
513         }
514     }
515
516     *w = pw;
517     *h = ph;
518
519     return TRUE;
520 }