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;
201 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
203 g_assert(l->surface.type == Surface_Planar);
205 oldp = l->pixmap; /* save to free after changing the visible pixmap */
206 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
207 g_assert(l->pixmap != None);
209 if (l->xftdraw != NULL)
210 XftDrawDestroy(l->xftdraw);
211 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
213 g_assert(l->xftdraw != NULL);
215 g_free(l->surface.data.planar.pixel_data);
216 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
219 if (l->surface.data.planar.grad == Background_ParentRelative) {
220 sw = l->surface.data.planar.parent->area.width;
221 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
222 + l->surface.data.planar.parentx
223 + sw * l->surface.data.planar.parenty;
224 dest = l->surface.data.planar.pixel_data;
225 for (i = 0; i < h; i++, source += sw, dest += w) {
226 memcpy(dest, source, w * sizeof(pixel32));
229 else if (l->surface.data.planar.grad == Background_Solid)
230 gradient_solid(l, x, y, w, h);
231 else gradient_render(&l->surface, w, h);
233 for (i = 0; i < l->textures; i++) {
234 switch (l->texture[i].type) {
238 if (l->surface.data.planar.grad != Background_Solid)
239 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
242 if (l->xftdraw == NULL) {
243 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
244 render_visual, render_colormap);
246 font_draw(l->xftdraw, &l->texture[i].data.text,
247 &l->texture[i].position);
252 if (l->surface.data.planar.grad != Background_Solid)
253 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
256 if (l->texture[i].data.mask.color->gc == None)
257 color_allocate_gc(l->texture[i].data.mask.color);
258 mask_draw(l->pixmap, &l->texture[i].data.mask,
259 &l->texture[i].position);
262 image_draw(l->surface.data.planar.pixel_data,
263 &l->texture[i].data.rgba,
264 &l->texture[i].position);
271 if (l->surface.data.planar.grad != Background_Solid)
272 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
277 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
278 XClearWindow(ob_display, win);
279 if (oldp != None) XFreePixmap(ob_display, oldp);
283 void gl_paint(Window win, Appearance *l)
285 glXMakeCurrent(ob_display, win, gl_context);
289 void render_shutdown(void)
293 Appearance *appearance_new(SurfaceType type, int numtex)
298 out = g_new(Appearance, 1);
299 out->surface.type = type;
300 out->textures = numtex;
302 if (numtex) out->texture = g_new0(Texture, numtex);
303 else out->texture = NULL;
308 p = &out->surface.data.planar;
311 p->border_color = NULL;
312 p->pixel_data = NULL;
318 Appearance *appearance_copy(Appearance *orig)
320 PlanarSurface *spo, *spc;
321 Appearance *copy = g_new(Appearance, 1);
322 copy->surface.type = orig->surface.type;
323 switch (orig->surface.type) {
325 spo = &(orig->surface.data.planar);
326 spc = &(copy->surface.data.planar);
327 spc->grad = spo->grad;
328 spc->relief = spo->relief;
329 spc->bevel = spo->bevel;
330 if (spo->primary != NULL)
331 spc->primary = color_new(spo->primary->r,
334 else spc->primary = NULL;
336 if (spo->secondary != NULL)
337 spc->secondary = color_new(spo->secondary->r,
340 else spc->secondary = NULL;
342 if (spo->border_color != NULL)
343 spc->border_color = color_new(spo->border_color->r,
344 spo->border_color->g,
345 spo->border_color->b);
346 else spc->border_color = NULL;
348 spc->interlaced = spo->interlaced;
349 spc->border = spo->border;
350 spc->pixel_data = NULL;
353 copy->textures = orig->textures;
354 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
356 copy->xftdraw = NULL;
360 void appearance_free(Appearance *a)
363 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
364 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
367 if (a->surface.type == Surface_Planar) {
368 p = &a->surface.data.planar;
369 if (p->primary != NULL) color_free(p->primary);
370 if (p->secondary != NULL) color_free(p->secondary);
371 if (p->border_color != NULL) color_free(p->border_color);
372 if (p->pixel_data != NULL) g_free(p->pixel_data);
378 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
380 unsigned char *scratch;
382 im = XCreateImage(ob_display, render_visual, render_depth,
383 ZPixmap, 0, NULL, w, h, 32, 0);
384 g_assert(im != NULL);
385 im->byte_order = endian;
386 /* this malloc is a complete waste of time on normal 32bpp
387 as reduce_depth just sets im->data = data and returns
389 scratch = malloc(im->width * im->height * sizeof(pixel32));
391 reduce_depth(in, im);
392 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
393 im, 0, 0, x, y, w, h);
399 void appearance_minsize(Appearance *l, Size *s)
404 switch (l->surface.type) {
406 if (l->surface.data.planar.border ||
407 l->surface.data.planar.bevel == Bevel1)
409 else if (l->surface.data.planar.bevel == Bevel2)
412 for (i = 0; i < l->textures; ++i)
413 switch (l->texture[i].type) {
415 s->width += l->texture[i].data.mask.mask->w;
416 s->height += l->texture[i].data.mask.mask->h;
419 s->width +=font_measure_string(l->texture[i].data.text.font,
420 l->texture[i].data.text.string,
421 l->texture[i].data.text.shadow,
422 l->texture[i].data.text.offset);
423 s->height += font_height(l->texture[i].data.text.font,
424 l->texture[i].data.text.shadow,
425 l->texture[i].data.text.offset);
428 s->width += l->texture[i].data.rgba.width;
429 s->height += l->texture[i].data.rgba.height;