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