2 * vim:ts=4:sw=4:expandtab
4 * © 2015 Ingo Bürk and contributors (see also: LICENSE)
6 * draw.c: Utility for drawing.
15 #include <xcb/xcb_aux.h>
16 #include <cairo/cairo-xcb.h>
18 /* The default visual_type to use if none is specified when creating the surface. Must be defined globally. */
19 xcb_visualtype_t *visual_type;
21 /* Forward declarations */
22 static void draw_util_set_source_color(surface_t *surface, color_t color);
24 #define RETURN_UNLESS_SURFACE_INITIALIZED(surface) \
26 if ((surface)->id == XCB_NONE) { \
27 ELOG("Surface %p is not initialized, skipping drawing.\n", surface); \
33 * Initialize the surface to represent the given drawable.
36 void draw_util_surface_init(xcb_connection_t *conn, surface_t *surface, xcb_drawable_t drawable,
37 xcb_visualtype_t *visual, int width, int height) {
38 surface->id = drawable;
39 surface->visual_type = ((visual == NULL) ? visual_type : visual);
40 surface->width = width;
41 surface->height = height;
43 surface->gc = xcb_generate_id(conn);
44 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(conn, surface->gc, surface->id, 0, NULL);
46 xcb_generic_error_t *error = xcb_request_check(conn, gc_cookie);
48 ELOG("Could not create graphical context. Error code: %d. Please report this bug.\n", error->error_code);
51 surface->surface = cairo_xcb_surface_create(conn, surface->id, surface->visual_type, width, height);
52 surface->cr = cairo_create(surface->surface);
56 * Destroys the surface.
59 void draw_util_surface_free(xcb_connection_t *conn, surface_t *surface) {
60 xcb_free_gc(conn, surface->gc);
61 cairo_surface_destroy(surface->surface);
62 cairo_destroy(surface->cr);
64 /* We need to explicitly set these to NULL to avoid assertion errors in
65 * cairo when calling this multiple times. This can happen, for example,
66 * when setting the border of a window to none and then closing it. */
67 surface->surface = NULL;
72 * Resize the surface to the given size.
75 void draw_util_surface_set_size(surface_t *surface, int width, int height) {
76 surface->width = width;
77 surface->height = height;
78 cairo_xcb_surface_set_size(surface->surface, width, height);
82 * Parses the given color in hex format to an internal color representation.
83 * Note that the input must begin with a hash sign, e.g., "#3fbc59".
86 color_t draw_util_hex_to_color(const char *color) {
87 if (strlen(color) < 6 || color[0] != '#') {
88 ELOG("Could not parse color: %s\n", color);
89 return draw_util_hex_to_color("#A9A9A9");
93 if (strlen(color) == strlen("#rrggbbaa")) {
97 alpha[0] = alpha[1] = 'F';
100 char groups[4][3] = {
101 {color[1], color[2], '\0'},
102 {color[3], color[4], '\0'},
103 {color[5], color[6], '\0'},
104 {alpha[0], alpha[1], '\0'}};
107 .red = strtol(groups[0], NULL, 16) / 255.0,
108 .green = strtol(groups[1], NULL, 16) / 255.0,
109 .blue = strtol(groups[2], NULL, 16) / 255.0,
110 .alpha = strtol(groups[3], NULL, 16) / 255.0,
111 .colorpixel = get_colorpixel(color)};
115 * Set the given color as the source color on the surface.
118 static void draw_util_set_source_color(surface_t *surface, color_t color) {
119 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
121 cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha);
125 * Draw the given text using libi3.
126 * This function also marks the surface dirty which is needed if other means of
127 * drawing are used. This will be the case when using XCB to draw text.
130 void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
131 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
133 /* Flush any changes before we draw the text as this might use XCB directly. */
134 CAIRO_SURFACE_FLUSH(surface->surface);
136 set_font_colors(surface->gc, fg_color, bg_color);
137 draw_text(text, surface->id, surface->gc, surface->visual_type, x, y, max_width);
139 /* Notify cairo that we (possibly) used another way to draw on the surface. */
140 cairo_surface_mark_dirty(surface->surface);
144 * Draws a filled rectangle.
145 * This function is a convenience wrapper and takes care of flushing the
146 * surface as well as restoring the cairo state.
149 void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h) {
150 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
152 cairo_save(surface->cr);
154 /* Using the SOURCE operator will copy both color and alpha information directly
155 * onto the surface rather than blending it. This is a bit more efficient and
156 * allows better color control for the user when using opacity. */
157 cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
158 draw_util_set_source_color(surface, color);
160 cairo_rectangle(surface->cr, x, y, w, h);
161 cairo_fill(surface->cr);
163 /* Make sure we flush the surface for any text drawing operations that could follow.
164 * Since we support drawing text via XCB, we need this. */
165 CAIRO_SURFACE_FLUSH(surface->surface);
167 cairo_restore(surface->cr);
171 * Clears a surface with the given color.
174 void draw_util_clear_surface(surface_t *surface, color_t color) {
175 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
177 cairo_save(surface->cr);
179 /* Using the SOURCE operator will copy both color and alpha information directly
180 * onto the surface rather than blending it. This is a bit more efficient and
181 * allows better color control for the user when using opacity. */
182 cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
183 draw_util_set_source_color(surface, color);
185 cairo_paint(surface->cr);
187 /* Make sure we flush the surface for any text drawing operations that could follow.
188 * Since we support drawing text via XCB, we need this. */
189 CAIRO_SURFACE_FLUSH(surface->surface);
191 cairo_restore(surface->cr);
195 * Copies a surface onto another surface.
198 void draw_util_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y,
199 double dest_x, double dest_y, double width, double height) {
200 RETURN_UNLESS_SURFACE_INITIALIZED(src);
201 RETURN_UNLESS_SURFACE_INITIALIZED(dest);
203 cairo_save(dest->cr);
205 /* Using the SOURCE operator will copy both color and alpha information directly
206 * onto the surface rather than blending it. This is a bit more efficient and
207 * allows better color control for the user when using opacity. */
208 cairo_set_operator(dest->cr, CAIRO_OPERATOR_SOURCE);
209 cairo_set_source_surface(dest->cr, src->surface, dest_x - src_x, dest_y - src_y);
211 cairo_rectangle(dest->cr, dest_x, dest_y, width, height);
212 cairo_fill(dest->cr);
214 /* Make sure we flush the surface for any text drawing operations that could follow.
215 * Since we support drawing text via XCB, we need this. */
216 CAIRO_SURFACE_FLUSH(src->surface);
217 CAIRO_SURFACE_FLUSH(dest->surface);
219 cairo_restore(dest->cr);