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 switch (render_visual
->class) {
42 pseudocolor_startup();
45 g_critical("unsupported visual class.\n");
51 void truecolor_startup(void)
53 unsigned long red_mask
, green_mask
, blue_mask
;
54 XImage
*timage
= NULL
;
56 timage
= XCreateImage(ob_display
, render_visual
, render_depth
,
57 ZPixmap
, 0, NULL
, 1, 1, 32, 0);
58 g_assert(timage
!= NULL
);
59 /* find the offsets for each color in the visual's masks */
60 render_red_mask
= red_mask
= timage
->red_mask
;
61 render_green_mask
= green_mask
= timage
->green_mask
;
62 render_blue_mask
= blue_mask
= timage
->blue_mask
;
64 render_red_offset
= 0;
65 render_green_offset
= 0;
66 render_blue_offset
= 0;
68 while (! (red_mask
& 1)) { render_red_offset
++; red_mask
>>= 1; }
69 while (! (green_mask
& 1)) { render_green_offset
++; green_mask
>>= 1; }
70 while (! (blue_mask
& 1)) { render_blue_offset
++; blue_mask
>>= 1; }
72 render_red_shift
= render_green_shift
= render_blue_shift
= 8;
73 while (red_mask
) { red_mask
>>= 1; render_red_shift
--; }
74 while (green_mask
) { green_mask
>>= 1; render_green_shift
--; }
75 while (blue_mask
) { blue_mask
>>= 1; render_blue_shift
--; }
79 void pseudocolor_startup(void)
82 int tr
, tg
, tb
, n
, r
, g
, b
, i
, incolors
, ii
;
85 g_message("Initializing PseudoColor RenderControl\n");
87 /* determine the number of colors and the bits-per-color */
88 pseudo_bpc
= 2; /* XXX THIS SHOULD BE A USER OPTION */
89 g_assert(pseudo_bpc
>= 1);
90 _ncolors
= pseudo_ncolors();
92 if (_ncolors
> 1 << render_depth
) {
93 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
94 pseudo_bpc
= 1 << (render_depth
/3) >> 3;
95 _ncolors
= 1 << (pseudo_bpc
* 3);
98 /* build a color cube */
99 pseudo_colors
= malloc(_ncolors
* sizeof(XColor
));
100 cpc
= 1 << pseudo_bpc
; /* colors per channel */
102 for (n
= 0, r
= 0; r
< cpc
; r
++)
103 for (g
= 0; g
< cpc
; g
++)
104 for (b
= 0; b
< cpc
; b
++, n
++) {
105 tr
= (int)(((float)(r
)/(float)(cpc
-1)) * 0xFF);
106 tg
= (int)(((float)(g
)/(float)(cpc
-1)) * 0xFF);
107 tb
= (int)(((float)(b
)/(float)(cpc
-1)) * 0xFF);
108 pseudo_colors
[n
].red
= tr
| tr
<< 8;
109 pseudo_colors
[n
].green
= tg
| tg
<< 8;
110 pseudo_colors
[n
].blue
= tb
| tb
<< 8;
111 pseudo_colors
[n
].flags
= DoRed
|DoGreen
|DoBlue
; /* used to track
115 /* allocate the colors */
116 for (i
= 0; i
< _ncolors
; i
++)
117 if (!XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
118 pseudo_colors
[i
].flags
= 0; /* mark it as unallocated */
120 /* try allocate any colors that failed allocation above */
122 /* get the allocated values from the X server (only the first 256 XXX why!?)
124 incolors
= (((1 << render_depth
) > 256) ? 256 : (1 << render_depth
));
125 for (i
= 0; i
< incolors
; i
++)
126 icolors
[i
].pixel
= i
;
127 XQueryColors(ob_display
, render_colormap
, icolors
, incolors
);
129 /* try match unallocated ones */
130 for (i
= 0; i
< _ncolors
; i
++) {
131 if (!pseudo_colors
[i
].flags
) { /* if it wasn't allocated... */
132 unsigned long closest
= 0xffffffff, close
= 0;
133 for (ii
= 0; ii
< incolors
; ii
++) {
134 /* find deviations */
135 r
= (pseudo_colors
[i
].red
- icolors
[ii
].red
) & 0xff;
136 g
= (pseudo_colors
[i
].green
- icolors
[ii
].green
) & 0xff;
137 b
= (pseudo_colors
[i
].blue
- icolors
[ii
].blue
) & 0xff;
138 /* find a weighted absolute deviation */
139 dev
= (r
* r
) + (g
* g
) + (b
* b
);
147 pseudo_colors
[i
].red
= icolors
[close
].red
;
148 pseudo_colors
[i
].green
= icolors
[close
].green
;
149 pseudo_colors
[i
].blue
= icolors
[close
].blue
;
150 pseudo_colors
[i
].pixel
= icolors
[close
].pixel
;
152 /* try alloc this closest color, it had better succeed! */
153 if (XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
154 pseudo_colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
; /* mark as alloced */
156 g_assert(FALSE
); /* wtf has gone wrong, its already alloced for
162 void x_paint(Window win
, Appearance
*l
)
164 int i
, transferred
= 0, sw
;
165 pixel32
*source
, *dest
;
169 int w
= l
->area
.width
;
170 int h
= l
->area
.height
;
171 Rect tarea
; /* area in which to draw textures */
173 if (w
<= 0 || h
<= 0 || x
+w
<= 0 || y
+h
<= 0) return;
175 g_assert(l
->surface
.type
== Surface_Planar
);
177 oldp
= l
->pixmap
; /* save to free after changing the visible pixmap */
178 l
->pixmap
= XCreatePixmap(ob_display
, ob_root
, x
+w
, y
+h
, render_depth
);
179 g_assert(l
->pixmap
!= None
);
181 if (l
->xftdraw
!= NULL
)
182 XftDrawDestroy(l
->xftdraw
);
183 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
, render_visual
,
185 g_assert(l
->xftdraw
!= NULL
);
187 g_free(l
->surface
.data
.planar
.pixel_data
);
188 l
->surface
.data
.planar
.pixel_data
= g_new(pixel32
, w
* h
);
191 if (l
->surface
.data
.planar
.grad
== Background_ParentRelative
) {
192 sw
= l
->surface
.data
.planar
.parent
->area
.width
;
193 source
= l
->surface
.data
.planar
.parent
->surface
.data
.planar
.pixel_data
194 + l
->surface
.data
.planar
.parentx
195 + sw
* l
->surface
.data
.planar
.parenty
;
196 dest
= l
->surface
.data
.planar
.pixel_data
;
197 for (i
= 0; i
< h
; i
++, source
+= sw
, dest
+= w
) {
198 memcpy(dest
, source
, w
* sizeof(pixel32
));
201 else if (l
->surface
.data
.planar
.grad
== Background_Solid
)
202 gradient_solid(l
, x
, y
, w
, h
);
203 else gradient_render(&l
->surface
, w
, h
);
205 for (i
= 0; i
< l
->textures
; i
++) {
206 tarea
= l
->texture
[i
].position
;
207 if (l
->surface
.data
.planar
.grad
!= Background_ParentRelative
) {
208 if (l
->surface
.data
.planar
.relief
!= Flat
) {
209 switch (l
->surface
.data
.planar
.bevel
) {
211 tarea
.x
+= 1; tarea
.y
+= 1;
212 tarea
.width
-= 2; tarea
.height
-= 2;
215 tarea
.x
+= 2; tarea
.y
+= 2;
216 tarea
.width
-= 4; tarea
.height
-= 4;
219 } else if (l
->surface
.data
.planar
.border
) {
220 tarea
.x
+= 1; tarea
.y
+= 1;
221 tarea
.width
-= 2; tarea
.height
-= 2;
225 switch (l
->texture
[i
].type
) {
229 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
230 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
233 if (l
->xftdraw
== NULL
) {
234 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
,
235 render_visual
, render_colormap
);
237 font_draw(l
->xftdraw
, &l
->texture
[i
].data
.text
,
243 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
244 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
247 if (l
->texture
[i
].data
.mask
.color
->gc
== None
)
248 color_allocate_gc(l
->texture
[i
].data
.mask
.color
);
249 mask_draw(l
->pixmap
, &l
->texture
[i
].data
.mask
,
253 image_draw(l
->surface
.data
.planar
.pixel_data
,
254 &l
->texture
[i
].data
.rgba
,
262 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
263 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
, l
->pixmap
268 XSetWindowBackgroundPixmap(ob_display
, win
, l
->pixmap
);
269 XClearWindow(ob_display
, win
);
270 if (oldp
!= None
) XFreePixmap(ob_display
, oldp
);
274 void gl_paint(Window win, Appearance *l)
276 glXMakeCurrent(ob_display, win, gl_context);
280 void render_shutdown(void)
284 Appearance
*appearance_new(SurfaceType type
, int numtex
)
289 out
= g_new(Appearance
, 1);
290 out
->surface
.type
= type
;
291 out
->textures
= numtex
;
293 if (numtex
) out
->texture
= g_new0(Texture
, numtex
);
294 else out
->texture
= NULL
;
299 p
= &out
->surface
.data
.planar
;
302 p
->border_color
= NULL
;
303 p
->bevel_dark
= NULL
;
304 p
->bevel_light
= NULL
;
305 p
->pixel_data
= NULL
;
311 Appearance
*appearance_copy(Appearance
*orig
)
313 PlanarSurface
*spo
, *spc
;
314 Appearance
*copy
= g_new(Appearance
, 1);
315 copy
->surface
.type
= orig
->surface
.type
;
316 switch (orig
->surface
.type
) {
318 spo
= &(orig
->surface
.data
.planar
);
319 spc
= &(copy
->surface
.data
.planar
);
320 spc
->grad
= spo
->grad
;
321 spc
->relief
= spo
->relief
;
322 spc
->bevel
= spo
->bevel
;
323 if (spo
->primary
!= NULL
)
324 spc
->primary
= color_new(spo
->primary
->r
,
327 else spc
->primary
= NULL
;
329 if (spo
->secondary
!= NULL
)
330 spc
->secondary
= color_new(spo
->secondary
->r
,
333 else spc
->secondary
= NULL
;
335 if (spo
->border_color
!= NULL
)
336 spc
->border_color
= color_new(spo
->border_color
->r
,
337 spo
->border_color
->g
,
338 spo
->border_color
->b
);
339 else spc
->border_color
= NULL
;
341 if (spo
->bevel_dark
!= NULL
)
342 spc
->bevel_dark
= color_new(spo
->bevel_dark
->r
,
345 else spc
->bevel_dark
= NULL
;
347 if (spo
->bevel_light
!= NULL
)
348 spc
->bevel_light
= color_new(spo
->bevel_light
->r
,
350 spo
->bevel_light
->b
);
351 else spc
->bevel_light
= NULL
;
353 spc
->interlaced
= spo
->interlaced
;
354 spc
->border
= spo
->border
;
355 spc
->pixel_data
= NULL
;
358 copy
->textures
= orig
->textures
;
359 copy
->texture
= g_memdup(orig
->texture
, orig
->textures
* sizeof(Texture
));
361 copy
->xftdraw
= NULL
;
365 void appearance_free(Appearance
*a
)
369 if (a
->pixmap
!= None
) XFreePixmap(ob_display
, a
->pixmap
);
370 if (a
->xftdraw
!= NULL
) XftDrawDestroy(a
->xftdraw
);
373 if (a
->surface
.type
== Surface_Planar
) {
374 p
= &a
->surface
.data
.planar
;
375 color_free(p
->primary
);
376 color_free(p
->secondary
);
377 color_free(p
->border_color
);
378 color_free(p
->bevel_dark
);
379 color_free(p
->bevel_light
);
380 g_free(p
->pixel_data
);
387 void pixel32_to_pixmap(pixel32
*in
, Pixmap out
, int x
, int y
, int w
, int h
)
391 im
= XCreateImage(ob_display
, render_visual
, render_depth
,
392 ZPixmap
, 0, NULL
, w
, h
, 32, 0);
393 g_assert(im
!= NULL
);
394 im
->byte_order
= render_endian
;
395 /* this malloc is a complete waste of time on normal 32bpp
396 as reduce_depth just sets im->data = data and returns
398 scratch
= g_new(pixel32
, im
->width
* im
->height
);
399 im
->data
= (char*) scratch
;
400 reduce_depth(in
, im
);
401 XPutImage(ob_display
, out
, DefaultGC(ob_display
, ob_screen
),
402 im
, 0, 0, x
, y
, w
, h
);
408 void appearance_minsize(Appearance
*l
, int *w
, int *h
)
414 switch (l
->surface
.type
) {
416 if (l
->surface
.data
.planar
.relief
!= Flat
) {
417 switch (l
->surface
.data
.planar
.bevel
) {
425 } else if (l
->surface
.data
.planar
.border
)
428 for (i
= 0; i
< l
->textures
; ++i
) {
429 switch (l
->texture
[i
].type
) {
431 *w
+= l
->texture
[i
].data
.mask
.mask
->w
;
432 *h
+= l
->texture
[i
].data
.mask
.mask
->h
;
435 m
= font_measure_string(l
->texture
[i
].data
.text
.font
,
436 l
->texture
[i
].data
.text
.string
,
437 l
->texture
[i
].data
.text
.shadow
,
438 l
->texture
[i
].data
.text
.offset
);
440 m
= font_height(l
->texture
[i
].data
.text
.font
,
441 l
->texture
[i
].data
.text
.shadow
,
442 l
->texture
[i
].data
.text
.offset
);
446 *w
+= l
->texture
[i
].data
.rgba
.width
;
447 *h
+= l
->texture
[i
].data
.rgba
.height
;
457 gboolean
render_pixmap_to_rgba(Pixmap pmap
, Pixmap mask
,
458 int *w
, int *h
, pixel32
**data
)
462 guint pw
, ph
, mw
, mh
, xb
, xd
, i
, x
, y
, di
;
463 XImage
*xi
, *xm
= NULL
;
465 if (!XGetGeometry(ob_display
, pmap
, &xr
, &xx
, &xy
, &pw
, &ph
, &xb
, &xd
))
468 if (!XGetGeometry(ob_display
, mask
, &xr
, &xx
, &xy
, &mw
, &mh
, &xb
, &xd
))
470 if (pw
!= mw
|| ph
!= mh
|| xd
!= 1)
474 xi
= XGetImage(ob_display
, pmap
, 0, 0, pw
, ph
, 0xffffffff, ZPixmap
);
479 xm
= XGetImage(ob_display
, mask
, 0, 0, mw
, mh
, 0xffffffff, ZPixmap
);
484 *data
= g_new(pixel32
, pw
* ph
);
485 increase_depth(*data
, xi
);
488 /* apply transparency from the mask */
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 << default_alpha_offset
);
495 di
+= xm
->bytes_per_line
;