11 #include "kernel/openbox.h"
18 XVisualInfo render_visual_info
;
20 Visual
*render_visual
;
21 Colormap render_colormap
;
22 int render_red_offset
= 0, render_green_offset
= 0, render_blue_offset
= 0;
23 int render_red_shift
, render_green_shift
, render_blue_shift
;
24 int render_red_mask
, render_green_mask
, render_blue_mask
;
26 void render_startup(void)
30 render_depth
= DefaultDepth(ob_display
, ob_screen
);
31 render_visual
= DefaultVisual(ob_display
, ob_screen
);
32 render_colormap
= DefaultColormap(ob_display
, ob_screen
);
34 if (render_depth
< 8) {
35 XVisualInfo vinfo_template
, *vinfo_return
;
36 /* search for a TrueColor Visual... if we can't find one...
37 we will use the default visual for the screen */
41 vinfo_template
.screen
= ob_screen
;
42 vinfo_template
.class = TrueColor
;
43 vinfo_return
= XGetVisualInfo(ob_display
,
44 VisualScreenMask
| VisualClassMask
,
45 &vinfo_template
, &vinfo_nitems
);
49 for (i
= 0; i
< vinfo_nitems
; ++i
) {
50 if (vinfo_return
[i
].depth
> max_depth
) {
51 if (max_depth
== 24 && vinfo_return
[i
].depth
> 24)
52 break; /* prefer 24 bit over 32 */
53 max_depth
= vinfo_return
[i
].depth
;
57 if (max_depth
< render_depth
) best
= -1;
60 render_depth
= vinfo_return
[best
].depth
;
61 render_visual
= vinfo_return
[best
].visual
;
62 render_colormap
= XCreateColormap(ob_display
, ob_root
, render_visual
,
64 render_visual_info
= vinfo_return
[best
];
68 switch (render_visual
->class) {
76 pseudocolor_startup();
79 g_critical("unsupported visual class.\n");
85 void truecolor_startup(void)
87 unsigned long red_mask
, green_mask
, blue_mask
;
88 XImage
*timage
= NULL
;
90 timage
= XCreateImage(ob_display
, render_visual
, render_depth
,
91 ZPixmap
, 0, NULL
, 1, 1, 32, 0);
92 g_assert(timage
!= NULL
);
93 /* find the offsets for each color in the visual's masks */
94 render_red_mask
= red_mask
= timage
->red_mask
;
95 render_green_mask
= green_mask
= timage
->green_mask
;
96 render_blue_mask
= blue_mask
= timage
->blue_mask
;
98 render_red_offset
= 0;
99 render_green_offset
= 0;
100 render_blue_offset
= 0;
102 while (! (red_mask
& 1)) { render_red_offset
++; red_mask
>>= 1; }
103 while (! (green_mask
& 1)) { render_green_offset
++; green_mask
>>= 1; }
104 while (! (blue_mask
& 1)) { render_blue_offset
++; blue_mask
>>= 1; }
106 render_red_shift
= render_green_shift
= render_blue_shift
= 8;
107 while (red_mask
) { red_mask
>>= 1; render_red_shift
--; }
108 while (green_mask
) { green_mask
>>= 1; render_green_shift
--; }
109 while (blue_mask
) { blue_mask
>>= 1; render_blue_shift
--; }
113 void pseudocolor_startup(void)
116 int tr
, tg
, tb
, n
, r
, g
, b
, i
, incolors
, ii
;
119 g_message("Initializing PseudoColor RenderControl\n");
121 /* determine the number of colors and the bits-per-color */
122 pseudo_bpc
= 2; /* XXX THIS SHOULD BE A USER OPTION */
123 g_assert(pseudo_bpc
>= 1);
124 _ncolors
= pseudo_ncolors();
126 if (_ncolors
> 1 << render_depth
) {
127 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
128 pseudo_bpc
= 1 << (render_depth
/3) >> 3;
129 _ncolors
= 1 << (pseudo_bpc
* 3);
132 /* build a color cube */
133 pseudo_colors
= malloc(_ncolors
* sizeof(XColor
));
134 cpc
= 1 << pseudo_bpc
; /* colors per channel */
136 for (n
= 0, r
= 0; r
< cpc
; r
++)
137 for (g
= 0; g
< cpc
; g
++)
138 for (b
= 0; b
< cpc
; b
++, n
++) {
139 tr
= (int)(((float)(r
)/(float)(cpc
-1)) * 0xFF);
140 tg
= (int)(((float)(g
)/(float)(cpc
-1)) * 0xFF);
141 tb
= (int)(((float)(b
)/(float)(cpc
-1)) * 0xFF);
142 pseudo_colors
[n
].red
= tr
| tr
<< 8;
143 pseudo_colors
[n
].green
= tg
| tg
<< 8;
144 pseudo_colors
[n
].blue
= tb
| tb
<< 8;
145 pseudo_colors
[n
].flags
= DoRed
|DoGreen
|DoBlue
; /* used to track
149 /* allocate the colors */
150 for (i
= 0; i
< _ncolors
; i
++)
151 if (!XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
152 pseudo_colors
[i
].flags
= 0; /* mark it as unallocated */
154 /* try allocate any colors that failed allocation above */
156 /* get the allocated values from the X server (only the first 256 XXX why!?)
158 incolors
= (((1 << render_depth
) > 256) ? 256 : (1 << render_depth
));
159 for (i
= 0; i
< incolors
; i
++)
160 icolors
[i
].pixel
= i
;
161 XQueryColors(ob_display
, render_colormap
, icolors
, incolors
);
163 /* try match unallocated ones */
164 for (i
= 0; i
< _ncolors
; i
++) {
165 if (!pseudo_colors
[i
].flags
) { /* if it wasn't allocated... */
166 unsigned long closest
= 0xffffffff, close
= 0;
167 for (ii
= 0; ii
< incolors
; ii
++) {
168 /* find deviations */
169 r
= (pseudo_colors
[i
].red
- icolors
[ii
].red
) & 0xff;
170 g
= (pseudo_colors
[i
].green
- icolors
[ii
].green
) & 0xff;
171 b
= (pseudo_colors
[i
].blue
- icolors
[ii
].blue
) & 0xff;
172 /* find a weighted absolute deviation */
173 dev
= (r
* r
) + (g
* g
) + (b
* b
);
181 pseudo_colors
[i
].red
= icolors
[close
].red
;
182 pseudo_colors
[i
].green
= icolors
[close
].green
;
183 pseudo_colors
[i
].blue
= icolors
[close
].blue
;
184 pseudo_colors
[i
].pixel
= icolors
[close
].pixel
;
186 /* try alloc this closest color, it had better succeed! */
187 if (XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
188 pseudo_colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
; /* mark as alloced */
190 g_assert(FALSE
); /* wtf has gone wrong, its already alloced for
196 void x_paint(Window win
, Appearance
*l
)
198 int i
, transferred
= 0, sw
;
199 pixel32
*source
, *dest
;
203 int w
= l
->area
.width
;
204 int h
= l
->area
.height
;
205 Rect tarea
; /* area in which to draw textures */
207 if (w
<= 0 || h
<= 0 || x
+w
<= 0 || y
+h
<= 0) return;
209 g_assert(l
->surface
.type
== Surface_Planar
);
211 oldp
= l
->pixmap
; /* save to free after changing the visible pixmap */
212 l
->pixmap
= XCreatePixmap(ob_display
, ob_root
, x
+w
, y
+h
, render_depth
);
213 g_assert(l
->pixmap
!= None
);
215 if (l
->xftdraw
!= NULL
)
216 XftDrawDestroy(l
->xftdraw
);
217 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
, render_visual
,
219 g_assert(l
->xftdraw
!= NULL
);
221 g_free(l
->surface
.data
.planar
.pixel_data
);
222 l
->surface
.data
.planar
.pixel_data
= g_new(pixel32
, w
* h
);
225 if (l
->surface
.data
.planar
.grad
== Background_ParentRelative
) {
226 sw
= l
->surface
.data
.planar
.parent
->area
.width
;
227 source
= l
->surface
.data
.planar
.parent
->surface
.data
.planar
.pixel_data
228 + l
->surface
.data
.planar
.parentx
229 + sw
* l
->surface
.data
.planar
.parenty
;
230 dest
= l
->surface
.data
.planar
.pixel_data
;
231 for (i
= 0; i
< h
; i
++, source
+= sw
, dest
+= w
) {
232 memcpy(dest
, source
, w
* sizeof(pixel32
));
235 else if (l
->surface
.data
.planar
.grad
== Background_Solid
)
236 gradient_solid(l
, x
, y
, w
, h
);
237 else gradient_render(&l
->surface
, w
, h
);
239 for (i
= 0; i
< l
->textures
; i
++) {
240 tarea
= l
->texture
[i
].position
;
241 if (l
->surface
.data
.planar
.grad
!= Background_ParentRelative
) {
242 if (l
->surface
.data
.planar
.relief
!= Flat
) {
243 switch (l
->surface
.data
.planar
.bevel
) {
245 tarea
.x
+= 1; tarea
.y
+= 1;
246 tarea
.width
-= 2; tarea
.height
-= 2;
249 tarea
.x
+= 2; tarea
.y
+= 2;
250 tarea
.width
-= 4; tarea
.height
-= 4;
253 } else if (l
->surface
.data
.planar
.border
) {
254 tarea
.x
+= 1; tarea
.y
+= 1;
255 tarea
.width
-= 2; tarea
.height
-= 2;
259 switch (l
->texture
[i
].type
) {
263 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
264 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
267 if (l
->xftdraw
== NULL
) {
268 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
,
269 render_visual
, render_colormap
);
271 font_draw(l
->xftdraw
, &l
->texture
[i
].data
.text
,
277 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
278 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
281 if (l
->texture
[i
].data
.mask
.color
->gc
== None
)
282 color_allocate_gc(l
->texture
[i
].data
.mask
.color
);
283 mask_draw(l
->pixmap
, &l
->texture
[i
].data
.mask
,
287 image_draw(l
->surface
.data
.planar
.pixel_data
,
288 &l
->texture
[i
].data
.rgba
,
296 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
297 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
, l
->pixmap
302 XSetWindowBackgroundPixmap(ob_display
, win
, l
->pixmap
);
303 XClearWindow(ob_display
, win
);
304 if (oldp
!= None
) XFreePixmap(ob_display
, oldp
);
308 void gl_paint(Window win, Appearance *l)
310 glXMakeCurrent(ob_display, win, gl_context);
314 void render_shutdown(void)
318 Appearance
*appearance_new(SurfaceType type
, int numtex
)
323 out
= g_new(Appearance
, 1);
324 out
->surface
.type
= type
;
325 out
->textures
= numtex
;
327 if (numtex
) out
->texture
= g_new0(Texture
, numtex
);
328 else out
->texture
= NULL
;
333 p
= &out
->surface
.data
.planar
;
336 p
->border_color
= NULL
;
337 p
->bevel_dark
= NULL
;
338 p
->bevel_light
= NULL
;
339 p
->pixel_data
= NULL
;
345 Appearance
*appearance_copy(Appearance
*orig
)
347 PlanarSurface
*spo
, *spc
;
348 Appearance
*copy
= g_new(Appearance
, 1);
349 copy
->surface
.type
= orig
->surface
.type
;
350 switch (orig
->surface
.type
) {
352 spo
= &(orig
->surface
.data
.planar
);
353 spc
= &(copy
->surface
.data
.planar
);
354 spc
->grad
= spo
->grad
;
355 spc
->relief
= spo
->relief
;
356 spc
->bevel
= spo
->bevel
;
357 if (spo
->primary
!= NULL
)
358 spc
->primary
= color_new(spo
->primary
->r
,
361 else spc
->primary
= NULL
;
363 if (spo
->secondary
!= NULL
)
364 spc
->secondary
= color_new(spo
->secondary
->r
,
367 else spc
->secondary
= NULL
;
369 if (spo
->border_color
!= NULL
)
370 spc
->border_color
= color_new(spo
->border_color
->r
,
371 spo
->border_color
->g
,
372 spo
->border_color
->b
);
373 else spc
->border_color
= NULL
;
375 if (spo
->bevel_dark
!= NULL
)
376 spc
->bevel_dark
= color_new(spo
->bevel_dark
->r
,
379 else spc
->bevel_dark
= NULL
;
381 if (spo
->bevel_light
!= NULL
)
382 spc
->bevel_light
= color_new(spo
->bevel_light
->r
,
384 spo
->bevel_light
->b
);
385 else spc
->bevel_light
= NULL
;
387 spc
->interlaced
= spo
->interlaced
;
388 spc
->border
= spo
->border
;
389 spc
->pixel_data
= NULL
;
392 copy
->textures
= orig
->textures
;
393 copy
->texture
= g_memdup(orig
->texture
, orig
->textures
* sizeof(Texture
));
395 copy
->xftdraw
= NULL
;
399 void appearance_free(Appearance
*a
)
403 if (a
->pixmap
!= None
) XFreePixmap(ob_display
, a
->pixmap
);
404 if (a
->xftdraw
!= NULL
) XftDrawDestroy(a
->xftdraw
);
407 if (a
->surface
.type
== Surface_Planar
) {
408 p
= &a
->surface
.data
.planar
;
409 color_free(p
->primary
);
410 color_free(p
->secondary
);
411 color_free(p
->border_color
);
412 color_free(p
->bevel_dark
);
413 color_free(p
->bevel_light
);
414 g_free(p
->pixel_data
);
421 void pixel32_to_pixmap(pixel32
*in
, Pixmap out
, int x
, int y
, int w
, int h
)
425 im
= XCreateImage(ob_display
, render_visual
, render_depth
,
426 ZPixmap
, 0, NULL
, w
, h
, 32, 0);
427 g_assert(im
!= NULL
);
428 im
->byte_order
= render_endian
;
429 /* this malloc is a complete waste of time on normal 32bpp
430 as reduce_depth just sets im->data = data and returns
432 scratch
= g_new(pixel32
, im
->width
* im
->height
);
433 im
->data
= (char*) scratch
;
434 reduce_depth(in
, im
);
435 XPutImage(ob_display
, out
, DefaultGC(ob_display
, ob_screen
),
436 im
, 0, 0, x
, y
, w
, h
);
442 void appearance_minsize(Appearance
*l
, int *w
, int *h
)
448 switch (l
->surface
.type
) {
450 if (l
->surface
.data
.planar
.relief
!= Flat
) {
451 switch (l
->surface
.data
.planar
.bevel
) {
459 } else if (l
->surface
.data
.planar
.border
)
462 for (i
= 0; i
< l
->textures
; ++i
) {
463 switch (l
->texture
[i
].type
) {
465 *w
+= l
->texture
[i
].data
.mask
.mask
->w
;
466 *h
+= l
->texture
[i
].data
.mask
.mask
->h
;
469 m
= font_measure_string(l
->texture
[i
].data
.text
.font
,
470 l
->texture
[i
].data
.text
.string
,
471 l
->texture
[i
].data
.text
.shadow
,
472 l
->texture
[i
].data
.text
.offset
);
474 m
= font_height(l
->texture
[i
].data
.text
.font
,
475 l
->texture
[i
].data
.text
.shadow
,
476 l
->texture
[i
].data
.text
.offset
);
480 *w
+= l
->texture
[i
].data
.rgba
.width
;
481 *h
+= l
->texture
[i
].data
.rgba
.height
;
491 gboolean
render_pixmap_to_rgba(Pixmap pmap
, Pixmap mask
,
492 int *w
, int *h
, pixel32
**data
)
496 guint pw
, ph
, mw
, mh
, xb
, xd
, i
, x
, y
, di
;
497 XImage
*xi
, *xm
= NULL
;
499 if (!XGetGeometry(ob_display
, pmap
, &xr
, &xx
, &xy
, &pw
, &ph
, &xb
, &xd
))
502 if (!XGetGeometry(ob_display
, mask
, &xr
, &xx
, &xy
, &mw
, &mh
, &xb
, &xd
))
504 if (pw
!= mw
|| ph
!= mh
|| xd
!= 1)
508 xi
= XGetImage(ob_display
, pmap
, 0, 0, pw
, ph
, 0xffffffff, ZPixmap
);
513 xm
= XGetImage(ob_display
, mask
, 0, 0, mw
, mh
, 0xffffffff, ZPixmap
);
518 *data
= g_new(pixel32
, pw
* ph
);
519 increase_depth(*data
, xi
);
522 /* apply transparency from the mask */
524 for (i
= 0, y
= 0; y
< ph
; ++y
) {
525 for (x
= 0; x
< pw
; ++x
, ++i
) {
526 if (!((((unsigned)xm
->data
[di
+ x
/ 8]) >> (x
% 8)) & 0x1))
527 (*data
)[i
] &= ~(0xff << default_alpha_offset
);
529 di
+= xm
->bytes_per_line
;