10 #include "kernel/openbox.h"
17 Visual *render_visual;
18 Colormap render_colormap;
19 int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0;
20 int render_red_shift, render_green_shift, render_blue_shift;
22 void render_startup(void)
26 render_depth = DefaultDepth(ob_display, ob_screen);
27 render_visual = DefaultVisual(ob_display, ob_screen);
28 render_colormap = DefaultColormap(ob_display, ob_screen);
30 if (render_depth < 8) {
31 XVisualInfo vinfo_template, *vinfo_return;
32 /* search for a TrueColor Visual... if we can't find one...
33 we will use the default visual for the screen */
37 vinfo_template.screen = ob_screen;
38 vinfo_template.class = TrueColor;
39 vinfo_return = XGetVisualInfo(ob_display,
40 VisualScreenMask | VisualClassMask,
41 &vinfo_template, &vinfo_nitems);
45 for (i = 0; i < vinfo_nitems; ++i) {
46 if (vinfo_return[i].depth > max_depth) {
47 if (max_depth == 24 && vinfo_return[i].depth > 24)
48 break; /* prefer 24 bit over 32 */
49 max_depth = vinfo_return[i].depth;
53 if (max_depth < render_depth) best = -1;
56 render_depth = vinfo_return[best].depth;
57 render_visual = vinfo_return[best].visual;
58 render_colormap = XCreateColormap(ob_display, ob_root, render_visual,
63 switch (render_visual->class) {
71 pseudocolor_startup();
74 g_critical("unsupported visual class.\n");
80 void truecolor_startup(void)
82 unsigned long red_mask, green_mask, blue_mask;
83 XImage *timage = NULL;
85 timage = XCreateImage(ob_display, render_visual, render_depth,
86 ZPixmap, 0, NULL, 1, 1, 32, 0);
87 g_assert(timage != NULL);
88 /* find the offsets for each color in the visual's masks */
89 red_mask = timage->red_mask;
90 green_mask = timage->green_mask;
91 blue_mask = timage->blue_mask;
93 render_red_offset = 0;
94 render_green_offset = 0;
95 render_blue_offset = 0;
97 while (! (red_mask & 1)) { render_red_offset++; red_mask >>= 1; }
98 while (! (green_mask & 1)) { render_green_offset++; green_mask >>= 1; }
99 while (! (blue_mask & 1)) { render_blue_offset++; blue_mask >>= 1; }
101 render_red_shift = render_green_shift = render_blue_shift = 8;
102 while (red_mask) { red_mask >>= 1; render_red_shift--; }
103 while (green_mask) { green_mask >>= 1; render_green_shift--; }
104 while (blue_mask) { blue_mask >>= 1; render_blue_shift--; }
108 void pseudocolor_startup(void)
111 int tr, tg, tb, n, r, g, b, i, incolors, ii;
114 g_message("Initializing PseudoColor RenderControl\n");
116 /* determine the number of colors and the bits-per-color */
117 pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
118 g_assert(pseudo_bpc >= 1);
119 _ncolors = 1 << (pseudo_bpc * 3);
121 if (_ncolors > 1 << render_depth) {
122 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
123 pseudo_bpc = 1 << (render_depth/3) >> 3;
124 _ncolors = 1 << (pseudo_bpc * 3);
127 /* build a color cube */
128 pseudo_colors = malloc(_ncolors * sizeof(XColor));
129 cpc = 1 << pseudo_bpc; /* colors per channel */
131 for (n = 0, r = 0; r < cpc; r++)
132 for (g = 0; g < cpc; g++)
133 for (b = 0; b < cpc; b++, n++) {
134 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
135 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
136 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
137 pseudo_colors[n].red = tr | tr << 8;
138 pseudo_colors[n].green = tg | tg << 8;
139 pseudo_colors[n].blue = tb | tb << 8;
140 pseudo_colors[n].flags = DoRed|DoGreen|DoBlue; /* used to track
144 /* allocate the colors */
145 for (i = 0; i < _ncolors; i++)
146 if (!XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
147 pseudo_colors[i].flags = 0; /* mark it as unallocated */
149 /* try allocate any colors that failed allocation above */
151 /* get the allocated values from the X server (only the first 256 XXX why!?)
153 incolors = (((1 << render_depth) > 256) ? 256 : (1 << render_depth));
154 for (i = 0; i < incolors; i++)
155 icolors[i].pixel = i;
156 XQueryColors(ob_display, render_colormap, icolors, incolors);
158 /* try match unallocated ones */
159 for (i = 0; i < _ncolors; i++) {
160 if (!pseudo_colors[i].flags) { /* if it wasn't allocated... */
161 unsigned long closest = 0xffffffff, close = 0;
162 for (ii = 0; ii < incolors; ii++) {
163 /* find deviations */
164 r = (pseudo_colors[i].red - icolors[ii].red) & 0xff;
165 g = (pseudo_colors[i].green - icolors[ii].green) & 0xff;
166 b = (pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
167 /* find a weighted absolute deviation */
168 dev = (r * r) + (g * g) + (b * b);
176 pseudo_colors[i].red = icolors[close].red;
177 pseudo_colors[i].green = icolors[close].green;
178 pseudo_colors[i].blue = icolors[close].blue;
179 pseudo_colors[i].pixel = icolors[close].pixel;
181 /* try alloc this closest color, it had better succeed! */
182 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
183 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
185 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
191 void x_paint(Window win, Appearance *l)
193 int i, transferred = 0, sw;
194 pixel32 *source, *dest;
198 int w = l->area.width;
199 int h = l->area.height;
200 Rect tarea; /* area in which to draw textures */
202 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
204 g_assert(l->surface.type == Surface_Planar);
206 oldp = l->pixmap; /* save to free after changing the visible pixmap */
207 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
208 g_assert(l->pixmap != None);
210 if (l->xftdraw != NULL)
211 XftDrawDestroy(l->xftdraw);
212 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
214 g_assert(l->xftdraw != NULL);
216 g_free(l->surface.data.planar.pixel_data);
217 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
220 if (l->surface.data.planar.grad == Background_ParentRelative) {
221 sw = l->surface.data.planar.parent->area.width;
222 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
223 + l->surface.data.planar.parentx
224 + sw * l->surface.data.planar.parenty;
225 dest = l->surface.data.planar.pixel_data;
226 for (i = 0; i < h; i++, source += sw, dest += w) {
227 memcpy(dest, source, w * sizeof(pixel32));
230 else if (l->surface.data.planar.grad == Background_Solid)
231 gradient_solid(l, x, y, w, h);
232 else gradient_render(&l->surface, w, h);
234 for (i = 0; i < l->textures; i++) {
235 tarea = l->texture[i].position;
236 if (l->surface.data.planar.relief != Flat) {
237 switch (l->surface.data.planar.bevel) {
239 tarea.x += 1; tarea.y += 1;
240 tarea.width -= 2; tarea.height -= 2;
243 tarea.x += 2; tarea.y += 2;
244 tarea.width -= 4; tarea.height -= 4;
247 } else if (l->surface.data.planar.border) {
248 tarea.x += 1; tarea.y += 1;
249 tarea.width -= 2; tarea.height -= 2;
252 switch (l->texture[i].type) {
256 if (l->surface.data.planar.grad != Background_Solid)
257 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
260 if (l->xftdraw == NULL) {
261 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
262 render_visual, render_colormap);
264 font_draw(l->xftdraw, &l->texture[i].data.text,
270 if (l->surface.data.planar.grad != Background_Solid)
271 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
274 if (l->texture[i].data.mask.color->gc == None)
275 color_allocate_gc(l->texture[i].data.mask.color);
276 mask_draw(l->pixmap, &l->texture[i].data.mask,
280 image_draw(l->surface.data.planar.pixel_data,
281 &l->texture[i].data.rgba,
289 if (l->surface.data.planar.grad != Background_Solid)
290 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
295 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
296 XClearWindow(ob_display, win);
297 if (oldp != None) XFreePixmap(ob_display, oldp);
301 void gl_paint(Window win, Appearance *l)
303 glXMakeCurrent(ob_display, win, gl_context);
307 void render_shutdown(void)
311 Appearance *appearance_new(SurfaceType type, int numtex)
316 out = g_new(Appearance, 1);
317 out->surface.type = type;
318 out->textures = numtex;
320 if (numtex) out->texture = g_new0(Texture, numtex);
321 else out->texture = NULL;
326 p = &out->surface.data.planar;
329 p->border_color = NULL;
330 p->pixel_data = NULL;
336 Appearance *appearance_copy(Appearance *orig)
338 PlanarSurface *spo, *spc;
339 Appearance *copy = g_new(Appearance, 1);
340 copy->surface.type = orig->surface.type;
341 switch (orig->surface.type) {
343 spo = &(orig->surface.data.planar);
344 spc = &(copy->surface.data.planar);
345 spc->grad = spo->grad;
346 spc->relief = spo->relief;
347 spc->bevel = spo->bevel;
348 if (spo->primary != NULL)
349 spc->primary = color_new(spo->primary->r,
352 else spc->primary = NULL;
354 if (spo->secondary != NULL)
355 spc->secondary = color_new(spo->secondary->r,
358 else spc->secondary = NULL;
360 if (spo->border_color != NULL)
361 spc->border_color = color_new(spo->border_color->r,
362 spo->border_color->g,
363 spo->border_color->b);
364 else spc->border_color = NULL;
366 spc->interlaced = spo->interlaced;
367 spc->border = spo->border;
368 spc->pixel_data = NULL;
371 copy->textures = orig->textures;
372 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
374 copy->xftdraw = NULL;
378 void appearance_free(Appearance *a)
382 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
383 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
386 if (a->surface.type == Surface_Planar) {
387 p = &a->surface.data.planar;
388 if (p->primary != NULL) color_free(p->primary);
389 if (p->secondary != NULL) color_free(p->secondary);
390 if (p->border_color != NULL) color_free(p->border_color);
391 if (p->pixel_data != NULL) g_free(p->pixel_data);
398 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
400 unsigned char *scratch;
402 im = XCreateImage(ob_display, render_visual, render_depth,
403 ZPixmap, 0, NULL, w, h, 32, 0);
404 g_assert(im != NULL);
405 im->byte_order = endian;
406 /* this malloc is a complete waste of time on normal 32bpp
407 as reduce_depth just sets im->data = data and returns
409 scratch = malloc(im->width * im->height * sizeof(pixel32));
410 im->data = (char*) scratch;
411 reduce_depth(in, im);
412 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
413 im, 0, 0, x, y, w, h);
419 void appearance_minsize(Appearance *l, Size *s)
424 switch (l->surface.type) {
426 if (l->surface.data.planar.relief != Flat) {
427 switch (l->surface.data.planar.bevel) {
435 } else if (l->surface.data.planar.border)
438 for (i = 0; i < l->textures; ++i)
439 switch (l->texture[i].type) {
441 s->width += l->texture[i].data.mask.mask->w;
442 s->height += l->texture[i].data.mask.mask->h;
445 s->width +=font_measure_string(l->texture[i].data.text.font,
446 l->texture[i].data.text.string,
447 l->texture[i].data.text.shadow,
448 l->texture[i].data.text.offset);
449 s->height += font_height(l->texture[i].data.text.font,
450 l->texture[i].data.text.shadow,
451 l->texture[i].data.text.offset);
454 s->width += l->texture[i].data.rgba.width;
455 s->height += l->texture[i].data.rgba.height;