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>
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.
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
)
58 if(iso2utf8
== (iconv_t
) -1)
60 iso2utf8
= iconv_open("UTF-8", nl_langinfo(CODESET
));
61 if(iso2utf8
== (iconv_t
) -1)
64 warn("unable to convert text from %s to UTF-8, not available",
65 nl_langinfo(CODESET
));
67 warn("unable to convert text: %s", strerror(errno
));
73 size_t orig_utf8len
, utf8len
;
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
));
86 *dlen
= orig_utf8len
- utf8len
;
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
;
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
;
110 /** Create a new Pango font.
111 * \param fontname Pango fontname (e.g. [FAMILY-LIST] [STYLE-OPTIONS] [SIZE]).
112 * \return A new font.
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
);
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
),
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
);
139 pango_layout_get_pixel_size(layout
, NULL
, &font
->height
);
141 g_object_unref(layout
);
143 cairo_surface_destroy(surface
);
149 * \param font Font to delete.
152 draw_font_delete(font_t
**font
)
156 pango_font_description_free((*font
)->desc
);
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.
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");
180 data
->len
= a_strlen(data
->text
);
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.
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
;
204 d
->depth
= s
->root_depth
;
205 d
->visual
= draw_screen_default_visual(s
);
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
);
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.
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
)
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
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
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
)
254 x
+= (area
.width
- ext
->width
) / 2;
257 x
+= area
.width
- ext
->width
;
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);
300 pat
= cairo_pattern_create_linear(gradient_vector
.x
,
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);
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);
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);
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
);
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
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);
358 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
,
359 geometry
.width
, geometry
.height
);
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.
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
);
396 cairo_rectangle(ctx
->cr
, geometry
.x
, geometry
.y
, geometry
.width
, geometry
.height
);
401 cairo_rectangle(ctx
->cr
, geometry
.x
+ 1, geometry
.y
, geometry
.width
- 1, geometry
.height
- 1);
402 cairo_stroke(ctx
->cr
);
406 cairo_pattern_destroy(pat
);
409 /** Setup some cairo-things for drawing a graph
410 * \param ctx Draw context
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
);
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.
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
)
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 */
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
]);
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
]);
468 cur_index
= rect
.width
- 1;
471 cairo_stroke(ctx
->cr
);
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.
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
)
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 */
514 /* go through the values from old to new. Begin with the oldest. */
515 if(++cur_index
> w
- 1)
518 cairo_move_to(ctx
->cr
, x
, y
- to
[cur_index
]);
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
]);
529 /* cycles around the index */
532 if (++cur_index
> w
- 1)
542 /* onto the right border: fills a pixel also when there's only one value */
544 cairo_line_to(ctx
->cr
, x
, y
- to
[cur_index
]);
546 cairo_stroke(ctx
->cr
);
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.
562 * \param ratio The ratio to apply to the image.
563 * \param data The image pixels array.
566 draw_image_from_argb_data(draw_context_t
*ctx
, int x
, int y
, int w
, int h
,
567 double ratio
, unsigned char *data
)
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
);
576 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, w
));
578 cr
= cairo_create(ctx
->surface
);
579 cairo_scale(cr
, ratio
, ratio
);
580 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
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.
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
);
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.
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
;
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);
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.
645 draw_text_extents(draw_text_context_t
*data
)
647 cairo_surface_t
*surface
;
651 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
652 area_t geom
= { 0, 0, 0, 0 };
657 surface
= cairo_xcb_surface_create(globalconf
.connection
,
658 globalconf
.default_screen
,
659 draw_screen_default_visual(s
),
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
);
671 cairo_surface_destroy(surface
);
673 geom
.width
= ext
.width
;
674 geom
.height
= ext
.height
;
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.
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.
706 draw_align_tostr(alignment_t 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
;
740 req
.has_error
= true;
747 /* The color is given in RGB value */
754 colnum
= strtoul(colstr
+ 1, &p
, 16);
761 colnum
= strtoul(colstr
+ 1, &p
, 16);
764 req
.alpha
= RGB_COLOR_8_TO_16(colnum
);
770 warn("awesome: error, invalid color '%s'", colstr
);
771 req
.has_error
= true;
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
);
780 req
.cookie_hexa
= xcb_alloc_color_unchecked(globalconf
.connection
,
787 req
.cookie_named
= xcb_alloc_named_color_unchecked(globalconf
.connection
,
788 s
->default_colormap
, len
,
792 req
.has_error
= false;
798 /** Initialize a X color.
799 * \param req xcolor_init request.
800 * \return True if color allocation was successfull.
803 xcolor_init_reply(xcolor_init_request_t req
)
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
);
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
);
843 warn("awesome: error, cannot allocate color '%s'", req
.colstr
);
847 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80