awful.client: use property for floating registering
[awesome.git] / draw.c
blob6b0764455d0440670286980c7bbfeb2e6edf9d1c
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 <ctype.h>
30 #include <math.h>
32 #include "structs.h"
34 #include "common/tokenize.h"
36 extern awesome_t globalconf;
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);
85 if(dlen)
86 *dlen = orig_utf8len - utf8len;
88 return true;
91 static xcb_visualtype_t *
92 draw_screen_default_visual(xcb_screen_t *s)
94 xcb_depth_iterator_t depth_iter;
95 xcb_visualtype_iterator_t visual_iter;
97 if(!s)
98 return NULL;
100 for(depth_iter = xcb_screen_allowed_depths_iterator(s);
101 depth_iter.rem; xcb_depth_next (&depth_iter))
102 for(visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
103 visual_iter.rem; xcb_visualtype_next (&visual_iter))
104 if (s->root_visual == visual_iter.data->visual_id)
105 return visual_iter.data;
107 return NULL;
110 /** Create a new Pango font.
111 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
112 * \return A new font.
114 font_t *
115 draw_font_new(const char *fontname)
117 cairo_surface_t *surface;
118 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
119 cairo_t *cr;
120 PangoLayout *layout;
121 font_t *font = p_new(font_t, 1);
123 /* Create a dummy cairo surface, cairo context and pango layout in
124 * order to get font informations */
125 surface = cairo_xcb_surface_create(globalconf.connection,
126 globalconf.default_screen,
127 draw_screen_default_visual(s),
128 s->width_in_pixels,
129 s->height_in_pixels);
131 cr = cairo_create(surface);
132 layout = pango_cairo_create_layout(cr);
134 /* Get the font description used to set text on a PangoLayout */
135 font->desc = pango_font_description_from_string(fontname);
136 pango_layout_set_font_description(layout, font->desc);
138 /* Get height */
139 pango_layout_get_pixel_size(layout, NULL, &font->height);
141 g_object_unref(layout);
142 cairo_destroy(cr);
143 cairo_surface_destroy(surface);
145 return font;
148 /** Delete a font.
149 * \param font Font to delete.
151 void
152 draw_font_delete(font_t **font)
154 if(*font)
156 pango_font_description_free((*font)->desc);
157 p_delete(font);
161 /** Initialize a draw_text_context_t with text data.
162 * \param data The draw text context to init.
163 * \param str The text string to render.
164 * \param slen The text string length.
165 * \return True if everything is ok, false otherwise.
167 bool
168 draw_text_context_init(draw_text_context_t *data, const char *str, ssize_t slen)
170 GError *error = NULL;
172 if(!pango_parse_markup(str, slen, 0, &data->attr_list, &data->text, NULL, &error))
174 warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
175 if(error)
176 g_error_free(error);
177 return false;
180 data->len = a_strlen(data->text);
182 return true;
185 /** Initialize a new draw context.
186 * \param d The draw context to initialize.
187 * \param phys_screen Physical screen id.
188 * \param width Width.
189 * \param height Height.
190 * \param px Pixmap object to store.
191 * \param fg Foreground color.
192 * \param bg Background color.
194 void
195 draw_context_init(draw_context_t *d, int phys_screen,
196 int width, int height, xcb_pixmap_t px,
197 const xcolor_t *fg, const xcolor_t *bg)
199 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
201 d->phys_screen = phys_screen;
202 d->width = width;
203 d->height = height;
204 d->depth = s->root_depth;
205 d->visual = draw_screen_default_visual(s);
206 d->pixmap = px;
207 d->surface = cairo_xcb_surface_create(globalconf.connection, px, d->visual, width, height);
208 d->cr = cairo_create(d->surface);
209 d->layout = pango_cairo_create_layout(d->cr);
210 d->fg = *fg;
211 d->bg = *bg;
214 /** Draw text into a draw context.
215 * \param ctx Draw context to draw to.
216 * \param data Draw text context data.
217 * \param elip Ellipsize mode.
218 * \param wrap Wrap mode.
219 * \param align Text alignment.
220 * \param margin Margin to respect when drawing text.
221 * \param area Area to draw to.
222 * \param ext Text extents.
224 void
225 draw_text(draw_context_t *ctx, draw_text_context_t *data,
226 PangoEllipsizeMode ellip, PangoWrapMode wrap,
227 alignment_t align, padding_t *margin, area_t area, area_t *ext)
229 int x, y;
231 pango_layout_set_text(ctx->layout, data->text, data->len);
232 pango_layout_set_width(ctx->layout,
233 pango_units_from_double(area.width
234 - (margin->left
235 + margin->right)));
236 pango_layout_set_height(ctx->layout, pango_units_from_double(area.height)
237 - (margin->top + margin->bottom));
238 pango_layout_set_ellipsize(ctx->layout, ellip);
239 pango_layout_set_wrap(ctx->layout, wrap);
240 pango_layout_set_attributes(ctx->layout, data->attr_list);
241 pango_layout_set_font_description(ctx->layout, globalconf.font->desc);
243 x = area.x + margin->left;
244 /* + 1 is added for rounding, so that in any case of doubt we rather draw
245 * the text 1px lower than too high which usually results in a better type
246 * face */
247 y = area.y + (ctx->height - ext->height + 1) / 2 + margin->top;
249 /* only honors alignment if enough space */
250 if(ext->width < area.width)
251 switch(align)
253 case AlignCenter:
254 x += (area.width - ext->width) / 2;
255 break;
256 case AlignRight:
257 x += area.width - ext->width;
258 break;
259 default:
260 break;
263 cairo_move_to(ctx->cr, x, y);
265 cairo_set_source_rgba(ctx->cr,
266 ctx->fg.red / 65535.0,
267 ctx->fg.green / 65535.0,
268 ctx->fg.blue / 65535.0,
269 ctx->fg.alpha / 65535.0);
270 pango_cairo_update_layout(ctx->cr, ctx->layout);
271 pango_cairo_show_layout(ctx->cr, ctx->layout);
274 /** Setup color-source for cairo (gradient or mono).
275 * \param ctx Draw context.
276 * \param gradient_vector x, y to x + x_offset, y + y_offset.
277 * \param pcolor Color to use at start of gradient_vector.
278 * \param pcolor_center Color at center of gradient_vector.
279 * \param pcolor_end Color at end of gradient_vector.
280 * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
282 static cairo_pattern_t *
283 draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
284 const xcolor_t *pcolor, const xcolor_t *pcolor_center,
285 const xcolor_t *pcolor_end)
287 cairo_pattern_t *pat = NULL;
288 bool has_center = pcolor_center->initialized;
289 bool has_end = pcolor_end->initialized;
291 /* no need for a real pattern: */
292 if(!has_end && !has_center)
293 cairo_set_source_rgba(ctx->cr,
294 pcolor->red / 65535.0,
295 pcolor->green / 65535.0,
296 pcolor->blue / 65535.0,
297 pcolor->alpha / 65535.0);
298 else
300 pat = cairo_pattern_create_linear(gradient_vector.x,
301 gradient_vector.y,
302 gradient_vector.x + gradient_vector.x_offset,
303 gradient_vector.y + gradient_vector.y_offset);
305 /* pcolor is always set (so far in awesome) */
306 cairo_pattern_add_color_stop_rgba(pat, 0.0,
307 pcolor->red / 65535.0,
308 pcolor->green / 65535.0,
309 pcolor->blue / 65535.0,
310 pcolor->alpha / 65535.0);
312 if(has_center)
313 cairo_pattern_add_color_stop_rgba(pat, 0.5,
314 pcolor_center->red / 65535.0,
315 pcolor_center->green / 65535.0,
316 pcolor_center->blue / 65535.0,
317 pcolor_center->alpha / 65535.0);
319 if(has_end)
320 cairo_pattern_add_color_stop_rgba(pat, 1.0,
321 pcolor_end->red / 65535.0,
322 pcolor_end->green / 65535.0,
323 pcolor_end->blue / 65535.0,
324 pcolor_end->alpha / 65535.0);
325 else
326 cairo_pattern_add_color_stop_rgba(pat, 1.0,
327 pcolor->red / 65535.0,
328 pcolor->green / 65535.0,
329 pcolor->blue / 65535.0,
330 pcolor->alpha / 65535.0);
331 cairo_set_source(ctx->cr, pat);
333 return pat;
336 /** Draw rectangle inside the coordinates
337 * \param ctx Draw context
338 * \param geometry geometry
339 * \param line_width line width
340 * \param filled fill rectangle?
341 * \param color color to use
343 void
344 draw_rectangle(draw_context_t *ctx, area_t geometry,
345 float line_width, bool filled, const xcolor_t *color)
347 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
348 cairo_set_line_width(ctx->cr, line_width);
349 cairo_set_miter_limit(ctx->cr, 10.0);
350 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
351 cairo_set_source_rgba(ctx->cr,
352 color->red / 65535.0,
353 color->green / 65535.0,
354 color->blue / 65535.0,
355 color->alpha / 65535.0);
356 if(filled)
358 cairo_rectangle(ctx->cr, geometry.x, geometry.y,
359 geometry.width, geometry.height);
360 cairo_fill(ctx->cr);
362 else
364 cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
365 geometry.width - line_width, geometry.height - line_width);
366 cairo_stroke(ctx->cr);
370 /** Draw rectangle with gradient colors
371 * \param ctx Draw context.
372 * \param geometry Geometry.
373 * \param line_width Line width.
374 * \param filled Filled rectangle?
375 * \param gradient_vector Color-gradient course.
376 * \param pcolor Color at start of gradient_vector.
377 * \param pcolor_center Color in the center.
378 * \param pcolor_end Color at end of gradient_vector.
380 void
381 draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
382 vector_t gradient_vector, const xcolor_t *pcolor,
383 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
385 cairo_pattern_t *pat;
387 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
388 cairo_set_line_width(ctx->cr, line_width);
389 cairo_set_miter_limit(ctx->cr, 10.0);
390 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
392 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
394 if(filled)
396 cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
397 cairo_fill(ctx->cr);
399 else
401 cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
402 cairo_stroke(ctx->cr);
405 if(pat)
406 cairo_pattern_destroy(pat);
409 /** Setup some cairo-things for drawing a graph
410 * \param ctx Draw context
412 void
413 draw_graph_setup(draw_context_t *ctx)
415 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
416 cairo_set_line_width(ctx->cr, 1.0);
417 /* without it, it can draw over the path on sharp angles
418 * ...too long lines * (...graph_line) */
419 cairo_set_miter_limit(ctx->cr, 0.0);
420 cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
423 /** Draw a graph.
424 * \param ctx Draw context.
425 * \param rect The area to draw into.
426 * \param from Array of starting-point offsets to draw a graph lines.
427 * \param to Array of end-point offsets to draw a graph lines.
428 * \param cur_index Current position in data-array (cycles around).
429 * \param grow Put new values to the left or to the right.
430 * \param gradient_vector Color-Gradient course.
431 * \param pcolor Color at start of gradient_vector.
432 * \param pcolor_center Color in the center.
433 * \param pcolor_end Color at end of gradient_vector.
435 void
436 draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
437 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
438 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
440 int i = -1;
441 float x = rect.x + 0.5; /* middle of a pixel */
442 cairo_pattern_t *pat;
444 pat = draw_setup_cairo_color_source(ctx, gradient_vector,
445 pcolor, pcolor_center, pcolor_end);
447 if(grow == Right) /* draw from right to left */
449 x += rect.width - 1;
450 while(++i < rect.width)
452 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
453 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
454 x -= 1.0;
456 if (--cur_index < 0)
457 cur_index = rect.width - 1;
460 else /* draw from left to right */
461 while(++i < rect.width)
463 cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
464 cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
465 x += 1.0;
467 if (--cur_index < 0)
468 cur_index = rect.width - 1;
471 cairo_stroke(ctx->cr);
473 if(pat)
474 cairo_pattern_destroy(pat);
477 /** Draw a line into a graph-widget.
478 * \param ctx Draw context.
479 * \param rect The area to draw into.
480 * \param to array of offsets to draw the line through...
481 * \param cur_index current position in data-array (cycles around)
482 * \param grow put new values to the left or to the right
483 * \param gradient_vector Color-gradient course.
484 * \param pcolor Color at start of gradient_vector.
485 * \param pcolor_center Color in the center.
486 * \param pcolor_end Color at end of gradient_vector.
488 void
489 draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
490 position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
491 const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
493 int i, w;
494 float x, y;
495 cairo_pattern_t *pat;
497 /* NOTE: without, it sometimes won't draw to some path (centered in a pixel)
498 * ... it won't fill some pixels! It also looks much nicer so.
499 * (since line-drawing is the last on the graph, no need to reset to _NONE) */
500 /* Not much difference to CAIRO_ANTIALIAS_DEFAULT, but recommend for LCD */
501 cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_SUBPIXEL);
502 /* a nicer, better visible line compared to 1.0 */
503 cairo_set_line_width(ctx->cr, 1.25);
505 pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
507 /* path through the centers of pixels */
508 x = rect.x + 0.5;
509 y = rect.y + 0.5;
510 w = rect.width;
512 if(grow == Right)
514 /* go through the values from old to new. Begin with the oldest. */
515 if(++cur_index > w - 1)
516 cur_index = 0;
518 cairo_move_to(ctx->cr, x, y - to[cur_index]);
520 else
521 /* on the left border: fills a pixel also when there's only one value */
522 cairo_move_to(ctx->cr, x - 1.0, y - to[cur_index]);
524 for(i = 0; i < w; i++)
526 cairo_line_to(ctx->cr, x, y - to[cur_index]);
527 x += 1.0;
529 /* cycles around the index */
530 if(grow == Right)
532 if (++cur_index > w - 1)
533 cur_index = 0;
535 else
537 if(--cur_index < 0)
538 cur_index = w - 1;
542 /* onto the right border: fills a pixel also when there's only one value */
543 if(grow == Right)
544 cairo_line_to(ctx->cr, x, y - to[cur_index]);
546 cairo_stroke(ctx->cr);
548 if(pat)
549 cairo_pattern_destroy(pat);
550 /* reset line-width */
551 cairo_set_line_width(ctx->cr, 1.0);
554 /** Draw an image from ARGB data to a draw context.
555 * Data should be stored as an array of alpha, red, blue, green for each pixel
556 * and the array size should be w * h elements long.
557 * \param ctx Draw context to draw to.
558 * \param x X coordinate.
559 * \param y Y coordinate.
560 * \param w Width.
561 * \param h Height.
562 * \param ratio The ratio to apply to the image.
563 * \param data The image pixels array.
565 static void
566 draw_image_from_argb_data(draw_context_t *ctx, int x, int y, int w, int h,
567 double ratio, unsigned char *data)
569 cairo_t *cr;
570 cairo_surface_t *source;
572 source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h,
573 #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)
574 sizeof(unsigned char) * 4 * w);
575 #else
576 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
577 #endif
578 cr = cairo_create(ctx->surface);
579 cairo_scale(cr, ratio, ratio);
580 cairo_set_source_surface(cr, source, x / ratio, y / ratio);
582 cairo_paint(cr);
584 cairo_destroy(cr);
585 cairo_surface_destroy(source);
588 /** Draw an image to a draw context.
589 * \param ctx Draw context to draw to.
590 * \param x X coordinate.
591 * \param y Y coordinate.
592 * \param ratio The ratio to apply to the image.
593 * \param image The image to draw.
595 void
596 draw_image(draw_context_t *ctx, int x, int y, double ratio, image_t *image)
598 draw_image_from_argb_data(ctx, x, y, image->width, image->height, ratio, image->data);
601 /** Rotate a pixmap.
602 * \param ctx Draw context to draw with.
603 * \param src Drawable to draw from.
604 * \param dest Drawable to draw to.
605 * \param src_w Drawable width.
606 * \param src_h Drawable height.
607 * \param dest_w Drawable width.
608 * \param dest_h Drawable height.
609 * \param angle angle to rotate.
610 * \param tx Translate to this x coordinate.
611 * \param ty Translate to this y coordinate.
613 void
614 draw_rotate(draw_context_t *ctx,
615 xcb_pixmap_t src, xcb_pixmap_t dest,
616 int src_w, int src_h,
617 int dest_w, int dest_h,
618 double angle, int tx, int ty)
620 cairo_surface_t *surface, *source;
621 cairo_t *cr;
623 surface = cairo_xcb_surface_create(globalconf.connection, dest,
624 ctx->visual, dest_w, dest_h);
625 source = cairo_xcb_surface_create(globalconf.connection, src,
626 ctx->visual, src_w, src_h);
627 cr = cairo_create (surface);
629 cairo_translate(cr, tx, ty);
630 cairo_rotate(cr, angle);
632 cairo_set_source_surface(cr, source, 0.0, 0.0);
633 cairo_paint(cr);
635 cairo_destroy(cr);
636 cairo_surface_destroy(source);
637 cairo_surface_destroy(surface);
640 /** Return the width and height of a text in pixel.
641 * \param data The draw context text data.
642 * \return Text height and width.
644 area_t
645 draw_text_extents(draw_text_context_t *data)
647 cairo_surface_t *surface;
648 cairo_t *cr;
649 PangoLayout *layout;
650 PangoRectangle ext;
651 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
652 area_t geom = { 0, 0, 0, 0 };
654 if(data->len <= 0)
655 return geom;
657 surface = cairo_xcb_surface_create(globalconf.connection,
658 globalconf.default_screen,
659 draw_screen_default_visual(s),
660 s->width_in_pixels,
661 s->height_in_pixels);
663 cr = cairo_create(surface);
664 layout = pango_cairo_create_layout(cr);
665 pango_layout_set_text(layout, data->text, data->len);
666 pango_layout_set_attributes(layout, data->attr_list);
667 pango_layout_set_font_description(layout, globalconf.font->desc);
668 pango_layout_get_pixel_extents(layout, NULL, &ext);
669 g_object_unref(layout);
670 cairo_destroy(cr);
671 cairo_surface_destroy(surface);
673 geom.width = ext.width;
674 geom.height = ext.height;
676 return geom;
679 /** Transform a string to a alignment_t type.
680 * Recognized string are flex, fixed, left, center or right. Everything else
681 * will be recognized as AlignLeft.
682 * \param align Atring with align text.
683 * \param len The string length.
684 * \return An alignment_t type.
686 alignment_t
687 draw_align_fromstr(const char *align, ssize_t len)
689 switch (a_tokenize(align, len))
691 case A_TK_CENTER: return AlignCenter;
692 case A_TK_RIGHT: return AlignRight;
693 case A_TK_FLEX: return AlignFlex;
694 case A_TK_FIXED: return AlignFixed;
695 case A_TK_TOP: return AlignTop;
696 case A_TK_BOTTOM: return AlignBottom;
697 default: return AlignLeft;
701 /** Transform an alignment to a string.
702 * \param a The alignment.
703 * \return A string which must not be freed.
705 const char *
706 draw_align_tostr(alignment_t a)
708 switch(a)
710 case AlignLeft: return "left";
711 case AlignCenter: return "center";
712 case AlignRight: return "right";
713 case AlignFlex: return "flex";
714 case AlignFixed: return "fixed";
715 case AlignBottom: return "bottom";
716 case AlignTop: return "top";
717 default: return NULL;
721 #define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255)
723 /** Send a request to initialize a X color.
724 * \param color xcolor_t struct to store color into.
725 * \param colstr Color specification.
726 * \return request informations.
728 xcolor_init_request_t
729 xcolor_init_unchecked(xcolor_t *color, const char *colstr, ssize_t len)
731 xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen);
732 xcolor_init_request_t req;
733 unsigned long colnum;
734 uint16_t red, green, blue;
736 p_clear(&req, 1);
738 if(!len)
740 req.has_error = true;
741 return req;
744 req.alpha = 0xffff;
745 req.color = color;
747 /* The color is given in RGB value */
748 if(colstr[0] == '#')
750 char *p;
752 if(len == 7)
754 colnum = strtoul(colstr + 1, &p, 16);
755 if(p - colstr != 7)
756 goto invalid;
758 /* we have alpha */
759 else if(len == 9)
761 colnum = strtoul(colstr + 1, &p, 16);
762 if(p - colstr != 9)
763 goto invalid;
764 req.alpha = RGB_COLOR_8_TO_16(colnum);
765 colnum >>= 8;
767 else
769 invalid:
770 warn("awesome: error, invalid color '%s'", colstr);
771 req.has_error = true;
772 return req;
775 red = RGB_COLOR_8_TO_16(colnum >> 16);
776 green = RGB_COLOR_8_TO_16(colnum >> 8);
777 blue = RGB_COLOR_8_TO_16(colnum);
779 req.is_hexa = true;
780 req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
781 s->default_colormap,
782 red, green, blue);
784 else
786 req.is_hexa = false;
787 req.cookie_named = xcb_alloc_named_color_unchecked(globalconf.connection,
788 s->default_colormap, len,
789 colstr);
792 req.has_error = false;
793 req.colstr = colstr;
795 return req;
798 /** Initialize a X color.
799 * \param req xcolor_init request.
800 * \return True if color allocation was successfull.
802 bool
803 xcolor_init_reply(xcolor_init_request_t req)
805 if(req.has_error)
806 return false;
808 if(req.is_hexa)
810 xcb_alloc_color_reply_t *hexa_color;
812 if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
813 req.cookie_hexa, NULL)))
815 req.color->pixel = hexa_color->pixel;
816 req.color->red = hexa_color->red;
817 req.color->green = hexa_color->green;
818 req.color->blue = hexa_color->blue;
819 req.color->alpha = req.alpha;
820 req.color->initialized = true;
821 p_delete(&hexa_color);
822 return true;
825 else
827 xcb_alloc_named_color_reply_t *named_color;
829 if((named_color = xcb_alloc_named_color_reply(globalconf.connection,
830 req.cookie_named, NULL)))
832 req.color->pixel = named_color->pixel;
833 req.color->red = named_color->visual_red;
834 req.color->green = named_color->visual_green;
835 req.color->blue = named_color->visual_blue;
836 req.color->alpha = req.alpha;
837 req.color->initialized = true;
838 p_delete(&named_color);
839 return true;
843 warn("awesome: error, cannot allocate color '%s'", req.colstr);
844 return false;
847 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80