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