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