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>
28 #include <gdk/gdkcairo.h>
29 #include <gdk-pixbuf/gdk-pixbuf.h>
33 #include <xcb/xcb_aux.h>
42 #include "common/tokenize.h"
43 #include "common/draw.h"
44 #include "common/markup.h"
45 #include "common/xutil.h"
48 draw_parser_data_init(draw_parser_data_t
*pdata
)
54 draw_parser_data_wipe(draw_parser_data_t
*pdata
)
57 draw_image_delete(&pdata
->bg_image
);
60 static iconv_t iso2utf8
= (iconv_t
) -1;
62 /** Convert text from any charset to UTF-8 using iconv.
63 * \param iso The ISO string to convert.
64 * \param len The string size.
65 * \return NULL if error, otherwise pointer to the new converted string.
68 draw_iso2utf8(const char *iso
, size_t len
)
76 if(!a_strcmp(nl_langinfo(CODESET
), "UTF-8"))
79 if(iso2utf8
== (iconv_t
) -1)
81 iso2utf8
= iconv_open("UTF-8", nl_langinfo(CODESET
));
82 if(iso2utf8
== (iconv_t
) -1)
85 warn("unable to convert text from %s to UTF-8, not available",
86 nl_langinfo(CODESET
));
88 warn("unable to convert text: %s", strerror(errno
));
94 utf8len
= 2 * len
+ 1;
95 utf8
= utf8p
= p_new(char, utf8len
);
97 if(iconv(iso2utf8
, (char **) &iso
, &len
, &utf8
, &utf8len
) == (size_t) -1)
99 warn("text conversion failed: %s", strerror(errno
));
106 static xcb_visualtype_t
*
107 draw_screen_default_visual(xcb_screen_t
*s
)
109 xcb_depth_iterator_t depth_iter
;
110 xcb_visualtype_iterator_t visual_iter
;
115 for(depth_iter
= xcb_screen_allowed_depths_iterator(s
);
116 depth_iter
.rem
; xcb_depth_next (&depth_iter
))
117 for(visual_iter
= xcb_depth_visuals_iterator (depth_iter
.data
);
118 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
119 if (s
->root_visual
== visual_iter
.data
->visual_id
)
120 return visual_iter
.data
;
125 /** Create a new draw context.
126 * \param conn Connection ref.
127 * \param phys_screen Physical screen id.
128 * \param width Width.
129 * \param height Height.
130 * \param px Pixmap object to store.
131 * \param fg Foreground color.
132 * \param bg Background color.
133 * \return A draw context pointer.
136 draw_context_new(xcb_connection_t
*conn
, int phys_screen
,
137 int width
, int height
, xcb_pixmap_t px
,
138 const xcolor_t
*fg
, const xcolor_t
*bg
)
140 draw_context_t
*d
= p_new(draw_context_t
, 1);
141 xcb_screen_t
*s
= xutil_screen_get(conn
, phys_screen
);
143 d
->connection
= conn
;
144 d
->phys_screen
= phys_screen
;
147 d
->depth
= s
->root_depth
;
148 d
->visual
= draw_screen_default_visual(s
);
150 d
->surface
= cairo_xcb_surface_create(conn
, px
, d
->visual
, width
, height
);
151 d
->cr
= cairo_create(d
->surface
);
152 d
->layout
= pango_cairo_create_layout(d
->cr
);
159 /** Create a new Pango font
160 * \param conn Connection ref
161 * \param phys_screen The physical screen number.
162 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE])
166 draw_font_new(xcb_connection_t
*conn
, int phys_screen
, const char *fontname
)
168 cairo_surface_t
*surface
;
169 xcb_screen_t
*s
= xutil_screen_get(conn
, phys_screen
);
172 font_t
*font
= p_new(font_t
, 1);
174 /* Create a dummy cairo surface, cairo context and pango layout in
175 * order to get font informations */
176 surface
= cairo_xcb_surface_create(conn
,
178 draw_screen_default_visual(s
),
180 s
->height_in_pixels
);
182 cr
= cairo_create(surface
);
183 layout
= pango_cairo_create_layout(cr
);
185 /* Get the font description used to set text on a PangoLayout */
186 font
->desc
= pango_font_description_from_string(fontname
);
187 pango_layout_set_font_description(layout
, font
->desc
);
190 pango_layout_get_pixel_size(layout
, NULL
, &font
->height
);
192 g_object_unref(layout
);
194 cairo_surface_destroy(surface
);
200 * \param font Font to delete.
203 draw_font_delete(font_t
**font
)
207 pango_font_description_free((*font
)->desc
);
213 draw_markup_on_element(markup_parser_data_t
*p
, const char *elem
,
214 const char **names
, const char **values
)
216 draw_parser_data_t
*data
= p
->priv
;
218 xcolor_init_request_t reqs
[3];
219 int8_t i
, bg_color_nbr
= -1, reqs_nbr
= -1;
221 /* hack: markup.c validates tags so we can avoid strcmps here */
224 if(elem
[1] == 'g') /* bg */
225 for(; *names
; names
++, values
++)
226 switch(a_tokenize(*names
, -1))
229 reqs
[++reqs_nbr
] = xcolor_init_unchecked(data
->connection
,
235 bg_color_nbr
= reqs_nbr
;
239 draw_image_delete(&data
->bg_image
);
240 data
->bg_image
= draw_image_new(*values
);
243 data
->bg_align
= draw_align_fromstr(*values
, -1);
246 data
->bg_resize
= a_strtobool(*values
, -1);
251 for(; *names
; names
++, values
++)
252 switch(a_tokenize(*names
, -1))
255 reqs
[++reqs_nbr
] = xcolor_init_unchecked(data
->connection
,
262 data
->border
.width
= atoi(*values
);
269 for(; *names
; names
++, values
++)
270 switch(a_tokenize(*names
, -1))
273 data
->align
= draw_align_fromstr(*values
, -1);
276 reqs
[++reqs_nbr
] = xcolor_init_unchecked(data
->connection
,
282 case A_TK_SHADOW_OFFSET
:
283 data
->shadow
.offset
= atoi(*values
);
289 case 'm': /* margin */
290 for (; *names
; names
++, values
++)
291 switch(a_tokenize(*names
, -1))
294 data
->margin
.left
= atoi(*values
);
297 data
->margin
.right
= atoi(*values
);
305 for(i
= 0; i
<= reqs_nbr
; i
++)
306 if(i
== bg_color_nbr
)
307 data
->has_bg_color
= xcolor_init_reply(data
->connection
, reqs
[i
]);
309 xcolor_init_reply(data
->connection
, reqs
[i
]);
313 draw_text_markup_expand(draw_parser_data_t
*data
,
314 const char *str
, ssize_t slen
)
316 static char const * const elements
[] = { "bg", "text", "margin", "border", NULL
};
317 markup_parser_data_t p
=
319 .elements
= elements
,
321 .on_element
= &draw_markup_on_element
,
324 GError
*error
= NULL
;
327 markup_parser_data_init(&p
);
329 if(!markup_parse(&p
, str
, slen
))
332 if(!pango_parse_markup(p
.text
.s
, p
.text
.len
, 0, &data
->attr_list
, &text
, NULL
, &error
))
337 data
->len
= a_strlen(text
);
341 markup_parser_data_wipe(&p
);
345 /** Draw text into a draw context.
346 * \param ctx Draw context to draw to.
347 * \param font The font to use.
348 * \param area Area to draw to.
349 * \param text Text to draw.
350 * \param len Text to draw length.
351 * \param data Optional parser data.
354 draw_text(draw_context_t
*ctx
, font_t
*font
,
355 area_t area
, const char *text
, ssize_t len
, draw_parser_data_t
*pdata
)
359 draw_parser_data_t parser_data
;
363 draw_parser_data_init(&parser_data
);
364 parser_data
.connection
= ctx
->connection
;
365 parser_data
.phys_screen
= ctx
->phys_screen
;
366 if(draw_text_markup_expand(&parser_data
, text
, len
))
368 text
= parser_data
.text
;
369 len
= parser_data
.len
;
371 pdata
= &parser_data
;
379 if(pdata
->has_bg_color
)
380 draw_rectangle(ctx
, area
, 1.0, true, &pdata
->bg_color
);
382 if(pdata
->border
.width
> 0)
383 draw_rectangle(ctx
, area
, pdata
->border
.width
, false, &pdata
->border
.color
);
389 switch(pdata
->bg_align
)
392 x
+= (area
.width
- pdata
->bg_image
->width
) / 2;
395 x
+= area
.width
- pdata
->bg_image
->width
;
400 draw_image(ctx
, x
, y
, pdata
->bg_resize
? area
.height
: 0, pdata
->bg_image
);
403 pango_layout_set_text(ctx
->layout
, pdata
->text
, pdata
->len
);
404 pango_layout_set_width(ctx
->layout
,
405 pango_units_from_double(area
.width
406 - (pdata
->margin
.left
407 + pdata
->margin
.right
)));
408 pango_layout_set_ellipsize(ctx
->layout
, PANGO_ELLIPSIZE_END
);
409 pango_layout_set_attributes(ctx
->layout
, pdata
->attr_list
);
410 pango_layout_set_font_description(ctx
->layout
, font
->desc
);
411 pango_layout_get_pixel_extents(ctx
->layout
, NULL
, &ext
);
413 x
= area
.x
+ pdata
->margin
.left
;
414 /* + 1 is added for rounding, so that in any case of doubt we rather draw
415 * the text 1px lower than too high which usually results in a better type
417 y
= area
.y
+ (ctx
->height
- font
->height
+ 1) / 2;
422 x
+= (area
.width
- ext
.width
) / 2;
425 x
+= area
.width
- ext
.width
;
431 if(pdata
->shadow
.offset
)
433 cairo_set_source_rgba(ctx
->cr
,
434 pdata
->shadow
.color
.red
/ 65535.0,
435 pdata
->shadow
.color
.green
/ 65535.0,
436 pdata
->shadow
.color
.blue
/ 65535.0,
437 pdata
->shadow
.color
.alpha
/ 65535.0);
438 cairo_move_to(ctx
->cr
, x
+ pdata
->shadow
.offset
, y
+ pdata
->shadow
.offset
);
439 pango_cairo_layout_path(ctx
->cr
, ctx
->layout
);
440 cairo_stroke(ctx
->cr
);
443 cairo_move_to(ctx
->cr
, x
, y
);
445 cairo_set_source_rgba(ctx
->cr
,
446 ctx
->fg
.red
/ 65535.0,
447 ctx
->fg
.green
/ 65535.0,
448 ctx
->fg
.blue
/ 65535.0,
449 ctx
->fg
.alpha
/ 65535.0);
450 pango_cairo_update_layout(ctx
->cr
, ctx
->layout
);
451 pango_cairo_show_layout(ctx
->cr
, ctx
->layout
);
453 if (pdata
== &parser_data
)
454 draw_parser_data_wipe(&parser_data
);
457 /** Setup color-source for cairo (gradient or mono).
458 * \param ctx Draw context.
459 * \param gradient_vector x, y to x + x_offset, y + y_offset.
460 * \param pcolor Color to use at start of gradient_vector.
461 * \param pcolor_center Color at center of gradient_vector.
462 * \param pcolor_end Color at end of gradient_vector.
463 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
465 static cairo_pattern_t
*
466 draw_setup_cairo_color_source(draw_context_t
*ctx
, vector_t gradient_vector
,
467 const xcolor_t
*pcolor
, const xcolor_t
*pcolor_center
,
468 const xcolor_t
*pcolor_end
)
470 cairo_pattern_t
*pat
= NULL
;
471 bool has_center
= pcolor_center
->initialized
;
472 bool has_end
= pcolor_end
->initialized
;
474 /* no need for a real pattern: */
475 if(!has_end
&& !has_center
)
476 cairo_set_source_rgba(ctx
->cr
,
477 pcolor
->red
/ 65535.0,
478 pcolor
->green
/ 65535.0,
479 pcolor
->blue
/ 65535.0,
480 pcolor
->alpha
/ 65535.0);
483 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
485 gradient_vector
.x
+ gradient_vector
.x_offset
,
486 gradient_vector
.y
+ gradient_vector
.y_offset
);
488 /* pcolor is always set (so far in awesome) */
489 cairo_pattern_add_color_stop_rgba(pat
, 0.0,
490 pcolor
->red
/ 65535.0,
491 pcolor
->green
/ 65535.0,
492 pcolor
->blue
/ 65535.0,
493 pcolor
->alpha
/ 65535.0);
496 cairo_pattern_add_color_stop_rgba(pat
, 0.5,
497 pcolor_center
->red
/ 65535.0,
498 pcolor_center
->green
/ 65535.0,
499 pcolor_center
->blue
/ 65535.0,
500 pcolor_center
->alpha
/ 65535.0);
503 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
504 pcolor_end
->red
/ 65535.0,
505 pcolor_end
->green
/ 65535.0,
506 pcolor_end
->blue
/ 65535.0,
507 pcolor_end
->alpha
/ 65535.0);
509 cairo_pattern_add_color_stop_rgba(pat
, 1.0,
510 pcolor
->red
/ 65535.0,
511 pcolor
->green
/ 65535.0,
512 pcolor
->blue
/ 65535.0,
513 pcolor
->alpha
/ 65535.0);
514 cairo_set_source(ctx
->cr
, pat
);
519 /** Draw rectangle inside the coordinates
520 * \param ctx Draw context
521 * \param geometry geometry
522 * \param line_width line width
523 * \param filled fill rectangle?
524 * \param color color to use
527 draw_rectangle(draw_context_t
*ctx
, area_t geometry
,
528 float line_width
, bool filled
, const xcolor_t
*color
)
530 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
531 cairo_set_line_width(ctx
->cr
, line_width
);
532 cairo_set_miter_limit(ctx
->cr
, 10.0);
533 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
534 cairo_set_source_rgba(ctx
->cr
,
535 color
->red
/ 65535.0,
536 color
->green
/ 65535.0,
537 color
->blue
/ 65535.0,
538 color
->alpha
/ 65535.0);
541 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
542 geometry
.width
, geometry
.height
);
547 cairo_rectangle(ctx
->cr
, geometry
.x
+ line_width
/ 2.0, geometry
.y
+ line_width
/ 2.0,
548 geometry
.width
- line_width
, geometry
.height
- line_width
);
549 cairo_stroke(ctx
->cr
);
553 /** Draw rectangle with gradient colors
554 * \param ctx Draw context.
555 * \param geometry Geometry.
556 * \param line_width Line width.
557 * \param filled Filled rectangle?
558 * \param gradient_vector Color-gradient course.
559 * \param pcolor Color at start of gradient_vector.
560 * \param pcolor_center Color in the center.
561 * \param pcolor_end Color at end of gradient_vector.
564 draw_rectangle_gradient(draw_context_t
*ctx
, area_t geometry
, float line_width
, bool filled
,
565 vector_t gradient_vector
, const xcolor_t
*pcolor
,
566 const xcolor_t
*pcolor_center
, const xcolor_t
*pcolor_end
)
568 cairo_pattern_t
*pat
;
570 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
571 cairo_set_line_width(ctx
->cr
, line_width
);
572 cairo_set_miter_limit(ctx
->cr
, 10.0);
573 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
575 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
579 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
584 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
585 cairo_stroke(ctx
->cr
);
589 cairo_pattern_destroy(pat
);
592 /** Setup some cairo-things for drawing a graph
593 * \param ctx Draw context
596 draw_graph_setup(draw_context_t
*ctx
)
598 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_NONE
);
599 cairo_set_line_width(ctx
->cr
, 1.0);
600 /* without it, it can draw over the path on sharp angles
601 * ...too long lines * (...graph_line) */
602 cairo_set_miter_limit(ctx
->cr
, 0.0);
603 cairo_set_line_join(ctx
->cr
, CAIRO_LINE_JOIN_MITER
);
607 * \param ctx Draw context.
608 * \param rect The area to draw into.
609 * \param from Array of starting-point offsets to draw a graph lines.
610 * \param to Array of end-point offsets to draw a graph lines.
611 * \param cur_index Current position in data-array (cycles around).
612 * \param grow Put new values to the left or to the right.
613 * \param gradient_vector Color-Gradient course.
614 * \param pcolor Color at start of gradient_vector.
615 * \param pcolor_center Color in the center.
616 * \param pcolor_end Color at end of gradient_vector.
619 draw_graph(draw_context_t
*ctx
, area_t rect
, int *from
, int *to
, int cur_index
,
620 position_t grow
, vector_t gradient_vector
, const xcolor_t
*pcolor
,
621 const xcolor_t
*pcolor_center
, const xcolor_t
*pcolor_end
)
624 float x
= rect
.x
+ 0.5; /* middle of a pixel */
625 cairo_pattern_t
*pat
;
627 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
,
628 pcolor
, pcolor_center
, pcolor_end
);
630 if(grow
== Right
) /* draw from right to left */
633 while(++i
< rect
.width
)
635 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
636 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
640 cur_index
= rect
.width
- 1;
643 else /* draw from left to right */
644 while(++i
< rect
.width
)
646 cairo_move_to(ctx
->cr
, x
, rect
.y
- from
[cur_index
]);
647 cairo_line_to(ctx
->cr
, x
, rect
.y
- to
[cur_index
]);
651 cur_index
= rect
.width
- 1;
654 cairo_stroke(ctx
->cr
);
657 cairo_pattern_destroy(pat
);
660 /** Draw a line into a graph-widget.
661 * \param ctx Draw context.
662 * \param rect The area to draw into.
663 * \param to array of offsets to draw the line through...
664 * \param cur_index current position in data-array (cycles around)
665 * \param grow put new values to the left or to the right
666 * \param gradient_vector Color-gradient course.
667 * \param pcolor Color at start of gradient_vector.
668 * \param pcolor_center Color in the center.
669 * \param pcolor_end Color at end of gradient_vector.
672 draw_graph_line(draw_context_t
*ctx
, area_t rect
, int *to
, int cur_index
,
673 position_t grow
, vector_t gradient_vector
, const xcolor_t
*pcolor
,
674 const xcolor_t
*pcolor_center
, const xcolor_t
*pcolor_end
)
678 cairo_pattern_t
*pat
;
680 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
681 * ... it won't fill some pixels! It also looks much nicer so.
682 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
683 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
684 cairo_set_antialias(ctx
->cr
, CAIRO_ANTIALIAS_SUBPIXEL
);
685 /* a nicer, better visible line compared to 1.0 */
686 cairo_set_line_width(ctx
->cr
, 1.25);
688 pat
= draw_setup_cairo_color_source(ctx
, gradient_vector
, pcolor
, pcolor_center
, pcolor_end
);
690 /* path through the centers of pixels */
697 /* go through the values from old to new. Begin with the oldest. */
698 if(++cur_index
> w
- 1)
701 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
704 /* on the left border: fills a pixel also when there's only one value */
705 cairo_move_to(ctx
->cr
, x
- 1.0, y
- to
[cur_index
]);
707 for(i
= 0; i
< w
; i
++)
709 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
712 /* cycles around the index */
715 if (++cur_index
> w
- 1)
725 /* onto the right border: fills a pixel also when there's only one value */
727 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
729 cairo_stroke(ctx
->cr
);
732 cairo_pattern_destroy(pat
);
733 /* reset line-width */
734 cairo_set_line_width(ctx
->cr
, 1.0);
737 /** Draw an image from ARGB data to a draw context.
738 * Data should be stored as an array of alpha, red, blue, green for each pixel
739 * and the array size should be w * h elements long.
740 * \param ctx Draw context to draw to.
741 * \param x X coordinate.
742 * \param y Y coordinate.
745 * \param wanted_h Wanted height: if > 0, image will be resized.
746 * \param data The image pixels array.
749 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
750 int wanted_h
, unsigned char *data
)
754 cairo_surface_t
*source
;
756 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
,
757 #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)
758 sizeof(unsigned char) * 4 * w
);
760 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
762 cr
= cairo_create(ctx
->surface
);
763 if(wanted_h
> 0 && h
> 0)
765 ratio
= (double) wanted_h
/ (double) h
;
766 cairo_scale(cr
, ratio
, ratio
);
767 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
770 cairo_set_source_surface(cr
, source
, x
, y
);
775 cairo_surface_destroy(source
);
780 /** Load an image from filename.
781 * \param filename The image file to load.
782 * \return A new image.
785 draw_image_new(const char *filename
)
787 draw_image_t
*image
= NULL
;
789 GError
*error
= NULL
;
793 if(!(pixbuf
= gdk_pixbuf_new_from_file(filename
,&error
)))
794 warn("cannot load image %s: %s", filename
, error
->message
);
797 image
= p_new(draw_image_t
, 1);
798 image
->data
= pixbuf
;
799 image
->width
= gdk_pixbuf_get_width(pixbuf
);
800 image
->height
= gdk_pixbuf_get_height(pixbuf
);
808 * \param image The image to delete.
811 draw_image_delete(draw_image_t
**image
)
815 gdk_pixbuf_unref((*image
)->data
);
820 /** Draw an image to a draw context.
821 * \param ctx Draw context to draw to.
822 * \param x x coordinate.
823 * \param y y coordinate.
824 * \param wanted_h Wanted height: if > 0, image will be resized.
825 * \param image The image to draw.
828 draw_image(draw_context_t
*ctx
, int x
, int y
, int wanted_h
, draw_image_t
*image
)
832 cr
= cairo_create(ctx
->surface
);
833 if(wanted_h
> 0 && image
->height
> 0)
835 double ratio
= (double) wanted_h
/ (double) image
->height
;
836 cairo_scale(cr
, ratio
, ratio
);
837 gdk_cairo_set_source_pixbuf(cr
, image
->data
, x
/ ratio
, y
/ ratio
);
840 gdk_cairo_set_source_pixbuf(cr
, image
->data
, x
, y
);
847 #else /* WITH_IMLIB2 */
850 draw_imlib_load_strerror(Imlib_Load_Error e
)
854 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST
:
855 return "no such file or directory";
856 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY
:
857 return "file is a directory";
858 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ
:
859 return "read permission denied";
860 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT
:
861 return "no loader for file format";
862 case IMLIB_LOAD_ERROR_PATH_TOO_LONG
:
863 return "path too long";
864 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT
:
865 return "path component non existant";
866 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY
:
867 return "path compoment not a directory";
868 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE
:
869 return "path points oustide address space";
870 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS
:
871 return "too many symbolic links";
872 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY
:
873 return "out of memory";
874 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS
:
875 return "out of file descriptors";
876 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE
:
877 return "write permission denied";
878 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE
:
879 return "out of disk space";
880 case IMLIB_LOAD_ERROR_UNKNOWN
:
881 return "unknown error, that's really bad";
882 case IMLIB_LOAD_ERROR_NONE
:
883 return "no error, oops";
886 return "unknown error";
890 /** Load an image from filename.
891 * \param filename The image file to load.
892 * \return A new image.
895 draw_image_new(const char *filename
)
900 unsigned char *dataimg
, *rdataimg
;
902 Imlib_Load_Error e
= IMLIB_LOAD_ERROR_NONE
;
908 if(!(imimage
= imlib_load_image_with_error_return(filename
, &e
)))
910 warn("cannot load image %s: %s", filename
, draw_imlib_load_strerror(e
));
914 imlib_context_set_image(imimage
);
916 w
= imlib_image_get_width();
917 h
= imlib_image_get_height();
921 data
= imlib_image_get_data_for_reading_only();
923 rdataimg
= dataimg
= p_new(unsigned char, size
* 4);
925 for(i
= 0; i
< size
; i
++, dataimg
+= 4)
927 dataimg
[3] = (data
[i
] >> 24) & 0xff; /* A */
928 /* cairo wants pre-multiplied alpha */
929 alpha
= dataimg
[3] / 255.0;
930 dataimg
[2] = ((data
[i
] >> 16) & 0xff) * alpha
; /* R */
931 dataimg
[1] = ((data
[i
] >> 8) & 0xff) * alpha
; /* G */
932 dataimg
[0] = (data
[i
] & 0xff) * alpha
; /* B */
935 image
= p_new(draw_image_t
, 1);
937 image
->data
= rdataimg
;
947 * \param image The image to delete.
950 draw_image_delete(draw_image_t
**image
)
954 p_delete(&(*image
)->data
);
959 /** Draw an image to a draw context.
960 * \param ctx Draw context to draw to.
961 * \param x X coordinate.
962 * \param y Y coordinate.
963 * \param wanted_h Wanted height: if > 0, image will be resized.
964 * \param image The image to draw.
967 draw_image(draw_context_t
*ctx
, int x
, int y
, int wanted_h
, draw_image_t
*image
)
969 draw_image_from_argb_data(ctx
, x
, y
, image
->width
, image
->height
, wanted_h
, image
->data
);
972 #endif /* WITH_IMLIB2 */
975 * \param ctx Draw context to draw with.
976 * \param src Drawable to draw from.
977 * \param dest Drawable to draw to.
978 * \param src_w Drawable width.
979 * \param src_h Drawable height.
980 * \param dest_w Drawable width.
981 * \param dest_h Drawable height.
982 * \param angle angle to rotate.
983 * \param tx Translate to this x coordinate.
984 * \param ty Translate to this y coordinate.
987 draw_rotate(draw_context_t
*ctx
,
988 xcb_pixmap_t src
, xcb_pixmap_t dest
,
989 int src_w
, int src_h
,
990 int dest_w
, int dest_h
,
991 double angle
, int tx
, int ty
)
993 cairo_surface_t
*surface
, *source
;
996 surface
= cairo_xcb_surface_create(ctx
->connection
, dest
,
997 ctx
->visual
, dest_w
, dest_h
);
998 source
= cairo_xcb_surface_create(ctx
->connection
, src
,
999 ctx
->visual
, src_w
, src_h
);
1000 cr
= cairo_create (surface
);
1002 cairo_translate(cr
, tx
, ty
);
1003 cairo_rotate(cr
, angle
);
1005 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
1009 cairo_surface_destroy(source
);
1010 cairo_surface_destroy(surface
);
1013 /** Return the width and height of a text in pixel.
1014 * \param conn Connection ref.
1015 * \param phys_screen Physical screen number.
1016 * \param font Font to use.
1017 * \param text The text.
1018 * \param len The text length.
1019 * \param pdata The parser data to fill.
1020 * \return Text height and width.
1023 draw_text_extents(xcb_connection_t
*conn
, int phys_screen
, font_t
*font
,
1024 const char *text
, ssize_t len
, draw_parser_data_t
*parser_data
)
1026 cairo_surface_t
*surface
;
1028 PangoLayout
*layout
;
1030 xcb_screen_t
*s
= xutil_screen_get(conn
, phys_screen
);
1031 area_t geom
= { 0, 0, 0, 0 };
1036 parser_data
->connection
= conn
;
1037 parser_data
->phys_screen
= phys_screen
;
1038 if(draw_text_markup_expand(parser_data
, text
, len
))
1040 text
= parser_data
->text
;
1041 len
= parser_data
->len
;
1044 surface
= cairo_xcb_surface_create(conn
, phys_screen
,
1045 draw_screen_default_visual(s
),
1047 s
->height_in_pixels
);
1049 cr
= cairo_create(surface
);
1050 layout
= pango_cairo_create_layout(cr
);
1051 pango_layout_set_text(layout
, parser_data
->text
, parser_data
->len
);
1052 pango_layout_set_attributes(layout
, parser_data
->attr_list
);
1053 pango_layout_set_font_description(layout
, font
->desc
);
1054 pango_layout_get_pixel_extents(layout
, NULL
, &ext
);
1055 g_object_unref(layout
);
1057 cairo_surface_destroy(surface
);
1059 geom
.width
= ext
.width
;
1060 geom
.height
= ext
.height
* 1.5;
1065 /** Transform a string to a alignment_t type.
1066 * Recognized string are left, center or right. Everything else will be
1067 * recognized as AlignAuto.
1068 * \param align Atring with align text.
1069 * \param len The string length.
1070 * \return An alignment_t type.
1073 draw_align_fromstr(const char *align
, ssize_t len
)
1075 switch (a_tokenize(align
, len
))
1077 case A_TK_LEFT
: return AlignLeft
;
1078 case A_TK_CENTER
: return AlignCenter
;
1079 case A_TK_RIGHT
: return AlignRight
;
1080 case A_TK_FLEX
: return AlignFlex
;
1081 default: return AlignAuto
;
1085 /** Transform an alignment to a string.
1086 * \param a The alignment.
1087 * \return A string which must not be freed.
1090 draw_align_tostr(alignment_t a
)
1094 case AlignLeft
: return "left";
1095 case AlignCenter
: return "center";
1096 case AlignRight
: return "right";
1097 case AlignFlex
: return "flex";
1098 case AlignAuto
: return "auto";
1099 default: return NULL
;
1103 #define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
1105 /** Send a request to initialize a X color.
1106 * \param conn Connection ref.
1107 * \param color xcolor_t struct to store color into.
1108 * \param phys_screen Physical screen number.
1109 * \param colstr Color specification.
1110 * \return request informations.
1112 xcolor_init_request_t
1113 xcolor_init_unchecked(xcb_connection_t
*conn
, xcolor_t
*color
, int phys_screen
,
1114 const char *colstr
, ssize_t len
)
1116 xcb_screen_t
*s
= xutil_screen_get(conn
, phys_screen
);
1117 xcolor_init_request_t req
;
1118 unsigned long colnum
;
1119 uint16_t red
, green
, blue
;
1125 req
.has_error
= true;
1132 /* The color is given in RGB value */
1133 if(colstr
[0] == '#')
1139 colnum
= strtoul(colstr
+ 1, &p
, 16);
1146 colnum
= strtoul(colstr
+ 1, &p
, 16);
1149 req
.alpha
= RGB_COLOR_8_TO_16(colnum
);
1155 warn("awesome: error, invalid color '%s'", colstr
);
1156 req
.has_error
= true;
1160 red
= RGB_COLOR_8_TO_16(colnum
>> 16);
1161 green
= RGB_COLOR_8_TO_16(colnum
>> 8);
1162 blue
= RGB_COLOR_8_TO_16(colnum
);
1165 req
.cookie_hexa
= xcb_alloc_color_unchecked(conn
, s
->default_colormap
,
1170 req
.is_hexa
= false;
1171 req
.cookie_named
= xcb_alloc_named_color_unchecked(conn
, s
->default_colormap
, len
,
1175 req
.has_error
= false;
1176 req
.colstr
= colstr
;
1181 /** Initialize a X color.
1182 * \param conn Connection ref.
1183 * \param req xcolor_init request.
1184 * \return True if color allocation was successfull.
1187 xcolor_init_reply(xcb_connection_t
*conn
,
1188 xcolor_init_request_t req
)
1195 xcb_alloc_color_reply_t
*hexa_color
;
1197 if((hexa_color
= xcb_alloc_color_reply(conn
, req
.cookie_hexa
, NULL
)))
1199 req
.color
->pixel
= hexa_color
->pixel
;
1200 req
.color
->red
= hexa_color
->red
;
1201 req
.color
->green
= hexa_color
->green
;
1202 req
.color
->blue
= hexa_color
->blue
;
1203 req
.color
->alpha
= req
.alpha
;
1204 req
.color
->initialized
= true;
1205 p_delete(&hexa_color
);
1211 xcb_alloc_named_color_reply_t
*named_color
;
1213 if((named_color
= xcb_alloc_named_color_reply(conn
, req
.cookie_named
, NULL
)))
1215 req
.color
->pixel
= named_color
->pixel
;
1216 req
.color
->red
= named_color
->visual_red
;
1217 req
.color
->green
= named_color
->visual_green
;
1218 req
.color
->blue
= named_color
->visual_blue
;
1219 req
.color
->alpha
= req
.alpha
;
1220 req
.color
->initialized
= true;
1221 p_delete(&named_color
);
1226 warn("awesome: error, cannot allocate color '%s'", req
.colstr
);
1230 /** Remove a area from a list of them,
1231 * spliting the space between several area that can overlap
1232 * \param areas Array of areas.
1233 * \param elem Area to remove.
1236 area_array_remove(area_array_t
*areas
, area_t elem
)
1238 /* loop from the end because:
1239 * (1) we remove elements ;
1240 * (2) the one we add to the end are okay wrt the invariants
1242 for(int i
= areas
->len
- 1; i
>= 0; i
--)
1243 if(area_intersect_area(areas
->tab
[i
], elem
))
1245 /* remove it from the list */
1246 area_t r
= area_array_take(areas
, i
);
1247 area_t inter
= area_get_intersect_area(r
, elem
);
1249 if(AREA_LEFT(inter
) > AREA_LEFT(r
))
1255 .width
= AREA_LEFT(inter
) - AREA_LEFT(r
),
1258 area_array_append(areas
, extra
);
1261 if(AREA_TOP(inter
) > AREA_TOP(r
))
1268 .height
= AREA_TOP(inter
) - AREA_LEFT(r
),
1270 area_array_append(areas
, extra
);
1273 if(AREA_RIGHT(inter
) < AREA_RIGHT(r
))
1277 .x
= AREA_RIGHT(inter
),
1279 .width
= AREA_RIGHT(r
) - AREA_RIGHT(inter
),
1282 area_array_append(areas
, extra
);
1285 if(AREA_BOTTOM(inter
) < AREA_BOTTOM(r
))
1290 .y
= AREA_BOTTOM(inter
),
1292 .height
= AREA_BOTTOM(r
) - AREA_BOTTOM(inter
),
1294 area_array_append(areas
, extra
);
1299 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80