]> Dogcows Code - chaz/openbox/blob - render/render.c
add the group.c
[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 #ifdef HAVE_STDLIB_H
13 # include <stdlib.h>
14 #endif
15
16 int render_depth;
17 Visual *render_visual;
18 Colormap render_colormap;
19 int render_red_offset = 0, render_green_offset = 0, render_blue_offset = 0;
20 int render_red_shift, render_green_shift, render_blue_shift;
21
22 void render_startup(void)
23 {
24 paint = x_paint;
25
26 render_depth = DefaultDepth(ob_display, ob_screen);
27 render_visual = DefaultVisual(ob_display, ob_screen);
28 render_colormap = DefaultColormap(ob_display, ob_screen);
29
30 if (render_depth < 8) {
31 XVisualInfo vinfo_template, *vinfo_return;
32 /* search for a TrueColor Visual... if we can't find one...
33 we will use the default visual for the screen */
34 int vinfo_nitems;
35 int best = -1;
36
37 vinfo_template.screen = ob_screen;
38 vinfo_template.class = TrueColor;
39 vinfo_return = XGetVisualInfo(ob_display,
40 VisualScreenMask | VisualClassMask,
41 &vinfo_template, &vinfo_nitems);
42 if (vinfo_return) {
43 int i;
44 int max_depth = 1;
45 for (i = 0; i < vinfo_nitems; ++i) {
46 if (vinfo_return[i].depth > max_depth) {
47 if (max_depth == 24 && vinfo_return[i].depth > 24)
48 break; /* prefer 24 bit over 32 */
49 max_depth = vinfo_return[i].depth;
50 best = i;
51 }
52 }
53 if (max_depth < render_depth) best = -1;
54 }
55 if (best != -1) {
56 render_depth = vinfo_return[best].depth;
57 render_visual = vinfo_return[best].visual;
58 render_colormap = XCreateColormap(ob_display, ob_root, render_visual,
59 AllocNone);
60 }
61 XFree(vinfo_return);
62 }
63 switch (render_visual->class) {
64 case TrueColor:
65 truecolor_startup();
66 break;
67 case PseudoColor:
68 case StaticColor:
69 case GrayScale:
70 case StaticGray:
71 pseudocolor_startup();
72 break;
73 default:
74 g_critical("unsupported visual class.\n");
75 exit(EXIT_FAILURE);
76
77 }
78 }
79
80 void truecolor_startup(void)
81 {
82 unsigned long red_mask, green_mask, blue_mask;
83 XImage *timage = NULL;
84
85 timage = XCreateImage(ob_display, render_visual, render_depth,
86 ZPixmap, 0, NULL, 1, 1, 32, 0);
87 g_assert(timage != NULL);
88 /* find the offsets for each color in the visual's masks */
89 red_mask = timage->red_mask;
90 green_mask = timage->green_mask;
91 blue_mask = timage->blue_mask;
92
93 render_red_offset = 0;
94 render_green_offset = 0;
95 render_blue_offset = 0;
96
97 while (! (red_mask & 1)) { render_red_offset++; red_mask >>= 1; }
98 while (! (green_mask & 1)) { render_green_offset++; green_mask >>= 1; }
99 while (! (blue_mask & 1)) { render_blue_offset++; blue_mask >>= 1; }
100
101 render_red_shift = render_green_shift = render_blue_shift = 8;
102 while (red_mask) { red_mask >>= 1; render_red_shift--; }
103 while (green_mask) { green_mask >>= 1; render_green_shift--; }
104 while (blue_mask) { blue_mask >>= 1; render_blue_shift--; }
105 XFree(timage);
106 }
107
108 void pseudocolor_startup(void)
109 {
110 XColor icolors[256];
111 int tr, tg, tb, n, r, g, b, i, incolors, ii;
112 unsigned long dev;
113 int cpc, _ncolors;
114 g_message("Initializing PseudoColor RenderControl\n");
115
116 /* determine the number of colors and the bits-per-color */
117 pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
118 g_assert(pseudo_bpc >= 1);
119 _ncolors = 1 << (pseudo_bpc * 3);
120
121 if (_ncolors > 1 << render_depth) {
122 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
123 pseudo_bpc = 1 << (render_depth/3) >> 3;
124 _ncolors = 1 << (pseudo_bpc * 3);
125 }
126
127 /* build a color cube */
128 pseudo_colors = malloc(_ncolors * sizeof(XColor));
129 cpc = 1 << pseudo_bpc; /* colors per channel */
130
131 for (n = 0, r = 0; r < cpc; r++)
132 for (g = 0; g < cpc; g++)
133 for (b = 0; b < cpc; b++, n++) {
134 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
135 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
136 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
137 pseudo_colors[n].red = tr | tr << 8;
138 pseudo_colors[n].green = tg | tg << 8;
139 pseudo_colors[n].blue = tb | tb << 8;
140 pseudo_colors[n].flags = DoRed|DoGreen|DoBlue; /* used to track
141 allocation */
142 }
143
144 /* allocate the colors */
145 for (i = 0; i < _ncolors; i++)
146 if (!XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
147 pseudo_colors[i].flags = 0; /* mark it as unallocated */
148
149 /* try allocate any colors that failed allocation above */
150
151 /* get the allocated values from the X server (only the first 256 XXX why!?)
152 */
153 incolors = (((1 << render_depth) > 256) ? 256 : (1 << render_depth));
154 for (i = 0; i < incolors; i++)
155 icolors[i].pixel = i;
156 XQueryColors(ob_display, render_colormap, icolors, incolors);
157
158 /* try match unallocated ones */
159 for (i = 0; i < _ncolors; i++) {
160 if (!pseudo_colors[i].flags) { /* if it wasn't allocated... */
161 unsigned long closest = 0xffffffff, close = 0;
162 for (ii = 0; ii < incolors; ii++) {
163 /* find deviations */
164 r = (pseudo_colors[i].red - icolors[ii].red) & 0xff;
165 g = (pseudo_colors[i].green - icolors[ii].green) & 0xff;
166 b = (pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
167 /* find a weighted absolute deviation */
168 dev = (r * r) + (g * g) + (b * b);
169
170 if (dev < closest) {
171 closest = dev;
172 close = ii;
173 }
174 }
175
176 pseudo_colors[i].red = icolors[close].red;
177 pseudo_colors[i].green = icolors[close].green;
178 pseudo_colors[i].blue = icolors[close].blue;
179 pseudo_colors[i].pixel = icolors[close].pixel;
180
181 /* try alloc this closest color, it had better succeed! */
182 if (XAllocColor(ob_display, render_colormap, &pseudo_colors[i]))
183 pseudo_colors[i].flags = DoRed|DoGreen|DoBlue; /* mark as alloced */
184 else
185 g_assert(FALSE); /* wtf has gone wrong, its already alloced for
186 chissake! */
187 }
188 }
189 }
190
191 void x_paint(Window win, Appearance *l)
192 {
193 int i, transferred = 0, sw;
194 pixel32 *source, *dest;
195 Pixmap oldp;
196 int x = l->area.x;
197 int y = l->area.y;
198 int w = l->area.width;
199 int h = l->area.height;
200
201 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
202
203 g_assert(l->surface.type == Surface_Planar);
204
205 oldp = l->pixmap; /* save to free after changing the visible pixmap */
206 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
207 g_assert(l->pixmap != None);
208
209 if (l->xftdraw != NULL)
210 XftDrawDestroy(l->xftdraw);
211 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
212 render_colormap);
213 g_assert(l->xftdraw != NULL);
214
215 g_free(l->surface.data.planar.pixel_data);
216 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
217
218
219 if (l->surface.data.planar.grad == Background_ParentRelative) {
220 sw = l->surface.data.planar.parent->area.width;
221 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
222 + l->surface.data.planar.parentx
223 + sw * l->surface.data.planar.parenty;
224 dest = l->surface.data.planar.pixel_data;
225 for (i = 0; i < h; i++, source += sw, dest += w) {
226 memcpy(dest, source, w * sizeof(pixel32));
227 }
228 }
229 else if (l->surface.data.planar.grad == Background_Solid)
230 gradient_solid(l, x, y, w, h);
231 else gradient_render(&l->surface, w, h);
232
233 for (i = 0; i < l->textures; i++) {
234 switch (l->texture[i].type) {
235 case Text:
236 if (!transferred) {
237 transferred = 1;
238 if (l->surface.data.planar.grad != Background_Solid)
239 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
240 l->pixmap,x,y,w,h);
241 }
242 if (l->xftdraw == NULL) {
243 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
244 render_visual, render_colormap);
245 }
246 font_draw(l->xftdraw, &l->texture[i].data.text,
247 &l->texture[i].position);
248 break;
249 case Bitmask:
250 if (!transferred) {
251 transferred = 1;
252 if (l->surface.data.planar.grad != Background_Solid)
253 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
254 l->pixmap,x,y,w,h);
255 }
256 if (l->texture[i].data.mask.color->gc == None)
257 color_allocate_gc(l->texture[i].data.mask.color);
258 mask_draw(l->pixmap, &l->texture[i].data.mask,
259 &l->texture[i].position);
260 break;
261 case RGBA:
262 image_draw(l->surface.data.planar.pixel_data,
263 &l->texture[i].data.rgba,
264 &l->texture[i].position);
265 break;
266 }
267 }
268
269 if (!transferred) {
270 transferred = 1;
271 if (l->surface.data.planar.grad != Background_Solid)
272 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
273 ,x,y,w,h);
274 }
275
276
277 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
278 XClearWindow(ob_display, win);
279 if (oldp != None) XFreePixmap(ob_display, oldp);
280 }
281
282 /*
283 void gl_paint(Window win, Appearance *l)
284 {
285 glXMakeCurrent(ob_display, win, gl_context);
286 }
287 */
288
289 void render_shutdown(void)
290 {
291 }
292
293 Appearance *appearance_new(SurfaceType type, int numtex)
294 {
295 PlanarSurface *p;
296 Appearance *out;
297
298 out = g_new(Appearance, 1);
299 out->surface.type = type;
300 out->textures = numtex;
301 out->xftdraw = NULL;
302 if (numtex) out->texture = g_new0(Texture, numtex);
303 else out->texture = NULL;
304 out->pixmap = None;
305
306 switch (type) {
307 case Surface_Planar:
308 p = &out->surface.data.planar;
309 p->primary = NULL;
310 p->secondary = NULL;
311 p->border_color = NULL;
312 p->pixel_data = NULL;
313 break;
314 }
315 return out;
316 }
317
318 Appearance *appearance_copy(Appearance *orig)
319 {
320 PlanarSurface *spo, *spc;
321 Appearance *copy = g_new(Appearance, 1);
322 copy->surface.type = orig->surface.type;
323 switch (orig->surface.type) {
324 case Surface_Planar:
325 spo = &(orig->surface.data.planar);
326 spc = &(copy->surface.data.planar);
327 spc->grad = spo->grad;
328 spc->relief = spo->relief;
329 spc->bevel = spo->bevel;
330 if (spo->primary != NULL)
331 spc->primary = color_new(spo->primary->r,
332 spo->primary->g,
333 spo->primary->b);
334 else spc->primary = NULL;
335
336 if (spo->secondary != NULL)
337 spc->secondary = color_new(spo->secondary->r,
338 spo->secondary->g,
339 spo->secondary->b);
340 else spc->secondary = NULL;
341
342 if (spo->border_color != NULL)
343 spc->border_color = color_new(spo->border_color->r,
344 spo->border_color->g,
345 spo->border_color->b);
346 else spc->border_color = NULL;
347
348 spc->interlaced = spo->interlaced;
349 spc->border = spo->border;
350 spc->pixel_data = NULL;
351 break;
352 }
353 copy->textures = orig->textures;
354 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
355 copy->pixmap = None;
356 copy->xftdraw = NULL;
357 return copy;
358 }
359
360 void appearance_free(Appearance *a)
361 {
362 PlanarSurface *p;
363 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
364 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
365 if (a->textures)
366 g_free(a->texture);
367 if (a->surface.type == Surface_Planar) {
368 p = &a->surface.data.planar;
369 if (p->primary != NULL) color_free(p->primary);
370 if (p->secondary != NULL) color_free(p->secondary);
371 if (p->border_color != NULL) color_free(p->border_color);
372 if (p->pixel_data != NULL) g_free(p->pixel_data);
373 }
374 g_free(a);
375 }
376
377
378 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
379 {
380 unsigned char *scratch;
381 XImage *im = NULL;
382 im = XCreateImage(ob_display, render_visual, render_depth,
383 ZPixmap, 0, NULL, w, h, 32, 0);
384 g_assert(im != NULL);
385 im->byte_order = endian;
386 /* this malloc is a complete waste of time on normal 32bpp
387 as reduce_depth just sets im->data = data and returns
388 */
389 scratch = malloc(im->width * im->height * sizeof(pixel32));
390 im->data = scratch;
391 reduce_depth(in, im);
392 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
393 im, 0, 0, x, y, w, h);
394 im->data = NULL;
395 XDestroyImage(im);
396 free(scratch);
397 }
398
399 void appearance_minsize(Appearance *l, Size *s)
400 {
401 int i;
402 SIZE_SET(*s, 0, 0);
403
404 switch (l->surface.type) {
405 case Surface_Planar:
406 if (l->surface.data.planar.border ||
407 l->surface.data.planar.bevel == Bevel1)
408 SIZE_SET(*s, 2, 2);
409 else if (l->surface.data.planar.bevel == Bevel2)
410 SIZE_SET(*s, 4, 4);
411
412 for (i = 0; i < l->textures; ++i)
413 switch (l->texture[i].type) {
414 case Bitmask:
415 s->width += l->texture[i].data.mask.mask->w;
416 s->height += l->texture[i].data.mask.mask->h;
417 break;
418 case Text:
419 s->width +=font_measure_string(l->texture[i].data.text.font,
420 l->texture[i].data.text.string,
421 l->texture[i].data.text.shadow,
422 l->texture[i].data.text.offset);
423 s->height += font_height(l->texture[i].data.text.font,
424 l->texture[i].data.text.shadow,
425 l->texture[i].data.text.offset);
426 break;
427 case RGBA:
428 s->width += l->texture[i].data.rgba.width;
429 s->height += l->texture[i].data.rgba.height;
430 break;
431 case NoTexture:
432 break;
433 }
434 break;
435 }
436 return s;
437 }
This page took 0.054819 seconds and 4 git commands to generate.