12 #include "kernel/openbox.h"
19 XVisualInfo render_visual_info;
21 Visual *render_visual;
22 Colormap render_colormap;
24 int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0;
25 int render_red_shift, render_green_shift, render_blue_shift;
26 int render_red_mask, render_green_mask, render_blue_mask;
29 void render_startup(void)
31 render_depth = DefaultDepth(ob_display, ob_screen);
32 render_visual = DefaultVisual(ob_display, ob_screen);
33 render_colormap = DefaultColormap(ob_display, ob_screen);
35 switch (render_visual->class) {
43 pseudocolor_startup();
46 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 paint(Window win, Appearance *l, int w, int h)
164 int i, transferred = 0, sw;
165 pixel32 *source, *dest;
167 Rect tarea; /* area in which to draw textures */
170 if (w <= 0 || h <= 0) return;
172 resized = (l->w != w || l->h != h);
175 oldp = l->pixmap; /* save to free after changing the visible pixmap */
176 l->pixmap = XCreatePixmap(ob_display, ob_root, w, h, render_depth);
180 g_assert(l->pixmap != None);
184 if (l->xftdraw != NULL)
185 XftDrawDestroy(l->xftdraw);
186 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
188 g_assert(l->xftdraw != NULL);
190 g_free(l->surface.pixel_data);
191 l->surface.pixel_data = g_new(pixel32, w * h);
193 if (l->surface.grad == Background_ParentRelative) {
194 g_assert (l->surface.parent);
195 g_assert (l->surface.parent->w);
197 sw = l->surface.parent->w;
198 source = (l->surface.parent->surface.pixel_data + l->surface.parentx +
199 sw * l->surface.parenty);
200 dest = l->surface.pixel_data;
201 for (i = 0; i < h; i++, source += sw, dest += w) {
202 memcpy(dest, source, w * sizeof(pixel32));
205 else if (l->surface.grad == Background_Solid)
206 gradient_solid(l, 0, 0, w, h);
207 else gradient_render(&l->surface, w, h);
209 RECT_SET(tarea, 0, 0, w, h);
210 if (l->surface.grad != Background_ParentRelative) {
211 if (l->surface.relief != Flat) {
212 switch (l->surface.bevel) {
214 tarea.x += 1; tarea.y += 1;
215 tarea.width -= 2; tarea.height -= 2;
218 tarea.x += 2; tarea.y += 2;
219 tarea.width -= 4; tarea.height -= 4;
222 } else if (l->surface.border) {
223 tarea.x += 1; tarea.y += 1;
224 tarea.width -= 2; tarea.height -= 2;
228 for (i = 0; i < l->textures; i++) {
229 switch (l->texture[i].type) {
235 if (l->surface.grad != Background_Solid)
236 pixel32_to_pixmap(l->surface.pixel_data,
237 l->pixmap, 0, 0, w, h);
239 if (l->xftdraw == NULL) {
240 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
241 render_visual, render_colormap);
243 font_draw(l->xftdraw, &l->texture[i].data.text, &tarea);
248 if (l->surface.grad != Background_Solid)
249 pixel32_to_pixmap(l->surface.pixel_data,
250 l->pixmap, 0, 0, w, h);
252 if (l->texture[i].data.mask.color->gc == None)
253 color_allocate_gc(l->texture[i].data.mask.color);
254 mask_draw(l->pixmap, &l->texture[i].data.mask, &tarea);
257 image_draw(l->surface.pixel_data,
258 &l->texture[i].data.rgba, &tarea);
265 if (l->surface.grad != Background_Solid)
266 pixel32_to_pixmap(l->surface.pixel_data, l->pixmap, 0, 0, w, h);
270 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
271 XClearWindow(ob_display, win);
272 if (oldp) XFreePixmap(ob_display, oldp);
275 void render_shutdown(void)
279 Appearance *appearance_new(int numtex)
284 out = g_new(Appearance, 1);
285 out->textures = numtex;
287 if (numtex) out->texture = g_new0(Texture, numtex);
288 else out->texture = NULL;
295 p->border_color = NULL;
296 p->bevel_dark = NULL;
297 p->bevel_light = NULL;
298 p->pixel_data = NULL;
302 Appearance *appearance_copy(Appearance *orig)
305 Appearance *copy = g_new(Appearance, 1);
307 spo = &(orig->surface);
308 spc = &(copy->surface);
309 spc->grad = spo->grad;
310 spc->relief = spo->relief;
311 spc->bevel = spo->bevel;
312 if (spo->primary != NULL)
313 spc->primary = color_new(spo->primary->r,
316 else spc->primary = NULL;
318 if (spo->secondary != NULL)
319 spc->secondary = color_new(spo->secondary->r,
322 else spc->secondary = NULL;
324 if (spo->border_color != NULL)
325 spc->border_color = color_new(spo->border_color->r,
326 spo->border_color->g,
327 spo->border_color->b);
328 else spc->border_color = NULL;
330 if (spo->bevel_dark != NULL)
331 spc->bevel_dark = color_new(spo->bevel_dark->r,
334 else spc->bevel_dark = NULL;
336 if (spo->bevel_light != NULL)
337 spc->bevel_light = color_new(spo->bevel_light->r,
339 spo->bevel_light->b);
340 else spc->bevel_light = NULL;
342 spc->interlaced = spo->interlaced;
343 spc->border = spo->border;
344 spc->pixel_data = NULL;
346 copy->textures = orig->textures;
347 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
349 copy->xftdraw = NULL;
350 copy->w = copy->h = 0;
354 void appearance_free(Appearance *a)
358 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
359 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
363 color_free(p->primary);
364 color_free(p->secondary);
365 color_free(p->border_color);
366 color_free(p->bevel_dark);
367 color_free(p->bevel_light);
368 g_free(p->pixel_data);
375 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
379 im = XCreateImage(ob_display, render_visual, render_depth,
380 ZPixmap, 0, NULL, w, h, 32, 0);
381 g_assert(im != NULL);
382 im->byte_order = render_endian;
383 /* this malloc is a complete waste of time on normal 32bpp
384 as reduce_depth just sets im->data = data and returns
386 scratch = g_new(pixel32, im->width * im->height);
387 im->data = (char*) scratch;
388 reduce_depth(in, im);
389 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
390 im, 0, 0, x, y, w, h);
396 void appearance_minsize(Appearance *l, int *w, int *h)
402 for (i = 0; i < l->textures; ++i) {
403 switch (l->texture[i].type) {
405 *w = MAX(*w, l->texture[i].data.mask.mask->w);
406 *h = MAX(*h, l->texture[i].data.mask.mask->h);
409 m = font_measure_string(l->texture[i].data.text.font,
410 l->texture[i].data.text.string,
411 l->texture[i].data.text.shadow,
412 l->texture[i].data.text.offset);
414 m = font_height(l->texture[i].data.text.font,
415 l->texture[i].data.text.shadow,
416 l->texture[i].data.text.offset);
420 *w += MAX(*w, l->texture[i].data.rgba.width);
421 *h += MAX(*h, l->texture[i].data.rgba.height);
428 if (l->surface.relief != Flat) {
429 switch (l->surface.bevel) {
439 } else if (l->surface.border) {
448 gboolean render_pixmap_to_rgba(Pixmap pmap, Pixmap mask,
449 int *w, int *h, pixel32 **data)
453 guint pw, ph, mw, mh, xb, xd, i, x, y, di;
454 XImage *xi, *xm = NULL;
456 if (!XGetGeometry(ob_display, pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
459 if (!XGetGeometry(ob_display, mask, &xr, &xx, &xy, &mw, &mh, &xb, &xd))
461 if (pw != mw || ph != mh || xd != 1)
465 xi = XGetImage(ob_display, pmap, 0, 0, pw, ph, 0xffffffff, ZPixmap);
470 xm = XGetImage(ob_display, mask, 0, 0, mw, mh, 0xffffffff, ZPixmap);
475 *data = g_new(pixel32, pw * ph);
476 increase_depth(*data, xi);
479 /* apply transparency from the mask */
481 for (i = 0, y = 0; y < ph; ++y) {
482 for (x = 0; x < pw; ++x, ++i) {
483 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
484 (*data)[i] &= ~(0xff << default_alpha_offset);
486 di += xm->bytes_per_line;