2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/xcb.c: Communicating with X
12 #include <xcb/xproto.h>
13 #include <xcb/xcb_event.h>
23 #include <X11/XKBlib.h>
24 #include <X11/extensions/XKB.h>
28 /* We save the Atoms in an easy to access array, indexed by an enum */
32 #define ATOM_DO(name) name,
33 #include "xcb_atoms.def"
36 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
37 xcb_atom_t atoms[NUM_ATOMS];
39 /* Variables, that are the same for all functions at all times */
40 xcb_connection_t *xcb_connection;
41 xcb_screen_t *xcb_screens;
42 xcb_window_t xcb_root;
49 /* Event-Watchers, to interact with the user */
56 * Converts a colorstring to a colorpixel as expected from xcb_change_gc.
57 * s is assumed to be in the format "rrggbb"
60 uint32_t get_colorpixel(const char *s) {
61 char strings[3][3] = { { s[0], s[1], '\0'} ,
63 { s[4], s[5], '\0'} };
64 uint8_t r = strtol(strings[0], NULL, 16);
65 uint8_t g = strtol(strings[1], NULL, 16);
66 uint8_t b = strtol(strings[2], NULL, 16);
67 return (r << 16 | g << 8 | b);
71 * Hides all bars (unmaps them)
75 if (!config.hide_on_modifier) {
80 SLIST_FOREACH(walk, outputs, slist) {
81 xcb_unmap_window(xcb_connection, walk->bar);
87 * Unhides all bars (maps them)
91 if (!config.hide_on_modifier) {
96 xcb_void_cookie_t cookie;
97 xcb_generic_error_t *err;
103 SLIST_FOREACH(walk, outputs, slist) {
104 if (walk->bar == XCB_NONE) {
107 mask = XCB_CONFIG_WINDOW_X |
108 XCB_CONFIG_WINDOW_Y |
109 XCB_CONFIG_WINDOW_WIDTH |
110 XCB_CONFIG_WINDOW_HEIGHT |
111 XCB_CONFIG_WINDOW_STACK_MODE;
112 values[0] = walk->rect.x;
113 values[1] = walk->rect.y + walk->rect.h - font_height - 6;
114 values[2] = walk->rect.w;
115 values[3] = font_height + 6;
116 values[4] = XCB_STACK_MODE_ABOVE;
117 printf("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
118 cookie = xcb_configure_window_checked(xcb_connection,
123 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
124 printf("ERROR: Could not reconfigure window. XCB-errorcode: %d\n", err->error_code);
127 xcb_map_window(xcb_connection, walk->bar);
132 * Handle a button-press-event (i.c. a mouse click on one of our bars).
133 * We determine, wether the click occured on a ws-button or if the scroll-
134 * wheel was used and change the workspace appropriately
137 void handle_button(xcb_button_press_event_t *event) {
140 /* Determine, which bar was clicked */
142 xcb_window_t bar = event->event;
143 SLIST_FOREACH(walk, outputs, slist) {
144 if (walk->bar == bar) {
150 printf("Unknown Bar klicked!\n");
154 /* TODO: Move this to exern get_ws_for_output() */
155 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
156 if (cur_ws->visible) {
161 if (cur_ws == NULL) {
162 printf("No Workspace active?\n");
166 int32_t x = event->event_x;
168 printf("Got Button %d\n", event->detail);
170 switch (event->detail) {
172 /* Left Mousbutton. We determine, which button was clicked
173 * and set cur_ws accordingly */
174 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
175 printf("x = %d\n", x);
176 if (x < cur_ws->name_width + 10) {
179 x -= cur_ws->name_width + 10;
181 if (cur_ws == NULL) {
186 /* Mouse wheel down. We select the next ws */
187 if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
188 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
190 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
194 /* Mouse wheel up. We select the previos ws */
195 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
196 cur_ws = TAILQ_FIRST(walk->workspaces);
198 cur_ws = TAILQ_NEXT(cur_ws, tailq);
204 snprintf(buffer, 50, "%d", cur_ws->num);
205 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
209 * This function is called immediately bevor the main loop locks. We flush xcb
210 * then (and only then)
213 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revenst) {
214 xcb_flush(xcb_connection);
218 * This function is called immediately after the main loop locks, so when one
219 * of the watchers registered an event.
220 * We check wether an X-Event arrived and handle it.
223 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
224 xcb_generic_event_t *event;
225 while ((event = xcb_poll_for_event(xcb_connection)) == NULL) {
229 switch (event->response_type & ~0x80) {
231 /* Expose-events happen, when the window needs to be redrawn */
234 case XCB_BUTTON_PRESS:
235 /* Button-press-events are mouse-buttons clicked on one of our bars */
236 handle_button((xcb_button_press_event_t*) event);
243 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
247 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
251 * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
254 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
258 printf("Got XKB-Event!\n");
260 while (XPending(xkb_dpy)) {
261 XNextEvent(xkb_dpy, (XEvent*)&ev);
263 if (ev.type != xkb_event_base) {
264 printf("ERROR: No Xkb-Event!\n");
268 if (ev.any.xkb_type != XkbStateNotify) {
269 printf("ERROR: No State Notify!\n");
273 unsigned int mods = ev.state.mods;
274 modstate = mods & Mod4Mask;
277 if (modstate != mod_pressed) {
279 printf("Mod4 got released!\n");
282 printf("Mod4 got pressed!\n");
285 mod_pressed = modstate;
290 * Calculate the rendered width of a string with the configured font.
291 * The string has to be encoded in ucs2 and glyph_len has to be the length
292 * of the string (in width)
295 int get_string_width(xcb_char2b_t *string, int glyph_len) {
296 xcb_query_text_extents_cookie_t cookie;
297 xcb_query_text_extents_reply_t *reply;
298 xcb_generic_error_t *error = NULL;
301 cookie = xcb_query_text_extents(xcb_connection, xcb_font, glyph_len, string);
302 reply = xcb_query_text_extents_reply(xcb_connection, cookie, &error);
304 printf("ERROR: Could not get text extents! XCB-errorcode: %d\n", error->error_code);
308 width = reply->overall_width;
314 * Initialize xcb and use the specified fontname for text-rendering
317 void init_xcb(char *fontname) {
318 /* FIXME: xcb_connect leaks Memory */
319 xcb_connection = xcb_connect(NULL, NULL);
320 if (xcb_connection_has_error(xcb_connection)) {
321 printf("Cannot open display\n");
324 printf("Connected to xcb\n");
326 /* We have to request the atoms we need */
327 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
328 #include "xcb_atoms.def"
330 xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
331 xcb_root = xcb_screens->root;
333 /* We load and allocate the font */
334 xcb_font = xcb_generate_id(xcb_connection);
335 xcb_void_cookie_t open_font_cookie;
336 open_font_cookie = xcb_open_font_checked(xcb_connection,
341 xcb_generic_error_t *err = xcb_request_check(xcb_connection,
345 printf("ERROR: Could not open font! XCB-Error-Code: %d\n", err->error_code);
349 /* We also need the fontheight to configure our bars accordingly */
350 xcb_list_fonts_with_info_cookie_t font_info_cookie;
351 font_info_cookie = xcb_list_fonts_with_info(xcb_connection,
356 if (config.hide_on_modifier) {
357 int xkb_major, xkb_minor, xkb_errbase, xkb_err;
358 xkb_major = XkbMajorVersion;
359 xkb_minor = XkbMinorVersion;
361 xkb_dpy = XkbOpenDisplay(":0",
368 if (xkb_dpy == NULL) {
369 printf("ERROR: No XKB!\n");
373 if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
374 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
379 if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
380 printf("ERROR: XKB not supported by X-server!\n");
384 if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
385 printf("Could not grab Key!\n");
389 xkb_io = malloc(sizeof(ev_io));
390 ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
391 ev_io_start(main_loop, xkb_io);
395 /* The varios Watchers to communicate with xcb */
396 xcb_io = malloc(sizeof(ev_io));
397 xcb_prep = malloc(sizeof(ev_prepare));
398 xcb_chk = malloc(sizeof(ev_check));
400 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
401 ev_prepare_init(xcb_prep, &xcb_prep_cb);
402 ev_check_init(xcb_chk, &xcb_chk_cb);
404 ev_io_start(main_loop, xcb_io);
405 ev_prepare_start(main_loop, xcb_prep);
406 ev_check_start(main_loop, xcb_chk);
408 /* Now we get the atoms and save them in a nice data-structure */
411 /* Now we calculate the font-height */
412 xcb_list_fonts_with_info_reply_t *reply;
413 reply = xcb_list_fonts_with_info_reply(xcb_connection,
416 font_height = reply->font_ascent + reply->font_descent;
419 printf("Calculated Font-height: %d\n", font_height);
423 * Cleanup the xcb-stuff.
424 * Called once, before the program terminates.
429 SLIST_FOREACH(walk, outputs, slist) {
430 destroy_window(walk);
432 FREE_SLIST(outputs, i3_output);
434 xcb_disconnect(xcb_connection);
436 ev_check_stop(main_loop, xcb_chk);
437 ev_prepare_stop(main_loop, xcb_prep);
438 ev_io_stop(main_loop, xcb_io);
446 * Get the earlier requested atoms and save them in the prepared data-structure
450 xcb_intern_atom_reply_t *reply;
451 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
452 if (reply == NULL) { \
453 printf("ERROR: Could not get atom %s\n", #name); \
454 exit(EXIT_FAILURE); \
456 atoms[name] = reply->atom; \
459 #include "xcb_atoms.def"
460 printf("Got Atoms\n");
464 * Destroy the bar of the specified output
467 void destroy_window(i3_output *output) {
468 if (output == NULL) {
471 if (output->bar == XCB_NONE) {
474 xcb_destroy_window(xcb_connection, output->bar);
475 output->bar = XCB_NONE;
479 * Reconfigure all bars and create new for newly activated outputs
482 void reconfig_windows() {
486 xcb_void_cookie_t cookie;
487 xcb_generic_error_t *err;
490 SLIST_FOREACH(walk, outputs, slist) {
492 /* If an output is not active, we destroy it's bar */
493 /* FIXME: Maybe we rather want to unmap? */
494 printf("Destroying window for output %s\n", walk->name);
495 destroy_window(walk);
498 if (walk->bar == XCB_NONE) {
499 printf("Creating Window for output %s\n", walk->name);
501 walk->bar = xcb_generate_id(xcb_connection);
502 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
503 /* Black background */
504 values[0] = xcb_screens->black_pixel;
505 /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
506 values[1] = config.hide_on_modifier;
507 /* The events we want to receive */
508 values[2] = XCB_EVENT_MASK_EXPOSURE |
509 XCB_EVENT_MASK_BUTTON_PRESS;
510 cookie = xcb_create_window_checked(xcb_connection,
511 xcb_screens->root_depth,
514 walk->rect.x, walk->rect.y,
515 walk->rect.w, font_height + 6,
517 XCB_WINDOW_CLASS_INPUT_OUTPUT,
518 xcb_screens->root_visual,
521 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
522 printf("ERROR: Could not create Window. XCB-errorcode: %d\n", err->error_code);
526 /* We want dock-windows (for now) */
527 xcb_change_property(xcb_connection,
528 XCB_PROP_MODE_REPLACE,
530 atoms[_NET_WM_WINDOW_TYPE],
534 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
535 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
536 printf("ERROR: Could not set dock mode. XCB-errorcode: %d\n", err->error_code);
540 /* We also want a graphics-context (the "canvas" on which we draw) */
541 walk->bargc = xcb_generate_id(xcb_connection);
543 values[0] = xcb_font;
544 cookie = xcb_create_gc_checked(xcb_connection,
550 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
551 printf("ERROR: Could not create graphical context. XCB-errorcode: %d\n", err->error_code);
555 /* We finally map the bar (display it on screen) */
556 cookie = xcb_map_window_checked(xcb_connection, walk->bar);
558 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
559 printf("ERROR: Could not map window. XCB-errorcode: %d\n", err->error_code);
563 /* We already have a bar, so we just reconfigure it */
564 mask = XCB_CONFIG_WINDOW_X |
565 XCB_CONFIG_WINDOW_Y |
566 XCB_CONFIG_WINDOW_WIDTH |
567 XCB_CONFIG_WINDOW_HEIGHT |
568 XCB_CONFIG_WINDOW_STACK_MODE;
569 values[0] = walk->rect.x;
570 values[1] = walk->rect.y + walk->rect.h - font_height - 6;
571 values[2] = walk->rect.w;
572 values[3] = font_height + 6;
573 values[4] = XCB_STACK_MODE_ABOVE;
574 printf("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
575 cookie = xcb_configure_window_checked(xcb_connection,
580 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
581 printf("ERROR: Could not reconfigure window. XCB-errorcode: %d\n", err->error_code);
589 * Render the bars, with buttons and statusline
593 printf("Drawing Bars...\n");
595 i3_output *outputs_walk;
596 SLIST_FOREACH(outputs_walk, outputs, slist) {
597 if (!outputs_walk->active) {
598 printf("Output %s inactive, skipping...\n", outputs_walk->name);
601 if (outputs_walk->bar == XCB_NONE) {
604 uint32_t color = get_colorpixel("000000");
605 xcb_change_gc(xcb_connection,
609 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
610 xcb_poly_fill_rectangle(xcb_connection,
615 if (statusline != NULL) {
616 printf("Printing statusline!\n");
617 xcb_change_gc(xcb_connection,
621 color = get_colorpixel("FFFFFF");
622 xcb_change_gc(xcb_connection,
628 xcb_char2b_t *text = (xcb_char2b_t*) convert_utf8_to_ucs2(statusline, &glyph_count);
630 xcb_void_cookie_t cookie;
631 cookie = xcb_image_text_16(xcb_connection,
635 outputs_walk->rect.w - get_string_width(text, glyph_count) - 4,
637 (xcb_char2b_t*) text);
639 xcb_generic_error_t *err = xcb_request_check(xcb_connection, cookie);
642 printf("XCB-Error: %d\n", err->error_code);
646 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
647 printf("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
648 uint32_t color = get_colorpixel("240000");
649 if (ws_walk->visible) {
650 color = get_colorpixel("480000");
652 if (ws_walk->urgent) {
653 printf("WS %s is urgent!\n", ws_walk->name);
654 color = get_colorpixel("002400");
655 /* The urgent-hint should get noticed, so we unhide the bars shortly */
658 xcb_change_gc(xcb_connection,
662 xcb_change_gc(xcb_connection,
666 xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
667 xcb_poly_fill_rectangle(xcb_connection,
672 color = get_colorpixel("FFFFFF");
673 xcb_change_gc(xcb_connection,
677 xcb_image_text_16(xcb_connection,
678 ws_walk->name_glyphs,
681 i + 5, font_height + 1,
683 i += 10 + ws_walk->name_width;