awful.util: pread return error
[awesome.git] / draw.c
blobc997cbcf5112053a97fa7eeec8bd4b20399e154f
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <cairo-xcb.h>
24 #include "config.h"
26 #include <langinfo.h>
27 #include <iconv.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <math.h>
32 #include "structs.h"
33 #include "screen.h"
35 #include "common/tokenize.h"
36 #include "common/xutil.h"
38 /** Convert text from any charset to UTF-8 using iconv.
39 * \param iso The ISO string to convert.
40 * \param len The string size.
41 * \param dest The destination pointer. Memory will be allocated, up to you to
42 * free, like any char *.
43 * \param dlen The destination length, can be NULL.
44 * \return True if conversion was done.
46 bool
47 draw_iso2utf8(const char *iso, size_t len, char **dest, ssize_t *dlen)
49 static iconv_t iso2utf8 = (iconv_t) -1;
50 static int8_t dont_need_convert = -1;
52 if(dont_need_convert == -1)
53 dont_need_convert = !a_strcmp(nl_langinfo(CODESET), "UTF-8");
55 if(!len || dont_need_convert)
56 return false;
58 if(iso2utf8 == (iconv_t) -1)
60 iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
61 if(iso2utf8 == (iconv_t) -1)
63 if(errno == EINVAL)
64 warn("unable to convert text from %s to UTF-8, not available",
65 nl_langinfo(CODESET));
66 else
67 warn("unable to convert text: %s", strerror(errno));
69 return false;
73 size_t orig_utf8len, utf8len;
74 char *utf8;
76 orig_utf8len = utf8len = 2 * len + 1;
77 utf8 = *dest = p_new(char, utf8len);
79 if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
81 warn("text conversion failed: %s", strerror(errno));
82 p_delete(dest);
83 return false;
86 if(dlen)
87 *dlen = orig_utf8len - utf8len;
89 return true;
92 /** Create a new Pango font.
93 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
94 * \return A new font.
96 font_t *
97 draw_font_new(const char *fontname)
99 cairo_surface_t *surface;
100 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
101 cairo_t *cr;
102 PangoLayout *layout;
103 font_t *font = p_new(font_t, 1);
105 /* Create a dummy cairo surface, cairo context and pango layout in
106 * order to get font informations */
107 surface = cairo_xcb_surface_create(globalconf.connection,
108 globalconf.default_screen,
109 globalconf.screens.tab[0].visual,
110 s->width_in_pixels,
111 s->height_in_pixels);
113 cr = cairo_create(surface);
114 layout = pango_cairo_create_layout(cr);
116 /* Get the font description used to set text on a PangoLayout */
117 font->desc = pango_font_description_from_string(fontname);
118 pango_layout_set_font_description(layout, font->desc);
120 /* Get height */
121 pango_layout_get_pixel_size(layout, NULL, &font->height);
123 g_object_unref(layout);
124 cairo_destroy(cr);
125 cairo_surface_destroy(surface);
127 return font;
130 /** Delete a font.
131 * \param font Font to delete.
133 void
134 draw_font_delete(font_t **font)
136 if(*font)
138 pango_font_description_free((*font)->desc);
139 p_delete(font);
143 /** Initialize a draw_text_context_t with text data.
144 * \param data The draw text context to init.
145 * \param str The text string to render.
146 * \param slen The text string length.
147 * \return True if everything is ok, false otherwise.
149 bool
150 draw_text_context_init(draw_text_context_t *data, const char *str, ssize_t slen)
152 GError *error = NULL;
154 if(!str)
155 return false;
157 if(!pango_parse_markup(str, slen, 0, &data->attr_list, &data->text, NULL, &error))
159 warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
160 if(error)
161 g_error_free(error);
162 return false;
165 data->len = a_strlen(data->text);
167 return true;
170 /** Initialize a new draw context.
171 * \param d The draw context to initialize.
172 * \param phys_screen Physical screen id.
173 * \param width Width.
174 * \param height Height.
175 * \param px Pixmap object to store.
176 * \param fg Foreground color.
177 * \param bg Background color.
179 void
180 draw_context_init(draw_context_t *d, int phys_screen,
181 int width, int height, xcb_pixmap_t px,
182 const xcolor_t *fg, const xcolor_t *bg)
184 d->phys_screen = phys_screen;
185 d->width = width;
186 d->height = height;
187 d->pixmap = px;
188 d->surface = cairo_xcb_surface_create(globalconf.connection,
189 px, globalconf.screens.tab[phys_screen].visual,
190 width, height);
191 d->cr = cairo_create(d->surface);
192 d->layout = pango_cairo_create_layout(d->cr);
193 d->fg = *fg;
194 d->bg = *bg;
197 /** Draw text into a draw context.
198 * \param ctx Draw context to draw to.
199 * \param data Draw text context data.
200 * \param ellip Ellipsize mode.
201 * \param wrap Wrap mode.
202 * \param align Text alignment.
203 * \param valign Vertical text alignment.
204 * \param margin Margin to respect when drawing text.
205 * \param area Area to draw to.
206 * \param ext Text extents.
208 void
209 draw_text(draw_context_t *ctx, draw_text_context_t *data,
210 PangoEllipsizeMode ellip, PangoWrapMode wrap,
211 alignment_t align, alignment_t valign, padding_t *margin, area_t area, area_t *ext)
213 int x, y;
215 pango_layout_set_text(ctx->layout, data->text, data->len);
216 pango_layout_set_width(ctx->layout,
217 pango_units_from_double(area.width
218 - (margin->left
219 + margin->right)));
220 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height)
221 - (margin->top + margin->bottom));
222 pango_layout_set_ellipsize(ctx->layout, ellip);
223 pango_layout_set_wrap(ctx->layout, wrap);
224 pango_layout_set_attributes(ctx->layout, data->attr_list);
225 pango_layout_set_font_description(ctx->layout, globalconf.font->desc);
227 x = area.x + margin->left;
228 y = area.y + margin->top;
230 /* only honors alignment if enough space */
231 if(ext->width < area.width)
232 switch(align)
234 case AlignCenter:
235 x += (area.width - ext->width) / 2;
236 break;
237 case AlignRight:
238 x += area.width - ext->width;
239 break;
240 default:
241 break;
244 switch(valign)
246 case AlignCenter:
247 y += (area.height - ext->height) / 2;
248 break;
249 case AlignBottom:
250 y += area.height - ext->height;
251 break;
252 default:
253 break;
256 cairo_move_to(ctx->cr, x, y);
258 cairo_set_source_rgba(ctx->cr,
259 ctx->fg.red / 65535.0,
260 ctx->fg.green / 65535.0,
261 ctx->fg.blue / 65535.0,
262 ctx->fg.alpha / 65535.0);
263 pango_cairo_update_layout(ctx->cr, ctx->layout);
264 pango_cairo_show_layout(ctx->cr, ctx->layout);
267 /** Setup color-source for cairo (gradient or mono).
268 * \param ctx Draw context.
269 * \param gradient_vector x, y to x + x_offset, y + y_offset.
270 * \param pcolor Color to use at start of gradient_vector.
271 * \param pcolor_center Color at center of gradient_vector.
272 * \param pcolor_end Color at end of gradient_vector.
273 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
275 static cairo_pattern_t *
276 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
277 const color_t *pcolor, const color_t *pcolor_center,
278 const color_t *pcolor_end)
280 cairo_pattern_t *pat = NULL;
281 bool has_center = pcolor_center->initialized;
282 bool has_end = pcolor_end->initialized;
284 /* no need for a real pattern: */
285 if(!has_end && !has_center)
286 cairo_set_source_rgba(ctx->cr,
287 pcolor->red / 255.0,
288 pcolor->green / 255.0,
289 pcolor->blue / 255.0,
290 pcolor->alpha / 255.0);
291 else
293 pat = cairo_pattern_create_linear(gradient_vector.x,
294 gradient_vector.y,
295 gradient_vector.x + gradient_vector.x_offset,
296 gradient_vector.y + gradient_vector.y_offset);
298 /* pcolor is always set (so far in awesome) */
299 cairo_pattern_add_color_stop_rgba(pat, 0.0,
300 pcolor->red / 255.0,
301 pcolor->green / 255.0,
302 pcolor->blue / 255.0,
303 pcolor->alpha / 255.0);
305 if(has_center)
306 cairo_pattern_add_color_stop_rgba(pat, 0.5,
307 pcolor_center->red / 255.0,
308 pcolor_center->green / 255.0,
309 pcolor_center->blue / 255.0,
310 pcolor_center->alpha / 255.0);
312 if(has_end)
313 cairo_pattern_add_color_stop_rgba(pat, 1.0,
314 pcolor_end->red / 255.0,
315 pcolor_end->green / 255.0,
316 pcolor_end->blue / 255.0,
317 pcolor_end->alpha / 255.0);
318 else
319 cairo_pattern_add_color_stop_rgba(pat, 1.0,
320 pcolor->red / 255.0,
321 pcolor->green / 255.0,
322 pcolor->blue / 255.0,
323 pcolor->alpha / 255.0);
324 cairo_set_source(ctx->cr, pat);
326 return pat;
329 /** Draw rectangle inside the coordinates
330 * \param ctx Draw context
331 * \param geometry geometry
332 * \param line_width line width
333 * \param filled fill rectangle?
334 * \param color color to use
336 void
337 draw_rectangle(draw_context_t *ctx, area_t geometry,
338 float line_width, bool filled, const color_t *color)
340 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
341 cairo_set_line_width(ctx->cr, line_width);
342 cairo_set_miter_limit(ctx->cr, 10.0);
343 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
344 cairo_set_source_rgba(ctx->cr,
345 color->red / 255.0,
346 color->green / 255.0,
347 color->blue / 255.0,
348 color->alpha / 255.0);
349 if(filled)
351 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
352 geometry.width, geometry.height);
353 cairo_fill(ctx->cr);
355 else
357 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
358 geometry.width - line_width, geometry.height - line_width);
359 cairo_stroke(ctx->cr);
363 /** Draw rectangle with gradient colors
364 * \param ctx Draw context.
365 * \param geometry Geometry.
366 * \param line_width Line width.
367 * \param filled Filled rectangle?
368 * \param gradient_vector Color-gradient course.
369 * \param pcolor Color at start of gradient_vector.
370 * \param pcolor_center Color in the center.
371 * \param pcolor_end Color at end of gradient_vector.
373 void
374 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
375 vector_t gradient_vector, const color_t *pcolor,
376 const color_t *pcolor_center, const color_t *pcolor_end)
378 cairo_pattern_t *pat;
380 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
381 cairo_set_line_width(ctx->cr, line_width);
382 cairo_set_miter_limit(ctx->cr, 10.0);
383 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
385 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
387 if(filled)
389 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
390 cairo_fill(ctx->cr);
392 else
394 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
395 cairo_stroke(ctx->cr);
398 if(pat)
399 cairo_pattern_destroy(pat);
402 /** Setup some cairo-things for drawing a graph
403 * \param ctx Draw context
405 void
406 draw_graph_setup(draw_context_t *ctx)
408 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
409 cairo_set_line_width(ctx->cr, 1.0);
410 /* without it, it can draw over the path on sharp angles
411 * ...too long lines * (...graph_line) */
412 cairo_set_miter_limit(ctx->cr, 0.0);
413 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
416 /** Draw a graph.
417 * \param ctx Draw context.
418 * \param rect The area to draw into.
419 * \param from Array of starting-point offsets to draw a graph lines.
420 * \param to Array of end-point offsets to draw a graph lines.
421 * \param cur_index Current position in data-array (cycles around).
422 * \param grow Put new values to the left or to the right.
423 * \param gradient_vector Color-Gradient course.
424 * \param pcolor Color at start of gradient_vector.
425 * \param pcolor_center Color in the center.
426 * \param pcolor_end Color at end of gradient_vector.
428 void
429 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
430 position_t grow, vector_t gradient_vector, const color_t *pcolor,
431 const color_t *pcolor_center, const color_t *pcolor_end)
433 int i = -1;
434 float x = rect.x + 0.5; /* middle of a pixel */
435 cairo_pattern_t *pat;
437 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
438 pcolor, pcolor_center, pcolor_end);
440 if(grow == Right) /* draw from right to left */
442 x += rect.width - 1;
443 while(++i < rect.width)
445 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
446 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
447 x -= 1.0;
449 if(--cur_index < 0)
450 cur_index = rect.width - 1;
453 else /* draw from left to right */
454 while(++i < rect.width)
456 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
457 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
458 x += 1.0;
460 if(--cur_index < 0)
461 cur_index = rect.width - 1;
464 cairo_stroke(ctx->cr);
466 if(pat)
467 cairo_pattern_destroy(pat);
470 /** Draw a line into a graph-widget.
471 * \param ctx Draw context.
472 * \param rect The area to draw into.
473 * \param to array of offsets to draw the line through...
474 * \param cur_index current position in data-array (cycles around)
475 * \param grow put new values to the left or to the right
476 * \param gradient_vector Color-gradient course.
477 * \param pcolor Color at start of gradient_vector.
478 * \param pcolor_center Color in the center.
479 * \param pcolor_end Color at end of gradient_vector.
481 void
482 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
483 position_t grow, vector_t gradient_vector, const color_t *pcolor,
484 const color_t *pcolor_center, const color_t *pcolor_end)
486 int i, w;
487 float x, y;
488 cairo_pattern_t *pat;
490 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
491 * ... it won't fill some pixels! It also looks much nicer so.
492 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
493 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
494 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
495 /* a nicer, better visible line compared to 1.0 */
496 cairo_set_line_width(ctx->cr, 1.25);
498 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
500 /* path through the centers of pixels */
501 x = rect.x + 0.5;
502 y = rect.y + 0.5;
503 w = rect.width;
505 if(grow == Right)
507 /* go through the values from old to new. Begin with the oldest. */
508 if(++cur_index > w - 1)
509 cur_index = 0;
511 cairo_move_to(ctx->cr, x, y - to[cur_index]);
513 else
514 /* on the left border: fills a pixel also when there's only one value */
515 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
517 for(i = 0; i < w; i++)
519 cairo_line_to(ctx->cr, x, y - to[cur_index]);
520 x += 1.0;
522 /* cycles around the index */
523 if(grow == Right)
525 if(++cur_index > w - 1)
526 cur_index = 0;
528 else
530 if(--cur_index < 0)
531 cur_index = w - 1;
535 /* onto the right border: fills a pixel also when there's only one value */
536 if(grow == Right)
537 cairo_line_to(ctx->cr, x, y - to[(cur_index + (w - 1)) % w]);
539 cairo_stroke(ctx->cr);
541 if(pat)
542 cairo_pattern_destroy(pat);
543 /* reset line-width */
544 cairo_set_line_width(ctx->cr, 1.0);
547 /** Draw an image from ARGB data to a draw context.
548 * Data should be stored as an array of alpha, red, blue, green for each pixel
549 * and the array size should be w * h elements long.
550 * \param ctx Draw context to draw to.
551 * \param x X coordinate.
552 * \param y Y coordinate.
553 * \param w Width.
554 * \param h Height.
555 * \param ratio The ratio to apply to the image.
556 * \param data The image pixels array.
558 static void
559 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
560 double ratio, unsigned char *data)
562 cairo_t *cr;
563 cairo_surface_t *source;
565 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
566 #if CAIRO_VERSION_MAJOR < 1 || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR < 5) || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR == 5 && CAIRO_VERSION_MICRO < 8)
567 sizeof(unsigned char) * 4 * w);
568 #else
569 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
570 #endif
571 cr = cairo_create(ctx->surface);
572 cairo_scale(cr, ratio, ratio);
573 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
575 cairo_paint(cr);
577 cairo_destroy(cr);
578 cairo_surface_destroy(source);
581 /** Draw an image to a draw context.
582 * \param ctx Draw context to draw to.
583 * \param x X coordinate.
584 * \param y Y coordinate.
585 * \param ratio The ratio to apply to the image.
586 * \param image The image to draw.
588 void
589 draw_image(draw_context_t *ctx, int x, int y, double ratio, image_t *image)
591 draw_image_from_argb_data(ctx, x, y, image_getwidth(image), image_getheight(image), ratio, image_getdata(image));
594 /** Rotate a pixmap.
595 * \param ctx Draw context to draw with.
596 * \param src Drawable to draw from.
597 * \param dest Drawable to draw to.
598 * \param src_w Drawable width.
599 * \param src_h Drawable height.
600 * \param dest_w Drawable width.
601 * \param dest_h Drawable height.
602 * \param angle angle to rotate.
603 * \param tx Translate to this x coordinate.
604 * \param ty Translate to this y coordinate.
606 void
607 draw_rotate(draw_context_t *ctx,
608 xcb_pixmap_t src, xcb_pixmap_t dest,
609 int src_w, int src_h,
610 int dest_w, int dest_h,
611 double angle, int tx, int ty)
613 cairo_surface_t *surface, *source;
614 cairo_t *cr;
616 surface = cairo_xcb_surface_create(globalconf.connection, dest,
617 globalconf.screens.tab[ctx->phys_screen].visual,
618 dest_w, dest_h);
619 source = cairo_xcb_surface_create(globalconf.connection, src,
620 globalconf.screens.tab[ctx->phys_screen].visual,
621 src_w, src_h);
622 cr = cairo_create (surface);
624 cairo_translate(cr, tx, ty);
625 cairo_rotate(cr, angle);
627 cairo_set_source_surface(cr, source, 0.0, 0.0);
628 cairo_paint(cr);
630 cairo_destroy(cr);
631 cairo_surface_destroy(source);
632 cairo_surface_destroy(surface);
635 /** Return the width and height of a text in pixel.
636 * \param data The draw context text data.
637 * \return Text height and width.
639 area_t
640 draw_text_extents(draw_text_context_t *data)
642 cairo_surface_t *surface;
643 cairo_t *cr;
644 PangoLayout *layout;
645 PangoRectangle ext;
646 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
647 area_t geom = { 0, 0, 0, 0 };
649 if(data->len <= 0)
650 return geom;
652 surface = cairo_xcb_surface_create(globalconf.connection,
653 globalconf.default_screen,
654 globalconf.screens.tab[0].visual,
655 s->width_in_pixels,
656 s->height_in_pixels);
658 cr = cairo_create(surface);
659 layout = pango_cairo_create_layout(cr);
660 pango_layout_set_text(layout, data->text, data->len);
661 pango_layout_set_attributes(layout, data->attr_list);
662 pango_layout_set_font_description(layout, globalconf.font->desc);
663 pango_layout_get_pixel_extents(layout, NULL, &ext);
664 g_object_unref(layout);
665 cairo_destroy(cr);
666 cairo_surface_destroy(surface);
668 geom.width = ext.width;
669 geom.height = ext.height;
671 return geom;
674 /** Transform a string to a alignment_t type.
675 * Recognized string are flex, fixed, left, center, middle or right.
676 * \param align Atring with align text.
677 * \param len The string length.
678 * \return An alignment_t type.
680 alignment_t
681 draw_align_fromstr(const char *align, ssize_t len)
683 switch(a_tokenize(align, len))
685 case A_TK_CENTER: return AlignCenter;
686 case A_TK_RIGHT: return AlignRight;
687 case A_TK_TOP: return AlignTop;
688 case A_TK_BOTTOM: return AlignBottom;
689 case A_TK_MIDDLE: return AlignMiddle;
690 default: return AlignLeft;
694 /** Transform an alignment to a string.
695 * \param a The alignment.
696 * \return A string which must not be freed.
698 const char *
699 draw_align_tostr(alignment_t a)
701 switch(a)
703 case AlignLeft: return "left";
704 case AlignCenter: return "center";
705 case AlignRight: return "right";
706 case AlignBottom: return "bottom";
707 case AlignTop: return "top";
708 case AlignMiddle: return "middle";
709 default: return NULL;
713 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80