]> git.sur5r.net Git - i3/i3/blob - libi3/draw_util.c
816a81df2472a68b1ebfc397e6c0f1c8c2e3937b
[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\n", error->error_code);
51         exit(EXIT_FAILURE);
52     }
53
54 #ifdef CAIRO_SUPPORT
55     surface->surface = cairo_xcb_surface_create(conn, surface->id, surface->visual_type, width, height);
56     surface->cr = cairo_create(surface->surface);
57 #endif
58 }
59
60 /*
61  * Destroys the surface.
62  *
63  */
64 void draw_util_surface_free(xcb_connection_t *conn, surface_t *surface) {
65     xcb_free_gc(conn, surface->gc);
66 #ifdef CAIRO_SUPPORT
67     cairo_surface_destroy(surface->surface);
68     cairo_destroy(surface->cr);
69 #endif
70 }
71
72 /*
73  * Resize the surface to the given size.
74  *
75  */
76 void draw_util_surface_set_size(surface_t *surface, int width, int height) {
77     surface->width = width;
78     surface->height = height;
79 #ifdef CAIRO_SUPPORT
80     cairo_xcb_surface_set_size(surface->surface, width, height);
81 #endif
82 }
83
84 /*
85  * Parses the given color in hex format to an internal color representation.
86  * Note that the input must begin with a hash sign, e.g., "#3fbc59".
87  *
88  */
89 color_t draw_util_hex_to_color(const char *color) {
90     char groups[3][3] = {
91         {color[1], color[2], '\0'},
92         {color[3], color[4], '\0'},
93         {color[5], color[6], '\0'}};
94
95     return (color_t){
96         .red = strtol(groups[0], NULL, 16) / 255.0,
97         .green = strtol(groups[1], NULL, 16) / 255.0,
98         .blue = strtol(groups[2], NULL, 16) / 255.0,
99         .colorpixel = get_colorpixel(color)};
100 }
101
102 color_t draw_util_colorpixel_to_color(uint32_t colorpixel) {
103     return (color_t){
104         .red = ((colorpixel >> 16) & 0xFF) / 255.0,
105         .green = ((colorpixel >> 8) & 0xFF) / 255.0,
106         .blue = (colorpixel & 0xFF) / 255.0,
107         .colorpixel = colorpixel};
108 }
109
110 /*
111  * Set the given color as the source color on the surface.
112  *
113  */
114 static void draw_util_set_source_color(xcb_connection_t *conn, surface_t *surface, color_t color) {
115     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
116
117 #ifdef CAIRO_SUPPORT
118     cairo_set_source_rgb(surface->cr, color.red, color.green, color.blue);
119 #else
120     uint32_t colorpixel = color.colorpixel;
121     xcb_change_gc(conn, surface->gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
122                   (uint32_t[]){colorpixel, colorpixel});
123 #endif
124 }
125
126 /**
127  * Draw the given text using libi3.
128  * This function also marks the surface dirty which is needed if other means of
129  * drawing are used. This will be the case when using XCB to draw text.
130  *
131  */
132 void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
133     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
134
135 #ifdef CAIRO_SUPPORT
136     /* Flush any changes before we draw the text as this might use XCB directly. */
137     CAIRO_SURFACE_FLUSH(surface->surface);
138 #endif
139
140     set_font_colors(surface->gc, fg_color.colorpixel, bg_color.colorpixel);
141     draw_text(text, surface->id, surface->gc, surface->visual_type, x, y, max_width);
142
143 #ifdef CAIRO_SUPPORT
144     /* Notify cairo that we (possibly) used another way to draw on the surface. */
145     cairo_surface_mark_dirty(surface->surface);
146 #endif
147 }
148
149 /**
150  * Draws a filled rectangle.
151  * This function is a convenience wrapper and takes care of flushing the
152  * surface as well as restoring the cairo state.
153  *
154  */
155 void draw_util_rectangle(xcb_connection_t *conn, surface_t *surface, color_t color, double x, double y, double w, double h) {
156     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
157
158 #ifdef CAIRO_SUPPORT
159     cairo_save(surface->cr);
160
161     /* Using the SOURCE operator will copy both color and alpha information directly
162      * onto the surface rather than blending it. This is a bit more efficient and
163      * allows better color control for the user when using opacity. */
164     cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
165     draw_util_set_source_color(conn, surface, color);
166
167     cairo_rectangle(surface->cr, x, y, w, h);
168     cairo_fill(surface->cr);
169
170     /* Make sure we flush the surface for any text drawing operations that could follow.
171      * Since we support drawing text via XCB, we need this. */
172     CAIRO_SURFACE_FLUSH(surface->surface);
173
174     cairo_restore(surface->cr);
175 #else
176     draw_util_set_source_color(conn, surface, color);
177
178     xcb_rectangle_t rect = {x, y, w, h};
179     xcb_poly_fill_rectangle(conn, surface->id, surface->gc, 1, &rect);
180 #endif
181 }
182
183 /**
184  * Clears a surface with the given color.
185  *
186  */
187 void draw_util_clear_surface(xcb_connection_t *conn, surface_t *surface, color_t color) {
188     RETURN_UNLESS_SURFACE_INITIALIZED(surface);
189
190 #ifdef CAIRO_SUPPORT
191     cairo_save(surface->cr);
192
193     /* Using the SOURCE operator will copy both color and alpha information directly
194      * onto the surface rather than blending it. This is a bit more efficient and
195      * allows better color control for the user when using opacity. */
196     cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
197     draw_util_set_source_color(conn, surface, color);
198
199     cairo_paint(surface->cr);
200
201     /* Make sure we flush the surface for any text drawing operations that could follow.
202      * Since we support drawing text via XCB, we need this. */
203     CAIRO_SURFACE_FLUSH(surface->surface);
204
205     cairo_restore(surface->cr);
206 #else
207     draw_util_set_source_color(conn, surface, color);
208
209     xcb_rectangle_t rect = {0, 0, surface->width, surface->height};
210     xcb_poly_fill_rectangle(conn, surface->id, surface->gc, 1, &rect);
211 #endif
212 }
213
214 /**
215  * Copies a surface onto another surface.
216  *
217  */
218 void draw_util_copy_surface(xcb_connection_t *conn, surface_t *src, surface_t *dest, double src_x, double src_y,
219                             double dest_x, double dest_y, double width, double height) {
220     RETURN_UNLESS_SURFACE_INITIALIZED(src);
221     RETURN_UNLESS_SURFACE_INITIALIZED(dest);
222
223 #ifdef CAIRO_SUPPORT
224     cairo_save(dest->cr);
225
226     /* Using the SOURCE operator will copy both color and alpha information directly
227      * onto the surface rather than blending it. This is a bit more efficient and
228      * allows better color control for the user when using opacity. */
229     cairo_set_operator(dest->cr, CAIRO_OPERATOR_SOURCE);
230     cairo_set_source_surface(dest->cr, src->surface, dest_x - src_x, dest_y - src_y);
231
232     cairo_rectangle(dest->cr, dest_x, dest_y, width, height);
233     cairo_fill(dest->cr);
234
235     /* Make sure we flush the surface for any text drawing operations that could follow.
236      * Since we support drawing text via XCB, we need this. */
237     CAIRO_SURFACE_FLUSH(src->surface);
238     CAIRO_SURFACE_FLUSH(dest->surface);
239
240     cairo_restore(dest->cr);
241 #else
242     xcb_copy_area(conn, src->id, dest->id, dest->gc, (int16_t)src_x, (int16_t)src_y,
243                   (int16_t)dest_x, (int16_t)dest_y, (uint16_t)width, (uint16_t)height);
244 #endif
245 }