]> git.sur5r.net Git - i3/i3/blob - i3bar/src/cairo_util.c
Remove support for 32-bit visuals and RGBA colors.
[i3/i3] / i3bar / src / cairo_util.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * © 2015 Ingo Bürk and contributors (see also: LICENSE)
5  *
6  * cairo_util.c: Utility for operations using cairo.
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 #include <cairo/cairo-xcb.h>
15
16 #include "common.h"
17 #include "libi3.h"
18
19 xcb_connection_t *xcb_connection;
20 xcb_visualtype_t *visual_type;
21
22 /*
23  * Initialize the cairo surface to represent the given drawable.
24  *
25  */
26 void cairo_surface_init(surface_t *surface, xcb_drawable_t drawable, int width, int height) {
27     surface->id = drawable;
28
29     surface->gc = xcb_generate_id(xcb_connection);
30     xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection, surface->gc, surface->id, 0, NULL);
31     if (xcb_request_failed(gc_cookie, "Could not create graphical context"))
32         exit(EXIT_FAILURE);
33
34     surface->surface = cairo_xcb_surface_create(xcb_connection, surface->id, visual_type, width, height);
35     surface->cr = cairo_create(surface->surface);
36 }
37
38 /*
39  * Destroys the surface.
40  *
41  */
42 void cairo_surface_free(surface_t *surface) {
43     xcb_free_gc(xcb_connection, surface->gc);
44     cairo_surface_destroy(surface->surface);
45     cairo_destroy(surface->cr);
46 }
47
48 /*
49  * Parses the given color in hex format to an internal color representation.
50  * Note that the input must begin with a hash sign, e.g., "#3fbc59".
51  *
52  */
53 color_t cairo_hex_to_color(const char *color) {
54     char groups[3][3] = {
55         {color[1], color[2], '\0'},
56         {color[3], color[4], '\0'},
57         {color[5], color[6], '\0'}};
58
59     return (color_t){
60         .red = strtol(groups[0], NULL, 16) / 255.0,
61         .green = strtol(groups[1], NULL, 16) / 255.0,
62         .blue = strtol(groups[2], NULL, 16) / 255.0,
63         .colorpixel = get_colorpixel(color)};
64 }
65
66 /*
67  * Set the given color as the source color on the surface.
68  *
69  */
70 void cairo_set_source_color(surface_t *surface, color_t color) {
71     cairo_set_source_rgb(surface->cr, color.red, color.green, color.blue);
72 }
73
74 /**
75  * Draw the given text using libi3.
76  * This function also marks the surface dirty which is needed if other means of
77  * drawing are used. This will be the case when using XCB to draw text.
78  *
79  */
80 void cairo_draw_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
81     set_font_colors(surface->gc, fg_color.colorpixel, bg_color.colorpixel);
82     draw_text(text, surface->id, surface->gc, visual_type, x, y, max_width);
83
84     cairo_surface_mark_dirty(surface->surface);
85 }
86
87 /**
88  * Draws a filled rectangle.
89  * This function is a convenience wrapper and takes care of flushing the
90  * surface as well as restoring the cairo state.
91  * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
92  *
93  */
94 void cairo_draw_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h) {
95     cairo_save(surface->cr);
96
97     /* Using the SOURCE operator will copy both color and alpha information directly
98      * onto the surface rather than blending it. This is a bit more efficient and
99      * allows better color control for the user when using opacity. */
100     cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
101     cairo_set_source_color(surface, color);
102
103     cairo_rectangle(surface->cr, x, y, w, h);
104     cairo_fill(surface->cr);
105
106     /* Make sure we flush the surface for any text drawing operations that could follow.
107      * Since we support drawing text via XCB, we need this. */
108     cairo_surface_flush(surface->surface);
109
110     cairo_restore(surface->cr);
111 }
112
113 /**
114  * Copies a surface onto another surface.
115  * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
116  *
117  */
118 void cairo_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y,
119                         double dest_x, double dest_y, double dest_w, double dest_h) {
120     cairo_save(dest->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(dest->cr, CAIRO_OPERATOR_SOURCE);
126     cairo_set_source_surface(dest->cr, src->surface, src_x, src_y);
127
128     cairo_rectangle(dest->cr, dest_x, dest_y, dest_w, dest_h);
129     cairo_fill(dest->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(dest->surface);
134     cairo_restore(dest->cr);
135 }