]> Dogcows Code - chaz/openbox/blob - render/render.c
Merge branch 'backport' into work
[chaz/openbox] / 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_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 }
174 }
175
176 if (!transferred) {
177 transferred = 1;
178 if ((a->surface.grad != RR_SURFACE_SOLID) || (a->surface.interlaced) ||
179 force_transfer)
180 {
181 pixel_data_to_pixmap(a, 0, 0, w, h);
182 }
183 }
184
185 return oldp;
186 }
187
188 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
189 {
190 Pixmap oldp;
191
192 oldp = RrPaintPixmap(a, w, h);
193 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
194 XClearWindow(RrDisplay(a->inst), win);
195 /* free this after changing the visible pixmap */
196 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
197 }
198
199 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
200 {
201 RrAppearance *out;
202
203 out = g_new0(RrAppearance, 1);
204 out->inst = inst;
205 out->textures = numtex;
206 out->surface.bevel_light_adjust = 128;
207 out->surface.bevel_dark_adjust = 64;
208 if (numtex) out->texture = g_new0(RrTexture, numtex);
209
210 return out;
211 }
212
213 void RrAppearanceRemoveTextures(RrAppearance *a)
214 {
215 g_free(a->texture);
216 a->textures = 0;
217 }
218
219 void RrAppearanceAddTextures(RrAppearance *a, gint numtex)
220 {
221 g_assert(a->textures == 0);
222
223 a->textures = numtex;
224 if (numtex) a->texture = g_new0(RrTexture, numtex);
225 }
226
227 void RrAppearanceClearTextures(RrAppearance *a)
228 {
229 memset(a->texture, 0, a->textures * sizeof(RrTexture));
230 }
231
232 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
233 {
234 RrSurface *spo, *spc;
235 RrAppearance *copy = g_new(RrAppearance, 1);
236
237 copy->inst = orig->inst;
238
239 spo = &(orig->surface);
240 spc = &(copy->surface);
241 spc->grad = spo->grad;
242 spc->relief = spo->relief;
243 spc->bevel = spo->bevel;
244 if (spo->primary != NULL)
245 spc->primary = RrColorNew(copy->inst,
246 spo->primary->r,
247 spo->primary->g,
248 spo->primary->b);
249 else spc->primary = NULL;
250
251 if (spo->secondary != NULL)
252 spc->secondary = RrColorNew(copy->inst,
253 spo->secondary->r,
254 spo->secondary->g,
255 spo->secondary->b);
256 else spc->secondary = NULL;
257
258 if (spo->border_color != NULL)
259 spc->border_color = RrColorNew(copy->inst,
260 spo->border_color->r,
261 spo->border_color->g,
262 spo->border_color->b);
263 else spc->border_color = NULL;
264
265 if (spo->interlace_color != NULL)
266 spc->interlace_color = RrColorNew(copy->inst,
267 spo->interlace_color->r,
268 spo->interlace_color->g,
269 spo->interlace_color->b);
270 else spc->interlace_color = NULL;
271
272 if (spo->bevel_dark != NULL)
273 spc->bevel_dark = RrColorNew(copy->inst,
274 spo->bevel_dark->r,
275 spo->bevel_dark->g,
276 spo->bevel_dark->b);
277 else spc->bevel_dark = NULL;
278
279 if (spo->bevel_light != NULL)
280 spc->bevel_light = RrColorNew(copy->inst,
281 spo->bevel_light->r,
282 spo->bevel_light->g,
283 spo->bevel_light->b);
284 else spc->bevel_light = NULL;
285
286 if (spo->split_primary != NULL)
287 spc->split_primary = RrColorNew(copy->inst,
288 spo->split_primary->r,
289 spo->split_primary->g,
290 spo->split_primary->b);
291 else spc->split_primary = NULL;
292
293 if (spo->split_secondary != NULL)
294 spc->split_secondary = RrColorNew(copy->inst,
295 spo->split_secondary->r,
296 spo->split_secondary->g,
297 spo->split_secondary->b);
298 else spc->split_secondary = NULL;
299
300 spc->interlaced = spo->interlaced;
301 spc->bevel_light_adjust = spo->bevel_light_adjust;
302 spc->bevel_dark_adjust = spo->bevel_dark_adjust;
303 spc->border = spo->border;
304 spc->parent = NULL;
305 spc->parentx = spc->parenty = 0;
306 spc->pixel_data = NULL;
307
308 copy->textures = orig->textures;
309 copy->texture = g_memdup(orig->texture,
310 orig->textures * sizeof(RrTexture));
311 copy->pixmap = None;
312 copy->xftdraw = NULL;
313 copy->w = copy->h = 0;
314 return copy;
315 }
316
317 void RrAppearanceFree(RrAppearance *a)
318 {
319 if (a) {
320 RrSurface *p;
321 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
322 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
323 if (a->textures)
324 g_free(a->texture);
325 p = &a->surface;
326 RrColorFree(p->primary);
327 RrColorFree(p->secondary);
328 RrColorFree(p->border_color);
329 RrColorFree(p->interlace_color);
330 RrColorFree(p->bevel_dark);
331 RrColorFree(p->bevel_light);
332 RrColorFree(p->split_primary);
333 RrColorFree(p->split_secondary);
334 g_free(p->pixel_data);
335 p->pixel_data = NULL;
336 g_free(a);
337 }
338 }
339
340 static void pixel_data_to_pixmap(RrAppearance *l,
341 gint x, gint y, gint w, gint h)
342 {
343 RrPixel32 *in, *scratch;
344 Pixmap out;
345 XImage *im = NULL;
346 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
347 ZPixmap, 0, NULL, w, h, 32, 0);
348 g_assert(im != NULL);
349
350 in = l->surface.pixel_data;
351 out = l->pixmap;
352
353 /* this malloc is a complete waste of time on normal 32bpp
354 as reduce_depth just sets im->data = data and returns
355 */
356 scratch = g_new(RrPixel32, im->width * im->height);
357 im->data = (gchar*) scratch;
358 RrReduceDepth(l->inst, in, im);
359 XPutImage(RrDisplay(l->inst), out,
360 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
361 im, 0, 0, x, y, w, h);
362 im->data = NULL;
363 XDestroyImage(im);
364 g_free(scratch);
365 }
366
367 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
368 {
369 *l = *t = *r = *b = 0;
370
371 if (a->surface.grad != RR_SURFACE_PARENTREL) {
372 if (a->surface.relief != RR_RELIEF_FLAT) {
373 switch (a->surface.bevel) {
374 case RR_BEVEL_1:
375 *l = *t = *r = *b = 1;
376 break;
377 case RR_BEVEL_2:
378 *l = *t = *r = *b = 2;
379 break;
380 }
381 } else if (a->surface.border) {
382 *l = *t = *r = *b = 1;
383 }
384 }
385 }
386
387 void RrMinSize(RrAppearance *a, gint *w, gint *h)
388 {
389 *w = RrMinWidth(a);
390 *h = RrMinHeight(a);
391 }
392
393 gint RrMinWidth(RrAppearance *a)
394 {
395 gint i;
396 RrSize *m;
397 gint l, t, r, b;
398 gint w = 0;
399
400 RrMargins(a, &l, &t, &r, &b);
401
402 for (i = 0; i < a->textures; ++i) {
403 switch (a->texture[i].type) {
404 case RR_TEXTURE_NONE:
405 break;
406 case RR_TEXTURE_MASK:
407 w = MAX(w, a->texture[i].data.mask.mask->width);
408 break;
409 case RR_TEXTURE_TEXT:
410 m = RrFontMeasureString(a->texture[i].data.text.font,
411 a->texture[i].data.text.string,
412 a->texture[i].data.text.shadow_offset_x,
413 a->texture[i].data.text.shadow_offset_y,
414 a->texture[i].data.text.flow,
415 a->texture[i].data.text.maxwidth);
416 w = MAX(w, m->width);
417 g_free(m);
418 break;
419 case RR_TEXTURE_RGBA:
420 w += MAX(w, a->texture[i].data.rgba.width);
421 break;
422 case RR_TEXTURE_IMAGE:
423 /* images resize so they don't contribute anything to the min */
424 break;
425 case RR_TEXTURE_LINE_ART:
426 w = MAX(w, MAX(a->texture[i].data.lineart.x1 - l - r,
427 a->texture[i].data.lineart.x2 - l - r));
428 break;
429 }
430 }
431
432 w += l + r;
433
434 if (w < 1) w = 1;
435 return w;
436 }
437
438 gint RrMinHeight(RrAppearance *a)
439 {
440 gint i;
441 gint l, t, r, b;
442 RrSize *m;
443 gint h = 0;
444
445 RrMargins(a, &l, &t, &r, &b);
446
447 for (i = 0; i < a->textures; ++i) {
448 switch (a->texture[i].type) {
449 case RR_TEXTURE_NONE:
450 break;
451 case RR_TEXTURE_MASK:
452 h = MAX(h, a->texture[i].data.mask.mask->height);
453 break;
454 case RR_TEXTURE_TEXT:
455 if (a->texture[i].data.text.flow) {
456 g_assert(a->texture[i].data.text.string != NULL);
457
458 m = RrFontMeasureString
459 (a->texture[i].data.text.font,
460 a->texture[i].data.text.string,
461 a->texture[i].data.text.shadow_offset_x,
462 a->texture[i].data.text.shadow_offset_y,
463 a->texture[i].data.text.flow,
464 a->texture[i].data.text.maxwidth);
465 h += MAX(h, m->height);
466 g_free(m);
467 }
468 else
469 h += MAX(h,
470 RrFontHeight
471 (a->texture[i].data.text.font,
472 a->texture[i].data.text.shadow_offset_y));
473 break;
474 case RR_TEXTURE_RGBA:
475 h += MAX(h, a->texture[i].data.rgba.height);
476 break;
477 case RR_TEXTURE_IMAGE:
478 /* images resize so they don't contribute anything to the min */
479 break;
480 case RR_TEXTURE_LINE_ART:
481 h = MAX(h, MAX(a->texture[i].data.lineart.y1 - t - b,
482 a->texture[i].data.lineart.y2 - t - b));
483 break;
484 }
485 }
486
487 h += t + b;
488
489 if (h < 1) h = 1;
490 return h;
491 }
492
493 static void reverse_bits(gchar *c, gint n)
494 {
495 gint i;
496 for (i = 0; i < n; i++, c++)
497 *c = (((*c * 0x0802UL & 0x22110UL) |
498 (*c * 0x8020UL & 0x88440UL)) * 0x10101UL) >> 16;
499 }
500
501 gboolean RrPixmapToRGBA(const RrInstance *inst,
502 Pixmap pmap, Pixmap mask,
503 gint *w, gint *h, RrPixel32 **data)
504 {
505 Window xr;
506 gint xx, xy;
507 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
508 XImage *xi, *xm = NULL;
509
510 if (!XGetGeometry(RrDisplay(inst), pmap,
511 &xr, &xx, &xy, &pw, &ph, &xb, &xd))
512 return FALSE;
513
514 if (mask) {
515 if (!XGetGeometry(RrDisplay(inst), mask,
516 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
517 return FALSE;
518 if (pw != mw || ph != mh || xd != 1)
519 return FALSE;
520 }
521
522 xi = XGetImage(RrDisplay(inst), pmap,
523 0, 0, pw, ph, 0xffffffff, ZPixmap);
524 if (!xi)
525 return FALSE;
526
527 if (mask) {
528 xm = XGetImage(RrDisplay(inst), mask,
529 0, 0, mw, mh, 0xffffffff, ZPixmap);
530 if (!xm) {
531 XDestroyImage(xi);
532 return FALSE;
533 }
534 if ((xm->bits_per_pixel == 1) && (xm->bitmap_bit_order != LSBFirst))
535 reverse_bits(xm->data, xm->bytes_per_line * xm->height);
536 }
537
538 if ((xi->bits_per_pixel == 1) && (xi->bitmap_bit_order != LSBFirst))
539 reverse_bits(xi->data, xi->bytes_per_line * xi->height);
540
541 *data = g_new(RrPixel32, pw * ph);
542 RrIncreaseDepth(inst, *data, xi);
543
544 if (mask) {
545 /* apply transparency from the mask */
546 di = 0;
547 for (i = 0, y = 0; y < ph; ++y) {
548 for (x = 0; x < pw; ++x, ++i) {
549 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
550 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
551 }
552 di += xm->bytes_per_line;
553 }
554 }
555
556 *w = pw;
557 *h = ph;
558
559 XDestroyImage(xi);
560 if (mask)
561 XDestroyImage(xm);
562
563 return TRUE;
564 }
This page took 0.056914 seconds and 4 git commands to generate.