11 #include "kernel/openbox.h"
18 Visual
*render_visual
;
19 Colormap render_colormap
;
20 int render_red_offset
= 0, render_green_offset
= 0, render_blue_offset
= 0;
21 int render_red_shift
, render_green_shift
, render_blue_shift
;
22 int render_red_mask
, render_green_mask
, render_blue_mask
;
24 void render_startup(void)
28 render_depth
= DefaultDepth(ob_display
, ob_screen
);
29 render_visual
= DefaultVisual(ob_display
, ob_screen
);
30 render_colormap
= DefaultColormap(ob_display
, ob_screen
);
32 if (render_depth
< 8) {
33 XVisualInfo vinfo_template
, *vinfo_return
;
34 /* search for a TrueColor Visual... if we can't find one...
35 we will use the default visual for the screen */
39 vinfo_template
.screen
= ob_screen
;
40 vinfo_template
.class = TrueColor
;
41 vinfo_return
= XGetVisualInfo(ob_display
,
42 VisualScreenMask
| VisualClassMask
,
43 &vinfo_template
, &vinfo_nitems
);
47 for (i
= 0; i
< vinfo_nitems
; ++i
) {
48 if (vinfo_return
[i
].depth
> max_depth
) {
49 if (max_depth
== 24 && vinfo_return
[i
].depth
> 24)
50 break; /* prefer 24 bit over 32 */
51 max_depth
= vinfo_return
[i
].depth
;
55 if (max_depth
< render_depth
) best
= -1;
58 render_depth
= vinfo_return
[best
].depth
;
59 render_visual
= vinfo_return
[best
].visual
;
60 render_colormap
= XCreateColormap(ob_display
, ob_root
, render_visual
,
65 switch (render_visual
->class) {
73 pseudocolor_startup();
76 g_critical("unsupported visual class.\n");
82 void truecolor_startup(void)
84 unsigned long red_mask
, green_mask
, blue_mask
;
85 XImage
*timage
= NULL
;
87 timage
= XCreateImage(ob_display
, render_visual
, render_depth
,
88 ZPixmap
, 0, NULL
, 1, 1, 32, 0);
89 g_assert(timage
!= NULL
);
90 /* find the offsets for each color in the visual's masks */
91 render_red_mask
= red_mask
= timage
->red_mask
;
92 render_green_mask
= green_mask
= timage
->green_mask
;
93 render_blue_mask
= blue_mask
= timage
->blue_mask
;
95 render_red_offset
= 0;
96 render_green_offset
= 0;
97 render_blue_offset
= 0;
99 while (! (red_mask
& 1)) { render_red_offset
++; red_mask
>>= 1; }
100 while (! (green_mask
& 1)) { render_green_offset
++; green_mask
>>= 1; }
101 while (! (blue_mask
& 1)) { render_blue_offset
++; blue_mask
>>= 1; }
103 render_red_shift
= render_green_shift
= render_blue_shift
= 8;
104 while (red_mask
) { red_mask
>>= 1; render_red_shift
--; }
105 while (green_mask
) { green_mask
>>= 1; render_green_shift
--; }
106 while (blue_mask
) { blue_mask
>>= 1; render_blue_shift
--; }
110 void pseudocolor_startup(void)
113 int tr
, tg
, tb
, n
, r
, g
, b
, i
, incolors
, ii
;
116 g_message("Initializing PseudoColor RenderControl\n");
118 /* determine the number of colors and the bits-per-color */
119 pseudo_bpc
= 2; /* XXX THIS SHOULD BE A USER OPTION */
120 g_assert(pseudo_bpc
>= 1);
121 _ncolors
= pseudo_ncolors();
123 if (_ncolors
> 1 << render_depth
) {
124 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
125 pseudo_bpc
= 1 << (render_depth
/3) >> 3;
126 _ncolors
= 1 << (pseudo_bpc
* 3);
129 /* build a color cube */
130 pseudo_colors
= malloc(_ncolors
* sizeof(XColor
));
131 cpc
= 1 << pseudo_bpc
; /* colors per channel */
133 for (n
= 0, r
= 0; r
< cpc
; r
++)
134 for (g
= 0; g
< cpc
; g
++)
135 for (b
= 0; b
< cpc
; b
++, n
++) {
136 tr
= (int)(((float)(r
)/(float)(cpc
-1)) * 0xFF);
137 tg
= (int)(((float)(g
)/(float)(cpc
-1)) * 0xFF);
138 tb
= (int)(((float)(b
)/(float)(cpc
-1)) * 0xFF);
139 pseudo_colors
[n
].red
= tr
| tr
<< 8;
140 pseudo_colors
[n
].green
= tg
| tg
<< 8;
141 pseudo_colors
[n
].blue
= tb
| tb
<< 8;
142 pseudo_colors
[n
].flags
= DoRed
|DoGreen
|DoBlue
; /* used to track
146 /* allocate the colors */
147 for (i
= 0; i
< _ncolors
; i
++)
148 if (!XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
149 pseudo_colors
[i
].flags
= 0; /* mark it as unallocated */
151 /* try allocate any colors that failed allocation above */
153 /* get the allocated values from the X server (only the first 256 XXX why!?)
155 incolors
= (((1 << render_depth
) > 256) ? 256 : (1 << render_depth
));
156 for (i
= 0; i
< incolors
; i
++)
157 icolors
[i
].pixel
= i
;
158 XQueryColors(ob_display
, render_colormap
, icolors
, incolors
);
160 /* try match unallocated ones */
161 for (i
= 0; i
< _ncolors
; i
++) {
162 if (!pseudo_colors
[i
].flags
) { /* if it wasn't allocated... */
163 unsigned long closest
= 0xffffffff, close
= 0;
164 for (ii
= 0; ii
< incolors
; ii
++) {
165 /* find deviations */
166 r
= (pseudo_colors
[i
].red
- icolors
[ii
].red
) & 0xff;
167 g
= (pseudo_colors
[i
].green
- icolors
[ii
].green
) & 0xff;
168 b
= (pseudo_colors
[i
].blue
- icolors
[ii
].blue
) & 0xff;
169 /* find a weighted absolute deviation */
170 dev
= (r
* r
) + (g
* g
) + (b
* b
);
178 pseudo_colors
[i
].red
= icolors
[close
].red
;
179 pseudo_colors
[i
].green
= icolors
[close
].green
;
180 pseudo_colors
[i
].blue
= icolors
[close
].blue
;
181 pseudo_colors
[i
].pixel
= icolors
[close
].pixel
;
183 /* try alloc this closest color, it had better succeed! */
184 if (XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
185 pseudo_colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
; /* mark as alloced */
187 g_assert(FALSE
); /* wtf has gone wrong, its already alloced for
193 void x_paint(Window win
, Appearance
*l
)
195 int i
, transferred
= 0, sw
;
196 pixel32
*source
, *dest
;
200 int w
= l
->area
.width
;
201 int h
= l
->area
.height
;
202 Rect tarea
; /* area in which to draw textures */
204 if (w
<= 0 || h
<= 0 || x
+w
<= 0 || y
+h
<= 0) return;
206 g_assert(l
->surface
.type
== Surface_Planar
);
208 oldp
= l
->pixmap
; /* save to free after changing the visible pixmap */
209 l
->pixmap
= XCreatePixmap(ob_display
, ob_root
, x
+w
, y
+h
, render_depth
);
210 g_assert(l
->pixmap
!= None
);
212 if (l
->xftdraw
!= NULL
)
213 XftDrawDestroy(l
->xftdraw
);
214 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
, render_visual
,
216 g_assert(l
->xftdraw
!= NULL
);
218 g_free(l
->surface
.data
.planar
.pixel_data
);
219 l
->surface
.data
.planar
.pixel_data
= g_new(pixel32
, w
* h
);
222 if (l
->surface
.data
.planar
.grad
== Background_ParentRelative
) {
223 sw
= l
->surface
.data
.planar
.parent
->area
.width
;
224 source
= l
->surface
.data
.planar
.parent
->surface
.data
.planar
.pixel_data
225 + l
->surface
.data
.planar
.parentx
226 + sw
* l
->surface
.data
.planar
.parenty
;
227 dest
= l
->surface
.data
.planar
.pixel_data
;
228 for (i
= 0; i
< h
; i
++, source
+= sw
, dest
+= w
) {
229 memcpy(dest
, source
, w
* sizeof(pixel32
));
232 else if (l
->surface
.data
.planar
.grad
== Background_Solid
)
233 gradient_solid(l
, x
, y
, w
, h
);
234 else gradient_render(&l
->surface
, w
, h
);
236 for (i
= 0; i
< l
->textures
; i
++) {
237 tarea
= l
->texture
[i
].position
;
238 if (l
->surface
.data
.planar
.grad
!= Background_ParentRelative
) {
239 if (l
->surface
.data
.planar
.relief
!= Flat
) {
240 switch (l
->surface
.data
.planar
.bevel
) {
242 tarea
.x
+= 1; tarea
.y
+= 1;
243 tarea
.width
-= 2; tarea
.height
-= 2;
246 tarea
.x
+= 2; tarea
.y
+= 2;
247 tarea
.width
-= 4; tarea
.height
-= 4;
250 } else if (l
->surface
.data
.planar
.border
) {
251 tarea
.x
+= 1; tarea
.y
+= 1;
252 tarea
.width
-= 2; tarea
.height
-= 2;
256 switch (l
->texture
[i
].type
) {
260 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
261 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
264 if (l
->xftdraw
== NULL
) {
265 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
,
266 render_visual
, render_colormap
);
268 font_draw(l
->xftdraw
, &l
->texture
[i
].data
.text
,
274 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
275 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
278 if (l
->texture
[i
].data
.mask
.color
->gc
== None
)
279 color_allocate_gc(l
->texture
[i
].data
.mask
.color
);
280 mask_draw(l
->pixmap
, &l
->texture
[i
].data
.mask
,
284 image_draw(l
->surface
.data
.planar
.pixel_data
,
285 &l
->texture
[i
].data
.rgba
,
293 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
294 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
, l
->pixmap
299 XSetWindowBackgroundPixmap(ob_display
, win
, l
->pixmap
);
300 XClearWindow(ob_display
, win
);
301 if (oldp
!= None
) XFreePixmap(ob_display
, oldp
);
305 void gl_paint(Window win, Appearance *l)
307 glXMakeCurrent(ob_display, win, gl_context);
311 void render_shutdown(void)
315 Appearance
*appearance_new(SurfaceType type
, int numtex
)
320 out
= g_new(Appearance
, 1);
321 out
->surface
.type
= type
;
322 out
->textures
= numtex
;
324 if (numtex
) out
->texture
= g_new0(Texture
, numtex
);
325 else out
->texture
= NULL
;
330 p
= &out
->surface
.data
.planar
;
333 p
->border_color
= NULL
;
334 p
->bevel_dark
= NULL
;
335 p
->bevel_light
= NULL
;
336 p
->pixel_data
= NULL
;
342 Appearance
*appearance_copy(Appearance
*orig
)
344 PlanarSurface
*spo
, *spc
;
345 Appearance
*copy
= g_new(Appearance
, 1);
346 copy
->surface
.type
= orig
->surface
.type
;
347 switch (orig
->surface
.type
) {
349 spo
= &(orig
->surface
.data
.planar
);
350 spc
= &(copy
->surface
.data
.planar
);
351 spc
->grad
= spo
->grad
;
352 spc
->relief
= spo
->relief
;
353 spc
->bevel
= spo
->bevel
;
354 if (spo
->primary
!= NULL
)
355 spc
->primary
= color_new(spo
->primary
->r
,
358 else spc
->primary
= NULL
;
360 if (spo
->secondary
!= NULL
)
361 spc
->secondary
= color_new(spo
->secondary
->r
,
364 else spc
->secondary
= NULL
;
366 if (spo
->border_color
!= NULL
)
367 spc
->border_color
= color_new(spo
->border_color
->r
,
368 spo
->border_color
->g
,
369 spo
->border_color
->b
);
370 else spc
->border_color
= NULL
;
372 if (spo
->bevel_dark
!= NULL
)
373 spc
->bevel_dark
= color_new(spo
->bevel_dark
->r
,
376 else spc
->bevel_dark
= NULL
;
378 if (spo
->bevel_light
!= NULL
)
379 spc
->bevel_light
= color_new(spo
->bevel_light
->r
,
381 spo
->bevel_light
->b
);
382 else spc
->bevel_light
= NULL
;
384 spc
->interlaced
= spo
->interlaced
;
385 spc
->border
= spo
->border
;
386 spc
->pixel_data
= NULL
;
389 copy
->textures
= orig
->textures
;
390 copy
->texture
= g_memdup(orig
->texture
, orig
->textures
* sizeof(Texture
));
392 copy
->xftdraw
= NULL
;
396 void appearance_free(Appearance
*a
)
400 if (a
->pixmap
!= None
) XFreePixmap(ob_display
, a
->pixmap
);
401 if (a
->xftdraw
!= NULL
) XftDrawDestroy(a
->xftdraw
);
404 if (a
->surface
.type
== Surface_Planar
) {
405 p
= &a
->surface
.data
.planar
;
406 color_free(p
->primary
);
407 color_free(p
->secondary
);
408 color_free(p
->border_color
);
409 color_free(p
->bevel_dark
);
410 color_free(p
->bevel_light
);
411 g_free(p
->pixel_data
);
418 void pixel32_to_pixmap(pixel32
*in
, Pixmap out
, int x
, int y
, int w
, int h
)
422 im
= XCreateImage(ob_display
, render_visual
, render_depth
,
423 ZPixmap
, 0, NULL
, w
, h
, 32, 0);
424 g_assert(im
!= NULL
);
425 im
->byte_order
= render_endian
;
426 /* this malloc is a complete waste of time on normal 32bpp
427 as reduce_depth just sets im->data = data and returns
429 scratch
= g_new(pixel32
, im
->width
* im
->height
);
430 im
->data
= (char*) scratch
;
431 reduce_depth(in
, im
);
432 XPutImage(ob_display
, out
, DefaultGC(ob_display
, ob_screen
),
433 im
, 0, 0, x
, y
, w
, h
);
439 void appearance_minsize(Appearance
*l
, int *w
, int *h
)
445 switch (l
->surface
.type
) {
447 if (l
->surface
.data
.planar
.relief
!= Flat
) {
448 switch (l
->surface
.data
.planar
.bevel
) {
456 } else if (l
->surface
.data
.planar
.border
)
459 for (i
= 0; i
< l
->textures
; ++i
) {
460 switch (l
->texture
[i
].type
) {
462 *w
+= l
->texture
[i
].data
.mask
.mask
->w
;
463 *h
+= l
->texture
[i
].data
.mask
.mask
->h
;
466 m
= font_measure_string(l
->texture
[i
].data
.text
.font
,
467 l
->texture
[i
].data
.text
.string
,
468 l
->texture
[i
].data
.text
.shadow
,
469 l
->texture
[i
].data
.text
.offset
);
471 m
= font_height(l
->texture
[i
].data
.text
.font
,
472 l
->texture
[i
].data
.text
.shadow
,
473 l
->texture
[i
].data
.text
.offset
);
477 *w
+= l
->texture
[i
].data
.rgba
.width
;
478 *h
+= l
->texture
[i
].data
.rgba
.height
;
488 gboolean
render_pixmap_to_rgba(Pixmap pmap
, Pixmap mask
,
489 int *w
, int *h
, pixel32
**data
)
493 guint pw
, ph
, mw
, mh
, xb
, xd
, i
, x
, y
, di
;
494 XImage
*xi
, *xm
= NULL
;
496 if (!XGetGeometry(ob_display
, pmap
, &xr
, &xx
, &xy
, &pw
, &ph
, &xb
, &xd
))
499 if (!XGetGeometry(ob_display
, mask
, &xr
, &xx
, &xy
, &mw
, &mh
, &xb
, &xd
))
501 if (pw
!= mw
|| ph
!= mh
|| xd
!= 1)
505 xi
= XGetImage(ob_display
, pmap
, 0, 0, pw
, ph
, 0xffffffff, ZPixmap
);
510 xm
= XGetImage(ob_display
, mask
, 0, 0, mw
, mh
, 0xffffffff, ZPixmap
);
515 *data
= g_new(pixel32
, pw
* ph
);
516 increase_depth(*data
, xi
);
519 /* apply transparency from the mask */
521 for (i
= 0, y
= 0; y
< ph
; ++y
) {
522 for (x
= 0; x
< pw
; ++x
, ++i
) {
523 if (!((((unsigned)xm
->data
[di
+ x
/ 8]) >> (x
% 8)) & 0x1))
524 (*data
)[i
] &= ~(0xff << default_alpha_offset
);
526 di
+= xm
->bytes_per_line
;