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 Ben 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.
22 #include <X11/Xutil.h>
38 static void pixel_data_to_pixmap(RrAppearance *l,
39 gint x, gint y, gint w, gint h);
41 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
43 gint i, transferred = 0, sw, sh, partial_w, partial_h;
44 RrPixel32 *source, *dest;
46 RrRect tarea; /* area in which to draw textures */
49 if (w <= 0 || h <= 0) return;
51 if (a->surface.parentx < 0 || a->surface.parenty < 0) {
52 /* ob_debug("Invalid parent co-ordinates\n"); */
55 resized = (a->w != w || a->h != h);
57 oldp = a->pixmap; /* save to free after changing the visible pixmap */
58 a->pixmap = XCreatePixmap(RrDisplay(a->inst),
59 RrRootWindow(a->inst),
60 w, h, RrDepth(a->inst));
62 g_assert(a->pixmap != None);
66 if (a->xftdraw != NULL)
67 XftDrawDestroy(a->xftdraw);
68 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
69 RrVisual(a->inst), RrColormap(a->inst));
70 g_assert(a->xftdraw != NULL);
72 g_free(a->surface.pixel_data);
73 a->surface.pixel_data = g_new(RrPixel32, w * h);
75 if (a->surface.grad == RR_SURFACE_PARENTREL) {
76 g_assert (a->surface.parent);
77 g_assert (a->surface.parent->w);
79 sw = a->surface.parent->w;
80 sh = a->surface.parent->h;
82 if (a->surface.parentx >= sw || a->surface.parenty >= sh) {
86 source = (a->surface.parent->surface.pixel_data +
87 a->surface.parentx + sw * a->surface.parenty);
88 dest = a->surface.pixel_data;
90 if (a->surface.parentx + w > sw) {
91 partial_w = sw - a->surface.parentx;
94 if (a->surface.parenty + h > sh) {
95 partial_h = sh - a->surface.parenty;
98 for (i = 0; i < partial_h; i++, source += sw, dest += w) {
99 memcpy(dest, source, partial_w * sizeof(RrPixel32));
106 RrMargins(a, &l, &t, &r, &b);
107 RECT_SET(tarea, l, t, w - l - r, h - t - b);
110 for (i = 0; i < a->textures; i++) {
111 switch (a->texture[i].type) {
112 case RR_TEXTURE_NONE:
114 case RR_TEXTURE_TEXT:
117 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
118 pixel_data_to_pixmap(a, 0, 0, w, h);
120 if (a->xftdraw == NULL) {
121 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
123 RrColormap(a->inst));
125 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
127 case RR_TEXTURE_LINE_ART:
130 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
131 pixel_data_to_pixmap(a, 0, 0, w, h);
133 XDrawLine(RrDisplay(a->inst), a->pixmap,
134 RrColorGC(a->texture[i].data.lineart.color),
135 a->texture[i].data.lineart.x1,
136 a->texture[i].data.lineart.y1,
137 a->texture[i].data.lineart.x2,
138 a->texture[i].data.lineart.y2);
140 case RR_TEXTURE_MASK:
143 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
144 pixel_data_to_pixmap(a, 0, 0, w, h);
146 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
148 case RR_TEXTURE_RGBA:
149 g_assert(!transferred);
150 RrImageDraw(a->surface.pixel_data,
151 &a->texture[i].data.rgba,
160 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced))
161 pixel_data_to_pixmap(a, 0, 0, w, h);
164 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
165 XClearWindow(RrDisplay(a->inst), win);
166 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
169 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
173 out = g_new0(RrAppearance, 1);
175 out->textures = numtex;
176 if (numtex) out->texture = g_new0(RrTexture, numtex);
181 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
183 RrSurface *spo, *spc;
184 RrAppearance *copy = g_new(RrAppearance, 1);
187 copy->inst = orig->inst;
189 spo = &(orig->surface);
190 spc = &(copy->surface);
191 spc->grad = spo->grad;
192 spc->relief = spo->relief;
193 spc->bevel = spo->bevel;
194 if (spo->primary != NULL)
195 spc->primary = RrColorNew(copy->inst,
199 else spc->primary = NULL;
201 if (spo->secondary != NULL)
202 spc->secondary = RrColorNew(copy->inst,
206 else spc->secondary = NULL;
208 if (spo->border_color != NULL)
209 spc->border_color = RrColorNew(copy->inst,
210 spo->border_color->r,
211 spo->border_color->g,
212 spo->border_color->b);
213 else spc->border_color = NULL;
215 if (spo->interlace_color != NULL)
216 spc->interlace_color = RrColorNew(copy->inst,
217 spo->interlace_color->r,
218 spo->interlace_color->g,
219 spo->interlace_color->b);
220 else spc->interlace_color = NULL;
222 if (spo->bevel_dark != NULL)
223 spc->bevel_dark = RrColorNew(copy->inst,
227 else spc->bevel_dark = NULL;
229 if (spo->bevel_light != NULL)
230 spc->bevel_light = RrColorNew(copy->inst,
233 spo->bevel_light->b);
234 else spc->bevel_light = NULL;
236 spc->interlaced = spo->interlaced;
237 spc->border = spo->border;
239 spc->parentx = spc->parenty = 0;
240 spc->pixel_data = NULL;
242 copy->textures = orig->textures;
243 copy->texture = g_memdup(orig->texture,
244 orig->textures * sizeof(RrTexture));
245 for (i = 0; i < copy->textures; ++i)
246 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
247 copy->texture[i].data.rgba.cache = NULL;
250 copy->xftdraw = NULL;
251 copy->w = copy->h = 0;
255 void RrAppearanceFree(RrAppearance *a)
261 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
262 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
263 for (i = 0; i < a->textures; ++i)
264 if (a->texture[i].type == RR_TEXTURE_RGBA) {
265 g_free(a->texture[i].data.rgba.cache);
266 a->texture[i].data.rgba.cache = NULL;
271 RrColorFree(p->primary);
272 RrColorFree(p->secondary);
273 RrColorFree(p->border_color);
274 RrColorFree(p->interlace_color);
275 RrColorFree(p->bevel_dark);
276 RrColorFree(p->bevel_light);
277 g_free(p->pixel_data);
278 p->pixel_data = NULL;
284 static void pixel_data_to_pixmap(RrAppearance *l,
285 gint x, gint y, gint w, gint h)
287 RrPixel32 *in, *scratch;
290 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
291 ZPixmap, 0, NULL, w, h, 32, 0);
292 g_assert(im != NULL);
294 in = l->surface.pixel_data;
297 /* this malloc is a complete waste of time on normal 32bpp
298 as reduce_depth just sets im->data = data and returns
300 scratch = g_new(RrPixel32, im->width * im->height);
301 im->data = (gchar*) scratch;
302 RrReduceDepth(l->inst, in, im);
303 XPutImage(RrDisplay(l->inst), out,
304 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
305 im, 0, 0, x, y, w, h);
311 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
313 *l = *t = *r = *b = 0;
315 if (a->surface.grad != RR_SURFACE_PARENTREL) {
316 if (a->surface.relief != RR_RELIEF_FLAT) {
317 switch (a->surface.bevel) {
319 *l = *t = *r = *b = 1;
322 *l = *t = *r = *b = 2;
325 } else if (a->surface.border) {
326 *l = *t = *r = *b = 1;
331 void RrMinsize(RrAppearance *a, gint *w, gint *h)
338 for (i = 0; i < a->textures; ++i) {
339 switch (a->texture[i].type) {
340 case RR_TEXTURE_NONE:
342 case RR_TEXTURE_MASK:
343 *w = MAX(*w, a->texture[i].data.mask.mask->width);
344 *h = MAX(*h, a->texture[i].data.mask.mask->height);
346 case RR_TEXTURE_TEXT:
347 m = RrFontMeasureString(a->texture[i].data.text.font,
348 a->texture[i].data.text.string);
349 *w = MAX(*w, m->width + 4);
350 m->height = RrFontHeight(a->texture[i].data.text.font);
351 *h += MAX(*h, m->height);
354 case RR_TEXTURE_RGBA:
355 *w += MAX(*w, a->texture[i].data.rgba.width);
356 *h += MAX(*h, a->texture[i].data.rgba.height);
358 case RR_TEXTURE_LINE_ART:
359 *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
360 a->texture[i].data.lineart.x2));
361 *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
362 a->texture[i].data.lineart.y2));
367 RrMargins(a, &l, &t, &r, &b);
376 static void reverse_bits(gchar *c, gint n)
379 for (i = 0; i < n; i++, c++)
380 *c = (((*c * 0x0802UL & 0x22110UL) |
381 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
384 gboolean RrPixmapToRGBA(const RrInstance *inst,
385 Pixmap pmap, Pixmap mask,
386 gint *w, gint *h, RrPixel32 **data)
390 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
391 XImage *xi, *xm = NULL;
393 if (!XGetGeometry(RrDisplay(inst), pmap,
394 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
398 if (!XGetGeometry(RrDisplay(inst), mask,
399 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
401 if (pw != mw || ph != mh || xd != 1)
405 xi = XGetImage(RrDisplay(inst), pmap,
406 0, 0, pw, ph, 0xffffffff, ZPixmap);
411 xm = XGetImage(RrDisplay(inst), mask,
412 0, 0, mw, mh, 0xffffffff, ZPixmap);
417 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
418 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
421 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
422 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
424 *data = g_new(RrPixel32, pw * ph);
425 RrIncreaseDepth(inst, *data, xi);
428 /* apply transparency from the mask */
430 for (i = 0, y = 0; y < ph; ++y) {
431 for (x = 0; x < pw; ++x, ++i) {
432 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
433 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
435 di += xm->bytes_per_line;