]> Dogcows Code - chaz/openbox/blob - render/render.c
add a reconfigure action, also reconfigure on SIGUSR2.
[chaz/openbox] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3
4 #include "render.h"
5 #include "gradient.h"
6 #include "font.h"
7 #include "mask.h"
8 #include "color.h"
9 #include "image.h"
10 #include "theme.h"
11
12 #include <glib.h>
13
14 #ifdef HAVE_STDLIB_H
15 # include <stdlib.h>
16 #endif
17
18 static void pixel_data_to_pixmap(RrAppearance *l,
19 gint x, gint y, gint w, gint h);
20
21 void RrPaint(RrAppearance *a, Window win, gint w, gint h)
22 {
23 int i, transferred = 0, sw;
24 RrPixel32 *source, *dest;
25 Pixmap oldp;
26 RrRect tarea; /* area in which to draw textures */
27 gboolean resized;
28
29 if (w <= 0 || h <= 0) return;
30
31 resized = (a->w != w || a->h != h);
32
33 oldp = a->pixmap; /* save to free after changing the visible pixmap */
34 a->pixmap = XCreatePixmap(RrDisplay(a->inst),
35 RrRootWindow(a->inst),
36 w, h, RrDepth(a->inst));
37
38 g_assert(a->pixmap != None);
39 a->w = w;
40 a->h = h;
41
42 if (a->xftdraw != NULL)
43 XftDrawDestroy(a->xftdraw);
44 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
45 RrVisual(a->inst), RrColormap(a->inst));
46 g_assert(a->xftdraw != NULL);
47
48 g_free(a->surface.pixel_data);
49 a->surface.pixel_data = g_new(RrPixel32, w * h);
50
51 if (a->surface.grad == RR_SURFACE_PARENTREL) {
52 g_assert (a->surface.parent);
53 g_assert (a->surface.parent->w);
54
55 sw = a->surface.parent->w;
56 source = (a->surface.parent->surface.pixel_data +
57 a->surface.parentx + sw * a->surface.parenty);
58 dest = a->surface.pixel_data;
59 for (i = 0; i < h; i++, source += sw, dest += w) {
60 memcpy(dest, source, w * sizeof(RrPixel32));
61 }
62 } else
63 RrRender(a, w, h);
64
65 {
66 gint l, t, r, b;
67 RrMargins(a, &l, &t, &r, &b);
68 RECT_SET(tarea, l, t, w - l - r, h - t - b);
69 }
70
71 for (i = 0; i < a->textures; i++) {
72 switch (a->texture[i].type) {
73 case RR_TEXTURE_NONE:
74 break;
75 case RR_TEXTURE_TEXT:
76 if (!transferred) {
77 transferred = 1;
78 if (a->surface.grad != RR_SURFACE_SOLID)
79 pixel_data_to_pixmap(a, 0, 0, w, h);
80 }
81 if (a->xftdraw == NULL) {
82 a->xftdraw = XftDrawCreate(RrDisplay(a->inst), a->pixmap,
83 RrVisual(a->inst),
84 RrColormap(a->inst));
85 }
86 RrFontDraw(a->xftdraw, &a->texture[i].data.text, &tarea);
87 break;
88 case RR_TEXTURE_LINE_ART:
89 if (!transferred) {
90 transferred = 1;
91 if (a->surface.grad != RR_SURFACE_SOLID)
92 pixel_data_to_pixmap(a, 0, 0, w, h);
93 }
94 XDrawLine(RrDisplay(a->inst), a->pixmap,
95 RrColorGC(a->texture[i].data.lineart.color),
96 a->texture[i].data.lineart.x1,
97 a->texture[i].data.lineart.y1,
98 a->texture[i].data.lineart.x2,
99 a->texture[i].data.lineart.y2);
100 break;
101 case RR_TEXTURE_MASK:
102 if (!transferred) {
103 transferred = 1;
104 if (a->surface.grad != RR_SURFACE_SOLID)
105 pixel_data_to_pixmap(a, 0, 0, w, h);
106 }
107 RrPixmapMaskDraw(a->pixmap, &a->texture[i].data.mask, &tarea);
108 break;
109 case RR_TEXTURE_RGBA:
110 g_assert(!transferred);
111 RrImageDraw(a->surface.pixel_data,
112 &a->texture[i].data.rgba, &tarea);
113 break;
114 }
115 }
116
117 if (!transferred) {
118 transferred = 1;
119 if (a->surface.grad != RR_SURFACE_SOLID)
120 pixel_data_to_pixmap(a, 0, 0, w, h);
121 }
122
123 XSetWindowBackgroundPixmap(RrDisplay(a->inst), win, a->pixmap);
124 XClearWindow(RrDisplay(a->inst), win);
125 if (oldp) XFreePixmap(RrDisplay(a->inst), oldp);
126 }
127
128 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
129 {
130 RrAppearance *out;
131
132 out = g_new0(RrAppearance, 1);
133 out->inst = inst;
134 out->textures = numtex;
135 if (numtex) out->texture = g_new0(RrTexture, numtex);
136
137 return out;
138 }
139
140 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
141 {
142 RrSurface *spo, *spc;
143 RrAppearance *copy = g_new(RrAppearance, 1);
144 gint i;
145
146 copy->inst = orig->inst;
147
148 spo = &(orig->surface);
149 spc = &(copy->surface);
150 spc->grad = spo->grad;
151 spc->relief = spo->relief;
152 spc->bevel = spo->bevel;
153 if (spo->primary != NULL)
154 spc->primary = RrColorNew(copy->inst,
155 spo->primary->r,
156 spo->primary->g,
157 spo->primary->b);
158 else spc->primary = NULL;
159
160 if (spo->secondary != NULL)
161 spc->secondary = RrColorNew(copy->inst,
162 spo->secondary->r,
163 spo->secondary->g,
164 spo->secondary->b);
165 else spc->secondary = NULL;
166
167 if (spo->border_color != NULL)
168 spc->border_color = RrColorNew(copy->inst,
169 spo->border_color->r,
170 spo->border_color->g,
171 spo->border_color->b);
172 else spc->border_color = NULL;
173
174 if (spo->interlace_color != NULL)
175 spc->interlace_color = RrColorNew(copy->inst,
176 spo->interlace_color->r,
177 spo->interlace_color->g,
178 spo->interlace_color->b);
179 else spc->interlace_color = NULL;
180
181 if (spo->bevel_dark != NULL)
182 spc->bevel_dark = RrColorNew(copy->inst,
183 spo->bevel_dark->r,
184 spo->bevel_dark->g,
185 spo->bevel_dark->b);
186 else spc->bevel_dark = NULL;
187
188 if (spo->bevel_light != NULL)
189 spc->bevel_light = RrColorNew(copy->inst,
190 spo->bevel_light->r,
191 spo->bevel_light->g,
192 spo->bevel_light->b);
193 else spc->bevel_light = NULL;
194
195 spc->interlaced = spo->interlaced;
196 spc->border = spo->border;
197 spc->parent = NULL;
198 spc->parentx = spc->parenty = 0;
199 spc->pixel_data = NULL;
200
201 copy->textures = orig->textures;
202 copy->texture = g_memdup(orig->texture,
203 orig->textures * sizeof(RrTexture));
204 for (i = 0; i < copy->textures; ++i)
205 if (copy->texture[i].type == RR_TEXTURE_RGBA) {
206 g_free(copy->texture[i].data.rgba.cache);
207 copy->texture[i].data.rgba.cache = NULL;
208 }
209 copy->pixmap = None;
210 copy->xftdraw = NULL;
211 copy->w = copy->h = 0;
212 return copy;
213 }
214
215 void RrAppearanceFree(RrAppearance *a)
216 {
217 gint i;
218
219 if (a) {
220 RrSurface *p;
221 if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
222 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
223 for (i = 0; i < a->textures; ++i)
224 if (a->texture[i].type == RR_TEXTURE_RGBA) {
225 g_free(a->texture[i].data.rgba.cache);
226 a->texture[i].data.rgba.cache = NULL;
227 }
228 if (a->textures)
229 g_free(a->texture);
230 p = &a->surface;
231 RrColorFree(p->primary);
232 RrColorFree(p->secondary);
233 RrColorFree(p->border_color);
234 RrColorFree(p->interlace_color);
235 RrColorFree(p->bevel_dark);
236 RrColorFree(p->bevel_light);
237 g_free(p->pixel_data);
238
239 g_free(a);
240 }
241 }
242
243
244 static void pixel_data_to_pixmap(RrAppearance *l,
245 gint x, gint y, gint w, gint h)
246 {
247 RrPixel32 *in, *scratch;
248 Pixmap out;
249 XImage *im = NULL;
250 im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
251 ZPixmap, 0, NULL, w, h, 32, 0);
252 g_assert(im != NULL);
253
254 in = l->surface.pixel_data;
255 out = l->pixmap;
256
257 /* this malloc is a complete waste of time on normal 32bpp
258 as reduce_depth just sets im->data = data and returns
259 */
260 scratch = g_new(RrPixel32, im->width * im->height);
261 im->data = (char*) scratch;
262 RrReduceDepth(l->inst, in, im);
263 XPutImage(RrDisplay(l->inst), out,
264 DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
265 im, 0, 0, x, y, w, h);
266 im->data = NULL;
267 XDestroyImage(im);
268 g_free(scratch);
269 }
270
271 void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
272 {
273 *l = *t = *r = *b = 0;
274
275 if (a->surface.grad != RR_SURFACE_PARENTREL) {
276 if (a->surface.relief != RR_RELIEF_FLAT) {
277 switch (a->surface.bevel) {
278 case RR_BEVEL_1:
279 *l = *t = *r = *b = 1;
280 break;
281 case RR_BEVEL_2:
282 *l = *t = *r = *b = 2;
283 break;
284 }
285 } else if (a->surface.border) {
286 *l = *t = *r = *b = 1;
287 }
288 }
289 }
290
291 void RrMinsize(RrAppearance *a, gint *w, gint *h)
292 {
293 gint i;
294 gint m;
295 gint l, t, r, b;
296 *w = *h = 0;
297
298 for (i = 0; i < a->textures; ++i) {
299 switch (a->texture[i].type) {
300 case RR_TEXTURE_NONE:
301 break;
302 case RR_TEXTURE_MASK:
303 *w = MAX(*w, a->texture[i].data.mask.mask->width);
304 *h = MAX(*h, a->texture[i].data.mask.mask->height);
305 break;
306 case RR_TEXTURE_TEXT:
307 m = RrFontMeasureString(a->texture[i].data.text.font,
308 a->texture[i].data.text.string);
309 *w = MAX(*w, m);
310 m = RrFontHeight(a->texture[i].data.text.font);
311 *h += MAX(*h, m);
312 break;
313 case RR_TEXTURE_RGBA:
314 *w += MAX(*w, a->texture[i].data.rgba.width);
315 *h += MAX(*h, a->texture[i].data.rgba.height);
316 break;
317 case RR_TEXTURE_LINE_ART:
318 *w += MAX(*w, MAX(a->texture[i].data.lineart.x1,
319 a->texture[i].data.lineart.x2));
320 *h += MAX(*h, MAX(a->texture[i].data.lineart.y1,
321 a->texture[i].data.lineart.y2));
322 break;
323 }
324 }
325
326 RrMargins(a, &l, &t, &r, &b);
327
328 *w += l + r;
329 *h += t + b;
330
331 if (*w < 1) *w = 1;
332 if (*h < 1) *h = 1;
333 }
334
335 gboolean RrPixmapToRGBA(const RrInstance *inst,
336 Pixmap pmap, Pixmap mask,
337 gint *w, gint *h, RrPixel32 **data)
338 {
339 Window xr;
340 gint xx, xy;
341 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
342 XImage *xi, *xm = NULL;
343
344 if (!XGetGeometry(RrDisplay(inst),
345 pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
346 return FALSE;
347 if (mask) {
348 if (!XGetGeometry(RrDisplay(inst), mask,
349 &xr, &xx, &xy, &mw, &mh, &xb, &xd))
350 return FALSE;
351 if (pw != mw || ph != mh || xd != 1)
352 return FALSE;
353 }
354
355 xi = XGetImage(RrDisplay(inst), pmap,
356 0, 0, pw, ph, 0xffffffff, ZPixmap);
357 if (!xi)
358 return FALSE;
359
360 if (mask) {
361 xm = XGetImage(RrDisplay(inst), mask,
362 0, 0, mw, mh, 0xffffffff, ZPixmap);
363 if (!xm)
364 return FALSE;
365 }
366
367 *data = g_new(RrPixel32, pw * ph);
368 RrIncreaseDepth(inst, *data, xi);
369
370 if (mask) {
371 /* apply transparency from the mask */
372 di = 0;
373 for (i = 0, y = 0; y < ph; ++y) {
374 for (x = 0; x < pw; ++x, ++i) {
375 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
376 (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
377 }
378 di += xm->bytes_per_line;
379 }
380 }
381
382 *w = pw;
383 *h = ph;
384
385 return TRUE;
386 }
This page took 0.056184 seconds and 4 git commands to generate.