]> icculus.org git repositories - mikachu/openbox.git/blob - render/render.c
Adding RrButton to libobrender, ref counted appearances.
[mikachu/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-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_RGBA:
134             g_assert(!transferred);
135             RrImageDraw(a->surface.pixel_data,
136                         &a->texture[i].data.rgba,
137                         a->w, a->h,
138                         &tarea);
139             force_transfer = 1;
140         break;
141         }
142     }
143
144     if (!transferred) {
145         transferred = 1;
146         if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
147             force_transfer)
148         {
149             pixel_data_to_pixmap(a, 0, 0, w, h);
150         }
151     }
152
153     return oldp;
154 }
155
156 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
157 {
158     Pixmap oldp;
159
160     oldp = RrPaintPixmap(a, w, h);
161     XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
162     XClearWindow(RrDisplay(a->inst), win);
163     /* free this after changing the visible pixmap */
164     if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
165 }
166
167 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
168 {
169   RrAppearance *out;
170
171   out = g_new0(RrAppearance, 1);
172   out->inst = inst;
173   out->ref = 1;
174   out->textures = numtex;
175   out->surface.bevel_light_adjust = 128;
176   out->surface.bevel_dark_adjust = 64;
177   if (numtex) out->texture = g_new0(RrTexture, numtex);
178
179   return out;
180 }
181
182 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
183 {
184     g_assert(a->textures == 0);
185
186     a->textures = numtex;
187     if (numtex) a->texture = g_new0(RrTexture, numtex);
188 }
189
190 /* shallow copy means up the ref count and return it */
191 RrAppearance *RrAppearanceCopyShallow(RrAppearance *orig)
192 {
193     orig->ref++;
194     return orig;
195 }
196
197 /* deep copy of orig, means reset ref to 1 on copy
198  * and copy each thing memwise. */
199 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
200 {
201     RrSurface *spo, *spc;
202     RrAppearance *copy = g_new(RrAppearance, 1);
203     gint i;
204
205     copy->inst = orig->inst;
206     copy->ref = 1;
207
208     spo = &(orig->surface);
209     spc = &(copy->surface);
210     spc->grad = spo->grad;
211     spc->relief = spo->relief;
212     spc->bevel = spo->bevel;
213     if (spo->primary != NULL)
214         spc->primary = RrColorNew(copy->inst,
215                                   spo->primary->r,
216                                   spo->primary->g,
217                                   spo->primary->b);
218     else spc->primary = NULL;
219
220     if (spo->secondary != NULL)
221         spc->secondary = RrColorNew(copy->inst,
222                                     spo->secondary->r,
223                                     spo->secondary->g,
224                                     spo->secondary->b);
225     else spc->secondary = NULL;
226
227     if (spo->border_color != NULL)
228         spc->border_color = RrColorNew(copy->inst,
229                                        spo->border_color->r,
230                                        spo->border_color->g,
231                                        spo->border_color->b);
232     else spc->border_color = NULL;
233
234     if (spo->interlace_color != NULL)
235         spc->interlace_color = RrColorNew(copy->inst,
236                                        spo->interlace_color->r,
237                                        spo->interlace_color->g,
238                                        spo->interlace_color->b);
239     else spc->interlace_color = NULL;
240
241     if (spo->bevel_dark != NULL)
242         spc->bevel_dark = RrColorNew(copy->inst,
243                                      spo->bevel_dark->r,
244                                      spo->bevel_dark->g,
245                                      spo->bevel_dark->b);
246     else spc->bevel_dark = NULL;
247
248     if (spo->bevel_light != NULL)
249         spc->bevel_light = RrColorNew(copy->inst,
250                                       spo->bevel_light->r,
251                                       spo->bevel_light->g,
252                                       spo->bevel_light->b);
253     else spc->bevel_light = NULL;
254
255     if (spo->split_primary != NULL)
256         spc->split_primary = RrColorNew(copy->inst,
257                                         spo->split_primary->r,
258                                         spo->split_primary->g,
259                                         spo->split_primary->b);
260     else spc->split_primary = NULL;
261
262     if (spo->split_secondary != NULL)
263         spc->split_secondary = RrColorNew(copy->inst,
264                                         spo->split_secondary->r,
265                                         spo->split_secondary->g,
266                                         spo->split_secondary->b);
267     else spc->split_secondary = NULL;
268
269     spc->interlaced = spo->interlaced;
270     spc->bevel_light_adjust = spo->bevel_light_adjust;
271     spc->bevel_dark_adjust = spo->bevel_dark_adjust;
272     spc->border = spo->border;
273     spc->parent = NULL;
274     spc->parentx = spc->parenty = 0;
275     spc->pixel_data = NULL;
276
277     copy->textures = orig->textures;
278     copy->texture = g_memdup(orig->texture,
279                              orig->textures * sizeof(RrTexture));
280     for (i = 0; i < copy->textures; ++i)
281         if (copy->texture[i].type == RR_TEXTURE_RGBA) {
282             copy->texture[i].data.rgba.cache = NULL;
283         }
284     copy->pixmap = None;
285     copy->xftdraw = NULL;
286     copy->w = copy->h = 0;
287     return copy;
288 }
289
290 /* now decrements ref counter, and frees only if ref <= 0 */
291 void RrAppearanceFree(RrAppearance *a)
292 {
293     gint i;
294
295     if (!a) return;
296
297     /* decrement ref counter */
298     if (a->ref-- > 0) 
299         return;
300
301     /* if we're here we have no more refs to this appearance, free it */   
302     RrSurface *p;
303     if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
304     if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
305     for (i = 0; i < a->textures; ++i)
306         if (a->texture[i].type == RR_TEXTURE_RGBA) {
307             g_free(a->texture[i].data.rgba.cache);
308             a->texture[i].data.rgba.cache = NULL;
309         }
310     if (a->textures)
311         g_free(a->texture);
312     p = &a->surface;
313     RrColorFree(p->primary);
314     RrColorFree(p->secondary);
315     RrColorFree(p->border_color);
316     RrColorFree(p->interlace_color);
317     RrColorFree(p->bevel_dark);
318     RrColorFree(p->bevel_light);
319     RrColorFree(p->split_primary);
320     RrColorFree(p->split_secondary);
321     g_free(p->pixel_data);
322     p->pixel_data = NULL;
323     g_free(a);
324
325     /* set a to NULL so its testable afterwards if it still exists */
326     a = NULL;
327 }
328
329
330 static void pixel_data_to_pixmap(RrAppearance *l,
331                                  gint x, gint y, gint w, gint h)
332 {
333     RrPixel32 *in, *scratch;
334     Pixmap out;
335     XImage *im = NULL;
336     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
337                       ZPixmap, 0, NULL, w, h, 32, 0);
338     g_assert(im != NULL);
339
340     in = l->surface.pixel_data;
341     out = l->pixmap;
342
343 /* this malloc is a complete waste of time on normal 32bpp
344    as reduce_depth just sets im->data = data and returns
345 */
346     scratch = g_new(RrPixel32, im->width * im->height);
347     im->data = (gchar*) scratch;
348     RrReduceDepth(l->inst, in, im);
349     XPutImage(RrDisplay(l->inst), out,
350               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
351               im, 0, 0, x, y, w, h);
352     im->data = NULL;
353     XDestroyImage(im);
354     g_free(scratch);
355 }
356
357 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
358 {
359     *l = *t = *r = *b = 0;
360
361     if (a->surface.grad != RR_SURFACE_PARENTREL) {
362         if (a->surface.relief != RR_RELIEF_FLAT) {
363             switch (a->surface.bevel) {
364             case RR_BEVEL_1:
365                 *l = *t = *r = *b = 1;
366                 break;
367             case RR_BEVEL_2:
368                 *l = *t = *r = *b = 2;
369                 break;
370             }
371         } else if (a->surface.border) {
372             *l = *t = *r = *b = 1;
373         }
374     }
375 }
376
377 void RrMinSize(RrAppearance *a, gint *w, gint *h)
378 {
379     *w = RrMinWidth(a);
380     *h = RrMinHeight(a);
381 }
382
383 gint RrMinWidth(RrAppearance *a)
384 {
385     gint i;
386     RrSize *m;
387     gint l, t, r, b;
388     gint w = 0;
389
390     for (i = 0; i < a->textures; ++i) {
391         switch (a->texture[i].type) {
392         case RR_TEXTURE_NONE:
393             break;
394         case RR_TEXTURE_MASK:
395             w = MAX(w, a->texture[i].data.mask.mask->width);
396             break;
397         case RR_TEXTURE_TEXT:
398             m = RrFontMeasureString(a->texture[i].data.text.font,
399                                     a->texture[i].data.text.string,
400                                     a->texture[i].data.text.shadow_offset_x,
401                                     a->texture[i].data.text.shadow_offset_y);
402             w = MAX(w, m->width);
403             g_free(m);
404             break;
405         case RR_TEXTURE_RGBA:
406             w += MAX(w, a->texture[i].data.rgba.width);
407             break;
408         case RR_TEXTURE_LINE_ART:
409             w += MAX(w, MAX(a->texture[i].data.lineart.x1,
410                             a->texture[i].data.lineart.x2));
411             break;
412         }
413     }
414
415     RrMargins(a, &l, &t, &r, &b);
416
417     w += l + r;
418
419     if (w < 1) w = 1;
420     return w;
421 }
422
423 gint RrMinHeight(RrAppearance *a)
424 {
425     gint i;
426     gint l, t, r, b;
427     gint h = 0;
428
429     for (i = 0; i < a->textures; ++i) {
430         switch (a->texture[i].type) {
431         case RR_TEXTURE_NONE:
432             break;
433         case RR_TEXTURE_MASK:
434             h = MAX(h, a->texture[i].data.mask.mask->height);
435             break;
436         case RR_TEXTURE_TEXT:
437             h += MAX(h, RrFontHeight(a->texture[i].data.text.font,
438                                      a->texture[i].data.text.shadow_offset_y));
439             break;
440         case RR_TEXTURE_RGBA:
441             h += MAX(h, a->texture[i].data.rgba.height);
442             break;
443         case RR_TEXTURE_LINE_ART:
444             h += MAX(h, MAX(a->texture[i].data.lineart.y1,
445                             a->texture[i].data.lineart.y2));
446             break;
447         }
448     }
449
450     RrMargins(a, &l, &t, &r, &b);
451
452     h += t + b;
453
454     if (h < 1) h = 1;
455     return h;
456 }
457
458 static void reverse_bits(gchar *c, gint n)
459 {
460     gint i;
461     for (i = 0; i < n; i++, c++)
462         *c = (((*c * 0x0802UL & 0x22110UL) |
463                (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
464 }
465
466 gboolean RrPixmapToRGBA(const RrInstance *inst,
467                         Pixmap pmap, Pixmap mask,
468                         gint *w, gint *h, RrPixel32 **data)
469 {
470     Window xr;
471     gint xx, xy;
472     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
473     XImage *xi, *xm = NULL;
474
475     if (!XGetGeometry(RrDisplay(inst), pmap,
476                       &xr, &xx, &xy, &pw, &ph, &xb, &xd))
477         return FALSE;
478
479     if (mask) {
480         if (!XGetGeometry(RrDisplay(inst), mask,
481                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
482             return FALSE;
483         if (pw != mw || ph != mh || xd != 1)
484             return FALSE;
485     }
486
487     xi = XGetImage(RrDisplay(inst), pmap,
488                    0, 0, pw, ph, 0xffffffff, ZPixmap);
489     if (!xi)
490         return FALSE;
491
492     if (mask) {
493         xm = XGetImage(RrDisplay(inst), mask,
494                        0, 0, mw, mh, 0xffffffff, ZPixmap);
495         if (!xm) {
496             XDestroyImage(xi);
497             return FALSE;
498         }
499         if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
500             reverse_bits(xm->data, xm->bytes_per_line * xm->height);
501     }
502
503     if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
504         reverse_bits(xi->data, xi->bytes_per_line * xi->height);
505
506     *data = g_new(RrPixel32, pw * ph);
507     RrIncreaseDepth(inst, *data, xi);
508
509     if (mask) {
510         /* apply transparency from the mask */
511         di = 0;
512         for (i = 0, y = 0; y < ph; ++y) {
513             for (x = 0; x < pw; ++x, ++i) {
514                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
515                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
516             }
517             di += xm->bytes_per_line;
518         }
519     }
520
521     *w = pw;
522     *h = ph;
523
524     XDestroyImage(xi);
525     if (mask)
526         XDestroyImage(xm);
527
528     return TRUE;
529 }