merge the C branch into HEAD
[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 "../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         printf("I AM DOING SOMETHING NOW\n");
84     }
85 //reduce depth
86     if (l->surface.data.planar.grad != Background_Solid) {
87         im = XCreateImage(ob_display, render_visual, render_depth,
88                           ZPixmap, 0, NULL, w, h, 32, 0);
89         g_assert(im != None);
90         im->byte_order = endian;
91         im->data = l->surface.data.planar.pixel_data;
92         XPutImage(ob_display, l->pixmap, DefaultGC(ob_display, ob_screen),
93                   im, 0, 0, 0, 0, w, h);
94         im->data = NULL;
95         XDestroyImage(im);
96     }
97     XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
98     XClearWindow(ob_display, win);
99 }
100
101 /*
102 void gl_paint(Window win, Appearance *l)
103 {
104     glXMakeCurrent(ob_display, win, gl_context);
105 }
106 */
107
108 void render_shutdown(void)
109 {
110 }
111
112 Appearance *appearance_new(SurfaceType type, int numtex)
113 {
114   PlanarSurface *p;
115   Appearance *out;
116
117   out = g_new(Appearance, 1);
118   out->surface.type = type;
119   out->textures = numtex;
120   out->xftdraw = NULL;
121   if (numtex) out->texture = g_new(Texture, numtex);
122   out->pixmap = None;
123
124   switch (type) {
125   case Surface_Planar:
126     p = &out->surface.data.planar;
127     p->primary = NULL;
128     p->secondary = NULL;
129     p->border_color = NULL;
130     p->pixel_data = NULL;
131     break;
132   }
133   return out;
134 }
135
136 Appearance *appearance_copy(Appearance *orig)
137 {
138     PlanarSurface *spo, *spc;
139     Appearance *copy = g_new(Appearance, 1);
140     copy->surface.type = orig->surface.type;
141     switch (orig->surface.type) {
142     case Surface_Planar:
143         spo = &(orig->surface.data.planar);
144         spc = &(copy->surface.data.planar);
145         spc->grad = spo->grad;
146         spc->relief = spo->relief;
147         spc->bevel = spo->bevel;
148         if (spo->primary != NULL)
149             spc->primary = color_new(spo->primary->r,
150                                      spo->primary->g, 
151                                      spo->primary->b);
152         else spc->primary = NULL;
153
154         if (spo->secondary != NULL)
155             spc->secondary = color_new(spo->secondary->r,
156                                        spo->secondary->g,
157                                        spo->secondary->b);
158         else spc->secondary = NULL;
159
160         if (spo->border_color != NULL)
161             spc->border_color = color_new(spo->border_color->r,
162                                           spo->border_color->g,
163                                           spo->border_color->b);
164         else spc->border_color = NULL;
165
166         spc->interlaced = spo->interlaced;
167         spc->border = spo->border;
168         spc->pixel_data = NULL;
169     break;
170     }
171     copy->textures = orig->textures;
172     copy->texture = NULL; /* XXX FIX ME */
173     copy->pixmap = None;
174     copy->xftdraw = NULL;
175     return copy;
176 }
177
178 void appearance_free(Appearance *a)
179 {
180     PlanarSurface *p;
181     if (a->textures)
182         g_free(a->texture);
183     if (a->surface.type == Surface_Planar) {
184         p = &a->surface.data.planar;
185         if (p->primary != NULL) color_free(p->primary);
186         if (p->secondary != NULL) color_free(p->secondary);
187         if (p->border_color != NULL) color_free(p->border_color);
188     }
189     g_free(a);
190 }