4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * xcb.c: Helper functions for easier usage of XCB
23 * Returns the colorpixel to use for the given hex color (think of HTML).
25 * The hex_color has to start with #, for example #FF00FF.
27 * The client argument is optional. If it is given, the colorpixel will be cached.
29 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
30 * This has to be done by the caller.
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 */
36 struct Colorpixel *pixel;
37 SLIST_FOREACH(pixel, &(client->colorpixels), colorpixels)
38 if (strcmp(pixel->hex, hex) == 0)
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))};
49 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
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);
59 printf("Could not allocate color\n");
63 uint32_t pixel = reply->pixel;
65 xcb_free_colormap(conn, colormap_id);
67 /* Store the result in the cache if a client was specified */
69 struct Colorpixel *cache_pixel = scalloc(sizeof(struct Colorpixel));
70 cache_pixel->hex = sstrdup(hex);
71 cache_pixel->pixel = pixel;
73 SLIST_INSERT_HEAD(&(client->colorpixels), cache_pixel, colorpixels);
80 * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking
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;
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);
92 cookie = xcb_create_window_checked(conn,
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 */
99 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
102 check_error(conn, cookie, "Could not create window");
104 /* Map the window (= make it visible) */
105 xcb_map_window(conn, result);
111 * Changes a single value in the graphic context (so one doesn’t have to define an array of values)
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);
119 * Draws a line from x,y to to_x,to_y using the given color
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);
130 * Draws a rectangle from x,y with width,height using the given color
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);