]> git.sur5r.net Git - i3/i3/blob - src/xcb.c
Implement an internal bar which displays the workspaces
[i3/i3] / src / xcb.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * xcb.c: Helper functions for easier usage of XCB
11  *
12  */
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include <xcb/xcb.h>
19
20 #include "util.h"
21
22 /*
23  * Returns the colorpixel to use for the given hex color (think of HTML).
24  *
25  * The hex_color has to start with #, for example #FF00FF.
26  *
27  * The client argument is optional. If it is given, the colorpixel will be cached.
28  *
29  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
30  * This has to be done by the caller.
31  *
32  */
33 uint32_t get_colorpixel(xcb_connection_t *conn, Client *client, xcb_window_t window, char *hex) {
34         /* Lookup this colorpixel in the cache if a client was specified */
35         if (client != NULL) {
36                 struct Colorpixel *pixel;
37                 SLIST_FOREACH(pixel, &(client->colorpixels), colorpixels)
38                         if (strcmp(pixel->hex, hex) == 0)
39                                 return pixel->pixel;
40         }
41         #define RGB_8_TO_16(i) (65535 * ((i) & 0xFF) / 255)
42         char strgroups[3][3] = {{hex[1], hex[2], '\0'},
43                                 {hex[3], hex[4], '\0'},
44                                 {hex[5], hex[6], '\0'}};
45         int rgb16[3] = {RGB_8_TO_16(strtol(strgroups[0], NULL, 16)),
46                         RGB_8_TO_16(strtol(strgroups[1], NULL, 16)),
47                         RGB_8_TO_16(strtol(strgroups[2], NULL, 16))};
48
49         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
50
51         xcb_colormap_t colormap_id = xcb_generate_id(conn);
52         xcb_void_cookie_t cookie = xcb_create_colormap_checked(conn, XCB_COLORMAP_ALLOC_NONE,
53                                    colormap_id, window, root_screen->root_visual);
54         check_error(conn, cookie, "Could not create colormap");
55         xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(conn,
56                         xcb_alloc_color(conn, colormap_id, rgb16[0], rgb16[1], rgb16[2]), NULL);
57
58         if (!reply) {
59                 printf("Could not allocate color\n");
60                 exit(1);
61         }
62
63         uint32_t pixel = reply->pixel;
64         free(reply);
65         xcb_free_colormap(conn, colormap_id);
66
67         /* Store the result in the cache if a client was specified */
68         if (client != NULL) {
69                 struct Colorpixel *cache_pixel = scalloc(sizeof(struct Colorpixel));
70                 cache_pixel->hex = sstrdup(hex);
71                 cache_pixel->pixel = pixel;
72
73                 SLIST_INSERT_HEAD(&(client->colorpixels), cache_pixel, colorpixels);
74         }
75
76         return pixel;
77 }
78
79 /*
80  * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking
81  * for errors.
82  *
83  */
84 xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class, uint32_t mask, uint32_t *values) {
85         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
86         xcb_window_t result = xcb_generate_id(conn);
87         xcb_void_cookie_t cookie;
88
89         /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */
90         uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT);
91
92         cookie = xcb_create_window_checked(conn,
93                                            depth,
94                                            result, /* the window id */
95                                            root, /* parent == root */
96                                            dims.x, dims.y, dims.width, dims.height, /* dimensions */
97                                            0, /* border = 0, we draw our own */
98                                            window_class,
99                                            XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
100                                            mask,
101                                            values);
102         check_error(conn, cookie, "Could not create window");
103
104         /* Map the window (= make it visible) */
105         xcb_map_window(conn, result);
106
107         return result;
108 }
109
110 /*
111  * Changes a single value in the graphic context (so one doesn’t have to define an array of values)
112  *
113  */
114 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
115         xcb_change_gc(conn, gc, mask, &value);
116 }
117
118 /*
119  * Draws a line from x,y to to_x,to_y using the given color
120  *
121  */
122 void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
123                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y) {
124         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
125         xcb_point_t points[] = {{x, y}, {to_x, to_y}};
126         xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, points);
127 }
128
129 /*
130  * Draws a rectangle from x,y with width,height using the given color
131  *
132  */
133 void xcb_draw_rect(xcb_connection_t *connection, xcb_drawable_t drawable, xcb_gcontext_t gc,
134                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
135         xcb_change_gc_single(connection, gc, XCB_GC_FOREGROUND, colorpixel);
136         xcb_rectangle_t rect = {x, y, width, height};
137         xcb_poly_fill_rectangle(connection, drawable, gc, 1, &rect);
138 }