]> icculus.org git repositories - mikachu/openbox.git/blob - render/render.c
solids should now work with offsets
[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 "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);
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   out->pixmap = None;
145
146   switch (type) {
147   case Surface_Planar:
148     p = &out->surface.data.planar;
149     p->primary = NULL;
150     p->secondary = NULL;
151     p->border_color = NULL;
152     p->pixel_data = NULL;
153     break;
154   }
155   return out;
156 }
157
158 Appearance *appearance_copy(Appearance *orig)
159 {
160     PlanarSurface *spo, *spc;
161     Appearance *copy = g_new(Appearance, 1);
162     copy->surface.type = orig->surface.type;
163     switch (orig->surface.type) {
164     case Surface_Planar:
165         spo = &(orig->surface.data.planar);
166         spc = &(copy->surface.data.planar);
167         spc->grad = spo->grad;
168         spc->relief = spo->relief;
169         spc->bevel = spo->bevel;
170         if (spo->primary != NULL)
171             spc->primary = color_new(spo->primary->r,
172                                      spo->primary->g, 
173                                      spo->primary->b);
174         else spc->primary = NULL;
175
176         if (spo->secondary != NULL)
177             spc->secondary = color_new(spo->secondary->r,
178                                        spo->secondary->g,
179                                        spo->secondary->b);
180         else spc->secondary = NULL;
181
182         if (spo->border_color != NULL)
183             spc->border_color = color_new(spo->border_color->r,
184                                           spo->border_color->g,
185                                           spo->border_color->b);
186         else spc->border_color = NULL;
187
188         spc->interlaced = spo->interlaced;
189         spc->border = spo->border;
190         spc->pixel_data = NULL;
191     break;
192     }
193     copy->textures = orig->textures;
194     if (orig->textures) {
195         copy->texture = malloc(orig->textures * sizeof(Texture));
196         memcpy(copy->texture, orig->texture, orig->textures * sizeof(Texture));
197     } else copy->texture = NULL;
198     copy->pixmap = None;
199     copy->xftdraw = NULL;
200     return copy;
201 }
202
203 void appearance_free(Appearance *a)
204 {
205     PlanarSurface *p;
206     if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
207     if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
208     if (a->textures)
209         g_free(a->texture);
210     if (a->surface.type == Surface_Planar) {
211         p = &a->surface.data.planar;
212         if (p->primary != NULL) color_free(p->primary);
213         if (p->secondary != NULL) color_free(p->secondary);
214         if (p->border_color != NULL) color_free(p->border_color);
215     }
216     g_free(a);
217 }