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