]>
Dogcows Code - chaz/openbox/blob - render/render.c
82e6f96e3d155d1a0830eca6a7742ded5a0f9a8e
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
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
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.
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.
18 See the COPYING file for a copy of the GNU General Public License.
31 #include <X11/Xutil.h>
32 #include <X11/Xft/Xft.h>
38 static void pixel_data_to_pixmap(RrAppearance
*l
,
39 gint x
, gint y
, gint w
, gint h
);
41 Pixmap
RrPaintPixmap(RrAppearance
*a
, gint w
, gint h
)
43 gint i
, transferred
= 0, sw
, sh
, partial_w
, partial_h
;
44 RrPixel32
*source
, *dest
;
46 RrRect tarea
; /* area in which to draw textures */
49 if (w
<= 0 || h
<= 0) return None
;
51 if (a
->surface
.parentx
< 0 || a
->surface
.parenty
< 0) {
52 /* ob_debug("Invalid parent co-ordinates\n"); */
56 if (a
->surface
.grad
== RR_SURFACE_PARENTREL
&&
57 (a
->surface
.parentx
>= a
->surface
.parent
->w
||
58 a
->surface
.parenty
>= a
->surface
.parent
->h
))
63 resized
= (a
->w
!= w
|| a
->h
!= h
);
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
));
70 g_assert(a
->pixmap
!= None
);
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
);
81 g_free(a
->surface
.pixel_data
);
82 a
->surface
.pixel_data
= g_new(RrPixel32
, w
* h
);
85 if (a
->surface
.grad
== RR_SURFACE_PARENTREL
) {
86 g_assert (a
->surface
.parent
);
87 g_assert (a
->surface
.parent
->w
);
89 sw
= a
->surface
.parent
->w
;
90 sh
= a
->surface
.parent
->h
;
92 source
= (a
->surface
.parent
->surface
.pixel_data
+
93 a
->surface
.parentx
+ sw
* a
->surface
.parenty
);
94 dest
= a
->surface
.pixel_data
;
96 if (a
->surface
.parentx
+ w
> sw
) {
97 partial_w
= sw
- a
->surface
.parentx
;
100 if (a
->surface
.parenty
+ h
> sh
) {
101 partial_h
= sh
- a
->surface
.parenty
;
102 } else partial_h
= h
;
104 for (i
= 0; i
< partial_h
; i
++, source
+= sw
, dest
+= w
) {
105 memcpy(dest
, source
, partial_w
* sizeof(RrPixel32
));
112 RrMargins(a
, &l
, &t
, &r
, &b
);
113 RECT_SET(tarea
, l
, t
, w
- l
- r
, h
- t
- b
);
116 for (i
= 0; i
< a
->textures
; i
++) {
117 switch (a
->texture
[i
].type
) {
118 case RR_TEXTURE_NONE
:
120 case RR_TEXTURE_TEXT
:
123 if ((a
->surface
.grad
!= RR_SURFACE_SOLID
)
124 || (a
->surface
.interlaced
))
125 pixel_data_to_pixmap(a
, 0, 0, w
, h
);
127 if (a
->xftdraw
== NULL
) {
128 a
->xftdraw
= XftDrawCreate(RrDisplay(a
->inst
), a
->pixmap
,
130 RrColormap(a
->inst
));
132 RrFontDraw(a
->xftdraw
, &a
->texture
[i
].data
.text
, &tarea
);
134 case RR_TEXTURE_LINE_ART
:
137 if ((a
->surface
.grad
!= RR_SURFACE_SOLID
)
138 || (a
->surface
.interlaced
))
139 pixel_data_to_pixmap(a
, 0, 0, w
, h
);
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
);
148 case RR_TEXTURE_MASK
:
151 if ((a
->surface
.grad
!= RR_SURFACE_SOLID
)
152 || (a
->surface
.interlaced
))
153 pixel_data_to_pixmap(a
, 0, 0, w
, h
);
155 RrPixmapMaskDraw(a
->pixmap
, &a
->texture
[i
].data
.mask
, &tarea
);
157 case RR_TEXTURE_RGBA
:
158 g_assert(!transferred
);
159 RrImageDraw(a
->surface
.pixel_data
,
160 &a
->texture
[i
].data
.rgba
,
169 if ((a
->surface
.grad
!= RR_SURFACE_SOLID
) || (a
->surface
.interlaced
))
170 pixel_data_to_pixmap(a
, 0, 0, w
, h
);
176 void RrPaint(RrAppearance
*a
, Window win
, gint w
, gint h
)
180 oldp
= RrPaintPixmap(a
, w
, h
);
181 XSetWindowBackgroundPixmap(RrDisplay(a
->inst
), win
, a
->pixmap
);
182 XClearWindow(RrDisplay(a
->inst
), win
);
183 /* free this after changing the visible pixmap */
184 if (oldp
) XFreePixmap(RrDisplay(a
->inst
), oldp
);
187 RrAppearance
*RrAppearanceNew(const RrInstance
*inst
, gint numtex
)
191 out
= g_new0(RrAppearance
, 1);
193 out
->textures
= numtex
;
194 if (numtex
) out
->texture
= g_new0(RrTexture
, numtex
);
199 void RrAppearanceAddTextures(RrAppearance
*a
, gint numtex
)
201 g_assert(a
->textures
== 0);
203 a
->textures
= numtex
;
204 if (numtex
) a
->texture
= g_new0(RrTexture
, numtex
);
207 RrAppearance
*RrAppearanceCopy(RrAppearance
*orig
)
209 RrSurface
*spo
, *spc
;
210 RrAppearance
*copy
= g_new(RrAppearance
, 1);
213 copy
->inst
= orig
->inst
;
215 spo
= &(orig
->surface
);
216 spc
= &(copy
->surface
);
217 spc
->grad
= spo
->grad
;
218 spc
->relief
= spo
->relief
;
219 spc
->bevel
= spo
->bevel
;
220 if (spo
->primary
!= NULL
)
221 spc
->primary
= RrColorNew(copy
->inst
,
225 else spc
->primary
= NULL
;
227 if (spo
->secondary
!= NULL
)
228 spc
->secondary
= RrColorNew(copy
->inst
,
232 else spc
->secondary
= NULL
;
234 if (spo
->border_color
!= NULL
)
235 spc
->border_color
= RrColorNew(copy
->inst
,
236 spo
->border_color
->r
,
237 spo
->border_color
->g
,
238 spo
->border_color
->b
);
239 else spc
->border_color
= NULL
;
241 if (spo
->interlace_color
!= NULL
)
242 spc
->interlace_color
= RrColorNew(copy
->inst
,
243 spo
->interlace_color
->r
,
244 spo
->interlace_color
->g
,
245 spo
->interlace_color
->b
);
246 else spc
->interlace_color
= NULL
;
248 if (spo
->bevel_dark
!= NULL
)
249 spc
->bevel_dark
= RrColorNew(copy
->inst
,
253 else spc
->bevel_dark
= NULL
;
255 if (spo
->bevel_light
!= NULL
)
256 spc
->bevel_light
= RrColorNew(copy
->inst
,
259 spo
->bevel_light
->b
);
260 else spc
->bevel_light
= NULL
;
262 spc
->interlaced
= spo
->interlaced
;
263 spc
->border
= spo
->border
;
265 spc
->parentx
= spc
->parenty
= 0;
266 spc
->pixel_data
= NULL
;
268 copy
->textures
= orig
->textures
;
269 copy
->texture
= g_memdup(orig
->texture
,
270 orig
->textures
* sizeof(RrTexture
));
271 for (i
= 0; i
< copy
->textures
; ++i
)
272 if (copy
->texture
[i
].type
== RR_TEXTURE_RGBA
) {
273 copy
->texture
[i
].data
.rgba
.cache
= NULL
;
276 copy
->xftdraw
= NULL
;
277 copy
->w
= copy
->h
= 0;
281 void RrAppearanceFree(RrAppearance
*a
)
287 if (a
->pixmap
!= None
) XFreePixmap(RrDisplay(a
->inst
), a
->pixmap
);
288 if (a
->xftdraw
!= NULL
) XftDrawDestroy(a
->xftdraw
);
289 for (i
= 0; i
< a
->textures
; ++i
)
290 if (a
->texture
[i
].type
== RR_TEXTURE_RGBA
) {
291 g_free(a
->texture
[i
].data
.rgba
.cache
);
292 a
->texture
[i
].data
.rgba
.cache
= NULL
;
297 RrColorFree(p
->primary
);
298 RrColorFree(p
->secondary
);
299 RrColorFree(p
->border_color
);
300 RrColorFree(p
->interlace_color
);
301 RrColorFree(p
->bevel_dark
);
302 RrColorFree(p
->bevel_light
);
303 g_free(p
->pixel_data
);
304 p
->pixel_data
= NULL
;
310 static void pixel_data_to_pixmap(RrAppearance
*l
,
311 gint x
, gint y
, gint w
, gint h
)
313 RrPixel32
*in
, *scratch
;
316 im
= XCreateImage(RrDisplay(l
->inst
), RrVisual(l
->inst
), RrDepth(l
->inst
),
317 ZPixmap
, 0, NULL
, w
, h
, 32, 0);
318 g_assert(im
!= NULL
);
320 in
= l
->surface
.pixel_data
;
323 /* this malloc is a complete waste of time on normal 32bpp
324 as reduce_depth just sets im->data = data and returns
326 scratch
= g_new(RrPixel32
, im
->width
* im
->height
);
327 im
->data
= (gchar
*) scratch
;
328 RrReduceDepth(l
->inst
, in
, im
);
329 XPutImage(RrDisplay(l
->inst
), out
,
330 DefaultGC(RrDisplay(l
->inst
), RrScreen(l
->inst
)),
331 im
, 0, 0, x
, y
, w
, h
);
337 void RrMargins (RrAppearance
*a
, gint
*l
, gint
*t
, gint
*r
, gint
*b
)
339 *l
= *t
= *r
= *b
= 0;
341 if (a
->surface
.grad
!= RR_SURFACE_PARENTREL
) {
342 if (a
->surface
.relief
!= RR_RELIEF_FLAT
) {
343 switch (a
->surface
.bevel
) {
345 *l
= *t
= *r
= *b
= 1;
348 *l
= *t
= *r
= *b
= 2;
351 } else if (a
->surface
.border
) {
352 *l
= *t
= *r
= *b
= 1;
357 void RrMinSize(RrAppearance
*a
, gint
*w
, gint
*h
)
363 gint
RrMinWidth(RrAppearance
*a
)
370 for (i
= 0; i
< a
->textures
; ++i
) {
371 switch (a
->texture
[i
].type
) {
372 case RR_TEXTURE_NONE
:
374 case RR_TEXTURE_MASK
:
375 w
= MAX(w
, a
->texture
[i
].data
.mask
.mask
->width
);
377 case RR_TEXTURE_TEXT
:
378 m
= RrFontMeasureString(a
->texture
[i
].data
.text
.font
,
379 a
->texture
[i
].data
.text
.string
,
380 a
->texture
[i
].data
.text
.shadow_offset_x
,
381 a
->texture
[i
].data
.text
.shadow_offset_y
);
382 w
= MAX(w
, m
->width
);
385 case RR_TEXTURE_RGBA
:
386 w
+= MAX(w
, a
->texture
[i
].data
.rgba
.width
);
388 case RR_TEXTURE_LINE_ART
:
389 w
+= MAX(w
, MAX(a
->texture
[i
].data
.lineart
.x1
,
390 a
->texture
[i
].data
.lineart
.x2
));
395 RrMargins(a
, &l
, &t
, &r
, &b
);
403 gint
RrMinHeight(RrAppearance
*a
)
409 for (i
= 0; i
< a
->textures
; ++i
) {
410 switch (a
->texture
[i
].type
) {
411 case RR_TEXTURE_NONE
:
413 case RR_TEXTURE_MASK
:
414 h
= MAX(h
, a
->texture
[i
].data
.mask
.mask
->height
);
416 case RR_TEXTURE_TEXT
:
417 h
+= MAX(h
, RrFontHeight(a
->texture
[i
].data
.text
.font
,
418 a
->texture
[i
].data
.text
.shadow_offset_y
));
420 case RR_TEXTURE_RGBA
:
421 h
+= MAX(h
, a
->texture
[i
].data
.rgba
.height
);
423 case RR_TEXTURE_LINE_ART
:
424 h
+= MAX(h
, MAX(a
->texture
[i
].data
.lineart
.y1
,
425 a
->texture
[i
].data
.lineart
.y2
));
430 RrMargins(a
, &l
, &t
, &r
, &b
);
438 static void reverse_bits(gchar
*c
, gint n
)
441 for (i
= 0; i
< n
; i
++, c
++)
442 *c
= (((*c
* 0x0802UL
& 0x22110UL
) |
443 (*c
* 0x8020UL
& 0x88440UL
)) * 0x10101UL
) >> 16;
446 gboolean
RrPixmapToRGBA(const RrInstance
*inst
,
447 Pixmap pmap
, Pixmap mask
,
448 gint
*w
, gint
*h
, RrPixel32
**data
)
452 guint pw
, ph
, mw
, mh
, xb
, xd
, i
, x
, y
, di
;
453 XImage
*xi
, *xm
= NULL
;
455 if (!XGetGeometry(RrDisplay(inst
), pmap
,
456 &xr
, &xx
, &xy
, &pw
, &ph
, &xb
, &xd
))
460 if (!XGetGeometry(RrDisplay(inst
), mask
,
461 &xr
, &xx
, &xy
, &mw
, &mh
, &xb
, &xd
))
463 if (pw
!= mw
|| ph
!= mh
|| xd
!= 1)
467 xi
= XGetImage(RrDisplay(inst
), pmap
,
468 0, 0, pw
, ph
, 0xffffffff, ZPixmap
);
473 xm
= XGetImage(RrDisplay(inst
), mask
,
474 0, 0, mw
, mh
, 0xffffffff, ZPixmap
);
479 if ((xm
->bits_per_pixel
== 1) && (xm
->bitmap_bit_order
!= LSBFirst
))
480 reverse_bits(xm
->data
, xm
->bytes_per_line
* xm
->height
);
483 if ((xi
->bits_per_pixel
== 1) && (xi
->bitmap_bit_order
!= LSBFirst
))
484 reverse_bits(xi
->data
, xi
->bytes_per_line
* xi
->height
);
486 *data
= g_new(RrPixel32
, pw
* ph
);
487 RrIncreaseDepth(inst
, *data
, xi
);
490 /* apply transparency from the mask */
492 for (i
= 0, y
= 0; y
< ph
; ++y
) {
493 for (x
= 0; x
< pw
; ++x
, ++i
) {
494 if (!((((unsigned)xm
->data
[di
+ x
/ 8]) >> (x
% 8)) & 0x1))
495 (*data
)[i
] &= ~(0xff << RrDefaultAlphaOffset
);
497 di
+= xm
->bytes_per_line
;
This page took 0.064308 seconds and 4 git commands to generate.