add missing include, pass right args to font_draw
[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
61     if (w <= 0 || h <= 0) return;
62
63     g_assert(l->surface.type == Surface_Planar);
64 //    printf("painting window %ld\n", win);
65
66     if (l->pixmap != None) XFreePixmap(ob_display, l->pixmap);
67     l->pixmap = XCreatePixmap(ob_display, ob_root, w, h, render_depth);
68     g_assert(l->pixmap != None);
69
70     if (l->xftdraw != NULL)
71         XftDrawDestroy(l->xftdraw);
72     l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual, 
73                                render_colormap);
74     g_assert(l->xftdraw != NULL);
75
76     if (l->surface.data.planar.pixel_data != NULL)
77         g_free(l->surface.data.planar.pixel_data);
78     l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
79
80     if (l->surface.data.planar.grad == Background_Solid)
81         gradient_solid(l, w, h);
82     else gradient_render(&l->surface, w, h);
83     for (i = 0; i < l->textures; i++) {
84         switch (l->texture[i].type) {
85         case Text:
86             if (l->xftdraw == NULL) {
87                 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, 
88                                         render_visual, render_colormap);
89             }
90             font_draw(l->xftdraw, &l->texture[i].data.text);
91         break;
92         }
93     }
94 //reduce depth
95     if (l->surface.data.planar.grad != Background_Solid) {
96         im = XCreateImage(ob_display, render_visual, render_depth,
97                           ZPixmap, 0, NULL, w, h, 32, 0);
98         g_assert(im != None);
99         im->byte_order = endian;
100         im->data = l->surface.data.planar.pixel_data;
101         XPutImage(ob_display, l->pixmap, DefaultGC(ob_display, ob_screen),
102                   im, 0, 0, 0, 0, w, h);
103         im->data = NULL;
104         XDestroyImage(im);
105     }
106     XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
107     XClearWindow(ob_display, win);
108 }
109
110 /*
111 void gl_paint(Window win, Appearance *l)
112 {
113     glXMakeCurrent(ob_display, win, gl_context);
114 }
115 */
116
117 void render_shutdown(void)
118 {
119 }
120
121 Appearance *appearance_new(SurfaceType type, int numtex)
122 {
123   PlanarSurface *p;
124   Appearance *out;
125
126   out = g_new(Appearance, 1);
127   out->surface.type = type;
128   out->textures = numtex;
129   out->xftdraw = NULL;
130   if (numtex) out->texture = g_new(Texture, numtex);
131   out->pixmap = None;
132
133   switch (type) {
134   case Surface_Planar:
135     p = &out->surface.data.planar;
136     p->primary = NULL;
137     p->secondary = NULL;
138     p->border_color = NULL;
139     p->pixel_data = NULL;
140     break;
141   }
142   return out;
143 }
144
145 Appearance *appearance_copy(Appearance *orig)
146 {
147     PlanarSurface *spo, *spc;
148     Appearance *copy = g_new(Appearance, 1);
149     copy->surface.type = orig->surface.type;
150     switch (orig->surface.type) {
151     case Surface_Planar:
152         spo = &(orig->surface.data.planar);
153         spc = &(copy->surface.data.planar);
154         spc->grad = spo->grad;
155         spc->relief = spo->relief;
156         spc->bevel = spo->bevel;
157         if (spo->primary != NULL)
158             spc->primary = color_new(spo->primary->r,
159                                      spo->primary->g, 
160                                      spo->primary->b);
161         else spc->primary = NULL;
162
163         if (spo->secondary != NULL)
164             spc->secondary = color_new(spo->secondary->r,
165                                        spo->secondary->g,
166                                        spo->secondary->b);
167         else spc->secondary = NULL;
168
169         if (spo->border_color != NULL)
170             spc->border_color = color_new(spo->border_color->r,
171                                           spo->border_color->g,
172                                           spo->border_color->b);
173         else spc->border_color = NULL;
174
175         spc->interlaced = spo->interlaced;
176         spc->border = spo->border;
177         spc->pixel_data = NULL;
178     break;
179     }
180     copy->textures = orig->textures;
181     if (orig->textures) {
182         copy->texture = malloc(orig->textures * sizeof(Texture));
183         memcpy(copy->texture, orig->texture, orig->textures * sizeof(Texture));
184     } else copy->texture = NULL;
185     copy->pixmap = None;
186     copy->xftdraw = NULL;
187     return copy;
188 }
189
190 void appearance_free(Appearance *a)
191 {
192     PlanarSurface *p;
193     if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
194     if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
195     if (a->textures)
196         g_free(a->texture);
197     if (a->surface.type == Surface_Planar) {
198         p = &a->surface.data.planar;
199         if (p->primary != NULL) color_free(p->primary);
200         if (p->secondary != NULL) color_free(p->secondary);
201         if (p->border_color != NULL) color_free(p->border_color);
202     }
203     g_free(a);
204 }