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