1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
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
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.
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.
18 See the COPYING file for a copy of the GNU General Public License.
31 #include <X11/Xutil.h>
32 #include <X11/Xft/Xft.h>
38 static void pixel_data_to_pixmap(RrAppearance *l,
39 gint x, gint y, gint w, gint h);
41 Pixmap RrPaintPixmap(RrAppearance *a, gint w, gint h)
43 gint i, transferred = 0, force_transfer = 0;
45 RrRect tarea; /* area in which to draw textures */
48 if (w <= 0 || h <= 0) return None;
50 if (a->surface.parentx < 0 || a->surface.parenty < 0) {
51 /* ob_debug("Invalid parent co-ordinates\n"); */
55 if (a->surface.grad == RR_SURFACE_PARENTREL &&
56 (a->surface.parentx >= a->surface.parent->w ||
57 a->surface.parenty >= a->surface.parent->h))
62 resized = (a->w != w || a->h != h);
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));
69 g_assert(a->pixmap != None);
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);
80 g_free(a->surface.pixel_data);
81 a->surface.pixel_data = g_new(RrPixel32, w * h);
88 RrMargins(a, &l, &t, &r, &b);
89 RECT_SET(tarea, l, t, w - l - r, h - t - b);
92 for (i = 0; i < a->textures; i++) {
93 switch (a->texture[i].type) {
99 if ((a->surface.grad != RR_SURFACE_SOLID)
100 || (a->surface.interlaced))
101 pixel_data_to_pixmap(a, 0, 0, w, h);
103 if (a->xftdraw == NULL) {
104 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
106 RrColormap(a->inst));
108 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
110 case RR_TEXTURE_LINE_ART:
113 if ((a->surface.grad != RR_SURFACE_SOLID)
114 || (a->surface.interlaced))
115 pixel_data_to_pixmap(a, 0, 0, w, h);
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);
124 case RR_TEXTURE_MASK:
127 if ((a->surface.grad != RR_SURFACE_SOLID)
128 || (a->surface.interlaced))
129 pixel_data_to_pixmap(a, 0, 0, w, h);
131 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
133 case RR_TEXTURE_IMAGE:
134 g_assert(!transferred);
136 RrRect narea = tarea;
137 RrTextureImage *img = &a->texture[i].data.image;
139 narea.width -= img->tx;
141 narea.height -= img->ty;
143 narea.width = MIN(narea.width, img->twidth);
145 narea.height = MIN(narea.height, img->theight);
146 RrImageDrawImage(a->surface.pixel_data,
147 &a->texture[i].data.image,
153 case RR_TEXTURE_RGBA:
154 g_assert(!transferred);
156 RrRect narea = tarea;
157 RrTextureRGBA *rgb = &a->texture[i].data.rgba;
159 narea.width -= rgb->tx;
161 narea.height -= rgb->ty;
163 narea.width = MIN(narea.width, rgb->twidth);
165 narea.height = MIN(narea.height, rgb->theight);
166 RrImageDrawRGBA(a->surface.pixel_data,
167 &a->texture[i].data.rgba,
173 case RR_TEXTURE_NUM_TYPES:
174 g_assert_not_reached();
180 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
183 pixel_data_to_pixmap(a, 0, 0, w, h);
190 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
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);
201 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
205 out = g_slice_new0(RrAppearance);
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);
215 void RrAppearanceRemoveTextures(RrAppearance *a)
221 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
223 g_assert(a->textures == 0);
225 a->textures = numtex;
226 if (numtex) a->texture = g_new0(RrTexture, numtex);
229 void RrAppearanceClearTextures(RrAppearance *a)
231 memset(a->texture, 0, a->textures * sizeof(RrTexture));
234 /* shallow copy means up the ref count and return it */
235 RrAppearance *RrAppearanceCopyShallow(RrAppearance *orig)
241 /* deep copy of orig, means reset ref to 1 on copy
242 * and copy each thing memwise. */
243 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
245 RrSurface *spo, *spc;
246 RrAppearance *copy = g_slice_new(RrAppearance);
248 copy->inst = orig->inst;
250 spo = &(orig->surface);
251 spc = &(copy->surface);
252 spc->grad = spo->grad;
253 spc->relief = spo->relief;
254 spc->bevel = spo->bevel;
255 if (spo->primary != NULL)
256 spc->primary = RrColorNew(copy->inst,
260 else spc->primary = NULL;
262 if (spo->secondary != NULL)
263 spc->secondary = RrColorNew(copy->inst,
267 else spc->secondary = NULL;
269 if (spo->border_color != NULL)
270 spc->border_color = RrColorNew(copy->inst,
271 spo->border_color->r,
272 spo->border_color->g,
273 spo->border_color->b);
274 else spc->border_color = NULL;
276 if (spo->interlace_color != NULL)
277 spc->interlace_color = RrColorNew(copy->inst,
278 spo->interlace_color->r,
279 spo->interlace_color->g,
280 spo->interlace_color->b);
281 else spc->interlace_color = NULL;
283 if (spo->bevel_dark != NULL)
284 spc->bevel_dark = RrColorNew(copy->inst,
288 else spc->bevel_dark = NULL;
290 if (spo->bevel_light != NULL)
291 spc->bevel_light = RrColorNew(copy->inst,
294 spo->bevel_light->b);
295 else spc->bevel_light = NULL;
297 if (spo->split_primary != NULL)
298 spc->split_primary = RrColorNew(copy->inst,
299 spo->split_primary->r,
300 spo->split_primary->g,
301 spo->split_primary->b);
302 else spc->split_primary = NULL;
304 if (spo->split_secondary != NULL)
305 spc->split_secondary = RrColorNew(copy->inst,
306 spo->split_secondary->r,
307 spo->split_secondary->g,
308 spo->split_secondary->b);
309 else spc->split_secondary = NULL;
311 spc->interlaced = spo->interlaced;
312 spc->bevel_light_adjust = spo->bevel_light_adjust;
313 spc->bevel_dark_adjust = spo->bevel_dark_adjust;
314 spc->border = spo->border;
316 spc->parentx = spc->parenty = 0;
317 spc->pixel_data = NULL;
319 copy->textures = orig->textures;
320 copy->texture = g_memdup(orig->texture,
321 orig->textures * sizeof(RrTexture));
323 copy->xftdraw = NULL;
324 copy->w = copy->h = 0;
328 /* now decrements ref counter, and frees only if ref <= 0 */
329 void RrAppearanceFree(RrAppearance *a)
333 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
334 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
338 RrColorFree(p->primary);
339 RrColorFree(p->secondary);
340 RrColorFree(p->border_color);
341 RrColorFree(p->interlace_color);
342 RrColorFree(p->bevel_dark);
343 RrColorFree(p->bevel_light);
344 RrColorFree(p->split_primary);
345 RrColorFree(p->split_secondary);
346 g_free(p->pixel_data);
347 p->pixel_data = NULL;
348 g_slice_free(RrAppearance, a);
352 static void pixel_data_to_pixmap(RrAppearance *l,
353 gint x, gint y, gint w, gint h)
355 RrPixel32 *in, *scratch;
358 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
359 ZPixmap, 0, NULL, w, h, 32, 0);
360 g_assert(im != NULL);
362 in = l->surface.pixel_data;
365 /* this malloc is a complete waste of time on normal 32bpp
366 as reduce_depth just sets im->data = data and returns
368 scratch = g_new(RrPixel32, im->width * im->height);
369 im->data = (gchar*) scratch;
370 RrReduceDepth(l->inst, in, im);
371 XPutImage(RrDisplay(l->inst), out,
372 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
373 im, 0, 0, x, y, w, h);
379 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
381 *l = *t = *r = *b = 0;
383 if (a->surface.grad != RR_SURFACE_PARENTREL) {
384 if (a->surface.relief != RR_RELIEF_FLAT) {
385 switch (a->surface.bevel) {
387 *l = *t = *r = *b = 1;
390 *l = *t = *r = *b = 2;
392 case RR_BEVEL_NUM_TYPES:
393 g_assert_not_reached();
395 } else if (a->surface.border) {
396 *l = *t = *r = *b = 1;
401 void RrMinSize(RrAppearance *a, gint *w, gint *h)
407 gint RrMinWidth(RrAppearance *a)
414 RrMargins(a, &l, &t, &r, &b);
416 for (i = 0; i < a->textures; ++i) {
417 switch (a->texture[i].type) {
418 case RR_TEXTURE_NONE:
420 case RR_TEXTURE_MASK:
421 w = MAX(w, a->texture[i].data.mask.mask->width);
423 case RR_TEXTURE_TEXT:
424 m = RrFontMeasureString(a->texture[i].data.text.font,
425 a->texture[i].data.text.string,
426 a->texture[i].data.text.shadow_offset_x,
427 a->texture[i].data.text.shadow_offset_y,
428 a->texture[i].data.text.flow,
429 a->texture[i].data.text.maxwidth);
430 w = MAX(w, m->width);
431 g_slice_free(RrSize, m);
433 case RR_TEXTURE_RGBA:
434 w += MAX(w, a->texture[i].data.rgba.width);
436 case RR_TEXTURE_IMAGE:
437 /* images resize so they don't contribute anything to the min */
439 case RR_TEXTURE_LINE_ART:
440 w = MAX(w, MAX(a->texture[i].data.lineart.x1 - l - r,
441 a->texture[i].data.lineart.x2 - l - r));
443 case RR_TEXTURE_NUM_TYPES:
444 g_assert_not_reached();
454 gint RrMinHeight(RrAppearance *a)
461 RrMargins(a, &l, &t, &r, &b);
463 for (i = 0; i < a->textures; ++i) {
464 switch (a->texture[i].type) {
465 case RR_TEXTURE_NONE:
467 case RR_TEXTURE_MASK:
468 h = MAX(h, a->texture[i].data.mask.mask->height);
470 case RR_TEXTURE_TEXT:
471 if (a->texture[i].data.text.flow) {
472 g_assert(a->texture[i].data.text.string != NULL);
474 m = RrFontMeasureString
475 (a->texture[i].data.text.font,
476 a->texture[i].data.text.string,
477 a->texture[i].data.text.shadow_offset_x,
478 a->texture[i].data.text.shadow_offset_y,
479 a->texture[i].data.text.flow,
480 a->texture[i].data.text.maxwidth);
481 h += MAX(h, m->height);
482 g_slice_free(RrSize, m);
487 (a->texture[i].data.text.font,
488 a->texture[i].data.text.shadow_offset_y));
490 case RR_TEXTURE_RGBA:
491 h += MAX(h, a->texture[i].data.rgba.height);
493 case RR_TEXTURE_IMAGE:
494 /* images resize so they don't contribute anything to the min */
496 case RR_TEXTURE_LINE_ART:
497 h = MAX(h, MAX(a->texture[i].data.lineart.y1 - t - b,
498 a->texture[i].data.lineart.y2 - t - b));
500 case RR_TEXTURE_NUM_TYPES:
501 g_assert_not_reached();
511 static void reverse_bits(gchar *c, gint n)
514 for (i = 0; i < n; i++, c++)
515 *c = (((*c * 0x0802UL & 0x22110UL) |
516 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
519 gboolean RrPixmapToRGBA(const RrInstance *inst,
520 Pixmap pmap, Pixmap mask,
521 gint *w, gint *h, RrPixel32 **data)
525 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
526 XImage *xi, *xm = NULL;
528 if (!XGetGeometry(RrDisplay(inst), pmap,
529 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
533 if (!XGetGeometry(RrDisplay(inst), mask,
534 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
536 if (pw != mw || ph != mh || xd != 1)
540 xi = XGetImage(RrDisplay(inst), pmap,
541 0, 0, pw, ph, 0xffffffff, ZPixmap);
546 xm = XGetImage(RrDisplay(inst), mask,
547 0, 0, mw, mh, 0xffffffff, ZPixmap);
552 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
553 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
556 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
557 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
559 *data = g_new(RrPixel32, pw * ph);
560 RrIncreaseDepth(inst, *data, xi);
563 /* apply transparency from the mask */
565 for (i = 0, y = 0; y < ph; ++y) {
566 for (x = 0; x < pw; ++x, ++i) {
567 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
568 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
570 di += xm->bytes_per_line;