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