]> git.sur5r.net Git - i3/i3/blob - libi3/draw_util.c
Add missing MAX macros, ensure that i3 can be compiled without pango
[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 <stdlib.h>
10 #include <err.h>
11 #include <string.h>
12 #include <xcb/xcb.h>
13 #include <xcb/xcb_aux.h>
14 #ifdef CAIRO_SUPPORT
15 #include <cairo/cairo-xcb.h>
16 #endif
17
18 #include "libi3.h"
19
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;
22
23 /* Forward declarations */
24 static void draw_util_set_source_color(xcb_connection_t *conn, surface_t *surface, color_t color);
25
26 #define RETURN_UNLESS_SURFACE_INITIALIZED(surface)                               \
27     do {                                                                         \
28         if ((surface)->id == XCB_NONE) {                                         \
29             ELOG("Surface %p is not initialized, skipping drawing.\n", surface); \
30             return;                                                              \
31         }                                                                        \
32     } while (0)
33
34 /*
35  * Initialize the surface to represent the given drawable.
36  *
37  */
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;
44
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);
47
48     xcb_generic_error_t *error = xcb_request_check(conn, gc_cookie);
49     if (error != NULL) {
50         ELOG("Could not create graphical context. Error code: %d. Please report this bug.\n", error->error_code);
51     }
52
53 #ifdef CAIRO_SUPPORT
54     surface->surface = cairo_xcb_surface_create(conn, surface->id, surface->visual_type, width, height);
55     surface->cr = cairo_create(surface->surface);
56 #endif
57 }
58
59 /*
60  * Destroys the surface.
61  *
62  */
63 void draw_util_surface_free(xcb_connection_t *conn, surface_t *surface) {
64     xcb_free_gc(conn, surface->gc);
65 #ifdef CAIRO_SUPPORT
66     cairo_surface_destroy(surface->surface);
67     cairo_destroy(surface->cr);
68
69     /* We need to explicitly set these to NULL to avoid assertion errors in
70      * cairo when calling this multiple times. This can happen, for example,
71      * when setting the border of a window to none and then closing it. */
72     surface->surface = NULL;
73     surface->cr = NULL;
74 #endif
75 }
76
77 /*
78  * Resize the surface to the given size.
79  *
80  */
81 void draw_util_surface_set_size(surface_t *surface, int width, int height) {
82     surface->width = width;
83     surface->height = height;
84 #ifdef CAIRO_SUPPORT
85     cairo_xcb_surface_set_size(surface->surface, width, height);
86 #endif
87 }
88
89 /*
90  * Parses the given color in hex format to an internal color representation.
91  * Note that the input must begin with a hash sign, e.g., "#3fbc59".
92  *
93  */
94 color_t draw_util_hex_to_color(const char *color) {
95     char alpha[2];
96     if (strlen(color) == strlen("#rrggbbaa")) {
97         alpha[0] = color[7];
98         alpha[1] = color[8];
99     } else {
100         alpha[0] = alpha[1] = 'F';
101     }
102
103     char groups[4][3] = {
104         {color[1], color[2], '\0'},
105         {color[3], color[4], '\0'},
106         {color[5], color[6], '\0'},
107         {alpha[0], alpha[1], '\0'}};
108
109     return (color_t){
110         .red = strtol(groups[0], NULL, 16) / 255.0,
111         .green = strtol(groups[1], NULL, 16) / 255.0,
112         .blue = strtol(groups[2], NULL, 16) / 255.0,
113         .alpha = strtol(groups[3], NULL, 16) / 255.0,
114         .colorpixel = get_colorpixel(color)};
115 }
116
117 /*
118  * Set the given color as the source color on the surface.
119  *
120  */
121 static void draw_util_set_source_color(xcb_connection_t *conn, surface_t *surface, color_t color) {
122     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
123
124 #ifdef CAIRO_SUPPORT
125     cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha);
126 #else
127     uint32_t colorpixel = color.colorpixel;
128     xcb_change_gc(conn, surface->gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
129                   (uint32_t[]){colorpixel, colorpixel});
130 #endif
131 }
132
133 /**
134  * Draw the given text using libi3.
135  * This function also marks the surface dirty which is needed if other means of
136  * drawing are used. This will be the case when using XCB to draw text.
137  *
138  */
139 void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
140     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
141
142 #ifdef CAIRO_SUPPORT
143     /* Flush any changes before we draw the text as this might use XCB directly. */
144     CAIRO_SURFACE_FLUSH(surface->surface);
145 #endif
146
147     set_font_colors(surface->gc, fg_color, bg_color);
148     draw_text(text, surface->id, surface->gc, surface->visual_type, x, y, max_width);
149
150 #ifdef CAIRO_SUPPORT
151     /* Notify cairo that we (possibly) used another way to draw on the surface. */
152     cairo_surface_mark_dirty(surface->surface);
153 #endif
154 }
155
156 /**
157  * Draws a filled rectangle.
158  * This function is a convenience wrapper and takes care of flushing the
159  * surface as well as restoring the cairo state.
160  *
161  */
162 void draw_util_rectangle(xcb_connection_t *conn, surface_t *surface, color_t color, double x, double y, double w, double h) {
163     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
164
165 #ifdef CAIRO_SUPPORT
166     cairo_save(surface->cr);
167
168     /* Using the SOURCE operator will copy both color and alpha information directly
169      * onto the surface rather than blending it. This is a bit more efficient and
170      * allows better color control for the user when using opacity. */
171     cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
172     draw_util_set_source_color(conn, surface, color);
173
174     cairo_rectangle(surface->cr, x, y, w, h);
175     cairo_fill(surface->cr);
176
177     /* Make sure we flush the surface for any text drawing operations that could follow.
178      * Since we support drawing text via XCB, we need this. */
179     CAIRO_SURFACE_FLUSH(surface->surface);
180
181     cairo_restore(surface->cr);
182 #else
183     draw_util_set_source_color(conn, surface, color);
184
185     xcb_rectangle_t rect = {x, y, w, h};
186     xcb_poly_fill_rectangle(conn, surface->id, surface->gc, 1, &rect);
187 #endif
188 }
189
190 /**
191  * Clears a surface with the given color.
192  *
193  */
194 void draw_util_clear_surface(xcb_connection_t *conn, surface_t *surface, color_t color) {
195     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
196
197 #ifdef CAIRO_SUPPORT
198     cairo_save(surface->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(surface->cr, CAIRO_OPERATOR_SOURCE);
204     draw_util_set_source_color(conn, surface, color);
205
206     cairo_paint(surface->cr);
207
208     /* Make sure we flush the surface for any text drawing operations that could follow.
209      * Since we support drawing text via XCB, we need this. */
210     CAIRO_SURFACE_FLUSH(surface->surface);
211
212     cairo_restore(surface->cr);
213 #else
214     draw_util_set_source_color(conn, surface, color);
215
216     xcb_rectangle_t rect = {0, 0, surface->width, surface->height};
217     xcb_poly_fill_rectangle(conn, surface->id, surface->gc, 1, &rect);
218 #endif
219 }
220
221 /**
222  * Copies a surface onto another surface.
223  *
224  */
225 void draw_util_copy_surface(xcb_connection_t *conn, surface_t *src, surface_t *dest, double src_x, double src_y,
226                             double dest_x, double dest_y, double width, double height) {
227     RETURN_UNLESS_SURFACE_INITIALIZED(src);
228     RETURN_UNLESS_SURFACE_INITIALIZED(dest);
229
230 #ifdef CAIRO_SUPPORT
231     cairo_save(dest->cr);
232
233     /* Using the SOURCE operator will copy both color and alpha information directly
234      * onto the surface rather than blending it. This is a bit more efficient and
235      * allows better color control for the user when using opacity. */
236     cairo_set_operator(dest->cr, CAIRO_OPERATOR_SOURCE);
237     cairo_set_source_surface(dest->cr, src->surface, dest_x - src_x, dest_y - src_y);
238
239     cairo_rectangle(dest->cr, dest_x, dest_y, width, height);
240     cairo_fill(dest->cr);
241
242     /* Make sure we flush the surface for any text drawing operations that could follow.
243      * Since we support drawing text via XCB, we need this. */
244     CAIRO_SURFACE_FLUSH(src->surface);
245     CAIRO_SURFACE_FLUSH(dest->surface);
246
247     cairo_restore(dest->cr);
248 #else
249     xcb_copy_area(conn, src->id, dest->id, dest->gc, (int16_t)src_x, (int16_t)src_y,
250                   (int16_t)dest_x, (int16_t)dest_y, (uint16_t)width, (uint16_t)height);
251 #endif
252 }