]> git.sur5r.net Git - i3/i3/blob - i3bar/include/draw_util.h
Merge pull request #2004 from Airblader/bug-2003
[i3/i3] / i3bar / include / draw_util.h
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * © 2015 Ingo Bürk and contributors (see also: LICENSE)
5  *
6  * draw.h: Utility for drawing.
7  *
8  */
9 #pragma once
10
11 #ifdef I3BAR_CAIRO
12 #include <cairo/cairo-xcb.h>
13 #endif
14
15 #ifdef I3BAR_CAIRO
16 /* We need to flush cairo surfaces twice to avoid an assertion bug. See #1989
17  * and https://bugs.freedesktop.org/show_bug.cgi?id=92455. */
18 #define CAIRO_SURFACE_FLUSH(surface)  \
19     do {                              \
20         cairo_surface_flush(surface); \
21         cairo_surface_flush(surface); \
22     } while (0)
23 #endif
24
25 /* Represents a color split by color channel. */
26 typedef struct color_t {
27     double red;
28     double green;
29     double blue;
30
31     /* For compatibility, we also store the colorpixel for now. */
32     uint32_t colorpixel;
33 } color_t;
34
35 /* A wrapper grouping an XCB drawable and both a graphics context
36  * and the corresponding cairo objects representing it. */
37 typedef struct surface_t {
38     /* The drawable which is being represented. */
39     xcb_drawable_t id;
40
41     /* A classic XCB graphics context. */
42     xcb_gcontext_t gc;
43
44     int width;
45     int height;
46
47 #ifdef I3BAR_CAIRO
48     /* A cairo surface representing the drawable. */
49     cairo_surface_t *surface;
50
51     /* The cairo object representing the drawale. In general,
52      * this is what one should use for any drawing operation. */
53     cairo_t *cr;
54 #endif
55 } surface_t;
56
57 /**
58  * Initialize the surface to represent the given drawable.
59  *
60  */
61 void draw_util_surface_init(surface_t *surface, xcb_drawable_t drawable, int width, int height);
62
63 /**
64  * Destroys the surface.
65  *
66  */
67 void draw_util_surface_free(surface_t *surface);
68
69 /**
70  * Parses the given color in hex format to an internal color representation.
71  * Note that the input must begin with a hash sign, e.g., "#3fbc59".
72  *
73  */
74 color_t draw_util_hex_to_color(const char *color);
75
76 /**
77  * Draw the given text using libi3.
78  * This function also marks the surface dirty which is needed if other means of
79  * drawing are used. This will be the case when using XCB to draw text.
80  *
81  */
82 void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width);
83
84 /**
85  * Draws a filled rectangle.
86  * This function is a convenience wrapper and takes care of flushing the
87  * surface as well as restoring the cairo state.
88  *
89  */
90 void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h);
91
92 /**
93  * Clears a surface with the given color.
94  *
95  */
96 void draw_util_clear_surface(surface_t *surface, color_t color);
97
98 /**
99  * Copies a surface onto another surface.
100  *
101  */
102 void draw_util_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y,
103                             double dest_x, double dest_y, double width, double height);