]> icculus.org git repositories - mikachu/openbox.git/blob - render/render.c
oops typo
[mikachu/openbox.git] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3
4 #include <glib.h>
5 #include "render.h"
6 #include "gradient.h"
7 #include "font.h"
8 #include "mask.h"
9 #include "color.h"
10 #include "image.h"
11 #include "theme.h"
12 #include "kernel/openbox.h"
13
14 #ifdef HAVE_STDLIB_H
15 #  include <stdlib.h>
16 #endif
17
18 int render_depth;
19 XVisualInfo render_visual_info;
20
21 Visual *render_visual;
22 Colormap render_colormap;
23
24 int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0;
25 int render_red_shift, render_green_shift, render_blue_shift;
26 int render_red_mask, render_green_mask, render_blue_mask;
27
28
29 void render_startup(void)
30 {
31     render_depth = DefaultDepth(ob_display, ob_screen);
32     render_visual = DefaultVisual(ob_display, ob_screen);
33     render_colormap = DefaultColormap(ob_display, ob_screen);
34
35     switch (render_visual->class) {
36     case TrueColor:
37         truecolor_startup();
38         break;
39     case PseudoColor:
40     case StaticColor:
41     case GrayScale:
42     case StaticGray:
43         pseudocolor_startup();
44         break;
45     default:
46         g_critical("unsupported visual class.\n");
47         exit(EXIT_FAILURE);
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 paint(Window win, Appearance *l, int w, int h)
163 {
164     int i, transferred = 0, sw;
165     pixel32 *source, *dest;
166     Pixmap oldp;
167     Rect tarea; /* area in which to draw textures */
168     gboolean resized;
169
170     if (w <= 0 || h <= 0) return;
171
172     resized = (l->w != w || l->h != h);
173
174     if (resized) {
175         oldp = l->pixmap; /* save to free after changing the visible pixmap */
176         l->pixmap = XCreatePixmap(ob_display, ob_root, w, h, render_depth);
177     } else
178         oldp = None;
179
180     g_assert(l->pixmap != None);
181     l->w = w;
182     l->h = h;
183
184     if (l->xftdraw != NULL)
185         XftDrawDestroy(l->xftdraw);
186     l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual, 
187                                render_colormap);
188     g_assert(l->xftdraw != NULL);
189
190     g_free(l->surface.pixel_data);
191     l->surface.pixel_data = g_new(pixel32, w * h);
192
193     if (l->surface.grad == Background_ParentRelative) {
194         g_assert (l->surface.parent);
195         g_assert (l->surface.parent->w);
196
197         sw = l->surface.parent->w;
198         source = (l->surface.parent->surface.pixel_data + l->surface.parentx +
199                   sw * l->surface.parenty);
200         dest = l->surface.pixel_data;
201         for (i = 0; i < h; i++, source += sw, dest += w) {
202             memcpy(dest, source, w * sizeof(pixel32));
203         }
204     }
205     else if (l->surface.grad == Background_Solid)
206         gradient_solid(l, 0, 0, w, h);
207     else gradient_render(&l->surface, w, h);
208
209     RECT_SET(tarea, 0, 0, w, h);
210     if (l->surface.grad != Background_ParentRelative) {
211         if (l->surface.relief != Flat) {
212             switch (l->surface.bevel) {
213             case Bevel1:
214                 tarea.x += 1; tarea.y += 1;
215                 tarea.width -= 2; tarea.height -= 2;
216                 break;
217             case Bevel2:
218                 tarea.x += 2; tarea.y += 2;
219                 tarea.width -= 4; tarea.height -= 4;
220                 break;
221             }
222         } else if (l->surface.border) {
223             tarea.x += 1; tarea.y += 1;
224             tarea.width -= 2; tarea.height -= 2;
225         }
226     }
227
228     for (i = 0; i < l->textures; i++) {
229         switch (l->texture[i].type) {
230         case NoTexture:
231             break;
232         case Text:
233             if (!transferred) {
234                 transferred = 1;
235                 if (l->surface.grad != Background_Solid)
236                     pixel32_to_pixmap(l->surface.pixel_data, 
237                                       l->pixmap, 0, 0, w, h);
238             }
239             if (l->xftdraw == NULL) {
240                 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, 
241                                         render_visual, render_colormap);
242             }
243             font_draw(l->xftdraw, &l->texture[i].data.text, &tarea);
244         break;
245         case Bitmask:
246             if (!transferred) {
247                 transferred = 1;
248                 if (l->surface.grad != Background_Solid)
249                     pixel32_to_pixmap(l->surface.pixel_data, 
250                                       l->pixmap, 0, 0, w, h);
251             }
252             if (l->texture[i].data.mask.color->gc == None)
253                 color_allocate_gc(l->texture[i].data.mask.color);
254             mask_draw(l->pixmap, &l->texture[i].data.mask, &tarea);
255         break;
256         case RGBA:
257             image_draw(l->surface.pixel_data,
258                        &l->texture[i].data.rgba, &tarea);
259         break;
260         }
261     }
262
263     if (!transferred) {
264         transferred = 1;
265         if (l->surface.grad != Background_Solid)
266             pixel32_to_pixmap(l->surface.pixel_data, l->pixmap, 0, 0, w, h);
267     }
268
269
270     XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
271     XClearWindow(ob_display, win);
272     if (oldp) XFreePixmap(ob_display, oldp);
273 }
274
275 void render_shutdown(void)
276 {
277 }
278
279 Appearance *appearance_new(int numtex)
280 {
281   Surface *p;
282   Appearance *out;
283
284   out = g_new(Appearance, 1);
285   out->textures = numtex;
286   out->xftdraw = NULL;
287   if (numtex) out->texture = g_new0(Texture, numtex);
288   else out->texture = NULL;
289   out->pixmap = None;
290   out->w = out->h = 0;
291
292   p = &out->surface;
293   p->primary = NULL;
294   p->secondary = NULL;
295   p->border_color = NULL;
296   p->bevel_dark = NULL;
297   p->bevel_light = NULL;
298   p->pixel_data = NULL;
299   return out;
300 }
301
302 Appearance *appearance_copy(Appearance *orig)
303 {
304     Surface *spo, *spc;
305     Appearance *copy = g_new(Appearance, 1);
306
307     spo = &(orig->surface);
308     spc = &(copy->surface);
309     spc->grad = spo->grad;
310     spc->relief = spo->relief;
311     spc->bevel = spo->bevel;
312     if (spo->primary != NULL)
313         spc->primary = color_new(spo->primary->r,
314                                  spo->primary->g, 
315                                  spo->primary->b);
316     else spc->primary = NULL;
317
318     if (spo->secondary != NULL)
319         spc->secondary = color_new(spo->secondary->r,
320                                    spo->secondary->g,
321                                    spo->secondary->b);
322     else spc->secondary = NULL;
323
324     if (spo->border_color != NULL)
325         spc->border_color = color_new(spo->border_color->r,
326                                       spo->border_color->g,
327                                       spo->border_color->b);
328     else spc->border_color = NULL;
329
330     if (spo->bevel_dark != NULL)
331         spc->bevel_dark = color_new(spo->bevel_dark->r,
332                                     spo->bevel_dark->g,
333                                     spo->bevel_dark->b);
334     else spc->bevel_dark = NULL;
335
336     if (spo->bevel_light != NULL)
337         spc->bevel_light = color_new(spo->bevel_light->r,
338                                      spo->bevel_light->g,
339                                      spo->bevel_light->b);
340     else spc->bevel_light = NULL;
341
342     spc->interlaced = spo->interlaced;
343     spc->border = spo->border;
344     spc->pixel_data = NULL;
345
346     copy->textures = orig->textures;
347     copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
348     copy->pixmap = None;
349     copy->xftdraw = NULL;
350     copy->w = copy->h = 0;
351     return copy;
352 }
353
354 void appearance_free(Appearance *a)
355 {
356     if (a) {
357         Surface *p;
358         if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
359         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
360         if (a->textures)
361             g_free(a->texture);
362         p = &a->surface;
363         color_free(p->primary);
364         color_free(p->secondary);
365         color_free(p->border_color);
366         color_free(p->bevel_dark);
367         color_free(p->bevel_light);
368         g_free(p->pixel_data);
369
370         g_free(a);
371     }
372 }
373
374
375 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
376 {
377     pixel32 *scratch;
378     XImage *im = NULL;
379     im = XCreateImage(ob_display, render_visual, render_depth,
380                       ZPixmap, 0, NULL, w, h, 32, 0);
381     g_assert(im != NULL);
382     im->byte_order = render_endian;
383 /* this malloc is a complete waste of time on normal 32bpp
384    as reduce_depth just sets im->data = data and returns
385 */
386     scratch = g_new(pixel32, im->width * im->height);
387     im->data = (char*) scratch;
388     reduce_depth(in, im);
389     XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
390               im, 0, 0, x, y, w, h);
391     im->data = NULL;
392     XDestroyImage(im);
393     g_free(scratch);
394 }
395
396 void appearance_minsize(Appearance *l, int *w, int *h)
397 {
398     int i;
399     int m;
400     *w = *h = 0;
401
402     for (i = 0; i < l->textures; ++i) {
403         switch (l->texture[i].type) {
404         case Bitmask:
405             *w = MAX(*w, l->texture[i].data.mask.mask->w);
406             *h = MAX(*h, l->texture[i].data.mask.mask->h);
407             break;
408         case Text:
409             m = font_measure_string(l->texture[i].data.text.font,
410                                     l->texture[i].data.text.string,
411                                     l->texture[i].data.text.shadow,
412                                     l->texture[i].data.text.offset);
413             *w = MAX(*w, m);
414             m = font_height(l->texture[i].data.text.font,
415                             l->texture[i].data.text.shadow,
416                             l->texture[i].data.text.offset);
417             *h += MAX(*h, m);
418             break;
419         case RGBA:
420             *w += MAX(*w, l->texture[i].data.rgba.width);
421             *h += MAX(*h, l->texture[i].data.rgba.height);
422             break;
423         case NoTexture:
424             break;
425         }
426     }
427
428     if (l->surface.relief != Flat) {
429         switch (l->surface.bevel) {
430         case Bevel1:
431             *w += 2;
432             *h += 2;
433             break;
434         case Bevel2:
435             *w += 4;
436             *h += 4;
437             break;
438         }
439     } else if (l->surface.border) {
440         *w += 2;
441         *h += 2;
442     }
443
444     if (*w < 1) *w = 1;
445     if (*h < 1) *h = 1;
446 }
447
448 gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask,
449                                int *w, int *h, pixel32 **data)
450 {
451     Window xr;
452     int xx, xy;
453     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
454     XImage *xi, *xm = NULL;
455
456     if (!XGetGeometry(ob_display, pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
457         return FALSE;
458     if (mask) {
459         if (!XGetGeometry(ob_display, mask, &xr, &xx, &xy, &mw, &mh, &xb, &xd))
460             return FALSE;
461         if (pw != mw || ph != mh || xd != 1)
462             return FALSE;
463     }
464
465     xi = XGetImage(ob_display, pmap, 0, 0, pw, ph, 0xffffffff, ZPixmap);
466     if (!xi)
467         return FALSE;
468
469     if (mask) {
470         xm = XGetImage(ob_display, mask, 0, 0, mw, mh, 0xffffffff, ZPixmap);
471         if (!xm)
472             return FALSE;
473     }
474
475     *data = g_new(pixel32, pw * ph);
476     increase_depth(*data, xi);
477
478     if (mask) {
479         /* apply transparency from the mask */
480         di = 0;
481         for (i = 0, y = 0; y < ph; ++y) {
482             for (x = 0; x < pw; ++x, ++i) {
483                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
484                     (*data)[i] &= ~(0xff << default_alpha_offset);
485             }
486             di += xm->bytes_per_line;
487         }
488     }
489
490     *w = pw;
491     *h = ph;
492
493     return TRUE;
494 }