]> Dogcows Code - chaz/openbox/blob - render/render.c
make minsize take int*'s not a Size*
[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 Rect tarea; /* area in which to draw textures */
201
202 if (w <= 0 || h <= 0 || x+w <= 0 || y+h <= 0) return;
203
204 g_assert(l->surface.type == Surface_Planar);
205
206 oldp = l->pixmap; /* save to free after changing the visible pixmap */
207 l->pixmap = XCreatePixmap(ob_display, ob_root, x+w, y+h, render_depth);
208 g_assert(l->pixmap != None);
209
210 if (l->xftdraw != NULL)
211 XftDrawDestroy(l->xftdraw);
212 l->xftdraw = XftDrawCreate(ob_display, l->pixmap, render_visual,
213 render_colormap);
214 g_assert(l->xftdraw != NULL);
215
216 g_free(l->surface.data.planar.pixel_data);
217 l->surface.data.planar.pixel_data = g_new(pixel32, w * h);
218
219
220 if (l->surface.data.planar.grad == Background_ParentRelative) {
221 sw = l->surface.data.planar.parent->area.width;
222 source = l->surface.data.planar.parent->surface.data.planar.pixel_data
223 + l->surface.data.planar.parentx
224 + sw * l->surface.data.planar.parenty;
225 dest = l->surface.data.planar.pixel_data;
226 for (i = 0; i < h; i++, source += sw, dest += w) {
227 memcpy(dest, source, w * sizeof(pixel32));
228 }
229 }
230 else if (l->surface.data.planar.grad == Background_Solid)
231 gradient_solid(l, x, y, w, h);
232 else gradient_render(&l->surface, w, h);
233
234 for (i = 0; i < l->textures; i++) {
235 tarea = l->texture[i].position;
236 if (l->surface.data.planar.relief != Flat) {
237 switch (l->surface.data.planar.bevel) {
238 case Bevel1:
239 tarea.x += 1; tarea.y += 1;
240 tarea.width -= 2; tarea.height -= 2;
241 break;
242 case Bevel2:
243 tarea.x += 2; tarea.y += 2;
244 tarea.width -= 4; tarea.height -= 4;
245 break;
246 }
247 } else if (l->surface.data.planar.border) {
248 tarea.x += 1; tarea.y += 1;
249 tarea.width -= 2; tarea.height -= 2;
250 }
251
252 switch (l->texture[i].type) {
253 case Text:
254 if (!transferred) {
255 transferred = 1;
256 if (l->surface.data.planar.grad != Background_Solid)
257 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
258 l->pixmap,x,y,w,h);
259 }
260 if (l->xftdraw == NULL) {
261 l->xftdraw = XftDrawCreate(ob_display, l->pixmap,
262 render_visual, render_colormap);
263 }
264 font_draw(l->xftdraw, &l->texture[i].data.text,
265 &tarea);
266 break;
267 case Bitmask:
268 if (!transferred) {
269 transferred = 1;
270 if (l->surface.data.planar.grad != Background_Solid)
271 pixel32_to_pixmap(l->surface.data.planar.pixel_data,
272 l->pixmap,x,y,w,h);
273 }
274 if (l->texture[i].data.mask.color->gc == None)
275 color_allocate_gc(l->texture[i].data.mask.color);
276 mask_draw(l->pixmap, &l->texture[i].data.mask,
277 &tarea);
278 break;
279 case RGBA:
280 image_draw(l->surface.data.planar.pixel_data,
281 &l->texture[i].data.rgba,
282 &tarea);
283 break;
284 }
285 }
286
287 if (!transferred) {
288 transferred = 1;
289 if (l->surface.data.planar.grad != Background_Solid)
290 pixel32_to_pixmap(l->surface.data.planar.pixel_data, l->pixmap
291 ,x,y,w,h);
292 }
293
294
295 XSetWindowBackgroundPixmap(ob_display, win, l->pixmap);
296 XClearWindow(ob_display, win);
297 if (oldp != None) XFreePixmap(ob_display, oldp);
298 }
299
300 /*
301 void gl_paint(Window win, Appearance *l)
302 {
303 glXMakeCurrent(ob_display, win, gl_context);
304 }
305 */
306
307 void render_shutdown(void)
308 {
309 }
310
311 Appearance *appearance_new(SurfaceType type, int numtex)
312 {
313 PlanarSurface *p;
314 Appearance *out;
315
316 out = g_new(Appearance, 1);
317 out->surface.type = type;
318 out->textures = numtex;
319 out->xftdraw = NULL;
320 if (numtex) out->texture = g_new0(Texture, numtex);
321 else out->texture = NULL;
322 out->pixmap = None;
323
324 switch (type) {
325 case Surface_Planar:
326 p = &out->surface.data.planar;
327 p->primary = NULL;
328 p->secondary = NULL;
329 p->border_color = NULL;
330 p->pixel_data = NULL;
331 break;
332 }
333 return out;
334 }
335
336 Appearance *appearance_copy(Appearance *orig)
337 {
338 PlanarSurface *spo, *spc;
339 Appearance *copy = g_new(Appearance, 1);
340 copy->surface.type = orig->surface.type;
341 switch (orig->surface.type) {
342 case Surface_Planar:
343 spo = &(orig->surface.data.planar);
344 spc = &(copy->surface.data.planar);
345 spc->grad = spo->grad;
346 spc->relief = spo->relief;
347 spc->bevel = spo->bevel;
348 if (spo->primary != NULL)
349 spc->primary = color_new(spo->primary->r,
350 spo->primary->g,
351 spo->primary->b);
352 else spc->primary = NULL;
353
354 if (spo->secondary != NULL)
355 spc->secondary = color_new(spo->secondary->r,
356 spo->secondary->g,
357 spo->secondary->b);
358 else spc->secondary = NULL;
359
360 if (spo->border_color != NULL)
361 spc->border_color = color_new(spo->border_color->r,
362 spo->border_color->g,
363 spo->border_color->b);
364 else spc->border_color = NULL;
365
366 spc->interlaced = spo->interlaced;
367 spc->border = spo->border;
368 spc->pixel_data = NULL;
369 break;
370 }
371 copy->textures = orig->textures;
372 copy->texture = g_memdup(orig->texture, orig->textures * sizeof(Texture));
373 copy->pixmap = None;
374 copy->xftdraw = NULL;
375 return copy;
376 }
377
378 void appearance_free(Appearance *a)
379 {
380 if (a) {
381 PlanarSurface *p;
382 if (a->pixmap != None) XFreePixmap(ob_display, a->pixmap);
383 if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
384 if (a->textures)
385 g_free(a->texture);
386 if (a->surface.type == Surface_Planar) {
387 p = &a->surface.data.planar;
388 if (p->primary != NULL) color_free(p->primary);
389 if (p->secondary != NULL) color_free(p->secondary);
390 if (p->border_color != NULL) color_free(p->border_color);
391 if (p->pixel_data != NULL) g_free(p->pixel_data);
392 }
393 g_free(a);
394 }
395 }
396
397
398 void pixel32_to_pixmap(pixel32 *in, Pixmap out, int x, int y, int w, int h)
399 {
400 unsigned char *scratch;
401 XImage *im = NULL;
402 im = XCreateImage(ob_display, render_visual, render_depth,
403 ZPixmap, 0, NULL, w, h, 32, 0);
404 g_assert(im != NULL);
405 im->byte_order = endian;
406 /* this malloc is a complete waste of time on normal 32bpp
407 as reduce_depth just sets im->data = data and returns
408 */
409 scratch = malloc(im->width * im->height * sizeof(pixel32));
410 im->data = (char*) scratch;
411 reduce_depth(in, im);
412 XPutImage(ob_display, out, DefaultGC(ob_display, ob_screen),
413 im, 0, 0, x, y, w, h);
414 im->data = NULL;
415 XDestroyImage(im);
416 free(scratch);
417 }
418
419 void appearance_minsize(Appearance *l, int *w, int *h)
420 {
421 int i;
422 *w = *h = 1;
423
424 switch (l->surface.type) {
425 case Surface_Planar:
426 if (l->surface.data.planar.relief != Flat) {
427 switch (l->surface.data.planar.bevel) {
428 case Bevel1:
429 *w = *h = 2;
430 break;
431 case Bevel2:
432 *w = *h = 4;
433 break;
434 }
435 } else if (l->surface.data.planar.border)
436 *w = *h = 2;
437
438 for (i = 0; i < l->textures; ++i)
439 switch (l->texture[i].type) {
440 case Bitmask:
441 *w += l->texture[i].data.mask.mask->w;
442 *h += l->texture[i].data.mask.mask->h;
443 break;
444 case Text:
445 *w +=font_measure_string(l->texture[i].data.text.font,
446 l->texture[i].data.text.string,
447 l->texture[i].data.text.shadow,
448 l->texture[i].data.text.offset);
449 *h += font_height(l->texture[i].data.text.font,
450 l->texture[i].data.text.shadow,
451 l->texture[i].data.text.offset);
452 break;
453 case RGBA:
454 *w += l->texture[i].data.rgba.width;
455 *h += l->texture[i].data.rgba.height;
456 break;
457 case NoTexture:
458 break;
459 }
460 break;
461 }
462 }
This page took 0.057792 seconds and 5 git commands to generate.