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