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