Fix a double free when you have a mousebind without a button attribute
[dana/openbox.git] / obrender / 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-2007   Dana 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 "render.h"
22 #include "gradient.h"
23 #include "font.h"
24 #include "mask.h"
25 #include "color.h"
26 #include "image.h"
27 #include "theme.h"
28
29 #include <glib.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <X11/Xft/Xft.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, force_transfer = 0;
44     Pixmap oldp = None;
45     RrRect tarea; /* area in which to draw textures */
46     gboolean resized;
47
48     if (w <= 0 || h <= 0) return None;
49
50     if (a->surface.parentx < 0 || a->surface.parenty < 0) {
51         /* ob_debug("Invalid parent co-ordinates\n"); */
52         return None;
53     }
54
55     if (a->surface.grad == RR_SURFACE_PARENTREL &&
56         (a->surface.parentx >= a->surface.parent->w ||
57          a->surface.parenty >= a->surface.parent->h))
58     {
59         return None;
60     }
61
62     resized = (a->w != w || a->h != h);
63
64     oldp = a->pixmap; /* save to free after changing the visible pixmap */
65     a->pixmap = XCreatePixmap(RrDisplay(a->inst),
66                               RrRootWindow(a->inst),
67                               w, h, RrDepth(a->inst));
68
69     g_assert(a->pixmap != None);
70     a->w = w;
71     a->h = h;
72
73     if (a->xftdraw != NULL)
74         XftDrawDestroy(a->xftdraw);
75     a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
76                                RrVisual(a->inst), RrColormap(a->inst));
77     g_assert(a->xftdraw != NULL);
78
79     if (resized) {
80         g_free(a->surface.pixel_data);
81         a->surface.pixel_data = g_new(RrPixel32, w * h);
82     }
83
84     RrRender(a, w, h);
85
86     {
87         gint l, t, r, b;
88         RrMargins(a, &l, &t, &r, &b);
89         RECT_SET(tarea, l, t, w - l - r, h - t - b);
90     }
91
92     for (i = 0; i < a->textures; i++) {
93         switch (a->texture[i].type) {
94         case RR_TEXTURE_NONE:
95             break;
96         case RR_TEXTURE_TEXT:
97             if (!transferred) {
98                 transferred = 1;
99                 if ((a->surface.grad != RR_SURFACE_SOLID)
100                     || (a->surface.interlaced))
101                     pixel_data_to_pixmap(a, 0, 0, w, h);
102             }
103             if (a->xftdraw == NULL) {
104                 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
105                                            RrVisual(a->inst),
106                                            RrColormap(a->inst));
107             }
108             RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
109             break;
110         case RR_TEXTURE_LINE_ART:
111             if (!transferred) {
112                 transferred = 1;
113                 if ((a->surface.grad != RR_SURFACE_SOLID)
114                     || (a->surface.interlaced))
115                     pixel_data_to_pixmap(a, 0, 0, w, h);
116             }
117             XDrawLine(RrDisplay(a->inst), a->pixmap,
118                       RrColorGC(a->texture[i].data.lineart.color),
119                       a->texture[i].data.lineart.x1,
120                       a->texture[i].data.lineart.y1,
121                       a->texture[i].data.lineart.x2,
122                       a->texture[i].data.lineart.y2);
123             break;
124         case RR_TEXTURE_MASK:
125             if (!transferred) {
126                 transferred = 1;
127                 if ((a->surface.grad != RR_SURFACE_SOLID)
128                     || (a->surface.interlaced))
129                     pixel_data_to_pixmap(a, 0, 0, w, h);
130             }
131             RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
132             break;
133         case RR_TEXTURE_IMAGE:
134             g_assert(!transferred);
135             {
136                 RrRect narea = tarea;
137                 RrTextureImage *img = &a->texture[i].data.image;
138                 narea.x += img->tx;
139                 narea.width -= img->tx;
140                 narea.y += img->ty;
141                 narea.height -= img->ty;
142                 if (img->twidth)
143                     narea.width = MIN(narea.width, img->twidth);
144                 if (img->theight)
145                     narea.height = MIN(narea.height, img->theight);
146                 RrImageDrawImage(a->surface.pixel_data,
147                                  &a->texture[i].data.image,
148                                  a->w, a->h,
149                                  &narea);
150             }
151             force_transfer = 1;
152             break;
153         case RR_TEXTURE_RGBA:
154             g_assert(!transferred);
155             {
156                 RrRect narea = tarea;
157                 RrTextureRGBA *rgb = &a->texture[i].data.rgba;
158                 narea.x += rgb->tx;
159                 narea.width -= rgb->tx;
160                 narea.y += rgb->ty;
161                 narea.height -= rgb->ty;
162                 if (rgb->twidth)
163                     narea.width = MIN(narea.width, rgb->twidth);
164                 if (rgb->theight)
165                     narea.height = MIN(narea.height, rgb->theight);
166                 RrImageDrawRGBA(a->surface.pixel_data,
167                                 &a->texture[i].data.rgba,
168                                 a->w, a->h,
169                                 &narea);
170             }
171             force_transfer = 1;
172         break;
173         case RR_TEXTURE_NUM_TYPES:
174             g_assert_not_reached();
175         }
176     }
177
178     if (!transferred) {
179         transferred = 1;
180         if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
181             force_transfer)
182         {
183             pixel_data_to_pixmap(a, 0, 0, w, h);
184         }
185     }
186
187     return oldp;
188 }
189
190 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
191 {
192     Pixmap oldp;
193
194     oldp = RrPaintPixmap(a, w, h);
195     XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
196     XClearWindow(RrDisplay(a->inst), win);
197     /* free this after changing the visible pixmap */
198     if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
199 }
200
201 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
202 {
203   RrAppearance *out;
204
205   out = g_slice_new0(RrAppearance);
206   out->inst = inst;
207   out->textures = numtex;
208   out->surface.bevel_light_adjust = 128;
209   out->surface.bevel_dark_adjust = 64;
210   if (numtex) out->texture = g_new0(RrTexture, numtex);
211
212   return out;
213 }
214
215 void RrAppearanceRemoveTextures(RrAppearance *a)
216 {
217     g_free(a->texture);
218     a->textures = 0;
219 }
220
221 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
222 {
223     g_assert(a->textures == 0);
224
225     a->textures = numtex;
226     if (numtex) a->texture = g_new0(RrTexture, numtex);
227 }
228
229 void RrAppearanceClearTextures(RrAppearance *a)
230 {
231     memset(a->texture, 0, a->textures * sizeof(RrTexture));
232 }
233
234 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
235 {
236     RrSurface *spo, *spc;
237     RrAppearance *copy = g_slice_new(RrAppearance);
238
239     copy->inst = orig->inst;
240
241     spo = &(orig->surface);
242     spc = &(copy->surface);
243     spc->grad = spo->grad;
244     spc->relief = spo->relief;
245     spc->bevel = spo->bevel;
246     if (spo->primary != NULL)
247         spc->primary = RrColorNew(copy->inst,
248                                   spo->primary->r,
249                                   spo->primary->g,
250                                   spo->primary->b);
251     else spc->primary = NULL;
252
253     if (spo->secondary != NULL)
254         spc->secondary = RrColorNew(copy->inst,
255                                     spo->secondary->r,
256                                     spo->secondary->g,
257                                     spo->secondary->b);
258     else spc->secondary = NULL;
259
260     if (spo->border_color != NULL)
261         spc->border_color = RrColorNew(copy->inst,
262                                        spo->border_color->r,
263                                        spo->border_color->g,
264                                        spo->border_color->b);
265     else spc->border_color = NULL;
266
267     if (spo->interlace_color != NULL)
268         spc->interlace_color = RrColorNew(copy->inst,
269                                        spo->interlace_color->r,
270                                        spo->interlace_color->g,
271                                        spo->interlace_color->b);
272     else spc->interlace_color = NULL;
273
274     if (spo->bevel_dark != NULL)
275         spc->bevel_dark = RrColorNew(copy->inst,
276                                      spo->bevel_dark->r,
277                                      spo->bevel_dark->g,
278                                      spo->bevel_dark->b);
279     else spc->bevel_dark = NULL;
280
281     if (spo->bevel_light != NULL)
282         spc->bevel_light = RrColorNew(copy->inst,
283                                       spo->bevel_light->r,
284                                       spo->bevel_light->g,
285                                       spo->bevel_light->b);
286     else spc->bevel_light = NULL;
287
288     if (spo->split_primary != NULL)
289         spc->split_primary = RrColorNew(copy->inst,
290                                         spo->split_primary->r,
291                                         spo->split_primary->g,
292                                         spo->split_primary->b);
293     else spc->split_primary = NULL;
294
295     if (spo->split_secondary != NULL)
296         spc->split_secondary = RrColorNew(copy->inst,
297                                         spo->split_secondary->r,
298                                         spo->split_secondary->g,
299                                         spo->split_secondary->b);
300     else spc->split_secondary = NULL;
301
302     spc->interlaced = spo->interlaced;
303     spc->bevel_light_adjust = spo->bevel_light_adjust;
304     spc->bevel_dark_adjust = spo->bevel_dark_adjust;
305     spc->border = spo->border;
306     spc->parent = NULL;
307     spc->parentx = spc->parenty = 0;
308     spc->pixel_data = NULL;
309
310     copy->textures = orig->textures;
311     copy->texture = g_memdup(orig->texture,
312                              orig->textures * sizeof(RrTexture));
313     copy->pixmap = None;
314     copy->xftdraw = NULL;
315     copy->w = copy->h = 0;
316     return copy;
317 }
318
319 void RrAppearanceFree(RrAppearance *a)
320 {
321     if (a) {
322         RrSurface *p;
323         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
324         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
325         if (a->textures)
326             g_free(a->texture);
327         p = &a->surface;
328         RrColorFree(p->primary);
329         RrColorFree(p->secondary);
330         RrColorFree(p->border_color);
331         RrColorFree(p->interlace_color);
332         RrColorFree(p->bevel_dark);
333         RrColorFree(p->bevel_light);
334         RrColorFree(p->split_primary);
335         RrColorFree(p->split_secondary);
336         g_free(p->pixel_data);
337         p->pixel_data = NULL;
338         g_slice_free(RrAppearance, a);
339     }
340 }
341
342 static void pixel_data_to_pixmap(RrAppearance *l,
343                                  gint x, gint y, gint w, gint h)
344 {
345     RrPixel32 *in, *scratch;
346     Pixmap out;
347     XImage *im = NULL;
348     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
349                       ZPixmap, 0, NULL, w, h, 32, 0);
350     g_assert(im != NULL);
351
352     in = l->surface.pixel_data;
353     out = l->pixmap;
354
355 /* this malloc is a complete waste of time on normal 32bpp
356    as reduce_depth just sets im->data = data and returns
357 */
358     scratch = g_new(RrPixel32, im->width * im->height);
359     im->data = (gchar*) scratch;
360     RrReduceDepth(l->inst, in, im);
361     XPutImage(RrDisplay(l->inst), out,
362               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
363               im, 0, 0, x, y, w, h);
364     im->data = NULL;
365     XDestroyImage(im);
366     g_free(scratch);
367 }
368
369 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
370 {
371     *l = *t = *r = *b = 0;
372
373     if (a->surface.grad != RR_SURFACE_PARENTREL) {
374         if (a->surface.relief != RR_RELIEF_FLAT) {
375             switch (a->surface.bevel) {
376             case RR_BEVEL_1:
377                 *l = *t = *r = *b = 1;
378                 break;
379             case RR_BEVEL_2:
380                 *l = *t = *r = *b = 2;
381                 break;
382             case RR_BEVEL_NUM_TYPES:
383                 g_assert_not_reached();
384             }
385         } else if (a->surface.border) {
386             *l = *t = *r = *b = 1;
387         }
388     }
389 }
390
391 void RrMinSize(RrAppearance *a, gint *w, gint *h)
392 {
393     *w = RrMinWidth(a);
394     *h = RrMinHeight(a);
395 }
396
397 gint RrMinWidth(RrAppearance *a)
398 {
399     gint i;
400     RrSize *m;
401     gint l, t, r, b;
402     gint w = 0;
403
404     RrMargins(a, &l, &t, &r, &b);
405
406     for (i = 0; i < a->textures; ++i) {
407         switch (a->texture[i].type) {
408         case RR_TEXTURE_NONE:
409             break;
410         case RR_TEXTURE_MASK:
411             w = MAX(w, a->texture[i].data.mask.mask->width);
412             break;
413         case RR_TEXTURE_TEXT:
414             m = RrFontMeasureString(a->texture[i].data.text.font,
415                                     a->texture[i].data.text.string,
416                                     a->texture[i].data.text.shadow_offset_x,
417                                     a->texture[i].data.text.shadow_offset_y,
418                                     a->texture[i].data.text.flow,
419                                     a->texture[i].data.text.maxwidth);
420             w = MAX(w, m->width);
421             g_slice_free(RrSize, m);
422             break;
423         case RR_TEXTURE_RGBA:
424             w += MAX(w, a->texture[i].data.rgba.width);
425             break;
426         case RR_TEXTURE_IMAGE:
427             /* images resize so they don't contribute anything to the min */
428             break;
429         case RR_TEXTURE_LINE_ART:
430             w = MAX(w, MAX(a->texture[i].data.lineart.x1 - l - r,
431                            a->texture[i].data.lineart.x2 - l - r));
432             break;
433         case RR_TEXTURE_NUM_TYPES:
434             g_assert_not_reached();
435         }
436     }
437
438     w += l + r;
439
440     if (w < 1) w = 1;
441     return w;
442 }
443
444 gint RrMinHeight(RrAppearance *a)
445 {
446     gint i;
447     gint l, t, r, b;
448     RrSize *m;
449     gint h = 0;
450
451     RrMargins(a, &l, &t, &r, &b);
452
453     for (i = 0; i < a->textures; ++i) {
454         switch (a->texture[i].type) {
455         case RR_TEXTURE_NONE:
456             break;
457         case RR_TEXTURE_MASK:
458             h = MAX(h, a->texture[i].data.mask.mask->height);
459             break;
460         case RR_TEXTURE_TEXT:
461             if (a->texture[i].data.text.flow) {
462                 g_assert(a->texture[i].data.text.string != NULL);
463
464                 m = RrFontMeasureString
465                     (a->texture[i].data.text.font,
466                      a->texture[i].data.text.string,
467                      a->texture[i].data.text.shadow_offset_x,
468                      a->texture[i].data.text.shadow_offset_y,
469                      a->texture[i].data.text.flow,
470                      a->texture[i].data.text.maxwidth);
471                 h += MAX(h, m->height);
472                 g_slice_free(RrSize, m);
473             }
474             else
475                 h += MAX(h,
476                          RrFontHeight
477                          (a->texture[i].data.text.font,
478                           a->texture[i].data.text.shadow_offset_y));
479             break;
480         case RR_TEXTURE_RGBA:
481             h += MAX(h, a->texture[i].data.rgba.height);
482             break;
483         case RR_TEXTURE_IMAGE:
484             /* images resize so they don't contribute anything to the min */
485             break;
486         case RR_TEXTURE_LINE_ART:
487             h = MAX(h, MAX(a->texture[i].data.lineart.y1 - t - b,
488                            a->texture[i].data.lineart.y2 - t - b));
489             break;
490         case RR_TEXTURE_NUM_TYPES:
491             g_assert_not_reached();
492         }
493     }
494
495     h += t + b;
496
497     if (h < 1) h = 1;
498     return h;
499 }
500
501 static void reverse_bits(gchar *c, gint n)
502 {
503     gint i;
504     for (i = 0; i < n; i++, c++)
505         *c = (((*c * 0x0802UL & 0x22110UL) |
506                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
507 }
508
509 gboolean RrPixmapToRGBA(const RrInstance *inst,
510                         Pixmap pmap, Pixmap mask,
511                         gint *w, gint *h, RrPixel32 **data)
512 {
513     Window xr;
514     gint xx, xy;
515     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
516     XImage *xi, *xm = NULL;
517
518     if (!XGetGeometry(RrDisplay(inst), pmap,
519                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
520         return FALSE;
521
522     if (mask) {
523         if (!XGetGeometry(RrDisplay(inst), mask,
524                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
525             return FALSE;
526         if (pw != mw || ph != mh || xd != 1)
527             return FALSE;
528     }
529
530     xi = XGetImage(RrDisplay(inst), pmap,
531                    0, 0, pw, ph, 0xffffffff, ZPixmap);
532     if (!xi)
533         return FALSE;
534
535     if (mask) {
536         xm = XGetImage(RrDisplay(inst), mask,
537                        0, 0, mw, mh, 0xffffffff, ZPixmap);
538         if (!xm) {
539             XDestroyImage(xi);
540             return FALSE;
541         }
542         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
543             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
544     }
545
546     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
547         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
548
549     *data = g_new(RrPixel32, pw * ph);
550     RrIncreaseDepth(inst, *data, xi);
551
552     if (mask) {
553         /* apply transparency from the mask */
554         di = 0;
555         for (i = 0, y = 0; y < ph; ++y) {
556             for (x = 0; x < pw; ++x, ++i) {
557                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
558                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
559             }
560             di += xm->bytes_per_line;
561         }
562     }
563
564     *w = pw;
565     *h = ph;
566
567     XDestroyImage(xi);
568     if (mask)
569         XDestroyImage(xm);
570
571     return TRUE;
572 }