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);
79 xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class, uint32_t mask, uint32_t *values) {
80 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
81 xcb_window_t result = xcb_generate_id(conn);
82 xcb_void_cookie_t cookie;
84 /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */
85 uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT);
87 cookie = xcb_create_window_checked(conn,
89 result, /* the window id */
90 root, /* parent == root */
91 dims.x, dims.y, dims.width, dims.height, /* dimensions */
92 0, /* border = 0, we draw our own */
94 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
97 check_error(conn, cookie, "Could not create window");
99 /* Map the window (= make it visible) */
100 xcb_map_window(conn, result);
106 * Changes a single value in the graphic context (so one doesn’t have to define an array of values)
109 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
110 xcb_change_gc(conn, gc, mask, &value);
114 * Draws a line from x,y to to_x,to_y using the given color
117 void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
118 uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y) {
119 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
120 xcb_point_t points[] = {{x, y}, {to_x, to_y}};
121 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, points);