16 #include "kernel/openbox.h"
23 XVisualInfo render_visual_info
;
25 Visual
*render_visual
;
26 Colormap render_colormap
;
28 int render_red_offset
= 0, render_green_offset
= 0, render_blue_offset
= 0;
29 int render_red_shift
, render_green_shift
, render_blue_shift
;
30 int render_red_mask
, render_green_mask
, render_blue_mask
;
35 GLXContext render_glx_context
;
37 int render_glx_rating(XVisualInfo
*v
)
42 printf("evaluating visual %d\n", v
->visualid
);
43 glXGetConfig(ob_display
, v
, GLX_BUFFER_SIZE
, &val
);
44 printf("buffer size %d\n", val
);
58 glXGetConfig(ob_display
, v
, GLX_LEVEL
, &val
);
59 printf("level %d\n", val
);
63 glXGetConfig(ob_display
, v
, GLX_DEPTH_SIZE
, &val
);
64 printf("depth size %d\n", val
);
79 glXGetConfig(ob_display
, v
, GLX_DOUBLEBUFFER
, &val
);
80 printf("double buffer %d\n", val
);
87 void render_startup(void)
89 int count
, i
= 0, val
, best
= 0, rate
= 0, temp
;
90 XVisualInfo vimatch
, *vilist
;
93 render_depth
= DefaultDepth(ob_display
, ob_screen
);
94 render_visual
= DefaultVisual(ob_display
, ob_screen
);
95 render_colormap
= DefaultColormap(ob_display
, ob_screen
);
98 vimatch
.screen
= ob_screen
;
99 vimatch
.class = TrueColor
;
100 vilist
= XGetVisualInfo(ob_display
, VisualScreenMask
| VisualClassMask
,
104 printf("looking for a GL visualin %d visuals\n", count
);
105 for (i
= 0; i
< count
; i
++) {
106 glXGetConfig(ob_display
, &vilist
[i
], GLX_USE_GL
, &val
);
108 temp
= render_glx_rating(&vilist
[i
]);
117 printf("picked visual %d with rating %d\n", best
, rate
);
118 render_depth
= vilist
[best
].depth
;
119 render_visual
= vilist
[best
].visual
;
120 render_colormap
= XCreateColormap(ob_display
, ob_root
,
121 render_visual
, AllocNone
);
122 render_visual_info
= vilist
[best
];
123 render_glx_context
= glXCreateContext(ob_display
, &render_visual_info
,
125 if (render_glx_context
== NULL
)
134 switch (render_visual
->class) {
142 pseudocolor_startup();
145 g_critical("unsupported visual class.\n");
151 void truecolor_startup(void)
153 unsigned long red_mask
, green_mask
, blue_mask
;
154 XImage
*timage
= NULL
;
156 timage
= XCreateImage(ob_display
, render_visual
, render_depth
,
157 ZPixmap
, 0, NULL
, 1, 1, 32, 0);
158 g_assert(timage
!= NULL
);
159 /* find the offsets for each color in the visual's masks */
160 render_red_mask
= red_mask
= timage
->red_mask
;
161 render_green_mask
= green_mask
= timage
->green_mask
;
162 render_blue_mask
= blue_mask
= timage
->blue_mask
;
164 render_red_offset
= 0;
165 render_green_offset
= 0;
166 render_blue_offset
= 0;
168 while (! (red_mask
& 1)) { render_red_offset
++; red_mask
>>= 1; }
169 while (! (green_mask
& 1)) { render_green_offset
++; green_mask
>>= 1; }
170 while (! (blue_mask
& 1)) { render_blue_offset
++; blue_mask
>>= 1; }
172 render_red_shift
= render_green_shift
= render_blue_shift
= 8;
173 while (red_mask
) { red_mask
>>= 1; render_red_shift
--; }
174 while (green_mask
) { green_mask
>>= 1; render_green_shift
--; }
175 while (blue_mask
) { blue_mask
>>= 1; render_blue_shift
--; }
179 void pseudocolor_startup(void)
182 int tr
, tg
, tb
, n
, r
, g
, b
, i
, incolors
, ii
;
185 g_message("Initializing PseudoColor RenderControl\n");
187 /* determine the number of colors and the bits-per-color */
188 pseudo_bpc
= 2; /* XXX THIS SHOULD BE A USER OPTION */
189 g_assert(pseudo_bpc
>= 1);
190 _ncolors
= pseudo_ncolors();
192 if (_ncolors
> 1 << render_depth
) {
193 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
194 pseudo_bpc
= 1 << (render_depth
/3) >> 3;
195 _ncolors
= 1 << (pseudo_bpc
* 3);
198 /* build a color cube */
199 pseudo_colors
= malloc(_ncolors
* sizeof(XColor
));
200 cpc
= 1 << pseudo_bpc
; /* colors per channel */
202 for (n
= 0, r
= 0; r
< cpc
; r
++)
203 for (g
= 0; g
< cpc
; g
++)
204 for (b
= 0; b
< cpc
; b
++, n
++) {
205 tr
= (int)(((float)(r
)/(float)(cpc
-1)) * 0xFF);
206 tg
= (int)(((float)(g
)/(float)(cpc
-1)) * 0xFF);
207 tb
= (int)(((float)(b
)/(float)(cpc
-1)) * 0xFF);
208 pseudo_colors
[n
].red
= tr
| tr
<< 8;
209 pseudo_colors
[n
].green
= tg
| tg
<< 8;
210 pseudo_colors
[n
].blue
= tb
| tb
<< 8;
211 pseudo_colors
[n
].flags
= DoRed
|DoGreen
|DoBlue
; /* used to track
215 /* allocate the colors */
216 for (i
= 0; i
< _ncolors
; i
++)
217 if (!XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
218 pseudo_colors
[i
].flags
= 0; /* mark it as unallocated */
220 /* try allocate any colors that failed allocation above */
222 /* get the allocated values from the X server (only the first 256 XXX why!?)
224 incolors
= (((1 << render_depth
) > 256) ? 256 : (1 << render_depth
));
225 for (i
= 0; i
< incolors
; i
++)
226 icolors
[i
].pixel
= i
;
227 XQueryColors(ob_display
, render_colormap
, icolors
, incolors
);
229 /* try match unallocated ones */
230 for (i
= 0; i
< _ncolors
; i
++) {
231 if (!pseudo_colors
[i
].flags
) { /* if it wasn't allocated... */
232 unsigned long closest
= 0xffffffff, close
= 0;
233 for (ii
= 0; ii
< incolors
; ii
++) {
234 /* find deviations */
235 r
= (pseudo_colors
[i
].red
- icolors
[ii
].red
) & 0xff;
236 g
= (pseudo_colors
[i
].green
- icolors
[ii
].green
) & 0xff;
237 b
= (pseudo_colors
[i
].blue
- icolors
[ii
].blue
) & 0xff;
238 /* find a weighted absolute deviation */
239 dev
= (r
* r
) + (g
* g
) + (b
* b
);
247 pseudo_colors
[i
].red
= icolors
[close
].red
;
248 pseudo_colors
[i
].green
= icolors
[close
].green
;
249 pseudo_colors
[i
].blue
= icolors
[close
].blue
;
250 pseudo_colors
[i
].pixel
= icolors
[close
].pixel
;
252 /* try alloc this closest color, it had better succeed! */
253 if (XAllocColor(ob_display
, render_colormap
, &pseudo_colors
[i
]))
254 pseudo_colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
; /* mark as alloced */
256 g_assert(FALSE
); /* wtf has gone wrong, its already alloced for
262 void x_paint(Window win
, Appearance
*l
)
264 int i
, transferred
= 0, sw
;
265 pixel32
*source
, *dest
;
269 int w
= l
->area
.width
;
270 int h
= l
->area
.height
;
271 Rect tarea
; /* area in which to draw textures */
273 if (w
<= 0 || h
<= 0 || x
+w
<= 0 || y
+h
<= 0) return;
275 g_assert(l
->surface
.type
== Surface_Planar
);
277 oldp
= l
->pixmap
; /* save to free after changing the visible pixmap */
278 l
->pixmap
= XCreatePixmap(ob_display
, ob_root
, x
+w
, y
+h
, render_depth
);
279 g_assert(l
->pixmap
!= None
);
281 if (l
->xftdraw
!= NULL
)
282 XftDrawDestroy(l
->xftdraw
);
283 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
, render_visual
,
285 g_assert(l
->xftdraw
!= NULL
);
287 g_free(l
->surface
.data
.planar
.pixel_data
);
288 l
->surface
.data
.planar
.pixel_data
= g_new(pixel32
, w
* h
);
291 if (l
->surface
.data
.planar
.grad
== Background_ParentRelative
) {
292 sw
= l
->surface
.data
.planar
.parent
->area
.width
;
293 source
= l
->surface
.data
.planar
.parent
->surface
.data
.planar
.pixel_data
294 + l
->surface
.data
.planar
.parentx
295 + sw
* l
->surface
.data
.planar
.parenty
;
296 dest
= l
->surface
.data
.planar
.pixel_data
;
297 for (i
= 0; i
< h
; i
++, source
+= sw
, dest
+= w
) {
298 memcpy(dest
, source
, w
* sizeof(pixel32
));
301 else if (l
->surface
.data
.planar
.grad
== Background_Solid
)
302 gradient_solid(l
, x
, y
, w
, h
);
303 else gradient_render(&l
->surface
, w
, h
);
305 for (i
= 0; i
< l
->textures
; i
++) {
306 tarea
= l
->texture
[i
].position
;
307 if (l
->surface
.data
.planar
.grad
!= Background_ParentRelative
) {
308 if (l
->surface
.data
.planar
.relief
!= Flat
) {
309 switch (l
->surface
.data
.planar
.bevel
) {
311 tarea
.x
+= 1; tarea
.y
+= 1;
312 tarea
.width
-= 2; tarea
.height
-= 2;
315 tarea
.x
+= 2; tarea
.y
+= 2;
316 tarea
.width
-= 4; tarea
.height
-= 4;
319 } else if (l
->surface
.data
.planar
.border
) {
320 tarea
.x
+= 1; tarea
.y
+= 1;
321 tarea
.width
-= 2; tarea
.height
-= 2;
325 switch (l
->texture
[i
].type
) {
329 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
330 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
333 if (l
->xftdraw
== NULL
) {
334 l
->xftdraw
= XftDrawCreate(ob_display
, l
->pixmap
,
335 render_visual
, render_colormap
);
337 font_draw(l
->xftdraw
, &l
->texture
[i
].data
.text
,
343 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
344 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
,
347 if (l
->texture
[i
].data
.mask
.color
->gc
== None
)
348 color_allocate_gc(l
->texture
[i
].data
.mask
.color
);
349 mask_draw(l
->pixmap
, &l
->texture
[i
].data
.mask
,
353 image_draw(l
->surface
.data
.planar
.pixel_data
,
354 &l
->texture
[i
].data
.rgba
,
362 if (l
->surface
.data
.planar
.grad
!= Background_Solid
)
363 pixel32_to_pixmap(l
->surface
.data
.planar
.pixel_data
, l
->pixmap
368 XSetWindowBackgroundPixmap(ob_display
, win
, l
->pixmap
);
369 XClearWindow(ob_display
, win
);
370 if (oldp
!= None
) XFreePixmap(ob_display
, oldp
);
373 void render_shutdown(void)
377 Appearance
*appearance_new(SurfaceType type
, int numtex
)
382 out
= g_new(Appearance
, 1);
383 out
->surface
.type
= type
;
384 out
->textures
= numtex
;
386 if (numtex
) out
->texture
= g_new0(Texture
, numtex
);
387 else out
->texture
= NULL
;
392 p
= &out
->surface
.data
.planar
;
395 p
->border_color
= NULL
;
396 p
->bevel_dark
= NULL
;
397 p
->bevel_light
= NULL
;
398 p
->pixel_data
= NULL
;
404 Appearance
*appearance_copy(Appearance
*orig
)
406 PlanarSurface
*spo
, *spc
;
407 Appearance
*copy
= g_new(Appearance
, 1);
408 copy
->surface
.type
= orig
->surface
.type
;
409 switch (orig
->surface
.type
) {
411 spo
= &(orig
->surface
.data
.planar
);
412 spc
= &(copy
->surface
.data
.planar
);
413 spc
->grad
= spo
->grad
;
414 spc
->relief
= spo
->relief
;
415 spc
->bevel
= spo
->bevel
;
416 if (spo
->primary
!= NULL
)
417 spc
->primary
= color_new(spo
->primary
->r
,
420 else spc
->primary
= NULL
;
422 if (spo
->secondary
!= NULL
)
423 spc
->secondary
= color_new(spo
->secondary
->r
,
426 else spc
->secondary
= NULL
;
428 if (spo
->border_color
!= NULL
)
429 spc
->border_color
= color_new(spo
->border_color
->r
,
430 spo
->border_color
->g
,
431 spo
->border_color
->b
);
432 else spc
->border_color
= NULL
;
434 if (spo
->bevel_dark
!= NULL
)
435 spc
->bevel_dark
= color_new(spo
->bevel_dark
->r
,
438 else spc
->bevel_dark
= NULL
;
440 if (spo
->bevel_light
!= NULL
)
441 spc
->bevel_light
= color_new(spo
->bevel_light
->r
,
443 spo
->bevel_light
->b
);
444 else spc
->bevel_light
= NULL
;
446 spc
->interlaced
= spo
->interlaced
;
447 spc
->border
= spo
->border
;
448 spc
->pixel_data
= NULL
;
451 copy
->textures
= orig
->textures
;
452 copy
->texture
= g_memdup(orig
->texture
, orig
->textures
* sizeof(Texture
));
454 copy
->xftdraw
= NULL
;
458 void appearance_free(Appearance
*a
)
462 if (a
->pixmap
!= None
) XFreePixmap(ob_display
, a
->pixmap
);
463 if (a
->xftdraw
!= NULL
) XftDrawDestroy(a
->xftdraw
);
466 if (a
->surface
.type
== Surface_Planar
) {
467 p
= &a
->surface
.data
.planar
;
468 color_free(p
->primary
);
469 color_free(p
->secondary
);
470 color_free(p
->border_color
);
471 color_free(p
->bevel_dark
);
472 color_free(p
->bevel_light
);
473 g_free(p
->pixel_data
);
480 void pixel32_to_pixmap(pixel32
*in
, Pixmap out
, int x
, int y
, int w
, int h
)
484 im
= XCreateImage(ob_display
, render_visual
, render_depth
,
485 ZPixmap
, 0, NULL
, w
, h
, 32, 0);
486 g_assert(im
!= NULL
);
487 im
->byte_order
= render_endian
;
488 /* this malloc is a complete waste of time on normal 32bpp
489 as reduce_depth just sets im->data = data and returns
491 scratch
= g_new(pixel32
, im
->width
* im
->height
);
492 im
->data
= (char*) scratch
;
493 reduce_depth(in
, im
);
494 XPutImage(ob_display
, out
, DefaultGC(ob_display
, ob_screen
),
495 im
, 0, 0, x
, y
, w
, h
);
501 void appearance_minsize(Appearance
*l
, int *w
, int *h
)
507 switch (l
->surface
.type
) {
509 if (l
->surface
.data
.planar
.relief
!= Flat
) {
510 switch (l
->surface
.data
.planar
.bevel
) {
518 } else if (l
->surface
.data
.planar
.border
)
521 for (i
= 0; i
< l
->textures
; ++i
) {
522 switch (l
->texture
[i
].type
) {
524 *w
+= l
->texture
[i
].data
.mask
.mask
->w
;
525 *h
+= l
->texture
[i
].data
.mask
.mask
->h
;
528 m
= font_measure_string(l
->texture
[i
].data
.text
.font
,
529 l
->texture
[i
].data
.text
.string
,
530 l
->texture
[i
].data
.text
.shadow
,
531 l
->texture
[i
].data
.text
.offset
);
533 m
= font_height(l
->texture
[i
].data
.text
.font
,
534 l
->texture
[i
].data
.text
.shadow
,
535 l
->texture
[i
].data
.text
.offset
);
539 *w
+= l
->texture
[i
].data
.rgba
.width
;
540 *h
+= l
->texture
[i
].data
.rgba
.height
;
550 gboolean
render_pixmap_to_rgba(Pixmap pmap
, Pixmap mask
,
551 int *w
, int *h
, pixel32
**data
)
555 guint pw
, ph
, mw
, mh
, xb
, xd
, i
, x
, y
, di
;
556 XImage
*xi
, *xm
= NULL
;
558 if (!XGetGeometry(ob_display
, pmap
, &xr
, &xx
, &xy
, &pw
, &ph
, &xb
, &xd
))
561 if (!XGetGeometry(ob_display
, mask
, &xr
, &xx
, &xy
, &mw
, &mh
, &xb
, &xd
))
563 if (pw
!= mw
|| ph
!= mh
|| xd
!= 1)
567 xi
= XGetImage(ob_display
, pmap
, 0, 0, pw
, ph
, 0xffffffff, ZPixmap
);
572 xm
= XGetImage(ob_display
, mask
, 0, 0, mw
, mh
, 0xffffffff, ZPixmap
);
577 *data
= g_new(pixel32
, pw
* ph
);
578 increase_depth(*data
, xi
);
581 /* apply transparency from the mask */
583 for (i
= 0, y
= 0; y
< ph
; ++y
) {
584 for (x
= 0; x
< pw
; ++x
, ++i
) {
585 if (!((((unsigned)xm
->data
[di
+ x
/ 8]) >> (x
% 8)) & 0x1))
586 (*data
)[i
] &= ~(0xff << default_alpha_offset
);
588 di
+= xm
->bytes_per_line
;
599 void gl_paint(Window win
, Appearance
*l
)
603 int i
, transferred
= 0, sw
, b
, d
;
604 pixel32
*source
, *dest
;
606 int tempx
, tempy
, absx
, absy
, absw
, absh
;
609 int w
= l
->area
.width
;
610 int h
= l
->area
.height
;
611 Rect tarea
; /* area in which to draw textures */
612 if (w
<= 0 || h
<= 0 || x
+w
<= 0 || y
+h
<= 0) return;
614 g_assert(l
->surface
.type
== Surface_Planar
);
616 printf("making %p, %p, %p current\n", ob_display
, win
, render_glx_context
);
617 err
= glXMakeCurrent(ob_display
, win
, render_glx_context
);
620 glMatrixMode(GL_MODELVIEW
);
622 glMatrixMode(GL_PROJECTION
);
624 glOrtho(0, 1376, 1032, 0, 0, 10);
625 if (XGetGeometry(ob_display
, win
, &root
, &tempx
, &tempy
,
626 &absw
, &absh
, &b
, &d
) &&
627 XTranslateCoordinates(ob_display
, win
, root
, tempx
, tempy
,
628 &absx
, &absy
, &child
))
629 printf("window at %d, %d (%d,%d)\n", absx
, absy
, absw
, absh
);
633 glViewport(0, 0, 1376, 1032);
634 glMatrixMode(GL_MODELVIEW
);
635 glTranslatef(-absx
, 1032-absh
-absy
, 0);
636 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
639 if (l
->surface
.data
.planar
.grad
== Background_ParentRelative
) {
642 render_gl_gradient(&l
->surface
, absx
+x
, absy
+y
, absw
, absh
);
644 glXSwapBuffers(ob_display
, win
);