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