initialize texture to NULL if theres no textures. use g_memdup to copy it
[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 "../kernel/openbox.h"
9
10 int render_depth;
11 Visual *render_visual;
12 Colormap render_colormap;
13
14 void render_startup(void)
15 {
16     paint = x_paint;
17
18     render_depth = DefaultDepth(ob_display, ob_screen);
19     render_visual = DefaultVisual(ob_display, ob_screen);
20     render_colormap = DefaultColormap(ob_display, ob_screen);
21
22     if (render_depth < 8) {
23       XVisualInfo vinfo_template, *vinfo_return;
24       // search for a TrueColor Visual... if we can't find one...
25       // we will use the default visual for the screen
26       int vinfo_nitems;
27       int best = -1;
28
29       vinfo_template.screen = ob_screen;
30       vinfo_template.class = TrueColor;
31       vinfo_return = XGetVisualInfo(ob_display,
32                                     VisualScreenMask | VisualClassMask,
33                                     &vinfo_template, &vinfo_nitems);
34       if (vinfo_return) {
35         int i;
36         int max_depth = 1;
37         for (i = 0; i < vinfo_nitems; ++i) {
38           if (vinfo_return[i].depth > max_depth) {
39             if (max_depth == 24 && vinfo_return[i].depth > 24)
40               break;          // prefer 24 bit over 32
41             max_depth = vinfo_return[i].depth;
42             best = i;
43           }
44         }
45         if (max_depth < render_depth) best = -1;
46       }
47       if (best != -1) {
48         render_depth = vinfo_return[best].depth;
49         render_visual = vinfo_return[best].visual;
50         render_colormap = XCreateColormap(ob_display, ob_root, render_visual,
51                                           AllocNone);
52       }
53       XFree(vinfo_return);
54     }  
55 }
56
57 void x_paint(Window win, Appearance *l, int x, int y, int w, int h)
58 {
59     int i;
60     XImage *im;
61     Pixmap oldp;
62
63     if (w <= 0 || h <= 0) return;
64
65     g_assert(l->surface.type == Surface_Planar);
66 //    printf("painting window %ld\n", win);
67
68     oldp = l->pixmap; /* save to free after changing the visible pixmap */
69     l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
70     g_assert(l->pixmap != None);
71
72     if (l->xftdraw != NULL)
73         XftDrawDestroy(l->xftdraw);
74     l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual, 
75                                render_colormap);
76     g_assert(l->xftdraw != NULL);
77
78     if (l->surface.data.planar.pixel_data != NULL)
79         g_free(l->surface.data.planar.pixel_data);
80     l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
81
82     if (l->surface.data.planar.grad == Background_Solid)
83         gradient_solid(l, x, y, w, h);
84     else gradient_render(&l->surface, w, h);
85
86 /*reduce depth here...
87   also, this is not the right place for this code, it's only here so
88   text rendering shows up for now.
89 */
90     if (l->surface.data.planar.grad != Background_Solid) {
91         im = XCreateImage(ob_display, render_visual, render_depth,
92                           ZPixmap, 0, NULL, w, h, 32, 0);
93         g_assert(im != None);
94         im->byte_order = endian;
95         im->data = l->surface.data.planar.pixel_data;
96         XPutImage(ob_display, l->pixmap, DefaultGC(ob_display, ob_screen),
97                   im, 0, 0, x, y, w, h);
98         im->data = NULL;
99         XDestroyImage(im);
100     }
101
102     for (i = 0; i < l->textures; i++) {
103         switch (l->texture[i].type) {
104         case Text:
105             if (l->xftdraw == NULL) {
106                 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, 
107                                         render_visual, render_colormap);
108             }
109             font_draw(l->xftdraw, &l->texture[i].data.text, x, y, w, h);
110         break;
111         case Bitmask:
112             if (l->texture[i].data.mask.color->gc == None)
113                 color_allocate_gc(l->texture[i].data.mask.color);
114             mask_draw(l->pixmap, &l->texture[i].data.mask, w, h);
115         break;
116         }
117     }
118     XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
119     XClearWindow(ob_display, win);
120     if (oldp != None) XFreePixmap(ob_display, oldp);
121 }
122
123 /*
124 void gl_paint(Window win, Appearance *l)
125 {
126     glXMakeCurrent(ob_display, win, gl_context);
127 }
128 */
129
130 void render_shutdown(void)
131 {
132 }
133
134 Appearance *appearance_new(SurfaceType type, int numtex)
135 {
136   PlanarSurface *p;
137   Appearance *out;
138
139   out = g_new(Appearance, 1);
140   out->surface.type = type;
141   out->textures = numtex;
142   out->xftdraw = NULL;
143   if (numtex) out->texture = g_new(Texture, numtex);
144   else out->texture = NULL;
145   out->pixmap = None;
146
147   switch (type) {
148   case Surface_Planar:
149     p = &out->surface.data.planar;
150     p->primary = NULL;
151     p->secondary = NULL;
152     p->border_color = NULL;
153     p->pixel_data = NULL;
154     break;
155   }
156   return out;
157 }
158
159 Appearance *appearance_copy(Appearance *orig)
160 {
161     PlanarSurface *spo, *spc;
162     Appearance *copy = g_new(Appearance, 1);
163     copy->surface.type = orig->surface.type;
164     switch (orig->surface.type) {
165     case Surface_Planar:
166         spo = &(orig->surface.data.planar);
167         spc = &(copy->surface.data.planar);
168         spc->grad = spo->grad;
169         spc->relief = spo->relief;
170         spc->bevel = spo->bevel;
171         if (spo->primary != NULL)
172             spc->primary = color_new(spo->primary->r,
173                                      spo->primary->g, 
174                                      spo->primary->b);
175         else spc->primary = NULL;
176
177         if (spo->secondary != NULL)
178             spc->secondary = color_new(spo->secondary->r,
179                                        spo->secondary->g,
180                                        spo->secondary->b);
181         else spc->secondary = NULL;
182
183         if (spo->border_color != NULL)
184             spc->border_color = color_new(spo->border_color->r,
185                                           spo->border_color->g,
186                                           spo->border_color->b);
187         else spc->border_color = NULL;
188
189         spc->interlaced = spo->interlaced;
190         spc->border = spo->border;
191         spc->pixel_data = NULL;
192     break;
193     }
194     copy->textures = orig->textures;
195     copy->texture = g_memdup(orig->texture,
196                              orig->textures * sizeof(Texture));
197     copy->pixmap = None;
198     copy->xftdraw = NULL;
199     return copy;
200 }
201
202 void appearance_free(Appearance *a)
203 {
204     PlanarSurface *p;
205     if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
206     if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
207     if (a->textures)
208         g_free(a->texture);
209     if (a->surface.type == Surface_Planar) {
210         p = &a->surface.data.planar;
211         if (p->primary != NULL) color_free(p->primary);
212         if (p->secondary != NULL) color_free(p->secondary);
213         if (p->border_color != NULL) color_free(p->border_color);
214     }
215     g_free(a);
216 }