2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2011 Axel Wagner and contributors (see also: LICENSE)
7 * xcb.c: Communicating with X
11 #include <xcb/xproto.h>
12 #include <xcb/xcb_atom.h>
15 #include "xcb_compat.h"
30 #include <X11/XKBlib.h>
31 #include <X11/extensions/XKB.h>
36 /* We save the Atoms in an easy to access array, indexed by an enum */
38 #define ATOM_DO(name) name,
39 #include "xcb_atoms.def"
43 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
44 xcb_atom_t atoms[NUM_ATOMS];
46 /* Variables, that are the same for all functions at all times */
47 xcb_connection_t *xcb_connection;
49 xcb_screen_t *xcb_screen;
50 xcb_window_t xcb_root;
52 /* This is needed for integration with libi3 */
53 xcb_connection_t *conn;
55 /* The font we'll use */
58 /* These are only relevant for XKB, which we only need for grabbing modifiers */
63 /* Because the statusline is the same on all outputs, we have
64 * global buffer to render it on */
65 xcb_gcontext_t statusline_ctx;
66 xcb_gcontext_t statusline_clear;
67 xcb_pixmap_t statusline_pm;
68 uint32_t statusline_width;
70 /* Event-Watchers, to interact with the user */
76 /* The parsed colors */
80 uint32_t active_ws_fg;
81 uint32_t active_ws_bg;
82 uint32_t active_ws_border;
83 uint32_t inactive_ws_fg;
84 uint32_t inactive_ws_bg;
85 uint32_t inactive_ws_border;
86 uint32_t urgent_ws_bg;
87 uint32_t urgent_ws_fg;
88 uint32_t urgent_ws_border;
91 uint32_t focus_ws_border;
93 struct xcb_colors_t colors;
95 /* We define xcb_request_failed as a macro to include the relevant line-number */
96 #define xcb_request_failed(cookie, err_msg) _xcb_request_failed(cookie, err_msg, __LINE__)
97 int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
98 xcb_generic_error_t *err;
99 if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
100 fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
101 return err->error_code;
107 * Redraws the statusline to the buffer
110 void refresh_statusline() {
113 if (statusline == NULL) {
117 xcb_char2b_t *text = (xcb_char2b_t*)convert_utf8_to_ucs2(statusline, &glyph_count);
118 uint32_t old_statusline_width = statusline_width;
119 statusline_width = predict_text_width((char*)text, glyph_count, true);
120 /* If the statusline is bigger than our screen we need to make sure that
121 * the pixmap provides enough space, so re-allocate if the width grew */
122 if (statusline_width > xcb_screen->width_in_pixels &&
123 statusline_width > old_statusline_width)
126 xcb_rectangle_t rect = { 0, 0, xcb_screen->width_in_pixels, font.height };
127 xcb_poly_fill_rectangle(xcb_connection, statusline_pm, statusline_clear, 1, &rect);
128 set_font_colors(statusline_ctx, colors.bar_fg, colors.bar_bg);
129 draw_text((char*)text, glyph_count, true, statusline_pm, statusline_ctx,
130 0, 0, xcb_screen->width_in_pixels);
136 * Hides all bars (unmaps them)
140 if (!config.hide_on_modifier) {
145 SLIST_FOREACH(walk, outputs, slist) {
149 xcb_unmap_window(xcb_connection, walk->bar);
155 * Unhides all bars (maps them)
159 if (!config.hide_on_modifier) {
164 xcb_void_cookie_t cookie;
170 SLIST_FOREACH(walk, outputs, slist) {
171 if (walk->bar == XCB_NONE) {
174 mask = XCB_CONFIG_WINDOW_X |
175 XCB_CONFIG_WINDOW_Y |
176 XCB_CONFIG_WINDOW_WIDTH |
177 XCB_CONFIG_WINDOW_HEIGHT |
178 XCB_CONFIG_WINDOW_STACK_MODE;
179 values[0] = walk->rect.x;
180 if (config.position == POS_TOP)
181 values[1] = walk->rect.y;
182 else values[1] = walk->rect.y + walk->rect.h - font.height - 6;
183 values[2] = walk->rect.w;
184 values[3] = font.height + 6;
185 values[4] = XCB_STACK_MODE_ABOVE;
186 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
187 cookie = xcb_configure_window_checked(xcb_connection,
192 if (xcb_request_failed(cookie, "Could not reconfigure window")) {
195 xcb_map_window(xcb_connection, walk->bar);
200 * Parse the colors into a format that we can use
203 void init_colors(const struct xcb_color_strings_t *new_colors) {
204 #define PARSE_COLOR(name, def) \
206 colors.name = get_colorpixel(new_colors->name ? new_colors->name : def); \
208 PARSE_COLOR(bar_fg, "#FFFFFF");
209 PARSE_COLOR(bar_bg, "#000000");
210 PARSE_COLOR(active_ws_fg, "#FFFFFF");
211 PARSE_COLOR(active_ws_bg, "#333333");
212 PARSE_COLOR(active_ws_border, "#333333");
213 PARSE_COLOR(inactive_ws_fg, "#888888");
214 PARSE_COLOR(inactive_ws_bg, "#222222");
215 PARSE_COLOR(inactive_ws_border, "#333333");
216 PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
217 PARSE_COLOR(urgent_ws_bg, "#900000");
218 PARSE_COLOR(urgent_ws_border, "#2f343a");
219 PARSE_COLOR(focus_ws_fg, "#FFFFFF");
220 PARSE_COLOR(focus_ws_bg, "#285577");
221 PARSE_COLOR(focus_ws_border, "#4c7899");
226 * Handle a button-press-event (i.e. a mouse click on one of our bars).
227 * We determine, whether the click occured on a ws-button or if the scroll-
228 * wheel was used and change the workspace appropriately
231 void handle_button(xcb_button_press_event_t *event) {
234 /* Determine, which bar was clicked */
236 xcb_window_t bar = event->event;
237 SLIST_FOREACH(walk, outputs, slist) {
238 if (walk->bar == bar) {
244 DLOG("Unknown Bar klicked!\n");
248 /* TODO: Move this to extern get_ws_for_output() */
249 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
250 if (cur_ws->visible) {
255 if (cur_ws == NULL) {
256 DLOG("No Workspace active?\n");
260 int32_t x = event->event_x;
262 DLOG("Got Button %d\n", event->detail);
264 switch (event->detail) {
266 /* Left Mousbutton. We determine, which button was clicked
267 * and set cur_ws accordingly */
268 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
270 if (x < cur_ws->name_width + 10) {
273 x -= cur_ws->name_width + 10;
275 if (cur_ws == NULL) {
280 /* Mouse wheel down. We select the next ws */
281 if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
282 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
284 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
288 /* Mouse wheel up. We select the previos ws */
289 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
290 cur_ws = TAILQ_FIRST(walk->workspaces);
292 cur_ws = TAILQ_NEXT(cur_ws, tailq);
297 const size_t len = strlen(cur_ws->name) + strlen("workspace \"\"") + 1;
299 snprintf(buffer, len, "workspace \"%s\"", cur_ws->name);
300 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
304 * Configures the x coordinate of all trayclients. To be called after adding a
305 * new tray client or removing an old one.
308 static void configure_trayclients() {
309 trayclient *trayclient;
311 SLIST_FOREACH(output, outputs, slist) {
316 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
317 if (!trayclient->mapped)
321 DLOG("Configuring tray window %08x to x=%d\n",
322 trayclient->win, output->rect.w - (clients * (font.height + 2)));
323 uint32_t x = output->rect.w - (clients * (font.height + 2));
324 xcb_configure_window(xcb_connection,
333 * Handles ClientMessages (messages sent from another client directly to us).
335 * At the moment, only the tray window will receive client messages. All
336 * supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
339 static void handle_client_message(xcb_client_message_event_t* event) {
340 if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
341 event->format == 32) {
342 DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
343 /* event->data.data32[0] is the timestamp */
344 uint32_t op = event->data.data32[1];
347 if (op == SYSTEM_TRAY_REQUEST_DOCK) {
348 xcb_window_t client = event->data.data32[2];
350 /* Listen for PropertyNotify events to get the most recent value of
351 * the XEMBED_MAPPED atom, also listen for UnmapNotify events */
352 mask = XCB_CW_EVENT_MASK;
353 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
354 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
355 xcb_change_window_attributes(xcb_connection,
360 /* Request the _XEMBED_INFO property. The XEMBED specification
361 * (which is referred by the tray specification) says this *has* to
362 * be set, but VLC does not set it… */
365 xcb_get_property_cookie_t xembedc;
366 xembedc = xcb_get_property_unchecked(xcb_connection,
370 XCB_GET_PROPERTY_TYPE_ANY,
374 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
377 if (xembedr != NULL && xembedr->length != 0) {
378 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
379 uint32_t *xembed = xcb_get_property_value(xembedr);
380 DLOG("xembed version = %d\n", xembed[0]);
381 DLOG("xembed flags = %d\n", xembed[1]);
382 map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
383 xe_version = xembed[0];
388 ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
391 DLOG("X window %08x requested docking\n", client);
392 i3_output *walk, *output = NULL;
393 SLIST_FOREACH(walk, outputs, slist) {
396 if (config.tray_output &&
397 strcasecmp(walk->name, config.tray_output) != 0)
399 DLOG("using output %s\n", walk->name);
402 if (output == NULL) {
403 ELOG("No output found\n");
406 xcb_reparent_window(xcb_connection,
409 output->rect.w - font.height - 2,
411 /* We reconfigure the window to use a reasonable size. The systray
412 * specification explicitly says:
413 * Tray icons may be assigned any size by the system tray, and
414 * should do their best to cope with any size effectively
416 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
417 values[0] = font.height;
418 values[1] = font.height;
419 xcb_configure_window(xcb_connection,
424 /* send the XEMBED_EMBEDDED_NOTIFY message */
425 void *event = scalloc(32);
426 xcb_client_message_event_t *ev = event;
427 ev->response_type = XCB_CLIENT_MESSAGE;
429 ev->type = atoms[_XEMBED];
431 ev->data.data32[0] = XCB_CURRENT_TIME;
432 ev->data.data32[1] = atoms[XEMBED_EMBEDDED_NOTIFY];
433 ev->data.data32[2] = output->bar;
434 ev->data.data32[3] = xe_version;
435 xcb_send_event(xcb_connection,
438 XCB_EVENT_MASK_NO_EVENT,
442 /* Put the client inside the save set. Upon termination (whether
443 * killed or normal exit does not matter) of i3bar, these clients
444 * will be correctly reparented to their most closest living
445 * ancestor. Without this, tray icons might die when i3bar
447 xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
450 DLOG("Mapping dock client\n");
451 xcb_map_window(xcb_connection, client);
453 DLOG("Not mapping dock client yet\n");
455 trayclient *tc = smalloc(sizeof(trayclient));
458 tc->xe_version = xe_version;
459 TAILQ_INSERT_TAIL(output->trayclients, tc, tailq);
461 /* Trigger an update to copy the statusline text to the appropriate
463 configure_trayclients();
470 * Handles UnmapNotify events. These events happen when a tray window unmaps
471 * itself. We then update our data structure
474 static void handle_unmap_notify(xcb_unmap_notify_event_t* event) {
475 DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
478 SLIST_FOREACH(walk, outputs, slist) {
481 DLOG("checking output %s\n", walk->name);
482 trayclient *trayclient;
483 TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
484 if (trayclient->win != event->window)
487 DLOG("Removing tray client with window ID %08x\n", event->window);
488 TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
490 /* Trigger an update, we now have more space for the statusline */
491 configure_trayclients();
499 * Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
500 * handled, which tells us whether a dock client should be mapped or unmapped.
503 static void handle_property_notify(xcb_property_notify_event_t *event) {
504 DLOG("PropertyNotify\n");
505 if (event->atom == atoms[_XEMBED_INFO] &&
506 event->state == XCB_PROPERTY_NEW_VALUE) {
507 DLOG("xembed_info updated\n");
508 trayclient *trayclient = NULL, *walk;
510 SLIST_FOREACH(o_walk, outputs, slist) {
514 TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
515 if (walk->win != event->window)
525 ELOG("PropertyNotify received for unknown window %08x\n",
529 xcb_get_property_cookie_t xembedc;
530 xembedc = xcb_get_property_unchecked(xcb_connection,
534 XCB_GET_PROPERTY_TYPE_ANY,
538 xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
541 if (xembedr == NULL || xembedr->length == 0) {
542 DLOG("xembed_info unset\n");
546 DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
547 uint32_t *xembed = xcb_get_property_value(xembedr);
548 DLOG("xembed version = %d\n", xembed[0]);
549 DLOG("xembed flags = %d\n", xembed[1]);
550 bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
551 DLOG("map-state now %d\n", map_it);
552 if (trayclient->mapped && !map_it) {
553 /* need to unmap the window */
554 xcb_unmap_window(xcb_connection, trayclient->win);
555 trayclient->mapped = map_it;
556 configure_trayclients();
558 } else if (!trayclient->mapped && map_it) {
559 /* need to map the window */
560 xcb_map_window(xcb_connection, trayclient->win);
561 trayclient->mapped = map_it;
562 configure_trayclients();
570 * Handle ConfigureRequests by denying them and sending the client a
571 * ConfigureNotify with its actual size.
574 static void handle_configure_request(xcb_configure_request_event_t *event) {
575 DLOG("ConfigureRequest for window = %08x\n", event->window);
577 trayclient *trayclient;
579 SLIST_FOREACH(output, outputs, slist) {
584 TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
585 if (!trayclient->mapped)
589 if (trayclient->win != event->window)
592 xcb_rectangle_t rect;
593 rect.x = output->rect.w - (clients * (font.height + 2));
595 rect.width = font.height;
596 rect.height = font.height;
598 DLOG("This is a tray window. x = %d\n", rect.x);
599 fake_configure_notify(xcb_connection, rect, event->window, 0);
604 DLOG("WARNING: Could not find corresponding tray window.\n");
608 * This function is called immediately before the main loop locks. We flush xcb
609 * then (and only then)
612 void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) {
613 xcb_flush(xcb_connection);
617 * This function is called immediately after the main loop locks, so when one
618 * of the watchers registered an event.
619 * We check whether an X-Event arrived and handle it.
622 void xcb_chk_cb(struct ev_loop *loop, ev_check *watcher, int revents) {
623 xcb_generic_event_t *event;
625 if (xcb_connection_has_error(xcb_connection)) {
626 ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n");
630 while ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
631 switch (event->response_type & ~0x80) {
633 /* Expose-events happen, when the window needs to be redrawn */
636 case XCB_BUTTON_PRESS:
637 /* Button-press-events are mouse-buttons clicked on one of our bars */
638 handle_button((xcb_button_press_event_t*) event);
640 case XCB_CLIENT_MESSAGE:
641 /* Client messages are used for client-to-client communication, for
642 * example system tray widgets talk to us directly via client messages. */
643 handle_client_message((xcb_client_message_event_t*) event);
645 case XCB_UNMAP_NOTIFY:
646 case XCB_DESTROY_NOTIFY:
647 /* UnmapNotifies are received when a tray window unmaps itself */
648 handle_unmap_notify((xcb_unmap_notify_event_t*) event);
650 case XCB_PROPERTY_NOTIFY:
652 handle_property_notify((xcb_property_notify_event_t*) event);
654 case XCB_CONFIGURE_REQUEST:
655 /* ConfigureRequest, sent by a tray child */
656 handle_configure_request((xcb_configure_request_event_t*) event);
664 * Dummy Callback. We only need this, so that the Prepare- and Check-Watchers
668 void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
672 * We need to bind to the modifier per XKB. Sadly, XCB does not implement this
675 void xkb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
679 DLOG("Got XKB-Event!\n");
681 while (XPending(xkb_dpy)) {
682 XNextEvent(xkb_dpy, (XEvent*)&ev);
684 if (ev.type != xkb_event_base) {
685 ELOG("No Xkb-Event!\n");
689 if (ev.any.xkb_type != XkbStateNotify) {
690 ELOG("No State Notify!\n");
694 unsigned int mods = ev.state.mods;
695 modstate = mods & config.modifier;
698 #define DLOGMOD(modmask, status, barfunc) \
702 DLOG("ShiftMask got " #status "!\n"); \
705 DLOG("ControlMask got " #status "!\n"); \
708 DLOG("Mod1Mask got " #status "!\n"); \
711 DLOG("Mod2Mask got " #status "!\n"); \
714 DLOG("Mod3Mask got " #status "!\n"); \
717 DLOG("Mod4Mask got " #status "!\n"); \
720 DLOG("Mod5Mask got " #status "!\n"); \
726 if (modstate != mod_pressed) {
728 DLOGMOD(config.modifier, released, hide_bars);
730 DLOGMOD(config.modifier, pressed, unhide_bars);
732 mod_pressed = modstate;
739 * Early initialization of the connection to X11: Everything which does not
740 * depend on 'config'.
743 char *init_xcb_early() {
744 /* FIXME: xcb_connect leaks Memory */
745 xcb_connection = xcb_connect(NULL, &screen);
746 if (xcb_connection_has_error(xcb_connection)) {
747 ELOG("Cannot open display\n");
750 conn = xcb_connection;
751 DLOG("Connected to xcb\n");
753 /* We have to request the atoms we need */
754 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
755 #include "xcb_atoms.def"
757 xcb_screen = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
758 xcb_root = xcb_screen->root;
760 /* We draw the statusline to a seperate pixmap, because it looks the same on all bars and
761 * this way, we can choose to crop it */
762 uint32_t mask = XCB_GC_FOREGROUND;
763 uint32_t vals[] = { colors.bar_bg, colors.bar_bg };
765 statusline_clear = xcb_generate_id(xcb_connection);
766 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
772 statusline_ctx = xcb_generate_id(xcb_connection);
773 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
779 statusline_pm = xcb_generate_id(xcb_connection);
780 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
781 xcb_screen->root_depth,
784 xcb_screen->width_in_pixels,
785 xcb_screen->height_in_pixels);
788 /* The various Watchers to communicate with xcb */
789 xcb_io = smalloc(sizeof(ev_io));
790 xcb_prep = smalloc(sizeof(ev_prepare));
791 xcb_chk = smalloc(sizeof(ev_check));
793 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
794 ev_prepare_init(xcb_prep, &xcb_prep_cb);
795 ev_check_init(xcb_chk, &xcb_chk_cb);
797 ev_io_start(main_loop, xcb_io);
798 ev_prepare_start(main_loop, xcb_prep);
799 ev_check_start(main_loop, xcb_chk);
801 /* Now we get the atoms and save them in a nice data structure */
804 xcb_get_property_cookie_t path_cookie;
805 path_cookie = xcb_get_property_unchecked(xcb_connection,
808 atoms[I3_SOCKET_PATH],
809 XCB_GET_PROPERTY_TYPE_ANY,
812 /* We check, if i3 set its socket-path */
813 xcb_get_property_reply_t *path_reply = xcb_get_property_reply(xcb_connection,
818 int len = xcb_get_property_value_length(path_reply);
820 path = strndup(xcb_get_property_value(path_reply), len);
825 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
826 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
827 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
835 * Initialization which depends on 'config' being usable. Called after the
836 * configuration has arrived.
839 void init_xcb_late(char *fontname) {
840 if (fontname == NULL)
841 fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
844 font = load_font(fontname, true);
846 DLOG("Calculated Font-height: %d\n", font.height);
848 xcb_flush(xcb_connection);
850 /* To grab modifiers without blocking other applications from receiving key-events
851 * involving that modifier, we sadly have to use xkb which is not yet fully supported
853 if (config.hide_on_modifier) {
854 int xkb_major, xkb_minor, xkb_errbase, xkb_err;
855 xkb_major = XkbMajorVersion;
856 xkb_minor = XkbMinorVersion;
858 xkb_dpy = XkbOpenDisplay(NULL,
865 if (xkb_dpy == NULL) {
870 if (fcntl(ConnectionNumber(xkb_dpy), F_SETFD, FD_CLOEXEC) == -1) {
871 ELOG("Could not set FD_CLOEXEC on xkbdpy: %s\n", strerror(errno));
876 if (!XkbQueryExtension(xkb_dpy, &i1, &xkb_event_base, &xkb_errbase, &xkb_major, &xkb_minor)) {
877 ELOG("XKB not supported by X-server!\n");
881 if (!XkbSelectEvents(xkb_dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask)) {
882 ELOG("Could not grab Key!\n");
886 xkb_io = smalloc(sizeof(ev_io));
887 ev_io_init(xkb_io, &xkb_io_cb, ConnectionNumber(xkb_dpy), EV_READ);
888 ev_io_start(main_loop, xkb_io);
894 * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
895 * for the X11 display we are running on, then acquiring the selection for this
896 * atom. Afterwards, tray clients will send ClientMessages to our window.
900 DLOG("Initializing system tray functionality\n");
901 /* request the tray manager atom for the X11 display we are running on */
902 char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
903 snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
904 xcb_intern_atom_cookie_t tray_cookie;
905 xcb_intern_atom_reply_t *tray_reply;
906 tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
908 /* tray support: we need a window to own the selection */
909 xcb_window_t selwin = xcb_generate_id(xcb_connection);
910 uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
911 uint32_t selval[] = { 1 };
912 xcb_create_window(xcb_connection,
913 xcb_screen->root_depth,
919 XCB_WINDOW_CLASS_INPUT_OUTPUT,
920 xcb_screen->root_visual,
924 uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
926 xcb_change_property(xcb_connection,
927 XCB_PROP_MODE_REPLACE,
929 atoms[_NET_SYSTEM_TRAY_ORIENTATION],
935 if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
936 ELOG("Could not get atom %s\n", atomname);
940 xcb_set_selection_owner(xcb_connection,
945 /* Verify that we have the selection */
946 xcb_get_selection_owner_cookie_t selcookie;
947 xcb_get_selection_owner_reply_t *selreply;
949 selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom);
950 if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) {
951 ELOG("Could not get selection owner for %s\n", atomname);
955 if (selreply->owner != selwin) {
956 ELOG("Could not set the %s selection. " \
957 "Maybe another tray is already running?\n", atomname);
958 /* NOTE that this error is not fatal. We just can’t provide tray
964 /* Inform clients waiting for a new _NET_SYSTEM_TRAY that we are here */
965 void *event = scalloc(32);
966 xcb_client_message_event_t *ev = event;
967 ev->response_type = XCB_CLIENT_MESSAGE;
968 ev->window = xcb_root;
969 ev->type = atoms[MANAGER];
971 ev->data.data32[0] = XCB_CURRENT_TIME;
972 ev->data.data32[1] = tray_reply->atom;
973 ev->data.data32[2] = selwin;
974 xcb_send_event(xcb_connection,
977 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
984 * Cleanup the xcb-stuff.
985 * Called once, before the program terminates.
990 trayclient *trayclient;
992 SLIST_FOREACH(o_walk, outputs, slist) {
993 TAILQ_FOREACH(trayclient, o_walk->trayclients, tailq) {
994 /* Unmap, then reparent (to root) the tray client windows */
995 xcb_unmap_window(xcb_connection, trayclient->win);
996 xcb_reparent_window(xcb_connection,
1002 destroy_window(o_walk);
1003 FREE(o_walk->trayclients);
1004 FREE(o_walk->workspaces);
1007 FREE_SLIST(outputs, i3_output);
1010 xcb_flush(xcb_connection);
1011 xcb_disconnect(xcb_connection);
1013 ev_check_stop(main_loop, xcb_chk);
1014 ev_prepare_stop(main_loop, xcb_prep);
1015 ev_io_stop(main_loop, xcb_io);
1023 * Get the earlier requested atoms and save them in the prepared data structure
1027 xcb_intern_atom_reply_t *reply;
1028 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
1029 if (reply == NULL) { \
1030 ELOG("Could not get atom %s\n", #name); \
1031 exit(EXIT_FAILURE); \
1033 atoms[name] = reply->atom; \
1036 #include "xcb_atoms.def"
1037 DLOG("Got Atoms\n");
1041 * Destroy the bar of the specified output
1044 void destroy_window(i3_output *output) {
1045 if (output == NULL) {
1048 if (output->bar == XCB_NONE) {
1051 xcb_destroy_window(xcb_connection, output->bar);
1052 output->bar = XCB_NONE;
1056 * Reallocate the statusline-buffer
1059 void realloc_sl_buffer() {
1060 DLOG("Re-allocating statusline-buffer, statusline_width = %d, xcb_screen->width_in_pixels = %d\n",
1061 statusline_width, xcb_screen->width_in_pixels);
1062 xcb_free_pixmap(xcb_connection, statusline_pm);
1063 statusline_pm = xcb_generate_id(xcb_connection);
1064 xcb_void_cookie_t sl_pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1065 xcb_screen->root_depth,
1068 MAX(xcb_screen->width_in_pixels, statusline_width),
1069 xcb_screen->height_in_pixels);
1071 uint32_t mask = XCB_GC_FOREGROUND;
1072 uint32_t vals[2] = { colors.bar_bg, colors.bar_bg };
1073 xcb_free_gc(xcb_connection, statusline_clear);
1074 statusline_clear = xcb_generate_id(xcb_connection);
1075 xcb_void_cookie_t clear_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1081 mask |= XCB_GC_BACKGROUND;
1082 vals[0] = colors.bar_fg;
1083 statusline_ctx = xcb_generate_id(xcb_connection);
1084 xcb_free_gc(xcb_connection, statusline_ctx);
1085 xcb_void_cookie_t sl_ctx_cookie = xcb_create_gc_checked(xcb_connection,
1091 if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") ||
1092 xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") ||
1093 xcb_request_failed(sl_ctx_cookie, "Could not allocate statusline-buffer-context")) {
1100 * Reconfigure all bars and create new bars for recently activated outputs
1103 void reconfig_windows() {
1106 static bool tray_configured = false;
1109 SLIST_FOREACH(walk, outputs, slist) {
1110 if (!walk->active) {
1111 /* If an output is not active, we destroy its bar */
1112 /* FIXME: Maybe we rather want to unmap? */
1113 DLOG("Destroying window for output %s\n", walk->name);
1114 destroy_window(walk);
1117 if (walk->bar == XCB_NONE) {
1118 DLOG("Creating Window for output %s\n", walk->name);
1120 walk->bar = xcb_generate_id(xcb_connection);
1121 walk->buffer = xcb_generate_id(xcb_connection);
1122 mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
1123 /* Black background */
1124 values[0] = colors.bar_bg;
1125 /* If hide_on_modifier is set, i3 is not supposed to manage our bar-windows */
1126 values[1] = config.hide_on_modifier;
1127 /* We enable the following EventMask fields:
1128 * EXPOSURE, to get expose events (we have to re-draw then)
1129 * SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray
1130 * child windows use ConfigureWindow
1131 * BUTTON_PRESS, to handle clicks on the workspace buttons
1133 values[2] = XCB_EVENT_MASK_EXPOSURE |
1134 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
1135 if (!config.disable_ws) {
1136 values[2] |= XCB_EVENT_MASK_BUTTON_PRESS;
1138 xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection,
1139 xcb_screen->root_depth,
1142 walk->rect.x, walk->rect.y + walk->rect.h - font.height - 6,
1143 walk->rect.w, font.height + 6,
1145 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1146 xcb_screen->root_visual,
1150 /* The double-buffer we use to render stuff off-screen */
1151 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1152 xcb_screen->root_depth,
1158 /* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */
1159 xcb_void_cookie_t class_cookie;
1160 class_cookie = xcb_change_property(xcb_connection,
1161 XCB_PROP_MODE_REPLACE,
1166 (strlen("i3bar") + 1) * 2,
1170 if (asprintf(&name, "i3bar for output %s", walk->name) == -1)
1171 err(EXIT_FAILURE, "asprintf()");
1172 xcb_void_cookie_t name_cookie;
1173 name_cookie = xcb_change_property(xcb_connection,
1174 XCB_PROP_MODE_REPLACE,
1183 /* We want dock-windows (for now). When override_redirect is set, i3 is ignoring
1185 xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection,
1186 XCB_PROP_MODE_REPLACE,
1188 atoms[_NET_WM_WINDOW_TYPE],
1192 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
1194 /* We need to tell i3, where to reserve space for i3bar */
1195 /* left, right, top, bottom, left_start_y, left_end_y,
1196 * right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x,
1198 /* A local struct to save the strut_partial property */
1204 uint32_t left_start_y;
1205 uint32_t left_end_y;
1206 uint32_t right_start_y;
1207 uint32_t right_end_y;
1208 uint32_t top_start_x;
1210 uint32_t bottom_start_x;
1211 uint32_t bottom_end_x;
1212 } __attribute__((__packed__)) strut_partial = {0,};
1213 switch (config.position) {
1217 strut_partial.top = font.height + 6;
1218 strut_partial.top_start_x = walk->rect.x;
1219 strut_partial.top_end_x = walk->rect.x + walk->rect.w;
1222 strut_partial.bottom = font.height + 6;
1223 strut_partial.bottom_start_x = walk->rect.x;
1224 strut_partial.bottom_end_x = walk->rect.x + walk->rect.w;
1227 xcb_void_cookie_t strut_cookie = xcb_change_property(xcb_connection,
1228 XCB_PROP_MODE_REPLACE,
1230 atoms[_NET_WM_STRUT_PARTIAL],
1236 /* We also want a graphics-context for the bars (it defines the properties
1237 * with which we draw to them) */
1238 walk->bargc = xcb_generate_id(xcb_connection);
1239 xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection,
1245 /* We finally map the bar (display it on screen), unless the modifier-switch is on */
1246 xcb_void_cookie_t map_cookie;
1247 if (!config.hide_on_modifier) {
1248 map_cookie = xcb_map_window_checked(xcb_connection, walk->bar);
1251 if (xcb_request_failed(win_cookie, "Could not create window") ||
1252 xcb_request_failed(pm_cookie, "Could not create pixmap") ||
1253 xcb_request_failed(dock_cookie, "Could not set dock mode") ||
1254 xcb_request_failed(class_cookie, "Could not set WM_CLASS") ||
1255 xcb_request_failed(name_cookie, "Could not set WM_NAME") ||
1256 xcb_request_failed(strut_cookie, "Could not set strut") ||
1257 xcb_request_failed(gc_cookie, "Could not create graphical context") ||
1258 (!config.hide_on_modifier && xcb_request_failed(map_cookie, "Could not map window"))) {
1262 if (!tray_configured &&
1263 (!config.tray_output ||
1264 strcasecmp("none", config.tray_output) != 0)) {
1266 tray_configured = true;
1269 /* We already have a bar, so we just reconfigure it */
1270 mask = XCB_CONFIG_WINDOW_X |
1271 XCB_CONFIG_WINDOW_Y |
1272 XCB_CONFIG_WINDOW_WIDTH |
1273 XCB_CONFIG_WINDOW_HEIGHT |
1274 XCB_CONFIG_WINDOW_STACK_MODE;
1275 values[0] = walk->rect.x;
1276 values[1] = walk->rect.y + walk->rect.h - font.height - 6;
1277 values[2] = walk->rect.w;
1278 values[3] = font.height + 6;
1279 values[4] = XCB_STACK_MODE_ABOVE;
1281 DLOG("Destroying buffer for output %s", walk->name);
1282 xcb_free_pixmap(xcb_connection, walk->buffer);
1284 DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
1285 xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection,
1290 DLOG("Recreating buffer for output %s", walk->name);
1291 xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
1292 xcb_screen->root_depth,
1298 if (xcb_request_failed(cfg_cookie, "Could not reconfigure window")) {
1301 if (xcb_request_failed(pm_cookie, "Could not create pixmap")) {
1309 * Render the bars, with buttons and statusline
1313 DLOG("Drawing Bars...\n");
1316 refresh_statusline();
1318 i3_output *outputs_walk;
1319 SLIST_FOREACH(outputs_walk, outputs, slist) {
1320 if (!outputs_walk->active) {
1321 DLOG("Output %s inactive, skipping...\n", outputs_walk->name);
1324 if (outputs_walk->bar == XCB_NONE) {
1325 /* Oh shit, an active output without an own bar. Create it now! */
1328 /* First things first: clear the backbuffer */
1329 uint32_t color = colors.bar_bg;
1330 xcb_change_gc(xcb_connection,
1331 outputs_walk->bargc,
1334 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font.height + 6 };
1335 xcb_poly_fill_rectangle(xcb_connection,
1336 outputs_walk->buffer,
1337 outputs_walk->bargc,
1341 if (statusline != NULL) {
1342 DLOG("Printing statusline!\n");
1344 /* Luckily we already prepared a seperate pixmap containing the rendered
1345 * statusline, we just have to copy the relevant parts to the relevant
1347 trayclient *trayclient;
1349 TAILQ_FOREACH(trayclient, outputs_walk->trayclients, tailq) {
1350 if (!trayclient->mapped)
1352 /* We assume the tray icons are quadratic (we use the font
1353 * *height* as *width* of the icons) because we configured them
1355 traypx += font.height + 2;
1357 /* Add 2px of padding if there are any tray icons */
1360 xcb_copy_area(xcb_connection,
1362 outputs_walk->buffer,
1363 outputs_walk->bargc,
1364 MAX(0, (int16_t)(statusline_width - outputs_walk->rect.w + 4)), 0,
1365 MAX(0, (int16_t)(outputs_walk->rect.w - statusline_width - traypx - 4)), 3,
1366 MIN(outputs_walk->rect.w - traypx - 4, statusline_width), font.height);
1369 if (config.disable_ws) {
1374 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
1375 DLOG("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
1376 uint32_t fg_color = colors.inactive_ws_fg;
1377 uint32_t bg_color = colors.inactive_ws_bg;
1378 uint32_t border_color = colors.inactive_ws_border;
1379 if (ws_walk->visible) {
1380 if (!ws_walk->focused) {
1381 fg_color = colors.active_ws_fg;
1382 bg_color = colors.active_ws_bg;
1383 border_color = colors.active_ws_border;
1385 fg_color = colors.focus_ws_fg;
1386 bg_color = colors.focus_ws_bg;
1387 border_color = colors.focus_ws_border;
1390 if (ws_walk->urgent) {
1391 DLOG("WS %s is urgent!\n", ws_walk->name);
1392 fg_color = colors.urgent_ws_fg;
1393 bg_color = colors.urgent_ws_bg;
1394 border_color = colors.urgent_ws_border;
1395 /* The urgent-hint should get noticed, so we unhide the bars shortly */
1398 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
1399 uint32_t vals_border[] = { border_color, border_color };
1400 xcb_change_gc(xcb_connection,
1401 outputs_walk->bargc,
1404 xcb_rectangle_t rect_border = { i + 1, 0, ws_walk->name_width + 10, font.height + 4 };
1405 xcb_poly_fill_rectangle(xcb_connection,
1406 outputs_walk->buffer,
1407 outputs_walk->bargc,
1410 uint32_t vals[] = { bg_color, bg_color };
1411 xcb_change_gc(xcb_connection,
1412 outputs_walk->bargc,
1415 xcb_rectangle_t rect = { i + 2, 1, ws_walk->name_width + 8, font.height + 2 };
1416 xcb_poly_fill_rectangle(xcb_connection,
1417 outputs_walk->buffer,
1418 outputs_walk->bargc,
1421 set_font_colors(outputs_walk->bargc, fg_color, bg_color);
1422 draw_text((char*)ws_walk->ucs2_name, ws_walk->name_glyphs, true,
1423 outputs_walk->buffer, outputs_walk->bargc, i + 6, 2, ws_walk->name_width);
1424 i += 12 + ws_walk->name_width;
1434 * Redraw the bars, i.e. simply copy the buffer to the barwindow
1437 void redraw_bars() {
1438 i3_output *outputs_walk;
1439 SLIST_FOREACH(outputs_walk, outputs, slist) {
1440 if (!outputs_walk->active) {
1443 xcb_copy_area(xcb_connection,
1444 outputs_walk->buffer,
1446 outputs_walk->bargc,
1449 outputs_walk->rect.w,
1450 outputs_walk->rect.h);
1451 xcb_flush(xcb_connection);