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) * (0xff - (icolors[ii].red & 0xff)) +
171 (g * g) * (0xff - (icolors[ii].green & 0xff)) +
172 (b * b) * (0xff - (icolors[ii].blue & 0xff));
180 pseudo_colors[i].red = icolors[close].red;
181 pseudo_colors[i].green = icolors[close].green;
182 pseudo_colors[i].blue = icolors[close].blue;
183 pseudo_colors[i].pixel = icolors[close].pixel;
185 /* try alloc this closest color, it had better succeed! */
186 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
187 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
189 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
195 void x_paint(Window win, Appearance *l)
197 int i, transferred = 0, sw;
198 pixel32 *source, *dest;
202 int w = l->area.width;
203 int h = l->area.height;
204 Rect tarea; /* area in which to draw textures */
206 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
208 g_assert(l->surface.type == Surface_Planar);
210 oldp = l->pixmap; /* save to free after changing the visible pixmap */
211 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
212 g_assert(l->pixmap != None);
214 if (l->xftdraw != NULL)
215 XftDrawDestroy(l->xftdraw);
216 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
218 g_assert(l->xftdraw != NULL);
220 g_free(l->surface.data.planar.pixel_data);
221 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
224 if (l->surface.data.planar.grad == Background_ParentRelative) {
225 sw = l->surface.data.planar.parent->area.width;
226 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
227 + l->surface.data.planar.parentx
228 + sw * l->surface.data.planar.parenty;
229 dest = l->surface.data.planar.pixel_data;
230 for (i = 0; i < h; i++, source += sw, dest += w) {
231 memcpy(dest, source, w * sizeof(pixel32));
234 else if (l->surface.data.planar.grad == Background_Solid)
235 gradient_solid(l, x, y, w, h);
236 else gradient_render(&l->surface, w, h);
238 for (i = 0; i < l->textures; i++) {
239 tarea = l->texture[i].position;
240 if (l->surface.data.planar.grad != Background_ParentRelative) {
241 if (l->surface.data.planar.relief != Flat) {
242 switch (l->surface.data.planar.bevel) {
244 tarea.x += 1; tarea.y += 1;
245 tarea.width -= 2; tarea.height -= 2;
248 tarea.x += 2; tarea.y += 2;
249 tarea.width -= 4; tarea.height -= 4;
252 } else if (l->surface.data.planar.border) {
253 tarea.x += 1; tarea.y += 1;
254 tarea.width -= 2; tarea.height -= 2;
258 switch (l->texture[i].type) {
262 if (l->surface.data.planar.grad != Background_Solid)
263 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
266 if (l->xftdraw == NULL) {
267 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
268 render_visual, render_colormap);
270 font_draw(l->xftdraw, &l->texture[i].data.text,
276 if (l->surface.data.planar.grad != Background_Solid)
277 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
280 if (l->texture[i].data.mask.color->gc == None)
281 color_allocate_gc(l->texture[i].data.mask.color);
282 mask_draw(l->pixmap, &l->texture[i].data.mask,
286 image_draw(l->surface.data.planar.pixel_data,
287 &l->texture[i].data.rgba,
295 if (l->surface.data.planar.grad != Background_Solid)
296 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
301 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
302 XClearWindow(ob_display, win);
303 if (oldp != None) XFreePixmap(ob_display, oldp);
307 void gl_paint(Window win, Appearance *l)
309 glXMakeCurrent(ob_display, win, gl_context);
313 void render_shutdown(void)
317 Appearance *appearance_new(SurfaceType type, int numtex)
322 out = g_new(Appearance, 1);
323 out->surface.type = type;
324 out->textures = numtex;
326 if (numtex) out->texture = g_new0(Texture, numtex);
327 else out->texture = NULL;
332 p = &out->surface.data.planar;
335 p->border_color = 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 spc->interlaced = spo->interlaced;
373 spc->border = spo->border;
374 spc->pixel_data = NULL;
377 copy->textures = orig->textures;
378 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
380 copy->xftdraw = NULL;
384 void appearance_free(Appearance *a)
388 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
389 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
392 if (a->surface.type == Surface_Planar) {
393 p = &a->surface.data.planar;
394 if (p->primary != NULL) color_free(p->primary);
395 if (p->secondary != NULL) color_free(p->secondary);
396 if (p->border_color != NULL) color_free(p->border_color);
397 if (p->pixel_data != NULL) g_free(p->pixel_data);
404 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
408 im = XCreateImage(ob_display, render_visual, render_depth,
409 ZPixmap, 0, NULL, w, h, 32, 0);
410 g_assert(im != NULL);
411 im->byte_order = render_endian;
412 /* this malloc is a complete waste of time on normal 32bpp
413 as reduce_depth just sets im->data = data and returns
415 scratch = g_new(pixel32, im->width * im->height);
416 im->data = (char*) scratch;
417 reduce_depth(in, im);
418 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
419 im, 0, 0, x, y, w, h);
425 void appearance_minsize(Appearance *l, int *w, int *h)
431 switch (l->surface.type) {
433 if (l->surface.data.planar.relief != Flat) {
434 switch (l->surface.data.planar.bevel) {
442 } else if (l->surface.data.planar.border)
445 for (i = 0; i < l->textures; ++i) {
446 switch (l->texture[i].type) {
448 *w += l->texture[i].data.mask.mask->w;
449 *h += l->texture[i].data.mask.mask->h;
452 m = font_measure_string(l->texture[i].data.text.font,
453 l->texture[i].data.text.string,
454 l->texture[i].data.text.shadow,
455 l->texture[i].data.text.offset);
457 m = font_height(l->texture[i].data.text.font,
458 l->texture[i].data.text.shadow,
459 l->texture[i].data.text.offset);
463 *w += l->texture[i].data.rgba.width;
464 *h += l->texture[i].data.rgba.height;
474 gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask,
475 int *w, int *h, pixel32 **data)
479 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
480 XImage *xi, *xm = NULL;
482 if (!XGetGeometry(ob_display, pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
485 if (!XGetGeometry(ob_display, mask, &xr, &xx, &xy, &mw, &mh, &xb, &xd))
487 if (pw != mw || ph != mh || xd != 1)
491 xi = XGetImage(ob_display, pmap, 0, 0, pw, ph, 0xffffffff, ZPixmap);
496 xm = XGetImage(ob_display, mask, 0, 0, mw, mh, 0xffffffff, ZPixmap);
501 *data = g_new(pixel32, pw * ph);
502 increase_depth(*data, xi);
505 /* apply transparency from the mask */
507 for (i = 0, y = 0; y < ph; ++y) {
508 for (x = 0; x < pw; ++x, ++i) {
509 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
510 (*data)[i] &= ~(0xff << default_alpha_offset);
512 di += xm->bytes_per_line;