Move wmsetbg as a last option (FS#737)
[awesome.git] / draw.c
blob48ff00b00f847697d085a3b4ebe3c701bdc4c564
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 "globalconf.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 /** Initialize a draw_text_context_t with text data.
93 * \param data The draw text context to init.
94 * \param str The text string to render.
95 * \param slen The text string length.
96 * \return True if everything is ok, false otherwise.
98 bool
99 draw_text_context_init(draw_text_context_t *data, const char *str, ssize_t slen)
101 GError *error = NULL;
103 if(!str)
104 return false;
106 if(!pango_parse_markup(str, slen, 0, &data->attr_list, &data->text, NULL, &error))
108 warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
109 if(error)
110 g_error_free(error);
111 return false;
114 data->len = a_strlen(data->text);
116 return true;
119 /** Initialize a new draw context.
120 * \param d The draw context to initialize.
121 * \param phys_screen Physical screen id.
122 * \param width Width.
123 * \param height Height.
124 * \param px Pixmap object to store.
125 * \param fg Foreground color.
126 * \param bg Background color.
128 void
129 draw_context_init(draw_context_t *d, int phys_screen,
130 int width, int height, xcb_pixmap_t px,
131 const xcolor_t *fg, const xcolor_t *bg)
133 d->phys_screen = phys_screen;
134 d->width = width;
135 d->height = height;
136 d->pixmap = px;
137 d->surface = cairo_xcb_surface_create(globalconf.connection,
138 px, globalconf.screens.tab[phys_screen].visual,
139 width, height);
140 d->cr = cairo_create(d->surface);
141 d->layout = pango_cairo_create_layout(d->cr);
142 d->fg = *fg;
143 d->bg = *bg;
146 /** Draw text into a draw context.
147 * \param ctx Draw context to draw to.
148 * \param data Draw text context data.
149 * \param ellip Ellipsize mode.
150 * \param wrap Wrap mode.
151 * \param align Text alignment.
152 * \param valign Vertical text alignment.
153 * \param area Area to draw to.
155 void
156 draw_text(draw_context_t *ctx, draw_text_context_t *data,
157 PangoEllipsizeMode ellip, PangoWrapMode wrap,
158 alignment_t align, alignment_t valign, area_t area)
160 pango_layout_set_text(ctx->layout, data->text, data->len);
161 pango_layout_set_width(ctx->layout,
162 pango_units_from_double(area.width));
163 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
164 pango_layout_set_ellipsize(ctx->layout, ellip);
165 pango_layout_set_wrap(ctx->layout, wrap);
166 pango_layout_set_attributes(ctx->layout, data->attr_list);
167 pango_layout_set_font_description(ctx->layout, globalconf.font->desc);
169 PangoRectangle ext;
170 pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
172 /* Not enough space, draw nothing */
173 if(ext.width > area.width || ext.height > area.height)
174 return;
176 switch(align)
178 case AlignCenter:
179 area.x += (area.width - ext.width) / 2;
180 break;
181 case AlignRight:
182 area.x += area.width - ext.width;
183 break;
184 default:
185 break;
188 switch(valign)
190 case AlignCenter:
191 area.y += (area.height - ext.height) / 2;
192 break;
193 case AlignBottom:
194 area.y += area.height - ext.height;
195 break;
196 default:
197 break;
200 cairo_move_to(ctx->cr, area.x, area.y);
202 cairo_set_source_rgba(ctx->cr,
203 ctx->fg.red / 65535.0,
204 ctx->fg.green / 65535.0,
205 ctx->fg.blue / 65535.0,
206 ctx->fg.alpha / 65535.0);
207 pango_cairo_update_layout(ctx->cr, ctx->layout);
208 pango_cairo_show_layout(ctx->cr, ctx->layout);
211 /** Setup color-source for cairo (gradient or mono).
212 * \param ctx Draw context.
213 * \param gradient_vector x, y to x + x_offset, y + y_offset.
214 * \param pcolor Color to use at start of gradient_vector.
215 * \param pcolor_center Color at center of gradient_vector.
216 * \param pcolor_end Color at end of gradient_vector.
217 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
219 static cairo_pattern_t *
220 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
221 const color_t *pcolor, const color_t *pcolor_center,
222 const color_t *pcolor_end)
224 cairo_pattern_t *pat = NULL;
225 bool has_center = pcolor_center->initialized;
226 bool has_end = pcolor_end->initialized;
228 /* no need for a real pattern: */
229 if(!has_end && !has_center)
230 cairo_set_source_rgba(ctx->cr,
231 pcolor->red / 255.0,
232 pcolor->green / 255.0,
233 pcolor->blue / 255.0,
234 pcolor->alpha / 255.0);
235 else
237 pat = cairo_pattern_create_linear(gradient_vector.x,
238 gradient_vector.y,
239 gradient_vector.x + gradient_vector.x_offset,
240 gradient_vector.y + gradient_vector.y_offset);
242 /* pcolor is always set (so far in awesome) */
243 cairo_pattern_add_color_stop_rgba(pat, 0.0,
244 pcolor->red / 255.0,
245 pcolor->green / 255.0,
246 pcolor->blue / 255.0,
247 pcolor->alpha / 255.0);
249 if(has_center)
250 cairo_pattern_add_color_stop_rgba(pat, 0.5,
251 pcolor_center->red / 255.0,
252 pcolor_center->green / 255.0,
253 pcolor_center->blue / 255.0,
254 pcolor_center->alpha / 255.0);
256 if(has_end)
257 cairo_pattern_add_color_stop_rgba(pat, 1.0,
258 pcolor_end->red / 255.0,
259 pcolor_end->green / 255.0,
260 pcolor_end->blue / 255.0,
261 pcolor_end->alpha / 255.0);
262 else
263 cairo_pattern_add_color_stop_rgba(pat, 1.0,
264 pcolor->red / 255.0,
265 pcolor->green / 255.0,
266 pcolor->blue / 255.0,
267 pcolor->alpha / 255.0);
268 cairo_set_source(ctx->cr, pat);
270 return pat;
273 /** Draw rectangle inside the coordinates
274 * \param ctx Draw context
275 * \param geometry geometry
276 * \param line_width line width
277 * \param filled fill rectangle?
278 * \param color color to use
280 void
281 draw_rectangle(draw_context_t *ctx, area_t geometry,
282 float line_width, bool filled, const color_t *color)
284 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
285 cairo_set_line_width(ctx->cr, line_width);
286 cairo_set_miter_limit(ctx->cr, 10.0);
287 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
288 cairo_set_source_rgba(ctx->cr,
289 color->red / 255.0,
290 color->green / 255.0,
291 color->blue / 255.0,
292 color->alpha / 255.0);
293 if(filled)
295 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
296 geometry.width, geometry.height);
297 cairo_fill(ctx->cr);
299 else
301 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
302 geometry.width - line_width, geometry.height - line_width);
303 cairo_stroke(ctx->cr);
307 /** Draw rectangle with gradient colors
308 * \param ctx Draw context.
309 * \param geometry Geometry.
310 * \param line_width Line width.
311 * \param filled Filled rectangle?
312 * \param gradient_vector Color-gradient course.
313 * \param pcolor Color at start of gradient_vector.
314 * \param pcolor_center Color in the center.
315 * \param pcolor_end Color at end of gradient_vector.
317 void
318 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
319 vector_t gradient_vector, const color_t *pcolor,
320 const color_t *pcolor_center, const color_t *pcolor_end)
322 cairo_pattern_t *pat;
324 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
325 cairo_set_line_width(ctx->cr, line_width);
326 cairo_set_miter_limit(ctx->cr, 10.0);
327 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
329 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
331 if(filled)
333 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
334 cairo_fill(ctx->cr);
336 else
338 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
339 cairo_stroke(ctx->cr);
342 if(pat)
343 cairo_pattern_destroy(pat);
346 /** Setup some cairo-things for drawing a graph
347 * \param ctx Draw context
349 void
350 draw_graph_setup(draw_context_t *ctx)
352 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
353 cairo_set_line_width(ctx->cr, 1.0);
354 /* without it, it can draw over the path on sharp angles
355 * ...too long lines * (...graph_line) */
356 cairo_set_miter_limit(ctx->cr, 0.0);
357 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
360 /** Draw a graph.
361 * \param ctx Draw context.
362 * \param rect The area to draw into.
363 * \param from Array of starting-point offsets to draw a graph lines.
364 * \param to Array of end-point offsets to draw a graph lines.
365 * \param cur_index Current position in data-array (cycles around).
366 * \param grow Put new values to the left or to the right.
367 * \param gradient_vector Color-Gradient course.
368 * \param pcolor Color at start of gradient_vector.
369 * \param pcolor_center Color in the center.
370 * \param pcolor_end Color at end of gradient_vector.
372 void
373 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
374 position_t grow, vector_t gradient_vector, const color_t *pcolor,
375 const color_t *pcolor_center, const color_t *pcolor_end)
377 int i = -1;
378 float x = rect.x + 0.5; /* middle of a pixel */
379 cairo_pattern_t *pat;
381 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
382 pcolor, pcolor_center, pcolor_end);
384 if(grow == Right) /* draw from right to left */
386 x += rect.width - 1;
387 while(++i < rect.width)
389 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
390 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
391 x -= 1.0;
393 if(--cur_index < 0)
394 cur_index = rect.width - 1;
397 else /* draw from left to right */
398 while(++i < rect.width)
400 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
401 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
402 x += 1.0;
404 if(--cur_index < 0)
405 cur_index = rect.width - 1;
408 cairo_stroke(ctx->cr);
410 if(pat)
411 cairo_pattern_destroy(pat);
414 /** Draw a line into a graph-widget.
415 * \param ctx Draw context.
416 * \param rect The area to draw into.
417 * \param to array of offsets to draw the line through...
418 * \param cur_index current position in data-array (cycles around)
419 * \param grow put new values to the left or to the right
420 * \param gradient_vector Color-gradient course.
421 * \param pcolor Color at start of gradient_vector.
422 * \param pcolor_center Color in the center.
423 * \param pcolor_end Color at end of gradient_vector.
425 void
426 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
427 position_t grow, vector_t gradient_vector, const color_t *pcolor,
428 const color_t *pcolor_center, const color_t *pcolor_end)
430 int i, w;
431 float x, y;
432 cairo_pattern_t *pat;
434 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
435 * ... it won't fill some pixels! It also looks much nicer so.
436 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
437 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
438 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
439 /* a nicer, better visible line compared to 1.0 */
440 cairo_set_line_width(ctx->cr, 1.25);
442 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
444 /* path through the centers of pixels */
445 x = rect.x + 0.5;
446 y = rect.y + 0.5;
447 w = rect.width;
449 if(grow == Right)
451 /* go through the values from old to new. Begin with the oldest. */
452 if(++cur_index > w - 1)
453 cur_index = 0;
455 cairo_move_to(ctx->cr, x, y - to[cur_index]);
457 else
458 /* on the left border: fills a pixel also when there's only one value */
459 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
461 for(i = 0; i < w; i++)
463 cairo_line_to(ctx->cr, x, y - to[cur_index]);
464 x += 1.0;
466 /* cycles around the index */
467 if(grow == Right)
469 if(++cur_index > w - 1)
470 cur_index = 0;
472 else
474 if(--cur_index < 0)
475 cur_index = w - 1;
479 /* onto the right border: fills a pixel also when there's only one value */
480 if(grow == Right)
481 cairo_line_to(ctx->cr, x, y - to[(cur_index + (w - 1)) % w]);
483 cairo_stroke(ctx->cr);
485 if(pat)
486 cairo_pattern_destroy(pat);
487 /* reset line-width */
488 cairo_set_line_width(ctx->cr, 1.0);
491 /** Draw an image from ARGB data to a draw context.
492 * Data should be stored as an array of alpha, red, blue, green for each pixel
493 * and the array size should be w * h elements long.
494 * \param ctx Draw context to draw to.
495 * \param x X coordinate.
496 * \param y Y coordinate.
497 * \param w Width.
498 * \param h Height.
499 * \param ratio The ratio to apply to the image.
500 * \param data The image pixels array.
502 static void
503 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
504 double ratio, unsigned char *data)
506 cairo_t *cr;
507 cairo_surface_t *source;
509 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
510 #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)
511 sizeof(unsigned char) * 4 * w);
512 #else
513 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
514 #endif
515 cr = cairo_create(ctx->surface);
516 cairo_scale(cr, ratio, ratio);
517 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
519 cairo_paint(cr);
521 cairo_destroy(cr);
522 cairo_surface_destroy(source);
525 /** Draw an image to a draw context.
526 * \param ctx Draw context to draw to.
527 * \param x X coordinate.
528 * \param y Y coordinate.
529 * \param ratio The ratio to apply to the image.
530 * \param image The image to draw.
532 void
533 draw_image(draw_context_t *ctx, int x, int y, double ratio, image_t *image)
535 draw_image_from_argb_data(ctx, x, y, image_getwidth(image), image_getheight(image), ratio, image_getdata(image));
538 /** Rotate a pixmap.
539 * \param ctx Draw context to draw with.
540 * \param src Drawable to draw from.
541 * \param dest Drawable to draw to.
542 * \param src_w Drawable width.
543 * \param src_h Drawable height.
544 * \param dest_w Drawable width.
545 * \param dest_h Drawable height.
546 * \param angle angle to rotate.
547 * \param tx Translate to this x coordinate.
548 * \param ty Translate to this y coordinate.
550 void
551 draw_rotate(draw_context_t *ctx,
552 xcb_pixmap_t src, xcb_pixmap_t dest,
553 int src_w, int src_h,
554 int dest_w, int dest_h,
555 double angle, int tx, int ty)
557 cairo_surface_t *surface, *source;
558 cairo_t *cr;
560 surface = cairo_xcb_surface_create(globalconf.connection, dest,
561 globalconf.screens.tab[ctx->phys_screen].visual,
562 dest_w, dest_h);
563 source = cairo_xcb_surface_create(globalconf.connection, src,
564 globalconf.screens.tab[ctx->phys_screen].visual,
565 src_w, src_h);
566 cr = cairo_create (surface);
568 cairo_translate(cr, tx, ty);
569 cairo_rotate(cr, angle);
571 cairo_set_source_surface(cr, source, 0.0, 0.0);
572 cairo_paint(cr);
574 cairo_destroy(cr);
575 cairo_surface_destroy(source);
576 cairo_surface_destroy(surface);
579 /** Return the width and height of a text in pixel.
580 * \param data The draw context text data.
581 * \return Text height and width.
583 area_t
584 draw_text_extents(draw_text_context_t *data)
586 cairo_surface_t *surface;
587 cairo_t *cr;
588 PangoLayout *layout;
589 PangoRectangle ext;
590 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
591 area_t geom = { 0, 0, 0, 0 };
593 if(data->len <= 0)
594 return geom;
596 surface = cairo_xcb_surface_create(globalconf.connection,
597 globalconf.default_screen,
598 globalconf.screens.tab[0].visual,
599 s->width_in_pixels,
600 s->height_in_pixels);
602 cr = cairo_create(surface);
603 layout = pango_cairo_create_layout(cr);
604 pango_layout_set_text(layout, data->text, data->len);
605 pango_layout_set_attributes(layout, data->attr_list);
606 pango_layout_set_font_description(layout, globalconf.font->desc);
607 pango_layout_get_pixel_extents(layout, NULL, &ext);
608 g_object_unref(layout);
609 cairo_destroy(cr);
610 cairo_surface_destroy(surface);
612 geom.width = ext.width;
613 geom.height = ext.height;
615 return geom;
618 /** Transform a string to a alignment_t type.
619 * Recognized string are flex, fixed, left, center, middle or right.
620 * \param align A string with align text.
621 * \param len The string length.
622 * \return An alignment_t type.
624 alignment_t
625 draw_align_fromstr(const char *align, ssize_t len)
627 switch(a_tokenize(align, len))
629 case A_TK_CENTER: return AlignCenter;
630 case A_TK_RIGHT: return AlignRight;
631 case A_TK_TOP: return AlignTop;
632 case A_TK_BOTTOM: return AlignBottom;
633 case A_TK_MIDDLE: return AlignMiddle;
634 default: return AlignLeft;
638 /** Transform an alignment to a string.
639 * \param a The alignment.
640 * \return A string which must not be freed.
642 const char *
643 draw_align_tostr(alignment_t a)
645 switch(a)
647 case AlignLeft: return "left";
648 case AlignCenter: return "center";
649 case AlignRight: return "right";
650 case AlignBottom: return "bottom";
651 case AlignTop: return "top";
652 case AlignMiddle: return "middle";
653 default: return NULL;
657 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80