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;
23 void render_startup(void)
27 render_depth = DefaultDepth(ob_display, ob_screen);
28 render_visual = DefaultVisual(ob_display, ob_screen);
29 render_colormap = DefaultColormap(ob_display, ob_screen);
31 if (render_depth < 8) {
32 XVisualInfo vinfo_template, *vinfo_return;
33 /* search for a TrueColor Visual... if we can't find one...
34 we will use the default visual for the screen */
38 vinfo_template.screen = ob_screen;
39 vinfo_template.class = TrueColor;
40 vinfo_return = XGetVisualInfo(ob_display,
41 VisualScreenMask | VisualClassMask,
42 &vinfo_template, &vinfo_nitems);
46 for (i = 0; i < vinfo_nitems; ++i) {
47 if (vinfo_return[i].depth > max_depth) {
48 if (max_depth == 24 && vinfo_return[i].depth > 24)
49 break; /* prefer 24 bit over 32 */
50 max_depth = vinfo_return[i].depth;
54 if (max_depth < render_depth) best = -1;
57 render_depth = vinfo_return[best].depth;
58 render_visual = vinfo_return[best].visual;
59 render_colormap = XCreateColormap(ob_display, ob_root, render_visual,
64 switch (render_visual->class) {
72 pseudocolor_startup();
75 g_critical("unsupported visual class.\n");
81 void truecolor_startup(void)
83 unsigned long red_mask, green_mask, blue_mask;
84 XImage *timage = NULL;
86 timage = XCreateImage(ob_display, render_visual, render_depth,
87 ZPixmap, 0, NULL, 1, 1, 32, 0);
88 g_assert(timage != NULL);
89 /* find the offsets for each color in the visual's masks */
90 red_mask = timage->red_mask;
91 green_mask = timage->green_mask;
92 blue_mask = timage->blue_mask;
94 render_red_offset = 0;
95 render_green_offset = 0;
96 render_blue_offset = 0;
98 while (! (red_mask & 1)) { render_red_offset++; red_mask >>= 1; }
99 while (! (green_mask & 1)) { render_green_offset++; green_mask >>= 1; }
100 while (! (blue_mask & 1)) { render_blue_offset++; blue_mask >>= 1; }
102 render_red_shift = render_green_shift = render_blue_shift = 8;
103 while (red_mask) { red_mask >>= 1; render_red_shift--; }
104 while (green_mask) { green_mask >>= 1; render_green_shift--; }
105 while (blue_mask) { blue_mask >>= 1; render_blue_shift--; }
109 void pseudocolor_startup(void)
112 int tr, tg, tb, n, r, g, b, i, incolors, ii;
115 g_message("Initializing PseudoColor RenderControl\n");
117 /* determine the number of colors and the bits-per-color */
118 pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
119 g_assert(pseudo_bpc >= 1);
120 _ncolors = 1 << (pseudo_bpc * 3);
122 if (_ncolors > 1 << render_depth) {
123 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
124 pseudo_bpc = 1 << (render_depth/3) >> 3;
125 _ncolors = 1 << (pseudo_bpc * 3);
128 /* build a color cube */
129 pseudo_colors = malloc(_ncolors * sizeof(XColor));
130 cpc = 1 << pseudo_bpc; /* colors per channel */
132 for (n = 0, r = 0; r < cpc; r++)
133 for (g = 0; g < cpc; g++)
134 for (b = 0; b < cpc; b++, n++) {
135 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
136 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
137 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
138 pseudo_colors[n].red = tr | tr << 8;
139 pseudo_colors[n].green = tg | tg << 8;
140 pseudo_colors[n].blue = tb | tb << 8;
141 pseudo_colors[n].flags = DoRed|DoGreen|DoBlue; /* used to track
145 /* allocate the colors */
146 for (i = 0; i < _ncolors; i++)
147 if (!XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
148 pseudo_colors[i].flags = 0; /* mark it as unallocated */
150 /* try allocate any colors that failed allocation above */
152 /* get the allocated values from the X server (only the first 256 XXX why!?)
154 incolors = (((1 << render_depth) > 256) ? 256 : (1 << render_depth));
155 for (i = 0; i < incolors; i++)
156 icolors[i].pixel = i;
157 XQueryColors(ob_display, render_colormap, icolors, incolors);
159 /* try match unallocated ones */
160 for (i = 0; i < _ncolors; i++) {
161 if (!pseudo_colors[i].flags) { /* if it wasn't allocated... */
162 unsigned long closest = 0xffffffff, close = 0;
163 for (ii = 0; ii < incolors; ii++) {
164 /* find deviations */
165 r = (pseudo_colors[i].red - icolors[ii].red) & 0xff;
166 g = (pseudo_colors[i].green - icolors[ii].green) & 0xff;
167 b = (pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
168 /* find a weighted absolute deviation */
169 dev = (r * r) + (g * g) + (b * b);
177 pseudo_colors[i].red = icolors[close].red;
178 pseudo_colors[i].green = icolors[close].green;
179 pseudo_colors[i].blue = icolors[close].blue;
180 pseudo_colors[i].pixel = icolors[close].pixel;
182 /* try alloc this closest color, it had better succeed! */
183 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
184 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
186 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
192 void x_paint(Window win, Appearance *l)
194 int i, transferred = 0, sw;
195 pixel32 *source, *dest;
199 int w = l->area.width;
200 int h = l->area.height;
201 Rect tarea; /* area in which to draw textures */
203 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
205 g_assert(l->surface.type == Surface_Planar);
207 oldp = l->pixmap; /* save to free after changing the visible pixmap */
208 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
209 g_assert(l->pixmap != None);
211 if (l->xftdraw != NULL)
212 XftDrawDestroy(l->xftdraw);
213 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
215 g_assert(l->xftdraw != NULL);
217 g_free(l->surface.data.planar.pixel_data);
218 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
221 if (l->surface.data.planar.grad == Background_ParentRelative) {
222 sw = l->surface.data.planar.parent->area.width;
223 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
224 + l->surface.data.planar.parentx
225 + sw * l->surface.data.planar.parenty;
226 dest = l->surface.data.planar.pixel_data;
227 for (i = 0; i < h; i++, source += sw, dest += w) {
228 memcpy(dest, source, w * sizeof(pixel32));
231 else if (l->surface.data.planar.grad == Background_Solid)
232 gradient_solid(l, x, y, w, h);
233 else gradient_render(&l->surface, w, h);
235 for (i = 0; i < l->textures; i++) {
236 tarea = l->texture[i].position;
237 if (l->surface.data.planar.grad != Background_ParentRelative) {
238 if (l->surface.data.planar.relief != Flat) {
239 switch (l->surface.data.planar.bevel) {
241 tarea.x += 1; tarea.y += 1;
242 tarea.width -= 2; tarea.height -= 2;
245 tarea.x += 2; tarea.y += 2;
246 tarea.width -= 4; tarea.height -= 4;
249 } else if (l->surface.data.planar.border) {
250 tarea.x += 1; tarea.y += 1;
251 tarea.width -= 2; tarea.height -= 2;
255 switch (l->texture[i].type) {
259 if (l->surface.data.planar.grad != Background_Solid)
260 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
263 if (l->xftdraw == NULL) {
264 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
265 render_visual, render_colormap);
267 font_draw(l->xftdraw, &l->texture[i].data.text,
273 if (l->surface.data.planar.grad != Background_Solid)
274 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
277 if (l->texture[i].data.mask.color->gc == None)
278 color_allocate_gc(l->texture[i].data.mask.color);
279 mask_draw(l->pixmap, &l->texture[i].data.mask,
283 image_draw(l->surface.data.planar.pixel_data,
284 &l->texture[i].data.rgba,
292 if (l->surface.data.planar.grad != Background_Solid)
293 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
298 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
299 XClearWindow(ob_display, win);
300 if (oldp != None) XFreePixmap(ob_display, oldp);
304 void gl_paint(Window win, Appearance *l)
306 glXMakeCurrent(ob_display, win, gl_context);
310 void render_shutdown(void)
314 Appearance *appearance_new(SurfaceType type, int numtex)
319 out = g_new(Appearance, 1);
320 out->surface.type = type;
321 out->textures = numtex;
323 if (numtex) out->texture = g_new0(Texture, numtex);
324 else out->texture = NULL;
329 p = &out->surface.data.planar;
332 p->border_color = NULL;
333 p->pixel_data = NULL;
339 Appearance *appearance_copy(Appearance *orig)
341 PlanarSurface *spo, *spc;
342 Appearance *copy = g_new(Appearance, 1);
343 copy->surface.type = orig->surface.type;
344 switch (orig->surface.type) {
346 spo = &(orig->surface.data.planar);
347 spc = &(copy->surface.data.planar);
348 spc->grad = spo->grad;
349 spc->relief = spo->relief;
350 spc->bevel = spo->bevel;
351 if (spo->primary != NULL)
352 spc->primary = color_new(spo->primary->r,
355 else spc->primary = NULL;
357 if (spo->secondary != NULL)
358 spc->secondary = color_new(spo->secondary->r,
361 else spc->secondary = NULL;
363 if (spo->border_color != NULL)
364 spc->border_color = color_new(spo->border_color->r,
365 spo->border_color->g,
366 spo->border_color->b);
367 else spc->border_color = NULL;
369 spc->interlaced = spo->interlaced;
370 spc->border = spo->border;
371 spc->pixel_data = NULL;
374 copy->textures = orig->textures;
375 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
377 copy->xftdraw = NULL;
381 void appearance_free(Appearance *a)
385 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
386 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
389 if (a->surface.type == Surface_Planar) {
390 p = &a->surface.data.planar;
391 if (p->primary != NULL) color_free(p->primary);
392 if (p->secondary != NULL) color_free(p->secondary);
393 if (p->border_color != NULL) color_free(p->border_color);
394 if (p->pixel_data != NULL) g_free(p->pixel_data);
401 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
403 unsigned char *scratch;
405 im = XCreateImage(ob_display, render_visual, render_depth,
406 ZPixmap, 0, NULL, w, h, 32, 0);
407 g_assert(im != NULL);
408 im->byte_order = endian;
409 /* this malloc is a complete waste of time on normal 32bpp
410 as reduce_depth just sets im->data = data and returns
412 scratch = malloc(im->width * im->height * sizeof(pixel32));
413 im->data = (char*) scratch;
414 reduce_depth(in, im);
415 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
416 im, 0, 0, x, y, w, h);
422 void appearance_minsize(Appearance *l, int *w, int *h)
428 switch (l->surface.type) {
430 if (l->surface.data.planar.relief != Flat) {
431 switch (l->surface.data.planar.bevel) {
439 } else if (l->surface.data.planar.border)
442 for (i = 0; i < l->textures; ++i) {
443 switch (l->texture[i].type) {
445 *w += l->texture[i].data.mask.mask->w;
446 *h += l->texture[i].data.mask.mask->h;
449 m = font_measure_string(l->texture[i].data.text.font,
450 l->texture[i].data.text.string,
451 l->texture[i].data.text.shadow,
452 l->texture[i].data.text.offset);
454 m = font_height(l->texture[i].data.text.font,
455 l->texture[i].data.text.shadow,
456 l->texture[i].data.text.offset);
460 *w += l->texture[i].data.rgba.width;
461 *h += l->texture[i].data.rgba.height;
471 void render_pixmap_to_rgba(Pixmap pmap, Pixmap mask,
472 int *w, int *h, gulong **data)