2 * vim:ts=4:sw=4:expandtab
4 * © 2015 Ingo Bürk and contributors (see also: LICENSE)
6 * draw.c: Utility for drawing.
13 #include <xcb/xcb_aux.h>
15 #include <cairo/cairo-xcb.h>
20 /* The default visual_type to use if none is specified when creating the surface. Must be defined globally. */
21 xcb_visualtype_t *visual_type;
23 /* Forward declarations */
24 static void draw_util_set_source_color(xcb_connection_t *conn, surface_t *surface, color_t color);
26 #define RETURN_UNLESS_SURFACE_INITIALIZED(surface) \
28 if ((surface)->id == XCB_NONE) { \
29 ELOG("Surface %p is not initialized, skipping drawing.\n", surface); \
35 * Initialize the surface to represent the given drawable.
38 void draw_util_surface_init(xcb_connection_t *conn, surface_t *surface, xcb_drawable_t drawable,
39 xcb_visualtype_t *visual, int width, int height) {
40 surface->id = drawable;
41 surface->visual_type = ((visual == NULL) ? visual_type : visual);
42 surface->width = width;
43 surface->height = height;
45 surface->gc = xcb_generate_id(conn);
46 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(conn, surface->gc, surface->id, 0, NULL);
48 xcb_generic_error_t *error = xcb_request_check(conn, gc_cookie);
50 ELOG("Could not create graphical context. Error code: %d\n", error->error_code);
55 surface->surface = cairo_xcb_surface_create(conn, surface->id, surface->visual_type, width, height);
56 surface->cr = cairo_create(surface->surface);
61 * Destroys the surface.
64 void draw_util_surface_free(xcb_connection_t *conn, surface_t *surface) {
65 xcb_free_gc(conn, surface->gc);
67 cairo_surface_destroy(surface->surface);
68 cairo_destroy(surface->cr);
70 /* We need to explicitly set these to NULL to avoid assertion errors in
71 * cairo when calling this multiple times. This can happen, for example,
72 * when setting the border of a window to none and then closing it. */
73 surface->surface = NULL;
79 * Resize the surface to the given size.
82 void draw_util_surface_set_size(surface_t *surface, int width, int height) {
83 surface->width = width;
84 surface->height = height;
86 cairo_xcb_surface_set_size(surface->surface, width, height);
91 * Parses the given color in hex format to an internal color representation.
92 * Note that the input must begin with a hash sign, e.g., "#3fbc59".
95 color_t draw_util_hex_to_color(const char *color) {
97 if (strlen(color) == strlen("#rrggbbaa")) {
101 alpha[0] = alpha[1] = 'F';
104 char groups[4][3] = {
105 {color[1], color[2], '\0'},
106 {color[3], color[4], '\0'},
107 {color[5], color[6], '\0'},
108 {alpha[0], alpha[1], '\0'}};
111 .red = strtol(groups[0], NULL, 16) / 255.0,
112 .green = strtol(groups[1], NULL, 16) / 255.0,
113 .blue = strtol(groups[2], NULL, 16) / 255.0,
114 .alpha = strtol(groups[3], NULL, 16) / 255.0,
115 .colorpixel = get_colorpixel(color)};
119 * Set the given color as the source color on the surface.
122 static void draw_util_set_source_color(xcb_connection_t *conn, surface_t *surface, color_t color) {
123 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
126 cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha);
128 uint32_t colorpixel = color.colorpixel;
129 xcb_change_gc(conn, surface->gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
130 (uint32_t[]){colorpixel, colorpixel});
135 * Draw the given text using libi3.
136 * This function also marks the surface dirty which is needed if other means of
137 * drawing are used. This will be the case when using XCB to draw text.
140 void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
141 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
144 /* Flush any changes before we draw the text as this might use XCB directly. */
145 CAIRO_SURFACE_FLUSH(surface->surface);
148 set_font_colors(surface->gc, fg_color.colorpixel, bg_color.colorpixel);
149 draw_text(text, surface->id, surface->gc, surface->visual_type, x, y, max_width);
152 /* Notify cairo that we (possibly) used another way to draw on the surface. */
153 cairo_surface_mark_dirty(surface->surface);
158 * Draws a filled rectangle.
159 * This function is a convenience wrapper and takes care of flushing the
160 * surface as well as restoring the cairo state.
163 void draw_util_rectangle(xcb_connection_t *conn, surface_t *surface, color_t color, double x, double y, double w, double h) {
164 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
167 cairo_save(surface->cr);
169 /* Using the SOURCE operator will copy both color and alpha information directly
170 * onto the surface rather than blending it. This is a bit more efficient and
171 * allows better color control for the user when using opacity. */
172 cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
173 draw_util_set_source_color(conn, surface, color);
175 cairo_rectangle(surface->cr, x, y, w, h);
176 cairo_fill(surface->cr);
178 /* Make sure we flush the surface for any text drawing operations that could follow.
179 * Since we support drawing text via XCB, we need this. */
180 CAIRO_SURFACE_FLUSH(surface->surface);
182 cairo_restore(surface->cr);
184 draw_util_set_source_color(conn, surface, color);
186 xcb_rectangle_t rect = {x, y, w, h};
187 xcb_poly_fill_rectangle(conn, surface->id, surface->gc, 1, &rect);
192 * Clears a surface with the given color.
195 void draw_util_clear_surface(xcb_connection_t *conn, surface_t *surface, color_t color) {
196 RETURN_UNLESS_SURFACE_INITIALIZED(surface);
199 cairo_save(surface->cr);
201 /* Using the SOURCE operator will copy both color and alpha information directly
202 * onto the surface rather than blending it. This is a bit more efficient and
203 * allows better color control for the user when using opacity. */
204 cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
205 draw_util_set_source_color(conn, surface, color);
207 cairo_paint(surface->cr);
209 /* Make sure we flush the surface for any text drawing operations that could follow.
210 * Since we support drawing text via XCB, we need this. */
211 CAIRO_SURFACE_FLUSH(surface->surface);
213 cairo_restore(surface->cr);
215 draw_util_set_source_color(conn, surface, color);
217 xcb_rectangle_t rect = {0, 0, surface->width, surface->height};
218 xcb_poly_fill_rectangle(conn, surface->id, surface->gc, 1, &rect);
223 * Copies a surface onto another surface.
226 void draw_util_copy_surface(xcb_connection_t *conn, surface_t *src, surface_t *dest, double src_x, double src_y,
227 double dest_x, double dest_y, double width, double height) {
228 RETURN_UNLESS_SURFACE_INITIALIZED(src);
229 RETURN_UNLESS_SURFACE_INITIALIZED(dest);
232 cairo_save(dest->cr);
234 /* Using the SOURCE operator will copy both color and alpha information directly
235 * onto the surface rather than blending it. This is a bit more efficient and
236 * allows better color control for the user when using opacity. */
237 cairo_set_operator(dest->cr, CAIRO_OPERATOR_SOURCE);
238 cairo_set_source_surface(dest->cr, src->surface, dest_x - src_x, dest_y - src_y);
240 cairo_rectangle(dest->cr, dest_x, dest_y, width, height);
241 cairo_fill(dest->cr);
243 /* Make sure we flush the surface for any text drawing operations that could follow.
244 * Since we support drawing text via XCB, we need this. */
245 CAIRO_SURFACE_FLUSH(src->surface);
246 CAIRO_SURFACE_FLUSH(dest->surface);
248 cairo_restore(dest->cr);
250 xcb_copy_area(conn, src->id, dest->id, dest->gc, (int16_t)src_x, (int16_t)src_y,
251 (int16_t)dest_x, (int16_t)dest_y, (uint16_t)width, (uint16_t)height);