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