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