]> git.sur5r.net Git - i3/i3/blob - libi3/draw_util.c
Ensure all *.[ch] files include config.h
[i3/i3] / libi3 / draw_util.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * © 2015 Ingo Bürk and contributors (see also: LICENSE)
5  *
6  * draw.c: Utility for drawing.
7  *
8  */
9 #include "libi3.h"
10
11 #include <stdlib.h>
12 #include <err.h>
13 #include <string.h>
14 #include <xcb/xcb.h>
15 #include <xcb/xcb_aux.h>
16 #include <cairo/cairo-xcb.h>
17
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;
20
21 /* Forward declarations */
22 static void draw_util_set_source_color(xcb_connection_t *conn, surface_t *surface, color_t color);
23
24 #define RETURN_UNLESS_SURFACE_INITIALIZED(surface)                               \
25     do {                                                                         \
26         if ((surface)->id == XCB_NONE) {                                         \
27             ELOG("Surface %p is not initialized, skipping drawing.\n", surface); \
28             return;                                                              \
29         }                                                                        \
30     } while (0)
31
32 /*
33  * Initialize the surface to represent the given drawable.
34  *
35  */
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;
42
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);
45
46     xcb_generic_error_t *error = xcb_request_check(conn, gc_cookie);
47     if (error != NULL) {
48         ELOG("Could not create graphical context. Error code: %d. Please report this bug.\n", error->error_code);
49     }
50
51     surface->surface = cairo_xcb_surface_create(conn, surface->id, surface->visual_type, width, height);
52     surface->cr = cairo_create(surface->surface);
53 }
54
55 /*
56  * Destroys the surface.
57  *
58  */
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);
63
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;
68     surface->cr = NULL;
69 }
70
71 /*
72  * Resize the surface to the given size.
73  *
74  */
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);
79 }
80
81 /*
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".
84  *
85  */
86 color_t draw_util_hex_to_color(const char *color) {
87     char alpha[2];
88     if (strlen(color) == strlen("#rrggbbaa")) {
89         alpha[0] = color[7];
90         alpha[1] = color[8];
91     } else {
92         alpha[0] = alpha[1] = 'F';
93     }
94
95     char groups[4][3] = {
96         {color[1], color[2], '\0'},
97         {color[3], color[4], '\0'},
98         {color[5], color[6], '\0'},
99         {alpha[0], alpha[1], '\0'}};
100
101     return (color_t){
102         .red = strtol(groups[0], NULL, 16) / 255.0,
103         .green = strtol(groups[1], NULL, 16) / 255.0,
104         .blue = strtol(groups[2], NULL, 16) / 255.0,
105         .alpha = strtol(groups[3], NULL, 16) / 255.0,
106         .colorpixel = get_colorpixel(color)};
107 }
108
109 /*
110  * Set the given color as the source color on the surface.
111  *
112  */
113 static void draw_util_set_source_color(xcb_connection_t *conn, surface_t *surface, color_t color) {
114     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
115
116     cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha);
117 }
118
119 /**
120  * Draw the given text using libi3.
121  * This function also marks the surface dirty which is needed if other means of
122  * drawing are used. This will be the case when using XCB to draw text.
123  *
124  */
125 void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
126     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
127
128     /* Flush any changes before we draw the text as this might use XCB directly. */
129     CAIRO_SURFACE_FLUSH(surface->surface);
130
131     set_font_colors(surface->gc, fg_color, bg_color);
132     draw_text(text, surface->id, surface->gc, surface->visual_type, x, y, max_width);
133
134     /* Notify cairo that we (possibly) used another way to draw on the surface. */
135     cairo_surface_mark_dirty(surface->surface);
136 }
137
138 /**
139  * Draws a filled rectangle.
140  * This function is a convenience wrapper and takes care of flushing the
141  * surface as well as restoring the cairo state.
142  *
143  */
144 void draw_util_rectangle(xcb_connection_t *conn, surface_t *surface, color_t color, double x, double y, double w, double h) {
145     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
146
147     cairo_save(surface->cr);
148
149     /* Using the SOURCE operator will copy both color and alpha information directly
150      * onto the surface rather than blending it. This is a bit more efficient and
151      * allows better color control for the user when using opacity. */
152     cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
153     draw_util_set_source_color(conn, surface, color);
154
155     cairo_rectangle(surface->cr, x, y, w, h);
156     cairo_fill(surface->cr);
157
158     /* Make sure we flush the surface for any text drawing operations that could follow.
159      * Since we support drawing text via XCB, we need this. */
160     CAIRO_SURFACE_FLUSH(surface->surface);
161
162     cairo_restore(surface->cr);
163 }
164
165 /**
166  * Clears a surface with the given color.
167  *
168  */
169 void draw_util_clear_surface(xcb_connection_t *conn, surface_t *surface, color_t color) {
170     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
171
172     cairo_save(surface->cr);
173
174     /* Using the SOURCE operator will copy both color and alpha information directly
175      * onto the surface rather than blending it. This is a bit more efficient and
176      * allows better color control for the user when using opacity. */
177     cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
178     draw_util_set_source_color(conn, surface, color);
179
180     cairo_paint(surface->cr);
181
182     /* Make sure we flush the surface for any text drawing operations that could follow.
183      * Since we support drawing text via XCB, we need this. */
184     CAIRO_SURFACE_FLUSH(surface->surface);
185
186     cairo_restore(surface->cr);
187 }
188
189 /**
190  * Copies a surface onto another surface.
191  *
192  */
193 void draw_util_copy_surface(xcb_connection_t *conn, surface_t *src, surface_t *dest, double src_x, double src_y,
194                             double dest_x, double dest_y, double width, double height) {
195     RETURN_UNLESS_SURFACE_INITIALIZED(src);
196     RETURN_UNLESS_SURFACE_INITIALIZED(dest);
197
198     cairo_save(dest->cr);
199
200     /* Using the SOURCE operator will copy both color and alpha information directly
201      * onto the surface rather than blending it. This is a bit more efficient and
202      * allows better color control for the user when using opacity. */
203     cairo_set_operator(dest->cr, CAIRO_OPERATOR_SOURCE);
204     cairo_set_source_surface(dest->cr, src->surface, dest_x - src_x, dest_y - src_y);
205
206     cairo_rectangle(dest->cr, dest_x, dest_y, width, height);
207     cairo_fill(dest->cr);
208
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(src->surface);
212     CAIRO_SURFACE_FLUSH(dest->surface);
213
214     cairo_restore(dest->cr);
215 }