]> icculus.org git repositories - mikachu/openbox.git/blob - render/render.c
will seg again
[mikachu/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 "../kernel/openbox.h"
11
12 int render_depth;
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;
17
18 void render_startup(void)
19 {
20     paint = x_paint;
21
22     render_depth = DefaultDepth(ob_display, ob_screen);
23     render_visual = DefaultVisual(ob_display, ob_screen);
24     render_colormap = DefaultColormap(ob_display, ob_screen);
25
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 */
30       int vinfo_nitems;
31       int best = -1;
32
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);
38       if (vinfo_return) {
39         int i;
40         int max_depth = 1;
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;
46             best = i;
47           }
48         }
49         if (max_depth < render_depth) best = -1;
50       }
51       if (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,
55                                           AllocNone);
56       }
57       XFree(vinfo_return);
58     }  
59   truecolor_startup();
60 }
61
62 void truecolor_startup(void)
63 {
64   unsigned long red_mask, green_mask, blue_mask;
65   XImage *timage = NULL;
66
67   timage = XCreateImage(ob_display, render_visual, render_depth,
68                         ZPixmap, 0, NULL, 1, 1, 32, 0);
69   g_assert(timage != NULL);
70   /* find the offsets for each color in the visual's masks */
71   red_mask = timage->red_mask;
72   green_mask = timage->green_mask;
73   blue_mask = timage->blue_mask;
74
75   render_red_offset = 0;
76   render_green_offset = 0;
77   render_blue_offset = 0;
78
79   while (! (red_mask & 1))   { render_red_offset++;   red_mask   >>= 1; }
80   while (! (green_mask & 1)) { render_green_offset++; green_mask >>= 1; }
81   while (! (blue_mask & 1))  { render_blue_offset++;  blue_mask  >>= 1; }
82
83   render_red_shift = render_green_shift = render_blue_shift = 8;
84   while (red_mask)   { red_mask   >>= 1; render_red_shift--;   }
85   while (green_mask) { green_mask >>= 1; render_green_shift--; }
86   while (blue_mask)  { blue_mask  >>= 1; render_blue_shift--;  }
87   XFree(timage);
88 }
89
90 void x_paint(Window win, Appearance *l)
91 {
92     int i, transferred = 0, sw;
93     pixel32 *source, *dest;
94     Pixmap oldp;
95     int x = l->area.x;
96     int y = l->area.y;
97     int w = l->area.width;
98     int h = l->area.height;
99
100     if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
101
102     g_assert(l->surface.type == Surface_Planar);
103
104     oldp = l->pixmap; /* save to free after changing the visible pixmap */
105     l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
106     g_assert(l->pixmap != None);
107
108     if (l->xftdraw != NULL)
109         XftDrawDestroy(l->xftdraw);
110     l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual, 
111                                render_colormap);
112     g_assert(l->xftdraw != NULL);
113
114     g_free(l->surface.data.planar.pixel_data);
115     l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
116
117
118     if (l->surface.data.planar.grad == Background_ParentRelative) {
119         sw = l->surface.data.planar.parent->area.width;
120         source = l->surface.data.planar.parent->surface.data.planar.pixel_data
121             + l->surface.data.planar.parentx
122             + sw * l->surface.data.planar.parenty;
123         dest = l->surface.data.planar.pixel_data;
124         for (i = 0; i < h; i++, source += sw, dest += w) {
125             memcpy(dest, source, w * sizeof(pixel32));
126         }
127     }
128     else if (l->surface.data.planar.grad == Background_Solid)
129         gradient_solid(l, x, y, w, h);
130     else gradient_render(&l->surface, w, h);
131
132     for (i = 0; i < l->textures; i++) {
133         switch (l->texture[i].type) {
134         case Text:
135             if (!transferred) {
136                 transferred = 1;
137                 if (l->surface.data.planar.grad != Background_Solid)
138                     pixel32_to_pixmap(l->surface.data.planar.pixel_data, 
139                                       l->pixmap,x,y,w,h);
140             }
141             if (l->xftdraw == NULL) {
142                 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, 
143                                         render_visual, render_colormap);
144             }
145             font_draw(l->xftdraw, &l->texture[i].data.text, 
146                       &l->texture[i].position);
147         break;
148         case Bitmask:
149             if (!transferred) {
150                 transferred = 1;
151                 if (l->surface.data.planar.grad != Background_Solid)
152                     pixel32_to_pixmap(l->surface.data.planar.pixel_data, 
153                                       l->pixmap,x,y,w,h);
154             }
155             if (l->texture[i].data.mask.color->gc == None)
156                 color_allocate_gc(l->texture[i].data.mask.color);
157             mask_draw(l->pixmap, &l->texture[i].data.mask,
158                       &l->texture[i].position);
159         break;
160         case RGBA:
161             image_draw(l->surface.data.planar.pixel_data, 
162                        &l->texture[i].data.rgba,
163                        &l->texture[i].position);
164         break;
165         }
166     }
167
168     if (!transferred) {
169         transferred = 1;
170         if (l->surface.data.planar.grad != Background_Solid)
171             pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
172                               ,x,y,w,h);
173     }
174
175
176     XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
177     XClearWindow(ob_display, win);
178     if (oldp != None) XFreePixmap(ob_display, oldp);
179 }
180
181 /*
182 void gl_paint(Window win, Appearance *l)
183 {
184     glXMakeCurrent(ob_display, win, gl_context);
185 }
186 */
187
188 void render_shutdown(void)
189 {
190 }
191
192 Appearance *appearance_new(SurfaceType type, int numtex)
193 {
194   PlanarSurface *p;
195   Appearance *out;
196
197   out = g_new(Appearance, 1);
198   out->surface.type = type;
199   out->textures = numtex;
200   out->xftdraw = NULL;
201   if (numtex) out->texture = g_new0(Texture, numtex);
202   else out->texture = NULL;
203   out->pixmap = None;
204
205   switch (type) {
206   case Surface_Planar:
207     p = &out->surface.data.planar;
208     p->primary = NULL;
209     p->secondary = NULL;
210     p->border_color = NULL;
211     p->pixel_data = NULL;
212     break;
213   }
214   return out;
215 }
216
217 Appearance *appearance_copy(Appearance *orig)
218 {
219     PlanarSurface *spo, *spc;
220     Appearance *copy = g_new(Appearance, 1);
221     copy->surface.type = orig->surface.type;
222     switch (orig->surface.type) {
223     case Surface_Planar:
224         spo = &(orig->surface.data.planar);
225         spc = &(copy->surface.data.planar);
226         spc->grad = spo->grad;
227         spc->relief = spo->relief;
228         spc->bevel = spo->bevel;
229         if (spo->primary != NULL)
230             spc->primary = color_new(spo->primary->r,
231                                      spo->primary->g, 
232                                      spo->primary->b);
233         else spc->primary = NULL;
234
235         if (spo->secondary != NULL)
236             spc->secondary = color_new(spo->secondary->r,
237                                        spo->secondary->g,
238                                        spo->secondary->b);
239         else spc->secondary = NULL;
240
241         if (spo->border_color != NULL)
242             spc->border_color = color_new(spo->border_color->r,
243                                           spo->border_color->g,
244                                           spo->border_color->b);
245         else spc->border_color = NULL;
246
247         spc->interlaced = spo->interlaced;
248         spc->border = spo->border;
249         spc->pixel_data = NULL;
250     break;
251     }
252     copy->textures = orig->textures;
253     copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
254     copy->pixmap = None;
255     copy->xftdraw = NULL;
256     return copy;
257 }
258
259 void appearance_free(Appearance *a)
260 {
261     PlanarSurface *p;
262     if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
263     if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
264     if (a->textures)
265         g_free(a->texture);
266     if (a->surface.type == Surface_Planar) {
267         p = &a->surface.data.planar;
268         if (p->primary != NULL) color_free(p->primary);
269         if (p->secondary != NULL) color_free(p->secondary);
270         if (p->border_color != NULL) color_free(p->border_color);
271         if (p->pixel_data != NULL) g_free(p->pixel_data);
272     }
273     g_free(a);
274 }
275
276
277 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
278 {
279     XImage *im = NULL;
280     im = XCreateImage(ob_display, render_visual, render_depth,
281                       ZPixmap, 0, NULL, w, h, 32, 0);
282     g_assert(im != NULL);
283     im->byte_order = endian;
284     im->data = (char *)in;
285     reduce_depth((pixel32*)im->data, im);
286     XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
287               im, 0, 0, x, y, w, h);
288     im->data = NULL;
289     XDestroyImage(im);
290 }