]> icculus.org git repositories - dana/openbox.git/blob - render/render.c
add shit that i made in the last week!
[dana/openbox.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 "theme.h"
11 #include "kernel/openbox.h"
12
13 #ifdef HAVE_STDLIB_H
14 #  include <stdlib.h>
15 #endif
16
17 int render_depth;
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;
22
23 void render_startup(void)
24 {
25     paint = x_paint;
26
27     render_depth = DefaultDepth(ob_display, ob_screen);
28     render_visual = DefaultVisual(ob_display, ob_screen);
29     render_colormap = DefaultColormap(ob_display, ob_screen);
30
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 */
35       int vinfo_nitems;
36       int best = -1;
37
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);
43       if (vinfo_return) {
44         int i;
45         int max_depth = 1;
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;
51             best = i;
52           }
53         }
54         if (max_depth < render_depth) best = -1;
55       }
56       if (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,
60                                           AllocNone);
61       }
62       XFree(vinfo_return);
63     }  
64   switch (render_visual->class) {
65   case TrueColor:
66     truecolor_startup();
67     break;
68   case PseudoColor:
69   case StaticColor:
70   case GrayScale:
71   case StaticGray:
72     pseudocolor_startup();
73     break;
74   default:
75     g_critical("unsupported visual class.\n");
76     exit(EXIT_FAILURE);
77
78   }
79 }
80
81 void truecolor_startup(void)
82 {
83   unsigned long red_mask, green_mask, blue_mask;
84   XImage *timage = NULL;
85
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;
93
94   render_red_offset = 0;
95   render_green_offset = 0;
96   render_blue_offset = 0;
97
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; }
101
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--;  }
106   XFree(timage);
107 }
108
109 void pseudocolor_startup(void)
110 {
111   XColor icolors[256];
112   int tr, tg, tb, n, r, g, b, i, incolors, ii;
113   unsigned long dev;
114   int cpc, _ncolors;
115   g_message("Initializing PseudoColor RenderControl\n");
116
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);
121
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);
126   }
127
128   /* build a color cube */
129   pseudo_colors = malloc(_ncolors * sizeof(XColor));
130   cpc = 1 << pseudo_bpc; /* colors per channel */
131
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 
142                                                           allocation */
143       }
144
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 */
149
150   /* try allocate any colors that failed allocation above */
151
152   /* get the allocated values from the X server (only the first 256 XXX why!?)
153    */
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);
158
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);
170
171         if (dev < closest) {
172           closest = dev;
173           close = ii;
174         }
175       }
176
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;
181
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 */
185       else
186         g_assert(FALSE); /* wtf has gone wrong, its already alloced for
187                             chissake! */
188     }
189   }
190 }
191
192 void x_paint(Window win, Appearance *l)
193 {
194     int i, transferred = 0, sw;
195     pixel32 *source, *dest;
196     Pixmap oldp;
197     int x = l->area.x;
198     int y = l->area.y;
199     int w = l->area.width;
200     int h = l->area.height;
201     Rect tarea; /* area in which to draw textures */
202
203     if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
204
205     g_assert(l->surface.type == Surface_Planar);
206
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);
210
211     if (l->xftdraw != NULL)
212         XftDrawDestroy(l->xftdraw);
213     l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual, 
214                                render_colormap);
215     g_assert(l->xftdraw != NULL);
216
217     g_free(l->surface.data.planar.pixel_data);
218     l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
219
220
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));
229         }
230     }
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);
234
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) {
240                 case Bevel1:
241                     tarea.x += 1; tarea.y += 1;
242                     tarea.width -= 2; tarea.height -= 2;
243                     break;
244                 case Bevel2:
245                     tarea.x += 2; tarea.y += 2;
246                     tarea.width -= 4; tarea.height -= 4;
247                     break;
248                 }
249             } else if (l->surface.data.planar.border) {
250                 tarea.x += 1; tarea.y += 1;
251                 tarea.width -= 2; tarea.height -= 2;
252             }
253         }
254
255         switch (l->texture[i].type) {
256         case Text:
257             if (!transferred) {
258                 transferred = 1;
259                 if (l->surface.data.planar.grad != Background_Solid)
260                     pixel32_to_pixmap(l->surface.data.planar.pixel_data, 
261                                       l->pixmap,x,y,w,h);
262             }
263             if (l->xftdraw == NULL) {
264                 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, 
265                                         render_visual, render_colormap);
266             }
267             font_draw(l->xftdraw, &l->texture[i].data.text, 
268                       &tarea);
269         break;
270         case Bitmask:
271             if (!transferred) {
272                 transferred = 1;
273                 if (l->surface.data.planar.grad != Background_Solid)
274                     pixel32_to_pixmap(l->surface.data.planar.pixel_data, 
275                                       l->pixmap,x,y,w,h);
276             }
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,
280                       &tarea);
281         break;
282         case RGBA:
283             image_draw(l->surface.data.planar.pixel_data, 
284                        &l->texture[i].data.rgba,
285                        &tarea, &l->area);
286         break;
287         }
288     }
289
290     if (!transferred) {
291         transferred = 1;
292         if (l->surface.data.planar.grad != Background_Solid)
293             pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
294                               ,x,y,w,h);
295     }
296
297
298     XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
299     XClearWindow(ob_display, win);
300     if (oldp != None) XFreePixmap(ob_display, oldp);
301 }
302
303 /*
304 void gl_paint(Window win, Appearance *l)
305 {
306     glXMakeCurrent(ob_display, win, gl_context);
307 }
308 */
309
310 void render_shutdown(void)
311 {
312 }
313
314 Appearance *appearance_new(SurfaceType type, int numtex)
315 {
316   PlanarSurface *p;
317   Appearance *out;
318
319   out = g_new(Appearance, 1);
320   out->surface.type = type;
321   out->textures = numtex;
322   out->xftdraw = NULL;
323   if (numtex) out->texture = g_new0(Texture, numtex);
324   else out->texture = NULL;
325   out->pixmap = None;
326
327   switch (type) {
328   case Surface_Planar:
329     p = &out->surface.data.planar;
330     p->primary = NULL;
331     p->secondary = NULL;
332     p->border_color = NULL;
333     p->pixel_data = NULL;
334     break;
335   }
336   return out;
337 }
338
339 Appearance *appearance_copy(Appearance *orig)
340 {
341     PlanarSurface *spo, *spc;
342     Appearance *copy = g_new(Appearance, 1);
343     copy->surface.type = orig->surface.type;
344     switch (orig->surface.type) {
345     case Surface_Planar:
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,
353                                      spo->primary->g, 
354                                      spo->primary->b);
355         else spc->primary = NULL;
356
357         if (spo->secondary != NULL)
358             spc->secondary = color_new(spo->secondary->r,
359                                        spo->secondary->g,
360                                        spo->secondary->b);
361         else spc->secondary = NULL;
362
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;
368
369         spc->interlaced = spo->interlaced;
370         spc->border = spo->border;
371         spc->pixel_data = NULL;
372     break;
373     }
374     copy->textures = orig->textures;
375     copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
376     copy->pixmap = None;
377     copy->xftdraw = NULL;
378     return copy;
379 }
380
381 void appearance_free(Appearance *a)
382 {
383     if (a) {
384         PlanarSurface *p;
385         if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
386         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
387         if (a->textures)
388             g_free(a->texture);
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);
395         }
396         g_free(a);
397     }
398 }
399
400
401 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
402 {
403     unsigned char *scratch;
404     XImage *im = NULL;
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
411 */
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);
417     im->data = NULL;
418     XDestroyImage(im);
419     free(scratch);
420 }
421
422 void appearance_minsize(Appearance *l, int *w, int *h)
423 {
424     int i;
425     int m;
426     *w = *h = 1;
427
428     switch (l->surface.type) {
429     case Surface_Planar:
430         if (l->surface.data.planar.relief != Flat) {
431             switch (l->surface.data.planar.bevel) {
432             case Bevel1:
433                 *w = *h = 2;
434                 break;
435             case Bevel2:
436                 *w = *h = 4;
437                 break;
438             }
439         } else if (l->surface.data.planar.border)
440             *w = *h = 2;
441
442         for (i = 0; i < l->textures; ++i) {
443             switch (l->texture[i].type) {
444             case Bitmask:
445                 *w += l->texture[i].data.mask.mask->w;
446                 *h += l->texture[i].data.mask.mask->h;
447                 break;
448             case Text:
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);
453                 *w += m;
454                 m = font_height(l->texture[i].data.text.font,
455                                 l->texture[i].data.text.shadow,
456                                 l->texture[i].data.text.offset);
457                 *h += m;
458                 break;
459             case RGBA:
460                 *w += l->texture[i].data.rgba.width;
461                 *h += l->texture[i].data.rgba.height;
462                 break;
463             case NoTexture:
464                 break;
465             }
466         }
467         break;
468     }
469 }