a1412741bbc6aa44a3dffbbc94f834321e4aa03d
[dana/openbox-history.git] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3 #include <glib.h>
4 #include "render.h"
5 #include "gradient.h"
6 #include "font.h"
7 #include "mask.h"
8 #include "color.h"
9 #include "image.h"
10 #include "kernel/openbox.h"
11
12 #ifdef HAVE_STDLIB_H
13 #  include <stdlib.h>
14 #endif
15
16 int render_depth;
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;
21
22 void render_startup(void)
23 {
24     paint = x_paint;
25
26     render_depth = DefaultDepth(ob_display, ob_screen);
27     render_visual = DefaultVisual(ob_display, ob_screen);
28     render_colormap = DefaultColormap(ob_display, ob_screen);
29
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 */
34       int vinfo_nitems;
35       int best = -1;
36
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);
42       if (vinfo_return) {
43         int i;
44         int max_depth = 1;
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;
50             best = i;
51           }
52         }
53         if (max_depth < render_depth) best = -1;
54       }
55       if (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,
59                                           AllocNone);
60       }
61       XFree(vinfo_return);
62     }  
63   switch (render_visual->class) {
64   case TrueColor:
65     truecolor_startup();
66     break;
67   case PseudoColor:
68   case StaticColor:
69   case GrayScale:
70   case StaticGray:
71     pseudocolor_startup();
72     break;
73   default:
74     g_critical("unsupported visual class.\n");
75     exit(EXIT_FAILURE);
76
77   }
78 }
79
80 void truecolor_startup(void)
81 {
82   unsigned long red_mask, green_mask, blue_mask;
83   XImage *timage = NULL;
84
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;
92
93   render_red_offset = 0;
94   render_green_offset = 0;
95   render_blue_offset = 0;
96
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; }
100
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--;  }
105   XFree(timage);
106 }
107
108 void pseudocolor_startup(void)
109 {
110   XColor icolors[256];
111   int tr, tg, tb, n, r, g, b, i, incolors, ii;
112   unsigned long dev;
113   int cpc, _ncolors;
114   g_message("Initializing PseudoColor RenderControl\n");
115
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);
120
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);
125   }
126
127   /* build a color cube */
128   pseudo_colors = malloc(_ncolors * sizeof(XColor));
129   cpc = 1 << pseudo_bpc; /* colors per channel */
130
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 
141                                                           allocation */
142       }
143
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 */
148
149   /* try allocate any colors that failed allocation above */
150
151   /* get the allocated values from the X server (only the first 256 XXX why!?)
152    */
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);
157
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);
169
170         if (dev < closest) {
171           closest = dev;
172           close = ii;
173         }
174       }
175
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;
180
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 */
184       else
185         g_assert(FALSE); /* wtf has gone wrong, its already alloced for
186                             chissake! */
187     }
188   }
189 }
190
191 void x_paint(Window win, Appearance *l)
192 {
193     int i, transferred = 0, sw;
194     pixel32 *source, *dest;
195     Pixmap oldp;
196     int x = l->area.x;
197     int y = l->area.y;
198     int w = l->area.width;
199     int h = l->area.height;
200     Rect tarea; /* area in which to draw textures */
201
202     if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
203
204     g_assert(l->surface.type == Surface_Planar);
205
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);
209
210     if (l->xftdraw != NULL)
211         XftDrawDestroy(l->xftdraw);
212     l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual, 
213                                render_colormap);
214     g_assert(l->xftdraw != NULL);
215
216     g_free(l->surface.data.planar.pixel_data);
217     l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
218
219
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));
228         }
229     }
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);
233
234     for (i = 0; i < l->textures; i++) {
235         tarea = l->texture[i].position;
236         if (l->surface.data.planar.grad != Background_ParentRelative) {
237             if (l->surface.data.planar.relief != Flat) {
238                 switch (l->surface.data.planar.bevel) {
239                 case Bevel1:
240                     tarea.x += 1; tarea.y += 1;
241                     tarea.width -= 2; tarea.height -= 2;
242                     break;
243                 case Bevel2:
244                     tarea.x += 2; tarea.y += 2;
245                     tarea.width -= 4; tarea.height -= 4;
246                     break;
247                 }
248             } else if (l->surface.data.planar.border) {
249                 tarea.x += 1; tarea.y += 1;
250                 tarea.width -= 2; tarea.height -= 2;
251             }
252         }
253
254         switch (l->texture[i].type) {
255         case Text:
256             if (!transferred) {
257                 transferred = 1;
258                 if (l->surface.data.planar.grad != Background_Solid)
259                     pixel32_to_pixmap(l->surface.data.planar.pixel_data, 
260                                       l->pixmap,x,y,w,h);
261             }
262             if (l->xftdraw == NULL) {
263                 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, 
264                                         render_visual, render_colormap);
265             }
266             font_draw(l->xftdraw, &l->texture[i].data.text, 
267                       &tarea);
268         break;
269         case Bitmask:
270             if (!transferred) {
271                 transferred = 1;
272                 if (l->surface.data.planar.grad != Background_Solid)
273                     pixel32_to_pixmap(l->surface.data.planar.pixel_data, 
274                                       l->pixmap,x,y,w,h);
275             }
276             if (l->texture[i].data.mask.color->gc == None)
277                 color_allocate_gc(l->texture[i].data.mask.color);
278             mask_draw(l->pixmap, &l->texture[i].data.mask,
279                       &tarea);
280         break;
281         case RGBA:
282             image_draw(l->surface.data.planar.pixel_data, 
283                        &l->texture[i].data.rgba,
284                        &tarea, &l->area);
285         break;
286         }
287     }
288
289     if (!transferred) {
290         transferred = 1;
291         if (l->surface.data.planar.grad != Background_Solid)
292             pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
293                               ,x,y,w,h);
294     }
295
296
297     XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
298     XClearWindow(ob_display, win);
299     if (oldp != None) XFreePixmap(ob_display, oldp);
300 }
301
302 /*
303 void gl_paint(Window win, Appearance *l)
304 {
305     glXMakeCurrent(ob_display, win, gl_context);
306 }
307 */
308
309 void render_shutdown(void)
310 {
311 }
312
313 Appearance *appearance_new(SurfaceType type, int numtex)
314 {
315   PlanarSurface *p;
316   Appearance *out;
317
318   out = g_new(Appearance, 1);
319   out->surface.type = type;
320   out->textures = numtex;
321   out->xftdraw = NULL;
322   if (numtex) out->texture = g_new0(Texture, numtex);
323   else out->texture = NULL;
324   out->pixmap = None;
325
326   switch (type) {
327   case Surface_Planar:
328     p = &out->surface.data.planar;
329     p->primary = NULL;
330     p->secondary = NULL;
331     p->border_color = NULL;
332     p->pixel_data = NULL;
333     break;
334   }
335   return out;
336 }
337
338 Appearance *appearance_copy(Appearance *orig)
339 {
340     PlanarSurface *spo, *spc;
341     Appearance *copy = g_new(Appearance, 1);
342     copy->surface.type = orig->surface.type;
343     switch (orig->surface.type) {
344     case Surface_Planar:
345         spo = &(orig->surface.data.planar);
346         spc = &(copy->surface.data.planar);
347         spc->grad = spo->grad;
348         spc->relief = spo->relief;
349         spc->bevel = spo->bevel;
350         if (spo->primary != NULL)
351             spc->primary = color_new(spo->primary->r,
352                                      spo->primary->g, 
353                                      spo->primary->b);
354         else spc->primary = NULL;
355
356         if (spo->secondary != NULL)
357             spc->secondary = color_new(spo->secondary->r,
358                                        spo->secondary->g,
359                                        spo->secondary->b);
360         else spc->secondary = NULL;
361
362         if (spo->border_color != NULL)
363             spc->border_color = color_new(spo->border_color->r,
364                                           spo->border_color->g,
365                                           spo->border_color->b);
366         else spc->border_color = NULL;
367
368         spc->interlaced = spo->interlaced;
369         spc->border = spo->border;
370         spc->pixel_data = NULL;
371     break;
372     }
373     copy->textures = orig->textures;
374     copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
375     copy->pixmap = None;
376     copy->xftdraw = NULL;
377     return copy;
378 }
379
380 void appearance_free(Appearance *a)
381 {
382     if (a) {
383         PlanarSurface *p;
384         if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
385         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
386         if (a->textures)
387             g_free(a->texture);
388         if (a->surface.type == Surface_Planar) {
389             p = &a->surface.data.planar;
390             if (p->primary != NULL) color_free(p->primary);
391             if (p->secondary != NULL) color_free(p->secondary);
392             if (p->border_color != NULL) color_free(p->border_color);
393             if (p->pixel_data != NULL) g_free(p->pixel_data);
394         }
395         g_free(a);
396     }
397 }
398
399
400 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
401 {
402     unsigned char *scratch;
403     XImage *im = NULL;
404     im = XCreateImage(ob_display, render_visual, render_depth,
405                       ZPixmap, 0, NULL, w, h, 32, 0);
406     g_assert(im != NULL);
407     im->byte_order = endian;
408 /* this malloc is a complete waste of time on normal 32bpp
409    as reduce_depth just sets im->data = data and returns
410 */
411     scratch = malloc(im->width * im->height * sizeof(pixel32));
412     im->data = (char*) scratch;
413     reduce_depth(in, im);
414     XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
415               im, 0, 0, x, y, w, h);
416     im->data = NULL;
417     XDestroyImage(im);
418     free(scratch);
419 }
420
421 void appearance_minsize(Appearance *l, int *w, int *h)
422 {
423     int i;
424     *w = *h = 1;
425
426     switch (l->surface.type) {
427     case Surface_Planar:
428         if (l->surface.data.planar.relief != Flat) {
429             switch (l->surface.data.planar.bevel) {
430             case Bevel1:
431                 *w = *h = 2;
432                 break;
433             case Bevel2:
434                 *w = *h = 4;
435                 break;
436             }
437         } else if (l->surface.data.planar.border)
438             *w = *h = 2;
439
440         for (i = 0; i < l->textures; ++i)
441             switch (l->texture[i].type) {
442             case Bitmask:
443                 *w += l->texture[i].data.mask.mask->w;
444                 *h += l->texture[i].data.mask.mask->h;
445                 break;
446             case Text:
447                 *w +=font_measure_string(l->texture[i].data.text.font,
448                                          l->texture[i].data.text.string,
449                                          l->texture[i].data.text.shadow,
450                                          l->texture[i].data.text.offset);
451                 *h +=  font_height(l->texture[i].data.text.font,
452                                    l->texture[i].data.text.shadow,
453                                    l->texture[i].data.text.offset);
454                 break;
455             case RGBA:
456                 *w += l->texture[i].data.rgba.width;
457                 *h += l->texture[i].data.rgba.height;
458                 break;
459             case NoTexture:
460                 break;
461             }            
462         break;
463     }
464 }