awesomerc.lua: use nano by default if no editor found in env
[awesome.git] / draw.c
blobfb9a6c316a5e3e6108e2255612dd066cef943ab1
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007-2008 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 <string.h>
30 #include <ctype.h>
31 #include <math.h>
33 #include "structs.h"
35 #include "common/tokenize.h"
36 #include "common/markup.h"
38 extern awesome_t globalconf;
40 /** Convert text from any charset to UTF-8 using iconv.
41 * \param iso The ISO string to convert.
42 * \param len The string size.
43 * \return NULL if error, otherwise pointer to the new converted string.
45 char *
46 draw_iso2utf8(const char *iso, size_t len)
48 size_t utf8len;
49 char *utf8, *utf8p;
50 static iconv_t iso2utf8 = (iconv_t) -1;
52 if(!len)
53 return NULL;
55 if(!a_strcmp(nl_langinfo(CODESET), "UTF-8"))
56 return NULL;
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 NULL;
73 utf8len = 2 * len + 1;
74 utf8 = utf8p = p_new(char, utf8len);
76 if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
78 warn("text conversion failed: %s", strerror(errno));
79 p_delete(&utf8p);
82 return utf8p;
85 static xcb_visualtype_t *
86 draw_screen_default_visual(xcb_screen_t *s)
88 xcb_depth_iterator_t depth_iter;
89 xcb_visualtype_iterator_t visual_iter;
91 if(!s)
92 return NULL;
94 for(depth_iter = xcb_screen_allowed_depths_iterator(s);
95 depth_iter.rem; xcb_depth_next (&depth_iter))
96 for(visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
97 visual_iter.rem; xcb_visualtype_next (&visual_iter))
98 if (s->root_visual == visual_iter.data->visual_id)
99 return visual_iter.data;
101 return NULL;
104 /** Create a new Pango font
105 * \param phys_screen The physical screen number.
106 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE])
107 * \return a new font
109 font_t *
110 draw_font_new(int phys_screen, const char *fontname)
112 cairo_surface_t *surface;
113 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
114 cairo_t *cr;
115 PangoLayout *layout;
116 font_t *font = p_new(font_t, 1);
118 /* Create a dummy cairo surface, cairo context and pango layout in
119 * order to get font informations */
120 surface = cairo_xcb_surface_create(globalconf.connection,
121 phys_screen,
122 draw_screen_default_visual(s),
123 s->width_in_pixels,
124 s->height_in_pixels);
126 cr = cairo_create(surface);
127 layout = pango_cairo_create_layout(cr);
129 /* Get the font description used to set text on a PangoLayout */
130 font->desc = pango_font_description_from_string(fontname);
131 pango_layout_set_font_description(layout, font->desc);
133 /* Get height */
134 pango_layout_get_pixel_size(layout, NULL, &font->height);
136 g_object_unref(layout);
137 cairo_destroy(cr);
138 cairo_surface_destroy(surface);
140 return font;
143 /** Delete a font.
144 * \param font Font to delete.
146 void
147 draw_font_delete(font_t **font)
149 if(*font)
151 pango_font_description_free((*font)->desc);
152 p_delete(font);
156 static void
157 draw_markup_on_element(markup_parser_data_t *p, const char *elem,
158 const char **names, const char **values)
160 draw_parser_data_t *data = p->priv;
162 xcolor_init_request_t reqs[3];
163 int8_t i, bg_color_nbr = -1, reqs_nbr = -1;
165 /* hack: markup.c validates tags so we can avoid strcmps here */
166 switch (*elem) {
167 case 'b':
168 if(elem[1] == 'g') /* bg? */
170 if(elem[2] == '_') /* bg_margin */
171 for(; *names; names++, values++)
172 switch(a_tokenize(*names, -1))
174 case A_TK_LEFT:
175 data->bg_margin.left = atoi(*values);
176 break;
177 case A_TK_TOP:
178 data->bg_margin.top = atoi(*values);
179 default:
180 break;
182 else /* bg */
183 for(; *names; names++, values++)
184 switch(a_tokenize(*names, -1))
186 case A_TK_COLOR:
187 reqs[++reqs_nbr] = xcolor_init_unchecked(&data->bg_color,
188 *values,
189 a_strlen(*values));
191 bg_color_nbr = reqs_nbr;
192 break;
193 case A_TK_IMAGE:
194 if(data->bg_image)
195 image_delete(&data->bg_image);
196 data->bg_image = image_new_from_file(*values);
197 break;
198 case A_TK_ALIGN:
199 data->bg_align = draw_align_fromstr(*values, -1);
200 break;
201 case A_TK_RESIZE:
202 data->bg_resize = a_strtobool(*values, -1);
203 default:
204 break;
208 else /* border */
209 for(; *names; names++, values++)
210 switch(a_tokenize(*names, -1))
212 case A_TK_COLOR:
213 reqs[++reqs_nbr] = xcolor_init_unchecked(&data->border.color,
214 *values,
215 a_strlen(*values));
216 break;
217 case A_TK_WIDTH:
218 data->border.width = atoi(*values);
219 break;
220 default:
221 break;
223 break;
224 case 't': /* text */
225 for(; *names; names++, values++)
226 switch(a_tokenize(*names, -1))
228 case A_TK_ALIGN:
229 data->align = draw_align_fromstr(*values, -1);
230 break;
231 case A_TK_SHADOW:
232 reqs[++reqs_nbr] = xcolor_init_unchecked(&data->shadow.color,
233 *values,
234 a_strlen(*values));
235 break;
236 case A_TK_SHADOW_OFFSET:
237 data->shadow.offset = atoi(*values);
238 break;
239 default:
240 break;
242 break;
243 case 'm': /* margin */
244 for (; *names; names++, values++)
245 switch(a_tokenize(*names, -1))
247 case A_TK_LEFT:
248 data->margin.left = atoi(*values);
249 break;
250 case A_TK_RIGHT:
251 data->margin.right = atoi(*values);
252 break;
253 case A_TK_TOP:
254 data->margin.top = atoi(*values);
255 default:
256 break;
258 break;
261 for(i = 0; i <= reqs_nbr; i++)
262 if(i == bg_color_nbr)
263 data->has_bg_color = xcolor_init_reply(reqs[i]);
264 else
265 xcolor_init_reply(reqs[i]);
268 bool
269 draw_text_markup_expand(draw_parser_data_t *data,
270 const char *str, ssize_t slen)
272 static char const * const elements[] = { "bg", "bg_margin", "text", "margin", "border", NULL };
273 markup_parser_data_t p =
275 .elements = elements,
276 .priv = data,
277 .on_element = &draw_markup_on_element,
279 GError *error = NULL;
280 bool ret = false;
282 markup_parser_data_init(&p);
284 if(!markup_parse(&p, str, slen))
285 goto bailout;
287 if(!pango_parse_markup(p.text.s, p.text.len, 0, &data->attr_list, &data->text, NULL, &error))
289 warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
290 if(error)
291 g_error_free(error);
292 goto bailout;
295 data->len = a_strlen(data->text);
296 ret = true;
298 bailout:
299 markup_parser_data_wipe(&p);
300 return ret;
303 /** Initialize a new draw context.
304 * \param d The draw context to initialize.
305 * \param phys_screen Physical screen id.
306 * \param width Width.
307 * \param height Height.
308 * \param px Pixmap object to store.
309 * \param fg Foreground color.
310 * \param bg Background color.
312 void
313 draw_context_init(draw_context_t *d, int phys_screen,
314 int width, int height, xcb_pixmap_t px,
315 const xcolor_t *fg, const xcolor_t *bg)
317 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
319 d->phys_screen = phys_screen;
320 d->width = width;
321 d->height = height;
322 d->depth = s->root_depth;
323 d->visual = draw_screen_default_visual(s);
324 d->pixmap = px;
325 d->surface = cairo_xcb_surface_create(globalconf.connection, px, d->visual, width, height);
326 d->cr = cairo_create(d->surface);
327 d->layout = pango_cairo_create_layout(d->cr);
328 d->fg = *fg;
329 d->bg = *bg;
332 /** Draw text into a draw context.
333 * \param ctx Draw context to draw to.
334 * \param font The font to use.
335 * \param area Area to draw to.
336 * \param text Text to draw.
337 * \param len Text to draw length.
338 * \param data Optional parser data.
340 void
341 draw_text(draw_context_t *ctx, font_t *font,
342 area_t area, const char *text, ssize_t len, draw_parser_data_t *pdata)
344 int x, y;
345 PangoRectangle ext;
346 draw_parser_data_t parser_data;
348 if(!pdata)
350 draw_parser_data_init(&parser_data);
351 if(draw_text_markup_expand(&parser_data, text, len))
353 text = parser_data.text;
354 len = parser_data.len;
356 pdata = &parser_data;
358 else
360 text = pdata->text;
361 len = pdata->len;
364 if(pdata->has_bg_color)
365 draw_rectangle(ctx, area, 1.0, true, &pdata->bg_color);
367 if(pdata->border.width > 0)
368 draw_rectangle(ctx, area, pdata->border.width, false, &pdata->border.color);
370 if(pdata->bg_image)
372 x = area.x;
373 y = area.y;
374 switch(pdata->bg_align)
376 case AlignCenter:
377 x += (area.width - pdata->bg_image->width) / 2;
378 break;
379 case AlignRight:
380 x += area.width- pdata->bg_image->width;
381 break;
382 default:
383 break;
385 draw_image(ctx,
386 x + pdata->bg_margin.left,
387 y + pdata->bg_margin.top,
388 pdata->bg_resize ? area.height : 0, pdata->bg_image);
391 pango_layout_set_text(ctx->layout, pdata->text, pdata->len);
392 pango_layout_set_width(ctx->layout,
393 pango_units_from_double(area.width
394 - (pdata->margin.left
395 + pdata->margin.right)));
396 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
397 pango_layout_set_ellipsize(ctx->layout, PANGO_ELLIPSIZE_END);
398 pango_layout_set_wrap(ctx->layout, PANGO_WRAP_WORD_CHAR);
399 pango_layout_set_attributes(ctx->layout, pdata->attr_list);
400 pango_layout_set_font_description(ctx->layout, font->desc);
401 pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
403 x = area.x + pdata->margin.left;
404 /* + 1 is added for rounding, so that in any case of doubt we rather draw
405 * the text 1px lower than too high which usually results in a better type
406 * face */
407 y = area.y + (ctx->height - ext.height + 1) / 2 + pdata->margin.top;
409 switch(pdata->align)
411 case AlignCenter:
412 x += (area.width - ext.width) / 2;
413 break;
414 case AlignRight:
415 x += area.width - ext.width;
416 break;
417 default:
418 break;
421 if(pdata->shadow.offset)
423 cairo_set_source_rgba(ctx->cr,
424 pdata->shadow.color.red / 65535.0,
425 pdata->shadow.color.green / 65535.0,
426 pdata->shadow.color.blue / 65535.0,
427 pdata->shadow.color.alpha / 65535.0);
428 cairo_move_to(ctx->cr, x + pdata->shadow.offset, y + pdata->shadow.offset);
429 pango_cairo_layout_path(ctx->cr, ctx->layout);
430 cairo_stroke(ctx->cr);
433 cairo_move_to(ctx->cr, x, y);
435 cairo_set_source_rgba(ctx->cr,
436 ctx->fg.red / 65535.0,
437 ctx->fg.green / 65535.0,
438 ctx->fg.blue / 65535.0,
439 ctx->fg.alpha / 65535.0);
440 pango_cairo_update_layout(ctx->cr, ctx->layout);
441 pango_cairo_show_layout(ctx->cr, ctx->layout);
443 if (pdata == &parser_data)
444 draw_parser_data_wipe(&parser_data);
447 /** Setup color-source for cairo (gradient or mono).
448 * \param ctx Draw context.
449 * \param gradient_vector x, y to x + x_offset, y + y_offset.
450 * \param pcolor Color to use at start of gradient_vector.
451 * \param pcolor_center Color at center of gradient_vector.
452 * \param pcolor_end Color at end of gradient_vector.
453 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
455 static cairo_pattern_t *
456 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
457 const xcolor_t *pcolor, const xcolor_t *pcolor_center,
458 const xcolor_t *pcolor_end)
460 cairo_pattern_t *pat = NULL;
461 bool has_center = pcolor_center->initialized;
462 bool has_end = pcolor_end->initialized;
464 /* no need for a real pattern: */
465 if(!has_end && !has_center)
466 cairo_set_source_rgba(ctx->cr,
467 pcolor->red / 65535.0,
468 pcolor->green / 65535.0,
469 pcolor->blue / 65535.0,
470 pcolor->alpha / 65535.0);
471 else
473 pat = cairo_pattern_create_linear(gradient_vector.x,
474 gradient_vector.y,
475 gradient_vector.x + gradient_vector.x_offset,
476 gradient_vector.y + gradient_vector.y_offset);
478 /* pcolor is always set (so far in awesome) */
479 cairo_pattern_add_color_stop_rgba(pat, 0.0,
480 pcolor->red / 65535.0,
481 pcolor->green / 65535.0,
482 pcolor->blue / 65535.0,
483 pcolor->alpha / 65535.0);
485 if(has_center)
486 cairo_pattern_add_color_stop_rgba(pat, 0.5,
487 pcolor_center->red / 65535.0,
488 pcolor_center->green / 65535.0,
489 pcolor_center->blue / 65535.0,
490 pcolor_center->alpha / 65535.0);
492 if(has_end)
493 cairo_pattern_add_color_stop_rgba(pat, 1.0,
494 pcolor_end->red / 65535.0,
495 pcolor_end->green / 65535.0,
496 pcolor_end->blue / 65535.0,
497 pcolor_end->alpha / 65535.0);
498 else
499 cairo_pattern_add_color_stop_rgba(pat, 1.0,
500 pcolor->red / 65535.0,
501 pcolor->green / 65535.0,
502 pcolor->blue / 65535.0,
503 pcolor->alpha / 65535.0);
504 cairo_set_source(ctx->cr, pat);
506 return pat;
509 /** Draw rectangle inside the coordinates
510 * \param ctx Draw context
511 * \param geometry geometry
512 * \param line_width line width
513 * \param filled fill rectangle?
514 * \param color color to use
516 void
517 draw_rectangle(draw_context_t *ctx, area_t geometry,
518 float line_width, bool filled, const xcolor_t *color)
520 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
521 cairo_set_line_width(ctx->cr, line_width);
522 cairo_set_miter_limit(ctx->cr, 10.0);
523 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
524 cairo_set_source_rgba(ctx->cr,
525 color->red / 65535.0,
526 color->green / 65535.0,
527 color->blue / 65535.0,
528 color->alpha / 65535.0);
529 if(filled)
531 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
532 geometry.width, geometry.height);
533 cairo_fill(ctx->cr);
535 else
537 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
538 geometry.width - line_width, geometry.height - line_width);
539 cairo_stroke(ctx->cr);
543 /** Draw rectangle with gradient colors
544 * \param ctx Draw context.
545 * \param geometry Geometry.
546 * \param line_width Line width.
547 * \param filled Filled rectangle?
548 * \param gradient_vector Color-gradient course.
549 * \param pcolor Color at start of gradient_vector.
550 * \param pcolor_center Color in the center.
551 * \param pcolor_end Color at end of gradient_vector.
553 void
554 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
555 vector_t gradient_vector, const xcolor_t *pcolor,
556 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
558 cairo_pattern_t *pat;
560 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
561 cairo_set_line_width(ctx->cr, line_width);
562 cairo_set_miter_limit(ctx->cr, 10.0);
563 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
565 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
567 if(filled)
569 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
570 cairo_fill(ctx->cr);
572 else
574 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
575 cairo_stroke(ctx->cr);
578 if(pat)
579 cairo_pattern_destroy(pat);
582 /** Setup some cairo-things for drawing a graph
583 * \param ctx Draw context
585 void
586 draw_graph_setup(draw_context_t *ctx)
588 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
589 cairo_set_line_width(ctx->cr, 1.0);
590 /* without it, it can draw over the path on sharp angles
591 * ...too long lines * (...graph_line) */
592 cairo_set_miter_limit(ctx->cr, 0.0);
593 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
596 /** Draw a graph.
597 * \param ctx Draw context.
598 * \param rect The area to draw into.
599 * \param from Array of starting-point offsets to draw a graph lines.
600 * \param to Array of end-point offsets to draw a graph lines.
601 * \param cur_index Current position in data-array (cycles around).
602 * \param grow Put new values to the left or to the right.
603 * \param gradient_vector Color-Gradient course.
604 * \param pcolor Color at start of gradient_vector.
605 * \param pcolor_center Color in the center.
606 * \param pcolor_end Color at end of gradient_vector.
608 void
609 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
610 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
611 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
613 int i = -1;
614 float x = rect.x + 0.5; /* middle of a pixel */
615 cairo_pattern_t *pat;
617 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
618 pcolor, pcolor_center, pcolor_end);
620 if(grow == Right) /* draw from right to left */
622 x += rect.width - 1;
623 while(++i < rect.width)
625 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
626 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
627 x -= 1.0;
629 if (--cur_index < 0)
630 cur_index = rect.width - 1;
633 else /* draw from left to right */
634 while(++i < rect.width)
636 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
637 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
638 x += 1.0;
640 if (--cur_index < 0)
641 cur_index = rect.width - 1;
644 cairo_stroke(ctx->cr);
646 if(pat)
647 cairo_pattern_destroy(pat);
650 /** Draw a line into a graph-widget.
651 * \param ctx Draw context.
652 * \param rect The area to draw into.
653 * \param to array of offsets to draw the line through...
654 * \param cur_index current position in data-array (cycles around)
655 * \param grow put new values to the left or to the right
656 * \param gradient_vector Color-gradient course.
657 * \param pcolor Color at start of gradient_vector.
658 * \param pcolor_center Color in the center.
659 * \param pcolor_end Color at end of gradient_vector.
661 void
662 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
663 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
664 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
666 int i, w;
667 float x, y;
668 cairo_pattern_t *pat;
670 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
671 * ... it won't fill some pixels! It also looks much nicer so.
672 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
673 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
674 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
675 /* a nicer, better visible line compared to 1.0 */
676 cairo_set_line_width(ctx->cr, 1.25);
678 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
680 /* path through the centers of pixels */
681 x = rect.x + 0.5;
682 y = rect.y + 0.5;
683 w = rect.width;
685 if(grow == Right)
687 /* go through the values from old to new. Begin with the oldest. */
688 if(++cur_index > w - 1)
689 cur_index = 0;
691 cairo_move_to(ctx->cr, x, y - to[cur_index]);
693 else
694 /* on the left border: fills a pixel also when there's only one value */
695 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
697 for(i = 0; i < w; i++)
699 cairo_line_to(ctx->cr, x, y - to[cur_index]);
700 x += 1.0;
702 /* cycles around the index */
703 if(grow == Right)
705 if (++cur_index > w - 1)
706 cur_index = 0;
708 else
710 if(--cur_index < 0)
711 cur_index = w - 1;
715 /* onto the right border: fills a pixel also when there's only one value */
716 if(grow == Right)
717 cairo_line_to(ctx->cr, x, y - to[cur_index]);
719 cairo_stroke(ctx->cr);
721 if(pat)
722 cairo_pattern_destroy(pat);
723 /* reset line-width */
724 cairo_set_line_width(ctx->cr, 1.0);
727 /** Draw an image from ARGB data to a draw context.
728 * Data should be stored as an array of alpha, red, blue, green for each pixel
729 * and the array size should be w * h elements long.
730 * \param ctx Draw context to draw to.
731 * \param x X coordinate.
732 * \param y Y coordinate.
733 * \param w Width.
734 * \param h Height.
735 * \param wanted_h Wanted height: if > 0, image will be resized.
736 * \param data The image pixels array.
738 static void
739 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
740 int wanted_h, unsigned char *data)
742 double ratio;
743 cairo_t *cr;
744 cairo_surface_t *source;
746 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
747 #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)
748 sizeof(unsigned char) * 4 * w);
749 #else
750 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
751 #endif
752 cr = cairo_create(ctx->surface);
753 if(wanted_h > 0 && h > 0)
755 ratio = (double) wanted_h / (double) h;
756 cairo_scale(cr, ratio, ratio);
757 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
759 else
760 cairo_set_source_surface(cr, source, x, y);
762 cairo_paint(cr);
764 cairo_destroy(cr);
765 cairo_surface_destroy(source);
768 /** Draw an image to a draw context.
769 * \param ctx Draw context to draw to.
770 * \param x X coordinate.
771 * \param y Y coordinate.
772 * \param wanted_h Wanted height: if > 0, image will be resized.
773 * \param image The image to draw.
775 void
776 draw_image(draw_context_t *ctx, int x, int y, int wanted_h, image_t *image)
778 draw_image_from_argb_data(ctx, x, y, image->width, image->height, wanted_h, image->data);
781 /** Rotate a pixmap.
782 * \param ctx Draw context to draw with.
783 * \param src Drawable to draw from.
784 * \param dest Drawable to draw to.
785 * \param src_w Drawable width.
786 * \param src_h Drawable height.
787 * \param dest_w Drawable width.
788 * \param dest_h Drawable height.
789 * \param angle angle to rotate.
790 * \param tx Translate to this x coordinate.
791 * \param ty Translate to this y coordinate.
793 void
794 draw_rotate(draw_context_t *ctx,
795 xcb_pixmap_t src, xcb_pixmap_t dest,
796 int src_w, int src_h,
797 int dest_w, int dest_h,
798 double angle, int tx, int ty)
800 cairo_surface_t *surface, *source;
801 cairo_t *cr;
803 surface = cairo_xcb_surface_create(globalconf.connection, dest,
804 ctx->visual, dest_w, dest_h);
805 source = cairo_xcb_surface_create(globalconf.connection, src,
806 ctx->visual, src_w, src_h);
807 cr = cairo_create (surface);
809 cairo_translate(cr, tx, ty);
810 cairo_rotate(cr, angle);
812 cairo_set_source_surface(cr, source, 0.0, 0.0);
813 cairo_paint(cr);
815 cairo_destroy(cr);
816 cairo_surface_destroy(source);
817 cairo_surface_destroy(surface);
820 /** Return the width and height of a text in pixel.
821 * \param phys_screen Physical screen number.
822 * \param font Font to use.
823 * \param text The text.
824 * \param len The text length.
825 * \param pdata The parser data to fill.
826 * \return Text height and width.
828 area_t
829 draw_text_extents(int phys_screen, font_t *font,
830 const char *text, ssize_t len, draw_parser_data_t *parser_data)
832 cairo_surface_t *surface;
833 cairo_t *cr;
834 PangoLayout *layout;
835 PangoRectangle ext;
836 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
837 area_t geom = { 0, 0, 0, 0 };
839 if(!len)
840 return geom;
842 if(!draw_text_markup_expand(parser_data, text, len))
843 return geom;
845 surface = cairo_xcb_surface_create(globalconf.connection, phys_screen,
846 draw_screen_default_visual(s),
847 s->width_in_pixels,
848 s->height_in_pixels);
850 cr = cairo_create(surface);
851 layout = pango_cairo_create_layout(cr);
852 pango_layout_set_text(layout, parser_data->text, parser_data->len);
853 pango_layout_set_attributes(layout, parser_data->attr_list);
854 pango_layout_set_font_description(layout, font->desc);
855 pango_layout_get_pixel_extents(layout, NULL, &ext);
856 g_object_unref(layout);
857 cairo_destroy(cr);
858 cairo_surface_destroy(surface);
860 geom.width = ext.width;
861 geom.height = ext.height * 1.5;
863 return geom;
866 /** Transform a string to a alignment_t type.
867 * Recognized string are left, center or right. Everything else will be
868 * recognized as AlignAuto.
869 * \param align Atring with align text.
870 * \param len The string length.
871 * \return An alignment_t type.
873 alignment_t
874 draw_align_fromstr(const char *align, ssize_t len)
876 switch (a_tokenize(align, len))
878 case A_TK_LEFT: return AlignLeft;
879 case A_TK_CENTER: return AlignCenter;
880 case A_TK_RIGHT: return AlignRight;
881 case A_TK_FLEX: return AlignFlex;
882 default: return AlignAuto;
886 /** Transform an alignment to a string.
887 * \param a The alignment.
888 * \return A string which must not be freed.
890 const char *
891 draw_align_tostr(alignment_t a)
893 switch(a)
895 case AlignLeft: return "left";
896 case AlignCenter: return "center";
897 case AlignRight: return "right";
898 case AlignFlex: return "flex";
899 case AlignAuto: return "auto";
900 default: return NULL;
904 #define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
906 /** Send a request to initialize a X color.
907 * \param color xcolor_t struct to store color into.
908 * \param colstr Color specification.
909 * \return request informations.
911 xcolor_init_request_t
912 xcolor_init_unchecked(xcolor_t *color, const char *colstr, ssize_t len)
914 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
915 xcolor_init_request_t req;
916 unsigned long colnum;
917 uint16_t red, green, blue;
919 p_clear(&req, 1);
921 if(!len)
923 req.has_error = true;
924 return req;
927 req.alpha = 0xffff;
928 req.color = color;
930 /* The color is given in RGB value */
931 if(colstr[0] == '#')
933 char *p;
935 if(len == 7)
937 colnum = strtoul(colstr + 1, &p, 16);
938 if(p - colstr != 7)
939 goto invalid;
941 /* we have alpha */
942 else if(len == 9)
944 colnum = strtoul(colstr + 1, &p, 16);
945 if(p - colstr != 9)
946 goto invalid;
947 req.alpha = RGB_COLOR_8_TO_16(colnum);
948 colnum >>= 8;
950 else
952 invalid:
953 warn("awesome: error, invalid color '%s'", colstr);
954 req.has_error = true;
955 return req;
958 red = RGB_COLOR_8_TO_16(colnum >> 16);
959 green = RGB_COLOR_8_TO_16(colnum >> 8);
960 blue = RGB_COLOR_8_TO_16(colnum);
962 req.is_hexa = true;
963 req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
964 s->default_colormap,
965 red, green, blue);
967 else
969 req.is_hexa = false;
970 req.cookie_named = xcb_alloc_named_color_unchecked(globalconf.connection,
971 s->default_colormap, len,
972 colstr);
975 req.has_error = false;
976 req.colstr = colstr;
978 return req;
981 /** Initialize a X color.
982 * \param req xcolor_init request.
983 * \return True if color allocation was successfull.
985 bool
986 xcolor_init_reply(xcolor_init_request_t req)
988 if(req.has_error)
989 return false;
991 if(req.is_hexa)
993 xcb_alloc_color_reply_t *hexa_color;
995 if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
996 req.cookie_hexa, NULL)))
998 req.color->pixel = hexa_color->pixel;
999 req.color->red = hexa_color->red;
1000 req.color->green = hexa_color->green;
1001 req.color->blue = hexa_color->blue;
1002 req.color->alpha = req.alpha;
1003 req.color->initialized = true;
1004 p_delete(&hexa_color);
1005 return true;
1008 else
1010 xcb_alloc_named_color_reply_t *named_color;
1012 if((named_color = xcb_alloc_named_color_reply(globalconf.connection,
1013 req.cookie_named, NULL)))
1015 req.color->pixel = named_color->pixel;
1016 req.color->red = named_color->visual_red;
1017 req.color->green = named_color->visual_green;
1018 req.color->blue = named_color->visual_blue;
1019 req.color->alpha = req.alpha;
1020 req.color->initialized = true;
1021 p_delete(&named_color);
1022 return true;
1026 warn("awesome: error, cannot allocate color '%s'", req.colstr);
1027 return false;
1030 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80