]> icculus.org git repositories - dana/openbox.git/blob - render/render.c
xflush after changing the focus decorations so it gets shown faster
[dana/openbox.git] / render / render.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    render.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003        Ben Jansens
6    Copyright (c) 2003        Derek Foreman
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    See the COPYING file for a copy of the GNU General Public License.
19 */
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23
24 #include "render.h"
25 #include "gradient.h"
26 #include "font.h"
27 #include "mask.h"
28 #include "color.h"
29 #include "image.h"
30 #include "theme.h"
31
32 #include <glib.h>
33
34 #ifdef HAVE_STDLIB_H
35 #  include <stdlib.h>
36 #endif
37
38 static void pixel_data_to_pixmap(RrAppearance *l,
39                                  gint x, gint y, gint w, gint h);
40
41 Pixmap RrPaintPixmap(RrAppearance *a, gint w, gint h)
42 {
43     gint i, transferred = 0, sw, sh, partial_w, partial_h;
44     RrPixel32 *source, *dest;
45     Pixmap oldp;
46     RrRect tarea; /* area in which to draw textures */
47     gboolean resized;
48
49     if (w <= 0 || h <= 0) return None;
50
51     if (a->surface.parentx < 0 || a->surface.parenty < 0) {
52         /* ob_debug("Invalid parent co-ordinates\n"); */
53         return None;
54     }
55
56     if (a->surface.grad == RR_SURFACE_PARENTREL &&
57         (a->surface.parentx >= a->surface.parent->w ||
58          a->surface.parenty >= a->surface.parent->h))
59     {
60         return None;
61     }
62
63     resized = (a->w != w || a->h != h);
64
65     oldp = a->pixmap; /* save to free after changing the visible pixmap */
66     a->pixmap = XCreatePixmap(RrDisplay(a->inst),
67                               RrRootWindow(a->inst),
68                               w, h, RrDepth(a->inst));
69
70     g_assert(a->pixmap != None);
71     a->w = w;
72     a->h = h;
73
74     if (a->xftdraw != NULL)
75         XftDrawDestroy(a->xftdraw);
76     a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
77                                RrVisual(a->inst), RrColormap(a->inst));
78     g_assert(a->xftdraw != NULL);
79
80     g_free(a->surface.pixel_data);
81     a->surface.pixel_data = g_new(RrPixel32, w * h);
82
83     if (a->surface.grad == RR_SURFACE_PARENTREL) {
84         g_assert (a->surface.parent);
85         g_assert (a->surface.parent->w);
86
87         sw = a->surface.parent->w;
88         sh = a->surface.parent->h;
89
90         source = (a->surface.parent->surface.pixel_data +
91                   a->surface.parentx + sw * a->surface.parenty);
92         dest = a->surface.pixel_data;
93
94         if (a->surface.parentx + w > sw) {
95             partial_w = sw - a->surface.parentx;
96         } else partial_w = w;
97
98         if (a->surface.parenty + h > sh) {
99             partial_h = sh - a->surface.parenty;
100         } else partial_h = h;
101
102         for (i = 0; i < partial_h; i++, source += sw, dest += w) {
103             memcpy(dest, source, partial_w * sizeof(RrPixel32));
104         }
105     } else
106         RrRender(a, w, h);
107
108     {
109         gint l, t, r, b;
110         RrMargins(a, &l, &t, &r, &b);
111         RECT_SET(tarea, l, t, w - l - r, h - t - b); 
112     }       
113
114     for (i = 0; i < a->textures; i++) {
115         switch (a->texture[i].type) {
116         case RR_TEXTURE_NONE:
117             break;
118         case RR_TEXTURE_TEXT:
119             if (!transferred) {
120                 transferred = 1;
121                 if ((a->surface.grad != RR_SURFACE_SOLID)
122                     || (a->surface.interlaced))
123                     pixel_data_to_pixmap(a, 0, 0, w, h);
124             }
125             if (a->xftdraw == NULL) {
126                 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap, 
127                                            RrVisual(a->inst),
128                                            RrColormap(a->inst));
129             }
130             RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
131             break;
132         case RR_TEXTURE_LINE_ART:
133             if (!transferred) {
134                 transferred = 1;
135                 if ((a->surface.grad != RR_SURFACE_SOLID)
136                     || (a->surface.interlaced))
137                     pixel_data_to_pixmap(a, 0, 0, w, h);
138             }
139             XDrawLine(RrDisplay(a->inst), a->pixmap,
140                       RrColorGC(a->texture[i].data.lineart.color),
141                       a->texture[i].data.lineart.x1,
142                       a->texture[i].data.lineart.y1,
143                       a->texture[i].data.lineart.x2,
144                       a->texture[i].data.lineart.y2);
145             break;
146         case RR_TEXTURE_MASK:
147             if (!transferred) {
148                 transferred = 1;
149                 if ((a->surface.grad != RR_SURFACE_SOLID)
150                     || (a->surface.interlaced))
151                     pixel_data_to_pixmap(a, 0, 0, w, h);
152             }
153             RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
154             break;
155         case RR_TEXTURE_RGBA:
156             g_assert(!transferred);
157             RrImageDraw(a->surface.pixel_data,
158                         &a->texture[i].data.rgba,
159                         a->w, a->h,
160                         &tarea);
161         break;
162         }
163     }
164
165     if (!transferred) {
166         transferred = 1;
167         if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
168             pixel_data_to_pixmap(a, 0, 0, w, h);
169     }
170
171     return oldp;
172 }
173
174 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
175 {
176     Pixmap oldp;
177
178     oldp = RrPaintPixmap(a, w, h);
179     XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
180     XClearWindow(RrDisplay(a->inst), win);
181     /* free this after changing the visible pixmap */
182     if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
183 }
184
185 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
186 {
187   RrAppearance *out;
188
189   out = g_new0(RrAppearance, 1);
190   out->inst = inst;
191   out->textures = numtex;
192   if (numtex) out->texture = g_new0(RrTexture, numtex);
193
194   return out;
195 }
196
197 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
198 {
199     RrSurface *spo, *spc;
200     RrAppearance *copy = g_new(RrAppearance, 1);
201     gint i;
202
203     copy->inst = orig->inst;
204
205     spo = &(orig->surface);
206     spc = &(copy->surface);
207     spc->grad = spo->grad;
208     spc->relief = spo->relief;
209     spc->bevel = spo->bevel;
210     if (spo->primary != NULL)
211         spc->primary = RrColorNew(copy->inst,
212                                   spo->primary->r,
213                                   spo->primary->g, 
214                                   spo->primary->b);
215     else spc->primary = NULL;
216
217     if (spo->secondary != NULL)
218         spc->secondary = RrColorNew(copy->inst,
219                                     spo->secondary->r,
220                                     spo->secondary->g,
221                                     spo->secondary->b);
222     else spc->secondary = NULL;
223
224     if (spo->border_color != NULL)
225         spc->border_color = RrColorNew(copy->inst,
226                                        spo->border_color->r,
227                                        spo->border_color->g,
228                                        spo->border_color->b);
229     else spc->border_color = NULL;
230
231     if (spo->interlace_color != NULL)
232         spc->interlace_color = RrColorNew(copy->inst,
233                                        spo->interlace_color->r,
234                                        spo->interlace_color->g,
235                                        spo->interlace_color->b);
236     else spc->interlace_color = NULL;
237
238     if (spo->bevel_dark != NULL)
239         spc->bevel_dark = RrColorNew(copy->inst,
240                                      spo->bevel_dark->r,
241                                      spo->bevel_dark->g,
242                                      spo->bevel_dark->b);
243     else spc->bevel_dark = NULL;
244
245     if (spo->bevel_light != NULL)
246         spc->bevel_light = RrColorNew(copy->inst,
247                                       spo->bevel_light->r,
248                                       spo->bevel_light->g,
249                                       spo->bevel_light->b);
250     else spc->bevel_light = NULL;
251
252     spc->interlaced = spo->interlaced;
253     spc->border = spo->border;
254     spc->parent = NULL;
255     spc->parentx = spc->parenty = 0;
256     spc->pixel_data = NULL;
257
258     copy->textures = orig->textures;
259     copy->texture = g_memdup(orig->texture,
260                              orig->textures * sizeof(RrTexture));
261     for (i = 0; i < copy->textures; ++i)
262         if (copy->texture[i].type == RR_TEXTURE_RGBA) {
263             copy->texture[i].data.rgba.cache = NULL;
264         }
265     copy->pixmap = None;
266     copy->xftdraw = NULL;
267     copy->w = copy->h = 0;
268     return copy;
269 }
270
271 void RrAppearanceFree(RrAppearance *a)
272 {
273     gint i;
274
275     if (a) {
276         RrSurface *p;
277         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
278         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
279         for (i = 0; i < a->textures; ++i)
280             if (a->texture[i].type == RR_TEXTURE_RGBA) {
281                 g_free(a->texture[i].data.rgba.cache);
282                 a->texture[i].data.rgba.cache = NULL;
283             }
284         if (a->textures)
285             g_free(a->texture);
286         p = &a->surface;
287         RrColorFree(p->primary);
288         RrColorFree(p->secondary);
289         RrColorFree(p->border_color);
290         RrColorFree(p->interlace_color);
291         RrColorFree(p->bevel_dark);
292         RrColorFree(p->bevel_light);
293         g_free(p->pixel_data);
294         p->pixel_data = NULL;
295         g_free(a);
296     }
297 }
298
299
300 static void pixel_data_to_pixmap(RrAppearance *l,
301                                  gint x, gint y, gint w, gint h)
302 {
303     RrPixel32 *in, *scratch;
304     Pixmap out;
305     XImage *im = NULL;
306     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
307                       ZPixmap, 0, NULL, w, h, 32, 0);
308     g_assert(im != NULL);
309
310     in = l->surface.pixel_data;
311     out = l->pixmap;
312
313 /* this malloc is a complete waste of time on normal 32bpp
314    as reduce_depth just sets im->data = data and returns
315 */
316     scratch = g_new(RrPixel32, im->width * im->height);
317     im->data = (gchar*) scratch;
318     RrReduceDepth(l->inst, in, im);
319     XPutImage(RrDisplay(l->inst), out,
320               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
321               im, 0, 0, x, y, w, h);
322     im->data = NULL;
323     XDestroyImage(im);
324     g_free(scratch);
325 }
326
327 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
328 {
329     *l = *t = *r = *b = 0;
330
331     if (a->surface.grad != RR_SURFACE_PARENTREL) {
332         if (a->surface.relief != RR_RELIEF_FLAT) {
333             switch (a->surface.bevel) {
334             case RR_BEVEL_1:
335                 *l = *t = *r = *b = 1;
336                 break;
337             case RR_BEVEL_2:
338                 *l = *t = *r = *b = 2;
339                 break;
340             }
341         } else if (a->surface.border) {
342             *l = *t = *r = *b = 1;
343         }
344     }
345 }
346
347 void RrMinsize(RrAppearance *a, gint *w, gint *h)
348 {
349     gint i;
350     RrSize *m;
351     gint l, t, r, b;
352     *w = *h = 0;
353
354     for (i = 0; i < a->textures; ++i) {
355         switch (a->texture[i].type) {
356         case RR_TEXTURE_NONE:
357             break;
358         case RR_TEXTURE_MASK:
359             *w = MAX(*w, a->texture[i].data.mask.mask->width);
360             *h = MAX(*h, a->texture[i].data.mask.mask->height);
361             break;
362         case RR_TEXTURE_TEXT:
363             m = RrFontMeasureString(a->texture[i].data.text.font,
364                                     a->texture[i].data.text.string, 
365                                     a->texture[i].data.text.shadow_offset_x,
366                                     a->texture[i].data.text.shadow_offset_y);
367             *w = MAX(*w, m->width + 4);
368             m->height = RrFontHeight(a->texture[i].data.text.font,
369                                      a->texture[i].data.text.shadow_offset_y);
370             *h += MAX(*h, m->height);
371             g_free(m);
372             break;
373         case RR_TEXTURE_RGBA:
374             *w += MAX(*w, a->texture[i].data.rgba.width);
375             *h += MAX(*h, a->texture[i].data.rgba.height);
376             break;
377         case RR_TEXTURE_LINE_ART:
378             *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
379                               a->texture[i].data.lineart.x2));
380             *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
381                               a->texture[i].data.lineart.y2));
382             break;
383         }
384     }
385
386     RrMargins(a, &l, &t, &r, &b);
387
388     *w += l + r;
389     *h += t + b;
390
391     if (*w < 1) *w = 1;
392     if (*h < 1) *h = 1;
393 }
394
395 static void reverse_bits(gchar *c, gint n)
396 {
397     gint i;
398     for (i = 0; i < n; i++, c++)
399         *c = (((*c * 0x0802UL & 0x22110UL) |
400                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
401 }
402
403 gboolean RrPixmapToRGBA(const RrInstance *inst,
404                         Pixmap pmap, Pixmap mask,
405                         gint *w, gint *h, RrPixel32 **data)
406 {
407     Window xr;
408     gint xx, xy;
409     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
410     XImage *xi, *xm = NULL;
411
412     if (!XGetGeometry(RrDisplay(inst), pmap,
413                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
414         return FALSE;
415
416     if (mask) {
417         if (!XGetGeometry(RrDisplay(inst), mask,
418                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
419             return FALSE;
420         if (pw != mw || ph != mh || xd != 1)
421             return FALSE;
422     }
423
424     xi = XGetImage(RrDisplay(inst), pmap,
425                    0, 0, pw, ph, 0xffffffff, ZPixmap);
426     if (!xi)
427         return FALSE;
428
429     if (mask) {
430         xm = XGetImage(RrDisplay(inst), mask,
431                        0, 0, mw, mh, 0xffffffff, ZPixmap);
432         if (!xm) {
433             XDestroyImage(xi);
434             return FALSE;
435         }
436         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
437             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
438     }
439
440     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
441         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
442
443     *data = g_new(RrPixel32, pw * ph);
444     RrIncreaseDepth(inst, *data, xi);
445
446     if (mask) {
447         /* apply transparency from the mask */
448         di = 0;
449         for (i = 0, y = 0; y < ph; ++y) {
450             for (x = 0; x < pw; ++x, ++i) {
451                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
452                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
453             }
454             di += xm->bytes_per_line;
455         }
456     }
457
458     *w = pw;
459     *h = ph;
460
461     XDestroyImage(xi);
462     if (mask)
463         XDestroyImage(xm);
464
465     return TRUE;
466 }