10 #include "../kernel/openbox.h"
13 Visual *render_visual;
14 Colormap render_colormap;
15 int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0;
16 int render_red_shift, render_green_shift, render_blue_shift;
18 void render_startup(void)
22 render_depth = DefaultDepth(ob_display, ob_screen);
23 render_visual = DefaultVisual(ob_display, ob_screen);
24 render_colormap = DefaultColormap(ob_display, ob_screen);
26 if (render_depth < 8) {
27 XVisualInfo vinfo_template, *vinfo_return;
28 /* search for a TrueColor Visual... if we can't find one...
29 we will use the default visual for the screen */
33 vinfo_template.screen = ob_screen;
34 vinfo_template.class = TrueColor;
35 vinfo_return = XGetVisualInfo(ob_display,
36 VisualScreenMask | VisualClassMask,
37 &vinfo_template, &vinfo_nitems);
41 for (i = 0; i < vinfo_nitems; ++i) {
42 if (vinfo_return[i].depth > max_depth) {
43 if (max_depth == 24 && vinfo_return[i].depth > 24)
44 break; /* prefer 24 bit over 32 */
45 max_depth = vinfo_return[i].depth;
49 if (max_depth < render_depth) best = -1;
52 render_depth = vinfo_return[best].depth;
53 render_visual = vinfo_return[best].visual;
54 render_colormap = XCreateColormap(ob_display, ob_root, render_visual,
59 switch (render_visual->class) {
67 pseudocolor_startup();
70 g_critical("unsupported visual class.\n");
76 void truecolor_startup(void)
78 unsigned long red_mask, green_mask, blue_mask;
79 XImage *timage = NULL;
81 timage = XCreateImage(ob_display, render_visual, render_depth,
82 ZPixmap, 0, NULL, 1, 1, 32, 0);
83 g_assert(timage != NULL);
84 /* find the offsets for each color in the visual's masks */
85 red_mask = timage->red_mask;
86 green_mask = timage->green_mask;
87 blue_mask = timage->blue_mask;
89 render_red_offset = 0;
90 render_green_offset = 0;
91 render_blue_offset = 0;
93 while (! (red_mask & 1)) { render_red_offset++; red_mask >>= 1; }
94 while (! (green_mask & 1)) { render_green_offset++; green_mask >>= 1; }
95 while (! (blue_mask & 1)) { render_blue_offset++; blue_mask >>= 1; }
97 render_red_shift = render_green_shift = render_blue_shift = 8;
98 while (red_mask) { red_mask >>= 1; render_red_shift--; }
99 while (green_mask) { green_mask >>= 1; render_green_shift--; }
100 while (blue_mask) { blue_mask >>= 1; render_blue_shift--; }
104 void pseudocolor_startup(void)
107 int tr, tg, tb, n, r, g, b, i, incolors, ii;
110 g_message("Initializing PseudoColor RenderControl\n");
112 /* determine the number of colors and the bits-per-color */
113 pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
114 g_assert(pseudo_bpc >= 1);
115 _ncolors = 1 << (pseudo_bpc * 3);
117 if (_ncolors > 1 << render_depth) {
118 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
119 pseudo_bpc = 1 << (render_depth/3) >> 3;
120 _ncolors = 1 << (pseudo_bpc * 3);
123 /* build a color cube */
124 pseudo_colors = malloc(_ncolors * sizeof(XColor));
125 cpc = 1 << pseudo_bpc; /* colors per channel */
127 for (n = 0, r = 0; r < cpc; r++)
128 for (g = 0; g < cpc; g++)
129 for (b = 0; b < cpc; b++, n++) {
130 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
131 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
132 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
133 pseudo_colors[n].red = tr | tr << 8;
134 pseudo_colors[n].green = tg | tg << 8;
135 pseudo_colors[n].blue = tb | tb << 8;
136 pseudo_colors[n].flags = DoRed|DoGreen|DoBlue; /* used to track
140 /* allocate the colors */
141 for (i = 0; i < _ncolors; i++)
142 if (!XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
143 pseudo_colors[i].flags = 0; /* mark it as unallocated */
145 /* try allocate any colors that failed allocation above */
147 /* get the allocated values from the X server (only the first 256 XXX why!?)
149 incolors = (((1 << render_depth) > 256) ? 256 : (1 << render_depth));
150 for (i = 0; i < incolors; i++)
151 icolors[i].pixel = i;
152 XQueryColors(ob_display, render_colormap, icolors, incolors);
154 /* try match unallocated ones */
155 for (i = 0; i < _ncolors; i++) {
156 if (!pseudo_colors[i].flags) { /* if it wasn't allocated... */
157 unsigned long closest = 0xffffffff, close = 0;
158 for (ii = 0; ii < incolors; ii++) {
159 /* find deviations */
160 r = (pseudo_colors[i].red - icolors[ii].red) & 0xff;
161 g = (pseudo_colors[i].green - icolors[ii].green) & 0xff;
162 b = (pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
163 /* find a weighted absolute deviation */
164 dev = (r * r) + (g * g) + (b * b);
172 pseudo_colors[i].red = icolors[close].red;
173 pseudo_colors[i].green = icolors[close].green;
174 pseudo_colors[i].blue = icolors[close].blue;
175 pseudo_colors[i].pixel = icolors[close].pixel;
177 /* try alloc this closest color, it had better succeed! */
178 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
179 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
181 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
187 void x_paint(Window win, Appearance *l)
189 int i, transferred = 0, sw;
190 pixel32 *source, *dest;
194 int w = l->area.width;
195 int h = l->area.height;
197 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
199 g_assert(l->surface.type == Surface_Planar);
201 oldp = l->pixmap; /* save to free after changing the visible pixmap */
202 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
203 g_assert(l->pixmap != None);
205 if (l->xftdraw != NULL)
206 XftDrawDestroy(l->xftdraw);
207 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
209 g_assert(l->xftdraw != NULL);
211 g_free(l->surface.data.planar.pixel_data);
212 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
215 if (l->surface.data.planar.grad == Background_ParentRelative) {
216 sw = l->surface.data.planar.parent->area.width;
217 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
218 + l->surface.data.planar.parentx
219 + sw * l->surface.data.planar.parenty;
220 dest = l->surface.data.planar.pixel_data;
221 for (i = 0; i < h; i++, source += sw, dest += w) {
222 memcpy(dest, source, w * sizeof(pixel32));
225 else if (l->surface.data.planar.grad == Background_Solid)
226 gradient_solid(l, x, y, w, h);
227 else gradient_render(&l->surface, w, h);
229 for (i = 0; i < l->textures; i++) {
230 switch (l->texture[i].type) {
234 if (l->surface.data.planar.grad != Background_Solid)
235 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
238 if (l->xftdraw == NULL) {
239 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
240 render_visual, render_colormap);
242 font_draw(l->xftdraw, &l->texture[i].data.text,
243 &l->texture[i].position);
248 if (l->surface.data.planar.grad != Background_Solid)
249 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
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,
255 &l->texture[i].position);
258 image_draw(l->surface.data.planar.pixel_data,
259 &l->texture[i].data.rgba,
260 &l->texture[i].position);
267 if (l->surface.data.planar.grad != Background_Solid)
268 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
273 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
274 XClearWindow(ob_display, win);
275 if (oldp != None) XFreePixmap(ob_display, oldp);
279 void gl_paint(Window win, Appearance *l)
281 glXMakeCurrent(ob_display, win, gl_context);
285 void render_shutdown(void)
289 Appearance *appearance_new(SurfaceType type, int numtex)
294 out = g_new(Appearance, 1);
295 out->surface.type = type;
296 out->textures = numtex;
298 if (numtex) out->texture = g_new0(Texture, numtex);
299 else out->texture = NULL;
304 p = &out->surface.data.planar;
307 p->border_color = NULL;
308 p->pixel_data = NULL;
314 Appearance *appearance_copy(Appearance *orig)
316 PlanarSurface *spo, *spc;
317 Appearance *copy = g_new(Appearance, 1);
318 copy->surface.type = orig->surface.type;
319 switch (orig->surface.type) {
321 spo = &(orig->surface.data.planar);
322 spc = &(copy->surface.data.planar);
323 spc->grad = spo->grad;
324 spc->relief = spo->relief;
325 spc->bevel = spo->bevel;
326 if (spo->primary != NULL)
327 spc->primary = color_new(spo->primary->r,
330 else spc->primary = NULL;
332 if (spo->secondary != NULL)
333 spc->secondary = color_new(spo->secondary->r,
336 else spc->secondary = NULL;
338 if (spo->border_color != NULL)
339 spc->border_color = color_new(spo->border_color->r,
340 spo->border_color->g,
341 spo->border_color->b);
342 else spc->border_color = NULL;
344 spc->interlaced = spo->interlaced;
345 spc->border = spo->border;
346 spc->pixel_data = NULL;
349 copy->textures = orig->textures;
350 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
352 copy->xftdraw = NULL;
356 void appearance_free(Appearance *a)
359 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
360 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
363 if (a->surface.type == Surface_Planar) {
364 p = &a->surface.data.planar;
365 if (p->primary != NULL) color_free(p->primary);
366 if (p->secondary != NULL) color_free(p->secondary);
367 if (p->border_color != NULL) color_free(p->border_color);
368 if (p->pixel_data != NULL) g_free(p->pixel_data);
374 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
377 im = XCreateImage(ob_display, render_visual, render_depth,
378 ZPixmap, 0, NULL, w, h, 32, 0);
379 g_assert(im != NULL);
380 im->byte_order = endian;
381 im->data = (char *)in;
382 reduce_depth((pixel32*)im->data, im);
383 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
384 im, 0, 0, x, y, w, h);